Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/loginutils/adduser.c
- Timestamp:
- Nov 4, 2007, 3:16:40 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.5/mindi-busybox/loginutils/adduser.c
r821 r1765 9 9 */ 10 10 11 #include <stdio.h> 12 #include <sys/types.h> 13 #include <string.h> 14 #include <unistd.h> 15 #include <time.h> 16 #include <getopt.h> 17 #include <sys/stat.h> 18 19 #include "busybox.h" 20 21 #define DONT_SET_PASS (1 << 4) 22 #define DONT_MAKE_HOME (1 << 6) 11 #include "libbb.h" 12 13 #define OPT_DONT_SET_PASS (1 << 4) 14 #define OPT_DONT_MAKE_HOME (1 << 6) 23 15 24 16 … … 27 19 static int passwd_study(const char *filename, struct passwd *p) 28 20 { 29 struct passwd *pw;21 enum { min = 500, max = 65000 }; 30 22 FILE *passwd; 31 32 const int min = 500; 33 const int max = 65000; 34 35 passwd = bb_xfopen(filename, "r"); 23 /* We are using reentrant fgetpwent_r() in order to avoid 24 * pulling in static buffers from libc (think static build here) */ 25 char buffer[256]; 26 struct passwd pw; 27 struct passwd *result; 28 29 passwd = xfopen(filename, "r"); 36 30 37 31 /* EDR if uid is out of bounds, set to min */ … … 43 37 * find free uid and gid; 44 38 */ 45 while ( (pw = fgetpwent(passwd))) {46 if (strcmp(pw ->pw_name, p->pw_name) == 0) {39 while (!fgetpwent_r(passwd, &pw, buffer, sizeof(buffer), &result)) { 40 if (strcmp(pw.pw_name, p->pw_name) == 0) { 47 41 /* return 0; */ 48 42 return 1; 49 43 } 50 if ((pw ->pw_uid >= p->pw_uid) && (pw->pw_uid < max)51 && (pw ->pw_uid >= min)) {52 p->pw_uid = pw ->pw_uid + 1;44 if ((pw.pw_uid >= p->pw_uid) && (pw.pw_uid < max) 45 && (pw.pw_uid >= min)) { 46 p->pw_uid = pw.pw_uid + 1; 53 47 } 54 48 } … … 79 73 char *cmd; 80 74 81 cmd = bb_xasprintf("addgroup -g %d \"%s\"", p->pw_gid, p->pw_name);75 cmd = xasprintf("addgroup -g %d \"%s\"", p->pw_gid, p->pw_name); 82 76 system(cmd); 83 77 free(cmd); … … 88 82 static void passwd_wrapper(const char *login) 89 83 { 90 static const char prog[] = "passwd"; 91 execlp(prog, prog, login, NULL); 92 bb_error_msg_and_die("Failed to execute '%s', you must set the password for '%s' manually", prog, login); 84 static const char prog[] ALIGN1 = "passwd"; 85 86 BB_EXECLP(prog, prog, login, NULL); 87 bb_error_msg_and_die("failed to execute '%s', you must set the password for '%s' manually", prog, login); 93 88 } 94 89 95 90 /* putpwent(3) remix */ 96 static int adduser(struct passwd *p , unsigned long flags)91 static int adduser(struct passwd *p) 97 92 { 98 93 FILE *file; … … 100 95 101 96 /* make sure everything is kosher and setup uid && gid */ 102 file = bb_xfopen(bb_path_passwd_file, "a");97 file = xfopen(bb_path_passwd_file, "a"); 103 98 fseek(file, 0, SEEK_END); 104 99 … … 110 105 case 3: 111 106 bb_error_msg_and_die("%s: group name already in use", p->pw_name); 112 107 } 113 108 114 109 /* add to passwd */ … … 116 111 bb_perror_nomsg_and_die(); 117 112 } 113 /* Do fclose even if !ENABLE_FEATURE_CLEAN_UP. 114 * We will exec passwd, files must be flushed & closed before that! */ 118 115 fclose(file); 119 116 120 117 #if ENABLE_FEATURE_SHADOWPASSWDS 121 118 /* add to shadow if necessary */ 122 file = bb_xfopen(bb_path_shadow_file, "a"); 123 fseek(file, 0, SEEK_END); 124 fprintf(file, "%s:!:%ld:%d:%d:%d:::\n", 125 p->pw_name, /* username */ 126 time(NULL) / 86400, /* sp->sp_lstchg */ 127 0, /* sp->sp_min */ 128 99999, /* sp->sp_max */ 129 7); /* sp->sp_warn */ 130 fclose(file); 119 file = fopen_or_warn(bb_path_shadow_file, "a"); 120 if (file) { 121 fseek(file, 0, SEEK_END); 122 fprintf(file, "%s:!:%ld:%d:%d:%d:::\n", 123 p->pw_name, /* username */ 124 time(NULL) / 86400, /* sp->sp_lstchg */ 125 0, /* sp->sp_min */ 126 99999, /* sp->sp_max */ 127 7); /* sp->sp_warn */ 128 fclose(file); 129 } 131 130 #endif 132 131 … … 137 136 138 137 /* Clear the umask for this process so it doesn't 139 * *screw up the permissions on the mkdir and chown. */138 * screw up the permissions on the mkdir and chown. */ 140 139 umask(0); 141 if (!( flags &DONT_MAKE_HOME)) {140 if (!(option_mask32 & OPT_DONT_MAKE_HOME)) { 142 141 /* Set the owner and group so it is owned by the new user, 143 142 then fix up the permissions to 2755. Can't do it before … … 146 145 || chown(p->pw_dir, p->pw_uid, p->pw_gid) 147 146 || chmod(p->pw_dir, 02755)) { 148 149 150 } 151 152 if (!( flags &DONT_SET_PASS)) {147 bb_perror_msg("%s", p->pw_dir); 148 } 149 } 150 151 if (!(option_mask32 & OPT_DONT_SET_PASS)) { 153 152 /* interactively set passwd */ 154 153 passwd_wrapper(p->pw_name); … … 166 165 * 167 166 * can be customized via command-line parameters. 168 * ________________________________________________________________________ */ 167 */ 168 int adduser_main(int argc, char **argv); 169 169 int adduser_main(int argc, char **argv) 170 170 { 171 171 struct passwd pw; 172 172 const char *usegroup = NULL; 173 unsigned long flags; 174 175 pw.pw_gecos = "Linux User,,,"; 173 174 /* got root? */ 175 if (geteuid()) { 176 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); 177 } 178 179 pw.pw_gecos = (char *)"Linux User,,,"; 176 180 pw.pw_shell = (char *)DEFAULT_SHELL; 177 181 pw.pw_dir = NULL; 178 182 179 /* check for min, max and missing args and exit on error */ 180 bb_opt_complementally = "-1:?1:?"; 181 flags = bb_getopt_ulflags(argc, argv, "h:g:s:G:DSH", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup); 182 183 /* got root? */ 184 if(geteuid()) { 185 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); 186 } 187 188 /* create string for $HOME if not specified already */ 183 /* exactly one non-option arg */ 184 opt_complementary = "=1"; 185 getopt32(argv, "h:g:s:G:DSH", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup); 186 argv += optind; 187 188 /* create a passwd struct */ 189 pw.pw_name = argv[0]; 189 190 if (!pw.pw_dir) { 190 snprintf(bb_common_bufsiz1, BUFSIZ, "/home/%s", argv[optind]); 191 pw.pw_dir = &bb_common_bufsiz1[0]; 192 } 193 194 /* create a passwd struct */ 195 pw.pw_name = argv[optind]; 196 pw.pw_passwd = "x"; 191 /* create string for $HOME if not specified already */ 192 pw.pw_dir = xasprintf("/home/%s", argv[0]); 193 } 194 pw.pw_passwd = (char *)"x"; 197 195 pw.pw_uid = 0; 198 pw.pw_gid = (usegroup) ? bb_xgetgrnam(usegroup) : 0; /* exits on failure */196 pw.pw_gid = usegroup ? xgroup2gid(usegroup) : 0; /* exits on failure */ 199 197 200 198 /* grand finale */ 201 return adduser(&pw , flags);202 } 199 return adduser(&pw); 200 }
Note:
See TracChangeset
for help on using the changeset viewer.