6729ff
From 9346945c4b57ffc937f7800202e5c42504750d3d Mon Sep 17 00:00:00 2001
6729ff
From: Andreas Schneider <asn@samba.org>
6729ff
Date: Mon, 18 Nov 2013 14:58:04 +0100
6729ff
Subject: [PATCH 1/2] s3-lib: Add grpname to talloc_sub_specified().
6729ff
6729ff
BUG: https://bugzilla.samba.org/show_bug.cgi?id=2191
6729ff
6729ff
Signed-off-by: Andreas Schneider <asn@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
(cherry picked from commit 6366ebb79bb72d9dcb12f8fe8d6e35611fcff150)
6729ff
---
6729ff
 source3/include/proto.h          |  1 +
6729ff
 source3/lib/substitute.c         | 31 +++++++++++++++++++++++++------
6729ff
 source3/passdb/passdb.c          |  8 ++++----
6729ff
 source3/passdb/pdb_ldap.c        | 24 +++++++++++++++++++++---
6729ff
 source3/torture/torture.c        |  2 +-
6729ff
 source3/utils/net_sam.c          |  2 ++
6729ff
 source3/winbindd/wb_fill_pwent.c |  4 ++--
6729ff
 7 files changed, 56 insertions(+), 16 deletions(-)
6729ff
6729ff
diff --git a/source3/include/proto.h b/source3/include/proto.h
6729ff
index ddf3fab..a42faf8 100644
6729ff
--- a/source3/include/proto.h
6729ff
+++ b/source3/include/proto.h
6729ff
@@ -222,6 +222,7 @@ char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
6729ff
 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
6729ff
 			const char *input_string,
6729ff
 			const char *username,
6729ff
+			const char *grpname,
6729ff
 			const char *domain,
6729ff
 			uid_t uid,
6729ff
 			gid_t gid);
6729ff
diff --git a/source3/lib/substitute.c b/source3/lib/substitute.c
6729ff
index a254bca..ca2ac79 100644
6729ff
--- a/source3/lib/substitute.c
6729ff
+++ b/source3/lib/substitute.c
6729ff
@@ -613,6 +613,7 @@ done:
6729ff
 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
6729ff
 			const char *input_string,
6729ff
 			const char *username,
6729ff
+			const char *grpname,
6729ff
 			const char *domain,
6729ff
 			uid_t uid,
6729ff
 			gid_t gid)
6729ff
@@ -648,9 +649,18 @@ char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
6729ff
 			break;
6729ff
 		case 'G' :
