src/vlock.sh: fall back to old option parsing if gnu getopt is not available
[vlock.git] / src / auth-shadow.c
blobb1d058bbe625ac53b55bae3d9e50f6effc39f1e7
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
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <string.h>
20 #include <sys/mman.h>
22 #include <shadow.h>
24 #define PWD_BUFFER_SIZE 256
26 int auth(const char *user) {
27 char buffer[PWD_BUFFER_SIZE];
28 size_t pwlen;
29 char *cryptpw;
30 struct spwd *spw;
31 int result = 0;
33 /* lock the password buffer */
34 (void) mlock(buffer, sizeof buffer);
36 /* write out the prompt */
37 fprintf(stderr, "%s's Password: ", user); fflush(stderr);
39 /* read the password, echo was switched of by vlock-current */
40 if (fgets(buffer, sizeof buffer, stdin) == NULL)
41 goto out;
43 /* put newline */
44 fputc('\n', stderr);
46 pwlen = strlen(buffer);
48 /* strip the newline */
49 if (buffer[pwlen-1] == '\n')
50 buffer[pwlen-1] = '\0';
52 /* get the shadow password */
53 if ((spw = getspnam(user)) == NULL)
54 goto out_shadow;
56 /* hash the password */
57 if ((cryptpw = crypt(buffer, spw->sp_pwdp)) == NULL) {
58 perror("vlock-auth: crypt()");
59 goto out_shadow;
62 /* XXX: sp_lstchg, sp_min, sp_inact, sp_expire should also be checked here */
64 result = strcmp(cryptpw, spw->sp_pwdp) == 0;
66 out_shadow:
67 /* deallocate shadow resources */
68 endspent();
70 out:
71 /* clear the buffer */
72 memset(buffer, 0, sizeof buffer);
74 /* unlock the password buffer */
75 (void) munlock(buffer, sizeof buffer);
77 return result;