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