s3: Fix some debug msgs in ntlm_auth
[samba.git] / source3 / utils / ntlm_auth.c
index 57e425154385daa94c906f0146ebd45b27bcc4c9..82819cf1455f02282061dbaadcb947142a8a3777 100644 (file)
 */
 
 #include "includes.h"
+#include "popt_common.h"
 #include "utils/ntlm_auth.h"
 #include "../libcli/auth/libcli_auth.h"
 #include "../libcli/auth/spnego.h"
-#include "ntlmssp.h"
+#include "../libcli/auth/ntlmssp.h"
 #include "smb_krb5.h"
 #include <iniparser.h>
+#include "../lib/crypto/arcfour.h"
+#include "libads/kerberos_proto.h"
+#include "nsswitch/winbind_client.h"
 
 #ifndef PAM_WINBIND_CONFIG_FILE
 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
@@ -223,7 +227,7 @@ DATA_BLOB get_challenge(void)
        static DATA_BLOB chal;
        if (opt_challenge.length)
                return opt_challenge;
-       
+
        chal = data_blob(NULL, 8);
 
        generate_random_buffer(chal.data, chal.length);
@@ -242,7 +246,7 @@ static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
        if (!p) {
                return False;
        }
-        
+
        fstrcpy(user, p+1);
        fstrcpy(domain, domuser);
        domain[PTR_DIFF(p, domuser)] = 0;
@@ -300,23 +304,23 @@ int get_pam_winbind_config()
 {
        int ctrl = 0;
        dictionary *d = NULL;
-       
+
        if (!opt_pam_winbind_conf || !*opt_pam_winbind_conf) {
                opt_pam_winbind_conf = PAM_WINBIND_CONFIG_FILE;
        }
 
        d = iniparser_load(CONST_DISCARD(char *, opt_pam_winbind_conf));
-       
+
        if (!d) {
                return 0;
        }
-       
+
        if (iniparser_getboolean(d, CONST_DISCARD(char *, "global:krb5_auth"), false)) {
                ctrl |= WINBIND_KRB5_AUTH;
        }
 
        iniparser_freedict(d);
-       
+
        return ctrl;
 }
 
@@ -438,7 +442,7 @@ NTSTATUS contact_winbind_auth_crap(const char *username,
                }
                 request.data.auth_crap.nt_resp_len = nt_response->length;
        }
-       
+
        result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
        SAFE_FREE(request.extra_data.data);
 
@@ -451,7 +455,7 @@ NTSTATUS contact_winbind_auth_crap(const char *username,
                winbindd_free_response(&response);
                return nt_status;
        }
-       
+
        nt_status = (NT_STATUS(response.data.auth.nt_status));
        if (!NT_STATUS_IS_OK(nt_status)) {
                if (error_string) 
@@ -533,7 +537,7 @@ static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
                memcpy(request.data.chng_pswd_auth_crap.old_lm_hash_enc, old_lm_hash_enc.data, sizeof(request.data.chng_pswd_auth_crap.old_lm_hash_enc));
                request.data.chng_pswd_auth_crap.old_lm_hash_enc_len = old_lm_hash_enc.length;
        }
-       
+
        result = winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, &request, &response);
 
        /* Display response */
@@ -546,7 +550,7 @@ static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
                winbindd_free_response(&response);
                return nt_status;
        }
-       
+
        nt_status = (NT_STATUS(response.data.auth.nt_status));
        if (!NT_STATUS_IS_OK(nt_status))
        {
@@ -557,7 +561,7 @@ static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
        }
 
        winbindd_free_response(&response);
-       
+
     return nt_status;
 }
 
@@ -571,7 +575,7 @@ static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB
        char *unix_name = NULL;
 
        nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
-                                             ntlmssp_state->workstation,
+                                             ntlmssp_state->client.netbios_name,
                                              &ntlmssp_state->chal,
                                              &ntlmssp_state->lm_resp,
                                              &ntlmssp_state->nt_resp, 
