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