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