fixing domain join and domain login problems
[samba.git] / source / rpc_server / srv_netlog.c
index 6aa1cd707e5adcfad8cf4277e197ae53f5526bf4..9fa3ed79cc106303fe84cd72907718f4d764121a 100644 (file)
@@ -1,4 +1,3 @@
-
 /* 
  *  Unix SMB/Netbios implementation.
  *  Version 1.9.
@@ -6,7 +5,8 @@
  *  Copyright (C) Andrew Tridgell              1992-1997,
  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
  *  Copyright (C) Paul Ashton                       1997.
- *  
+ *  Copyright (C) Jeremy Allison                    1998.
+ *
  *  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
@@ -31,6 +31,7 @@ extern int DEBUGLEVEL;
 extern BOOL sam_logon_in_ssb;
 extern pstring samlogon_user;
 extern pstring global_myname;
+extern DOM_SID global_sam_sid;
 
 /*************************************************************************
  make_net_r_req_chal:
@@ -124,10 +125,13 @@ static void net_reply_auth_2(NET_Q_AUTH_2 *q_a, prs_struct *rdata,
                                DOM_CHAL *resp_cred, int status)
 {
        NET_R_AUTH_2 r_a;
+       NEG_FLAGS srv_flgs;
+
+       srv_flgs.neg_flags = 0x000001ff;
 
        /* set up the LSA AUTH 2 response */
 
-       make_net_r_auth_2(&r_a, resp_cred, &(q_a->clnt_flgs), status);
+       make_net_r_auth_2(&r_a, resp_cred, &srv_flgs, status);
 
        /* store the response in the SMB stream */
        net_io_r_auth_2("", &r_a, rdata, 0);
@@ -227,7 +231,16 @@ static void net_reply_sam_logoff(NET_Q_SAM_LOGOFF *q_s, prs_struct *rdata,
 static BOOL get_md4pw(char *md4pw, char *mach_name, char *mach_acct)
 {
        struct smb_passwd *smb_pass;
-       extern int Client;
+
+#if 0
+    /*
+     * Currently this code is redundent as we already have a filter
+     * by hostname list. What this code really needs to do is to 
+     * get a hosts allowed/hosts denied list from the SAM database
+     * on a per user basis, and make the access decision there.
+     * I will leave this code here for now as a reminder to implement
+     * this at a later date. JRA.
+     */
 
        if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
                          client_name(Client), client_addr(Client)))
@@ -235,12 +248,14 @@ static BOOL get_md4pw(char *md4pw, char *mach_name, char *mach_acct)
                DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
                return False;
        }
+#endif /* 0 */
 
        become_root(True);
        smb_pass = getsmbpwnam(mach_acct);
        unbecome_root(True);
 
