e62334c3774b863965a03f313dc316d2b2a1e984
[sharpe/samba-autobuild/.git] / source3 / rpc_server / srv_dfs_nt.c
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.
14  *  
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.
19  *  
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.
23  */
24
25 /* This is the implementation of the dfs pipe. */
26
27 #include "includes.h"
28 #include "nterr.h"
29
30 extern pstring global_myname;
31
32 #define MAX_MSDFS_JUNCTIONS 256
33
34 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
35    dfs exists, or 0 otherwise. */
36
37 uint32 _dfs_exist(pipes_struct *p, DFS_Q_DFS_EXIST *q_u, DFS_R_DFS_EXIST *r_u)
38 {
39         if(lp_host_msdfs()) 
40                 return 1;
41         else
42                 return 0;
43 }
44
45 WERROR _dfs_add(pipes_struct *p, DFS_Q_DFS_ADD* q_u, DFS_R_DFS_ADD *r_u)
46 {
47   struct current_user user;
48   struct junction_map jn;
49   struct referral* old_referral_list = NULL;
50   BOOL exists = False;
51
52   pstring dfspath, servername, sharename;
53   pstring altpath;
54
55   get_current_user(&user,p);
56
57   if (user.uid != 0) {
58         DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
59         return WERR_ACCESS_DENIED;
60   }
61
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);
65
66   DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
67            dfspath, servername, sharename));
68
69   pstrcpy(altpath, servername);
70   pstrcat(altpath, "\\");
71   pstrcat(altpath, sharename);
72
73   if(!create_junction(dfspath, &jn))
74     return WERR_DFS_NO_SUCH_SERVER;
75
76   if(get_referred_path(&jn))
77     {
78       exists = True;
79       jn.referral_count += 1;
80       old_referral_list = jn.referral_list;
81     }
82   else
83     jn.referral_count = 1;
84
85   jn.referral_list = (struct referral*) talloc(p->mem_ctx, jn.referral_count 
86                                                * sizeof(struct referral));
87
88   if(jn.referral_list == NULL)
89     {
90       DEBUG(0,("init_reply_dfs_add: talloc failed for referral list!\n"));
91       return WERR_DFS_INTERNAL_ERROR;
92     }
93
94   if(old_referral_list)
95     {
96       memcpy(jn.referral_list, old_referral_list, 
97              sizeof(struct referral)*jn.referral_count-1);
98       SAFE_FREE(old_referral_list);
99     }
100   
101   jn.referral_list[jn.referral_count-1].proximity = 0;
102   jn.referral_list[jn.referral_count-1].ttl = REFERRAL_TTL;
103
104   pstrcpy(jn.referral_list[jn.referral_count-1].alternate_path, altpath);
105   
106   if(!create_msdfs_link(&jn, exists))
107     return WERR_DFS_CANT_CREATE_JUNCT;
108
109   return WERR_OK;
110 }
111
112 WERROR _dfs_remove(pipes_struct *p, DFS_Q_DFS_REMOVE *q_u, 
113                    DFS_R_DFS_REMOVE *r_u)
114 {
115   struct current_user user;
116   struct junction_map jn;
117   BOOL found = False;
118
119   pstring dfspath, servername, sharename;
120   pstring altpath;
121
122   get_current_user(&user,p);
123
124   if (user.uid != 0) {
125         DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
126         return WERR_ACCESS_DENIED;
127   }
128
129   unistr2_to_ascii(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
130   if(q_u->ptr_ServerName)
131     unistr2_to_ascii(servername, &q_u->ServerName, sizeof(servername)-1);
132
133   if(q_u->ptr_ShareName)
134     unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
135
136   if(q_u->ptr_ServerName && q_u->ptr_ShareName)
137     {
138       pstrcpy(altpath, servername);
139       pstrcat(altpath, "\\");
140       pstrcat(altpath, sharename);
141     }
142
143   DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
144            dfspath, servername, sharename));
145
146   if(!create_junction(dfspath, &jn))
147     return WERR_DFS_NO_SUCH_SERVER;
148
149   if(!get_referred_path(&jn))
150     return WERR_DFS_NO_SUCH_VOL;
151
152   /* if no server-share pair given, remove the msdfs link completely */
153   if(!q_u->ptr_ServerName && !q_u->ptr_ShareName)
154     {
155       if(!remove_msdfs_link(&jn))
156         return WERR_DFS_NO_SUCH_VOL;
157     }
158   else
159     {
160       int i=0;
161       /* compare each referral in the list with the one to remove */
162       for(i=0;i<jn.referral_count;i++)
163         {
164           pstring refpath;
165           pstrcpy(refpath,jn.referral_list[i].alternate_path);
166           trim_string(refpath, "\\", "\\");
167           if(strequal(refpath, altpath))
168             {
169               *(jn.referral_list[i].alternate_path)='\0';
170               found = True;
171             }
172         }
173       if(!found)
174         return WERR_DFS_NO_SUCH_SHARE;
175       
176       /* Only one referral, remove it */
177       if(jn.referral_count == 1)
178         {
179           if(!remove_msdfs_link(&jn))
180             return WERR_DFS_NO_SUCH_VOL;
181         }
182       else
183         {
184           if(!create_msdfs_link(&jn, True))
185             return WERR_DFS_CANT_CREATE_JUNCT;
186         }
187     }
188
189   return WERR_OK;
190 }
191
192 static BOOL init_reply_dfs_info_1(struct junction_map* j, DFS_INFO_1* dfs1, int num_j)
193 {
194   int i=0;
195   for(i=0;i<num_j;i++) 
196     {
197       pstring str;
198       dfs1[i].ptr_entrypath = 1;
199       slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname, 
200                j[i].service_name, j[i].volume_name);
201       DEBUG(5,("init_reply_dfs_info_1: %d) initing entrypath: %s\n",i,str));
202       init_unistr2(&dfs1[i].entrypath,str,strlen(str)+1);
203     }
204   return True;
205 }
206
207 static BOOL init_reply_dfs_info_2(struct junction_map* j, DFS_INFO_2* dfs2, int num_j)
208 {
209   int i=0;
210   for(i=0;i<num_j;i++)
211     {
212       pstring str;
213       dfs2[i].ptr_entrypath = 1;
214       slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
215                j[i].service_name, j[i].volume_name);
216       init_unistr2(&dfs2[i].entrypath, str, strlen(str)+1);
217       dfs2[i].ptr_comment = 0;
218       dfs2[i].state = 1; /* set up state of dfs junction as OK */
219       dfs2[i].num_storages = j[i].referral_count;
220     }
221   return True;
222 }
223
224 static BOOL init_reply_dfs_info_3(TALLOC_CTX *ctx, struct junction_map* j, DFS_INFO_3* dfs3, int num_j)
225 {
226   int i=0,ii=0;
227   for(i=0;i<num_j;i++)
228     {
229       pstring str;
230       dfs3[i].ptr_entrypath = 1;
231       slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
232                j[i].service_name, j[i].volume_name);
233       init_unistr2(&dfs3[i].entrypath, str, strlen(str)+1);
234       dfs3[i].ptr_comment = 1;
235       init_unistr2(&dfs3[i].comment, "", 1); 
236       dfs3[i].state = 1;
237       dfs3[i].num_storages = dfs3[i].num_storage_infos = j[i].referral_count;
238       dfs3[i].ptr_storages = 1;
239      
240       /* also enumerate the storages */
241       dfs3[i].storages = (DFS_STORAGE_INFO*) talloc(ctx, j[i].referral_count * 
242                                                     sizeof(DFS_STORAGE_INFO));
243       if (!dfs3[i].storages)
244         return False;
245
246       memset(dfs3[i].storages, '\0', j[i].referral_count * sizeof(DFS_STORAGE_INFO));
247
248       for(ii=0;ii<j[i].referral_count;ii++)
249         {
250           char* p; 
251           pstring path;
252           DFS_STORAGE_INFO* stor = &(dfs3[i].storages[ii]);
253           struct referral* ref = &(j[i].referral_list[ii]);
254           
255           pstrcpy(path, ref->alternate_path);
256           trim_string(path,"\\","");
257           p = strrchr_m(path,'\\');
258           if(p==NULL)
259             {
260               DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
261               continue;
262             }
263           *p = '\0';
264           DEBUG(5,("storage %d: %s.%s\n",ii,path,p+1));
265           stor->state = 2; /* set all storages as ONLINE */
266           init_unistr2(&stor->servername, path, strlen(path)+1);
267           init_unistr2(&stor->sharename,  p+1, strlen(p+1)+1);
268           stor->ptr_servername = stor->ptr_sharename = 1;
269         }
270     }
271   return True;
272 }
273
274 static WERROR init_reply_dfs_ctr(TALLOC_CTX *ctx, uint32 level, 
275                                    DFS_INFO_CTR* ctr, struct junction_map* jn,
276                                    int num_jn)
277 {
278   /* do the levels */
279   switch(level)
280     {
281     case 1:
282       {
283         DFS_INFO_1* dfs1;
284         dfs1 = (DFS_INFO_1*) talloc(ctx, num_jn * sizeof(DFS_INFO_1));
285         if (!dfs1)
286                 return WERR_NOMEM;
287         init_reply_dfs_info_1(jn, dfs1, num_jn);
288         ctr->dfs.info1 = dfs1;
289         break;
290       }
291     case 2:
292       {
293         DFS_INFO_2* dfs2;
294         dfs2 = (DFS_INFO_2*) talloc(ctx, num_jn * sizeof(DFS_INFO_2));
295         if (!dfs2)
296                 return WERR_NOMEM;
297         init_reply_dfs_info_2(jn, dfs2, num_jn);
298         ctr->dfs.info2 = dfs2;
299         break;
300       }
301     case 3:
302       {
303         DFS_INFO_3* dfs3;
304         dfs3 = (DFS_INFO_3*) talloc(ctx, num_jn * sizeof(DFS_INFO_3));
305         if (!dfs3)
306                 return WERR_NOMEM;
307         init_reply_dfs_info_3(ctx, jn, dfs3, num_jn);
308         ctr->dfs.info3 = dfs3;
309         break;
310       }
311         default:
312                 return WERR_INVALID_PARAM;
313     }
314   return WERR_OK;
315 }
316       
317 WERROR _dfs_enum(pipes_struct *p, DFS_Q_DFS_ENUM *q_u, DFS_R_DFS_ENUM *r_u)
318 {
319   uint32 level = q_u->level;
320   struct junction_map jn[MAX_MSDFS_JUNCTIONS];
321   int num_jn = 0;
322
323   num_jn = enum_msdfs_links(jn);
324   
325   DEBUG(5,("make_reply_dfs_enum: %d junctions found in Dfs, doing level %d\n", num_jn, level));
326
327   r_u->ptr_buffer = level;
328   r_u->level = r_u->level2 = level;
329   r_u->ptr_num_entries = r_u->ptr_num_entries2 = 1;
330   r_u->num_entries = r_u->num_entries2 = num_jn;
331   r_u->reshnd.ptr_hnd = 1;
332   r_u->reshnd.handle = num_jn;
333   
334   r_u->ctr = (DFS_INFO_CTR*)talloc(p->mem_ctx, sizeof(DFS_INFO_CTR));
335   if (!r_u->ctr)
336     return WERR_NOMEM;
337   ZERO_STRUCTP(r_u->ctr);
338   r_u->ctr->switch_value = level;
339   r_u->ctr->num_entries = num_jn;
340   r_u->ctr->ptr_dfs_ctr = 1;
341   
342   r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, r_u->ctr, jn, num_jn);
343
344   return r_u->status;
345 }
346       
347 WERROR _dfs_get_info(pipes_struct *p, DFS_Q_DFS_GET_INFO *q_u, 
348                      DFS_R_DFS_GET_INFO *r_u)
349 {
350   UNISTR2* uni_path = &q_u->uni_path;
351   uint32 level = q_u->level;
352   pstring path;
353   struct junction_map jn;
354
355   unistr2_to_ascii(path, uni_path, sizeof(path)-1);
356   if(!create_junction(path, &jn))
357      return WERR_DFS_NO_SUCH_SERVER;
358   
359   if(!get_referred_path(&jn))
360     return WERR_DFS_NO_SUCH_VOL;
361
362   r_u->level = level;
363   r_u->ptr_ctr = 1;
364   r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, &r_u->ctr, &jn, 1);
365   
366   return r_u->status;
367 }