Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 7 Jun 2015 14:34:38 +0000 (UTC)
From:      Baptiste Daroussin <bapt@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r284118 - head/usr.sbin/pw
Message-ID:  <201506071434.t57EYcK0091841@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: bapt
Date: Sun Jun  7 14:34:38 2015
New Revision: 284118
URL: https://svnweb.freebsd.org/changeset/base/284118

Log:
  Add a new global struct pwconf to store etcpath, rootdir and struct userconf
  
  Do not add anymore -R and -V to arglist
  Add an error message if both -V and -R are set in arguments

Modified:
  head/usr.sbin/pw/grupd.c
  head/usr.sbin/pw/pw.c
  head/usr.sbin/pw/pw.h
  head/usr.sbin/pw/pw_group.c
  head/usr.sbin/pw/pw_user.c
  head/usr.sbin/pw/pwupd.c
  head/usr.sbin/pw/pwupd.h

Modified: head/usr.sbin/pw/grupd.c
==============================================================================
--- head/usr.sbin/pw/grupd.c	Sun Jun  7 14:32:52 2015	(r284117)
+++ head/usr.sbin/pw/grupd.c	Sun Jun  7 14:34:38 2015	(r284118)
@@ -39,28 +39,14 @@ static const char rcsid[] =
 
 #include "pwupd.h"
 
-static char * grpath = _PATH_PWD;
-
-int
-setgrdir(const char * dir)
-{
-	if (dir == NULL)
-		return -1;
-	else
-		grpath = strdup(dir);
-	if (grpath == NULL)
-		return -1;
-
-	return 0;
-}
-
 char *
 getgrpath(const char * file)
 {
 	static char pathbuf[MAXPATHLEN];
 
-	snprintf(pathbuf, sizeof pathbuf, "%s/%s", grpath, file);
-	return pathbuf;
+	snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file);
+
+	return (pathbuf);
 }
 
 static int
@@ -76,7 +62,7 @@ gr_update(struct group * grp, char const
 	if (group != NULL)
 		old_gr = GETGRNAM(group);
 
-	if (gr_init(grpath, NULL))
+	if (gr_init(conf.etcpath, NULL))
 		err(1, "gr_init()");
 
 	if ((pfd = gr_lock()) == -1) {

Modified: head/usr.sbin/pw/pw.c
==============================================================================
--- head/usr.sbin/pw/pw.c	Sun Jun  7 14:32:52 2015	(r284117)
+++ head/usr.sbin/pw/pw.c	Sun Jun  7 14:34:38 2015	(r284118)
@@ -33,6 +33,7 @@ static const char rcsid[] =
 #include <fcntl.h>
 #include <locale.h>
 #include <paths.h>
+#include <stdbool.h>
 #include <sys/wait.h>
 #include "pw.h"
 
@@ -84,6 +85,8 @@ struct pwf VPWF =
 	vgetgrnam,
 };
 
+struct pwconf conf;
+
 static struct cargs arglist;
 
 static int      getindex(const char *words[], const char *word);
@@ -97,11 +100,9 @@ main(int argc, char *argv[])
 	int             mode = -1;
 	int             which = -1;
 	char		*config = NULL;
-	struct userconf *cnf;
 	struct stat	st;
 	char		arg;
-	struct carg	*carg;
-	char		*etcpath = NULL;
+	bool		relocated = false;
 
 	static const char *opts[W_NUM][M_NUM] =
 	{
@@ -123,12 +124,15 @@ main(int argc, char *argv[])
 		 }
 	};
 
