From ba1556b538e52b19b85695ac79906abce8406487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Wed, 8 May 2002 15:42:37 +0000 Subject: [PATCH] Use libutil instead of pw_{copy,util}.c. Clean up a little. Warnsify. Sponsored by: DARPA, NAI Labs --- usr.sbin/rpc.yppasswdd/Makefile | 12 +- usr.sbin/rpc.yppasswdd/pw_copy.c | 150 ------------- usr.sbin/rpc.yppasswdd/pw_util.c | 181 ---------------- usr.sbin/rpc.yppasswdd/yppasswdd_extern.h | 17 +- usr.sbin/rpc.yppasswdd/yppasswdd_main.c | 85 +++++--- usr.sbin/rpc.yppasswdd/yppasswdd_server.c | 249 ++++++++++------------ 6 files changed, 173 insertions(+), 521 deletions(-) delete mode 100644 usr.sbin/rpc.yppasswdd/pw_copy.c delete mode 100644 usr.sbin/rpc.yppasswdd/pw_util.c diff --git a/usr.sbin/rpc.yppasswdd/Makefile b/usr.sbin/rpc.yppasswdd/Makefile index 56ae0d16a1f3..871a57881f92 100644 --- a/usr.sbin/rpc.yppasswdd/Makefile +++ b/usr.sbin/rpc.yppasswdd/Makefile @@ -7,17 +7,19 @@ RPCDIR= ${DESTDIR}/usr/include/rpcsvc PROG= rpc.yppasswdd MAN= rpc.yppasswdd.8 -SRCS= pw_copy.c pw_util.c util.c yp_access.c yp_dblookup.c yp_dbwrite.c \ +SRCS= util.c yp_access.c yp_dblookup.c yp_dbwrite.c \ yp_error.c yppasswdd_main.c yppasswdd_server.c ypxfr_misc.c ${GENSRCS} GENSRCS=yp.h yp_clnt.c yppasswd.h yppasswd_private.h yppasswd_private_svc.c \ yppasswd_private_xdr.c yppasswd_svc.c -CFLAGS+= -I${.CURDIR}/../../usr.sbin/vipw -I${.CURDIR}/../../usr.sbin/ypserv \ - -I${.CURDIR}/../../libexec/ypxfr -I${.CURDIR}/../../usr.bin/chpass \ +CFLAGS+= -I${.CURDIR}/../../usr.sbin/vipw \ + -I${.CURDIR}/../../usr.sbin/ypserv \ + -I${.CURDIR}/../../libexec/ypxfr \ -I${.CURDIR} -I. +WARNS?= 4 -DPADD= ${LIBRPCSVC} ${LIBCRYPT} -LDADD= -lrpcsvc -lcrypt +DPADD= ${LIBRPCSVC} ${LIBCRYPT} ${LIBUTIL} +LDADD= -lrpcsvc -lcrypt -lutil CLEANFILES= ${GENSRCS} diff --git a/usr.sbin/rpc.yppasswdd/pw_copy.c b/usr.sbin/rpc.yppasswdd/pw_copy.c deleted file mode 100644 index 24caf4d3c746..000000000000 --- a/usr.sbin/rpc.yppasswdd/pw_copy.c +++ /dev/null @@ -1,150 +0,0 @@ -/*- - * Copyright (c) 1990, 1993, 1994 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef lint -#if 0 -static char sccsid[] = "@(#)pw_copy.c 8.4 (Berkeley) 4/2/94"; -#endif -static const char rcsid[] = - "$FreeBSD$"; -#endif /* not lint */ - -/* - * This module is used to copy the master password file, replacing a single - * record, by chpass(1) and passwd(1). - */ - -#include -#include -#include -#include - -#include "yppasswdd_extern.h" - -int -pw_copy(int ffd, int tfd, struct passwd *pw) -{ - FILE *from, *to; - int done; - char *p, buf[8192]; - char uidstr[20]; - char gidstr[20]; - char chgstr[20]; - char expstr[20]; - - snprintf(uidstr, sizeof(uidstr), "%d", pw->pw_uid); - snprintf(gidstr, sizeof(gidstr), "%d", pw->pw_gid); - snprintf(chgstr, sizeof(chgstr), "%ld", pw->pw_change); - snprintf(expstr, sizeof(expstr), "%ld", pw->pw_expire); - - if (!(from = fdopen(ffd, "r"))) { - pw_error(passfile, 1, 1); - return(-1); - } - if (!(to = fdopen(tfd, "w"))) { - pw_error(tempname, 1, 1); - return(-1); - } - for (done = 0; fgets(buf, sizeof(buf), from);) { - if (!strchr(buf, '\n')) { - yp_error("%s: line too long", passfile); - pw_error(NULL, 0, 1); - goto err; - } - if (done) { - (void)fprintf(to, "%s", buf); - if (ferror(to)) - goto err; - continue; - } - /* - * Just copy comments and blank lines - */ - p = buf + strspn(buf, " \t\n"); - if (*p == '\0' || *p == '#') { - (void)fprintf(to, "%s", buf); - if (ferror(to)) - goto err; - continue; - } - if (!(p = strchr(buf, ':'))) { - yp_error("%s: corrupted entry", passfile); - pw_error(NULL, 0, 1); - goto err; - } - *p = '\0'; - if (strcmp(buf, pw->pw_name)) { - *p = ':'; - (void)fprintf(to, "%s", buf); - if (ferror(to)) - goto err; - continue; - } - (void)fprintf(to, "%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n", - pw->pw_name, pw->pw_passwd, - pw->pw_fields & _PWF_UID ? uidstr : "", - pw->pw_fields & _PWF_GID ? gidstr : "", - pw->pw_class, - pw->pw_fields & _PWF_CHANGE ? chgstr : "", - pw->pw_fields & _PWF_EXPIRE ? expstr : "", - pw->pw_gecos, pw->pw_dir, pw->pw_shell); - done = 1; - if (ferror(to)) - goto err; - } - if (!done) { - if (allow_additions) { - (void)fprintf(to, "%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n", - pw->pw_name, pw->pw_passwd, - pw->pw_fields & _PWF_UID ? uidstr : "", - pw->pw_fields & _PWF_GID ? gidstr : "", - pw->pw_class, - pw->pw_fields & _PWF_CHANGE ? chgstr : "", - pw->pw_fields & _PWF_EXPIRE ? expstr : "", - pw->pw_gecos, pw->pw_dir, pw->pw_shell); - } else { - yp_error("user \"%s\" not found in %s -- \ -NIS maps and password file possibly out of sync", pw->pw_name, passfile); - goto err; - } - } - if (ferror(to)) { -err: pw_error(NULL, 1, 1); - (void)fclose(to); - (void)fclose(from); - return(-1); - } - (void)fclose(to); - (void)fclose(from); - return(0); -} diff --git a/usr.sbin/rpc.yppasswdd/pw_util.c b/usr.sbin/rpc.yppasswdd/pw_util.c deleted file mode 100644 index bd85b1501bf4..000000000000 --- a/usr.sbin/rpc.yppasswdd/pw_util.c +++ /dev/null @@ -1,181 +0,0 @@ -/*- - * Copyright (c) 1990, 1993, 1994 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef lint -#if 0 -static char sccsid[] = "@(#)pw_util.c 8.3 (Berkeley) 4/2/94"; -#endif -static const char rcsid[] = - "$FreeBSD$"; -#endif /* not lint */ - -/* - * This file is used by all the "password" programs; vipw(8), chpass(1), - * and passwd(1). - */ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "yppasswdd_extern.h" - -int pstat; -pid_t pid; - -void -pw_init(void) -{ - struct rlimit rlim; - - /* Unlimited resource limits. */ - rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; - (void)setrlimit(RLIMIT_CPU, &rlim); - (void)setrlimit(RLIMIT_FSIZE, &rlim); - (void)setrlimit(RLIMIT_STACK, &rlim); - (void)setrlimit(RLIMIT_DATA, &rlim); - (void)setrlimit(RLIMIT_RSS, &rlim); - - /* Don't drop core (not really necessary, but GP's). */ - rlim.rlim_cur = rlim.rlim_max = 0; - (void)setrlimit(RLIMIT_CORE, &rlim); - - /* Turn off signals. */ - /* (void)signal(SIGALRM, SIG_IGN); */ - (void)signal(SIGHUP, SIG_IGN); - (void)signal(SIGINT, SIG_IGN); - (void)signal(SIGPIPE, SIG_IGN); - (void)signal(SIGQUIT, SIG_IGN); - (void)signal(SIGTSTP, SIG_IGN); - (void)signal(SIGTTOU, SIG_IGN); - - /* Create with exact permissions. */ - (void)umask(0); -} - -static int lockfd; - -int -pw_lock(void) -{ - /* - * If the master password file doesn't exist, the system is hosed. - * Might as well try to build one. Set the close-on-exec bit so - * that users can't get at the encrypted passwords while editing. - * Open should allow flock'ing the file; see 4.4BSD. XXX - */ - lockfd = open(passfile, O_RDONLY, 0); - if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1) { - yp_error("%s: %s", passfile, strerror(errno)); - return (-1); - } - if (flock(lockfd, LOCK_EX|LOCK_NB)) { - yp_error("%s: the password db file is busy", passfile); - return(-1); - } - return (lockfd); -} - -int -pw_tmp(void) -{ - static char path[MAXPATHLEN]; - int fd; - char *p; - - sprintf(path,"%s",passfile); - if ((p = strrchr(path, '/'))) - ++p; - else - p = path; - strcpy(p, "pw.XXXXXX"); - if ((fd = mkstemp(path)) == -1) { - yp_error("%s: %s", path, strerror(errno)); - return(-1); - } - tempname = path; - return (fd); -} - -int -pw_mkdb(const char *username) -{ - - yp_error("rebuilding the database..."); - (void)fflush(stderr); - /* Temporarily turn off SIGCHLD catching */ - install_reaper(0); - if (!(pid = vfork())) { - if (!username) { - execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", tempname, - (char *)NULL); - } else { - execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", "-u", username, - tempname, (char *)NULL); - } - pw_error(_PATH_PWD_MKDB, 1, 1); - return(-1); - } - /* Handle this ourselves. */ - reaper(-1); - /* Put the handler back. Foo. */ - install_reaper(1); - if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) { - return (-1); - } - yp_error("done"); - return (0); -} - -void -pw_error(const char *name, int err, int eval) -{ - if (err && name != NULL) - yp_error("%s", name); - - yp_error("%s: unchanged", passfile); - (void)unlink(tempname); -} diff --git a/usr.sbin/rpc.yppasswdd/yppasswdd_extern.h b/usr.sbin/rpc.yppasswdd/yppasswdd_extern.h index 5590639bd6a8..98cab6e9e511 100644 --- a/usr.sbin/rpc.yppasswdd/yppasswdd_extern.h +++ b/usr.sbin/rpc.yppasswdd/yppasswdd_extern.h @@ -32,17 +32,8 @@ * $FreeBSD$ */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "yp_extern.h" -#include "ypxfr_extern.h" +#ifndef _YPPASSWDD_EXTERN_H +#define _YPPASSWDD_EXTERN_H #ifndef YPLIBDIR #define YPLIBDIR "/usr/libexec/" @@ -63,11 +54,9 @@ extern void master_yppasswdprog_1 __P((struct svc_req *, register SVCXPRT *)); extern void reaper(int); extern void install_reaper(int); -extern int pw_copy(int, int, struct passwd *); extern char *ok_shell __P ((char *)); extern char *passfile; extern char *passfile_default; -extern char *tempname; extern char *yppasswd_domain; extern int no_chsh; extern int no_chfn; @@ -77,3 +66,5 @@ extern int resvport; extern int inplace; extern int verbose; extern int _rpc_dtablesize(void); + +#endif diff --git a/usr.sbin/rpc.yppasswdd/yppasswdd_main.c b/usr.sbin/rpc.yppasswdd/yppasswdd_main.c index b261feb644ed..c2b0d572d34e 100644 --- a/usr.sbin/rpc.yppasswdd/yppasswdd_main.c +++ b/usr.sbin/rpc.yppasswdd/yppasswdd_main.c @@ -35,34 +35,37 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ -#include "yppasswd.h" -#include -#include -#include /* getenv, exit */ -#include -#include #include -#include /* for pmap_unset */ -#include /* strcmp */ -#include -#include #include #include -#ifdef __cplusplus -#include /* getdtablesize, open */ -#endif /* __cplusplus */ -#include #include +#include +#include #include -#include + #include #include +#include +#include +#include +#include +#include /* getenv, exit */ +#include +#include /* strcmp */ +#include +#include + +#include +#include /* for pmap_unset */ #include struct dom_binding {}; #include + +#include "yppasswd.h" #include "yppasswdd_extern.h" #include "yppasswd_private.h" #include "ypxfr_extern.h" +#include "yp_extern.h" #ifndef SIG_PF #define SIG_PF void(*)(int) @@ -82,10 +85,15 @@ static int _rpcfdtype; #define _SERVED 1 #define _SERVING 2 +static char _localhost[] = "localhost"; +static char _passwd_byname[] = "passwd.byname"; extern int _rpcsvcstate; /* Set when a request is serviced */ -char *progname = "rpc.yppasswdd"; -char *yp_dir = _PATH_YP; -char *passfile_default = _PATH_YP "master.passwd"; +static char _progname[] = "rpc.yppasswdd"; +char *progname = _progname; +static char _yp_dir[] = _PATH_YP; +char *yp_dir = _yp_dir; +static char _passfile_default[] = _PATH_YP "master.passwd"; +char *passfile_default = _passfile_default; char *passfile; char *yppasswd_domain = NULL; int no_chsh = 0; @@ -95,10 +103,10 @@ int multidomain = 0; int verbose = 0; int resvport = 1; int inplace = 0; -char *sockname = YP_SOCKNAME; +char sockname[] = YP_SOCKNAME; static void -terminate(int sig) +terminate(int sig __unused) { rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); @@ -107,13 +115,13 @@ terminate(int sig) } static void -reload(int sig) +reload(int sig __unused) { load_securenets(); } static void -closedown(int sig) +closedown(int sig __unused) { if (_rpcsvcstate == _IDLE) { extern fd_set svc_fdset; @@ -154,7 +162,8 @@ usage(void) int main(int argc, char *argv[]) { - register SVCXPRT *transp = NULL; + struct rlimit rlim; + SVCXPRT *transp = NULL; struct sockaddr_in saddr; int asize = sizeof (saddr); struct netconfig *nconf; @@ -216,14 +225,14 @@ name isn't set -- aborting"); load_securenets(); - if (getrpcport("localhost", YPPROG, YPVERS, IPPROTO_UDP) <= 0) { + if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) { yp_error("no ypserv processes registered with local portmap"); yp_error("this host is not an NIS server -- aborting"); exit(1); } - if ((mastername = ypxfr_get_master(yppasswd_domain, "passwd.byname", - "localhost",0)) == NULL) { + if ((mastername = ypxfr_get_master(yppasswd_domain, + _passwd_byname, _localhost, 0)) == NULL) { yp_error("can't get name of NIS master server for domain %s", yppasswd_domain); exit(1); @@ -311,14 +320,26 @@ the %s domain -- aborting", yppasswd_domain); (void) signal(SIGALRM, (SIG_PF) closedown); (void) alarm(_RPCSVC_CLOSEDOWN/2); } - /* set up resource limits and block signals */ - pw_init(); - /* except SIGCHLD, which we need to catch */ - install_reaper(1); - signal(SIGTERM, (SIG_PF) terminate); + /* Unlimited resource limits. */ + rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; + (void)setrlimit(RLIMIT_CPU, &rlim); + (void)setrlimit(RLIMIT_FSIZE, &rlim); + (void)setrlimit(RLIMIT_STACK, &rlim); + (void)setrlimit(RLIMIT_DATA, &rlim); + (void)setrlimit(RLIMIT_RSS, &rlim); - signal(SIGHUP, (SIG_PF) reload); + /* Don't drop core (not really necessary, but GP's). */ + rlim.rlim_cur = rlim.rlim_max = 0; + (void)setrlimit(RLIMIT_CORE, &rlim); + + /* Turn off signals. */ + (void)signal(SIGALRM, SIG_IGN); + (void)signal(SIGHUP, (SIG_PF) reload); + (void)signal(SIGINT, SIG_IGN); + (void)signal(SIGPIPE, SIG_IGN); + (void)signal(SIGQUIT, SIG_IGN); + (void)signal(SIGTERM, (SIG_PF) terminate); svc_run(); yp_error("svc_run returned"); diff --git a/usr.sbin/rpc.yppasswdd/yppasswdd_server.c b/usr.sbin/rpc.yppasswdd/yppasswdd_server.c index 2ba3bac46791..9841397ae5d8 100644 --- a/usr.sbin/rpc.yppasswdd/yppasswdd_server.c +++ b/usr.sbin/rpc.yppasswdd/yppasswdd_server.c @@ -35,78 +35,51 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include -struct dom_binding {}; +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +struct dom_binding; #include #include "yppasswdd_extern.h" #include "yppasswd.h" #include "yppasswd_private.h" - -char *tempname; - -void -reaper(int sig) -{ - extern pid_t pid; - extern int pstat; - int st; - int saved_errno; - - saved_errno = errno; - - if (sig > 0) { - if (sig == SIGCHLD) - while (wait3(&st, WNOHANG, NULL) > 0) ; - } else { - pid = waitpid(pid, &pstat, 0); - } - - errno = saved_errno; - return; -} - -void -install_reaper(int on) -{ - if (on) { - signal(SIGCHLD, reaper); - } else { - signal(SIGCHLD, SIG_DFL); - } - return; -} +#include "ypxfr_extern.h" +#include "yp_extern.h" static struct passwd yp_password; static void copy_yp_pass(char *p, int x, int m) { - register char *t, *s = p; + char *t, *s = p; static char *buf; yp_password.pw_fields = 0; - buf = (char *)realloc(buf, m + 10); + buf = realloc(buf, m + 10); bzero(buf, m + 10); /* Turn all colons into NULLs */ @@ -150,7 +123,7 @@ copy_yp_pass(char *p, int x, int m) static int validchars(char *arg) { - int i; + size_t i; for (i = 0; i < strlen(arg); i++) { if (iscntrl(arg[i])) { @@ -171,7 +144,7 @@ validchars(char *arg) } static int -validate_master(struct passwd *opw, struct x_master_passwd *npw) +validate_master(struct passwd *opw __unused, struct x_master_passwd *npw) { if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') { @@ -205,7 +178,7 @@ validate(struct passwd *opw, struct x_passwd *npw) return(1); } - if (npw->pw_uid != opw->pw_uid) { + if ((uid_t)npw->pw_uid != opw->pw_uid) { yp_error("UID mismatch: client says user %s has UID %d", npw->pw_name, npw->pw_uid); yp_error("database says user %s has UID %d", opw->pw_name, @@ -213,7 +186,7 @@ validate(struct passwd *opw, struct x_passwd *npw) return(1); } - if (npw->pw_gid != opw->pw_gid) { + if ((gid_t)npw->pw_gid != opw->pw_gid) { yp_error("GID mismatch: client says user %s has GID %d", npw->pw_name, npw->pw_gid); yp_error("database says user %s has GID %d", opw->pw_name, @@ -276,7 +249,7 @@ find_domain(struct x_passwd *pw) } while ((dirp = readdir(dird)) != NULL) { - snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", + snprintf(yp_mapdir, sizeof yp_mapdir, "%s/%s", yp_dir, dirp->d_name); if (stat(yp_mapdir, &statbuf) < 0) { yp_error("stat(%s) failed: %s", yp_mapdir, @@ -293,10 +266,10 @@ find_domain(struct x_passwd *pw) &key, &data, 0) != YP_TRUE) { continue; } - *(char *)(data.data + data.size) = '\0'; + *((char *)data.data + data.size) = '\0'; copy_yp_pass(data.data, 1, data.size); - if (yp_password.pw_uid == pw->pw_uid && - yp_password.pw_gid == pw->pw_gid) { + if (yp_password.pw_uid == (uid_t)pw->pw_uid && + yp_password.pw_gid == (gid_t)pw->pw_gid) { hit++; snprintf(domain, YPMAXDOMAIN, "%s", tmp); } @@ -311,6 +284,20 @@ find_domain(struct x_passwd *pw) return((char *)&domain); } +static const char *maps[] = { + "master.passwd.byname", + "master.passwd.byuid", + "passwd.byname", + "passwd.byuid" +}; + +static const char *formats[] = { + "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", + "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", + "%s:%s:%d:%d:%s:%s:%s", + "%s:%s:%d:%d:%s:%s:%s" +}; + static int update_inplace(struct passwd *pw, char *domain) { @@ -320,23 +307,19 @@ update_inplace(struct passwd *pw, char *domain) char pwbuf[YPMAXRECORD]; char keybuf[20]; int i; - char *maps[] = { "master.passwd.byname", "master.passwd.byuid", - "passwd.byname", "passwd.byuid" }; - - char *formats[] = { "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", - "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", - "%s:%s:%d:%d:%s:%s:%s", "%s:%s:%d:%d:%s:%s:%s" }; char *ptr = NULL; - char *yp_last = "YP_LAST_MODIFIED"; + static char yp_last[] = "YP_LAST_MODIFIED"; char yplastbuf[YPMAXRECORD]; - snprintf(yplastbuf, sizeof(yplastbuf), "%lu", time(NULL)); + snprintf(yplastbuf, sizeof yplastbuf, "%llu", + (unsigned long long)time(NULL)); for (i = 0; i < 4; i++) { if (i % 2) { - snprintf(keybuf, sizeof(keybuf), "%ld", pw->pw_uid); - key.data = (char *)&keybuf; + snprintf(keybuf, sizeof keybuf, + "%llu", (unsigned long long)pw->pw_uid); + key.data = &keybuf; key.size = strlen(keybuf); } else { key.data = pw->pw_name; @@ -380,7 +363,7 @@ update_inplace(struct passwd *pw, char *domain) strlen(pw->pw_name))) { yp_error("warning: found entry for UID %d \ in map %s@%s with wrong name (%.*s)", pw->pw_uid, maps[i], domain, - ptr - (char *)data.data, data.data); + ptr - (char *)data.data, (char *)data.data); yp_error("there may be more than one user \ with the same UID - continuing"); continue; @@ -390,7 +373,7 @@ with the same UID - continuing"); * We're really being ultra-paranoid here. * This is generally a 'can't happen' condition. */ - snprintf(pwbuf, sizeof(pwbuf), ":%d:%d:", pw->pw_uid, + snprintf(pwbuf, sizeof pwbuf, ":%d:%d:", pw->pw_uid, pw->pw_gid); if (!strstr(data.data, pwbuf)) { yp_error("warning: found entry for user %s \ @@ -402,13 +385,13 @@ with the same name - continuing"); } if (i < 2) { - snprintf(pwbuf, sizeof(pwbuf), formats[i], + snprintf(pwbuf, sizeof pwbuf, formats[i], pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir, pw->pw_shell); } else { - snprintf(pwbuf, sizeof(pwbuf), formats[i], + snprintf(pwbuf, sizeof pwbuf, formats[i], pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell); @@ -450,21 +433,6 @@ with the same name - continuing"); return(0); } -static char * -yp_mktmpnam(void) -{ - static char path[MAXPATHLEN]; - char *p; - - sprintf(path,"%s",passfile); - if ((p = strrchr(path, '/'))) - ++p; - else - p = path; - strcpy(p, "yppwtmp.XXXXXX"); - return(mktemp(path)); -} - int * yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp) { @@ -495,17 +463,16 @@ yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp) rqhost = svc_getcaller(rqstp->rq_xprt); gettimeofday(&t_test, NULL); - if (!bcmp((char *)rqhost, (char *)&clntaddr, - sizeof(struct sockaddr_in)) && + if (!bcmp(rqhost, &clntaddr, sizeof *rqhost) && t_test.tv_sec > t_saved.tv_sec && t_test.tv_sec - t_saved.tv_sec < 300) { - bzero((char *)&clntaddr, sizeof(struct sockaddr_in)); - bzero((char *)&t_saved, sizeof(struct timeval)); + bzero(&clntaddr, sizeof clntaddr); + bzero(&t_saved, sizeof t_saved); return(NULL); } - bcopy((char *)rqhost, (char *)&clntaddr, sizeof(struct sockaddr_in)); + bcopy(rqhost, &clntaddr, sizeof clntaddr); gettimeofday(&t_saved, NULL); if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) { @@ -548,7 +515,7 @@ yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp) } /* Nul terminate, please. */ - *(char *)(data.data + data.size) = '\0'; + *((char *)data.data + data.size) = '\0'; copy_yp_pass(data.data, 1, data.size); @@ -608,30 +575,31 @@ yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp) /* Step 5: make a new password file with the updated info. */ - if ((pfd = pw_lock()) < 0) { - return (&result); + if (pw_init(dirname(passfile), passfile)) { + yp_error("pw_init() failed"); + return &result; } - if ((tfd = pw_tmp()) < 0) { - return (&result); + if ((pfd = pw_lock()) == -1) { + pw_fini(); + yp_error("pw_lock() failed"); + return &result; } - - if (pw_copy(pfd, tfd, &yp_password)) { - yp_error("failed to created updated password file -- \ -cleaning up and bailing out"); - unlink(tempname); - return(&result); + if ((tfd = pw_tmp(-1)) == -1) { + pw_fini(); + yp_error("pw_tmp() failed"); + return &result; } - - passfile_hold = yp_mktmpnam(); - rename(passfile, passfile_hold); - if (strcmp(passfile, _PATH_MASTERPASSWD)) { - rename(tempname, passfile); - } else { - if (pw_mkdb(argp->newpw.pw_name) < 0) { - yp_error("pwd_mkdb failed"); - return(&result); - } + if (pw_copy(pfd, tfd, &yp_password, NULL) == -1) { + pw_fini(); + yp_error("pw_copy() failed"); + return &result; } + if (pw_mkdb(yp_password.pw_name) == -1) { + pw_fini(); + yp_error("pw_mkdb() failed"); + return &result; + } + pw_fini(); if (inplace) { if ((rval = update_inplace(&yp_password, domain))) { @@ -769,7 +737,7 @@ allow additions to be made to the password database"); } else { /* Nul terminate, please. */ - *(char *)(data.data + data.size) = '\0'; + *((char *)data.data + data.size) = '\0'; copy_yp_pass(data.data, 1, data.size); } @@ -794,30 +762,31 @@ allow additions to be made to the password database"); passfile = (char *)&passfile_buf; } - if ((pfd = pw_lock()) < 0) { - return (&result); + if (pw_init(dirname(passfile), passfile)) { + yp_error("pw_init() failed"); + return &result; } - if ((tfd = pw_tmp()) < 0) { - return (&result); + if ((pfd = pw_lock()) == -1) { + pw_fini(); + yp_error("pw_lock() failed"); + return &result; } - - if (pw_copy(pfd, tfd, (struct passwd *)&argp->newpw)) { - yp_error("failed to created updated password file -- \ -cleaning up and bailing out"); - unlink(tempname); - return(&result); + if ((tfd = pw_tmp(-1)) == -1) { + pw_fini(); + yp_error("pw_tmp() failed"); + return &result; } - - passfile_hold = yp_mktmpnam(); - rename(passfile, passfile_hold); - if (strcmp(passfile, _PATH_MASTERPASSWD)) { - rename(tempname, passfile); - } else { - if (pw_mkdb(argp->newpw.pw_name) < 0) { - yp_error("pwd_mkdb failed"); - return(&result); - } + if (pw_copy(pfd, tfd, (struct passwd *)&argp->newpw, NULL) == -1) { + pw_fini(); + yp_error("pw_copy() failed"); + return &result; } + if (pw_mkdb(argp->newpw.pw_name) == -1) { + pw_fini(); + yp_error("pw_mkdb() failed"); + return &result; + } + pw_fini(); if (inplace) { if ((rval = update_inplace((struct passwd *)&argp->newpw,