cldap: avoid duplicate definitions so remove ads_cldap.h.
[Samba/gebeck_regimport.git] / source3 / nmbd / nmbd_processlogon.c
blob6e110dd1ca699477c45a18df2810fbf492e6387f
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
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.
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, see <http://www.gnu.org/licenses/>.
22 Revision History:
26 #include "includes.h"
28 struct sam_database_info {
29 uint32 index;
30 uint32 serial_lo, serial_hi;
31 uint32 date_lo, date_hi;
34 /****************************************************************************
35 Process a domain logon packet
36 **************************************************************************/
38 void process_logon_packet(struct packet_struct *p, char *buf,int len,
39 const char *mailslot)
41 struct dgram_packet *dgram = &p->packet.dgram;
42 fstring my_name;
43 fstring reply_name;
44 char outbuf[1024];
45 int code;
46 uint16 token = 0;
47 uint32 ntversion = 0;
48 uint16 lmnttoken = 0;
49 uint16 lm20token = 0;
50 uint32 domainsidsize;
51 bool short_request = False;
52 char *getdc;
53 char *uniuser; /* Unicode user name. */
54 fstring ascuser;
55 char *unicomp; /* Unicode computer name. */
56 size_t size;
57 struct sockaddr_storage ss;
58 const struct sockaddr_storage *pss;
59 struct in_addr ip;
61 in_addr_to_sockaddr_storage(&ss, p->ip);
62 pss = iface_ip(&ss);
63 if (!pss) {
64 DEBUG(5,("process_logon_packet:can't find outgoing interface "
65 "for packet from IP %s\n",
66 inet_ntoa(p->ip) ));
67 return;
69 ip = ((struct sockaddr_in *)pss)->sin_addr;
71 memset(outbuf, 0, sizeof(outbuf));
73 if (!lp_domain_logons()) {
74 DEBUG(5,("process_logon_packet: Logon packet received from IP %s and domain \
75 logons are not enabled.\n", inet_ntoa(p->ip) ));
76 return;
79 fstrcpy(my_name, global_myname());
81 code = get_safe_SVAL(buf,len,buf,0,-1);
82 DEBUG(4,("process_logon_packet: Logon from %s: code = 0x%x\n", inet_ntoa(p->ip), code));
84 switch (code) {
85 case 0:
87 fstring mach_str, user_str, getdc_str;
88 char *q = buf + 2;
89 char *machine = q;
90 char *user = skip_string(buf,len,machine);
92 if (!user || PTR_DIFF(user, buf) >= len) {
93 DEBUG(0,("process_logon_packet: bad packet\n"));
94 return;
96 getdc = skip_string(buf,len,user);
98 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
99 DEBUG(0,("process_logon_packet: bad packet\n"));
100 return;
102 q = skip_string(buf,len,getdc);
104 if (!q || PTR_DIFF(q + 5, buf) > len) {
105 DEBUG(0,("process_logon_packet: bad packet\n"));
106 return;
108 token = SVAL(q,3);
110 fstrcpy(reply_name,my_name);
112 pull_ascii_fstring(mach_str, machine);
113 pull_ascii_fstring(user_str, user);
114 pull_ascii_fstring(getdc_str, getdc);
116 DEBUG(5,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
117 mach_str,inet_ntoa(p->ip),user_str,token));
119 q = outbuf;
120 SSVAL(q, 0, 6);
121 q += 2;
123 fstrcpy(reply_name, "\\\\");
124 fstrcat(reply_name, my_name);
125 size = push_ascii(q,reply_name,
126 sizeof(outbuf)-PTR_DIFF(q, outbuf),
127 STR_TERMINATE);
128 if (size == (size_t)-1) {
129 return;
131 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
133 SSVAL(q, 0, token);
134 q += 2;
136 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
138 send_mailslot(True, getdc_str,
139 outbuf,PTR_DIFF(q,outbuf),
140 global_myname(), 0x0,
141 mach_str,
142 dgram->source_name.name_type,
143 p->ip, ip, p->port);
144 break;
147 case QUERYFORPDC:
149 fstring mach_str, getdc_str;
150 fstring source_name;
151 char *q = buf + 2;
152 char *machine = q;
154 if (!lp_domain_master()) {
155 /* We're not Primary Domain Controller -- ignore this */
156 return;
159 getdc = skip_string(buf,len,machine);
161 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
162 DEBUG(0,("process_logon_packet: bad packet\n"));
163 return;
165 q = skip_string(buf,len,getdc);
167 if (!q || PTR_DIFF(q, buf) >= len) {
168 DEBUG(0,("process_logon_packet: bad packet\n"));
169 return;
171 q = ALIGN2(q, buf);
173 /* At this point we can work out if this is a W9X or NT style
174 request. Experiments show that the difference is wether the
175 packet ends here. For a W9X request we now end with a pair of
176 bytes (usually 0xFE 0xFF) whereas with NT we have two further
177 strings - the following is a simple way of detecting this */
179 if (len - PTR_DIFF(q, buf) <= 3) {
180 short_request = True;
181 } else {
182 unicomp = q;
184 if (PTR_DIFF(q, buf) >= len) {
185 DEBUG(0,("process_logon_packet: bad packet\n"));
186 return;
189 /* A full length (NT style) request */
190 q = skip_unibuf(unicomp, PTR_DIFF(buf + len, unicomp));
192 if (PTR_DIFF(q, buf) >= len) {
193 DEBUG(0,("process_logon_packet: bad packet\n"));
194 return;
197 if (len - PTR_DIFF(q, buf) > 8) {
198 /* with NT5 clients we can sometimes
199 get additional data - a length specificed string
200 containing the domain name, then 16 bytes of
201 data (no idea what it is) */
202 int dom_len = CVAL(q, 0);
203 q++;
204 if (dom_len != 0) {
205 q += dom_len + 1;
207 q += 16;
210 if (PTR_DIFF(q + 8, buf) > len) {
211 DEBUG(0,("process_logon_packet: bad packet\n"));
212 return;
215 ntversion = IVAL(q, 0);
216 lmnttoken = SVAL(q, 4);
217 lm20token = SVAL(q, 6);
220 /* Construct reply. */
221 q = outbuf;
222 SSVAL(q, 0, QUERYFORPDC_R);
223 q += 2;
225 fstrcpy(reply_name,my_name);
226 size = push_ascii(q, reply_name,
227 sizeof(outbuf)-PTR_DIFF(q, outbuf),
228 STR_TERMINATE);
229 if (size == (size_t)-1) {
230 return;
232 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
234 /* PDC and domain name */
235 if (!short_request) {
236 /* Make a full reply */
237 q = ALIGN2(q, outbuf);
239 q += dos_PutUniCode(q, my_name,
240 sizeof(outbuf) - PTR_DIFF(q, outbuf),
241 True); /* PDC name */
242 q += dos_PutUniCode(q, lp_workgroup(),
243 sizeof(outbuf) - PTR_DIFF(q, outbuf),
244 True); /* Domain name*/
245 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
246 return;
248 SIVAL(q, 0, 1); /* our nt version */
249 SSVAL(q, 4, 0xffff); /* our lmnttoken */
250 SSVAL(q, 6, 0xffff); /* our lm20token */
251 q += 8;
254 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
256 pull_ascii_fstring(mach_str, machine);
258 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, \
259 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
260 mach_str,inet_ntoa(p->ip), reply_name, lp_workgroup(),
261 QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
262 (uint32)lm20token ));
264 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
266 pull_ascii_fstring(getdc_str, getdc);
267 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
269 send_mailslot(True, getdc_str,
270 outbuf,PTR_DIFF(q,outbuf),
271 global_myname(), 0x0,
272 source_name,
273 dgram->source_name.name_type,
274 p->ip, ip, p->port);
275 return;
278 case SAMLOGON:
281 fstring getdc_str;
282 fstring source_name;
283 char *q = buf + 2;
284 fstring asccomp;
286 q += 2;
288 if (PTR_DIFF(q, buf) >= len) {
289 DEBUG(0,("process_logon_packet: bad packet\n"));
290 return;
293 unicomp = q;
294 uniuser = skip_unibuf(unicomp, PTR_DIFF(buf+len, unicomp));
296 if (PTR_DIFF(uniuser, buf) >= len) {
297 DEBUG(0,("process_logon_packet: bad packet\n"));
298 return;
301 getdc = skip_unibuf(uniuser,PTR_DIFF(buf+len, uniuser));
303 if (PTR_DIFF(getdc, buf) >= len) {
304 DEBUG(0,("process_logon_packet: bad packet\n"));
305 return;
308 q = skip_string(buf,len,getdc);
310 if (!q || PTR_DIFF(q + 8, buf) >= len) {
311 DEBUG(0,("process_logon_packet: bad packet\n"));
312 return;
315 q += 4; /* Account Control Bits - indicating username type */
316 domainsidsize = IVAL(q, 0);
317 q += 4;
319 DEBUG(5,("process_logon_packet: SAMLOGON sidsize %d, len = %d\n", domainsidsize, len));
321 if (domainsidsize < (len - PTR_DIFF(q, buf)) && (domainsidsize != 0)) {
322 q += domainsidsize;
323 q = ALIGN4(q, buf);
326 DEBUG(5,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %ld\n", len, (unsigned long)PTR_DIFF(q, buf) ));
328 if (len - PTR_DIFF(q, buf) > 8) {
329 /* with NT5 clients we can sometimes
330 get additional data - a length specificed string
331 containing the domain name, then 16 bytes of
332 data (no idea what it is) */
333 int dom_len = CVAL(q, 0);
334 q++;
335 if (dom_len < (len - PTR_DIFF(q, buf)) && (dom_len != 0)) {
336 q += dom_len + 1;
338 q += 16;
341 if (PTR_DIFF(q + 8, buf) > len) {
342 DEBUG(0,("process_logon_packet: bad packet\n"));
343 return;
346 ntversion = IVAL(q, 0);
347 lmnttoken = SVAL(q, 4);
348 lm20token = SVAL(q, 6);
349 q += 8;
351 DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
354 * we respond regadless of whether the machine is in our password
355 * database. If it isn't then we let smbd send an appropriate error.
356 * Let's ignore the SID.
358 pull_ucs2_fstring(ascuser, uniuser);
359 pull_ucs2_fstring(asccomp, unicomp);
360 DEBUG(5,("process_logon_packet: SAMLOGON user %s\n", ascuser));
362 fstrcpy(reply_name, "\\\\"); /* Here it wants \\LOGONSERVER. */
363 fstrcat(reply_name, my_name);
365 DEBUG(5,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
366 asccomp,inet_ntoa(p->ip), ascuser, reply_name, lp_workgroup(),
367 SAMLOGON_R ,lmnttoken));
369 /* Construct reply. */
371 q = outbuf;
372 /* we want the simple version unless we are an ADS PDC..which means */
373 /* never, at least for now */
374 if ((ntversion < 11) || (SEC_ADS != lp_security()) || (ROLE_DOMAIN_PDC != lp_server_role())) {
375 if (SVAL(uniuser, 0) == 0) {
376 SSVAL(q, 0, SAMLOGON_UNK_R); /* user unknown */
377 } else {
378 SSVAL(q, 0, SAMLOGON_R);
381 q += 2;
383 q += dos_PutUniCode(q, reply_name,
384 sizeof(outbuf) - PTR_DIFF(q, outbuf),
385 True);
386 q += dos_PutUniCode(q, ascuser,
387 sizeof(outbuf) - PTR_DIFF(q, outbuf),
388 True);
389 q += dos_PutUniCode(q, lp_workgroup(),
390 sizeof(outbuf) - PTR_DIFF(q, outbuf),
391 True);
393 #ifdef HAVE_ADS
394 else {
395 struct GUID domain_guid;
396 UUID_FLAT flat_guid;
397 char *domain;
398 char *hostname;
399 char *component, *dc, *q1;
400 char *q_orig = q;
401 int str_offset;
402 char *saveptr;
404 domain = get_mydnsdomname(talloc_tos());
405 if (!domain) {
406 DEBUG(2,
407 ("get_mydnsdomname failed.\n"));
408 return;
410 hostname = get_myname(talloc_tos());
411 if (!hostname) {
412 DEBUG(2,
413 ("get_myname failed.\n"));
414 return;
417 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
418 return;
420 if (SVAL(uniuser, 0) == 0) {
421 SIVAL(q, 0, SAMLOGON_AD_UNK_R); /* user unknown */
422 } else {
423 SIVAL(q, 0, SAMLOGON_AD_R);
425 q += 4;
427 SIVAL(q, 0, NBT_SERVER_PDC|NBT_SERVER_GC|NBT_SERVER_LDAP|NBT_SERVER_DS|
428 NBT_SERVER_KDC|NBT_SERVER_TIMESERV|NBT_SERVER_CLOSEST|NBT_SERVER_WRITABLE);
429 q += 4;
431 /* Push Domain GUID */
432 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < UUID_FLAT_SIZE) {
433 return;
435 if (False == secrets_fetch_domain_guid(domain, &domain_guid)) {
436 DEBUG(2, ("Could not fetch DomainGUID for %s\n", domain));
437 return;
440 smb_uuid_pack(domain_guid, &flat_guid);
441 memcpy(q, &flat_guid.info, UUID_FLAT_SIZE);
442 q += UUID_FLAT_SIZE;
444 /* Forest */
445 str_offset = q - q_orig;
446 dc = domain;
447 q1 = q;
448 while ((component = strtok_r(dc, ".", &saveptr)) != NULL) {
449 dc = NULL;
450 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
451 return;
453 size = push_ascii(&q[1], component,
454 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
456 if (size == (size_t)-1 || size > 0xff) {
457 return;
459 SCVAL(q, 0, size);
460 q += (size + 1);
463 /* Unk0 */
464 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
465 return;
467 SCVAL(q, 0, 0);
468 q++;
470 /* Domain */
471 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
472 SCVAL(q, 1, str_offset & 0xFF);
473 q += 2;
475 /* Hostname */
476 size = push_ascii(&q[1], hostname,
477 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
479 if (size == (size_t)-1 || size > 0xff) {
480 return;
482 SCVAL(q, 0, size);
483 q += (size + 1);
485 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 3) {
486 return;
489 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
490 SCVAL(q, 1, str_offset & 0xFF);
491 q += 2;
493 /* NETBIOS of domain */
494 size = push_ascii(&q[1], lp_workgroup(),
495 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
496 STR_UPPER);
497 if (size == (size_t)-1 || size > 0xff) {
498 return;
500 SCVAL(q, 0, size);
501 q += (size + 1);
503 /* Unk1 */
504 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 2) {
505 return;
508 SCVAL(q, 0, 0);
509 q++;
511 /* NETBIOS of hostname */
512 size = push_ascii(&q[1], my_name,
513 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
515 if (size == (size_t)-1 || size > 0xff) {
516 return;
518 SCVAL(q, 0, size);
519 q += (size + 1);
521 /* Unk2 */
522 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
523 return;
526 SCVAL(q, 0, 0);
527 q++;
529 /* User name */
530 if (SVAL(uniuser, 0) != 0) {
531 size = push_ascii(&q[1], ascuser,
532 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
534 if (size == (size_t)-1 || size > 0xff) {
535 return;
537 SCVAL(q, 0, size);
538 q += (size + 1);
541 q_orig = q;
542 /* Site name */
543 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
544 return;
546 size = push_ascii(&q[1], "Default-First-Site-Name",
547 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
549 if (size == (size_t)-1 || size > 0xff) {
550 return;
552 SCVAL(q, 0, size);
553 q += (size + 1);
555 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 18) {
556 return;
559 /* Site name (2) */
560 str_offset = q - q_orig;
561 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
562 SCVAL(q, 1, str_offset & 0xFF);
563 q += 2;
565 SCVAL(q, 0, PTR_DIFF(q,q1));
566 SCVAL(q, 1, 0x10); /* unknown */
568 SIVAL(q, 0, 0x00000002);
569 q += 4; /* unknown */
570 SIVAL(q, 0, ntohl(ip.s_addr));
571 q += 4;
572 SIVAL(q, 0, 0x00000000);
573 q += 4; /* unknown */
574 SIVAL(q, 0, 0x00000000);
575 q += 4; /* unknown */
577 #endif
579 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
580 return;
583 /* tell the client what version we are */
584 SIVAL(q, 0, ((ntversion < 11) || (SEC_ADS != lp_security())) ? 1 : 13);
585 /* our ntversion */
586 SSVAL(q, 4, 0xffff); /* our lmnttoken */
587 SSVAL(q, 6, 0xffff); /* our lm20token */
588 q += 8;
590 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
592 pull_ascii_fstring(getdc_str, getdc);
593 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
595 send_mailslot(True, getdc,
596 outbuf,PTR_DIFF(q,outbuf),
597 global_myname(), 0x0,
598 source_name,
599 dgram->source_name.name_type,
600 p->ip, ip, p->port);
601 break;
604 /* Announce change to UAS or SAM. Send by the domain controller when a
605 replication event is required. */
607 case SAM_UAS_CHANGE:
608 DEBUG(5, ("Got SAM_UAS_CHANGE\n"));
609 break;
611 default:
612 DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
613 return;