@@ -585,19 +589,19 @@ static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB
                        memcpy(lm_session_key->data, lm_key, 8);
                        memset(lm_session_key->data+8, '\0', 8);
                }
-               
+
                if (memcmp(user_sess_key, zeros, 16) != 0) {
                        *user_session_key = data_blob_talloc(ntlmssp_state, user_sess_key, 16);
                }
-               ntlmssp_state->auth_context = talloc_strdup(ntlmssp_state,
-                                                           unix_name);
+               ntlmssp_state->callback_private = talloc_strdup(ntlmssp_state,
+                                                               unix_name);
        } else {
                DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3, 
                      ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n", 
                       ntlmssp_state->domain, ntlmssp_state->user, 
-                      ntlmssp_state->workstation, 
+                      ntlmssp_state->client.netbios_name,
                       error_string ? error_string : "unknown error (NULL)"));
-               ntlmssp_state->auth_context = NULL;
+               ntlmssp_state->callback_private = NULL;
        }
 
        SAFE_FREE(error_string);
@@ -611,7 +615,7 @@ static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *u
        struct samr_Password lm_pw, nt_pw;
 
        nt_lm_owf_gen (opt_password, nt_pw.hash, lm_pw.hash);
-       
+
        nt_status = ntlm_password_check(ntlmssp_state,
                                        true, true, 0,
                                        &ntlmssp_state->chal,
@@ -621,17 +625,18 @@ static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *u
                                        ntlmssp_state->user, 
                                        ntlmssp_state->domain,
                                        &lm_pw, &nt_pw, user_session_key, lm_session_key);
-       
+
        if (NT_STATUS_IS_OK(nt_status)) {
-               ntlmssp_state->auth_context = talloc_asprintf(ntlmssp_state,
+               ntlmssp_state->callback_private = talloc_asprintf(ntlmssp_state,
                                                              "%s%c%s", ntlmssp_state->domain, 
                                                              *lp_winbind_separator(), 
                                                              ntlmssp_state->user);
        } else {
                DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n", 
-                         ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation, 
+                         ntlmssp_state->domain, ntlmssp_state->user,
+                         ntlmssp_state->client.netbios_name,
                          nt_errstr(nt_status)));
-               ntlmssp_state->auth_context = NULL;
+               ntlmssp_state->callback_private = NULL;
        }
        return nt_status;
 }
@@ -645,12 +650,16 @@ static NTSTATUS ntlm_auth_start_ntlmssp_client(struct ntlmssp_state **client_ntl
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       status = ntlmssp_client_start(client_ntlmssp_state);
+       status = ntlmssp_client_start(NULL,
+                                     global_myname(),
+                                     lp_workgroup(),
+                                     lp_client_ntlmv2_auth(),
+                                     client_ntlmssp_state);
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Could not start NTLMSSP client: %s\n",
                          nt_errstr(status)));
-               ntlmssp_end(client_ntlmssp_state);
+               TALLOC_FREE(*client_ntlmssp_state);
                return status;
        }
 
@@ -659,7 +668,7 @@ static NTSTATUS ntlm_auth_start_ntlmssp_client(struct ntlmssp_state **client_ntl
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Could not set username: %s\n",
                          nt_errstr(status)));
-               ntlmssp_end(client_ntlmssp_state);
+               TALLOC_FREE(*client_ntlmssp_state);
                return status;
        }
 
@@ -668,17 +677,17 @@ static NTSTATUS ntlm_auth_start_ntlmssp_client(struct ntlmssp_state **client_ntl
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Could not set domain: %s\n",
                          nt_errstr(status)));
-               ntlmssp_end(client_ntlmssp_state);
+               TALLOC_FREE(*client_ntlmssp_state);
                return status;
        }
 
        if (opt_password) {
                status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
-       
+
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(1, ("Could not set password: %s\n",
                                  nt_errstr(status)));
-                       ntlmssp_end(client_ntlmssp_state);
+                       TALLOC_FREE(*client_ntlmssp_state);
                        return status;
                }
        }
