r18665: Remove two type-punned warnings
[tprouty/samba.git] / source / auth / auth_server.c
index 7178e3147c4fcfba7302dd3d42bc255dfa08001c..7ffea1ca11b04a5724db262fd25dd0fbddfe04f6 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    Authenticate to a remote server
    Copyright (C) Andrew Tridgell 1992-1998
    Copyright (C) Andrew Bartlett 2001
@@ -22,7 +21,9 @@
 
 #include "includes.h"
 
-extern pstring global_myname;
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_AUTH
+
 extern userdom_struct current_user_info;
 
 /****************************************************************************
@@ -34,10 +35,11 @@ static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
        struct cli_state *cli = NULL;
        fstring desthost;
        struct in_addr dest_ip;
-       char *p, *pserver;
+       const char *p;
+       char *pserver;
        BOOL connected_ok = False;
 
-       if (!(cli = cli_initialise(cli)))
+       if (!(cli = cli_initialise()))
                return NULL;
 
        /* security = server just can't function with spnego */
@@ -47,8 +49,9 @@ static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
        p = pserver;
 
         while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
-               standard_sub_basic(current_user_info.smb_name, desthost);
-               strupper(desthost);
+               standard_sub_basic(current_user_info.smb_name, current_user_info.domain,
+                                  desthost, sizeof(desthost));
+               strupper_m(desthost);
 
                if(!resolve_name( desthost, &dest_ip, 0x20)) {
                        DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
@@ -60,6 +63,15 @@ static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
                        continue;
                }
 
+               /* we use a mutex to prevent two connections at once - when a 
+                  Win2k PDC get two connections where one hasn't completed a 
+                  session setup yet it will send a TCP reset to the first 
+                  connection (tridge) */
+
+               if (!grab_server_mutex(desthost)) {
+                       return NULL;
+               }
+
                if (cli_connect(cli, desthost, &dest_ip)) {
                        DEBUG(3,("connected to password server %s\n",desthost));
                        connected_ok = True;
@@ -68,13 +80,19 @@ static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
        }
 
        if (!connected_ok) {
+               release_server_mutex();
                DEBUG(0,("password server not available\n"));
                cli_shutdown(cli);
                return NULL;
        }
-
-       if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
+       
+       if (!attempt_netbios_session_request(&cli, global_myname(), 
+                                            desthost, &dest_ip)) {
+               release_server_mutex();
+               DEBUG(1,("password server fails session request\n"));
+               cli_shutdown(cli);
                return NULL;
+       }
        
        if (strequal(desthost,myhostname())) {
                exit_server("Password server loop!");
@@ -84,19 +102,37 @@ static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
 
        if (!cli_negprot(cli)) {
                DEBUG(1,("%s rejected the negprot\n",desthost));
+               release_server_mutex();
                cli_shutdown(cli);
                return NULL;
        }
 
        if (cli->protocol < PROTOCOL_LANMAN2 ||
-           !(cli->sec_mode & 1)) {
+           !(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
                DEBUG(1,("%s isn't in user level security mode\n",desthost));
+               release_server_mutex();
                cli_shutdown(cli);
                return NULL;
        }
 
-       DEBUG(3,("password server OK\n"));
+       /* Get the first session setup done quickly, to avoid silly 
+          Win2k bugs.  (The next connection to the server will kill
+          this one... 
+       */
+
+       if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 0, "", 0,
+                                              ""))) {
+               DEBUG(0,("%s rejected the initial session setup (%s)\n",
+                        desthost, cli_errstr(cli)));
+               release_server_mutex();
+               cli_shutdown(cli);
+               return NULL;
+       }
+       
+       release_server_mutex();
 
+       DEBUG(3,("password server OK\n"));
+       
        return cli;
 }
 
@@ -108,8 +144,10 @@ static void free_server_private_data(void **private_data_pointer)
 {
        struct cli_state **cli = (struct cli_state **)private_data_pointer;
        if (*cli && (*cli)->initialised) {
+               DEBUG(10, ("Shutting down smbserver connection\n"));
                cli_shutdown(*cli);
        }
+       *private_data_pointer = NULL;
 }
 
 /****************************************************************************
@@ -118,14 +156,16 @@ static void free_server_private_data(void **private_data_pointer)
 
 static void send_server_keepalive(void **private_data_pointer) 
 {
-       struct cli_state **cli = (struct cli_state **)private_data_pointer;
-       
        /* also send a keepalive to the password server if its still
           connected */
