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