1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * passprompt.c - pppd plugin to invoke an external PAP password prompter
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Copyright 1999 Paul Mackerras, Alan Curry.
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  *  This program is free software; you can redistribute it and/or
7*0Sstevel@tonic-gate  *  modify it under the terms of the GNU General Public License
8*0Sstevel@tonic-gate  *  as published by the Free Software Foundation; either version
9*0Sstevel@tonic-gate  *  2 of the License, or (at your option) any later version.
10*0Sstevel@tonic-gate  */
11*0Sstevel@tonic-gate #include <errno.h>
12*0Sstevel@tonic-gate #include <unistd.h>
13*0Sstevel@tonic-gate #include <fcntl.h>
14*0Sstevel@tonic-gate #include <sys/wait.h>
15*0Sstevel@tonic-gate #include <syslog.h>
16*0Sstevel@tonic-gate #include "pppd.h"
17*0Sstevel@tonic-gate 
18*0Sstevel@tonic-gate static char promptprog[PATH_MAX+1];
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate static option_t options[] = {
21*0Sstevel@tonic-gate     { "promptprog", o_string, promptprog,
22*0Sstevel@tonic-gate       "External PAP password prompting program",
23*0Sstevel@tonic-gate       OPT_STATIC, NULL, PATH_MAX },
24*0Sstevel@tonic-gate     { NULL }
25*0Sstevel@tonic-gate };
26*0Sstevel@tonic-gate 
promptpass(char * user,char * passwd)27*0Sstevel@tonic-gate static int promptpass(char *user, char *passwd)
28*0Sstevel@tonic-gate {
29*0Sstevel@tonic-gate     int p[2];
30*0Sstevel@tonic-gate     pid_t kid;
31*0Sstevel@tonic-gate     int readgood, wstat;
32*0Sstevel@tonic-gate     int red;
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate     if (promptprog[0] == 0 || access(promptprog, X_OK) < 0)
35*0Sstevel@tonic-gate 	return -1;	/* sorry, can't help */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate     /* This occurs when we're probed for the ability to supply a password */
38*0Sstevel@tonic-gate     if (user != NULL && passwd == NULL)
39*0Sstevel@tonic-gate 	return 1;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate     if (pipe(p)) {
42*0Sstevel@tonic-gate 	warn("Can't make a pipe for %s", promptprog);
43*0Sstevel@tonic-gate 	return 0;
44*0Sstevel@tonic-gate     }
45*0Sstevel@tonic-gate     if ((kid = fork()) == (pid_t) -1) {
46*0Sstevel@tonic-gate 	warn("Can't fork to run %s", promptprog);
47*0Sstevel@tonic-gate 	(void) close(p[0]);
48*0Sstevel@tonic-gate 	(void) close(p[1]);
49*0Sstevel@tonic-gate 	return 0;
50*0Sstevel@tonic-gate     }
51*0Sstevel@tonic-gate     if (kid == (pid_t)0) {
52*0Sstevel@tonic-gate 	/* we are the child, exec the program */
53*0Sstevel@tonic-gate 	char *argv[5], fdstr[32];
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	sys_close();
56*0Sstevel@tonic-gate 	closelog();
57*0Sstevel@tonic-gate 	if (detached && p[1] <= 2) {
58*0Sstevel@tonic-gate 	    (void) dup2(p[1], 3);
59*0Sstevel@tonic-gate 	    p[1] = 3;
60*0Sstevel@tonic-gate 	}
61*0Sstevel@tonic-gate 	(void) close(p[0]);
62*0Sstevel@tonic-gate 	if (detached) {
63*0Sstevel@tonic-gate 	    red = open("/etc/ppp/prompt-errors", O_WRONLY | O_APPEND | O_CREAT,
64*0Sstevel@tonic-gate 		0600);
65*0Sstevel@tonic-gate 	    (void) dup2(red, 1);
66*0Sstevel@tonic-gate 	    (void) dup2(red, 2);
67*0Sstevel@tonic-gate 	}
68*0Sstevel@tonic-gate 	(void) seteuid(getuid());
69*0Sstevel@tonic-gate 	(void) setegid(getgid());
70*0Sstevel@tonic-gate 	argv[0] = promptprog;
71*0Sstevel@tonic-gate 	argv[1] = user == NULL ? "" : user;
72*0Sstevel@tonic-gate 	argv[2] = remote_name;
73*0Sstevel@tonic-gate 	slprintf(fdstr, sizeof (fdstr), "%d", p[1]);
74*0Sstevel@tonic-gate 	argv[3] = fdstr;
75*0Sstevel@tonic-gate 	argv[4] = NULL;
76*0Sstevel@tonic-gate 	(void) execv(*argv, argv);
77*0Sstevel@tonic-gate 	_exit(127);
78*0Sstevel@tonic-gate     }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate     /* we are the parent, read the password from the pipe */
81*0Sstevel@tonic-gate     (void) close(p[1]);
82*0Sstevel@tonic-gate     readgood = 0;
83*0Sstevel@tonic-gate     do {
84*0Sstevel@tonic-gate 	red = read(p[0], passwd + readgood, MAXSECRETLEN-1 - readgood);
85*0Sstevel@tonic-gate 	if (red == 0)
86*0Sstevel@tonic-gate 	    break;
87*0Sstevel@tonic-gate 	if (red < 0) {
88*0Sstevel@tonic-gate 	    if (errno == EINTR)
89*0Sstevel@tonic-gate 		continue;
90*0Sstevel@tonic-gate 	    error("Can't read secret from %s: %m", promptprog);
91*0Sstevel@tonic-gate 	    readgood = -1;
92*0Sstevel@tonic-gate 	    break;
93*0Sstevel@tonic-gate 	}
94*0Sstevel@tonic-gate 	readgood += red;
95*0Sstevel@tonic-gate     } while (readgood < MAXSECRETLEN - 1);
96*0Sstevel@tonic-gate     passwd[readgood] = 0;
97*0Sstevel@tonic-gate     (void) close(p[0]);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate     /* now wait for child to exit */
100*0Sstevel@tonic-gate     while (waitpid(kid, &wstat, 0) < 0) {
101*0Sstevel@tonic-gate 	if (errno != EINTR) {
102*0Sstevel@tonic-gate 	    warn("error waiting for %s: %m", promptprog);
103*0Sstevel@tonic-gate 	    break;
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate     }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate     if (readgood < 0)
108*0Sstevel@tonic-gate 	return 0;
109*0Sstevel@tonic-gate     if (readgood > 0 && passwd[--readgood] == '\n')
110*0Sstevel@tonic-gate 	passwd[readgood] = '\0';
111*0Sstevel@tonic-gate     if (!WIFEXITED(wstat))
112*0Sstevel@tonic-gate 	warn("%s terminated abnormally", promptprog);
113*0Sstevel@tonic-gate     if (WEXITSTATUS(wstat) != 0)
114*0Sstevel@tonic-gate 	warn("%s exited with code %d", promptprog, WEXITSTATUS(wstat));
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate     return 1;
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
plugin_init(void)119*0Sstevel@tonic-gate void plugin_init(void)
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate     add_options(options);
122*0Sstevel@tonic-gate     pap_passwd_hook = promptpass;
123*0Sstevel@tonic-gate }
124