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