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>
18 #include <pppd/pppd.h>
20 char pppd_version[] = PPPD_VERSION;
22 static char promptprog[PATH_MAX+1];
23 static int promptprog_refused = 0;
25 static option_t options[] = {
26 { "promptprog", o_string, promptprog,
27 "External PAP password prompting program",
28 OPT_STATIC, NULL, PATH_MAX },
32 static int promptpass(char *user, char *passwd)
36 int readgood, wstat, ret;
39 if (promptprog_refused || promptprog[0] == 0 || access(promptprog, X_OK) < 0)
40 return -1; /* sorry, can't help */
46 warn("Can't make a pipe for %s", promptprog);
49 if ((kid = fork()) == (pid_t) -1) {
50 warn("Can't fork to run %s", promptprog);
56 /* we are the child, exec the program */
57 char *argv[5], fdstr[32];
61 ret = seteuid(getuid());
63 warn("Couldn't set effective user id");
65 ret = setegid(getgid());
67 warn("Couldn't set effective user id");
71 argv[2] = remote_name;
72 sprintf(fdstr, "%d", p[1]);
79 /* we are the parent, read the password from the pipe */
83 red = read(p[0], passwd + readgood, MAXSECRETLEN-1 - readgood);
87 if (errno == EINTR && !got_sigterm)
89 error("Can't read secret from %s: %m", promptprog);
94 } while (readgood < MAXSECRETLEN - 1);
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);
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;
121 void plugin_init(void)
123 add_options(options);
124 pap_passwd_hook = promptpass;
125 #ifdef PPP_WITH_EAPTLS
126 eaptls_passwd_hook = promptpass;