-	static int      (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
+	static int      (*funcs[W_NUM]) (int _mode, struct cargs * _args) =
 	{			/* Request handlers */
 		pw_user,
 		pw_group
 	};
 
+	conf.rootdir[0] = '\0';
+	strlcpy(conf.etcpath, _PATH_PWD, sizeof(conf.etcpath));
+
 	LIST_INIT(&arglist);
 
 	(void)setlocale(LC_ALL, "");
@@ -146,6 +150,10 @@ main(int argc, char *argv[])
 			 */
 			arg = argv[1][1];
 			if (arg == 'V' || arg == 'R') {
+				if (relocated)
+					errx(EXIT_FAILURE, "Both '-R' and '-V' "
+					    "specified, only one accepted");
+				relocated = true;
 				optarg = &argv[1][2];
 				if (*optarg == '\0') {
 					if (stat(argv[2], &st) != 0)
@@ -159,7 +167,14 @@ main(int argc, char *argv[])
 					++argv;
 					--argc;
 				}
-				addarg(&arglist, arg, optarg);
+				memcpy(&PWF, &VPWF, sizeof PWF);
+				if (arg == 'R') {
+					strlcpy(conf.rootdir, optarg,
+					    sizeof(conf.rootdir));
+					PWF._altdir = PWF_ROOTDIR;
+				}
+				snprintf(conf.etcpath, sizeof(conf.etcpath),
+				    "%s%s", optarg, arg == 'R' ? "/etc" : "");
 			} else
 				break;
 		}
@@ -220,37 +235,18 @@ main(int argc, char *argv[])
 	 */
 
 	config = getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL;
-
-	if ((carg = getarg(&arglist, 'R')) != NULL) {
-		asprintf(&etcpath, "%s/etc", carg->val);
-		if (etcpath == NULL)
+	if (config == NULL) {	/* Only override config location if -C not specified */
+		asprintf(&config, "%s/pw.conf", conf.etcpath);
+		if (config == NULL)
 			errx(EX_OSERR, "out of memory");
 	}
-	if (etcpath == NULL && (carg = getarg(&arglist, 'V')) != NULL) {
-		etcpath = strdup(carg->val);
-		if (etcpath == NULL)
-			errx(EX_OSERR, "out of memory");
-	}
-	if (etcpath && *etcpath) {
-		if (config == NULL) {	/* Only override config location if -C not specified */
-			asprintf(&config, "%s/pw.conf", etcpath);
-			if (config == NULL)
-				 errx(EX_OSERR, "out of memory");
-		}
-		setpwdir(etcpath);
-		setgrdir(etcpath);
-		memcpy(&PWF, &VPWF, sizeof PWF);
-		if (getarg(&arglist, 'R'))
-			PWF._altdir = PWF_ROOTDIR;
-	}
-	free(etcpath);
 
 	/*
 	 * Now, let's do the common initialisation
 	 */
-	cnf = read_userconfig(config);
+	conf.userconf = read_userconfig(config);
 
-	ch = funcs[which] (cnf, mode, &arglist);
+	ch = funcs[which] (mode, &arglist);
 
 	/*
 	 * If everything went ok, and we've been asked to update
@@ -274,7 +270,7 @@ main(int argc, char *argv[])
 			if ((i = WEXITSTATUS(i)) != 0)
 				errx(ch, "make exited with status %d", i);
 			else
-				pw_log(cnf, mode, which, "NIS maps updated");
+				pw_log(conf.userconf, mode, which, "NIS maps updated");
 		}
 	}
 	return ch;

Modified: head/usr.sbin/pw/pw.h
==============================================================================
--- head/usr.sbin/pw/pw.h	Sun Jun  7 14:32:52 2015	(r284117)
+++ head/usr.sbin/pw/pw.h	Sun Jun  7 14:34:38 2015	(r284118)
@@ -72,30 +72,6 @@ struct carg
 
 LIST_HEAD(cargs, carg);
 
-struct userconf
-{
-	int	default_password;	/* Default password for new users? */
-	int	reuse_uids;		/* Reuse uids? */
-	int	reuse_gids;		/* Reuse gids? */
-	char	*nispasswd;		/* Path to NIS version of the passwd file */
-	char	*dotdir;		/* Where to obtain skeleton files */
-	char	*newmail;		/* Mail to send to new accounts */
-	char	*logfile;		/* Where to log changes */
-	char	*home;			/* Where to create home directory */
-	mode_t	homemode;		/* Home directory permissions */
-	char	*shelldir;		/* Where shells are located */
-	char	**shells;		/* List of shells */
-	char	*shell_default;		/* Default shell */
-	char	*default_group;		/* Default group number */
-	char	**groups;		/* Default (additional) groups */
-	char	*default_class;		/* Default user class */
-	uid_t	min_uid, max_uid;	/* Allowed range of uids */
-	gid_t	min_gid, max_gid;	/* Allowed range of gids */
-	int	expire_days;		/* Days to expiry */
-	int	password_days;		/* Days to password expiry */
-	int	numgroups;		/* (internal) size of default_group array */
-};
-
 #define	_DEF_DIRMODE	(S_IRWXU | S_IRWXG | S_IRWXO)
 #define _PATH_PW_CONF	"/etc/pw.conf"
 #define _UC_MAXLINE	1024
