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