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