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