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