#ifdef USING_GROUPNAME_MAP
#include "includes.h"
extern DOM_SID global_sam_sid;
typedef struct groupname_map {
ubi_slNode next;
char *windows_name;
DOM_SID windows_sid;
char *unix_name;
gid_t unix_gid;
} groupname_map_entry;
static ubi_slList groupname_map_list;
static void delete_groupname_map_list(void)
{
groupname_map_entry *gmep;
while((gmep = (groupname_map_entry *)ubi_slRemHead( &groupname_map_list )) != NULL) {
SAFE_FREE(gmep->windows_name);
SAFE_FREE(gmep->unix_name);
SAFE_FREE(gmep);
}
}
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();
SMB_STRUCT_STAT st;
char **lines;
int i;
groupname_map_entry *new_ep;
if(!initialized) {
ubi_slInitList( &groupname_map_list );
initialized = True;
}
if (!*groupname_map_file)
return;
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;
}
if( st.st_mtime <= groupmap_file_last_modified)
return;
groupmap_file_last_modified = st.st_mtime;
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);
delete_groupname_map_list();
DEBUG(4,("load_groupname_map: Scanning groupname map %s\n",groupname_map_file));
for (i=0; lines[i]; i++) {
pstring unixname;
pstring windows_name;
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))
continue;
if(!next_token(&s,unixname, "\t\n\r=", sizeof(unixname)))
continue;
if(!next_token(&s,windows_name, "\t\n\r=", sizeof(windows_name)))
continue;
trim_string(unixname, " ", " ");
trim_string(windows_name, " ", " ");
if (!*windows_name)
continue;
if(!*unixname)
continue;
DEBUG(5,("load_groupname_map: unixname = %s, windowsname = %s.\n",
unixname, windows_name));
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;
}
if(!lookup_wellknown_sid_from_name(windows_name, &tmp_sid)) {
tmp_sid = global_sam_sid;
tmp_sid.sub_auths[tmp_sid.num_auths++] =
pdb_gid_to_group_rid(gid);
}
if((new_ep = (groupname_map_entry *)malloc( sizeof(groupname_map_entry) ))== NULL) {
DEBUG(0,("load_groupname_map: malloc fail for groupname_map_entry.\n"));
fclose(fp);
return;
}
new_ep->unix_gid = gid;
new_ep->windows_sid = tmp_sid;
new_ep->windows_name = strdup( windows_name );
new_ep->unix_name = strdup( unixname );
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);
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) );
ubi_slAddHead( &groupname_map_list, (ubi_slNode *)new_ep);
}
DEBUG(10,("load_groupname_map: Added %ld entries to groupname map.\n",
ubi_slCount(&groupname_map_list)));
file_lines_free(lines);
}
void map_gid_to_sid( gid_t gid, DOM_SID *psid)
{
groupname_map_entry *gmep;
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;
}
}
*psid = global_sam_sid;
psid->sub_auths[psid->num_auths++] = pdb_gid_to_group_rid(gid);
return;
}
#else
void load_groupname_map(void) {;}
#endif