]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/passprompt.c
Header file reorganization and cleaning up the public API for pppd version 2.5.0...
[ppp.git] / pppd / plugins / passprompt.c
1 /*
2  * passprompt.c - pppd plugin to invoke an external PAP password prompter
3  *
4  * Copyright 1999 Paul Mackerras, Alan Curry.
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version
9  *  2 of the License, or (at your option) any later version.
10  */
11
12 #include <errno.h>
13 #include <unistd.h>
14 #include <sys/wait.h>
15 #include <sys/param.h>
16 #include <limits.h>
17 #include <stdio.h>
18 #include <syslog.h>
19 #include <stdarg.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <string.h>
23
24 #include <pppd/pppd.h>
25 #include <pppd/upap.h>
26 #include <pppd/eap.h>
27 #include <pppd/options.h>
28
29 char pppd_version[] = PPPD_VERSION;
30
31 static char promptprog[PATH_MAX+1];
32 static int promptprog_refused = 0;
33
34 static struct option options[] = {
35     { "promptprog", o_string, promptprog,
36       "External PAP password prompting program",
37       OPT_STATIC, NULL, PATH_MAX },
38     { NULL }
39 };
40
41 static int promptpass(char *user, char *passwd)
42 {
43     int p[2];
44     pid_t kid;
45     int readgood, wstat, ret;
46     ssize_t red;
47
48     if (promptprog_refused || promptprog[0] == 0 || access(promptprog, X_OK) < 0)
49         return -1;      /* sorry, can't help */
50
51     if (!passwd)
52         return 1;
53
54     if (pipe(p)) {
55         warn("Can't make a pipe for %s", promptprog);
56         return 0;
57     }
58     if ((kid = fork()) == (pid_t) -1) {
59         warn("Can't fork to run %s", promptprog);
60         close(p[0]);
61         close(p[1]);
62         return 0;
63     }
64     if (!kid) {
65         /* we are the child, exec the program */
66         char *argv[5], fdstr[32];
67         ppp_sys_close();
68         closelog();
69         close(p[0]);
70         ret = seteuid(getuid());
71         if (ret != 0) {
72                 warn("Couldn't set effective user id");
73         }
74         ret = setegid(getgid());
75         if (ret != 0) {
76                 warn("Couldn't set effective user id");
77         }
78         sprintf(fdstr, "%d", p[1]);
79         argv[0] = promptprog;
80         argv[1] = strdup(user);
81         argv[2] = strdup(ppp_remote_name());
82         argv[3] = fdstr;
83         argv[4] = 0;
84         execv(*argv, argv);
85         _exit(127);
86     }
87
88     /* we are the parent, read the password from the pipe */
89     close(p[1]);
90     readgood = 0;
91     do {
92         red = read(p[0], passwd + readgood, MAXSECRETLEN-1 - readgood);
93         if (red == 0)
94             break;
95         if (red < 0) {
96             if (errno == EINTR && !ppp_signaled(SIGTERM))
97                 continue;
98             error("Can't read secret from %s: %m", promptprog);
99             readgood = -1;
100             break;
101         }
102         readgood += red;
103     } while (readgood < MAXSECRETLEN - 1);
104     close(p[0]);
105
106     /* now wait for child to exit */
107     while (waitpid(kid, &wstat, 0) < 0) {
108         if (errno != EINTR || ppp_signaled(SIGTERM)) {
109             warn("error waiting for %s: %m", promptprog);
110             break;
111         }
112     }
113
114     if (readgood < 0)
115         return 0;
116     passwd[readgood] = 0;
117     if (!WIFEXITED(wstat))
118         warn("%s terminated abnormally", promptprog);
119     if (WEXITSTATUS(wstat)) {
120             warn("%s exited with code %d", promptprog, WEXITSTATUS(wstat));
121             /* code when cancel was hit in the prompt prog */
122             if (WEXITSTATUS(wstat) == 128) {
123                 promptprog_refused = 1;
124             }
125             return -1;
126     }
127     return 1;
128 }
129
130 void plugin_init(void)
131 {
132     ppp_add_options(options);
133     pap_passwd_hook = promptpass;
134 #ifdef PPP_WITH_EAPTLS
135     eaptls_passwd_hook = promptpass;
136 #endif
137 }