summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBaptiste Daroussin <bapt@FreeBSD.org>2015-07-11 23:07:17 +0000
committerBaptiste Daroussin <bapt@FreeBSD.org>2015-07-11 23:07:17 +0000
commit37cf831c25d3fbcc63a8548fc4ed7ead82562c5b (patch)
tree65798e51a2ab7ec3c658a3d7d078d80f7c532ff9
parentffa872f6d718f788860c825c902727e81fa0ebca (diff)
downloadpw-darwin-37cf831c25d3fbcc63a8548fc4ed7ead82562c5b.tar.gz
pw-darwin-37cf831c25d3fbcc63a8548fc4ed7ead82562c5b.tar.zst
pw-darwin-37cf831c25d3fbcc63a8548fc4ed7ead82562c5b.zip
Replace custom string array with stringlist(3)
-rw-r--r--pw/Makefile3
-rw-r--r--pw/fileupd.c47
-rw-r--r--pw/pw_conf.c23
-rw-r--r--pw/pw_user.c16
-rw-r--r--pw/pwupd.h41
5 files changed, 35 insertions, 95 deletions
diff --git a/pw/Makefile b/pw/Makefile
index 4bcf691..c265399 100644
--- a/pw/Makefile
+++ b/pw/Makefile
@@ -3,8 +3,7 @@
PROG= pw
MAN= pw.conf.5 pw.8
SRCS= pw.c pw_conf.c pw_user.c pw_group.c pw_log.c pw_nis.c pw_vpw.c \
- grupd.c pwupd.c fileupd.c psdate.c \
- bitmap.c cpdir.c rm_r.c
+ grupd.c pwupd.c psdate.c bitmap.c cpdir.c rm_r.c
WARNS?= 3
diff --git a/pw/fileupd.c b/pw/fileupd.c
deleted file mode 100644
index dc32712..0000000
--- a/pw/fileupd.c
+++ /dev/null
@@ -1,47 +0,0 @@
-/*-
- * Copyright (C) 1996
- * David L. Nugent. 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.
- *
- * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT 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 DAVID L. NUGENT 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
-static const char rcsid[] =
- "$FreeBSD$";
-#endif /* not lint */
-
-#include <stdlib.h>
-
-#include "pwupd.h"
-
-int
-extendarray(char ***buf, int * buflen, int needed)
-{
- if (needed > *buflen) {
- char **tmp = realloc(*buf, needed * sizeof(char *));
- if (tmp == NULL)
- return -1;
- *buf = tmp;
- *buflen = needed;
- }
- return *buflen;
-}
diff --git a/pw/pw_conf.c b/pw/pw_conf.c
index 24c0650..33bb6b3 100644
--- a/pw/pw_conf.c
+++ b/pw/pw_conf.c
@@ -104,8 +104,7 @@ static struct userconf config =
1000, 32000, /* Allowed range of uids */
1000, 32000, /* Allowed range of gids */
0, /* Days until account expires */
- 0, /* Days until password expires */
- 0 /* size of default_group array */
+ 0 /* Days until password expires */
};
static char const *comments[_UC_FIELDS] =
@@ -234,10 +233,9 @@ read_userconfig(char const * file)
buf = NULL;
linecap = 0;
- config.numgroups = 200;
- config.groups = calloc(config.numgroups, sizeof(char *));
+ config.groups = sl_init();
if (config.groups == NULL)
- err(1, "calloc()");
+ err(1, "sl_init()");
if (file == NULL)
file = _PATH_PW_CONF;
@@ -316,13 +314,8 @@ read_userconfig(char const * file)
? NULL : newstr(q);
break;
case _UC_EXTRAGROUPS:
- for (i = 0; q != NULL; q = strtok(NULL, toks)) {
- if (extendarray(&config.groups, &config.numgroups, i + 2) != -1)
- config.groups[i++] = newstr(q);
- }
- if (i > 0)
- while (i < config.numgroups)
- config.groups[i++] = NULL;
+ for (i = 0; q != NULL; q = strtok(NULL, toks))
+ sl_add(config.groups, newstr(q));
break;
case _UC_DEFAULTCLASS:
config.default_class = (q == NULL || !boolean_val(q, 1))
@@ -442,10 +435,10 @@ write_userconfig(char const * file)
config.default_group : "");
break;
case _UC_EXTRAGROUPS:
- for (j = 0; j < config.numgroups &&
- config.groups[j] != NULL; j++)
+ for (j = 0; config.groups != NULL &&
+ j < (int)config.groups->sl_cur; j++)
sbuf_printf(buf, "%s\"%s\"", j ?
- "," : "", config.groups[j]);
+ "," : "", config.groups->sl_str[j]);
quote = 0;
break;
case _UC_DEFAULTCLASS:
diff --git a/pw/pw_user.c b/pw/pw_user.c
index 6aa0c47..f1dae74 100644
--- a/pw/pw_user.c
+++ b/pw/pw_user.c
@@ -440,18 +440,13 @@ pw_user(int mode, char *name, long id, struct cargs * args)
cnf->default_class = pw_checkname(arg->val, 0);
if ((arg = getarg(args, 'G')) != NULL && arg->val) {
- int i = 0;
-
for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
if ((grp = GETGRNAM(p)) == NULL) {
if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
errx(EX_NOUSER, "group `%s' does not exist", p);
}
- if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
- cnf->groups[i++] = newstr(grp->gr_name);
+ sl_add(cnf->groups, newstr(grp->gr_name));
}
- while (i < cnf->numgroups)
- cnf->groups[i++] = NULL;
}
if ((arg = getarg(args, 'k')) != NULL) {
@@ -690,7 +685,8 @@ pw_user(int mode, char *name, long id, struct cargs * args)
*/
if (mode == M_ADD || getarg(args, 'G') != NULL) {
- int i, j;
+ int j;
+ size_t i;
/* First remove the user from all group */
SETGRENT();
while ((grp = GETGRENT()) != NULL) {
@@ -709,8 +705,8 @@ pw_user(int mode, char *name, long id, struct cargs * args)
ENDGRENT();
/* now add to group where needed */
- for (i = 0; cnf->groups[i] != NULL; i++) {
- grp = GETGRNAM(cnf->groups[i]);
+ for (i = 0; i < cnf->groups->sl_cur; i++) {
+ grp = GETGRNAM(cnf->groups->sl_str[i]);
grp = gr_add(grp, pwd->pw_name);
/*
* grp can only be NULL in 2 cases:
@@ -720,7 +716,7 @@ pw_user(int mode, char *name, long id, struct cargs * args)
*/
if (grp == NULL)
continue;
- chggrent(cnf->groups[i], grp);
+ chggrent(grp->gr_name, grp);
free(grp);
}
}
diff --git a/pw/pwupd.h b/pw/pwupd.h
index 7a0cd2d..92e5548 100644
--- a/pw/pwupd.h
+++ b/pw/pwupd.h
@@ -36,6 +36,7 @@
#include <pwd.h>
#include <grp.h>
#include <stdbool.h>
+#include <stringlist.h>
#if defined(__FreeBSD__)
#define RET_SETGRENT int
@@ -58,26 +59,25 @@ struct pwf {
};
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 */
+ 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 */
+ StringList *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 */
};
struct pwconf {
@@ -158,7 +158,6 @@ void vendgrent(void);
void copymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid);
void rm_r(char const * dir, uid_t uid);
-int extendarray(char ***buf, int *buflen, int needed);
__END_DECLS
#endif /* !_PWUPD_H */