@@ -688,8 +697,34 @@ static NTSTATUS ntlm_auth_start_ntlmssp_client(struct ntlmssp_state **client_ntl
 
 static NTSTATUS ntlm_auth_start_ntlmssp_server(struct ntlmssp_state **ntlmssp_state)
 {
-       NTSTATUS status = ntlmssp_server_start(ntlmssp_state);
-       
+       NTSTATUS status;
+       const char *netbios_name;
+       const char *netbios_domain;
+       const char *dns_name;
+       char *dns_domain;
+       bool is_standalone = false;
+
+       if (opt_password) {
+               netbios_name = global_myname();
+               netbios_domain = lp_workgroup();
+       } else {
+               netbios_name = get_winbind_netbios_name();
+               netbios_domain = get_winbind_domain();
+       }
+       /* This should be a 'netbios domain -> DNS domain' mapping */
+       dns_domain = get_mydnsdomname(talloc_tos());
+       if (dns_domain) {
+               strlower_m(dns_domain);
+       }
+       dns_name = get_mydnsfullname();
+
+       status = ntlmssp_server_start(NULL,
+                                     is_standalone,
+                                     netbios_name,
+                                     netbios_domain,
+                                     dns_name,
+                                     dns_domain,
+                                     ntlmssp_state);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Could not start NTLMSSP server: %s\n",
                          nt_errstr(status)));
@@ -699,12 +734,8 @@ static NTSTATUS ntlm_auth_start_ntlmssp_server(struct ntlmssp_state **ntlmssp_st
        /* Have we been given a local password, or should we ask winbind? */
        if (opt_password) {
                (*ntlmssp_state)->check_password = local_pw_check;
-               (*ntlmssp_state)->get_domain = lp_workgroup;
-               (*ntlmssp_state)->get_global_myname = global_myname;
        } else {
                (*ntlmssp_state)->check_password = winbind_pw_check;
-               (*ntlmssp_state)->get_domain = get_winbind_domain;
-               (*ntlmssp_state)->get_global_myname = get_winbind_netbios_name;
        }
        return NT_STATUS_OK;
 }
@@ -731,7 +762,7 @@ static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_m
         * child of the trusted domain. If we ask the primary domain for
         * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
         * domain's child for ccache_ntlm_auth. that is to say, we have to 
-        * set WBFALG_PAM_CONTACT_TRUSTDOM in request.flags.
+        * set WBFLAG_PAM_CONTACT_TRUSTDOM in request.flags.
         */
        ctrl = get_pam_winbind_config();
 
@@ -786,7 +817,7 @@ static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
        NTSTATUS nt_status;
 
        if (strlen(buf) < 2) {
-               DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
+               DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
                x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
                return;
        }
@@ -825,8 +856,7 @@ static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
        }
 
        if (strncmp(buf, "YR", 2) == 0) {
-               if (state->ntlmssp_state)
-                       ntlmssp_end(&state->ntlmssp_state);
+               TALLOC_FREE(state->ntlmssp_state);
                state->svr_state = SERVER_INITIAL;
        } else if (strncmp(buf, "KK", 2) == 0) {
                /* No special preprocessing required */
@@ -855,7 +885,7 @@ static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
                data_blob_free(&request);
                return;
        } else {
-               DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
+               DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
                x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
                return;
        }
@@ -888,13 +918,13 @@ static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
                x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
                DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
 
-               ntlmssp_end(&state->ntlmssp_state);
+               TALLOC_FREE(state->ntlmssp_state);
        } else if (!NT_STATUS_IS_OK(nt_status)) {
                x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
                DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
        } else {
                x_fprintf(x_stdout, "AF %s\n",
-                               (char *)state->ntlmssp_state->auth_context);
+                               (char *)state->ntlmssp_state->callback_private);
                DEBUG(10, ("NTLMSSP OK!\n"));
 
                if(state->have_session_key)
