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 - 2011, 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.
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 /*@noreturn@*/void usage (int status);
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 /*@noreturn@*/void usage (int status)
119 FILE *usageout = (E_SUCCESS != status) ? stderr : stdout;
120 (void) fprintf (usageout,
121 _("Usage: %s [options]\n"
125 (void) fprintf (usageout,
126 _(" -c, --crypt-method <METHOD> the crypt method (one of %s)\n"),
127 #ifndef USE_SHA_CRYPT
129 #else /* USE_SHA_CRYPT */
130 "NONE DES MD5 SHA256 SHA512"
131 #endif /* USE_SHA_CRYPT */
133 (void) fputs (_(" -e, --encrypted supplied passwords are encrypted\n"), usageout);
134 (void) fputs (_(" -h, --help display this help message and exit\n"), usageout);
135 (void) fputs (_(" -m, --md5 encrypt the clear text password using\n"
136 " the MD5 algorithm\n"),
139 (void) fputs (_(" -s, --sha-rounds number of SHA rounds for the SHA*\n"
140 " crypt algorithms\n"),
142 #endif /* USE_SHA_CRYPT */
143 (void) fputs ("\n", usageout);
149 * process_flags - parse the command line options
151 * It will not return if an error is encountered.
153 static void process_flags (int argc, char **argv)
155 int option_index = 0;
157 static struct option long_options[] = {
158 {"crypt-method", required_argument, NULL, 'c'},
159 {"encrypted", no_argument, NULL, 'e'},
160 {"help", no_argument, NULL, 'h'},
161 {"md5", no_argument, NULL, 'm'},
163 {"sha-rounds", required_argument, NULL, 's'},
165 {NULL, 0, NULL, '\0'}
168 while ((c = getopt_long (argc, argv,
174 long_options, &option_index)) != -1) {
178 crypt_method = optarg;
185 /*@notreached@*/break;
192 if (getlong(optarg, &sha_rounds) == 0) {
194 _("%s: invalid numeric argument '%s'\n"),
202 /*@notreached@*/break;
206 /* validate options */
211 * check_flags - check flags and parameters consistency
213 * It will not return if an error is encountered.
215 static void check_flags (void)
220 _("%s: %s flag is only allowed with the %s flag\n"),
226 if ((eflg && (md5flg || cflg)) ||
229 _("%s: the -c, -e, and -m flags are exclusive\n"),
235 if ( (0 != strcmp (crypt_method, "DES"))
236 && (0 != strcmp (crypt_method, "MD5"))
237 && (0 != strcmp (crypt_method, "NONE"))
239 && (0 != strcmp (crypt_method, "SHA256"))
240 && (0 != strcmp (crypt_method, "SHA512"))
244 _("%s: unsupported crypt method: %s\n"),
252 * check_perms - check if the caller is allowed to add a group
254 * With PAM support, the setuid bit can be set on chgpasswd to allow
255 * non-root users to groups.
256 * Without PAM support, only users who can write in the group databases
259 * It will not return if the user is not allowed.
261 static void check_perms (void)
263 #ifdef ACCT_TOOLS_SETUID
265 pam_handle_t *pamh = NULL;
267 struct passwd *pampw;
269 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
272 _("%s: Cannot determine your user name.\n"),
277 retval = pam_start ("chgpasswd", pampw->pw_name, &conv, &pamh);
279 if (PAM_SUCCESS == retval) {
280 retval = pam_authenticate (pamh, 0);
283 if (PAM_SUCCESS == retval) {
284 retval = pam_acct_mgmt (pamh, 0);
288 (void) pam_end (pamh, retval);
290 if (PAM_SUCCESS != retval) {
291 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
295 #endif /* ACCT_TOOLS_SETUID */
299 * open_files - lock and open the group databases
301 static void open_files (void)
304 * Lock the group file and open it for reading and writing. This will
305 * bring all of the entries into memory where they may be updated.
307 if (gr_lock () == 0) {
309 _("%s: cannot lock %s; try again later.\n"),
314 if (gr_open (O_RDWR) == 0) {
316 _("%s: cannot open %s\n"), Prog, gr_dbname ());
321 /* Do the same for the shadowed database, if it exist */
323 if (sgr_lock () == 0) {
325 _("%s: cannot lock %s; try again later.\n"),
326 Prog, sgr_dbname ());
330 if (sgr_open (O_RDWR) == 0) {
331 fprintf (stderr, _("%s: cannot open %s\n"),
332 Prog, sgr_dbname ());
340 * close_files - close and unlock the group databases
342 static void close_files (void)
346 if (sgr_close () == 0) {
348 _("%s: failure while writing changes to %s\n"),
349 Prog, sgr_dbname ());
350 SYSLOG ((LOG_ERR, "failure while writing changes to %s", sgr_dbname ()));
353 if (sgr_unlock () == 0) {
354 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
355 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
362 if (gr_close () == 0) {
364 _("%s: failure while writing changes to %s\n"),
366 SYSLOG ((LOG_ERR, "failure while writing changes to %s", gr_dbname ()));
369 if (gr_unlock () == 0) {
370 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
371 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
377 int main (int argc, char **argv)
385 const struct sgrp *sg;
389 const struct group *gr;
394 Prog = Basename (argv[0]);
396 (void) setlocale (LC_ALL, "");
397 (void) bindtextdomain (PACKAGE, LOCALEDIR);
398 (void) textdomain (PACKAGE);
400 process_flags (argc, argv);
402 OPENLOG ("chgpasswd");
407 is_shadow_grp = sgr_file_present ();
413 * Read each line, separating the group name from the password. The
414 * group entry for each group will be looked up in the appropriate
415 * file (gshadow or group) and the password changed.
417 while (fgets (buf, (int) sizeof buf, stdin) != (char *) 0) {
419 cp = strrchr (buf, '\n');
423 fprintf (stderr, _("%s: line %d: line too long\n"),
430 * The group's name is the first field. It is separated from
431 * the password with a ":" character which is replaced with a
432 * NUL to give the new password. The new password will then
433 * be encrypted in the normal fashion with a new salt
434 * generated, unless the '-e' is given, in which case it is
435 * assumed to already be encrypted.
439 cp = strchr (name, ':');
445 _("%s: line %d: missing new password\n"),
452 && ( (NULL == crypt_method)
453 || (0 != strcmp(crypt_method, "NONE")))) {
456 crypt_method = "MD5";
463 cp = pw_encrypt (newpwd,
464 crypt_make_salt(crypt_method, arg));
468 * Get the group file entry for this group. The group must
471 gr = gr_locate (name);
474 _("%s: line %d: group '%s' does not exist\n"), Prog,
481 sg = sgr_locate (name);
488 * The freshly encrypted new password is merged into the
494 newsg.sg_passwd = cp;
499 newgr.gr_passwd = cp;
503 * The updated group file entry is then put back and will
504 * be written to the group file later, after all the
505 * other entries have been updated as well.
509 if (sgr_update (&newsg) == 0) {
511 _("%s: line %d: failed to prepare the new %s entry '%s'\n"),
512 Prog, line, sgr_dbname (), newsg.sg_name);
519 if (gr_update (&newgr) == 0) {
521 _("%s: line %d: failed to prepare the new %s entry '%s'\n"),
522 Prog, line, gr_dbname (), newgr.gr_name);
530 * Any detected errors will cause the entire set of changes to be
531 * aborted. Unlocking the group file will cause all of the
532 * changes to be ignored. Otherwise the file is closed, causing the
533 * changes to be written out all at once, and then unlocked
538 _("%s: error detected, changes ignored\n"), Prog);
544 nscd_flush_cache ("group");