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.
18 static char promptprog
[PATH_MAX
+1];
20 static option_t options
[] = {
21 { "promptprog", o_string
, promptprog
,
22 "External PAP password prompting program",
23 OPT_STATIC
, NULL
, PATH_MAX
},
27 static int promptpass(char *user
, char *passwd
)
34 if (promptprog
[0] == 0 || access(promptprog
, X_OK
) < 0)
35 return -1; /* sorry, can't help */
37 /* This occurs when we're probed for the ability to supply a password */
38 if (user
!= NULL
&& passwd
== NULL
)
42 warn("Can't make a pipe for %s", promptprog
);
45 if ((kid
= fork()) == (pid_t
) -1) {
46 warn("Can't fork to run %s", promptprog
);
51 if (kid
== (pid_t
)0) {
52 /* we are the child, exec the program */
53 char *argv
[5], fdstr
[32];
57 if (detached
&& p
[1] <= 2) {
63 red
= open("/etc/ppp/prompt-errors", O_WRONLY
| O_APPEND
| O_CREAT
,
68 (void) seteuid(getuid());
69 (void) setegid(getgid());
71 argv
[1] = user
== NULL
? "" : user
;
72 argv
[2] = remote_name
;
73 slprintf(fdstr
, sizeof (fdstr
), "%d", p
[1]);
76 (void) execv(*argv
, argv
);
80 /* we are the parent, read the password from the pipe */
84 red
= read(p
[0], passwd
+ readgood
, MAXSECRETLEN
-1 - readgood
);
90 error("Can't read secret from %s: %m", promptprog
);
95 } while (readgood
< MAXSECRETLEN
- 1);
99 /* now wait for child to exit */
100 while (waitpid(kid
, &wstat
, 0) < 0) {
101 if (errno
!= EINTR
) {
102 warn("error waiting for %s: %m", promptprog
);
109 if (readgood
> 0 && passwd
[--readgood
] == '\n')
110 passwd
[readgood
] = '\0';
111 if (!WIFEXITED(wstat
))
112 warn("%s terminated abnormally", promptprog
);
113 if (WEXITSTATUS(wstat
) != 0)
114 warn("%s exited with code %d", promptprog
, WEXITSTATUS(wstat
));
119 void plugin_init(void)
121 add_options(options
);
122 pap_passwd_hook
= promptpass
;