From 36ef1b50e52157eca9107dbe7dcb5f5a70728f49 Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Sat, 23 Oct 2004 15:16:10 +0000 Subject: r3146: Some cleanup for idmap_rid: - fix several memleaks found by valgrind - turn off support for trusted domains (can be reenabled with #define IDMAP_RID_SUPPORT_TRUSTED_DOMAINS 1) - improve readability Guenther (This used to be commit 351a1227e80db5d87b71e17cd1443c11ea6ace4e) --- source3/sam/idmap_rid.c | 305 ++++++++++++++++++++++++------------------------ 1 file changed, 150 insertions(+), 155 deletions(-) diff --git a/source3/sam/idmap_rid.c b/source3/sam/idmap_rid.c index 0a81475378..222d5e0963 100644 --- a/source3/sam/idmap_rid.c +++ b/source3/sam/idmap_rid.c @@ -7,14 +7,6 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * - * TODO: - * - use str_list_copy to work on a either space or comma-separated list - * - assure somehow that we cannot swap uids/gids (difficult without any - * knowledge about the sid-type) - * - fix memory allocation - * - further cleanup - * - add spnego-login */ #include "includes.h" @@ -22,13 +14,15 @@ #undef DBGC_CLASS #define DBGC_CLASS DBGC_IDMAP +#define IDMAP_RID_SUPPORT_TRUSTED_DOMAINS 0 + NTSTATUS init_module(void); struct dom_entry { - char *name; - char *sid; - int min_id; - int max_id; + fstring name; + fstring sid; + uint32 min_id; + uint32 max_id; }; typedef struct trust_dom_array { @@ -38,139 +32,111 @@ typedef struct trust_dom_array { static trust_dom_array trust; -static NTSTATUS rid_idmap_parse(char *init_param, +static NTSTATUS rid_idmap_parse(const char *init_param, uint32 num_domains, - char **domain_names, + fstring *domain_names, DOM_SID *domain_sids, uid_t u_low, uid_t u_high) { - char *p, *e; + const char *p; int i; trust.number = 0; - trust.dom = NULL; fstring sid_str; BOOL known_domain = False; p = init_param; - - /* init sizes */ - trust.dom = (struct dom_entry *) malloc(sizeof(struct dom_entry)); - if (trust.dom == NULL) - return NT_STATUS_NO_MEMORY; - - trust.dom[0].sid = (char *) malloc(strlen(sid_str)); - if (trust.dom[0].sid == NULL) - return NT_STATUS_NO_MEMORY; - - trust.dom[0].name = (char *) malloc(strlen(p)); - if (trust.dom[0].name == NULL) - return NT_STATUS_NO_MEMORY; + fstring tok; /* falling back to automatic mapping when there were no options given */ - if (!*init_param) { + if (!*init_param || !lp_allow_trusted_domains()) { + + DEBUG(3,("rid_idmap_parse: no domain list given or trusted domain-support deactivated, falling back to automatic mapping for own domain:\n")); - DEBUG(3,("rid_idmap_parse: no domain list given, falling back to automatic mapping for own domain:\n")); + sid_to_string(sid_str, &domain_sids[0]); - sid_to_string(sid_str, &domain_sids[num_domains-1]); - trust.dom[0].name = domain_names[num_domains-1]; - trust.dom[0].sid = strdup(sid_str); + fstrcpy(trust.dom[0].name, domain_names[0]); + fstrcpy(trust.dom[0].sid, sid_str); trust.dom[0].min_id = u_low; trust.dom[0].max_id = u_high; trust.number = 1; + DEBUGADD(3,("rid_idmap_parse:\tdomain: [%s], sid: [%s], range=[%d-%d]\n", trust.dom[0].name, trust.dom[0].sid, trust.dom[0].min_id, trust.dom[0].max_id)); return NT_STATUS_OK; } /* scan through the init_param-list */ - for(;;) { + while (next_token(&init_param, tok, LIST_SEP, sizeof(tok))) { + p = tok; DEBUG(3,("rid_idmap_parse: parsing entry: %d\n", trust.number)); /* reinit sizes */ - trust.dom = (struct dom_entry *) realloc(trust.dom,sizeof(struct dom_entry)*(trust.number+1)); - if ( trust.dom == NULL ) + trust.dom = (struct dom_entry *) realloc(trust.dom, sizeof(struct dom_entry)*(trust.number+1)); + + if ( trust.dom == NULL ) { return NT_STATUS_NO_MEMORY; + } - trust.dom[trust.number].sid = (char *) malloc(strlen(p)); - trust.dom[trust.number].name = (char *) malloc(strlen(p)); - if (trust.dom[trust.number].sid == NULL || trust.dom[trust.number].name == NULL) - return NT_STATUS_NO_MEMORY; - - - if ( (e = strchr(p,'=')) == NULL ) { + if (!next_token(&p, tok, "=", sizeof(tok))) { DEBUG(0, ("rid_idmap_parse: no '=' sign found in domain list [%s]\n", init_param)); - return NT_STATUS_INVALID_PARAMETER; + return NT_STATUS_UNSUCCESSFUL; } - *e = '\0'; - - /* add the name */ - i=0; - do { - if ( *p != ' ' && *p != '\t' ) { - trust.dom[trust.number].name[i++] = *p; - } - p++; - } while(*p); - trust.dom[trust.number].name[i++] = '\0'; + /* add the name */ + fstrcpy(trust.dom[trust.number].name, tok); DEBUGADD(3,("rid_idmap_parse:\tentry %d has name: [%s]\n", trust.number, trust.dom[trust.number].name)); /* add the domain-sid */ for (i=0; i trust.dom[i].max_id) { - DEBUG(0, ("rid_idmap_init: min_id has to be smaller than max_id for %s\n",trust.dom[i].name)); - return NT_STATUS_INVALID_PARAMETER; + DEBUG(0, ("rid_idmap_init: min_id (%d) has to be smaller than max_id (%d) for domain [%s]\n", + trust.dom[i].min_id, trust.dom[i].max_id, trust.dom[i].name)); + goto out; } + if (trust.dom[i].min_id < u_low || trust.dom[i].max_id > u_high) { - DEBUG(0, ("rid_idmap_init: mapping of domain %s (%d-%d) has to fit into global idmap range (%d-%d).\n", + DEBUG(0, ("rid_idmap_init: mapping of domain [%s] (%d-%d) has to fit into global idmap range (%d-%d).\n", trust.dom[i].name, trust.dom[i].min_id, trust.dom[i].max_id, u_low, u_high)); - return NT_STATUS_INVALID_PARAMETER; + goto out; } } /* check for overlaps */ - for(i=0;i= unid.uid ) break; } - + if (i == trust.number) { - DEBUG(0,("no range found for uid: %d\n",unid.uid)); - return NT_STATUS_NOT_IMPLEMENTED; + DEBUG(0,("rid_idmap_get_sid_from_id: no suitable range available for id: %d\n", unid.uid)); + return NT_STATUS_INVALID_PARAMETER; } /* use lower-end of idmap-range as offset for users and groups*/ - (unsigned long)unid.uid -= trust.dom[i].min_id; + unid.uid -= trust.dom[i].min_id; - string_to_sid(&sidstr,trust.dom[i].sid); + if (!trust.dom[i].sid) + return NT_STATUS_INVALID_PARAMETER; + + string_to_sid(&sidstr, trust.dom[i].sid); sid_copy(sid, &sidstr); if (!sid_append_rid( sid, (unsigned long)unid.uid )) { DEBUG(0,("rid_idmap_get_sid_from_id: could not append rid to domain sid\n")); @@ -421,30 +422,29 @@ static NTSTATUS rid_idmap_get_id_from_sid(unid_t *unid, int *id_type, const DOM_ uint32 rid; DOM_SID sidstr; - if (unid == NULL) { - return NT_STATUS_INVALID_PARAMETER; - } - - /* check if we have a mapping for the sid*/ - for(i=0;iuid = rid + trust.dom[i].min_id; + unid->uid = rid + trust.dom[i].min_id; if (unid->uid > trust.dom[i].max_id) { DEBUG(0,("rid_idmap_get_id_from_sid: rid: %d too high for mapping of domain: %s\n", rid, trust.dom[i].name)); @@ -470,12 +470,7 @@ static NTSTATUS rid_idmap_set_mapping(const DOM_SID *sid, unid_t id, int id_type static NTSTATUS rid_idmap_close(void) { - int i; - for(i=0;i