Greg Dickie spotted some wierd memory corruption problem with group
[samba.git] / source3 / groupdb / groupunix.c
index 89406fe1391907a8534cb4688bec2448b7d1edf8..306e499858ce44891ee8bab8aabc7176c1a81a4e 100644 (file)
@@ -25,17 +25,38 @@ extern int DEBUGLEVEL;
 
 
 extern DOM_SID global_sam_sid;
-extern fstring global_sam_name;
 
 /***************************************************************
  Start to enumerate the grppasswd list. Returns a void pointer
  to ensure no modification outside this module.
 ****************************************************************/
 
+struct unix_entries
+{
+       struct group *grps;
+       int num_grps;
+       int grp_idx;
+};
+
 static void *startgrpunixpwent(BOOL update)
 {
-       setgrent();
-       return (void*)(-1);
+       struct unix_entries *grps;
+       grps = (struct unix_entries*)malloc(sizeof(struct unix_entries));
+
+       if (grps == NULL)
+       {
+               return NULL;
+       }
+
+       if (!get_unix_grps(&grps->num_grps, &grps->grps))
+       {
+               free(grps);
+               return NULL;
+       }
+
+       grps->grp_idx = 0;
+
+       return (void*)grps;
 }
 
 /***************************************************************
@@ -44,7 +65,13 @@ static void *startgrpunixpwent(BOOL update)
 
 static void endgrpunixpwent(void *vp)
 {
-       endgrent();
+       struct unix_entries *grps = (struct unix_entries *)vp;
+
+       if (grps != NULL)
+       {
+               free_unix_grps(grps->num_grps, grps->grps);
+               free(vp);
+       }
 }
 
 /*************************************************************************
@@ -65,48 +92,6 @@ static BOOL setgrpunixpwpos(void *vp, SMB_BIG_UINT tok)
        return False;
 }
 
-/*************************************************************************
- maps a unix group to a domain sid and an nt group name.  
-*************************************************************************/
-static void map_unix_grp_to_nt_grp(char *unix_name,
-       struct group *unix_grp, char *nt_name, DOM_SID *sid)
-{
-       BOOL found = False;
-       uint32 rid;
-
-       if (isdigit(unix_name[0]))
-       {
-               unix_grp->gr_gid = get_number(unix_name);
-               unix_grp->gr_name = unix_name;
-               found = map_group_gid(unix_grp->gr_gid, sid, nt_name, NULL);
-       }
-       else
-       {
-               unix_grp->gr_name = unix_name;
-               found = map_unix_group_name(unix_grp->gr_name, sid, nt_name, NULL);
-       }
-
-       if (found)
-       {
-               /*
-                * find the NT name represented by this UNIX gid.
-                * then, only accept NT groups that are in our domain
-                */
-
-               sid_split_rid(sid, &rid);
-       }
-       else
-       {
-               /*
-                * assume that the UNIX group is an NT group with
-                * the same name.  convert gid to a group rid.
-                */
-               
-               fstrcpy(nt_name, unix_grp->gr_name);
-               sid_copy(sid, &global_sam_sid);
-       }
-}
-
 /*************************************************************************
  Routine to return the next entry in the smbdomaingroup list.
  *************************************************************************/
