s3: Lift the server_messaging_context from notify_job_status
[Samba/gbeck.git] / source3 / nmbd / nmbd_processlogon.c
blob607260b230a529367c3fba1681a211de8633fe86
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"
27 #include "../libcli/netlogon.h"
28 #include "../libcli/cldap/cldap.h"
29 #include "../lib/tsocket/tsocket.h"
30 #include "../libcli/security/dom_sid.h"
31 #include "secrets.h"
33 struct sam_database_info {
34 uint32 index;
35 uint32 serial_lo, serial_hi;
36 uint32 date_lo, date_hi;
39 /**
40 * check whether the client belongs to the hosts
41 * for which initial logon should be delayed...
43 static bool delay_logon(const char *peer_name, const char *peer_addr)
45 const char **delay_list = lp_init_logon_delayed_hosts();
46 const char *peer[2];
48 if (delay_list == NULL) {
49 return False;
52 peer[0] = peer_name;
53 peer[1] = peer_addr;
55 return list_match(delay_list, (const char *)peer, client_match);
58 static void delayed_init_logon_handler(struct event_context *event_ctx,
59 struct timed_event *te,
60 struct timeval now,
61 void *private_data)
63 struct packet_struct *p = (struct packet_struct *)private_data;
65 DEBUG(10, ("delayed_init_logon_handler (%lx): re-queuing packet.\n",
66 (unsigned long)te));
68 queue_packet(p);
70 TALLOC_FREE(te);
73 struct nmbd_proxy_logon_context {
74 struct cldap_socket *cldap_sock;
77 static struct nmbd_proxy_logon_context *global_nmbd_proxy_logon;
79 bool initialize_nmbd_proxy_logon(void)
81 const char *cldap_server = lp_parm_const_string(-1, "nmbd_proxy_logon",
82 "cldap_server", NULL);
83 struct nmbd_proxy_logon_context *ctx;
84 NTSTATUS status;
85 struct in_addr addr;
86 char addrstr[INET_ADDRSTRLEN];
87 const char *server_str;
88 int ret;
89 struct tsocket_address *server_addr;
91 if (!cldap_server) {
92 return true;
95 addr = interpret_addr2(cldap_server);
96 server_str = inet_ntop(AF_INET, &addr,
97 addrstr, sizeof(addrstr));
98 if (!server_str || strcmp("0.0.0.0", server_str) == 0) {
99 DEBUG(0,("Failed to resolve[%s] for nmbd_proxy_logon\n",
100 cldap_server));
101 return false;
104 ctx = talloc_zero(nmbd_event_context(),
105 struct nmbd_proxy_logon_context);
106 if (!ctx) {
107 return false;
110 ret = tsocket_address_inet_from_strings(ctx, "ipv4",
111 server_str, LDAP_PORT,
112 &server_addr);
113 if (ret != 0) {
114 TALLOC_FREE(ctx);
115 status = map_nt_error_from_unix(errno);
116 DEBUG(0,("Failed to create cldap tsocket_address for %s - %s\n",
117 server_str, nt_errstr(status)));
118 return false;
121 /* we create a connected udp socket */
122 status = cldap_socket_init(ctx, nmbd_event_context(), NULL,
123 server_addr, &ctx->cldap_sock);
124 TALLOC_FREE(server_addr);
125 if (!NT_STATUS_IS_OK(status)) {
126 TALLOC_FREE(ctx);
127 DEBUG(0,("failed to create cldap socket for %s: %s\n",
128 server_str, nt_errstr(status)));
129 return false;
132 global_nmbd_proxy_logon = ctx;
133 return true;
136 struct nmbd_proxy_logon_state {
137 struct in_addr local_ip;
138 struct packet_struct *p;
139 const char *remote_name;
140 uint8_t remote_name_type;
141 const char *remote_mailslot;
142 struct nbt_netlogon_packet req;
143 struct nbt_netlogon_response resp;
144 struct cldap_netlogon io;
147 static int nmbd_proxy_logon_state_destructor(struct nmbd_proxy_logon_state *s)
149 s->p->locked = false;
150 free_packet(s->p);
151 return 0;
154 static void nmbd_proxy_logon_done(struct tevent_req *subreq);
156 static void nmbd_proxy_logon(struct nmbd_proxy_logon_context *ctx,
157 struct in_addr local_ip,
158 struct packet_struct *p,
159 uint8_t *buf,
160 uint32_t len)
162 struct nmbd_proxy_logon_state *state;
163 enum ndr_err_code ndr_err;
164 DATA_BLOB blob = data_blob_const(buf, len);
165 const char *computer_name = NULL;
166 const char *mailslot_name = NULL;
167 const char *user_name = NULL;
168 const char *domain_sid = NULL;
169 uint32_t acct_control = 0;
170 uint32_t nt_version = 0;
171 struct tevent_req *subreq;
172 fstring source_name;
173 struct dgram_packet *dgram = &p->packet.dgram;
175 state = TALLOC_ZERO_P(ctx, struct nmbd_proxy_logon_state);
176 if (!state) {
177 DEBUG(0,("failed to allocate nmbd_proxy_logon_state\n"));
178 return;
181 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
182 state->remote_name = talloc_strdup(state, source_name);
183 state->remote_name_type = dgram->source_name.name_type,
184 state->local_ip = local_ip;
185 state->p = p;
187 ndr_err = ndr_pull_struct_blob(
188 &blob, state, &state->req,
189 (ndr_pull_flags_fn_t)ndr_pull_nbt_netlogon_packet);
190 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
191 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
192 DEBUG(0,("failed parse nbt_netlogon_packet: %s\n",
193 nt_errstr(status)));
194 TALLOC_FREE(state);
195 return;
198 if (DEBUGLEVEL >= 10) {
199 DEBUG(10, ("nmbd_proxy_logon:\n"));
200 NDR_PRINT_DEBUG(nbt_netlogon_packet, &state->req);
203 switch (state->req.command) {
204 case LOGON_SAM_LOGON_REQUEST:
205 computer_name = state->req.req.logon.computer_name;
206 user_name = state->req.req.logon.user_name;
207 mailslot_name = state->req.req.logon.mailslot_name;
208 acct_control = state->req.req.logon.acct_control;
209 if (state->req.req.logon.sid_size > 0) {
210 domain_sid = dom_sid_string(state,
211 &state->req.req.logon.sid);
212 if (!domain_sid) {
213 DEBUG(0,("failed to get a string for sid\n"));
214 TALLOC_FREE(state);
215 return;
218 nt_version = state->req.req.logon.nt_version;
219 break;
221 default:
222 /* this can't happen as the caller already checks the command */
223 break;
226 state->remote_mailslot = mailslot_name;
228 if (user_name && strlen(user_name) == 0) {
229 user_name = NULL;
232 if (computer_name && strlen(computer_name) == 0) {
233 computer_name = NULL;
237 * as the socket is connected,
238 * we don't need to specify the destination
240 state->io.in.dest_address = NULL;
241 state->io.in.dest_port = 0;
242 state->io.in.realm = NULL;
243 state->io.in.host = computer_name;
244 state->io.in.user = user_name;
245 state->io.in.domain_guid = NULL;
246 state->io.in.domain_sid = domain_sid;
247 state->io.in.acct_control = acct_control;
248 state->io.in.version = nt_version;
249 state->io.in.map_response = false;
251 subreq = cldap_netlogon_send(state,
252 ctx->cldap_sock,
253 &state->io);
254 if (!subreq) {
255 DEBUG(0,("failed to send cldap netlogon call\n"));
256 TALLOC_FREE(state);
257 return;
259 tevent_req_set_callback(subreq, nmbd_proxy_logon_done, state);
261 /* we reply async */
262 state->p->locked = true;
263 talloc_set_destructor(state, nmbd_proxy_logon_state_destructor);
266 static void nmbd_proxy_logon_done(struct tevent_req *subreq)
268 struct nmbd_proxy_logon_state *state =
269 tevent_req_callback_data(subreq,
270 struct nmbd_proxy_logon_state);
271 NTSTATUS status;
272 DATA_BLOB response = data_blob_null;
274 status = cldap_netlogon_recv(subreq, state, &state->io);
275 if (!NT_STATUS_IS_OK(status)) {
276 DEBUG(0,("failed to recv cldap netlogon call: %s\n",
277 nt_errstr(status)));
278 TALLOC_FREE(state);
279 return;
282 status = push_netlogon_samlogon_response(&response, state,
283 &state->io.out.netlogon);
284 if (!NT_STATUS_IS_OK(status)) {
285 DEBUG(0,("failed to push netlogon_samlogon_response: %s\n",
286 nt_errstr(status)));
287 TALLOC_FREE(state);
288 return;
291 send_mailslot(true, state->remote_mailslot,
292 (char *)response.data, response.length,
293 global_myname(), 0x0,
294 state->remote_name,
295 state->remote_name_type,
296 state->p->ip,
297 state->local_ip,
298 state->p->port);
299 TALLOC_FREE(state);
302 /****************************************************************************
303 Process a domain logon packet
304 **************************************************************************/
306 void process_logon_packet(struct packet_struct *p, char *buf,int len,
307 const char *mailslot)
309 struct dgram_packet *dgram = &p->packet.dgram;
310 fstring my_name;
311 fstring reply_name;
312 char outbuf[1024];
313 int code;
314 uint16 token = 0;
315 uint32 ntversion = 0;
316 uint16 lmnttoken = 0;
317 uint16 lm20token = 0;
318 uint32 domainsidsize;
319 bool short_request = False;
320 char *getdc;
321 char *uniuser; /* Unicode user name. */
322 fstring ascuser;
323 char *unicomp; /* Unicode computer name. */
324 size_t size;
325 struct sockaddr_storage ss;
326 const struct sockaddr_storage *pss;
327 struct in_addr ip;
329 in_addr_to_sockaddr_storage(&ss, p->ip);
330 pss = iface_ip((struct sockaddr *)&ss);
331 if (!pss) {
332 DEBUG(5,("process_logon_packet:can't find outgoing interface "
333 "for packet from IP %s\n",
334 inet_ntoa(p->ip) ));
335 return;
337 ip = ((struct sockaddr_in *)pss)->sin_addr;
339 memset(outbuf, 0, sizeof(outbuf));
341 if (!lp_domain_logons()) {
342 DEBUG(5,("process_logon_packet: Logon packet received from IP %s and domain \
343 logons are not enabled.\n", inet_ntoa(p->ip) ));
344 return;
347 fstrcpy(my_name, global_myname());
349 code = get_safe_SVAL(buf,len,buf,0,-1);
350 DEBUG(4,("process_logon_packet: Logon from %s: code = 0x%x\n", inet_ntoa(p->ip), code));
352 switch (code) {
353 case 0:
355 fstring mach_str, user_str, getdc_str;
356 char *q = buf + 2;
357 char *machine = q;
358 char *user = skip_string(buf,len,machine);
360 if (!user || PTR_DIFF(user, buf) >= len) {
361 DEBUG(0,("process_logon_packet: bad packet\n"));
362 return;
364 getdc = skip_string(buf,len,user);
366 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
367 DEBUG(0,("process_logon_packet: bad packet\n"));
368 return;
370 q = skip_string(buf,len,getdc);
372 if (!q || PTR_DIFF(q + 5, buf) > len) {
373 DEBUG(0,("process_logon_packet: bad packet\n"));
374 return;
376 token = SVAL(q,3);
378 fstrcpy(reply_name,my_name);
380 pull_ascii_fstring(mach_str, machine);
381 pull_ascii_fstring(user_str, user);
382 pull_ascii_fstring(getdc_str, getdc);
384 DEBUG(5,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
385 mach_str,inet_ntoa(p->ip),user_str,token));
387 q = outbuf;
388 SSVAL(q, 0, 6);
389 q += 2;
391 fstrcpy(reply_name, "\\\\");
392 fstrcat(reply_name, my_name);
393 size = push_ascii(q,reply_name,
394 sizeof(outbuf)-PTR_DIFF(q, outbuf),
395 STR_TERMINATE);
396 if (size == (size_t)-1) {
397 return;
399 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
401 SSVAL(q, 0, token);
402 q += 2;
404 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
406 send_mailslot(True, getdc_str,
407 outbuf,PTR_DIFF(q,outbuf),
408 global_myname(), 0x0,
409 mach_str,
410 dgram->source_name.name_type,
411 p->ip, ip, p->port);
412 break;
415 case LOGON_PRIMARY_QUERY:
417 fstring mach_str, getdc_str;
418 fstring source_name;
419 char *q = buf + 2;
420 char *machine = q;
422 if (!lp_domain_master()) {
423 /* We're not Primary Domain Controller -- ignore this */
424 return;
427 getdc = skip_string(buf,len,machine);
429 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
430 DEBUG(0,("process_logon_packet: bad packet\n"));
431 return;
433 q = skip_string(buf,len,getdc);
435 if (!q || PTR_DIFF(q, buf) >= len) {
436 DEBUG(0,("process_logon_packet: bad packet\n"));
437 return;
439 q = ALIGN2(q, buf);
441 /* At this point we can work out if this is a W9X or NT style
442 request. Experiments show that the difference is wether the
443 packet ends here. For a W9X request we now end with a pair of
444 bytes (usually 0xFE 0xFF) whereas with NT we have two further
445 strings - the following is a simple way of detecting this */
447 if (len - PTR_DIFF(q, buf) <= 3) {
448 short_request = True;
449 } else {
450 unicomp = q;
452 if (PTR_DIFF(q, buf) >= len) {
453 DEBUG(0,("process_logon_packet: bad packet\n"));
454 return;
457 /* A full length (NT style) request */
458 q = skip_unibuf(unicomp, PTR_DIFF(buf + len, unicomp));
460 if (PTR_DIFF(q, buf) >= len) {
461 DEBUG(0,("process_logon_packet: bad packet\n"));
462 return;
465 if (len - PTR_DIFF(q, buf) > 8) {
466 /* with NT5 clients we can sometimes
467 get additional data - a length specificed string
468 containing the domain name, then 16 bytes of
469 data (no idea what it is) */
470 int dom_len = CVAL(q, 0);
471 q++;
472 if (dom_len != 0) {
473 q += dom_len + 1;
475 q += 16;
478 if (PTR_DIFF(q + 8, buf) > len) {
479 DEBUG(0,("process_logon_packet: bad packet\n"));
480 return;
483 ntversion = IVAL(q, 0);
484 lmnttoken = SVAL(q, 4);
485 lm20token = SVAL(q, 6);
488 /* Construct reply. */
489 q = outbuf;
490 SSVAL(q, 0, NETLOGON_RESPONSE_FROM_PDC);
491 q += 2;
493 fstrcpy(reply_name,my_name);
494 size = push_ascii(q, reply_name,
495 sizeof(outbuf)-PTR_DIFF(q, outbuf),
496 STR_TERMINATE);
497 if (size == (size_t)-1) {
498 return;
500 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
502 /* PDC and domain name */
503 if (!short_request) {
504 /* Make a full reply */
505 q = ALIGN2(q, outbuf);
507 q += dos_PutUniCode(q, my_name,
508 sizeof(outbuf) - PTR_DIFF(q, outbuf),
509 True); /* PDC name */
510 q += dos_PutUniCode(q, lp_workgroup(),
511 sizeof(outbuf) - PTR_DIFF(q, outbuf),
512 True); /* Domain name*/
513 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
514 return;
516 SIVAL(q, 0, 1); /* our nt version */
517 SSVAL(q, 4, 0xffff); /* our lmnttoken */
518 SSVAL(q, 6, 0xffff); /* our lm20token */
519 q += 8;
522 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
524 pull_ascii_fstring(mach_str, machine);
526 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, \
527 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
528 mach_str,inet_ntoa(p->ip), reply_name, lp_workgroup(),
529 NETLOGON_RESPONSE_FROM_PDC, (uint32)ntversion, (uint32)lmnttoken,
530 (uint32)lm20token ));
532 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
534 pull_ascii_fstring(getdc_str, getdc);
535 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
537 send_mailslot(True, getdc_str,
538 outbuf,PTR_DIFF(q,outbuf),
539 global_myname(), 0x0,
540 source_name,
541 dgram->source_name.name_type,
542 p->ip, ip, p->port);
543 return;
546 case LOGON_SAM_LOGON_REQUEST:
549 fstring getdc_str;
550 fstring source_name;
551 char *source_addr;
552 char *q = buf + 2;
553 fstring asccomp;
555 if (global_nmbd_proxy_logon) {
556 nmbd_proxy_logon(global_nmbd_proxy_logon,
557 ip, p, (uint8_t *)buf, len);
558 return;
561 q += 2;
563 if (PTR_DIFF(q, buf) >= len) {
564 DEBUG(0,("process_logon_packet: bad packet\n"));
565 return;
568 unicomp = q;
569 uniuser = skip_unibuf(unicomp, PTR_DIFF(buf+len, unicomp));
571 if (PTR_DIFF(uniuser, buf) >= len) {
572 DEBUG(0,("process_logon_packet: bad packet\n"));
573 return;
576 getdc = skip_unibuf(uniuser,PTR_DIFF(buf+len, uniuser));
578 if (PTR_DIFF(getdc, buf) >= len) {
579 DEBUG(0,("process_logon_packet: bad packet\n"));
580 return;
583 q = skip_string(buf,len,getdc);
585 if (!q || PTR_DIFF(q + 8, buf) >= len) {
586 DEBUG(0,("process_logon_packet: bad packet\n"));
587 return;
590 q += 4; /* Account Control Bits - indicating username type */
591 domainsidsize = IVAL(q, 0);
592 q += 4;
594 DEBUG(5,("process_logon_packet: LOGON_SAM_LOGON_REQUEST sidsize %d, len = %d\n", domainsidsize, len));
596 if (domainsidsize < (len - PTR_DIFF(q, buf)) && (domainsidsize != 0)) {
597 q += domainsidsize;
598 q = ALIGN4(q, buf);
601 DEBUG(5,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %ld\n", len, (unsigned long)PTR_DIFF(q, buf) ));
603 if (len - PTR_DIFF(q, buf) > 8) {
604 /* with NT5 clients we can sometimes
605 get additional data - a length specificed string
606 containing the domain name, then 16 bytes of
607 data (no idea what it is) */
608 int dom_len = CVAL(q, 0);
609 q++;
610 if (dom_len < (len - PTR_DIFF(q, buf)) && (dom_len != 0)) {
611 q += dom_len + 1;
613 q += 16;
616 if (PTR_DIFF(q + 8, buf) > len) {
617 DEBUG(0,("process_logon_packet: bad packet\n"));
618 return;
621 ntversion = IVAL(q, 0);
622 lmnttoken = SVAL(q, 4);
623 lm20token = SVAL(q, 6);
624 q += 8;
626 DEBUG(3,("process_logon_packet: LOGON_SAM_LOGON_REQUEST sidsize %d ntv %d\n", domainsidsize, ntversion));
629 * we respond regadless of whether the machine is in our password
630 * database. If it isn't then we let smbd send an appropriate error.
631 * Let's ignore the SID.
633 pull_ucs2_fstring(ascuser, uniuser);
634 pull_ucs2_fstring(asccomp, unicomp);
635 DEBUG(5,("process_logon_packet: LOGON_SAM_LOGON_REQUEST user %s\n", ascuser));
637 fstrcpy(reply_name, "\\\\"); /* Here it wants \\LOGONSERVER. */
638 fstrcat(reply_name, my_name);
640 DEBUG(5,("process_logon_packet: LOGON_SAM_LOGON_REQUEST request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
641 asccomp,inet_ntoa(p->ip), ascuser, reply_name, lp_workgroup(),
642 LOGON_SAM_LOGON_RESPONSE ,lmnttoken));
644 /* Construct reply. */
646 q = outbuf;
647 /* we want the simple version unless we are an ADS PDC..which means */
648 /* never, at least for now */
649 if ((ntversion < 11) || (SEC_ADS != lp_security()) || (ROLE_DOMAIN_PDC != lp_server_role())) {
650 if (SVAL(uniuser, 0) == 0) {
651 SSVAL(q, 0, LOGON_SAM_LOGON_USER_UNKNOWN); /* user unknown */
652 } else {
653 SSVAL(q, 0, LOGON_SAM_LOGON_RESPONSE);
656 q += 2;
658 q += dos_PutUniCode(q, reply_name,
659 sizeof(outbuf) - PTR_DIFF(q, outbuf),
660 True);
661 q += dos_PutUniCode(q, ascuser,
662 sizeof(outbuf) - PTR_DIFF(q, outbuf),
663 True);
664 q += dos_PutUniCode(q, lp_workgroup(),
665 sizeof(outbuf) - PTR_DIFF(q, outbuf),
666 True);
668 #ifdef HAVE_ADS
669 else {
670 struct GUID domain_guid;
671 UUID_FLAT flat_guid;
672 char *domain;
673 char *hostname;
674 char *component, *dc, *q1;
675 char *q_orig = q;
676 int str_offset;
677 char *saveptr = NULL;
679 domain = get_mydnsdomname(talloc_tos());
680 if (!domain) {
681 DEBUG(2,
682 ("get_mydnsdomname failed.\n"));
683 return;
685 hostname = get_myname(talloc_tos());
686 if (!hostname) {
687 DEBUG(2,
688 ("get_myname failed.\n"));
689 return;
692 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
693 return;
695 if (SVAL(uniuser, 0) == 0) {
696 SIVAL(q, 0, LOGON_SAM_LOGON_USER_UNKNOWN_EX); /* user unknown */
697 } else {
698 SIVAL(q, 0, LOGON_SAM_LOGON_RESPONSE_EX);
700 q += 4;
702 SIVAL(q, 0, NBT_SERVER_PDC|NBT_SERVER_GC|NBT_SERVER_LDAP|NBT_SERVER_DS|
703 NBT_SERVER_KDC|NBT_SERVER_TIMESERV|NBT_SERVER_CLOSEST|NBT_SERVER_WRITABLE);
704 q += 4;
706 /* Push Domain GUID */
707 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < UUID_FLAT_SIZE) {
708 return;
710 if (False == secrets_fetch_domain_guid(domain, &domain_guid)) {
711 DEBUG(2, ("Could not fetch DomainGUID for %s\n", domain));
712 return;
715 smb_uuid_pack(domain_guid, &flat_guid);
716 memcpy(q, &flat_guid.info, UUID_FLAT_SIZE);
717 q += UUID_FLAT_SIZE;
719 /* Forest */
720 str_offset = q - q_orig;
721 dc = domain;
722 q1 = q;
723 while ((component = strtok_r(dc, ".", &saveptr)) != NULL) {
724 dc = NULL;
725 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
726 return;
728 size = push_ascii(&q[1], component,
729 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
731 if (size == (size_t)-1 || size > 0xff) {
732 return;
734 SCVAL(q, 0, size);
735 q += (size + 1);
738 /* Unk0 */
739 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
740 return;
742 SCVAL(q, 0, 0);
743 q++;
745 /* Domain */
746 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
747 SCVAL(q, 1, str_offset & 0xFF);
748 q += 2;
750 /* Hostname */
751 size = push_ascii(&q[1], hostname,
752 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
754 if (size == (size_t)-1 || size > 0xff) {
755 return;
757 SCVAL(q, 0, size);
758 q += (size + 1);
760 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 3) {
761 return;
764 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
765 SCVAL(q, 1, str_offset & 0xFF);
766 q += 2;
768 /* NETBIOS of domain */
769 size = push_ascii(&q[1], lp_workgroup(),
770 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
771 STR_UPPER);
772 if (size == (size_t)-1 || size > 0xff) {
773 return;
775 SCVAL(q, 0, size);
776 q += (size + 1);
778 /* Unk1 */
779 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 2) {
780 return;
783 SCVAL(q, 0, 0);
784 q++;
786 /* NETBIOS of hostname */
787 size = push_ascii(&q[1], my_name,
788 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
790 if (size == (size_t)-1 || size > 0xff) {
791 return;
793 SCVAL(q, 0, size);
794 q += (size + 1);
796 /* Unk2 */
797 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
798 return;
801 SCVAL(q, 0, 0);
802 q++;
804 /* User name */
805 if (SVAL(uniuser, 0) != 0) {
806 size = push_ascii(&q[1], ascuser,
807 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
809 if (size == (size_t)-1 || size > 0xff) {
810 return;
812 SCVAL(q, 0, size);
813 q += (size + 1);
816 q_orig = q;
817 /* Site name */
818 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
819 return;
821 size = push_ascii(&q[1], "Default-First-Site-Name",
822 sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
824 if (size == (size_t)-1 || size > 0xff) {
825 return;
827 SCVAL(q, 0, size);
828 q += (size + 1);
830 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 18) {
831 return;
834 /* Site name (2) */
835 str_offset = q - q_orig;
836 SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
837 SCVAL(q, 1, str_offset & 0xFF);
838 q += 2;
840 SCVAL(q, 0, PTR_DIFF(q,q1));
841 SCVAL(q, 1, 0x10); /* unknown */
843 SIVAL(q, 0, 0x00000002);
844 q += 4; /* unknown */
845 SIVAL(q, 0, ntohl(ip.s_addr));
846 q += 4;
847 SIVAL(q, 0, 0x00000000);
848 q += 4; /* unknown */
849 SIVAL(q, 0, 0x00000000);
850 q += 4; /* unknown */
852 #endif
854 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
855 return;
858 /* tell the client what version we are */
859 SIVAL(q, 0, ((ntversion < 11) || (SEC_ADS != lp_security())) ? 1 : 13);
860 /* our ntversion */
861 SSVAL(q, 4, 0xffff); /* our lmnttoken */
862 SSVAL(q, 6, 0xffff); /* our lm20token */
863 q += 8;
865 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
867 pull_ascii_fstring(getdc_str, getdc);
868 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
869 source_addr = SMB_STRDUP(inet_ntoa(dgram->header.source_ip));
870 if (source_addr == NULL) {
871 DEBUG(3, ("out of memory copying client"
872 " address string\n"));
873 return;
877 * handle delay.
878 * packets requeued after delay are marked as
879 * locked.
881 if ((p->locked == False) &&
882 (strlen(ascuser) == 0) &&
883 delay_logon(source_name, source_addr))
885 struct timeval when;
887 DEBUG(3, ("process_logon_packet: "
888 "delaying initial logon "
889 "reply for client %s(%s) for "
890 "%u milliseconds\n",
891 source_name, source_addr,
892 lp_init_logon_delay()));
894 when = timeval_current_ofs(0,
895 lp_init_logon_delay() * 1000);
896 p->locked = true;
897 event_add_timed(nmbd_event_context(),
898 NULL,
899 when,
900 delayed_init_logon_handler,
902 } else {
903 DEBUG(3, ("process_logon_packet: "
904 "processing delayed initial "
905 "logon reply for client "
906 "%s(%s)\n",
907 source_name, source_addr));
909 p->locked = false;
910 send_mailslot(true, getdc,
911 outbuf,PTR_DIFF(q,outbuf),
912 global_myname(), 0x0,
913 source_name,
914 dgram->source_name.name_type,
915 p->ip, ip, p->port);
918 SAFE_FREE(source_addr);
920 break;
923 /* Announce change to UAS or SAM. Send by the domain controller when a
924 replication event is required. */
926 case NETLOGON_ANNOUNCE_UAS:
927 DEBUG(5, ("Got NETLOGON_ANNOUNCE_UAS\n"));
928 break;
930 default:
931 DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
932 return;