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