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