Initial version imported to CVS
[Samba.git] / source / smbd / chgpasswd.c
blobdc0514c1ed7f8163b37d130ba1b5e4e2e8b863f9
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.
4 */
6 /*
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
26 password.
29 #include "includes.h"
30 #include "loadparm.h"
32 extern int DEBUGLEVEL;
34 #ifdef ALLOW_CHANGE_PASSWORD
36 #define MINPASSWDLENGTH 5
37 #define BUFSIZE 512
39 static int findpty(char **slave)
41 int master;
42 #ifdef SVR4
43 extern char *ptsname();
44 #else
45 static char line[12] = "/dev/ptyXX";
46 void *dirp;
47 char *dpname;
48 #endif
50 #ifdef SVR4
51 if ((master = open("/dev/ptmx", O_RDWR)) >= 1) {
52 grantpt(master);
53 unlockpt(master);
54 *slave = ptsname(master);
55 return (master);
57 #else
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) {
62 line[8] = dpname[3];
63 line[9] = dpname[4];
64 if ((master = open(line, O_RDWR)) >= 0) {
65 line[5] = 't';
66 *slave = line;
67 CloseDir(dirp);
68 return (master);
72 CloseDir(dirp);
73 #endif
74 return (-1);
77 static int dochild(int master,char *slavedev, char *name, char *passwordprogram)
79 int slave;
80 struct termios stermios;
81 struct passwd *pass = Get_Pwnam(name,True);
82 int gid = pass->pw_gid;
83 int uid = pass->pw_uid;
85 #ifdef USE_SETRES
86 setresuid(0,0,0);
87 #else
88 setuid(0);
89 #endif
91 /* Start new session - gets rid of controlling terminal. */
92 if (setsid() < 0) {
93 DEBUG(3,("Weirdness, couldn't let go of controlling terminal\n"));
94 return(False);
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",
100 slavedev));
101 return(False);
103 #ifdef SVR4
104 ioctl(slave, I_PUSH, "ptem");
105 ioctl(slave, I_PUSH, "ldterm");
106 #else
107 if (ioctl(slave,TIOCSCTTY,0) <0) {
108 DEBUG(3,("Error in ioctl call for slave pty\n"));
109 /* return(False); */
111 #endif
113 /* Close master. */
114 close(master);
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"));
120 return(False);
122 if (dup2(slave, STDOUT_FILENO) != STDOUT_FILENO) {
123 DEBUG(3,("Could not re-direct stdout\n"));
124 return(False);
126 if (dup2(slave, STDERR_FILENO) != STDERR_FILENO) {
127 DEBUG(3,("Could not re-direct stderr\n"));
128 return(False);
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"));
137 return(False);
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"));
144 return(False);
147 /* make us completely into the right uid */
148 #ifdef USE_SETRES
149 setresgid(0,0,0);
150 setresuid(0,0,0);
151 setresgid(gid,gid,gid);
152 setresuid(uid,uid,uid);
153 #else
154 setuid(0);
155 seteuid(0);
156 setgid(gid);
157 setegid(gid);
158 setuid(uid);
159 seteuid(uid);
160 #endif
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));
165 return(False);
167 return(True);
170 static int expect(int master,char *expected,char *buf)
172 int n, m;
174 n = 0;
175 buf[0] = 0;
176 while (1) {
177 if (n >= BUFSIZE-1) {
178 return False;
181 /* allow 4 seconds for some output to appear */
182 m = read_with_timeout(master, buf+n, 1, BUFSIZE-1-n, 4000, True);
183 if (m < 0)
184 return False;
186 n += m;
187 buf[n] = 0;
190 pstring s1,s2;
191 strcpy(s1,buf);
192 strcpy(s2,expected);
193 if (do_match(s1, s2, False))
194 return(True);
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)
209 int l;
211 l = strlen (s);
212 write (fd, s, l);
216 static int talktochild(int master, char *chatsequence)
218 char buf[BUFSIZE];
219 int count=0;
220 char *ptr=chatsequence;
221 fstring chatbuf;
223 *buf = 0;
224 sleep(1);
226 while (next_token(&ptr,chatbuf,NULL)) {
227 BOOL ok=True;
228 count++;
229 pwd_sub(chatbuf);
230 if (!strequal(chatbuf,"."))
231 ok = expect(master,chatbuf,buf);
233 #if DEBUG_PASSWORD
234 DEBUG(100,("chatbuf=[%s] responsebuf=[%s]\n",chatbuf,buf));
235 #endif
237 if (!ok) {
238 DEBUG(3,("response %d incorrect\n",count));
239 return(False);
242 if (!next_token(&ptr,chatbuf,NULL)) break;
243 pwd_sub(chatbuf);
244 if (!strequal(chatbuf,"."))
245 writestring(master,chatbuf);
247 #if DEBUG_PASSWORD
248 DEBUG(100,("sendbuf=[%s]\n",chatbuf));
249 #endif
252 if (count<1) return(False);
254 return (True);
258 BOOL chat_with_program(char *passwordprogram,char *name,char *chatsequence)
260 char *slavedev;
261 int master;
262 pid_t pid, wpid;
263 int wstat;
264 BOOL chstat;
266 /* allocate a pseudo-terminal device */
267 if ((master = findpty (&slavedev)) < 0) {
268 DEBUG(3,("Cannot Allocate pty for password change: %s",name));
269 return(False);
272 if ((pid = fork()) < 0) {
273 DEBUG(3,("Cannot fork() child for password change: %s",name));
274 return(False);
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 */
282 return(False);
284 if ((wpid = waitpid(pid, &wstat, 0)) < 0) {
285 DEBUG(3,("The process is no longer waiting!\n\n"));
286 return(False);
288 if (pid != wpid) {
289 DEBUG(3,("We were waiting for the wrong process ID\n"));
290 return(False);
292 if (WIFEXITED(wstat) == 0) {
293 DEBUG(3,("The process exited while we were waiting\n"));
294 return(False);
296 if (WEXITSTATUS(wstat) != 0) {
297 DEBUG(3,("The status of the process exiting was %d\n", wstat));
298 return(False);
301 } else {
302 /* CHILD */
304 /* make sure it doesn't freeze */
305 alarm(20);
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));
311 return (chstat);
315 BOOL chgpasswd(char *name,char *oldpass,char *newpass)
317 pstring passwordprogram;
318 pstring chatsequence;
320 strlower(name);
321 DEBUG(3,("Password change for user: %s\n",name));
323 #if DEBUG_PASSWORD
324 DEBUG(100,("Passwords: old=%s new=%s\n",oldpass,newpass));
325 #endif
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);
345 #else
346 strcpy(passwordprogram,lp_passwd_program());
347 strcpy(chatsequence,lp_passwd_chat());
348 #endif
350 if (!*chatsequence) {
351 DEBUG(2,("Null chat sequence - no password changing\n"));
352 return(False);
355 if (!*passwordprogram) {
356 DEBUG(2,("Null password program - no password changing\n"));
357 return(False);
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));
370 #else
371 BOOL chgpasswd(char *name,char *oldpass,char *newpass)
373 DEBUG(0,("Password changing not compiled in (user=%s)\n",name));
374 return(False);
376 #endif