Blame SOURCES/sudo-1.8.6p7-ipahostname.patch

523624
diff -up sudo-1.8.6p7/configure.in.ipahostname sudo-1.8.6p7/configure.in
523624
--- sudo-1.8.6p7/configure.in.ipahostname	2014-09-29 11:14:38.393846226 +0200
523624
+++ sudo-1.8.6p7/configure.in	2014-09-29 11:14:38.428845807 +0200
523624
@@ -309,7 +309,7 @@ dnl Handle SSSD support.
523624
 dnl
523624
 AC_ARG_WITH(sssd, [AS_HELP_STRING([--with-sssd], [enable SSSD support])],
523624
 [case $with_sssd in
523624
-    yes)	SUDOERS_OBJS="${SUDOERS_OBJS} sssd.lo"
523624
+    yes)	SUDOERS_OBJS="${SUDOERS_OBJS} sssd.lo ipa_hostname.lo"
523624
 		AC_DEFINE(HAVE_SSSD)
523624
 		;;
523624
     no)		;;
523624
diff -up sudo-1.8.6p7/plugins/sudoers/ipa_hostname.c.ipahostname sudo-1.8.6p7/plugins/sudoers/ipa_hostname.c
523624
--- sudo-1.8.6p7/plugins/sudoers/ipa_hostname.c.ipahostname	2014-09-29 11:14:38.429845795 +0200
523624
+++ sudo-1.8.6p7/plugins/sudoers/ipa_hostname.c	2014-09-29 11:14:38.429845795 +0200
523624
@@ -0,0 +1,88 @@
523624
+/*
523624
+ * Copyright 2013 Red Hat Inc., Durham, North Carolina.
523624
+ * All Rights Reserved.
523624
+ *
523624
+ * This library is free software; you can redistribute it and/or
523624
+ * modify it under the terms of the GNU Lesser General Public
523624
+ * License as published by the Free Software Foundation; either
523624
+ * version 2.1 of the License, or (at your option) any later version.
523624
+ *
523624
+ * This library is distributed in the hope that it will be useful,
523624
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
523624
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
523624
+ * Lesser General Public License for more details.
523624
+ *
523624
+ * You should have received a copy of the GNU Lesser General Public
523624
+ * License along with this library; if not, write to the Free Software
523624
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
523624
+ *
523624
+ * Authors:
523624
+ *      Daniel Kopecek <dkopecek@redhat.com>
523624
+ */
523624
+#define _GNU_SOURCE
523624
+#include <stdio.h>
523624
+#include <stdlib.h>
523624
+#include <resolv.h>
523624
+#include <string.h>
523624
+#include <ctype.h>
523624
+
523624
+static const char *sssd_conf_path = "/etc/sssd/sssd.conf";
523624
+
523624
+char *ipa_hostname(void)
523624
+{
523624
+	static char hname[MAXHOSTNAMELEN+1];
523624
+	size_t hname_len = 0;
523624
+	char *line = NULL;
523624
+	ssize_t line_len = 0;
523624
+	size_t line_buflen = 0;
523624
+	FILE *fp;
523624
+
523624
+	if ((fp = fopen(sssd_conf_path, "r")) == NULL)
523624
+		return NULL;
523624
+	while ((line_len = getline(&line, &line_buflen, fp)) > 0) {
523624
+		char *keyword_loc;
523624
+		if ((keyword_loc = strstr(line, "ipa_hostname")) != NULL) {
523624
+			size_t i;
523624
+			char *value_loc;
523624
+			size_t value_len;
523624
+
523624
+			value_loc = keyword_loc + strlen("ipa_hostname") + 1;
523624
+			value_len = line_len - (size_t)(value_loc - line);
523624
+
523624
+			/* Skip spaces and the assignment operator */
523624
+			for (i = 0; i < value_len; ++i) {
523624
+				if (isspace(value_loc[i]) || value_loc[i] == '=') {
523624
+					continue;
523624
+				} else {
523624
+					break;
523624
+				}
523624
+			}
523624
+
523624
+			value_loc += i;
523624
+			value_len -= i;
523624
+
523624
+			if (value_len <= MAXHOSTNAMELEN) {
523624
+				memcpy(hname, value_loc, value_len * sizeof(char));
523624
+				free(line);
523624
+				fclose(fp);
523624
+				hname_len = value_len;
523624
+				hname[hname_len] = '\0';
523624
+				/* Remove spaces from the end of the string */
523624
+				for (i = hname_len - 1; i > 0; --i) {
523624
+					if (isspace(hname[i])) {
523624
+						hname[i] = '\0';
523624
+						--hname_len;
523624
+					} else {
523624
+						break;
523624
+					}
523624
+				}
523624
+				return hname;
523624
+			}
523624
+		}
523624
+		free(line);
523624
+		line = NULL;
523624
+	}
523624
+
523624
+	fclose(fp);
523624
+	return NULL;
523624
+}
523624
diff -up sudo-1.8.6p7/plugins/sudoers/ipa_hostname.h.ipahostname sudo-1.8.6p7/plugins/sudoers/ipa_hostname.h
523624
--- sudo-1.8.6p7/plugins/sudoers/ipa_hostname.h.ipahostname	2014-09-29 11:14:38.429845795 +0200
523624
+++ sudo-1.8.6p7/plugins/sudoers/ipa_hostname.h	2014-09-29 11:14:38.429845795 +0200
523624
@@ -0,0 +1,27 @@
523624
+/*
523624
+ * Copyright 2013 Red Hat Inc., Durham, North Carolina.
523624
+ * All Rights Reserved.
523624
+ *
523624
+ * This library is free software; you can redistribute it and/or
523624
+ * modify it under the terms of the GNU Lesser General Public
523624
+ * License as published by the Free Software Foundation; either
523624
+ * version 2.1 of the License, or (at your option) any later version.
523624
+ *
523624
+ * This library is distributed in the hope that it will be useful,
523624
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
523624
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
523624
+ * Lesser General Public License for more details.
523624
+ *
523624
+ * You should have received a copy of the GNU Lesser General Public
523624
+ * License along with this library; if not, write to the Free Software
523624
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
523624
+ *
523624
+ * Authors:
523624
+ *      Daniel Kopecek <dkopecek@redhat.com>
523624
+ */
523624
+#ifndef _IPA_HOSTNAME_H_
523624
+#define _IPA_HOSTNAME_H_
523624
+
523624
+char *ipa_hostname(void);
523624
+
523624
+#endif /* _IPA_HOSTNAME_H_ */
523624
diff -up sudo-1.8.6p7/plugins/sudoers/Makefile.in.ipahostname sudo-1.8.6p7/plugins/sudoers/Makefile.in
523624
--- sudo-1.8.6p7/plugins/sudoers/Makefile.in.ipahostname	2014-09-29 11:14:38.429845795 +0200
523624
+++ sudo-1.8.6p7/plugins/sudoers/Makefile.in	2014-09-29 11:16:54.923210160 +0200
523624
@@ -728,6 +728,9 @@ sia.lo: $(authdir)/sia.c $(top_builddir)
523624
         $(devdir)/def_data.h $(srcdir)/logging.h $(srcdir)/sudo_nss.h \
