]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/libuser.c
2 * libuser.c -- Utilize libuser to set a user attribute
3 * (c) 2012 by Cody Maloney <cmaloney@theoreticalchaos.com>
5 * this program is free software. you can redistribute it and
6 * modify it under the terms of the gnu general public license.
7 * there is no warranty.
14 #include <libuser/user.h>
21 static int auth_lu(const char *service_name
, struct lu_context
*ctx
, uid_t uid
,
22 const char *username
);
24 static int auth_lu(const char *service_name
, struct lu_context
*ctx
, uid_t uid
,
25 const char *username
) {
26 if (!lu_uses_elevated_privileges(ctx
)) {
28 if (setegid(getgid()) == -1)
29 err(EXIT_FAILURE
, _("Couldn't drop group privileges"));
30 if (seteuid(getuid()) == -1)
31 err(EXIT_FAILURE
, _("Couldn't drop group privileges"));
35 return auth_pam(service_name
, uid
, username
);
38 int set_value_libuser(const char *service_name
, const char *username
, uid_t uid
,
39 const char *attr
, const char *val
) {
40 struct lu_context
*ctx
;
41 struct lu_error
*error
= NULL
;
44 ctx
= lu_start(username
, lu_user
, NULL
, NULL
, lu_prompt_console_quiet
,
47 errx(EXIT_FAILURE
, _("libuser initialization failed: %s."),
50 if (!auth_lu(service_name
, ctx
, uid
, username
)) {
52 err(EXIT_FAILURE
, _("changing user attribute failed"));
55 /* Look up the user's record. */
57 if (lu_user_lookup_name(ctx
, username
, ent
, &error
) == FALSE
) {
59 errx(EXIT_FAILURE
, _("user \"%s\" does not exist."), username
);
62 lu_ent_set_string(ent
, attr
, val
);
63 if (!lu_user_modify(ctx
, ent
, &error
)) {
66 errx(EXIT_FAILURE
, _("user attribute not changed: %s"), lu_strerror(error
));