1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/*
* Copyright (C) 2001, 2002, 2004, 2006 Red Hat, Inc.
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <errno.h>
#include <inttypes.h>
#include <libintl.h>
#include <limits.h>
#include <locale.h>
#include <popt.h>
#include <stdlib.h>
#include <string.h>
#include "../lib/user.h"
#include "apputil.h"
int
main(int argc, const char **argv)
{
struct lu_context *ctx = NULL;
struct lu_error *error = NULL;
struct lu_ent *ent = NULL;
struct lu_ent *groupEnt = NULL;
int interactive = FALSE, nocreatehome = FALSE, nocreatemail = FALSE;
int c;
int result;
char *file = NULL;
FILE *fp = stdin;
char buf[LINE_MAX];
poptContext popt;
struct poptOption options[] = {
{"interactive", 'i', POPT_ARG_NONE, &interactive, 0,
N_("prompt for all information"), NULL},
{"file", 'f', POPT_ARG_STRING, &file, 0,
N_("file with user information records"), N_("PATH")},
{"nocreatehome", 'M', POPT_ARG_NONE, &nocreatehome, 0,
N_("don't create home directories"), NULL},
{"nocreatemail", 'n', POPT_ARG_NONE, &nocreatemail, 0,
N_("don't create mail spools"), NULL},
POPT_AUTOHELP
POPT_TABLEEND
};
/* Initialize i18n support. */
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
setlocale(LC_ALL, "");
/* Parse arguments. */
popt = poptGetContext("lnewusers", argc, argv, options, 0);
poptSetOtherOptionHelp(popt, _("[OPTION...]"));
c = poptGetNextOpt(popt);
if (c != -1) {
fprintf(stderr, _("Error parsing arguments: %s.\n"),
poptStrerror(c));
poptPrintUsage(popt, stderr, 0);
result = 1;
goto done;
}
/* Start up the library. */
ctx = lu_start(NULL, lu_user, NULL, NULL,
interactive ? lu_prompt_console :
lu_prompt_console_quiet, NULL, &error);
if (ctx == NULL) {
fprintf(stderr, _("Error initializing %s: %s.\n"), PACKAGE,
lu_strerror(error));
result = 1;
goto done;
}
/* Open the file we're going to look at. */
if (file != NULL) {
fp = fopen(file, "r");
if (fp == NULL) {
fprintf(stderr, _("Error opening `%s': %s.\n"),
file, strerror(errno));
result = 2;
goto done;
}
} else {
fp = stdin;
}
ent = lu_ent_new();
groupEnt = lu_ent_new();
while (fgets(buf, sizeof(buf), fp)) {
gboolean creategroup, dubious_homedir;
char **fields, *homedir, *gidstring, *p;
intmax_t imax;
uid_t uid;
gid_t gid;
/* Strip off the end-of-line terminators. */
p = strchr(buf, '\r');
if (p != NULL)
*p = '\0';
p = strchr(buf, '\n');
if (p != NULL)
*p = '\0';
/* Make sure the line splits into *exactly* seven fields. */
fields = g_strsplit(buf, ":", 7);
if (g_strv_length(fields) != 7) {
fprintf(stderr,
_("Error creating account for `%s': line "
"improperly formatted.\n"), buf);
g_strfreev(fields);
continue;
}
errno = 0;
imax = strtoimax(fields[2], &p, 10);
if (errno != 0 || *p != 0 || p == fields[2]
|| (uid_t)imax != imax
|| (uid_t)imax == LU_VALUE_INVALID_ID) {
g_print(_("Invalid user ID %s\n"), fields[2]);
g_strfreev(fields);
continue;
}
/* Sorry, but we're bastards here. No root accounts. */
uid = imax;
if (uid == 0) {
g_print(_("Refusing to create account with UID 0.\n"));
g_strfreev(fields);
continue;
}
/* Try to figure out if the field is the name of a group, or
* a gid. If it's just empty, make it the same as the user's
* name. FIXME: provide some way to set a default other than
* the user's own name, like "users" or something. */
if (strlen(fields[3]) > 0) {
gidstring = fields[3];
} else {
gidstring = fields[0];
}
/* Try to convert the field to a number. */
errno = 0;
imax = strtoimax(gidstring, &p, 10);
gid = LU_VALUE_INVALID_ID;
if (errno != 0 || *p != '\0' || p == gidstring
|| (gid_t)imax != imax) {
/* It's not a number, so it's a group name --
* see if it's being used. */
if (lu_group_lookup_name(ctx, gidstring, ent, &error)) {
/* Retrieve the group's GID. */
gid = lu_ent_get_first_id(ent, LU_GIDNUMBER);
creategroup = FALSE;
} else {
/* Mark that we need to create a group for the
* user to be in. */
creategroup = TRUE;
}
} else {
/* It's a group number -- see if it's being used. */
gid = imax;
if (gid == LU_VALUE_INVALID_ID) {
g_print(_("Invalid group ID %s\n"), gidstring);
g_strfreev(fields);
continue;
}
if (lu_group_lookup_id(ctx, gid, ent, &error)) {
/* Retrieve the group's GID. */
gid = lu_ent_get_first_id(ent, LU_GIDNUMBER);
creategroup = FALSE;
} else {
/* Mark that we need to create a group for the
* user to be in. */
creategroup = TRUE;
}
}
/* If we need to create a group, create a template group and
* try to apply what the user has asked us to. */
if (creategroup) {
/* If we got a GID, then we need to use the user's name,
* otherwise we need to use the default group name. */
if (gid != LU_VALUE_INVALID_ID) {
lu_group_default(ctx, fields[0], FALSE, ent);
lu_ent_set_id(ent, LU_GIDNUMBER, gid);
} else {
lu_group_default(ctx, gidstring, FALSE, ent);
}
/* Try to create the group, and if it works, get its
* GID, which we need to give to this user. */
if (lu_group_add(ctx, ent, &error)) {
lu_nscd_flush_cache(LU_NSCD_CACHE_GROUP);
gid = lu_ent_get_first_id(ent, LU_GIDNUMBER);
g_assert(gid != LU_VALUE_INVALID_ID);
} else {
/* Aargh! Abandon all hope. */
fprintf(stderr,
_("Error creating group for `%s' with "
"GID %jd: %s\n"), fields[0], imax,
lu_strerror(error));
g_strfreev(fields);
continue;
}
}
/* Create a new user record, and set the user's primary GID. */
lu_user_default(ctx, fields[0], FALSE, ent);
lu_ent_set_id(ent, LU_UIDNUMBER, uid);
lu_ent_set_id(ent, LU_GIDNUMBER, gid);
/* Set other fields if we've got them. */
if (strlen(fields[4]) > 0)
lu_ent_set_string(ent, LU_GECOS, fields[4]);
dubious_homedir = 0;
if (strlen(fields[5]) > 0) {
homedir = g_strdup(fields[5]);
lu_ent_set_string(ent, LU_HOMEDIRECTORY, homedir);
} else {
const char *home;
home = lu_ent_get_first_string(ent, LU_HOMEDIRECTORY);
if (home != NULL)
homedir = g_strdup(home);
else {
homedir = g_strconcat("/home/", fields[0],
(const gchar *)NULL);
if (strcmp(fields[0], ".") == 0
|| strcmp(fields[0], "..") == 0)
dubious_homedir = 1;
}
}
if (strlen(fields[6]) > 0)
lu_ent_set_string(ent, LU_LOGINSHELL, fields[6]);
/* Now try to add the user's account. */
if (dubious_homedir)
fprintf(stderr,
_("Refusing to use dangerous home directory `%s' "
"for %s by default\n"), homedir, fields[0]);
else if (lu_user_add(ctx, ent, &error)) {
lu_nscd_flush_cache(LU_NSCD_CACHE_PASSWD);
/* Unless the nocreatehomedirs flag was given, attempt
* to create the user's home directory. */
if (!nocreatehome) {
if (lu_homedir_populate(ctx, NULL, homedir,
uid, gid, 0700, &error)
== FALSE) {
fprintf(stderr,
_("Error creating home "
"directory for %s: %s\n"),
fields[0], lu_strerror(error));
if (error) {
lu_error_free(&error);
}
}
}
/* Unless the nocreatemail flag was given, give the
* user a mail spool. */
if (!nocreatemail) {
if (!lu_mail_spool_create(ctx, ent, &error)) {
fprintf(stderr,
_("Error creating mail spool "
"for %s: %s\n"), fields[0],
lu_strerror(error));
if (error) {
lu_error_free(&error);
}
}
}
/* Set the password after creating the home directory
to prevent the user from seeing an incomplete
home, and to prevent the user from interfering with
the creation of the home directory. */
if (!lu_user_setpass(ctx, ent, fields[1], FALSE,
&error)) {
fprintf(stderr,
_("Error setting initial password for "
"%s: %s\n"), fields[0],
lu_strerror(error));
if (error) {
lu_error_free(&error);
}
}
lu_nscd_flush_cache(LU_NSCD_CACHE_PASSWD);
} else {
fprintf(stderr,
_("Error creating user account for %s: %s\n"),
fields[0], lu_strerror(error));
if (error) {
lu_error_free(&error);
}
}
g_free(homedir);
g_strfreev(fields);
lu_ent_clear_all(ent);
lu_ent_clear_all(groupEnt);
}
result = 0;
done:
if (groupEnt) lu_ent_free(groupEnt);
if (ent) lu_ent_free(ent);
if (ctx) lu_end(ctx);
poptFreeContext(popt);
return result;
}
|