2 * Copyright (c) 1996 , Michael Meskes
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2002 - 2006, Tomasz Kłoczko
5 * Copyright (c) 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.
34 * grpunconv - update /etc/group with information from /etc/gshadow.
40 #ident "$Id: grpunconv.c 2348 2008-09-06 12:51:53Z nekral-guest $"
50 #include "prototypes.h"
59 static bool gr_locked = false;
60 static bool sgr_locked = false;
62 /* local function prototypes */
63 static void fail_exit (int status);
65 static void fail_exit (int status)
68 if (gr_unlock () == 0) {
69 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
70 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
76 if (sgr_unlock () == 0) {
77 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
78 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
86 int main (int argc, char **argv)
88 const struct group *gr;
90 const struct sgrp *sg;
93 (void) fputs (_("Usage: grpunconv\n"), stderr);
95 Prog = Basename (argv[0]);
97 (void) setlocale (LC_ALL, "");
98 (void) bindtextdomain (PACKAGE, LOCALEDIR);
99 (void) textdomain (PACKAGE);
101 OPENLOG ("grpunconv");
103 if (sgr_file_present () == 0) {
104 exit (0); /* no /etc/gshadow, nothing to do */
107 if (gr_lock () == 0) {
109 _("%s: cannot lock %s; try again later.\n"),
114 if (gr_open (O_RDWR) == 0) {
116 _("%s: cannot open %s\n"), Prog, gr_dbname ());
120 if (sgr_lock () == 0) {
122 _("%s: cannot lock %s; try again later.\n"),
123 Prog, sgr_dbname ());
127 if (sgr_open (O_RDWR) == 0) {
129 _("%s: cannot open %s\n"), Prog, sgr_dbname ());
134 * Update group passwords if non-shadow password is "x".
137 while ((gr = gr_next ()) != NULL) {
138 sg = sgr_locate (gr->gr_name);
140 && (strcmp (gr->gr_passwd, SHADOW_PASSWD_STRING) == 0)) {
141 /* add password to /etc/group */
143 grent.gr_passwd = sg->sg_passwd;
144 if (gr_update (&grent) == 0) {
146 _("%s: failed to prepare the new %s entry '%s'\n"),
147 Prog, gr_dbname (), grent.gr_name);
153 if (sgr_close () == 0) {
155 _("%s: failure while writing changes to %s\n"),
156 Prog, sgr_dbname ());
157 SYSLOG ((LOG_ERR, "failure while writing changes to %s", sgr_dbname ()));
161 if (gr_close () == 0) {
163 _("%s: failure while writing changes to %s\n"),
165 SYSLOG ((LOG_ERR, "failure while writing changes to %s", gr_dbname ()));
169 if (unlink (SGROUP_FILE) != 0) {
171 _("%s: cannot delete %s\n"),
173 SYSLOG ((LOG_ERR, "cannot delete %s", SGROUP_FILE));
177 if (gr_unlock () == 0) {
178 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
179 SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
183 if (sgr_unlock () == 0) {
184 fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
185 SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
189 nscd_flush_cache ("group");
193 #else /* !SHADOWGRP */
194 int main (int unused(argc), char **argv)
197 "%s: not configured for shadow group support.\n", argv[0]);
200 #endif /* !SHADOWGRP */