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 - 2008, 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.
46 #include "exitcodes.h"
48 #include "prototypes.h"
57 static bool cflg = false;
58 static bool eflg = false;
59 static bool md5flg = false;
60 static bool sflg = false;
62 static const char *crypt_method = NULL;
63 static long sha_rounds = 5000;
66 static bool is_shadow_grp;
67 static bool sgr_locked = false;
69 static bool gr_locked = false;
72 static pam_handle_t *pamh = NULL;
75 /* local function prototypes */
76 static void fail_exit (int code);
77 static void usage (void);
78 static void process_flags (int argc, char **argv);
79 static void check_flags (void);
80 static void check_perms (void);
81 static void open_files (void);
82 static void close_files (void);
85 * fail_exit - exit with a failure code after unlocking the files
87 static void fail_exit (int code)
90 if (gr_unlock () == 0) {
91 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
92 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
99 if (sgr_unlock () == 0) {
100 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
101 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
111 * usage - display usage message and exit
113 static void usage (void)
115 fprintf (stderr, _("Usage: %s [options]\n"
118 " -c, --crypt-method the crypt method (one of %s)\n"
119 " -e, --encrypted supplied passwords are encrypted\n"
120 " -h, --help display this help message and exit\n"
121 " -m, --md5 encrypt the clear text password using\n"
122 " the MD5 algorithm\n"
126 #ifndef USE_SHA_CRYPT
129 "NONE DES MD5 SHA256 SHA512",
130 _(" -s, --sha-rounds number of SHA rounds for the SHA*\n"
131 " crypt algorithms\n")
138 * process_flags - parse the command line options
140 * It will not return if an error is encountered.
142 static void process_flags (int argc, char **argv)
144 int option_index = 0;
146 static struct option long_options[] = {
147 {"crypt-method", required_argument, NULL, 'c'},
148 {"encrypted", no_argument, NULL, 'e'},
149 {"help", no_argument, NULL, 'h'},
150 {"md5", no_argument, NULL, 'm'},
152 {"sha-rounds", required_argument, NULL, 's'},
154 {NULL, 0, NULL, '\0'}
157 while ((c = getopt_long (argc, argv,
163 long_options, &option_index)) != -1) {
167 crypt_method = optarg;
181 if (getlong(optarg, &sha_rounds) != 0) {
183 _("%s: invalid numeric argument '%s'\n"),
198 /* validate options */
203 * check_flags - check flags and parameters consistency
205 * It will not return if an error is encountered.
207 static void check_flags (void)
211 _("%s: %s flag is only allowed with the %s flag\n"),
216 if ((eflg && (md5flg || cflg)) ||
219 _("%s: the -c, -e, and -m flags are exclusive\n"),
225 if ( (0 != strcmp (crypt_method, "DES"))
226 && (0 != strcmp (crypt_method, "MD5"))
227 && (0 != strcmp (crypt_method, "NONE"))
229 && (0 != strcmp (crypt_method, "SHA256"))
230 && (0 != strcmp (crypt_method, "SHA512"))
234 _("%s: unsupported crypt method: %s\n"),
242 * check_perms - check if the caller is allowed to add a group
244 * With PAM support, the setuid bit can be set on chgpasswd to allow
245 * non-root users to groups.
246 * Without PAM support, only users who can write in the group databases
249 * It will not return if the user is not allowed.
251 static void check_perms (void)
254 int retval = PAM_SUCCESS;
255 struct passwd *pampw;
257 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
259 retval = PAM_USER_UNKNOWN;
262 if (PAM_SUCCESS == retval) {
263 retval = pam_start ("chgpasswd", pampw->pw_name, &conv, &pamh);
266 if (PAM_SUCCESS == retval) {
267 retval = pam_authenticate (pamh, 0);
270 if (PAM_SUCCESS == retval) {
271 retval = pam_acct_mgmt (pamh, 0);
274 if (PAM_SUCCESS != retval) {
275 (void) pam_end (pamh, retval);
276 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
283 * open_files - lock and open the group databases
285 static void open_files (void)
288 * Lock the group file and open it for reading and writing. This will
289 * bring all of the entries into memory where they may be updated.
291 if (gr_lock () == 0) {
293 _("%s: cannot lock %s; try again later.\n"),
298 if (gr_open (O_RDWR) == 0) {
300 _("%s: cannot open %s\n"), Prog, gr_dbname ());
305 /* Do the same for the shadowed database, if it exist */
307 if (sgr_lock () == 0) {
309 _("%s: cannot lock %s; try again later.\n"),
310 Prog, sgr_dbname ());
314 if (sgr_open (O_RDWR) == 0) {
315 fprintf (stderr, _("%s: cannot open %s\n"),
316 Prog, sgr_dbname ());
324 * close_files - close and unlock the group databases
326 static void close_files (void)
330 if (sgr_close () == 0) {
332 _("%s: failure while writing changes to %s\n"),
333 Prog, sgr_dbname ());
334 SYSLOG ((LOG_ERR, "failure while writing changes to %s", sgr_dbname ()));
337 if (sgr_unlock () == 0) {
338 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
339 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
346 if (gr_close () == 0) {
348 _("%s: failure while writing changes to %s\n"),
350 SYSLOG ((LOG_ERR, "failure while writing changes to %s", gr_dbname ()));
353 if (gr_unlock () == 0) {
354 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
355 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
361 int main (int argc, char **argv)
369 const struct sgrp *sg;
373 const struct group *gr;
379 Prog = Basename (argv[0]);
381 (void) setlocale (LC_ALL, "");
382 (void) bindtextdomain (PACKAGE, LOCALEDIR);
383 (void) textdomain (PACKAGE);
385 process_flags (argc, argv);
387 OPENLOG ("chgpasswd");
392 is_shadow_grp = sgr_file_present ();
398 * Read each line, separating the group name from the password. The
399 * group entry for each group will be looked up in the appropriate
400 * file (gshadow or group) and the password changed.
402 while (fgets (buf, (int) sizeof buf, stdin) != (char *) 0) {
404 cp = strrchr (buf, '\n');
408 fprintf (stderr, _("%s: line %d: line too long\n"),
415 * The group's name is the first field. It is separated from
416 * the password with a ":" character which is replaced with a
417 * NUL to give the new password. The new password will then
418 * be encrypted in the normal fashion with a new salt
419 * generated, unless the '-e' is given, in which case it is
420 * assumed to already be encrypted.
424 cp = strchr (name, ':');
430 _("%s: line %d: missing new password\n"),
437 (NULL == crypt_method ||
438 0 != strcmp(crypt_method, "NONE"))) {
441 crypt_method = "MD5";
442 } else if (crypt_method != NULL) {
449 cp = pw_encrypt (newpwd,
450 crypt_make_salt(crypt_method, arg));
454 * Get the group file entry for this group. The group must
457 gr = gr_locate (name);
460 _("%s: line %d: group '%s' does not exist\n"), Prog,
467 sg = sgr_locate (name);
474 * The freshly encrypted new password is merged into the
480 newsg.sg_passwd = cp;
485 newgr.gr_passwd = cp;
489 * The updated group file entry is then put back and will
490 * be written to the group file later, after all the
491 * other entries have been updated as well.
495 ok = sgr_update (&newsg);
499 ok = gr_update (&newgr);
504 _("%s: line %d: cannot update group entry\n"),
512 * Any detected errors will cause the entire set of changes to be
513 * aborted. Unlocking the group file will cause all of the
514 * changes to be ignored. Otherwise the file is closed, causing the
515 * changes to be written out all at once, and then unlocked
520 _("%s: error detected, changes ignored\n"), Prog);
526 nscd_flush_cache ("group");
529 (void) pam_end (pamh, PAM_SUCCESS);