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