@@ -106,8 +82,8 @@ int write_userconfig(char const * file);
 struct carg *addarg(struct cargs * _args, int ch, char *argstr);
 struct carg *getarg(struct cargs * _args, int ch);
 
-int pw_user(struct userconf * cnf, int mode, struct cargs * _args);
-int pw_group(struct userconf * cnf, int mode, struct cargs * _args);
+int pw_user(int mode, struct cargs * _args);
+int pw_group(int mode, struct cargs * _args);
 char *pw_checkname(char *name, int gecos);
 
 int addnispwent(const char *path, struct passwd *pwd);

Modified: head/usr.sbin/pw/pw_group.c
==============================================================================
--- head/usr.sbin/pw/pw_group.c	Sun Jun  7 14:32:52 2015	(r284117)
+++ head/usr.sbin/pw/pw_group.c	Sun Jun  7 14:34:38 2015	(r284118)
@@ -48,7 +48,7 @@ static int      print_group(struct group
 static gid_t    gr_gidpolicy(struct userconf * cnf, struct cargs * args);
 
 int
-pw_group(struct userconf * cnf, int mode, struct cargs * args)
+pw_group(int mode, struct cargs * args)
 {
 	int		rc;
 	struct carg    *a_newname = getarg(args, 'l');
@@ -58,6 +58,7 @@ pw_group(struct userconf * cnf, int mode
 	struct group   *grp = NULL;
 	int	        grmembers = 0;
 	char           **members = NULL;
+	struct userconf	*cnf = conf.userconf;
 
 	static struct group fakegroup =
 	{

Modified: head/usr.sbin/pw/pw_user.c
==============================================================================
--- head/usr.sbin/pw/pw_user.c	Sun Jun  7 14:32:52 2015	(r284117)
+++ head/usr.sbin/pw/pw_user.c	Sun Jun  7 14:34:38 2015	(r284118)
@@ -55,7 +55,7 @@ static int	delete_user(struct userconf *
 		    struct carg *a_name, int delete, int mode);
 static int      print_user(struct passwd * pwd, int pretty, int v7);
 static uid_t    pw_uidpolicy(struct userconf * cnf, struct cargs * args);
-static uid_t    pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
+static uid_t    pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer);
 static time_t   pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
 static time_t   pw_exppolicy(struct userconf * cnf, struct cargs * args);
 static char    *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
@@ -66,19 +66,18 @@ static void     rmat(uid_t uid);
 static void     rmopie(char const * name);
 
 static void
-create_and_populate_homedir(int mode, struct cargs *args, struct passwd *pwd,
-    struct userconf *cnf)
+create_and_populate_homedir(int mode, struct passwd *pwd)
 {
 	char *homedir, *dotdir;
-	struct carg	*arg;
+	struct userconf *cnf = conf.userconf;
 
 	homedir = dotdir = NULL;
 
-	if ((arg = getarg(args, 'R'))) {
-		asprintf(&homedir, "%s/%s", arg->val, pwd->pw_dir);
+	if (conf.rootdir[0] != '\0') {
+		asprintf(&homedir, "%s/%s", conf.rootdir, pwd->pw_dir);
 		if (homedir == NULL)
 			errx(EX_OSERR, "out of memory");
-		asprintf(&dotdir, "%s/%s", arg->val, cnf->dotdir);
+		asprintf(&dotdir, "%s/%s", conf.rootdir, cnf->dotdir);
 	}
 
 	copymkdir(homedir ? homedir : pwd->pw_dir, dotdir ? dotdir: cnf->dotdir,
@@ -120,7 +119,7 @@ create_and_populate_homedir(int mode, st
  */
 
 int
-pw_user(struct userconf * cnf, int mode, struct cargs * args)
+pw_user(int mode, struct cargs * args)
 {
 	int	        rc, edited = 0;
 	char           *p = NULL;
@@ -131,6 +130,7 @@ pw_user(struct userconf * cnf, int mode,
 	struct passwd  *pwd = NULL;
 	struct group   *grp;
 	struct stat     st;
+	struct userconf	*cnf;
 	char            line[_PASSWORD_LEN+1];
 	char		path[MAXPATHLEN];
 	FILE	       *fp;
@@ -154,6 +154,7 @@ pw_user(struct userconf * cnf, int mode,
 #endif
 	};
 
+	cnf = conf.userconf;
 
 	/*
 	 * With M_NEXT, we only need to return the
@@ -165,7 +166,7 @@ pw_user(struct userconf * cnf, int mode,
 		if (getarg(args, 'q'))
 			return next;
 		printf("%u:", next);
-		pw_group(cnf, mode, args);
+		pw_group(mode, args);
 		return EXIT_SUCCESS;
 	}
 
@@ -528,7 +529,7 @@ pw_user(struct userconf * cnf, int mode,
 		pwd->pw_name = a_name->val;
 		pwd->pw_class = cnf->default_class ? cnf->default_class : "";
 		pwd->pw_uid = pw_uidpolicy(cnf, args);
-		pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
+		pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid);
 		pwd->pw_change = pw_pwdpolicy(cnf, args);
 		pwd->pw_expire = pw_exppolicy(cnf, args);
 		pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
@@ -740,7 +741,7 @@ pw_user(struct userconf * cnf, int mode,
 	 */
 	if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir &&
 	    *pwd->pw_dir == '/' && pwd->pw_dir[1])
-		create_and_populate_homedir(mode, args, pwd, cnf);
+		create_and_populate_homedir(mode, pwd);
 
 	/*
 	 * Finally, send mail to the new user as well, if we are asked to
@@ -824,11 +825,12 @@ pw_uidpolicy(struct userconf * cnf, stru
 
 
 static          uid_t
-pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
+pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer)
 {
 	struct group   *grp;
 	gid_t           gid = (uid_t) - 1;
 	struct carg    *a_gid = getarg(args, 'g');
+	struct userconf	*cnf = conf.userconf;
 
 	/*
 	 * If no arg given, see if default can help out
@@ -874,11 +876,11 @@ pw_gidpolicy(struct userconf * cnf, stru
 		{
 			addarg(&grpargs, 'N', NULL);
 			addarg(&grpargs, 'q', NULL);
-			gid = pw_group(cnf, M_NEXT, &grpargs);
+			gid = pw_group(M_NEXT, &grpargs);
 		}
 		else
 		{
-			pw_group(cnf, M_ADD, &grpargs);
+			pw_group(M_ADD, &grpargs);
 			if ((grp = GETGRNAM(nam)) != NULL)
 				gid = grp->gr_gid;
 		}

Modified: head/usr.sbin/pw/pwupd.c
==============================================================================
--- head/usr.sbin/pw/pwupd.c	Sun Jun  7 14:32:52 2015	(r284117)
+++ head/usr.sbin/pw/pwupd.c	Sun Jun  7 14:34:38 2015	(r284118)
@@ -44,28 +44,12 @@ static const char rcsid[] =
 
 #include "pwupd.h"
 
-static char pathpwd[] = _PATH_PWD;
-static char * pwpath = pathpwd;
- 
-int
-setpwdir(const char * dir)
-{
-	if (dir == NULL)
-		return (-1);
-	else
-		pwpath = strdup(dir);
-	if (pwpath == NULL)
-		return (-1);
-
-	return (0);
-}
-
 char *
 getpwpath(char const * file)
 {
 	static char pathbuf[MAXPATHLEN];
 
-	snprintf(pathbuf, sizeof pathbuf, "%s/%s", pwpath, file);
+	snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file);
 
 	return (pathbuf);
 }
@@ -80,9 +64,9 @@ pwdb_check(void)
 	args[i++] = _PATH_PWD_MKDB;
 	args[i++] = "-C";
 
-	if (pwpath != pathpwd) {
+	if (strcmp(conf.etcpath, _PATH_PWD) != 0) {
 		args[i++] = "-d";
-		args[i++] = pwpath;
+		args[i++] = conf.etcpath;
 	}
 	args[i++] = getpwpath(_MASTERPASSWD);
 	args[i] = NULL;
@@ -117,7 +101,7 @@ pw_update(struct passwd * pwd, char cons
 	if (user != NULL)
 		old_pw = GETPWNAM(user);
 
-	if (pw_init(pwpath, NULL))
+	if (pw_init(conf.etcpath, NULL))
 		err(1, "pw_init()");
 	if ((pfd = pw_lock()) == -1) {
 		pw_fini();

Modified: head/usr.sbin/pw/pwupd.h
==============================================================================
--- head/usr.sbin/pw/pwupd.h	Sun Jun  7 14:32:52 2015	(r284117)
+++ head/usr.sbin/pw/pwupd.h	Sun Jun  7 14:34:38 2015	(r284118)
@@ -29,6 +29,7 @@
 #ifndef _PWUPD_H_
 #define _PWUPD_H_
 
+#include <sys/param.h>
 #include <sys/types.h>
 #include <pwd.h>
 #include <grp.h>
@@ -41,8 +42,7 @@
 #define	RET_SETGRENT	void
 #endif
 
-struct pwf
-{
+struct pwf {
 	int		    _altdir;
 	void		  (*_setpwent)(void);
 	void		  (*_endpwent)(void);
@@ -56,8 +56,38 @@ struct pwf
 	struct group  * (*_getgrnam)(const char * nam);
 };
 
+struct userconf {
+	int	default_password;	/* Default password for new users? */
+	int	reuse_uids;		/* Reuse uids? */
+	int	reuse_gids;		/* Reuse gids? */
+	char	*nispasswd;		/* Path to NIS version of the passwd file */
+	char	*dotdir;		/* Where to obtain skeleton files */
+	char	*newmail;		/* Mail to send to new accounts */
+	char	*logfile;		/* Where to log changes */
+	char	*home;			/* Where to create home directory */
+	mode_t	homemode;		/* Home directory permissions */
+	char	*shelldir;		/* Where shells are located */
+	char	**shells;		/* List of shells */
+	char	*shell_default;		/* Default shell */
+	char	*default_group;		/* Default group number */
+	char	**groups;		/* Default (additional) groups */
+	char	*default_class;		/* Default user class */
+	uid_t	min_uid, max_uid;	/* Allowed range of uids */
+	gid_t	min_gid, max_gid;	/* Allowed range of gids */
+	int	expire_days;		/* Days to expiry */
+	int	password_days;		/* Days to password expiry */
+	int	numgroups;		/* (internal) size of default_group array */
+};
+
+struct pwconf {
+	char		 rootdir[MAXPATHLEN];
+	char		 etcpath[MAXPATHLEN];
+	struct userconf	*userconf;
+};
+
 extern struct pwf PWF;
 extern struct pwf VPWF;
+extern struct pwconf conf;
 
 #define SETPWENT()	PWF._setpwent()
 #define ENDPWENT()	PWF._endpwent()
@@ -91,14 +121,12 @@ int addpwent(struct passwd * pwd);
 int delpwent(struct passwd * pwd);
 int chgpwent(char const * login, struct passwd * pwd);
 
-int setpwdir(const char * dir);
 char * getpwpath(char const * file);
 
 int addgrent(struct group * grp);
 int delgrent(struct group * grp);
 int chggrent(char const * name, struct group * grp);
 
-int setgrdir(const char * dir);
 char * getgrpath(const char *file);
 
 void vsetpwent(void);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201506071434.t57EYcK0091841>