static void * domain_for_user(char *uname, char *locn, ni_id * dir) { char *upath; int status; void *d, *d1; struct sockaddr_in server; char *tag; int bytag; if (uname == NULL) return NULL; /* * Find the user in NetInfo. */ upath = malloc(8 + strlen(uname)); sprintf(upath, "/users/%s", uname); if (locn != NULL) { bytag = 1; if (locn[0] == '/') bytag = 0; else if (!strncmp(locn, "./", 2)) bytag = 0; else if (!strncmp(locn, "../", 3)) bytag = 0; if (bytag == 1) { parse_server_tag(locn, &server, &tag); d = ni_connect(&server, tag); if (d == (void *) NULL) return (void *) NULL; } else status = ni_open(NULL, locn, &d); status = ni_pathsearch(d, dir, upath); free(upath); if (status == NI_OK) return d; ni_free(d); return (void *) NULL; } status = ni_open(NULL, ".", &d); while (status == NI_OK) { status = ni_pathsearch(d, dir, upath); if (status == NI_OK) break; d1 = d; status = ni_open(d1, "..", &d); ni_free(d1); } free(upath); if (status == NI_OK) return d; return (void *) NULL; }
int do_open(char *tool, char *name, void **domain, bool bytag, int timeout, char *user, char *passwd) { /* do an ni_open or an ni_connect, as appropriate */ char *tag; enum ni_parse_status pstatus; ni_status status; struct sockaddr_in server; ni_id rootdir; if (bytag) { /* connect by tag */ /* call a function to parse the input arg */ pstatus = ni_parse_server_tag(name, &server, &tag); if (pstatus != NI_PARSE_OK) { qtss_fprintf(stderr, "%s: incorrect format for domain %s (%s)\n", tool, name, ni_parse_error_string(pstatus)); qtss_fprintf(stderr, "usage: -t <host>/<tag>\n"); qtss_fprintf(stderr, "<host> can be a host name or IP address\n"); return NI_FAILED + 1 + pstatus; } /* connect to the specified server */ *domain = ni_connect(&server, tag); if (*domain == NULL) { qtss_fprintf(stderr, "%s: can't connect to server %s\n", tool, name); free(tag); return NI_FAILED + 1; } } else { /* open domain */ status = ni_open(NULL, name, domain); if (status != NI_OK) { qtss_fprintf(stderr, "%s: can't connect to server for domain %s\n", tool, name); return status; } } /* abort on errors */ ni_setabort(*domain, 1); /* set timeouts */ ni_setreadtimeout(*domain, timeout); ni_setwritetimeout(*domain, timeout); /* authentication */ if (user != NULL) { ni_setuser(*domain, user); if (passwd != NULL) ni_setpassword(*domain, passwd); } /* get the root directory to see if the connection is alive */ status = ni_root(*domain, &rootdir); if (status != NI_OK) { if (bytag) qtss_fprintf(stderr, "%s: can't connect to server %s: %s\n", tool, name, ni_error(status)); else qtss_fprintf(stderr, "%s: can't connect to server for domain %s: %s\n", tool, name, ni_error(status)); return status; } return 0; }