]> git.ipfire.org Git - thirdparty/util-linux.git/blame - login-utils/chfn.c
rename: make --no-act to imply --verbose
[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>
3ca10299 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
e41ae450
SK
59#ifdef HAVE_LIBREADLINE
60# define _FUNCTION_DEF
61# include <readline/readline.h>
62#endif
63
6dbe3af9 64struct finfo {
3ca10299
SK
65 char *full_name;
66 char *office;
67 char *office_phone;
68 char *home_phone;
69 char *other;
6dbe3af9
KZ
70};
71
d5fdba03 72struct chfn_control {
5fe1c32f
SK
73 struct passwd *pw;
74 char *username;
75 /* "oldf" Contains the users original finger information.
76 * "newf" Contains the changed finger information, and contains
77 * NULL in fields that haven't been changed.
78 * In the end, "newf" is folded into "oldf". */
79 struct finfo oldf, newf;
d5fdba03 80 unsigned int
e88f0059
SK
81 allow_fullname:1, /* The login.defs restriction */
82 allow_room:1, /* see: man login.defs(5) */
83 allow_work:1, /* and look for CHFN_RESTRICT */
84 allow_home:1, /* keyword for these four. */
f723cbf5 85 changed:1, /* is change requested */
d5fdba03
SK
86 interactive:1; /* whether to prompt for fields or not */
87};
88
7eda085c 89/* we do not accept gecos field sizes longer than MAX_FIELD_SIZE */
5c36a0eb
KZ
90#define MAX_FIELD_SIZE 256
91
39941b4e
MP
92static void __attribute__((__noreturn__)) usage(FILE *fp)
93{
3ca10299 94 fputs(USAGE_HEADER, fp);
09af3db4 95 fprintf(fp, _(" %s [options] [<username>]\n"), program_invocation_short_name);
451dbcfa
BS
96
97 fputs(USAGE_SEPARATOR, fp);
98 fputs(_("Change your finger information.\n"), fp);
99
3ca10299
SK
100 fputs(USAGE_OPTIONS, fp);
101 fputs(_(" -f, --full-name <full-name> real name\n"), fp);
102 fputs(_(" -o, --office <office> office number\n"), fp);
103 fputs(_(" -p, --office-phone <phone> office phone number\n"), fp);
104 fputs(_(" -h, --home-phone <phone> home phone number\n"), fp);
105 fputs(USAGE_SEPARATOR, fp);
496083ba
SK
106 fputs(_(" -u, --help display this help and exit\n"), fp);
107 fputs(_(" -v, --version output version information and exit\n"), fp);
3ca10299
SK
108 fprintf(fp, USAGE_MAN_TAIL("chfn(1)"));
109 exit(fp == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
39941b4e
MP
110}
111
24016335
SK
112/*
113 * check_gecos_string () --
114 * check that the given gecos string is legal. if it's not legal,
115 * output "msg" followed by a description of the problem, and return (-1).
116 */
5a57c00a 117static int check_gecos_string(const char *msg, char *gecos)
3ca10299 118{
5a57c00a 119 const size_t len = strlen(gecos);
3ca10299 120
5a57c00a
SK
121 if (MAX_FIELD_SIZE < len) {
122 warnx(_("field %s is too long"), msg);
24016335 123 return -1;
3ca10299 124 }
144ae70e
SK
125 if (illegal_passwd_chars(gecos)) {
126 warnx(_("%s: has illegal characters"), gecos);
127 return -1;
3ca10299 128 }
24016335 129 return 0;
6dbe3af9
KZ
130}
131
132/*
133 * parse_argv () --
134 * parse the command line arguments.
135 * returns true if no information beyond the username was given.
136 */
5fe1c32f 137static void parse_argv(struct chfn_control *ctl, int argc, char **argv)
6dbe3af9 138{
e4efecc4
SK
139 int index, c, status = 0;
140 static const struct option long_options[] = {
87918040
SK
141 { "full-name", required_argument, NULL, 'f' },
142 { "office", required_argument, NULL, 'o' },
143 { "office-phone", required_argument, NULL, 'p' },
144 { "home-phone", required_argument, NULL, 'h' },
145 { "help", no_argument, NULL, 'u' },
146 { "version", no_argument, NULL, 'v' },
147 { NULL, 0, NULL, 0 },
3ca10299
SK
148 };
149
e4efecc4
SK
150 while ((c = getopt_long(argc, argv, "f:r:p:h:o:uv", long_options,
151 &index)) != -1) {
3ca10299
SK
152 switch (c) {
153 case 'f':
e88f0059
SK
154 if (!ctl->allow_fullname)
155 errx(EXIT_FAILURE, _("login.defs forbids setting %s"), _("Name"));
5fe1c32f 156 ctl->newf.full_name = optarg;
e4efecc4 157 status += check_gecos_string(_("Name"), optarg);
3ca10299
SK
158 break;
159 case 'o':
e88f0059
SK
160 if (!ctl->allow_room)
161 errx(EXIT_FAILURE, _("login.defs forbids setting %s"), _("Office"));
5fe1c32f 162 ctl->newf.office = optarg;
e4efecc4 163 status += check_gecos_string(_("Office"), optarg);
3ca10299
SK
164 break;
165 case 'p':
e88f0059
SK
166 if (!ctl->allow_work)
167 errx(EXIT_FAILURE, _("login.defs forbids setting %s"), _("Office Phone"));
5fe1c32f 168 ctl->newf.office_phone = optarg;
e4efecc4 169 status += check_gecos_string(_("Office Phone"), optarg);
3ca10299
SK
170 break;
171 case 'h':
e88f0059
SK
172 if (!ctl->allow_home)
173 errx(EXIT_FAILURE, _("login.defs forbids setting %s"), _("Home Phone"));
5fe1c32f 174 ctl->newf.home_phone = optarg;
e4efecc4 175 status += check_gecos_string(_("Home Phone"), optarg);
3ca10299 176 break;
e4efecc4
SK
177 case 'v':
178 printf(UTIL_LINUX_VERSION);
179 exit(EXIT_SUCCESS);
180 case 'u':
181 usage(stdout);
3ca10299 182 default:
677ec86c 183 errtryhelp(EXIT_FAILURE);
3ca10299 184 }
f723cbf5 185 ctl->changed = 1;
e4efecc4 186 ctl->interactive = 0;
6dbe3af9 187 }
e4efecc4
SK
188 if (status != 0)
189 exit(EXIT_FAILURE);
3ca10299
SK
190 /* done parsing arguments. check for a username. */
191 if (optind < argc) {
192 if (optind + 1 < argc)
193 usage(stderr);
5fe1c32f 194 ctl->username = argv[optind];
6dbe3af9 195 }
d5fdba03 196 return;
6dbe3af9
KZ
197}
198
6dbe3af9
KZ
199/*
200 * parse_passwd () --
3ca10299 201 * take a struct password and fill in the fields of the struct finfo.
6dbe3af9 202 */
5fe1c32f 203static void parse_passwd(struct chfn_control *ctl)
6dbe3af9 204{
3ca10299 205 char *gecos;
58985f67 206
5fe1c32f 207 if (!ctl->pw)
58985f67 208 return;
58985f67 209 /* use pw_gecos - we take a copy since PAM destroys the original */
5fe1c32f 210 gecos = xstrdup(ctl->pw->pw_gecos);
58985f67 211 /* extract known fields */
5fe1c32f
SK
212 ctl->oldf.full_name = strsep(&gecos, ",");
213 ctl->oldf.office = strsep(&gecos, ",");
214 ctl->oldf.office_phone = strsep(&gecos, ",");
215 ctl->oldf.home_phone = strsep(&gecos, ",");
58985f67
SK
216 /* extra fields contain site-specific information, and can
217 * not be changed by this version of chfn. */
5fe1c32f 218 ctl->oldf.other = strsep(&gecos, ",");
6dbe3af9
KZ
219}
220
6dbe3af9 221/*
d9e1ac99 222 * ask_new_field () --
6dbe3af9
KZ
223 * ask the user for a given field and check that the string is legal.
224 */
d9e1ac99
SK
225static char *ask_new_field(struct chfn_control *ctl, const char *question,
226 char *def_val)
6dbe3af9 227{
3ca10299 228 int len;
e41ae450
SK
229 char *buf;
230#ifndef HAVE_LIBREADLINE
231 size_t dummy = 0;
232#endif
3ca10299 233
5a57c00a
SK
234 if (!def_val)
235 def_val = "";
3ca10299 236 while (true) {
3ca10299 237 printf("%s [%s]: ", question, def_val);
5a57c00a 238 __fpurge(stdin);
e41ae450
SK
239#ifdef HAVE_LIBREADLINE
240 if ((buf = readline(NULL)) == NULL)
241#else
242 if (getline(&buf, &dummy, stdin) < 0)
243#endif
3ca10299 244 errx(EXIT_FAILURE, _("Aborted."));
5a57c00a 245 /* remove white spaces from string end */
e41ae450
SK
246 ltrim_whitespace((unsigned char *) buf);
247 len = rtrim_whitespace((unsigned char *) buf);
248 if (len == 0) {
249 free(buf);
f723cbf5 250 return xstrdup(def_val);
e41ae450
SK
251 }
252 if (!strcasecmp(buf, "none")) {
253 free(buf);
f723cbf5
SK
254 ctl->changed = 1;
255 return xstrdup("");
256 }
e41ae450 257 if (check_gecos_string(question, buf) >= 0)
3ca10299
SK
258 break;
259 }
f723cbf5 260 ctl->changed = 1;
e41ae450 261 return buf;
6dbe3af9
KZ
262}
263
e88f0059
SK
264/*
265 * get_login_defs()
266 * find /etc/login.defs CHFN_RESTRICT and save restrictions to run time
267 */
268static void get_login_defs(struct chfn_control *ctl)
269{
270 const char *s;
271 size_t i;
272 int broken = 0;
273
274 /* real root does not have restrictions */
275 if (geteuid() == getuid() && getuid() == 0) {
276 ctl->allow_fullname = ctl->allow_room = ctl->allow_work = ctl->allow_home = 1;
277 return;
278 }
279 s = getlogindefs_str("CHFN_RESTRICT", "");
280 if (!strcmp(s, "yes")) {
281 ctl->allow_room = ctl->allow_work = ctl->allow_home = 1;
282 return;
283 }
284 if (!strcmp(s, "no")) {
285 ctl->allow_fullname = ctl->allow_room = ctl->allow_work = ctl->allow_home = 1;
286 return;
287 }
288 for (i = 0; s[i]; i++) {
289 switch (s[i]) {
290 case 'f':
291 ctl->allow_fullname = 1;
292 break;
293 case 'r':
294 ctl->allow_room = 1;
295 break;
296 case 'w':
297 ctl->allow_work = 1;
298 break;
299 case 'h':
300 ctl->allow_home = 1;
301 break;
302 default:
303 broken = 1;
304 }
305 }
306 if (broken)
307 warnx(_("%s: CHFN_RESTRICT has unexpected value: %s"), _PATH_LOGINDEFS, s);
308 if (!ctl->allow_fullname && !ctl->allow_room && !ctl->allow_work && !ctl->allow_home)
309 errx(EXIT_FAILURE, _("%s: CHFN_RESTRICT does not allow any changes"), _PATH_LOGINDEFS);
310 return;
311}
312
6dbe3af9 313/*
24016335
SK
314 * ask_info () --
315 * prompt the user for the finger information and store it.
6dbe3af9 316 */
5fe1c32f 317static void ask_info(struct chfn_control *ctl)
6dbe3af9 318{
e88f0059
SK
319 if (ctl->allow_fullname)
320 ctl->newf.full_name = ask_new_field(ctl, _("Name"), ctl->oldf.full_name);
321 if (ctl->allow_room)
322 ctl->newf.office = ask_new_field(ctl, _("Office"), ctl->oldf.office);
323 if (ctl->allow_work)
324 ctl->newf.office_phone = ask_new_field(ctl, _("Office Phone"), ctl->oldf.office_phone);
325 if (ctl->allow_home)
326 ctl->newf.home_phone = ask_new_field(ctl, _("Home Phone"), ctl->oldf.home_phone);
327 putchar('\n');
6dbe3af9
KZ
328}
329
330/*
f723cbf5
SK
331 * find_field () --
332 * find field value in uninteractive mode; can be new, old, or blank
6dbe3af9 333 */
f723cbf5 334static char *find_field(char *nf, char *of)
6dbe3af9 335{
f723cbf5
SK
336 if (nf)
337 return nf;
338 if (of)
339 return of;
340 return xstrdup("");
341}
3ca10299 342
f723cbf5
SK
343/*
344 * add_missing () --
345 * add not supplied field values when in uninteractive mode
346 */
347static void add_missing(struct chfn_control *ctl)
348{
349 ctl->newf.full_name = find_field(ctl->newf.full_name, ctl->oldf.full_name);
350 ctl->newf.office = find_field(ctl->newf.office, ctl->oldf.office);
351 ctl->newf.office_phone = find_field(ctl->newf.office_phone, ctl->oldf.office_phone);
352 ctl->newf.home_phone = find_field(ctl->newf.home_phone, ctl->oldf.home_phone);
353 ctl->newf.other = find_field(ctl->newf.other, ctl->oldf.other);
354 printf("\n");
6dbe3af9
KZ
355}
356
357/*
358 * save_new_data () --
359 * save the given finger info in /etc/passwd.
360 * return zero on success.
361 */
5fe1c32f 362static int save_new_data(struct chfn_control *ctl)
6dbe3af9 363{
3ca10299
SK
364 char *gecos;
365 int len;
366
3ca10299 367 /* create the new gecos string */
f723cbf5
SK
368 len = xasprintf(&gecos, "%s,%s,%s,%s,%s",
369 ctl->newf.full_name,
370 ctl->newf.office,
371 ctl->newf.office_phone,
372 ctl->newf.home_phone,
373 ctl->newf.other);
3ca10299 374
5fe1c32f 375 /* remove trailing empty fields (but not subfields of ctl->newf.other) */
f723cbf5 376 if (!ctl->newf.other) {
3ca10299
SK
377 while (len > 0 && gecos[len - 1] == ',')
378 len--;
379 gecos[len] = 0;
380 }
381
8c24b6aa 382#ifdef HAVE_LIBUSER
5fe1c32f 383 if (set_value_libuser("chfn", ctl->username, ctl->pw->pw_uid,
d86918b6 384 LU_GECOS, gecos) < 0) {
8c24b6aa 385#else /* HAVE_LIBUSER */
3ca10299 386 /* write the new struct passwd to the passwd file. */
5fe1c32f 387 ctl->pw->pw_gecos = gecos;
bde91c85 388 if (setpwnam(ctl->pw, ".chfn") < 0) {
d86918b6 389 warn("setpwnam failed");
8c24b6aa 390#endif
3ca10299
SK
391 printf(_
392 ("Finger information *NOT* changed. Try again later.\n"));
393 return -1;
394 }
1b7a19eb 395 free(gecos);
3ca10299
SK
396 printf(_("Finger information changed.\n"));
397 return 0;
6dbe3af9 398}
24016335
SK
399
400int main(int argc, char **argv)
401{
402 uid_t uid;
d5fdba03
SK
403 struct chfn_control ctl = {
404 .interactive = 1
405 };
24016335
SK
406
407 sanitize_env();
408 setlocale(LC_ALL, ""); /* both for messages and for iscntrl() below */
409 bindtextdomain(PACKAGE, LOCALEDIR);
410 textdomain(PACKAGE);
411 atexit(close_stdout);
24016335 412 uid = getuid();
24016335 413
e88f0059
SK
414 /* check /etc/login.defs CHFN_RESTRICT */
415 get_login_defs(&ctl);
416
5fe1c32f
SK
417 parse_argv(&ctl, argc, argv);
418 if (!ctl.username) {
419 ctl.pw = getpwuid(uid);
420 if (!ctl.pw)
24016335
SK
421 errx(EXIT_FAILURE, _("you (user %d) don't exist."),
422 uid);
5fe1c32f 423 ctl.username = ctl.pw->pw_name;
24016335 424 } else {
5fe1c32f
SK
425 ctl.pw = getpwnam(ctl.username);
426 if (!ctl.pw)
24016335 427 errx(EXIT_FAILURE, _("user \"%s\" does not exist."),
5fe1c32f 428 ctl.username);
24016335 429 }
5fe1c32f 430 parse_passwd(&ctl);
24016335 431#ifndef HAVE_LIBUSER
5fe1c32f 432 if (!(is_local(ctl.username)))
24016335
SK
433 errx(EXIT_FAILURE, _("can only change local entries"));
434#endif
435
436#ifdef HAVE_LIBSELINUX
437 if (is_selinux_enabled() > 0) {
438 if (uid == 0) {
dd5ef107
KZ
439 access_vector_t av = get_access_vector("passwd", "chfn");
440
441 if (selinux_check_passwd_access(av) != 0) {
24016335
SK
442 security_context_t user_context;
443 if (getprevcon(&user_context) < 0)
444 user_context = NULL;
445 errx(EXIT_FAILURE,
446 _("%s is not authorized to change "
447 "the finger info of %s"),
448 user_context ? : _("Unknown user context"),
5fe1c32f 449 ctl.username);
24016335
SK
450 }
451 }
452 if (setupDefaultContext(_PATH_PASSWD))
453 errx(EXIT_FAILURE,
454 _("can't set default context for %s"), _PATH_PASSWD);
455 }
456#endif
457
458#ifdef HAVE_LIBUSER
459 /* If we're setuid and not really root, disallow the password change. */
5fe1c32f 460 if (geteuid() != getuid() && uid != ctl.pw->pw_uid) {
24016335 461#else
bf6c15ed 462 if (uid != 0 && uid != ctl.pw->pw_uid) {
24016335
SK
463#endif
464 errno = EACCES;
465 err(EXIT_FAILURE, _("running UID doesn't match UID of user we're "
466 "altering, change denied"));
467 }
468
5fe1c32f 469 printf(_("Changing finger information for %s.\n"), ctl.username);
24016335
SK
470
471#if !defined(HAVE_LIBUSER) && defined(CHFN_CHSH_PASSWORD)
5fe1c32f 472 if (!auth_pam("chfn", uid, ctl.username)) {
24016335
SK
473 return EXIT_FAILURE;
474 }
475#endif
476
d5fdba03 477 if (ctl.interactive)
5fe1c32f 478 ask_info(&ctl);
e88f0059
SK
479
480 add_missing(&ctl);
24016335 481
f723cbf5 482 if (!ctl.changed) {
24016335
SK
483 printf(_("Finger information not changed.\n"));
484 return EXIT_SUCCESS;
485 }
486
5fe1c32f 487 return save_new_data(&ctl) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
24016335 488}