File: cppw.c

package info (click to toggle)
shadow 1:4.2-3+deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 13,868 kB
  • ctags: 2,298
  • sloc: ansic: 29,890; xml: 11,397; sh: 1,602; makefile: 1,271; yacc: 815; perl: 59
file content (266 lines) | stat: -rw-r--r-- 6,372 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
  cppw, cpgr  copy with locking given file over the password or group file
  with -s will copy with locking given file over shadow or gshadow file

  Copyright (C) 1999 Stephen Frost <sfrost@snowman.net>

  Based on vipw, vigr by:
  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
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  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>
#include "defines.h"

#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <utime.h>
#ifdef WITH_SELINUX
#include <selinux/selinux.h>
#endif				/* WITH_SELINUX */
#include "exitcodes.h"
#include "prototypes.h"
#include "pwio.h"
#include "shadowio.h"
#include "groupio.h"
#include "sgroupio.h"


const char *Prog;

const char *filename, *filenewname;
static bool filelocked = false;
static int (*unlock) (void);

/* local function prototypes */
static int create_copy (FILE *fp, const char *dest, struct stat *sb);
static void cppwexit (const char *msg, int syserr, int ret);
static void cppwcopy (const char *file,
                      const char *in_file,
                      int (*file_lock) (void),
                      int (*file_unlock) (void));

static int create_copy (FILE *fp, const char *dest, struct stat *sb)
{
	struct utimbuf ub;
	FILE *bkfp;
	int c;
	mode_t mask;

	mask = umask (077);
	bkfp = fopen (dest, "w");
	(void) umask (mask);
	if (NULL == bkfp) {
		return -1;
	}

	rewind (fp);
	while ((c = getc (fp)) != EOF) {
		if (putc (c, bkfp) == EOF) {
			break;
		}
	}

	if (   (c != EOF)
	    || (fflush (bkfp) != 0)) {
		(void) fclose (bkfp);
		(void) unlink (dest);
		return -1;
	}
	if (   (fsync (fileno (bkfp)) != 0)
	    || (fclose (bkfp) != 0)) {
		(void) unlink (dest);
		return -1;
	}

	ub.actime = sb->st_atime;
	ub.modtime = sb->st_mtime;
	if (   (utime (dest, &ub) != 0)
	    || (chmod (dest, sb->st_mode) != 0)
	    || (chown (dest, sb->st_uid, sb->st_gid) != 0)) {
		(void) unlink (dest);
		return -1;
	}
	return 0;
}

static void cppwexit (const char *msg, int syserr, int ret)
{
	int err = errno;
	if (filelocked) {
		(*unlock) ();
	}
	if (NULL != msg) {
		fprintf (stderr, "%s: %s", Prog, msg);
		if (0 != syserr) {
			fprintf (stderr, ": %s", strerror (err));
		}
		(void) fputs ("\n", stderr);
	}
	if (NULL != filename) {
		fprintf (stderr, _("%s: %s is unchanged\n"), Prog, filename);
	} else {
		fprintf (stderr, _("%s: no changes\n"), Prog);
	}

	exit (ret);
}

static void cppwcopy (const char *file,
                      const char *in_file,
                      int (*file_lock) (void),
                      int (*file_unlock) (void))
{
	struct stat st1;
	FILE *f;
	char filenew[1024];

	snprintf (filenew, sizeof filenew, "%s.new", file);
	unlock = file_unlock;
	filename = file;
	filenewname = filenew;

	if (access (file, F_OK) != 0) {
		cppwexit (file, 1, 1);
	}
#ifdef WITH_SELINUX
	/* if SE Linux is enabled then set the context of all new files
	 * to be the context of the file we are editing */
	if (is_selinux_enabled () > 0) {
		security_context_t passwd_context=NULL;
		int ret = 0;
		if (getfilecon (file, &passwd_context) < 0) {
			cppwexit (_("Couldn't get file context"), errno, 1);
		}
		ret = setfscreatecon (passwd_context);
		freecon (passwd_context);
		if (0 != ret) {
			cppwexit (_("setfscreatecon () failed"), errno, 1);
		}
	}
#endif				/* WITH_SELINUX */
	if (file_lock () == 0) {
		cppwexit (_("Couldn't lock file"), 0, 5);
	}
	filelocked = true;

	/* file to copy has same owners, perm */
	if (stat (file, &st1) != 0) {
		cppwexit (file, 1, 1);
	}
	f = fopen (in_file, "r");
	if (NULL == f) {
		cppwexit (in_file, 1, 1);
	}
	if (create_copy (f, filenew, &st1) != 0) {
		cppwexit (_("Couldn't make copy"), errno, 1);
	}

	/* XXX - here we should check filenew for errors; if there are any,
	 * fail w/ an appropriate error code and let the user manually fix
	 * it. Use pwck or grpck to do the check.  - Stephen (Shamelessly
	 * stolen from '--marekm's comment) */

	if (rename (filenew, file) != 0) {
		fprintf (stderr, _("%s: can't copy %s: %s)\n"),
                         Prog, filenew, strerror (errno));
		cppwexit (NULL,0,1);
	}

#ifdef WITH_SELINUX
	/* unset the fscreatecon */
	if (is_selinux_enabled () > 0) {
		if (setfscreatecon (NULL)) {
		cppwexit (_("setfscreatecon() failed"), errno, 1);
		}
	}
#endif				/* WITH_SELINUX */

	(*file_unlock) ();
}

int main (int argc, char **argv)
{
	int flag;
	bool cpshadow = false;
	char *in_file;
	int e = E_USAGE;
	bool do_cppw = true;

	(void) setlocale (LC_ALL, "");
	(void) bindtextdomain (PACKAGE, LOCALEDIR);
	(void) textdomain (PACKAGE);

	Prog = Basename (argv[0]);
	if (strcmp (Prog, "cpgr") == 0) {
		do_cppw = false;
	}

	while ((flag = getopt (argc, argv, "ghps")) != EOF) {
		switch (flag) {
		case 'p':
			do_cppw = true;
			break;
		case 'g':
			do_cppw = false;
			break;
		case 's':
			cpshadow = true;
			break;
		case 'h':
			e = E_SUCCESS;
			/*pass through*/
		default:
			(void) fputs (_("Usage:\n\
`cppw <file>' copys over /etc/passwd   `cppw -s <file>' copys over /etc/shadow\n\
`cpgr <file>' copys over /etc/group    `cpgr -s <file>' copys over /etc/gshadow\n\
"), (E_SUCCESS != e) ? stderr : stdout);
			exit (e);
		}
	}

	if (argc != optind + 1) {
		cppwexit (_("wrong number of arguments, -h for usage"),0,1);
	}

	in_file = argv[optind];

	if (do_cppw) {
		if (cpshadow) {
			cppwcopy (SHADOW_FILE, in_file, spw_lock, spw_unlock);
		} else {
			cppwcopy (PASSWD_FILE, in_file, pw_lock, pw_unlock);
		}
	} else {
#ifdef SHADOWGRP
		if (cpshadow) {
			cppwcopy (SGROUP_FILE, in_file, sgr_lock, sgr_unlock);
		} else
#endif				/* SHADOWGRP */
		{
			cppwcopy (GROUP_FILE, in_file, gr_lock, gr_unlock);
		}
	}

	return 0;
}