2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
vipw, vigr edit the password or group file
|
|
|
|
with -s will edit shadow or gshadow file
|
|
|
|
|
|
|
|
Copyright (C) 1997 Guy Maor <maor@ece.utexas.edu>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-10-07 17:14:59 +05:30
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2007-10-07 17:14:02 +05:30
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-10-07 17:17:11 +05:30
|
|
|
#ident "$Id: vipw.c,v 1.20 2005/12/13 14:01:08 kloczek Exp $"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <errno.h>
|
2007-10-07 17:17:11 +05:30
|
|
|
#include <getopt.h>
|
2007-10-07 17:17:01 +05:30
|
|
|
#include <signal.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-10-07 17:17:01 +05:30
|
|
|
#include <sys/stat.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <sys/types.h>
|
2007-10-07 17:17:01 +05:30
|
|
|
#include <unistd.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <utime.h>
|
2007-10-07 17:17:01 +05:30
|
|
|
#include "defines.h"
|
2007-10-07 17:17:11 +05:30
|
|
|
#include "exitcodes.h"
|
2007-10-07 17:17:01 +05:30
|
|
|
#include "groupio.h"
|
|
|
|
#include "nscd.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
#include "pwio.h"
|
|
|
|
#include "sgroupio.h"
|
2007-10-07 17:17:01 +05:30
|
|
|
#include "shadowio.h"
|
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
static const char *progname, *filename, *fileeditname;
|
|
|
|
static int filelocked = 0, createedit = 0;
|
2007-10-07 17:14:59 +05:30
|
|
|
static int (*unlock) (void);
|
2007-10-07 17:17:11 +05:30
|
|
|
static int quiet = 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/* local function prototypes */
|
2007-10-07 17:17:11 +05:30
|
|
|
static void usage (void);
|
2007-10-07 17:14:59 +05:30
|
|
|
static int create_backup_file (FILE *, const char *, struct stat *);
|
|
|
|
static void vipwexit (const char *, int, int);
|
|
|
|
static void vipwedit (const char *, int (*)(void), int (*)(void));
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:17:11 +05:30
|
|
|
/*
|
|
|
|
* usage - display usage message and exit
|
|
|
|
*/
|
|
|
|
static void usage (void)
|
|
|
|
{
|
|
|
|
fprintf (stderr, _("Usage: vipw [options]\n"
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -g, --group edit group database\n"
|
|
|
|
" -h, --help display this help message and exit\n"
|
|
|
|
" -p, --passwd edit passwd database\n"
|
|
|
|
" -q, --quiet quiet mode\n"
|
|
|
|
" -s, --shadow edit shadow or gshadow database\n"));
|
|
|
|
exit (E_USAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
*/
|
2007-10-07 17:16:07 +05:30
|
|
|
static int create_backup_file (FILE * fp, const char *backup, struct stat *sb)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
struct utimbuf ub;
|
|
|
|
FILE *bkfp;
|
|
|
|
int c;
|
|
|
|
mode_t mask;
|
|
|
|
|
|
|
|
mask = umask (077);
|
|
|
|
bkfp = fopen (backup, "w");
|
|
|
|
umask (mask);
|
|
|
|
if (!bkfp)
|
|
|
|
return -1;
|
|
|
|
|
2007-10-07 17:15:40 +05:30
|
|
|
c = 0;
|
2007-10-07 17:16:07 +05:30
|
|
|
if (fseeko (fp, 0, SEEK_SET) == 0)
|
|
|
|
while ((c = getc (fp)) != EOF) {
|
|
|
|
if (putc (c, bkfp) == EOF)
|
2007-10-07 17:15:40 +05:30
|
|
|
break;
|
|
|
|
}
|
2007-10-07 17:16:07 +05:30
|
|
|
if (c != EOF || ferror (fp) || fflush (bkfp)) {
|
2007-10-07 17:14:59 +05:30
|
|
|
fclose (bkfp);
|
|
|
|
unlink (backup);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (fclose (bkfp)) {
|
|
|
|
unlink (backup);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ub.actime = sb->st_atime;
|
|
|
|
ub.modtime = sb->st_mtime;
|
|
|
|
if (utime (backup, &ub) ||
|
|
|
|
chmod (backup, sb->st_mode) ||
|
|
|
|
chown (backup, sb->st_uid, sb->st_gid)) {
|
|
|
|
unlink (backup);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:17:11 +05:30
|
|
|
/*
|
|
|
|
*
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
static void vipwexit (const char *msg, int syserr, int ret)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
int err = errno;
|
|
|
|
|
|
|
|
if (createedit)
|
|
|
|
unlink (fileeditname);
|
2007-10-07 17:16:07 +05:30
|
|
|
if (filelocked)
|
|
|
|
(*unlock) ();
|
2007-10-07 17:14:59 +05:30
|
|
|
if (msg)
|
|
|
|
fprintf (stderr, "%s: %s", progname, msg);
|
|
|
|
if (syserr)
|
|
|
|
fprintf (stderr, ": %s", strerror (err));
|
2007-10-07 17:17:11 +05:30
|
|
|
if (!quiet)
|
|
|
|
fprintf (stdout, _("\n%s: %s is unchanged\n"), progname,
|
|
|
|
filename);
|
2007-10-07 17:14:59 +05:30
|
|
|
exit (ret);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef DEFAULT_EDITOR
|
|
|
|
#define DEFAULT_EDITOR "vi"
|
|
|
|
#endif
|
|
|
|
|
2007-10-07 17:17:11 +05:30
|
|
|
/*
|
|
|
|
*
|
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
static void
|
2007-10-07 17:16:07 +05:30
|
|
|
vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
const char *editor;
|
|
|
|
pid_t pid;
|
|
|
|
struct stat st1, st2;
|
|
|
|
int status;
|
|
|
|
FILE *f;
|
|
|
|
char filebackup[1024], fileedit[1024];
|
|
|
|
|
|
|
|
snprintf (filebackup, sizeof filebackup, "%s-", file);
|
|
|
|
snprintf (fileedit, sizeof fileedit, "%s.edit", file);
|
|
|
|
unlock = file_unlock;
|
|
|
|
filename = file;
|
|
|
|
fileeditname = fileedit;
|
|
|
|
|
|
|
|
if (access (file, F_OK))
|
|
|
|
vipwexit (file, 1, 1);
|
|
|
|
if (!file_lock ())
|
|
|
|
vipwexit (_("Couldn't lock file"), errno, 5);
|
|
|
|
filelocked = 1;
|
|
|
|
|
|
|
|
/* edited copy has same owners, perm */
|
|
|
|
if (stat (file, &st1))
|
|
|
|
vipwexit (file, 1, 1);
|
|
|
|
if (!(f = fopen (file, "r")))
|
|
|
|
vipwexit (file, 1, 1);
|
|
|
|
if (create_backup_file (f, fileedit, &st1))
|
|
|
|
vipwexit (_("Couldn't make backup"), errno, 1);
|
|
|
|
createedit = 1;
|
|
|
|
|
|
|
|
editor = getenv ("VISUAL");
|
|
|
|
if (!editor)
|
|
|
|
editor = getenv ("EDITOR");
|
|
|
|
if (!editor)
|
|
|
|
editor = DEFAULT_EDITOR;
|
|
|
|
|
|
|
|
if ((pid = fork ()) == -1)
|
|
|
|
vipwexit ("fork", 1, 1);
|
|
|
|
else if (!pid) {
|
|
|
|
/* use the system() call to invoke the editor so that it accepts
|
|
|
|
command line args in the EDITOR and VISUAL environment vars */
|
|
|
|
char *buf;
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
buf = (char *) malloc (strlen (editor) + strlen (fileedit) + 2);
|
2007-10-07 17:14:59 +05:30
|
|
|
snprintf (buf, strlen (editor) + strlen (fileedit) + 2,
|
|
|
|
"%s %s", editor, fileedit);
|
|
|
|
if (system (buf) != 0) {
|
|
|
|
fprintf (stderr, "%s: %s: %s\n", progname, editor,
|
|
|
|
strerror (errno));
|
|
|
|
exit (1);
|
|
|
|
} else
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pid = waitpid (pid, &status, WUNTRACED);
|
|
|
|
if (WIFSTOPPED (status)) {
|
|
|
|
kill (getpid (), SIGSTOP);
|
|
|
|
kill (getpid (), SIGCONT);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == -1 || !WIFEXITED (status) || WEXITSTATUS (status))
|
|
|
|
vipwexit (editor, 1, 1);
|
|
|
|
|
|
|
|
if (stat (fileedit, &st2))
|
|
|
|
vipwexit (fileedit, 1, 1);
|
|
|
|
if (st1.st_mtime == st2.st_mtime)
|
|
|
|
vipwexit (0, 0, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - here we should check fileedit for errors; if there are any,
|
|
|
|
* ask the user what to do (edit again, save changes anyway, or quit
|
|
|
|
* without saving). Use pwck or grpck to do the check. --marekm
|
|
|
|
*/
|
|
|
|
createedit = 0;
|
|
|
|
unlink (filebackup);
|
|
|
|
link (file, filebackup);
|
|
|
|
if (rename (fileedit, file) == -1) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_
|
|
|
|
("%s: can't restore %s: %s (your changes are in %s)\n"),
|
|
|
|
progname, file, strerror (errno), fileedit);
|
|
|
|
vipwexit (0, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*file_unlock) ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
int flag;
|
|
|
|
int editshadow = 0;
|
2007-10-07 17:17:11 +05:30
|
|
|
char *a;
|
2007-10-07 17:14:59 +05:30
|
|
|
int do_vipw;
|
|
|
|
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
|
2007-10-07 17:17:11 +05:30
|
|
|
progname = ((a = strrchr (*argv, '/')) ? a + 1 : *argv);
|
2007-10-07 17:14:59 +05:30
|
|
|
do_vipw = (strcmp (progname, "vigr") != 0);
|
|
|
|
|
2007-10-07 17:17:11 +05:30
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Parse the command line options.
|
|
|
|
*/
|
|
|
|
int c;
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"group", no_argument, NULL, 'g'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"passwd", no_argument, NULL, 'p'},
|
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
|
|
|
{"shadow", no_argument, NULL, 's'},
|
|
|
|
};
|
|
|
|
while ((c =
|
|
|
|
getopt_long (argc, argv, "ghpqs",
|
|
|
|
long_options, NULL)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'g':
|
|
|
|
do_vipw = 0;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage ();
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
do_vipw = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
editshadow = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
if (do_vipw) {
|
|
|
|
if (editshadow)
|
|
|
|
vipwedit (SHADOW_FILE, spw_lock, spw_unlock);
|
|
|
|
else
|
|
|
|
vipwedit (PASSWD_FILE, pw_lock, pw_unlock);
|
|
|
|
} else {
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef SHADOWGRP
|
2007-10-07 17:14:59 +05:30
|
|
|
if (editshadow)
|
|
|
|
vipwedit (SGROUP_FILE, sgr_lock, sgr_unlock);
|
|
|
|
else
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:14:59 +05:30
|
|
|
vipwedit (GROUP_FILE, gr_lock, gr_unlock);
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
nscd_flush_cache ("passwd");
|
|
|
|
nscd_flush_cache ("group");
|
2007-10-07 17:16:52 +05:30
|
|
|
|
2007-10-07 17:17:11 +05:30
|
|
|
exit (E_SUCCESS);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|