6729ff
 			if (gid != -1) {
6729ff
-				a_string = talloc_string_sub(
6729ff
-					tmp_ctx, a_string, "%G",
6729ff
-					gidtoname(gid));
6729ff
+				const char *name;
6729ff
+
6729ff
+				if (grpname != NULL) {
6729ff
+					name = grpname;
6729ff
+				} else {
6729ff
+					name = gidtoname(gid);
6729ff
+				}
6729ff
+
6729ff
+				a_string = talloc_string_sub(tmp_ctx,
6729ff
+							     a_string,
6729ff
+							     "%G",
6729ff
+							     name);
6729ff
 			} else {
6729ff
 				a_string = talloc_string_sub(
6729ff
 					tmp_ctx, a_string,
6729ff
@@ -659,9 +669,18 @@ char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
6729ff
 			break;
6729ff
 		case 'g' :
6729ff
 			if (gid != -1) {
6729ff
-				a_string = talloc_string_sub(
6729ff
-					tmp_ctx, a_string, "%g",
6729ff
-					gidtoname(gid));
6729ff
+				const char *name;
6729ff
+
6729ff
+				if (grpname != NULL) {
6729ff
+					name = grpname;
6729ff
+				} else {
6729ff
+					name = gidtoname(gid);
6729ff
+				}
6729ff
+
6729ff
+				a_string = talloc_string_sub(tmp_ctx,
6729ff
+							     a_string,
6729ff
+							     "%g",
6729ff
+							     name);
6729ff
 			} else {
6729ff
 				a_string = talloc_string_sub(
6729ff
 					tmp_ctx, a_string, "%g", "NO_GROUP");
6729ff
diff --git a/source3/passdb/passdb.c b/source3/passdb/passdb.c
6729ff
index 379d858..5a4620f 100644
6729ff
--- a/source3/passdb/passdb.c
6729ff
+++ b/source3/passdb/passdb.c
6729ff
@@ -228,16 +228,16 @@ static NTSTATUS samu_set_unix_internal(struct pdb_methods *methods,
6729ff
 		/* set some basic attributes */
6729ff
 
6729ff
 		pdb_set_profile_path(user, talloc_sub_specified(user, 
6729ff
-			lp_logon_path(), pwd->pw_name, domain, pwd->pw_uid, pwd->pw_gid), 
6729ff
+			lp_logon_path(), pwd->pw_name, NULL, domain, pwd->pw_uid, pwd->pw_gid),
6729ff
 			PDB_DEFAULT);		
6729ff
 		pdb_set_homedir(user, talloc_sub_specified(user, 
6729ff
-			lp_logon_home(), pwd->pw_name, domain, pwd->pw_uid, pwd->pw_gid),
6729ff
+			lp_logon_home(), pwd->pw_name, NULL, domain, pwd->pw_uid, pwd->pw_gid),
6729ff
 			PDB_DEFAULT);
6729ff
 		pdb_set_dir_drive(user, talloc_sub_specified(user, 
6729ff
-			lp_logon_drive(), pwd->pw_name, domain, pwd->pw_uid, pwd->pw_gid),
6729ff
+			lp_logon_drive(), pwd->pw_name, NULL, domain, pwd->pw_uid, pwd->pw_gid),
6729ff
 			PDB_DEFAULT);
6729ff
 		pdb_set_logon_script(user, talloc_sub_specified(user, 
6729ff
-			lp_logon_script(), pwd->pw_name, domain, pwd->pw_uid, pwd->pw_gid), 
6729ff
+			lp_logon_script(), pwd->pw_name, NULL, domain, pwd->pw_uid, pwd->pw_gid),
6729ff
 			PDB_DEFAULT);
6729ff
 	}
6729ff
 
6729ff
diff --git a/source3/passdb/pdb_ldap.c b/source3/passdb/pdb_ldap.c
6729ff
index d7db4d8..bb0d3b3 100644
6729ff
--- a/source3/passdb/pdb_ldap.c
6729ff
+++ b/source3/passdb/pdb_ldap.c
6729ff
@@ -5316,11 +5316,29 @@ static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
6729ff
 
6729ff
 		if (is_machine) {
6729ff
 			/* TODO: choose a more appropriate default for machines */
6729ff
-			homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), "SMB_workstations_home", ldap_state->domain_name, uid, gid);
6729ff
+			homedir = talloc_sub_specified(tmp_ctx,
6729ff
+						       lp_template_homedir(),
6729ff
+						       "SMB_workstations_home",
6729ff
+						       NULL,
6729ff
+						       ldap_state->domain_name,
6729ff
+						       uid,
6729ff
+						       gid);
6729ff
 			shell = talloc_strdup(tmp_ctx, "/bin/false");
6729ff
 		} else {
6729ff
-			homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
6729ff
-			shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
6729ff
+			homedir = talloc_sub_specified(tmp_ctx,
6729ff
+						       lp_template_homedir(),
6729ff
+						       name,
6729ff
+						       NULL,
6729ff
+						       ldap_state->domain_name,
6729ff
+						       uid,
6729ff
+						       gid);
6729ff
+			shell = talloc_sub_specified(tmp_ctx,
6729ff
+						     lp_template_shell(),
6729ff
+						     name,
6729ff
+						     NULL,
6729ff
+						     ldap_state->domain_name,
6729ff
+						     uid,
6729ff
+						     gid);
6729ff
 		}
