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