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>
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.
12 * $Date: 1998/06/11 22:30:11 $
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>
17 * Hacked by Peter Breitenlohner, peb@mppmu.mpg.de,
18 * to remove trailing empty fields. Oct 5, 96.
20 * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
21 * - added Native Language Support
32 #include <sys/types.h>
37 #include "closestream.h"
43 #include "logindefs.h"
45 #include "ch-common.h"
47 #ifdef HAVE_LIBSELINUX
48 # include <selinux/selinux.h>
49 # include "selinux_utils.h"
53 # include <libuser/user.h>
55 #elif CHFN_CHSH_PASSWORD
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
;
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. */
80 changed
:1, /* is change requested */
81 interactive
:1; /* whether to prompt for fields or not */
84 /* we do not accept gecos field sizes longer than MAX_FIELD_SIZE */
85 #define MAX_FIELD_SIZE 256
87 static void __attribute__((__noreturn__
)) usage(FILE *fp
)
89 fputs(USAGE_HEADER
, fp
);
90 fprintf(fp
, _(" %s [options] [<username>]\n"), program_invocation_short_name
);
92 fputs(USAGE_SEPARATOR
, fp
);
93 fputs(_("Change your finger information.\n"), fp
);
95 fputs(USAGE_OPTIONS
, fp
);
96 fputs(_(" -f, --full-name <full-name> real name\n"), fp
);
97 fputs(_(" -o, --office <office> office number\n"), fp
);
98 fputs(_(" -p, --office-phone <phone> office phone number\n"), fp
);
99 fputs(_(" -h, --home-phone <phone> home phone number\n"), fp
);
100 fputs(USAGE_SEPARATOR
, fp
);
101 fputs(_(" -u, --help display this help and exit\n"), fp
);
102 fputs(_(" -v, --version output version information and exit\n"), fp
);
103 fprintf(fp
, USAGE_MAN_TAIL("chfn(1)"));
104 exit(fp
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
108 * check_gecos_string () --
109 * check that the given gecos string is legal. if it's not legal,
110 * output "msg" followed by a description of the problem, and return (-1).
112 static int check_gecos_string(const char *msg
, char *gecos
)
114 const size_t len
= strlen(gecos
);
116 if (MAX_FIELD_SIZE
< len
) {
117 warnx(_("field %s is too long"), msg
);
120 if (illegal_passwd_chars(gecos
)) {
121 warnx(_("%s: has illegal characters"), gecos
);
129 * parse the command line arguments.
130 * returns true if no information beyond the username was given.
132 static void parse_argv(struct chfn_control
*ctl
, int argc
, char **argv
)
134 int index
, c
, status
= 0;
135 static const struct option long_options
[] = {
136 {"full-name", required_argument
, 0, 'f'},
137 {"office", required_argument
, 0, 'o'},
138 {"office-phone", required_argument
, 0, 'p'},
139 {"home-phone", required_argument
, 0, 'h'},
140 {"help", no_argument
, 0, 'u'},
141 {"version", no_argument
, 0, 'v'},
142 {NULL
, no_argument
, 0, '0'},
145 while ((c
= getopt_long(argc
, argv
, "f:r:p:h:o:uv", long_options
,
149 if (!ctl
->allow_fullname
)
150 errx(EXIT_FAILURE
, _("login.defs forbids setting %s"), _("Name"));
151 ctl
->newf
.full_name
= optarg
;
152 status
+= check_gecos_string(_("Name"), optarg
);
155 if (!ctl
->allow_room
)
156 errx(EXIT_FAILURE
, _("login.defs forbids setting %s"), _("Office"));
157 ctl
->newf
.office
= optarg
;
158 status
+= check_gecos_string(_("Office"), optarg
);
161 if (!ctl
->allow_work
)
162 errx(EXIT_FAILURE
, _("login.defs forbids setting %s"), _("Office Phone"));
163 ctl
->newf
.office_phone
= optarg
;
164 status
+= check_gecos_string(_("Office Phone"), optarg
);
167 if (!ctl
->allow_home
)
168 errx(EXIT_FAILURE
, _("login.defs forbids setting %s"), _("Home Phone"));
169 ctl
->newf
.home_phone
= optarg
;
170 status
+= check_gecos_string(_("Home Phone"), optarg
);
173 printf(UTIL_LINUX_VERSION
);
181 ctl
->interactive
= 0;
185 /* done parsing arguments. check for a username. */
187 if (optind
+ 1 < argc
)
189 ctl
->username
= argv
[optind
];
196 * take a struct password and fill in the fields of the struct finfo.
198 static void parse_passwd(struct chfn_control
*ctl
)
204 /* use pw_gecos - we take a copy since PAM destroys the original */
205 gecos
= xstrdup(ctl
->pw
->pw_gecos
);
206 /* extract known fields */
207 ctl
->oldf
.full_name
= strsep(&gecos
, ",");
208 ctl
->oldf
.office
= strsep(&gecos
, ",");
209 ctl
->oldf
.office_phone
= strsep(&gecos
, ",");
210 ctl
->oldf
.home_phone
= strsep(&gecos
, ",");
211 /* extra fields contain site-specific information, and can
212 * not be changed by this version of chfn. */
213 ctl
->oldf
.other
= strsep(&gecos
, ",");
217 * ask_new_field () --
218 * ask the user for a given field and check that the string is legal.
220 static char *ask_new_field(struct chfn_control
*ctl
, const char *question
,
225 char buf
[MAX_FIELD_SIZE
+ 2];
230 printf("%s [%s]: ", question
, def_val
);
232 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
233 errx(EXIT_FAILURE
, _("Aborted."));
235 /* remove white spaces from string end */
236 ltrim_whitespace((unsigned char *) ans
);
237 len
= rtrim_whitespace((unsigned char *) ans
);
239 return xstrdup(def_val
);
240 if (!strcasecmp(ans
, "none")) {
244 if (check_gecos_string(question
, ans
) >= 0)
253 * find /etc/login.defs CHFN_RESTRICT and save restrictions to run time
255 static void get_login_defs(struct chfn_control
*ctl
)
261 /* real root does not have restrictions */
262 if (geteuid() == getuid() && getuid() == 0) {
263 ctl
->allow_fullname
= ctl
->allow_room
= ctl
->allow_work
= ctl
->allow_home
= 1;
266 s
= getlogindefs_str("CHFN_RESTRICT", "");
267 if (!strcmp(s
, "yes")) {
268 ctl
->allow_room
= ctl
->allow_work
= ctl
->allow_home
= 1;
271 if (!strcmp(s
, "no")) {
272 ctl
->allow_fullname
= ctl
->allow_room
= ctl
->allow_work
= ctl
->allow_home
= 1;
275 for (i
= 0; s
[i
]; i
++) {
278 ctl
->allow_fullname
= 1;
294 warnx(_("%s: CHFN_RESTRICT has unexpected value: %s"), _PATH_LOGINDEFS
, s
);
295 if (!ctl
->allow_fullname
&& !ctl
->allow_room
&& !ctl
->allow_work
&& !ctl
->allow_home
)
296 errx(EXIT_FAILURE
, _("%s: CHFN_RESTRICT does not allow any changes"), _PATH_LOGINDEFS
);
302 * prompt the user for the finger information and store it.
304 static void ask_info(struct chfn_control
*ctl
)
306 if (ctl
->allow_fullname
)
307 ctl
->newf
.full_name
= ask_new_field(ctl
, _("Name"), ctl
->oldf
.full_name
);
309 ctl
->newf
.office
= ask_new_field(ctl
, _("Office"), ctl
->oldf
.office
);
311 ctl
->newf
.office_phone
= ask_new_field(ctl
, _("Office Phone"), ctl
->oldf
.office_phone
);
313 ctl
->newf
.home_phone
= ask_new_field(ctl
, _("Home Phone"), ctl
->oldf
.home_phone
);
319 * find field value in uninteractive mode; can be new, old, or blank
321 static char *find_field(char *nf
, char *of
)
332 * add not supplied field values when in uninteractive mode
334 static void add_missing(struct chfn_control
*ctl
)
336 ctl
->newf
.full_name
= find_field(ctl
->newf
.full_name
, ctl
->oldf
.full_name
);
337 ctl
->newf
.office
= find_field(ctl
->newf
.office
, ctl
->oldf
.office
);
338 ctl
->newf
.office_phone
= find_field(ctl
->newf
.office_phone
, ctl
->oldf
.office_phone
);
339 ctl
->newf
.home_phone
= find_field(ctl
->newf
.home_phone
, ctl
->oldf
.home_phone
);
340 ctl
->newf
.other
= find_field(ctl
->newf
.other
, ctl
->oldf
.other
);
345 * save_new_data () --
346 * save the given finger info in /etc/passwd.
347 * return zero on success.
349 static int save_new_data(struct chfn_control
*ctl
)
354 /* create the new gecos string */
355 len
= xasprintf(&gecos
, "%s,%s,%s,%s,%s",
358 ctl
->newf
.office_phone
,
359 ctl
->newf
.home_phone
,
362 /* remove trailing empty fields (but not subfields of ctl->newf.other) */
363 if (!ctl
->newf
.other
) {
364 while (len
> 0 && gecos
[len
- 1] == ',')
370 if (set_value_libuser("chfn", ctl
->username
, ctl
->pw
->pw_uid
,
371 LU_GECOS
, gecos
) < 0) {
372 #else /* HAVE_LIBUSER */
373 /* write the new struct passwd to the passwd file. */
374 ctl
->pw
->pw_gecos
= gecos
;
375 if (setpwnam(ctl
->pw
, ".chfn") < 0) {
376 warn("setpwnam failed");
379 ("Finger information *NOT* changed. Try again later.\n"));
383 printf(_("Finger information changed.\n"));
387 int main(int argc
, char **argv
)
390 struct chfn_control ctl
= {
395 setlocale(LC_ALL
, ""); /* both for messages and for iscntrl() below */
396 bindtextdomain(PACKAGE
, LOCALEDIR
);
398 atexit(close_stdout
);
401 /* check /etc/login.defs CHFN_RESTRICT */
402 get_login_defs(&ctl
);
404 parse_argv(&ctl
, argc
, argv
);
406 ctl
.pw
= getpwuid(uid
);
408 errx(EXIT_FAILURE
, _("you (user %d) don't exist."),
410 ctl
.username
= ctl
.pw
->pw_name
;
412 ctl
.pw
= getpwnam(ctl
.username
);
414 errx(EXIT_FAILURE
, _("user \"%s\" does not exist."),
419 if (!(is_local(ctl
.username
)))
420 errx(EXIT_FAILURE
, _("can only change local entries"));
423 #ifdef HAVE_LIBSELINUX
424 if (is_selinux_enabled() > 0) {
426 access_vector_t av
= get_access_vector("passwd", "chfn");
428 if (selinux_check_passwd_access(av
) != 0) {
429 security_context_t user_context
;
430 if (getprevcon(&user_context
) < 0)
433 _("%s is not authorized to change "
434 "the finger info of %s"),
435 user_context
? : _("Unknown user context"),
439 if (setupDefaultContext(_PATH_PASSWD
))
441 _("can't set default context for %s"), _PATH_PASSWD
);
446 /* If we're setuid and not really root, disallow the password change. */
447 if (geteuid() != getuid() && uid
!= ctl
.pw
->pw_uid
) {
449 if (uid
!= 0 && uid
!= ctl
.pw
->pw_uid
) {
452 err(EXIT_FAILURE
, _("running UID doesn't match UID of user we're "
453 "altering, change denied"));
456 printf(_("Changing finger information for %s.\n"), ctl
.username
);
458 #if !defined(HAVE_LIBUSER) && defined(CHFN_CHSH_PASSWORD)
459 if (!auth_pam("chfn", uid
, ctl
.username
)) {
470 printf(_("Finger information not changed.\n"));
474 return save_new_data(&ctl
) == 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;