add --prefix option

This commit is contained in:
fariouche
2016-05-15 15:49:39 +02:00
parent db57db52cf
commit b6b2c756c9
20 changed files with 766 additions and 93 deletions

View File

@ -115,6 +115,10 @@ static const char *user_comment = "";
static const char *user_home = "";
static const char *user_shell = "";
static const char *create_mail_spool = "";
static const char *prefix = "";
static const char *prefix_user_home = NULL;
#ifdef WITH_SELINUX
static /*@notnull@*/const char *user_selinux = "";
#endif /* WITH_SELINUX */
@ -226,11 +230,11 @@ static void create_mail (void);
static void fail_exit (int code)
{
if (home_added) {
if (rmdir (user_home) != 0) {
if (rmdir (prefix_user_home) != 0) {
fprintf (stderr,
_("%s: %s was created, but could not be removed\n"),
Prog, user_home);
SYSLOG ((LOG_ERR, "failed to remove %s", user_home));
Prog, prefix_user_home);
SYSLOG ((LOG_ERR, "failed to remove %s", prefix_user_home));
}
}
@ -339,14 +343,25 @@ static void fail_exit (int code)
static void get_defaults (void)
{
FILE *fp;
char* default_file = USER_DEFAULTS_FILE;
char buf[1024];
char *cp;
if(prefix[0]) {
size_t len;
int wlen;
len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
default_file = malloc(len);
wlen = snprintf(default_file, len, "%s/%s", prefix, USER_DEFAULTS_FILE);
assert (wlen == (int) len -1);
}
/*
* Open the defaults file for reading.
*/
fp = fopen (USER_DEFAULTS_FILE, "r");
fp = fopen (default_file, "r");
if (NULL == fp) {
return;
}
@ -372,14 +387,14 @@ static void get_defaults (void)
* Primary GROUP identifier
*/
if (MATCH (buf, DGROUP)) {
const struct group *grp = getgr_nam_gid (cp);
const struct group *grp = prefix_getgr_nam_gid (cp);
if (NULL == grp) {
fprintf (stderr,
_("%s: group '%s' does not exist\n"),
Prog, cp);
fprintf (stderr,
_("%s: the %s configuration in %s will be ignored\n"),
Prog, DGROUP, USER_DEFAULTS_FILE);
Prog, DGROUP, default_file);
} else {
def_group = grp->gr_gid;
def_gname = xstrdup (grp->gr_name);
@ -411,7 +426,7 @@ static void get_defaults (void)
Prog, cp);
fprintf (stderr,
_("%s: the %s configuration in %s will be ignored\n"),
Prog, DINACT, USER_DEFAULTS_FILE);
Prog, DINACT, default_file);
def_inactive = -1;
}
}
@ -430,8 +445,21 @@ static void get_defaults (void)
if ('\0' == *cp) {
cp = SKEL_DIR; /* XXX warning: const */
}
if(prefix[0]) {
size_t len;
int wlen;
char* _def_template; /* avoid const warning */
def_template = xstrdup (cp);
len = strlen(prefix) + strlen(cp) + 2;
_def_template = xmalloc(len);
wlen = snprintf(_def_template, len, "%s/%s", prefix, cp);
assert (wlen == (int) len -1);
def_template = _def_template;
}
else {
def_template = xstrdup (cp);
}
}
/*
@ -446,6 +474,10 @@ static void get_defaults (void)
}
}
(void) fclose (fp);
if(prefix[0]) {
free(default_file);
}
}
/*
@ -477,7 +509,8 @@ static int set_defaults (void)
FILE *ifp;
FILE *ofp;
char buf[1024];
static char new_file[] = NEW_USER_FILE;
char* new_file = NEW_USER_FILE;
char* default_file = USER_DEFAULTS_FILE;
char *cp;
int ofd;
int wlen;
@ -489,6 +522,20 @@ static int set_defaults (void)
bool out_skel = false;
bool out_create_mail_spool = false;
if(prefix[0]) {
size_t len;
len = strlen(prefix) + strlen(NEW_USER_FILE) + 2;
new_file = malloc(len);
wlen = snprintf(new_file, len, "%s/%s", prefix, NEW_USER_FILE);
assert (wlen == (int) len -1);
len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
default_file = malloc(len);
wlen = snprintf(default_file, len, "%s/%s", prefix, USER_DEFAULTS_FILE);
assert (wlen == (int) len -1);
}
/*
* Create a temporary file to copy the new output to.
*/
@ -513,7 +560,7 @@ static int set_defaults (void)
* temporary file, using any new values. Each line is checked
* to insure that it is not output more than once.
*/
ifp = fopen (USER_DEFAULTS_FILE, "r");
ifp = fopen (default_file, "r");
if (NULL == ifp) {
fprintf (ofp, "# useradd defaults file\n");
goto skip;
@ -530,7 +577,7 @@ static int set_defaults (void)
if (feof (ifp) == 0) {
fprintf (stderr,
_("%s: line too long in %s: %s..."),
Prog, USER_DEFAULTS_FILE, buf);
Prog, default_file, buf);
(void) fclose (ifp);
return -1;
}
@ -602,10 +649,10 @@ static int set_defaults (void)
/*
* Rename the current default file to its backup name.
*/
wlen = snprintf (buf, sizeof buf, "%s-", USER_DEFAULTS_FILE);
wlen = snprintf (buf, sizeof buf, "%s-", default_file);
assert (wlen < (int) sizeof buf);
unlink (buf);
if ((link (USER_DEFAULTS_FILE, buf) != 0) && (ENOENT != errno)) {
if ((link (default_file, buf) != 0) && (ENOENT != errno)) {
int err = errno;
fprintf (stderr,
_("%s: Cannot create backup file (%s): %s\n"),
@ -617,7 +664,7 @@ static int set_defaults (void)
/*
* Rename the new default file to its correct name.
*/
if (rename (new_file, USER_DEFAULTS_FILE) != 0) {
if (rename (new_file, default_file) != 0) {
int err = errno;
fprintf (stderr,
_("%s: rename: %s: %s\n"),
@ -636,6 +683,12 @@ static int set_defaults (void)
(unsigned int) def_group, def_home, def_shell,
def_inactive, def_expire, def_template,
def_create_mail_spool));
if(prefix[0]) {
free(new_file);
free(default_file);
}
return 0;
}
@ -675,7 +728,7 @@ static int get_groups (char *list)
* Names starting with digits are treated as numerical
* GID values, otherwise the string is looked up as is.
*/
grp = getgr_nam_gid (list);
grp = prefix_getgr_nam_gid (list);
/*
* There must be a match, either by GID value or by
@ -775,6 +828,7 @@ static void usage (int status)
(void) fputs (_(" -p, --password PASSWORD encrypted password of the new account\n"), usageout);
(void) fputs (_(" -r, --system create a system account\n"), usageout);
(void) fputs (_(" -R, --root CHROOT_DIR directory to chroot into\n"), usageout);
(void) fputs (_(" -P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files\n"), usageout);
(void) fputs (_(" -s, --shell SHELL login shell of the new account\n"), usageout);
(void) fputs (_(" -u, --uid UID user ID of the new account\n"), usageout);
(void) fputs (_(" -U, --user-group create a group with the same name as the user\n"), usageout);
@ -1049,6 +1103,7 @@ static void process_flags (int argc, char **argv)
{"password", required_argument, NULL, 'p'},
{"system", no_argument, NULL, 'r'},
{"root", required_argument, NULL, 'R'},
{"prefix", required_argument, NULL, 'P'},
{"shell", required_argument, NULL, 's'},
{"uid", required_argument, NULL, 'u'},
{"user-group", no_argument, NULL, 'U'},
@ -1059,9 +1114,9 @@ static void process_flags (int argc, char **argv)
};
while ((c = getopt_long (argc, argv,
#ifdef WITH_SELINUX
"b:c:d:De:f:g:G:hk:K:lmMNop:rR:s:u:UZ:",
"b:c:d:De:f:g:G:hk:K:lmMNop:rR:P:s:u:UZ:",
#else /* !WITH_SELINUX */
"b:c:d:De:f:g:G:hk:K:lmMNop:rR:s:u:U",
"b:c:d:De:f:g:G:hk:K:lmMNop:rR:P:s:u:U",
#endif /* !WITH_SELINUX */
long_options, NULL)) != -1) {
switch (c) {
@ -1152,7 +1207,7 @@ static void process_flags (int argc, char **argv)
fflg = true;
break;
case 'g':
grp = getgr_nam_gid (optarg);
grp = prefix_getgr_nam_gid (optarg);
if (NULL == grp) {
fprintf (stderr,
_("%s: group '%s' does not exist\n"),
@ -1232,6 +1287,8 @@ static void process_flags (int argc, char **argv)
break;
case 'R': /* no-op, handled in process_root_flag () */
break;
case 'P': /* no-op, handled in process_prefix_flag () */
break;
case 's':
if ( ( !VALID (optarg) )
|| ( ('\0' != optarg[0])
@ -1261,6 +1318,12 @@ static void process_flags (int argc, char **argv)
break;
#ifdef WITH_SELINUX
case 'Z':
if (prefix[0]) {
fprintf (stderr,
_("%s: -Z cannot be used with --prefix\n"),
Prog);
exit (E_BAD_ARG);
}
if (is_selinux_enabled () > 0) {
user_selinux = optarg;
} else {
@ -1360,6 +1423,18 @@ static void process_flags (int argc, char **argv)
user_home = uh;
}
if(prefix[0]) {
size_t len = strlen(prefix) + strlen(user_home) + 2;
int wlen;
char* _prefix_user_home; /* to avoid const warning */
_prefix_user_home = xmalloc(len);
wlen = snprintf(_prefix_user_home, len, "%s/%s", prefix, user_home);
assert (wlen == (int) len -1);
prefix_user_home = _prefix_user_home;
}
else {
prefix_user_home = user_home;
}
}
if (!eflg) {
@ -1872,7 +1947,7 @@ static void usr_update (void)
* are left unchanged). --marekm
*/
/* local, no need for xgetpwuid */
if ((!lflg) && (getpwuid (user_id) == NULL)) {
if ((!lflg) && (prefix_getpwuid (user_id) == NULL)) {
faillog_reset (user_id);
lastlog_reset (user_id);
}
@ -1942,9 +2017,9 @@ static void usr_update (void)
*/
static void create_home (void)
{
if (access (user_home, F_OK) != 0) {
if (access (prefix_user_home, F_OK) != 0) {
#ifdef WITH_SELINUX
if (set_selinux_file_context (user_home) != 0) {
if (set_selinux_file_context (prefix_user_home) != 0) {
fprintf (stderr,
_("%s: cannot set SELinux context for home directory %s\n"),
Prog, user_home);
@ -1952,10 +2027,10 @@ static void create_home (void)
}
#endif
/* XXX - create missing parent directories. --marekm */
if (mkdir (user_home, 0) != 0) {
if (mkdir (prefix_user_home, 0) != 0) {
fprintf (stderr,
_("%s: cannot create directory %s\n"),
Prog, user_home);
Prog, prefix_user_home);
#ifdef WITH_AUDIT
audit_logger (AUDIT_ADD_USER, Prog,
"adding home directory",
@ -1964,8 +2039,8 @@ static void create_home (void)
#endif
fail_exit (E_HOMEDIR);
}
chown (user_home, user_id, user_gid);
chmod (user_home,
(void) chown (prefix_user_home, user_id, user_gid);
chmod (prefix_user_home,
0777 & ~getdef_num ("UMASK", GETDEF_DEFAULT_UMASK));
home_added = true;
#ifdef WITH_AUDIT
@ -2007,15 +2082,18 @@ static void create_mail (void)
if (NULL == spool) {
spool = "/var/mail";
}
file = alloca (strlen (spool) + strlen (user_name) + 2);
sprintf (file, "%s/%s", spool, user_name);
file = alloca (strlen (prefix) + strlen (spool) + strlen (user_name) + 2);
if(prefix[0])
sprintf (file, "%s/%s/%s", prefix, spool, user_name);
else
sprintf (file, "%s/%s", spool, user_name);
fd = open (file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0);
if (fd < 0) {
perror (_("Creating mailbox file"));
return;
}
gr = getgrnam ("mail"); /* local, no need for xgetgrnam */
gr = prefix_getgrnam ("mail"); /* local, no need for xgetgrnam */
if (NULL == gr) {
fputs (_("Group 'mail' not found. Creating the user mailbox file with 0600 mode.\n"),
stderr);
@ -2064,6 +2142,8 @@ int main (int argc, char **argv)
process_root_flag ("-R", argc, argv);
prefix = process_prefix_flag("-P", argc, argv);
OPENLOG ("useradd");
#ifdef WITH_AUDIT
audit_help_open ();
@ -2147,7 +2227,7 @@ int main (int argc, char **argv)
/*
* Start with a quick check to see if the user exists.
*/
if (getpwnam (user_name) != NULL) { /* local, no need for xgetpwnam */
if (prefix_getpwnam (user_name) != NULL) { /* local, no need for xgetpwnam */
fprintf (stderr, _("%s: user '%s' already exists\n"), Prog, user_name);
#ifdef WITH_AUDIT
audit_logger (AUDIT_ADD_USER, Prog,
@ -2166,7 +2246,7 @@ int main (int argc, char **argv)
*/
if (Uflg) {
/* local, no need for xgetgrnam */
if (getgrnam (user_name) != NULL) {
if (prefix_getgrnam (user_name) != NULL) {
fprintf (stderr,
_("%s: group %s exists - if you want to add this user to that group, use -g.\n"),
Prog, user_name);
@ -2201,7 +2281,7 @@ int main (int argc, char **argv)
fail_exit (E_UID_IN_USE);
}
} else {
if (getpwuid (user_id) != NULL) {
if (prefix_getpwuid (user_id) != NULL) {
fprintf (stderr,
_("%s: UID %lu is not unique\n"),
Prog, (unsigned long) user_id);
@ -2264,7 +2344,7 @@ int main (int argc, char **argv)
if (mflg) {
create_home ();
if (home_added) {
copy_tree (def_template, user_home, false, false,
copy_tree (def_template, prefix_user_home, false, false,
(uid_t)-1, user_id, (gid_t)-1, user_gid);
} else {
fprintf (stderr,