1 /* fork a child process to exec passwd and write to its
2 * tty to change a users password. This is running as the
3 * user who is attempting to change the password.
7 * This code was copied/borrowed and stolen from various sources.
8 * The primary source was the poppasswd.c from the authors of POPMail. This software
9 * was included as a client to change passwords using the 'passwd' program
10 * on the remote machine.
12 * This routine is called by set_user_password() in password.c only if ALLOW_PASSWORD_CHANGE
13 * is defined in the compiler directives located in the Makefile.
15 * This code has been hacked by Bob Nance (nance@niehs.nih.gov) and Evan Patterson
16 * (patters2@niehs.nih.gov) at the National Institute of Environmental Health Sciences
17 * and rights to modify, distribute or incorporate this change to the CAP suite or
18 * using it for any other reason are granted, so long as this disclaimer is left intact.
22 This code was hacked considerably for inclusion in Samba, primarily
23 by Andrew.Tridgell@anu.edu.au. The biggest change was the addition
24 of the "password chat" option, which allows the easy runtime
25 specification of the expected sequence of events to change a
32 extern int DEBUGLEVEL
;
34 #ifdef ALLOW_CHANGE_PASSWORD
36 #define MINPASSWDLENGTH 5
39 static int findpty(char **slave
)
43 extern char *ptsname();
45 static char line
[12] = "/dev/ptyXX";
51 if ((master
= open("/dev/ptmx", O_RDWR
)) >= 1) {
54 *slave
= ptsname(master
);
58 dirp
= OpenDir("/dev");
59 if (!dirp
) return(-1);
60 while ((dpname
= ReadDirName(dirp
)) != NULL
) {
61 if (strncmp(dpname
, "pty", 3) == 0 && strlen(dpname
) == 5) {
64 if ((master
= open(line
, O_RDWR
)) >= 0) {
77 static int dochild(int master
,char *slavedev
, char *name
, char *passwordprogram
)
80 struct termios stermios
;
81 struct passwd
*pass
= Get_Pwnam(name
,True
);
82 int gid
= pass
->pw_gid
;
83 int uid
= pass
->pw_uid
;
91 /* Start new session - gets rid of controlling terminal. */
93 DEBUG(3,("Weirdness, couldn't let go of controlling terminal\n"));
97 /* Open slave pty and acquire as new controlling terminal. */
98 if ((slave
= open(slavedev
, O_RDWR
)) < 0) {
99 DEBUG(3,("More weirdness, could not open %s\n",
104 ioctl(slave
, I_PUSH
, "ptem");
105 ioctl(slave
, I_PUSH
, "ldterm");
107 if (ioctl(slave
,TIOCSCTTY
,0) <0) {
108 DEBUG(3,("Error in ioctl call for slave pty\n"));
116 /* Make slave stdin/out/err of child. */
118 if (dup2(slave
, STDIN_FILENO
) != STDIN_FILENO
) {
119 DEBUG(3,("Could not re-direct stdin\n"));
122 if (dup2(slave
, STDOUT_FILENO
) != STDOUT_FILENO
) {
123 DEBUG(3,("Could not re-direct stdout\n"));
126 if (dup2(slave
, STDERR_FILENO
) != STDERR_FILENO
) {
127 DEBUG(3,("Could not re-direct stderr\n"));
130 if (slave
> 2) close(slave
);
132 /* Set proper terminal attributes - no echo, canonical input processing,
133 no map NL to CR/NL on output. */
135 if (tcgetattr(0, &stermios
) < 0) {
136 DEBUG(3,("could not read default terminal attributes on pty\n"));
139 stermios
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
140 stermios
.c_lflag
|= ICANON
;
141 stermios
.c_oflag
&= ~(ONLCR
);
142 if (tcsetattr(0, TCSANOW
, &stermios
) < 0) {
143 DEBUG(3,("could not set attributes of pty\n"));
147 /* make us completely into the right uid */
151 setresgid(gid
,gid
,gid
);
152 setresuid(uid
,uid
,uid
);
162 /* execl() password-change application */
163 if (execl("/bin/sh","sh","-c",passwordprogram
,NULL
) < 0) {
164 DEBUG(3,("Bad status returned from %s\n",passwordprogram
));
170 static int expect(int master
,char *expected
,char *buf
)
177 if (n
>= BUFSIZE
-1) {
181 /* allow 4 seconds for some output to appear */
182 m
= read_with_timeout(master
, buf
+n
, 1, BUFSIZE
-1-n
, 4000, True
);
193 if (do_match(s1
, s2
, False
))
199 static void pwd_sub(char *buf
)
201 string_sub(buf
,"\\n","\n");
202 string_sub(buf
,"\\r","\r");
203 string_sub(buf
,"\\s"," ");
204 string_sub(buf
,"\\t","\t");
207 static void writestring(int fd
,char *s
)
216 static int talktochild(int master
, char *chatsequence
)
220 char *ptr
=chatsequence
;
226 while (next_token(&ptr
,chatbuf
,NULL
)) {
230 if (!strequal(chatbuf
,"."))
231 ok
= expect(master
,chatbuf
,buf
);
234 DEBUG(100,("chatbuf=[%s] responsebuf=[%s]\n",chatbuf
,buf
));
238 DEBUG(3,("response %d incorrect\n",count
));
242 if (!next_token(&ptr
,chatbuf
,NULL
)) break;
244 if (!strequal(chatbuf
,"."))
245 writestring(master
,chatbuf
);
248 DEBUG(100,("sendbuf=[%s]\n",chatbuf
));
252 if (count
<1) return(False
);
258 BOOL
chat_with_program(char *passwordprogram
,char *name
,char *chatsequence
)
266 /* allocate a pseudo-terminal device */
267 if ((master
= findpty (&slavedev
)) < 0) {
268 DEBUG(3,("Cannot Allocate pty for password change: %s",name
));
272 if ((pid
= fork()) < 0) {
273 DEBUG(3,("Cannot fork() child for password change: %s",name
));
277 /* we now have a pty */
278 if (pid
> 0){ /* This is the parent process */
279 if ((chstat
= talktochild(master
, chatsequence
)) == False
) {
280 DEBUG(3,("Child failed to change password: %s\n",name
));
281 kill(pid
, SIGKILL
); /* be sure to end this process */
284 if ((wpid
= waitpid(pid
, &wstat
, 0)) < 0) {
285 DEBUG(3,("The process is no longer waiting!\n\n"));
289 DEBUG(3,("We were waiting for the wrong process ID\n"));
292 if (WIFEXITED(wstat
) == 0) {
293 DEBUG(3,("The process exited while we were waiting\n"));
296 if (WEXITSTATUS(wstat
) != 0) {
297 DEBUG(3,("The status of the process exiting was %d\n", wstat
));
304 /* make sure it doesn't freeze */
307 DEBUG(3,("Dochild for user %s (uid=%d,gid=%d)\n",name
,getuid(),getgid()));
308 chstat
= dochild(master
, slavedev
, name
, passwordprogram
);
310 DEBUG(3,("Password change %ssuccessful for user %s\n", (chstat
?"":"un"), name
));
315 BOOL
chgpasswd(char *name
,char *oldpass
,char *newpass
)
317 pstring passwordprogram
;
318 pstring chatsequence
;
321 DEBUG(3,("Password change for user: %s\n",name
));
324 DEBUG(100,("Passwords: old=%s new=%s\n",oldpass
,newpass
));
327 /* Take the passed information and test it for minimum criteria */
328 /* Minimum password length */
329 if (strlen(newpass
) < MINPASSWDLENGTH
) /* too short, must be at least MINPASSWDLENGTH */
331 DEBUG(2,("Password Change: %s, New password is shorter than MINPASSWDLENGTH\n",name
));
332 return (False
); /* inform the user */
335 /* Password is same as old password */
336 if (strcmp(oldpass
,newpass
) == 0) /* don't allow same password */
338 DEBUG(2,("Password Change: %s, New password is same as old\n",name
)); /* log the attempt */
339 return (False
); /* inform the user */
342 #if (defined(PASSWD_PROGRAM) && defined(PASSWD_CHAT))
343 strcpy(passwordprogram
,PASSWD_PROGRAM
);
344 strcpy(chatsequence
,PASSWD_CHAT
);
346 strcpy(passwordprogram
,lp_passwd_program());
347 strcpy(chatsequence
,lp_passwd_chat());
350 if (!*chatsequence
) {
351 DEBUG(2,("Null chat sequence - no password changing\n"));
355 if (!*passwordprogram
) {
356 DEBUG(2,("Null password program - no password changing\n"));
360 string_sub(passwordprogram
,"%u",name
);
361 string_sub(passwordprogram
,"%o",oldpass
);
362 string_sub(passwordprogram
,"%n",newpass
);
364 string_sub(chatsequence
,"%u",name
);
365 string_sub(chatsequence
,"%o",oldpass
);
366 string_sub(chatsequence
,"%n",newpass
);
367 return(chat_with_program(passwordprogram
,name
,chatsequence
));
371 BOOL
chgpasswd(char *name
,char *oldpass
,char *newpass
)
373 DEBUG(0,("Password changing not compiled in (user=%s)\n",name
));