@@ -115,7 +100,6 @@ BOOL get_unixgroup_members(struct group *grp,
 {
        int i;
        char *unix_name;
-       fstring nt_name;
 
        if (num_mem == NULL || members == NULL)
        {
@@ -127,15 +111,30 @@ BOOL get_unixgroup_members(struct group *grp,
 
        for (i = 0; (unix_name = grp->gr_mem[i]) != NULL; i++)
        {
-               DOM_SID sid;
-               struct group unix_grp;
+               DOM_NAME_MAP gmep;
+               DOMAIN_GRP_MEMBER *mem;
+               uint32 rid;
 
-               map_unix_grp_to_nt_grp(unix_name, &unix_grp, nt_name, &sid);
+               if (!lookupsmbpwnam (unix_name, &gmep) &&
+                   !lookupsmbgrpnam(unix_name, &gmep))
+               {
+                       continue;
+               }
 
-               if (!sid_equal(&sid, &global_sam_sid))
+               if (gmep.type != SID_NAME_DOM_GRP &&
+                   gmep.type != SID_NAME_USER &&
+                   gmep.type != SID_NAME_WKN_GRP)
                {
-                       DEBUG(0,("group database: could not resolve name %s in domain %s\n",
-                                 unix_name, global_sam_name));
+                       DEBUG(0,("group database: name %s is not in a Domain Group\n",
+                                 unix_name));
+                       continue;
+               }
+                       
+               sid_split_rid(&gmep.sid, &rid);
+               if (!sid_equal(&global_sam_sid, &gmep.sid))
+               {
+                       DEBUG(0,("group database: could not resolve name %s (wrong Domain SID)\n",
+                                 unix_name));
                        continue;
                }
 
@@ -145,9 +144,13 @@ BOOL get_unixgroup_members(struct group *grp,
                        return False;
                }
 
-               fstrcpy((*members)[(*num_mem)].name, nt_name);
-               (*members)[(*num_mem)].attr = 0x07;
+               mem = &(*members)[(*num_mem)];
                (*num_mem)++;
+
+               fstrcpy(mem->name, gmep.nt_name);
+               mem->attr    = 0x07;
+               mem->sid_use = gmep.type;
+               mem->rid     = rid;
        }
        return True;
 }
@@ -167,7 +170,13 @@ static DOMAIN_GRP *getgrpunixpwent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_m
 {
        /* Static buffers we will return. */
        static DOMAIN_GRP gp_buf;
-       struct group *unix_grp;
+       struct group unix_grp;
+       struct unix_entries *grps = (struct unix_entries *)vp;
+
+       if (grps == NULL)
+       {
+               return NULL;
+       }
 
        if (lp_server_role() == ROLE_DOMAIN_NONE || 
            lp_server_role() == ROLE_DOMAIN_MEMBER)
@@ -186,54 +195,42 @@ static DOMAIN_GRP *getgrpunixpwent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_m
        fstrcpy(gp_buf.comment, "");
        gp_buf.attr    = 0x07;
 
+       /* get array of unix names + gids.  this function does NOT
+          get a copy of the unix group members
+        */
+
        /* cycle through unix groups */
-       while ((unix_grp = getgrent()) != NULL)
+       for (; grps->grp_idx < grps->num_grps; grps->grp_idx++)
        {
-               DOM_SID sid;
-               BOOL is_alias;
+               DOM_NAME_MAP gmep;
+
+               memcpy(&unix_grp, &grps->grps[grps->grp_idx], sizeof(unix_grp));
 
                DEBUG(10,("getgrpunixpwent: enum unix group entry %s\n",
-                          unix_grp->gr_name));
-               is_alias = map_alias_gid(unix_grp->gr_gid, &sid, NULL, NULL);
-               if (is_alias)
+                          unix_grp.gr_name));
+                       
+               if (!lookupsmbgrpgid(unix_grp.gr_gid, &gmep))
                {
-                       sid_split_rid(&sid, NULL);
-                       is_alias = sid_equal(&sid, &global_sam_sid);
+                       continue;
                }
 
-               if (map_group_gid(unix_grp->gr_gid, &sid, gp_buf.name, NULL))
+               if (gmep.type != SID_NAME_DOM_GRP &&
+                   gmep.type != SID_NAME_WKN_GRP)
                {
-                       /*
-                        * find the NT name represented by this UNIX gid.
-                        * then, only accept NT groups that are in our domain
-                        */
-
-                       sid_split_rid(&sid, &gp_buf.rid);
-                       if (sid_equal(&sid, &global_sam_sid))
-                       {
-                               if (!is_alias)
-                               {
-                                       break; /* hooray. */
-                               }
-                               DEBUG(0,("configuration mistake: unix group %s is mapped to both an NT alias and an NT group\n",
-                                         gp_buf.name));
-                       }
+                       continue;
                }
-               else
-               {
-                       /*
-                        * assume that the UNIX group is an NT group with
-                        * the same name.  convert gid to a group rid.
-                        */
-                       
-                       fstrcpy(gp_buf.name, unix_grp->gr_name);
-                       gp_buf.rid = pwdb_gid_to_group_rid(unix_grp->gr_gid);
 
-                       break;
+               sid_split_rid(&gmep.sid, &gp_buf.rid);
+               if (!sid_equal(&gmep.sid, &global_sam_sid))
+               {
+                       continue;
                }
+
+               fstrcpy(gp_buf.name, gmep.nt_name);
+               break;
        }
 
-       if (unix_grp == NULL)
+       if (grps->grp_idx >= grps->num_grps)
        {
                return NULL;
        }
@@ -245,7 +242,8 @@ static DOMAIN_GRP *getgrpunixpwent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_m
                (*mem) = NULL;
                (*num_mem) = 0;
 
-               get_unixgroup_members(unix_grp, num_mem, mem);
+               memcpy(&unix_grp, getgrgid(unix_grp.gr_gid), sizeof(unix_grp));
+               get_unixgroup_members(&unix_grp, num_mem, mem);
        }
 
        {
@@ -254,6 +252,7 @@ static DOMAIN_GRP *getgrpunixpwent(void *vp, DOMAIN_GRP_MEMBER **mem, int *num_m
                DEBUG(10,("line: '%s'\n", linebuf));
        }
 
+       grps->grp_idx++; /* advance so next enum gets next entry */
        return &gp_buf;
 }
 
@@ -268,12 +267,8 @@ static BOOL add_grpunixgrp_entry(DOMAIN_GRP *newgrp)
 }
 
 /************************************************************************
- Routine to search the grppasswd file for an entry matching the groupname.
- and then modify its group entry. We can't use the startgrppwent()/
- getgrppwent()/endgrppwent() interfaces here as we depend on looking
- in the actual file to decide how much room we have to write data.
- override = False, normal
- override = True, override XXXXXXXX'd out group or NO PASS
+ Routine to search database for entry matching the groupname and/or rid.
+ and then modify its group entry. 
 ************************************************************************/
 
 static BOOL mod_grpunixgrp_entry(DOMAIN_GRP* grp)
@@ -282,6 +277,34 @@ static BOOL mod_grpunixgrp_entry(DOMAIN_GRP* grp)
        return False;
 }
 
