r22064: Fix the DFS code to work better with Vista clients. Allow
[kai/samba.git] / source3 / rpc_server / srv_dfs_nt.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines for Dfs
4  *  Copyright (C) Shirish Kalele        2000.
5  *  Copyright (C) Jeremy Allison        2001.
6  *  Copyright (C) Jelmer Vernooij       2005-2006.
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
23 /* This is the implementation of the dfs pipe. */
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_MSDFS
29
30 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
31    dfs exists, or 0 otherwise. */
32
33 void _dfs_GetManagerVersion(pipes_struct *p, struct dfs_GetManagerVersion *r)
34 {
35         if(lp_host_msdfs()) 
36                 *r->out.exist_flag = 1;
37         else
38                 *r->out.exist_flag = 0;
39 }
40
41 WERROR _dfs_Add(pipes_struct *p, struct dfs_Add *r)
42 {
43         struct junction_map jn;
44         struct referral* old_referral_list = NULL;
45         BOOL self_ref = False;
46         int consumedcnt = 0;
47         BOOL exists = False;
48
49         pstring altpath;
50
51         if (p->pipe_user.ut.uid != 0) {
52                 DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
53                 return WERR_ACCESS_DENIED;
54         }
55
56         DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
57                 r->in.path, r->in.server, r->in.share));
58
59         pstrcpy(altpath, r->in.server);
60         pstrcat(altpath, "\\");
61         pstrcat(altpath, r->in.share);
62
63         /* The following call can change the cwd. */
64         if(NT_STATUS_IS_OK(get_referred_path(p->mem_ctx, r->in.path, &jn, &consumedcnt, &self_ref))) {
65                 exists = True;
66                 jn.referral_count += 1;
67                 old_referral_list = jn.referral_list;
68         } else {
69                 jn.referral_count = 1;
70         }
71
72         vfs_ChDir(p->conn,p->conn->connectpath);
73
74         jn.referral_list = TALLOC_ARRAY(p->mem_ctx, struct referral, jn.referral_count);
75         if(jn.referral_list == NULL) {
76                 DEBUG(0,("init_reply_dfs_add: talloc failed for referral list!\n"));
77                 return WERR_DFS_INTERNAL_ERROR;
78         }
79
80         if(old_referral_list) {
81                 memcpy(jn.referral_list, old_referral_list, sizeof(struct referral)*jn.referral_count-1);
82         }
83   
84         jn.referral_list[jn.referral_count-1].proximity = 0;
85         jn.referral_list[jn.referral_count-1].ttl = REFERRAL_TTL;
86
87         pstrcpy(jn.referral_list[jn.referral_count-1].alternate_path, altpath);
88   
89         if(!create_msdfs_link(&jn, exists)) {
90                 vfs_ChDir(p->conn,p->conn->connectpath);
91                 return WERR_DFS_CANT_CREATE_JUNCT;
92         }
93         vfs_ChDir(p->conn,p->conn->connectpath);
94
95         return WERR_OK;
96 }
97
98 WERROR _dfs_Remove(pipes_struct *p, struct dfs_Remove *r)
99 {
100         struct junction_map jn;
101         BOOL self_ref = False;
102         int consumedcnt = 0;
103         BOOL found = False;
104
105         pstring altpath;
106
107         if (p->pipe_user.ut.uid != 0) {
108                 DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
109                 return WERR_ACCESS_DENIED;
110         }
111
112         if (r->in.server && r->in.share) {
113                 pstrcpy(altpath, r->in.server);
114                 pstrcat(altpath, "\\");
115                 pstrcat(altpath, r->in.share);
116                 strlower_m(altpath);
117         }
118
119         DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
120                 r->in.path, r->in.server, r->in.share));
121
122         if(!NT_STATUS_IS_OK(get_referred_path(p->mem_ctx, r->in.path, &jn, &consumedcnt, &self_ref))) {
123                 return WERR_DFS_NO_SUCH_VOL;
124         }
125
126         /* if no server-share pair given, remove the msdfs link completely */
127         if(!r->in.server && !r->in.share) {
128                 if(!remove_msdfs_link(&jn)) {
129                         vfs_ChDir(p->conn,p->conn->connectpath);
130                         return WERR_DFS_NO_SUCH_VOL;
131                 }
132                 vfs_ChDir(p->conn,p->conn->connectpath);
133         } else {
134                 int i=0;
135                 /* compare each referral in the list with the one to remove */
136                 DEBUG(10,("altpath: .%s. refcnt: %d\n", altpath, jn.referral_count));
137                 for(i=0;i<jn.referral_count;i++) {
138                         pstring refpath;
139                         pstrcpy(refpath,jn.referral_list[i].alternate_path);
140                         trim_char(refpath, '\\', '\\');
141                         DEBUG(10,("_dfs_remove:  refpath: .%s.\n", refpath));
142                         if(strequal(refpath, altpath)) {
143                                 *(jn.referral_list[i].alternate_path)='\0';
144                                 DEBUG(10,("_dfs_remove: Removal request matches referral %s\n",
145                                         refpath));
146                                 found = True;
147                         }
148                 }
149
150                 if(!found) {
151                         return WERR_DFS_NO_SUCH_SHARE;
152                 }
153
154                 /* Only one referral, remove it */
155                 if(jn.referral_count == 1) {
156                         if(!remove_msdfs_link(&jn)) {
157                                 vfs_ChDir(p->conn,p->conn->connectpath);
158                                 return WERR_DFS_NO_SUCH_VOL;
159                         }
160                 } else {
161                         if(!create_msdfs_link(&jn, True)) { 
162                                 vfs_ChDir(p->conn,p->conn->connectpath);
163                                 return WERR_DFS_CANT_CREATE_JUNCT;
164                         }
165                 }
166                 vfs_ChDir(p->conn,p->conn->connectpath);
167         }
168
169         return WERR_OK;
170 }
171
172 static BOOL init_reply_dfs_info_1(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info1* dfs1)
173 {
174         dfs1->path = talloc_asprintf(mem_ctx, 
175                                 "\\\\%s\\%s\\%s", global_myname(), 
176                                 j->service_name, j->volume_name);
177         if (dfs1->path == NULL)
178                 return False;
179
180         DEBUG(5,("init_reply_dfs_info_1: initing entrypath: %s\n",dfs1->path));
181         return True;
182 }
183
184 static BOOL init_reply_dfs_info_2(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info2* dfs2)
185 {
186         dfs2->path = talloc_asprintf(mem_ctx, 
187                         "\\\\%s\\%s\\%s", global_myname(), j->service_name, j->volume_name);
188         if (dfs2->path == NULL)
189                 return False;
190         dfs2->comment = talloc_strdup(mem_ctx, j->comment);
191         dfs2->state = 1; /* set up state of dfs junction as OK */
192         dfs2->num_stores = j->referral_count;
193         return True;
194 }
195
196 static BOOL init_reply_dfs_info_3(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info3* dfs3)
197 {
198         int ii;
199         if (j->volume_name[0] == '\0')
200                 dfs3->path = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
201                         global_myname(), j->service_name);
202         else
203                 dfs3->path = talloc_asprintf(mem_ctx, "\\\\%s\\%s\\%s", global_myname(),
204                         j->service_name, j->volume_name);
205
206         if (dfs3->path == NULL)
207                 return False;
208
209         dfs3->comment = talloc_strdup(mem_ctx, j->comment);
210         dfs3->state = 1;
211         dfs3->num_stores = j->referral_count;
212     
213         /* also enumerate the stores */
214         dfs3->stores = TALLOC_ARRAY(mem_ctx, struct dfs_StorageInfo, j->referral_count);
215         if (!dfs3->stores)
216                 return False;
217
218         memset(dfs3->stores, '\0', j->referral_count * sizeof(struct dfs_StorageInfo));
219
220         for(ii=0;ii<j->referral_count;ii++) {
221                 char* p; 
222                 pstring path;
223                 struct dfs_StorageInfo* stor = &(dfs3->stores[ii]);
224                 struct referral* ref = &(j->referral_list[ii]);
225   
226                 pstrcpy(path, ref->alternate_path);
227                 trim_char(path,'\\','\0');
228                 p = strrchr_m(path,'\\');
229                 if(p==NULL) {
230                         DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
231                         continue;
232                 }
233                 *p = '\0';
234                 DEBUG(5,("storage %d: %s.%s\n",ii,path,p+1));
235                 stor->state = 2; /* set all stores as ONLINE */
236                 stor->server = talloc_strdup(mem_ctx, path);
237                 stor->share = talloc_strdup(mem_ctx, p+1);
238         }
239         return True;
240 }
241
242 static BOOL init_reply_dfs_info_100(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info100* dfs100)
243 {
244         dfs100->comment = talloc_strdup(mem_ctx, j->comment);
245         return True;
246 }
247
248
249 WERROR _dfs_Enum(pipes_struct *p, struct dfs_Enum *r)
250 {
251         struct junction_map jn[MAX_MSDFS_JUNCTIONS];
252         int num_jn = 0;
253         int i;
254
255         num_jn = enum_msdfs_links(p->mem_ctx, jn, ARRAY_SIZE(jn));
256         vfs_ChDir(p->conn,p->conn->connectpath);
257     
258         DEBUG(5,("_dfs_Enum: %d junctions found in Dfs, doing level %d\n", num_jn, r->in.level));
259
260         *r->out.total = num_jn;
261
262         /* Create the return array */
263         switch (r->in.level) {
264         case 1:
265                 if ((r->out.info->e.info1->s = TALLOC_ARRAY(p->mem_ctx, struct dfs_Info1, num_jn)) == NULL) {
266                         return WERR_NOMEM;
267                 }
268                 r->out.info->e.info1->count = num_jn;
269                 break;
270         case 2:
271                 if ((r->out.info->e.info2->s = TALLOC_ARRAY(p->mem_ctx, struct dfs_Info2, num_jn)) == NULL) {
272                         return WERR_NOMEM;
273                 }
274                 r->out.info->e.info2->count = num_jn;
275                 break;
276         case 3:
277                 if ((r->out.info->e.info3->s = TALLOC_ARRAY(p->mem_ctx, struct dfs_Info3, num_jn)) == NULL) {
278                         return WERR_NOMEM;
279                 }
280                 r->out.info->e.info3->count = num_jn;
281                 break;
282         default:
283                 return WERR_INVALID_PARAM;
284         }
285
286         for (i = 0; i < num_jn; i++) {
287                 switch (r->in.level) {
288                 case 1: 
289                         init_reply_dfs_info_1(p->mem_ctx, &jn[i], &r->out.info->e.info1->s[i]);
290                         break;
291                 case 2:
292                         init_reply_dfs_info_2(p->mem_ctx, &jn[i], &r->out.info->e.info2->s[i]);
293                         break;
294                 case 3:
295                         init_reply_dfs_info_3(p->mem_ctx, &jn[i], &r->out.info->e.info3->s[i]);
296                         break;
297                 default:
298                         return WERR_INVALID_PARAM;
299                 }
300         }
301   
302         return WERR_OK;
303 }
304       
305 WERROR _dfs_GetInfo(pipes_struct *p, struct dfs_GetInfo *r)
306 {
307         int consumedcnt = sizeof(pstring);
308         struct junction_map jn;
309         BOOL self_ref = False;
310         BOOL ret;
311
312         if(!create_junction(r->in.path, &jn))
313                 return WERR_DFS_NO_SUCH_SERVER;
314   
315         /* The following call can change the cwd. */
316         if(!NT_STATUS_IS_OK(get_referred_path(p->mem_ctx, r->in.path, &jn, &consumedcnt, &self_ref)) || consumedcnt < strlen(r->in.path)) {
317                 vfs_ChDir(p->conn,p->conn->connectpath);
318                 return WERR_DFS_NO_SUCH_VOL;
319         }
320
321         vfs_ChDir(p->conn,p->conn->connectpath);
322
323         switch (r->in.level) {
324                 case 1: ret = init_reply_dfs_info_1(p->mem_ctx, &jn, r->out.info->info1); break;
325                 case 2: ret = init_reply_dfs_info_2(p->mem_ctx, &jn, r->out.info->info2); break;
326                 case 3: ret = init_reply_dfs_info_3(p->mem_ctx, &jn, r->out.info->info3); break;
327                 case 100: ret = init_reply_dfs_info_100(p->mem_ctx, &jn, r->out.info->info100); break;
328                 default:
329                         r->out.info->info1 = NULL;
330                         return WERR_INVALID_PARAM;
331         }
332
333         if (!ret) 
334                 return WERR_INVALID_PARAM;
335   
336         return WERR_OK;
337 }
338
339 WERROR _dfs_SetInfo(pipes_struct *p, struct dfs_SetInfo *r)
340 {
341         /* FIXME: Implement your code here */
342         p->rng_fault_state = True;
343         return WERR_NOT_SUPPORTED;
344 }
345
346 WERROR _dfs_Rename(pipes_struct *p, struct dfs_Rename *r)
347 {
348         /* FIXME: Implement your code here */
349         p->rng_fault_state = True;
350         return WERR_NOT_SUPPORTED;
351 }
352
353 WERROR _dfs_Move(pipes_struct *p, struct dfs_Move *r)
354 {
355         /* FIXME: Implement your code here */
356         p->rng_fault_state = True;
357         return WERR_NOT_SUPPORTED;
358 }
359
360 WERROR _dfs_ManagerGetConfigInfo(pipes_struct *p, struct dfs_ManagerGetConfigInfo *r)
361 {
362         /* FIXME: Implement your code here */
363         p->rng_fault_state = True;
364         return WERR_NOT_SUPPORTED;
365 }
366
367 WERROR _dfs_ManagerSendSiteInfo(pipes_struct *p, struct dfs_ManagerSendSiteInfo *r)
368 {
369         /* FIXME: Implement your code here */
370         p->rng_fault_state = True;
371         return WERR_NOT_SUPPORTED;
372 }
373
374 WERROR _dfs_AddFtRoot(pipes_struct *p, struct dfs_AddFtRoot *r)
375 {
376         /* FIXME: Implement your code here */
377         p->rng_fault_state = True;
378         return WERR_NOT_SUPPORTED;
379 }
380
381 WERROR _dfs_RemoveFtRoot(pipes_struct *p, struct dfs_RemoveFtRoot *r)
382 {
383         /* FIXME: Implement your code here */
384         p->rng_fault_state = True;
385         return WERR_NOT_SUPPORTED;
386 }
387
388 WERROR _dfs_AddStdRoot(pipes_struct *p, struct dfs_AddStdRoot *r)
389 {
390         /* FIXME: Implement your code here */
391         p->rng_fault_state = True;
392         return WERR_NOT_SUPPORTED;
393 }
394
395 WERROR _dfs_RemoveStdRoot(pipes_struct *p, struct dfs_RemoveStdRoot *r)
396 {
397         /* FIXME: Implement your code here */
398         p->rng_fault_state = True;
399         return WERR_NOT_SUPPORTED;
400 }
401
402 WERROR _dfs_ManagerInitialize(pipes_struct *p, struct dfs_ManagerInitialize *r)
403 {
404         /* FIXME: Implement your code here */
405         p->rng_fault_state = True;
406         return WERR_NOT_SUPPORTED;
407 }
408
409 WERROR _dfs_AddStdRootForced(pipes_struct *p, struct dfs_AddStdRootForced *r)
410 {
411         /* FIXME: Implement your code here */
412         p->rng_fault_state = True;
413         return WERR_NOT_SUPPORTED;
414 }
415
416 WERROR _dfs_GetDcAddress(pipes_struct *p, struct dfs_GetDcAddress *r)
417 {
418         /* FIXME: Implement your code here */
419         p->rng_fault_state = True;
420         return WERR_NOT_SUPPORTED;
421 }
422
423 WERROR _dfs_SetDcAddress(pipes_struct *p, struct dfs_SetDcAddress *r)
424 {
425         /* FIXME: Implement your code here */
426         p->rng_fault_state = True;
427         return WERR_NOT_SUPPORTED;
428 }
429
430 WERROR _dfs_FlushFtTable(pipes_struct *p, struct dfs_FlushFtTable *r)
431 {
432         /* FIXME: Implement your code here */
433         p->rng_fault_state = True;
434         return WERR_NOT_SUPPORTED;
435 }
436
437 WERROR _dfs_Add2(pipes_struct *p, struct dfs_Add2 *r)
438 {
439         /* FIXME: Implement your code here */
440         p->rng_fault_state = True;
441         return WERR_NOT_SUPPORTED;
442 }
443
444 WERROR _dfs_Remove2(pipes_struct *p, struct dfs_Remove2 *r)
445 {
446         /* FIXME: Implement your code here */
447         p->rng_fault_state = True;
448         return WERR_NOT_SUPPORTED;
449 }
450
451 WERROR _dfs_EnumEx(pipes_struct *p, struct dfs_EnumEx *r)
452 {
453         /* FIXME: Implement your code here */
454         p->rng_fault_state = True;
455         return WERR_NOT_SUPPORTED;
456 }
457
458 WERROR _dfs_SetInfo2(pipes_struct *p, struct dfs_SetInfo2 *r)
459 {
460         /* FIXME: Implement your code here */
461         p->rng_fault_state = True;
462         return WERR_NOT_SUPPORTED;
463 }