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