6c6be87d658d2aff9d0fbc11878d5b82ced5c67f
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>
34 #ifdef _POSIX2_LINE_MAX
35 #define LINE_MAX _POSIX2_LINE_MAX
59 #include "../strsep.c"
71 #define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
74 /* This is the order of options when the 'P' option is specified. It selects
75 * all available options. */
76 #define PASSWD_OPTION_ORDER "lpugicedsm"
77 #define PASSWD_OPTION_STRING "Plpugcedsmi:"
80 static char **strings
;
81 static char options
[11]; /* NULL terminated. */
82 static char *gecos_options
;
84 void add_string(char ***, const char *);
85 char *stamp(time_t, const char *);
86 char *safe_strncat(char *, const char *, size_t);
88 void ui_module_init(int *chainable
)
91 * Keep the password file open if possible (*BSD).
93 #ifdef HAVE_SETPASSENT
103 void ui_module_exit()
114 /* See if the gecos options are valid. */
115 static int parse_gecos_options(const char *args
)
119 for (i
= 0; i
< strlen(args
); i
++) {
135 /* Break up the gecos string into sections and add the sections to the output
136 * string array if needed. */
137 static void gecos_strings(char *str
)
141 const char *name
, *first
, *second
, *third
;
143 name
= first
= second
= third
= "-";
145 while ((buf
= strsep(&str
, ",")) != NULL
) {
167 for (i
= 0; i
< strlen(gecos_options
); i
++) {
168 switch (gecos_options
[i
]) {
170 add_string(&strings
, name
);
173 add_string(&strings
, first
);
176 add_string(&strings
, second
);
179 add_string(&strings
, third
);
182 add_string(&strings
, name
);
183 add_string(&strings
, first
);
184 add_string(&strings
, second
);
185 add_string(&strings
, third
);
193 /* Get all groups that a user is a member of. The primary group will be the
195 static void groups(const struct passwd
*pw
, const int multi
,
205 if ((grp
= getgrgid(pw
->pw_gid
)) == NULL
) {
206 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s", (long) pw
->pw_gid
,
207 (verbose
) ? "(" : "", (verbose
) ? "!" : "",
208 (verbose
) ? ")" : "");
209 add_string(&strings
, tmp
);
213 primary
= grp
->gr_gid
;
214 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s%c", (long) pw
->pw_gid
,
215 (verbose
) ? "(" : "", (verbose
) ? grp
->gr_name
: "",
216 (verbose
) ? ")" : "", multi
);
217 safe_strncat(line
, tmp
, sizeof(line
));
219 #ifdef HAVE_SETGROUPENT
225 while ((grp
= getgrent()) != NULL
) {
226 char **members
= grp
->gr_mem
;
229 if (strcmp(*members
++, pw
->pw_name
) == 0) {
230 if (grp
->gr_gid
== primary
)
233 snprintf(tmp
, sizeof(tmp
), "%li%s%s%s%c", (long) grp
->gr_gid
,
234 (verbose
) ? "(" : "", (verbose
) ? grp
->gr_name
: "",
235 (verbose
) ? ")" : "", multi
);
236 safe_strncat(line
, tmp
, sizeof(line
));
242 * Trim the remaining multi-string deliminator.
244 line
[strlen(line
) - 1] = '\0';
246 add_string(&strings
, line
);
249 /* This is output if the -h command line option is passed to the main program.
251 void ui_module_help()
253 printf(" Password/Group file information [-P (-%s)]:\n",
254 PASSWD_OPTION_ORDER
);
255 printf("\t-l login name\t\t");
256 printf("\t-p encrypted password\n");
257 printf("\t-u user id (uid)\t");
258 printf("\t-g group id (gid)\n");
259 printf("\t-c password change time");
260 printf("\t-e password expire time\n");
261 printf("\t-d home directory\t");
262 printf("\t-m home directory mode\n");
263 printf("\t-s login shell\n");
264 printf("\t-i gecos (any of [n]ame,[1]st,[2]nd,[3]rd or [a]ll)\n\n");
267 /* This is the equivalent to main() only without argc and argv available. */
268 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi_char
,
269 const int verbose
, char *tf
)
275 struct spwd
*spwd
= NULL
;
280 if ((spwd
= getspnam(pw
->pw_name
)) == NULL
)
281 warnx("%s", "getspnam(): unknown error");
294 add_string(&strings
, "!");
298 snprintf(tmp
, sizeof(tmp
), "%li", (long) spwd
->sp_max
);
299 add_string(&strings
, tmp
);
303 add_string(&strings
, "!");
307 snprintf(tmp
, sizeof(tmp
), "%li", (long) spwd
->sp_expire
);
308 add_string(&strings
, tmp
);
312 snprintf(tmp
, sizeof(tmp
), "%li", (long) pw
->pw_change
);
313 add_string(&strings
, tmp
);
316 snprintf(tmp
, sizeof(tmp
), "%li", (long) pw
->pw_expire
);
317 add_string(&strings
, tmp
);
321 add_string(&strings
, pw
->pw_name
);
324 add_string(&strings
, (pw
->pw_dir
&& pw
->pw_dir
[0]) ? pw
->pw_dir
: "-");
327 add_string(&strings
, (pw
->pw_shell
&& pw
->pw_shell
[0]) ? pw
->pw_shell
: "-");
332 add_string(&strings
, (pw
->pw_passwd
333 && pw
->pw_passwd
[0]) ? pw
->pw_passwd
: "-");
335 add_string(&strings
, (spwd
->sp_pwdp
336 && spwd
->sp_pwdp
[0]) ? spwd
->sp_pwdp
: "-");
338 add_string(&strings
, (pw
->pw_passwd
339 && pw
->pw_passwd
[0]) ? pw
->pw_passwd
: "-");
343 sprintf(tmp
, "%li", (long) pw
->pw_uid
);
344 add_string(&strings
, tmp
);
347 groups(pw
, multi_char
, verbose
);
350 if (stat(pw
->pw_dir
, &st
) == -1) {
351 add_string(&strings
, "!");
355 sprintf(tmp
, "%.4o", (unsigned) st
.st_mode
& ALLPERMS
);
356 add_string(&strings
, tmp
);
359 gecos_strings(pw
->pw_gecos
);
372 char *ui_module_options_init(char **defaults
)
375 return PASSWD_OPTION_STRING
;
378 /* Check module option validity. */
379 int ui_module_options(int argc
, char **argv
)
384 while ((opt
= getopt(argc
, argv
, PASSWD_OPTION_STRING
)) != -1) {
387 if (parse_gecos_options(optarg
))
390 gecos_options
= optarg
;
393 strncpy(options
, PASSWD_OPTION_ORDER
, sizeof(options
));
407 warnx("passwd: invalid option -- %c", optopt
);