2 * Unix SMB/Netbios implementation.
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.
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 /* This is the implementation of the dfs pipe. */
30 extern int DEBUGLEVEL;
31 extern pstring global_myname;
35 #define MAX_MSDFS_JUNCTIONS 256
37 uint32 _dfs_exist(pipes_struct *p, DFS_Q_DFS_EXIST *q_u, DFS_R_DFS_EXIST *r_u)
45 uint32 _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;
52 pstring dfspath, servername, sharename;
55 get_current_user(&user,p);
58 DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
59 return ERROR_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 if(!create_junction(dfspath, &jn))
74 return NERR_DfsNoSuchServer;
76 if(get_referred_path(&jn))
79 jn.referral_count += 1;
80 old_referral_list = jn.referral_list;
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 NERR_DfsInternalError;
96 memcpy(jn.referral_list, old_referral_list,
97 sizeof(struct referral)*jn.referral_count-1);
98 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 NERR_DfsCantCreateJunctionPoint;
109 return NT_STATUS_NOPROBLEMO;
112 uint32 _dfs_remove(pipes_struct *p, DFS_Q_DFS_REMOVE *q_u, DFS_R_DFS_REMOVE *r_u)
114 struct current_user user;
115 struct junction_map jn;
118 pstring dfspath, servername, sharename;
121 get_current_user(&user,p);
124 DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
125 return ERROR_ACCESS_DENIED;
128 unistr2_to_ascii(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
129 if(q_u->ptr_ServerName)
130 unistr2_to_ascii(servername, &q_u->ServerName, sizeof(servername)-1);
132 if(q_u->ptr_ShareName)
133 unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
135 if(q_u->ptr_ServerName && q_u->ptr_ShareName)
137 pstrcpy(altpath, servername);
138 pstrcat(altpath, "\\");
139 pstrcat(altpath, sharename);
142 DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
143 dfspath, servername, sharename));
145 if(!create_junction(dfspath, &jn))
146 return NERR_DfsNoSuchServer;
148 if(!get_referred_path(&jn))
149 return NERR_DfsNoSuchVolume;
151 /* if no server-share pair given, remove the msdfs link completely */
152 if(!q_u->ptr_ServerName && !q_u->ptr_ShareName)
154 if(!remove_msdfs_link(&jn))
155 return NERR_DfsNoSuchVolume;
160 /* compare each referral in the list with the one to remove */
161 for(i=0;i<jn.referral_count;i++)
164 pstrcpy(refpath,jn.referral_list[i].alternate_path);
165 trim_string(refpath, "\\", "\\");
166 if(strequal(refpath, altpath))
168 *(jn.referral_list[i].alternate_path)='\0';
173 return NERR_DfsNoSuchShare;
175 /* Only one referral, remove it */
176 if(jn.referral_count == 1)
178 if(!remove_msdfs_link(&jn))
179 return NERR_DfsNoSuchVolume;
183 if(!create_msdfs_link(&jn, True))
184 return NERR_DfsCantCreateJunctionPoint;
188 return NT_STATUS_NOPROBLEMO;
191 static BOOL init_reply_dfs_info_1(struct junction_map* j, DFS_INFO_1* dfs1, int num_j)
197 dfs1[i].ptr_entrypath = 1;
198 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
199 j[i].service_name, j[i].volume_name);
200 DEBUG(5,("init_reply_dfs_info_1: %d) initing entrypath: %s\n",i,str));
201 init_unistr2(&dfs1[i].entrypath,str,strlen(str)+1);
206 static BOOL init_reply_dfs_info_2(struct junction_map* j, DFS_INFO_2* dfs2, int num_j)
212 dfs2[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 init_unistr2(&dfs2[i].entrypath, str, strlen(str)+1);
216 dfs2[i].ptr_comment = 0;
217 dfs2[i].state = 1; /* set up state of dfs junction as OK */
218 dfs2[i].num_storages = j[i].referral_count;
223 static BOOL init_reply_dfs_info_3(TALLOC_CTX *ctx, struct junction_map* j, DFS_INFO_3* dfs3, int num_j)
229 dfs3[i].ptr_entrypath = 1;
230 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
231 j[i].service_name, j[i].volume_name);
232 init_unistr2(&dfs3[i].entrypath, str, strlen(str)+1);
233 dfs3[i].ptr_comment = 1;
234 init_unistr2(&dfs3[i].comment, "", 1);
236 dfs3[i].num_storages = dfs3[i].num_storage_infos = j[i].referral_count;
237 dfs3[i].ptr_storages = 1;
239 /* also enumerate the storages */
240 dfs3[i].storages = (DFS_STORAGE_INFO*) talloc(ctx, j[i].referral_count *
241 sizeof(DFS_STORAGE_INFO));
242 if (!dfs3[i].storages)
245 memset(dfs3[i].storages, '\0', j[i].referral_count * sizeof(DFS_STORAGE_INFO));
247 for(ii=0;ii<j[i].referral_count;ii++)
251 DFS_STORAGE_INFO* stor = &(dfs3[i].storages[ii]);
252 struct referral* ref = &(j[i].referral_list[ii]);
254 pstrcpy(path, ref->alternate_path);
255 trim_string(path,"\\","");
256 p = strrchr(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 storages as ONLINE */
265 init_unistr2(&stor->servername, path, strlen(path)+1);
266 init_unistr2(&stor->sharename, p+1, strlen(p+1)+1);
267 stor->ptr_servername = stor->ptr_sharename = 1;
273 static uint32 init_reply_dfs_ctr(TALLOC_CTX *ctx, uint32 level, DFS_INFO_CTR* ctr,
274 struct junction_map* jn, int num_jn)
282 dfs1 = (DFS_INFO_1*) talloc(ctx, num_jn * sizeof(DFS_INFO_1));
284 return NT_STATUS_NO_MEMORY;
285 init_reply_dfs_info_1(jn, dfs1, num_jn);
286 ctr->dfs.info1 = dfs1;
292 dfs2 = (DFS_INFO_2*) talloc(ctx, num_jn * sizeof(DFS_INFO_2));
294 return NT_STATUS_NO_MEMORY;
295 init_reply_dfs_info_2(jn, dfs2, num_jn);
296 ctr->dfs.info2 = dfs2;
302 dfs3 = (DFS_INFO_3*) talloc(ctx, num_jn * sizeof(DFS_INFO_3));
304 return NT_STATUS_NO_MEMORY;
305 init_reply_dfs_info_3(ctx, jn, dfs3, num_jn);
306 ctr->dfs.info3 = dfs3;
310 return NT_STATUS_INVALID_LEVEL;
312 return NT_STATUS_NOPROBLEMO;
315 uint32 _dfs_enum(pipes_struct *p, DFS_Q_DFS_ENUM *q_u, DFS_R_DFS_ENUM *r_u)
317 uint32 level = q_u->level;
318 struct junction_map jn[MAX_MSDFS_JUNCTIONS];
321 num_jn = enum_msdfs_links(jn);
323 DEBUG(5,("make_reply_dfs_enum: %d junctions found in Dfs, doing level %d\n", num_jn, level));
325 r_u->ptr_buffer = level;
326 r_u->level = r_u->level2 = level;
327 r_u->ptr_num_entries = r_u->ptr_num_entries2 = 1;
328 r_u->num_entries = r_u->num_entries2 = num_jn;
329 r_u->reshnd.ptr_hnd = 1;
330 r_u->reshnd.handle = num_jn;
332 r_u->ctr = (DFS_INFO_CTR*)talloc(p->mem_ctx, sizeof(DFS_INFO_CTR));
334 return NT_STATUS_NO_MEMORY;
335 ZERO_STRUCTP(r_u->ctr);
336 r_u->ctr->switch_value = level;
337 r_u->ctr->num_entries = num_jn;
338 r_u->ctr->ptr_dfs_ctr = 1;
340 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, r_u->ctr, jn, num_jn);
345 uint32 _dfs_get_info(pipes_struct *p, DFS_Q_DFS_GET_INFO *q_u, DFS_R_DFS_GET_INFO *r_u)
347 UNISTR2* uni_path = &q_u->uni_path;
348 uint32 level = q_u->level;
350 struct junction_map jn;
352 unistr2_to_ascii(path, uni_path, sizeof(path)-1);
353 if(!create_junction(path, &jn))
354 return NERR_DfsNoSuchServer;
356 if(!get_referred_path(&jn))
357 return NERR_DfsNoSuchVolume;
361 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, &r_u->ctr, &jn, 1);
366 void dfs_dummy1(void) {;} /* So some compilers don't complain. */