2 * DNS referrals give two main fields: the path to connect to in
3 * /Netbios-host-name/share-name/path... form and a network
4 * address of how to find this path of the form domain.dom.
6 * The domain.dom is resolved in XP/Win2k etc using AD to do
7 * a lookup (this is a consensus view, I don't think anyone
8 * has proved it). I cannot do this as AD needs Kerberos and
9 * LDAP which I don't have.
11 * Instead I just use the NetBios names passed in the paths
12 * and assume that the servers are in the same DNS domain as me
13 * and have their DNS hostname set the same as their netbios
14 * called-name; thankfully this always seems to be the case (so far).
16 * I have not added support for starting another instance of
17 * cifs to connect to other servers referenced in DFS links,
18 * this is not a problem for me and I think it hides a load
19 * of problems of its own wrt plan9's private namespaces.
21 * The proximity of my test server (AD enabled) is always 0 but some
22 * systems may report more meaningful values. The expiry time is
23 * similarly zero, so I guess at 5 mins.
25 * If the redirection points to a "hidden" share (i.e., its name
26 * ends in a $) then the type of the redirection is 0 (unknown) even
27 * though it is a CIFS share.
29 * It would be nice to add a check for which subnet a server is on
30 * so our first choice is always the the server on the same subnet
31 * as us which replies to a ping (i.e., is up). This could short-
32 * circuit the tests as the a server on the same subnet will always
33 * be the fastest to get to.
35 * If I set Flags2_DFS then I don't see DFS links, I just get
36 * path not found (?!).
38 * If I do a QueryFileInfo of a DFS link point (IE when I'am doing a walk)
39 * Then I just see a directory, its not until I try to walk another level
40 * That I get "IO reparse tag not handled" error rather than
43 * If I check the extended attributes of the QueryFileInfo in walk() then I can
44 * see this is a reparse point and so I can get the referral. The only
45 * problem here is that samba and the like may not support this.
57 Nomatch, /* not found in cache */
58 Exactmatch, /* perfect match found */
59 Badmatch /* matched but wrong case */
62 #define SINT_MAX 0x7fffffff
64 typedef struct Dfscache Dfscache;
66 Dfscache*next; /* next entry */
71 long expiry; /* expiry time in sec */
72 long rtt; /* round trip time, nsec */
73 int prox; /* proximity, lower = closer */
84 for(cp = Cache; cp; cp = cp->next){
85 ex = cp->expiry - time(nil);
88 fmtprint(f, "%-42s %6ld %8.1f %4d %-16s %-24s %s\n",
89 cp->src, ex, (double)cp->rtt/1000.0L, cp->prox,
90 cp->host, cp->share, cp->path);
99 static char name[128];
101 strncpy(name, s, sizeof(name));
102 name[sizeof(name)-1] = 0;
103 if((p = strrchr(name, '$')) != nil && p[1] == 0)
109 lookup(char *path, int *match)
120 for(cp = Cache; cp; cp = cp->next){
124 if(strncmp(path, cp->src, n) != 0)
126 if(path[n] != 0 && path[n] != '/')
145 static char npath[MAX_DFS_PATH];
148 if((cp = lookup(path, &exact)) != nil){
149 snprint(npath, sizeof npath, "/%s%s%s%s", cp->share,
150 *cp->path? "/": "", cp->path, path + strlen(cp->src));
154 if((p = strchr(path+1, '/')) == nil)
156 if(Debug && strstr(Debug, "dfs") != nil)
157 print("mapfile src=%q => dst=%q\n", opath, p);
162 mapshare(char *path, Share **osp)
168 char *tail[] = { "", "$" };
170 if((cp = lookup(path, nil)) == nil)
173 for(sp = Shares; sp < Shares+Nshares; sp++){
174 s = trimshare(sp->name);
175 if(cistrcmp(cp->share, s) != 0)
177 if(Checkcase && strcmp(cp->share, s) != 0)
179 if(Debug && strstr(Debug, "dfs") != nil)
180 print("mapshare, already connected, src=%q => dst=%q\n", path, sp->name);
185 * Try to autoconnect to share if it is not known. Note even if you
186 * didn't specify any shares and let the system autoconnect you may
187 * not already have the share you need as RAP (which we use) throws
188 * away names > 12 chars long. If we where to use RPC then this block
189 * of code would be less important, though it would still be useful
190 * to catch Shares added since cifs(1) was started.
192 sp = Shares + Nshares;
193 for(i = 0; i < 2; i++){
194 try = smprint("%s%s", cp->share, tail[i]);
195 if(CIFStreeconnect(Sess, Sess->cname, try, sp) == 0){
199 if(Debug && strstr(Debug, "dfs") != nil)
200 print("mapshare connected, src=%q dst=%q\n",
207 if(Debug && strstr(Debug, "dfs") != nil)
208 print("mapshare failed src=%s\n", path);
209 werrstr("not found");
214 * Rtt_tol is the fractional tollerance for RTT comparisons.
215 * If a later (further down the list) host's RTT is less than
216 * 1/Rtt_tol better than my current best then I don't bother
217 * with it. This biases me towards entries at the top of the list
218 * which Active Directory has already chosen for me and prevents
219 * noise in RTTs from pushing me to more distant machines.
222 remap(Dfscache *cp, Refer *re)
235 if(Debug && strstr(Debug, "dfs") != nil)
236 print(" remap %s\n", re->addr);
238 for(p = re->addr; *p; p++)
242 if(cp->prox < re->prox){
243 if(Debug && strstr(Debug, "dfs") != nil)
244 print(" remap %d < %d\n", cp->prox, re->prox);
247 if((n = getfields(re->addr, a, sizeof(a), 0, "/")) < 3){
248 if(Debug && strstr(Debug, "dfs") != nil)
249 print(" remap nfields=%d\n", n);
252 if((rtt = ping(a[Hostname], Dfstout)) == -1){
253 if(Debug && strstr(Debug, "dfs") != nil)
254 print(" remap ping failed\n");
257 if(cp->rtt < rtt && (rtt/labs(rtt-cp->rtt)) < Rtt_tol){
258 if(Debug && strstr(Debug, "dfs") != nil)
259 print(" remap bad ping %ld < %ld && %ld < %d\n",
260 cp->rtt, rtt, (rtt/labs(rtt-cp->rtt)), Rtt_tol);
274 cp->expiry = time(nil)+re->ttl;
275 cp->host = estrdup9p(a[Hostname]);
276 cp->share = estrdup9p(trimshare(a[Sharename]));
277 cp->path = estrdup9p(a[Pathname]);
278 if(Debug && strstr(Debug, "dfs") != nil)
279 print(" remap ping OK prox=%d host=%s share=%s path=%s\n",
280 cp->prox, cp->host, cp->share, cp->path);
285 redir1(Session *s, char *path, Dfscache *cp, int level)
287 Refer retab[16], *re;
288 int n, gflags, used, found;
293 if((n = T2getdfsreferral(s, &Ipc, path, &gflags, &used, retab,
294 nelem(retab))) == -1)
297 if(! (gflags & DFS_HEADER_ROOT))
301 for(re = retab; re < retab+n; re++){
302 if(Debug && strstr(Debug, "dfs") != nil)
303 print("referal level=%d prox=%d path=%q addr=%q\n",
304 level, re->prox, re->path, re->addr);
306 if(gflags & DFS_HEADER_STORAGE){
307 if(remap(cp, re) == 0)
310 if(redir1(s, re->addr, cp, level+1) != -1) /* ???? */
317 if(Debug && strstr(Debug, "dfs") != nil)
318 print("referal level=%d path=%q found=%d used=%d\n",
319 level, path, found, used);
326 * We can afford to ignore the used count returned by redir
327 * because of the semantics of 9p - we always walk to directories
328 * ome and we a time and we always walk before any other file operations
331 redirect(Session *s, Share *sp, char *path)
337 if(Debug && strstr(Debug, "dfs") != nil)
338 print("redirect name=%q path=%q\n", sp->name, path);
340 cp = lookup(path, &match);
341 if(match == Badmatch)
344 if(cp && match == Exactmatch){
345 if(cp->expiry >= time(nil)){ /* cache hit */
346 if(Debug && strstr(Debug, "dfs") != nil)
347 print("redirect cache=hit src=%q => share=%q path=%q\n",
348 cp->src, cp->share, cp->path);
351 } else{ /* cache hit, but entry stale */
355 unc = smprint("//%s/%s/%s%s%s", s->auth->windom,
356 cp->share, cp->path, *cp->path? "/": "",
357 path + strlen(cp->src) + 1);
359 sysfatal("no memory: %r");
360 if(redir1(s, unc, cp, 1) == -1){
361 if(Debug && strstr(Debug, "dfs") != nil)
362 print("redirect refresh failed unc=%q\n",
368 if(Debug && strstr(Debug, "dfs") != nil)
369 print("redirect refresh cache=stale src=%q => share=%q path=%q\n",
370 cp->src, cp->share, cp->path);
376 /* in-exact match or complete miss */
378 unc = smprint("//%s/%s/%s%s%s", s->auth->windom, cp->share,
379 cp->path, *cp->path? "/": "", path + strlen(cp->src) + 1);
381 unc = smprint("//%s%s", s->auth->windom, path);
383 sysfatal("no memory: %r");
385 cp = emalloc9p(sizeof(Dfscache));
386 memset(cp, 0, sizeof(Dfscache));
390 if(redir1(s, unc, cp, 1) == -1){
391 if(Debug && strstr(Debug, "dfs") != nil)
392 print("redirect new failed unc=%q\n", unc);
399 cp->src = estrdup9p(path);
402 if(Debug && strstr(Debug, "dfs") != nil)
403 print("redirect cache=miss src=%q => share=%q path=%q\n",
404 cp->src, cp->share, cp->path);