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