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();
50 if ((master = open("/dev/ptmx", O_RDWR)) >= 1) {
53 *slave = ptsname(master);
57 strcpy( line, "/dev/ptyXX" );
59 dirp = OpenDir(-1, "/dev", True);
60 if (!dirp) return(-1);
61 while ((dpname = ReadDirName(dirp)) != NULL) {
62 if (strncmp(dpname, "pty", 3) == 0 && strlen(dpname) == 5) {
63 DEBUG(3,("pty: try to open %s, line was %s\n", dpname, line ) );
66 if ((master = open(line, O_RDWR)) >= 0) {
67 DEBUG(3,("pty: opened %s\n", line ) );
80 static int dochild(int master,char *slavedev, char *name, char *passwordprogram)
83 struct termios stermios;
84 struct passwd *pass = Get_Pwnam(name,True);
85 int gid = pass->pw_gid;
86 int uid = pass->pw_uid;
94 /* Start new session - gets rid of controlling terminal. */
96 DEBUG(3,("Weirdness, couldn't let go of controlling terminal\n"));
100 /* Open slave pty and acquire as new controlling terminal. */
101 if ((slave = open(slavedev, O_RDWR)) < 0) {
102 DEBUG(3,("More weirdness, could not open %s\n",
107 ioctl(slave, I_PUSH, "ptem");
108 ioctl(slave, I_PUSH, "ldterm");
110 if (ioctl(slave,TIOCSCTTY,0) <0) {
111 DEBUG(3,("Error in ioctl call for slave pty\n"));
119 /* Make slave stdin/out/err of child. */
121 if (dup2(slave, STDIN_FILENO) != STDIN_FILENO) {
122 DEBUG(3,("Could not re-direct stdin\n"));
125 if (dup2(slave, STDOUT_FILENO) != STDOUT_FILENO) {
126 DEBUG(3,("Could not re-direct stdout\n"));
129 if (dup2(slave, STDERR_FILENO) != STDERR_FILENO) {
130 DEBUG(3,("Could not re-direct stderr\n"));
133 if (slave > 2) close(slave);
135 /* Set proper terminal attributes - no echo, canonical input processing,
136 no map NL to CR/NL on output. */
138 if (tcgetattr(0, &stermios) < 0) {
139 DEBUG(3,("could not read default terminal attributes on pty\n"));
142 stermios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
143 stermios.c_lflag |= ICANON;
144 stermios.c_oflag &= ~(ONLCR);
145 if (tcsetattr(0, TCSANOW, &stermios) < 0) {
146 DEBUG(3,("could not set attributes of pty\n"));
150 /* make us completely into the right uid */
154 setresgid(gid,gid,gid);
155 setresuid(uid,uid,uid);
165 /* execl() password-change application */
166 if (execl("/bin/sh","sh","-c",passwordprogram,NULL) < 0) {
167 DEBUG(3,("Bad status returned from %s\n",passwordprogram));
173 static int expect(int master,char *expected,char *buf)
180 if (n >= BUFSIZE-1) {
184 /* allow 4 seconds for some output to appear */
185 m = read_with_timeout(master, buf+n, 1, BUFSIZE-1-n, 4000);
196 if (do_match(s1, s2, False))
202 static void pwd_sub(char *buf)
204 string_sub(buf,"\\n","\n");
205 string_sub(buf,"\\r","\r");
206 string_sub(buf,"\\s"," ");
207 string_sub(buf,"\\t","\t");
210 static void writestring(int fd,char *s)
219 static int talktochild(int master, char *chatsequence)
223 char *ptr=chatsequence;
229 while (next_token(&ptr,chatbuf,NULL)) {
233 if (!strequal(chatbuf,"."))
234 ok = expect(master,chatbuf,buf);
237 DEBUG(100,("chatbuf=[%s] responsebuf=[%s]\n",chatbuf,buf));
241 DEBUG(3,("response %d incorrect\n",count));
245 if (!next_token(&ptr,chatbuf,NULL)) break;
247 if (!strequal(chatbuf,"."))
248 writestring(master,chatbuf);
251 DEBUG(100,("sendbuf=[%s]\n",chatbuf));
255 if (count<1) return(False);
261 BOOL chat_with_program(char *passwordprogram,char *name,char *chatsequence)
269 /* allocate a pseudo-terminal device */
270 if ((master = findpty (&slavedev)) < 0) {
271 DEBUG(3,("Cannot Allocate pty for password change: %s",name));
275 if ((pid = fork()) < 0) {
276 DEBUG(3,("Cannot fork() child for password change: %s",name));
280 /* we now have a pty */
281 if (pid > 0){ /* This is the parent process */
282 if ((chstat = talktochild(master, chatsequence)) == False) {
283 DEBUG(3,("Child failed to change password: %s\n",name));
284 kill(pid, SIGKILL); /* be sure to end this process */
287 if ((wpid = sys_waitpid(pid, &wstat, 0)) < 0) {
288 DEBUG(3,("The process is no longer waiting!\n\n"));
292 DEBUG(3,("We were waiting for the wrong process ID\n"));
295 if (WIFEXITED(wstat) == 0) {
296 DEBUG(3,("The process exited while we were waiting\n"));
299 if (WEXITSTATUS(wstat) != 0) {
300 DEBUG(3,("The status of the process exiting was %d\n", wstat));
307 /* make sure it doesn't freeze */
310 DEBUG(3,("Dochild for user %s (uid=%d,gid=%d)\n",name,getuid(),getgid()));
311 chstat = dochild(master, slavedev, name, passwordprogram);
313 DEBUG(3,("Password change %ssuccessful for user %s\n", (chstat?"":"un"), name));
318 BOOL chgpasswd(char *name,char *oldpass,char *newpass)
320 pstring passwordprogram;
321 pstring chatsequence;
324 DEBUG(3,("Password change for user: %s\n",name));
327 DEBUG(100,("Passwords: old=%s new=%s\n",oldpass,newpass));
330 /* Take the passed information and test it for minimum criteria */
331 /* Minimum password length */
332 if (strlen(newpass) < MINPASSWDLENGTH) /* too short, must be at least MINPASSWDLENGTH */
334 DEBUG(2,("Password Change: %s, New password is shorter than MINPASSWDLENGTH\n",name));
335 return (False); /* inform the user */
338 /* Password is same as old password */
339 if (strcmp(oldpass,newpass) == 0) /* don't allow same password */
341 DEBUG(2,("Password Change: %s, New password is same as old\n",name)); /* log the attempt */
342 return (False); /* inform the user */
345 #if (defined(PASSWD_PROGRAM) && defined(PASSWD_CHAT))
346 strcpy(passwordprogram,PASSWD_PROGRAM);
347 strcpy(chatsequence,PASSWD_CHAT);
349 strcpy(passwordprogram,lp_passwd_program());
350 strcpy(chatsequence,lp_passwd_chat());
353 if (!*chatsequence) {
354 DEBUG(2,("Null chat sequence - no password changing\n"));
358 if (!*passwordprogram) {
359 DEBUG(2,("Null password program - no password changing\n"));
363 string_sub(passwordprogram,"%u",name);
364 string_sub(passwordprogram,"%o",oldpass);
365 string_sub(passwordprogram,"%n",newpass);
367 string_sub(chatsequence,"%u",name);
368 string_sub(chatsequence,"%o",oldpass);
369 string_sub(chatsequence,"%n",newpass);
370 return(chat_with_program(passwordprogram,name,chatsequence));
374 BOOL chgpasswd(char *name,char *oldpass,char *newpass)
376 DEBUG(0,("Password changing not compiled in (user=%s)\n",name));