Removed version number from file header.
[kai/samba.git] / source3 / smbd / groupname.c
index 4cfd3e98f52f4128714862e624a11ef7b2f4160e..812488571a33b2442a444ba307f20a0f496779e8 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    Groupname handling
    Copyright (C) Jeremy Allison 1998.
    
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-#include "includes.h"
-extern int DEBUGLEVEL;
-extern DOM_SID global_machine_sid;
+#ifdef USING_GROUPNAME_MAP
 
+#include "includes.h"
+extern DOM_SID global_sam_sid;
 
 /**************************************************************************
  Groupname map functionality. The code loads a groupname map file and
@@ -51,11 +50,9 @@ static void delete_groupname_map_list(void)
   groupname_map_entry *gmep;
 
   while((gmep = (groupname_map_entry *)ubi_slRemHead( &groupname_map_list )) != NULL) {
-    if(gmep->windows_name)
-      free(gmep->windows_name);
-    if(gmep->unix_name)
-      free(gmep->unix_name);
-    free((char *)gmep);
+    SAFE_FREE(gmep->windows_name);
+    SAFE_FREE(gmep->unix_name);
+    SAFE_FREE(gmep);
   }
 }
 
@@ -68,10 +65,9 @@ void load_groupname_map(void)
   static time_t groupmap_file_last_modified = (time_t)0;
   static BOOL initialized = False;
   char *groupname_map_file = lp_groupname_map();
-  struct stat st;
-  FILE *fp;
-  char *s;
-  pstring buf;
+  SMB_STRUCT_STAT st;
+  char **lines;
+  int i;
   groupname_map_entry *new_ep;
 
   if(!initialized) {
@@ -82,7 +78,7 @@ void load_groupname_map(void)
   if (!*groupname_map_file)
     return;
 
-  if(stat(groupname_map_file, &st) != 0) {
+  if(sys_stat(groupname_map_file, &st) != 0) {
     DEBUG(0, ("load_groupname_map: Unable to stat file %s. Error was %s\n",
                groupname_map_file, strerror(errno) ));
     return;
@@ -100,12 +96,13 @@ void load_groupname_map(void)
    * Load the file.
    */
 
-  fp = fopen(groupname_map_file,"r");
-  if (!fp) {
+  lines = file_lines_load(groupname_map_file,NULL,False);
+  if (!lines) {
     DEBUG(0,("load_groupname_map: can't open groupname map %s. Error was %s\n",
           groupname_map_file, strerror(errno)));
     return;
   }
+  file_lines_slashcont(lines);
 
   /*
    * Throw away any previous list.
@@ -114,21 +111,22 @@ void load_groupname_map(void)
 
   DEBUG(4,("load_groupname_map: Scanning groupname map %s\n",groupname_map_file));
 
-  while((s=fgets_slash(buf,sizeof(buf),fp))!=NULL) {
+  for (i=0; lines[i]; i++) {
     pstring unixname;
     pstring windows_name;
-    struct group *gptr;
+    gid_t gid;
     DOM_SID tmp_sid;
+    char *s = lines[i];
 
     DEBUG(10,("load_groupname_map: Read line |%s|\n", s));
 
-    if (!*s || strchr("#;",*s))
+    if (!*s || strchr_m("#;",*s))
       continue;
 
-    if(!next_token(&s,unixname, "\t\n\r="))
+    if(!next_token(&s,unixname, "\t\n\r=", sizeof(unixname)))
       continue;
 
-    if(!next_token(&s,windows_name, "\t\n\r="))
+    if(!next_token(&s,windows_name, "\t\n\r=", sizeof(windows_name)))
       continue;
 
     trim_string(unixname, " ", " ");
@@ -147,8 +145,8 @@ void load_groupname_map(void)
      * Attempt to get the unix gid_t for this name.
      */
 
-    if((gptr = (struct group *)getgrnam(unixname)) == NULL) {
-      DEBUG(0,("load_groupname_map: getgrnam for group %s failed.\
+       if ((gid = nametogid(unixname)) == (gid_t)-1)
+      DEBUG(0,("load_groupname_map: nametogid for group %s failed.\
 Error was %s.\n", unixname, strerror(errno) ));
       continue;
     }
@@ -162,9 +160,9 @@ Error was %s.\n", unixname, strerror(errno) ));
        * It's not a well known name, convert the UNIX gid_t
        * to a rid within this domain SID.
        */
-      tmp_sid = global_machine_sid;
+      tmp_sid = global_sam_sid;
       tmp_sid.sub_auths[tmp_sid.num_auths++] = 
-                    pdb_gid_to_group_rid((gid_t)gptr->gr_gid);
+                    pdb_gid_to_group_rid(gid);
     }
 
     /*
@@ -177,7 +175,7 @@ Error was %s.\n", unixname, strerror(errno) ));
       return;
     } 
 
-    new_ep->unix_gid = gptr->gr_gid;
+    new_ep->unix_gid = gid;
     new_ep->windows_sid = tmp_sid;
     new_ep->windows_name = strdup( windows_name );
     new_ep->unix_name = strdup( unixname );
@@ -185,11 +183,10 @@ Error was %s.\n", unixname, strerror(errno) ));
     if(new_ep->windows_name == NULL || new_ep->unix_name == NULL) {
       DEBUG(0,("load_groupname_map: malloc fail for names in groupname_map_entry.\n"));
       fclose(fp);
-      if(new_ep->windows_name != NULL)
-        free(new_ep->windows_name);
-      if(new_ep->unix_name != NULL)
-        free(new_ep->unix_name);
-      free((char *)new_ep);
+      SAFE_FREE(new_ep->windows_name);
+      SAFE_FREE(new_ep->unix_name);
+      SAFE_FREE(new_ep);
+      file_lines_free(lines);
       return;
     }
     memset((char *)&new_ep->next, '\0', sizeof(new_ep->next) );
@@ -197,8 +194,45 @@ Error was %s.\n", unixname, strerror(errno) ));
     ubi_slAddHead( &groupname_map_list, (ubi_slNode *)new_ep);
   }
 
-  DEBUG(10,("load_groupname_map: Added %d entries to groupname map.\n",
-        ubi_slCount( &groupname_map_list ) ));
+  DEBUG(10,("load_groupname_map: Added %ld entries to groupname map.\n",
+           ubi_slCount(&groupname_map_list)));
            
-  fclose(fp);
+  file_lines_free(lines);
+}
+
+/***********************************************************
+ Lookup a SID entry by gid_t.
+************************************************************/
+
+void map_gid_to_sid( gid_t gid, DOM_SID *psid)
+{
+  groupname_map_entry *gmep;
+
+  /*
+   * Initialize and load if not already loaded.
+   */
+  load_groupname_map();
+
+  for( gmep = (groupname_map_entry *)ubi_slFirst( &groupname_map_list);
+       gmep; gmep = (groupname_map_entry *)ubi_slNext( gmep )) {
+
+    if( gmep->unix_gid == gid) {
+      *psid = gmep->windows_sid;
+      DEBUG(7,("map_gid_to_sid: Mapping unix group %s to windows group %s.\n",
+               gmep->unix_name, gmep->windows_name ));
+      return;
+    }
+  }
+
+  /*
+   * If there's no map, convert the UNIX gid_t
+   * to a rid within this domain SID.
+   */
+  *psid = global_sam_sid;
+  psid->sub_auths[psid->num_auths++] = pdb_gid_to_group_rid(gid);
+
+  return;
 }
+#else /* USING_GROUPNAME_MAP */
+ void load_groupname_map(void) {;}
+#endif /* USING_GROUPNAME_MAP */