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