v-check-user-password.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /***************************************************************************/
  2. /* v_check_user_password.c */
  3. /* */
  4. /* This program compare user pasword from input with /etc/shadow */
  5. /* To compile run: */
  6. /* "gcc v-check-user-password.c -o v-check-user-password -lcrypt" */
  7. /* */
  8. /* Thanks to: bogolt, richie and burus */
  9. /* */
  10. /***************************************************************************/
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include <pwd.h>
  16. #include <shadow.h>
  17. #include <time.h>
  18. #include <string.h>
  19. int main (int argc, char** argv) {
  20. /* define ip */
  21. char *ip = "127.0.0.1";
  22. /* check argument list */
  23. if (3 > argc) {
  24. printf("Error: bad args\n",argv[0]);
  25. printf("Usage: %s user password [ip]\n",argv[0]);
  26. exit(1);
  27. };
  28. /* check ip */
  29. if (4 <= argc) {
  30. ip = (char*)malloc(strlen(argv[3]));
  31. strcpy(ip, argv[3]);
  32. }
  33. /* format current time */
  34. time_t lt = time(NULL);
  35. struct tm* ptr = localtime(&lt);
  36. char str[280];
  37. strftime(str, 100, "%Y-%m-%d %H:%M:%S ", ptr);
  38. /* open log file */
  39. FILE* pFile = fopen ("/usr/local/vesta/log/auth.log","a+");
  40. if (NULL == pFile) {
  41. printf("Error: can not open file /usr/local/vesta/log/auth.log \n");
  42. exit(12);
  43. }
  44. int len = 0;
  45. if(strlen(argv[1]) >= 100) {
  46. printf("Too long username\n");
  47. exit(1);
  48. }
  49. /* parse user argument */
  50. struct passwd* userinfo = getpwnam(argv[1]);
  51. if (NULL != userinfo) {
  52. struct spwd* passw = getspnam(userinfo->pw_name);
  53. if (NULL != passw) {
  54. char* cryptedPasswrd = (char*)crypt(argv[2], passw->sp_pwdp);
  55. if (strcmp(passw->sp_pwdp,crypt(argv[2],passw->sp_pwdp))==0) {
  56. /* concatinate time with user and ip */
  57. strcat(str, userinfo->pw_name);
  58. strcat(str, " ");
  59. strcat(str, ip);
  60. strcat(str, " successfully logged in \n");
  61. fputs (str,pFile); /* write */
  62. fclose (pFile); /* close */
  63. exit(EXIT_SUCCESS); /* exit */
  64. } else {
  65. /* concatinate time with user string */
  66. printf ("Error: password missmatch\n");
  67. strcat(str, userinfo->pw_name);
  68. strcat(str, " ");
  69. strcat(str, ip);
  70. strcat(str, " failed to login \n");
  71. fputs (str,pFile); /* write */
  72. fclose (pFile); /* close */
  73. exit(9); /* exit */
  74. };
  75. }
  76. } else {
  77. printf("Error: no such user\n",argv[1]);
  78. strcat(str, argv[1]);
  79. strcat(str, " ");
  80. strcat(str, ip);
  81. strcat(str, " failed to login \n");
  82. fputs (str,pFile); /* write */
  83. fclose (pFile); /* close */
  84. exit(3);
  85. };
  86. return EXIT_SUCCESS;
  87. };