v-check-user-password.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 %s \n", argv[0]);
  42. exit(12);
  43. }
  44. /* parse user argument */
  45. struct passwd* userinfo = getpwnam(argv[1]);
  46. if (NULL != userinfo) {
  47. struct spwd* passw = getspnam(userinfo->pw_name);
  48. if (NULL != passw) {
  49. char* cryptedPasswrd = (char*)crypt(argv[2], passw->sp_pwdp);
  50. if (strcmp(passw->sp_pwdp,crypt(argv[2],passw->sp_pwdp))==0) {
  51. /* concatinate time with user and ip */
  52. strcat(str, userinfo->pw_name);
  53. strcat(str, " ");
  54. strcat(str, ip);
  55. strcat(str, " successfully logged in \n");
  56. fputs (str,pFile); /* write */
  57. fclose (pFile); /* close */
  58. exit(EXIT_SUCCESS); /* exit */
  59. } else {
  60. /* concatinate time with user string */
  61. printf ("Error: password missmatch\n");
  62. strcat(str, userinfo->pw_name);
  63. strcat(str, " ");
  64. strcat(str, ip);
  65. strcat(str, " failed to login \n");
  66. fputs (str,pFile); /* write */
  67. fclose (pFile); /* close */
  68. exit(9); /* exit */
  69. };
  70. }
  71. } else {
  72. printf("Error: no such user\n",argv[1]);
  73. strcat(str, argv[1]);
  74. strcat(str, " ");
  75. strcat(str, ip);
  76. strcat(str, " failed to login \n");
  77. fputs (str,pFile); /* write */
  78. fclose (pFile); /* close */
  79. exit(3);
  80. };
  81. return EXIT_SUCCESS;
  82. };