Replace sid_string_static by sid_string_dbg in DEBUGs
[amitay/samba.git] / source3 / rpc_server / srv_netlog_nt.c
index a71d97ada783864ea8d96d75a4f06aefcdf865e5..218ce734440482b4e277495b5c7d2e9c8618e9a9 100644 (file)
@@ -9,7 +9,7 @@
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation; either version 3 of the License, or
  *  (at your option) any later version.
  *  
  *  This program is distributed in the hope that it will be useful,
@@ -18,8 +18,7 @@
  *  GNU General Public License for more details.
  *  
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /* This is the implementation of the netlogon pipe. */
@@ -75,22 +74,9 @@ Send a message to smbd to do a sam synchronisation
 
 static void send_sync_message(void)
 {
-        TDB_CONTEXT *tdb;
-
-        tdb = tdb_open_log(lock_path("connections.tdb"), 0,
-                           TDB_DEFAULT, O_RDONLY, 0);
-
-        if (!tdb) {
-                DEBUG(3, ("send_sync_message(): failed to open connections "
-                          "database\n"));
-                return;
-        }
-
         DEBUG(3, ("sending sam synchronisation message\n"));
-        
-        message_send_all(tdb, MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
-
-        tdb_close(tdb);
+        message_send_all(smbd_messaging_context(), MSG_SMB_SAM_SYNC, NULL, 0,
+                        NULL);
 }
 
 /*************************************************************************
@@ -104,7 +90,7 @@ NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_C
         uint32 logon_attempts = 0x0;
         uint32 tc_status;
        fstring servername, domain, dc_name, dc_name2;
-       struct in_addr dc_ip;
+       struct sockaddr_storage dc_ss;
 
        /* this should be \\global_myname() */
        unistr2_to_ascii(servername, &q_u->uni_server_name, sizeof(servername));
@@ -121,7 +107,7 @@ NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_C
                        if ( !is_trusted_domain( domain ) )
                                break;
                                
-                       if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
+                       if ( !get_dc_name( domain, NULL, dc_name2, &dc_ss ) ) {
                                tc_status = ERROR_NO_LOGON_SERVERS;
                                break;
                        }
@@ -138,7 +124,7 @@ NTSTATUS _net_logon_ctrl2(pipes_struct *p, NET_Q_LOGON_CTRL2 *q_u, NET_R_LOGON_C
                        if ( !is_trusted_domain( domain ) )
                                break;
                                
-                       if ( !get_dc_name( domain, NULL, dc_name2, &dc_ip ) ) {
+                       if ( !get_dc_name( domain, NULL, dc_name2, &dc_ss ) ) {
                                tc_status = ERROR_NO_LOGON_SERVERS;
                                break;
                        }
@@ -204,14 +190,16 @@ static void init_net_r_srv_pwset(NET_R_SRV_PWSET *r_s,
  gets a machine password entry.  checks access rights of the host.
  ******************************************************************/
 
-static BOOL get_md4pw(char *md4pw, char *mach_acct)
+static NTSTATUS get_md4pw(char *md4pw, char *mach_acct, uint16 sec_chan_type)
 {
        struct samu *sampass = NULL;
        const uint8 *pass;
-       BOOL ret;
+       bool ret;
        uint32 acct_ctrl;
-
+       
 #if 0
+       char addr[INET6_ADDRSTRLEN];
+
     /*
      * Currently this code is redundent as we already have a filter
      * by hostname list. What this code really needs to do is to 
@@ -222,43 +210,86 @@ static BOOL get_md4pw(char *md4pw, char *mach_acct)
      */
 
        if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
-                         client_name(), client_addr()))
-       {
+                       client_name(get_client_fd()),
+                       client_addr(get_client_fd(),addr,sizeof(addr)))) {
                DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
                return False;
        }
 #endif /* 0 */
 
        if ( !(sampass = samu_new( NULL )) ) {
-               return False;
+               return NT_STATUS_NO_MEMORY;
        }
 
        /* JRA. This is ok as it is only used for generating the challenge. */
        become_root();
-       ret=pdb_getsampwnam(sampass, mach_acct);
+       ret = pdb_getsampwnam(sampass, mach_acct);
        unbecome_root();
  
-       if (ret==False) {
+       if (!ret) {
                DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
                TALLOC_FREE(sampass);
-               return False;
+               return NT_STATUS_ACCESS_DENIED;
        }
 
        acct_ctrl = pdb_get_acct_ctrl(sampass);
-       if (!(acct_ctrl & ACB_DISABLED) &&
-           ((acct_ctrl & ACB_DOMTRUST) ||
-            (acct_ctrl & ACB_WSTRUST) ||
-            (acct_ctrl & ACB_SVRTRUST)) &&
-           ((pass=pdb_get_nt_passwd(sampass)) != NULL)) {
-               memcpy(md4pw, pass, 16);
-               dump_data(5, md4pw, 16);
-               TALLOC_FREE(sampass);
-               return True;
+       if (acct_ctrl & ACB_DISABLED) {
+               DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
+               TALLOC_FREE(sampass);
+               return NT_STATUS_ACCOUNT_DISABLED;
        }
-       
-       DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
+
+       if (!(acct_ctrl & ACB_SVRTRUST) &&
+           !(acct_ctrl & ACB_WSTRUST) &&
+           !(acct_ctrl & ACB_DOMTRUST)) 
+       {
+               DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
+               TALLOC_FREE(sampass);
+               return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+       }
+
+       switch (sec_chan_type) {
+               case SEC_CHAN_BDC:
+                       if (!(acct_ctrl & ACB_SVRTRUST)) {
+                               DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
+                                        "but not a server trust account\n", mach_acct));
+                               TALLOC_FREE(sampass);
+                               return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+                       }
+                       break;
+               case SEC_CHAN_WKSTA:
+                       if (!(acct_ctrl & ACB_WSTRUST)) {
+                               DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
+                                        "but not a workstation trust account\n", mach_acct));
+                               TALLOC_FREE(sampass);
+                               return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+                       }
+                       break;
+               case SEC_CHAN_DOMAIN:
+                       if (!(acct_ctrl & ACB_DOMTRUST)) {
+                               DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
+                                        "but not a interdomain trust account\n", mach_acct));
+                               TALLOC_FREE(sampass);
+                               return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+                       }
+                       break;
+               default:
+                       break;
+       }
+
+       if ((pass = pdb_get_nt_passwd(sampass)) == NULL) {
+               DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
+               TALLOC_FREE(sampass);
+               return NT_STATUS_LOGON_FAILURE;
+       }
+
+       memcpy(md4pw, pass, 16);
+       dump_data(5, (uint8 *)md4pw, 16);
+
        TALLOC_FREE(sampass);
