RIP BOOL. Convert BOOL -> bool. I found a few interesting
[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
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         pstring my_name;
43         fstring reply_name;
44         pstring outbuf;
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         pstring 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         pstrcpy(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(pstring) - PTR_DIFF(q, outbuf),
241                                                 True); /* PDC name */
242                                         q += dos_PutUniCode(q, lp_workgroup(),
243                                                 sizeof(pstring) - PTR_DIFF(q, outbuf),
244                                                 True); /* Domain name*/
245                                         if (sizeof(pstring) - 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_pstring(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(pstring) - PTR_DIFF(q, outbuf),
385                                                 True);
386                                         q += dos_PutUniCode(q, ascuser,
387                                                 sizeof(pstring) - PTR_DIFF(q, outbuf),
388                                                 True);
389                                         q += dos_PutUniCode(q, lp_workgroup(),
390                                                 sizeof(pstring) - PTR_DIFF(q, outbuf),
391                                                 True);
392                                 }
393 #ifdef HAVE_ADS
394                                 else {
395                                         struct GUID domain_guid;
396                                         UUID_FLAT flat_guid;
397                                         pstring domain;
398                                         pstring hostname;
399                                         char *component, *dc, *q1;
400                                         char *q_orig = q;
401                                         int str_offset;
402
403                                         get_mydnsdomname(domain);
404                                         get_myname(hostname);
405
406                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
407                                                 return;
408                                         }
409                                         if (SVAL(uniuser, 0) == 0) {
410                                                 SIVAL(q, 0, SAMLOGON_AD_UNK_R); /* user unknown */
411                                         } else {
412                                                 SIVAL(q, 0, SAMLOGON_AD_R);
413                                         }
414                                         q += 4;
415
416                                         SIVAL(q, 0, ADS_PDC|ADS_GC|ADS_LDAP|ADS_DS|
417                                                 ADS_KDC|ADS_TIMESERV|ADS_CLOSEST|ADS_WRITABLE);
418                                         q += 4;
419
420                                         /* Push Domain GUID */
421                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < UUID_FLAT_SIZE) {
422                                                 return;
423                                         }
424                                         if (False == secrets_fetch_domain_guid(domain, &domain_guid)) {
425                                                 DEBUG(2, ("Could not fetch DomainGUID for %s\n", domain));
426                                                 return;
427                                         }
428
429                                         smb_uuid_pack(domain_guid, &flat_guid);
430                                         memcpy(q, &flat_guid.info, UUID_FLAT_SIZE);
431                                         q += UUID_FLAT_SIZE;
432
433                                         /* Forest */
434                                         str_offset = q - q_orig;
435                                         dc = domain;
436                                         q1 = q;
437                                         while ((component = strtok(dc, "."))) {
438                                                 dc = NULL;
439                                                 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
440                                                         return;
441                                                 }
442                                                 size = push_ascii(&q[1], component,
443                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
444                                                         0);
445                                                 if (size == (size_t)-1 || size > 0xff) {
446                                                         return;
447                                                 }
448                                                 SCVAL(q, 0, size);
449                                                 q += (size + 1);
450                                         }
451
452                                         /* Unk0 */
453                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
454                                                 return;
455                                         }
456                                         SCVAL(q, 0, 0);
457                                         q++;
458
459                                         /* Domain */
460                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
461                                         SCVAL(q, 1, str_offset & 0xFF);
462                                         q += 2;
463
464                                         /* Hostname */
465                                         size = push_ascii(&q[1], hostname,
466                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
467                                                         0);
468                                         if (size == (size_t)-1 || size > 0xff) {
469                                                 return;
470                                         }
471                                         SCVAL(q, 0, size);
472                                         q += (size + 1);
473
474                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 3) {
475                                                 return;
476                                         }
477
478                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
479                                         SCVAL(q, 1, str_offset & 0xFF);
480                                         q += 2;
481
482                                         /* NETBIOS of domain */
483                                         size = push_ascii(&q[1], lp_workgroup(),
484                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
485                                                         STR_UPPER);
486                                         if (size == (size_t)-1 || size > 0xff) {
487                                                 return;
488                                         }
489                                         SCVAL(q, 0, size);
490                                         q += (size + 1);
491
492                                         /* Unk1 */
493                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 2) {
494                                                 return;
495                                         }
496
497                                         SCVAL(q, 0, 0);
498                                         q++;
499
500                                         /* NETBIOS of hostname */
501                                         size = push_ascii(&q[1], my_name,
502                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
503                                                         0);
504                                         if (size == (size_t)-1 || size > 0xff) {
505                                                 return;
506                                         }
507                                         SCVAL(q, 0, size);
508                                         q += (size + 1);
509
510                                         /* Unk2 */
511                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
512                                                 return;
513                                         }
514
515                                         SCVAL(q, 0, 0);
516                                         q++;
517
518                                         /* User name */
519                                         if (SVAL(uniuser, 0) != 0) {
520                                                 size = push_ascii(&q[1], ascuser,
521                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
522                                                         0);
523                                                 if (size == (size_t)-1 || size > 0xff) {
524                                                         return;
525                                                 }
526                                                 SCVAL(q, 0, size);
527                                                 q += (size + 1);
528                                         }
529
530                                         q_orig = q;
531                                         /* Site name */
532                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
533                                                 return;
534                                         }
535                                         size = push_ascii(&q[1], "Default-First-Site-Name",
536                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
537                                                         0);
538                                         if (size == (size_t)-1 || size > 0xff) {
539                                                 return;
540                                         }
541                                         SCVAL(q, 0, size);
542                                         q += (size + 1);
543
544                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 18) {
545                                                 return;
546                                         }
547
548                                         /* Site name (2) */
549                                         str_offset = q - q_orig;
550                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
551                                         SCVAL(q, 1, str_offset & 0xFF);
552                                         q += 2;
553
554                                         SCVAL(q, 0, PTR_DIFF(q,q1));
555                                         SCVAL(q, 1, 0x10); /* unknown */
556
557                                         SIVAL(q, 0, 0x00000002);
558                                         q += 4; /* unknown */
559                                         SIVAL(q, 0, ntohl(ip.s_addr));
560                                         q += 4;
561                                         SIVAL(q, 0, 0x00000000);
562                                         q += 4; /* unknown */
563                                         SIVAL(q, 0, 0x00000000);
564                                         q += 4; /* unknown */
565                                 }
566 #endif
567
568                                 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
569                                         return;
570                                 }
571
572                                 /* tell the client what version we are */
573                                 SIVAL(q, 0, ((ntversion < 11) || (SEC_ADS != lp_security())) ? 1 : 13); 
574                                 /* our ntversion */
575                                 SSVAL(q, 4, 0xffff); /* our lmnttoken */
576                                 SSVAL(q, 6, 0xffff); /* our lm20token */
577                                 q += 8;
578
579                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
580
581                                 pull_ascii_fstring(getdc_str, getdc);
582                                 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
583
584                                 send_mailslot(True, getdc,
585                                         outbuf,PTR_DIFF(q,outbuf),
586                                         global_myname(), 0x0,
587                                         source_name,
588                                         dgram->source_name.name_type,
589                                         p->ip, ip, p->port);
590                                 break;
591                         }
592
593                 /* Announce change to UAS or SAM.  Send by the domain controller when a
594                 replication event is required. */
595
596                 case SAM_UAS_CHANGE:
597                         DEBUG(5, ("Got SAM_UAS_CHANGE\n"));
598                         break;
599
600                 default:
601                         DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
602                         return;
603         }
604 }