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
31 extern int DEBUGLEVEL;
33 #ifdef ALLOW_CHANGE_PASSWORD
35 #define MINPASSWDLENGTH 5
38 static int findpty(char **slave)
42 extern char *ptsname();
44 static char line[12] = "/dev/ptyXX";
50 if ((master = open("/dev/ptmx", O_RDWR)) >= 1) {
53 *slave = ptsname(master);
57 dirp = OpenDir("/dev");
58 if (!dirp) return(-1);
59 while ((dpname = ReadDirName(dirp)) != NULL) {
60 if (strncmp(dpname, "pty", 3) == 0 && strlen(dpname) == 5) {
63 if ((master = open(line, O_RDWR)) >= 0) {
76 static int dochild(int master,char *slavedev, char *name, char *passwordprogram)
79 struct termios stermios;
80 struct passwd *pass = Get_Pwnam(name,True);
81 int gid = pass->pw_gid;
82 int uid = pass->pw_uid;
90 /* Start new session - gets rid of controlling terminal. */
92 DEBUG(3,("Weirdness, couldn't let go of controlling terminal\n"));
96 /* Open slave pty and acquire as new controlling terminal. */
97 if ((slave = open(slavedev, O_RDWR)) < 0) {
98 DEBUG(3,("More weirdness, could not open %s\n",
103 ioctl(slave, I_PUSH, "ptem");
104 ioctl(slave, I_PUSH, "ldterm");
106 if (ioctl(slave,TIOCSCTTY,0) <0) {
107 DEBUG(3,("Error in ioctl call for slave pty\n"));
115 /* Make slave stdin/out/err of child. */
117 if (dup2(slave, STDIN_FILENO) != STDIN_FILENO) {
118 DEBUG(3,("Could not re-direct stdin\n"));
121 if (dup2(slave, STDOUT_FILENO) != STDOUT_FILENO) {
122 DEBUG(3,("Could not re-direct stdout\n"));
125 if (dup2(slave, STDERR_FILENO) != STDERR_FILENO) {
126 DEBUG(3,("Could not re-direct stderr\n"));
129 if (slave > 2) close(slave);
131 /* Set proper terminal attributes - no echo, canonical input processing,
132 no map NL to CR/NL on output. */
134 if (tcgetattr(0, &stermios) < 0) {
135 DEBUG(3,("could not read default terminal attributes on pty\n"));
138 stermios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
139 stermios.c_lflag |= ICANON;
140 stermios.c_oflag &= ~(ONLCR);
141 if (tcsetattr(0, TCSANOW, &stermios) < 0) {
142 DEBUG(3,("could not set attributes of pty\n"));
146 /* make us completely into the right uid */
150 setresgid(gid,gid,gid);
151 setresuid(uid,uid,uid);
161 /* execl() password-change application */
162 if (execl("/bin/sh","sh","-c",passwordprogram,NULL) < 0) {
163 DEBUG(3,("Bad status returned from %s\n",passwordprogram));
169 static int expect(int master,char *expected,char *buf)
176 if (n >= BUFSIZE-1) {
180 /* allow 4 seconds for some output to appear */
181 m = read_with_timeout(master, buf+n, 1, BUFSIZE-1-n, 4000, True);
192 if (do_match(s1, s2, False))
198 static void pwd_sub(char *buf)
200 string_sub(buf,"\\n","\n");
201 string_sub(buf,"\\r","\r");
202 string_sub(buf,"\\s"," ");
203 string_sub(buf,"\\t","\t");
206 static void writestring(int fd,char *s)
215 static int talktochild(int master, char *chatsequence)
219 char *ptr=chatsequence;
225 while (next_token(&ptr,chatbuf,NULL)) {
229 if (!strequal(chatbuf,"."))
230 ok = expect(master,chatbuf,buf);
233 DEBUG(100,("chatbuf=[%s] responsebuf=[%s]\n",chatbuf,buf));
237 DEBUG(3,("response %d incorrect\n",count));
241 if (!next_token(&ptr,chatbuf,NULL)) break;
243 if (!strequal(chatbuf,"."))
244 writestring(master,chatbuf);
247 DEBUG(100,("sendbuf=[%s]\n",chatbuf));
251 if (count<1) return(False);
257 BOOL chat_with_program(char *passwordprogram,char *name,char *chatsequence)
265 /* allocate a pseudo-terminal device */
266 if ((master = findpty (&slavedev)) < 0) {
267 DEBUG(3,("Cannot Allocate pty for password change: %s",name));
271 if ((pid = fork()) < 0) {
272 DEBUG(3,("Cannot fork() child for password change: %s",name));
276 /* we now have a pty */
277 if (pid > 0){ /* This is the parent process */
278 if ((chstat = talktochild(master, chatsequence)) == False) {
279 DEBUG(3,("Child failed to change password: %s\n",name));
280 kill(pid, SIGKILL); /* be sure to end this process */
283 if ((wpid = waitpid(pid, &wstat, 0)) < 0) {
284 DEBUG(3,("The process is no longer waiting!\n\n"));
288 DEBUG(3,("We were waiting for the wrong process ID\n"));
291 if (WIFEXITED(wstat) == 0) {
292 DEBUG(3,("The process exited while we were waiting\n"));
295 if (WEXITSTATUS(wstat) != 0) {
296 DEBUG(3,("The status of the process exiting was %d\n", wstat));
303 /* make sure it doesn't freeze */
306 DEBUG(3,("Dochild for user %s (uid=%d,gid=%d)\n",name,getuid(),getgid()));
307 chstat = dochild(master, slavedev, name, passwordprogram);
309 DEBUG(3,("Password change %ssuccessful for user %s\n", (chstat?"":"un"), name));
314 BOOL chgpasswd(char *name,char *oldpass,char *newpass)
316 pstring passwordprogram;
317 pstring chatsequence;
320 DEBUG(3,("Password change for user: %s\n",name));
323 DEBUG(100,("Passwords: old=%s new=%s\n",oldpass,newpass));
326 /* Take the passed information and test it for minimum criteria */
327 /* Minimum password length */
328 if (strlen(newpass) < MINPASSWDLENGTH) /* too short, must be at least MINPASSWDLENGTH */
330 DEBUG(2,("Password Change: %s, New password is shorter than MINPASSWDLENGTH\n",name));
331 return (False); /* inform the user */
334 /* Password is same as old password */
335 if (strcmp(oldpass,newpass) == 0) /* don't allow same password */
337 DEBUG(2,("Password Change: %s, New password is same as old\n",name)); /* log the attempt */
338 return (False); /* inform the user */
341 #if (defined(PASSWD_PROGRAM) && defined(PASSWD_CHAT))
342 strcpy(passwordprogram,PASSWD_PROGRAM);
343 strcpy(chatsequence,PASSWD_CHAT);
345 strcpy(passwordprogram,lp_passwd_program());
346 strcpy(chatsequence,lp_passwd_chat());
349 if (!*chatsequence) {
350 DEBUG(2,("Null chat sequence - no password changing\n"));
354 if (!*passwordprogram) {
355 DEBUG(2,("Null password program - no password changing\n"));
359 string_sub(passwordprogram,"%u",name);
360 string_sub(passwordprogram,"%o",oldpass);
361 string_sub(passwordprogram,"%n",newpass);
363 string_sub(chatsequence,"%u",name);
364 string_sub(chatsequence,"%o",oldpass);
365 string_sub(chatsequence,"%n",newpass);
366 return(chat_with_program(passwordprogram,name,chatsequence));
370 BOOL chgpasswd(char *name,char *oldpass,char *newpass)
372 DEBUG(0,("Password changing not compiled in (user=%s)\n",name));