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