@@ -922,7 +952,7 @@ static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
        }
 
        if (strlen(buf) < 2) {
-               DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
+               DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
                x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
                return;
        }
@@ -981,8 +1011,7 @@ static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
        }
 
        if (strncmp(buf, "YR", 2) == 0) {
-               if (state->ntlmssp_state)
-                       ntlmssp_end(&state->ntlmssp_state);
+               TALLOC_FREE(state->ntlmssp_state);
                state->cli_state = CLIENT_INITIAL;
        } else if (strncmp(buf, "TT", 2) == 0) {
                /* No special preprocessing required */
@@ -1014,7 +1043,7 @@ static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
                data_blob_free(&request);
                return;
        } else {
-               DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
+               DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
                x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
                return;
        }
@@ -1073,14 +1102,12 @@ static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
 
                DEBUG(10, ("NTLMSSP OK!\n"));
                state->cli_state = CLIENT_FINISHED;
-               if (state->ntlmssp_state)
-                       ntlmssp_end(&state->ntlmssp_state);
+               TALLOC_FREE(state->ntlmssp_state);
        } else {
                x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
                DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
                state->cli_state = CLIENT_ERROR;
-               if (state->ntlmssp_state)
-                       ntlmssp_end(&state->ntlmssp_state);
+               TALLOC_FREE(state->ntlmssp_state);
        }
 
        data_blob_free(&request);
