]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/passprompt.c
Cleanup in pppd/pppd.h, eliminate unecessary headers
[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 <syslog.h>
17 #include <pppd/pppd.h>
18
19 char pppd_version[] = PPPD_VERSION;
20
21 static char promptprog[PATH_MAX+1];
22 static int promptprog_refused = 0;
23
24 static option_t options[] = {
25     { "promptprog", o_string, promptprog,
26       "External PAP password prompting program",
27       OPT_STATIC, NULL, PATH_MAX },
28     { NULL }
29 };
30
31 static int promptpass(char *user, char *passwd)
32 {
33     int p[2];
34     pid_t kid;
35     int readgood, wstat, ret;
36     ssize_t red;
37
38     if (promptprog_refused || promptprog[0] == 0 || access(promptprog, X_OK) < 0)
39         return -1;      /* sorry, can't help */
40
41     if (!passwd)
42         return 1;
43
44     if (pipe(p)) {
45         warn("Can't make a pipe for %s", promptprog);
46         return 0;
47     }
48     if ((kid = fork()) == (pid_t) -1) {
49         warn("Can't fork to run %s", promptprog);
50         close(p[0]);
51         close(p[1]);
52         return 0;
53     }
54     if (!kid) {
55         /* we are the child, exec the program */
56         char *argv[5], fdstr[32];
57         sys_close();
58         closelog();
59         close(p[0]);
60         ret = seteuid(getuid());
61         if (ret != 0) {
62                 warn("Couldn't set effective user id");
63         }
64         ret = setegid(getgid());
65         if (ret != 0) {
66                 warn("Couldn't set effective user id");
67         }
68         argv[0] = promptprog;
69         argv[1] = user;
70         argv[2] = remote_name;
71         sprintf(fdstr, "%d", p[1]);
72         argv[3] = fdstr;
73         argv[4] = 0;
74         execv(*argv, argv);
75         _exit(127);
76     }
77
78     /* we are the parent, read the password from the pipe */
79     close(p[1]);
80     readgood = 0;
81     do {
82         red = read(p[0], passwd + readgood, MAXSECRETLEN-1 - readgood);
83         if (red == 0)
84             break;
85         if (red < 0) {
86             if (errno == EINTR && !got_sigterm)
87                 continue;
88             error("Can't read secret from %s: %m", promptprog);
89             readgood = -1;
90             break;
91         }
92         readgood += red;
93     } while (readgood < MAXSECRETLEN - 1);
94     close(p[0]);
95
96     /* now wait for child to exit */
97     while (waitpid(kid, &wstat, 0) < 0) {
98         if (errno != EINTR || got_sigterm) {
99             warn("error waiting for %s: %m", promptprog);
100             break;
101         }
102     }
103
104     if (readgood < 0)
105         return 0;
106     passwd[readgood] = 0;
107     if (!WIFEXITED(wstat))
108         warn("%s terminated abnormally", promptprog);
109     if (WEXITSTATUS(wstat)) {
110             warn("%s exited with code %d", promptprog, WEXITSTATUS(wstat));
111             /* code when cancel was hit in the prompt prog */
112             if (WEXITSTATUS(wstat) == 128) {
113                 promptprog_refused = 1;
114             }
115             return -1;
116     }
117     return 1;
118 }
119
120 void plugin_init(void)
121 {
122     add_options(options);
123     pap_passwd_hook = promptpass;
124 #ifdef PPP_WITH_EAPTLS
125     eaptls_passwd_hook = promptpass;
126 #endif
127 }