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 int fflg = 0; /* -f - set full name */
68 static int rflg = 0; /* -r - set room number */
69 static int wflg = 0; /* -w - set work phone number */
70 static int hflg = 0; /* -h - set home phone number */
71 static int oflg = 0; /* -o - set other information */
73 static pam_handle_t *pamh = NULL;
77 * External identifiers
80 /* local function prototypes */
81 static void usage (void);
82 static int may_change_field (int);
83 static void new_fields (void);
84 static char *copy_field (char *, char *, char *);
85 static void process_flags (int argc, char **argv);
86 static void check_perms (const struct passwd *pw);
87 static void update_gecos (const char *user, char *gecos);
88 static void get_old_fields (const char *gecos);
91 * usage - print command line syntax and exit
93 static void usage (void)
97 _("Usage: %s [-f full_name] [-r room_no] "
99 "\t[-h home_ph] [-o other] [user]\n"), Prog);
102 _("Usage: %s [-f full_name] [-r room_no] "
103 "[-w work_ph] [-h home_ph]\n"), Prog);
109 * may_change_field - indicate if the user is allowed to change a given field
110 * of her gecos information
112 * root can change any field.
114 * field should be one of 'f', 'r', 'w', 'h'
116 * Return 1 if the user can change the field and 0 otherwise.
118 static int may_change_field (int field)
123 * CHFN_RESTRICT can now specify exactly which fields may be changed
124 * by regular users, by using any combination of the following
131 * This makes it possible to disallow changing the room number
132 * information, for example - this feature was suggested by Maciej
133 * 'Tycoon' Majchrowski.
135 * For backward compatibility, "yes" is equivalent to "rwh",
136 * "no" is equivalent to "frwh". Only root can change anything
137 * if the string is empty or not defined at all.
143 cp = getdef_str ("CHFN_RESTRICT");
146 } else if (strcmp (cp, "yes") == 0) {
148 } else if (strcmp (cp, "no") == 0) {
152 if (strchr (cp, field) != NULL) {
160 * new_fields - change the user's GECOS information interactively
162 * prompt the user for each of the four fields and fill in the fields from
163 * the user's response, or leave alone if nothing was entered.
165 static void new_fields (void)
167 puts (_("Enter the new value, or press ENTER for the default"));
169 if (may_change_field ('f')) {
170 change_field (fullnm, sizeof fullnm, _("Full Name"));
172 printf (_("\tFull Name: %s\n"), fullnm);
175 if (may_change_field ('r')) {
176 change_field (roomno, sizeof roomno, _("Room Number"));
178 printf (_("\tRoom Number: %s\n"), roomno);
181 if (may_change_field ('w')) {
182 change_field (workph, sizeof workph, _("Work Phone"));
184 printf (_("\tWork Phone: %s\n"), workph);
187 if (may_change_field ('h')) {
188 change_field (homeph, sizeof homeph, _("Home Phone"));
190 printf (_("\tHome Phone: %s\n"), homeph);
194 change_field (slop, sizeof slop, _("Other"));
199 * copy_field - get the next field from the gecos field
201 * copy_field copies the next field from the gecos field, returning a
202 * pointer to the field which follows, or NULL if there are no more fields.
204 * in - the current GECOS field
205 * out - where to copy the field to
206 * extra - fields with '=' get copied here
208 static char *copy_field (char *in, char *out, char *extra)
213 cp = strchr (in, ',');
218 if (strchr (in, '=') == NULL) {
223 if ('\0' != extra[0]) {
231 if ((NULL != in) && (NULL != out)) {
239 * process_flags - parse the command line options
241 * It will not return if an error is encountered.
243 static void process_flags (int argc, char **argv)
245 int flag; /* flag currently being processed */
248 * The remaining arguments will be processed one by one and executed
249 * by this command. The name is the last argument if it does not
250 * begin with a "-", otherwise the name is determined from the
251 * environment and must agree with the real UID. Also, the UID will
252 * be checked for any commands which are restricted to root only.
254 while ((flag = getopt (argc, argv, "f:r:w:h:o:")) != EOF) {
257 if (!may_change_field ('f')) {
259 _("%s: Permission denied.\n"), Prog);
263 STRFCPY (fullnm, optarg);
266 if (!may_change_field ('h')) {
268 _("%s: Permission denied.\n"), Prog);
272 STRFCPY (homeph, optarg);
275 if (!may_change_field ('r')) {
277 _("%s: Permission denied.\n"), Prog);
281 STRFCPY (roomno, optarg);
286 _("%s: Permission denied.\n"), Prog);
290 STRFCPY (slop, optarg);
293 if (!may_change_field ('w')) {
295 _("%s: Permission denied.\n"), Prog);
299 STRFCPY (workph, optarg);
308 * check_perms - check if the caller is allowed to add a group
310 * Non-root users are only allowed to change their gecos field.
311 * (see also may_change_field())
313 * Non-root users must be authenticated.
315 * It will not return if the user is not allowed.
317 static void check_perms (const struct passwd *pw)
321 struct passwd *pampw;
325 * Non-privileged users are only allowed to change the gecos field
326 * if the UID of the user matches the current real UID.
328 if (!amroot && pw->pw_uid != getuid ()) {
329 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
335 * If the UID of the user does not match the current real UID,
336 * check if the change is allowed by SELinux policy.
338 if ((pw->pw_uid != getuid ())
339 && (is_selinux_enabled () > 0)
340 && (selinux_check_passwd_access (PASSWD__CHFN) != 0)) {
341 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
349 * Non-privileged users are optionally authenticated (must enter the
350 * password of the user whose information is being changed) before
351 * any changes can be made. Idea from util-linux chfn/chsh.
354 if (!amroot && getdef_bool ("CHFN_AUTH")) {
355 passwd_check (pw->pw_name, pw->pw_passwd, "chfn");
359 retval = PAM_SUCCESS;
361 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
363 retval = PAM_USER_UNKNOWN;
366 if (retval == PAM_SUCCESS) {
367 retval = pam_start ("chfn", pampw->pw_name, &conv, &pamh);
370 if (retval == PAM_SUCCESS) {
371 retval = pam_authenticate (pamh, 0);
372 if (retval != PAM_SUCCESS) {
373 pam_end (pamh, retval);
377 if (retval == PAM_SUCCESS) {
378 retval = pam_acct_mgmt (pamh, 0);
379 if (retval != PAM_SUCCESS) {
380 pam_end (pamh, retval);
384 if (retval != PAM_SUCCESS) {
385 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
392 * update_gecos - update the gecos fields in the password database
394 * Commit the user's entry after changing her gecos field.
396 static void update_gecos (const char *user, char *gecos)
398 const struct passwd *pw; /* The user's password file entry */
399 struct passwd pwent; /* modified password file entry */
402 * Before going any further, raise the ulimit to prevent colliding
403 * into a lowered ulimit, and set the real UID to root to protect
404 * against unexpected signals. Any keyboard signals are set to be
407 if (setuid (0) != 0) {
408 fputs (_("Cannot change ID to root.\n"), stderr);
409 SYSLOG ((LOG_ERR, "can't setuid(0)"));
416 * The passwd entry is now ready to be committed back to the
417 * password file. Get a lock on the file and open it.
419 if (pw_lock () == 0) {
420 fputs (_("Cannot lock the password file; try again later.\n"),
422 SYSLOG ((LOG_WARN, "can't lock /etc/passwd"));
426 if (pw_open (O_RDWR) == 0) {
427 fputs (_("Cannot open the password file.\n"), stderr);
429 SYSLOG ((LOG_ERR, "can't open /etc/passwd"));
435 * Get the entry to update using pw_locate() - we want the real one
436 * from /etc/passwd, not the one from getpwnam() which could contain
437 * the shadow password if (despite the warnings) someone enables
438 * AUTOSHADOW (or SHADOW_COMPAT in libc). --marekm
440 pw = pw_locate (user);
444 _("%s: %s not found in /etc/passwd\n"), Prog, user);
449 * Make a copy of the entry, then change the gecos field. The other
450 * fields remain unchanged.
453 pwent.pw_gecos = gecos;
456 * Update the passwd file entry. If there is a DBM file, update that
459 if (pw_update (&pwent) == 0) {
460 fputs (_("Error updating the password entry.\n"), stderr);
462 SYSLOG ((LOG_ERR, "error updating passwd entry"));
468 * Changes have all been made, so commit them and unlock the file.
470 if (pw_close () == 0) {
471 fputs (_("Cannot commit password file changes.\n"), stderr);
473 SYSLOG ((LOG_ERR, "can't rewrite /etc/passwd"));
477 if (pw_unlock () == 0) {
478 fputs (_("Cannot unlock the password file.\n"), stderr);
479 SYSLOG ((LOG_ERR, "can't unlock /etc/passwd"));
486 * get_old_fields - parse the old gecos and use the old value for the fields
487 * which are not set on the command line
489 static void get_old_fields (const char *gecos)
491 char *cp; /* temporary character pointer */
492 char old_gecos[BUFSIZ]; /* buffer for old GECOS fields */
493 STRFCPY (old_gecos, gecos);
496 * Now get the full name. It is the first comma separated field in
499 cp = copy_field (old_gecos, fflg ? (char *) 0 : fullnm, slop);
502 * Now get the room number. It is the next comma separated field,
503 * if there is indeed one.
506 cp = copy_field (cp, rflg ? (char *) 0 : roomno, slop);
510 * Now get the work phone number. It is the third field.
513 cp = copy_field (cp, wflg ? (char *) 0 : workph, slop);
517 * Now get the home phone number. It is the fourth field.
520 cp = copy_field (cp, hflg ? (char *) 0 : homeph, slop);
524 * Anything left over is "slop".
526 if ((NULL != cp) && !oflg) {
527 if ('\0' != slop[0]) {
536 * check_fields - check all of the fields for valid information
538 * It will not return if a field is not valid.
540 static void check_fields (void)
543 err = valid_field (fullnm, ":,=");
545 fprintf (stderr, _("%s: name with non-ASCII characters: '%s'\n"), Prog, fullnm);
546 } else if (err < 0) {
547 fprintf (stderr, _("%s: invalid name: '%s'\n"), Prog, fullnm);
551 err = valid_field (roomno, ":,=");
553 fprintf (stderr, _("%s: room number with non-ASCII characters: '%s'\n"), Prog, roomno);
554 } else if (err < 0) {
555 fprintf (stderr, _("%s: invalid room number: '%s'\n"),
560 if (valid_field (workph, ":,=")) {
561 fprintf (stderr, _("%s: invalid work phone: '%s'\n"),
566 if (valid_field (homeph, ":,=")) {
567 fprintf (stderr, _("%s: invalid home phone: '%s'\n"),
572 err = valid_field (slop, ":");
574 fprintf (stderr, _("%s: '%s' contains non-ASCII characters\n"), Prog, slop);
575 } else if (err < 0) {
577 _("%s: '%s' contains illegal characters\n"),
585 * chfn - change a user's password file information
587 * This command controls the GECOS field information in the password
590 * The valid options are
594 * -w work phone number
595 * -h home phone number
596 * -o other information (*)
598 * (*) requires root permission to execute.
600 int main (int argc, char **argv)
602 const struct passwd *pw; /* password file entry */
603 char new_gecos[BUFSIZ]; /* buffer for new GECOS fields */
607 setlocale (LC_ALL, "");
608 bindtextdomain (PACKAGE, LOCALEDIR);
609 textdomain (PACKAGE);
612 * This command behaves different for root and non-root
615 amroot = (getuid () == 0);
618 * Get the program name. The program name is used as a
619 * prefix to most error messages.
621 Prog = Basename (argv[0]);
625 /* parse the command line options */
626 process_flags (argc, argv);
629 * Get the name of the user to check. It is either the command line
630 * name, or the name getlogin() returns.
634 pw = xgetpwnam (user);
636 fprintf (stderr, _("%s: unknown user %s\n"), Prog,
641 pw = get_my_pwent ();
645 ("%s: Cannot determine your user name.\n"),
649 user = xstrdup (pw->pw_name);
654 * Now we make sure this is a LOCAL password entry for this user ...
661 _("%s: cannot change user '%s' on NIS client.\n"),
664 if (!yp_get_default_domain (&nis_domain) &&
665 !yp_master (nis_domain, "passwd.byname", &nis_master)) {
668 ("%s: '%s' is the NIS master for this client.\n"),
675 /* Check that the caller is allowed to change the gecos of the
679 /* If some fields were not set on the command line, load the value from
680 * the old gecos fields. */
681 get_old_fields (pw->pw_gecos);
684 * If none of the fields were changed from the command line, let the
685 * user interactively change them.
687 if (!fflg && !rflg && !wflg && !hflg && !oflg) {
688 printf (_("Changing the user information for %s\n"), user);
693 * Check all of the fields for valid information
698 * Build the new GECOS field by plastering all the pieces together,
699 * if they will fit ...
701 if ((strlen (fullnm) + strlen (roomno) + strlen (workph) +
702 strlen (homeph) + strlen (slop)) > (unsigned int) 80) {
703 fprintf (stderr, _("%s: fields too long\n"), Prog);
707 snprintf (new_gecos, sizeof new_gecos, "%s,%s,%s,%s%s%s",
708 fullnm, roomno, workph, homeph, slop[0] ? "," : "", slop);
710 /* Rewrite the user's gecos in the passwd file */
711 update_gecos (user, new_gecos);
713 SYSLOG ((LOG_INFO, "changed user `%s' information", user));
715 nscd_flush_cache ("passwd");
718 pam_end (pamh, PAM_SUCCESS);