2 vipw, vigr edit the password or group file
3 with -s will edit shadow or gshadow file
5 Copyright (c) 1997 , Guy Maor <maor@ece.utexas.edu>
6 Copyright (c) 1999 - 2000, Marek Michałkiewicz
7 Copyright (c) 2002 - 2006, Tomasz Kłoczko
8 Copyright (c) 2007 - 2008, Nicolas François
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA. */
33 #include <selinux/selinux.h>
34 #endif /* WITH_SELINUX */
39 #include <sys/types.h>
45 #include "prototypes.h"
50 #include "exitcodes.h"
56 #define MSG_WARN_EDIT_OTHER_FILE _( \
57 "You have modified %s.\n"\
58 "You may need to modify %s for consistency.\n"\
59 "Please use the command '%s' to do so.\n")
64 static const char *progname, *filename, *fileeditname;
65 static bool filelocked = false;
66 static bool createedit = false;
67 static int (*unlock) (void);
68 static bool quiet = false;
70 static const char *user = NULL;
71 static bool tcb_mode = false;
72 #define SHADOWTCB_SCRATCHDIR ":tmp"
75 /* local function prototypes */
76 static void usage (int status);
77 static int create_backup_file (FILE *, const char *, struct stat *);
78 static void vipwexit (const char *msg, int syserr, int ret);
79 static void vipwedit (const char *, int (*)(void), int (*)(void));
82 * usage - display usage message and exit
84 static void usage (int status)
86 FILE *usageout = (E_SUCCESS != status) ? stderr : stdout;
87 (void) fputs (_("Usage: vipw [options]\n"
89 "Options:\n"), usageout);
90 (void) fputs (_(" -g, --group edit group database\n"), usageout);
91 (void) fputs (_(" -h, --help display this help message and exit\n"), usageout);
92 (void) fputs (_(" -p, --passwd edit passwd database\n"), usageout);
93 (void) fputs (_(" -q, --quiet quiet mode\n"), usageout);
94 (void) fputs (_(" -s, --shadow edit shadow or gshadow database\n"), usageout);
96 (void) fputs (_(" -u, --user which user's tcb shadow file to edit\n"), usageout);
98 (void) fputs (_("\n"), usageout);
105 static int create_backup_file (FILE * fp, const char *backup, struct stat *sb)
113 bkfp = fopen (backup, "w");
120 if (fseeko (fp, 0, SEEK_SET) == 0)
121 while ((c = getc (fp)) != EOF) {
122 if (putc (c, bkfp) == EOF) {
126 if ((EOF != c) || (ferror (fp) != 0) || (fflush (bkfp) != 0)) {
131 if (fsync (fileno (bkfp)) != 0) {
132 (void) fclose (bkfp);
136 if (fclose (bkfp) != 0) {
141 ub.actime = sb->st_atime;
142 ub.modtime = sb->st_mtime;
143 if ( (utime (backup, &ub) != 0)
144 || (chmod (backup, sb->st_mode) != 0)
145 || (chown (backup, sb->st_uid, sb->st_gid) != 0)) {
155 static void vipwexit (const char *msg, int syserr, int ret)
160 if (unlink (fileeditname) != 0) {
161 fprintf (stderr, _("%s: failed to remove %s\n"), progname, fileeditname);
166 if ((*unlock) () == 0) {
167 fprintf (stderr, _("%s: failed to unlock %s\n"), progname, fileeditname);
168 SYSLOG ((LOG_ERR, "failed to unlock %s", fileeditname));
173 fprintf (stderr, "%s: %s", progname, msg);
176 fprintf (stderr, ": %s", strerror (err));
178 (void) fputs ("\n", stderr);
180 fprintf (stdout, _("%s: %s is unchanged\n"), progname,
186 #ifndef DEFAULT_EDITOR
187 #define DEFAULT_EDITOR "vi"
194 vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
198 struct stat st1, st2;
201 /* FIXME: the following should have variable sizes */
202 char filebackup[1024], fileedit[1024];
205 snprintf (filebackup, sizeof filebackup, "%s-", file);
208 if ( (mkdir (TCB_DIR "/" SHADOWTCB_SCRATCHDIR, 0700) != 0)
209 && (errno != EEXIST)) {
210 vipwexit (_("failed to create scratch directory"), errno, 1);
212 if (shadowtcb_drop_priv () == SHADOWTCB_FAILURE) {
213 vipwexit (_("failed to drop privileges"), errno, 1);
215 snprintf (fileedit, sizeof fileedit,
216 TCB_DIR "/" SHADOWTCB_SCRATCHDIR "/.vipw.shadow.%s",
219 #endif /* WITH_TCB */
220 snprintf (fileedit, sizeof fileedit, "%s.edit", file);
223 #endif /* WITH_TCB */
224 unlock = file_unlock;
226 fileeditname = fileedit;
228 if (access (file, F_OK) != 0) {
229 vipwexit (file, 1, 1);
232 /* if SE Linux is enabled then set the context of all new files
233 to be the context of the file we are editing */
234 if (is_selinux_enabled ()) {
235 security_context_t passwd_context=NULL;
237 if (getfilecon (file, &passwd_context) < 0) {
238 vipwexit (_("Couldn't get file context"), errno, 1);
240 ret = setfscreatecon (passwd_context);
241 freecon (passwd_context);
243 vipwexit (_("setfscreatecon () failed"), errno, 1);
246 #endif /* WITH_SELINUX */
248 if (tcb_mode && (shadowtcb_gain_priv () == SHADOWTCB_FAILURE)) {
249 vipwexit (_("failed to gain privileges"), errno, 1);
251 #endif /* WITH_TCB */
252 if (file_lock () == 0) {
253 vipwexit (_("Couldn't lock file"), errno, 5);
257 if (tcb_mode && (shadowtcb_drop_priv () == SHADOWTCB_FAILURE)) {
258 vipwexit (_("failed to drop privileges"), errno, 1);
260 #endif /* WITH_TCB */
262 /* edited copy has same owners, perm */
263 if (stat (file, &st1) != 0) {
264 vipwexit (file, 1, 1);
266 f = fopen (file, "r");
268 vipwexit (file, 1, 1);
271 if (tcb_mode && (shadowtcb_gain_priv () == SHADOWTCB_FAILURE))
272 vipwexit (_("failed to gain privileges"), errno, 1);
273 #endif /* WITH_TCB */
274 if (create_backup_file (f, fileedit, &st1) != 0) {
275 vipwexit (_("Couldn't make backup"), errno, 1);
280 editor = getenv ("VISUAL");
281 if (NULL == editor) {
282 editor = getenv ("EDITOR");
284 if (NULL == editor) {
285 editor = DEFAULT_EDITOR;
290 vipwexit ("fork", 1, 1);
291 } else if (0 == pid) {
292 /* use the system() call to invoke the editor so that it accepts
293 command line args in the EDITOR and VISUAL environment vars */
296 buf = (char *) malloc (strlen (editor) + strlen (fileedit) + 2);
297 snprintf (buf, strlen (editor) + strlen (fileedit) + 2,
298 "%s %s", editor, fileedit);
299 if (system (buf) != 0) {
300 fprintf (stderr, "%s: %s: %s\n", progname, editor,
309 pid = waitpid (pid, &status, WUNTRACED);
310 if ((pid != -1) && (WIFSTOPPED (status) != 0)) {
311 /* The child (editor) was suspended.
313 kill (getpid (), SIGSTOP);
314 /* wake child when resumed */
322 || (WIFEXITED (status) == 0)
323 || (WEXITSTATUS (status) != 0)) {
324 vipwexit (editor, 1, 1);
327 if (stat (fileedit, &st2) != 0) {
328 vipwexit (fileedit, 1, 1);
330 if (st1.st_mtime == st2.st_mtime) {
334 /* unset the fscreatecon */
335 if (is_selinux_enabled ()) {
336 if (setfscreatecon (NULL)) {
337 vipwexit (_("setfscreatecon () failed"), errno, 1);
340 #endif /* WITH_SELINUX */
343 * XXX - here we should check fileedit for errors; if there are any,
344 * ask the user what to do (edit again, save changes anyway, or quit
345 * without saving). Use pwck or grpck to do the check. --marekm
350 f = fopen (fileedit, "r");
352 vipwexit (_("failed to open scratch file"), errno, 1);
354 if (unlink (fileedit) != 0) {
355 vipwexit (_("failed to unlink scratch file"), errno, 1);
357 if (shadowtcb_drop_priv () == SHADOWTCB_FAILURE) {
358 vipwexit (_("failed to drop privileges"), errno, 1);
360 if (stat (file, &st1) != 0) {
361 vipwexit (_("failed to stat edited file"), errno, 1);
363 to_rename = malloc (strlen (file) + 2);
364 if (NULL == to_rename) {
365 vipwexit (_("failed to allocate memory"), errno, 1);
367 snprintf (to_rename, strlen (file) + 2, "%s+", file);
368 if (create_backup_file (f, to_rename, &st1) != 0) {
370 vipwexit (_("failed to create backup file"), errno, 1);
374 #endif /* WITH_TCB */
375 to_rename = fileedit;
378 #endif /* WITH_TCB */
380 link (file, filebackup);
381 if (rename (to_rename, file) == -1) {
383 _("%s: can't restore %s: %s (your changes are in %s)\n"),
384 progname, file, strerror (errno), to_rename);
389 #endif /* WITH_TCB */
396 if (shadowtcb_gain_priv () == SHADOWTCB_FAILURE) {
397 vipwexit (_("failed to gain privileges"), errno, 1);
400 #endif /* WITH_TCB */
402 if ((*file_unlock) () == 0) {
403 fprintf (stderr, _("%s: failed to unlock %s\n"), progname, fileeditname);
404 SYSLOG ((LOG_ERR, "failed to unlock %s", fileeditname));
407 SYSLOG ((LOG_INFO, "file %s edited", fileeditname));
410 int main (int argc, char **argv)
412 bool editshadow = false;
416 (void) setlocale (LC_ALL, "");
417 (void) bindtextdomain (PACKAGE, LOCALEDIR);
418 (void) textdomain (PACKAGE);
420 progname = ((a = strrchr (*argv, '/')) ? a + 1 : *argv);
421 do_vipw = (strcmp (progname, "vigr") != 0);
423 OPENLOG (do_vipw ? "vipw" : "vigr");
427 * Parse the command line options.
430 static struct option long_options[] = {
431 {"group", no_argument, NULL, 'g'},
432 {"help", no_argument, NULL, 'h'},
433 {"passwd", no_argument, NULL, 'p'},
434 {"quiet", no_argument, NULL, 'q'},
435 {"shadow", no_argument, NULL, 's'},
437 {"user", required_argument, NULL, 'u'},
438 #endif /* WITH_TCB */
439 {NULL, 0, NULL, '\0'}
441 while ((c = getopt_long (argc, argv,
444 #else /* !WITH_TCB */
446 #endif /* !WITH_TCB */
447 long_options, NULL)) != -1) {
468 #endif /* WITH_TCB */
478 if (getdef_bool ("USE_TCB") && (NULL != user)) {
479 if (shadowtcb_set_user (user) == SHADOWTCB_FAILURE) {
481 _("%s: failed to find tcb directory for %s\n"),
483 return E_SHADOW_NOTFOUND;
487 #endif /* WITH_TCB */
488 vipwedit (spw_dbname (), spw_lock, spw_unlock);
489 printf (MSG_WARN_EDIT_OTHER_FILE,
494 vipwedit (pw_dbname (), pw_lock, pw_unlock);
495 if (spw_file_present ()) {
496 printf (MSG_WARN_EDIT_OTHER_FILE,
505 vipwedit (sgr_dbname (), sgr_lock, sgr_unlock);
506 printf (MSG_WARN_EDIT_OTHER_FILE,
511 #endif /* SHADOWGRP */
512 vipwedit (gr_dbname (), gr_lock, gr_unlock);
514 if (sgr_file_present ()) {
515 printf (MSG_WARN_EDIT_OTHER_FILE,
521 #endif /* SHADOWGRP */
524 nscd_flush_cache ("passwd");
525 nscd_flush_cache ("group");