summaryrefslogtreecommitdiff
path: root/loginutils/adduser.c
diff options
context:
space:
mode:
authorDenis Vlasenko2009-04-22 21:35:52 +0000
committerDenis Vlasenko2009-04-22 21:35:52 +0000
commitc2931aa2df2ddc6a627f3c40eb1cf5c221067092 (patch)
tree8b4b92c8c5bea933f7ea68302328bd17c0610b4a /loginutils/adduser.c
parentc8d7109f6036fab6b5993c24fc6514ba0c6a7d3d (diff)
downloadbusybox-c2931aa2df2ddc6a627f3c40eb1cf5c221067092.zip
busybox-c2931aa2df2ddc6a627f3c40eb1cf5c221067092.tar.gz
adduser/addgroup: support specifying uid/gid, add system
account creation mode. By Tito. function old new delta adduser_main 650 726 +76 addgroup_main 341 402 +61 addgroup_longopts - 16 +16 adduser_longopts 97 103 +6 packed_usage 26161 26163 +2 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 4/0 up/down: 161/0) Total: 161 bytes
Diffstat (limited to 'loginutils/adduser.c')
-rw-r--r--loginutils/adduser.c82
1 files changed, 60 insertions, 22 deletions
diff --git a/loginutils/adduser.c b/loginutils/adduser.c
index df4fad6..a399d9e 100644
--- a/loginutils/adduser.c
+++ b/loginutils/adduser.c
@@ -9,38 +9,55 @@
*/
#include "libbb.h"
+/* #define OPT_HOME (1 << 0) */ /* unused */
+/* #define OPT_GECOS (1 << 1) */ /* unused */
+#define OPT_SHELL (1 << 2)
+#define OPT_GID (1 << 3)
#define OPT_DONT_SET_PASS (1 << 4)
#define OPT_SYSTEM_ACCOUNT (1 << 5)
#define OPT_DONT_MAKE_HOME (1 << 6)
+#define OPT_UID (1 << 7)
+/* We assume UID_T_MAX == INT_MAX */
/* remix */
/* recoded such that the uid may be passed in *p */
static void passwd_study(struct passwd *p)
{
- int max;
+ int max = UINT_MAX;
- if (getpwnam(p->pw_name))
- bb_error_msg_and_die("login '%s' is in use", p->pw_name);
-
- if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
- p->pw_uid = 0;
- max = 999;
- } else {
- p->pw_uid = 1000;
- max = 64999;
+ if (getpwnam(p->pw_name)) {
+ bb_error_msg_and_die("%s '%s' in use", "user", p->pw_name);
+ /* this format string is reused in adduser and addgroup */
}
+ if (!(option_mask32 & OPT_UID)) {
+ if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
+ p->pw_uid = 100; /* FIRST_SYSTEM_UID */
+ max = 999; /* LAST_SYSTEM_UID */
+ } else {
+ p->pw_uid = 1000; /* FIRST_UID */
+ max = 64999; /* LAST_UID */
+ }
+ }
/* check for a free uid (and maybe gid) */
while (getpwuid(p->pw_uid) || (p->pw_gid == (gid_t)-1 && getgrgid(p->pw_uid))) {
+ if (option_mask32 & OPT_UID) {
+ /* -u N, cannot pick uid other than N: error */
+ bb_error_msg_and_die("%s '%s' in use", "uid", itoa(p->pw_uid));
+ /* this format string is reused in adduser and addgroup */
+ }
+ if (p->pw_uid == max) {
+ bb_error_msg_and_die("no %cids left", 'u');
+ }
p->pw_uid++;
- if (p->pw_uid > max)
- bb_error_msg_and_die("no free uids left");
}
if (p->pw_gid == (gid_t)-1) {
p->pw_gid = p->pw_uid; /* new gid = uid */
- if (getgrnam(p->pw_name))
- bb_error_msg_and_die("group name '%s' is in use", p->pw_name);
+ if (getgrnam(p->pw_name)) {
+ bb_error_msg_and_die("%s '%s' in use", "group", p->pw_name);
+ /* this format string is reused in adduser and addgroup */
+ }
}
}
@@ -73,6 +90,7 @@ static const char adduser_longopts[] ALIGN1 =
"empty-password\0" No_argument "D"
"system\0" No_argument "S"
"no-create-home\0" No_argument "H"
+ "uid\0" Required_argument "u"
;
#endif
@@ -87,6 +105,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
struct passwd pw;
const char *usegroup = NULL;
char *p;
+ unsigned opts;
#if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
applet_long_options = adduser_longopts;
@@ -102,8 +121,17 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
pw.pw_dir = NULL;
/* exactly one non-option arg */
- opt_complementary = "=1";
- getopt32(argv, "h:g:s:G:DSH", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup);
+ /* disable interactive passwd for system accounts */
+ opt_complementary = "=1:SD:u+";
+ if (sizeof(pw.pw_uid) == sizeof(int)) {
+ opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid);
+ } else {
+ unsigned uid;
+ opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid);
+ if (opts & OPT_UID) {
+ pw.pw_uid = uid;
+ }
+ }
argv += optind;
/* fill in the passwd struct */
@@ -114,12 +142,22 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
pw.pw_dir = xasprintf("/home/%s", argv[0]);
}
pw.pw_passwd = (char *)"x";
+ if (opts & OPT_SYSTEM_ACCOUNT) {
+ if (!usegroup) {
+ usegroup = "nogroup";
+ }
+ if (!(opts & OPT_SHELL)) {
+ pw.pw_shell = (char *) "/bin/false";
+ }
+ }
pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */
/* make sure everything is kosher and setup uid && maybe gid */
passwd_study(&pw);
- p = xasprintf("x:%u:%u:%s:%s:%s", pw.pw_uid, pw.pw_gid, pw.pw_gecos, pw.pw_dir, pw.pw_shell);
+ p = xasprintf("x:%u:%u:%s:%s:%s",
+ (unsigned) pw.pw_uid, (unsigned) pw.pw_gid,
+ pw.pw_gecos, pw.pw_dir, pw.pw_shell);
if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) {
return EXIT_FAILURE;
}
@@ -143,10 +181,10 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
/* clear the umask for this process so it doesn't
* screw up the permissions on the mkdir and chown. */
umask(0);
- if (!(option_mask32 & OPT_DONT_MAKE_HOME)) {
- /* Set the owner and group so it is owned by the new user,
- then fix up the permissions to 2755. Can't do it before
- since chown will clear the setgid bit */
+ if (!(opts & OPT_DONT_MAKE_HOME)) {
+ /* set the owner and group so it is owned by the new user,
+ * then fix up the permissions to 2755. Can't do it before
+ * since chown will clear the setgid bit */
if (mkdir(pw.pw_dir, 0755)
|| chown(pw.pw_dir, pw.pw_uid, pw.pw_gid)
|| chmod(pw.pw_dir, 02755) /* set setgid bit on homedir */
@@ -155,7 +193,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
}
}
- if (!(option_mask32 & OPT_DONT_SET_PASS)) {
+ if (!(opts & OPT_DONT_SET_PASS)) {
/* interactively set passwd */
passwd_wrapper(pw.pw_name);
}