523624
         $(incdir)/sudo_plugin.h $(incdir)/sudo_debug.h $(incdir)/gettext.h
523624
 	$(LIBTOOL) --mode=compile $(CC) -c $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(authdir)/sia.c
523624
+ipa_hostname.lo: $(srcdir)/ipa_hostname.c $(srcdir)/ipa_hostname.h
523624
+	$(LIBTOOL) --mode=compile $(CC) -c $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(DEFS) $(srcdir)/ipa_hostname.c
523624
+
523624
 sssd.lo: $(srcdir)/sssd.c $(top_builddir)/config.h \
523624
          $(top_srcdir)/compat/dlfcn.h $(srcdir)/sudoers.h \
523624
          $(top_srcdir)/compat/stdbool.h $(top_builddir)/pathnames.h \
523624
diff -up sudo-1.8.6p7/plugins/sudoers/sssd.c.ipahostname sudo-1.8.6p7/plugins/sudoers/sssd.c
523624
--- sudo-1.8.6p7/plugins/sudoers/sssd.c.ipahostname	2014-09-29 11:14:38.424845855 +0200
523624
+++ sudo-1.8.6p7/plugins/sudoers/sssd.c	2014-09-29 11:14:38.429845795 +0200
523624
@@ -60,6 +60,7 @@
523624
 #include "parse.h"
523624
 #include "lbuf.h"
523624
 #include "sudo_debug.h"
523624
+#include "ipa_hostname.h"
523624
 
523624
 /* SSSD <--> SUDO interface - do not change */
523624
 struct sss_sudo_attr {
523624
@@ -549,6 +550,24 @@ sudo_sss_check_runas(struct sudo_sss_han
523624
     debug_return_bool(ret);
523624
 }
523624
 
523624
+static bool sudo_sss_ipa_hostname_matches(const char *hostname_val)
523624
+{
523624
+	bool ret = false;
523624
+	char *ipa_hostname_val;
523624
+	debug_decl(sudo_sss_ipa_hostname_matches, SUDO_DEBUG_SSSD)
523624
+
523624
+	if ((ipa_hostname_val = ipa_hostname()) != NULL) {
523624
+		ret = hostname_matches(ipa_hostname_val, ipa_hostname_val, hostname_val) || \
523624
+		      netgr_matches(hostname_val, ipa_hostname_val, ipa_hostname_val, NULL);
523624
+	}
523624
+
523624
+	sudo_debug_printf(SUDO_DEBUG_TRACE, "IPA hostname (%s) matches %s => %s",
523624
+	                  ipa_hostname_val ? ipa_hostname_val : "<none>", hostname_val,
523624
+	                  ret ? "true" : "false");
523624
+
523624
+	debug_return_bool(ret);
523624
+}
523624
+
523624
 static bool
523624
 sudo_sss_check_host(struct sudo_sss_handle *handle, struct sss_sudo_rule *rule)
523624
 {
523624
@@ -580,6 +599,7 @@ sudo_sss_check_host(struct sudo_sss_hand
523624
 
523624
 	/* match any or address or netgroup or hostname */
523624
 	if (!strcmp(val, "ALL") || addr_matches(val) ||
523624
+	    sudo_sss_ipa_hostname_matches(val) ||
523624
 	    netgr_matches(val, user_host, user_shost, NULL) ||
523624
 	    hostname_matches(user_shost, user_host, val))
523624
 	    ret = true;