r23779: Change from v2 or later to v3 or later.
[sfrench/samba-autobuild/.git] / source3 / nmbd / nmbd_processlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6    Copyright (C) Jeremy Allison 1994-2003
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
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 3 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    Revision History:
24
25 */
26
27 #include "includes.h"
28
29 struct sam_database_info {
30         uint32 index;
31         uint32 serial_lo, serial_hi;
32         uint32 date_lo, date_hi;
33 };
34
35 /****************************************************************************
36 Send a message to smbd to do a sam delta sync
37 **************************************************************************/
38
39 static void send_repl_message(uint32 low_serial)
40 {
41         DEBUG(3, ("sending replication message, serial = 0x%04x\n", 
42                   low_serial));
43         message_send_all(nmbd_messaging_context(), MSG_SMB_SAM_REPL,
44                          &low_serial, sizeof(low_serial), NULL);
45 }
46
47 /****************************************************************************
48 Process a domain logon packet
49 **************************************************************************/
50
51 void process_logon_packet(struct packet_struct *p, char *buf,int len, 
52                           const char *mailslot)
53 {
54         struct dgram_packet *dgram = &p->packet.dgram;
55         pstring my_name;
56         fstring reply_name;
57         pstring outbuf;
58         int code;
59         uint16 token = 0;
60         uint32 ntversion = 0;
61         uint16 lmnttoken = 0;
62         uint16 lm20token = 0;
63         uint32 domainsidsize;
64         BOOL short_request = False;
65         char *getdc;
66         char *uniuser; /* Unicode user name. */
67         pstring ascuser;
68         char *unicomp; /* Unicode computer name. */
69
70         memset(outbuf, 0, sizeof(outbuf));
71
72         if (!lp_domain_logons()) {
73                 DEBUG(5,("process_logon_packet: Logon packet received from IP %s and domain \
74 logons are not enabled.\n", inet_ntoa(p->ip) ));
75                 return;
76         }
77
78         pstrcpy(my_name, global_myname());
79
80         code = get_safe_SVAL(buf,len,buf,0,-1);
81         DEBUG(4,("process_logon_packet: Logon from %s: code = 0x%x\n", inet_ntoa(p->ip), code));
82
83         switch (code) {
84                 case 0:    
85                         {
86                                 fstring mach_str, user_str, getdc_str;
87                                 char *q = buf + 2;
88                                 char *machine = q;
89                                 char *user = skip_string(buf,len,machine);
90
91                                 if (!user || PTR_DIFF(user, buf) >= len) {
92                                         DEBUG(0,("process_logon_packet: bad packet\n"));
93                                         return;
94                                 }
95                                 getdc = skip_string(buf,len,user);
96
97                                 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
98                                         DEBUG(0,("process_logon_packet: bad packet\n"));
99                                         return;
100                                 }
101                                 q = skip_string(buf,len,getdc);
102
103                                 if (!q || PTR_DIFF(q + 5, buf) > len) {
104                                         DEBUG(0,("process_logon_packet: bad packet\n"));
105                                         return;
106                                 }
107                                 token = SVAL(q,3);
108
109                                 fstrcpy(reply_name,my_name); 
110
111                                 pull_ascii_fstring(mach_str, machine);
112                                 pull_ascii_fstring(user_str, user);
113                                 pull_ascii_fstring(getdc_str, getdc);
114
115                                 DEBUG(5,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
116                                         mach_str,inet_ntoa(p->ip),user_str,token));
117
118                                 q = outbuf;
119                                 SSVAL(q, 0, 6);
120                                 q += 2;
121
122                                 fstrcpy(reply_name, "\\\\");
123                                 fstrcat(reply_name, my_name);
124                                 push_ascii_fstring(q, reply_name);
125                                 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
126
127                                 SSVAL(q, 0, token);
128                                 q += 2;
129
130                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
131
132                                 send_mailslot(True, getdc_str, 
133                                                 outbuf,PTR_DIFF(q,outbuf),
134                                                 global_myname(), 0x0,
135                                                 mach_str,
136                                                 dgram->source_name.name_type,
137                                                 p->ip, *iface_ip(p->ip), p->port);  
138                                 break;
139                         }
140
141                 case QUERYFORPDC:
142                         {
143                                 fstring mach_str, getdc_str;
144                                 fstring source_name;
145                                 char *q = buf + 2;
146                                 char *machine = q;
147
148                                 if (!lp_domain_master()) {  
149                                         /* We're not Primary Domain Controller -- ignore this */
150                                         return;
151                                 }
152
153                                 getdc = skip_string(buf,len,machine);
154
155                                 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
156                                         DEBUG(0,("process_logon_packet: bad packet\n"));
157                                         return;
158                                 }
159                                 q = skip_string(buf,len,getdc);
160
161                                 if (!q || PTR_DIFF(q, buf) >= len) {
162                                         DEBUG(0,("process_logon_packet: bad packet\n"));
163                                         return;
164                                 }
165                                 q = ALIGN2(q, buf);
166
167                                 /* At this point we can work out if this is a W9X or NT style
168                                    request. Experiments show that the difference is wether the
169                                    packet ends here. For a W9X request we now end with a pair of
170                                    bytes (usually 0xFE 0xFF) whereas with NT we have two further
171                                    strings - the following is a simple way of detecting this */
172
173                                 if (len - PTR_DIFF(q, buf) <= 3) {
174                                         short_request = True;
175                                 } else {
176                                         unicomp = q;
177
178                                         if (PTR_DIFF(q, buf) >= len) {
179                                                 DEBUG(0,("process_logon_packet: bad packet\n"));
180                                                 return;
181                                         }
182
183                                         /* A full length (NT style) request */
184                                         q = skip_unibuf(unicomp, PTR_DIFF(buf + len, unicomp));
185
186                                         if (PTR_DIFF(q, buf) >= len) {
187                                                 DEBUG(0,("process_logon_packet: bad packet\n"));
188                                                 return;
189                                         }
190
191                                         if (len - PTR_DIFF(q, buf) > 8) {
192                                                 /* with NT5 clients we can sometimes
193                                                         get additional data - a length specificed string
194                                                         containing the domain name, then 16 bytes of
195                                                         data (no idea what it is) */
196                                                 int dom_len = CVAL(q, 0);
197                                                 q++;
198                                                 if (dom_len != 0) {
199                                                         q += dom_len + 1;
200                                                 }
201                                                 q += 16;
202                                         }
203
204                                         if (PTR_DIFF(q + 8, buf) > len) {
205                                                 DEBUG(0,("process_logon_packet: bad packet\n"));
206                                                 return;
207                                         }
208
209                                         ntversion = IVAL(q, 0);
210                                         lmnttoken = SVAL(q, 4);
211                                         lm20token = SVAL(q, 6);
212                                 }
213
214                                 /* Construct reply. */
215                                 q = outbuf;
216                                 SSVAL(q, 0, QUERYFORPDC_R);
217                                 q += 2;
218
219                                 fstrcpy(reply_name,my_name);
220                                 push_ascii_fstring(q, reply_name);
221                                 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
222
223                                 /* PDC and domain name */
224                                 if (!short_request) {
225                                         /* Make a full reply */
226                                         q = ALIGN2(q, outbuf);
227
228                                         q += dos_PutUniCode(q, my_name, sizeof(pstring), True); /* PDC name */
229                                         q += dos_PutUniCode(q, lp_workgroup(),sizeof(pstring), True); /* Domain name*/
230                                         SIVAL(q, 0, 1); /* our nt version */
231                                         SSVAL(q, 4, 0xffff); /* our lmnttoken */
232                                         SSVAL(q, 6, 0xffff); /* our lm20token */
233                                         q += 8;
234                                 }
235
236                                 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
237
238                                 pull_ascii_fstring(mach_str, machine);
239
240                                 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, \
241 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
242                                         mach_str,inet_ntoa(p->ip), reply_name, lp_workgroup(),
243                                         QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
244                                         (uint32)lm20token ));
245
246                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
247
248                                 pull_ascii_fstring(getdc_str, getdc);
249                                 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
250
251                                 send_mailslot(True, getdc_str,
252                                         outbuf,PTR_DIFF(q,outbuf),
253                                         global_myname(), 0x0,
254                                         source_name,
255                                         dgram->source_name.name_type,
256                                         p->ip, *iface_ip(p->ip), p->port);  
257                                 return;
258                         }
259
260                 case SAMLOGON:
261
262                         {
263                                 fstring getdc_str;
264                                 fstring source_name;
265                                 char *q = buf + 2;
266                                 fstring asccomp;
267
268                                 q += 2;
269
270                                 if (PTR_DIFF(q, buf) >= len) {
271                                         DEBUG(0,("process_logon_packet: bad packet\n"));
272                                         return;
273                                 }
274
275                                 unicomp = q;
276                                 uniuser = skip_unibuf(unicomp, PTR_DIFF(buf+len, unicomp));
277
278                                 if (PTR_DIFF(uniuser, buf) >= len) {
279                                         DEBUG(0,("process_logon_packet: bad packet\n"));
280                                         return;
281                                 }
282
283                                 getdc = skip_unibuf(uniuser,PTR_DIFF(buf+len, uniuser));
284
285                                 if (PTR_DIFF(getdc, buf) >= len) {
286                                         DEBUG(0,("process_logon_packet: bad packet\n"));
287                                         return;
288                                 }
289
290                                 q = skip_string(buf,len,getdc);
291
292                                 if (!q || PTR_DIFF(q + 8, buf) >= len) {
293                                         DEBUG(0,("process_logon_packet: bad packet\n"));
294                                         return;
295                                 }
296
297                                 q += 4; /* Account Control Bits - indicating username type */
298                                 domainsidsize = IVAL(q, 0);
299                                 q += 4;
300
301                                 DEBUG(5,("process_logon_packet: SAMLOGON sidsize %d, len = %d\n", domainsidsize, len));
302
303                                 if (domainsidsize < (len - PTR_DIFF(q, buf)) && (domainsidsize != 0)) {
304                                         q += domainsidsize;
305                                         q = ALIGN4(q, buf);
306                                 }
307
308                                 DEBUG(5,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %ld\n", len, (unsigned long)PTR_DIFF(q, buf) ));
309
310                                 if (len - PTR_DIFF(q, buf) > 8) {
311                                         /* with NT5 clients we can sometimes
312                                                 get additional data - a length specificed string
313                                                 containing the domain name, then 16 bytes of
314                                                 data (no idea what it is) */
315                                         int dom_len = CVAL(q, 0);
316                                         q++;
317                                         if (dom_len < (len - PTR_DIFF(q, buf)) && (dom_len != 0)) {
318                                                 q += dom_len + 1;
319                                         }
320                                         q += 16;
321                                 }
322
323                                 if (PTR_DIFF(q + 8, buf) > len) {
324                                         DEBUG(0,("process_logon_packet: bad packet\n"));
325                                         return;
326                                 }
327
328                                 ntversion = IVAL(q, 0);
329                                 lmnttoken = SVAL(q, 4);
330                                 lm20token = SVAL(q, 6);
331                                 q += 8;
332
333                                 DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
334
335                                 /*
336                                  * we respond regadless of whether the machine is in our password 
337                                  * database. If it isn't then we let smbd send an appropriate error.
338                                  * Let's ignore the SID.
339                                  */
340                                 pull_ucs2_pstring(ascuser, uniuser);
341                                 pull_ucs2_fstring(asccomp, unicomp);
342                                 DEBUG(5,("process_logon_packet: SAMLOGON user %s\n", ascuser));
343
344                                 fstrcpy(reply_name, "\\\\"); /* Here it wants \\LOGONSERVER. */
345                                 fstrcat(reply_name, my_name);
346
347                                 DEBUG(5,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
348                                         asccomp,inet_ntoa(p->ip), ascuser, reply_name, lp_workgroup(),
349                                 SAMLOGON_R ,lmnttoken));
350
351                                 /* Construct reply. */
352
353                                 q = outbuf;
354                                 /* we want the simple version unless we are an ADS PDC..which means  */
355                                 /* never, at least for now */
356                                 if ((ntversion < 11) || (SEC_ADS != lp_security()) || (ROLE_DOMAIN_PDC != lp_server_role())) {
357                                         if (SVAL(uniuser, 0) == 0) {
358                                                 SSVAL(q, 0, SAMLOGON_UNK_R);    /* user unknown */
359                                         } else {
360                                                 SSVAL(q, 0, SAMLOGON_R);
361                                         }
362
363                                         q += 2;
364
365                                         q += dos_PutUniCode(q, reply_name,sizeof(pstring), True);
366                                         q += dos_PutUniCode(q, ascuser, sizeof(pstring), True);
367                                         q += dos_PutUniCode(q, lp_workgroup(),sizeof(pstring), True);
368                                 }
369 #ifdef HAVE_ADS
370                                 else {
371                                         struct GUID domain_guid;
372                                         UUID_FLAT flat_guid;
373                                         pstring domain;
374                                         pstring hostname;
375                                         char *component, *dc, *q1;
376                                         uint8 size;
377                                         char *q_orig = q;
378                                         int str_offset;
379
380                                         get_mydnsdomname(domain);
381                                         get_myname(hostname);
382         
383                                         if (SVAL(uniuser, 0) == 0) {
384                                                 SIVAL(q, 0, SAMLOGON_AD_UNK_R); /* user unknown */
385                                         } else {
386                                                 SIVAL(q, 0, SAMLOGON_AD_R);
387                                         }
388                                         q += 4;
389
390                                         SIVAL(q, 0, ADS_PDC|ADS_GC|ADS_LDAP|ADS_DS|
391                                                 ADS_KDC|ADS_TIMESERV|ADS_CLOSEST|ADS_WRITABLE);
392                                         q += 4;
393
394                                         /* Push Domain GUID */
395                                         if (False == secrets_fetch_domain_guid(domain, &domain_guid)) {
396                                                 DEBUG(2, ("Could not fetch DomainGUID for %s\n", domain));
397                                                 return;
398                                         }
399
400                                         smb_uuid_pack(domain_guid, &flat_guid);
401                                         memcpy(q, &flat_guid.info, UUID_FLAT_SIZE);
402                                         q += UUID_FLAT_SIZE;
403
404                                         /* Forest */
405                                         str_offset = q - q_orig;
406                                         dc = domain;
407                                         q1 = q;
408                                         while ((component = strtok(dc, "."))) {
409                                                 dc = NULL;
410                                                 size = push_ascii(&q[1], component, -1, 0);
411                                                 SCVAL(q, 0, size);
412                                                 q += (size + 1);
413                                         }
414
415                                         /* Unk0 */
416                                         SCVAL(q, 0, 0);
417                                         q++;
418
419                                         /* Domain */
420                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
421                                         SCVAL(q, 1, str_offset & 0xFF);
422                                         q += 2;
423
424                                         /* Hostname */
425                                         size = push_ascii(&q[1], hostname, -1, 0);
426                                         SCVAL(q, 0, size);
427                                         q += (size + 1);
428                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
429                                         SCVAL(q, 1, str_offset & 0xFF);
430                                         q += 2;
431
432                                         /* NETBIOS of domain */
433                                         size = push_ascii(&q[1], lp_workgroup(), -1, STR_UPPER);
434                                         SCVAL(q, 0, size);
435                                         q += (size + 1);
436
437                                         /* Unk1 */
438                                         SCVAL(q, 0, 0);
439                                         q++;
440
441                                         /* NETBIOS of hostname */
442                                         size = push_ascii(&q[1], my_name, -1, 0);
443                                         SCVAL(q, 0, size);
444                                         q += (size + 1);
445
446                                         /* Unk2 */
447                                         SCVAL(q, 0, 0);
448                                         q++;
449
450                                         /* User name */
451                                         if (SVAL(uniuser, 0) != 0) {
452                                                 size = push_ascii(&q[1], ascuser, -1, 0);
453                                                 SCVAL(q, 0, size);
454                                                 q += (size + 1);
455                                         }
456
457                                         q_orig = q;
458                                         /* Site name */
459                                         size = push_ascii(&q[1], "Default-First-Site-Name", -1, 0);
460                                         SCVAL(q, 0, size);
461                                         q += (size + 1);
462
463                                         /* Site name (2) */
464                                         str_offset = q - q_orig;
465                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
466                                         SCVAL(q, 1, str_offset & 0xFF);
467                                         q += 2;
468
469                                         SCVAL(q, 0, PTR_DIFF(q,q1));
470                                         SCVAL(q, 1, 0x10); /* unknown */
471
472                                         SIVAL(q, 0, 0x00000002);
473                                         q += 4; /* unknown */
474                                         SIVAL(q, 0, (iface_ip(p->ip))->s_addr);
475                                         q += 4;
476                                         SIVAL(q, 0, 0x00000000);
477                                         q += 4; /* unknown */
478                                         SIVAL(q, 0, 0x00000000);
479                                         q += 4; /* unknown */
480                                 }       
481 #endif
482
483                                 /* tell the client what version we are */
484                                 SIVAL(q, 0, ((ntversion < 11) || (SEC_ADS != lp_security())) ? 1 : 13); 
485                                 /* our ntversion */
486                                 SSVAL(q, 4, 0xffff); /* our lmnttoken */ 
487                                 SSVAL(q, 6, 0xffff); /* our lm20token */
488                                 q += 8;
489
490                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
491
492                                 pull_ascii_fstring(getdc_str, getdc);
493                                 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
494
495                                 send_mailslot(True, getdc,
496                                         outbuf,PTR_DIFF(q,outbuf),
497                                         global_myname(), 0x0,
498                                         source_name,
499                                         dgram->source_name.name_type,
500                                         p->ip, *iface_ip(p->ip), p->port);  
501                                 break;
502                         }
503
504                 /* Announce change to UAS or SAM.  Send by the domain controller when a
505                 replication event is required. */
506
507                 case SAM_UAS_CHANGE:
508                         {
509                                 struct sam_database_info *db_info;
510                                 char *q = buf + 2;
511                                 int i, db_count;
512                                 uint32 low_serial;
513           
514                                 /* Header */
515           
516                                 if (PTR_DIFF(q + 16, buf) >= len) {
517                                         DEBUG(0,("process_logon_packet: bad packet\n"));
518                                         return;
519                                 }
520
521                                 low_serial = IVAL(q, 0); q += 4;     /* Low serial number */
522
523                                 q += 4;                   /* Date/time */
524                                 q += 4;                   /* Pulse */
525                                 q += 4;                   /* Random */
526           
527                                 /* Domain info */
528           
529                                 q = skip_string(buf,len,q);    /* PDC name */
530
531                                 if (!q || PTR_DIFF(q, buf) >= len) {
532                                         DEBUG(0,("process_logon_packet: bad packet\n"));
533                                         return;
534                                 }
535
536                                 q = skip_string(buf,len,q);    /* Domain name */
537
538                                 if (!q || PTR_DIFF(q, buf) >= len) {
539                                         DEBUG(0,("process_logon_packet: bad packet\n"));
540                                         return;
541                                 }
542
543                                 q = skip_unibuf(q, PTR_DIFF(buf + len, q)); /* Unicode PDC name */
544
545                                 if (PTR_DIFF(q, buf) >= len) {
546                                         DEBUG(0,("process_logon_packet: bad packet\n"));
547                                         return;
548                                 }
549
550                                 q = skip_unibuf(q, PTR_DIFF(buf + len, q)); /* Unicode domain name */
551           
552                                 /* Database info */
553           
554                                 if (PTR_DIFF(q + 2, buf) >= len) {
555                                         DEBUG(0,("process_logon_packet: bad packet\n"));
556                                         return;
557                                 }
558
559                                 db_count = SVAL(q, 0); q += 2;
560
561                                 if (PTR_DIFF(q + (db_count*20), buf) >= len) {
562                                         DEBUG(0,("process_logon_packet: bad packet\n"));
563                                         return;
564                                 }
565
566                                 db_info = SMB_MALLOC_ARRAY(struct sam_database_info, db_count);
567
568                                 if (db_info == NULL) {
569                                         DEBUG(3, ("out of memory allocating info for %d databases\n", db_count));
570                                         return;
571                                 }
572           
573                                 for (i = 0; i < db_count; i++) {
574                                         db_info[i].index = IVAL(q, 0);
575                                         db_info[i].serial_lo = IVAL(q, 4);
576                                         db_info[i].serial_hi = IVAL(q, 8);
577                                         db_info[i].date_lo = IVAL(q, 12);
578                                         db_info[i].date_hi = IVAL(q, 16);
579                                         q += 20;
580                                 }
581
582                                 /* Domain SID */
583
584 #if 0
585                                 /* We must range check this. */
586                                 q += IVAL(q, 0) + 4;  /* 4 byte length plus data */
587           
588                                 q += 2;               /* Alignment? */
589
590                                 /* Misc other info */
591
592                                 q += 4;               /* NT version (0x1) */
593                                 q += 2;               /* LMNT token (0xff) */
594                                 q += 2;               /* LM20 token (0xff) */
595 #endif
596
597                                 SAFE_FREE(db_info);        /* Not sure whether we need to do anything useful with these */
598
599                                 /* Send message to smbd */
600
601                                 send_repl_message(low_serial);
602                                 break;
603                         }
604
605                 default:
606                         DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
607                         return;
608         }
609 }