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.
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.
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.
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/>.
22 /* This is the implementation of the dfs pipe. */
26 #include "../librpc/gen_ndr/srv_dfs.h"
28 #include "smbd/smbd.h"
29 #include "smbd/globals.h"
33 #define DBGC_CLASS DBGC_MSDFS
35 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
36 dfs exists, or 0 otherwise. */
38 void _dfs_GetManagerVersion(struct pipes_struct
*p
, struct dfs_GetManagerVersion
*r
)
40 if (lp_host_msdfs()) {
41 *r
->out
.version
= DFS_MANAGER_VERSION_NT4
;
43 *r
->out
.version
= (enum dfs_ManagerVersion
)0;
47 WERROR
_dfs_Add(struct pipes_struct
*p
, struct dfs_Add
*r
)
49 struct junction_map
*jn
= NULL
;
50 struct referral
*old_referral_list
= NULL
;
51 bool self_ref
= False
;
55 TALLOC_CTX
*ctx
= talloc_tos();
57 if (p
->session_info
->unix_token
->uid
!= sec_initial_uid()) {
58 DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
59 return WERR_ACCESS_DENIED
;
62 jn
= talloc_zero(ctx
, struct junction_map
);
67 DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
68 r
->in
.path
, r
->in
.server
, r
->in
.share
));
70 altpath
= talloc_asprintf(ctx
, "%s\\%s",
77 /* The following call can change the cwd. */
78 status
= get_referred_path(ctx
, r
->in
.path
, smbd_server_conn
, jn
,
79 &consumedcnt
, &self_ref
);
80 if(!NT_STATUS_IS_OK(status
)) {
81 return ntstatus_to_werror(status
);
84 jn
->referral_count
+= 1;
85 old_referral_list
= jn
->referral_list
;
87 if (jn
->referral_count
< 1) {
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
;
97 if(old_referral_list
&& jn
->referral_list
) {
98 memcpy(jn
->referral_list
, old_referral_list
,
99 sizeof(struct referral
)*jn
->referral_count
-1);
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
;
106 if(!create_msdfs_link(jn
)) {
107 return WERR_DFS_CANT_CREATE_JUNCT
;
113 WERROR
_dfs_Remove(struct pipes_struct
*p
, struct dfs_Remove
*r
)
115 struct junction_map
*jn
= NULL
;
116 bool self_ref
= False
;
119 TALLOC_CTX
*ctx
= talloc_tos();
120 char *altpath
= NULL
;
123 if (p
->session_info
->unix_token
->uid
!= sec_initial_uid()) {
124 DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
125 return WERR_ACCESS_DENIED
;
128 jn
= talloc_zero(ctx
, struct junction_map
);
133 if (r
->in
.servername
&& r
->in
.sharename
) {
134 altpath
= talloc_asprintf(ctx
, "%s\\%s",
141 DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
142 r
->in
.dfs_entry_path
, r
->in
.servername
, r
->in
.sharename
));
145 status
= get_referred_path(ctx
, r
->in
.dfs_entry_path
, smbd_server_conn
,
146 jn
, &consumedcnt
, &self_ref
);
147 if(!NT_STATUS_IS_OK(status
)) {
148 return WERR_DFS_NO_SUCH_VOL
;
151 /* if no server-share pair given, remove the msdfs link completely */
152 if(!r
->in
.servername
&& !r
->in
.sharename
) {
153 if(!remove_msdfs_link(jn
)) {
154 return WERR_DFS_NO_SUCH_VOL
;
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
);
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",
177 return WERR_DFS_NO_SUCH_SHARE
;
180 /* Only one referral, remove it */
181 if(jn
->referral_count
== 1) {
182 if(!remove_msdfs_link(jn
)) {
183 return WERR_DFS_NO_SUCH_VOL
;
186 if(!create_msdfs_link(jn
)) {
187 return WERR_DFS_CANT_CREATE_JUNCT
;
195 static bool init_reply_dfs_info_1(TALLOC_CTX
*mem_ctx
, struct junction_map
* j
,struct dfs_Info1
* dfs1
)
197 dfs1
->path
= talloc_asprintf(mem_ctx
,
198 "\\\\%s\\%s\\%s", lp_netbios_name(),
199 j
->service_name
, j
->volume_name
);
200 if (dfs1
->path
== NULL
)
203 DEBUG(5,("init_reply_dfs_info_1: initing entrypath: %s\n",dfs1
->path
));
207 static bool init_reply_dfs_info_2(TALLOC_CTX
*mem_ctx
, struct junction_map
* j
, struct dfs_Info2
* dfs2
)
209 dfs2
->path
= talloc_asprintf(mem_ctx
,
210 "\\\\%s\\%s\\%s", lp_netbios_name(), j
->service_name
, j
->volume_name
);
211 if (dfs2
->path
== NULL
)
213 dfs2
->comment
= talloc_strdup(mem_ctx
, j
->comment
);
214 dfs2
->state
= 1; /* set up state of dfs junction as OK */
215 dfs2
->num_stores
= j
->referral_count
;
219 static bool init_reply_dfs_info_3(TALLOC_CTX
*mem_ctx
, struct junction_map
* j
, struct dfs_Info3
* dfs3
)
222 if (j
->volume_name
[0] == '\0')
223 dfs3
->path
= talloc_asprintf(mem_ctx
, "\\\\%s\\%s",
224 lp_netbios_name(), j
->service_name
);
226 dfs3
->path
= talloc_asprintf(mem_ctx
, "\\\\%s\\%s\\%s", lp_netbios_name(),
227 j
->service_name
, j
->volume_name
);
229 if (dfs3
->path
== NULL
)
232 dfs3
->comment
= talloc_strdup(mem_ctx
, j
->comment
);
234 dfs3
->num_stores
= j
->referral_count
;
236 /* also enumerate the stores */
237 if (j
->referral_count
) {
238 dfs3
->stores
= talloc_array(mem_ctx
, struct dfs_StorageInfo
, j
->referral_count
);
241 memset(dfs3
->stores
, '\0', j
->referral_count
* sizeof(struct dfs_StorageInfo
));
246 for(ii
=0;ii
<j
->referral_count
;ii
++) {
249 struct dfs_StorageInfo
* stor
= &(dfs3
->stores
[ii
]);
250 struct referral
* ref
= &(j
->referral_list
[ii
]);
252 path
= talloc_strdup(mem_ctx
, ref
->alternate_path
);
256 trim_char(path
,'\\','\0');
257 p
= strrchr_m(path
,'\\');
259 DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path
));
263 DEBUG(5,("storage %d: %s.%s\n",ii
,path
,p
+1));
264 stor
->state
= 2; /* set all stores as ONLINE */
265 stor
->server
= talloc_strdup(mem_ctx
, path
);
266 stor
->share
= talloc_strdup(mem_ctx
, p
+1);
271 static bool init_reply_dfs_info_100(TALLOC_CTX
*mem_ctx
, struct junction_map
* j
, struct dfs_Info100
* dfs100
)
273 dfs100
->comment
= talloc_strdup(mem_ctx
, j
->comment
);
277 WERROR
_dfs_Enum(struct pipes_struct
*p
, struct dfs_Enum
*r
)
279 struct junction_map
*jn
= NULL
;
282 TALLOC_CTX
*ctx
= talloc_tos();
284 jn
= enum_msdfs_links(ctx
, &num_jn
);
285 if (!jn
|| num_jn
== 0) {
290 DEBUG(5,("_dfs_Enum: %u junctions found in Dfs, doing level %d\n",
291 (unsigned int)num_jn
, r
->in
.level
));
293 *r
->out
.total
= num_jn
;
295 /* Create the return array */
296 switch (r
->in
.level
) {
299 if ((r
->out
.info
->e
.info1
->s
= talloc_array(ctx
, struct dfs_Info1
, num_jn
)) == NULL
) {
303 r
->out
.info
->e
.info1
->s
= NULL
;
305 r
->out
.info
->e
.info1
->count
= num_jn
;
309 if ((r
->out
.info
->e
.info2
->s
= talloc_array(ctx
, struct dfs_Info2
, num_jn
)) == NULL
) {
313 r
->out
.info
->e
.info2
->s
= NULL
;
315 r
->out
.info
->e
.info2
->count
= num_jn
;
319 if ((r
->out
.info
->e
.info3
->s
= talloc_array(ctx
, struct dfs_Info3
, num_jn
)) == NULL
) {
323 r
->out
.info
->e
.info3
->s
= NULL
;
325 r
->out
.info
->e
.info3
->count
= num_jn
;
328 return WERR_INVALID_PARAM
;
331 for (i
= 0; i
< num_jn
; i
++) {
332 switch (r
->in
.level
) {
334 init_reply_dfs_info_1(ctx
, &jn
[i
], &r
->out
.info
->e
.info1
->s
[i
]);
337 init_reply_dfs_info_2(ctx
, &jn
[i
], &r
->out
.info
->e
.info2
->s
[i
]);
340 init_reply_dfs_info_3(ctx
, &jn
[i
], &r
->out
.info
->e
.info3
->s
[i
]);
343 return WERR_INVALID_PARAM
;
350 WERROR
_dfs_GetInfo(struct pipes_struct
*p
, struct dfs_GetInfo
*r
)
352 int consumedcnt
= strlen(r
->in
.dfs_entry_path
);
353 struct junction_map
*jn
= NULL
;
354 bool self_ref
= False
;
355 TALLOC_CTX
*ctx
= talloc_tos();
359 jn
= talloc_zero(ctx
, struct junction_map
);
364 if(!create_junction(ctx
, r
->in
.dfs_entry_path
,
365 !smbd_server_conn
->using_smb2
, jn
)) {
366 return WERR_DFS_NO_SUCH_SERVER
;
369 /* The following call can change the cwd. */
370 status
= get_referred_path(ctx
, r
->in
.dfs_entry_path
, smbd_server_conn
,
371 jn
, &consumedcnt
, &self_ref
);
372 if(!NT_STATUS_IS_OK(status
) ||
373 consumedcnt
< strlen(r
->in
.dfs_entry_path
)) {
374 return WERR_DFS_NO_SUCH_VOL
;
377 switch (r
->in
.level
) {
379 r
->out
.info
->info1
= talloc_zero(ctx
,struct dfs_Info1
);
380 if (!r
->out
.info
->info1
) {
383 ret
= init_reply_dfs_info_1(ctx
, jn
, r
->out
.info
->info1
);
386 r
->out
.info
->info2
= talloc_zero(ctx
,struct dfs_Info2
);
387 if (!r
->out
.info
->info2
) {
390 ret
= init_reply_dfs_info_2(ctx
, jn
, r
->out
.info
->info2
);
393 r
->out
.info
->info3
= talloc_zero(ctx
,struct dfs_Info3
);
394 if (!r
->out
.info
->info3
) {
397 ret
= init_reply_dfs_info_3(ctx
, jn
, r
->out
.info
->info3
);
400 r
->out
.info
->info100
= talloc_zero(ctx
,struct dfs_Info100
);
401 if (!r
->out
.info
->info100
) {
404 ret
= init_reply_dfs_info_100(ctx
, jn
, r
->out
.info
->info100
);
407 r
->out
.info
->info1
= NULL
;
408 return WERR_INVALID_PARAM
;
412 return WERR_INVALID_PARAM
;
417 WERROR
_dfs_SetInfo(struct pipes_struct
*p
, struct dfs_SetInfo
*r
)
419 /* FIXME: Implement your code here */
420 p
->rng_fault_state
= True
;
421 return WERR_NOT_SUPPORTED
;
424 WERROR
_dfs_Rename(struct pipes_struct
*p
, struct dfs_Rename
*r
)
426 /* FIXME: Implement your code here */
427 p
->rng_fault_state
= True
;
428 return WERR_NOT_SUPPORTED
;
431 WERROR
_dfs_Move(struct pipes_struct
*p
, struct dfs_Move
*r
)
433 /* FIXME: Implement your code here */
434 p
->rng_fault_state
= True
;
435 return WERR_NOT_SUPPORTED
;
438 WERROR
_dfs_ManagerGetConfigInfo(struct pipes_struct
*p
, struct dfs_ManagerGetConfigInfo
*r
)
440 /* FIXME: Implement your code here */
441 p
->rng_fault_state
= True
;
442 return WERR_NOT_SUPPORTED
;
445 WERROR
_dfs_ManagerSendSiteInfo(struct pipes_struct
*p
, struct dfs_ManagerSendSiteInfo
*r
)
447 /* FIXME: Implement your code here */
448 p
->rng_fault_state
= True
;
449 return WERR_NOT_SUPPORTED
;
452 WERROR
_dfs_AddFtRoot(struct pipes_struct
*p
, struct dfs_AddFtRoot
*r
)
454 /* FIXME: Implement your code here */
455 p
->rng_fault_state
= True
;
456 return WERR_NOT_SUPPORTED
;
459 WERROR
_dfs_RemoveFtRoot(struct pipes_struct
*p
, struct dfs_RemoveFtRoot
*r
)
461 /* FIXME: Implement your code here */
462 p
->rng_fault_state
= True
;
463 return WERR_NOT_SUPPORTED
;
466 WERROR
_dfs_AddStdRoot(struct pipes_struct
*p
, struct dfs_AddStdRoot
*r
)
468 /* FIXME: Implement your code here */
469 p
->rng_fault_state
= True
;
470 return WERR_NOT_SUPPORTED
;
473 WERROR
_dfs_RemoveStdRoot(struct pipes_struct
*p
, struct dfs_RemoveStdRoot
*r
)
475 /* FIXME: Implement your code here */
476 p
->rng_fault_state
= True
;
477 return WERR_NOT_SUPPORTED
;
480 WERROR
_dfs_ManagerInitialize(struct pipes_struct
*p
, struct dfs_ManagerInitialize
*r
)
482 /* FIXME: Implement your code here */
483 p
->rng_fault_state
= True
;
484 return WERR_NOT_SUPPORTED
;
487 WERROR
_dfs_AddStdRootForced(struct pipes_struct
*p
, struct dfs_AddStdRootForced
*r
)
489 /* FIXME: Implement your code here */
490 p
->rng_fault_state
= True
;
491 return WERR_NOT_SUPPORTED
;
494 WERROR
_dfs_GetDcAddress(struct pipes_struct
*p
, struct dfs_GetDcAddress
*r
)
496 /* FIXME: Implement your code here */
497 p
->rng_fault_state
= True
;
498 return WERR_NOT_SUPPORTED
;
501 WERROR
_dfs_SetDcAddress(struct pipes_struct
*p
, struct dfs_SetDcAddress
*r
)
503 /* FIXME: Implement your code here */
504 p
->rng_fault_state
= True
;
505 return WERR_NOT_SUPPORTED
;
508 WERROR
_dfs_FlushFtTable(struct pipes_struct
*p
, struct dfs_FlushFtTable
*r
)
510 /* FIXME: Implement your code here */
511 p
->rng_fault_state
= True
;
512 return WERR_NOT_SUPPORTED
;
515 WERROR
_dfs_Add2(struct pipes_struct
*p
, struct dfs_Add2
*r
)
517 /* FIXME: Implement your code here */
518 p
->rng_fault_state
= True
;
519 return WERR_NOT_SUPPORTED
;
522 WERROR
_dfs_Remove2(struct pipes_struct
*p
, struct dfs_Remove2
*r
)
524 /* FIXME: Implement your code here */
525 p
->rng_fault_state
= True
;
526 return WERR_NOT_SUPPORTED
;
529 WERROR
_dfs_EnumEx(struct pipes_struct
*p
, struct dfs_EnumEx
*r
)
531 /* FIXME: Implement your code here */
532 p
->rng_fault_state
= True
;
533 return WERR_NOT_SUPPORTED
;
536 WERROR
_dfs_SetInfo2(struct pipes_struct
*p
, struct dfs_SetInfo2
*r
)
538 /* FIXME: Implement your code here */
539 p
->rng_fault_state
= True
;
540 return WERR_NOT_SUPPORTED
;