2 Copyright (C) 2001-2006 Ben Kibbey <bjk@luxsci.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/types.h>
34 #include "../strsep.c"
41 void ui_module_init(int *chainable
)
44 fprintf(stderr
, "%s: ui_module_init()\n", __FILE__
);
48 * Keep the password file open if possible (*BSD).
50 #ifdef HAVE_SETPASSENT
64 fprintf(stderr
, "%s: ui_module_exit()\n", __FILE__
);
77 /* See if the gecos options are valid. */
78 static int parse_gecos_options(const char *args
)
82 for (i
= 0; i
< strlen(args
); i
++) {
98 /* Break up the gecos string into sections and add the sections to the output
99 * string array if needed. */
100 static void gecos_strings(char *str
)
104 const char *name
, *first
, *second
, *third
;
106 name
= first
= second
= third
= "-";
108 while ((buf
= strsep(&str
, ",")) != NULL
) {
130 for (i
= 0; i
< strlen(gecos_options
); i
++) {
131 switch (gecos_options
[i
]) {
133 add_string(&strings
, name
);
136 add_string(&strings
, first
);
139 add_string(&strings
, second
);
142 add_string(&strings
, third
);
145 add_string(&strings
, name
);
146 add_string(&strings
, first
);
147 add_string(&strings
, second
);
148 add_string(&strings
, third
);
158 /* Get all groups that a user is a member of. The primary group will be the
160 static void groups(const struct passwd
*pw
, const int multi
,
170 if ((grp
= getgrgid(pw
->pw_gid
)) == NULL
) {
171 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s", (long) pw
->pw_gid
,
172 (verbose
) ? "(" : "", (verbose
) ? "!" : "",
173 (verbose
) ? ")" : "");
174 add_string(&strings
, tmp
);
178 primary
= grp
->gr_gid
;
179 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s%c", (long) pw
->pw_gid
,
180 (verbose
) ? "(" : "", (verbose
) ? grp
->gr_name
: "",
181 (verbose
) ? ")" : "", multi
);
182 strncat(line
, tmp
, sizeof(line
));
184 #ifdef HAVE_SETGROUPENT
190 while ((grp
= getgrent()) != NULL
) {
191 char **members
= grp
->gr_mem
;
194 if (strcmp(*members
++, pw
->pw_name
) == 0) {
195 if (grp
->gr_gid
== primary
)
198 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s%c", (long) grp
->gr_gid
,
199 (verbose
) ? "(" : "", (verbose
) ? grp
->gr_name
: "",
200 (verbose
) ? ")" : "", multi
);
201 strncat(line
, tmp
, sizeof(line
));
207 * Trim the remaining multi-string deliminator.
209 line
[strlen(line
) - 1] = '\0';
211 add_string(&strings
, line
);
215 /* This is output if the -h command line option is passed to the main program.
217 void ui_module_help()
220 fprintf(stderr
, "%s: ui_module_help()\n", __FILE__
);
223 printf(" Password/Group file information [-P (-%s)]:\n",
224 PASSWD_OPTION_ORDER
);
225 printf("\t-l login name\t\t");
226 printf("\t-p encrypted password\n");
227 printf("\t-u user id (uid)\t");
228 printf("\t-g group id (gid)\n");
229 printf("\t-c password change time");
230 printf("\t-e password expire time\n");
231 printf("\t-d home directory\t");
232 printf("\t-m home directory mode\n");
233 printf("\t-s login shell\n");
234 printf("\t-i gecos (any of [n]ame,[1]st,[2]nd,[3]rd or [a]ll)\n\n");
238 /* This is the equivalent to main() only without argc and argv available. */
239 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi_char
,
240 const int verbose
, char *tf
)
246 struct spwd
*spwd
= NULL
;
250 fprintf(stderr
, "%s: ui_module_exec()\n", __FILE__
);
255 if ((spwd
= getspnam(pw
->pw_name
)) == NULL
)
256 warnx("%s", "getspnam(): unknown error");
269 add_string(&strings
, "!");
273 snprintf(tmp
, sizeof(tmp
), "%li", (long) spwd
->sp_max
);
274 add_string(&strings
, tmp
);
278 add_string(&strings
, "!");
282 snprintf(tmp
, sizeof(tmp
), "%li", (long) spwd
->sp_expire
);
283 add_string(&strings
, tmp
);
287 snprintf(tmp
, sizeof(tmp
), "%li", (long) pw
->pw_change
);
288 add_string(&strings
, tmp
);
291 snprintf(tmp
, sizeof(tmp
), "%li", (long) pw
->pw_expire
);
292 add_string(&strings
, tmp
);
296 add_string(&strings
, pw
->pw_name
);
299 add_string(&strings
, (pw
->pw_dir
&& pw
->pw_dir
[0]) ? pw
->pw_dir
: "-");
302 add_string(&strings
, (pw
->pw_shell
&& pw
->pw_shell
[0]) ? pw
->pw_shell
: "-");
307 add_string(&strings
, (pw
->pw_passwd
308 && pw
->pw_passwd
[0]) ? pw
->pw_passwd
: "-");
310 add_string(&strings
, (spwd
->sp_pwdp
311 && spwd
->sp_pwdp
[0]) ? spwd
->sp_pwdp
: "-");
313 add_string(&strings
, (pw
->pw_passwd
314 && pw
->pw_passwd
[0]) ? pw
->pw_passwd
: "-");
318 sprintf(tmp
, "%li", (long) pw
->pw_uid
);
319 add_string(&strings
, tmp
);
322 groups(pw
, multi_char
, verbose
);
325 if (stat(pw
->pw_dir
, &st
) == -1) {
326 add_string(&strings
, "!");
330 sprintf(tmp
, "%.4o", (unsigned) st
.st_mode
& ALLPERMS
);
331 add_string(&strings
, tmp
);
334 gecos_strings(pw
->pw_gecos
);
347 char *ui_module_options_init(char **defaults
)
350 return PASSWD_OPTION_STRING
;
353 /* Check module option validity. */
354 int ui_module_options(int argc
, char **argv
)
360 fprintf(stderr
, "%s: ui_module_options()\n", __FILE__
);
363 while ((opt
= getopt(argc
, argv
, PASSWD_OPTION_STRING
)) != -1) {
366 gecos_options
= optarg
;
380 warnx("passwd: invalid option -- %c", optopt
);
386 if (parse_gecos_options(gecos_options
))
391 * This option '-P' sets all available options for this module.
394 strncpy(options
, PASSWD_OPTION_ORDER
, sizeof(options
));