r25138: More pstring elimination. Add a TALLOC_CTX parameter
[kai/samba.git] / source3 / nmbd / nmbd_processlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6    Copyright (C) Jeremy Allison 1994-2003
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21    
22    Revision History:
23
24 */
25
26 #include "includes.h"
27
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(q,reply_name,
112                                                 sizeof(outbuf)-PTR_DIFF(q, outbuf),
113                                                 STR_TERMINATE);
114                                 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
115
116                                 SSVAL(q, 0, token);
117                                 q += 2;
118
119                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
120
121                                 send_mailslot(True, getdc_str,
122                                                 outbuf,PTR_DIFF(q,outbuf),
123                                                 global_myname(), 0x0,
124                                                 mach_str,
125                                                 dgram->source_name.name_type,
126                                                 p->ip, *iface_ip(p->ip), p->port);  
127                                 break;
128                         }
129
130                 case QUERYFORPDC:
131                         {
132                                 fstring mach_str, getdc_str;
133                                 fstring source_name;
134                                 char *q = buf + 2;
135                                 char *machine = q;
136
137                                 if (!lp_domain_master()) {
138                                         /* We're not Primary Domain Controller -- ignore this */
139                                         return;
140                                 }
141
142                                 getdc = skip_string(buf,len,machine);
143
144                                 if (!getdc || PTR_DIFF(getdc, buf) >= len) {
145                                         DEBUG(0,("process_logon_packet: bad packet\n"));
146                                         return;
147                                 }
148                                 q = skip_string(buf,len,getdc);
149
150                                 if (!q || PTR_DIFF(q, buf) >= len) {
151                                         DEBUG(0,("process_logon_packet: bad packet\n"));
152                                         return;
153                                 }
154                                 q = ALIGN2(q, buf);
155
156                                 /* At this point we can work out if this is a W9X or NT style
157                                    request. Experiments show that the difference is wether the
158                                    packet ends here. For a W9X request we now end with a pair of
159                                    bytes (usually 0xFE 0xFF) whereas with NT we have two further
160                                    strings - the following is a simple way of detecting this */
161
162                                 if (len - PTR_DIFF(q, buf) <= 3) {
163                                         short_request = True;
164                                 } else {
165                                         unicomp = q;
166
167                                         if (PTR_DIFF(q, buf) >= len) {
168                                                 DEBUG(0,("process_logon_packet: bad packet\n"));
169                                                 return;
170                                         }
171
172                                         /* A full length (NT style) request */
173                                         q = skip_unibuf(unicomp, PTR_DIFF(buf + len, unicomp));
174
175                                         if (PTR_DIFF(q, buf) >= len) {
176                                                 DEBUG(0,("process_logon_packet: bad packet\n"));
177                                                 return;
178                                         }
179
180                                         if (len - PTR_DIFF(q, buf) > 8) {
181                                                 /* with NT5 clients we can sometimes
182                                                         get additional data - a length specificed string
183                                                         containing the domain name, then 16 bytes of
184                                                         data (no idea what it is) */
185                                                 int dom_len = CVAL(q, 0);
186                                                 q++;
187                                                 if (dom_len != 0) {
188                                                         q += dom_len + 1;
189                                                 }
190                                                 q += 16;
191                                         }
192
193                                         if (PTR_DIFF(q + 8, buf) > len) {
194                                                 DEBUG(0,("process_logon_packet: bad packet\n"));
195                                                 return;
196                                         }
197
198                                         ntversion = IVAL(q, 0);
199                                         lmnttoken = SVAL(q, 4);
200                                         lm20token = SVAL(q, 6);
201                                 }
202
203                                 /* Construct reply. */
204                                 q = outbuf;
205                                 SSVAL(q, 0, QUERYFORPDC_R);
206                                 q += 2;
207
208                                 fstrcpy(reply_name,my_name);
209                                 push_ascii(q, reply_name,
210                                                 sizeof(outbuf)-PTR_DIFF(q, outbuf),
211                                                 STR_TERMINATE);
212                                 q = skip_string(outbuf,sizeof(outbuf),q); /* PDC name */
213
214                                 /* PDC and domain name */
215                                 if (!short_request) {
216                                         /* Make a full reply */
217                                         q = ALIGN2(q, outbuf);
218
219                                         q += dos_PutUniCode(q, my_name,
220                                                 sizeof(pstring) - PTR_DIFF(q, outbuf),
221                                                 True); /* PDC name */
222                                         q += dos_PutUniCode(q, lp_workgroup(),
223                                                 sizeof(pstring) - (q-outbuf),
224                                                 True); /* Domain name*/
225                                         if (sizeof(pstring) - PTR_DIFF(q, outbuf) < 8) {
226                                                 return;
227                                         }
228                                         SIVAL(q, 0, 1); /* our nt version */
229                                         SSVAL(q, 4, 0xffff); /* our lmnttoken */
230                                         SSVAL(q, 6, 0xffff); /* our lm20token */
231                                         q += 8;
232                                 }
233
234                                 /* RJS, 21-Feb-2000, we send a short reply if the request was short */
235
236                                 pull_ascii_fstring(mach_str, machine);
237
238                                 DEBUG(5,("process_logon_packet: GETDC request from %s at IP %s, \
239 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
240                                         mach_str,inet_ntoa(p->ip), reply_name, lp_workgroup(),
241                                         QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
242                                         (uint32)lm20token ));
243
244                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
245
246                                 pull_ascii_fstring(getdc_str, getdc);
247                                 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
248
249                                 send_mailslot(True, getdc_str,
250                                         outbuf,PTR_DIFF(q,outbuf),
251                                         global_myname(), 0x0,
252                                         source_name,
253                                         dgram->source_name.name_type,
254                                         p->ip, *iface_ip(p->ip), p->port);  
255                                 return;
256                         }
257
258                 case SAMLOGON:
259
260                         {
261                                 fstring getdc_str;
262                                 fstring source_name;
263                                 char *q = buf + 2;
264                                 fstring asccomp;
265
266                                 q += 2;
267
268                                 if (PTR_DIFF(q, buf) >= len) {
269                                         DEBUG(0,("process_logon_packet: bad packet\n"));
270                                         return;
271                                 }
272
273                                 unicomp = q;
274                                 uniuser = skip_unibuf(unicomp, PTR_DIFF(buf+len, unicomp));
275
276                                 if (PTR_DIFF(uniuser, buf) >= len) {
277                                         DEBUG(0,("process_logon_packet: bad packet\n"));
278                                         return;
279                                 }
280
281                                 getdc = skip_unibuf(uniuser,PTR_DIFF(buf+len, uniuser));
282
283                                 if (PTR_DIFF(getdc, buf) >= len) {
284                                         DEBUG(0,("process_logon_packet: bad packet\n"));
285                                         return;
286                                 }
287
288                                 q = skip_string(buf,len,getdc);
289
290                                 if (!q || PTR_DIFF(q + 8, buf) >= len) {
291                                         DEBUG(0,("process_logon_packet: bad packet\n"));
292                                         return;
293                                 }
294
295                                 q += 4; /* Account Control Bits - indicating username type */
296                                 domainsidsize = IVAL(q, 0);
297                                 q += 4;
298
299                                 DEBUG(5,("process_logon_packet: SAMLOGON sidsize %d, len = %d\n", domainsidsize, len));
300
301                                 if (domainsidsize < (len - PTR_DIFF(q, buf)) && (domainsidsize != 0)) {
302                                         q += domainsidsize;
303                                         q = ALIGN4(q, buf);
304                                 }
305
306                                 DEBUG(5,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %ld\n", len, (unsigned long)PTR_DIFF(q, buf) ));
307
308                                 if (len - PTR_DIFF(q, buf) > 8) {
309                                         /* with NT5 clients we can sometimes
310                                                 get additional data - a length specificed string
311                                                 containing the domain name, then 16 bytes of
312                                                 data (no idea what it is) */
313                                         int dom_len = CVAL(q, 0);
314                                         q++;
315                                         if (dom_len < (len - PTR_DIFF(q, buf)) && (dom_len != 0)) {
316                                                 q += dom_len + 1;
317                                         }
318                                         q += 16;
319                                 }
320
321                                 if (PTR_DIFF(q + 8, buf) > len) {
322                                         DEBUG(0,("process_logon_packet: bad packet\n"));
323                                         return;
324                                 }
325
326                                 ntversion = IVAL(q, 0);
327                                 lmnttoken = SVAL(q, 4);
328                                 lm20token = SVAL(q, 6);
329                                 q += 8;
330
331                                 DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
332
333                                 /*
334                                  * we respond regadless of whether the machine is in our password 
335                                  * database. If it isn't then we let smbd send an appropriate error.
336                                  * Let's ignore the SID.
337                                  */
338                                 pull_ucs2_pstring(ascuser, uniuser);
339                                 pull_ucs2_fstring(asccomp, unicomp);
340                                 DEBUG(5,("process_logon_packet: SAMLOGON user %s\n", ascuser));
341
342                                 fstrcpy(reply_name, "\\\\"); /* Here it wants \\LOGONSERVER. */
343                                 fstrcat(reply_name, my_name);
344
345                                 DEBUG(5,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
346                                         asccomp,inet_ntoa(p->ip), ascuser, reply_name, lp_workgroup(),
347                                 SAMLOGON_R ,lmnttoken));
348
349                                 /* Construct reply. */
350
351                                 q = outbuf;
352                                 /* we want the simple version unless we are an ADS PDC..which means  */
353                                 /* never, at least for now */
354                                 if ((ntversion < 11) || (SEC_ADS != lp_security()) || (ROLE_DOMAIN_PDC != lp_server_role())) {
355                                         if (SVAL(uniuser, 0) == 0) {
356                                                 SSVAL(q, 0, SAMLOGON_UNK_R);    /* user unknown */
357                                         } else {
358                                                 SSVAL(q, 0, SAMLOGON_R);
359                                         }
360
361                                         q += 2;
362
363                                         q += dos_PutUniCode(q, reply_name,
364                                                 sizeof(pstring) - PTR_DIFF(q, outbuf),
365                                                 True);
366                                         q += dos_PutUniCode(q, ascuser,
367                                                 sizeof(pstring) - PTR_DIFF(q, outbuf),
368                                                 True);
369                                         q += dos_PutUniCode(q, lp_workgroup(),
370                                                 sizeof(pstring) - PTR_DIFF(q, outbuf),
371                                                 True);
372                                 }
373 #ifdef HAVE_ADS
374                                 else {
375                                         struct GUID domain_guid;
376                                         UUID_FLAT flat_guid;
377                                         pstring domain;
378                                         pstring hostname;
379                                         char *component, *dc, *q1;
380                                         uint8 size;
381                                         char *q_orig = q;
382                                         int str_offset;
383
384                                         get_mydnsdomname(domain);
385                                         get_myname(hostname);
386
387                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
388                                                 return;
389                                         }
390                                         if (SVAL(uniuser, 0) == 0) {
391                                                 SIVAL(q, 0, SAMLOGON_AD_UNK_R); /* user unknown */
392                                         } else {
393                                                 SIVAL(q, 0, SAMLOGON_AD_R);
394                                         }
395                                         q += 4;
396
397                                         SIVAL(q, 0, ADS_PDC|ADS_GC|ADS_LDAP|ADS_DS|
398                                                 ADS_KDC|ADS_TIMESERV|ADS_CLOSEST|ADS_WRITABLE);
399                                         q += 4;
400
401                                         /* Push Domain GUID */
402                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < UUID_FLAT_SIZE) {
403                                                 return;
404                                         }
405                                         if (False == secrets_fetch_domain_guid(domain, &domain_guid)) {
406                                                 DEBUG(2, ("Could not fetch DomainGUID for %s\n", domain));
407                                                 return;
408                                         }
409
410                                         smb_uuid_pack(domain_guid, &flat_guid);
411                                         memcpy(q, &flat_guid.info, UUID_FLAT_SIZE);
412                                         q += UUID_FLAT_SIZE;
413
414                                         /* Forest */
415                                         str_offset = q - q_orig;
416                                         dc = domain;
417                                         q1 = q;
418                                         while ((component = strtok(dc, "."))) {
419                                                 dc = NULL;
420                                                 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
421                                                         return;
422                                                 }
423                                                 size = push_ascii(&q[1], component,
424                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
425                                                         0);
426                                                 SCVAL(q, 0, size);
427                                                 q += (size + 1);
428                                         }
429
430                                         /* Unk0 */
431                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
432                                                 return;
433                                         }
434                                         SCVAL(q, 0, 0);
435                                         q++;
436
437                                         /* Domain */
438                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
439                                         SCVAL(q, 1, str_offset & 0xFF);
440                                         q += 2;
441
442                                         /* Hostname */
443                                         size = push_ascii(&q[1], hostname,
444                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
445                                                         0);
446                                         SCVAL(q, 0, size);
447                                         q += (size + 1);
448
449                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 3) {
450                                                 return;
451                                         }
452
453                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
454                                         SCVAL(q, 1, str_offset & 0xFF);
455                                         q += 2;
456
457                                         /* NETBIOS of domain */
458                                         size = push_ascii(&q[1], lp_workgroup(),
459                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
460                                                         STR_UPPER);
461                                         SCVAL(q, 0, size);
462                                         q += (size + 1);
463
464                                         /* Unk1 */
465                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 2) {
466                                                 return;
467                                         }
468
469                                         SCVAL(q, 0, 0);
470                                         q++;
471
472                                         /* NETBIOS of hostname */
473                                         size = push_ascii(&q[1], my_name,
474                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
475                                                         0);
476                                         SCVAL(q, 0, size);
477                                         q += (size + 1);
478
479                                         /* Unk2 */
480                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 4) {
481                                                 return;
482                                         }
483
484                                         SCVAL(q, 0, 0);
485                                         q++;
486
487                                         /* User name */
488                                         if (SVAL(uniuser, 0) != 0) {
489                                                 size = push_ascii(&q[1], ascuser,
490                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
491                                                         0);
492                                                 SCVAL(q, 0, size);
493                                                 q += (size + 1);
494                                         }
495
496                                         q_orig = q;
497                                         /* Site name */
498                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 1) {
499                                                 return;
500                                         }
501                                         size = push_ascii(&q[1], "Default-First-Site-Name",
502                                                         sizeof(outbuf) - PTR_DIFF(q+1, outbuf),
503                                                         0);
504                                         SCVAL(q, 0, size);
505                                         q += (size + 1);
506
507                                         if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 18) {
508                                                 return;
509                                         }
510
511                                         /* Site name (2) */
512                                         str_offset = q - q_orig;
513                                         SCVAL(q, 0, 0xc0 | ((str_offset >> 8) & 0x3F));
514                                         SCVAL(q, 1, str_offset & 0xFF);
515                                         q += 2;
516
517                                         SCVAL(q, 0, PTR_DIFF(q,q1));
518                                         SCVAL(q, 1, 0x10); /* unknown */
519
520                                         SIVAL(q, 0, 0x00000002);
521                                         q += 4; /* unknown */
522                                         SIVAL(q, 0, (iface_ip(p->ip))->s_addr);
523                                         q += 4;
524                                         SIVAL(q, 0, 0x00000000);
525                                         q += 4; /* unknown */
526                                         SIVAL(q, 0, 0x00000000);
527                                         q += 4; /* unknown */
528                                 }       
529 #endif
530
531                                 if (sizeof(outbuf) - PTR_DIFF(q, outbuf) < 8) {
532                                         return;
533                                 }
534
535                                 /* tell the client what version we are */
536                                 SIVAL(q, 0, ((ntversion < 11) || (SEC_ADS != lp_security())) ? 1 : 13); 
537                                 /* our ntversion */
538                                 SSVAL(q, 4, 0xffff); /* our lmnttoken */ 
539                                 SSVAL(q, 6, 0xffff); /* our lm20token */
540                                 q += 8;
541
542                                 dump_data(4, (uint8 *)outbuf, PTR_DIFF(q, outbuf));
543
544                                 pull_ascii_fstring(getdc_str, getdc);
545                                 pull_ascii_nstring(source_name, sizeof(source_name), dgram->source_name.name);
546
547                                 send_mailslot(True, getdc,
548                                         outbuf,PTR_DIFF(q,outbuf),
549                                         global_myname(), 0x0,
550                                         source_name,
551                                         dgram->source_name.name_type,
552                                         p->ip, *iface_ip(p->ip), p->port);  
553                                 break;
554                         }
555
556                 /* Announce change to UAS or SAM.  Send by the domain controller when a
557                 replication event is required. */
558
559                 case SAM_UAS_CHANGE:
560                         DEBUG(5, ("Got SAM_UAS_CHANGE\n"));
561                         break;
562
563                 default:
564                         DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
565                         return;
566         }
567 }