2 * Copyright 1989 - 1994, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/types.h>
40 #include <selinux/selinux.h>
41 #include <selinux/av_permissions.h>
44 #include "exitcodes.h"
50 #include "prototypes.h"
57 static char fullnm[BUFSIZ];
58 static char roomno[BUFSIZ];
59 static char workph[BUFSIZ];
60 static char homeph[BUFSIZ];
61 static char slop[BUFSIZ];
64 static int fflg = 0; /* -f - set full name */
65 static int rflg = 0; /* -r - set room number */
66 static int wflg = 0; /* -w - set work phone number */
67 static int hflg = 0; /* -h - set home phone number */
68 static int oflg = 0; /* -o - set other information */
70 static pam_handle_t *pamh = NULL;
74 * External identifiers
77 /* local function prototypes */
78 static void usage (void);
79 static int may_change_field (int);
80 static void new_fields (void);
81 static char *copy_field (char *, char *, char *);
82 static void process_flags (int argc, char **argv);
83 static void check_perms (const struct passwd *pw);
84 static void update_gecos (const char *user, char *gecos);
85 static void get_old_fields (const char *gecos);
88 * usage - print command line syntax and exit
90 static void usage (void)
94 _("Usage: %s [-f full_name] [-r room_no] "
96 "\t[-h home_ph] [-o other] [user]\n"), Prog);
99 _("Usage: %s [-f full_name] [-r room_no] "
100 "[-w work_ph] [-h home_ph]\n"), Prog);
106 * may_change_field - indicate if the user is allowed to change a given field
107 * of her gecos information
109 * root can change any field.
111 * field should be one of 'f', 'r', 'w', 'h'
113 * Return 1 if the user can change the field and 0 otherwise.
115 static int may_change_field (int field)
120 * CHFN_RESTRICT can now specify exactly which fields may be changed
121 * by regular users, by using any combination of the following
128 * This makes it possible to disallow changing the room number
129 * information, for example - this feature was suggested by Maciej
130 * 'Tycoon' Majchrowski.
132 * For backward compatibility, "yes" is equivalent to "rwh",
133 * "no" is equivalent to "frwh". Only root can change anything
134 * if the string is empty or not defined at all.
140 cp = getdef_str ("CHFN_RESTRICT");
143 } else if (strcmp (cp, "yes") == 0) {
145 } else if (strcmp (cp, "no") == 0) {
149 if (strchr (cp, field) != NULL) {
157 * new_fields - change the user's GECOS information interactively
159 * prompt the user for each of the four fields and fill in the fields from
160 * the user's response, or leave alone if nothing was entered.
162 static void new_fields (void)
164 puts (_("Enter the new value, or press ENTER for the default"));
166 if (may_change_field ('f')) {
167 change_field (fullnm, sizeof fullnm, _("Full Name"));
169 printf (_("\tFull Name: %s\n"), fullnm);
172 if (may_change_field ('r')) {
173 change_field (roomno, sizeof roomno, _("Room Number"));
175 printf (_("\tRoom Number: %s\n"), roomno);
178 if (may_change_field ('w')) {
179 change_field (workph, sizeof workph, _("Work Phone"));
181 printf (_("\tWork Phone: %s\n"), workph);
184 if (may_change_field ('h')) {
185 change_field (homeph, sizeof homeph, _("Home Phone"));
187 printf (_("\tHome Phone: %s\n"), homeph);
191 change_field (slop, sizeof slop, _("Other"));
196 * copy_field - get the next field from the gecos field
198 * copy_field copies the next field from the gecos field, returning a
199 * pointer to the field which follows, or NULL if there are no more fields.
201 * in - the current GECOS field
202 * out - where to copy the field to
203 * extra - fields with '=' get copied here
205 static char *copy_field (char *in, char *out, char *extra)
210 cp = strchr (in, ',');
215 if (strchr (in, '=') == NULL) {
220 if ('\0' != extra[0]) {
228 if ((NULL != in) && (NULL != out)) {
236 * process_flags - parse the command line options
238 * It will not return if an error is encountered.
240 static void process_flags (int argc, char **argv)
242 int flag; /* flag currently being processed */
245 * The remaining arguments will be processed one by one and executed
246 * by this command. The name is the last argument if it does not
247 * begin with a "-", otherwise the name is determined from the
248 * environment and must agree with the real UID. Also, the UID will
249 * be checked for any commands which are restricted to root only.
251 while ((flag = getopt (argc, argv, "f:r:w:h:o:")) != EOF) {
254 if (!may_change_field ('f')) {
256 _("%s: Permission denied.\n"), Prog);
260 STRFCPY (fullnm, optarg);
263 if (!may_change_field ('h')) {
265 _("%s: Permission denied.\n"), Prog);
269 STRFCPY (homeph, optarg);
272 if (!may_change_field ('r')) {
274 _("%s: Permission denied.\n"), Prog);
278 STRFCPY (roomno, optarg);
283 _("%s: Permission denied.\n"), Prog);
287 STRFCPY (slop, optarg);
290 if (!may_change_field ('w')) {
292 _("%s: Permission denied.\n"), Prog);
296 STRFCPY (workph, optarg);
305 * check_perms - check if the caller is allowed to add a group
307 * Non-root users are only allowed to change their gecos field.
308 * (see also may_change_field())
310 * Non-root users must be authenticated.
312 * It will not return if the user is not allowed.
314 static void check_perms (const struct passwd *pw)
318 struct passwd *pampw;
322 * Non-privileged users are only allowed to change the gecos field
323 * if the UID of the user matches the current real UID.
325 if (!amroot && pw->pw_uid != getuid ()) {
326 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
332 * If the UID of the user does not match the current real UID,
333 * check if the change is allowed by SELinux policy.
335 if ((pw->pw_uid != getuid ())
336 && (is_selinux_enabled () > 0)
337 && (selinux_check_passwd_access (PASSWD__CHFN) != 0)) {
338 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
346 * Non-privileged users are optionally authenticated (must enter the
347 * password of the user whose information is being changed) before
348 * any changes can be made. Idea from util-linux chfn/chsh.
351 if (!amroot && getdef_bool ("CHFN_AUTH")) {
352 passwd_check (pw->pw_name, pw->pw_passwd, "chfn");
356 retval = PAM_SUCCESS;
358 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
360 retval = PAM_USER_UNKNOWN;
363 if (retval == PAM_SUCCESS) {
364 retval = pam_start ("chfn", pampw->pw_name, &conv, &pamh);
367 if (retval == PAM_SUCCESS) {
368 retval = pam_authenticate (pamh, 0);
369 if (retval != PAM_SUCCESS) {
370 pam_end (pamh, retval);
374 if (retval == PAM_SUCCESS) {
375 retval = pam_acct_mgmt (pamh, 0);
376 if (retval != PAM_SUCCESS) {
377 pam_end (pamh, retval);
381 if (retval != PAM_SUCCESS) {
382 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
389 * update_gecos - update the gecos fields in the password database
391 * Commit the user's entry after changing her gecos field.
393 static void update_gecos (const char *user, char *gecos)
395 const struct passwd *pw; /* The user's password file entry */
396 struct passwd pwent; /* modified password file entry */
399 * Before going any further, raise the ulimit to prevent colliding
400 * into a lowered ulimit, and set the real UID to root to protect
401 * against unexpected signals. Any keyboard signals are set to be
404 if (setuid (0) != 0) {
405 fputs (_("Cannot change ID to root.\n"), stderr);
406 SYSLOG ((LOG_ERR, "can't setuid(0)"));
413 * The passwd entry is now ready to be committed back to the
414 * password file. Get a lock on the file and open it.
416 if (pw_lock () == 0) {
417 fputs (_("Cannot lock the password file; try again later.\n"),
419 SYSLOG ((LOG_WARN, "can't lock /etc/passwd"));
423 if (pw_open (O_RDWR) == 0) {
424 fputs (_("Cannot open the password file.\n"), stderr);
426 SYSLOG ((LOG_ERR, "can't open /etc/passwd"));
432 * Get the entry to update using pw_locate() - we want the real one
433 * from /etc/passwd, not the one from getpwnam() which could contain
434 * the shadow password if (despite the warnings) someone enables
435 * AUTOSHADOW (or SHADOW_COMPAT in libc). --marekm
437 pw = pw_locate (user);
441 _("%s: %s not found in /etc/passwd\n"), Prog, user);
446 * Make a copy of the entry, then change the gecos field. The other
447 * fields remain unchanged.
450 pwent.pw_gecos = gecos;
453 * Update the passwd file entry. If there is a DBM file, update that
456 if (pw_update (&pwent) == 0) {
457 fputs (_("Error updating the password entry.\n"), stderr);
459 SYSLOG ((LOG_ERR, "error updating passwd entry"));
465 * Changes have all been made, so commit them and unlock the file.
467 if (pw_close () == 0) {
468 fputs (_("Cannot commit password file changes.\n"), stderr);
470 SYSLOG ((LOG_ERR, "can't rewrite /etc/passwd"));
474 if (pw_unlock () == 0) {
475 fputs (_("Cannot unlock the password file.\n"), stderr);
476 SYSLOG ((LOG_ERR, "can't unlock /etc/passwd"));
483 * get_old_fields - parse the old gecos and use the old value for the fields
484 * which are not set on the command line
486 static void get_old_fields (const char *gecos)
488 char *cp; /* temporary character pointer */
489 char old_gecos[BUFSIZ]; /* buffer for old GECOS fields */
490 STRFCPY (old_gecos, gecos);
493 * Now get the full name. It is the first comma separated field in
496 cp = copy_field (old_gecos, fflg ? (char *) 0 : fullnm, slop);
499 * Now get the room number. It is the next comma separated field,
500 * if there is indeed one.
503 cp = copy_field (cp, rflg ? (char *) 0 : roomno, slop);
507 * Now get the work phone number. It is the third field.
510 cp = copy_field (cp, wflg ? (char *) 0 : workph, slop);
514 * Now get the home phone number. It is the fourth field.
517 cp = copy_field (cp, hflg ? (char *) 0 : homeph, slop);
521 * Anything left over is "slop".
523 if ((NULL != cp) && !oflg) {
524 if ('\0' != slop[0]) {
533 * check_fields - check all of the fields for valid information
535 * It will not return if a field is not valid.
537 static void check_fields (void)
540 err = valid_field (fullnm, ":,=");
542 fprintf (stderr, _("%s: name with non-ASCII characters: '%s'\n"), Prog, fullnm);
543 } else if (err < 0) {
544 fprintf (stderr, _("%s: invalid name: '%s'\n"), Prog, fullnm);
548 err = valid_field (roomno, ":,=");
550 fprintf (stderr, _("%s: room number with non-ASCII characters: '%s'\n"), Prog, roomno);
551 } else if (err < 0) {
552 fprintf (stderr, _("%s: invalid room number: '%s'\n"),
557 if (valid_field (workph, ":,=")) {
558 fprintf (stderr, _("%s: invalid work phone: '%s'\n"),
563 if (valid_field (homeph, ":,=")) {
564 fprintf (stderr, _("%s: invalid home phone: '%s'\n"),
569 err = valid_field (slop, ":");
571 fprintf (stderr, _("%s: '%s' contains non-ASCII characters\n"), Prog, slop);
572 } else if (err < 0) {
574 _("%s: '%s' contains illegal characters\n"),
582 * chfn - change a user's password file information
584 * This command controls the GECOS field information in the password
587 * The valid options are
591 * -w work phone number
592 * -h home phone number
593 * -o other information (*)
595 * (*) requires root permission to execute.
597 int main (int argc, char **argv)
599 const struct passwd *pw; /* password file entry */
600 char new_gecos[BUFSIZ]; /* buffer for new GECOS fields */
604 setlocale (LC_ALL, "");
605 bindtextdomain (PACKAGE, LOCALEDIR);
606 textdomain (PACKAGE);
609 * This command behaves different for root and non-root
612 amroot = (getuid () == 0);
615 * Get the program name. The program name is used as a
616 * prefix to most error messages.
618 Prog = Basename (argv[0]);
622 /* parse the command line options */
623 process_flags (argc, argv);
626 * Get the name of the user to check. It is either the command line
627 * name, or the name getlogin() returns.
631 pw = xgetpwnam (user);
633 fprintf (stderr, _("%s: unknown user %s\n"), Prog,
638 pw = get_my_pwent ();
642 ("%s: Cannot determine your user name.\n"),
646 user = xstrdup (pw->pw_name);
651 * Now we make sure this is a LOCAL password entry for this user ...
658 _("%s: cannot change user '%s' on NIS client.\n"),
661 if (!yp_get_default_domain (&nis_domain) &&
662 !yp_master (nis_domain, "passwd.byname", &nis_master)) {
665 ("%s: '%s' is the NIS master for this client.\n"),
672 /* Check that the caller is allowed to change the gecos of the
676 /* If some fields were not set on the command line, load the value from
677 * the old gecos fields. */
678 get_old_fields (pw->pw_gecos);
681 * If none of the fields were changed from the command line, let the
682 * user interactively change them.
684 if (!fflg && !rflg && !wflg && !hflg && !oflg) {
685 printf (_("Changing the user information for %s\n"), user);
690 * Check all of the fields for valid information
695 * Build the new GECOS field by plastering all the pieces together,
696 * if they will fit ...
698 if ((strlen (fullnm) + strlen (roomno) + strlen (workph) +
699 strlen (homeph) + strlen (slop)) > (unsigned int) 80) {
700 fprintf (stderr, _("%s: fields too long\n"), Prog);
704 snprintf (new_gecos, sizeof new_gecos, "%s,%s,%s,%s%s%s",
705 fullnm, roomno, workph, homeph, slop[0] ? "," : "", slop);
707 /* Rewrite the user's gecos in the passwd file */
708 update_gecos (user, new_gecos);
710 SYSLOG ((LOG_INFO, "changed user `%s' information", user));
712 nscd_flush_cache ("passwd");
715 pam_end (pamh, PAM_SUCCESS);