src/auth-shadow.c: remove comment regarding check of sp_expire etc.
[vlock.git] / src / auth-shadow.c
blobf667f113dc7d879e9a6d445f06a74b8b59f250c2
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
10 * the author.
14 /* for crypt() */
15 #define _XOPEN_SOURCE
17 #ifndef __FreeBSD__
18 /* for asprintf() */
19 #define _GNU_SOURCE
20 #endif
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <termios.h>
29 #include <sys/mman.h>
31 #include <shadow.h>
33 #include "vlock.h"
35 int auth(const char *user, const struct timespec *timeout) {
36 char *pwd;
37 char *cryptpw;
38 char *msg = NULL;
39 struct spwd *spw;
40 int result = 0;
42 /* format the prompt */
43 (void) asprintf(&msg, "%s's Password: ", user);
45 if ((pwd = prompt_echo_off(msg, timeout)) == NULL)
46 goto out;
48 /* get the shadow password */
49 if ((spw = getspnam(user)) == NULL)
50 goto out_shadow;
52 /* hash the password */
53 if ((cryptpw = crypt(pwd, spw->sp_pwdp)) == NULL) {
54 perror("vlock-auth: crypt()");
55 goto out_shadow;
58 result = (strcmp(cryptpw, spw->sp_pwdp) == 0);
60 if (!result) {
61 sleep(1);
62 fprintf(stderr, "vlock-auth: Authentication error\n");
65 out_shadow:
66 /* deallocate shadow resources */
67 endspent();
69 out:
70 /* free the prompt */
71 free(msg);
73 /* free the password */
74 free(pwd);
76 return result;