2 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2001 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2008, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/types.h>
43 #include <selinux/selinux.h>
44 #include <selinux/av_permissions.h>
52 #include "prototypes.h"
56 #include "exitcodes.h"
62 static char fullnm[BUFSIZ];
63 static char roomno[BUFSIZ];
64 static char workph[BUFSIZ];
65 static char homeph[BUFSIZ];
66 static char slop[BUFSIZ];
69 static bool fflg = false; /* -f - set full name */
70 static bool rflg = false; /* -r - set room number */
71 static bool wflg = false; /* -w - set work phone number */
72 static bool hflg = false; /* -h - set home phone number */
73 static bool oflg = false; /* -o - set other information */
74 static bool pw_locked = false;
77 * External identifiers
80 /* local function prototypes */
81 static void fail_exit (int code);
82 static void usage (void);
83 static bool may_change_field (int);
84 static void new_fields (void);
85 static char *copy_field (char *, char *, char *);
86 static void process_flags (int argc, char **argv);
87 static void check_perms (const struct passwd *pw);
88 static void update_gecos (const char *user, char *gecos);
89 static void get_old_fields (const char *gecos);
92 * fail_exit - exit with an error and do some cleanup
94 static void fail_exit (int code)
97 if (pw_unlock () == 0) {
98 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
99 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
111 * usage - print command line syntax and exit
113 static void usage (void)
117 _("Usage: %s [-f full_name] [-r room_no] "
119 "\t[-h home_ph] [-o other] [user]\n"), Prog);
122 _("Usage: %s [-f full_name] [-r room_no] "
123 "[-w work_ph] [-h home_ph]\n"), Prog);
129 * may_change_field - indicate if the user is allowed to change a given field
130 * of her gecos information
132 * root can change any field.
134 * field should be one of 'f', 'r', 'w', 'h'
136 * Return true if the user can change the field and false otherwise.
138 static bool may_change_field (int field)
143 * CHFN_RESTRICT can now specify exactly which fields may be changed
144 * by regular users, by using any combination of the following
151 * This makes it possible to disallow changing the room number
152 * information, for example - this feature was suggested by Maciej
153 * 'Tycoon' Majchrowski.
155 * For backward compatibility, "yes" is equivalent to "rwh",
156 * "no" is equivalent to "frwh". Only root can change anything
157 * if the string is empty or not defined at all.
163 cp = getdef_str ("CHFN_RESTRICT");
166 } else if (strcmp (cp, "yes") == 0) {
168 } else if (strcmp (cp, "no") == 0) {
172 if (strchr (cp, field) != NULL) {
180 * new_fields - change the user's GECOS information interactively
182 * prompt the user for each of the four fields and fill in the fields from
183 * the user's response, or leave alone if nothing was entered.
185 static void new_fields (void)
187 puts (_("Enter the new value, or press ENTER for the default"));
189 if (may_change_field ('f')) {
190 change_field (fullnm, sizeof fullnm, _("Full Name"));
192 printf (_("\t%s: %s\n"), _("Full Name"), fullnm);
195 if (may_change_field ('r')) {
196 change_field (roomno, sizeof roomno, _("Room Number"));
198 printf (_("\t%s: %s\n"), _("Room Number"), fullnm);
201 if (may_change_field ('w')) {
202 change_field (workph, sizeof workph, _("Work Phone"));
204 printf (_("\t%s: %s\n"), _("Work Phone"), fullnm);
207 if (may_change_field ('h')) {
208 change_field (homeph, sizeof homeph, _("Home Phone"));
210 printf (_("\t%s: %s\n"), _("Home Phone"), fullnm);
214 change_field (slop, sizeof slop, _("Other"));
219 * copy_field - get the next field from the gecos field
221 * copy_field copies the next field from the gecos field, returning a
222 * pointer to the field which follows, or NULL if there are no more fields.
224 * in - the current GECOS field
225 * out - where to copy the field to
226 * extra - fields with '=' get copied here
228 static char *copy_field (char *in, char *out, char *extra)
233 cp = strchr (in, ',');
238 if (strchr (in, '=') == NULL) {
243 if ('\0' != extra[0]) {
251 if ((NULL != in) && (NULL != out)) {
259 * process_flags - parse the command line options
261 * It will not return if an error is encountered.
263 static void process_flags (int argc, char **argv)
265 int flag; /* flag currently being processed */
268 * The remaining arguments will be processed one by one and executed
269 * by this command. The name is the last argument if it does not
270 * begin with a "-", otherwise the name is determined from the
271 * environment and must agree with the real UID. Also, the UID will
272 * be checked for any commands which are restricted to root only.
274 while ((flag = getopt (argc, argv, "f:r:w:h:o:")) != EOF) {
277 if (!may_change_field ('f')) {
279 _("%s: Permission denied.\n"), Prog);
283 STRFCPY (fullnm, optarg);
286 if (!may_change_field ('h')) {
288 _("%s: Permission denied.\n"), Prog);
292 STRFCPY (homeph, optarg);
295 if (!may_change_field ('r')) {
297 _("%s: Permission denied.\n"), Prog);
301 STRFCPY (roomno, optarg);
306 _("%s: Permission denied.\n"), Prog);
310 STRFCPY (slop, optarg);
313 if (!may_change_field ('w')) {
315 _("%s: Permission denied.\n"), Prog);
319 STRFCPY (workph, optarg);
328 * check_perms - check if the caller is allowed to add a group
330 * Non-root users are only allowed to change their gecos field.
331 * (see also may_change_field())
333 * Non-root users must be authenticated.
335 * It will not return if the user is not allowed.
337 static void check_perms (const struct passwd *pw)
340 pam_handle_t *pamh = NULL;
342 struct passwd *pampw;
346 * Non-privileged users are only allowed to change the gecos field
347 * if the UID of the user matches the current real UID.
349 if (!amroot && pw->pw_uid != getuid ()) {
350 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
356 * If the UID of the user does not match the current real UID,
357 * check if the change is allowed by SELinux policy.
359 if ((pw->pw_uid != getuid ())
360 && (is_selinux_enabled () > 0)
361 && (selinux_check_passwd_access (PASSWD__CHFN) != 0)) {
362 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
370 * Non-privileged users are optionally authenticated (must enter the
371 * password of the user whose information is being changed) before
372 * any changes can be made. Idea from util-linux chfn/chsh.
375 if (!amroot && getdef_bool ("CHFN_AUTH")) {
376 passwd_check (pw->pw_name, pw->pw_passwd, "chfn");
380 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
383 _("%s: Cannot determine your user name.\n"),
388 retval = pam_start ("chfn", pampw->pw_name, &conv, &pamh);
390 if (PAM_SUCCESS == retval) {
391 retval = pam_authenticate (pamh, 0);
394 if (PAM_SUCCESS == retval) {
395 retval = pam_acct_mgmt (pamh, 0);
399 (void) pam_end (pamh, retval);
401 if (PAM_SUCCESS != retval) {
402 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
409 * update_gecos - update the gecos fields in the password database
411 * Commit the user's entry after changing her gecos field.
413 static void update_gecos (const char *user, char *gecos)
415 const struct passwd *pw; /* The user's password file entry */
416 struct passwd pwent; /* modified password file entry */
419 * Before going any further, raise the ulimit to prevent colliding
420 * into a lowered ulimit, and set the real UID to root to protect
421 * against unexpected signals. Any keyboard signals are set to be
424 if (setuid (0) != 0) {
425 fputs (_("Cannot change ID to root.\n"), stderr);
426 SYSLOG ((LOG_ERR, "can't setuid(0)"));
427 fail_exit (E_NOPERM);
432 * The passwd entry is now ready to be committed back to the
433 * password file. Get a lock on the file and open it.
435 if (pw_lock () == 0) {
437 _("%s: cannot lock %s; try again later.\n"),
439 fail_exit (E_NOPERM);
442 if (pw_open (O_RDWR) == 0) {
444 _("%s: cannot open %s\n"), Prog, pw_dbname ());
445 fail_exit (E_NOPERM);
449 * Get the entry to update using pw_locate() - we want the real one
450 * from /etc/passwd, not the one from getpwnam() which could contain
451 * the shadow password if (despite the warnings) someone enables
452 * AUTOSHADOW (or SHADOW_COMPAT in libc). --marekm
454 pw = pw_locate (user);
457 _("%s: user '%s' does not exist in %s\n"),
458 Prog, user, pw_dbname ());
459 fail_exit (E_NOPERM);
463 * Make a copy of the entry, then change the gecos field. The other
464 * fields remain unchanged.
467 pwent.pw_gecos = gecos;
470 * Update the passwd file entry. If there is a DBM file, update that
473 if (pw_update (&pwent) == 0) {
475 _("%s: failed to prepare the new %s entry '%s'\n"),
476 Prog, pw_dbname (), pwent.pw_name);
477 fail_exit (E_NOPERM);
481 * Changes have all been made, so commit them and unlock the file.
483 if (pw_close () == 0) {
484 fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, pw_dbname ());
485 SYSLOG ((LOG_ERR, "failure while writing changes to %s", pw_dbname ()));
486 fail_exit (E_NOPERM);
488 if (pw_unlock () == 0) {
489 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
490 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
497 * get_old_fields - parse the old gecos and use the old value for the fields
498 * which are not set on the command line
500 static void get_old_fields (const char *gecos)
502 char *cp; /* temporary character pointer */
503 char old_gecos[BUFSIZ]; /* buffer for old GECOS fields */
504 STRFCPY (old_gecos, gecos);
507 * Now get the full name. It is the first comma separated field in
510 cp = copy_field (old_gecos, fflg ? (char *) 0 : fullnm, slop);
513 * Now get the room number. It is the next comma separated field,
514 * if there is indeed one.
517 cp = copy_field (cp, rflg ? (char *) 0 : roomno, slop);
521 * Now get the work phone number. It is the third field.
524 cp = copy_field (cp, wflg ? (char *) 0 : workph, slop);
528 * Now get the home phone number. It is the fourth field.
531 cp = copy_field (cp, hflg ? (char *) 0 : homeph, slop);
535 * Anything left over is "slop".
537 if ((NULL != cp) && !oflg) {
538 if ('\0' != slop[0]) {
547 * check_fields - check all of the fields for valid information
549 * It will not return if a field is not valid.
551 static void check_fields (void)
554 err = valid_field (fullnm, ":,=\n");
556 fprintf (stderr, _("%s: name with non-ASCII characters: '%s'\n"), Prog, fullnm);
557 } else if (err < 0) {
558 fprintf (stderr, _("%s: invalid name: '%s'\n"), Prog, fullnm);
559 fail_exit (E_NOPERM);
561 err = valid_field (roomno, ":,=\n");
563 fprintf (stderr, _("%s: room number with non-ASCII characters: '%s'\n"), Prog, roomno);
564 } else if (err < 0) {
565 fprintf (stderr, _("%s: invalid room number: '%s'\n"),
567 fail_exit (E_NOPERM);
569 if (valid_field (workph, ":,=\n") != 0) {
570 fprintf (stderr, _("%s: invalid work phone: '%s'\n"),
572 fail_exit (E_NOPERM);
574 if (valid_field (homeph, ":,=\n") != 0) {
575 fprintf (stderr, _("%s: invalid home phone: '%s'\n"),
577 fail_exit (E_NOPERM);
579 err = valid_field (slop, ":\n");
581 fprintf (stderr, _("%s: '%s' contains non-ASCII characters\n"), Prog, slop);
582 } else if (err < 0) {
584 _("%s: '%s' contains illegal characters\n"),
586 fail_exit (E_NOPERM);
591 * chfn - change a user's password file information
593 * This command controls the GECOS field information in the password
596 * The valid options are
600 * -w work phone number
601 * -h home phone number
602 * -o other information (*)
604 * (*) requires root permission to execute.
606 int main (int argc, char **argv)
608 const struct passwd *pw; /* password file entry */
609 char new_gecos[BUFSIZ]; /* buffer for new GECOS fields */
613 (void) setlocale (LC_ALL, "");
614 (void) bindtextdomain (PACKAGE, LOCALEDIR);
615 (void) textdomain (PACKAGE);
618 * This command behaves different for root and non-root
621 amroot = (getuid () == 0);
624 * Get the program name. The program name is used as a
625 * prefix to most error messages.
627 Prog = Basename (argv[0]);
631 /* parse the command line options */
632 process_flags (argc, argv);
635 * Get the name of the user to check. It is either the command line
636 * name, or the name getlogin() returns.
640 pw = xgetpwnam (user);
642 fprintf (stderr, _("%s: user '%s' does not exist\n"), Prog,
644 fail_exit (E_NOPERM);
647 pw = get_my_pwent ();
650 _("%s: Cannot determine your user name.\n"),
652 SYSLOG ((LOG_WARN, "Cannot determine the user name of the caller (UID %lu)",
653 (unsigned long) getuid ()));
654 fail_exit (E_NOPERM);
656 user = xstrdup (pw->pw_name);
661 * Now we make sure this is a LOCAL password entry for this user ...
668 _("%s: cannot change user '%s' on NIS client.\n"),
671 if (!yp_get_default_domain (&nis_domain) &&
672 !yp_master (nis_domain, "passwd.byname", &nis_master)) {
675 ("%s: '%s' is the NIS master for this client.\n"),
678 fail_exit (E_NOPERM);
682 /* Check that the caller is allowed to change the gecos of the
686 /* If some fields were not set on the command line, load the value from
687 * the old gecos fields. */
688 get_old_fields (pw->pw_gecos);
691 * If none of the fields were changed from the command line, let the
692 * user interactively change them.
694 if (!fflg && !rflg && !wflg && !hflg && !oflg) {
695 printf (_("Changing the user information for %s\n"), user);
700 * Check all of the fields for valid information
705 * Build the new GECOS field by plastering all the pieces together,
706 * if they will fit ...
708 if ((strlen (fullnm) + strlen (roomno) + strlen (workph) +
709 strlen (homeph) + strlen (slop)) > (unsigned int) 80) {
710 fprintf (stderr, _("%s: fields too long\n"), Prog);
711 fail_exit (E_NOPERM);
713 snprintf (new_gecos, sizeof new_gecos, "%s,%s,%s,%s%s%s",
714 fullnm, roomno, workph, homeph,
715 ('\0' != slop[0]) ? "," : "", slop);
717 /* Rewrite the user's gecos in the passwd file */
718 update_gecos (user, new_gecos);
720 SYSLOG ((LOG_INFO, "changed user '%s' information", user));
722 nscd_flush_cache ("passwd");