changed *(uint32*)(clnt_cred) to SIVAL(clnt_cred, ....)
[kai/samba.git] / source3 / pipenetlog.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
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.
8    
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.
13    
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.
18    
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.
22 */
23 /*
24    This file handles reply_ calls on named pipes that the server
25    makes to handle specific protocols
26 */
27
28
29 #include "includes.h"
30 #include "trans2.h"
31 #include "nterr.h"
32
33 extern int DEBUGLEVEL;
34
35
36 #ifdef NTDOMAIN
37
38 static void make_lsa_r_req_chal(LSA_R_REQ_CHAL *r_c,
39                                 DOM_CHAL *srv_chal, int status)
40 {
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));
43         r_c->status = status;
44 }
45
46 static int lsa_reply_req_chal(LSA_Q_REQ_CHAL *q_c, char *q, char *base,
47                                         DOM_CHAL *srv_chal)
48 {
49         LSA_R_REQ_CHAL r_c;
50
51         DEBUG(6,("lsa_reply_req_chal: %d\n", __LINE__));
52
53         /* set up the LSA REQUEST CHALLENGE response */
54         make_lsa_r_req_chal(&r_c, srv_chal, 0);
55
56         /* store the response in the SMB stream */
57         q = lsa_io_r_req_chal(False, &r_c, q, base, 4, 0);
58
59         DEBUG(6,("lsa_reply_req_chal: %d\n", __LINE__));
60
61         /* return length of SMB data stored */
62         return PTR_DIFF(q, base);
63 }
64
65 static void make_lsa_r_auth_2(LSA_R_AUTH_2 *r_a,
66                               DOM_CHAL *resp_cred, NEG_FLAGS *flgs, int status)
67 {
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));
70         r_a->status = status;
71 }
72
73 static int lsa_reply_auth_2(LSA_Q_AUTH_2 *q_a, char *q, char *base,
74                                 DOM_CHAL *resp_cred, int status)
75 {
76         LSA_R_AUTH_2 r_a;
77
78         /* set up the LSA AUTH 2 response */
79
80         make_lsa_r_auth_2(&r_a, resp_cred, &(q_a->clnt_flgs), status);
81
82         /* store the response in the SMB stream */
83         q = lsa_io_r_auth_2(False, &r_a, q, base, 4, 0);
84
85         /* return length of SMB data stored */
86         return PTR_DIFF(q, base);
87 }
88
89 static void make_lsa_r_srv_pwset(LSA_R_SRV_PWSET *r_a,
90                              DOM_CRED *srv_cred, int status)  
91 {
92         memcpy(&(r_a->srv_cred), srv_cred, sizeof(r_a->srv_cred));
93         r_a->status = status;
94 }
95
96 static int lsa_reply_srv_pwset(LSA_Q_SRV_PWSET *q_s, char *q, char *base,
97                                 DOM_CRED *srv_cred, int status)
98 {
99         LSA_R_SRV_PWSET r_s;
100
101         /* set up the LSA Server Password Set response */
102         make_lsa_r_srv_pwset(&r_s, srv_cred, status);
103
104         /* store the response in the SMB stream */
105         q = lsa_io_r_srv_pwset(False, &r_s, q, base, 4, 0);
106
107         /* return length of SMB data stored */
108         return PTR_DIFF(q, base);
109 }
110
111 static void make_lsa_user_info(LSA_USER_INFO *usr,
112
113         NTTIME *logon_time,
114         NTTIME *logoff_time,
115         NTTIME *kickoff_time,
116         NTTIME *pass_last_set_time,
117         NTTIME *pass_can_change_time,
118         NTTIME *pass_must_change_time,
119
120         char *user_name,
121         char *full_name,
122         char *logon_script,
123         char *profile_path,
124         char *home_dir,
125         char *dir_drive,
126
127         uint16 logon_count,
128         uint16 bad_pw_count,
129
130         uint32 user_id,
131         uint32 group_id,
132         uint32 num_groups,
133         DOM_GID *gids,
134         uint32 user_flgs,
135
136         char sess_key[16],
137
138         char *logon_srv,
139         char *logon_dom,
140
141         char *dom_sid,
142         char *other_sids) /* space-delimited set of SIDs */ 
143 {
144         /* only cope with one "other" sid, right now. */
145         /* need to count the number of space-delimited sids */
146         int i;
147         int num_other_sids = other_sids != NULL ? 1 : 0;
148
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   );
155
156         int len_logon_srv    = strlen(logon_srv);
157         int len_logon_dom    = strlen(logon_dom);
158
159         usr->undoc_buffer = 1; /* yes, we're bothering to put USER_INFO data here */
160
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;
167
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);
174
175         usr->logon_count = logon_count;
176         usr->bad_pw_count = bad_pw_count;
177
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;
183
184         if (sess_key != NULL)
185         {
186                 memcpy(usr->sess_key, sess_key, sizeof(usr->sess_key));
187         }
188         else
189         {
190                 bzero(usr->sess_key, sizeof(usr->sess_key));
191         }
192
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);
195
196         usr->buffer_dom_id = dom_sid ? 1 : 0; /* yes, we're bothering to put a domain SID in */
197
198         bzero(usr->padding, sizeof(usr->padding));
199
200         usr->num_other_sids = num_other_sids;
201         usr->buffer_other_sids = num_other_sids != 0 ? 1 : 0; 
202         
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);
209
210         usr->num_groups2 = num_groups;
211         for (i = 0; i < num_groups; i++)
212         {
213                 usr->gids[i] = gids[i];
214         }
215
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);
218
219         make_dom_sid(&(usr->dom_sid), dom_sid);
220         make_dom_sid(&(usr->other_sids[0]), other_sids);
221 }
222
223
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)
226 {
227         LSA_R_SAM_LOGON r_s;
228
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));
232
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);
237
238         /* store the response in the SMB stream */
239         q = lsa_io_r_sam_logon(False, &r_s, q, base, 4, 0);
240
241         /* return length of SMB data stored */
242         return PTR_DIFF(q, base);
243 }
244
245
246 static int lsa_reply_sam_logoff(LSA_Q_SAM_LOGOFF *q_s, char *q, char *base,
247                                 DOM_CRED *srv_cred, 
248                                 uint32 status)
249 {
250         LSA_R_SAM_LOGOFF r_s;
251
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));
255
256         r_s.status = status;
257
258         /* store the response in the SMB stream */
259         q = lsa_io_r_sam_logoff(False, &r_s, q, base, 4, 0);
260
261         /* return length of SMB data stored */
262         return PTR_DIFF(q, base);
263 }
264
265
266 static BOOL update_dcinfo(int cnum, uint16 vuid,
267                 struct dcinfo *dc, DOM_CHAL *clnt_chal, char *mach_acct)
268 {
269     struct smb_passwd *smb_pass;
270         int i;
271
272         unbecome_user();
273         smb_pass = get_smbpwnam(mach_acct);
274         if (!become_user(cnum, vuid))
275         {
276                 DEBUG(0,("update_dcinfo: become_user failed\n"));
277                 return False;
278         }
279
280         if (smb_pass != NULL)
281         {
282                 memcpy(dc->md4pw, smb_pass->smb_nt_passwd, sizeof(dc->md4pw));
283                 DEBUG(5,("dc->md4pw(%d) :", sizeof(dc->md4pw)));
284                 dump_data(5, dc->md4pw, 16);
285         }
286         else
287         {
288                 /* No such machine account. Should error out here, but we'll
289                    print and carry on */
290                 DEBUG(1,("No account in domain for %s\n", mach_acct));
291                 return False;
292         }
293
294         {
295                 char foo[16];
296                 for (i = 0; i < 16; i++) sprintf(foo+i*2,"%02x ", dc->md4pw[i]);
297                 DEBUG(4,("pass %s %s\n", mach_acct, foo));
298         }
299
300         /* copy the client credentials */
301         memcpy(dc->clnt_chal.data, clnt_chal->data, sizeof(clnt_chal->data));
302         memcpy(dc->clnt_cred.data, clnt_chal->data, sizeof(clnt_chal->data));
303
304         /* create a server challenge for the client */
305         /* PAXX: set these to random values. */
306         /* lkcl: paul, you mentioned that it doesn't really matter much */
307         for (i = 0; i < 8; i++)
308         {
309                 dc->srv_chal.data[i] = 0xA5;
310                 dc->srv_cred.data[i] = 0xA5;
311         }
312
313         /* from client / server challenges and md4 password, generate sess key */
314         cred_session_key(&(dc->clnt_chal), &(dc->srv_chal),
315                            dc->md4pw, dc->sess_key);
316
317         DEBUG(6,("update_dcinfo: %d\n", __LINE__));
318
319         return True;
320 }
321
322 static void api_lsa_req_chal( int cnum, uint16 vuid,
323                               user_struct *vuser,
324                               char *param, char *data,
325                               char **rdata, int *rdata_len )
326 {
327         LSA_Q_REQ_CHAL q_r;
328
329         fstring mach_acct;
330
331         /* grab the challenge... */
332         lsa_io_q_req_chal(True, &q_r, data + 0x18, data, 4, 0);
333
334         fstrcpy(mach_acct, unistr2(q_r.uni_logon_clnt.buffer));
335
336         strcat(mach_acct, "$");
337
338         DEBUG(6,("q_r.clnt_chal.data(%d) :", sizeof(q_r.clnt_chal.data)));
339         dump_data(6, q_r.clnt_chal.data, 8);
340
341         update_dcinfo(cnum, vuid, &(vuser->dc), &(q_r.clnt_chal), mach_acct);
342
343         /* construct reply.  return status is always 0x0 */
344         *rdata_len = lsa_reply_req_chal(&q_r, *rdata + 0x18, *rdata,
345                                         &(vuser->dc.srv_chal));
346
347 }
348
349 static void api_lsa_auth_2( user_struct *vuser,
350                             char *param, char *data,
351                             char **rdata, int *rdata_len )
352 {
353         LSA_Q_AUTH_2 q_a;
354
355         UTIME srv_time;
356
357         srv_time.time = 0;
358
359         /* grab the challenge... */
360         lsa_io_q_auth_2(True, &q_a, data + 0x18, data, 4, 0);
361
362         /* check that the client credentials are valid */
363         cred_assert(&(q_a.clnt_chal), vuser->dc.sess_key,
364                 &(vuser->dc.clnt_cred), srv_time);
365
366         /* create server challenge for inclusion in the reply */
367         cred_create(vuser->dc.sess_key, &(vuser->dc.srv_cred), srv_time, &(vuser->dc.srv_chal));
368
369         /* update the client credentials (copy server challenge) for use next time */
370         memcpy(vuser->dc.clnt_cred.data, vuser->dc.srv_chal.data, sizeof(vuser->dc.clnt_cred.data));
371
372         /* construct reply. */
373         *rdata_len = lsa_reply_auth_2(&q_a, *rdata + 0x18, *rdata,
374                                         &(vuser->dc.srv_chal), 0x0);
375 }
376
377
378 static BOOL deal_with_credentials(user_struct *vuser,
379                         DOM_CRED *clnt_cred, DOM_CRED *srv_cred)
380 {
381         UTIME new_clnt_time;
382         uint32 new_cred;
383
384         /* doesn't matter that server time is 0 */
385         srv_cred->timestamp.time = 0;
386
387         /* check that the client credentials are valid */
388         if (cred_assert(&(clnt_cred->challenge), vuser->dc.sess_key,
389                     &(vuser->dc.clnt_cred), clnt_cred->timestamp))
390         {
391                 return False;
392         }
393
394         /* increment client time by one second */
395         new_clnt_time.time = clnt_cred->timestamp.time + 1;
396
397         /* create server credentials for inclusion in the reply */
398         cred_create(vuser->dc.sess_key, &(vuser->dc.clnt_cred), new_clnt_time,
399                     &(srv_cred->challenge));
400
401         /* update the client and server credentials, for use next time... */
402         new_cred = IVAL(vuser->dc.clnt_cred.data, 0) + new_clnt_time.time;
403         SIVAL(vuser->dc.clnt_cred.data, new_cred, 0);
404         SIVAL(vuser->dc.srv_cred.data , new_cred, 0);
405
406         return True;
407 }
408
409 static void api_lsa_srv_pwset( user_struct *vuser,
410                                char *param, char *data,
411                                char **rdata, int *rdata_len )
412 {
413         LSA_Q_SRV_PWSET q_a;
414
415         DOM_CRED srv_cred;
416
417         /* grab the challenge and encrypted password ... */
418         lsa_io_q_srv_pwset(True, &q_a, data + 0x18, data, 4, 0);
419
420         /* checks and updates credentials.  creates reply credentials */
421         deal_with_credentials(vuser, &(q_a.clnt_id.cred), &srv_cred);
422
423         /* construct reply.  always indicate failure.  nt keeps going... */
424         *rdata_len = lsa_reply_srv_pwset(&q_a, *rdata + 0x18, *rdata,
425                                         &srv_cred,
426                         NT_STATUS_WRONG_PASSWORD|0xC000000);
427 }
428
429
430 static void api_lsa_sam_logoff( user_struct *vuser,
431                                char *param, char *data,
432                                char **rdata, int *rdata_len )
433 {
434         LSA_Q_SAM_LOGOFF q_l;
435
436         DOM_CRED srv_cred;
437
438         /* grab the challenge... */
439         lsa_io_q_sam_logoff(True, &q_l, data + 0x18, data, 4, 0);
440
441         /* checks and updates credentials.  creates reply credentials */
442         deal_with_credentials(vuser, &(q_l.sam_id.client.cred), &srv_cred);
443
444         /* construct reply.  always indicate success */
445         *rdata_len = lsa_reply_sam_logoff(&q_l, *rdata + 0x18, *rdata,
446                                         &srv_cred,
447                         0x0);
448 }
449
450
451 static void api_lsa_sam_logon( user_struct *vuser,
452                                char *param, char *data,
453                                char **rdata, int *rdata_len )
454 {
455         LSA_Q_SAM_LOGON q_l;
456         LSA_USER_INFO usr_info;
457         LSA_USER_INFO *p_usr_info = NULL;
458
459         DOM_CRED srv_creds;
460
461         lsa_io_q_sam_logon(True, &q_l, data + 0x18, data, 4, 0);
462
463         /* checks and updates credentials.  creates reply credentials */
464         deal_with_credentials(vuser, &(q_l.sam_id.client.cred), &srv_creds);
465
466         if (vuser != NULL)
467         {
468                 NTTIME dummy_time;
469                 pstring logon_script;
470                 pstring profile_path;
471                 pstring home_dir;
472                 pstring home_drive;
473                 pstring my_name;
474                 pstring my_workgroup;
475                 pstring dom_sid;
476                 pstring username;
477                 extern pstring myname;
478
479                 dummy_time.low  = 0xffffffff;
480                 dummy_time.high = 0x7fffffff;
481
482                 get_myname(myname, NULL);
483
484                 pstrcpy(logon_script, lp_logon_script());
485                 pstrcpy(profile_path, lp_logon_path  ());
486                 pstrcpy(dom_sid     , lp_domainsid   ());
487                 pstrcpy(my_workgroup, lp_workgroup   ());
488
489                 pstrcpy(username, unistr2(q_l.sam_id.client.login.uni_acct_name.buffer));
490                 pstrcpy(my_name     , myname           );
491                 strupper(my_name);
492
493                 pstrcpy(home_drive  , "a:"             );
494
495 #if (defined(NETGROUP) && defined(AUTOMOUNT))
496                 pstrcpy(home_dir    , vuser->home_share);
497 #else
498                 pstrcpy(home_dir    , "\\\\%L\\%U");
499                 standard_sub_basic(home_dir);
500 #endif
501
502                 p_usr_info = &usr_info;
503
504                 make_lsa_user_info(p_usr_info,
505
506                                &dummy_time, /* logon_time */
507                                &dummy_time, /* logoff_time */
508                                &dummy_time, /* kickoff_time */
509                                &dummy_time, /* pass_last_set_time */
510                                &dummy_time, /* pass_can_change_time */
511                                &dummy_time, /* pass_must_change_time */
512
513                                username, /* user_name */
514                                vuser->real_name, /* full_name */
515                                logon_script, /* logon_script */
516                                profile_path, /* profile_path */
517                                home_dir, /* home_dir */
518                                home_drive, /* dir_drive */
519
520                                0, /* logon_count */
521                                0, /* bad_pw_count */
522
523                                vuser->uid, /* uint32 user_id */
524                                vuser->gid, /* uint32 group_id */
525                                0,    /* uint32 num_groups */
526                                NULL, /* DOM_GID *gids */
527                                0x20, /* uint32 user_flgs */
528
529                                NULL, /* char sess_key[16] */
530
531                                my_name, /* char *logon_srv */
532                                my_workgroup, /* char *logon_dom */
533
534                                dom_sid, /* char *dom_sid */
535                                NULL); /* char *other_sids */
536         }
537
538         *rdata_len = lsa_reply_sam_logon(&q_l, *rdata + 0x18, *rdata,
539                                         &srv_creds, p_usr_info);
540 }
541
542
543 BOOL api_netlogrpcTNP(int cnum,int uid, char *param,char *data,
544                      int mdrcnt,int mprcnt,
545                      char **rdata,char **rparam,
546                      int *rdata_len,int *rparam_len)
547 {
548         uint16 opnum = SVAL(data,22);
549         int pkttype  = CVAL(data, 2);
550
551         user_struct *vuser;
552
553         if (pkttype == 0x0b) /* RPC BIND */
554         {
555                 DEBUG(4,("netlogon rpc bind %x\n",pkttype));
556                 LsarpcTNP1(data,rdata,rdata_len);
557                 return True;
558         }
559
560         DEBUG(4,("netlogon TransactNamedPipe op %x\n",opnum));
561
562         if ((vuser = get_valid_user_struct(uid)) == NULL) return False;
563
564         DEBUG(3,("Username of UID %d is %s\n", vuser->uid, vuser->name));
565 #if defined(NETGROUP) && defined(AUTOMOUNT)
566         DEBUG(3,("HOMESHR for %s is %s\n", vuser->name, vuser->home_share));
567 #endif
568
569         switch (opnum)
570         {
571                 case LSA_REQCHAL:
572                 {
573                         DEBUG(3,("LSA_REQCHAL\n"));
574                         api_lsa_req_chal(cnum, uid, vuser, param, data, rdata, rdata_len);
575                         make_rpc_reply(data, *rdata, *rdata_len);
576                         break;
577                 }
578
579                 case LSA_AUTH2:
580                 {
581                         DEBUG(3,("LSA_AUTH2\n"));
582                         api_lsa_auth_2(vuser, param, data, rdata, rdata_len);
583                         make_rpc_reply(data, *rdata, *rdata_len);
584                         break;
585                 }
586
587                 case LSA_SRVPWSET:
588                 {
589                         DEBUG(3,("LSA_SRVPWSET\n"));
590                         api_lsa_srv_pwset(vuser, param, data, rdata, rdata_len);
591                         make_rpc_reply(data, *rdata, *rdata_len);
592                         break;
593                 }
594
595                 case LSA_SAMLOGON:
596                 {
597                         DEBUG(3,("LSA_SAMLOGON\n"));
598                         api_lsa_sam_logon(vuser, param, data, rdata, rdata_len);
599                         make_rpc_reply(data, *rdata, *rdata_len);
600                         break;
601                 }
602
603                 case LSA_SAMLOGOFF:
604                 {
605                         DEBUG(3,("LSA_SAMLOGOFF\n"));
606                         api_lsa_sam_logoff(vuser, param, data, rdata, rdata_len);
607                         break;
608                 }
609
610                 default:
611                 {
612                         DEBUG(4, ("**** netlogon, unknown code: %lx\n", opnum));
613                         break;
614                 }
615         }
616
617         return True;
618 }
619
620 #endif /* NTDOMAIN */