Replace custom string array with stringlist(3)
This commit is contained in:
parent
5ebd525aab
commit
10e6a51cf3
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=285412
@ -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
|
||||
|
||||
|
@ -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;
|
||||
}
|
@ -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:
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user