-       if (smb_pass != NULL)
+       if ((smb_pass) != NULL && !(smb_pass->acct_ctrl & ACB_DISABLED) &&
+        (smb_pass->smb_nt_passwd != NULL))
        {
                memcpy(md4pw, smb_pass->smb_nt_passwd, 16);
                dump_data(5, md4pw, 16);
@@ -254,7 +269,7 @@ static BOOL get_md4pw(char *md4pw, char *mach_name, char *mach_acct)
 /*************************************************************************
  api_net_req_chal:
  *************************************************************************/
-static void api_net_req_chal( int uid,
+static void api_net_req_chal( uint16 vuid,
                               prs_struct *data,
                               prs_struct *rdata)
 {
@@ -266,9 +281,10 @@ static void api_net_req_chal( int uid,
 
        user_struct *vuser;
 
-       DEBUG(5,("api_net_req_chal(%d): vuid %d\n", __LINE__, uid));
+       DEBUG(5,("api_net_req_chal(%d): vuid %d\n", __LINE__, (int)vuid));
 
-       if ((vuser = get_valid_user_struct(uid)) == NULL) return;
+       if ((vuser = get_valid_user_struct(vuid)) == NULL)
+      return;
 
        /* grab the challenge... */
        net_io_q_req_chal("", &q_r, data, 0);
@@ -279,9 +295,9 @@ static void api_net_req_chal( int uid,
        fstrcpy(mach_name, mach_acct);
        strlower(mach_name);
 
-       strcat(mach_acct, "$");
+       fstrcat(mach_acct, "$");
 
-       if (get_md4pw(vuser->dc.md4pw, mach_name, mach_acct))
+       if (get_md4pw((char *)vuser->dc.md4pw, mach_name, mach_acct))
        {
                /* copy the client credentials */
                memcpy(vuser->dc.clnt_chal.data          , q_r.clnt_chal.data, sizeof(q_r.clnt_chal.data));
@@ -297,7 +313,7 @@ static void api_net_req_chal( int uid,
 
                /* from client / server challenges and md4 password, generate sess key */
                cred_session_key(&(vuser->dc.clnt_chal), &(vuser->dc.srv_chal),
-                                                  vuser->dc.md4pw, vuser->dc.sess_key);
+                                (char *)vuser->dc.md4pw, vuser->dc.sess_key);
        }
        else
        {
@@ -314,7 +330,7 @@ static void api_net_req_chal( int uid,
 /*************************************************************************
  api_net_auth_2:
  *************************************************************************/
-static void api_net_auth_2( int uid,
+static void api_net_auth_2( uint16 vuid,
                             prs_struct *data,
                             prs_struct *rdata)
 {
@@ -326,7 +342,8 @@ static void api_net_auth_2( int uid,
 
        user_struct *vuser;
 
-       if ((vuser = get_valid_user_struct(uid)) == NULL) return;
+       if ((vuser = get_valid_user_struct(vuid)) == NULL)
+      return;
 
        srv_time.time = 0;
 
@@ -342,8 +359,8 @@ static void api_net_auth_2( int uid,
                cred_create(vuser->dc.sess_key, &(vuser->dc.srv_cred.challenge), srv_time, &srv_cred);
 
                /* copy the received client credentials for use next time */
-               memcpy(vuser->dc.clnt_cred.challenge.data, &(q_a.clnt_chal.data), sizeof(q_a.clnt_chal.data));
-               memcpy(vuser->dc.srv_cred.challenge.data, &(q_a.clnt_chal.data), sizeof(q_a.clnt_chal.data));
+               memcpy(vuser->dc.clnt_cred.challenge.data, q_a.clnt_chal.data, sizeof(q_a.clnt_chal.data));
+               memcpy(vuser->dc.srv_cred .challenge.data, q_a.clnt_chal.data, sizeof(q_a.clnt_chal.data));
        }
        else
        {
@@ -358,7 +375,7 @@ static void api_net_auth_2( int uid,
 /*************************************************************************
  api_net_srv_pwset:
  *************************************************************************/
-static void api_net_srv_pwset( int uid,
+static void api_net_srv_pwset( uint16 vuid,
                                prs_struct *data,
                                prs_struct *rdata)
 {
@@ -370,21 +387,22 @@ static void api_net_srv_pwset( int uid,
        BOOL ret;
        user_struct *vuser;
 
-       if ((vuser = get_valid_user_struct(uid)) == NULL) return;
+       if ((vuser = get_valid_user_struct(vuid)) == NULL)
+      return;
 
        /* grab the challenge and encrypted password ... */
        net_io_q_srv_pwset("", &q_a, data, 0);
 
        /* checks and updates credentials.  creates reply credentials */
        if (deal_with_creds(vuser->dc.sess_key, &(vuser->dc.clnt_cred), 
-                       &(q_a.clnt_id.cred), &srv_cred))
+                           &(q_a.clnt_id.cred), &srv_cred))
        {
                memcpy(&(vuser->dc.srv_cred), &(vuser->dc.clnt_cred), sizeof(vuser->dc.clnt_cred));
 
                DEBUG(5,("api_net_srv_pwset: %d\n", __LINE__));
 
                pstrcpy(mach_acct, unistrn2(q_a.clnt_id.login.uni_acct_name.buffer,
-                                                                       q_a.clnt_id.login.uni_acct_name.uni_str_len));
+                                           q_a.clnt_id.login.uni_acct_name.uni_str_len));
 
                DEBUG(3,("Server Password Set Wksta:[%s]\n", mach_acct));
 
@@ -394,31 +412,33 @@ static void api_net_srv_pwset( int uid,
 
                if (smb_pass != NULL)
                {
-                  unsigned char pwd[16];
-                  int i;
+                       unsigned char pwd[16];
+                       int i;
 
-                  DEBUG(100,("Server password set : new given value was :\n"));
-                  for(i = 0; i < 16; i++)
-                    DEBUG(100,("%02X ", q_a.pwd[i]));
-                  DEBUG(100,("\n"));
+                       DEBUG(100,("Server password set : new given value was :\n"));
+                       for(i = 0; i < 16; i++)
+                       {
+                               DEBUG(100,("%02X ", q_a.pwd[i]));
+                       }
+                       DEBUG(100,("\n"));
 
-                  cred_hash3( pwd, q_a.pwd, vuser->dc.sess_key);
+                       cred_hash3( pwd, q_a.pwd, vuser->dc.sess_key, 0);
 
-                  /* lies!  nt and lm passwords are _not_ the same: don't care */
-                  smb_pass->smb_passwd    = pwd;
-                  smb_pass->smb_nt_passwd = pwd;
-                  smb_pass->acct_ctrl     = ACB_WSTRUST;
+                       /* lies!  nt and lm passwords are _not_ the same: don't care */
+                       smb_pass->smb_passwd    = pwd;
+                       smb_pass->smb_nt_passwd = pwd;
+                       smb_pass->acct_ctrl     = ACB_WSTRUST;
 
-                  become_root(True);
-                  ret = mod_smbpwd_entry(smb_pass);
-                  unbecome_root(True);
+                       become_root(True);
+                       ret = mod_smbpwd_entry(smb_pass,False);
+                       unbecome_root(True);
 
-                  if (ret)
-                  {
-                    /* hooray! */
-                    status = 0x0;
-                  }
-                }
+                       if (ret)
+                       {
+                               /* hooray! */
+                               status = 0x0;
+                       }
+               }
 
                DEBUG(5,("api_net_srv_pwset: %d\n", __LINE__));
 
@@ -437,7 +457,7 @@ static void api_net_srv_pwset( int uid,
 /*************************************************************************
  api_net_sam_logoff:
  *************************************************************************/
-static void api_net_sam_logoff( int uid,
+static void api_net_sam_logoff( uint16 vuid,
                                prs_struct *data,
                                prs_struct *rdata)
 {
@@ -448,7 +468,8 @@ static void api_net_sam_logoff( int uid,
 
        user_struct *vuser;
 
-       if ((vuser = get_valid_user_struct(uid)) == NULL) return;
+       if ((vuser = get_valid_user_struct(vuid)) == NULL)
+      return;
 
        /* the DOM_ID_INFO_1 structure is a bit big.  plus we might want to
           dynamically allocate it inside net_io_q_sam_logon, at some point */
@@ -487,8 +508,19 @@ static uint32 net_login_interactive(NET_ID_INFO_1 *id1,
        memcpy(lm_pwd, id1->lm_owf.data, 16);
        memcpy(nt_pwd, id1->nt_owf.data, 16);
 
-       SamOEMhash(lm_pwd, key, False);
-       SamOEMhash(nt_pwd, key, False);
+#ifdef DEBUG_PASSWORD
+       DEBUG(100,("key:"));
+       dump_data(100, key, 16);
+
+       DEBUG(100,("lm owf password:"));
+       dump_data(100, lm_pwd, 16);
+
+       DEBUG(100,("nt owf password:"));
+       dump_data(100, nt_pwd, 16);
+#endif
+
+       SamOEMhash((uchar *)lm_pwd, key, False);
+       SamOEMhash((uchar *)nt_pwd, key, False);
 
 #ifdef DEBUG_PASSWORD
        DEBUG(100,("decrypt of lm owf password:"));
@@ -499,7 +531,7 @@ static uint32 net_login_interactive(NET_ID_INFO_1 *id1,
 #endif
 
        if (memcmp(smb_pass->smb_passwd   , lm_pwd, 16) != 0 &&
-               memcmp(smb_pass->smb_nt_passwd, nt_pwd, 16) != 0)
+           memcmp(smb_pass->smb_nt_passwd, nt_pwd, 16) != 0)
        {
                status = 0xC0000000 | NT_STATUS_WRONG_PASSWORD;
        }
@@ -515,16 +547,16 @@ static uint32 net_login_network(NET_ID_INFO_2 *id2,
                                user_struct *vuser)
 {
        DEBUG(5,("net_login_network: lm_len: %d nt_len: %d\n",
-               id2->lm_chal_resp.str_str_len, 
-               id2->nt_chal_resp.str_str_len));
+               id2->hdr_lm_chal_resp.str_str_len, 
+               id2->hdr_nt_chal_resp.str_str_len));
 
        /* JRA. Check the NT password first if it exists - this is a higher quality 
            password, if it exists and it doesn't match - fail. */
 
-       if (id2->nt_chal_resp.str_str_len == 24 && 
+       if (id2->hdr_nt_chal_resp.str_str_len == 24 && 
                smb_pass->smb_nt_passwd != NULL)
        {
-               if(smb_password_check(id2->nt_chal_resp.buffer,
+               if(smb_password_check((char *)id2->nt_chal_resp.buffer,
                                   smb_pass->smb_nt_passwd,
                            id2->lm_chal)) 
                   return 0x0;
@@ -540,8 +572,8 @@ static uint32 net_login_network(NET_ID_INFO_2 *id2,
           not do, for various security-hole reasons).
         */
 
-       if (id2->lm_chal_resp.str_str_len == 24 &&
-               smb_password_check(id2->lm_chal_resp.buffer,
+       if (id2->hdr_lm_chal_resp.str_str_len == 24 &&
+               smb_password_check((char *)id2->lm_chal_resp.buffer,
                                   smb_pass->smb_passwd,
                                   id2->lm_chal))
        {
@@ -557,7 +589,7 @@ static uint32 net_login_network(NET_ID_INFO_2 *id2,
 /*************************************************************************
  api_net_sam_logon:
  *************************************************************************/
-static void api_net_sam_logon( int uid,
+static void api_net_sam_logon( uint16 vuid,
                                prs_struct *data,
                                prs_struct *rdata)
 {
@@ -571,7 +603,8 @@ static void api_net_sam_logon( int uid,
 
        user_struct *vuser = NULL;
 
-       if ((vuser = get_valid_user_struct(uid)) == NULL) return;
+       if ((vuser = get_valid_user_struct(vuid)) == NULL)
+       return;
 
        q_l.sam_id.ctr = &ctr;
 
@@ -579,7 +612,7 @@ static void api_net_sam_logon( int uid,
 
        /* checks and updates credentials.  creates reply credentials */
        if (!deal_with_creds(vuser->dc.sess_key, &(vuser->dc.clnt_cred), 
-                       &(q_l.sam_id.client.cred), &srv_cred))
+                            &(q_l.sam_id.client.cred), &srv_cred))
        {
                status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
        }
@@ -590,24 +623,22 @@ static void api_net_sam_logon( int uid,
 
        /* find the username */
 
-       if (status == 0x0)
+       if (status == 0)
        {
                switch (q_l.sam_id.logon_level)
                {
-                       case 1:
+                       case INTERACTIVE_LOGON_TYPE:
                        {
                                uni_samlogon_user = &(q_l.sam_id.ctr->auth.id1.uni_user_name);
 
-                               DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ",
-                                         lp_workgroup()));
+                               DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ", lp_workgroup()));
                                break;
                        }
-                       case 2:
+                       case NET_LOGON_TYPE:
                        {
                                uni_samlogon_user = &(q_l.sam_id.ctr->auth.id2.uni_user_name);
 
-                               DEBUG(3,("SAM Logon (Network). Domain:[%s].  ",
-                                         lp_workgroup()));
+                               DEBUG(3,("SAM Logon (Network). Domain:[%s].  ", lp_workgroup()));
                                break;
                        }
                        default:
@@ -616,18 +647,28 @@ static void api_net_sam_logon( int uid,
                                status = 0xC0000000 | NT_STATUS_INVALID_INFO_CLASS;
                                break;
                        }
-               }
-       }
+               } /* end switch */
+       } /* end if status == 0 */
 
        /* check username exists */
 
-       if (status == 0x0)
+       if (status == 0)
        {
                pstrcpy(samlogon_user, unistrn2(uni_samlogon_user->buffer,
-                                               uni_samlogon_user->uni_str_len));
+               uni_samlogon_user->uni_str_len));
 
                DEBUG(3,("User:[%s]\n", samlogon_user));
 
+               /*
+                * Convert to a UNIX username.
+                */
+               map_username(samlogon_user);
+
+               /*
+                * Do any case conversions.
+                */
+               (void)Get_Pwnam(samlogon_user, True);
+
                become_root(True);
                smb_pass = getsmbpwnam(samlogon_user);
                unbecome_root(True);
@@ -636,41 +677,44 @@ static void api_net_sam_logon( int uid,
                {
                        status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
                }
+               else if (IS_BITS_SET_ALL(smb_pass->acct_ctrl, ACB_DISABLED) &&
+                        IS_BITS_CLR_ALL(smb_pass->acct_ctrl, ACB_PWNOTREQ))
+               {
+                       status =  0xC0000000 | NT_STATUS_ACCOUNT_DISABLED;
+               }
        }
 
-       /* validate password. */
+       /* validate password - if required */
 
-       if (status == 0x0)
+       if (status == 0 && !(IS_BITS_SET_ALL(smb_pass->acct_ctrl, ACB_PWNOTREQ)))
        {
                switch (q_l.sam_id.logon_level)
                {
-                       case 1:
+                       case INTERACTIVE_LOGON_TYPE:
                        {
                                /* interactive login. */
-                               status = net_login_interactive(&q_l.sam_id.ctr->auth.id1,
-                                                              smb_pass, vuser);
+                               status = net_login_interactive(&q_l.sam_id.ctr->auth.id1, smb_pass, vuser);
                                break;
                        }
-                       case 2:
+                       case NET_LOGON_TYPE:
                        {
                                /* network login.  lm challenge and 24 byte responses */
-                               status = net_login_network(&q_l.sam_id.ctr->auth.id2,
-                                                          smb_pass, vuser);
+                               status = net_login_network(&q_l.sam_id.ctr->auth.id2, smb_pass, vuser);
                                break;
                        }
                }
        }
-       
+
        /* lkclXXXX 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.
-        */
+       successful, that the SAM Local Security Authority should
+       record that the user is logged in to the domain.
+       */
 
        /* return the profile plus other bits :-) */
 
-       if (status == 0x0)
+       if (status == 0)
        {
-               DOM_GID gids[LSA_MAX_GROUPS];
+               DOM_GID *gids = NULL;
                int num_gids = 0;
                NTTIME dummy_time;
                pstring logon_script;
@@ -679,9 +723,7 @@ static void api_net_sam_logon( int uid,
                pstring home_drive;
                pstring my_name;
                pstring my_workgroup;
-               pstring domain_groups;
-               pstring dom_sid;
-               pstring other_sids;
+               DOMAIN_GRP *grp_mem;
                uint32 r_uid;
                uint32 r_gid;
 
@@ -695,62 +737,61 @@ static void api_net_sam_logon( int uid,
                /* possibly a better way would be to do a become_user() call */
                sam_logon_in_ssb = True;
 
-               pstrcpy(logon_script, lp_logon_script     ());
-               pstrcpy(profile_path, lp_logon_path       ());
-               pstrcpy(dom_sid     , lp_domain_sid       ());
-               pstrcpy(other_sids  , lp_domain_other_sids());
-               pstrcpy(my_workgroup, lp_workgroup        ());
+               pstrcpy(logon_script, lp_logon_script());
+               pstrcpy(profile_path, lp_logon_path());
 
-               pstrcpy(home_drive  , lp_logon_drive      ());
-               pstrcpy(home_dir    , lp_logon_home       ());
+               pstrcpy(my_workgroup, lp_workgroup());
 
-               pstrcpy(my_name     , global_myname         );
+               pstrcpy(home_drive, lp_logon_drive());
+               pstrcpy(home_dir, lp_logon_home());
+               pstrcpy(my_name, global_myname);
                strupper(my_name);
 
-               get_domain_user_groups(domain_groups, samlogon_user);
-
-               num_gids = make_dom_gids(domain_groups, gids);
+               status = lookup_user_rids(samlogon_user, &r_uid, &r_gid);
+               status = getusergroupsnam(samlogon_user, &grp_mem, &num_gids) ? 0 : 0xC0000000 | NT_STATUS_INVALID_PRIMARY_GROUP;
 
-               sam_logon_in_ssb = False;
-
-               if (name_to_rid(samlogon_user, &r_uid, &r_gid))
+               if (status == 0x0)
                {
-                       make_net_user_info3(&usr_info,
+                       gids = NULL;
+                       num_gids = make_dom_gids(grp_mem, num_gids, &gids);
 
-                                  &dummy_time, /* logon_time */
-                                  &dummy_time, /* logoff_time */
-                                  &dummy_time, /* kickoff_time */
-                                  &dummy_time, /* pass_last_set_time */
-                                  &dummy_time, /* pass_can_change_time */
-                                  &dummy_time, /* pass_must_change_time */
-
-                                  samlogon_user   , /* user_name */
-                                  vuser->real_name, /* full_name */
-                                  logon_script    , /* logon_script */
-                                  profile_path    , /* profile_path */
-                                  home_dir        , /* home_dir */
-                                  home_drive      , /* dir_drive */
-
-                                  0, /* logon_count */
-                                  0, /* bad_pw_count */
-
-                                  r_uid   , /* RID user_id */
-                                  r_gid   , /* RID group_id */
-                                  num_gids,    /* uint32 num_groups */
-                                  gids    , /* DOM_GID *gids */
-                                  0x20    , /* uint32 user_flgs (?) */
-
-                                  NULL, /* char sess_key[16] */
-
-                                  my_name     , /* char *logon_srv */
-                                  my_workgroup, /* char *logon_dom */
-
-                                  dom_sid,     /* char *dom_sid */
-                                  other_sids); /* char *other_sids */
+                       make_net_user_info3(&usr_info,
+                               &dummy_time, /* logon_time */
+                               &dummy_time, /* logoff_time */
+                               &dummy_time, /* kickoff_time */
+                               &dummy_time, /* pass_last_set_time */
+                               &dummy_time, /* pass_can_change_time */
+                               &dummy_time, /* pass_must_change_time */
+
+                               samlogon_user   , /* user_name */
+                               vuser->real_name, /* full_name */
+                               logon_script    , /* logon_script */
+                               profile_path    , /* profile_path */
+                               home_dir        , /* home_dir */
+                               home_drive      , /* dir_drive */
+
+                               0, /* logon_count */
+                               0, /* bad_pw_count */
+
+                               r_uid   , /* RID user_id */
+                               r_gid   , /* RID group_id */
+                               num_gids,    /* uint32 num_groups */
+                               gids    , /* DOM_GID *gids */
+                               0x20    , /* uint32 user_flgs (?) */
+
+                               NULL, /* char sess_key[16] */
+
+                               my_name     , /* char *logon_srv */
+                               my_workgroup, /* char *logon_dom */
+
+                               &global_sam_sid,     /* DOM_SID *dom_sid */
+                               NULL); /* char *other_sids */
                }
-               else
+
+               /* Free any allocated groups array. */
+               if (gids)
                {
-                       status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
+                       free((char *)gids);
                }
        }
 
@@ -761,7 +802,7 @@ static void api_net_sam_logon( int uid,
 /*************************************************************************
  api_net_trust_dom_list:
  *************************************************************************/
-static void api_net_trust_dom_list( int uid,
+static void api_net_trust_dom_list( uint16 vuid,
                                  prs_struct *data,
                                  prs_struct *rdata)
 {
@@ -791,7 +832,7 @@ static void api_net_trust_dom_list( int uid,
 /*************************************************************************
  api_net_logon_ctrl2:
  *************************************************************************/
-static void api_net_logon_ctrl2( int uid,
+static void api_net_logon_ctrl2( uint16 vuid,
                                  prs_struct *data,
                                  prs_struct *rdata)
 {