@@ -1091,7 +1118,7 @@ static void manage_squid_basic_request(struct ntlm_auth_state *state,
 {
        char *user, *pass;      
        user=buf;
-       
+
        pass=(char *)memchr(buf,' ',length);
        if (!pass) {
                DEBUG(2, ("Password not found. Denying access\n"));
@@ -1100,12 +1127,12 @@ static void manage_squid_basic_request(struct ntlm_auth_state *state,
        }
        *pass='\0';
        pass++;
-       
+
        if (state->helper_mode == SQUID_2_5_BASIC) {
                rfc1738_unescape(user);
                rfc1738_unescape(pass);
        }
-       
+
        if (check_plaintext_auth(user, pass, False)) {
                x_fprintf(x_stdout, "OK\n");
        } else {
@@ -1138,18 +1165,18 @@ static void offer_gss_spnego_mechs(void) {
 
        /* Server negTokenInit (mech offerings) */
        spnego.type = SPNEGO_NEG_TOKEN_INIT;
-       spnego.negTokenInit.mechTypes = SMB_XMALLOC_ARRAY(const char *, 2);
+       spnego.negTokenInit.mechTypes = talloc_array(ctx, const char *, 3);
 #ifdef HAVE_KRB5
-       spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
-       spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
+       spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_KERBEROS5_OLD);
+       spnego.negTokenInit.mechTypes[1] = talloc_strdup(ctx, OID_NTLMSSP);
        spnego.negTokenInit.mechTypes[2] = NULL;
 #else
-       spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
+       spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_NTLMSSP);
        spnego.negTokenInit.mechTypes[1] = NULL;
 #endif
 
 
-       spnego.negTokenInit.mechListMIC = data_blob(principal,
+       spnego.negTokenInit.mechListMIC = data_blob_talloc(ctx, principal,
                                                    strlen(principal));
 
        len = spnego_write_data(ctx, &token, &spnego);
@@ -1188,18 +1215,17 @@ static void manage_gss_spnego_request(struct ntlm_auth_state *state,
        char *reply_argument = NULL;
 
        if (strlen(buf) < 2) {
-               DEBUG(1, ("SPENGO query [%s] invalid", buf));
+               DEBUG(1, ("SPENGO query [%s] invalid\n", buf));
                x_fprintf(x_stdout, "BH SPENGO query invalid\n");
                return;
        }
 
        if (strncmp(buf, "YR", 2) == 0) {
-               if (ntlmssp_state)
-                       ntlmssp_end(&ntlmssp_state);
+               TALLOC_FREE(ntlmssp_state);
        } else if (strncmp(buf, "KK", 2) == 0) {
                ;
        } else {
-               DEBUG(1, ("SPENGO query [%s] invalid", buf));
+               DEBUG(1, ("SPENGO query [%s] invalid\n", buf));
                x_fprintf(x_stdout, "BH SPENGO query invalid\n");
                return;
        }
@@ -1226,7 +1252,7 @@ static void manage_gss_spnego_request(struct ntlm_auth_state *state,
        data_blob_free(&token);
 
        if (len == -1) {
-               DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
+               DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
                x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
                return;
        }
@@ -1238,7 +1264,7 @@ static void manage_gss_spnego_request(struct ntlm_auth_state *state,
 
                if ( (request.negTokenInit.mechTypes == NULL) ||
                     (request.negTokenInit.mechTypes[0] == NULL) ) {
-                       DEBUG(1, ("Client did not offer any mechanism"));
+                       DEBUG(1, ("Client did not offer any mechanism\n"));
                        x_fprintf(x_stdout, "BH Client did not offer any "
                                            "mechanism\n");
                        return;
@@ -1260,7 +1286,7 @@ static void manage_gss_spnego_request(struct ntlm_auth_state *state,
                                x_fprintf(x_stdout, "BH Client wants a new "
                                                    "NTLMSSP challenge, but "
                                                    "already got one\n");
-                               ntlmssp_end(&ntlmssp_state);
+                               TALLOC_FREE(ntlmssp_state);
                                return;
                        }
 
@@ -1274,8 +1300,8 @@ static void manage_gss_spnego_request(struct ntlm_auth_state *state,
                                  request.negTokenInit.mechToken.length);
 
                        response.type = SPNEGO_NEG_TOKEN_TARG;
-                       response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
-                       response.negTokenTarg.mechListMIC = data_blob_null;
+                       response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_NTLMSSP);
+                       response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
 
                        status = ntlmssp_update(ntlmssp_state,
                                                       request.negTokenInit.mechToken,
@@ -1289,7 +1315,7 @@ static void manage_gss_spnego_request(struct ntlm_auth_state *state,
                        char *principal;
                        DATA_BLOB ap_rep;
                        DATA_BLOB session_key;
-                       struct PAC_DATA *pac_data = NULL;
+                       struct PAC_LOGON_INFO *logon_info = NULL;
 
                        if ( request.negTokenInit.mechToken.data == NULL ) {
                                DEBUG(1, ("Client did not provide Kerberos data\n"));
@@ -1299,13 +1325,13 @@ static void manage_gss_spnego_request(struct ntlm_auth_state *state,
                        }
 
                        response.type = SPNEGO_NEG_TOKEN_TARG;
-                       response.negTokenTarg.supportedMech = SMB_STRDUP(OID_KERBEROS5_OLD);
-                       response.negTokenTarg.mechListMIC = data_blob_null;
-                       response.negTokenTarg.responseToken = data_blob_null;
+                       response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_KERBEROS5_OLD);
+                       response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
+                       response.negTokenTarg.responseToken = data_blob_talloc(ctx, NULL, 0);
 
                        status = ads_verify_ticket(mem_ctx, lp_realm(), 0,
                                                   &request.negTokenInit.mechToken,
-                                                  &principal, &pac_data, &ap_rep,
+                                                  &principal, &logon_info, &ap_rep,
                                                   &session_key, True);
 
                        /* Now in "principal" we have the name we are
@@ -1360,13 +1386,13 @@ static void manage_gss_spnego_request(struct ntlm_auth_state *state,
                                               &response.negTokenTarg.responseToken);
 
                response.type = SPNEGO_NEG_TOKEN_TARG;
-               response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
-               response.negTokenTarg.mechListMIC = data_blob_null;
+               response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_NTLMSSP);
+               response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
 
                if (NT_STATUS_IS_OK(status)) {
                        user = SMB_STRDUP(ntlmssp_state->user);
                        domain = SMB_STRDUP(ntlmssp_state->domain);
-                       ntlmssp_end(&ntlmssp_state);
+                       TALLOC_FREE(ntlmssp_state);
                }
        }
 
@@ -1467,7 +1493,7 @@ static bool manage_client_ntlmssp_init(struct spnego_data spnego)
                        NT_STATUS_IS_OK(status)) ) {
                DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
                          nt_errstr(status)));
-               ntlmssp_end(&client_ntlmssp_state);
+               TALLOC_FREE(client_ntlmssp_state);
                return False;
        }
 
@@ -1500,20 +1526,20 @@ static void manage_client_ntlmssp_targ(struct spnego_data spnego)
 
        if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
                x_fprintf(x_stdout, "NA\n");
-               ntlmssp_end(&client_ntlmssp_state);
+               TALLOC_FREE(client_ntlmssp_state);
                return;
        }
 
        if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
                x_fprintf(x_stdout, "AF\n");
-               ntlmssp_end(&client_ntlmssp_state);
+               TALLOC_FREE(client_ntlmssp_state);
                return;
        }
 
        status = ntlmssp_update(client_ntlmssp_state,
                                       spnego.negTokenTarg.responseToken,
                                       &request);
-               
+
        if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
                DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
                          "ntlmssp_client_update, got: %s\n",
@@ -1521,7 +1547,7 @@ static void manage_client_ntlmssp_targ(struct spnego_data spnego)
                x_fprintf(x_stdout, "BH Expected MORE_PROCESSING_REQUIRED from "
                                    "ntlmssp_client_update\n");
                data_blob_free(&request);
-               ntlmssp_end(&client_ntlmssp_state);
+               TALLOC_FREE(client_ntlmssp_state);
                return;
        }
 
@@ -1530,7 +1556,7 @@ static void manage_client_ntlmssp_targ(struct spnego_data spnego)
        spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
        spnego.negTokenTarg.responseToken = request;
        spnego.negTokenTarg.mechListMIC = null_blob;
-       
+
        spnego_write_data(ctx, &to_server, &spnego);
        data_blob_free(&request);
 
@@ -1574,8 +1600,9 @@ static bool manage_client_krb5_init(struct spnego_data spnego)
               spnego.negTokenInit.mechListMIC.length);
        principal[spnego.negTokenInit.mechListMIC.length] = '\0';
 
-       retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
-
+       retval = cli_krb5_get_ticket(ctx, principal, 0,
+                                         &tkt, &session_key_krb5,
+                                         0, NULL, NULL, NULL);
        if (retval) {
                char *user = NULL;
 
@@ -1598,8 +1625,9 @@ static bool manage_client_krb5_init(struct spnego_data spnego)
                        return False;
                }
 
-               retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
-
+               retval = cli_krb5_get_ticket(ctx, principal, 0,
+                                                 &tkt, &session_key_krb5,
+                                                 0, NULL, NULL, NULL);
                if (retval) {
                        DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
                        return False;
@@ -1684,7 +1712,7 @@ static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
                /* We asked for a password and obviously got it :-) */
 
                opt_password = SMB_STRNDUP((const char *)request.data, request.length);
-               
+
                if (opt_password == NULL) {
                        DEBUG(1, ("Out of memory\n"));
                        x_fprintf(x_stdout, "BH Out of memory\n");
@@ -1770,7 +1798,7 @@ static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
                                                    "negResult\n");
                        }
 
-                       ntlmssp_end(&client_ntlmssp_state);
+                       TALLOC_FREE(client_ntlmssp_state);
                        goto out;
                }
 
@@ -1812,7 +1840,7 @@ static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
        static char *plaintext_password;
        static bool ntlm_server_1_user_session_key;
        static bool ntlm_server_1_lm_session_key;
-       
+
        if (strequal(buf, ".")) {
                if (!full_username && !username) {      
                        x_fprintf(x_stdout, "Error: No username supplied!\n");
@@ -1842,7 +1870,7 @@ static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
                        if (full_username && !username) {
                                fstring fstr_user;
                                fstring fstr_domain;
-                               
+
                                if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
                                        /* username might be 'tainted', don't print into our new-line deleimianted stream */
                                        x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
@@ -1859,7 +1887,7 @@ static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
 
                        if (ntlm_server_1_lm_session_key) 
                                flags |= WBFLAG_PAM_LMKEY;
-                       
+
                        if (ntlm_server_1_user_session_key) 
                                flags |= WBFLAG_PAM_USER_SESSION_KEY;
 
@@ -1928,13 +1956,13 @@ static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
        parameter = strstr_m(request, ":: ");
        if (!parameter) {
                parameter = strstr_m(request, ": ");
-               
+
                if (!parameter) {
                        DEBUG(0, ("Parameter not found!\n"));
                        x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
                        return;
                }
-               
+
                parameter[0] ='\0';
                parameter++;
                parameter[0] ='\0';
@@ -2015,7 +2043,7 @@ static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
 
                        new_nt_pswd = data_blob(NULL, 516);
                        old_nt_hash_enc = data_blob(NULL, 16);
-                       
+
                        /* Calculate the MD4 hash (NT compatible) of the
                         * password */
                        E_md4hash(oldpswd, old_nt_hash);
@@ -2023,7 +2051,7 @@ static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
 
                        /* E_deshash returns false for 'long'
                           passwords (> 14 DOS chars).  
-                          
+
                           Therefore, don't send a buffer
                           encrypted with the truncated hash
                           (it could allow an even easier
@@ -2052,12 +2080,12 @@ static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
 
                        encode_pw_buffer(new_nt_pswd.data, newpswd,
                                         STR_UNICODE);
-       
+
                        arcfour_crypt(new_nt_pswd.data, old_nt_hash, 516);
                        E_old_pw_hash(new_nt_hash, old_nt_hash,
                                      old_nt_hash_enc.data);
                }
-               
+
                if (!full_username && !username) {      
                        x_fprintf(x_stdout, "Error: No username supplied!\n");
                } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
@@ -2066,11 +2094,11 @@ static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
                                  "blobs supplied!\n");
                } else {
                        char *error_string = NULL;
-                       
+
                        if (full_username && !username) {
                                fstring fstr_user;
                                fstring fstr_domain;
-                               
+
                                if (!parse_ntlm_auth_domain_user(full_username,
                                                                 fstr_user,
                                                                 fstr_domain)) {
@@ -2088,7 +2116,7 @@ static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
                                        username = smb_xstrdup(fstr_user);
                                        domain = smb_xstrdup(fstr_domain);
                                }
-                               
+
                        }
 
                        if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
@@ -2128,13 +2156,13 @@ static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
        parameter = strstr_m(request, ":: ");
        if (!parameter) {
                parameter = strstr_m(request, ": ");
-               
+
                if (!parameter) {
                        DEBUG(0, ("Parameter not found!\n"));
                        x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
                        return;
                }
-               
+
                parameter[0] ='\0';
                parameter++;
                parameter[0] ='\0';
@@ -2408,7 +2436,9 @@ enum {
                { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
                { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
                { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
-               { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
+               { "diagnostics", 0, POPT_ARG_NONE, &diagnostics,
+                 OPT_DIAGNOSTICS,
+                 "Perform diagnostics on the authentication chain"},
                { "require-membership-of", 0, POPT_ARG_STRING, &require_membership_of, OPT_REQUIRE_MEMBERSHIP, "Require that a user be a member of this group (either name or SID) for authentication to succeed" },
                { "pam-winbind-conf", 0, POPT_ARG_STRING, &opt_pam_winbind_conf, OPT_PAM_WINBIND_CONF, "Require that request must set WBFLAG_PAM_CONTACT_TRUSTDOM when krb5 auth is required" },
                POPT_COMMON_CONFIGFILE