7b23f05ab8daca018cd2f4036c0330f99da17df9
[xscreensaver] / driver / passwd-helper.c
1 /* passwd-helper.c --- verifying typed passwords with external helper program
2  * written by Olaf Kirch <okir@suse.de>
3  * xscreensaver, Copyright (c) 1993-2004 Jamie Zawinski <jwz@jwz.org>
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation.  No representations are made about the suitability of this
10  * software for any purpose.  It is provided "as is" without express or 
11  * implied warranty.
12  */
13
14 /* The idea here is to be able to run xscreensaver without any setuid bits.
15  * Password verification happens through an external program that you feed
16  * your password to on stdin.  The external command is invoked with a user
17  * name argument.
18  *
19  * The external helper does whatever authentication is necessary.  Currently,
20  * SuSE uses "unix2_chkpwd", which is a variation of "unix_chkpwd" from the
21  * PAM distribution.
22  *
23  * Normally, the password helper should just authenticate the calling user
24  * (i.e. based on the caller's real uid).  This is in order to prevent
25  * brute-forcing passwords in a shadow environment.  A less restrictive
26  * approach would be to allow verifying other passwords as well, but always
27  * with a 2 second delay or so.  (Not sure what SuSE's "unix2_chkpwd"
28  * currently does.)
29  *                         -- Olaf Kirch <okir@suse.de>, 16-Dec-2003
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #ifndef NO_LOCKING  /* whole file */
37
38 #include <stdlib.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42
43 extern char *blurb(void);
44
45
46 #include <stdio.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #include <pwd.h>
50 #include <errno.h>
51
52 #include <sys/wait.h>
53
54 extern void block_sigchld (void);
55 extern void unblock_sigchld (void);
56
57 static int
58 ext_run (const char *user, const char *typed_passwd, int verbose_p)
59 {
60   int pfd[2], status;
61   pid_t pid;
62
63   if (pipe(pfd) < 0)
64     return 0;
65
66   if (verbose_p)
67     fprintf (stderr, "%s: ext_run (%s, %s)\n",
68              blurb(), PASSWD_HELPER_PROGRAM, user);
69
70   block_sigchld();
71
72   if ((pid = fork()) < 0) {
73     close(pfd[0]);
74     close(pfd[1]);
75     return 0;
76   }
77
78   if (pid == 0) {
79     close(pfd[1]);
80     if (pfd[0] != 0)
81       dup2(pfd[0], 0);
82
83     /* Helper is invoked as helper service-name [user] */
84     execlp(PASSWD_HELPER_PROGRAM, PASSWD_HELPER_PROGRAM, "xscreensaver", user, NULL);
85     if (verbose_p)
86       fprintf(stderr, "%s: %s\n", PASSWD_HELPER_PROGRAM,
87                 strerror(errno));
88     exit(1);
89   }
90
91   close(pfd[0]);
92
93   /* Write out password to helper process */
94   if (!typed_passwd)
95     typed_passwd = "";
96   write(pfd[1], typed_passwd, strlen(typed_passwd));
97   close(pfd[1]);
98
99   while (waitpid(pid, &status, 0) < 0) {
100     if (errno == EINTR)
101       continue;
102     if (verbose_p)
103       fprintf(stderr, "%s: ext_run: waitpid failed: %s\n",
104                 blurb(), strerror(errno));
105     unblock_sigchld();
106     return 0;
107   }
108
109   unblock_sigchld();
110
111   if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
112     return 0;
113   return 1;
114 }
115
116
117
118 /* This can be called at any time, and says whether the typed password
119    belongs to either the logged in user (real uid, not effective); or
120    to root.
121  */
122 int
123 ext_passwd_valid_p (const char *typed_passwd, int verbose_p)
124 {
125   struct passwd *pw;
126   int res = 0;
127
128   if ((pw = getpwuid(getuid())) != NULL)
129     res = ext_run (pw->pw_name, typed_passwd, verbose_p);
130   endpwent();
131
132   if (!res)
133     res = ext_run ("root", typed_passwd, verbose_p);
134
135   return res;
136 }
137
138
139 int 
140 ext_priv_init (int argc, char **argv, int verbose_p)
141 {
142   /* Make sure the passwd helper exists */
143   if (access(PASSWD_HELPER_PROGRAM, X_OK) < 0) {
144     fprintf(stderr,
145                 "%s: warning: %s does not exist.\n"
146                 "%s: password authentication via "
147                         "external helper will not work.\n",
148                 blurb(), PASSWD_HELPER_PROGRAM, blurb());
149     return 0;
150   }
151   return 1;
152 }
153
154 #endif /* NO_LOCKING -- whole file */