7c6029dd614ee4315bc45ef198d76610116b0c28
2 Copyright (C) 2001-2011 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 02110-1301 USA
26 #include <sys/types.h>
52 #include "../strsep.c"
64 #define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
67 /* This is the order of options when the 'P' option is specified. It selects
68 * all available options. */
69 #define PASSWD_OPTION_ORDER "lpugicedsm"
70 #define PASSWD_OPTION_STRING "Plpugcedsmi:"
73 static char **strings
;
74 static char options
[11]; /* NULL terminated. */
75 static char *gecos_options
;
77 void add_string(char ***, const char *);
78 char *stamp(time_t, const char *);
79 char *safe_strncat(char *, const char *, size_t);
81 void ui_module_init(int *chainable
)
84 * Keep the password file open if possible (*BSD).
86 #ifdef HAVE_SETPASSENT
107 /* See if the gecos options are valid. */
108 static int parse_gecos_options(const char *args
)
112 for (i
= 0; i
< strlen(args
); i
++) {
128 /* Break up the gecos string into sections and add the sections to the output
129 * string array if needed. */
130 static void gecos_strings(char *str
)
134 const char *name
, *first
, *second
, *third
;
136 name
= first
= second
= third
= "-";
138 while ((buf
= strsep(&str
, ",")) != NULL
) {
160 for (i
= 0; i
< strlen(gecos_options
); i
++) {
161 switch (gecos_options
[i
]) {
163 add_string(&strings
, name
);
166 add_string(&strings
, first
);
169 add_string(&strings
, second
);
172 add_string(&strings
, third
);
175 add_string(&strings
, name
);
176 add_string(&strings
, first
);
177 add_string(&strings
, second
);
178 add_string(&strings
, third
);
186 /* Get all groups that a user is a member of. The primary group will be the
188 static void groups(const struct passwd
*pw
, const int multi
,
198 if ((grp
= getgrgid(pw
->pw_gid
)) == NULL
) {
199 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s", (long) pw
->pw_gid
,
200 (verbose
) ? "(" : "", (verbose
) ? "!" : "",
201 (verbose
) ? ")" : "");
202 add_string(&strings
, tmp
);
206 primary
= grp
->gr_gid
;
207 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s%c", (long) pw
->pw_gid
,
208 (verbose
) ? "(" : "", (verbose
) ? grp
->gr_name
: "",
209 (verbose
) ? ")" : "", multi
);
210 safe_strncat(line
, tmp
, sizeof(line
));
212 #ifdef HAVE_SETGROUPENT
218 while ((grp
= getgrent()) != NULL
) {
219 char **members
= grp
->gr_mem
;
222 if (strcmp(*members
++, pw
->pw_name
) == 0) {
223 if (grp
->gr_gid
== primary
)
226 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s%c", (long) grp
->gr_gid
,
227 (verbose
) ? "(" : "", (verbose
) ? grp
->gr_name
: "",
228 (verbose
) ? ")" : "", multi
);
229 safe_strncat(line
, tmp
, sizeof(line
));
235 * Trim the remaining multi-string deliminator.
237 line
[strlen(line
) - 1] = '\0';
239 add_string(&strings
, line
);
242 /* This is output if the -h command line option is passed to the main program.
244 void ui_module_help()
246 printf(" Password/Group file information [-P (-%s)]:\n",
247 PASSWD_OPTION_ORDER
);
248 printf("\t-l login name\t\t");
249 printf("\t-p encrypted password\n");
250 printf("\t-u user id (uid)\t");
251 printf("\t-g group id (gid)\n");
252 printf("\t-c password change time");
253 printf("\t-e password expire time\n");
254 printf("\t-d home directory\t");
255 printf("\t-m home directory mode\n");
256 printf("\t-s login shell\n");
257 printf("\t-i gecos (any of [n]ame,[1]st,[2]nd,[3]rd or [a]ll)\n\n");
260 /* This is the equivalent to main() only without argc and argv available. */
261 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi_char
,
262 const int verbose
, char *tf
)
268 struct spwd
*spwd
= NULL
;
273 if ((spwd
= getspnam(pw
->pw_name
)) == NULL
)
274 warnx("%s", "getspnam(): unknown error");
287 add_string(&strings
, "!");
291 snprintf(tmp
, sizeof(tmp
), "%li", (long) spwd
->sp_max
);
292 add_string(&strings
, tmp
);
296 add_string(&strings
, "!");
300 snprintf(tmp
, sizeof(tmp
), "%li", (long) spwd
->sp_expire
);
301 add_string(&strings
, tmp
);
305 snprintf(tmp
, sizeof(tmp
), "%li", (long) pw
->pw_change
);
306 add_string(&strings
, tmp
);
309 snprintf(tmp
, sizeof(tmp
), "%li", (long) pw
->pw_expire
);
310 add_string(&strings
, tmp
);
314 add_string(&strings
, pw
->pw_name
);
317 add_string(&strings
, (pw
->pw_dir
&& pw
->pw_dir
[0]) ? pw
->pw_dir
: "-");
320 add_string(&strings
, (pw
->pw_shell
&& pw
->pw_shell
[0]) ? pw
->pw_shell
: "-");
325 add_string(&strings
, (pw
->pw_passwd
326 && pw
->pw_passwd
[0]) ? pw
->pw_passwd
: "-");
328 add_string(&strings
, (spwd
->sp_pwdp
329 && spwd
->sp_pwdp
[0]) ? spwd
->sp_pwdp
: "-");
331 add_string(&strings
, (pw
->pw_passwd
332 && pw
->pw_passwd
[0]) ? pw
->pw_passwd
: "-");
336 sprintf(tmp
, "%li", (long) pw
->pw_uid
);
337 add_string(&strings
, tmp
);
340 groups(pw
, multi_char
, verbose
);
343 if (stat(pw
->pw_dir
, &st
) == -1) {
344 add_string(&strings
, "!");
348 sprintf(tmp
, "%.4o", (unsigned) st
.st_mode
& ALLPERMS
);
349 add_string(&strings
, tmp
);
352 gecos_strings(pw
->pw_gecos
);
365 char *ui_module_options_init(char **defaults
)
368 return PASSWD_OPTION_STRING
;
371 /* Check module option validity. */
372 int ui_module_options(int argc
, char **argv
)
377 while ((opt
= getopt(argc
, argv
, PASSWD_OPTION_STRING
)) != -1) {
380 if (parse_gecos_options(optarg
))
383 gecos_options
= optarg
;
386 strncpy(options
, PASSWD_OPTION_ORDER
, sizeof(options
));
400 warnx("passwd: invalid option -- %c", optopt
);