]> git.ipfire.org Git - thirdparty/util-linux.git/blame - login-utils/chfn.c
hwclock: free temporary variable before return
[thirdparty/util-linux.git] / login-utils / chfn.c
CommitLineData
6dbe3af9
KZ
1/*
2 * chfn.c -- change your finger information
3 * (c) 1994 by salvatore valente <svalente@athena.mit.edu>
8c24b6aa 4 * (c) 2012 by Cody Maloney <cmaloney@theoreticalchaos.com>
6dbe3af9
KZ
5 *
6 * this program is free software. you can redistribute it and
7 * modify it under the terms of the gnu general public license.
8 * there is no warranty.
9 *
fd6b7a7f 10 * $Author: aebr $
2b6fc908
KZ
11 * $Revision: 1.18 $
12 * $Date: 1998/06/11 22:30:11 $
726f69e2
KZ
13 *
14 * Updated Thu Oct 12 09:19:26 1995 by faith@cs.unc.edu with security
15 * patches from Zefram <A.Main@dcs.warwick.ac.uk>
6dbe3af9 16 *
7eda085c
KZ
17 * Hacked by Peter Breitenlohner, peb@mppmu.mpg.de,
18 * to remove trailing empty fields. Oct 5, 96.
19 *
b50945d4 20 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
7eda085c 21 * - added Native Language Support
6dbe3af9
KZ
22 */
23
6dbe3af9 24#include <ctype.h>
3ca10299 25#include <errno.h>
6dbe3af9 26#include <getopt.h>
3ca10299 27#include <pwd.h>
8187b555 28#include <stdbool.h>
3ca10299
SK
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/types.h>
33#include <unistd.h>
39941b4e 34
3ca10299
SK
35#include "c.h"
36#include "env.h"
439cdf1e 37#include "closestream.h"
66ee8158 38#include "islocal.h"
3ca10299 39#include "nls.h"
66ee8158 40#include "setpwnam.h"
8abcf290 41#include "strutils.h"
39941b4e 42#include "xalloc.h"
e88f0059 43#include "logindefs.h"
fd6b7a7f 44
144ae70e
SK
45#include "ch-common.h"
46
48d7b13a 47#ifdef HAVE_LIBSELINUX
3ca10299 48# include <selinux/selinux.h>
68f4aa2a 49# include "selinux-utils.h"
d03dd608
KZ
50#endif
51
8c24b6aa
CM
52#ifdef HAVE_LIBUSER
53# include <libuser/user.h>
54# include "libuser.h"
d86918b6 55#elif CHFN_CHSH_PASSWORD
8c24b6aa
CM
56# include "auth.h"
57#endif
58
6dbe3af9 59struct finfo {
3ca10299
SK
60 char *full_name;
61 char *office;
62 char *office_phone;
63 char *home_phone;
64 char *other;
6dbe3af9
KZ
65};
66
d5fdba03 67struct chfn_control {
5fe1c32f
SK
68 struct passwd *pw;
69 char *username;
70 /* "oldf" Contains the users original finger information.
71 * "newf" Contains the changed finger information, and contains
72 * NULL in fields that haven't been changed.
73 * In the end, "newf" is folded into "oldf". */
74 struct finfo oldf, newf;
d5fdba03 75 unsigned int
e88f0059
SK
76 allow_fullname:1, /* The login.defs restriction */
77 allow_room:1, /* see: man login.defs(5) */
78 allow_work:1, /* and look for CHFN_RESTRICT */
79 allow_home:1, /* keyword for these four. */
f723cbf5 80 changed:1, /* is change requested */
d5fdba03
SK
81 interactive:1; /* whether to prompt for fields or not */
82};
83
7eda085c 84/* we do not accept gecos field sizes longer than MAX_FIELD_SIZE */
5c36a0eb
KZ
85#define MAX_FIELD_SIZE 256
86
6e1eda6f 87static void __attribute__((__noreturn__)) usage(void)
39941b4e 88{
6e1eda6f 89 FILE *fp = stdout;
3ca10299 90 fputs(USAGE_HEADER, fp);
09af3db4 91 fprintf(fp, _(" %s [options] [<username>]\n"), program_invocation_short_name);
451dbcfa
BS
92
93 fputs(USAGE_SEPARATOR, fp);
94 fputs(_("Change your finger information.\n"), fp);
95
3ca10299
SK
96 fputs(USAGE_OPTIONS, fp);
97 fputs(_(" -f, --full-name <full-name> real name\n"), fp);
98 fputs(_(" -o, --office <office> office number\n"), fp);
99 fputs(_(" -p, --office-phone <phone> office phone number\n"), fp);
100 fputs(_(" -h, --home-phone <phone> home phone number\n"), fp);
101 fputs(USAGE_SEPARATOR, fp);
b3054454 102 printf( " -u, --help %s\n", USAGE_OPTSTR_HELP);
0222a2ca 103 printf( " -V, --version %s\n", USAGE_OPTSTR_VERSION);
f45f3ec3 104 printf(USAGE_MAN_TAIL("chfn(1)"));
6e1eda6f 105 exit(EXIT_SUCCESS);
39941b4e
MP
106}
107
24016335
SK
108/*
109 * check_gecos_string () --
110 * check that the given gecos string is legal. if it's not legal,
111 * output "msg" followed by a description of the problem, and return (-1).
112 */
5a57c00a 113static int check_gecos_string(const char *msg, char *gecos)
3ca10299 114{
5a57c00a 115 const size_t len = strlen(gecos);
3ca10299 116
5a57c00a
SK
117 if (MAX_FIELD_SIZE < len) {
118 warnx(_("field %s is too long"), msg);
24016335 119 return -1;
3ca10299 120 }
144ae70e
SK
121 if (illegal_passwd_chars(gecos)) {
122 warnx(_("%s: has illegal characters"), gecos);
123 return -1;
3ca10299 124 }
24016335 125 return 0;
6dbe3af9
KZ
126}
127
128/*
129 * parse_argv () --
130 * parse the command line arguments.
131 * returns true if no information beyond the username was given.
132 */
5fe1c32f 133static void parse_argv(struct chfn_control *ctl, int argc, char **argv)
6dbe3af9 134{
e4efecc4
SK
135 int index, c, status = 0;
136 static const struct option long_options[] = {
87918040
SK
137 { "full-name", required_argument, NULL, 'f' },
138 { "office", required_argument, NULL, 'o' },
139 { "office-phone", required_argument, NULL, 'p' },
140 { "home-phone", required_argument, NULL, 'h' },
141 { "help", no_argument, NULL, 'u' },
0222a2ca 142 { "version", no_argument, NULL, 'V' },
87918040 143 { NULL, 0, NULL, 0 },
3ca10299
SK
144 };
145
0222a2ca 146 while ((c = getopt_long(argc, argv, "f:r:p:h:o:uvV", long_options,
e4efecc4 147 &index)) != -1) {
3ca10299
SK
148 switch (c) {
149 case 'f':
e88f0059
SK
150 if (!ctl->allow_fullname)
151 errx(EXIT_FAILURE, _("login.defs forbids setting %s"), _("Name"));
5fe1c32f 152 ctl->newf.full_name = optarg;
e4efecc4 153 status += check_gecos_string(_("Name"), optarg);
3ca10299
SK
154 break;
155 case 'o':
e88f0059
SK
156 if (!ctl->allow_room)
157 errx(EXIT_FAILURE, _("login.defs forbids setting %s"), _("Office"));
5fe1c32f 158 ctl->newf.office = optarg;
e4efecc4 159 status += check_gecos_string(_("Office"), optarg);
3ca10299
SK
160 break;
161 case 'p':
e88f0059
SK
162 if (!ctl->allow_work)
163 errx(EXIT_FAILURE, _("login.defs forbids setting %s"), _("Office Phone"));
5fe1c32f 164 ctl->newf.office_phone = optarg;
e4efecc4 165 status += check_gecos_string(_("Office Phone"), optarg);
3ca10299
SK
166 break;
167 case 'h':
e88f0059
SK
168 if (!ctl->allow_home)
169 errx(EXIT_FAILURE, _("login.defs forbids setting %s"), _("Home Phone"));
5fe1c32f 170 ctl->newf.home_phone = optarg;
e4efecc4 171 status += check_gecos_string(_("Home Phone"), optarg);
3ca10299 172 break;
0222a2ca
KZ
173 case 'v': /* deprecated */
174 case 'V':
2c308875 175 print_version(EXIT_SUCCESS);
e4efecc4 176 case 'u':
6e1eda6f 177 usage();
3ca10299 178 default:
677ec86c 179 errtryhelp(EXIT_FAILURE);
3ca10299 180 }
f723cbf5 181 ctl->changed = 1;
e4efecc4 182 ctl->interactive = 0;
6dbe3af9 183 }
e4efecc4
SK
184 if (status != 0)
185 exit(EXIT_FAILURE);
3ca10299
SK
186 /* done parsing arguments. check for a username. */
187 if (optind < argc) {
6e1eda6f
RM
188 if (optind + 1 < argc) {
189 warnx(_("cannot handle multiple usernames"));
190 errtryhelp(EXIT_FAILURE);
191 }
5fe1c32f 192 ctl->username = argv[optind];
6dbe3af9 193 }
6dbe3af9
KZ
194}
195
6dbe3af9
KZ
196/*
197 * parse_passwd () --
3ca10299 198 * take a struct password and fill in the fields of the struct finfo.
6dbe3af9 199 */
5fe1c32f 200static void parse_passwd(struct chfn_control *ctl)
6dbe3af9 201{
3ca10299 202 char *gecos;
58985f67 203
5fe1c32f 204 if (!ctl->pw)
58985f67 205 return;
58985f67 206 /* use pw_gecos - we take a copy since PAM destroys the original */
5fe1c32f 207 gecos = xstrdup(ctl->pw->pw_gecos);
58985f67 208 /* extract known fields */
5fe1c32f
SK
209 ctl->oldf.full_name = strsep(&gecos, ",");
210 ctl->oldf.office = strsep(&gecos, ",");
211 ctl->oldf.office_phone = strsep(&gecos, ",");
212 ctl->oldf.home_phone = strsep(&gecos, ",");
58985f67
SK
213 /* extra fields contain site-specific information, and can
214 * not be changed by this version of chfn. */
5fe1c32f 215 ctl->oldf.other = strsep(&gecos, ",");
6dbe3af9
KZ
216}
217
6dbe3af9 218/*
d9e1ac99 219 * ask_new_field () --
6dbe3af9
KZ
220 * ask the user for a given field and check that the string is legal.
221 */
d9e1ac99
SK
222static char *ask_new_field(struct chfn_control *ctl, const char *question,
223 char *def_val)
6dbe3af9 224{
3ca10299 225 int len;
05907d0d 226 char *buf = NULL; /* leave initialized to NULL or getline segfaults */
e41ae450 227 size_t dummy = 0;
3ca10299 228
5a57c00a
SK
229 if (!def_val)
230 def_val = "";
faa5a3a8 231
3ca10299 232 while (true) {
49848aa5 233 printf("%s [%s]:", question, def_val);
5a57c00a 234 __fpurge(stdin);
faa5a3a8 235
49848aa5 236 putchar(' ');
05907d0d 237 fflush(stdout);
faa5a3a8 238
e41ae450 239 if (getline(&buf, &dummy, stdin) < 0)
3ca10299 240 errx(EXIT_FAILURE, _("Aborted."));
faa5a3a8 241
5a57c00a 242 /* remove white spaces from string end */
e41ae450
SK
243 ltrim_whitespace((unsigned char *) buf);
244 len = rtrim_whitespace((unsigned char *) buf);
245 if (len == 0) {
246 free(buf);
f723cbf5 247 return xstrdup(def_val);
e41ae450
SK
248 }
249 if (!strcasecmp(buf, "none")) {
250 free(buf);
f723cbf5
SK
251 ctl->changed = 1;
252 return xstrdup("");
253 }
e41ae450 254 if (check_gecos_string(question, buf) >= 0)
3ca10299
SK
255 break;
256 }
f723cbf5 257 ctl->changed = 1;
e41ae450 258 return buf;
6dbe3af9
KZ
259}
260
e88f0059
SK
261/*
262 * get_login_defs()
263 * find /etc/login.defs CHFN_RESTRICT and save restrictions to run time
264 */
265static void get_login_defs(struct chfn_control *ctl)
266{
267 const char *s;
268 size_t i;
269 int broken = 0;
270
271 /* real root does not have restrictions */
272 if (geteuid() == getuid() && getuid() == 0) {
273 ctl->allow_fullname = ctl->allow_room = ctl->allow_work = ctl->allow_home = 1;
274 return;
275 }
276 s = getlogindefs_str("CHFN_RESTRICT", "");
277 if (!strcmp(s, "yes")) {
278 ctl->allow_room = ctl->allow_work = ctl->allow_home = 1;
279 return;
280 }
281 if (!strcmp(s, "no")) {
282 ctl->allow_fullname = ctl->allow_room = ctl->allow_work = ctl->allow_home = 1;
283 return;
284 }
285 for (i = 0; s[i]; i++) {
286 switch (s[i]) {
287 case 'f':
288 ctl->allow_fullname = 1;
289 break;
290 case 'r':
291 ctl->allow_room = 1;
292 break;
293 case 'w':
294 ctl->allow_work = 1;
295 break;
296 case 'h':
297 ctl->allow_home = 1;
298 break;
299 default:
300 broken = 1;
301 }
302 }
303 if (broken)
304 warnx(_("%s: CHFN_RESTRICT has unexpected value: %s"), _PATH_LOGINDEFS, s);
305 if (!ctl->allow_fullname && !ctl->allow_room && !ctl->allow_work && !ctl->allow_home)
306 errx(EXIT_FAILURE, _("%s: CHFN_RESTRICT does not allow any changes"), _PATH_LOGINDEFS);
e88f0059
SK
307}
308
6dbe3af9 309/*
24016335
SK
310 * ask_info () --
311 * prompt the user for the finger information and store it.
6dbe3af9 312 */
5fe1c32f 313static void ask_info(struct chfn_control *ctl)
6dbe3af9 314{
e88f0059
SK
315 if (ctl->allow_fullname)
316 ctl->newf.full_name = ask_new_field(ctl, _("Name"), ctl->oldf.full_name);
317 if (ctl->allow_room)
318 ctl->newf.office = ask_new_field(ctl, _("Office"), ctl->oldf.office);
319 if (ctl->allow_work)
320 ctl->newf.office_phone = ask_new_field(ctl, _("Office Phone"), ctl->oldf.office_phone);
321 if (ctl->allow_home)
322 ctl->newf.home_phone = ask_new_field(ctl, _("Home Phone"), ctl->oldf.home_phone);
323 putchar('\n');
6dbe3af9
KZ
324}
325
326/*
f723cbf5
SK
327 * find_field () --
328 * find field value in uninteractive mode; can be new, old, or blank
6dbe3af9 329 */
f723cbf5 330static char *find_field(char *nf, char *of)
6dbe3af9 331{
f723cbf5
SK
332 if (nf)
333 return nf;
334 if (of)
335 return of;
336 return xstrdup("");
337}
3ca10299 338
f723cbf5
SK
339/*
340 * add_missing () --
341 * add not supplied field values when in uninteractive mode
342 */
343static void add_missing(struct chfn_control *ctl)
344{
345 ctl->newf.full_name = find_field(ctl->newf.full_name, ctl->oldf.full_name);
346 ctl->newf.office = find_field(ctl->newf.office, ctl->oldf.office);
347 ctl->newf.office_phone = find_field(ctl->newf.office_phone, ctl->oldf.office_phone);
348 ctl->newf.home_phone = find_field(ctl->newf.home_phone, ctl->oldf.home_phone);
349 ctl->newf.other = find_field(ctl->newf.other, ctl->oldf.other);
350 printf("\n");
6dbe3af9
KZ
351}
352
353/*
354 * save_new_data () --
355 * save the given finger info in /etc/passwd.
356 * return zero on success.
357 */
5fe1c32f 358static int save_new_data(struct chfn_control *ctl)
6dbe3af9 359{
3ca10299
SK
360 char *gecos;
361 int len;
362
3ca10299 363 /* create the new gecos string */
f723cbf5
SK
364 len = xasprintf(&gecos, "%s,%s,%s,%s,%s",
365 ctl->newf.full_name,
366 ctl->newf.office,
367 ctl->newf.office_phone,
368 ctl->newf.home_phone,
369 ctl->newf.other);
3ca10299 370
5fe1c32f 371 /* remove trailing empty fields (but not subfields of ctl->newf.other) */
9210c0d2 372 if (!ctl->newf.other || !*ctl->newf.other) {
3ca10299
SK
373 while (len > 0 && gecos[len - 1] == ',')
374 len--;
375 gecos[len] = 0;
376 }
377
8c24b6aa 378#ifdef HAVE_LIBUSER
5fe1c32f 379 if (set_value_libuser("chfn", ctl->username, ctl->pw->pw_uid,
d86918b6 380 LU_GECOS, gecos) < 0) {
8c24b6aa 381#else /* HAVE_LIBUSER */
3ca10299 382 /* write the new struct passwd to the passwd file. */
5fe1c32f 383 ctl->pw->pw_gecos = gecos;
bde91c85 384 if (setpwnam(ctl->pw, ".chfn") < 0) {
d86918b6 385 warn("setpwnam failed");
8c24b6aa 386#endif
3ca10299
SK
387 printf(_
388 ("Finger information *NOT* changed. Try again later.\n"));
389 return -1;
390 }
1b7a19eb 391 free(gecos);
3ca10299
SK
392 printf(_("Finger information changed.\n"));
393 return 0;
6dbe3af9 394}
24016335
SK
395
396int main(int argc, char **argv)
397{
398 uid_t uid;
d5fdba03
SK
399 struct chfn_control ctl = {
400 .interactive = 1
401 };
24016335
SK
402
403 sanitize_env();
404 setlocale(LC_ALL, ""); /* both for messages and for iscntrl() below */
405 bindtextdomain(PACKAGE, LOCALEDIR);
406 textdomain(PACKAGE);
2c308875
KZ
407 close_stdout_atexit();
408
24016335 409 uid = getuid();
24016335 410
e88f0059
SK
411 /* check /etc/login.defs CHFN_RESTRICT */
412 get_login_defs(&ctl);
413
5fe1c32f
SK
414 parse_argv(&ctl, argc, argv);
415 if (!ctl.username) {
416 ctl.pw = getpwuid(uid);
417 if (!ctl.pw)
24016335
SK
418 errx(EXIT_FAILURE, _("you (user %d) don't exist."),
419 uid);
5fe1c32f 420 ctl.username = ctl.pw->pw_name;
24016335 421 } else {
5fe1c32f
SK
422 ctl.pw = getpwnam(ctl.username);
423 if (!ctl.pw)
24016335 424 errx(EXIT_FAILURE, _("user \"%s\" does not exist."),
5fe1c32f 425 ctl.username);
24016335 426 }
5fe1c32f 427 parse_passwd(&ctl);
24016335 428#ifndef HAVE_LIBUSER
5fe1c32f 429 if (!(is_local(ctl.username)))
24016335
SK
430 errx(EXIT_FAILURE, _("can only change local entries"));
431#endif
432
433#ifdef HAVE_LIBSELINUX
434 if (is_selinux_enabled() > 0) {
e1de70b3 435 char *user_cxt = NULL;
dd5ef107 436
e1de70b3
KZ
437 if (uid == 0 && !ul_selinux_has_access("passwd", "chfn", &user_cxt))
438 errx(EXIT_FAILURE,
439 _("%s is not authorized to change "
440 "the finger info of %s"),
441 user_cxt ? : _("Unknown user context"),
442 ctl.username);
e5228150 443
0735d0ef 444 if (ul_setfscreatecon_from_file(_PATH_PASSWD) != 0)
24016335
SK
445 errx(EXIT_FAILURE,
446 _("can't set default context for %s"), _PATH_PASSWD);
447 }
448#endif
449
450#ifdef HAVE_LIBUSER
451 /* If we're setuid and not really root, disallow the password change. */
5fe1c32f 452 if (geteuid() != getuid() && uid != ctl.pw->pw_uid) {
24016335 453#else
bf6c15ed 454 if (uid != 0 && uid != ctl.pw->pw_uid) {
24016335
SK
455#endif
456 errno = EACCES;
457 err(EXIT_FAILURE, _("running UID doesn't match UID of user we're "
458 "altering, change denied"));
459 }
460
5fe1c32f 461 printf(_("Changing finger information for %s.\n"), ctl.username);
24016335
SK
462
463#if !defined(HAVE_LIBUSER) && defined(CHFN_CHSH_PASSWORD)
5fe1c32f 464 if (!auth_pam("chfn", uid, ctl.username)) {
24016335
SK
465 return EXIT_FAILURE;
466 }
467#endif
468
d5fdba03 469 if (ctl.interactive)
5fe1c32f 470 ask_info(&ctl);
e88f0059
SK
471
472 add_missing(&ctl);
24016335 473
f723cbf5 474 if (!ctl.changed) {
24016335
SK
475 printf(_("Finger information not changed.\n"));
476 return EXIT_SUCCESS;
477 }
478
5fe1c32f 479 return save_new_data(&ctl) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
24016335 480}