changed the definition of dos_PutUniCode
[samba.git] / source3 / nmbd / nmbd_processlogon.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios routines and daemon - version 2
5    Copyright (C) Andrew Tridgell 1994-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7    Copyright (C) Jeremy Allison 1994-1998
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 2 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 extern int DEBUGLEVEL;
30
31 extern pstring global_myname;
32 extern fstring global_myworkgroup;
33
34 /****************************************************************************
35 Process a domain logon packet
36 **************************************************************************/
37
38 void process_logon_packet(struct packet_struct *p,char *buf,int len, 
39                           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   {
61     DEBUG(3,("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   strupper(my_name);
68
69   code = SVAL(buf,0);
70   DEBUG(1,("process_logon_packet: Logon from %s: code = %x\n", inet_ntoa(p->ip), code));
71
72   switch (code)
73   {
74     case 0:    
75     {
76       char *q = buf + 2;
77       char *machine = q;
78       char *user = skip_string(machine,1);
79
80       getdc = skip_string(user,1);
81       q = skip_string(getdc,1);
82       token = SVAL(q,3);
83
84       fstrcpy(reply_name,my_name); 
85
86       DEBUG(3,("process_logon_packet: Domain login request from %s at IP %s user=%s token=%x\n",
87              machine,inet_ntoa(p->ip),user,token));
88
89       q = outbuf;
90       SSVAL(q, 0, 6);
91       q += 2;
92
93       fstrcpy(reply_name, "\\\\");
94       fstrcat(reply_name, my_name);
95       fstrcpy(q, reply_name); q = skip_string(q, 1); /* PDC name */
96
97       SSVAL(q, 0, token);
98       q += 2;
99
100       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
101
102       send_mailslot(True, getdc, 
103                     outbuf,PTR_DIFF(q,outbuf),
104                     dgram->dest_name.name,
105                     dgram->dest_name.name_type,
106                     dgram->source_name.name,
107                     dgram->source_name.name_type,
108                     p->ip, *iface_ip(p->ip), p->port);  
109       break;
110     }
111
112     case QUERYFORPDC:
113     {
114       char *q = buf + 2;
115       char *machine = q;
116
117       getdc = skip_string(machine,1);
118       unicomp = skip_string(getdc,1);
119
120       /* at this point we can work out if this is a W9X or NT style
121          request. Experiments show that the difference is wether the
122          packet ends here. For a W9X request we now end with a pair of
123          bytes (usually 0xFE 0xFF) whereas with NT we have two further
124          strings - the following is a simple way of detecting this */
125       if (len - PTR_DIFF(unicomp, buf) > 3) {
126               short_request = True;
127       } else {
128               /* A full length (NT style) request */
129               q = skip_unicode_string(unicomp, 1);
130
131               if (len - PTR_DIFF(q, buf) > 8) {
132                                         /* with NT5 clients we can sometimes
133                                            get additional data - a length specificed string
134                                            containing the domain name, then 16 bytes of
135                                            data (no idea what it is) */
136                                         int dom_len = CVAL(q, 0);
137                                         q++;
138                                         if (dom_len != 0) {
139                                                 q += dom_len + 1;
140                                         }
141                                         q += 16;
142               }
143               ntversion = IVAL(q, 0);
144               lmnttoken = SVAL(q, 4);
145               lm20token = SVAL(q, 6);
146       }
147
148       /* Construct reply. */
149       q = outbuf;
150       SSVAL(q, 0, QUERYFORPDC_R);
151       q += 2;
152
153       fstrcpy(reply_name,my_name);
154       fstrcpy(q, reply_name);
155       q = skip_string(q, 1); /* PDC name */
156
157       /* PDC and domain name */
158       if (!short_request)  /* Make a full reply */
159       {
160         q = align2(q, buf);
161
162         q += dos_PutUniCode(q, my_name, sizeof(pstring), True); /* PDC name */
163         q += dos_PutUniCode(q, global_myworkgroup,sizeof(pstring), True); /* Domain name*/
164
165         SIVAL(q, 0, ntversion);
166         SSVAL(q, 4, lmnttoken);
167         SSVAL(q, 6, lm20token);
168         q += 8;
169       }
170
171       /* RJS, 21-Feb-2000, we send a short reply if the request was short */
172
173       DEBUG(3,("process_logon_packet: GETDC request from %s at IP %s, \
174 reporting %s domain %s 0x%x ntversion=%x lm_nt token=%x lm_20 token=%x\n",
175             machine,inet_ntoa(p->ip), reply_name, lp_workgroup(),
176             QUERYFORPDC_R, (uint32)ntversion, (uint32)lmnttoken,
177             (uint32)lm20token ));
178
179       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
180
181       send_mailslot(True, getdc,
182                   outbuf,PTR_DIFF(q,outbuf),
183                   dgram->dest_name.name,
184                   dgram->dest_name.name_type,
185                   dgram->source_name.name,
186                   dgram->source_name.name_type,
187                   p->ip, *iface_ip(p->ip), p->port);  
188       return;
189     }
190
191     case SAMLOGON:
192     {
193       char *q = buf + 2;
194
195       q += 2;
196       unicomp = q;
197       uniuser = skip_unicode_string(unicomp,1);
198       getdc = skip_unicode_string(uniuser,1);
199       q = skip_string(getdc,1);
200       q += 4;
201       domainsidsize = IVAL(q, 0);
202       q += 4;
203       q += domainsidsize + 3;
204       ntversion = IVAL(q, 0);
205       q += 4;
206       lmnttoken = SVAL(q, 0);
207       q += 2;
208       lm20token = SVAL(q, 0);
209       q += 2;
210
211       DEBUG(3,("process_logon_packet: SAMLOGON sidsize %d ntv %d\n", domainsidsize, ntversion));
212
213       /*
214        * we respond regadless of whether the machine is in our password 
215        * database. If it isn't then we let smbd send an appropriate error.
216        * Let's ignore the SID.
217        */
218
219       pstrcpy(ascuser, dos_unistr(uniuser));
220       DEBUG(3,("process_logon_packet: SAMLOGON user %s\n", ascuser));
221
222       fstrcpy(reply_name,"\\\\"); /* Here it wants \\LOGONSERVER. */
223       fstrcpy(reply_name+2,my_name); 
224
225       DEBUG(3,("process_logon_packet: SAMLOGON request from %s(%s) for %s, returning logon svr %s domain %s code %x token=%x\n",
226                dos_unistr(unicomp),inet_ntoa(p->ip), ascuser, reply_name, global_myworkgroup,
227                SAMLOGON_R ,lmnttoken));
228
229       /* Construct reply. */
230
231       q = outbuf;
232       if (SVAL(uniuser, 0) == 0) {
233               SSVAL(q, 0, SAMLOGON_UNK_R);      /* user unknown */
234       } else {
235               SSVAL(q, 0, SAMLOGON_R);
236       }
237       q += 2;
238
239       q += dos_PutUniCode(q, reply_name,sizeof(pstring), True);
240       unistrcpy(q, uniuser);
241       q = skip_unicode_string(q, 1); /* User name (workstation trust account) */
242       q += dos_PutUniCode(q, lp_workgroup(),sizeof(pstring), True);
243
244       SIVAL(q, 0, ntversion);
245       q += 4;
246       SSVAL(q, 0, lmnttoken);
247       q += 2;
248       SSVAL(q, 0, lm20token);
249       q += 2;
250
251       dump_data(4, outbuf, PTR_DIFF(q, outbuf));
252
253       send_mailslot(True, getdc,
254                    outbuf,PTR_DIFF(q,outbuf),
255                    dgram->dest_name.name,
256                    dgram->dest_name.name_type,
257                    dgram->source_name.name,
258                    dgram->source_name.name_type,
259                    p->ip, *iface_ip(p->ip), p->port);  
260       break;
261     }
262
263     default:
264     {
265       DEBUG(3,("process_logon_packet: Unknown domain request %d\n",code));
266       return;
267     }
268   }
269 }