6729ff
 		uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
6729ff
 		gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
6729ff
diff --git a/source3/torture/torture.c b/source3/torture/torture.c
6729ff
index ee51a4d..b7badc6 100644
6729ff
--- a/source3/torture/torture.c
6729ff
+++ b/source3/torture/torture.c
6729ff
@@ -6553,7 +6553,7 @@ static bool subst_test(const char *str, const char *user, const char *domain,
6729ff
 	char *subst;
6729ff
 	bool result = true;
6729ff
 
6729ff
-	subst = talloc_sub_specified(talloc_tos(), str, user, domain, uid, gid);
6729ff
+	subst = talloc_sub_specified(talloc_tos(), str, user, NULL, domain, uid, gid);
6729ff
 
6729ff
 	if (strcmp(subst, expected) != 0) {
6729ff
 		printf("sub_specified(%s, %s, %s, %d, %d) returned [%s], expected "
6729ff
diff --git a/source3/utils/net_sam.c b/source3/utils/net_sam.c
6729ff
index 3a752ce..b7b76e8 100644
6729ff
--- a/source3/utils/net_sam.c
6729ff
+++ b/source3/utils/net_sam.c
6729ff
@@ -1873,10 +1873,12 @@ doma_done:
6729ff
 		gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
6729ff
 		dir = talloc_sub_specified(tc, lp_template_homedir(),
6729ff
 						"Administrator",
6729ff
+						NULL,
6729ff
 						get_global_sam_name(),
6729ff
 						uid, domadmins_gid);
6729ff
 		shell = talloc_sub_specified(tc, lp_template_shell(),
6729ff
 						"Administrator",
6729ff
+						NULL,
6729ff
 						get_global_sam_name(),
6729ff
 						uid, domadmins_gid);
6729ff
 
6729ff
diff --git a/source3/winbindd/wb_fill_pwent.c b/source3/winbindd/wb_fill_pwent.c
6729ff
index 688afc6..3b711bd 100644
6729ff
--- a/source3/winbindd/wb_fill_pwent.c
6729ff
+++ b/source3/winbindd/wb_fill_pwent.c
6729ff
@@ -214,11 +214,11 @@ static bool fillup_pw_field(const char *lp_template,
6729ff
 
6729ff
 	if ((in != NULL) && (in[0] != '\0') && (lp_security() == SEC_ADS)) {
6729ff
 		templ = talloc_sub_specified(talloc_tos(), in,
6729ff
-					     username, domname,
6729ff
+					     username, NULL, domname,
6729ff
 					     uid, gid);
6729ff
 	} else {
6729ff
 		templ = talloc_sub_specified(talloc_tos(), lp_template,
6729ff
-					     username, domname,
6729ff
+					     username, NULL, domname,
6729ff
 					     uid, gid);
6729ff
 	}
6729ff
 
6729ff
-- 
6729ff
1.8.4.3
6729ff
6729ff
6729ff
From 5ccae02f63d655d476d887660f28ad4dce08e790 Mon Sep 17 00:00:00 2001
6729ff
From: Andreas Schneider <asn@samba.org>
6729ff
Date: Mon, 18 Nov 2013 14:58:14 +0100
6729ff
Subject: [PATCH 2/2] s3-winbind: Pass the group name to fillup_pw_field().
6729ff
6729ff
BUG: https://bugzilla.samba.org/show_bug.cgi?id=2191
6729ff
6729ff
Signed-off-by: Andreas Schneider <asn@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
6729ff
Autobuild-User(master): Jeremy Allison <jra@samba.org>
6729ff
Autobuild-Date(master): Fri Nov 22 02:04:54 CET 2013 on sn-devel-104
6729ff
6729ff
(cherry picked from commit 000172a5ab7e4bfac7ef618d0d78ec7fe95d0e2a)
6729ff
---
6729ff
 source3/winbindd/wb_fill_pwent.c | 73 +++++++++++++++++++++++-----------------
6729ff
 1 file changed, 43 insertions(+), 30 deletions(-)
6729ff
6729ff
diff --git a/source3/winbindd/wb_fill_pwent.c b/source3/winbindd/wb_fill_pwent.c
6729ff
index 3b711bd..9d0abbd 100644
6729ff
--- a/source3/winbindd/wb_fill_pwent.c
6729ff
+++ b/source3/winbindd/wb_fill_pwent.c
6729ff
@@ -29,6 +29,7 @@ struct wb_fill_pwent_state {
6729ff
 
6729ff
 static bool fillup_pw_field(const char *lp_template,
6729ff
 			    const char *username,
6729ff
+			    const char *grpname,
6729ff
 			    const char *domname,
6729ff
 			    uid_t uid,
6729ff
 			    gid_t gid,
6729ff
@@ -36,7 +37,7 @@ static bool fillup_pw_field(const char *lp_template,
6729ff
 			    fstring out);
6729ff
 
6729ff
 static void wb_fill_pwent_sid2uid_done(struct tevent_req *subreq);
6729ff
-static void wb_fill_pwent_sid2gid_done(struct tevent_req *subreq);
6729ff
+static void wb_fill_pwent_getgrsid_done(struct tevent_req *subreq);
6729ff
 
6729ff
 struct tevent_req *wb_fill_pwent_send(TALLOC_CTX *mem_ctx,
6729ff
 				      struct tevent_context *ev,
6729ff
@@ -90,47 +91,45 @@ static void wb_fill_pwent_sid2uid_done(struct tevent_req *subreq)
6729ff
 
6729ff
 	state->pw->pw_uid = (uid_t)xid.id;
6729ff
 
6729ff
-	subreq = wb_sids2xids_send(state, state->ev, &state->info->group_sid, 1);
6729ff
+	subreq = wb_getgrsid_send(state, state->ev, &state->info->group_sid, 1);
6729ff
 	if (tevent_req_nomem(subreq, req)) {
6729ff
 		return;
6729ff
 	}
6729ff
-	tevent_req_set_callback(subreq, wb_fill_pwent_sid2gid_done, req);
6729ff
+	tevent_req_set_callback(subreq, wb_fill_pwent_getgrsid_done, req);
6729ff
 }
6729ff
 
6729ff
-static void wb_fill_pwent_sid2gid_done(struct tevent_req *subreq)
6729ff
+static void wb_fill_pwent_getgrsid_done(struct tevent_req *subreq)
6729ff
 {
6729ff
 	struct tevent_req *req = tevent_req_callback_data(
6729ff
 		subreq, struct tevent_req);
6729ff
 	struct wb_fill_pwent_state *state = tevent_req_data(
6729ff
 		req, struct wb_fill_pwent_state);
6729ff
 	struct winbindd_domain *domain;
6729ff
-	char *dom_name;
6729ff
+	const char *dom_name;
6729ff
+	const char *grp_name;
6729ff
 	fstring user_name, output_username;
6729ff
 	char *mapped_name = NULL;
6729ff
+	struct talloc_dict *members;
6729ff
+	TALLOC_CTX *tmp_ctx = talloc_stackframe();
6729ff
 	NTSTATUS status;
6729ff
-	struct unixid xid;
6729ff
-
6729ff
-	status = wb_sids2xids_recv(subreq, &xid;;
6729ff
+	bool ok;
6729ff
+
6729ff
+	/* xid handling is done in getgrsid() */
6729ff
+	status = wb_getgrsid_recv(subreq,
6729ff
+				  tmp_ctx,
6729ff
+				  &dom_name,
6729ff
+				  &grp_name,
6729ff
+				  &state->pw->pw_gid,
6729ff
+				  &members);
6729ff
 	TALLOC_FREE(subreq);
6729ff
 	if (tevent_req_nterror(req, status)) {
6729ff
+		talloc_free(tmp_ctx);
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
-	/*
6729ff
-	 * We are filtering further down in sids2xids, but that filtering
6729ff
-	 * depends on the actual type of the sid handed in (as determined
6729ff
-	 * by lookupsids). Here we need to filter for the type of object
6729ff
-	 * actually requested, in this case gid.
6729ff
-	 */
6729ff
-	if (!(xid.type == ID_TYPE_GID || xid.type == ID_TYPE_BOTH)) {
6729ff
-		tevent_req_nterror(req, NT_STATUS_NONE_MAPPED);
6729ff
-		return;
6729ff
-	}
6729ff
-
6729ff
-	state->pw->pw_gid = (gid_t)xid.id;
6729ff
-
6729ff
 	domain = find_domain_from_sid_noinit(&state->info->user_sid);
6729ff
 	if (domain == NULL) {
6729ff
+		talloc_free(tmp_ctx);
6729ff
 		tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
6729ff
 		return;
6729ff
 	}
6729ff
@@ -166,17 +165,30 @@ static void wb_fill_pwent_sid2gid_done(struct tevent_req *subreq)
6729ff
 	fstrcpy(state->pw->pw_gecos, state->info->full_name);
6729ff
 
6729ff
 	/* Home directory and shell */
6729ff
-
6729ff
-	if (!fillup_pw_field(lp_template_homedir(), user_name, dom_name,
6729ff
-			     state->pw->pw_uid, state->pw->pw_gid,
6729ff
-			     state->info->homedir, state->pw->pw_dir)) {
6729ff
+	ok = fillup_pw_field(lp_template_homedir(),
6729ff
+			     user_name,
6729ff
+			     grp_name,
6729ff
+			     dom_name,
6729ff
+			     state->pw->pw_uid,
6729ff
+			     state->pw->pw_gid,
6729ff
+			     state->info->homedir,
6729ff
+			     state->pw->pw_dir);
6729ff
+	if (!ok) {
6729ff
+		talloc_free(tmp_ctx);
6729ff
 		tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
-	if (!fillup_pw_field(lp_template_shell(), user_name, dom_name,
6729ff
-			     state->pw->pw_uid, state->pw->pw_gid,
6729ff
-			     state->info->shell, state->pw->pw_shell)) {
6729ff
+	ok = fillup_pw_field(lp_template_shell(),
6729ff
+			     user_name,
6729ff
+			     grp_name,
6729ff
+			     dom_name,
6729ff
+			     state->pw->pw_uid,
6729ff
+			     state->pw->pw_gid,
6729ff
+			     state->info->shell,
6729ff
+			     state->pw->pw_shell);
6729ff
+	talloc_free(tmp_ctx);
6729ff
+	if (!ok) {
6729ff
 		tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
6729ff
 		return;
6729ff
 	}
6729ff
@@ -195,6 +207,7 @@ NTSTATUS wb_fill_pwent_recv(struct tevent_req *req)
6729ff
 
6729ff
 static bool fillup_pw_field(const char *lp_template,
6729ff
 			    const char *username,
6729ff
+			    const char *grpname,
6729ff
 			    const char *domname,
6729ff
 			    uid_t uid,
6729ff
 			    gid_t gid,
6729ff
@@ -214,11 +227,11 @@ static bool fillup_pw_field(const char *lp_template,
6729ff
 
6729ff
 	if ((in != NULL) && (in[0] != '\0') && (lp_security() == SEC_ADS)) {
6729ff
 		templ = talloc_sub_specified(talloc_tos(), in,
6729ff
-					     username, NULL, domname,
6729ff
+					     username, grpname, domname,
6729ff
 					     uid, gid);
6729ff
 	} else {
6729ff
 		templ = talloc_sub_specified(talloc_tos(), lp_template,
6729ff
-					     username, NULL, domname,
6729ff
+					     username, grpname, domname,
6729ff
 					     uid, gid);
6729ff
 	}
6729ff
 
6729ff
-- 
6729ff
1.8.4.3
6729ff