1 /* auth-shadow.c -- shadow authentification routine for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software which is freely distributable under the terms of the
6 * GNU General Public License version 2, included as the file COPYING in this
7 * distribution. It is NOT public domain software, and any
8 * redistribution not permitted by the GNU General Public License is
9 * expressly forbidden without prior written permission from
23 #define PWD_BUFFER_SIZE 256
25 /* Try to authenticate the user. When the user is successfully authenticated
26 * this function returns 1. When the authentication fails for whatever reason
27 * the function returns 0.
29 int auth(const char *user
) {
30 char buffer
[PWD_BUFFER_SIZE
];
36 /* lock the password buffer */
37 (void) mlock(buffer
, sizeof buffer
);
39 /* write out the prompt */
40 fprintf(stderr
, "%s's Password: ", user
); fflush(stderr
);
42 /* read the password */
43 if (fgets(buffer
, sizeof buffer
, stdin
) == NULL
)
49 pwlen
= strlen(buffer
);
51 /* strip the newline */
52 if (buffer
[pwlen
-1] == '\n')
53 buffer
[pwlen
-1] = '\0';
55 /* get the shadow password */
56 if ((spw
= getspnam(user
)) == NULL
)
59 /* hash the password */
60 if ((cryptpw
= crypt(buffer
, spw
->sp_pwdp
)) == NULL
) {
61 perror("vlock: crypt()");
65 /* XXX: sp_lstchg, sp_min, sp_inact, sp_expire should also be checked here */
67 result
= strcmp(cryptpw
, spw
->sp_pwdp
) == 0;
70 /* deallocate shadow resources */
74 /* clear the buffer */
75 memset(buffer
, 0, sizeof buffer
);
77 /* unlock the password buffer */
78 (void) munlock(buffer
, sizeof buffer
);