-       return False;
+       
+       return NT_STATUS_OK;
+       
 
 }
 
@@ -313,6 +344,7 @@ static void init_net_r_auth(NET_R_AUTH *r_a, DOM_CHAL *resp_cred, NTSTATUS statu
 
 NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
 {
+       NTSTATUS status;
        fstring mach_acct;
        fstring remote_machine;
        DOM_CHAL srv_chal_out;
@@ -326,11 +358,13 @@ NTSTATUS _net_auth(pipes_struct *p, NET_Q_AUTH *q_u, NET_R_AUTH *r_u)
        rpcstr_pull(remote_machine, q_u->clnt_id.uni_comp_name.buffer,sizeof(fstring),
                                q_u->clnt_id.uni_comp_name.uni_str_len*2,0);
 
-       if (!get_md4pw((char *)p->dc->mach_pw, mach_acct)) {
+       status = get_md4pw((char *)p->dc->mach_pw, mach_acct, q_u->clnt_id.sec_chan);
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("_net_auth: creds_server_check failed. Failed to "
-                       "get pasword for machine account %s "
-                       "from client %s\n",
-                       mach_acct, remote_machine ));
+                       "get password for machine account %s "
+                       "from client %s: %s\n",
+                       mach_acct, remote_machine, nt_errstr(status) ));
+               /* always return NT_STATUS_ACCESS_DENIED */
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -379,6 +413,7 @@ static void init_net_r_auth_2(NET_R_AUTH_2 *r_a,
 
 NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
 {
+       NTSTATUS status;
        NEG_FLAGS srv_flgs;
        fstring mach_acct;
        fstring remote_machine;
@@ -407,10 +442,12 @@ NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
                return NT_STATUS_ACCESS_DENIED;
        }
 
-       if (!get_md4pw((char *)p->dc->mach_pw, mach_acct)) {
+       status = get_md4pw((char *)p->dc->mach_pw, mach_acct, q_u->clnt_id.sec_chan);
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("_net_auth2: failed to get machine password for "
-                       "account %s\n",
-                       mach_acct ));
+                       "account %s: %s\n",
+                       mach_acct, nt_errstr(status) ));
+               /* always return NT_STATUS_ACCESS_DENIED */
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -461,10 +498,9 @@ NTSTATUS _net_auth_2(pipes_struct *p, NET_Q_AUTH_2 *q_u, NET_R_AUTH_2 *r_u)
 
 NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *r_u)
 {
-       NTSTATUS status = NT_STATUS_ACCESS_DENIED;
        fstring remote_machine;
        struct samu *sampass=NULL;
-       BOOL ret = False;
+       bool ret = False;
        unsigned char pwd[16];
        int i;
        uint32 acct_ctrl;
@@ -514,26 +550,30 @@ NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *
        }
 
        /* We must store the creds state after an update. */
