2 Unix SMB/Netbios implementation.
4 Pipe SMB reply routines
5 Copyright (C) Andrew Tridgell 1992-1997,
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1997.
7 Copyright (C) Paul Ashton 1997.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 This file handles reply_ calls on named pipes that the server
25 makes to handle specific protocols
33 extern int DEBUGLEVEL;
38 static void make_lsa_r_req_chal(LSA_R_REQ_CHAL *r_c,
39 DOM_CHAL *srv_chal, int status)
41 DEBUG(6,("make_lsa_r_req_chal: %d\n", __LINE__));
42 memcpy(r_c->srv_chal.data, srv_chal->data, sizeof(srv_chal->data));
46 static int lsa_reply_req_chal(LSA_Q_REQ_CHAL *q_c, char *q, char *base,
51 DEBUG(6,("lsa_reply_req_chal: %d\n", __LINE__));
53 /* set up the LSA REQUEST CHALLENGE response */
54 make_lsa_r_req_chal(&r_c, srv_chal, 0);
56 /* store the response in the SMB stream */
57 q = lsa_io_r_req_chal(False, &r_c, q, base, 4, 0);
59 DEBUG(6,("lsa_reply_req_chal: %d\n", __LINE__));
61 /* return length of SMB data stored */
62 return PTR_DIFF(q, base);
65 static void make_lsa_r_auth_2(LSA_R_AUTH_2 *r_a,
66 DOM_CHAL *resp_cred, NEG_FLAGS *flgs, int status)
68 memcpy( r_a->srv_chal.data, resp_cred->data, sizeof(resp_cred->data));
69 memcpy(&(r_a->srv_flgs) , flgs , sizeof(r_a->srv_flgs));
73 static int lsa_reply_auth_2(LSA_Q_AUTH_2 *q_a, char *q, char *base,
74 DOM_CHAL *resp_cred, int status)
78 /* set up the LSA AUTH 2 response */
80 make_lsa_r_auth_2(&r_a, resp_cred, &(q_a->clnt_flgs), status);
82 /* store the response in the SMB stream */
83 q = lsa_io_r_auth_2(False, &r_a, q, base, 4, 0);
85 /* return length of SMB data stored */
86 return PTR_DIFF(q, base);
89 static void make_lsa_r_srv_pwset(LSA_R_SRV_PWSET *r_a,
90 DOM_CRED *srv_cred, int status)
92 memcpy(&(r_a->srv_cred), srv_cred, sizeof(r_a->srv_cred));
96 static int lsa_reply_srv_pwset(LSA_Q_SRV_PWSET *q_s, char *q, char *base,
97 DOM_CRED *srv_cred, int status)
101 /* set up the LSA Server Password Set response */
102 make_lsa_r_srv_pwset(&r_s, srv_cred, status);
104 /* store the response in the SMB stream */
105 q = lsa_io_r_srv_pwset(False, &r_s, q, base, 4, 0);
107 /* return length of SMB data stored */
108 return PTR_DIFF(q, base);
111 static void make_lsa_user_info(LSA_USER_INFO *usr,
115 NTTIME *kickoff_time,
116 NTTIME *pass_last_set_time,
117 NTTIME *pass_can_change_time,
118 NTTIME *pass_must_change_time,
142 char *other_sids) /* space-delimited set of SIDs */
144 /* only cope with one "other" sid, right now. */
145 /* need to count the number of space-delimited sids */
147 int num_other_sids = other_sids != NULL ? 1 : 0;
149 int len_user_name = strlen(user_name );
150 int len_full_name = strlen(full_name );
151 int len_logon_script = strlen(logon_script);
152 int len_profile_path = strlen(profile_path);
153 int len_home_dir = strlen(home_dir );
154 int len_dir_drive = strlen(dir_drive );
156 int len_logon_srv = strlen(logon_srv);
157 int len_logon_dom = strlen(logon_dom);
159 usr->undoc_buffer = 1; /* yes, we're bothering to put USER_INFO data here */
161 usr->logon_time = *logon_time;
162 usr->logoff_time = *logoff_time;
163 usr->kickoff_time = *kickoff_time;
164 usr->pass_last_set_time = *pass_last_set_time;
165 usr->pass_can_change_time = *pass_can_change_time;
166 usr->pass_must_change_time = *pass_must_change_time;
168 make_uni_hdr(&(usr->hdr_user_name ), len_user_name , len_user_name , 4);
169 make_uni_hdr(&(usr->hdr_full_name ), len_full_name , len_full_name , 4);
170 make_uni_hdr(&(usr->hdr_logon_script), len_logon_script, len_logon_script, 4);
171 make_uni_hdr(&(usr->hdr_profile_path), len_profile_path, len_profile_path, 4);
172 make_uni_hdr(&(usr->hdr_home_dir ), len_home_dir , len_home_dir , 4);
173 make_uni_hdr(&(usr->hdr_dir_drive ), len_dir_drive , len_dir_drive , 4);
175 usr->logon_count = logon_count;
176 usr->bad_pw_count = bad_pw_count;
178 usr->user_id = user_id;
179 usr->group_id = group_id;
180 usr->num_groups = num_groups;
181 usr->buffer_groups = num_groups ? 1 : 0; /* yes, we're bothering to put group info in */
182 usr->user_flgs = user_flgs;
184 if (sess_key != NULL)
186 memcpy(usr->sess_key, sess_key, sizeof(usr->sess_key));
190 bzero(usr->sess_key, sizeof(usr->sess_key));
193 make_uni_hdr(&(usr->hdr_logon_srv), len_logon_srv, len_logon_srv, 4);
194 make_uni_hdr(&(usr->hdr_logon_dom), len_logon_dom, len_logon_dom, 4);
196 usr->buffer_dom_id = dom_sid ? 1 : 0; /* yes, we're bothering to put a domain SID in */
198 bzero(usr->padding, sizeof(usr->padding));
200 usr->num_other_sids = num_other_sids;
201 usr->buffer_other_sids = num_other_sids != 0 ? 1 : 0;
203 make_unistr2(&(usr->uni_user_name ), user_name , len_user_name , 0);
204 make_unistr2(&(usr->uni_full_name ), full_name , len_full_name , 0);
205 make_unistr2(&(usr->uni_logon_script), logon_script, len_logon_script, 0);
206 make_unistr2(&(usr->uni_profile_path), profile_path, len_profile_path, 0);
207 make_unistr2(&(usr->uni_home_dir ), home_dir , len_home_dir , 0);
208 make_unistr2(&(usr->uni_dir_drive ), dir_drive , len_dir_drive , 0);
210 usr->num_groups2 = num_groups;
211 for (i = 0; i < num_groups; i++)
213 usr->gids[i] = gids[i];
216 make_unistr2(&(usr->uni_logon_srv), logon_srv, len_logon_srv, 0);
217 make_unistr2(&(usr->uni_logon_dom), logon_dom, len_logon_dom, 0);
219 make_dom_sid(&(usr->dom_sid), dom_sid);
220 make_dom_sid(&(usr->other_sids[0]), other_sids);
224 static int lsa_reply_sam_logon(LSA_Q_SAM_LOGON *q_s, char *q, char *base,
225 DOM_CRED *srv_cred, LSA_USER_INFO *user_info)
229 /* XXXX maybe we want to say 'no', reject the client's credentials */
230 r_s.buffer_creds = 1; /* yes, we have valid server credentials */
231 memcpy(&(r_s.srv_creds), srv_cred, sizeof(r_s.srv_creds));
233 /* store the user information, if there is any. */
234 r_s.user = user_info;
235 r_s.buffer_user = user_info != NULL ? 1 : 0;
236 r_s.status = user_info != NULL ? 0 : (0xC000000|NT_STATUS_NO_SUCH_USER);
238 /* store the response in the SMB stream */
239 q = lsa_io_r_sam_logon(False, &r_s, q, base, 4, 0);
241 /* return length of SMB data stored */
242 return PTR_DIFF(q, base);
246 static int lsa_reply_sam_logoff(LSA_Q_SAM_LOGOFF *q_s, char *q, char *base,
250 LSA_R_SAM_LOGOFF r_s;
252 /* XXXX maybe we want to say 'no', reject the client's credentials */
253 r_s.buffer_creds = 1; /* yes, we have valid server credentials */
254 memcpy(&(r_s.srv_creds), srv_cred, sizeof(r_s.srv_creds));
258 /* store the response in the SMB stream */
259 q = lsa_io_r_sam_logoff(False, &r_s, q, base, 4, 0);
261 /* return length of SMB data stored */
262 return PTR_DIFF(q, base);
266 static BOOL update_dcinfo(int cnum, uint16 vuid,
267 struct dcinfo *dc, DOM_CHAL *clnt_chal, char *mach_acct)
269 struct smb_passwd *smb_pass;
273 smb_pass = get_smbpwnam(mach_acct);
274 become_user(cnum, vuid);
276 if (smb_pass != NULL)
278 memcpy(dc->md4pw, smb_pass->smb_nt_passwd, sizeof(dc->md4pw));
279 DEBUG(5,("dc->md4pw(%d) :", sizeof(dc->md4pw)));
280 dump_data(5, dc->md4pw, 16);
284 /* No such machine account. Should error out here, but we'll
285 print and carry on */
286 DEBUG(1,("No account in domain for %s\n", mach_acct));
292 for (i = 0; i < 16; i++) sprintf(foo+i*2,"%02x ", dc->md4pw[i]);
293 DEBUG(4,("pass %s %s\n", mach_acct, foo));
296 /* copy the client credentials */
297 memcpy(dc->clnt_chal.data, clnt_chal->data, sizeof(clnt_chal->data));
298 memcpy(dc->clnt_cred.data, clnt_chal->data, sizeof(clnt_chal->data));
300 /* create a server challenge for the client */
301 /* PAXX: set these to random values. */
302 /* lkcl: paul, you mentioned that it doesn't really matter much */
303 for (i = 0; i < 8; i++)
305 dc->srv_chal.data[i] = 0xA5;
308 /* from client / server challenges and md4 password, generate sess key */
309 cred_session_key(&(dc->clnt_chal), &(dc->srv_chal),
310 dc->md4pw, dc->sess_key);
312 DEBUG(6,("update_dcinfo: %d\n", __LINE__));
317 static void api_lsa_req_chal( int cnum, uint16 vuid,
319 char *param, char *data,
320 char **rdata, int *rdata_len )
326 /* grab the challenge... */
327 lsa_io_q_req_chal(True, &q_r, data + 0x18, data, 4, 0);
329 fstrcpy(mach_acct, unistr2(q_r.uni_logon_clnt.buffer));
331 strcat(mach_acct, "$");
333 DEBUG(6,("q_r.clnt_chal.data(%d) :", sizeof(q_r.clnt_chal.data)));
334 dump_data(6, q_r.clnt_chal.data, 8);
336 update_dcinfo(cnum, vuid, &(vuser->dc), &(q_r.clnt_chal), mach_acct);
338 /* construct reply. return status is always 0x0 */
339 *rdata_len = lsa_reply_req_chal(&q_r, *rdata + 0x18, *rdata,
340 &(vuser->dc.srv_chal));
344 static void api_lsa_auth_2( user_struct *vuser,
345 char *param, char *data,
346 char **rdata, int *rdata_len )
355 /* grab the challenge... */
356 lsa_io_q_auth_2(True, &q_a, data + 0x18, data, 4, 0);
358 /* check that the client credentials are valid */
359 cred_assert(&(q_a.clnt_chal), vuser->dc.sess_key,
360 &(vuser->dc.srv_cred), srv_time);
362 /* create server credentials for inclusion in the reply */
363 cred_create(vuser->dc.sess_key, &(vuser->dc.clnt_cred), srv_time, &srv_chal);
365 /* construct reply. */
366 *rdata_len = lsa_reply_auth_2(&q_a, *rdata + 0x18, *rdata,
371 static BOOL deal_with_credentials(user_struct *vuser,
372 DOM_CRED *clnt_cred, DOM_CRED *srv_cred)
376 /* doesn't matter that server time is 0 */
377 srv_cred->timestamp.time = 0;
379 /* check that the client credentials are valid */
380 if (cred_assert(&(clnt_cred->challenge), vuser->dc.sess_key,
381 &(vuser->dc.srv_cred), clnt_cred->timestamp))
386 /* increment client time by one second */
387 new_clnt_time.time = clnt_cred->timestamp.time + 1;
389 /* create server credentials for inclusion in the reply */
390 cred_create(vuser->dc.sess_key, &(vuser->dc.clnt_cred), new_clnt_time,
391 &(srv_cred->challenge));
393 /* update the client and server credentials, for use next time... */
394 *(uint32*)(vuser->dc.srv_cred.data) = ( *(uint32*)(vuser->dc.clnt_cred.data) += new_clnt_time.time );
399 static void api_lsa_srv_pwset( user_struct *vuser,
400 char *param, char *data,
401 char **rdata, int *rdata_len )
407 /* grab the challenge and encrypted password ... */
408 lsa_io_q_srv_pwset(True, &q_a, data + 0x18, data, 4, 0);
410 /* checks and updates credentials. creates reply credentials */
411 deal_with_credentials(vuser, &(q_a.clnt_id.cred), &srv_cred);
413 /* construct reply. always indicate failure. nt keeps going... */
414 *rdata_len = lsa_reply_srv_pwset(&q_a, *rdata + 0x18, *rdata,
416 NT_STATUS_WRONG_PASSWORD|0xC000000);
420 static void api_lsa_sam_logoff( user_struct *vuser,
421 char *param, char *data,
422 char **rdata, int *rdata_len )
424 LSA_Q_SAM_LOGOFF q_l;
428 /* grab the challenge... */
429 lsa_io_q_sam_logoff(True, &q_l, data + 0x18, data, 4, 0);
431 /* checks and updates credentials. creates reply credentials */
432 deal_with_credentials(vuser, &(q_l.sam_id.client.cred), &srv_cred);
434 /* construct reply. always indicate success */
435 *rdata_len = lsa_reply_sam_logoff(&q_l, *rdata + 0x18, *rdata,
441 static void api_lsa_sam_logon( user_struct *vuser,
442 char *param, char *data,
443 char **rdata, int *rdata_len )
446 LSA_USER_INFO usr_info;
447 LSA_USER_INFO *p_usr_info = NULL;
451 lsa_io_q_sam_logon(True, &q_l, data + 0x18, data, 4, 0);
453 /* checks and updates credentials. creates reply credentials */
454 deal_with_credentials(vuser, &(q_l.sam_id.client.cred), &srv_creds);
459 pstring logon_script;
460 pstring profile_path;
464 pstring my_workgroup;
467 extern pstring myname;
469 dummy_time.low = 0xffffffff;
470 dummy_time.high = 0x7fffffff;
472 get_myname(myname, NULL);
474 pstrcpy(logon_script, lp_logon_script());
475 pstrcpy(profile_path, lp_logon_path ());
476 pstrcpy(dom_sid , lp_domainsid ());
477 pstrcpy(my_workgroup, lp_workgroup ());
479 pstrcpy(username, unistr2(q_l.sam_id.client.login.uni_acct_name.buffer));
480 pstrcpy(my_name , myname );
483 pstrcpy(home_drive , "a:" );
485 #if (defined(NETGROUP) && defined(AUTOMOUNT))
486 pstrcpy(home_dir , vuser->home_share);
488 pstrcpy(home_dir , "\\\\%L\\%U");
489 standard_sub_basic(home_dir);
492 p_usr_info = &usr_info;
494 make_lsa_user_info(p_usr_info,
496 &dummy_time, /* logon_time */
497 &dummy_time, /* logoff_time */
498 &dummy_time, /* kickoff_time */
499 &dummy_time, /* pass_last_set_time */
500 &dummy_time, /* pass_can_change_time */
501 &dummy_time, /* pass_must_change_time */
503 username, /* user_name */
504 vuser->real_name, /* full_name */
505 logon_script, /* logon_script */
506 profile_path, /* profile_path */
507 home_dir, /* home_dir */
508 home_drive, /* dir_drive */
511 0, /* bad_pw_count */
513 vuser->uid, /* uint32 user_id */
514 vuser->gid, /* uint32 group_id */
515 0, /* uint32 num_groups */
516 NULL, /* DOM_GID *gids */
517 0x20, /* uint32 user_flgs */
519 NULL, /* char sess_key[16] */
521 my_name, /* char *logon_srv */
522 my_workgroup, /* char *logon_dom */
524 dom_sid, /* char *dom_sid */
525 NULL); /* char *other_sids */
528 *rdata_len = lsa_reply_sam_logon(&q_l, *rdata + 0x18, *rdata,
529 &srv_creds, p_usr_info);
533 BOOL api_netlogrpcTNP(int cnum,int uid, char *param,char *data,
534 int mdrcnt,int mprcnt,
535 char **rdata,char **rparam,
536 int *rdata_len,int *rparam_len)
538 uint16 opnum = SVAL(data,22);
539 int pkttype = CVAL(data, 2);
543 if (pkttype == 0x0b) /* RPC BIND */
545 DEBUG(4,("netlogon rpc bind %x\n",pkttype));
546 LsarpcTNP1(data,rdata,rdata_len);
550 DEBUG(4,("netlogon TransactNamedPipe op %x\n",opnum));
552 if ((vuser = get_valid_user_struct(uid)) == NULL) return False;
554 DEBUG(3,("Username of UID %d is %s\n", vuser->uid, vuser->name));
555 #if defined(NETGROUP) && defined(AUTOMOUNT)
556 DEBUG(3,("HOMESHR for %s is %s\n", vuser->name, vuser->home_share));
563 DEBUG(3,("LSA_REQCHAL\n"));
564 api_lsa_req_chal(cnum, uid, vuser, param, data, rdata, rdata_len);
565 make_rpc_reply(data, *rdata, *rdata_len);
571 DEBUG(3,("LSA_AUTH2\n"));
572 api_lsa_auth_2(vuser, param, data, rdata, rdata_len);
573 make_rpc_reply(data, *rdata, *rdata_len);
579 DEBUG(3,("LSA_SRVPWSET\n"));
580 api_lsa_srv_pwset(vuser, param, data, rdata, rdata_len);
581 make_rpc_reply(data, *rdata, *rdata_len);
587 DEBUG(3,("LSA_SAMLOGON\n"));
588 api_lsa_sam_logon(vuser, param, data, rdata, rdata_len);
589 make_rpc_reply(data, *rdata, *rdata_len);
595 DEBUG(3,("LSA_SAMLOGOFF\n"));
596 api_lsa_sam_logoff(vuser, param, data, rdata, rdata_len);
602 DEBUG(4, ("**** netlogon, unknown code: %lx\n", opnum));
610 #endif /* NTDOMAIN */