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
26 #include <sys/types.h>
52 #include "../strsep.c"
66 #define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
69 /* This is the order of options when the 'P' option is specified. It selects
70 * all available options. */
71 #define PASSWD_OPTION_ORDER "lpugicedsm"
72 #define PASSWD_OPTION_STRING "Plpugcedsmi:"
75 static char **strings
;
76 static char options
[11]; /* NULL terminated. */
77 static char *gecos_options
;
79 void add_string(char ***, const char *);
80 char *stamp(time_t, const char *);
82 void ui_module_init(int *chainable
)
85 * Keep the password file open if possible (*BSD).
87 #ifdef HAVE_SETPASSENT
108 /* See if the gecos options are valid. */
109 static int parse_gecos_options(const char *args
)
113 for (i
= 0; i
< strlen(args
); i
++) {
129 /* Break up the gecos string into sections and add the sections to the output
130 * string array if needed. */
131 static void gecos_strings(char *str
)
135 const char *name
, *first
, *second
, *third
;
137 name
= first
= second
= third
= "-";
139 while ((buf
= strsep(&str
, ",")) != NULL
) {
161 for (i
= 0; i
< strlen(gecos_options
); i
++) {
162 switch (gecos_options
[i
]) {
164 add_string(&strings
, name
);
167 add_string(&strings
, first
);
170 add_string(&strings
, second
);
173 add_string(&strings
, third
);
176 add_string(&strings
, name
);
177 add_string(&strings
, first
);
178 add_string(&strings
, second
);
179 add_string(&strings
, third
);
187 /* Get all groups that a user is a member of. The primary group will be the
189 static void groups(const struct passwd
*pw
, const int multi
,
199 if ((grp
= getgrgid(pw
->pw_gid
)) == NULL
) {
200 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s", (long) pw
->pw_gid
,
201 (verbose
) ? "(" : "", (verbose
) ? "!" : "",
202 (verbose
) ? ")" : "");
203 add_string(&strings
, tmp
);
207 primary
= grp
->gr_gid
;
208 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s%c", (long) pw
->pw_gid
,
209 (verbose
) ? "(" : "", (verbose
) ? grp
->gr_name
: "",
210 (verbose
) ? ")" : "", multi
);
211 safe_strncat(line
, tmp
, sizeof(line
));
213 #ifdef HAVE_SETGROUPENT
219 while ((grp
= getgrent()) != NULL
) {
220 char **members
= grp
->gr_mem
;
223 if (strcmp(*members
++, pw
->pw_name
) == 0) {
224 if (grp
->gr_gid
== primary
)
227 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s%c", (long) grp
->gr_gid
,
228 (verbose
) ? "(" : "", (verbose
) ? grp
->gr_name
: "",
229 (verbose
) ? ")" : "", multi
);
230 safe_strncat(line
, tmp
, sizeof(line
));
236 * Trim the remaining multi-string deliminator.
238 line
[strlen(line
) - 1] = '\0';
240 add_string(&strings
, line
);
243 /* This is output if the -h command line option is passed to the main program.
245 void ui_module_help()
247 printf(" Password/Group file information [-P (-%s)]:\n",
248 PASSWD_OPTION_ORDER
);
249 printf("\t-l login name\t\t");
250 printf("\t-p encrypted password\n");
251 printf("\t-u user id (uid)\t");
252 printf("\t-g group id (gid)\n");
253 printf("\t-c password change time");
254 printf("\t-e password expire time\n");
255 printf("\t-d home directory\t");
256 printf("\t-m home directory mode\n");
257 printf("\t-s login shell\n");
258 printf("\t-i gecos (any of [n]ame,[1]st,[2]nd,[3]rd or [a]ll)\n\n");
261 /* This is the equivalent to main() only without argc and argv available. */
262 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi_char
,
263 const int verbose
, char *tf
)
269 struct spwd
*spwd
= NULL
;
274 if ((spwd
= getspnam(pw
->pw_name
)) == NULL
)
275 warnx("%s", "getspnam(): unknown error");
288 add_string(&strings
, "!");
292 snprintf(tmp
, sizeof(tmp
), "%li", (long) spwd
->sp_max
);
293 add_string(&strings
, tmp
);
297 add_string(&strings
, "!");
301 snprintf(tmp
, sizeof(tmp
), "%li", (long) spwd
->sp_expire
);
302 add_string(&strings
, tmp
);
306 snprintf(tmp
, sizeof(tmp
), "%li", (long) pw
->pw_change
);
307 add_string(&strings
, tmp
);
310 snprintf(tmp
, sizeof(tmp
), "%li", (long) pw
->pw_expire
);
311 add_string(&strings
, tmp
);
315 add_string(&strings
, pw
->pw_name
);
318 add_string(&strings
, (pw
->pw_dir
&& pw
->pw_dir
[0]) ? pw
->pw_dir
: "-");
321 add_string(&strings
, (pw
->pw_shell
&& pw
->pw_shell
[0]) ? pw
->pw_shell
: "-");
326 add_string(&strings
, (pw
->pw_passwd
327 && pw
->pw_passwd
[0]) ? pw
->pw_passwd
: "-");
329 add_string(&strings
, (spwd
->sp_pwdp
330 && spwd
->sp_pwdp
[0]) ? spwd
->sp_pwdp
: "-");
332 add_string(&strings
, (pw
->pw_passwd
333 && pw
->pw_passwd
[0]) ? pw
->pw_passwd
: "-");
337 sprintf(tmp
, "%li", (long) pw
->pw_uid
);
338 add_string(&strings
, tmp
);
341 groups(pw
, multi_char
, verbose
);
344 if (stat(pw
->pw_dir
, &st
) == -1) {
345 add_string(&strings
, "!");
349 sprintf(tmp
, "%.4o", (unsigned) st
.st_mode
& ALLPERMS
);
350 add_string(&strings
, tmp
);
353 gecos_strings(pw
->pw_gecos
);
366 char *ui_module_options_init(char **defaults
)
369 return PASSWD_OPTION_STRING
;
372 /* Check module option validity. */
373 int ui_module_options(int argc
, char **argv
)
378 while ((opt
= getopt(argc
, argv
, PASSWD_OPTION_STRING
)) != -1) {
381 if (parse_gecos_options(optarg
))
384 gecos_options
= optarg
;
387 strncpy(options
, PASSWD_OPTION_ORDER
, sizeof(options
));
401 warnx("passwd: invalid option -- %c", optopt
);