2 * Copyright (c) 1995, 1996
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 static const char rcsid[] =
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
54 #include <rpcsvc/yp.h>
55 #include <sys/types.h>
57 #include <sys/param.h>
58 #include <sys/fcntl.h>
59 struct dom_binding {};
60 #include <rpcsvc/ypclnt.h>
61 #include "yppasswdd_extern.h"
63 #include "yppasswd_private.h"
67 struct cmsgcred cmcred;
84 while(wait3(&st, WNOHANG, NULL) > 0) ;
86 pid = waitpid(pid, &pstat, 0);
93 void install_reaper(on)
97 signal(SIGCHLD, reaper);
99 signal(SIGCHLD, SIG_DFL);
104 static struct passwd yp_password;
106 static void copy_yp_pass(p, x, m)
110 register char *t, *s = p;
113 yp_password.pw_fields = 0;
115 buf = (char *)realloc(buf, m + 10);
118 /* Turn all colons into NULLs */
119 while (strchr(s, ':')) {
120 s = (strchr(s, ':') + 1);
125 #define EXPAND(e) e = t; while ((*t++ = *p++));
126 EXPAND(yp_password.pw_name);
127 yp_password.pw_fields |= _PWF_NAME;
128 EXPAND(yp_password.pw_passwd);
129 yp_password.pw_fields |= _PWF_PASSWD;
130 yp_password.pw_uid = atoi(p);
131 p += (strlen(p) + 1);
132 yp_password.pw_fields |= _PWF_UID;
133 yp_password.pw_gid = atoi(p);
134 p += (strlen(p) + 1);
135 yp_password.pw_fields |= _PWF_GID;
137 EXPAND(yp_password.pw_class);
138 yp_password.pw_fields |= _PWF_CLASS;
139 yp_password.pw_change = atol(p);
140 p += (strlen(p) + 1);
141 yp_password.pw_fields |= _PWF_CHANGE;
142 yp_password.pw_expire = atol(p);
143 p += (strlen(p) + 1);
144 yp_password.pw_fields |= _PWF_EXPIRE;
146 EXPAND(yp_password.pw_gecos);
147 yp_password.pw_fields |= _PWF_GECOS;
148 EXPAND(yp_password.pw_dir);
149 yp_password.pw_fields |= _PWF_DIR;
150 EXPAND(yp_password.pw_shell);
151 yp_password.pw_fields |= _PWF_SHELL;
156 static int validchars(arg)
161 for (i = 0; i < strlen(arg); i++) {
162 if (iscntrl(arg[i])) {
163 yp_error("string contains a control character");
167 yp_error("string contains a colon");
170 /* Be evil: truncate strings with \n in them silently. */
171 if (arg[i] == '\n') {
179 static int validate_master(opw, npw)
181 struct x_master_passwd *npw;
184 if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') {
185 yp_error("client tried to modify an NIS entry");
189 if (validchars(npw->pw_shell)) {
190 yp_error("specified shell contains invalid characters");
194 if (validchars(npw->pw_gecos)) {
195 yp_error("specified gecos field contains invalid characters");
199 if (validchars(npw->pw_passwd)) {
200 yp_error("specified password contains invalid characters");
206 static int validate(opw, npw)
208 struct x_passwd *npw;
211 if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') {
212 yp_error("client tried to modify an NIS entry");
216 if (npw->pw_uid != opw->pw_uid) {
217 yp_error("UID mismatch: client says user %s has UID %d",
218 npw->pw_name, npw->pw_uid);
219 yp_error("database says user %s has UID %d", opw->pw_name,
224 if (npw->pw_gid != opw->pw_gid) {
225 yp_error("GID mismatch: client says user %s has GID %d",
226 npw->pw_name, npw->pw_gid);
227 yp_error("database says user %s has GID %d", opw->pw_name,
233 * Don't allow the user to shoot himself in the foot,
236 if (!ok_shell(npw->pw_shell)) {
237 yp_error("%s is not a valid shell", npw->pw_shell);
241 if (validchars(npw->pw_shell)) {
242 yp_error("specified shell contains invalid characters");
246 if (validchars(npw->pw_gecos)) {
247 yp_error("specified gecos field contains invalid characters");
251 if (validchars(npw->pw_passwd)) {
252 yp_error("specified password contains invalid characters");
260 * In order to have one rpc.yppasswdd support multiple domains,
261 * we have to cheat: we search each directory under /var/yp
262 * and try to match the user in each master.passwd.byname
263 * map that we find. If the user matches (username, uid and gid
264 * all agree), then we use that domain. If we match the user in
265 * more than one database, we must abort.
267 static char *find_domain(pw)
273 char yp_mapdir[MAXPATHLEN + 2];
274 static char domain[YPMAXDOMAIN];
279 yp_error("performing multidomain lookup");
281 if ((dird = opendir(yp_dir)) == NULL) {
282 yp_error("opendir(%s) failed: %s", yp_dir, strerror(errno));
286 while ((dirp = readdir(dird)) != NULL) {
287 snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s",
288 yp_dir, dirp->d_name);
289 if (stat(yp_mapdir, &statbuf) < 0) {
290 yp_error("stat(%s) failed: %s", yp_mapdir,
295 if (S_ISDIR(statbuf.st_mode)) {
296 tmp = (char *)dirp->d_name;
297 key.data = pw->pw_name;
298 key.size = strlen(pw->pw_name);
300 if (yp_get_record(tmp,"master.passwd.byname",
301 &key, &data, 0) != YP_TRUE) {
304 *(char *)(data.data + data.size) = '\0';
305 copy_yp_pass(data.data, 1, data.size);
306 if (yp_password.pw_uid == pw->pw_uid &&
307 yp_password.pw_gid == pw->pw_gid) {
309 snprintf(domain, YPMAXDOMAIN, "%s", tmp);
316 yp_error("found same user in two different domains");
319 return((char *)&domain);
322 static int update_inplace(pw, domain)
327 DBT key = { NULL, 0 };
328 DBT data = { NULL, 0 };
329 char pwbuf[YPMAXRECORD];
332 char *maps[] = { "master.passwd.byname", "master.passwd.byuid",
333 "passwd.byname", "passwd.byuid" };
335 char *formats[] = { "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
336 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
337 "%s:%s:%d:%d:%s:%s:%s", "%s:%s:%d:%d:%s:%s:%s" };
339 char *yp_last = "YP_LAST_MODIFIED";
340 char yplastbuf[YPMAXRECORD];
342 snprintf(yplastbuf, sizeof(yplastbuf), "%lu", time(NULL));
344 for (i = 0; i < 4; i++) {
347 snprintf(keybuf, sizeof(keybuf), "%ld", pw->pw_uid);
348 key.data = (char *)&keybuf;
349 key.size = strlen(keybuf);
351 key.data = pw->pw_name;
352 key.size = strlen(pw->pw_name);
356 * XXX The passwd.byname and passwd.byuid maps come in
357 * two flavors: secure and insecure. The secure version
358 * has a '*' in the password field whereas the insecure one
359 * has a real crypted password. The maps will be insecure
360 * if they were built with 'unsecure = TRUE' enabled in
361 * /var/yp/Makefile, but we'd have no way of knowing if
362 * this has been done unless we were to try parsing the
363 * Makefile, which is a disgusting thought. Instead, we
364 * read the records from the maps, skip to the first ':'
365 * in them, and then look at the character immediately
366 * following it. If it's an '*' then the map is 'secure'
367 * and we must not insert a real password into the pw_passwd
368 * field. If it's not an '*', then we put the real crypted
371 if (yp_get_record(domain,maps[i],&key,&data,1) != YP_TRUE) {
372 yp_error("couldn't read %s/%s: %s", domain,
373 maps[i], strerror(errno));
377 if ((ptr = strchr(data.data, ':')) == NULL) {
378 yp_error("no colon in passwd record?!");
383 * XXX Supposing we have more than one user with the same
384 * UID? (Or more than one user with the same name?) We could
385 * end up modifying the wrong record if were not careful.
388 if (strncmp(data.data, pw->pw_name,
389 strlen(pw->pw_name))) {
390 yp_error("warning: found entry for UID %d \
391 in map %s@%s with wrong name (%.*s)", pw->pw_uid, maps[i], domain,
392 ptr - (char *)data.data, data.data);
393 yp_error("there may be more than one user \
394 with the same UID - continuing");
399 * We're really being ultra-paranoid here.
400 * This is generally a 'can't happen' condition.
402 snprintf(pwbuf, sizeof(pwbuf), ":%d:%d:", pw->pw_uid,
404 if (!strstr(data.data, pwbuf)) {
405 yp_error("warning: found entry for user %s \
406 in map %s@%s with wrong UID", pw->pw_name, maps[i], domain);
407 yp_error("there may be more than one user
408 with the same name - continuing");
414 snprintf(pwbuf, sizeof(pwbuf), formats[i],
415 pw->pw_name, pw->pw_passwd, pw->pw_uid,
416 pw->pw_gid, pw->pw_class, pw->pw_change,
417 pw->pw_expire, pw->pw_gecos, pw->pw_dir,
420 snprintf(pwbuf, sizeof(pwbuf), formats[i],
421 pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd,
422 pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir,
426 #define FLAGS O_RDWR|O_CREAT
428 if ((dbp = yp_open_db_rw(domain, maps[i], FLAGS)) == NULL) {
429 yp_error("couldn't open %s/%s r/w: %s",domain,
430 maps[i],strerror(errno));
435 data.size = strlen(pwbuf);
437 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
438 yp_error("failed to update record in %s/%s", domain,
440 (void)(dbp->close)(dbp);
445 key.size = strlen(yp_last);
446 data.data = (char *)&yplastbuf;
447 data.size = strlen(yplastbuf);
449 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
450 yp_error("failed to update timestamp in %s/%s", domain,
452 (void)(dbp->close)(dbp);
456 (void)(dbp->close)(dbp);
462 static char *yp_mktmpnam()
464 static char path[MAXPATHLEN];
467 sprintf(path,"%s",passfile);
468 if ((p = strrchr(path, '/')))
472 strcpy(p, "yppwtmp.XXXXXX");
473 return(mktemp(path));
477 yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp)
480 struct sockaddr_in *rqhost;
485 int passwd_changed = 0;
486 int shell_changed = 0;
487 int gecos_changed = 0;
488 char *oldshell = NULL;
489 char *oldgecos = NULL;
491 char passfile_buf[MAXPATHLEN + 2];
492 char *domain = yppasswd_domain;
493 static struct sockaddr_in clntaddr;
494 static struct timeval t_saved, t_test;
497 * Normal user updates always use the 'default' master.passwd file.
500 passfile = passfile_default;
503 rqhost = svc_getcaller(rqstp->rq_xprt);
505 gettimeofday(&t_test, NULL);
506 if (!bcmp((char *)rqhost, (char *)&clntaddr,
507 sizeof(struct sockaddr_in)) &&
508 t_test.tv_sec > t_saved.tv_sec &&
509 t_test.tv_sec - t_saved.tv_sec < 300) {
511 bzero((char *)&clntaddr, sizeof(struct sockaddr_in));
512 bzero((char *)&t_saved, sizeof(struct timeval));
516 bcopy((char *)rqhost, (char *)&clntaddr, sizeof(struct sockaddr_in));
517 gettimeofday(&t_saved, NULL);
519 if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) {
520 yp_error("rejected update request from unauthorized host");
521 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
526 * Step one: find the user. (It's kinda pointless to
527 * proceed if the user doesn't exist.) We look for the
528 * user in the master.passwd.byname database, _NOT_ by
529 * using getpwent() and friends! We can't use getpwent()
530 * since the NIS master server is not guaranteed to be
531 * configured as an NIS client.
535 if ((domain = find_domain(&argp->newpw)) == NULL) {
536 yp_error("multidomain lookup failed - aborting update");
539 yp_error("updating user %s in domain %s",
540 argp->newpw.pw_name, domain);
543 key.data = argp->newpw.pw_name;
544 key.size = strlen(argp->newpw.pw_name);
546 if ((rval=yp_get_record(domain,"master.passwd.byname",
547 &key, &data, 0)) != YP_TRUE) {
548 if (rval == YP_NOKEY) {
549 yp_error("user %s not found in passwd database",
550 argp->newpw.pw_name);
552 yp_error("database access error: %s",
558 /* Nul terminate, please. */
559 *(char *)(data.data + data.size) = '\0';
561 copy_yp_pass(data.data, 1, data.size);
563 /* Step 2: check that the supplied oldpass is valid. */
565 if (strcmp(crypt(argp->oldpass, yp_password.pw_passwd),
566 yp_password.pw_passwd)) {
567 yp_error("rejected change attempt -- bad password");
568 yp_error("client address: %s username: %s",
569 inet_ntoa(rqhost->sin_addr),
570 argp->newpw.pw_name);
574 /* Step 3: validate the arguments passed to us by the client. */
576 if (validate(&yp_password, &argp->newpw)) {
577 yp_error("rejecting change attempt: bad arguments");
578 yp_error("client address: %s username: %s",
579 inet_ntoa(rqhost->sin_addr),
580 argp->newpw.pw_name);
581 svcerr_decode(rqstp->rq_xprt);
585 /* Step 4: update the user's passwd structure. */
587 if (!no_chsh && strcmp(argp->newpw.pw_shell, yp_password.pw_shell)) {
588 oldshell = yp_password.pw_shell;
589 yp_password.pw_shell = argp->newpw.pw_shell;
594 if (!no_chfn && strcmp(argp->newpw.pw_gecos, yp_password.pw_gecos)) {
595 oldgecos = yp_password.pw_gecos;
596 yp_password.pw_gecos = argp->newpw.pw_gecos;
600 if (strcmp(argp->newpw.pw_passwd, yp_password.pw_passwd)) {
601 yp_password.pw_passwd = argp->newpw.pw_passwd;
602 yp_password.pw_change = 0;
607 * If the caller specified a domain other than our 'default'
608 * domain, change the path to master.passwd accordingly.
611 if (strcmp(domain, yppasswd_domain)) {
612 snprintf(passfile_buf, sizeof(passfile_buf),
613 "%s/%s/master.passwd", yp_dir, domain);
614 passfile = (char *)&passfile_buf;
617 /* Step 5: make a new password file with the updated info. */
619 if ((pfd = pw_lock()) < 0) {
622 if ((tfd = pw_tmp()) < 0) {
626 if (pw_copy(pfd, tfd, &yp_password)) {
627 yp_error("failed to created updated password file -- \
628 cleaning up and bailing out");
633 passfile_hold = yp_mktmpnam();
634 rename(passfile, passfile_hold);
635 if (strcmp(passfile, _PATH_MASTERPASSWD)) {
636 rename(tempname, passfile);
638 if (pw_mkdb(argp->newpw.pw_name) < 0) {
639 yp_error("pwd_mkdb failed");
645 if ((rval = update_inplace(&yp_password, domain))) {
646 yp_error("inplace update failed -- rebuilding maps");
650 switch((pid = fork())) {
652 if (inplace && !rval) {
653 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
654 yppasswd_domain, "pushpw", (char *)NULL);
656 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
657 yppasswd_domain, (char *)NULL);
659 yp_error("couldn't exec map update process: %s",
662 rename(passfile_hold, passfile);
666 yp_error("fork() failed: %s", strerror(errno));
668 rename(passfile_hold, passfile);
672 unlink(passfile_hold);
677 yp_error("update completed for user %s (uid %d):",
682 yp_error("password changed");
685 yp_error("gecos changed ('%s' -> '%s')",
686 oldgecos, argp->newpw.pw_gecos);
689 yp_error("shell changed ('%s' -> '%s')",
690 oldshell, argp->newpw.pw_shell);
698 * Note that this function performs a little less sanity checking
699 * than the last one. Since only the superuser is allowed to use it,
700 * it is assumed that the caller knows what he's doing.
702 int *yppasswdproc_update_master_1_svc(master_yppasswd *argp,
703 struct svc_req *rqstp)
711 char passfile_buf[MAXPATHLEN + 2];
712 struct sockaddr_in *rqhost;
719 * NO AF_INET CONNETCIONS ALLOWED!
721 rqhost = svc_getcaller(rqstp->rq_xprt);
722 if (rqhost->sin_family != AF_UNIX) {
723 yp_error("Alert! %s/%d attempted to use superuser-only \
724 procedure!\n", inet_ntoa(rqhost->sin_addr), rqhost->sin_port);
725 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
729 transp = rqstp->rq_xprt;
731 if (transp->xp_verf.oa_length < sizeof(struct cmessage) ||
732 transp->xp_verf.oa_base == NULL ||
733 transp->xp_verf.oa_flavor != AUTH_UNIX) {
734 yp_error("caller didn't send proper credentials");
735 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
739 cm = (struct cmessage *)transp->xp_verf.oa_base;
740 if (cm->cmsg.cmsg_type != SCM_CREDS) {
741 yp_error("caller didn't send proper credentials");
742 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
746 if (cm->cmcred.cmcred_euid) {
747 yp_error("caller euid is %d, expecting 0 -- rejecting request",
748 cm->cmcred.cmcred_euid);
749 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
753 passfile = passfile_default;
755 key.data = argp->newpw.pw_name;
756 key.size = strlen(argp->newpw.pw_name);
759 * The superuser may add entries to the passwd maps if
760 * rpc.yppasswdd is started with the -a flag. Paranoia
761 * prevents me from allowing additions by default.
763 if ((rval = yp_get_record(argp->domain, "master.passwd.byname",
764 &key, &data, 0)) != YP_TRUE) {
765 if (rval == YP_NOKEY) {
766 yp_error("user %s not found in passwd database",
767 argp->newpw.pw_name);
769 yp_error("notice: adding user %s to \
770 master.passwd database for domain %s", argp->newpw.pw_name, argp->domain);
772 yp_error("restart rpc.yppasswdd with the -a flag to \
773 allow additions to be made to the password database");
775 yp_error("database access error: %s",
778 if (!allow_additions)
782 /* Nul terminate, please. */
783 *(char *)(data.data + data.size) = '\0';
785 copy_yp_pass(data.data, 1, data.size);
789 * Perform a small bit of sanity checking.
791 if (validate_master(rval == YP_TRUE ? &yp_password:NULL,&argp->newpw)){
792 yp_error("rejecting update attempt for %s: bad arguments",
793 argp->newpw.pw_name);
798 * If the caller specified a domain other than our 'default'
799 * domain, change the path to master.passwd accordingly.
802 if (strcmp(argp->domain, yppasswd_domain)) {
803 snprintf(passfile_buf, sizeof(passfile_buf),
804 "%s/%s/master.passwd", yp_dir, argp->domain);
805 passfile = (char *)&passfile_buf;
808 if ((pfd = pw_lock()) < 0) {
811 if ((tfd = pw_tmp()) < 0) {
815 if (pw_copy(pfd, tfd, (struct passwd *)&argp->newpw)) {
816 yp_error("failed to created updated password file -- \
817 cleaning up and bailing out");
822 passfile_hold = yp_mktmpnam();
823 rename(passfile, passfile_hold);
824 if (strcmp(passfile, _PATH_MASTERPASSWD)) {
825 rename(tempname, passfile);
827 if (pw_mkdb(argp->newpw.pw_name) < 0) {
828 yp_error("pwd_mkdb failed");
834 if ((rval = update_inplace((struct passwd *)&argp->newpw,
836 yp_error("inplace update failed -- rebuilding maps");
840 switch((pid = fork())) {
842 if (inplace && !rval) {
843 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
844 argp->domain, "pushpw", (char *)NULL);
846 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
847 argp->domain, (char *)NULL);
849 yp_error("couldn't exec map update process: %s",
852 rename(passfile_hold, passfile);
856 yp_error("fork() failed: %s", strerror(errno));
858 rename(passfile_hold, passfile);
862 unlink(passfile_hold);
866 yp_error("performed update of user %s (uid %d) domain %s",