This commit was manufactured by cvs2svn to create branch 'SAMBA_TNG'.
[Samba.git] / source / smbd / dfs.c
blob55433f2c47974dd56720c92f3e2074b19274de00
2 #include "includes.h"
4 extern int DEBUGLEVEL;
6 dfs_internal dfs_struct;
7 extern pstring global_myname;
9 /****************************************************************************
10 read a line and split it
11 ****************************************************************************/
12 static BOOL parse_dfs_text_entry(char *line, dfs_internal_table *buf)
14 #define MAXTOK 4
15 char *tok[MAXTOK+1];
16 int count = 0;
18 tok[count] = strtok(line,":");
20 /* strip the comment lines */
21 if (tok[0][0] == '#') return (False);
22 count++;
24 while ( ((tok[count] = strtok(NULL,":")) != NULL ) && count < MAXTOK)
26 count++;
29 DEBUG(7,("Found [%d] tokens\n", count));
31 if (count > 1) {
32 StrnCpy(buf->localpath, tok[0], sizeof(buf->localpath)-1);
33 StrnCpy(buf->sharename, tok[1], sizeof(buf->sharename)-1);
35 strupper(buf->localpath);
36 strupper(buf->sharename);
38 buf->localpath_length = strlen(buf->localpath);
39 buf->sharename_length = strlen(buf->sharename);
41 else
42 return (False);
44 if (count > 2)
45 buf->proximity = atoi(tok[2]);
46 else
47 buf->proximity = 0;
49 if (count > 3)
50 buf->type = atoi(tok[3]);
51 else
52 buf->type = 2;
54 DEBUGADD(7,("localpath: [%s]\n", buf->localpath));
55 DEBUGADD(7,("sharename: [%s]\n", buf->sharename));
56 return True;
59 /****************************************************************************
60 mangle the localpath and store it.
61 ****************************************************************************/
62 static void mangle_dfs_path(dfs_internal_table *buf)
64 char *p;
65 char *mp;
66 char *q;
67 int mlen;
69 fstring temp;
71 p = buf->localpath;
72 mp = buf->mangledpath;
73 mlen = sizeof(buf->mangledpath);
75 ZERO_STRUCTP(mp);
76 DEBUG(2, ("DFS name is: [%s]\n", buf->localpath));
78 /* copy the head: \server-name\ */
79 q = strchr(p + 1, '\\');
80 safe_strcpy(mp, p, mlen);
81 p = q + 1;
83 while (q != NULL)
85 q = strchr(p, '\\');
87 safe_strcpy(temp, p, sizeof(temp));
89 if (!is_8_3(temp, True))
91 mangle_name_83(temp);
94 safe_strcat(mp, temp, mlen);
96 if (q != NULL)
98 safe_strcat(mp, "\\", mlen);
100 p = q + 1;
104 strupper(mp);
106 buf->mangledpath_length = strlen(mp);
107 DEBUGADD(2, ("DFS mangled name is: [%s]\n", mp));
110 /****************************************************************************
111 initialisation de la table dfs en memoire au demarrage de samba
112 ****************************************************************************/
113 BOOL init_dfs_table(void)
115 char *file = lp_dfs_map();
116 int num_lines = 0;
117 int total = 0;
118 FILE *f;
119 pstring line;
120 int i;
122 dfs_internal_table *entry;
124 entry = NULL;
125 dfs_struct.ready = False;
127 if (*file == '\0') {
128 DEBUG(0,("No DFS map, Samba is running in NON DFS mode\n"));
129 return False;
132 f = sys_fopen(file, "r");
133 if (!f) {
134 DEBUG(0,("No DFS map file, Samba is running in NON DFS mode\n"));
135 return False;
138 while ( fgets(line, sizeof(pstring), f) )
140 entry = Realloc(entry,sizeof(dfs_internal_table)*(total+1));
141 if (! entry)
143 total = 0;
144 break;
147 if ( parse_dfs_text_entry(line, &(entry[total]) ) )
149 total++;
151 num_lines++;
153 dfs_struct.size = total;
154 dfs_struct.table = entry;
155 fclose(f);
157 /* we have the file in memory */
158 /* now initialise the mangled names */
159 for (i = 0; i < total; i++)
161 mangle_dfs_path(&(entry[i]));
164 dfs_struct.ready = True;
165 DEBUG(0,("Samba is DFS aware now!\n"));
166 return True;
169 static BOOL check_dfs_path(int search_len, const char *search_path,
170 int path_len, const char* fullpath,
171 const char* sharename,
172 const char* share_path, size_t share_len,
173 char *localpath, size_t local_plen)
175 if (StrnCaseCmp(search_path, fullpath, search_len) != 0)
177 return False;
180 DEBUG(2,("found one linked to [%s]. share_path: %s\n",
181 sharename, share_path));
183 if (StrnCaseCmp(fullpath, share_path, share_len) == 0)
185 safe_strcpy(localpath, fullpath + share_len, local_plen);
187 else
189 localpath[0] = 0;
192 return True;
195 /****************************************************************************
196 check if a path name is a DFS branch
197 ****************************************************************************/
198 int under_dfs(connection_struct *conn, const char *path,
199 char *local_path, size_t local_plen)
201 fstring fullpath;
202 pstring share_path;
203 int i;
204 int snum;
206 int path_len;
207 int share_len;
209 dfs_internal_table *list = dfs_struct.table;
211 snum = SNUM(conn);
212 snprintf(share_path, sizeof(share_path), "\\%s\\%s",
213 global_myname, lp_servicename(snum));
214 share_len = strlen(share_path);
216 if (path[0] != '\\')
218 snprintf(fullpath, sizeof(fullpath), "%s\\%s",
219 share_path, path);
221 else
223 safe_strcpy(fullpath, path, sizeof(fullpath));
226 path_len = strlen(fullpath);
228 DEBUG(2,("DFS looking for: [%s]\n", fullpath));
230 for (i = 0; i < dfs_struct.size; i++)
232 DEBUG(6,("checking against [%s][%d]\n", list[i].localpath,i));
234 if (check_dfs_path(list[i].localpath_length,
235 list[i].localpath,
236 path_len, fullpath,
237 list[i].sharename,
238 share_path, share_len,
239 local_path, local_plen))
241 return True;
244 if (check_dfs_path(list[i].mangledpath_length,
245 list[i].mangledpath,
246 path_len, fullpath,
247 list[i].sharename,
248 share_path, share_len,
249 local_path, local_plen))
251 return True;
254 return False;