yppasswdd_server.c revision 30377
1/*
2 * Copyright (c) 1995, 1996
3 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34static const char rcsid[] =
35	"$Id$";
36#endif /* not lint */
37
38#include <stdio.h>
39#include <string.h>
40#include <ctype.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <dirent.h>
44#include <sys/stat.h>
45#include <sys/socket.h>
46#include <netinet/in.h>
47#include <arpa/inet.h>
48#include <limits.h>
49#include <db.h>
50#include <pwd.h>
51#include <errno.h>
52#include <signal.h>
53#include <rpc/rpc.h>
54#include <rpcsvc/yp.h>
55#include <sys/types.h>
56#include <sys/wait.h>
57#include <sys/param.h>
58#include <sys/fcntl.h>
59struct dom_binding {};
60#include <rpcsvc/ypclnt.h>
61#include "yppasswdd_extern.h"
62#include "yppasswd.h"
63#include "yppasswd_private.h"
64
65char *tempname;
66
67void reaper(sig)
68	int sig;
69{
70	extern pid_t pid;
71	extern int pstat;
72	int st;
73
74	if (sig > 0) {
75		if (sig == SIGCHLD)
76			while(wait3(&st, WNOHANG, NULL) > 0) ;
77	} else {
78		pid = waitpid(pid, &pstat, 0);
79	}
80	return;
81}
82
83void install_reaper(on)
84	int on;
85{
86	if (on) {
87		signal(SIGCHLD, reaper);
88	} else {
89		signal(SIGCHLD, SIG_DFL);
90	}
91	return;
92}
93
94static struct passwd yp_password;
95
96static void copy_yp_pass(p, x, m)
97char *p;
98int x, m;
99{
100	register char *t, *s = p;
101	static char *buf;
102
103	yp_password.pw_fields = 0;
104
105	buf = (char *)realloc(buf, m + 10);
106	bzero(buf, m + 10);
107
108	/* Turn all colons into NULLs */
109	while (strchr(s, ':')) {
110		s = (strchr(s, ':') + 1);
111		*(s - 1)= '\0';
112	}
113
114	t = buf;
115#define EXPAND(e)       e = t; while ((*t++ = *p++));
116        EXPAND(yp_password.pw_name);
117	yp_password.pw_fields |= _PWF_NAME;
118        EXPAND(yp_password.pw_passwd);
119	yp_password.pw_fields |= _PWF_PASSWD;
120	yp_password.pw_uid = atoi(p);
121        p += (strlen(p) + 1);
122	yp_password.pw_fields |= _PWF_UID;
123	yp_password.pw_gid = atoi(p);
124        p += (strlen(p) + 1);
125	yp_password.pw_fields |= _PWF_GID;
126	if (x) {
127		EXPAND(yp_password.pw_class);
128		yp_password.pw_fields |= _PWF_CLASS;
129		yp_password.pw_change = atol(p);
130		p += (strlen(p) + 1);
131		yp_password.pw_fields |= _PWF_CHANGE;
132		yp_password.pw_expire = atol(p);
133		p += (strlen(p) + 1);
134		yp_password.pw_fields |= _PWF_EXPIRE;
135	}
136        EXPAND(yp_password.pw_gecos);
137	yp_password.pw_fields |= _PWF_GECOS;
138        EXPAND(yp_password.pw_dir);
139	yp_password.pw_fields |= _PWF_DIR;
140        EXPAND(yp_password.pw_shell);
141	yp_password.pw_fields |= _PWF_SHELL;
142
143	return;
144}
145
146static int validchars(arg)
147	char *arg;
148{
149	int i;
150
151	for (i = 0; i < strlen(arg); i++) {
152		if (iscntrl(arg[i])) {
153			yp_error("string contains a control character");
154			return(1);
155		}
156		if (arg[i] == ':') {
157			yp_error("string contains a colon");
158			return(1);
159		}
160		/* Be evil: truncate strings with \n in them silently. */
161		if (arg[i] == '\n') {
162			arg[i] = '\0';
163			return(0);
164		}
165	}
166	return(0);
167}
168
169static int validate_master(opw, npw)
170	struct passwd *opw;
171	struct x_master_passwd *npw;
172{
173
174	if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') {
175		yp_error("client tried to modify an NIS entry");
176		return(1);
177	}
178
179	if (validchars(npw->pw_shell)) {
180		yp_error("specified shell contains invalid characters");
181		return(1);
182	}
183
184	if (validchars(npw->pw_gecos)) {
185		yp_error("specified gecos field contains invalid characters");
186		return(1);
187	}
188
189	if (validchars(npw->pw_passwd)) {
190		yp_error("specified password contains invalid characters");
191		return(1);
192	}
193	return(0);
194}
195
196static int validate(opw, npw)
197	struct passwd *opw;
198	struct x_passwd *npw;
199{
200
201	if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') {
202		yp_error("client tried to modify an NIS entry");
203		return(1);
204	}
205
206	if (npw->pw_uid != opw->pw_uid) {
207		yp_error("UID mismatch: client says user %s has UID %d",
208			 npw->pw_name, npw->pw_uid);
209		yp_error("database says user %s has UID %d", opw->pw_name,
210			 opw->pw_uid);
211		return(1);
212	}
213
214	if (npw->pw_gid != opw->pw_gid) {
215		yp_error("GID mismatch: client says user %s has GID %d",
216			 npw->pw_name, npw->pw_gid);
217		yp_error("database says user %s has GID %d", opw->pw_name,
218			 opw->pw_gid);
219		return(1);
220	}
221
222	/*
223	 * Don't allow the user to shoot himself in the foot,
224	 * even on purpose.
225	 */
226	if (!ok_shell(npw->pw_shell)) {
227		yp_error("%s is not a valid shell", npw->pw_shell);
228		return(1);
229	}
230
231	if (validchars(npw->pw_shell)) {
232		yp_error("specified shell contains invalid characters");
233		return(1);
234	}
235
236	if (validchars(npw->pw_gecos)) {
237		yp_error("specified gecos field contains invalid characters");
238		return(1);
239	}
240
241	if (validchars(npw->pw_passwd)) {
242		yp_error("specified password contains invalid characters");
243		return(1);
244	}
245	return(0);
246}
247
248/*
249 * Kludge alert:
250 * In order to have one rpc.yppasswdd support multiple domains,
251 * we have to cheat: we search each directory under /var/yp
252 * and try to match the user in each master.passwd.byname
253 * map that we find. If the user matches (username, uid and gid
254 * all agree), then we use that domain. If we match the user in
255 * more than one database, we must abort.
256 */
257static char *find_domain(pw)
258	struct x_passwd *pw;
259{
260	struct stat statbuf;
261	struct dirent *dirp;
262	DIR *dird;
263	char yp_mapdir[MAXPATHLEN + 2];
264	static char domain[YPMAXDOMAIN];
265	char *tmp = NULL;
266	DBT key, data;
267	int hit = 0;
268
269	yp_error("performing multidomain lookup");
270
271	if ((dird = opendir(yp_dir)) == NULL) {
272		yp_error("opendir(%s) failed: %s", yp_dir, strerror(errno));
273		return(NULL);
274	}
275
276	while ((dirp = readdir(dird)) != NULL) {
277		snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s",
278							yp_dir, dirp->d_name);
279		if (stat(yp_mapdir, &statbuf) < 0) {
280			yp_error("stat(%s) failed: %s", yp_mapdir,
281							strerror(errno));
282			closedir(dird);
283			return(NULL);
284		}
285		if (S_ISDIR(statbuf.st_mode)) {
286			tmp = (char *)dirp->d_name;
287			key.data = pw->pw_name;
288			key.size = strlen(pw->pw_name);
289
290			if (yp_get_record(tmp,"master.passwd.byname",
291			  		&key, &data, 0) != YP_TRUE) {
292				continue;
293			}
294			*(char *)(data.data + data.size) = '\0';
295			copy_yp_pass(data.data, 1, data.size);
296			if (yp_password.pw_uid == pw->pw_uid &&
297			    yp_password.pw_gid == pw->pw_gid) {
298				hit++;
299				snprintf(domain, YPMAXDOMAIN, "%s", tmp);
300			}
301		}
302	}
303
304	closedir(dird);
305	if (hit > 1) {
306		yp_error("found same user in two different domains");
307		return(NULL);
308	} else
309		return((char *)&domain);
310}
311
312static int update_inplace(pw, domain)
313	struct passwd *pw;
314	char *domain;
315{
316	DB *dbp = NULL;
317	DBT key = { NULL, 0 };
318	DBT data = { NULL, 0 };
319	char pwbuf[YPMAXRECORD];
320	char keybuf[20];
321	int i;
322	char *maps[] = { "master.passwd.byname", "master.passwd.byuid",
323			 "passwd.byname", "passwd.byuid" };
324
325	char *formats[] = { "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
326			    "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
327			    "%s:%s:%d:%d:%s:%s:%s", "%s:%s:%d:%d:%s:%s:%s" };
328	char *ptr = NULL;
329	char *yp_last = "YP_LAST_MODIFIED";
330	char yplastbuf[YPMAXRECORD];
331
332	snprintf(yplastbuf, sizeof(yplastbuf), "%lu", time(NULL));
333
334	for (i = 0; i < 4; i++) {
335
336		if (i % 2) {
337			snprintf(keybuf, sizeof(keybuf), "%ld", pw->pw_uid);
338			key.data = (char *)&keybuf;
339			key.size = strlen(keybuf);
340		} else {
341			key.data = pw->pw_name;
342			key.size = strlen(pw->pw_name);
343		}
344
345		/*
346		 * XXX The passwd.byname and passwd.byuid maps come in
347		 * two flavors: secure and insecure. The secure version
348		 * has a '*' in the password field whereas the insecure one
349		 * has a real crypted password. The maps will be insecure
350		 * if they were built with 'unsecure = TRUE' enabled in
351		 * /var/yp/Makefile, but we'd have no way of knowing if
352		 * this has been done unless we were to try parsing the
353		 * Makefile, which is a disgusting thought. Instead, we
354		 * read the records from the maps, skip to the first ':'
355		 * in them, and then look at the character immediately
356		 * following it. If it's an '*' then the map is 'secure'
357		 * and we must not insert a real password into the pw_passwd
358		 * field. If it's not an '*', then we put the real crypted
359		 * password in.
360		 */
361		if (yp_get_record(domain,maps[i],&key,&data,1) != YP_TRUE) {
362			yp_error("couldn't read %s/%s: %s", domain,
363						maps[i], strerror(errno));
364			return(1);
365		}
366
367		if ((ptr = strchr(data.data, ':')) == NULL) {
368			yp_error("no colon in passwd record?!");
369			return(1);
370		}
371
372		/*
373		 * XXX Supposing we have more than one user with the same
374		 * UID? (Or more than one user with the same name?) We could
375		 * end up modifying the wrong record if were not careful.
376		 */
377		if (i % 2) {
378			if (strncmp(data.data, pw->pw_name,
379							strlen(pw->pw_name))) {
380				yp_error("warning: found entry for UID %d \
381in map %s@%s with wrong name (%.*s)", pw->pw_uid, maps[i], domain,
382					ptr - (char *)data.data, data.data);
383				yp_error("there may be more than one user \
384with the same UID - continuing");
385				continue;
386			}
387		} else {
388			/*
389			 * We're really being ultra-paranoid here.
390			 * This is generally a 'can't happen' condition.
391			 */
392			snprintf(pwbuf, sizeof(pwbuf), ":%d:%d:", pw->pw_uid,
393								  pw->pw_gid);
394			if (!strstr(data.data, pwbuf)) {
395				yp_error("warning: found entry for user %s \
396in map %s@%s with wrong UID", pw->pw_name, maps[i], domain);
397				yp_error("there may ne more than one user
398with the same name - continuing");
399				continue;
400			}
401		}
402
403		if (i < 2) {
404			snprintf(pwbuf, sizeof(pwbuf), formats[i],
405			   pw->pw_name, pw->pw_passwd, pw->pw_uid,
406			   pw->pw_gid, pw->pw_class, pw->pw_change,
407			   pw->pw_expire, pw->pw_gecos, pw->pw_dir,
408			   pw->pw_shell);
409		} else {
410			snprintf(pwbuf, sizeof(pwbuf), formats[i],
411			   pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd,
412			   pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir,
413			   pw->pw_shell);
414		}
415
416#define FLAGS O_RDWR|O_CREAT
417
418		if ((dbp = yp_open_db_rw(domain, maps[i], FLAGS)) == NULL) {
419			yp_error("couldn't open %s/%s r/w: %s",domain,
420						maps[i],strerror(errno));
421			return(1);
422		}
423
424		data.data = pwbuf;
425		data.size = strlen(pwbuf);
426
427		if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
428			yp_error("failed to update record in %s/%s", domain,
429								maps[i]);
430			(void)(dbp->close)(dbp);
431			return(1);
432		}
433
434		key.data = yp_last;
435		key.size = strlen(yp_last);
436		data.data = (char *)&yplastbuf;
437		data.size = strlen(yplastbuf);
438
439		if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
440			yp_error("failed to update timestamp in %s/%s", domain,
441								maps[i]);
442			(void)(dbp->close)(dbp);
443			return(1);
444		}
445
446		(void)(dbp->close)(dbp);
447	}
448
449	return(0);
450}
451
452static char *yp_mktmpnam()
453{
454	static char path[MAXPATHLEN];
455	char *p;
456
457	sprintf(path,"%s",passfile);
458	if ((p = strrchr(path, '/')))
459		++p;
460	else
461		p = path;
462	strcpy(p, "yppwtmp.XXXXXX");
463	return(mktemp(path));
464}
465
466int *
467yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp)
468{
469	static int  result;
470	struct sockaddr_in *rqhost;
471	DBT key, data;
472	int rval = 0;
473	int pfd, tfd;
474	int pid;
475	int passwd_changed = 0;
476	int shell_changed = 0;
477	int gecos_changed = 0;
478	char *oldshell = NULL;
479	char *oldgecos = NULL;
480	char *passfile_hold;
481	char passfile_buf[MAXPATHLEN + 2];
482	char *domain = yppasswd_domain;
483	static struct sockaddr_in clntaddr;
484	static struct timeval t_saved, t_test;
485
486	/*
487	 * Normal user updates always use the 'default' master.passwd file.
488	 */
489
490	passfile = passfile_default;
491	result = 1;
492
493	rqhost = svc_getcaller(rqstp->rq_xprt);
494
495	gettimeofday(&t_test, NULL);
496	if (!bcmp((char *)rqhost, (char *)&clntaddr,
497						sizeof(struct sockaddr_in)) &&
498		t_test.tv_sec > t_saved.tv_sec &&
499		t_test.tv_sec - t_saved.tv_sec < 300) {
500
501		bzero((char *)&clntaddr, sizeof(struct sockaddr_in));
502		bzero((char *)&t_saved, sizeof(struct timeval));
503		return(NULL);
504	}
505
506	bcopy((char *)rqhost, (char *)&clntaddr, sizeof(struct sockaddr_in));
507	gettimeofday(&t_saved, NULL);
508
509	if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) {
510		yp_error("rejected update request from unauthorized host");
511		svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
512		return(&result);
513	}
514
515	/*
516	 * Step one: find the user. (It's kinda pointless to
517	 * proceed if the user doesn't exist.) We look for the
518	 * user in the master.passwd.byname database, _NOT_ by
519	 * using getpwent() and friends! We can't use getpwent()
520	 * since the NIS master server is not guaranteed to be
521	 * configured as an NIS client.
522	 */
523
524	if (multidomain) {
525		if ((domain = find_domain(&argp->newpw)) == NULL) {
526			yp_error("multidomain lookup failed - aborting update");
527			return(&result);
528		} else
529			yp_error("updating user %s in domain %s",
530					argp->newpw.pw_name, domain);
531	}
532
533	key.data = argp->newpw.pw_name;
534	key.size = strlen(argp->newpw.pw_name);
535
536	if ((rval=yp_get_record(domain,"master.passwd.byname",
537		  	&key, &data, 0)) != YP_TRUE) {
538		if (rval == YP_NOKEY) {
539			yp_error("user %s not found in passwd database",
540			 	argp->newpw.pw_name);
541		} else {
542			yp_error("database access error: %s",
543			 	yperr_string(rval));
544		}
545		return(&result);
546	}
547
548	/* Nul terminate, please. */
549	*(char *)(data.data + data.size) = '\0';
550
551	copy_yp_pass(data.data, 1, data.size);
552
553	/* Step 2: check that the supplied oldpass is valid. */
554
555	if (strcmp(crypt(argp->oldpass, yp_password.pw_passwd),
556					yp_password.pw_passwd)) {
557		yp_error("rejected change attempt -- bad password");
558		yp_error("client address: %s username: %s",
559			  inet_ntoa(rqhost->sin_addr),
560			  argp->newpw.pw_name);
561		return(&result);
562	}
563
564	/* Step 3: validate the arguments passed to us by the client. */
565
566	if (validate(&yp_password, &argp->newpw)) {
567		yp_error("rejecting change attempt: bad arguments");
568		yp_error("client address: %s username: %s",
569			 inet_ntoa(rqhost->sin_addr),
570			 argp->newpw.pw_name);
571		svcerr_decode(rqstp->rq_xprt);
572		return(&result);
573	}
574
575	/* Step 4: update the user's passwd structure. */
576
577	if (!no_chsh && strcmp(argp->newpw.pw_shell, yp_password.pw_shell)) {
578		oldshell = yp_password.pw_shell;
579		yp_password.pw_shell = argp->newpw.pw_shell;
580		shell_changed++;
581	}
582
583
584	if (!no_chfn && strcmp(argp->newpw.pw_gecos, yp_password.pw_gecos)) {
585		oldgecos = yp_password.pw_gecos;
586		yp_password.pw_gecos = argp->newpw.pw_gecos;
587		gecos_changed++;
588	}
589
590	if (strcmp(argp->newpw.pw_passwd, yp_password.pw_passwd)) {
591		yp_password.pw_passwd = argp->newpw.pw_passwd;
592		yp_password.pw_change = 0;
593		passwd_changed++;
594	}
595
596	/*
597	 * If the caller specified a domain other than our 'default'
598	 * domain, change the path to master.passwd accordingly.
599	 */
600
601	if (strcmp(domain, yppasswd_domain)) {
602		snprintf(passfile_buf, sizeof(passfile_buf),
603			"%s/%s/master.passwd", yp_dir, domain);
604		passfile = (char *)&passfile_buf;
605	}
606
607	/* Step 5: make a new password file with the updated info. */
608
609	if ((pfd = pw_lock()) < 0) {
610		return (&result);
611	}
612	if ((tfd = pw_tmp()) < 0) {
613		return (&result);
614	}
615
616	if (pw_copy(pfd, tfd, &yp_password)) {
617		yp_error("failed to created updated password file -- \
618cleaning up and bailing out");
619		unlink(tempname);
620		return(&result);
621	}
622
623	passfile_hold = yp_mktmpnam();
624	rename(passfile, passfile_hold);
625	if (strcmp(passfile, _PATH_MASTERPASSWD)) {
626		rename(tempname, passfile);
627	} else {
628		if (pw_mkdb(argp->newpw.pw_name) < 0) {
629			yp_error("pwd_mkdb failed");
630			return(&result);
631		}
632	}
633
634	if (inplace) {
635		if ((rval = update_inplace(&yp_password, domain))) {
636			yp_error("inplace update failed -- rebuilding maps");
637		}
638	}
639
640	switch((pid = fork())) {
641	case 0:
642		if (inplace && !rval) {
643    			execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
644				yppasswd_domain, "pushpw", NULL);
645		} else {
646    			execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
647				yppasswd_domain, NULL);
648		}
649    		yp_error("couldn't exec map update process: %s",
650					strerror(errno));
651		unlink(passfile);
652		rename(passfile_hold, passfile);
653    		exit(1);
654		break;
655	case -1:
656		yp_error("fork() failed: %s", strerror(errno));
657		unlink(passfile);
658		rename(passfile_hold, passfile);
659		return(&result);
660		break;
661	default:
662		unlink(passfile_hold);
663		break;
664	}
665
666	if (verbose) {
667		yp_error("update completed for user %s (uid %d):",
668						argp->newpw.pw_name,
669						argp->newpw.pw_uid);
670
671		if (passwd_changed)
672			yp_error("password changed");
673
674		if (gecos_changed)
675			yp_error("gecos changed ('%s' -> '%s')",
676					oldgecos, argp->newpw.pw_gecos);
677
678		if (shell_changed)
679			yp_error("shell changed ('%s' -> '%s')",
680					oldshell, argp->newpw.pw_shell);
681	}
682
683	result = 0;
684	return (&result);
685}
686
687struct cmessage {
688	struct cmsghdr		cmsg;
689	struct cmsgcred		cmcred;
690};
691
692/*
693 * Note that this function performs a little less sanity checking
694 * than the last one. Since only the superuser is allowed to use it,
695 * it is assumed that the caller knows what he's doing.
696 */
697int *yppasswdproc_update_master_1_svc(master_yppasswd *argp,
698					struct svc_req *rqstp)
699{
700	static int result;
701	int pfd, tfd;
702	int pid;
703	int rval = 0;
704	DBT key, data;
705	char *passfile_hold;
706	char passfile_buf[MAXPATHLEN + 2];
707	struct sockaddr_in *rqhost;
708	struct cmessage			*cm;
709	SVCXPRT				*transp;
710
711	result = 1;
712
713	/*
714	 * NO AF_INET CONNETCIONS ALLOWED!
715	 */
716	rqhost = svc_getcaller(rqstp->rq_xprt);
717	if (rqhost->sin_family != AF_UNIX) {
718		yp_error("Alert! %s/%d attempted to use superuser-only \
719procedure!\n", inet_ntoa(rqhost->sin_addr), rqhost->sin_port);
720		svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
721		return(&result);
722	}
723
724	transp = rqstp->rq_xprt;
725
726	if (transp->xp_verf.oa_length < sizeof(struct cmessage) ||
727		transp->xp_verf.oa_base == NULL ||
728		transp->xp_verf.oa_flavor != AUTH_UNIX) {
729		yp_error("caller didn't send proper credentials");
730		svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
731		return(&result);
732	}
733
734	cm = (struct cmessage *)transp->xp_verf.oa_base;
735	if (cm->cmsg.cmsg_type != SCM_CREDS) {
736		yp_error("caller didn't send proper credentials");
737		svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
738		return(&result);
739	}
740
741 	if (cm->cmcred.cmcred_euid) {
742		yp_error("caller euid is %d, expecting 0 -- rejecting request",
743				cm->cmcred.cmcred_euid);
744		svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
745		return(&result);
746	}
747
748	passfile = passfile_default;
749
750	key.data = argp->newpw.pw_name;
751	key.size = strlen(argp->newpw.pw_name);
752
753	/*
754	 * The superuser may add entries to the passwd maps if
755	 * rpc.yppasswdd is started with the -a flag. Paranoia
756	 * prevents me from allowing additions by default.
757	 */
758	if ((rval = yp_get_record(argp->domain, "master.passwd.byname",
759			  &key, &data, 0)) != YP_TRUE) {
760		if (rval == YP_NOKEY) {
761			yp_error("user %s not found in passwd database",
762				 argp->newpw.pw_name);
763			if (allow_additions)
764				yp_error("notice: adding user %s to \
765master.passwd database for domain %s", argp->newpw.pw_name, argp->domain);
766			else
767				yp_error("restart rpc.yppasswdd with the -a flag to \
768allow additions to be made to the password database");
769		} else {
770			yp_error("database access error: %s",
771				 yperr_string(rval));
772		}
773		if (!allow_additions)
774			return(&result);
775	} else {
776
777		/* Nul terminate, please. */
778		*(char *)(data.data + data.size) = '\0';
779
780		copy_yp_pass(data.data, 1, data.size);
781	}
782
783	/*
784	 * Perform a small bit of sanity checking.
785	 */
786	if (validate_master(rval == YP_TRUE ? &yp_password:NULL,&argp->newpw)){
787		yp_error("rejecting update attempt for %s: bad arguments",
788			 argp->newpw.pw_name);
789		return(&result);
790	}
791
792	/*
793	 * If the caller specified a domain other than our 'default'
794	 * domain, change the path to master.passwd accordingly.
795	 */
796
797	if (strcmp(argp->domain, yppasswd_domain)) {
798		snprintf(passfile_buf, sizeof(passfile_buf),
799			"%s/%s/master.passwd", yp_dir, argp->domain);
800		passfile = (char *)&passfile_buf;
801	}
802
803	if ((pfd = pw_lock()) < 0) {
804		return (&result);
805	}
806	if ((tfd = pw_tmp()) < 0) {
807		return (&result);
808	}
809
810	if (pw_copy(pfd, tfd, (struct passwd  *)&argp->newpw)) {
811		yp_error("failed to created updated password file -- \
812cleaning up and bailing out");
813		unlink(tempname);
814		return(&result);
815	}
816
817	passfile_hold = yp_mktmpnam();
818	rename(passfile, passfile_hold);
819	if (strcmp(passfile, _PATH_MASTERPASSWD)) {
820		rename(tempname, passfile);
821	} else {
822		if (pw_mkdb(argp->newpw.pw_name) < 0) {
823			yp_error("pwd_mkdb failed");
824			return(&result);
825		}
826	}
827
828	if (inplace) {
829		if ((rval = update_inplace((struct passwd *)&argp->newpw,
830							argp->domain))) {
831			yp_error("inplace update failed -- rebuilding maps");
832		}
833	}
834
835	switch((pid = fork())) {
836	case 0:
837		if (inplace && !rval) {
838    			execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
839				argp->domain, "pushpw", NULL);
840    		} else {
841			execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
842				argp->domain, NULL);
843		}
844    		yp_error("couldn't exec map update process: %s",
845					strerror(errno));
846		unlink(passfile);
847		rename(passfile_hold, passfile);
848    		exit(1);
849		break;
850	case -1:
851		yp_error("fork() failed: %s", strerror(errno));
852		unlink(passfile);
853		rename(passfile_hold, passfile);
854		return(&result);
855		break;
856	default:
857		unlink(passfile_hold);
858		break;
859	}
860
861	yp_error("performed update of user %s (uid %d) domain %s",
862						argp->newpw.pw_name,
863						argp->newpw.pw_uid,
864						argp->domain);
865
866	result = 0;
867	return(&result);
868}
869