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
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.5 2004/05/20 19:24:43 cpressey Exp $
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
52 #include <rpcsvc/yp.h>
53 #include <sys/types.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"
61 #include "yppasswd_private.h"
77 while (wait3(&st, WNOHANG, NULL) > 0) ;
79 pid = waitpid(pid, &pstat, 0);
86 void install_reaper(on)
90 signal(SIGCHLD, reaper);
92 signal(SIGCHLD, SIG_DFL);
97 static struct passwd yp_password;
99 static void copy_yp_pass(p, x, m)
106 yp_password.pw_fields = 0;
108 buf = (char *)realloc(buf, m + 10);
111 /* Turn all colons into NULLs */
112 while (strchr(s, ':')) {
113 s = (strchr(s, ':') + 1);
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;
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;
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;
149 static int validchars(arg)
154 for (i = 0; i < strlen(arg); i++) {
155 if (iscntrl(arg[i])) {
156 yp_error("string contains a control character");
160 yp_error("string contains a colon");
163 /* Be evil: truncate strings with \n in them silently. */
164 if (arg[i] == '\n') {
172 static int validate_master(opw, npw)
174 struct x_master_passwd *npw;
177 if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') {
178 yp_error("client tried to modify an NIS entry");
182 if (validchars(npw->pw_shell)) {
183 yp_error("specified shell contains invalid characters");
187 if (validchars(npw->pw_gecos)) {
188 yp_error("specified gecos field contains invalid characters");
192 if (validchars(npw->pw_passwd)) {
193 yp_error("specified password contains invalid characters");
199 static int validate(opw, npw)
201 struct x_passwd *npw;
204 if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') {
205 yp_error("client tried to modify an NIS entry");
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,
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,
226 * Don't allow the user to shoot himself in the foot,
229 if (!ok_shell(npw->pw_shell)) {
230 yp_error("%s is not a valid shell", npw->pw_shell);
234 if (validchars(npw->pw_shell)) {
235 yp_error("specified shell contains invalid characters");
239 if (validchars(npw->pw_gecos)) {
240 yp_error("specified gecos field contains invalid characters");
244 if (validchars(npw->pw_passwd)) {
245 yp_error("specified password contains invalid characters");
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.
260 static char *find_domain(pw)
266 char yp_mapdir[MAXPATHLEN + 2];
267 static char domain[YPMAXDOMAIN];
272 yp_error("performing multidomain lookup");
274 if ((dird = opendir(yp_dir)) == NULL) {
275 yp_error("opendir(%s) failed: %s", yp_dir, strerror(errno));
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,
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);
293 if (yp_get_record(tmp,"master.passwd.byname",
294 &key, &data, 0) != YP_TRUE) {
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) {
302 snprintf(domain, YPMAXDOMAIN, "%s", tmp);
309 yp_error("found same user in two different domains");
312 return((char *)&domain);
315 static int update_inplace(pw, domain)
320 DBT key = { NULL, 0 };
321 DBT data = { NULL, 0 };
322 char pwbuf[YPMAXRECORD];
325 char *maps[] = { "master.passwd.byname", "master.passwd.byuid",
326 "passwd.byname", "passwd.byuid" };
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" };
332 char *yp_last = "YP_LAST_MODIFIED";
333 char yplastbuf[YPMAXRECORD];
335 snprintf(yplastbuf, sizeof(yplastbuf), "%lu", time(NULL));
337 for (i = 0; i < 4; i++) {
340 snprintf(keybuf, sizeof(keybuf), "%ld", pw->pw_uid);
341 key.data = (char *)&keybuf;
342 key.size = strlen(keybuf);
344 key.data = pw->pw_name;
345 key.size = strlen(pw->pw_name);
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
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));
370 if ((ptr = strchr(data.data, ':')) == NULL) {
371 yp_error("no colon in passwd record?!");
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.
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)",
385 pw->pw_uid, maps[i], domain,
386 ptr - (char *)data.data, data.data);
387 yp_error("there may be more than one user "
388 "with the same UID - continuing");
393 * We're really being ultra-paranoid here.
394 * This is generally a 'can't happen' condition.
396 snprintf(pwbuf, sizeof(pwbuf), ":%d:%d:", pw->pw_uid,
398 if (!strstr(data.data, pwbuf)) {
399 yp_error("warning: found entry for user %s \
400 in map %s@%s with wrong UID", pw->pw_name, maps[i], domain);
401 yp_error("there may be more than one user \
402 with the same name - continuing");
408 snprintf(pwbuf, sizeof(pwbuf), formats[i],
409 pw->pw_name, pw->pw_passwd, pw->pw_uid,
410 pw->pw_gid, pw->pw_class, pw->pw_change,
411 pw->pw_expire, pw->pw_gecos, pw->pw_dir,
414 snprintf(pwbuf, sizeof(pwbuf), formats[i],
415 pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd,
416 pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir,
420 #define FLAGS O_RDWR|O_CREAT
422 if ((dbp = yp_open_db_rw(domain, maps[i], FLAGS)) == NULL) {
423 yp_error("couldn't open %s/%s r/w: %s",domain,
424 maps[i],strerror(errno));
429 data.size = strlen(pwbuf);
431 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
432 yp_error("failed to update record in %s/%s", domain,
434 (void)(dbp->close)(dbp);
439 key.size = strlen(yp_last);
440 data.data = (char *)&yplastbuf;
441 data.size = strlen(yplastbuf);
443 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
444 yp_error("failed to update timestamp in %s/%s", domain,
446 (void)(dbp->close)(dbp);
450 (void)(dbp->close)(dbp);
456 static char *yp_mktmpnam()
458 static char path[MAXPATHLEN];
461 sprintf(path,"%s",passfile);
462 if ((p = strrchr(path, '/')))
466 strcpy(p, "yppwtmp.XXXXXX");
467 return(mktemp(path));
471 yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp)
474 struct sockaddr_in *rqhost;
479 int passwd_changed = 0;
480 int shell_changed = 0;
481 int gecos_changed = 0;
482 char *oldshell = NULL;
483 char *oldgecos = NULL;
485 char passfile_buf[MAXPATHLEN + 2];
486 char *domain = yppasswd_domain;
487 static struct sockaddr_in clntaddr;
488 static struct timeval t_saved, t_test;
491 * Normal user updates always use the 'default' master.passwd file.
494 passfile = passfile_default;
497 rqhost = svc_getcaller(rqstp->rq_xprt);
499 gettimeofday(&t_test, NULL);
500 if (!bcmp((char *)rqhost, (char *)&clntaddr,
501 sizeof(struct sockaddr_in)) &&
502 t_test.tv_sec > t_saved.tv_sec &&
503 t_test.tv_sec - t_saved.tv_sec < 300) {
505 bzero((char *)&clntaddr, sizeof(struct sockaddr_in));
506 bzero((char *)&t_saved, sizeof(struct timeval));
510 bcopy((char *)rqhost, (char *)&clntaddr, sizeof(struct sockaddr_in));
511 gettimeofday(&t_saved, NULL);
513 if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) {
514 yp_error("rejected update request from unauthorized host");
515 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
520 * Step one: find the user. (It's kinda pointless to
521 * proceed if the user doesn't exist.) We look for the
522 * user in the master.passwd.byname database, _NOT_ by
523 * using getpwent() and friends! We can't use getpwent()
524 * since the NIS master server is not guaranteed to be
525 * configured as an NIS client.
529 if ((domain = find_domain(&argp->newpw)) == NULL) {
530 yp_error("multidomain lookup failed - aborting update");
533 yp_error("updating user %s in domain %s",
534 argp->newpw.pw_name, domain);
537 key.data = argp->newpw.pw_name;
538 key.size = strlen(argp->newpw.pw_name);
540 if ((rval = yp_get_record(domain,"master.passwd.byname",
541 &key, &data, 0)) != YP_TRUE) {
542 if (rval == YP_NOKEY) {
543 yp_error("user %s not found in passwd database",
544 argp->newpw.pw_name);
546 yp_error("database access error: %s",
552 /* Nul terminate, please. */
553 *(char *)(data.data + data.size) = '\0';
555 copy_yp_pass(data.data, 1, data.size);
557 /* Step 2: check that the supplied oldpass is valid. */
559 if (strcmp(crypt(argp->oldpass, yp_password.pw_passwd),
560 yp_password.pw_passwd)) {
561 yp_error("rejected change attempt -- bad password");
562 yp_error("client address: %s username: %s",
563 inet_ntoa(rqhost->sin_addr),
564 argp->newpw.pw_name);
568 /* Step 3: validate the arguments passed to us by the client. */
570 if (validate(&yp_password, &argp->newpw)) {
571 yp_error("rejecting change attempt: bad arguments");
572 yp_error("client address: %s username: %s",
573 inet_ntoa(rqhost->sin_addr),
574 argp->newpw.pw_name);
575 svcerr_decode(rqstp->rq_xprt);
579 /* Step 4: update the user's passwd structure. */
581 if (!no_chsh && strcmp(argp->newpw.pw_shell, yp_password.pw_shell)) {
582 oldshell = yp_password.pw_shell;
583 yp_password.pw_shell = argp->newpw.pw_shell;
588 if (!no_chfn && strcmp(argp->newpw.pw_gecos, yp_password.pw_gecos)) {
589 oldgecos = yp_password.pw_gecos;
590 yp_password.pw_gecos = argp->newpw.pw_gecos;
594 if (strcmp(argp->newpw.pw_passwd, yp_password.pw_passwd)) {
595 yp_password.pw_passwd = argp->newpw.pw_passwd;
596 yp_password.pw_change = 0;
601 * If the caller specified a domain other than our 'default'
602 * domain, change the path to master.passwd accordingly.
605 if (strcmp(domain, yppasswd_domain)) {
606 snprintf(passfile_buf, sizeof(passfile_buf),
607 "%s/%s/master.passwd", yp_dir, domain);
608 passfile = (char *)&passfile_buf;
611 /* Step 5: make a new password file with the updated info. */
613 if ((pfd = pw_lock()) < 0) {
616 if ((tfd = pw_tmp()) < 0) {
620 if (pw_copy(pfd, tfd, &yp_password)) {
621 yp_error("failed to created updated password file -- \
622 cleaning up and bailing out");
627 passfile_hold = yp_mktmpnam();
628 rename(passfile, passfile_hold);
629 if (strcmp(passfile, _PATH_MASTERPASSWD)) {
630 rename(tempname, passfile);
632 if (pw_mkdb(argp->newpw.pw_name) < 0) {
633 yp_error("pwd_mkdb failed");
639 if ((rval = update_inplace(&yp_password, domain))) {
640 yp_error("inplace update failed -- rebuilding maps");
644 switch ((pid = fork())) {
646 if (inplace && !rval) {
647 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
648 yppasswd_domain, "pushpw", NULL);
650 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
651 yppasswd_domain, NULL);
653 yp_error("couldn't exec map update process: %s",
656 rename(passfile_hold, passfile);
660 yp_error("fork() failed: %s", strerror(errno));
662 rename(passfile_hold, passfile);
666 unlink(passfile_hold);
671 yp_error("update completed for user %s (uid %d):",
676 yp_error("password changed");
679 yp_error("gecos changed ('%s' -> '%s')",
680 oldgecos, argp->newpw.pw_gecos);
683 yp_error("shell changed ('%s' -> '%s')",
684 oldshell, argp->newpw.pw_shell);
693 struct cmsgcred cmcred;
697 * Note that this function performs a little less sanity checking
698 * than the last one. Since only the superuser is allowed to use it,
699 * it is assumed that the caller knows what he's doing.
701 int *yppasswdproc_update_master_1_svc(master_yppasswd *argp,
702 struct svc_req *rqstp)
710 char passfile_buf[MAXPATHLEN + 2];
711 struct sockaddr_in *rqhost;
718 * NO AF_INET CONNETCIONS ALLOWED!
720 rqhost = svc_getcaller(rqstp->rq_xprt);
721 if (rqhost->sin_family != AF_UNIX) {
722 yp_error("Alert! %s/%d attempted to use superuser-only \
723 procedure!\n", inet_ntoa(rqhost->sin_addr), rqhost->sin_port);
724 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
728 transp = rqstp->rq_xprt;
730 if (transp->xp_verf.oa_length < sizeof(struct cmessage) ||
731 transp->xp_verf.oa_base == NULL ||
732 transp->xp_verf.oa_flavor != AUTH_UNIX) {
733 yp_error("caller didn't send proper credentials");
734 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
738 cm = (struct cmessage *)transp->xp_verf.oa_base;
739 if (cm->cmsg.cmsg_type != SCM_CREDS) {
740 yp_error("caller didn't send proper credentials");
741 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
745 if (cm->cmcred.cmcred_euid) {
746 yp_error("caller euid is %d, expecting 0 -- rejecting request",
747 cm->cmcred.cmcred_euid);
748 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
752 passfile = passfile_default;
754 key.data = argp->newpw.pw_name;
755 key.size = strlen(argp->newpw.pw_name);
758 * The superuser may add entries to the passwd maps if
759 * rpc.yppasswdd is started with the -a flag. Paranoia
760 * prevents me from allowing additions by default.
762 if ((rval = yp_get_record(argp->domain, "master.passwd.byname",
763 &key, &data, 0)) != YP_TRUE) {
764 if (rval == YP_NOKEY) {
765 yp_error("user %s not found in passwd database",
766 argp->newpw.pw_name);
768 yp_error("notice: adding user %s to \
769 master.passwd database for domain %s", argp->newpw.pw_name, argp->domain);
771 yp_error("restart rpc.yppasswdd with the -a flag to \
772 allow additions to be made to the password database");
774 yp_error("database access error: %s",
777 if (!allow_additions)
781 /* Nul terminate, please. */
782 *(char *)(data.data + data.size) = '\0';
784 copy_yp_pass(data.data, 1, data.size);
788 * Perform a small bit of sanity checking.
790 if (validate_master(rval == YP_TRUE ? &yp_password:NULL,&argp->newpw)){
791 yp_error("rejecting update attempt for %s: bad arguments",
792 argp->newpw.pw_name);
797 * If the caller specified a domain other than our 'default'
798 * domain, change the path to master.passwd accordingly.
801 if (strcmp(argp->domain, yppasswd_domain)) {
802 snprintf(passfile_buf, sizeof(passfile_buf),
803 "%s/%s/master.passwd", yp_dir, argp->domain);
804 passfile = (char *)&passfile_buf;
807 if ((pfd = pw_lock()) < 0) {
810 if ((tfd = pw_tmp()) < 0) {
814 if (pw_copy(pfd, tfd, (struct passwd *)&argp->newpw)) {
815 yp_error("failed to created updated password file -- \
816 cleaning up and bailing out");
821 passfile_hold = yp_mktmpnam();
822 rename(passfile, passfile_hold);
823 if (strcmp(passfile, _PATH_MASTERPASSWD)) {
824 rename(tempname, passfile);
826 if (pw_mkdb(argp->newpw.pw_name) < 0) {
827 yp_error("pwd_mkdb failed");
833 if ((rval = update_inplace((struct passwd *)&argp->newpw,
835 yp_error("inplace update failed -- rebuilding maps");
839 switch ((pid = fork())) {
841 if (inplace && !rval) {
842 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
843 argp->domain, "pushpw", NULL);
845 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
848 yp_error("couldn't exec map update process: %s",
851 rename(passfile_hold, passfile);
855 yp_error("fork() failed: %s", strerror(errno));
857 rename(passfile_hold, passfile);
861 unlink(passfile_hold);
865 yp_error("performed update of user %s (uid %d) domain %s",