Initialise cli variables and try not to do a cli_shutdown() of uninitialsed
[ira/wip.git] / source / nsswitch / winbindd_pam.c
index 9138583096177fc3fb71bbce2c52ce33a83c5160..95f0d527bbe92481dc9bdd9f366ecb1aca007247 100644 (file)
@@ -25,8 +25,7 @@
 
 #include "winbindd.h"
 
-/* Return a password structure from a username.  Specify whether cached data 
-   can be returned. */
+/* Return a password structure from a username.  */
 
 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state) 
 {
@@ -35,33 +34,50 @@ enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state)
        int passlen;
        unsigned char trust_passwd[16];
        time_t last_change_time;
-       auth_usersupplied_info *user_info;
         uint32 smb_uid_low;
         NET_USER_INFO_3 info3;
-       NET_ID_INFO_CTR ctr;
-        struct cli_state *cli;
+        struct cli_state *cli = NULL;
+       uchar chal[8];
+       TALLOC_CTX *mem_ctx;
+       DATA_BLOB lm_resp;
+       DATA_BLOB nt_resp;
+
+       extern pstring global_myname;
 
        DEBUG(3, ("[%5d]: pam auth %s\n", state->pid,
                  state->request.data.auth.user));
 
-       /* Parse domain and username */
-
-       parse_domain_user(state->request.data.auth.user, name_domain, 
-                          name_user);
-
-       /* don't allow the null domain */
+       if (!(mem_ctx = talloc_init_named("winbind pam auth for %s", state->request.data.auth.user))) {
+               DEBUG(0, ("winbindd_pam_auth: could not talloc_init()!\n"));
+               return WINBINDD_ERROR;
+       }
 
-       if (strcmp(name_domain,"") == 0) 
+       /* Parse domain and username */
+       
+       if (!parse_domain_user(state->request.data.auth.user, name_domain, 
+                              name_user)) {
+               DEBUG(5,("no domain seperator (%s) in username (%s) - failing fauth\n", lp_winbind_separator(), state->request.data.auth.user));
+               talloc_destroy(mem_ctx);
                return WINBINDD_ERROR;
+       }
 
        passlen = strlen(state->request.data.auth.pass);
                
-       if (state->request.data.auth.pass[0])
-               make_user_info_winbind(&user_info, 
-                                       name_user, name_domain,
-                                       state->request.data.auth.pass);
-       else
+       if (!*state->request.data.auth.pass) {
                return WINBINDD_ERROR;
+               talloc_destroy(mem_ctx);
+       } else {
+               unsigned char local_lm_response[24];
+               unsigned char local_nt_response[24];
+               
+               generate_random_buffer(chal, 8, False);
+               SMBencrypt( (const uchar *)state->request.data.auth.pass, chal, local_lm_response);
+               
+               SMBNTencrypt((const uchar *)state->request.data.auth.pass, chal, local_nt_response);
+
+               lm_resp = data_blob_talloc(mem_ctx, local_lm_response, sizeof(local_lm_response));
+               nt_resp = data_blob_talloc(mem_ctx, local_nt_response, sizeof(local_nt_response));
+       }
        
        /*
         * Get the machine account password for our primary domain
@@ -71,6 +87,7 @@ enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state)
                 lp_workgroup(), trust_passwd, &last_change_time)) {
                DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
                           "password for domain %s\n", lp_workgroup()));
+               talloc_destroy(mem_ctx);
                return WINBINDD_ERROR;
        }
 
@@ -87,47 +104,48 @@ enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state)
                 goto done;
         }
 
-       result = cli_nt_login_network(cli, user_info, smb_uid_low, 
-                                     &ctr, &info3);
+       result = cli_netlogon_sam_network_logon(cli, mem_ctx,
+                                               name_user, name_domain, 
+                                               global_myname, chal, 
+                                               lm_resp, nt_resp, 
+                                               &info3);
+        
+       uni_group_cache_store_netlogon(mem_ctx, &info3);
+done:
 
-        free_user_info(&user_info);
+       if (cli) 
+               cli_shutdown(cli);
 
-        cli_shutdown(cli);
-        
- done:
+       talloc_destroy(mem_ctx);
+       
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
-
+       
 /* Challenge Response Authentication Protocol */
 
 enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state) 
 {
        NTSTATUS result;
-       fstring name_domain, name_user;
        unsigned char trust_passwd[16];
        time_t last_change_time;
-
-       auth_usersupplied_info *user_info;
-        uint32 smb_uid_low;
         NET_USER_INFO_3 info3;
-       NET_ID_INFO_CTR ctr;
-        struct cli_state *cli;
+        struct cli_state *cli = NULL;
+       TALLOC_CTX *mem_ctx;
 
-       DEBUG(3, ("[%5d]: pam auth crap %s\n", state->pid,
-                 state->request.data.auth_crap.user));
+       DATA_BLOB lm_resp, nt_resp;
 
-       /* Parse domain and username */
+       extern pstring global_myname;
 
-       parse_domain_user(state->request.data.auth_crap.user, name_domain, 
-                          name_user);
+       DEBUG(3, ("[%5d]: pam auth crap domain: %s user: %s\n", state->pid,
+                 state->request.data.auth_crap.user, state->request.data.auth_crap.user));
 
-               make_user_info_winbind_crap(
-                &user_info, name_user, 
-                name_domain, state->request.data.auth_crap.chal,
-                (uchar *)state->request.data.auth_crap.lm_resp,
-                state->request.data.auth_crap.lm_resp_len,
-                (uchar *)state->request.data.auth_crap.nt_resp,
-                state->request.data.auth_crap.nt_resp_len);
+       if (!(mem_ctx = talloc_init_named("winbind pam auth crap for %s", state->request.data.auth.user))) {
+               DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
+               return WINBINDD_ERROR;
+       }
+
+       lm_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
+       nt_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
        
        /*
         * Get the machine account password for our primary domain
@@ -137,13 +155,10 @@ enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state)
                 lp_workgroup(), trust_passwd, &last_change_time)) {
                DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
                           "password for domain %s\n", lp_workgroup()));
+               talloc_destroy(mem_ctx);
                return WINBINDD_ERROR;
        }
 
-       /* We really don't care what LUID we give the user. */
-
-       generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
-
        ZERO_STRUCT(info3);
 
         result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd, &cli);
