First round of merging various UUID structures.
[Samba/gebeck_regimport.git] / source3 / rpc_server / srv_dfs_nt.c
blobac3ed9c3947db3280d06d331bfe9678a86201ff3
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines for Dfs
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Shirish Kalele 2000.
7 * Copyright (C) Jeremy Allison 2001.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* This is the implementation of the dfs pipe. */
26 #include "includes.h"
27 #include "nterr.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_RPC_SRV
32 #define MAX_MSDFS_JUNCTIONS 256
34 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
35 dfs exists, or 0 otherwise. */
37 uint32 _dfs_exist(pipes_struct *p, DFS_Q_DFS_EXIST *q_u, DFS_R_DFS_EXIST *r_u)
39 if(lp_host_msdfs())
40 return 1;
41 else
42 return 0;
45 WERROR _dfs_add(pipes_struct *p, DFS_Q_DFS_ADD* q_u, DFS_R_DFS_ADD *r_u)
47 struct current_user user;
48 struct junction_map jn;
49 struct referral* old_referral_list = NULL;
50 BOOL exists = False;
52 pstring dfspath, servername, sharename;
53 pstring altpath;
55 get_current_user(&user,p);
57 if (user.uid != 0) {
58 DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
59 return WERR_ACCESS_DENIED;
62 unistr2_to_ascii(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
63 unistr2_to_ascii(servername, &q_u->ServerName, sizeof(servername)-1);
64 unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
66 DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
67 dfspath, servername, sharename));
69 pstrcpy(altpath, servername);
70 pstrcat(altpath, "\\");
71 pstrcat(altpath, sharename);
73 /* The following call can change the cwd. */
74 if(get_referred_path(dfspath, &jn, NULL, NULL))
76 exists = True;
77 jn.referral_count += 1;
78 old_referral_list = jn.referral_list;
80 else
81 jn.referral_count = 1;
83 vfs_ChDir(p->conn,p->conn->connectpath);
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 vfs_ChDir(p->conn,p->conn->connectpath);
108 return WERR_DFS_CANT_CREATE_JUNCT;
110 vfs_ChDir(p->conn,p->conn->connectpath);
112 return WERR_OK;
115 WERROR _dfs_remove(pipes_struct *p, DFS_Q_DFS_REMOVE *q_u,
116 DFS_R_DFS_REMOVE *r_u)
118 struct current_user user;
119 struct junction_map jn;
120 BOOL found = False;
122 pstring dfspath, servername, sharename;
123 pstring altpath;
125 get_current_user(&user,p);
127 if (user.uid != 0) {
128 DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
129 return WERR_ACCESS_DENIED;
132 unistr2_to_ascii(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
133 if(q_u->ptr_ServerName)
134 unistr2_to_ascii(servername, &q_u->ServerName, sizeof(servername)-1);
136 if(q_u->ptr_ShareName)
137 unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
139 if(q_u->ptr_ServerName && q_u->ptr_ShareName)
141 pstrcpy(altpath, servername);
142 pstrcat(altpath, "\\");
143 pstrcat(altpath, sharename);
144 strlower_m(altpath);
147 DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
148 dfspath, servername, sharename));
150 if(!get_referred_path(dfspath, &jn, NULL, NULL))
151 return WERR_DFS_NO_SUCH_VOL;
153 /* if no server-share pair given, remove the msdfs link completely */
154 if(!q_u->ptr_ServerName && !q_u->ptr_ShareName)
156 if(!remove_msdfs_link(&jn)) {
157 vfs_ChDir(p->conn,p->conn->connectpath);
158 return WERR_DFS_NO_SUCH_VOL;
160 vfs_ChDir(p->conn,p->conn->connectpath);
162 else
164 int i=0;
165 /* compare each referral in the list with the one to remove */
166 DEBUG(10,("altpath: .%s. refcnt: %d\n", altpath, jn.referral_count));
167 for(i=0;i<jn.referral_count;i++)
169 pstring refpath;
170 pstrcpy(refpath,jn.referral_list[i].alternate_path);
171 trim_char(refpath, '\\', '\\');
172 DEBUG(10,("_dfs_remove: refpath: .%s.\n", refpath));
173 if(strequal(refpath, altpath))
175 *(jn.referral_list[i].alternate_path)='\0';
176 DEBUG(10,("_dfs_remove: Removal request matches referral %s\n",
177 refpath));
178 found = True;
181 if(!found)
182 return WERR_DFS_NO_SUCH_SHARE;
184 /* Only one referral, remove it */
185 if(jn.referral_count == 1)
187 if(!remove_msdfs_link(&jn)) {
188 vfs_ChDir(p->conn,p->conn->connectpath);
189 return WERR_DFS_NO_SUCH_VOL;
191 vfs_ChDir(p->conn,p->conn->connectpath);
193 else
195 if(!create_msdfs_link(&jn, True)) {
196 vfs_ChDir(p->conn,p->conn->connectpath);
197 return WERR_DFS_CANT_CREATE_JUNCT;
199 vfs_ChDir(p->conn,p->conn->connectpath);
203 return WERR_OK;
206 static BOOL init_reply_dfs_info_1(struct junction_map* j, DFS_INFO_1* dfs1, int num_j)
208 int i=0;
209 for(i=0;i<num_j;i++)
211 pstring str;
212 dfs1[i].ptr_entrypath = 1;
213 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname(),
214 j[i].service_name, j[i].volume_name);
215 DEBUG(5,("init_reply_dfs_info_1: %d) initing entrypath: %s\n",i,str));
216 init_unistr2(&dfs1[i].entrypath,str,UNI_STR_TERMINATE);
218 return True;
221 static BOOL init_reply_dfs_info_2(struct junction_map* j, DFS_INFO_2* dfs2, int num_j)
223 int i=0;
224 for(i=0;i<num_j;i++)
226 pstring str;
227 dfs2[i].ptr_entrypath = 1;
228 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname(),
229 j[i].service_name, j[i].volume_name);
230 init_unistr2(&dfs2[i].entrypath, str, UNI_STR_TERMINATE);
231 dfs2[i].ptr_comment = 0;
232 dfs2[i].state = 1; /* set up state of dfs junction as OK */
233 dfs2[i].num_storages = j[i].referral_count;
235 return True;
238 static BOOL init_reply_dfs_info_3(TALLOC_CTX *ctx, struct junction_map* j, DFS_INFO_3* dfs3, int num_j)
240 int i=0,ii=0;
241 for(i=0;i<num_j;i++)
243 pstring str;
244 dfs3[i].ptr_entrypath = 1;
245 if (j[i].volume_name[0] == '\0')
246 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s",
247 global_myname(), j[i].service_name);
248 else
249 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname(),
250 j[i].service_name, j[i].volume_name);
252 init_unistr2(&dfs3[i].entrypath, str, UNI_STR_TERMINATE);
253 dfs3[i].ptr_comment = 1;
254 init_unistr2(&dfs3[i].comment, "", UNI_STR_TERMINATE);
255 dfs3[i].state = 1;
256 dfs3[i].num_storages = dfs3[i].num_storage_infos = j[i].referral_count;
257 dfs3[i].ptr_storages = 1;
259 /* also enumerate the storages */
260 dfs3[i].storages = (DFS_STORAGE_INFO*) talloc(ctx, j[i].referral_count *
261 sizeof(DFS_STORAGE_INFO));
262 if (!dfs3[i].storages)
263 return False;
265 memset(dfs3[i].storages, '\0', j[i].referral_count * sizeof(DFS_STORAGE_INFO));
267 for(ii=0;ii<j[i].referral_count;ii++)
269 char* p;
270 pstring path;
271 DFS_STORAGE_INFO* stor = &(dfs3[i].storages[ii]);
272 struct referral* ref = &(j[i].referral_list[ii]);
274 pstrcpy(path, ref->alternate_path);
275 trim_char(path,'\\','\0');
276 p = strrchr_m(path,'\\');
277 if(p==NULL)
279 DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
280 continue;
282 *p = '\0';
283 DEBUG(5,("storage %d: %s.%s\n",ii,path,p+1));
284 stor->state = 2; /* set all storages as ONLINE */
285 init_unistr2(&stor->servername, path, UNI_STR_TERMINATE);
286 init_unistr2(&stor->sharename, p+1, UNI_STR_TERMINATE);
287 stor->ptr_servername = stor->ptr_sharename = 1;
290 return True;
293 static WERROR init_reply_dfs_ctr(TALLOC_CTX *ctx, uint32 level,
294 DFS_INFO_CTR* ctr, struct junction_map* jn,
295 int num_jn)
297 /* do the levels */
298 switch(level)
300 case 1:
302 DFS_INFO_1* dfs1;
303 dfs1 = (DFS_INFO_1*) talloc(ctx, num_jn * sizeof(DFS_INFO_1));
304 if (!dfs1)
305 return WERR_NOMEM;
306 init_reply_dfs_info_1(jn, dfs1, num_jn);
307 ctr->dfs.info1 = dfs1;
308 break;
310 case 2:
312 DFS_INFO_2* dfs2;
313 dfs2 = (DFS_INFO_2*) talloc(ctx, num_jn * sizeof(DFS_INFO_2));
314 if (!dfs2)
315 return WERR_NOMEM;
316 init_reply_dfs_info_2(jn, dfs2, num_jn);
317 ctr->dfs.info2 = dfs2;
318 break;
320 case 3:
322 DFS_INFO_3* dfs3;
323 dfs3 = (DFS_INFO_3*) talloc(ctx, num_jn * sizeof(DFS_INFO_3));
324 if (!dfs3)
325 return WERR_NOMEM;
326 init_reply_dfs_info_3(ctx, jn, dfs3, num_jn);
327 ctr->dfs.info3 = dfs3;
328 break;
330 default:
331 return WERR_INVALID_PARAM;
333 return WERR_OK;
336 WERROR _dfs_enum(pipes_struct *p, DFS_Q_DFS_ENUM *q_u, DFS_R_DFS_ENUM *r_u)
338 uint32 level = q_u->level;
339 struct junction_map jn[MAX_MSDFS_JUNCTIONS];
340 int num_jn = 0;
342 num_jn = enum_msdfs_links(jn);
343 vfs_ChDir(p->conn,p->conn->connectpath);
345 DEBUG(5,("make_reply_dfs_enum: %d junctions found in Dfs, doing level %d\n", num_jn, level));
347 r_u->ptr_buffer = level;
348 r_u->level = r_u->level2 = level;
349 r_u->ptr_num_entries = r_u->ptr_num_entries2 = 1;
350 r_u->num_entries = r_u->num_entries2 = num_jn;
351 r_u->reshnd.ptr_hnd = 1;
352 r_u->reshnd.handle = num_jn;
354 r_u->ctr = (DFS_INFO_CTR*)talloc(p->mem_ctx, sizeof(DFS_INFO_CTR));
355 if (!r_u->ctr)
356 return WERR_NOMEM;
357 ZERO_STRUCTP(r_u->ctr);
358 r_u->ctr->switch_value = level;
359 r_u->ctr->num_entries = num_jn;
360 r_u->ctr->ptr_dfs_ctr = 1;
362 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, r_u->ctr, jn, num_jn);
364 return r_u->status;
367 WERROR _dfs_get_info(pipes_struct *p, DFS_Q_DFS_GET_INFO *q_u,
368 DFS_R_DFS_GET_INFO *r_u)
370 UNISTR2* uni_path = &q_u->uni_path;
371 uint32 level = q_u->level;
372 pstring path;
373 struct junction_map jn;
375 unistr2_to_ascii(path, uni_path, sizeof(path)-1);
376 if(!create_junction(path, &jn))
377 return WERR_DFS_NO_SUCH_SERVER;
379 /* The following call can change the cwd. */
380 if(!get_referred_path(path, &jn, NULL, NULL)) {
381 vfs_ChDir(p->conn,p->conn->connectpath);
382 return WERR_DFS_NO_SUCH_VOL;
385 vfs_ChDir(p->conn,p->conn->connectpath);
386 r_u->level = level;
387 r_u->ptr_ctr = 1;
388 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, &r_u->ctr, &jn, 1);
390 return r_u->status;