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