2 * passprompt.c - pppd plugin to invoke an external PAP password prompter
4 * Copyright 1999 Paul Mackerras, Alan Curry.
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.
15 #include <sys/param.h>
24 #include <pppd/pppd.h>
25 #include <pppd/upap.h>
27 #include <pppd/options.h>
29 char pppd_version[] = PPPD_VERSION;
31 static char promptprog[PATH_MAX+1];
32 static int promptprog_refused = 0;
34 static struct option options[] = {
35 { "promptprog", o_string, promptprog,
36 "External PAP password prompting program",
37 OPT_STATIC, NULL, PATH_MAX },
41 static int promptpass(char *user, char *passwd)
45 int readgood, wstat, ret;
48 if (promptprog_refused || promptprog[0] == 0 || access(promptprog, X_OK) < 0)
49 return -1; /* sorry, can't help */
55 warn("Can't make a pipe for %s", promptprog);
58 if ((kid = fork()) == (pid_t) -1) {
59 warn("Can't fork to run %s", promptprog);
65 /* we are the child, exec the program */
66 char *argv[5], fdstr[32];
70 ret = seteuid(getuid());
72 warn("Couldn't set effective user id");
74 ret = setegid(getgid());
76 warn("Couldn't set effective user id");
78 sprintf(fdstr, "%d", p[1]);
80 argv[1] = strdup(user);
81 argv[2] = strdup(ppp_remote_name());
88 /* we are the parent, read the password from the pipe */
92 red = read(p[0], passwd + readgood, MAXSECRETLEN-1 - readgood);
96 if (errno == EINTR && !ppp_signaled(SIGTERM))
98 error("Can't read secret from %s: %m", promptprog);
103 } while (readgood < MAXSECRETLEN - 1);
106 /* now wait for child to exit */
107 while (waitpid(kid, &wstat, 0) < 0) {
108 if (errno != EINTR || ppp_signaled(SIGTERM)) {
109 warn("error waiting for %s: %m", promptprog);
116 passwd[readgood] = 0;
117 if (!WIFEXITED(wstat))
118 warn("%s terminated abnormally", promptprog);
119 if (WEXITSTATUS(wstat)) {
120 warn("%s exited with code %d", promptprog, WEXITSTATUS(wstat));
121 /* code when cancel was hit in the prompt prog */
122 if (WEXITSTATUS(wstat) == 128) {
123 promptprog_refused = 1;
130 void plugin_init(void)
132 ppp_add_options(options);
133 pap_passwd_hook = promptpass;
134 #ifdef PPP_WITH_EAPTLS
135 eaptls_passwd_hook = promptpass;