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>
47 #include "exitcodes.h"
53 #include "prototypes.h"
60 static char fullnm[BUFSIZ];
61 static char roomno[BUFSIZ];
62 static char workph[BUFSIZ];
63 static char homeph[BUFSIZ];
64 static char slop[BUFSIZ];
67 static bool fflg = false; /* -f - set full name */
68 static bool rflg = false; /* -r - set room number */
69 static bool wflg = false; /* -w - set work phone number */
70 static bool hflg = false; /* -h - set home phone number */
71 static bool oflg = false; /* -o - set other information */
73 static pam_handle_t *pamh = NULL;
75 static bool pw_locked = false;
78 * External identifiers
81 /* local function prototypes */
82 static void fail_exit (int code);
83 static void usage (void);
84 static bool may_change_field (int);
85 static void new_fields (void);
86 static char *copy_field (char *, char *, char *);
87 static void process_flags (int argc, char **argv);
88 static void check_perms (const struct passwd *pw);
89 static void update_gecos (const char *user, char *gecos);
90 static void get_old_fields (const char *gecos);
93 * fail_exit - exit with an error and do some cleanup
95 static void fail_exit (int code)
98 if (pw_unlock () == 0) {
99 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
100 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
112 * usage - print command line syntax and exit
114 static void usage (void)
118 _("Usage: %s [-f full_name] [-r room_no] "
120 "\t[-h home_ph] [-o other] [user]\n"), Prog);
123 _("Usage: %s [-f full_name] [-r room_no] "
124 "[-w work_ph] [-h home_ph]\n"), Prog);
130 * may_change_field - indicate if the user is allowed to change a given field
131 * of her gecos information
133 * root can change any field.
135 * field should be one of 'f', 'r', 'w', 'h'
137 * Return true if the user can change the field and false otherwise.
139 static bool may_change_field (int field)
144 * CHFN_RESTRICT can now specify exactly which fields may be changed
145 * by regular users, by using any combination of the following
152 * This makes it possible to disallow changing the room number
153 * information, for example - this feature was suggested by Maciej
154 * 'Tycoon' Majchrowski.
156 * For backward compatibility, "yes" is equivalent to "rwh",
157 * "no" is equivalent to "frwh". Only root can change anything
158 * if the string is empty or not defined at all.
164 cp = getdef_str ("CHFN_RESTRICT");
167 } else if (strcmp (cp, "yes") == 0) {
169 } else if (strcmp (cp, "no") == 0) {
173 if (strchr (cp, field) != NULL) {
181 * new_fields - change the user's GECOS information interactively
183 * prompt the user for each of the four fields and fill in the fields from
184 * the user's response, or leave alone if nothing was entered.
186 static void new_fields (void)
188 puts (_("Enter the new value, or press ENTER for the default"));
190 if (may_change_field ('f')) {
191 change_field (fullnm, sizeof fullnm, _("Full Name"));
193 printf (_("\tFull Name: %s\n"), fullnm);
196 if (may_change_field ('r')) {
197 change_field (roomno, sizeof roomno, _("Room Number"));
199 printf (_("\tRoom Number: %s\n"), roomno);
202 if (may_change_field ('w')) {
203 change_field (workph, sizeof workph, _("Work Phone"));
205 printf (_("\tWork Phone: %s\n"), workph);
208 if (may_change_field ('h')) {
209 change_field (homeph, sizeof homeph, _("Home Phone"));
211 printf (_("\tHome Phone: %s\n"), homeph);
215 change_field (slop, sizeof slop, _("Other"));
220 * copy_field - get the next field from the gecos field
222 * copy_field copies the next field from the gecos field, returning a
223 * pointer to the field which follows, or NULL if there are no more fields.
225 * in - the current GECOS field
226 * out - where to copy the field to
227 * extra - fields with '=' get copied here
229 static char *copy_field (char *in, char *out, char *extra)
234 cp = strchr (in, ',');
239 if (strchr (in, '=') == NULL) {
244 if ('\0' != extra[0]) {
252 if ((NULL != in) && (NULL != out)) {
260 * process_flags - parse the command line options
262 * It will not return if an error is encountered.
264 static void process_flags (int argc, char **argv)
266 int flag; /* flag currently being processed */
269 * The remaining arguments will be processed one by one and executed
270 * by this command. The name is the last argument if it does not
271 * begin with a "-", otherwise the name is determined from the
272 * environment and must agree with the real UID. Also, the UID will
273 * be checked for any commands which are restricted to root only.
275 while ((flag = getopt (argc, argv, "f:r:w:h:o:")) != EOF) {
278 if (!may_change_field ('f')) {
280 _("%s: Permission denied.\n"), Prog);
284 STRFCPY (fullnm, optarg);
287 if (!may_change_field ('h')) {
289 _("%s: Permission denied.\n"), Prog);
293 STRFCPY (homeph, optarg);
296 if (!may_change_field ('r')) {
298 _("%s: Permission denied.\n"), Prog);
302 STRFCPY (roomno, optarg);
307 _("%s: Permission denied.\n"), Prog);
311 STRFCPY (slop, optarg);
314 if (!may_change_field ('w')) {
316 _("%s: Permission denied.\n"), Prog);
320 STRFCPY (workph, optarg);
329 * check_perms - check if the caller is allowed to add a group
331 * Non-root users are only allowed to change their gecos field.
332 * (see also may_change_field())
334 * Non-root users must be authenticated.
336 * It will not return if the user is not allowed.
338 static void check_perms (const struct passwd *pw)
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 retval = PAM_SUCCESS;
382 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
384 retval = PAM_USER_UNKNOWN;
387 if (retval == PAM_SUCCESS) {
388 retval = pam_start ("chfn", pampw->pw_name, &conv, &pamh);
391 if (retval == PAM_SUCCESS) {
392 retval = pam_authenticate (pamh, 0);
393 if (retval != PAM_SUCCESS) {
394 pam_end (pamh, retval);
398 if (retval == PAM_SUCCESS) {
399 retval = pam_acct_mgmt (pamh, 0);
400 if (retval != PAM_SUCCESS) {
401 pam_end (pamh, retval);
405 if (retval != PAM_SUCCESS) {
406 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
413 * update_gecos - update the gecos fields in the password database
415 * Commit the user's entry after changing her gecos field.
417 static void update_gecos (const char *user, char *gecos)
419 const struct passwd *pw; /* The user's password file entry */
420 struct passwd pwent; /* modified password file entry */
423 * Before going any further, raise the ulimit to prevent colliding
424 * into a lowered ulimit, and set the real UID to root to protect
425 * against unexpected signals. Any keyboard signals are set to be
428 if (setuid (0) != 0) {
429 fputs (_("Cannot change ID to root.\n"), stderr);
430 SYSLOG ((LOG_ERR, "can't setuid(0)"));
431 fail_exit (E_NOPERM);
436 * The passwd entry is now ready to be committed back to the
437 * password file. Get a lock on the file and open it.
439 if (pw_lock () == 0) {
441 _("%s: cannot lock %s; try again later.\n"),
443 fail_exit (E_NOPERM);
446 if (pw_open (O_RDWR) == 0) {
448 _("%s: cannot open %s\n"), Prog, pw_dbname ());
449 fail_exit (E_NOPERM);
453 * Get the entry to update using pw_locate() - we want the real one
454 * from /etc/passwd, not the one from getpwnam() which could contain
455 * the shadow password if (despite the warnings) someone enables
456 * AUTOSHADOW (or SHADOW_COMPAT in libc). --marekm
458 pw = pw_locate (user);
461 _("%s: user '%s' does not exist in %s\n"),
462 Prog, user, pw_dbname ());
463 fail_exit (E_NOPERM);
467 * Make a copy of the entry, then change the gecos field. The other
468 * fields remain unchanged.
471 pwent.pw_gecos = gecos;
474 * Update the passwd file entry. If there is a DBM file, update that
477 if (pw_update (&pwent) == 0) {
479 _("%s: failed to prepare the new %s entry\n"),
481 fail_exit (E_NOPERM);
485 * Changes have all been made, so commit them and unlock the file.
487 if (pw_close () == 0) {
488 fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, pw_dbname ());
489 SYSLOG ((LOG_ERR, "failure while writing changes to %s", pw_dbname ()));
490 fail_exit (E_NOPERM);
492 if (pw_unlock () == 0) {
493 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
494 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
501 * get_old_fields - parse the old gecos and use the old value for the fields
502 * which are not set on the command line
504 static void get_old_fields (const char *gecos)
506 char *cp; /* temporary character pointer */
507 char old_gecos[BUFSIZ]; /* buffer for old GECOS fields */
508 STRFCPY (old_gecos, gecos);
511 * Now get the full name. It is the first comma separated field in
514 cp = copy_field (old_gecos, fflg ? (char *) 0 : fullnm, slop);
517 * Now get the room number. It is the next comma separated field,
518 * if there is indeed one.
521 cp = copy_field (cp, rflg ? (char *) 0 : roomno, slop);
525 * Now get the work phone number. It is the third field.
528 cp = copy_field (cp, wflg ? (char *) 0 : workph, slop);
532 * Now get the home phone number. It is the fourth field.
535 cp = copy_field (cp, hflg ? (char *) 0 : homeph, slop);
539 * Anything left over is "slop".
541 if ((NULL != cp) && !oflg) {
542 if ('\0' != slop[0]) {
551 * check_fields - check all of the fields for valid information
553 * It will not return if a field is not valid.
555 static void check_fields (void)
558 err = valid_field (fullnm, ":,=");
560 fprintf (stderr, _("%s: name with non-ASCII characters: '%s'\n"), Prog, fullnm);
561 } else if (err < 0) {
562 fprintf (stderr, _("%s: invalid name: '%s'\n"), Prog, fullnm);
563 fail_exit (E_NOPERM);
565 err = valid_field (roomno, ":,=");
567 fprintf (stderr, _("%s: room number with non-ASCII characters: '%s'\n"), Prog, roomno);
568 } else if (err < 0) {
569 fprintf (stderr, _("%s: invalid room number: '%s'\n"),
571 fail_exit (E_NOPERM);
573 if (valid_field (workph, ":,=") != 0) {
574 fprintf (stderr, _("%s: invalid work phone: '%s'\n"),
576 fail_exit (E_NOPERM);
578 if (valid_field (homeph, ":,=") != 0) {
579 fprintf (stderr, _("%s: invalid home phone: '%s'\n"),
581 fail_exit (E_NOPERM);
583 err = valid_field (slop, ":");
585 fprintf (stderr, _("%s: '%s' contains non-ASCII characters\n"), Prog, slop);
586 } else if (err < 0) {
588 _("%s: '%s' contains illegal characters\n"),
590 fail_exit (E_NOPERM);
595 * chfn - change a user's password file information
597 * This command controls the GECOS field information in the password
600 * The valid options are
604 * -w work phone number
605 * -h home phone number
606 * -o other information (*)
608 * (*) requires root permission to execute.
610 int main (int argc, char **argv)
612 const struct passwd *pw; /* password file entry */
613 char new_gecos[BUFSIZ]; /* buffer for new GECOS fields */
617 (void) setlocale (LC_ALL, "");
618 (void) bindtextdomain (PACKAGE, LOCALEDIR);
619 (void) textdomain (PACKAGE);
622 * This command behaves different for root and non-root
625 amroot = (getuid () == 0);
628 * Get the program name. The program name is used as a
629 * prefix to most error messages.
631 Prog = Basename (argv[0]);
635 /* parse the command line options */
636 process_flags (argc, argv);
639 * Get the name of the user to check. It is either the command line
640 * name, or the name getlogin() returns.
644 pw = xgetpwnam (user);
646 fprintf (stderr, _("%s: user '%s' does not exist\n"), Prog,
648 fail_exit (E_NOPERM);
651 pw = get_my_pwent ();
654 _("%s: Cannot determine your user name.\n"),
656 SYSLOG ((LOG_WARN, "Cannot determine the user name of the caller (UID %lu)",
657 (unsigned long) getuid ()));
658 fail_exit (E_NOPERM);
660 user = xstrdup (pw->pw_name);
665 * Now we make sure this is a LOCAL password entry for this user ...
672 _("%s: cannot change user '%s' on NIS client.\n"),
675 if (!yp_get_default_domain (&nis_domain) &&
676 !yp_master (nis_domain, "passwd.byname", &nis_master)) {
679 ("%s: '%s' is the NIS master for this client.\n"),
682 fail_exit (E_NOPERM);
686 /* Check that the caller is allowed to change the gecos of the
690 /* If some fields were not set on the command line, load the value from
691 * the old gecos fields. */
692 get_old_fields (pw->pw_gecos);
695 * If none of the fields were changed from the command line, let the
696 * user interactively change them.
698 if (!fflg && !rflg && !wflg && !hflg && !oflg) {
699 printf (_("Changing the user information for %s\n"), user);
704 * Check all of the fields for valid information
709 * Build the new GECOS field by plastering all the pieces together,
710 * if they will fit ...
712 if ((strlen (fullnm) + strlen (roomno) + strlen (workph) +
713 strlen (homeph) + strlen (slop)) > (unsigned int) 80) {
714 fprintf (stderr, _("%s: fields too long\n"), Prog);
715 fail_exit (E_NOPERM);
717 snprintf (new_gecos, sizeof new_gecos, "%s,%s,%s,%s%s%s",
718 fullnm, roomno, workph, homeph,
719 ('\0' != slop[0]) ? "," : "", slop);
721 /* Rewrite the user's gecos in the passwd file */
722 update_gecos (user, new_gecos);
724 SYSLOG ((LOG_INFO, "changed user '%s' information", user));
726 nscd_flush_cache ("passwd");
729 (void) pam_end (pamh, PAM_SUCCESS);