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