Based on a tinylogin patch from Philip Blundell, add several
additional options to adduser. -Erik
This commit is contained in:
parent
9d9cecfd63
commit
f0f754aeaf
@ -10,9 +10,13 @@
|
|||||||
#define adduser_full_usage \
|
#define adduser_full_usage \
|
||||||
"Adds a user to the system" \
|
"Adds a user to the system" \
|
||||||
"Options:\n" \
|
"Options:\n" \
|
||||||
"\t-h\t\thome directory\n" \
|
"\t-h DIR\t\tAssign home directory DIR\n" \
|
||||||
"\t-s\t\tshell\n" \
|
"\t-g GECOS\t\tAssign gecos field GECOS\n" \
|
||||||
"\t-g\t\tGECOS string\n"
|
"\t-s SHELL\t\tAssign login shell SHELL\n" \
|
||||||
|
"\t-G\t\tAdd the user to existing group GROUP\n" \
|
||||||
|
"\t-S\t\tcreate a system user (ignored)\n" \
|
||||||
|
"\t-D\t\tDo not assign a password (logins still possible via ssh)\n" \
|
||||||
|
"\t-H\t\tDo not create the home directory\n"
|
||||||
|
|
||||||
#define adjtimex_trivial_usage \
|
#define adjtimex_trivial_usage \
|
||||||
"[-q] [-o offset] [-f frequency] [-p timeconstant] [-t tick]"
|
"[-q] [-o offset] [-f frequency] [-p timeconstant] [-t tick]"
|
||||||
|
@ -21,6 +21,9 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef _GNU_SOURCE
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
#endif
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
@ -29,6 +32,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <getopt.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -89,21 +93,23 @@ static int passwd_study(const char *filename, struct passwd *p)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* EDR check for an already existing gid */
|
if (p->pw_gid == 0) {
|
||||||
while (getgrgid(p->pw_uid) != NULL)
|
/* EDR check for an already existing gid */
|
||||||
p->pw_uid++;
|
while (getgrgid(p->pw_uid) != NULL)
|
||||||
|
p->pw_uid++;
|
||||||
|
|
||||||
/* EDR also check for an existing group definition */
|
/* EDR also check for an existing group definition */
|
||||||
if (getgrnam(p->pw_name) != NULL)
|
if (getgrnam(p->pw_name) != NULL)
|
||||||
return 3;
|
return 3;
|
||||||
|
|
||||||
|
/* EDR create new gid always = uid */
|
||||||
|
p->pw_gid = p->pw_uid;
|
||||||
|
}
|
||||||
|
|
||||||
/* EDR bounds check */
|
/* EDR bounds check */
|
||||||
if ((p->pw_uid > max) || (p->pw_uid < min))
|
if ((p->pw_uid > max) || (p->pw_uid < min))
|
||||||
return 2;
|
return 2;
|
||||||
|
|
||||||
/* EDR create new gid always = uid */
|
|
||||||
p->pw_gid = p->pw_uid;
|
|
||||||
|
|
||||||
/* return 1; */
|
/* return 1; */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -127,7 +133,7 @@ static void passwd_wrapper(const char *login)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* putpwent(3) remix */
|
/* putpwent(3) remix */
|
||||||
static int adduser(const char *filename, struct passwd *p)
|
static int adduser(const char *filename, struct passwd *p, int makehome, int setpass)
|
||||||
{
|
{
|
||||||
FILE *passwd;
|
FILE *passwd;
|
||||||
int r;
|
int r;
|
||||||
@ -135,6 +141,11 @@ static int adduser(const char *filename, struct passwd *p)
|
|||||||
FILE *shadow;
|
FILE *shadow;
|
||||||
struct spwd *sp;
|
struct spwd *sp;
|
||||||
#endif
|
#endif
|
||||||
|
int new_group = 1;
|
||||||
|
|
||||||
|
/* if using a pre-existing group, don't create one */
|
||||||
|
if (p->pw_gid != 0)
|
||||||
|
new_group = 0;
|
||||||
|
|
||||||
/* make sure everything is kosher and setup uid && gid */
|
/* make sure everything is kosher and setup uid && gid */
|
||||||
passwd = bb_wfopen(filename, "a");
|
passwd = bb_wfopen(filename, "a");
|
||||||
@ -181,29 +192,38 @@ static int adduser(const char *filename, struct passwd *p)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* add to group */
|
if (new_group) {
|
||||||
/* addgroup should be responsible for dealing w/ gshadow */
|
/* add to group */
|
||||||
addgroup_wrapper(p->pw_name, p->pw_gid);
|
/* addgroup should be responsible for dealing w/ gshadow */
|
||||||
|
addgroup_wrapper(p->pw_name, p->pw_gid);
|
||||||
|
}
|
||||||
|
|
||||||
/* Clear the umask for this process so it doesn't
|
/* Clear the umask for this process so it doesn't
|
||||||
* * screw up the permissions on the mkdir and chown. */
|
* * screw up the permissions on the mkdir and chown. */
|
||||||
umask(0);
|
umask(0);
|
||||||
|
|
||||||
/* mkdir */
|
if (makehome) {
|
||||||
if (mkdir(p->pw_dir, 0755)) {
|
/* mkdir */
|
||||||
bb_perror_msg("%s", p->pw_dir);
|
if (mkdir(p->pw_dir, 0755)) {
|
||||||
|
bb_perror_msg("%s", p->pw_dir);
|
||||||
|
}
|
||||||
|
/* Set the owner and group so it is owned by the new user. */
|
||||||
|
if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
|
||||||
|
bb_perror_msg("%s", p->pw_dir);
|
||||||
|
}
|
||||||
|
/* Now fix up the permissions to 2755. Can't do it before now
|
||||||
|
* since chown will clear the setgid bit */
|
||||||
|
if (chmod(p->pw_dir, 02755)) {
|
||||||
|
bb_perror_msg("%s", p->pw_dir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/* Set the owner and group so it is owned by the new user. */
|
|
||||||
if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
|
if (setpass) {
|
||||||
bb_perror_msg("%s", p->pw_dir);
|
/* interactively set passwd */
|
||||||
|
passwd_wrapper(p->pw_name);
|
||||||
}
|
}
|
||||||
/* Now fix up the permissions to 2755. Can't do it before now
|
|
||||||
* since chown will clear the setgid bit */
|
return 0;
|
||||||
if (chmod(p->pw_dir, 02755)) {
|
|
||||||
bb_perror_msg("%s", p->pw_dir);
|
|
||||||
}
|
|
||||||
/* interactively set passwd */
|
|
||||||
passwd_wrapper(p->pw_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -219,6 +239,9 @@ void if_i_am_not_root(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define SETPASS 1
|
||||||
|
#define MAKEHOME 4
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* adduser will take a login_name as its first parameter.
|
* adduser will take a login_name as its first parameter.
|
||||||
*
|
*
|
||||||
@ -230,19 +253,29 @@ void if_i_am_not_root(void)
|
|||||||
* ________________________________________________________________________ */
|
* ________________________________________________________________________ */
|
||||||
int adduser_main(int argc, char **argv)
|
int adduser_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
struct passwd pw;
|
||||||
const char *login;
|
const char *login;
|
||||||
const char *gecos = default_gecos;
|
const char *gecos = default_gecos;
|
||||||
const char *home = NULL;
|
const char *home = NULL;
|
||||||
const char *shell = default_shell;
|
const char *shell = default_shell;
|
||||||
|
const char *usegroup = NULL;
|
||||||
struct passwd pw;
|
int flags;
|
||||||
|
int setpass = 1;
|
||||||
|
int makehome = 1;
|
||||||
|
|
||||||
/* init */
|
/* init */
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
bb_show_usage();
|
bb_show_usage();
|
||||||
}
|
}
|
||||||
/* get args */
|
/* get args */
|
||||||
bb_getopt_ulflags(argc, argv, "h:g:s:", &home, &gecos, &shell);
|
flags = bb_getopt_ulflags(argc, argv, "h:g:s:G:DSH", &home, &gecos, &shell, &usegroup);
|
||||||
|
|
||||||
|
if (flags & SETPASS) {
|
||||||
|
setpass = 0;
|
||||||
|
}
|
||||||
|
if (flags & MAKEHOME) {
|
||||||
|
makehome = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* got root? */
|
/* got root? */
|
||||||
if_i_am_not_root();
|
if_i_am_not_root();
|
||||||
@ -271,6 +304,17 @@ int adduser_main(int argc, char **argv)
|
|||||||
pw.pw_dir = (char *)home;
|
pw.pw_dir = (char *)home;
|
||||||
pw.pw_shell = (char *)shell;
|
pw.pw_shell = (char *)shell;
|
||||||
|
|
||||||
|
if (usegroup) {
|
||||||
|
/* Add user to a group that already exists */
|
||||||
|
struct group *g;
|
||||||
|
|
||||||
|
g = getgrnam(usegroup);
|
||||||
|
if (g == NULL)
|
||||||
|
bb_error_msg_and_die("group %s does not exist", usegroup);
|
||||||
|
|
||||||
|
pw.pw_gid = g->gr_gid;
|
||||||
|
}
|
||||||
|
|
||||||
/* grand finale */
|
/* grand finale */
|
||||||
return adduser(bb_path_passwd_file, &pw);
|
return adduser(bb_path_passwd_file, &pw, makehome, setpass);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user