6ed5dbd3b5a96daaa5a10330f5325cdbfb6cba8e
[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: %u junctions found in Dfs, doing level %d\n",
295                                 (unsigned int)num_jn, r->in.level));
296
297         *r->out.total = num_jn;
298
299         /* Create the return array */
300         switch (r->in.level) {
301         case 1:
302                 if (num_jn) {
303                         if ((r->out.info->e.info1->s = TALLOC_ARRAY(ctx, struct dfs_Info1, num_jn)) == NULL) {
304                                 return WERR_NOMEM;
305                         }
306                 } else {
307                         r->out.info->e.info1->s = NULL;
308                 }
309                 r->out.info->e.info1->count = num_jn;
310                 break;
311         case 2:
312                 if (num_jn) {
313                         if ((r->out.info->e.info2->s = TALLOC_ARRAY(ctx, struct dfs_Info2, num_jn)) == NULL) {
314                                 return WERR_NOMEM;
315                         }
316                 } else {
317                         r->out.info->e.info2->s = NULL;
318                 }
319                 r->out.info->e.info2->count = num_jn;
320                 break;
321         case 3:
322                 if (num_jn) {
323                         if ((r->out.info->e.info3->s = TALLOC_ARRAY(ctx, struct dfs_Info3, num_jn)) == NULL) {
324                                 return WERR_NOMEM;
325                         }
326                 } else {
327                         r->out.info->e.info3->s = NULL;
328                 }
329                 r->out.info->e.info3->count = num_jn;
330                 break;
331         default:
332                 return WERR_INVALID_PARAM;
333         }
334
335         for (i = 0; i < num_jn; i++) {
336                 switch (r->in.level) {
337                 case 1:
338                         init_reply_dfs_info_1(ctx, &jn[i], &r->out.info->e.info1->s[i]);
339                         break;
340                 case 2:
341                         init_reply_dfs_info_2(ctx, &jn[i], &r->out.info->e.info2->s[i]);
342                         break;
343                 case 3:
344                         init_reply_dfs_info_3(ctx, &jn[i], &r->out.info->e.info3->s[i]);
345                         break;
346                 default:
347                         return WERR_INVALID_PARAM;
348                 }
349         }
350
351         return WERR_OK;
352 }
353
354 WERROR _dfs_GetInfo(pipes_struct *p, struct dfs_GetInfo *r)
355 {
356         int consumedcnt = strlen(r->in.dfs_entry_path);
357         struct junction_map *jn = NULL;
358         BOOL self_ref = False;
359         TALLOC_CTX *ctx = talloc_tos();
360         BOOL ret;
361
362         jn = TALLOC_ZERO_P(ctx, struct junction_map);
363         if (!jn) {
364                 return WERR_NOMEM;
365         }
366
367         if(!create_junction(ctx, r->in.dfs_entry_path, jn)) {
368                 return WERR_DFS_NO_SUCH_SERVER;
369         }
370
371         /* The following call can change the cwd. */
372         if(!NT_STATUS_IS_OK(get_referred_path(ctx, r->in.dfs_entry_path,
373                                         jn, &consumedcnt, &self_ref)) ||
374                         consumedcnt < strlen(r->in.dfs_entry_path)) {
375                 vfs_ChDir(p->conn,p->conn->connectpath);
376                 return WERR_DFS_NO_SUCH_VOL;
377         }
378
379         vfs_ChDir(p->conn,p->conn->connectpath);
380
381         switch (r->in.level) {
382                 case 1:
383                         r->out.info->info1 = TALLOC_ZERO_P(ctx,struct dfs_Info1);
384                         if (!r->out.info->info1) {
385                                 return WERR_NOMEM;
386                         }
387                         ret = init_reply_dfs_info_1(ctx, jn, r->out.info->info1);
388                         break;
389                 case 2:
390                         r->out.info->info2 = TALLOC_ZERO_P(ctx,struct dfs_Info2);
391                         if (!r->out.info->info2) {
392                                 return WERR_NOMEM;
393                         }
394                         ret = init_reply_dfs_info_2(ctx, jn, r->out.info->info2);
395                         break;
396                 case 3:
397                         r->out.info->info3 = TALLOC_ZERO_P(ctx,struct dfs_Info3);
398                         if (!r->out.info->info3) {
399                                 return WERR_NOMEM;
400                         }
401                         ret = init_reply_dfs_info_3(ctx, jn, r->out.info->info3);
402                         break;
403                 case 100:
404                         r->out.info->info100 = TALLOC_ZERO_P(ctx,struct dfs_Info100);
405                         if (!r->out.info->info100) {
406                                 return WERR_NOMEM;
407                         }
408                         ret = init_reply_dfs_info_100(ctx, jn, r->out.info->info100);
409                         break;
410                 default:
411                         r->out.info->info1 = NULL;
412                         return WERR_INVALID_PARAM;
413         }
414
415         if (!ret)
416                 return WERR_INVALID_PARAM;
417
418         return WERR_OK;
419 }
420
421 WERROR _dfs_SetInfo(pipes_struct *p, struct dfs_SetInfo *r)
422 {
423         /* FIXME: Implement your code here */
424         p->rng_fault_state = True;
425         return WERR_NOT_SUPPORTED;
426 }
427
428 WERROR _dfs_Rename(pipes_struct *p, struct dfs_Rename *r)
429 {
430         /* FIXME: Implement your code here */
431         p->rng_fault_state = True;
432         return WERR_NOT_SUPPORTED;
433 }
434
435 WERROR _dfs_Move(pipes_struct *p, struct dfs_Move *r)
436 {
437         /* FIXME: Implement your code here */
438         p->rng_fault_state = True;
439         return WERR_NOT_SUPPORTED;
440 }
441
442 WERROR _dfs_ManagerGetConfigInfo(pipes_struct *p, struct dfs_ManagerGetConfigInfo *r)
443 {
444         /* FIXME: Implement your code here */
445         p->rng_fault_state = True;
446         return WERR_NOT_SUPPORTED;
447 }
448
449 WERROR _dfs_ManagerSendSiteInfo(pipes_struct *p, struct dfs_ManagerSendSiteInfo *r)
450 {
451         /* FIXME: Implement your code here */
452         p->rng_fault_state = True;
453         return WERR_NOT_SUPPORTED;
454 }
455
456 WERROR _dfs_AddFtRoot(pipes_struct *p, struct dfs_AddFtRoot *r)
457 {
458         /* FIXME: Implement your code here */
459         p->rng_fault_state = True;
460         return WERR_NOT_SUPPORTED;
461 }
462
463 WERROR _dfs_RemoveFtRoot(pipes_struct *p, struct dfs_RemoveFtRoot *r)
464 {
465         /* FIXME: Implement your code here */
466         p->rng_fault_state = True;
467         return WERR_NOT_SUPPORTED;
468 }
469
470 WERROR _dfs_AddStdRoot(pipes_struct *p, struct dfs_AddStdRoot *r)
471 {
472         /* FIXME: Implement your code here */
473         p->rng_fault_state = True;
474         return WERR_NOT_SUPPORTED;
475 }
476
477 WERROR _dfs_RemoveStdRoot(pipes_struct *p, struct dfs_RemoveStdRoot *r)
478 {
479         /* FIXME: Implement your code here */
480         p->rng_fault_state = True;
481         return WERR_NOT_SUPPORTED;
482 }
483
484 WERROR _dfs_ManagerInitialize(pipes_struct *p, struct dfs_ManagerInitialize *r)
485 {
486         /* FIXME: Implement your code here */
487         p->rng_fault_state = True;
488         return WERR_NOT_SUPPORTED;
489 }
490
491 WERROR _dfs_AddStdRootForced(pipes_struct *p, struct dfs_AddStdRootForced *r)
492 {
493         /* FIXME: Implement your code here */
494         p->rng_fault_state = True;
495         return WERR_NOT_SUPPORTED;
496 }
497
498 WERROR _dfs_GetDcAddress(pipes_struct *p, struct dfs_GetDcAddress *r)
499 {
500         /* FIXME: Implement your code here */
501         p->rng_fault_state = True;
502         return WERR_NOT_SUPPORTED;
503 }
504
505 WERROR _dfs_SetDcAddress(pipes_struct *p, struct dfs_SetDcAddress *r)
506 {
507         /* FIXME: Implement your code here */
508         p->rng_fault_state = True;
509         return WERR_NOT_SUPPORTED;
510 }
511
512 WERROR _dfs_FlushFtTable(pipes_struct *p, struct dfs_FlushFtTable *r)
513 {
514         /* FIXME: Implement your code here */
515         p->rng_fault_state = True;
516         return WERR_NOT_SUPPORTED;
517 }
518
519 WERROR _dfs_Add2(pipes_struct *p, struct dfs_Add2 *r)
520 {
521         /* FIXME: Implement your code here */
522         p->rng_fault_state = True;
523         return WERR_NOT_SUPPORTED;
524 }
525
526 WERROR _dfs_Remove2(pipes_struct *p, struct dfs_Remove2 *r)
527 {
528         /* FIXME: Implement your code here */
529         p->rng_fault_state = True;
530         return WERR_NOT_SUPPORTED;
531 }
532
533 WERROR _dfs_EnumEx(pipes_struct *p, struct dfs_EnumEx *r)
534 {
535         /* FIXME: Implement your code here */
536         p->rng_fault_state = True;
537         return WERR_NOT_SUPPORTED;
538 }
539
540 WERROR _dfs_SetInfo2(pipes_struct *p, struct dfs_SetInfo2 *r)
541 {
542         /* FIXME: Implement your code here */
543         p->rng_fault_state = True;
544         return WERR_NOT_SUPPORTED;
545 }