Removed version number from file header.
[jra/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-1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22    Revision History:
23
24 */
25
26 #include "includes.h"
27
28 extern pstring global_myname;
29 extern fstring global_myworkgroup;
30
31 struct sam_database_info {
32         uint32 index;
33         uint32 serial_lo, serial_hi;
34         uint32 date_lo, date_hi;
35 };
36
37 /****************************************************************************
38 Send a message to smbd to do a sam delta sync
39 **************************************************************************/
40 static void send_repl_message(uint32 low_serial)
41 {
42         TDB_CONTEXT *tdb;
43
44         tdb = tdb_open_log(lock_path("connections.tdb"), 0,
45                            TDB_DEFAULT, O_RDONLY, 0);
46
47         if (!tdb) {
48                 DEBUG(3, ("send_repl_message(): failed to open connections "
49                           "database\n"));
50                 return;
51         }
52
53         DEBUG(3, ("sending replication message, serial = 0x%04x\n", 
54                   low_serial));
55         
56         message_send_all(tdb, MSG_SMB_SAM_REPL, &low_serial,
57                          sizeof(low_serial), False, NULL);
58
59         tdb_close(tdb);
60 }
61
62 /****************************************************************************
63 Process a domain logon packet
64 **************************************************************************/
65
66 void process_logon_packet(struct packet_struct *p,char *buf,int len, 
67                           char *mailslot)
68 {
69   struct dgram_packet *dgram = &p->packet.dgram;
70   pstring my_name;
71   fstring reply_name;
72   pstring outbuf;
73   int code;
74   uint16 token = 0;
75   uint32 ntversion = 0;
76   uint16 lmnttoken = 0;
77   uint16 lm20token = 0;
78   uint32 domainsidsize;
79   BOOL short_request = False;
80   char *getdc;
81   char *uniuser; /* Unicode user name. */
82   pstring ascuser;
83   char *unicomp; /* Unicode computer name. */
84
85   memset(outbuf, 0, sizeof(outbuf));
86
87   if (!lp_domain_logons())
88   {
89     DEBUG(3,("process_logon_packet: Logon packet received from IP %s and domain \
90 logons are not enabled.\n", inet_ntoa(p->ip) ));
91     return;
92   }
93
94   pstrcpy(my_name, global_myname);
95   strupper(my_name);
96
97   code = SVAL(buf,0);
98   DEBUG(1,("process_logon_packet: Logon from %s: code = 0x%x\n", inet_ntoa(p->ip), code));
99
100   switch (code)
101   {
102     case 0:    
103     {
104       char *q = buf + 2;
105       char *machine = q;
106       char *user = skip_string(machine,1);
107
108       getdc = skip_string(user,1);
109       q = skip_string(getdc,1);
110       token = SVAL(q,3);
111
112       fstrcpy(reply_name,my_name); 
113
114       DEBUG(3,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
115              machine,inet_ntoa(p->ip),user,token));
116
117       q = outbuf;
118       SSVAL(q, 0, 6);
119       q += 2;
120
121       fstrcpy(reply_name, "\\\\");
122       fstrcat(reply_name, my_name);
123       fstrcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
124
125       SSVAL(q, 0, token);
126       q += 2;
127
128       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
129
130       send_mailslot(True, getdc, 
131                     outbuf,PTR_DIFF(q,outbuf),
132                     global_myname, 0x0,
133                                         machine,
134                     dgram->source_name.name_type,
135                     p->ip, *iface_ip(p->ip), p->port);  
136       break;
137     }
138
139     case QUERYFORPDC:
140     {
141       char *q = buf + 2;
142       char *machine = q;
143
144       if (!lp_domain_master())
145       {  
146          /* We're not Primary Domain Controller -- ignore this */
147          return;
148       }
149
150       getdc = skip_string(machine,1);
151       q = skip_string(getdc,1);
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       if (len - PTR_DIFF(q, buf) <= 3) {
160               short_request = True;
161       } else {
162               unicomp = q;
163
164               /* A full length (NT style) request */
165               q = skip_unibuf(unicomp, PTR_DIFF(buf + len, unicomp));
166
167               if (len - PTR_DIFF(q, buf) > 8) {
168                                         /* with NT5 clients we can sometimes
169                                            get additional data - a length specificed string
170                                            containing the domain name, then 16 bytes of
171                                            data (no idea what it is) */
172                                         int dom_len = CVAL(q, 0);
173                                         q++;
174                                         if (dom_len != 0) {
175                                                 q += dom_len + 1;
176                                         }
177                                         q += 16;
178               }
179               ntversion = IVAL(q, 0);
180               lmnttoken = SVAL(q, 4);
181               lm20token = SVAL(q, 6);
182       }
183
184       /* Construct reply. */
185       q = outbuf;
186       SSVAL(q, 0, QUERYFORPDC_R);
187       q += 2;
188
189       fstrcpy(reply_name,my_name);
190       fstrcpy(q, reply_name);
191       q = skip_string(q, 1); /* PDC name */
192
193       /* PDC and domain name */
194       if (!short_request)  /* Make a full reply */
195       {
196         q = ALIGN2(q, outbuf);
197
198         q += dos_PutUniCode(q, my_name, sizeof(pstring), True); /* PDC name */
199         q += dos_PutUniCode(q, global_myworkgroup,sizeof(pstring), True); /* Domain name*/
200         SIVAL(q, 0, 1); /* our nt version */
201         SSVAL(q, 4, 0xffff); /* our lmnttoken */
202         SSVAL(q, 6, 0xffff); /* our lm20token */
203         q += 8;
204       }
205
206       /* RJS, 21-Feb-2000, we send a short reply if the request was short */
207
208       DEBUG(3,("process_logon_packet: GETDC request from %s at IP %s, \
209 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
210             machine,inet_ntoa(p->ip), reply_name, global_myworkgroup,
211             QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
212             (uint32)lm20token ));
213
214       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
215
216       send_mailslot(True, getdc,
217                   outbuf,PTR_DIFF(q,outbuf),
218                     global_myname, 0x0,
219                   dgram->source_name.name,
220                   dgram->source_name.name_type,
221                   p->ip, *iface_ip(p->ip), p->port);  
222       return;
223     }
224
225     case SAMLOGON:
226     {
227       char *q = buf + 2;
228       fstring asccomp;
229
230       q += 2;
231       unicomp = q;
232       uniuser = skip_unibuf(unicomp, PTR_DIFF(buf+len, unicomp));
233       getdc = skip_unibuf(uniuser,PTR_DIFF(buf+len, uniuser));
234       q = skip_string(getdc,1);
235       q += 4; /* Account Control Bits - indicating username type */
236       domainsidsize = IVAL(q, 0);
237       q += 4;
238
239       DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d, len = %d\n", domainsidsize, len));
240
241       if (domainsidsize < (len - PTR_DIFF(q, buf)) && (domainsidsize != 0)) {
242               q += domainsidsize;
243               q = ALIGN4(q, buf);
244       }
245
246       DEBUG(3,("process_logon_packet: len = %d PTR_DIFF(q, buf) = %d\n", len, PTR_DIFF(q, buf) ));
247
248       if (len - PTR_DIFF(q, buf) > 8) {
249               /* with NT5 clients we can sometimes
250                  get additional data - a length specificed string
251                  containing the domain name, then 16 bytes of
252                  data (no idea what it is) */
253               int dom_len = CVAL(q, 0);
254               q++;
255               if (dom_len < (len - PTR_DIFF(q, buf)) && (dom_len != 0)) {
256                       q += dom_len + 1;
257               }
258               q += 16;
259       }
260
261       ntversion = IVAL(q, 0);
262       lmnttoken = SVAL(q, 4);
263       lm20token = SVAL(q, 6);
264       q += 8;
265
266       DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
267
268       /*
269        * we respond regadless of whether the machine is in our password 
270        * database. If it isn't then we let smbd send an appropriate error.
271        * Let's ignore the SID.
272        */
273       pull_ucs2_pstring(ascuser, uniuser);
274       pull_ucs2_fstring(asccomp, unicomp);
275       DEBUG(3,("process_logon_packet: SAMLOGON user %s\n", ascuser));
276
277       fstrcpy(reply_name,"\\\\"); /* Here it wants \\LOGONSERVER. */
278       fstrcpy(reply_name+2,my_name); 
279
280       DEBUG(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
281                asccomp,inet_ntoa(p->ip), ascuser, reply_name, global_myworkgroup,
282                SAMLOGON_R ,lmnttoken));
283
284       /* Construct reply. */
285
286       q = outbuf;
287       if (SVAL(uniuser, 0) == 0) {
288               SSVAL(q, 0, SAMLOGON_UNK_R);      /* user unknown */
289       } else {
290               SSVAL(q, 0, SAMLOGON_R);
291       }
292       q += 2;
293
294       q += dos_PutUniCode(q, reply_name,sizeof(pstring), True);
295       q += dos_PutUniCode(q, ascuser, sizeof(pstring), True);
296       q += dos_PutUniCode(q, global_myworkgroup,sizeof(pstring), True);
297
298       /* tell the client what version we are */
299       SIVAL(q, 0, 1); /* our ntversion */
300       SSVAL(q, 4, 0xffff); /* our lmnttoken */ 
301       SSVAL(q, 6, 0xffff); /* our lm20token */
302       q += 8;
303
304       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
305
306       send_mailslot(True, getdc,
307                    outbuf,PTR_DIFF(q,outbuf),
308                     global_myname, 0x0,
309                    dgram->source_name.name,
310                    dgram->source_name.name_type,
311                    p->ip, *iface_ip(p->ip), p->port);  
312       break;
313     }
314
315     /* Announce change to UAS or SAM.  Send by the domain controller when a
316        replication event is required. */
317
318   case SAM_UAS_CHANGE: {
319           struct sam_database_info *db_info;
320           char *q = buf + 2;
321           int i, db_count;
322           uint32 low_serial;
323           
324           /* Header */
325           
326           low_serial = IVAL(q, 0); q += 4;     /* Low serial number */
327
328           q += 4;                   /* Date/time */
329           q += 4;                   /* Pulse */
330           q += 4;                   /* Random */
331           
332           /* Domain info */
333           
334           q = skip_string(q, 1);    /* PDC name */
335           q = skip_string(q, 1);    /* Domain name */
336           q = skip_unibuf(q, PTR_DIFF(buf + len, q)); /* Unicode PDC name */
337           q = skip_unibuf(q, PTR_DIFF(buf + len, q)); /* Unicode domain name */
338           
339           /* Database info */
340           
341           db_count = SVAL(q, 0); q += 2;
342           
343           db_info = (struct sam_database_info *)
344                   malloc(sizeof(struct sam_database_info) * db_count);
345
346           if (db_info == NULL) {
347                   DEBUG(3, ("out of memory allocating info for %d databases\n",
348                             db_count));
349                   return;
350           }
351           
352           for (i = 0; i < db_count; i++) {
353                   db_info[i].index = IVAL(q, 0);
354                   db_info[i].serial_lo = IVAL(q, 4);
355                   db_info[i].serial_hi = IVAL(q, 8);
356                   db_info[i].date_lo = IVAL(q, 12);
357                   db_info[i].date_hi = IVAL(q, 16);
358                   q += 20;
359           }
360
361           /* Domain SID */
362
363           q += IVAL(q, 0) + 4;  /* 4 byte length plus data */
364           
365           q += 2;               /* Alignment? */
366
367           /* Misc other info */
368
369           q += 4;               /* NT version (0x1) */
370           q += 2;               /* LMNT token (0xff) */
371           q += 2;               /* LM20 token (0xff) */
372
373           SAFE_FREE(db_info);        /* Not sure whether we need to do anything
374                                    useful with these */
375
376           /* Send message to smbd */
377
378           send_repl_message(low_serial);
379
380           break;
381   }
382
383     default:
384     {
385       DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
386       return;
387     }
388   }
389 }