From: Cody Maloney <cmaloney@theoreticalchaos.com>
To: util-linux@vger.kernel.org
Cc: mitr@redhat.com, Cody Maloney <cmaloney@theoreticalchaos.com>
Subject: [PATCH v2 3/4] chsh: Add libuser support
Date: Sun, 13 Jan 2013 02:12:24 -0700 [thread overview]
Message-ID: <1358068345-773-5-git-send-email-cmaloney@theoreticalchaos.com> (raw)
In-Reply-To: <1358068345-773-1-git-send-email-cmaloney@theoreticalchaos.com>
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=UTF-8, Size: 6263 bytes --]
This is based directly on lchsh which is a part of libuser. libuser.{c,h}
exist because exactly the same code is needed for both chsh and chfn.
Signed-off-by: Cody Maloney <cmaloney@theoreticalchaos.com>
---
login-utils/Makemodule.am | 3 ++
login-utils/chsh.c | 24 ++++++++++++--
login-utils/libuser.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++
login-utils/libuser.h | 14 ++++++++
4 files changed, 123 insertions(+), 2 deletions(-)
create mode 100644 login-utils/libuser.c
create mode 100644 login-utils/libuser.h
diff --git a/login-utils/Makemodule.am b/login-utils/Makemodule.am
index ee85329..0c57118 100644
--- a/login-utils/Makemodule.am
+++ b/login-utils/Makemodule.am
@@ -73,6 +73,9 @@ chfn_chsh_ldadd = libcommon.la -lpam -lpam_misc
if HAVE_USER
chfn_chsh_ldflags += $(LIBUSER_LIBS)
chfn_chsh_cflags += $(LIBUSER_CFLAGS)
+chfn_chsh_sources+= \
+ login-utils/libuser.c \
+ login-utils/libuser.h
endif
if HAVE_SELINUX
diff --git a/login-utils/chsh.c b/login-utils/chsh.c
index 4113df5..fa2fa84 100644
--- a/login-utils/chsh.c
+++ b/login-utils/chsh.c
@@ -1,6 +1,7 @@
/*
* chsh.c -- change your login shell
* (c) 1994 by salvatore valente <svalente@athena.mit.edu>
+ * (c) 2012 by Cody Maloney <cmaloney@theoreticalchaos.com>
*
* this program is free software. you can redistribute it and
* modify it under the terms of the gnu general public license.
@@ -17,7 +18,7 @@
* suggestion from Zefram. Disallowing users with shells not in /etc/shells
* from changing their shell.
*
- * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
+ * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
* - added Native Language Support
*/
@@ -32,7 +33,6 @@
#include <sys/types.h>
#include <unistd.h>
-#include "auth.h"
#include "c.h"
#include "env.h"
#include "closestream.h"
@@ -48,6 +48,14 @@
# include "selinux_utils.h"
#endif
+
+#ifdef HAVE_LIBUSER
+# include <libuser/user.h>
+# include "libuser.h"
+#else
+# include "auth.h"
+#endif
+
struct sinfo {
char *username;
char *shell;
@@ -131,7 +139,12 @@ int main(int argc, char **argv)
oldshell = _PATH_BSHELL; /* default */
/* reality check */
+#ifdef HAVE_LIBUSER
+ /* If we're setuid and not really root, disallow the password change. */
+ if (geteuid() != getuid() && uid != pw->pw_uid) {
+#else
if (uid != 0 && uid != pw->pw_uid) {
+#endif
errno = EACCES;
err(EXIT_FAILURE,
_("running UID doesn't match UID of user we're "
@@ -147,9 +160,11 @@ int main(int argc, char **argv)
printf(_("Changing shell for %s.\n"), pw->pw_name);
+#ifndef HAVE_LIBUSER
if(!auth_pam("chsh", uid, pw->pw_name)) {
return EXIT_FAILURE;
}
+#endif
if (!shell) {
shell = prompt(_("New shell"), oldshell);
@@ -162,10 +177,15 @@ int main(int argc, char **argv)
if (strcmp(oldshell, shell) == 0)
errx(EXIT_SUCCESS, _("Shell not changed."));
+
+#ifdef HAVE_LIBUSER
+ set_value_libuser("chsh", pw->pw_name, uid, LU_LOGINSHELL, shell);
+#else
pw->pw_shell = shell;
if (setpwnam(pw) < 0)
err(EXIT_FAILURE, _("setpwnam failed\n"
"Shell *NOT* changed. Try again later."));
+#endif
printf(_("Shell changed.\n"));
return EXIT_SUCCESS;
diff --git a/login-utils/libuser.c b/login-utils/libuser.c
new file mode 100644
index 0000000..9a3e3b1
--- /dev/null
+++ b/login-utils/libuser.c
@@ -0,0 +1,84 @@
+/*
+ * libuser.c -- Utilize libuser to set a user attribute
+ * (c) 2012 by Cody Maloney <cmaloney@theoreticalchaos.com>
+ *
+ * this program is free software. you can redistribute it and
+ * modify it under the terms of the gnu general public license.
+ * there is no warranty.
+ *
+ */
+
+#include "libuser.h"
+
+#include <grp.h>
+#include <libuser/user.h>
+#include <unistd.h>
+
+#include "auth.h"
+#include "c.h"
+#include "nls.h"
+
+static int auth_lu(const char *service_name, struct lu_context *ctx, uid_t uid,
+ const char *username);
+
+static int auth_lu(const char *service_name, struct lu_context *ctx, uid_t uid,
+ const char *username) {
+ if(!lu_uses_elevated_privileges(ctx)) {
+ /* Drop privileges */
+ if (setegid(getgid()) == -1) {
+ errx(EXIT_FAILURE, _("Couldn't drop group privileges"));
+ return FALSE;
+ }
+ if (seteuid(getuid()) == -1) {
+ errx(EXIT_FAILURE, _("Couldn't drop group privileges"));
+ return FALSE;
+ }
+ if (initgroups(username, 0)) {
+ errx(EXIT_FAILURE, _("Couldn't drop group privileges"));
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+ return auth_pam(service_name, uid, username);
+}
+
+int set_value_libuser(const char *service_name, const char *username, uid_t uid,
+ const char *attr, const char *val) {
+ struct lu_context *ctx;
+ struct lu_error *error = NULL;
+ struct lu_ent *ent;
+
+ ctx = lu_start(username, lu_user, NULL, NULL, lu_prompt_console_quiet,
+ NULL, &error);
+ if (ctx == NULL) {
+ err(EXIT_FAILURE, _("Error initializing %s: %s.\n"), PACKAGE,
+ lu_strerror(error));
+ return FALSE;
+ }
+
+ if(!auth_lu(service_name, ctx, uid, username)) {
+ errno = EACCES;
+ err(EXIT_FAILURE, _("Permisison denied for changing user attribute"));
+ return FALSE;
+ }
+
+ /* Look up the user's record. */
+ ent = lu_ent_new();
+ if (lu_user_lookup_name(ctx, username, ent, &error) == FALSE) {
+ lu_end(ctx);
+ err(EXIT_FAILURE, _("user \"%s\" does not exist."), username);
+ return FALSE;
+ }
+
+ lu_ent_set_string(ent, attr, val);
+ if (!lu_user_modify(ctx, ent, &error)) {
+ lu_ent_free(ent);
+ lu_end(ctx);
+ err(EXIT_FAILURE, _("Shell not changed: %s\n"), lu_strerror(error));
+ return FALSE;
+ }
+ lu_ent_free(ent);
+ lu_end(ctx);
+ return TRUE;
+}
diff --git a/login-utils/libuser.h b/login-utils/libuser.h
new file mode 100644
index 0000000..7454b99
--- /dev/null
+++ b/login-utils/libuser.h
@@ -0,0 +1,14 @@
+/*
+ * libuser.h -- Utilize libuser to set a user attribute
+ * (c) 2012 by Cody Maloney <cmaloney@theoreticalchaos.com>
+ *
+ * this program is free software. you can redistribute it and
+ * modify it under the terms of the gnu general public license.
+ * there is no warranty.
+ *
+ */
+
+#include <sys/types.h>
+
+extern int set_value_libuser(const char *service_name, const char *username,
+ uid_t uid, const char *attr, const char *val);
--
1.8.1
next prev parent reply other threads:[~2013-01-13 9:12 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-13 9:12 [PATCH v2 0/4] Add support for using libuser to chsh and chfn Cody Maloney
2013-01-03 3:21 ` [PATCH 0/3] Add support for using libuser to chsh Cody Maloney
2013-01-03 3:21 ` [PATCH 1/3] chsh-chfn: Add flag for enabling/disabling libuser support Cody Maloney
2013-01-03 3:22 ` [PATCH 2/3] chsh: Move pam auth to its own function Cody Maloney
2013-01-03 3:22 ` [PATCH 3/3] chsh: Add libuser support Cody Maloney
2013-01-03 16:34 ` Miloslav Trmac
2013-01-07 11:51 ` Karel Zak
2013-01-08 6:15 ` Cody Maloney
2013-01-08 6:17 ` Cody Maloney
2013-01-08 6:18 ` Cody Maloney
2013-01-13 9:12 ` [PATCH 2/3] chsh: Move pam auth to its own function Cody Maloney
2013-01-13 9:12 ` [PATCH v2 1/4] chsh-chfn: Add flag for enabling/disabling libuser support Cody Maloney
2013-01-13 9:12 ` [PATCH v2 2/4] chsh-chfn: Move pam auth to its own function, factoring out common code Cody Maloney
2013-01-13 9:12 ` Cody Maloney [this message]
2013-01-14 18:16 ` [PATCH v2 3/4] chsh: Add libuser support Miloslav Trmac
2013-01-17 0:52 ` Cody Maloney
2013-01-28 22:01 ` Miloslav Trmac
2013-01-13 9:12 ` [PATCH v2 4/4] chfn: " Cody Maloney
[not found] ` <50F2FBBE.7000305@gmail.com>
2013-01-13 19:31 ` [PATCH v2 0/4] Add support for using libuser to chsh and chfn Cody Maloney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1358068345-773-5-git-send-email-cmaloney@theoreticalchaos.com \
--to=cmaloney@theoreticalchaos.com \
--cc=mitr@redhat.com \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox