pwdb_smb_to_sam was not returning NULL for nt name so that
[samba.git] / source / rpc_server / srv_netlog.c
index cb22cfddf0256582e2418596ada05b5ea779885a..33eb889ff2ed6a0da4786550fd0f87e14c442277 100644 (file)
@@ -1,4 +1,3 @@
-
 /* 
  *  Unix SMB/Netbios implementation.
  *  Version 1.9.
 
 extern int DEBUGLEVEL;
 
-extern BOOL sam_logon_in_ssb;
-extern pstring samlogon_user;
 extern pstring global_myname;
-extern DOM_SID global_machine_sid;
+extern DOM_SID global_sam_sid;
+extern fstring global_sam_name;
 
 /*************************************************************************
  make_net_r_req_chal:
@@ -126,10 +124,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);
@@ -267,7 +268,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)
 {
@@ -279,15 +280,15 @@ 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);
 
-       fstrcpy(mach_acct, unistrn2(q_r.uni_logon_clnt.buffer,
-                                   q_r.uni_logon_clnt.uni_str_len));
+       fstrcpy(mach_acct, unistr2_to_str(&q_r.uni_logon_clnt));
 
        fstrcpy(mach_name, mach_acct);
        strlower(mach_name);
@@ -327,7 +328,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)
 {
@@ -339,7 +340,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;
 
@@ -371,7 +373,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)
 {
@@ -383,7 +385,8 @@ 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);
@@ -396,8 +399,7 @@ static void api_net_srv_pwset( int uid,
 
                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));
+               fstrcpy(mach_acct, unistr2_to_str(&q_a.clnt_id.login.uni_acct_name));
 
                DEBUG(3,("Server Password Set Wksta:[%s]\n", mach_acct));
 
@@ -452,7 +454,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)
 {
@@ -463,7 +465,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,7 +490,7 @@ static void api_net_sam_logoff( int uid,
  net_login_interactive:
  *************************************************************************/
 static uint32 net_login_interactive(NET_ID_INFO_1 *id1,
-                               struct smb_passwd *smb_pass,
+                               struct sam_passwd *smb_pass,
                                user_struct *vuser)
 {
        uint32 status = 0x0;
@@ -502,6 +505,17 @@ 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);
 
+#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);
 
@@ -514,7 +528,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;
        }
@@ -526,7 +540,7 @@ static uint32 net_login_interactive(NET_ID_INFO_1 *id1,
  net_login_network:
  *************************************************************************/
 static uint32 net_login_network(NET_ID_INFO_2 *id2,
-                               struct smb_passwd *smb_pass,
+                               struct sam_passwd *smb_pass,
                                user_struct *vuser)
 {
        DEBUG(5,("net_login_network: lm_len: %d nt_len: %d\n",
@@ -572,230 +586,229 @@ 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)
 {
-  NET_Q_SAM_LOGON q_l;
-  NET_ID_INFO_CTR ctr; 
-  NET_USER_INFO_3 usr_info;
-  uint32 status = 0x0;
-  DOM_CRED srv_cred;
-  struct smb_passwd *smb_pass = NULL;
-  UNISTR2 *uni_samlogon_user = NULL;
-
-  user_struct *vuser = NULL;
-
-  if ((vuser = get_valid_user_struct(uid)) == NULL)
-    return;
-
-  q_l.sam_id.ctr = &ctr;
-
-  net_io_q_sam_logon("", &q_l, data, 0);
-
-  /* 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))
-  {
-    status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
-  }
-  else
-  {
-    memcpy(&(vuser->dc.srv_cred), &(vuser->dc.clnt_cred), sizeof(vuser->dc.clnt_cred));
-  }
-
-  /* find the username */
-
-  if (status == 0)
-  {
-    switch (q_l.sam_id.logon_level)
-    {
-      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()));
-        break;
-      }
-      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()));
-        break;
-      }
-      default:
-      {
-        DEBUG(2,("SAM Logon: unsupported switch value\n"));
-        status = 0xC0000000 | NT_STATUS_INVALID_INFO_CLASS;
-        break;
-      }
-    } /* end switch */
-  } /* end if status == 0 */
-
-  /* check username exists */
-
-  if (status == 0)
-  {
-    pstrcpy(samlogon_user, unistrn2(uni_samlogon_user->buffer,
-            uni_samlogon_user->uni_str_len));
-
-    DEBUG(3,("User:[%s]\n", samlogon_user));
+       NET_Q_SAM_LOGON q_l;
+       NET_ID_INFO_CTR ctr;    
+       NET_USER_INFO_3 usr_info;
+       uint32 status = 0x0;
+       DOM_CRED srv_cred;
+       struct sam_passwd *sam_pass = NULL;
+       UNISTR2 *uni_samlogon_user = NULL;
+       UNISTR2 *uni_domain = NULL;
+       fstring nt_username;
 
-    /*
-     * Convert to a UNIX username.
-     */
-    map_username(samlogon_user);
+       NTTIME logon_time           ;
+       NTTIME logoff_time          ;
+       NTTIME kickoff_time         ;
+       NTTIME pass_last_set_time   ;
+       NTTIME pass_can_change_time ;
+       NTTIME pass_must_change_time;
 
-    /*
-     * Do any case conversions.
-     */
-    (void)Get_Pwnam(samlogon_user, True);
-
-    become_root(True);
-    smb_pass = getsmbpwnam(samlogon_user);
-    unbecome_root(True);
-
-    if (smb_pass == NULL)
-      status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
-    else if (smb_pass->acct_ctrl & ACB_DISABLED)
-      status =  0xC0000000 | NT_STATUS_ACCOUNT_DISABLED;
-  }
-
-  /* validate password. */
-
-  if (status == 0)
-  {
-    switch (q_l.sam_id.logon_level)
-    {
-      case INTERACTIVE_LOGON_TYPE:
-      {
-        /* interactive login. */
-        status = net_login_interactive(&q_l.sam_id.ctr->auth.id1, smb_pass, vuser);
-        break;
-      }
-      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);
-        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.
-   */
-
-  /* return the profile plus other bits :-) */
-
-  if (status == 0)
-  {
-    DOM_GID *gids = NULL;
-    int num_gids = 0;
-    NTTIME dummy_time;
-    pstring logon_script;
-    pstring profile_path;
-    pstring home_dir;
-    pstring home_drive;
-    pstring my_name;
-    pstring my_workgroup;
-    pstring domain_groups;
-    uint32 r_uid;
-    uint32 r_gid;
-
-    /* set up pointer indicating user/password failed to be found */
-    usr_info.ptr_user_info = 0;
-
-    dummy_time.low  = 0xffffffff;
-    dummy_time.high = 0x7fffffff;
-
-    /* XXXX hack to get standard_sub_basic() to use sam logon username */
-    /* 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(my_workgroup, lp_workgroup());
-
-    pstrcpy(home_drive, lp_logon_drive());
-    pstrcpy(home_dir, lp_logon_home());
-
-    pstrcpy(my_name, global_myname);
-    strupper(my_name);
+       fstring nt_name     ;
+       fstring full_name   ;
+       fstring logon_script;
+       fstring profile_path;
+       fstring home_dir    ;
+       fstring dir_drive   ;
 
-    /*
-     * This is the point at which we get the group
-     * database - we should be getting the gid_t list
-     * from /etc/group and then turning the uids into
-     * rids and then into machine sids for this user.
-     * JRA.
-     */
+       uint32 user_rid ;
+       uint32 group_rid;
 
-    get_domain_user_groups(domain_groups, samlogon_user);
+       user_struct *vuser = NULL;
 
-    /*
-     * make_dom_gids allocates the gids array. JRA.
-     */
-    gids = NULL;
-    num_gids = make_dom_gids(domain_groups, &gids);
-
-    sam_logon_in_ssb = False;
-
-    if (pdb_name_to_rid(samlogon_user, &r_uid, &r_gid))
-    {
-      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_machine_sid,     /* DOM_SID *dom_sid */
-                          NULL); /* char *other_sids */
-    }
-    else
-    {
-      status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
-    }
-
-    /* Free any allocated groups array. */
-    if(gids)
-      free((char *)gids);
-  }
-
-  net_reply_sam_logon(&q_l, rdata, &srv_cred, &usr_info, status);
+       if ((vuser = get_valid_user_struct(vuid)) == NULL)
+       return;
+
+       q_l.sam_id.ctr = &ctr;
+
+       net_io_q_sam_logon("", &q_l, data, 0);
+
+       /* 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))
+       {
+               status = 0xC0000000 | NT_STATUS_INVALID_HANDLE;
+       }
+       else
+       {
+               memcpy(&(vuser->dc.srv_cred), &(vuser->dc.clnt_cred), sizeof(vuser->dc.clnt_cred));
+       }
+
+       /* find the username */
+
+       if (status == 0)
+       {
+               switch (q_l.sam_id.logon_level)
+               {
+                       case INTERACTIVE_LOGON_TYPE:
+                       {
+                               uni_samlogon_user = &(q_l.sam_id.ctr->auth.id1.uni_user_name);
+                               uni_domain        = &(q_l.sam_id.ctr->auth.id1.uni_domain_name);
+
+                               DEBUG(3,("SAM Logon (Interactive). Domain:[%s].  ", global_sam_name));
+                               break;
+                       }
+                       case NET_LOGON_TYPE:
+                       {
+                               uni_samlogon_user = &(q_l.sam_id.ctr->auth.id2.uni_user_name);
+                               uni_domain        = &(q_l.sam_id.ctr->auth.id2.uni_domain_name);
+
+                               DEBUG(3,("SAM Logon (Network). Domain:[%s].  ", global_sam_name));
+                               break;
+                       }
+                       default:
+                       {
+                               DEBUG(2,("SAM Logon: unsupported switch value\n"));
+                               status = 0xC0000000 | NT_STATUS_INVALID_INFO_CLASS;
+                               break;
+                       }
+               } /* end switch */
+       } /* end if status == 0 */
+
+       /* check username exists */
+
+       if (status == 0)
+       {
+               fstrcpy(nt_username, unistr2_to_str(uni_samlogon_user));
+#if 0
+               slprintf(nt_username, sizeof(nt_username), "%s\\%s",
+                        unistr2_to_str(uni_domain),
+                        unistr2_to_str(uni_samlogon_user));
+#endif
+               DEBUG(3,("User:[%s]\n", nt_username));
+
+               become_root(True);
+               sam_pass = getsam21pwntnam(nt_username);
+               unbecome_root(True);
+
+               if (sam_pass == NULL)
+               {
+                       status = 0xC0000000 | NT_STATUS_NO_SUCH_USER;
+               }
+               else if (IS_BITS_SET_ALL(sam_pass->acct_ctrl, ACB_DISABLED) &&
+                        IS_BITS_CLR_ALL(sam_pass->acct_ctrl, ACB_PWNOTREQ))
+               {
+                       status =  0xC0000000 | NT_STATUS_ACCOUNT_DISABLED;
+               }
+       }
+
+       if (status == 0x0)
+       {
+               logon_time            = sam_pass->logon_time;
+               logoff_time           = sam_pass->logoff_time;
+               kickoff_time          = sam_pass->kickoff_time;
+               pass_last_set_time    = sam_pass->pass_last_set_time;
+               pass_can_change_time  = sam_pass->pass_can_change_time;
+               pass_must_change_time = sam_pass->pass_must_change_time;
+
+               fstrcpy(nt_name     , sam_pass->nt_name);
+               fstrcpy(full_name   , sam_pass->full_name);
+               fstrcpy(logon_script, sam_pass->logon_script);
+               fstrcpy(profile_path, sam_pass->profile_path);
+               fstrcpy(home_dir    , sam_pass->home_dir);
+               fstrcpy(dir_drive   , sam_pass->dir_drive);
+
+               user_rid  = sam_pass->user_rid;
+               group_rid = sam_pass->group_rid;
+       }
+
+       /* validate password - if required */
+
+       if (status == 0 && !(IS_BITS_SET_ALL(sam_pass->acct_ctrl, ACB_PWNOTREQ)))
+       {
+               switch (q_l.sam_id.logon_level)
+               {
+                       case INTERACTIVE_LOGON_TYPE:
+                       {
+                               /* interactive login. */
+                               status = net_login_interactive(&q_l.sam_id.ctr->auth.id1, sam_pass, vuser);
+                               break;
+                       }
+                       case NET_LOGON_TYPE:
+                       {
+                               /* network login.  lm challenge and 24 byte responses */
+                               status = net_login_network(&q_l.sam_id.ctr->auth.id2, sam_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.
+       */
+
+       /* return the profile plus other bits :-) */
+
+       if (status == 0)
+       {
+               int num_gids = 0;
+               DOMAIN_GRP *grp_mem = NULL;
+
+               /* set up pointer indicating user/password failed to be found */
+               usr_info.ptr_user_info = 0;
+
+               if (!getusergroupsntnam(nt_username, &grp_mem, &num_gids))
+               {
+                       status = 0xC0000000 | NT_STATUS_INVALID_PRIMARY_GROUP;
+               }
+
+               if (status == 0x0)
+               {
+                       DOM_GID *gids = NULL;
+                       num_gids = make_dom_gids(grp_mem, num_gids, &gids);
+
+                       make_net_user_info3(&usr_info,
+                               &logon_time,
+                               &logoff_time,
+                               &kickoff_time,
+                               &pass_last_set_time,
+                               &pass_can_change_time,
+                               &pass_must_change_time,
+
+                               nt_name         , /* user_name */
+                               full_name       , /* full_name */
+                               logon_script    , /* logon_script */
+                               profile_path    , /* profile_path */
+                               home_dir        , /* home_dir */
+                               dir_drive       , /* dir_drive */
+
+                               0, /* logon_count */
+                               0, /* bad_pw_count */
+
+                               user_rid   , /* RID user_id */
+                               group_rid  , /* RID group_id */
+                               num_gids,    /* uint32 num_groups */
+                               gids    , /* DOM_GID *gids */
+                               0x20    , /* uint32 user_flgs (?) */
+
+                               NULL, /* char sess_key[16] */
+
+                               global_myname  , /* char *logon_srv */
+                               global_sam_name, /* char *logon_dom */
+                               &global_sam_sid, /* DOM_SID *dom_sid */
+                               NULL); /* char *other_sids */
+
+                       /* Free any allocated groups array. */
+                       if (gids)
+                       {
+                               free((char *)gids);
+                       }
+               }
+
+       }
+
+       net_reply_sam_logon(&q_l, rdata, &srv_cred, &usr_info, status);
 }
 
 
 /*************************************************************************
  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)
 {
@@ -825,7 +838,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)
 {