2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1997
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* fork a child process to exec passwd and write to its
23 * tty to change a users password. This is running as the
24 * user who is attempting to change the password.
28 * This code was copied/borrowed and stolen from various sources.
29 * The primary source was the poppasswd.c from the authors of POPMail. This software
30 * was included as a client to change passwords using the 'passwd' program
31 * on the remote machine.
33 * This routine is called by set_user_password() in password.c only if ALLOW_PASSWORD_CHANGE
34 * is defined in the compiler directives located in the Makefile.
36 * This code has been hacked by Bob Nance (nance@niehs.nih.gov) and Evan Patterson
37 * (patters2@niehs.nih.gov) at the National Institute of Environmental Health Sciences
38 * and rights to modify, distribute or incorporate this change to the CAP suite or
39 * using it for any other reason are granted, so long as this disclaimer is left intact.
43 This code was hacked considerably for inclusion in Samba, primarily
44 by Andrew.Tridgell@anu.edu.au. The biggest change was the addition
45 of the "password chat" option, which allows the easy runtime
46 specification of the expected sequence of events to change a
52 extern int DEBUGLEVEL
;
54 #ifdef ALLOW_CHANGE_PASSWORD
56 #define MINPASSWDLENGTH 5
59 static int findpty(char **slave
)
62 #if defined(SVR4) || defined(SUNOS5)
63 extern char *ptsname();
64 #else /* defined(SVR4) || defined(SUNOS5) */
68 #endif /* defined(SVR4) || defined(SUNOS5) */
70 #if defined(SVR4) || defined(SUNOS5)
71 if ((master
= open("/dev/ptmx", O_RDWR
)) >= 1) {
74 *slave
= ptsname(master
);
77 #else /* defined(SVR4) || defined(SUNOS5) */
78 strcpy( line
, "/dev/ptyXX" );
80 dirp
= OpenDir(-1, "/dev", True
);
81 if (!dirp
) return(-1);
82 while ((dpname
= ReadDirName(dirp
)) != NULL
) {
83 if (strncmp(dpname
, "pty", 3) == 0 && strlen(dpname
) == 5) {
84 DEBUG(3,("pty: try to open %s, line was %s\n", dpname
, line
) );
87 if ((master
= open(line
, O_RDWR
)) >= 0) {
88 DEBUG(3,("pty: opened %s\n", line
) );
97 #endif /* defined(SVR4) || defined(SUNOS5) */
101 static int dochild(int master
,char *slavedev
, char *name
, char *passwordprogram
)
104 struct termios stermios
;
105 struct passwd
*pass
= Get_Pwnam(name
,True
);
106 int gid
= pass
->pw_gid
;
107 int uid
= pass
->pw_uid
;
111 #else /* USE_SETRES */
113 #endif /* USE_SETRES */
115 /* Start new session - gets rid of controlling terminal. */
117 DEBUG(3,("Weirdness, couldn't let go of controlling terminal\n"));
121 /* Open slave pty and acquire as new controlling terminal. */
122 if ((slave
= open(slavedev
, O_RDWR
)) < 0) {
123 DEBUG(3,("More weirdness, could not open %s\n",
127 #if defined(SVR4) || defined(SUNOS5)
128 ioctl(slave
, I_PUSH
, "ptem");
129 ioctl(slave
, I_PUSH
, "ldterm");
130 #else /* defined(SVR4) || defined(SUNOS5) */
131 if (ioctl(slave
,TIOCSCTTY
,0) <0) {
132 DEBUG(3,("Error in ioctl call for slave pty\n"));
135 #endif /* defined(SVR4) || defined(SUNOS5) */
140 /* Make slave stdin/out/err of child. */
142 if (dup2(slave
, STDIN_FILENO
) != STDIN_FILENO
) {
143 DEBUG(3,("Could not re-direct stdin\n"));
146 if (dup2(slave
, STDOUT_FILENO
) != STDOUT_FILENO
) {
147 DEBUG(3,("Could not re-direct stdout\n"));
150 if (dup2(slave
, STDERR_FILENO
) != STDERR_FILENO
) {
151 DEBUG(3,("Could not re-direct stderr\n"));
154 if (slave
> 2) close(slave
);
156 /* Set proper terminal attributes - no echo, canonical input processing,
157 no map NL to CR/NL on output. */
159 if (tcgetattr(0, &stermios
) < 0) {
160 DEBUG(3,("could not read default terminal attributes on pty\n"));
163 stermios
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
164 stermios
.c_lflag
|= ICANON
;
165 stermios
.c_oflag
&= ~(ONLCR
);
166 if (tcsetattr(0, TCSANOW
, &stermios
) < 0) {
167 DEBUG(3,("could not set attributes of pty\n"));
171 /* make us completely into the right uid */
175 setresgid(gid
,gid
,gid
);
176 setresuid(uid
,uid
,uid
);
186 /* execl() password-change application */
187 if (execl("/bin/sh","sh","-c",passwordprogram
,NULL
) < 0) {
188 DEBUG(3,("Bad status returned from %s\n",passwordprogram
));
194 static int expect(int master
,char *expected
,char *buf
)
201 if (n
>= BUFSIZE
-1) {
205 /* allow 4 seconds for some output to appear */
206 m
= read_with_timeout(master
, buf
+n
, 1, BUFSIZE
-1-n
, 4000);
217 if (do_match(s1
, s2
, False
))
223 static void pwd_sub(char *buf
)
225 string_sub(buf
,"\\n","\n");
226 string_sub(buf
,"\\r","\r");
227 string_sub(buf
,"\\s"," ");
228 string_sub(buf
,"\\t","\t");
231 static void writestring(int fd
,char *s
)
240 static int talktochild(int master
, char *chatsequence
)
244 char *ptr
=chatsequence
;
250 while (next_token(&ptr
,chatbuf
,NULL
)) {
254 if (!strequal(chatbuf
,"."))
255 ok
= expect(master
,chatbuf
,buf
);
258 DEBUG(100,("chatbuf=[%s] responsebuf=[%s]\n",chatbuf
,buf
));
262 DEBUG(3,("response %d incorrect\n",count
));
266 if (!next_token(&ptr
,chatbuf
,NULL
)) break;
268 if (!strequal(chatbuf
,"."))
269 writestring(master
,chatbuf
);
272 DEBUG(100,("sendbuf=[%s]\n",chatbuf
));
276 if (count
<1) return(False
);
282 BOOL
chat_with_program(char *passwordprogram
,char *name
,char *chatsequence
)
290 /* allocate a pseudo-terminal device */
291 if ((master
= findpty (&slavedev
)) < 0) {
292 DEBUG(3,("Cannot Allocate pty for password change: %s",name
));
296 if ((pid
= fork()) < 0) {
297 DEBUG(3,("Cannot fork() child for password change: %s",name
));
301 /* we now have a pty */
302 if (pid
> 0){ /* This is the parent process */
303 if ((chstat
= talktochild(master
, chatsequence
)) == False
) {
304 DEBUG(3,("Child failed to change password: %s\n",name
));
305 kill(pid
, SIGKILL
); /* be sure to end this process */
308 if ((wpid
= sys_waitpid(pid
, &wstat
, 0)) < 0) {
309 DEBUG(3,("The process is no longer waiting!\n\n"));
313 DEBUG(3,("We were waiting for the wrong process ID\n"));
316 if (WIFEXITED(wstat
) == 0) {
317 DEBUG(3,("The process exited while we were waiting\n"));
320 if (WEXITSTATUS(wstat
) != 0) {
321 DEBUG(3,("The status of the process exiting was %d\n", wstat
));
328 /* make sure it doesn't freeze */
331 DEBUG(3,("Dochild for user %s (uid=%d,gid=%d)\n",name
,getuid(),getgid()));
332 chstat
= dochild(master
, slavedev
, name
, passwordprogram
);
334 DEBUG(3,("Password change %ssuccessful for user %s\n", (chstat
?"":"un"), name
));
339 BOOL
chgpasswd(char *name
,char *oldpass
,char *newpass
)
341 pstring passwordprogram
;
342 pstring chatsequence
;
345 DEBUG(3,("Password change for user: %s\n",name
));
348 DEBUG(100,("Passwords: old=%s new=%s\n",oldpass
,newpass
));
351 /* Take the passed information and test it for minimum criteria */
352 /* Minimum password length */
353 if (strlen(newpass
) < MINPASSWDLENGTH
) /* too short, must be at least MINPASSWDLENGTH */
355 DEBUG(2,("Password Change: %s, New password is shorter than MINPASSWDLENGTH\n",name
));
356 return (False
); /* inform the user */
359 /* Password is same as old password */
360 if (strcmp(oldpass
,newpass
) == 0) /* don't allow same password */
362 DEBUG(2,("Password Change: %s, New password is same as old\n",name
)); /* log the attempt */
363 return (False
); /* inform the user */
366 #if (defined(PASSWD_PROGRAM) && defined(PASSWD_CHAT))
367 strcpy(passwordprogram
,PASSWD_PROGRAM
);
368 strcpy(chatsequence
,PASSWD_CHAT
);
370 strcpy(passwordprogram
,lp_passwd_program());
371 strcpy(chatsequence
,lp_passwd_chat());
374 if (!*chatsequence
) {
375 DEBUG(2,("Null chat sequence - no password changing\n"));
379 if (!*passwordprogram
) {
380 DEBUG(2,("Null password program - no password changing\n"));
384 string_sub(passwordprogram
,"%u",name
);
385 string_sub(passwordprogram
,"%o",oldpass
);
386 string_sub(passwordprogram
,"%n",newpass
);
388 string_sub(chatsequence
,"%u",name
);
389 string_sub(chatsequence
,"%o",oldpass
);
390 string_sub(chatsequence
,"%n",newpass
);
391 return(chat_with_program(passwordprogram
,name
,chatsequence
));
395 BOOL
chgpasswd(char *name
,char *oldpass
,char *newpass
)
397 DEBUG(0,("Password changing not compiled in (user=%s)\n",name
));