2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Copyright 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of Julianne F. Haugh nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2007-10-07 17:14:59 +05:30
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
|
2007-10-07 17:14:02 +05:30
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "rcsid.h"
|
2007-10-07 17:16:34 +05:30
|
|
|
RCSID (PKG_VER "$Id: chfn.c,v 1.29 2005/07/07 15:32:50 kloczek Exp $")
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include <pwd.h>
|
|
|
|
#include "pwio.h"
|
|
|
|
#include "getdef.h"
|
|
|
|
#include "pwauth.h"
|
2007-10-07 17:15:23 +05:30
|
|
|
#include "nscd.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef HAVE_SHADOW_H
|
|
|
|
#include <shadow.h>
|
|
|
|
#endif
|
|
|
|
#ifdef USE_PAM
|
|
|
|
#include "pam_defs.h"
|
|
|
|
#endif
|
2007-10-07 17:16:07 +05:30
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/av_permissions.h>
|
|
|
|
#endif
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Global variables.
|
|
|
|
*/
|
|
|
|
static char *Prog;
|
|
|
|
static char fullnm[BUFSIZ];
|
|
|
|
static char roomno[BUFSIZ];
|
|
|
|
static char workph[BUFSIZ];
|
|
|
|
static char homeph[BUFSIZ];
|
|
|
|
static char slop[BUFSIZ];
|
|
|
|
static int amroot;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* External identifiers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* local function prototypes */
|
2007-10-07 17:14:59 +05:30
|
|
|
static void usage (void);
|
|
|
|
static int may_change_field (int);
|
|
|
|
static void new_fields (void);
|
|
|
|
static char *copy_field (char *, char *, char *);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* usage - print command line syntax and exit
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void usage (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
if (amroot)
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
|
|
|
_("Usage: %s [-f full_name] [-r room_no] "
|
|
|
|
"[-w work_ph]\n"
|
|
|
|
"\t[-h home_ph] [-o other] [user]\n"), Prog);
|
2007-10-07 17:14:02 +05:30
|
|
|
else
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
|
|
|
_("Usage: %s [-f full_name] [-r room_no] "
|
|
|
|
"[-w work_ph] [-h home_ph]\n"), Prog);
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static int may_change_field (int field)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* CHFN_RESTRICT can now specify exactly which fields may be changed
|
|
|
|
* by regular users, by using any combination of the following
|
|
|
|
* letters:
|
2007-10-07 17:14:02 +05:30
|
|
|
* f - full name
|
|
|
|
* r - room number
|
|
|
|
* w - work phone
|
|
|
|
* h - home phone
|
|
|
|
*
|
|
|
|
* This makes it possible to disallow changing the room number
|
2007-10-07 17:14:59 +05:30
|
|
|
* information, for example - this feature was suggested by Maciej
|
|
|
|
* 'Tycoon' Majchrowski.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
|
|
|
* For backward compatibility, "yes" is equivalent to "rwh",
|
2007-10-07 17:14:59 +05:30
|
|
|
* "no" is equivalent to "frwh". Only root can change anything
|
2007-10-07 17:14:02 +05:30
|
|
|
* if the string is empty or not defined at all.
|
|
|
|
*/
|
|
|
|
if (amroot)
|
|
|
|
return 1;
|
2007-10-07 17:14:59 +05:30
|
|
|
cp = getdef_str ("CHFN_RESTRICT");
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!cp)
|
|
|
|
cp = "";
|
2007-10-07 17:14:59 +05:30
|
|
|
else if (strcmp (cp, "yes") == 0)
|
2007-10-07 17:14:02 +05:30
|
|
|
cp = "rwh";
|
2007-10-07 17:14:59 +05:30
|
|
|
else if (strcmp (cp, "no") == 0)
|
2007-10-07 17:14:02 +05:30
|
|
|
cp = "frwh";
|
2007-10-07 17:14:59 +05:30
|
|
|
if (strchr (cp, field))
|
2007-10-07 17:14:02 +05:30
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* new_fields - change the user's GECOS information interactively
|
|
|
|
*
|
2007-10-07 17:14:59 +05:30
|
|
|
* prompt the user for each of the four fields and fill in the fields from
|
|
|
|
* the user's response, or leave alone if nothing was entered.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void new_fields (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
printf (_("Enter the new value, or press ENTER for the default\n"));
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (may_change_field ('f'))
|
|
|
|
change_field (fullnm, sizeof fullnm, _("Full Name"));
|
2007-10-07 17:14:02 +05:30
|
|
|
else
|
2007-10-07 17:14:59 +05:30
|
|
|
printf (_("\tFull Name: %s\n"), fullnm);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (may_change_field ('r'))
|
|
|
|
change_field (roomno, sizeof roomno, _("Room Number"));
|
2007-10-07 17:14:02 +05:30
|
|
|
else
|
2007-10-07 17:15:58 +05:30
|
|
|
printf ("\t%s: %s\n", _("Room Number"), roomno);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (may_change_field ('w'))
|
|
|
|
change_field (workph, sizeof workph, _("Work Phone"));
|
2007-10-07 17:14:02 +05:30
|
|
|
else
|
2007-10-07 17:15:58 +05:30
|
|
|
printf ("\t%s: %s\n", _("Work Phone"), workph);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (may_change_field ('h'))
|
|
|
|
change_field (homeph, sizeof homeph, _("Home Phone"));
|
2007-10-07 17:14:02 +05:30
|
|
|
else
|
2007-10-07 17:15:58 +05:30
|
|
|
printf ("\t%s: %s\n", _("Home Phone"), homeph);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (amroot)
|
2007-10-07 17:14:59 +05:30
|
|
|
change_field (slop, sizeof slop, _("Other"));
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy_field - get the next field from the gecos field
|
|
|
|
*
|
|
|
|
* copy_field copies the next field from the gecos field, returning a
|
2007-10-07 17:14:59 +05:30
|
|
|
* pointer to the field which follows, or NULL if there are no more fields.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
|
|
|
* in - the current GECOS field
|
|
|
|
* out - where to copy the field to
|
|
|
|
* extra - fields with '=' get copied here
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static char *copy_field (char *in, char *out, char *extra)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char *cp = NULL;
|
|
|
|
|
|
|
|
while (in) {
|
|
|
|
if ((cp = strchr (in, ',')))
|
|
|
|
*cp++ = '\0';
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!strchr (in, '='))
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
if (extra) {
|
|
|
|
if (extra[0])
|
|
|
|
strcat (extra, ",");
|
|
|
|
|
|
|
|
strcat (extra, in);
|
|
|
|
}
|
|
|
|
in = cp;
|
|
|
|
}
|
|
|
|
if (in && out)
|
|
|
|
strcpy (out, in);
|
|
|
|
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* chfn - change a user's password file information
|
|
|
|
*
|
2007-10-07 17:14:59 +05:30
|
|
|
* This command controls the GECOS field information in the password
|
|
|
|
* file entry.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
|
|
|
* The valid options are
|
|
|
|
*
|
|
|
|
* -f full name
|
|
|
|
* -r room number
|
|
|
|
* -w work phone number
|
|
|
|
* -h home phone number
|
|
|
|
* -o other information (*)
|
|
|
|
*
|
|
|
|
* (*) requires root permission to execute.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
int main (int argc, char **argv)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
char *cp; /* temporary character pointer */
|
2007-10-07 17:14:02 +05:30
|
|
|
const struct passwd *pw; /* password file entry */
|
2007-10-07 17:14:59 +05:30
|
|
|
struct passwd pwent; /* modified password file entry */
|
|
|
|
char old_gecos[BUFSIZ]; /* buffer for old GECOS fields */
|
|
|
|
char new_gecos[BUFSIZ]; /* buffer for new GECOS fields */
|
|
|
|
int flag; /* flag currently being processed */
|
|
|
|
int fflg = 0; /* -f - set full name */
|
|
|
|
int rflg = 0; /* -r - set room number */
|
|
|
|
int wflg = 0; /* -w - set work phone number */
|
|
|
|
int hflg = 0; /* -h - set home phone number */
|
|
|
|
int oflg = 0; /* -o - set other information */
|
2007-10-07 17:14:02 +05:30
|
|
|
char *user;
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
sanitize_env ();
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* This command behaves different for root and non-root
|
|
|
|
* users.
|
|
|
|
*/
|
|
|
|
|
|
|
|
amroot = (getuid () == 0);
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Get the program name. The program name is used as a
|
2007-10-07 17:14:32 +05:30
|
|
|
* prefix to most error messages.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
Prog = Basename (argv[0]);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
OPENLOG ("chfn");
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* The remaining arguments will be processed one by one and executed
|
|
|
|
* by this command. The name is the last argument if it does not
|
|
|
|
* begin with a "-", otherwise the name is determined from the
|
|
|
|
* environment and must agree with the real UID. Also, the UID will
|
|
|
|
* be checked for any commands which are restricted to root only.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
while ((flag = getopt (argc, argv, "f:r:w:h:o:")) != EOF) {
|
|
|
|
switch (flag) {
|
2007-10-07 17:14:59 +05:30
|
|
|
case 'f':
|
|
|
|
if (!may_change_field ('f')) {
|
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:25 +05:30
|
|
|
_("%s: Permission denied.\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
fflg++;
|
|
|
|
STRFCPY (fullnm, optarg);
|
|
|
|
break;
|
2007-10-07 17:16:34 +05:30
|
|
|
case 'h':
|
|
|
|
if (!may_change_field ('h')) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:25 +05:30
|
|
|
_("%s: Permission denied.\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
2007-10-07 17:16:34 +05:30
|
|
|
hflg++;
|
|
|
|
STRFCPY (homeph, optarg);
|
2007-10-07 17:14:59 +05:30
|
|
|
break;
|
2007-10-07 17:16:34 +05:30
|
|
|
case 'r':
|
|
|
|
if (!may_change_field ('r')) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:25 +05:30
|
|
|
_("%s: Permission denied.\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
2007-10-07 17:16:34 +05:30
|
|
|
rflg++;
|
|
|
|
STRFCPY (roomno, optarg);
|
2007-10-07 17:14:59 +05:30
|
|
|
break;
|
2007-10-07 17:16:34 +05:30
|
|
|
case 'o':
|
|
|
|
if (!amroot) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:25 +05:30
|
|
|
_("%s: Permission denied.\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
2007-10-07 17:16:34 +05:30
|
|
|
oflg++;
|
|
|
|
STRFCPY (slop, optarg);
|
2007-10-07 17:14:59 +05:30
|
|
|
break;
|
2007-10-07 17:16:34 +05:30
|
|
|
case 'w':
|
|
|
|
if (!may_change_field ('w')) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:25 +05:30
|
|
|
_("%s: Permission denied.\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
2007-10-07 17:16:34 +05:30
|
|
|
wflg++;
|
|
|
|
STRFCPY (workph, optarg);
|
2007-10-07 17:14:59 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Get the name of the user to check. It is either the command line
|
|
|
|
* name, or the name getlogin() returns.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
user = argv[optind];
|
2007-10-07 17:14:59 +05:30
|
|
|
pw = getpwnam (user);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!pw) {
|
2007-10-07 17:15:58 +05:30
|
|
|
fprintf (stderr, _("%s: unknown user %s\n"), Prog,
|
2007-10-07 17:14:59 +05:30
|
|
|
user);
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
} else {
|
2007-10-07 17:14:59 +05:30
|
|
|
pw = get_my_pwent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!pw) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
|
|
|
_
|
|
|
|
("%s: Cannot determine your user name.\n"),
|
|
|
|
Prog);
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
user = xstrdup (pw->pw_name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_NIS
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Now we make sure this is a LOCAL password entry for this user ...
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
if (__ispwNIS ()) {
|
2007-10-07 17:14:59 +05:30
|
|
|
char *nis_domain;
|
|
|
|
char *nis_master;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
|
|
|
_("%s: cannot change user `%s' on NIS client.\n"),
|
|
|
|
Prog, user);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!yp_get_default_domain (&nis_domain) &&
|
2007-10-07 17:16:07 +05:30
|
|
|
!yp_master (nis_domain, "passwd.byname", &nis_master)) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr,
|
|
|
|
_
|
|
|
|
("%s: `%s' is the NIS master for this client.\n"),
|
|
|
|
Prog, nis_master);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Non-privileged users are only allowed to change the gecos field
|
|
|
|
* if the UID of the user matches the current real UID.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!amroot && pw->pw_uid != getuid ()) {
|
2007-10-07 17:16:25 +05:30
|
|
|
fprintf (stderr, _("%s: Permission denied.\n"), Prog);
|
2007-10-07 17:14:59 +05:30
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:16:07 +05:30
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
/*
|
|
|
|
* If the UID of the user does not match the current real UID,
|
|
|
|
* check if the change is allowed by SELinux policy.
|
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
if ((pw->pw_uid != getuid ())
|
|
|
|
&& (checkPasswdAccess (PASSWD__CHFN) != 0)) {
|
|
|
|
fprintf (stderr, _("%s: Permission denied.\n"), Prog);
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef USE_PAM
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Non-privileged users are optionally authenticated (must enter the
|
|
|
|
* password of the user whose information is being changed) before
|
|
|
|
* any changes can be made. Idea from util-linux chfn/chsh.
|
|
|
|
* --marekm
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!amroot && getdef_bool ("CHFN_AUTH"))
|
|
|
|
passwd_check (pw->pw_name, pw->pw_passwd, "chfn");
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
#endif /* !USE_PAM */
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Now get the full name. It is the first comma separated field in
|
|
|
|
* the GECOS field.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
STRFCPY (old_gecos, pw->pw_gecos);
|
|
|
|
cp = copy_field (old_gecos, fflg ? (char *) 0 : fullnm, slop);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Now get the room number. It is the next comma separated field,
|
2007-10-07 17:14:02 +05:30
|
|
|
* if there is indeed one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (cp)
|
2007-10-07 17:14:59 +05:30
|
|
|
cp = copy_field (cp, rflg ? (char *) 0 : roomno, slop);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Now get the work phone number. It is the third field.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
if (cp)
|
2007-10-07 17:14:59 +05:30
|
|
|
cp = copy_field (cp, wflg ? (char *) 0 : workph, slop);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Now get the home phone number. It is the fourth field.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
if (cp)
|
2007-10-07 17:14:59 +05:30
|
|
|
cp = copy_field (cp, hflg ? (char *) 0 : homeph, slop);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Anything left over is "slop".
|
|
|
|
*/
|
|
|
|
if (cp && !oflg) {
|
|
|
|
if (slop[0])
|
|
|
|
strcat (slop, ",");
|
|
|
|
|
|
|
|
strcat (slop, cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* If none of the fields were changed from the command line, let the
|
|
|
|
* user interactively change them.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
if (!fflg && !rflg && !wflg && !hflg && !oflg) {
|
2007-10-07 17:14:59 +05:30
|
|
|
printf (_("Changing the user information for %s\n"), user);
|
|
|
|
new_fields ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check all of the fields for valid information
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (valid_field (fullnm, ":,=")) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("%s: invalid name: \"%s\"\n"), Prog, fullnm);
|
2007-10-07 17:14:59 +05:30
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (valid_field (roomno, ":,=")) {
|
|
|
|
fprintf (stderr, _("%s: invalid room number: \"%s\"\n"),
|
|
|
|
Prog, roomno);
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (valid_field (workph, ":,=")) {
|
|
|
|
fprintf (stderr, _("%s: invalid work phone: \"%s\"\n"),
|
|
|
|
Prog, workph);
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
if (valid_field (homeph, ":,=")) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fprintf (stderr, _("%s: invalid home phone: \"%s\"\n"),
|
|
|
|
Prog, homeph);
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (valid_field (slop, ":")) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_("%s: \"%s\" contains illegal characters\n"),
|
|
|
|
Prog, slop);
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build the new GECOS field by plastering all the pieces together,
|
|
|
|
* if they will fit ...
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (strlen (fullnm) + strlen (roomno) + strlen (workph) +
|
|
|
|
strlen (homeph) + strlen (slop) > (unsigned int) 80) {
|
|
|
|
fprintf (stderr, _("%s: fields too long\n"), Prog);
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
snprintf (new_gecos, sizeof new_gecos, "%s,%s,%s,%s%s%s",
|
2007-10-07 17:16:07 +05:30
|
|
|
fullnm, roomno, workph, homeph, slop[0] ? "," : "", slop);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Before going any further, raise the ulimit to prevent colliding
|
|
|
|
* into a lowered ulimit, and set the real UID to root to protect
|
|
|
|
* against unexpected signals. Any keyboard signals are set to be
|
|
|
|
* ignored.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (setuid (0)) {
|
|
|
|
fprintf (stderr, _("Cannot change ID to root.\n"));
|
|
|
|
SYSLOG ((LOG_ERR, "can't setuid(0)"));
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
pwd_init ();
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* The passwd entry is now ready to be committed back to the
|
|
|
|
* password file. Get a lock on the file and open it.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!pw_lock ()) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_
|
|
|
|
("Cannot lock the password file; try again later.\n"));
|
|
|
|
SYSLOG ((LOG_WARN, "can't lock /etc/passwd"));
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!pw_open (O_RDWR)) {
|
|
|
|
fprintf (stderr, _("Cannot open the password file.\n"));
|
|
|
|
pw_unlock ();
|
|
|
|
SYSLOG ((LOG_ERR, "can't open /etc/passwd"));
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Get the entry to update using pw_locate() - we want the real one
|
|
|
|
* from /etc/passwd, not the one from getpwnam() which could contain
|
|
|
|
* the shadow password if (despite the warnings) someone enables
|
|
|
|
* AUTOSHADOW (or SHADOW_COMPAT in libc). --marekm
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
pw = pw_locate (user);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!pw) {
|
2007-10-07 17:14:59 +05:30
|
|
|
pw_unlock ();
|
|
|
|
fprintf (stderr,
|
2007-10-07 17:16:07 +05:30
|
|
|
_("%s: %s not found in /etc/passwd\n"), Prog, user);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Make a copy of the entry, then change the gecos field. The other
|
2007-10-07 17:14:02 +05:30
|
|
|
* fields remain unchanged.
|
|
|
|
*/
|
|
|
|
pwent = *pw;
|
|
|
|
pwent.pw_gecos = new_gecos;
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Update the passwd file entry. If there is a DBM file, update that
|
|
|
|
* entry as well.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!pw_update (&pwent)) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("Error updating the password entry.\n"));
|
2007-10-07 17:14:59 +05:30
|
|
|
pw_unlock ();
|
|
|
|
SYSLOG ((LOG_ERR, "error updating passwd entry"));
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:14:59 +05:30
|
|
|
* Changes have all been made, so commit them and unlock the file.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!pw_close ()) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr, _("Cannot commit password file changes.\n"));
|
2007-10-07 17:14:59 +05:30
|
|
|
pw_unlock ();
|
|
|
|
SYSLOG ((LOG_ERR, "can't rewrite /etc/passwd"));
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (!pw_unlock ()) {
|
|
|
|
fprintf (stderr, _("Cannot unlock the password file.\n"));
|
|
|
|
SYSLOG ((LOG_ERR, "can't unlock /etc/passwd"));
|
|
|
|
closelog ();
|
|
|
|
exit (1);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
SYSLOG ((LOG_INFO, "changed user `%s' information", user));
|
2007-10-07 17:15:23 +05:30
|
|
|
|
|
|
|
nscd_flush_cache ("passwd");
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
closelog ();
|
2007-10-07 17:14:02 +05:30
|
|
|
exit (0);
|
|
|
|
}
|