fix restart option (noticed by Stanford K. Baldwin)
[Samba.git] / source / rpc_server / srv_dfs_nt.c
blobf3f495afcf7447321074b2c536b37b264536c8f6
1 /*
2 * Unix SMB/Netbios implementation.
3 * Version 1.9.
4 * RPC Pipe client / server routines for Dfs
5 * Copyright (C) Andrew Tridgell 1992-1997,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7 * Copyright (C) Shirish Kalele 2000.
8 * Copyright (C) Jeremy Allison 2001.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /*
26 * This is the implementation of the dfs pipe.
29 #include "includes.h"
30 #include "nterr.h"
32 extern pstring global_myname;
34 #define MAX_MSDFS_JUNCTIONS 256
35 #ifdef WITH_MSDFS
37 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
38 dfs exists, or 0 otherwise. */
40 uint32 _dfs_exist(pipes_struct *p, DFS_Q_DFS_EXIST *q_u, DFS_R_DFS_EXIST *r_u)
42 if(lp_host_msdfs())
43 return 1;
44 else
45 return 0;
48 WERROR _dfs_add(pipes_struct *p, DFS_Q_DFS_ADD* q_u, DFS_R_DFS_ADD *r_u)
50 struct current_user user;
51 struct junction_map jn;
52 struct referral* old_referral_list = NULL;
53 BOOL exists = False;
55 pstring dfspath, servername, sharename;
56 pstring altpath;
58 get_current_user(&user,p);
60 if (user.uid != 0) {
61 DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
62 return WERR_ACCESS_DENIED;
65 unistr2_to_dos(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
66 unistr2_to_dos(servername, &q_u->ServerName, sizeof(servername)-1);
67 unistr2_to_dos(sharename, &q_u->ShareName, sizeof(sharename)-1);
69 DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
70 dfspath, servername, sharename));
72 pstrcpy(altpath, servername);
73 pstrcat(altpath, "\\");
74 pstrcat(altpath, sharename);
76 if(get_referred_path(dfspath, &jn, NULL, NULL))
78 exists = True;
79 jn.referral_count += 1;
80 old_referral_list = jn.referral_list;
82 else
83 jn.referral_count = 1;
85 jn.referral_list = (struct referral*) talloc(p->mem_ctx, jn.referral_count
86 * sizeof(struct referral));
88 if(jn.referral_list == NULL)
90 DEBUG(0,("init_reply_dfs_add: talloc failed for referral list!\n"));
91 return WERR_DFS_INTERNAL_ERROR;
94 if(old_referral_list)
96 memcpy(jn.referral_list, old_referral_list,
97 sizeof(struct referral)*jn.referral_count-1);
98 SAFE_FREE(old_referral_list);
101 jn.referral_list[jn.referral_count-1].proximity = 0;
102 jn.referral_list[jn.referral_count-1].ttl = REFERRAL_TTL;
104 pstrcpy(jn.referral_list[jn.referral_count-1].alternate_path, altpath);
106 if(!create_msdfs_link(&jn, exists))
107 return WERR_DFS_CANT_CREATE_JUNCT;
109 return WERR_OK;
112 WERROR _dfs_remove(pipes_struct *p, DFS_Q_DFS_REMOVE *q_u,
113 DFS_R_DFS_REMOVE *r_u)
115 struct current_user user;
116 struct junction_map jn;
117 BOOL found = False;
119 pstring dfspath, servername, sharename;
120 pstring altpath;
122 get_current_user(&user,p);
124 if (user.uid != 0) {
125 DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
126 return WERR_ACCESS_DENIED;
129 unistr2_to_dos(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
130 if(q_u->ptr_ServerName)
131 unistr2_to_dos(servername, &q_u->ServerName, sizeof(servername)-1);
133 if(q_u->ptr_ShareName)
134 unistr2_to_dos(sharename, &q_u->ShareName, sizeof(sharename)-1);
136 if(q_u->ptr_ServerName && q_u->ptr_ShareName)
138 pstrcpy(altpath, servername);
139 pstrcat(altpath, "\\");
140 pstrcat(altpath, sharename);
141 strlower(altpath);
144 DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
145 dfspath, servername, sharename));
147 if(!get_referred_path(dfspath, &jn, NULL, NULL))
148 return WERR_DFS_NO_SUCH_VOL;
150 /* if no server-share pair given, remove the msdfs link completely */
151 if(!q_u->ptr_ServerName && !q_u->ptr_ShareName)
153 if(!remove_msdfs_link(&jn))
154 return WERR_DFS_NO_SUCH_VOL;
156 else
158 int i=0;
159 /* compare each referral in the list with the one to remove */
160 DEBUG(10,("altpath: .%s. refcnt: %d\n", altpath, jn.referral_count));
161 for(i=0;i<jn.referral_count;i++)
163 pstring refpath;
164 pstrcpy(refpath,jn.referral_list[i].alternate_path);
165 trim_string(refpath, "\\", "\\");
166 DEBUG(10,("_dfs_remove: refpath: .%s.\n", refpath));
167 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;
175 if(!found)
176 return WERR_DFS_NO_SUCH_SHARE;
178 /* Only one referral, remove it */
179 if(jn.referral_count == 1)
181 if(!remove_msdfs_link(&jn))
182 return WERR_DFS_NO_SUCH_VOL;
184 else
186 if(!create_msdfs_link(&jn, True))
187 return WERR_DFS_CANT_CREATE_JUNCT;
191 return WERR_OK;
194 static BOOL init_reply_dfs_info_1(struct junction_map* j, DFS_INFO_1* dfs1, int num_j)
196 int i=0;
197 for(i=0;i<num_j;i++)
199 pstring str;
200 dfs1[i].ptr_entrypath = 1;
201 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
202 j[i].service_name, j[i].volume_name);
203 DEBUG(5,("init_reply_dfs_info_1: %d) initing entrypath: %s\n",i,str));
204 init_unistr2(&dfs1[i].entrypath,str,strlen(str)+1);
206 return True;
209 static BOOL init_reply_dfs_info_2(struct junction_map* j, DFS_INFO_2* dfs2, int num_j)
211 int i=0;
212 for(i=0;i<num_j;i++)
214 pstring str;
215 dfs2[i].ptr_entrypath = 1;
216 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
217 j[i].service_name, j[i].volume_name);
218 init_unistr2(&dfs2[i].entrypath, str, strlen(str)+1);
219 dfs2[i].ptr_comment = 0;
220 dfs2[i].state = 1; /* set up state of dfs junction as OK */
221 dfs2[i].num_storages = j[i].referral_count;
223 return True;
226 static BOOL init_reply_dfs_info_3(TALLOC_CTX *ctx, struct junction_map* j, DFS_INFO_3* dfs3, int num_j)
228 int i=0,ii=0;
229 for(i=0;i<num_j;i++)
231 pstring str;
232 dfs3[i].ptr_entrypath = 1;
233 if (j[i].volume_name[0] == '\0')
234 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s",
235 global_myname, j[i].service_name);
236 else
237 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
238 j[i].service_name, j[i].volume_name);
240 init_unistr2(&dfs3[i].entrypath, str, strlen(str)+1);
241 dfs3[i].ptr_comment = 1;
242 init_unistr2(&dfs3[i].comment, "", 1);
243 dfs3[i].state = 1;
244 dfs3[i].num_storages = dfs3[i].num_storage_infos = j[i].referral_count;
245 dfs3[i].ptr_storages = 1;
247 /* also enumerate the storages */
248 dfs3[i].storages = (DFS_STORAGE_INFO*) talloc(ctx, j[i].referral_count *
249 sizeof(DFS_STORAGE_INFO));
250 if (!dfs3[i].storages)
251 return False;
253 memset(dfs3[i].storages, '\0', j[i].referral_count * sizeof(DFS_STORAGE_INFO));
255 for(ii=0;ii<j[i].referral_count;ii++)
257 char* p;
258 pstring path;
259 DFS_STORAGE_INFO* stor = &(dfs3[i].storages[ii]);
260 struct referral* ref = &(j[i].referral_list[ii]);
262 pstrcpy(path, ref->alternate_path);
263 trim_string(path,"\\","");
264 p = strrchr(path,'\\');
265 if(p==NULL)
267 DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
268 continue;
270 *p = '\0';
271 DEBUG(5,("storage %d: %s.%s\n",ii,path,p+1));
272 stor->state = 2; /* set all storages as ONLINE */
273 init_unistr2(&stor->servername, path, strlen(path)+1);
274 init_unistr2(&stor->sharename, p+1, strlen(p+1)+1);
275 stor->ptr_servername = stor->ptr_sharename = 1;
278 return True;
281 static WERROR init_reply_dfs_ctr(TALLOC_CTX *ctx, uint32 level,
282 DFS_INFO_CTR* ctr, struct junction_map* jn,
283 int num_jn)
285 /* do the levels */
286 switch(level)
288 case 1:
290 DFS_INFO_1* dfs1;
291 dfs1 = (DFS_INFO_1*) talloc(ctx, num_jn * sizeof(DFS_INFO_1));
292 if (!dfs1)
293 return WERR_NOMEM;
294 init_reply_dfs_info_1(jn, dfs1, num_jn);
295 ctr->dfs.info1 = dfs1;
296 break;
298 case 2:
300 DFS_INFO_2* dfs2;
301 dfs2 = (DFS_INFO_2*) talloc(ctx, num_jn * sizeof(DFS_INFO_2));
302 if (!dfs2)
303 return WERR_NOMEM;
304 init_reply_dfs_info_2(jn, dfs2, num_jn);
305 ctr->dfs.info2 = dfs2;
306 break;
308 case 3:
310 DFS_INFO_3* dfs3;
311 dfs3 = (DFS_INFO_3*) talloc(ctx, num_jn * sizeof(DFS_INFO_3));
312 if (!dfs3)
313 return WERR_NOMEM;
314 init_reply_dfs_info_3(ctx, jn, dfs3, num_jn);
315 ctr->dfs.info3 = dfs3;
316 break;
318 default:
319 return WERR_INVALID_PARAM;
321 return WERR_OK;
324 WERROR _dfs_enum(pipes_struct *p, DFS_Q_DFS_ENUM *q_u, DFS_R_DFS_ENUM *r_u)
326 uint32 level = q_u->level;
327 struct junction_map jn[MAX_MSDFS_JUNCTIONS];
328 int num_jn = 0;
330 num_jn = enum_msdfs_links(jn);
332 DEBUG(5,("make_reply_dfs_enum: %d junctions found in Dfs, doing level %d\n", num_jn, level));
334 r_u->ptr_buffer = level;
335 r_u->level = r_u->level2 = level;
336 r_u->ptr_num_entries = r_u->ptr_num_entries2 = 1;
337 r_u->num_entries = r_u->num_entries2 = num_jn;
338 r_u->reshnd.ptr_hnd = 1;
339 r_u->reshnd.handle = num_jn;
341 r_u->ctr = (DFS_INFO_CTR*)talloc(p->mem_ctx, sizeof(DFS_INFO_CTR));
342 if (!r_u->ctr)
343 return WERR_NOMEM;
344 ZERO_STRUCTP(r_u->ctr);
345 r_u->ctr->switch_value = level;
346 r_u->ctr->num_entries = num_jn;
347 r_u->ctr->ptr_dfs_ctr = 1;
349 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, r_u->ctr, jn, num_jn);
351 return r_u->status;
354 WERROR _dfs_get_info(pipes_struct *p, DFS_Q_DFS_GET_INFO *q_u,
355 DFS_R_DFS_GET_INFO *r_u)
357 UNISTR2* uni_path = &q_u->uni_path;
358 uint32 level = q_u->level;
359 pstring path;
360 struct junction_map jn;
362 unistr2_to_dos(path, uni_path, sizeof(path)-1);
363 if(!create_junction(path, &jn))
364 return WERR_DFS_NO_SUCH_SERVER;
366 if(!get_referred_path(path, &jn, NULL, NULL))
367 return WERR_DFS_NO_SUCH_VOL;
369 r_u->level = level;
370 r_u->ptr_ctr = 1;
371 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, &r_u->ctr, &jn, 1);
373 return r_u->status;
375 #else
376 void dfs_dummy1(void) {;} /* So some compilers don't complain. */
377 #endif