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.
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 (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 void usage (int status)
119 (void) fprintf ((E_SUCCESS != status) ? stderr : stdout,
120 _("Usage: %s [options]\n"
123 " -c, --crypt-method the crypt method (one of %s)\n"
124 " -e, --encrypted supplied passwords are encrypted\n"
125 " -h, --help display this help message and exit\n"
126 " -m, --md5 encrypt the clear text password using\n"
127 " the MD5 algorithm\n"
131 #ifndef USE_SHA_CRYPT
134 "NONE DES MD5 SHA256 SHA512",
135 _(" -s, --sha-rounds number of SHA rounds for the SHA*\n"
136 " crypt algorithms\n")
143 * process_flags - parse the command line options
145 * It will not return if an error is encountered.
147 static void process_flags (int argc, char **argv)
149 int option_index = 0;
151 static struct option long_options[] = {
152 {"crypt-method", required_argument, NULL, 'c'},
153 {"encrypted", no_argument, NULL, 'e'},
154 {"help", no_argument, NULL, 'h'},
155 {"md5", no_argument, NULL, 'm'},
157 {"sha-rounds", required_argument, NULL, 's'},
159 {NULL, 0, NULL, '\0'}
162 while ((c = getopt_long (argc, argv,
168 long_options, &option_index)) != -1) {
172 crypt_method = optarg;
186 if (getlong(optarg, &sha_rounds) == 0) {
188 _("%s: invalid numeric argument '%s'\n"),
200 /* validate options */
205 * check_flags - check flags and parameters consistency
207 * It will not return if an error is encountered.
209 static void check_flags (void)
214 _("%s: %s flag is only allowed with the %s flag\n"),
220 if ((eflg && (md5flg || cflg)) ||
223 _("%s: the -c, -e, and -m flags are exclusive\n"),
229 if ( (0 != strcmp (crypt_method, "DES"))
230 && (0 != strcmp (crypt_method, "MD5"))
231 && (0 != strcmp (crypt_method, "NONE"))
233 && (0 != strcmp (crypt_method, "SHA256"))
234 && (0 != strcmp (crypt_method, "SHA512"))
238 _("%s: unsupported crypt method: %s\n"),
246 * check_perms - check if the caller is allowed to add a group
248 * With PAM support, the setuid bit can be set on chgpasswd to allow
249 * non-root users to groups.
250 * Without PAM support, only users who can write in the group databases
253 * It will not return if the user is not allowed.
255 static void check_perms (void)
257 #ifdef ACCT_TOOLS_SETUID
259 pam_handle_t *pamh = NULL;
261 struct passwd *pampw;
263 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
266 _("%s: Cannot determine your user name.\n"),
271 retval = pam_start ("chgpasswd", pampw->pw_name, &conv, &pamh);
273 if (PAM_SUCCESS == retval) {
274 retval = pam_authenticate (pamh, 0);
277 if (PAM_SUCCESS == retval) {
278 retval = pam_acct_mgmt (pamh, 0);
282 (void) pam_end (pamh, retval);
284 if (PAM_SUCCESS != retval) {
285 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
289 #endif /* ACCT_TOOLS_SETUID */
293 * open_files - lock and open the group databases
295 static void open_files (void)
298 * Lock the group file and open it for reading and writing. This will
299 * bring all of the entries into memory where they may be updated.
301 if (gr_lock () == 0) {
303 _("%s: cannot lock %s; try again later.\n"),
308 if (gr_open (O_RDWR) == 0) {
310 _("%s: cannot open %s\n"), Prog, gr_dbname ());
315 /* Do the same for the shadowed database, if it exist */
317 if (sgr_lock () == 0) {
319 _("%s: cannot lock %s; try again later.\n"),
320 Prog, sgr_dbname ());
324 if (sgr_open (O_RDWR) == 0) {
325 fprintf (stderr, _("%s: cannot open %s\n"),
326 Prog, sgr_dbname ());
334 * close_files - close and unlock the group databases
336 static void close_files (void)
340 if (sgr_close () == 0) {
342 _("%s: failure while writing changes to %s\n"),
343 Prog, sgr_dbname ());
344 SYSLOG ((LOG_ERR, "failure while writing changes to %s", sgr_dbname ()));
347 if (sgr_unlock () == 0) {
348 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
349 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
356 if (gr_close () == 0) {
358 _("%s: failure while writing changes to %s\n"),
360 SYSLOG ((LOG_ERR, "failure while writing changes to %s", gr_dbname ()));
363 if (gr_unlock () == 0) {
364 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
365 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
371 int main (int argc, char **argv)
379 const struct sgrp *sg;
383 const struct group *gr;
388 Prog = Basename (argv[0]);
390 (void) setlocale (LC_ALL, "");
391 (void) bindtextdomain (PACKAGE, LOCALEDIR);
392 (void) textdomain (PACKAGE);
394 process_flags (argc, argv);
396 OPENLOG ("chgpasswd");
401 is_shadow_grp = sgr_file_present ();
407 * Read each line, separating the group name from the password. The
408 * group entry for each group will be looked up in the appropriate
409 * file (gshadow or group) and the password changed.
411 while (fgets (buf, (int) sizeof buf, stdin) != (char *) 0) {
413 cp = strrchr (buf, '\n');
417 fprintf (stderr, _("%s: line %d: line too long\n"),
424 * The group's name is the first field. It is separated from
425 * the password with a ":" character which is replaced with a
426 * NUL to give the new password. The new password will then
427 * be encrypted in the normal fashion with a new salt
428 * generated, unless the '-e' is given, in which case it is
429 * assumed to already be encrypted.
433 cp = strchr (name, ':');
439 _("%s: line %d: missing new password\n"),
446 (NULL == crypt_method ||
447 0 != strcmp(crypt_method, "NONE"))) {
450 crypt_method = "MD5";
451 } else if (crypt_method != NULL) {
460 cp = pw_encrypt (newpwd,
461 crypt_make_salt(crypt_method, arg));
465 * Get the group file entry for this group. The group must
468 gr = gr_locate (name);
471 _("%s: line %d: group '%s' does not exist\n"), Prog,
478 sg = sgr_locate (name);
485 * The freshly encrypted new password is merged into the
491 newsg.sg_passwd = cp;
496 newgr.gr_passwd = cp;
500 * The updated group file entry is then put back and will
501 * be written to the group file later, after all the
502 * other entries have been updated as well.
506 if (sgr_update (&newsg) == 0) {
508 _("%s: line %d: failed to prepare the new %s entry '%s'\n"),
509 Prog, line, sgr_dbname (), newsg.sg_name);
516 if (gr_update (&newgr) == 0) {
518 _("%s: line %d: failed to prepare the new %s entry '%s'\n"),
519 Prog, line, gr_dbname (), newgr.gr_name);
527 * Any detected errors will cause the entire set of changes to be
528 * aborted. Unlocking the group file will cause all of the
529 * changes to be ignored. Otherwise the file is closed, causing the
530 * changes to be written out all at once, and then unlocked
535 _("%s: error detected, changes ignored\n"), Prog);
541 nscd_flush_cache ("group");