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>
17 #include <pppd/pppd.h>
19 char pppd_version[] = PPPD_VERSION;
21 static char promptprog[PATH_MAX+1];
22 static int promptprog_refused = 0;
24 static option_t options[] = {
25 { "promptprog", o_string, promptprog,
26 "External PAP password prompting program",
27 OPT_STATIC, NULL, PATH_MAX },
31 static int promptpass(char *user, char *passwd)
35 int readgood, wstat, ret;
38 if (promptprog_refused || promptprog[0] == 0 || access(promptprog, X_OK) < 0)
39 return -1; /* sorry, can't help */
45 warn("Can't make a pipe for %s", promptprog);
48 if ((kid = fork()) == (pid_t) -1) {
49 warn("Can't fork to run %s", promptprog);
55 /* we are the child, exec the program */
56 char *argv[5], fdstr[32];
60 ret = seteuid(getuid());
62 warn("Couldn't set effective user id");
64 ret = setegid(getgid());
66 warn("Couldn't set effective user id");
70 argv[2] = remote_name;
71 sprintf(fdstr, "%d", p[1]);
78 /* we are the parent, read the password from the pipe */
82 red = read(p[0], passwd + readgood, MAXSECRETLEN-1 - readgood);
86 if (errno == EINTR && !got_sigterm)
88 error("Can't read secret from %s: %m", promptprog);
93 } while (readgood < MAXSECRETLEN - 1);
96 /* now wait for child to exit */
97 while (waitpid(kid, &wstat, 0) < 0) {
98 if (errno != EINTR || got_sigterm) {
99 warn("error waiting for %s: %m", promptprog);
106 passwd[readgood] = 0;
107 if (!WIFEXITED(wstat))
108 warn("%s terminated abnormally", promptprog);
109 if (WEXITSTATUS(wstat)) {
110 warn("%s exited with code %d", promptprog, WEXITSTATUS(wstat));
111 /* code when cancel was hit in the prompt prog */
112 if (WEXITSTATUS(wstat) == 128) {
113 promptprog_refused = 1;
120 void plugin_init(void)
122 add_options(options);
123 pap_passwd_hook = promptpass;
124 #ifdef PPP_WITH_EAPTLS
125 eaptls_passwd_hook = promptpass;