@@ -153,14 +168,19 @@ enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state)
                 goto done;
         }
 
-       result = cli_nt_login_network(cli, user_info, smb_uid_low, 
-                                     &ctr, &info3);
-
-        free_user_info(&user_info);
+       result = cli_netlogon_sam_network_logon(cli, mem_ctx,
+                                               state->request.data.auth_crap.user, state->request.data.auth_crap.domain, 
+                                               global_myname, state->request.data.auth_crap.chal, 
+                                               lm_resp, nt_resp, 
+                                               &info3);
+        
+       uni_group_cache_store_netlogon(mem_ctx, &info3);
+done:
+       talloc_destroy(mem_ctx);
 
-        cli_shutdown(cli);
+       if (cli)
+               cli_shutdown(cli);
 
- done:
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
@@ -168,37 +188,39 @@ enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state)
 
 enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
 {
-    char *oldpass, *newpass;
-    fstring domain, user;
-    uchar nt_oldhash[16];
-    uchar lm_oldhash[16];
+       char *oldpass, *newpass;
+       fstring domain, user;
+       uchar nt_oldhash[16];
+       uchar lm_oldhash[16];
 
-    DEBUG(3, ("[%5d]: pam chauthtok %s\n", state->pid,
-             state->request.data.chauthtok.user));
+       DEBUG(3, ("[%5d]: pam chauthtok %s\n", state->pid,
+               state->request.data.chauthtok.user));
 
-    /* Setup crap */
+       /* Setup crap */
 
-    if (state == NULL) return WINBINDD_ERROR;
+       if (state == NULL)
+               return WINBINDD_ERROR;
 
-    parse_domain_user(state->request.data.chauthtok.user, domain, user);
+       if (!parse_domain_user(state->request.data.chauthtok.user, domain, user))
+               return WINBINDD_ERROR;
 
-    oldpass = state->request.data.chauthtok.oldpass;
-    newpass = state->request.data.chauthtok.newpass;
+       oldpass = state->request.data.chauthtok.oldpass;
+       newpass = state->request.data.chauthtok.newpass;
 
-    nt_lm_owf_gen(oldpass, nt_oldhash, lm_oldhash);
+       nt_lm_owf_gen(oldpass, nt_oldhash, lm_oldhash);
 
-    /* Change password */
+       /* Change password */
 
 #if 0
 
-    /* XXX */
+       /* XXX */
 
-    if (!msrpc_sam_ntchange_pwd(server_state.controller, domain, user,
-                               lm_oldhash, nt_oldhash, newpass)) {
-        DEBUG(0, ("password change failed for user %s/%s\n", domain, user));
-        return WINBINDD_ERROR;
-    }
+       if (!msrpc_sam_ntchange_pwd(server_state.controller, domain, user,
+               lm_oldhash, nt_oldhash, newpass)) {
+               DEBUG(0, ("password change failed for user %s/%s\n", domain, user));
+               return WINBINDD_ERROR;
+       }
 #endif
     
-    return WINBINDD_OK;
+       return WINBINDD_OK;
 }