v_check_sys_user_password.c 2.9 KB

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