diff -uNrp samba-3.0.24.org/source/lib/substitute.c samba-3.0.24/source/lib/substitute.c
--- samba-3.0.24.org/source/lib/substitute.c	2007-03-05 20:04:57.000000000 +0900
+++ samba-3.0.24/source/lib/substitute.c	2007-03-05 20:06:46.000000000 +0900
@@ -581,7 +581,8 @@ char *talloc_sub_specified(TALLOC_CTX *m
 			gid_t gid)
 {
 	char *a, *t;
-       	a = alloc_sub_specified(input_string, username, domain, uid, gid);
+	const char *grpname = NULL;
+       	a = alloc_sub_specified(input_string, username, domain, uid, gid, grpname);
 	if (!a) {
 		return NULL;
 	}
@@ -597,7 +598,8 @@ char *alloc_sub_specified(const char *in
 			const char *username,
 			const char *domain,
 			uid_t uid,
-			gid_t gid)
+			gid_t gid,
+			const char *grpname)
 {
 	char *a_string, *ret_string;
 	char *b, *p, *s;
@@ -621,14 +623,20 @@ char *alloc_sub_specified(const char *in
 			break;
 		case 'G' :
 			if (gid != -1) {
-				a_string = realloc_string_sub(a_string, "%G", gidtoname(gid));
+				if (grpname == NULL)
+					a_string = realloc_string_sub(a_string, "%G", gidtoname(gid));
+				else
+					a_string = realloc_string_sub(a_string, "%G", grpname);
 			} else {
 				a_string = realloc_string_sub(a_string, "%G", "NO_GROUP");
 			}
 			break;
 		case 'g' :
 			if (gid != -1) {
-				a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
+				if (grpname == NULL)
+					a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
+				else
+					a_string = realloc_string_sub(a_string, "%g", grpname);
 			} else {
 				a_string = realloc_string_sub(a_string, "%g", "NO_GROUP");
 			}
diff -uNrp samba-3.0.24.org/source/nsswitch/winbindd_user.c samba-3.0.24/source/nsswitch/winbindd_user.c
--- samba-3.0.24.org/source/nsswitch/winbindd_user.c	2007-03-05 20:04:57.000000000 +0900
+++ samba-3.0.24/source/nsswitch/winbindd_user.c	2007-03-05 20:07:53.000000000 +0900
@@ -36,7 +36,8 @@ static BOOL fillup_pw_field(const char *
 			    uid_t uid,
 			    gid_t gid,
 			    const char *in, 
-			    fstring out)
+			    fstring out,
+			    const char *grpname)
 {
 	char *templ;
 
@@ -55,7 +56,7 @@ static BOOL fillup_pw_field(const char *
 	/* The substitution of %U and %D in the 'template homedir' is done
 	   by alloc_sub_specified() below. */
 
-	templ = alloc_sub_specified(lp_template, username, domname, uid, gid);
+	templ = alloc_sub_specified(lp_template, username, domname, uid, gid, grpname);
 		
 	if (!templ)
 		return False;
@@ -75,6 +76,9 @@ static BOOL winbindd_fill_pwent(char *do
 {
 	fstring output_username;
 	fstring sid_string;
+	fstring grpname;
+	enum SID_NAME_USE type;
+	TALLOC_CTX *mem_ctx=NULL;
 	
 	if (!pw || !dom_name || !user_name)
 		return False;
@@ -114,19 +118,29 @@ static BOOL winbindd_fill_pwent(char *do
 
 	fstrcpy(current_user_info.domain, dom_name);
 
+	/* Groupname */
+	if (!(mem_ctx = talloc_init("winbindd_lookup_name_by_sid")))
+		return False;
+
+	if (!winbindd_lookup_name_by_sid(mem_ctx, group_sid, dom_name, grpname, &type))
+		DEBUG(1, ("could not lookup sid\n"));
+
 	if (!fillup_pw_field(lp_template_homedir(), user_name, dom_name, 
-			     pw->pw_uid, pw->pw_gid, homedir, pw->pw_dir))
+			     pw->pw_uid, pw->pw_gid, homedir, pw->pw_dir, grpname)){
+		talloc_destroy(mem_ctx);
 		return False;
+	}
 
 	if (!fillup_pw_field(lp_template_shell(), user_name, dom_name, 
-			     pw->pw_uid, pw->pw_gid, shell, pw->pw_shell))
+			     pw->pw_uid, pw->pw_gid, shell, pw->pw_shell, grpname)){
+		talloc_destroy(mem_ctx);
 		return False;
-
+	}
 	/* Password - set to "*" as we can't generate anything useful here.
 	   Authentication can be done using the pam_winbind module. */
 
 	safe_strcpy(pw->pw_passwd, "*", sizeof(pw->pw_passwd) - 1);
-
+	talloc_destroy(mem_ctx);
 	return True;
 }
 
@@ -277,6 +291,9 @@ static void getpwsid_sid2gid_recv(void *
 		talloc_get_type_abort(private_data, struct getpwsid_state);
 	struct winbindd_pw *pw;
 	fstring output_username;
+	fstring grpname;
+	enum SID_NAME_USE type;
+	TALLOC_CTX *mem_ctx=NULL;
 
 	if (!success) {
 		DEBUG(5, ("Could not query user's %s\\%s\n gid",
@@ -295,14 +312,21 @@ static void getpwsid_sid2gid_recv(void *
 
 	fstrcpy(current_user_info.domain, s->domain->name);
 
+	/* Groupname */
+	if (!(mem_ctx = talloc_init("winbindd_lookup_name_by_sid")))
+		goto failed;
+
+	if (!winbindd_lookup_name_by_sid(mem_ctx, &s->group_sid, s->domain->name, grpname, &type))
+		DEBUG(1, ("could not lookup sid\n"));
+
 	if (!fillup_pw_field(lp_template_homedir(), s->username, s->domain->name, 
-			     pw->pw_uid, pw->pw_gid, s->homedir, pw->pw_dir)) {
+			     pw->pw_uid, pw->pw_gid, s->homedir, pw->pw_dir, grpname)) {
 		DEBUG(5, ("Could not compose homedir\n"));
 		goto failed;
 	}
 
 	if (!fillup_pw_field(lp_template_shell(), s->username, s->domain->name, 
-			     pw->pw_uid, pw->pw_gid, s->shell, pw->pw_shell)) {
+			     pw->pw_uid, pw->pw_gid, s->shell, pw->pw_shell, grpname)) {
 		DEBUG(5, ("Could not compose shell\n"));
 		goto failed;
 	}
@@ -313,9 +337,11 @@ static void getpwsid_sid2gid_recv(void *
 	safe_strcpy(pw->pw_passwd, "*", sizeof(pw->pw_passwd) - 1);
 
 	request_ok(s->state);
+	talloc_destroy(mem_ctx);
 	return;
 
  failed:
+	talloc_destroy(mem_ctx);
 	request_error(s->state);
 }
 