-       if (cli && *cli && (*cli)->initialised) {
-               if (!send_keepalive((*cli)->fd)) {
-                       DEBUG( 2, ( "password server keepalive failed.\n"));
-                       cli_shutdown(*cli);
+       if (private_data_pointer) {
+               struct cli_state *cli = (struct cli_state *)(*private_data_pointer);
+               if (cli && cli->initialised) {
+                       if (!send_keepalive(cli->fd)) {
+                               DEBUG( 2, ( "send_server_keepalive: password server keepalive failed.\n"));
+                               cli_shutdown(cli);
+                               *private_data_pointer = NULL;
+                       }
                }
        }
 }
@@ -134,16 +174,16 @@ static void send_server_keepalive(void **private_data_pointer)
  Get the challenge out of a password server.
 ****************************************************************************/
 
-static DATA_BLOB auth_get_challenge_server(void **my_private_data, 
-                                          TALLOC_CTX *mem_ctx,
-                                          const struct authsupplied_info *auth_info) 
+static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
+                                          void **my_private_data, 
+                                          TALLOC_CTX *mem_ctx)
 {
        struct cli_state *cli = server_cryptkey(mem_ctx);
        
        if (cli) {
                DEBUG(3,("using password server validation\n"));
 
-               if ((cli->sec_mode & 2) == 0) {
+               if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
                        /* We can't work with unencrypted password servers
                           unless 'encrypt passwords = no' */
                        DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
@@ -161,8 +201,10 @@ static DATA_BLOB auth_get_challenge_server(void **my_private_data,
                }
 
                *my_private_data = (void *)cli;
-               
-               return data_blob(cli->secblob.data,8);
+
+               /* The return must be allocated on the caller's mem_ctx, as our own will be
+                  destoyed just after the call. */
+               return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
        } else {
                return data_blob(NULL, 0);
        }
@@ -174,10 +216,10 @@ static DATA_BLOB auth_get_challenge_server(void **my_private_data,
   - Validate a password with the password server.
 ****************************************************************************/
 
-static NTSTATUS check_smbserver_security(void *my_private_data, 
+static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
+                                        void *my_private_data, 
                                         TALLOC_CTX *mem_ctx,
                                         const auth_usersupplied_info *user_info, 
-                                        const auth_authsupplied_info *auth_info,
                                         auth_serversupplied_info **server_info)
 {
        struct cli_state *cli;
@@ -185,7 +227,7 @@ static NTSTATUS check_smbserver_security(void *my_private_data,
        static fstring baduser; 
        static BOOL tested_password_server = False;
        static BOOL bad_password_server = False;
-       NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
+       NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
        BOOL locally_made_cli = False;
 
        /* 
@@ -194,12 +236,12 @@ static NTSTATUS check_smbserver_security(void *my_private_data,
         * password file.
         */
 
-       if(is_netbios_alias_or_name(user_info->domain.str)) {
+       if(is_myname(user_info->domain)) {
                DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
-               return NT_STATUS_LOGON_FAILURE;
+               return nt_status;
        }
 
-       cli = my_private_data;
+       cli = (struct cli_state *)my_private_data;
        
        if (cli) {
        } else {
@@ -212,13 +254,13 @@ static NTSTATUS check_smbserver_security(void *my_private_data,
                return NT_STATUS_LOGON_FAILURE;
        }  
        
-       if ((cli->sec_mode & 2) == 0) {
+       if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
                if (user_info->encrypted) {
                        DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
                        return NT_STATUS_LOGON_FAILURE;         
                }
        } else {
-               if (memcmp(cli->secblob.data, auth_info->challenge.data, 8) != 0) {
+               if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
                        DEBUG(1,("the challenge that the password server (%s) supplied us is not the one we gave our client. This just can't work :-(\n", cli->desthost));
                        return NT_STATUS_LOGON_FAILURE;         
                }
@@ -238,7 +280,7 @@ static NTSTATUS check_smbserver_security(void *my_private_data,
 
        if(baduser[0] == 0) {
                fstrcpy(baduser, INVALID_USER_PREFIX);
-               fstrcat(baduser, global_myname);
+               fstrcat(baduser, global_myname());
        }
 
        /*
@@ -248,14 +290,18 @@ static NTSTATUS check_smbserver_security(void *my_private_data,
         * need to detect this as some versions of NT4.x are broken. JRA.
         */
 
-       /* I sure as hell hope that there arn't servers out there that take 
+       /* I sure as hell hope that there aren't servers out there that take 
         * NTLMv2 and have this bug, as we don't test for that... 
         *  - abartlet@samba.org
         */
 
        if ((!tested_password_server) && (lp_paranoid_server_security())) {
-               if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass), 
-                                       (char *)badpass, sizeof(badpass), user_info->domain.str)) {
+               if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
+                                                     (char *)badpass,
+                                                     sizeof(badpass), 
+                                                     (char *)badpass,
+                                                     sizeof(badpass),
+                                                     user_info->domain))) {
 
                        /*
                         * We connected to the password server so we
@@ -301,30 +347,25 @@ use this machine as the password server.\n"));
 
        if (!user_info->encrypted) {
                /* Plaintext available */
-               if (!cli_session_setup(cli, user_info->smb_name.str, 
-                                      (char *)user_info->plaintext_password.data, 
-                                      user_info->plaintext_password.length, 
-                                      NULL, 0,
-                                      user_info->domain.str)) {
-                       DEBUG(1,("password server %s rejected the password\n", cli->desthost));
-                       /* Make this cli_nt_error() when the conversion is in */
-                       nt_status = cli_nt_error(cli);
-               } else {
-                       nt_status = NT_STATUS_OK;
-               }
+               nt_status = cli_session_setup(
+                       cli, user_info->smb_name, 
+                       (char *)user_info->plaintext_password.data, 
+                       user_info->plaintext_password.length, 
+                       NULL, 0, user_info->domain);
+
        } else {
-               if (!cli_session_setup(cli, user_info->smb_name.str, 
-                                      (char *)user_info->lm_resp.data
-                                      user_info->lm_resp.length
-                                      (char *)user_info->nt_resp.data
-                                      user_info->nt_resp.length
-                                      user_info->domain.str)) {
-                       DEBUG(1,("password server %s rejected the password\n", cli->desthost));
-                       /* Make this cli_nt_error() when the conversion is in */
-                       nt_status = cli_nt_error(cli);
-               } else {
-                       nt_status = NT_STATUS_OK;
-               }
+               nt_status = cli_session_setup(
+                       cli, user_info->smb_name
+                       (char *)user_info->lm_resp.data
+                       user_info->lm_resp.length
+                       (char *)user_info->nt_resp.data
+                       user_info->nt_resp.length, 
+                       user_info->domain);
+       }
+
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               DEBUG(1,("password server %s rejected the password: %s\n",
+                        cli->desthost, nt_errstr(nt_status)));
        }
 
        /* if logged in as guest then reject */
@@ -335,13 +376,18 @@ use this machine as the password server.\n"));
 
        cli_ulogoff(cli);
 
-       if NT_STATUS_IS_OK(nt_status) {
-               struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
-               if (pass) {
-                       if (!make_server_info_pw(server_info, pass)) { 
-                               nt_status = NT_STATUS_NO_MEMORY;
-                       }
-               } else {
+       if (NT_STATUS_IS_OK(nt_status)) {
+               fstring real_username;
+               struct passwd *pass;
+
+               if ( (pass = smb_getpwnam( NULL, user_info->internal_username, 
+                       real_username, True )) != NULL ) 
+               {
+                       nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
+                       TALLOC_FREE(pass);
+               }
+               else
+               {
                        nt_status = NT_STATUS_NO_SUCH_USER;
                }
        }
@@ -353,14 +399,20 @@ use this machine as the password server.\n"));
        return(nt_status);
 }
 
-BOOL auth_init_smbserver(auth_methods **auth_method) 
+static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
 {
-       if (!make_auth_methods(auth_method)) {
-               return False;
+       if (!make_auth_methods(auth_context, auth_method)) {
+               return NT_STATUS_NO_MEMORY;
        }
+       (*auth_method)->name = "smbserver";
        (*auth_method)->auth = check_smbserver_security;
        (*auth_method)->get_chal = auth_get_challenge_server;
        (*auth_method)->send_keepalive = send_server_keepalive;
        (*auth_method)->free_private_data = free_server_private_data;
-       return True;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS auth_server_init(void)
+{
+       return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);
 }