]> granicus.if.org Git - shadow/blob - src/pwconv.c
* src/chfn.c, man/chfn.1.xml: Add support for long options.
[shadow] / src / pwconv.c
1 /*
2  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
3  * Copyright (c) 2002 - 2006, Tomasz Kłoczko
4  * Copyright (c) 2009 - 2011, Nicolas François
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
30  */
31
32 /*
33  * pwconv - create or update /etc/shadow with information from
34  * /etc/passwd.
35  *
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.
41  *
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.
45  *
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.
50  *
51  * Doesn't currently support pw_age information in /etc/passwd, and doesn't
52  * support DBM files. Add it if you need it...
53  *
54  */
55
56 #include <config.h>
57
58 #ident "$Id$"
59
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <pwd.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <time.h>
67 #include <unistd.h>
68 #include <getopt.h>
69 #include "defines.h"
70 #include "getdef.h"
71 #include "prototypes.h"
72 #include "pwio.h"
73 #include "shadowio.h"
74 #include "nscd.h"
75
76 /*
77  * exit status values
78  */
79 /*@-exitarg@*/
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 */
87 /*
88  * Global variables
89  */
90 const char *Prog;
91
92 static bool spw_locked = false;
93 static bool pw_locked = false;
94
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);
99
100 static void fail_exit (int status)
101 {
102         if (pw_locked) {
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 ()));
106                         /* continue */
107                 }
108         }
109
110         if (spw_locked) {
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 ()));
114                         /* continue */
115                 }
116         }
117
118         exit (status);
119 }
120
121 static void usage (int status)
122 {
123         FILE *usageout = (E_SUCCESS != status) ? stderr : stdout;
124         (void) fprintf (usageout,
125                         _("Usage: %s [options]\n"
126                           "\n"
127                           "Options:\n"),
128                         Prog);
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);
132         exit (status);
133 }
134
135 /*
136  * process_flags - parse the command line options
137  *
138  *      It will not return if an error is encountered.
139  */
140 static void process_flags (int argc, char **argv)
141 {
142         /*
143          * Parse the command line options.
144          */
145         int c;
146         static struct option long_options[] = {
147                 {"help", no_argument, NULL, 'h'},
148                 {"root", required_argument, NULL, 'R'},
149                 {NULL, 0, NULL, '\0'}
150         };
151
152         while ((c = getopt_long (argc, argv, "hR:",
153                                  long_options, NULL)) != -1) {
154                 switch (c) {
155                 case 'h':
156                         usage (E_SUCCESS);
157                         /*@notreached@*/break;
158                 case 'R': /* no-op, handled in process_root_flag () */
159                         break;
160                 default:
161                         usage (E_USAGE);
162                 }
163         }
164
165         if (optind != argc) {
166                 usage (E_USAGE);
167         }
168 }
169
170 int main (int argc, char **argv)
171 {
172         const struct passwd *pw;
173         struct passwd pwent;
174         const struct spwd *sp;
175         struct spwd spent;
176
177         Prog = Basename (argv[0]);
178
179         (void) setlocale (LC_ALL, "");
180         (void) bindtextdomain (PACKAGE, LOCALEDIR);
181         (void) textdomain (PACKAGE);
182
183         process_root_flag ("-R", argc, argv);
184
185         OPENLOG ("pwconv");
186
187         process_flags (argc, argv);
188
189 #ifdef WITH_TCB
190         if (getdef_bool("USE_TCB")) {
191                 fprintf (stderr, _("%s: can't work with tcb enabled\n"), Prog);
192                 exit (E_FAILURE);
193         }
194 #endif                          /* WITH_TCB */
195
196         if (pw_lock () == 0) {
197                 fprintf (stderr,
198                          _("%s: cannot lock %s; try again later.\n"),
199                          Prog, pw_dbname ());
200                 fail_exit (E_PWDBUSY);
201         }
202         pw_locked = true;
203         if (pw_open (O_RDWR) == 0) {
204                 fprintf (stderr,
205                          _("%s: cannot open %s\n"), Prog, pw_dbname ());
206                 fail_exit (E_MISSING);
207         }
208
209         if (spw_lock () == 0) {
210                 fprintf (stderr,
211                          _("%s: cannot lock %s; try again later.\n"),
212                          Prog, spw_dbname ());
213                 fail_exit (E_PWDBUSY);
214         }
215         spw_locked = true;
216         if (spw_open (O_CREAT | O_RDWR) == 0) {
217                 fprintf (stderr,
218                          _("%s: cannot open %s\n"), Prog, spw_dbname ());
219                 fail_exit (E_FAILURE);
220         }
221
222         /*
223          * Remove /etc/shadow entries for users not in /etc/passwd.
224          */
225         spw_rewind ();
226         while ((sp = spw_next ()) != NULL) {
227                 if (pw_locate (sp->sp_namp) != NULL) {
228                         continue;
229                 }
230
231                 if (spw_remove (sp->sp_namp) == 0) {
232                         /*
233                          * This shouldn't happen (the entry exists) but...
234                          */
235                         fprintf (stderr,
236                                  _("%s: cannot remove entry '%s' from %s\n"),
237                                  Prog, sp->sp_namp, spw_dbname ());
238                         fail_exit (E_FAILURE);
239                 }
240         }
241
242         /*
243          * Update shadow entries which don't have "x" as pw_passwd. Add any
244          * missing shadow entries.
245          */
246         pw_rewind ();
247         while ((pw = pw_next ()) != NULL) {
248                 sp = spw_locate (pw->pw_name);
249                 if (NULL != sp) {
250                         /* do we need to update this entry? */
251                         if (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) == 0) {
252                                 continue;
253                         }
254                         /* update existing shadow entry */
255                         spent = *sp;
256                 } else {
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);
263                         spent.sp_inact  = -1;
264                         spent.sp_expire = -1;
265                         spent.sp_flag   = SHADOW_SP_FLAG_UNSET;
266                 }
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
271                          * change */
272                         spent.sp_lstchg = -1;
273                 }
274                 if (spw_update (&spent) == 0) {
275                         fprintf (stderr,
276                                  _("%s: failed to prepare the new %s entry '%s'\n"),
277                                  Prog, spw_dbname (), spent.sp_namp);
278                         fail_exit (E_FAILURE);
279                 }
280
281                 /* remove password from /etc/passwd */
282                 pwent = *pw;
283                 pwent.pw_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
284                 if (pw_update (&pwent) == 0) {
285                         fprintf (stderr,
286                                  _("%s: failed to prepare the new %s entry '%s'\n"),
287                                  Prog, pw_dbname (), pwent.pw_name);
288                         fail_exit (E_FAILURE);
289                 }
290         }
291
292         if (spw_close () == 0) {
293                 fprintf (stderr,
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);
298         }
299         if (pw_close () == 0) {
300                 fprintf (stderr,
301                          _("%s: failure while writing changes to %s\n"),
302                          Prog, pw_dbname ());
303                 SYSLOG ((LOG_ERR, "failure while writing changes to %s", pw_dbname ()));
304                 fail_exit (E_FAILURE);
305         }
306
307         /* /etc/passwd- (backup file) */
308         if (chmod (PASSWD_FILE "-", 0600) != 0) {
309                 fprintf (stderr,
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 "-"));
313                 /* continue */
314         }
315
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 ()));
319                 /* continue */
320         }
321
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 ()));
325                 /* continue */
326         }
327
328         nscd_flush_cache ("passwd");
329
330         return E_SUCCESS;
331 }
332