2 * Copyright (c) 1990 - 1994, Julianne Frances Haugh
3 * Copyright (c) 2006 , Tomasz Kłoczko
4 * Copyright (c) 2006 , Jonas Meurer
5 * Copyright (c) 2007 - 2009, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ident "$Id: chgpasswd.c 2851 2009-04-30 21:39:38Z nekral-guest $"
42 #ifdef ACCT_TOOLS_SETUID
46 #endif /* ACCT_TOOLS_SETUID */
49 #include "prototypes.h"
55 #include "exitcodes.h"
61 static bool cflg = false;
62 static bool eflg = false;
63 static bool md5flg = false;
65 static bool sflg = false;
68 static const char *crypt_method = NULL;
70 static long sha_rounds = 5000;
74 static bool is_shadow_grp;
75 static bool sgr_locked = false;
77 static bool gr_locked = false;
79 /* local function prototypes */
80 static void fail_exit (int code);
81 static void usage (void);
82 static void process_flags (int argc, char **argv);
83 static void check_flags (void);
84 static void check_perms (void);
85 static void open_files (void);
86 static void close_files (void);
89 * fail_exit - exit with a failure code after unlocking the files
91 static void fail_exit (int code)
94 if (gr_unlock () == 0) {
95 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
96 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
103 if (sgr_unlock () == 0) {
104 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
105 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
115 * usage - display usage message and exit
117 static void usage (void)
119 fprintf (stderr, _("Usage: %s [options]\n"
122 " -c, --crypt-method the crypt method (one of %s)\n"
123 " -e, --encrypted supplied passwords are encrypted\n"
124 " -h, --help display this help message and exit\n"
125 " -m, --md5 encrypt the clear text password using\n"
126 " the MD5 algorithm\n"
130 #ifndef USE_SHA_CRYPT
133 "NONE DES MD5 SHA256 SHA512",
134 _(" -s, --sha-rounds number of SHA rounds for the SHA*\n"
135 " crypt algorithms\n")
142 * process_flags - parse the command line options
144 * It will not return if an error is encountered.
146 static void process_flags (int argc, char **argv)
148 int option_index = 0;
150 static struct option long_options[] = {
151 {"crypt-method", required_argument, NULL, 'c'},
152 {"encrypted", no_argument, NULL, 'e'},
153 {"help", no_argument, NULL, 'h'},
154 {"md5", no_argument, NULL, 'm'},
156 {"sha-rounds", required_argument, NULL, 's'},
158 {NULL, 0, NULL, '\0'}
161 while ((c = getopt_long (argc, argv,
167 long_options, &option_index)) != -1) {
171 crypt_method = optarg;
185 if (getlong(optarg, &sha_rounds) == 0) {
187 _("%s: invalid numeric argument '%s'\n"),
199 /* validate options */
204 * check_flags - check flags and parameters consistency
206 * It will not return if an error is encountered.
208 static void check_flags (void)
213 _("%s: %s flag is only allowed with the %s flag\n"),
219 if ((eflg && (md5flg || cflg)) ||
222 _("%s: the -c, -e, and -m flags are exclusive\n"),
228 if ( (0 != strcmp (crypt_method, "DES"))
229 && (0 != strcmp (crypt_method, "MD5"))
230 && (0 != strcmp (crypt_method, "NONE"))
232 && (0 != strcmp (crypt_method, "SHA256"))
233 && (0 != strcmp (crypt_method, "SHA512"))
237 _("%s: unsupported crypt method: %s\n"),
245 * check_perms - check if the caller is allowed to add a group
247 * With PAM support, the setuid bit can be set on chgpasswd to allow
248 * non-root users to groups.
249 * Without PAM support, only users who can write in the group databases
252 * It will not return if the user is not allowed.
254 static void check_perms (void)
256 #ifdef ACCT_TOOLS_SETUID
258 pam_handle_t *pamh = NULL;
260 struct passwd *pampw;
262 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
265 _("%s: Cannot determine your user name.\n"),
270 retval = pam_start ("chgpasswd", pampw->pw_name, &conv, &pamh);
272 if (PAM_SUCCESS == retval) {
273 retval = pam_authenticate (pamh, 0);
276 if (PAM_SUCCESS == retval) {
277 retval = pam_acct_mgmt (pamh, 0);
281 (void) pam_end (pamh, retval);
283 if (PAM_SUCCESS != retval) {
284 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
288 #endif /* ACCT_TOOLS_SETUID */
292 * open_files - lock and open the group databases
294 static void open_files (void)
297 * Lock the group file and open it for reading and writing. This will
298 * bring all of the entries into memory where they may be updated.
300 if (gr_lock () == 0) {
302 _("%s: cannot lock %s; try again later.\n"),
307 if (gr_open (O_RDWR) == 0) {
309 _("%s: cannot open %s\n"), Prog, gr_dbname ());
314 /* Do the same for the shadowed database, if it exist */
316 if (sgr_lock () == 0) {
318 _("%s: cannot lock %s; try again later.\n"),
319 Prog, sgr_dbname ());
323 if (sgr_open (O_RDWR) == 0) {
324 fprintf (stderr, _("%s: cannot open %s\n"),
325 Prog, sgr_dbname ());
333 * close_files - close and unlock the group databases
335 static void close_files (void)
339 if (sgr_close () == 0) {
341 _("%s: failure while writing changes to %s\n"),
342 Prog, sgr_dbname ());
343 SYSLOG ((LOG_ERR, "failure while writing changes to %s", sgr_dbname ()));
346 if (sgr_unlock () == 0) {
347 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
348 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
355 if (gr_close () == 0) {
357 _("%s: failure while writing changes to %s\n"),
359 SYSLOG ((LOG_ERR, "failure while writing changes to %s", gr_dbname ()));
362 if (gr_unlock () == 0) {
363 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
364 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
370 int main (int argc, char **argv)
378 const struct sgrp *sg;
382 const struct group *gr;
387 Prog = Basename (argv[0]);
389 (void) setlocale (LC_ALL, "");
390 (void) bindtextdomain (PACKAGE, LOCALEDIR);
391 (void) textdomain (PACKAGE);
393 process_flags (argc, argv);
395 OPENLOG ("chgpasswd");
400 is_shadow_grp = sgr_file_present ();
406 * Read each line, separating the group name from the password. The
407 * group entry for each group will be looked up in the appropriate
408 * file (gshadow or group) and the password changed.
410 while (fgets (buf, (int) sizeof buf, stdin) != (char *) 0) {
412 cp = strrchr (buf, '\n');
416 fprintf (stderr, _("%s: line %d: line too long\n"),
423 * The group's name is the first field. It is separated from
424 * the password with a ":" character which is replaced with a
425 * NUL to give the new password. The new password will then
426 * be encrypted in the normal fashion with a new salt
427 * generated, unless the '-e' is given, in which case it is
428 * assumed to already be encrypted.
432 cp = strchr (name, ':');
438 _("%s: line %d: missing new password\n"),
445 (NULL == crypt_method ||
446 0 != strcmp(crypt_method, "NONE"))) {
449 crypt_method = "MD5";
450 } else if (crypt_method != NULL) {
459 cp = pw_encrypt (newpwd,
460 crypt_make_salt(crypt_method, arg));
464 * Get the group file entry for this group. The group must
467 gr = gr_locate (name);
470 _("%s: line %d: group '%s' does not exist\n"), Prog,
477 sg = sgr_locate (name);
484 * The freshly encrypted new password is merged into the
490 newsg.sg_passwd = cp;
495 newgr.gr_passwd = cp;
499 * The updated group file entry is then put back and will
500 * be written to the group file later, after all the
501 * other entries have been updated as well.
505 if (sgr_update (&newsg) == 0) {
507 _("%s: line %d: failed to prepare the new %s entry '%s'\n"),
508 Prog, line, sgr_dbname (), newsg.sg_name);
515 if (gr_update (&newgr) == 0) {
517 _("%s: line %d: failed to prepare the new %s entry '%s'\n"),
518 Prog, line, gr_dbname (), newgr.gr_name);
526 * Any detected errors will cause the entire set of changes to be
527 * aborted. Unlocking the group file will cause all of the
528 * changes to be ignored. Otherwise the file is closed, causing the
529 * changes to be written out all at once, and then unlocked
534 _("%s: error detected, changes ignored\n"), Prog);
540 nscd_flush_cache ("group");