+       sampass = samu_new( NULL );
+       if (!sampass) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
        become_root();
        secrets_store_schannel_session_info(p->pipe_state_mem_ctx,
                                                remote_machine,
                                                p->dc);
-       if ( (sampass = samu_new( NULL )) != NULL ) {
-               ret = pdb_getsampwnam(sampass, p->dc->mach_acct);
-       }
+       ret = pdb_getsampwnam(sampass, p->dc->mach_acct);
        unbecome_root();
 
-       if ( !sampass ) 
-               return NT_STATUS_NO_MEMORY;
+       if (!ret) {
+               TALLOC_FREE(sampass);
+               return NT_STATUS_ACCESS_DENIED;
+       }
 
        /* Ensure the account exists and is a machine account. */
        
        acct_ctrl = pdb_get_acct_ctrl(sampass);
 
-       if (!(ret 
-             && (acct_ctrl & ACB_WSTRUST ||
+       if (!(acct_ctrl & ACB_WSTRUST ||
                      acct_ctrl & ACB_SVRTRUST ||
-                     acct_ctrl & ACB_DOMTRUST))) {
+                     acct_ctrl & ACB_DOMTRUST)) {
                TALLOC_FREE(sampass);
                return NT_STATUS_NO_SUCH_USER;
        }
@@ -571,19 +611,19 @@ NTSTATUS _net_srv_pwset(pipes_struct *p, NET_Q_SRV_PWSET *q_u, NET_R_SRV_PWSET *
                        return NT_STATUS_NO_MEMORY;
                }
                
-               if (!pdb_set_pass_changed_now(sampass)) {
+               if (!pdb_set_pass_last_set_time(sampass, time(NULL), PDB_CHANGED)) {
                        TALLOC_FREE(sampass);
                        /* Not quite sure what this one qualifies as, but this will do */
                        return NT_STATUS_UNSUCCESSFUL; 
                }
                
                become_root();
-               r_u->status = pdb_update_sam_account (sampass);
+               r_u->status = pdb_update_sam_account(sampass);
                unbecome_root();
        }
 
        /* set up the LSA Server Password Set response */
-       init_net_r_srv_pwset(r_u, &cred_out, status);
+       init_net_r_srv_pwset(r_u, &cred_out, r_u->status);
 
        TALLOC_FREE(sampass);
        return r_u->status;
@@ -616,7 +656,7 @@ NTSTATUS _net_sam_logoff(pipes_struct *p, NET_Q_SAM_LOGOFF *q_u, NET_R_SAM_LOGOF
 
        if (!p->dc) {
                /* Restore the saved state of the netlogon creds. */
-               BOOL ret;
+               bool ret;
 
                become_root();
                ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
@@ -690,7 +730,7 @@ static NTSTATUS nt_token_to_group_list(TALLOC_CTX *mem_ctx,
 static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
                                        NET_Q_SAM_LOGON *q_u,
                                        NET_R_SAM_LOGON *r_u,
-                                       BOOL process_creds)
+                                       bool process_creds)
 {
        NTSTATUS status = NT_STATUS_OK;
        NET_USER_INFO_3 *usr_info = NULL;
@@ -746,7 +786,7 @@ static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
 
                if (!p->dc) {
                        /* Restore the saved state of the netlogon creds. */
-                       BOOL ret;
+                       bool ret;
 
                        become_root();
                        ret = secrets_restore_schannel_session_info(p->pipe_state_mem_ctx,
@@ -908,18 +948,16 @@ static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
        /* This is the point at which, if the login was successful, that
            the SAM Local Security Authority should record that the user is
            logged in to the domain.  */
-    
+
        {
                DOM_GID *gids = NULL;
                const DOM_SID *user_sid = NULL;
                const DOM_SID *group_sid = NULL;
                DOM_SID domain_sid;
-               uint32 user_rid, group_rid; 
+               uint32 user_rid, group_rid;
 
                int num_gids = 0;
-               pstring my_name;
-               fstring user_sid_string;
-               fstring group_sid_string;
+               const char *my_name;
                unsigned char user_session_key[16];
                unsigned char lm_session_key[16];
                unsigned char pipe_session_key[16];
@@ -933,6 +971,11 @@ static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
                user_sid = pdb_get_user_sid(sampw);
                group_sid = pdb_get_group_sid(sampw);
 
+               if ((user_sid == NULL) || (group_sid == NULL)) {
+                       DEBUG(1, ("_net_sam_logon: User without group or user SID\n"));
+                       return NT_STATUS_UNSUCCESSFUL;
+               }
+
                sid_copy(&domain_sid, user_sid);
                sid_split_rid(&domain_sid, &user_rid);
 
@@ -940,19 +983,18 @@ static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
                        DEBUG(1, ("_net_sam_logon: user %s\\%s has user sid "
                                  "%s\n but group sid %s.\n"
                                  "The conflicting domain portions are not "
-                                 "supported for NETLOGON calls\n",         
+                                 "supported for NETLOGON calls\n",
                                  pdb_get_domain(sampw),
                                  pdb_get_username(sampw),
-                                 sid_to_string(user_sid_string, user_sid),
-                                 sid_to_string(group_sid_string, group_sid)));
+                                 sid_string_dbg(user_sid),
+                                 sid_string_dbg(group_sid)));
                        return NT_STATUS_UNSUCCESSFUL;
                }
-               
-               
+
                if(server_info->login_server) {
-                       pstrcpy(my_name, server_info->login_server);
+                       my_name = server_info->login_server;
                } else {
-                       pstrcpy(my_name, global_myname());
+                       my_name = global_myname();
                }
 
                status = nt_token_to_group_list(p->mem_ctx, &domain_sid,
@@ -966,7 +1008,7 @@ static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
 
                if (server_info->user_session_key.length) {
                        memcpy(user_session_key,
-                              server_info->user_session_key.data, 
+                              server_info->user_session_key.data,
                               MIN(sizeof(user_session_key),
                                   server_info->user_session_key.length));
                        if (process_creds) {
@@ -984,7 +1026,7 @@ static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
                }
                if (server_info->lm_session_key.length) {
                        memcpy(lm_session_key,
-                              server_info->lm_session_key.data, 
+                              server_info->lm_session_key.data,
                               MIN(sizeof(lm_session_key),
                                   server_info->lm_session_key.length));
                        if (process_creds) {
@@ -1000,10 +1042,10 @@ static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
                        SamOEMhash(lm_session_key, pipe_session_key, 16);
                        memset(pipe_session_key, '\0', 16);
                }
-               
-               init_net_user_info3(p->mem_ctx, usr_info, 
+
+               init_net_user_info3(p->mem_ctx, usr_info,
                                    user_rid,
-                                   group_rid,   
+                                   group_rid,
                                    pdb_get_username(sampw),
                                    pdb_get_fullname(sampw),
                                    pdb_get_homedir(sampw),
@@ -1016,17 +1058,17 @@ static NTSTATUS _net_sam_logon_internal(pipes_struct *p,
                                    pdb_get_pass_last_set_time(sampw),
                                    pdb_get_pass_can_change_time(sampw),
                                    pdb_get_pass_must_change_time(sampw),
-                                   
                                    0, /* logon_count */
                                    0, /* bad_pw_count */
                                    num_gids,    /* uint32 num_groups */
                                    gids    , /* DOM_GID *gids */
-                                   0x20    , /* uint32 user_flgs (?) */
+                                   LOGON_EXTRA_SIDS, /* uint32 user_flgs (?) */
+                                   pdb_get_acct_ctrl(sampw),
                                    server_info->user_session_key.length ? user_session_key : NULL,
                                    server_info->lm_session_key.length ? lm_session_key : NULL,
                                    my_name     , /* char *logon_srv */
                                    pdb_get_domain(sampw),
-                                   &domain_sid);     /* DOM_SID *dom_sid */  
+                                   &domain_sid);     /* DOM_SID *dom_sid */
                ZERO_STRUCT(user_session_key);
                ZERO_STRUCT(lm_session_key);
        }