r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation
[tprouty/samba.git] / source / nsswitch / winbindd_user.c
index 37d92450759abf73e1bc42e945cbf7a5191975d4..fc3fe0f9631791d4b1bd46c4ed89d9f081c33268 100644 (file)
@@ -22,6 +22,7 @@
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include "includes.h"
 #include "winbindd.h"
 
 #undef DBGC_CLASS
@@ -36,7 +37,8 @@ static BOOL winbindd_fill_pwent(char *dom_name, char *user_name,
                                char *full_name, struct winbindd_pw *pw)
 {
        fstring output_username;
-       pstring homedir;
+       char *homedir;
+       char *shell;
        fstring sid_string;
        
        if (!pw || !dom_name || !user_name)
@@ -44,18 +46,20 @@ static BOOL winbindd_fill_pwent(char *dom_name, char *user_name,
        
        /* Resolve the uid number */
 
-       if (!NT_STATUS_IS_OK(idmap_sid_to_uid(user_sid, &(pw->pw_uid), 0))) {
+       if (!NT_STATUS_IS_OK(idmap_sid_to_uid(user_sid, &pw->pw_uid, 0))) {
                DEBUG(1, ("error getting user id for sid %s\n", sid_to_string(sid_string, user_sid)));
                return False;
        }
        
        /* Resolve the gid number */   
 
-       if (!NT_STATUS_IS_OK(idmap_sid_to_gid(group_sid, &(pw->pw_gid), 0))) {
+       if (!NT_STATUS_IS_OK(idmap_sid_to_gid(group_sid, &pw->pw_gid, 0))) {
                DEBUG(1, ("error getting group id for sid %s\n", sid_to_string(sid_string, group_sid)));
                return False;
        }
 
+       strlower_m(user_name);
+
        /* Username */
 
        fill_domain_username(output_username, dom_name, user_name); 
@@ -71,24 +75,34 @@ static BOOL winbindd_fill_pwent(char *dom_name, char *user_name,
           shell. */
        
        /* The substitution of %U and %D in the 'template homedir' is done
-          by lp_string() calling standard_sub_basic(). */
+          by alloc_sub_specified() below. */
 
-       fstrcpy(current_user_info.smb_name, user_name);
-       sub_set_smb_name(user_name);
        fstrcpy(current_user_info.domain, dom_name);
 
-       pstrcpy(homedir, lp_template_homedir());
+       homedir = alloc_sub_specified(lp_template_homedir(), user_name, dom_name, pw->pw_uid, pw->pw_gid);
+
+       if (!homedir)
+               return False;
        
        safe_strcpy(pw->pw_dir, homedir, sizeof(pw->pw_dir) - 1);
        
-       safe_strcpy(pw->pw_shell, lp_template_shell(), 
+       SAFE_FREE(homedir);
+       
+       shell = alloc_sub_specified(lp_template_shell(), user_name, dom_name, pw->pw_uid, pw->pw_gid);
+
+       if (!shell)
+               return False;
+
+       safe_strcpy(pw->pw_shell, shell, 
                    sizeof(pw->pw_shell) - 1);
        
+       SAFE_FREE(shell);
+
        /* Password - set to "x" as we can't generate anything useful here.
           Authentication can be done using the pam_winbind module. */
 
        safe_strcpy(pw->pw_passwd, "x", sizeof(pw->pw_passwd) - 1);
-       
+
        return True;
 }
 
@@ -97,6 +111,7 @@ static BOOL winbindd_fill_pwent(char *dom_name, char *user_name,
 enum winbindd_result winbindd_getpwnam(struct winbindd_cli_state *state) 
 {
        WINBIND_USERINFO user_info;
+       WINBINDD_PW *pw;
        DOM_SID user_sid;
        NTSTATUS status;
        fstring name_domain, name_user;
@@ -107,46 +122,53 @@ enum winbindd_result winbindd_getpwnam(struct winbindd_cli_state *state)
        /* Ensure null termination */
        state->request.data.username[sizeof(state->request.data.username)-1]='\0';
 
-       DEBUG(3, ("[%5d]: getpwnam %s\n", state->pid,
+       DEBUG(3, ("[%5lu]: getpwnam %s\n", (unsigned long)state->pid,
                  state->request.data.username));
        
        /* Parse domain and username */
 
-       if (!parse_domain_user(state->request.data.username, name_domain, 
-                              name_user))
-               return WINBINDD_ERROR;
+       parse_domain_user(state->request.data.username, 
+                         name_domain, name_user);
        
-       /* don't handle our own domain if we are a DC ( or a member of a Samba domain 
-          that shares UNIX accounts).  This code handles cases where
-          the account doesn't exist anywhere and gets passed on down the NSS layer */
+       /* if this is our local domain (or no domain), the do a local tdb search */
+       
+       if ( !*name_domain || strequal(name_domain, get_global_sam_name()) ) {
+               if ( !(pw = wb_getpwnam(name_user)) ) {
+                       DEBUG(5,("winbindd_getpwnam: lookup for %s\\%s failed\n",
+                               name_domain, name_user));
+                       return WINBINDD_ERROR;
+               }
+               memcpy( &state->response.data.pw, pw, sizeof(WINBINDD_PW) );
+               return WINBINDD_OK;
+       }
 
-       if ( (IS_DC || lp_winbind_trusted_domains_only()) && strequal(name_domain, lp_workgroup()) ) {
-               DEBUG(7,("winbindd_getpwnam: rejecting getpwnam() for %s\\%s since I am on the PDC for this domain\n", 
-                       name_domain, name_user));
-               return WINBINDD_ERROR;
-       }       
+       /* should we deal with users for our domain? */
        
        if ((domain = find_domain_from_name(name_domain)) == NULL) {
                DEBUG(5, ("no such domain: %s\n", name_domain));
                return WINBINDD_ERROR;
        }
        
+       if ( domain->primary && lp_winbind_trusted_domains_only()) {
+               DEBUG(7,("winbindd_getpwnam: My domain -- rejecting getpwnam() for %s\\%s.\n", 
+                       name_domain, name_user));
+               return WINBINDD_ERROR;
+       }       
+       
        /* Get rid and name type from name */
 
-       if (!winbindd_lookup_sid_by_name(domain, name_user, &user_sid, &name_type)) {
+       if (!winbindd_lookup_sid_by_name(domain, domain->name, name_user, &user_sid, &name_type)) {
                DEBUG(1, ("user '%s' does not exist\n", name_user));
                return WINBINDD_ERROR;
        }
 
-       if (name_type != SID_NAME_USER) {
+       if (name_type != SID_NAME_USER && name_type != SID_NAME_COMPUTER) {
                DEBUG(1, ("name '%s' is not a user name: %d\n", name_user, 
                          name_type));
                return WINBINDD_ERROR;
        }
        
-       /* Get some user info.  Split the user rid from the sid obtained
-          from the winbind_lookup_by_name() call and use it in a
-          winbind_lookup_userinfo() */
+       /* Get some user info. */
     
        if (!(mem_ctx = talloc_init("winbindd_getpwnam([%s]\\[%s])", 
                                          name_domain, name_user))) {
@@ -165,7 +187,7 @@ enum winbindd_result winbindd_getpwnam(struct winbindd_cli_state *state)
        }
     
        /* Now take all this information and fill in a passwd structure */      
-       if (!winbindd_fill_pwent(name_domain, name_user
+       if (!winbindd_fill_pwent(name_domain, user_info.acct_name
                                 user_info.user_sid, user_info.group_sid, 
                                 user_info.full_name,
                                 &state->response.data.pw)) {
@@ -184,6 +206,7 @@ enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
 {
        DOM_SID user_sid;
        struct winbindd_domain *domain;
+       WINBINDD_PW *pw;
        fstring dom_name;
        fstring user_name;
        enum SID_NAME_USE name_type;
@@ -198,14 +221,21 @@ enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
            (state->request.data.uid > server_state.uid_high))
                return WINBINDD_ERROR;
 
-       DEBUG(3, ("[%5d]: getpwuid %d\n", state->pid, 
-                 state->request.data.uid));
+       DEBUG(3, ("[%5lu]: getpwuid %lu\n", (unsigned long)state->pid, 
+                 (unsigned long)state->request.data.uid));
+
+       /* always try local tdb first */
+       
+       if ( (pw = wb_getpwuid(state->request.data.uid)) != NULL ) {
+               memcpy( &state->response.data.pw, pw, sizeof(WINBINDD_PW) );
+               return WINBINDD_OK;
+       }
        
        /* Get rid from uid */
 
        if (!NT_STATUS_IS_OK(idmap_uid_to_sid(&user_sid, state->request.data.uid))) {
-               DEBUG(1, ("could not convert uid %d to SID\n", 
-                         state->request.data.uid));
+               DEBUG(1, ("could not convert uid %lu to SID\n", 
+                         (unsigned long)state->request.data.uid));
                return WINBINDD_ERROR;
        }
        
@@ -228,8 +258,8 @@ enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
 
        /* Get some user info */
        
-       if (!(mem_ctx = talloc_init("winbind_getpwuid(%d)",
-                                         state->request.data.uid))) {
+       if (!(mem_ctx = talloc_init("winbind_getpwuid(%lu)",
+                                   (unsigned long)state->request.data.uid))) {
 
                DEBUG(1, ("out of memory\n"));
                return WINBINDD_ERROR;
@@ -255,7 +285,7 @@ enum winbindd_result winbindd_getpwuid(struct winbindd_cli_state *state)
 
        /* Fill in password structure */
 
-       if (!winbindd_fill_pwent(domain->name, user_name, user_info.user_sid, 
+       if (!winbindd_fill_pwent(domain->name, user_info.acct_name, user_info.user_sid, 
                                 user_info.group_sid,
                                 user_info.full_name, &state->response.data.pw)) {
                talloc_destroy(mem_ctx);
@@ -277,7 +307,7 @@ enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
 {
        struct winbindd_domain *domain;
         
-       DEBUG(3, ("[%5d]: setpwent\n", state->pid));
+       DEBUG(3, ("[%5lu]: setpwent\n", (unsigned long)state->pid));
         
        /* Check user has enabled this */
         
@@ -290,6 +320,19 @@ enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
                free_getent_state(state->getpwent_state);
                state->getpwent_state = NULL;
        }
+
+#if 0  /* JERRY */
+       /* add any local users we have */
+               
+       if ( (domain_state = (struct getent_state *)malloc(sizeof(struct getent_state))) == NULL )
+               return WINBINDD_ERROR;
+                
+       ZERO_STRUCTP(domain_state);
+
+       /* Add to list of open domains */
+                
+       DLIST_ADD(state->getpwent_state, domain_state);
+#endif
         
        /* Create sam pipes for each domain we know about */
         
@@ -308,8 +351,7 @@ enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
                                                
                /* Create a state record for this domain */
                 
-               if ((domain_state = (struct getent_state *)
-                    malloc(sizeof(struct getent_state))) == NULL)
+               if ((domain_state = SMB_MALLOC_P(struct getent_state)) == NULL)
                        return WINBINDD_ERROR;
                 
                ZERO_STRUCTP(domain_state);
@@ -321,6 +363,8 @@ enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
                DLIST_ADD(state->getpwent_state, domain_state);
        }
         
+       state->getpwent_initialized = True;
+        
        return WINBINDD_OK;
 }
 
@@ -328,9 +372,10 @@ enum winbindd_result winbindd_setpwent(struct winbindd_cli_state *state)
 
 enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
 {
-       DEBUG(3, ("[%5d]: endpwent\n", state->pid));
+       DEBUG(3, ("[%5lu]: endpwent\n", (unsigned long)state->pid));
 
        free_getent_state(state->getpwent_state);    
+       state->getpwent_initialized = False;
        state->getpwent_state = NULL;
         
        return WINBINDD_OK;
@@ -341,8 +386,6 @@ enum winbindd_result winbindd_endpwent(struct winbindd_cli_state *state)
    field is incremented to the index of the next user to fetch.  Return True if
    some users were returned, False otherwise. */
 
-#define MAX_FETCH_SAM_ENTRIES 100
-
 static BOOL get_sam_user_entries(struct getent_state *ent)
 {
        NTSTATUS status;
@@ -385,10 +428,7 @@ static BOOL get_sam_user_entries(struct getent_state *ent)
        if (num_entries) {
                struct getpwent_user *tnl;
                
-               tnl = (struct getpwent_user *)Realloc(name_list, 
-                                                     sizeof(struct getpwent_user) *
-                                                     (ent->num_sam_entries + 
-                                                      num_entries));
+               tnl = SMB_REALLOC_ARRAY(name_list, struct getpwent_user, ent->num_sam_entries + num_entries);
                
                if (!tnl) {
                        DEBUG(0,("get_sam_user_entries realloc failed.\n"));
@@ -443,7 +483,7 @@ enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
        struct winbindd_pw *user_list;
        int num_users, user_list_ndx = 0, i;
 
-       DEBUG(3, ("[%5d]: getpwent\n", state->pid));
+       DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)state->pid));
 
        /* Check user has enabled this */
 
@@ -454,14 +494,16 @@ enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
 
        num_users = MIN(MAX_GETPWENT_USERS, state->request.data.num_entries);
        
-       if ((state->response.extra_data = 
-            malloc(num_users * sizeof(struct winbindd_pw))) == NULL)
+       if ((state->response.extra_data = SMB_MALLOC_ARRAY(struct winbindd_pw, num_users)) == NULL)
                return WINBINDD_ERROR;
 
        memset(state->response.extra_data, 0, num_users * 
               sizeof(struct winbindd_pw));
 
        user_list = (struct winbindd_pw *)state->response.extra_data;
+
+       if (!state->getpwent_initialized)
+               winbindd_setpwent(state);
        
        if (!(ent = state->getpwent_state))
                return WINBINDD_ERROR;
@@ -498,15 +540,6 @@ enum winbindd_result winbindd_getpwent(struct winbindd_cli_state *state)
 
                name_list = ent->sam_entries;
 
-               /* Skip machine accounts */
-
-               if (name_list[ent->sam_entry_index].
-                   name[strlen(name_list[ent->sam_entry_index].name) - 1] 
-                   == '$') {
-                       ent->sam_entry_index++;
-                       continue;
-               }
-
                /* Lookup user info */
                
                result = winbindd_fill_pwent(
@@ -544,24 +577,36 @@ enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
 {
        struct winbindd_domain *domain;
        WINBIND_USERINFO *info;
+       const char *which_domain;
        uint32 num_entries = 0, total_entries = 0;
        char *ted, *extra_data = NULL;
        int extra_data_len = 0;
        TALLOC_CTX *mem_ctx;
        enum winbindd_result rv = WINBINDD_ERROR;
 
-       DEBUG(3, ("[%5d]: list users\n", state->pid));
+       DEBUG(3, ("[%5lu]: list users\n", (unsigned long)state->pid));
 
        if (!(mem_ctx = talloc_init("winbindd_list_users")))
                return WINBINDD_ERROR;
 
+       /* Ensure null termination */
+       state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';  
+       which_domain = state->request.domain_name;
+       
        /* Enumerate over trusted domains */
 
        for (domain = domain_list(); domain; domain = domain->next) {
                NTSTATUS status;
                struct winbindd_methods *methods;
                unsigned int i;
-
+               
+               /* if we have a domain name restricting the request and this
+                  one in the list doesn't match, then just bypass the remainder
+                  of the loop */
+                  
+               if ( *which_domain && !strequal(which_domain, domain->name) )
+                       continue;
+                       
                methods = domain->methods;
 
                /* Query display info */
@@ -574,7 +619,7 @@ enum winbindd_result winbindd_list_users(struct winbindd_cli_state *state)
                /* Allocate some memory for extra data */
                total_entries += num_entries;
                        
-               ted = Realloc(extra_data, sizeof(fstring) * total_entries);
+               ted = SMB_REALLOC(extra_data, sizeof(fstring) * total_entries);
                        
                if (!ted) {
                        DEBUG(0,("failed to enlarge buffer!\n"));