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