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