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, 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)
41 DEBUG(3, ("sending replication message, serial = 0x%04x\n",
43 message_send_all(nmbd_messaging_context(), MSG_SMB_SAM_REPL,
44 &low_serial, sizeof(low_serial), NULL);
47 /****************************************************************************
48 Process a domain logon packet
49 **************************************************************************/
51 void process_logon_packet(struct packet_struct *p, char *buf,int len,
54 struct dgram_packet *dgram = &p->packet.dgram;
64 BOOL short_request = False;
66 char *uniuser; /* Unicode user name. */
68 char *unicomp; /* Unicode computer name. */
70 memset(outbuf, 0, sizeof(outbuf));
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) ));
78 pstrcpy(my_name, global_myname());
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));
86 fstring mach_str, user_str, getdc_str;
89 char *user = skip_string(buf,len,machine);
91 if (!user || PTR_DIFF(user, buf) >= len) {
92 DEBUG(0,("process_logon_packet: bad packet\n"));
95 getdc = skip_string(buf,len,user);
97 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
98 DEBUG(0,("process_logon_packet: bad packet\n"));
101 q = skip_string(buf,len,getdc);
103 if (!q || PTR_DIFF(q + 5, buf) > len) {
104 DEBUG(0,("process_logon_packet: bad packet\n"));
109 fstrcpy(reply_name,my_name);
111 pull_ascii_fstring(mach_str, machine);
112 pull_ascii_fstring(user_str, user);
113 pull_ascii_fstring(getdc_str, getdc);
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));
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 */
130 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
132 send_mailslot(True, getdc_str,
133 outbuf,PTR_DIFF(q,outbuf),
134 global_myname(), 0x0,
136 dgram->source_name.name_type,
137 p->ip, *iface_ip(p->ip), p->port);
143 fstring mach_str, getdc_str;
148 if (!lp_domain_master()) {
149 /* We're not Primary Domain Controller -- ignore this */
153 getdc = skip_string(buf,len,machine);
155 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
156 DEBUG(0,("process_logon_packet: bad packet\n"));
159 q = skip_string(buf,len,getdc);
161 if (!q || PTR_DIFF(q, buf) >= len) {
162 DEBUG(0,("process_logon_packet: bad packet\n"));
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 */
173 if (len - PTR_DIFF(q, buf) <= 3) {
174 short_request = True;
178 if (PTR_DIFF(q, buf) >= len) {
179 DEBUG(0,("process_logon_packet: bad packet\n"));
183 /* A full length (NT style) request */
184 q = skip_unibuf(unicomp, PTR_DIFF(buf + len, unicomp));
186 if (PTR_DIFF(q, buf) >= len) {
187 DEBUG(0,("process_logon_packet: bad packet\n"));
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);
204 if (PTR_DIFF(q + 8, buf) > len) {
205 DEBUG(0,("process_logon_packet: bad packet\n"));
209 ntversion = IVAL(q, 0);
210 lmnttoken = SVAL(q, 4);
211 lm20token = SVAL(q, 6);
214 /* Construct reply. */
216 SSVAL(q, 0, QUERYFORPDC_R);
219 fstrcpy(reply_name,my_name);
220 push_ascii_fstring(q, reply_name);
221 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
223 /* PDC and domain name */
224 if (!short_request) {
225 /* Make a full reply */
226 q = ALIGN2(q, outbuf);
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 */
236 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
238 pull_ascii_fstring(mach_str, machine);
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 ));
246 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
248 pull_ascii_fstring(getdc_str, getdc);
249 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
251 send_mailslot(True, getdc_str,
252 outbuf,PTR_DIFF(q,outbuf),
253 global_myname(), 0x0,
255 dgram->source_name.name_type,
256 p->ip, *iface_ip(p->ip), p->port);
270 if (PTR_DIFF(q, buf) >= len) {
271 DEBUG(0,("process_logon_packet: bad packet\n"));
276 uniuser = skip_unibuf(unicomp, PTR_DIFF(buf+len, unicomp));
278 if (PTR_DIFF(uniuser, buf) >= len) {
279 DEBUG(0,("process_logon_packet: bad packet\n"));
283 getdc = skip_unibuf(uniuser,PTR_DIFF(buf+len, uniuser));
285 if (PTR_DIFF(getdc, buf) >= len) {
286 DEBUG(0,("process_logon_packet: bad packet\n"));
290 q = skip_string(buf,len,getdc);
292 if (!q || PTR_DIFF(q + 8, buf) >= len) {
293 DEBUG(0,("process_logon_packet: bad packet\n"));
297 q += 4; /* Account Control Bits - indicating username type */
298 domainsidsize = IVAL(q, 0);
301 DEBUG(5,("process_logon_packet: SAMLOGON sidsize %d, len = %d\n", domainsidsize, len));
303 if (domainsidsize < (len - PTR_DIFF(q, buf)) && (domainsidsize != 0)) {
308 DEBUG(5,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %ld\n", len, (unsigned long)PTR_DIFF(q, buf) ));
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);
317 if (dom_len < (len - PTR_DIFF(q, buf)) && (dom_len != 0)) {
323 if (PTR_DIFF(q + 8, buf) > len) {
324 DEBUG(0,("process_logon_packet: bad packet\n"));
328 ntversion = IVAL(q, 0);
329 lmnttoken = SVAL(q, 4);
330 lm20token = SVAL(q, 6);
333 DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
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.
340 pull_ucs2_pstring(ascuser, uniuser);
341 pull_ucs2_fstring(asccomp, unicomp);
342 DEBUG(5,("process_logon_packet: SAMLOGON user %s\n", ascuser));
344 fstrcpy(reply_name, "\\\\"); /* Here it wants \\LOGONSERVER. */
345 fstrcat(reply_name, my_name);
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));
351 /* Construct reply. */
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 */
360 SSVAL(q, 0, SAMLOGON_R);
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);
371 struct GUID domain_guid;
375 char *component, *dc, *q1;
380 get_mydnsdomname(domain);
381 get_myname(hostname);
383 if (SVAL(uniuser, 0) == 0) {
384 SIVAL(q, 0, SAMLOGON_AD_UNK_R); /* user unknown */
386 SIVAL(q, 0, SAMLOGON_AD_R);
390 SIVAL(q, 0, ADS_PDC|ADS_GC|ADS_LDAP|ADS_DS|
391 ADS_KDC|ADS_TIMESERV|ADS_CLOSEST|ADS_WRITABLE);
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));
400 smb_uuid_pack(domain_guid, &flat_guid);
401 memcpy(q, &flat_guid.info, UUID_FLAT_SIZE);
405 str_offset = q - q_orig;
408 while ((component = strtok(dc, "."))) {
410 size = push_ascii(&q[1], component, -1, 0);
420 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
421 SCVAL(q, 1, str_offset & 0xFF);
425 size = push_ascii(&q[1], hostname, -1, 0);
428 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
429 SCVAL(q, 1, str_offset & 0xFF);
432 /* NETBIOS of domain */
433 size = push_ascii(&q[1], lp_workgroup(), -1, STR_UPPER);
441 /* NETBIOS of hostname */
442 size = push_ascii(&q[1], my_name, -1, 0);
451 if (SVAL(uniuser, 0) != 0) {
452 size = push_ascii(&q[1], ascuser, -1, 0);
459 size = push_ascii(&q[1], "Default-First-Site-Name", -1, 0);
464 str_offset = q - q_orig;
465 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
466 SCVAL(q, 1, str_offset & 0xFF);
469 SCVAL(q, 0, PTR_DIFF(q,q1));
470 SCVAL(q, 1, 0x10); /* unknown */
472 SIVAL(q, 0, 0x00000002);
473 q += 4; /* unknown */
474 SIVAL(q, 0, (iface_ip(p->ip))->s_addr);
476 SIVAL(q, 0, 0x00000000);
477 q += 4; /* unknown */
478 SIVAL(q, 0, 0x00000000);
479 q += 4; /* unknown */
483 /* tell the client what version we are */
484 SIVAL(q, 0, ((ntversion < 11) || (SEC_ADS != lp_security())) ? 1 : 13);
486 SSVAL(q, 4, 0xffff); /* our lmnttoken */
487 SSVAL(q, 6, 0xffff); /* our lm20token */
490 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
492 pull_ascii_fstring(getdc_str, getdc);
493 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
495 send_mailslot(True, getdc,
496 outbuf,PTR_DIFF(q,outbuf),
497 global_myname(), 0x0,
499 dgram->source_name.name_type,
500 p->ip, *iface_ip(p->ip), p->port);
504 /* Announce change to UAS or SAM. Send by the domain controller when a
505 replication event is required. */
509 struct sam_database_info *db_info;
516 if (PTR_DIFF(q + 16, buf) >= len) {
517 DEBUG(0,("process_logon_packet: bad packet\n"));
521 low_serial = IVAL(q, 0); q += 4; /* Low serial number */
523 q += 4; /* Date/time */
529 q = skip_string(buf,len,q); /* PDC name */
531 if (!q || PTR_DIFF(q, buf) >= len) {
532 DEBUG(0,("process_logon_packet: bad packet\n"));
536 q = skip_string(buf,len,q); /* Domain name */
538 if (!q || PTR_DIFF(q, buf) >= len) {
539 DEBUG(0,("process_logon_packet: bad packet\n"));
543 q = skip_unibuf(q, PTR_DIFF(buf + len, q)); /* Unicode PDC name */
545 if (PTR_DIFF(q, buf) >= len) {
546 DEBUG(0,("process_logon_packet: bad packet\n"));
550 q = skip_unibuf(q, PTR_DIFF(buf + len, q)); /* Unicode domain name */
554 if (PTR_DIFF(q + 2, buf) >= len) {
555 DEBUG(0,("process_logon_packet: bad packet\n"));
559 db_count = SVAL(q, 0); q += 2;
561 if (PTR_DIFF(q + (db_count*20), buf) >= len) {
562 DEBUG(0,("process_logon_packet: bad packet\n"));
566 db_info = SMB_MALLOC_ARRAY(struct sam_database_info, db_count);
568 if (db_info == NULL) {
569 DEBUG(3, ("out of memory allocating info for %d databases\n", db_count));
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);
585 /* We must range check this. */
586 q += IVAL(q, 0) + 4; /* 4 byte length plus data */
588 q += 2; /* Alignment? */
590 /* Misc other info */
592 q += 4; /* NT version (0x1) */
593 q += 2; /* LMNT token (0xff) */
594 q += 2; /* LM20 token (0xff) */
597 SAFE_FREE(db_info); /* Not sure whether we need to do anything useful with these */
599 /* Send message to smbd */
601 send_repl_message(low_serial);
606 DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));