src/vlock.sh: remove debugging statement
[vlock.git] / src / auth-shadow.c
blob0ba1430946d9c941a9d45c3ebf96002338d56525
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 <stdbool.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include <sys/mman.h>
30 #include <shadow.h>
32 #include "auth.h"
33 #include "prompt.h"
35 bool auth(const char *user, struct timespec *timeout)
37 char *pwd;
38 char *cryptpw;
39 char *msg;
40 struct spwd *spw;
41 int result = false;
43 /* format the prompt */
44 if (asprintf(&msg, "%s's Password: ", user) < 0)
45 return false;
47 if ((pwd = prompt_echo_off(msg, timeout)) == NULL)
48 goto out_pwd;
50 /* get the shadow password */
51 if ((spw = getspnam(user)) == NULL)
52 goto out_shadow;
54 /* hash the password */
55 if ((cryptpw = crypt(pwd, spw->sp_pwdp)) == NULL) {
56 perror("vlock-auth: crypt()");
57 goto out_shadow;
60 result = (strcmp(cryptpw, spw->sp_pwdp) == 0);
62 if (!result) {
63 sleep(1);
64 fprintf(stderr, "vlock-auth: Authentication error\n");
67 out_shadow:
68 /* deallocate shadow resources */
69 endspent();
71 /* free the password */
72 free(pwd);
74 out_pwd:
75 /* free the prompt */
76 free(msg);
78 return result;