2 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
3 * Copyright (c) 2002 - 2006, Tomasz Kłoczko
4 * Copyright (c) 2009 - 2011, Nicolas François
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the copyright holders or contributors may not be used to
16 * endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * pwconv - create or update /etc/shadow with information from
36 * It is more like SysV pwconv, slightly different from the original Shadow
37 * pwconv. Depends on "x" as password in /etc/passwd which means that the
38 * password has already been moved to /etc/shadow. There is no need to move
39 * /etc/npasswd to /etc/passwd, password files are updated using library
40 * routines with proper locking.
42 * Can be used to update /etc/shadow after adding/deleting users by editing
43 * /etc/passwd. There is no man page yet, but this program should be close
44 * to pwconv(1M) on Solaris 2.x.
46 * Warning: make sure that all users have "x" as the password in /etc/passwd
47 * before running this program for the first time on a system which already
48 * has shadow passwords. Anything else (like "*" from old versions of the
49 * shadow suite) will replace the user's encrypted password in /etc/shadow.
51 * Doesn't currently support pw_age information in /etc/passwd, and doesn't
52 * support DBM files. Add it if you need it...
71 #include "prototypes.h"
80 #define E_SUCCESS 0 /* success */
81 #define E_NOPERM 1 /* permission denied */
82 #define E_USAGE 2 /* invalid command syntax */
83 #define E_FAILURE 3 /* unexpected failure, nothing done */
84 #define E_MISSING 4 /* unexpected failure, passwd file missing */
85 #define E_PWDBUSY 5 /* passwd file(s) busy */
86 #define E_BADENTRY 6 /* bad shadow entry */
92 static bool spw_locked = false;
93 static bool pw_locked = false;
95 /* local function prototypes */
96 static void fail_exit (int status);
97 static void usage (int status);
98 static void process_flags (int argc, char **argv);
100 static void fail_exit (int status)
103 if (pw_unlock () == 0) {
104 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
105 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
111 if (spw_unlock () == 0) {
112 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
113 SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
121 static void usage (int status)
123 FILE *usageout = (E_SUCCESS != status) ? stderr : stdout;
124 (void) fprintf (usageout,
125 _("Usage: %s [options]\n"
129 (void) fputs (_(" -h, --help display this help message and exit\n"), usageout);
130 (void) fputs (_(" -R, --root CHROOT_DIR directory to chroot into\n"), usageout);
131 (void) fputs ("\n", usageout);
136 * process_flags - parse the command line options
138 * It will not return if an error is encountered.
140 static void process_flags (int argc, char **argv)
143 * Parse the command line options.
146 static struct option long_options[] = {
147 {"help", no_argument, NULL, 'h'},
148 {"root", required_argument, NULL, 'R'},
149 {NULL, 0, NULL, '\0'}
152 while ((c = getopt_long (argc, argv, "hR:",
153 long_options, NULL)) != -1) {
157 /*@notreached@*/break;
158 case 'R': /* no-op, handled in process_root_flag () */
165 if (optind != argc) {
170 int main (int argc, char **argv)
172 const struct passwd *pw;
174 const struct spwd *sp;
177 Prog = Basename (argv[0]);
179 (void) setlocale (LC_ALL, "");
180 (void) bindtextdomain (PACKAGE, LOCALEDIR);
181 (void) textdomain (PACKAGE);
183 process_root_flag ("-R", argc, argv);
187 process_flags (argc, argv);
190 if (getdef_bool("USE_TCB")) {
191 fprintf (stderr, _("%s: can't work with tcb enabled\n"), Prog);
194 #endif /* WITH_TCB */
196 if (pw_lock () == 0) {
198 _("%s: cannot lock %s; try again later.\n"),
200 fail_exit (E_PWDBUSY);
203 if (pw_open (O_RDWR) == 0) {
205 _("%s: cannot open %s\n"), Prog, pw_dbname ());
206 fail_exit (E_MISSING);
209 if (spw_lock () == 0) {
211 _("%s: cannot lock %s; try again later.\n"),
212 Prog, spw_dbname ());
213 fail_exit (E_PWDBUSY);
216 if (spw_open (O_CREAT | O_RDWR) == 0) {
218 _("%s: cannot open %s\n"), Prog, spw_dbname ());
219 fail_exit (E_FAILURE);
223 * Remove /etc/shadow entries for users not in /etc/passwd.
226 while ((sp = spw_next ()) != NULL) {
227 if (pw_locate (sp->sp_namp) != NULL) {
231 if (spw_remove (sp->sp_namp) == 0) {
233 * This shouldn't happen (the entry exists) but...
236 _("%s: cannot remove entry '%s' from %s\n"),
237 Prog, sp->sp_namp, spw_dbname ());
238 fail_exit (E_FAILURE);
243 * Update shadow entries which don't have "x" as pw_passwd. Add any
244 * missing shadow entries.
247 while ((pw = pw_next ()) != NULL) {
248 sp = spw_locate (pw->pw_name);
250 /* do we need to update this entry? */
251 if (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) == 0) {
254 /* update existing shadow entry */
257 /* add new shadow entry */
258 memset (&spent, 0, sizeof spent);
259 spent.sp_namp = pw->pw_name;
260 spent.sp_min = getdef_num ("PASS_MIN_DAYS", -1);
261 spent.sp_max = getdef_num ("PASS_MAX_DAYS", -1);
262 spent.sp_warn = getdef_num ("PASS_WARN_AGE", -1);
264 spent.sp_expire = -1;
265 spent.sp_flag = SHADOW_SP_FLAG_UNSET;
267 spent.sp_pwdp = pw->pw_passwd;
268 spent.sp_lstchg = (long) time ((time_t *) 0) / SCALE;
269 if (0 == spent.sp_lstchg) {
270 /* Better disable aging than requiring a password
272 spent.sp_lstchg = -1;
274 if (spw_update (&spent) == 0) {
276 _("%s: failed to prepare the new %s entry '%s'\n"),
277 Prog, spw_dbname (), spent.sp_namp);
278 fail_exit (E_FAILURE);
281 /* remove password from /etc/passwd */
283 pwent.pw_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
284 if (pw_update (&pwent) == 0) {
286 _("%s: failed to prepare the new %s entry '%s'\n"),
287 Prog, pw_dbname (), pwent.pw_name);
288 fail_exit (E_FAILURE);
292 if (spw_close () == 0) {
294 _("%s: failure while writing changes to %s\n"),
295 Prog, spw_dbname ());
296 SYSLOG ((LOG_ERR, "failure while writing changes to %s", spw_dbname ()));
297 fail_exit (E_FAILURE);
299 if (pw_close () == 0) {
301 _("%s: failure while writing changes to %s\n"),
303 SYSLOG ((LOG_ERR, "failure while writing changes to %s", pw_dbname ()));
304 fail_exit (E_FAILURE);
307 /* /etc/passwd- (backup file) */
308 if (chmod (PASSWD_FILE "-", 0600) != 0) {
310 _("%s: failed to change the mode of %s to 0600\n"),
311 Prog, PASSWD_FILE "-");
312 SYSLOG ((LOG_ERR, "failed to change the mode of %s to 0600", PASSWD_FILE "-"));
316 if (pw_unlock () == 0) {
317 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname ());
318 SYSLOG ((LOG_ERR, "failed to unlock %s", pw_dbname ()));
322 if (spw_unlock () == 0) {
323 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, spw_dbname ());
324 SYSLOG ((LOG_ERR, "failed to unlock %s", spw_dbname ()));
328 nscd_flush_cache ("passwd");