Esempio n. 1
0
ni_status ni2_create(void *domain, char *pathname)
{
    /* make a directory with the given pathname */
    /* do nothing if the directory already exists */

    ni_status ret;
    ni_id dir;

    /* need to be talking to the master */
    ni_needwrite(domain, 1);

    /* see if it already exists */
    ret = ni2_pathsearch(domain, &dir, pathname);
    if (ret == NI_OK) return NI_OK;

    /* doesn't exist: create it */
    ret = ni_root(domain, &dir);
    if (ret != NI_OK) return ret;

    if (pathname[0] == '/') ret = ni2_createpath(domain, &dir, pathname+1);
    else ret = ni2_createpath(domain, &dir, pathname);

    return ret; 
}
Esempio n. 2
0
static int
is_root_on_master(void *d)
{
	int             uid;
	char            myhostname[MAXHOSTNAMELEN + 1];
	char           *p;
	ni_index        where;
	ni_proplist     pl;
	int             status;
	ni_id           dir;
	struct sockaddr_in addr;
	char           *tag;

	uid = getuid();
	if (uid != 0)
		return 0;

	gethostname(myhostname, MAXHOSTNAMELEN);
	p = strchr(myhostname, '.');
	if (p != NULL)
		*p = '\0';

	status = ni_root(d, &dir);
	if (status != NI_OK)
		return 0;

	status = ni_read(d, &dir, &pl);
	if (status != NI_OK)
		return 0;

	where = ni_proplist_match(pl, "master", NULL);
	if (where == NI_INDEX_NULL)
	{
		ni_proplist_free(&pl);
		return 0;
	}
	if (pl.ni_proplist_val[where].nip_val.ni_namelist_len == 0)
	{
		ni_proplist_free(&pl);
		fprintf(stderr, "No value for NetInfo master property\n");
		return 0;
	}
	p = strchr(pl.ni_proplist_val[where].nip_val.ni_namelist_val[0], '/');
	if (p != NULL)
		*p = '\0';

	p = strchr(pl.ni_proplist_val[where].nip_val.ni_namelist_val[0], '.');
	if (p != NULL)
		*p = '\0';

	if (!strcmp(pl.ni_proplist_val[where].nip_val.ni_namelist_val[0], myhostname))
	{
		ni_proplist_free(&pl);
		return 1;
	}
	if (!strcmp(pl.ni_proplist_val[where].nip_val.ni_namelist_val[0], "localhost"))
	{
		ni_proplist_free(&pl);
		ni_addrtag(d, &addr, &tag);
		if (sys_ismyaddress(addr.sin_addr.s_addr))
			return 1;
	}
	ni_proplist_free(&pl);
	return 0;
}
Esempio n. 3
0
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;
}