RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / rpc_server / srv_netlog_nt.c
index 8dbd4ff33f33c49bc5dcbd28f0e17618874903a6..b651fdaac320e175ff90fc2acfbb9f90316315dd 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);
 }
 
 /*************************************************************************
@@ -204,11 +190,11 @@ 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
@@ -230,35 +216,78 @@ static BOOL get_md4pw(char *md4pw, char *mach_acct)
 #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 +342,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 +356,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 +411,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 +440,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 +496,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 +548,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 +609,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 +654,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 +728,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 +784,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,
@@ -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);