source: branches/samba-3.0/source/nmbd/nmbd_processlogon.c

Last change on this file was 105, checked in by Paul Smedley, 18 years ago

Update source to 3.0.27a

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