yppasswdd_server.c revision 116421
155714Skris/*
2280297Sjkim * Copyright (c) 1995, 1996
3280297Sjkim *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4280297Sjkim *
555714Skris * Redistribution and use in source and binary forms, with or without
655714Skris * modification, are permitted provided that the following conditions
755714Skris * are met:
855714Skris * 1. Redistributions of source code must retain the above copyright
955714Skris *    notice, this list of conditions and the following disclaimer.
1055714Skris * 2. Redistributions in binary form must reproduce the above copyright
1155714Skris *    notice, this list of conditions and the following disclaimer in the
1255714Skris *    documentation and/or other materials provided with the distribution.
1355714Skris * 3. All advertising materials mentioning features or use of this software
14280297Sjkim *    must display the following acknowledgement:
1555714Skris *	This product includes software developed by Bill Paul.
1655714Skris * 4. Neither the name of the author nor the names of any co-contributors
1755714Skris *    may be used to endorse or promote products derived from this software
1855714Skris *    without specific prior written permission.
1955714Skris *
2055714Skris * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2155714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2355714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2455714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2555714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2655714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2755714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2855714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2955714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3055714Skris * SUCH DAMAGE.
3155714Skris */
3255714Skris
3355714Skris#include <sys/cdefs.h>
3455714Skris__FBSDID("$FreeBSD: head/usr.sbin/rpc.yppasswdd/yppasswdd_server.c 116421 2003-06-15 21:24:45Z mbr $");
3555714Skris
3655714Skris#include <sys/param.h>
3755714Skris#include <sys/fcntl.h>
3855714Skris#include <sys/socket.h>
3955714Skris#include <sys/stat.h>
4055714Skris#include <sys/wait.h>
4155714Skris
4255714Skris#include <arpa/inet.h>
4355714Skris#include <netinet/in.h>
4455714Skris
4555714Skris#include <ctype.h>
4655714Skris#include <db.h>
4755714Skris#include <dirent.h>
4855714Skris#include <errno.h>
4955714Skris#include <limits.h>
5055714Skris#include <pwd.h>
5155714Skris#include <signal.h>
5255714Skris#include <stdio.h>
5355714Skris#include <stdlib.h>
5455714Skris#include <string.h>
5555714Skris#include <unistd.h>
5655714Skris
5755714Skris#include <libgen.h>
5855714Skris#include <libutil.h>
5955714Skris
6055714Skris#include <rpc/rpc.h>
6155714Skris#include <rpcsvc/yp.h>
6255714Skrisstruct dom_binding;
6355714Skris#include <rpcsvc/ypclnt.h>
6455714Skris#include "yppasswdd_extern.h"
6555714Skris#include "yppasswd.h"
6655714Skris#include "yppasswd_private.h"
6755714Skris#include "ypxfr_extern.h"
6855714Skris#include "yp_extern.h"
6955714Skris
7055714Skrisstatic struct passwd yp_password;
71280297Sjkim
72280297Sjkimstatic void
73280297Sjkimcopy_yp_pass(char *p, int x, int m)
74280297Sjkim{
75280297Sjkim	char *t, *s = p;
76280297Sjkim	static char *buf;
77280297Sjkim
78280297Sjkim	yp_password.pw_fields = 0;
79280297Sjkim
80280297Sjkim	buf = realloc(buf, m + 10);
81280297Sjkim	bzero(buf, m + 10);
82280297Sjkim
83280297Sjkim	/* Turn all colons into NULLs */
84280297Sjkim	while (strchr(s, ':')) {
85280297Sjkim		s = (strchr(s, ':') + 1);
86280297Sjkim		*(s - 1)= '\0';
87280297Sjkim	}
88280297Sjkim
89280297Sjkim	t = buf;
90280297Sjkim#define EXPAND(e)       e = t; while ((*t++ = *p++));
91280297Sjkim        EXPAND(yp_password.pw_name);
92280297Sjkim	yp_password.pw_fields |= _PWF_NAME;
9355714Skris        EXPAND(yp_password.pw_passwd);
94280297Sjkim	yp_password.pw_fields |= _PWF_PASSWD;
95280297Sjkim	yp_password.pw_uid = atoi(p);
96280297Sjkim        p += (strlen(p) + 1);
97280297Sjkim	yp_password.pw_fields |= _PWF_UID;
98280297Sjkim	yp_password.pw_gid = atoi(p);
99280297Sjkim        p += (strlen(p) + 1);
100280297Sjkim	yp_password.pw_fields |= _PWF_GID;
101280297Sjkim	if (x) {
102280297Sjkim		EXPAND(yp_password.pw_class);
103280297Sjkim		yp_password.pw_fields |= _PWF_CLASS;
104280297Sjkim		yp_password.pw_change = atol(p);
10555714Skris		p += (strlen(p) + 1);
106280297Sjkim		yp_password.pw_fields |= _PWF_CHANGE;
107280297Sjkim		yp_password.pw_expire = atol(p);
10855714Skris		p += (strlen(p) + 1);
109280297Sjkim		yp_password.pw_fields |= _PWF_EXPIRE;
110280297Sjkim	}
111280297Sjkim        EXPAND(yp_password.pw_gecos);
112280297Sjkim	yp_password.pw_fields |= _PWF_GECOS;
113280297Sjkim        EXPAND(yp_password.pw_dir);
11455714Skris	yp_password.pw_fields |= _PWF_DIR;
115280297Sjkim        EXPAND(yp_password.pw_shell);
116280297Sjkim	yp_password.pw_fields |= _PWF_SHELL;
117280297Sjkim
118280297Sjkim	return;
119280297Sjkim}
120280297Sjkim
121280297Sjkimstatic int
122280297Sjkimvalidchars(char *arg)
123280297Sjkim{
124280297Sjkim	size_t i;
125280297Sjkim
126280297Sjkim	for (i = 0; i < strlen(arg); i++) {
127280297Sjkim		if (iscntrl(arg[i])) {
128280297Sjkim			yp_error("string contains a control character");
12955714Skris			return(1);
130		}
131		if (arg[i] == ':') {
132			yp_error("string contains a colon");
133			return(1);
134		}
135		/* Be evil: truncate strings with \n in them silently. */
136		if (arg[i] == '\n') {
137			arg[i] = '\0';
138			return(0);
139		}
140	}
141	return(0);
142}
143
144static int
145validate_master(struct passwd *opw __unused, struct x_master_passwd *npw)
146{
147
148	if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') {
149		yp_error("client tried to modify an NIS entry");
150		return(1);
151	}
152
153	if (validchars(npw->pw_shell)) {
154		yp_error("specified shell contains invalid characters");
155		return(1);
156	}
157
158	if (validchars(npw->pw_gecos)) {
159		yp_error("specified gecos field contains invalid characters");
160		return(1);
161	}
162
163	if (validchars(npw->pw_passwd)) {
164		yp_error("specified password contains invalid characters");
165		return(1);
166	}
167	return(0);
168}
169
170static int
171validate(struct passwd *opw, struct x_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 ((uid_t)npw->pw_uid != opw->pw_uid) {
180		yp_error("UID mismatch: client says user %s has UID %d",
181			 npw->pw_name, npw->pw_uid);
182		yp_error("database says user %s has UID %d", opw->pw_name,
183			 opw->pw_uid);
184		return(1);
185	}
186
187	if ((gid_t)npw->pw_gid != opw->pw_gid) {
188		yp_error("GID mismatch: client says user %s has GID %d",
189			 npw->pw_name, npw->pw_gid);
190		yp_error("database says user %s has GID %d", opw->pw_name,
191			 opw->pw_gid);
192		return(1);
193	}
194
195	/*
196	 * Don't allow the user to shoot himself in the foot,
197	 * even on purpose.
198	 */
199	if (!ok_shell(npw->pw_shell)) {
200		yp_error("%s is not a valid shell", npw->pw_shell);
201		return(1);
202	}
203
204	if (validchars(npw->pw_shell)) {
205		yp_error("specified shell contains invalid characters");
206		return(1);
207	}
208
209	if (validchars(npw->pw_gecos)) {
210		yp_error("specified gecos field contains invalid characters");
211		return(1);
212	}
213
214	if (validchars(npw->pw_passwd)) {
215		yp_error("specified password contains invalid characters");
216		return(1);
217	}
218	return(0);
219}
220
221/*
222 * Kludge alert:
223 * In order to have one rpc.yppasswdd support multiple domains,
224 * we have to cheat: we search each directory under /var/yp
225 * and try to match the user in each master.passwd.byname
226 * map that we find. If the user matches (username, uid and gid
227 * all agree), then we use that domain. If we match the user in
228 * more than one database, we must abort.
229 */
230static char *
231find_domain(struct x_passwd *pw)
232{
233	struct stat statbuf;
234	struct dirent *dirp;
235	DIR *dird;
236	char yp_mapdir[MAXPATHLEN + 2];
237	static char domain[YPMAXDOMAIN];
238	char *tmp = NULL;
239	DBT key, data;
240	int hit = 0;
241
242	yp_error("performing multidomain lookup");
243
244	if ((dird = opendir(yp_dir)) == NULL) {
245		yp_error("opendir(%s) failed: %s", yp_dir, strerror(errno));
246		return(NULL);
247	}
248
249	while ((dirp = readdir(dird)) != NULL) {
250		snprintf(yp_mapdir, sizeof yp_mapdir, "%s/%s",
251							yp_dir, dirp->d_name);
252		if (stat(yp_mapdir, &statbuf) < 0) {
253			yp_error("stat(%s) failed: %s", yp_mapdir,
254							strerror(errno));
255			closedir(dird);
256			return(NULL);
257		}
258		if (S_ISDIR(statbuf.st_mode)) {
259			tmp = (char *)dirp->d_name;
260			key.data = pw->pw_name;
261			key.size = strlen(pw->pw_name);
262
263			if (yp_get_record(tmp,"master.passwd.byname",
264			  		&key, &data, 0) != YP_TRUE) {
265				continue;
266			}
267			*((char *)data.data + data.size) = '\0';
268			copy_yp_pass(data.data, 1, data.size);
269			if (yp_password.pw_uid == (uid_t)pw->pw_uid &&
270			    yp_password.pw_gid == (gid_t)pw->pw_gid) {
271				hit++;
272				snprintf(domain, YPMAXDOMAIN, "%s", tmp);
273			}
274		}
275	}
276
277	closedir(dird);
278	if (hit > 1) {
279		yp_error("found same user in two different domains");
280		return(NULL);
281	} else
282		return((char *)&domain);
283}
284
285static const char *maps[] = {
286	"master.passwd.byname",
287	"master.passwd.byuid",
288	"passwd.byname",
289	"passwd.byuid"
290};
291
292static const char *formats[] = {
293	"%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
294	"%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
295	"%s:%s:%d:%d:%s:%s:%s",
296	"%s:%s:%d:%d:%s:%s:%s"
297};
298
299static int
300update_inplace(struct passwd *pw, char *domain)
301{
302	DB *dbp = NULL;
303	DBT key = { NULL, 0 };
304	DBT data = { NULL, 0 };
305	char pwbuf[YPMAXRECORD];
306	char keybuf[20];
307	int i;
308	char *ptr = NULL;
309	static char yp_last[] = "YP_LAST_MODIFIED";
310	char yplastbuf[YPMAXRECORD];
311
312	snprintf(yplastbuf, sizeof yplastbuf, "%llu",
313	    (unsigned long long)time(NULL));
314
315	for (i = 0; i < 4; i++) {
316
317		if (i % 2) {
318			snprintf(keybuf, sizeof keybuf,
319			    "%llu", (unsigned long long)pw->pw_uid);
320			key.data = &keybuf;
321			key.size = strlen(keybuf);
322		} else {
323			key.data = pw->pw_name;
324			key.size = strlen(pw->pw_name);
325		}
326
327		/*
328		 * XXX The passwd.byname and passwd.byuid maps come in
329		 * two flavors: secure and insecure. The secure version
330		 * has a '*' in the password field whereas the insecure one
331		 * has a real crypted password. The maps will be insecure
332		 * if they were built with 'unsecure = TRUE' enabled in
333		 * /var/yp/Makefile, but we'd have no way of knowing if
334		 * this has been done unless we were to try parsing the
335		 * Makefile, which is a disgusting thought. Instead, we
336		 * read the records from the maps, skip to the first ':'
337		 * in them, and then look at the character immediately
338		 * following it. If it's an '*' then the map is 'secure'
339		 * and we must not insert a real password into the pw_passwd
340		 * field. If it's not an '*', then we put the real crypted
341		 * password in.
342		 */
343		if (yp_get_record(domain,maps[i],&key,&data,1) != YP_TRUE) {
344			yp_error("couldn't read %s/%s: %s", domain,
345						maps[i], strerror(errno));
346			return(1);
347		}
348
349		if ((ptr = strchr(data.data, ':')) == NULL) {
350			yp_error("no colon in passwd record?!");
351			return(1);
352		}
353
354		/*
355		 * XXX Supposing we have more than one user with the same
356		 * UID? (Or more than one user with the same name?) We could
357		 * end up modifying the wrong record if were not careful.
358		 */
359		if (i % 2) {
360			if (strncmp(data.data, pw->pw_name,
361							strlen(pw->pw_name))) {
362				yp_error("warning: found entry for UID %d \
363in map %s@%s with wrong name (%.*s)", pw->pw_uid, maps[i], domain,
364				    (int)(ptr - (char *)data.data),
365				    (char *)data.data);
366				yp_error("there may be more than one user \
367with the same UID - continuing");
368				continue;
369			}
370		} else {
371			/*
372			 * We're really being ultra-paranoid here.
373			 * This is generally a 'can't happen' condition.
374			 */
375			snprintf(pwbuf, sizeof pwbuf, ":%d:%d:", pw->pw_uid,
376								  pw->pw_gid);
377			if (!strstr(data.data, pwbuf)) {
378				yp_error("warning: found entry for user %s \
379in map %s@%s with wrong UID", pw->pw_name, maps[i], domain);
380				yp_error("there may be more than one user \
381with the same name - continuing");
382				continue;
383			}
384		}
385
386		if (i < 2) {
387			snprintf(pwbuf, sizeof pwbuf, formats[i],
388			   pw->pw_name, pw->pw_passwd, pw->pw_uid,
389			   pw->pw_gid, pw->pw_class, pw->pw_change,
390			   pw->pw_expire, pw->pw_gecos, pw->pw_dir,
391			   pw->pw_shell);
392		} else {
393			snprintf(pwbuf, sizeof pwbuf, formats[i],
394			   pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd,
395			   pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir,
396			   pw->pw_shell);
397		}
398
399#define FLAGS O_RDWR|O_CREAT
400
401		if ((dbp = yp_open_db_rw(domain, maps[i], FLAGS)) == NULL) {
402			yp_error("couldn't open %s/%s r/w: %s",domain,
403						maps[i],strerror(errno));
404			return(1);
405		}
406
407		data.data = pwbuf;
408		data.size = strlen(pwbuf);
409
410		if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
411			yp_error("failed to update record in %s/%s", domain,
412								maps[i]);
413			(void)(dbp->close)(dbp);
414			return(1);
415		}
416
417		key.data = yp_last;
418		key.size = strlen(yp_last);
419		data.data = (char *)&yplastbuf;
420		data.size = strlen(yplastbuf);
421
422		if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
423			yp_error("failed to update timestamp in %s/%s", domain,
424								maps[i]);
425			(void)(dbp->close)(dbp);
426			return(1);
427		}
428
429		(void)(dbp->close)(dbp);
430	}
431
432	return(0);
433}
434
435int *
436yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp)
437{
438	static int  result;
439	struct sockaddr_in *rqhost;
440	DBT key, data;
441	int rval = 0;
442	int pfd, tfd;
443	int pid;
444	int passwd_changed = 0;
445	int shell_changed = 0;
446	int gecos_changed = 0;
447	char *oldshell = NULL;
448	char *oldgecos = NULL;
449	char *passfile_hold;
450	char passfile_buf[MAXPATHLEN + 2];
451	char passfile_hold_buf[MAXPATHLEN + 2];
452	char *domain = yppasswd_domain;
453	static struct sockaddr_in clntaddr;
454	static struct timeval t_saved, t_test;
455
456	/*
457	 * Normal user updates always use the 'default' master.passwd file.
458	 */
459
460	passfile = passfile_default;
461	result = 1;
462
463	rqhost = svc_getcaller(rqstp->rq_xprt);
464
465	gettimeofday(&t_test, NULL);
466	if (!bcmp(rqhost, &clntaddr, sizeof *rqhost) &&
467		t_test.tv_sec > t_saved.tv_sec &&
468		t_test.tv_sec - t_saved.tv_sec < 300) {
469
470		bzero(&clntaddr, sizeof clntaddr);
471		bzero(&t_saved, sizeof t_saved);
472		return(NULL);
473	}
474
475	bcopy(rqhost, &clntaddr, sizeof clntaddr);
476	gettimeofday(&t_saved, NULL);
477
478	if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) {
479		yp_error("rejected update request from unauthorized host");
480		svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
481		return(&result);
482	}
483
484	/*
485	 * Step one: find the user. (It's kinda pointless to
486	 * proceed if the user doesn't exist.) We look for the
487	 * user in the master.passwd.byname database, _NOT_ by
488	 * using getpwent() and friends! We can't use getpwent()
489	 * since the NIS master server is not guaranteed to be
490	 * configured as an NIS client.
491	 */
492
493	if (multidomain) {
494		if ((domain = find_domain(&argp->newpw)) == NULL) {
495			yp_error("multidomain lookup failed - aborting update");
496			return(&result);
497		} else
498			yp_error("updating user %s in domain %s",
499					argp->newpw.pw_name, domain);
500	}
501
502	key.data = argp->newpw.pw_name;
503	key.size = strlen(argp->newpw.pw_name);
504
505	if ((rval = yp_get_record(domain,"master.passwd.byname",
506		  	&key, &data, 0)) != YP_TRUE) {
507		if (rval == YP_NOKEY) {
508			yp_error("user %s not found in passwd database",
509			 	argp->newpw.pw_name);
510		} else {
511			yp_error("database access error: %s",
512			 	yperr_string(rval));
513		}
514		return(&result);
515	}
516
517	/* Nul terminate, please. */
518	*((char *)data.data + data.size) = '\0';
519
520	copy_yp_pass(data.data, 1, data.size);
521
522	/* Step 2: check that the supplied oldpass is valid. */
523
524	if (strcmp(crypt(argp->oldpass, yp_password.pw_passwd),
525					yp_password.pw_passwd)) {
526		yp_error("rejected change attempt -- bad password");
527		yp_error("client address: %s username: %s",
528			  inet_ntoa(rqhost->sin_addr),
529			  argp->newpw.pw_name);
530		return(&result);
531	}
532
533	/* Step 3: validate the arguments passed to us by the client. */
534
535	if (validate(&yp_password, &argp->newpw)) {
536		yp_error("rejecting change attempt: bad arguments");
537		yp_error("client address: %s username: %s",
538			 inet_ntoa(rqhost->sin_addr),
539			 argp->newpw.pw_name);
540		svcerr_decode(rqstp->rq_xprt);
541		return(&result);
542	}
543
544	/* Step 4: update the user's passwd structure. */
545
546	if (!no_chsh && strcmp(argp->newpw.pw_shell, yp_password.pw_shell)) {
547		oldshell = yp_password.pw_shell;
548		yp_password.pw_shell = argp->newpw.pw_shell;
549		shell_changed++;
550	}
551
552
553	if (!no_chfn && strcmp(argp->newpw.pw_gecos, yp_password.pw_gecos)) {
554		oldgecos = yp_password.pw_gecos;
555		yp_password.pw_gecos = argp->newpw.pw_gecos;
556		gecos_changed++;
557	}
558
559	if (strcmp(argp->newpw.pw_passwd, yp_password.pw_passwd)) {
560		yp_password.pw_passwd = argp->newpw.pw_passwd;
561		yp_password.pw_change = 0;
562		passwd_changed++;
563	}
564
565	/*
566	 * If the caller specified a domain other than our 'default'
567	 * domain, change the path to master.passwd accordingly.
568	 */
569
570	if (strcmp(domain, yppasswd_domain)) {
571		snprintf(passfile_buf, sizeof(passfile_buf),
572			"%s/%s/master.passwd", yp_dir, domain);
573		passfile = (char *)&passfile_buf;
574	}
575
576	/*
577	 * Create a filename to hold the original master.passwd
578	 * so if our call to yppwupdate fails we can roll back
579	 */
580	snprintf(passfile_hold_buf, sizeof(passfile_hold_buf),
581	    "%s.hold", passfile);
582	passfile_hold = (char *)&passfile_hold_buf;
583
584
585	/* Step 5: make a new password file with the updated info. */
586
587	if (pw_init(dirname(passfile), passfile)) {
588		yp_error("pw_init() failed");
589		return &result;
590	}
591	if ((pfd = pw_lock()) == -1) {
592		pw_fini();
593		yp_error("pw_lock() failed");
594		return &result;
595	}
596	if ((tfd = pw_tmp(-1)) == -1) {
597		pw_fini();
598		yp_error("pw_tmp() failed");
599		return &result;
600	}
601	if (pw_copy(pfd, tfd, &yp_password, NULL) == -1) {
602		pw_fini();
603		yp_error("pw_copy() failed");
604		return &result;
605	}
606	if (rename(passfile, passfile_hold) == -1) {
607		pw_fini();
608		yp_error("rename of %s to %s failed", passfile,
609		    passfile_hold);
610		return &result;
611	}
612
613	if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) {
614		/*
615		 * NIS server is exporting the system's master.passwd.
616		 * Call pw_mkdb to rebuild passwd and the .db files
617		 */
618		if (pw_mkdb(yp_password.pw_name) == -1) {
619			pw_fini();
620			yp_error("pw_mkdb() failed");
621			rename(passfile_hold, passfile);
622			return &result;
623		}
624	} else {
625		/*
626		 * NIS server is exporting a private master.passwd.
627		 * Rename tempfile into final location
628		 */
629		if (rename(pw_tempname(), passfile) == -1) {
630			pw_fini();
631			yp_error("rename of %s to %s failed",
632			    pw_tempname(), passfile);
633			rename(passfile_hold, passfile);
634			return &result;
635		}
636	}
637
638	pw_fini();
639
640	if (inplace) {
641		if ((rval = update_inplace(&yp_password, domain))) {
642			yp_error("inplace update failed -- rebuilding maps");
643		}
644	}
645
646	switch ((pid = fork())) {
647	case 0:
648		if (inplace && !rval) {
649    			execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
650				yppasswd_domain, "pushpw", (char *)NULL);
651		} else {
652    			execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
653				yppasswd_domain, (char *)NULL);
654		}
655    		yp_error("couldn't exec map update process: %s",
656					strerror(errno));
657		unlink(passfile);
658		rename(passfile_hold, passfile);
659    		exit(1);
660		break;
661	case -1:
662		yp_error("fork() failed: %s", strerror(errno));
663		unlink(passfile);
664		rename(passfile_hold, passfile);
665		return(&result);
666		break;
667	default:
668		unlink(passfile_hold);
669		break;
670	}
671
672	if (verbose) {
673		yp_error("update completed for user %s (uid %d) in %s:",
674		    argp->newpw.pw_name, argp->newpw.pw_uid, passfile);
675
676		if (passwd_changed)
677			yp_error("password changed");
678
679		if (gecos_changed)
680			yp_error("gecos changed ('%s' -> '%s')",
681					oldgecos, argp->newpw.pw_gecos);
682
683		if (shell_changed)
684			yp_error("shell changed ('%s' -> '%s')",
685					oldshell, argp->newpw.pw_shell);
686	}
687
688	result = 0;
689	return (&result);
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 *
698yppasswdproc_update_master_1_svc(master_yppasswd *argp,
699    struct svc_req *rqstp)
700{
701	static int result;
702	int pfd, tfd;
703	int pid;
704	uid_t uid;
705	int rval = 0;
706	DBT key, data;
707	char *passfile_hold;
708	char passfile_buf[MAXPATHLEN + 2];
709	char passfile_hold_buf[MAXPATHLEN + 2];
710	struct sockaddr_in *rqhost;
711	SVCXPRT	*transp;
712
713	result = 1;
714	transp = rqstp->rq_xprt;
715
716	/*
717	 * NO AF_INET CONNETCIONS ALLOWED!
718	 */
719	rqhost = svc_getcaller(transp);
720	if (rqhost->sin_family != AF_UNIX) {
721		yp_error("Alert! %s/%d attempted to use superuser-only \
722procedure!\n", inet_ntoa(rqhost->sin_addr), rqhost->sin_port);
723		svcerr_auth(transp, AUTH_BADCRED);
724		return(&result);
725	}
726
727	if (rqstp->rq_cred.oa_flavor != AUTH_SYS) {
728		yp_error("caller didn't send proper credentials");
729		svcerr_auth(transp, AUTH_BADCRED);
730		return(&result);
731	}
732
733	if (__rpc_get_local_uid(transp, &uid) < 0) {
734		yp_error("caller didn't send proper credentials");
735		svcerr_auth(transp, AUTH_BADCRED);
736		return(&result);
737	}
738
739	if (uid) {
740		yp_error("caller euid is %d, expecting 0 -- rejecting request",
741		    uid);
742		svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
743		return(&result);
744	}
745
746	passfile = passfile_default;
747
748	key.data = argp->newpw.pw_name;
749	key.size = strlen(argp->newpw.pw_name);
750
751	/*
752	 * The superuser may add entries to the passwd maps if
753	 * rpc.yppasswdd is started with the -a flag. Paranoia
754	 * prevents me from allowing additions by default.
755	 */
756	if ((rval = yp_get_record(argp->domain, "master.passwd.byname",
757			  &key, &data, 0)) != YP_TRUE) {
758		if (rval == YP_NOKEY) {
759			yp_error("user %s not found in passwd database",
760				 argp->newpw.pw_name);
761			if (allow_additions)
762				yp_error("notice: adding user %s to \
763master.passwd database for domain %s", argp->newpw.pw_name, argp->domain);
764			else
765				yp_error("restart rpc.yppasswdd with the -a flag to \
766allow additions to be made to the password database");
767		} else {
768			yp_error("database access error: %s",
769				 yperr_string(rval));
770		}
771		if (!allow_additions)
772			return(&result);
773	} else {
774
775		/* Nul terminate, please. */
776		*((char *)data.data + data.size) = '\0';
777
778		copy_yp_pass(data.data, 1, data.size);
779	}
780
781	/*
782	 * Perform a small bit of sanity checking.
783	 */
784	if (validate_master(rval == YP_TRUE ? &yp_password:NULL,&argp->newpw)){
785		yp_error("rejecting update attempt for %s: bad arguments",
786			 argp->newpw.pw_name);
787		return(&result);
788	}
789
790	/*
791	 * If the caller specified a domain other than our 'default'
792	 * domain, change the path to master.passwd accordingly.
793	 */
794
795	if (strcmp(argp->domain, yppasswd_domain)) {
796		snprintf(passfile_buf, sizeof(passfile_buf),
797			"%s/%s/master.passwd", yp_dir, argp->domain);
798		passfile = (char *)&passfile_buf;
799	}
800
801	/*
802	 * Create a filename to hold the original master.passwd
803	 * so if our call to yppwupdate fails we can roll back
804	 */
805	snprintf(passfile_hold_buf, sizeof(passfile_hold_buf),
806	    "%s.hold", passfile);
807	passfile_hold = (char *)&passfile_hold_buf;
808
809	if (pw_init(dirname(passfile), passfile)) {
810		yp_error("pw_init() failed");
811		return &result;
812	}
813	if ((pfd = pw_lock()) == -1) {
814		pw_fini();
815		yp_error("pw_lock() failed");
816		return &result;
817	}
818	if ((tfd = pw_tmp(-1)) == -1) {
819		pw_fini();
820		yp_error("pw_tmp() failed");
821		return &result;
822	}
823	if (pw_copy(pfd, tfd, (struct passwd *)&argp->newpw, NULL) == -1) {
824		pw_fini();
825		yp_error("pw_copy() failed");
826		return &result;
827	}
828	if (rename(passfile, passfile_hold) == -1) {
829		pw_fini();
830		yp_error("rename of %s to %s failed", passfile,
831		    passfile_hold);
832		return &result;
833	}
834	if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) {
835		/*
836		 * NIS server is exporting the system's master.passwd.
837		 * Call pw_mkdb to rebuild passwd and the .db files
838		 */
839		if (pw_mkdb(argp->newpw.pw_name) == -1) {
840			pw_fini();
841			yp_error("pw_mkdb() failed");
842			rename(passfile_hold, passfile);
843			return &result;
844		}
845	} else {
846		/*
847		 * NIS server is exporting a private master.passwd.
848		 * Rename tempfile into final location
849		 */
850		if (rename(pw_tempname(), passfile) == -1) {
851			pw_fini();
852			yp_error("rename of %s to %s failed",
853			    pw_tempname(), passfile);
854			rename(passfile_hold, passfile);
855			return &result;
856		}
857	}
858	pw_fini();
859
860	if (inplace) {
861		if ((rval = update_inplace((struct passwd *)&argp->newpw,
862							argp->domain))) {
863			yp_error("inplace update failed -- rebuilding maps");
864		}
865	}
866
867	switch ((pid = fork())) {
868	case 0:
869		if (inplace && !rval) {
870    			execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
871				argp->domain, "pushpw", (char *)NULL);
872    		} else {
873			execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
874				argp->domain, (char *)NULL);
875		}
876    		yp_error("couldn't exec map update process: %s",
877					strerror(errno));
878		unlink(passfile);
879		rename(passfile_hold, passfile);
880    		exit(1);
881		break;
882	case -1:
883		yp_error("fork() failed: %s", strerror(errno));
884		unlink(passfile);
885		rename(passfile_hold, passfile);
886		return(&result);
887		break;
888	default:
889		unlink(passfile_hold);
890		break;
891	}
892
893	yp_error("performed update of user %s (uid %d) domain %s",
894						argp->newpw.pw_name,
895						argp->newpw.pw_uid,
896						argp->domain);
897
898	result = 0;
899	return(&result);
900}
901