Merge commit 'origin/v3-2-test' into v3-2-stable
[ira/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
28 struct sam_database_info {
29         uint32 index;
30         uint32 serial_lo, serial_hi;
31         uint32 date_lo, date_hi;
32 };
33
34 /****************************************************************************
35 Process a domain logon packet
36 **************************************************************************/
37
38 void process_logon_packet(struct packet_struct *p, char *buf,int len, 
39                           const char *mailslot)
40 {
41         struct dgram_packet *dgram = &p->packet.dgram;
42         fstring my_name;
43         fstring reply_name;
44         char outbuf[1024];
45         int code;
46         uint16 token = 0;
47         uint32 ntversion = 0;
48         uint16 lmnttoken = 0;
49         uint16 lm20token = 0;
50         uint32 domainsidsize;
51         bool short_request = False;
52         char *getdc;
53         char *uniuser; /* Unicode user name. */
54         fstring ascuser;
55         char *unicomp; /* Unicode computer name. */
56         size_t size;
57         struct sockaddr_storage ss;
58         const struct sockaddr_storage *pss;
59         struct in_addr ip;
60
61         in_addr_to_sockaddr_storage(&ss, p->ip);
62         pss = iface_ip(&ss);
63         if (!pss) {
64                 DEBUG(5,("process_logon_packet:can't find outgoing interface "
65                         "for packet from IP %s\n",
66                         inet_ntoa(p->ip) ));
67                 return;
68         }
69         ip = ((struct sockaddr_in *)pss)->sin_addr;
70
71         memset(outbuf, 0, sizeof(outbuf));
72
73         if (!lp_domain_logons()) {
74                 DEBUG(5,("process_logon_packet: Logon packet received from IP %s and domain \
75 logons are not enabled.\n", inet_ntoa(p->ip) ));
76                 return;
77         }
78
79         fstrcpy(my_name, global_myname());
80
81         code = get_safe_SVAL(buf,len,buf,0,-1);
82         DEBUG(4,("process_logon_packet: Logon from %s: code = 0x%x\n", inet_ntoa(p->ip), code));
83
84         switch (code) {
85                 case 0:
86                         {
87                                 fstring mach_str, user_str, getdc_str;
88                                 char *q = buf + 2;
89                                 char *machine = q;
90                                 char *user = skip_string(buf,len,machine);
91
92                                 if (!user || PTR_DIFF(user, buf) >= len) {
93                                         DEBUG(0,("process_logon_packet: bad packet\n"));
94                                         return;
95                                 }
96                                 getdc = skip_string(buf,len,user);
97
98                                 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
99                                         DEBUG(0,("process_logon_packet: bad packet\n"));
100                                         return;
101                                 }
102                                 q = skip_string(buf,len,getdc);
103
104                                 if (!q || PTR_DIFF(q + 5, buf) > len) {
105                                         DEBUG(0,("process_logon_packet: bad packet\n"));
106                                         return;
107                                 }
108                                 token = SVAL(q,3);
109
110                                 fstrcpy(reply_name,my_name);
111
112                                 pull_ascii_fstring(mach_str, machine);
113                                 pull_ascii_fstring(user_str, user);
114                                 pull_ascii_fstring(getdc_str, getdc);
115
116                                 DEBUG(5,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
117                                         mach_str,inet_ntoa(p->ip),user_str,token));
118
119                                 q = outbuf;
120                                 SSVAL(q, 0, 6);
121                                 q += 2;
122
123                                 fstrcpy(reply_name, "\\\\");
124                                 fstrcat(reply_name, my_name);
125                                 size = push_ascii(q,reply_name,
126                                                 sizeof(outbuf)-PTR_DIFF(q, outbuf),
127                                                 STR_TERMINATE);
128                                 if (size == (size_t)-1) {
129                                         return;
130                                 }
131                                 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
132
133                                 SSVAL(q, 0, token);
134                                 q += 2;
135
136                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
137
138                                 send_mailslot(True, getdc_str,
139                                                 outbuf,PTR_DIFF(q,outbuf),
140                                                 global_myname(), 0x0,
141                                                 mach_str,
142                                                 dgram->source_name.name_type,
143                                                 p->ip, ip, p->port);
144                                 break;
145                         }
146
147                 case QUERYFORPDC:
148                         {
149                                 fstring mach_str, getdc_str;
150                                 fstring source_name;
151                                 char *q = buf + 2;
152                                 char *machine = q;
153
154                                 if (!lp_domain_master()) {
155                                         /* We're not Primary Domain Controller -- ignore this */
156                                         return;
157                                 }
158
159                                 getdc = skip_string(buf,len,machine);
160
161                                 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
162                                         DEBUG(0,("process_logon_packet: bad packet\n"));
163                                         return;
164                                 }
165                                 q = skip_string(buf,len,getdc);
166
167                                 if (!q || PTR_DIFF(q, buf) >= len) {
168                                         DEBUG(0,("process_logon_packet: bad packet\n"));
169                                         return;
170                                 }
171                                 q = ALIGN2(q, buf);
172
173                                 /* At this point we can work out if this is a W9X or NT style
174                                    request. Experiments show that the difference is wether the
175                                    packet ends here. For a W9X request we now end with a pair of
176                                    bytes (usually 0xFE 0xFF) whereas with NT we have two further
177                                    strings - the following is a simple way of detecting this */
178
179                                 if (len - PTR_DIFF(q, buf) <= 3) {
180                                         short_request = True;
181                                 } else {
182                                         unicomp = q;
183
184                                         if (PTR_DIFF(q, buf) >= len) {
185                                                 DEBUG(0,("process_logon_packet: bad packet\n"));
186                                                 return;
187                                         }
188
189                                         /* A full length (NT style) request */
190                                         q = skip_unibuf(unicomp, PTR_DIFF(buf + len, unicomp));
191
192                                         if (PTR_DIFF(q, buf) >= len) {
193                                                 DEBUG(0,("process_logon_packet: bad packet\n"));
194                                                 return;
195                                         }
196
197                                         if (len - PTR_DIFF(q, buf) > 8) {
198                                                 /* with NT5 clients we can sometimes
199                                                         get additional data - a length specificed string
200                                                         containing the domain name, then 16 bytes of
201                                                         data (no idea what it is) */
202                                                 int dom_len = CVAL(q, 0);
203                                                 q++;
204                                                 if (dom_len != 0) {
205                                                         q += dom_len + 1;
206                                                 }
207                                                 q += 16;
208                                         }
209
210                                         if (PTR_DIFF(q + 8, buf) > len) {
211                                                 DEBUG(0,("process_logon_packet: bad packet\n"));
212                                                 return;
213                                         }
214
215                                         ntversion = IVAL(q, 0);
216                                         lmnttoken = SVAL(q, 4);
217                                         lm20token = SVAL(q, 6);
218                                 }
219
220                                 /* Construct reply. */
221                                 q = outbuf;
222                                 SSVAL(q, 0, QUERYFORPDC_R);
223                                 q += 2;
224
225                                 fstrcpy(reply_name,my_name);
226                                 size = push_ascii(q, reply_name,
227                                                 sizeof(outbuf)-PTR_DIFF(q, outbuf),
228                                                 STR_TERMINATE);
229                                 if (size == (size_t)-1) {
230                                         return;
231                                 }
232                                 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
233
234                                 /* PDC and domain name */
235                                 if (!short_request) {
236                                         /* Make a full reply */
237                                         q = ALIGN2(q, outbuf);
238
239                                         q += dos_PutUniCode(q, my_name,
240                                                 sizeof(outbuf) - PTR_DIFF(q, outbuf),
241                                                 True); /* PDC name */
242                                         q += dos_PutUniCode(q, lp_workgroup(),
243                                                 sizeof(outbuf) - PTR_DIFF(q, outbuf),
244                                                 True); /* Domain name*/
245                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
246                                                 return;
247                                         }
248                                         SIVAL(q, 0, 1); /* our nt version */
249                                         SSVAL(q, 4, 0xffff); /* our lmnttoken */
250                                         SSVAL(q, 6, 0xffff); /* our lm20token */
251                                         q += 8;
252                                 }
253
254                                 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
255
256                                 pull_ascii_fstring(mach_str, machine);
257
258                                 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, \
259 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
260                                         mach_str,inet_ntoa(p->ip), reply_name, lp_workgroup(),
261                                         QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
262                                         (uint32)lm20token ));
263
264                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
265
266                                 pull_ascii_fstring(getdc_str, getdc);
267                                 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
268
269                                 send_mailslot(True, getdc_str,
270                                         outbuf,PTR_DIFF(q,outbuf),
271                                         global_myname(), 0x0,
272                                         source_name,
273                                         dgram->source_name.name_type,
274                                         p->ip, ip, p->port);
275                                 return;
276                         }
277
278                 case SAMLOGON:
279
280                         {
281                                 fstring getdc_str;
282                                 fstring source_name;
283                                 char *q = buf + 2;
284                                 fstring asccomp;
285
286                                 q += 2;
287
288                                 if (PTR_DIFF(q, buf) >= len) {
289                                         DEBUG(0,("process_logon_packet: bad packet\n"));
290                                         return;
291                                 }
292
293                                 unicomp = q;
294                                 uniuser = skip_unibuf(unicomp, PTR_DIFF(buf+len, unicomp));
295
296                                 if (PTR_DIFF(uniuser, buf) >= len) {
297                                         DEBUG(0,("process_logon_packet: bad packet\n"));
298                                         return;
299                                 }
300
301                                 getdc = skip_unibuf(uniuser,PTR_DIFF(buf+len, uniuser));
302
303                                 if (PTR_DIFF(getdc, buf) >= len) {
304                                         DEBUG(0,("process_logon_packet: bad packet\n"));
305                                         return;
306                                 }
307
308                                 q = skip_string(buf,len,getdc);
309
310                                 if (!q || PTR_DIFF(q + 8, buf) >= len) {
311                                         DEBUG(0,("process_logon_packet: bad packet\n"));
312                                         return;
313                                 }
314
315                                 q += 4; /* Account Control Bits - indicating username type */
316                                 domainsidsize = IVAL(q, 0);
317                                 q += 4;
318
319                                 DEBUG(5,("process_logon_packet: SAMLOGON sidsize %d, len = %d\n", domainsidsize, len));
320
321                                 if (domainsidsize < (len - PTR_DIFF(q, buf)) && (domainsidsize != 0)) {
322                                         q += domainsidsize;
323                                         q = ALIGN4(q, buf);
324                                 }
325
326                                 DEBUG(5,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %ld\n", len, (unsigned long)PTR_DIFF(q, buf) ));
327
328                                 if (len - PTR_DIFF(q, buf) > 8) {
329                                         /* with NT5 clients we can sometimes
330                                                 get additional data - a length specificed string
331                                                 containing the domain name, then 16 bytes of
332                                                 data (no idea what it is) */
333                                         int dom_len = CVAL(q, 0);
334                                         q++;
335                                         if (dom_len < (len - PTR_DIFF(q, buf)) && (dom_len != 0)) {
336                                                 q += dom_len + 1;
337                                         }
338                                         q += 16;
339                                 }
340
341                                 if (PTR_DIFF(q + 8, buf) > len) {
342                                         DEBUG(0,("process_logon_packet: bad packet\n"));
343                                         return;
344                                 }
345
346                                 ntversion = IVAL(q, 0);
347                                 lmnttoken = SVAL(q, 4);
348                                 lm20token = SVAL(q, 6);
349                                 q += 8;
350
351                                 DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
352
353                                 /*
354                                  * we respond regadless of whether the machine is in our password 
355                                  * database. If it isn't then we let smbd send an appropriate error.
356                                  * Let's ignore the SID.
357                                  */
358                                 pull_ucs2_fstring(ascuser, uniuser);
359                                 pull_ucs2_fstring(asccomp, unicomp);
360                                 DEBUG(5,("process_logon_packet: SAMLOGON user %s\n", ascuser));
361
362                                 fstrcpy(reply_name, "\\\\"); /* Here it wants \\LOGONSERVER. */
363                                 fstrcat(reply_name, my_name);
364
365                                 DEBUG(5,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
366                                         asccomp,inet_ntoa(p->ip), ascuser, reply_name, lp_workgroup(),
367                                 SAMLOGON_R ,lmnttoken));
368
369                                 /* Construct reply. */
370
371                                 q = outbuf;
372                                 /* we want the simple version unless we are an ADS PDC..which means  */
373                                 /* never, at least for now */
374                                 if ((ntversion < 11) || (SEC_ADS != lp_security()) || (ROLE_DOMAIN_PDC != lp_server_role())) {
375                                         if (SVAL(uniuser, 0) == 0) {
376                                                 SSVAL(q, 0, SAMLOGON_UNK_R);    /* user unknown */
377                                         } else {
378                                                 SSVAL(q, 0, SAMLOGON_R);
379                                         }
380
381                                         q += 2;
382
383                                         q += dos_PutUniCode(q, reply_name,
384                                                 sizeof(outbuf) - PTR_DIFF(q, outbuf),
385                                                 True);
386                                         q += dos_PutUniCode(q, ascuser,
387                                                 sizeof(outbuf) - PTR_DIFF(q, outbuf),
388                                                 True);
389                                         q += dos_PutUniCode(q, lp_workgroup(),
390                                                 sizeof(outbuf) - PTR_DIFF(q, outbuf),
391                                                 True);
392                                 }
393 #ifdef HAVE_ADS
394                                 else {
395                                         struct GUID domain_guid;
396                                         UUID_FLAT flat_guid;
397                                         char *domain;
398                                         char *hostname;
399                                         char *component, *dc, *q1;
400                                         char *q_orig = q;
401                                         int str_offset;
402                                         char *saveptr;
403
404                                         domain = get_mydnsdomname(talloc_tos());
405                                         if (!domain) {
406                                                 DEBUG(2,
407                                                 ("get_mydnsdomname failed.\n"));
408                                                 return;
409                                         }
410                                         hostname = get_myname(talloc_tos());
411                                         if (!hostname) {
412                                                 DEBUG(2,
413                                                 ("get_myname failed.\n"));
414                                                 return;
415                                         }
416
417                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
418                                                 return;
419                                         }
420                                         if (SVAL(uniuser, 0) == 0) {
421                                                 SIVAL(q, 0, SAMLOGON_AD_UNK_R); /* user unknown */
422                                         } else {
423                                                 SIVAL(q, 0, SAMLOGON_AD_R);
424                                         }
425                                         q += 4;
426
427                                         SIVAL(q, 0, ADS_PDC|ADS_GC|ADS_LDAP|ADS_DS|
428                                                 ADS_KDC|ADS_TIMESERV|ADS_CLOSEST|ADS_WRITABLE);
429                                         q += 4;
430
431                                         /* Push Domain GUID */
432                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < UUID_FLAT_SIZE) {
433                                                 return;
434                                         }
435                                         if (False == secrets_fetch_domain_guid(domain, &domain_guid)) {
436                                                 DEBUG(2, ("Could not fetch DomainGUID for %s\n", domain));
437                                                 return;
438                                         }
439
440                                         smb_uuid_pack(domain_guid, &flat_guid);
441                                         memcpy(q, &flat_guid.info, UUID_FLAT_SIZE);
442                                         q += UUID_FLAT_SIZE;
443
444                                         /* Forest */
445                                         str_offset = q - q_orig;
446                                         dc = domain;
447                                         q1 = q;
448                                         while ((component = strtok_r(dc, ".", &saveptr)) != NULL) {
449                                                 dc = NULL;
450                                                 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
451                                                         return;
452                                                 }
453                                                 size = push_ascii(&q[1], component,
454                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
455                                                         0);
456                                                 if (size == (size_t)-1 || size > 0xff) {
457                                                         return;
458                                                 }
459                                                 SCVAL(q, 0, size);
460                                                 q += (size + 1);
461                                         }
462
463                                         /* Unk0 */
464                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
465                                                 return;
466                                         }
467                                         SCVAL(q, 0, 0);
468                                         q++;
469
470                                         /* Domain */
471                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
472                                         SCVAL(q, 1, str_offset & 0xFF);
473                                         q += 2;
474
475                                         /* Hostname */
476                                         size = push_ascii(&q[1], hostname,
477                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
478                                                         0);
479                                         if (size == (size_t)-1 || size > 0xff) {
480                                                 return;
481                                         }
482                                         SCVAL(q, 0, size);
483                                         q += (size + 1);
484
485                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 3) {
486                                                 return;
487                                         }
488
489                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
490                                         SCVAL(q, 1, str_offset & 0xFF);
491                                         q += 2;
492
493                                         /* NETBIOS of domain */
494                                         size = push_ascii(&q[1], lp_workgroup(),
495                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
496                                                         STR_UPPER);
497                                         if (size == (size_t)-1 || size > 0xff) {
498                                                 return;
499                                         }
500                                         SCVAL(q, 0, size);
501                                         q += (size + 1);
502
503                                         /* Unk1 */
504                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 2) {
505                                                 return;
506                                         }
507
508                                         SCVAL(q, 0, 0);
509                                         q++;
510
511                                         /* NETBIOS of hostname */
512                                         size = push_ascii(&q[1], my_name,
513                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
514                                                         0);
515                                         if (size == (size_t)-1 || size > 0xff) {
516                                                 return;
517                                         }
518                                         SCVAL(q, 0, size);
519                                         q += (size + 1);
520
521                                         /* Unk2 */
522                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
523                                                 return;
524                                         }
525
526                                         SCVAL(q, 0, 0);
527                                         q++;
528
529                                         /* User name */
530                                         if (SVAL(uniuser, 0) != 0) {
531                                                 size = push_ascii(&q[1], ascuser,
532                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
533                                                         0);
534                                                 if (size == (size_t)-1 || size > 0xff) {
535                                                         return;
536                                                 }
537                                                 SCVAL(q, 0, size);
538                                                 q += (size + 1);
539                                         }
540
541                                         q_orig = q;
542                                         /* Site name */
543                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
544                                                 return;
545                                         }
546                                         size = push_ascii(&q[1], "Default-First-Site-Name",
547                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
548                                                         0);
549                                         if (size == (size_t)-1 || size > 0xff) {
550                                                 return;
551                                         }
552                                         SCVAL(q, 0, size);
553                                         q += (size + 1);
554
555                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 18) {
556                                                 return;
557                                         }
558
559                                         /* Site name (2) */
560                                         str_offset = q - q_orig;
561                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
562                                         SCVAL(q, 1, str_offset & 0xFF);
563                                         q += 2;
564
565                                         SCVAL(q, 0, PTR_DIFF(q,q1));
566                                         SCVAL(q, 1, 0x10); /* unknown */
567
568                                         SIVAL(q, 0, 0x00000002);
569                                         q += 4; /* unknown */
570                                         SIVAL(q, 0, ntohl(ip.s_addr));
571                                         q += 4;
572                                         SIVAL(q, 0, 0x00000000);
573                                         q += 4; /* unknown */
574                                         SIVAL(q, 0, 0x00000000);
575                                         q += 4; /* unknown */
576                                 }
577 #endif
578
579                                 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
580                                         return;
581                                 }
582
583                                 /* tell the client what version we are */
584                                 SIVAL(q, 0, ((ntversion < 11) || (SEC_ADS != lp_security())) ? 1 : 13); 
585                                 /* our ntversion */
586                                 SSVAL(q, 4, 0xffff); /* our lmnttoken */
587                                 SSVAL(q, 6, 0xffff); /* our lm20token */
588                                 q += 8;
589
590                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
591
592                                 pull_ascii_fstring(getdc_str, getdc);
593                                 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
594
595                                 send_mailslot(True, getdc,
596                                         outbuf,PTR_DIFF(q,outbuf),
597                                         global_myname(), 0x0,
598                                         source_name,
599                                         dgram->source_name.name_type,
600                                         p->ip, ip, p->port);
601                                 break;
602                         }
603
604                 /* Announce change to UAS or SAM.  Send by the domain controller when a
605                 replication event is required. */
606
607                 case SAM_UAS_CHANGE:
608                         DEBUG(5, ("Got SAM_UAS_CHANGE\n"));
609                         break;
610
611                 default:
612                         DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
613                         return;
614         }
615 }