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
32 #ident "$Id: chfn.c,v 1.41 2006/05/07 17:44:39 kloczek Exp $"
38 #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];
68 * External identifiers
71 /* local function prototypes */
72 static void usage (void);
73 static int may_change_field (int);
74 static void new_fields (void);
75 static char *copy_field (char *, char *, char *);
78 * usage - print command line syntax and exit
80 static void usage (void)
84 _("Usage: %s [-f full_name] [-r room_no] "
86 "\t[-h home_ph] [-o other] [user]\n"), Prog);
89 _("Usage: %s [-f full_name] [-r room_no] "
90 "[-w work_ph] [-h home_ph]\n"), Prog);
95 static int may_change_field (int field)
100 * CHFN_RESTRICT can now specify exactly which fields may be changed
101 * by regular users, by using any combination of the following
108 * This makes it possible to disallow changing the room number
109 * information, for example - this feature was suggested by Maciej
110 * 'Tycoon' Majchrowski.
112 * For backward compatibility, "yes" is equivalent to "rwh",
113 * "no" is equivalent to "frwh". Only root can change anything
114 * if the string is empty or not defined at all.
118 cp = getdef_str ("CHFN_RESTRICT");
121 else if (strcmp (cp, "yes") == 0)
123 else if (strcmp (cp, "no") == 0)
125 if (strchr (cp, field))
131 * new_fields - change the user's GECOS information interactively
133 * prompt the user for each of the four fields and fill in the fields from
134 * the user's response, or leave alone if nothing was entered.
136 static void new_fields (void)
138 printf (_("Enter the new value, or press ENTER for the default\n"));
140 if (may_change_field ('f'))
141 change_field (fullnm, sizeof fullnm, _("Full Name"));
143 printf (_("\tFull Name: %s\n"), fullnm);
145 if (may_change_field ('r'))
146 change_field (roomno, sizeof roomno, _("Room Number"));
148 printf (_("\tRoom Number: %s\n"), roomno);
150 if (may_change_field ('w'))
151 change_field (workph, sizeof workph, _("Work Phone"));
153 printf (_("\tWork Phone: %s\n"), workph);
155 if (may_change_field ('h'))
156 change_field (homeph, sizeof homeph, _("Home Phone"));
158 printf (_("\tHome Phone: %s\n"), homeph);
161 change_field (slop, sizeof slop, _("Other"));
165 * copy_field - get the next field from the gecos field
167 * copy_field copies the next field from the gecos field, returning a
168 * pointer to the field which follows, or NULL if there are no more fields.
170 * in - the current GECOS field
171 * out - where to copy the field to
172 * extra - fields with '=' get copied here
174 static char *copy_field (char *in, char *out, char *extra)
179 if ((cp = strchr (in, ',')))
182 if (!strchr (in, '='))
200 * chfn - change a user's password file information
202 * This command controls the GECOS field information in the password
205 * The valid options are
209 * -w work phone number
210 * -h home phone number
211 * -o other information (*)
213 * (*) requires root permission to execute.
215 int main (int argc, char **argv)
217 char *cp; /* temporary character pointer */
218 const struct passwd *pw; /* password file entry */
219 struct passwd pwent; /* modified password file entry */
220 char old_gecos[BUFSIZ]; /* buffer for old GECOS fields */
221 char new_gecos[BUFSIZ]; /* buffer for new GECOS fields */
222 int flag; /* flag currently being processed */
223 int fflg = 0; /* -f - set full name */
224 int rflg = 0; /* -r - set room number */
225 int wflg = 0; /* -w - set work phone number */
226 int hflg = 0; /* -h - set home phone number */
227 int oflg = 0; /* -o - set other information */
231 pam_handle_t *pamh = NULL;
232 struct passwd *pampw;
237 setlocale (LC_ALL, "");
238 bindtextdomain (PACKAGE, LOCALEDIR);
239 textdomain (PACKAGE);
242 * This command behaves different for root and non-root
245 amroot = (getuid () == 0);
248 * Get the program name. The program name is used as a
249 * prefix to most error messages.
251 Prog = Basename (argv[0]);
256 * The remaining arguments will be processed one by one and executed
257 * by this command. The name is the last argument if it does not
258 * begin with a "-", otherwise the name is determined from the
259 * environment and must agree with the real UID. Also, the UID will
260 * be checked for any commands which are restricted to root only.
262 while ((flag = getopt (argc, argv, "f:r:w:h:o:")) != EOF) {
265 if (!may_change_field ('f')) {
267 _("%s: Permission denied.\n"), Prog);
271 STRFCPY (fullnm, optarg);
274 if (!may_change_field ('h')) {
276 _("%s: Permission denied.\n"), Prog);
280 STRFCPY (homeph, optarg);
283 if (!may_change_field ('r')) {
285 _("%s: Permission denied.\n"), Prog);
289 STRFCPY (roomno, optarg);
294 _("%s: Permission denied.\n"), Prog);
298 STRFCPY (slop, optarg);
301 if (!may_change_field ('w')) {
303 _("%s: Permission denied.\n"), Prog);
307 STRFCPY (workph, optarg);
315 * Get the name of the user to check. It is either the command line
316 * name, or the name getlogin() returns.
320 pw = getpwnam (user);
322 fprintf (stderr, _("%s: unknown user %s\n"), Prog,
327 pw = get_my_pwent ();
331 ("%s: Cannot determine your user name.\n"),
335 user = xstrdup (pw->pw_name);
340 * Now we make sure this is a LOCAL password entry for this user ...
347 _("%s: cannot change user '%s' on NIS client.\n"),
350 if (!yp_get_default_domain (&nis_domain) &&
351 !yp_master (nis_domain, "passwd.byname", &nis_master)) {
354 ("%s: '%s' is the NIS master for this client.\n"),
362 * Non-privileged users are only allowed to change the gecos field
363 * if the UID of the user matches the current real UID.
365 if (!amroot && pw->pw_uid != getuid ()) {
366 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
372 * If the UID of the user does not match the current real UID,
373 * check if the change is allowed by SELinux policy.
375 if ((pw->pw_uid != getuid ())
376 && (is_selinux_enabled () > 0)
377 && (selinux_check_passwd_access (PASSWD__CHFN) != 0)) {
378 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
386 * Non-privileged users are optionally authenticated (must enter the
387 * password of the user whose information is being changed) before
388 * any changes can be made. Idea from util-linux chfn/chsh.
391 if (!amroot && getdef_bool ("CHFN_AUTH"))
392 passwd_check (pw->pw_name, pw->pw_passwd, "chfn");
395 retval = PAM_SUCCESS;
397 pampw = getpwuid (getuid ());
399 retval = PAM_USER_UNKNOWN;
402 if (retval == PAM_SUCCESS) {
403 retval = pam_start ("chfn", pampw->pw_name, &conv, &pamh);
406 if (retval == PAM_SUCCESS) {
407 retval = pam_authenticate (pamh, 0);
408 if (retval != PAM_SUCCESS) {
409 pam_end (pamh, retval);
413 if (retval == PAM_SUCCESS) {
414 retval = pam_acct_mgmt (pamh, 0);
415 if (retval != PAM_SUCCESS) {
416 pam_end (pamh, retval);
420 if (retval != PAM_SUCCESS) {
421 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
427 * Now get the full name. It is the first comma separated field in
430 STRFCPY (old_gecos, pw->pw_gecos);
431 cp = copy_field (old_gecos, fflg ? (char *) 0 : fullnm, slop);
434 * Now get the room number. It is the next comma separated field,
435 * if there is indeed one.
438 cp = copy_field (cp, rflg ? (char *) 0 : roomno, slop);
441 * Now get the work phone number. It is the third field.
444 cp = copy_field (cp, wflg ? (char *) 0 : workph, slop);
447 * Now get the home phone number. It is the fourth field.
450 cp = copy_field (cp, hflg ? (char *) 0 : homeph, slop);
453 * Anything left over is "slop".
463 * If none of the fields were changed from the command line, let the
464 * user interactively change them.
466 if (!fflg && !rflg && !wflg && !hflg && !oflg) {
467 printf (_("Changing the user information for %s\n"), user);
472 * Check all of the fields for valid information
474 if (valid_field (fullnm, ":,=")) {
475 fprintf (stderr, _("%s: invalid name: '%s'\n"), Prog, fullnm);
479 if (valid_field (roomno, ":,=")) {
480 fprintf (stderr, _("%s: invalid room number: '%s'\n"),
485 if (valid_field (workph, ":,=")) {
486 fprintf (stderr, _("%s: invalid work phone: '%s'\n"),
491 if (valid_field (homeph, ":,=")) {
492 fprintf (stderr, _("%s: invalid home phone: '%s'\n"),
497 if (valid_field (slop, ":")) {
499 _("%s: '%s' contains illegal characters\n"),
506 * Build the new GECOS field by plastering all the pieces together,
507 * if they will fit ...
509 if (strlen (fullnm) + strlen (roomno) + strlen (workph) +
510 strlen (homeph) + strlen (slop) > (unsigned int) 80) {
511 fprintf (stderr, _("%s: fields too long\n"), Prog);
515 snprintf (new_gecos, sizeof new_gecos, "%s,%s,%s,%s%s%s",
516 fullnm, roomno, workph, homeph, slop[0] ? "," : "", slop);
519 * Before going any further, raise the ulimit to prevent colliding
520 * into a lowered ulimit, and set the real UID to root to protect
521 * against unexpected signals. Any keyboard signals are set to be
525 fprintf (stderr, _("Cannot change ID to root.\n"));
526 SYSLOG ((LOG_ERR, "can't setuid(0)"));
533 * The passwd entry is now ready to be committed back to the
534 * password file. Get a lock on the file and open it.
539 ("Cannot lock the password file; try again later.\n"));
540 SYSLOG ((LOG_WARN, "can't lock /etc/passwd"));
544 if (!pw_open (O_RDWR)) {
545 fprintf (stderr, _("Cannot open the password file.\n"));
547 SYSLOG ((LOG_ERR, "can't open /etc/passwd"));
553 * Get the entry to update using pw_locate() - we want the real one
554 * from /etc/passwd, not the one from getpwnam() which could contain
555 * the shadow password if (despite the warnings) someone enables
556 * AUTOSHADOW (or SHADOW_COMPAT in libc). --marekm
558 pw = pw_locate (user);
562 _("%s: %s not found in /etc/passwd\n"), Prog, user);
567 * Make a copy of the entry, then change the gecos field. The other
568 * fields remain unchanged.
571 pwent.pw_gecos = new_gecos;
574 * Update the passwd file entry. If there is a DBM file, update that
577 if (!pw_update (&pwent)) {
578 fprintf (stderr, _("Error updating the password entry.\n"));
580 SYSLOG ((LOG_ERR, "error updating passwd entry"));
586 * Changes have all been made, so commit them and unlock the file.
589 fprintf (stderr, _("Cannot commit password file changes.\n"));
591 SYSLOG ((LOG_ERR, "can't rewrite /etc/passwd"));
596 fprintf (stderr, _("Cannot unlock the password file.\n"));
597 SYSLOG ((LOG_ERR, "can't unlock /etc/passwd"));
601 SYSLOG ((LOG_INFO, "changed user `%s' information", user));
603 nscd_flush_cache ("passwd");
606 if (retval == PAM_SUCCESS)
607 pam_end (pamh, PAM_SUCCESS);