+/************************************************************************
+ Routine to search the grppasswd file for an entry matching the rid.
+ and then delete it.
+************************************************************************/
+
+static BOOL del_grpunixgrp_entry(uint32 rid)
+{
+       DEBUG(0, ("del_grpunixgrp_entry: NOT IMPLEMENTED\n"));
+       return False;
+}
+
+/************************************************************************
+ Routine to add a member to an entry to the grppasswd file.
+*************************************************************************/
+static BOOL add_grpunixgrp_member(uint32 rid, uint32 member_rid)
+{
+       DEBUG(0, ("add_grpunixgrp_member: NOT IMPLEMENTED\n"));
+       return False;
+}
+
+/************************************************************************
+ Routine to delete a member from an entry to the grppasswd file.
+*************************************************************************/
+static BOOL del_grpunixgrp_member(uint32 rid, uint32 member_rid)
+{
+       DEBUG(0, ("del_grpunixgrp_member: NOT IMPLEMENTED\n"));
+       return False;
+}
 
 static struct groupdb_ops unix_ops =
 {
@@ -290,13 +313,17 @@ static struct groupdb_ops unix_ops =
        getgrpunixpwpos,
        setgrpunixpwpos,
 
-       iterate_getgroupnam,          /* In groupdb.c */
+       iterate_getgroupntnam,          /* In groupdb.c */
        iterate_getgroupgid,          /* In groupdb.c */
        iterate_getgrouprid,          /* In groupdb.c */
        getgrpunixpwent,
 
        add_grpunixgrp_entry,
        mod_grpunixgrp_entry,
+       del_grpunixgrp_entry,
+
+       add_grpunixgrp_member,
+       del_grpunixgrp_member,
 
        iterate_getusergroupsnam      /* in groupdb.c */
 };