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 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.
29 struct sam_database_info {
31 uint32 serial_lo, serial_hi;
32 uint32 date_lo, date_hi;
35 /****************************************************************************
36 Send a message to smbd to do a sam delta sync
37 **************************************************************************/
39 static void send_repl_message(uint32 low_serial)
43 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
44 TDB_DEFAULT, O_RDONLY, 0);
47 DEBUG(3, ("send_repl_message(): failed to open connections "
52 DEBUG(3, ("sending replication message, serial = 0x%04x\n",
55 message_send_all(tdb, MSG_SMB_SAM_REPL, &low_serial,
56 sizeof(low_serial), False, NULL);
61 /****************************************************************************
62 Process a domain logon packet
63 **************************************************************************/
65 void process_logon_packet(struct packet_struct *p, char *buf,int len,
68 struct dgram_packet *dgram = &p->packet.dgram;
78 BOOL short_request = False;
80 char *uniuser; /* Unicode user name. */
82 char *unicomp; /* Unicode computer name. */
84 memset(outbuf, 0, sizeof(outbuf));
86 if (!lp_domain_logons()) {
87 DEBUG(5,("process_logon_packet: Logon packet received from IP %s and domain \
88 logons are not enabled.\n", inet_ntoa(p->ip) ));
92 pstrcpy(my_name, global_myname());
95 DEBUG(4,("process_logon_packet: Logon from %s: code = 0x%x\n", inet_ntoa(p->ip), code));
100 fstring mach_str, user_str, getdc_str;
103 char *user = skip_string(machine,1);
105 if (PTR_DIFF(user, buf) >= len) {
106 DEBUG(0,("process_logon_packet: bad packet\n"));
109 getdc = skip_string(user,1);
111 if (PTR_DIFF(getdc, buf) >= len) {
112 DEBUG(0,("process_logon_packet: bad packet\n"));
115 q = skip_string(getdc,1);
117 if (PTR_DIFF(q + 5, buf) > len) {
118 DEBUG(0,("process_logon_packet: bad packet\n"));
123 fstrcpy(reply_name,my_name);
125 pull_ascii_fstring(mach_str, machine);
126 pull_ascii_fstring(user_str, user);
127 pull_ascii_fstring(getdc_str, getdc);
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));
136 fstrcpy(reply_name, "\\\\");
137 fstrcat(reply_name, my_name);
138 push_ascii_fstring(q, reply_name);
139 q = skip_string(q, 1); /* PDC name */
144 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
146 send_mailslot(True, getdc_str,
147 outbuf,PTR_DIFF(q,outbuf),
148 global_myname(), 0x0,
150 dgram->source_name.name_type,
151 p->ip, *iface_ip(p->ip), p->port);
157 fstring mach_str, getdc_str;
162 if (!lp_domain_master()) {
163 /* We're not Primary Domain Controller -- ignore this */
167 getdc = skip_string(machine,1);
169 if (PTR_DIFF(getdc, buf) >= len) {
170 DEBUG(0,("process_logon_packet: bad packet\n"));
173 q = skip_string(getdc,1);
175 if (PTR_DIFF(q, buf) >= len) {
176 DEBUG(0,("process_logon_packet: bad packet\n"));
181 /* At this point we can work out if this is a W9X or NT style
182 request. Experiments show that the difference is wether the
183 packet ends here. For a W9X request we now end with a pair of
184 bytes (usually 0xFE 0xFF) whereas with NT we have two further
185 strings - the following is a simple way of detecting this */
187 if (len - PTR_DIFF(q, buf) <= 3) {
188 short_request = True;
192 if (PTR_DIFF(q, buf) >= len) {
193 DEBUG(0,("process_logon_packet: bad packet\n"));
197 /* A full length (NT style) request */
198 q = skip_unibuf(unicomp, PTR_DIFF(buf + len, unicomp));
200 if (PTR_DIFF(q, buf) >= len) {
201 DEBUG(0,("process_logon_packet: bad packet\n"));
205 if (len - PTR_DIFF(q, buf) > 8) {
206 /* with NT5 clients we can sometimes
207 get additional data - a length specificed string
208 containing the domain name, then 16 bytes of
209 data (no idea what it is) */
210 int dom_len = CVAL(q, 0);
218 if (PTR_DIFF(q + 8, buf) > len) {
219 DEBUG(0,("process_logon_packet: bad packet\n"));
223 ntversion = IVAL(q, 0);
224 lmnttoken = SVAL(q, 4);
225 lm20token = SVAL(q, 6);
228 /* Construct reply. */
230 SSVAL(q, 0, QUERYFORPDC_R);
233 fstrcpy(reply_name,my_name);
234 push_ascii_fstring(q, reply_name);
235 q = skip_string(q, 1); /* PDC name */
237 /* PDC and domain name */
238 if (!short_request) {
239 /* Make a full reply */
240 q = ALIGN2(q, outbuf);
242 q += dos_PutUniCode(q, my_name, sizeof(pstring), True); /* PDC name */
243 q += dos_PutUniCode(q, lp_workgroup(),sizeof(pstring), True); /* Domain name*/
244 SIVAL(q, 0, 1); /* our nt version */
245 SSVAL(q, 4, 0xffff); /* our lmnttoken */
246 SSVAL(q, 6, 0xffff); /* our lm20token */
250 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
252 pull_ascii_fstring(mach_str, machine);
254 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, \
255 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
256 mach_str,inet_ntoa(p->ip), reply_name, lp_workgroup(),
257 QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
258 (uint32)lm20token ));
260 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
262 pull_ascii_fstring(getdc_str, getdc);
263 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
265 send_mailslot(True, getdc_str,
266 outbuf,PTR_DIFF(q,outbuf),
267 global_myname(), 0x0,
269 dgram->source_name.name_type,
270 p->ip, *iface_ip(p->ip), p->port);
284 if (PTR_DIFF(q, buf) >= len) {
285 DEBUG(0,("process_logon_packet: bad packet\n"));
290 uniuser = skip_unibuf(unicomp, PTR_DIFF(buf+len, unicomp));
292 if (PTR_DIFF(uniuser, buf) >= len) {
293 DEBUG(0,("process_logon_packet: bad packet\n"));
297 getdc = skip_unibuf(uniuser,PTR_DIFF(buf+len, uniuser));
299 if (PTR_DIFF(getdc, buf) >= len) {
300 DEBUG(0,("process_logon_packet: bad packet\n"));
304 q = skip_string(getdc,1);
306 if (PTR_DIFF(q + 8, buf) >= len) {
307 DEBUG(0,("process_logon_packet: bad packet\n"));
311 q += 4; /* Account Control Bits - indicating username type */
312 domainsidsize = IVAL(q, 0);
315 DEBUG(5,("process_logon_packet: SAMLOGON sidsize %d, len = %d\n", domainsidsize, len));
317 if (domainsidsize < (len - PTR_DIFF(q, buf)) && (domainsidsize != 0)) {
322 DEBUG(5,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %ld\n", len, (unsigned long)PTR_DIFF(q, buf) ));
324 if (len - PTR_DIFF(q, buf) > 8) {
325 /* with NT5 clients we can sometimes
326 get additional data - a length specificed string
327 containing the domain name, then 16 bytes of
328 data (no idea what it is) */
329 int dom_len = CVAL(q, 0);
331 if (dom_len < (len - PTR_DIFF(q, buf)) && (dom_len != 0)) {
337 if (PTR_DIFF(q + 8, buf) > len) {
338 DEBUG(0,("process_logon_packet: bad packet\n"));
342 ntversion = IVAL(q, 0);
343 lmnttoken = SVAL(q, 4);
344 lm20token = SVAL(q, 6);
347 DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
350 * we respond regadless of whether the machine is in our password
351 * database. If it isn't then we let smbd send an appropriate error.
352 * Let's ignore the SID.
354 pull_ucs2_pstring(ascuser, uniuser);
355 pull_ucs2_fstring(asccomp, unicomp);
356 DEBUG(5,("process_logon_packet: SAMLOGON user %s\n", ascuser));
358 fstrcpy(reply_name, "\\\\"); /* Here it wants \\LOGONSERVER. */
359 fstrcat(reply_name, my_name);
361 DEBUG(5,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
362 asccomp,inet_ntoa(p->ip), ascuser, reply_name, lp_workgroup(),
363 SAMLOGON_R ,lmnttoken));
365 /* Construct reply. */
368 /* we want the simple version unless we are an ADS PDC..which means */
369 /* never, at least for now */
370 if ((ntversion < 11) || (SEC_ADS != lp_security()) || (ROLE_DOMAIN_PDC != lp_server_role())) {
371 if (SVAL(uniuser, 0) == 0) {
372 SSVAL(q, 0, SAMLOGON_UNK_R); /* user unknown */
374 SSVAL(q, 0, SAMLOGON_R);
379 q += dos_PutUniCode(q, reply_name,sizeof(pstring), True);
380 q += dos_PutUniCode(q, ascuser, sizeof(pstring), True);
381 q += dos_PutUniCode(q, lp_workgroup(),sizeof(pstring), True);
385 struct GUID domain_guid;
389 char *component, *dc, *q1;
394 get_mydnsdomname(domain);
395 get_myname(hostname);
397 if (SVAL(uniuser, 0) == 0) {
398 SIVAL(q, 0, SAMLOGON_AD_UNK_R); /* user unknown */
400 SIVAL(q, 0, SAMLOGON_AD_R);
404 SIVAL(q, 0, ADS_PDC|ADS_GC|ADS_LDAP|ADS_DS|
405 ADS_KDC|ADS_TIMESERV|ADS_CLOSEST|ADS_WRITABLE);
408 /* Push Domain GUID */
409 if (False == secrets_fetch_domain_guid(domain, &domain_guid)) {
410 DEBUG(2, ("Could not fetch DomainGUID for %s\n", domain));
414 smb_uuid_pack(domain_guid, &flat_guid);
415 memcpy(q, &flat_guid.info, UUID_FLAT_SIZE);
419 str_offset = q - q_orig;
422 while ((component = strtok(dc, "."))) {
424 size = push_ascii(&q[1], component, -1, 0);
434 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
435 SCVAL(q, 1, str_offset & 0xFF);
439 size = push_ascii(&q[1], hostname, -1, 0);
442 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
443 SCVAL(q, 1, str_offset & 0xFF);
446 /* NETBIOS of domain */
447 size = push_ascii(&q[1], lp_workgroup(), -1, STR_UPPER);
455 /* NETBIOS of hostname */
456 size = push_ascii(&q[1], my_name, -1, 0);
465 if (SVAL(uniuser, 0) != 0) {
466 size = push_ascii(&q[1], ascuser, -1, 0);
473 size = push_ascii(&q[1], "Default-First-Site-Name", -1, 0);
478 str_offset = q - q_orig;
479 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
480 SCVAL(q, 1, str_offset & 0xFF);
483 SCVAL(q, 0, PTR_DIFF(q,q1));
484 SCVAL(q, 1, 0x10); /* unknown */
486 SIVAL(q, 0, 0x00000002);
487 q += 4; /* unknown */
488 SIVAL(q, 0, (iface_ip(p->ip))->s_addr);
490 SIVAL(q, 0, 0x00000000);
491 q += 4; /* unknown */
492 SIVAL(q, 0, 0x00000000);
493 q += 4; /* unknown */
497 /* tell the client what version we are */
498 SIVAL(q, 0, ((ntversion < 11) || (SEC_ADS != lp_security())) ? 1 : 13);
500 SSVAL(q, 4, 0xffff); /* our lmnttoken */
501 SSVAL(q, 6, 0xffff); /* our lm20token */
504 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
506 pull_ascii_fstring(getdc_str, getdc);
507 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
509 send_mailslot(True, getdc,
510 outbuf,PTR_DIFF(q,outbuf),
511 global_myname(), 0x0,
513 dgram->source_name.name_type,
514 p->ip, *iface_ip(p->ip), p->port);
518 /* Announce change to UAS or SAM. Send by the domain controller when a
519 replication event is required. */
523 struct sam_database_info *db_info;
530 if (PTR_DIFF(q + 16, buf) >= len) {
531 DEBUG(0,("process_logon_packet: bad packet\n"));
535 low_serial = IVAL(q, 0); q += 4; /* Low serial number */
537 q += 4; /* Date/time */
543 q = skip_string(q, 1); /* PDC name */
545 if (PTR_DIFF(q, buf) >= len) {
546 DEBUG(0,("process_logon_packet: bad packet\n"));
550 q = skip_string(q, 1); /* Domain name */
552 if (PTR_DIFF(q, buf) >= len) {
553 DEBUG(0,("process_logon_packet: bad packet\n"));
557 q = skip_unibuf(q, PTR_DIFF(buf + len, q)); /* Unicode PDC name */
559 if (PTR_DIFF(q, buf) >= len) {
560 DEBUG(0,("process_logon_packet: bad packet\n"));
564 q = skip_unibuf(q, PTR_DIFF(buf + len, q)); /* Unicode domain name */
568 if (PTR_DIFF(q + 2, buf) >= len) {
569 DEBUG(0,("process_logon_packet: bad packet\n"));
573 db_count = SVAL(q, 0); q += 2;
575 if (PTR_DIFF(q + (db_count*20), buf) >= len) {
576 DEBUG(0,("process_logon_packet: bad packet\n"));
580 db_info = SMB_MALLOC_ARRAY(struct sam_database_info, db_count);
582 if (db_info == NULL) {
583 DEBUG(3, ("out of memory allocating info for %d databases\n", db_count));
587 for (i = 0; i < db_count; i++) {
588 db_info[i].index = IVAL(q, 0);
589 db_info[i].serial_lo = IVAL(q, 4);
590 db_info[i].serial_hi = IVAL(q, 8);
591 db_info[i].date_lo = IVAL(q, 12);
592 db_info[i].date_hi = IVAL(q, 16);
599 /* We must range check this. */
600 q += IVAL(q, 0) + 4; /* 4 byte length plus data */
602 q += 2; /* Alignment? */
604 /* Misc other info */
606 q += 4; /* NT version (0x1) */
607 q += 2; /* LMNT token (0xff) */
608 q += 2; /* LM20 token (0xff) */
611 SAFE_FREE(db_info); /* Not sure whether we need to do anything useful with these */
613 /* Send message to smbd */
615 send_repl_message(low_serial);
620 DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));