Exemplo n.º 1
0
/* This is a multi-purpose funciton for use by either
 * GetNthIdentity or GetNthUser. The parameter 'id' indicates
 * whether we are counting all identities (if true), or just
 * ones which can be represented by the old-style interfaces
 * We return -1 for EOF, 0 for success, and >0 for all errors.
 */
static int
GetNthIdentityOrUser(struct afsconf_dir *dir, int count,
		     struct rx_identity **identity, int id)
{
    bufio_p bp;
    char *tbuffer;
    struct rx_identity fileUser;
    afs_int32 code;

    tbuffer = malloc(AFSDIR_PATH_MAX);
    if (tbuffer == NULL)
	return ENOMEM;

    LOCK_GLOBAL_MUTEX;
    UserListFileName(dir, tbuffer, AFSDIR_PATH_MAX);
    bp = BufioOpen(tbuffer, O_RDONLY, 0);
    if (!bp) {
	UNLOCK_GLOBAL_MUTEX;
	free(tbuffer);
	return -1;
    }
    while (1) {
	code = BufioGets(bp, tbuffer, AFSDIR_PATH_MAX);
	if (code < 0) {
	    code = -1;
	    break;
	}

	code = ParseLine(tbuffer, &fileUser);
	if (code != 0)
	    break;

	if (id || fileUser.kind == RX_ID_KRB4)
	    count--;

	if (count < 0)
	    break;
        else
	    rx_identity_freeContents(&fileUser);
    }
    if (code == 0) {
	*identity = rx_identity_copy(&fileUser);
	rx_identity_freeContents(&fileUser);
    }

    BufioClose(bp);

    UNLOCK_GLOBAL_MUTEX;
    free(tbuffer);
    return code;
}
Exemplo n.º 2
0
int
afsconf_IsSuperIdentity(struct afsconf_dir *adir,
			struct rx_identity *user)
{
    bufio_p bp;
    char tbuffer[1024];
    struct rx_identity fileUser;
    int match;
    afs_int32 code;

    UserListFileName(adir, tbuffer, sizeof tbuffer);
    bp = BufioOpen(tbuffer, O_RDONLY, 0);
    if (!bp)
	return 0;
    match = 0;
    while (!match) {
	code = BufioGets(bp, tbuffer, sizeof(tbuffer));
        if (code < 0)
	    break;

	code = ParseLine(tbuffer, &fileUser);
	if (code != 0)
	   break;

	match = rx_identity_match(user, &fileUser);

	rx_identity_freeContents(&fileUser);
    }
    BufioClose(bp);
    return match;
}
Exemplo n.º 3
0
void
rx_identity_free(struct rx_identity **identity)
{
    rx_identity_freeContents(*identity);
    rxi_Free(*identity, sizeof(struct rx_identity));
    *identity = NULL;
}
Exemplo n.º 4
0
/* This is a multi-purpose funciton for use by either
 * GetNthIdentity or GetNthUser. The parameter 'id' indicates
 * whether we are counting all identities (if true), or just
 * ones which can be represented by the old-style interfaces
 */
static int
GetNthIdentityOrUser(struct afsconf_dir *dir, int count,
		     struct rx_identity **identity, int id)
{
    bufio_p bp;
    char tbuffer[1024];
    struct rx_identity fileUser;
    afs_int32 code;

    LOCK_GLOBAL_MUTEX;
    UserListFileName(dir, tbuffer, sizeof(tbuffer));
    bp = BufioOpen(tbuffer, O_RDONLY, 0);
    if (!bp) {
	UNLOCK_GLOBAL_MUTEX;
	return EIO;
    }
    while (1) {
	code = BufioGets(bp, tbuffer, sizeof(tbuffer));
	if (code < 0)
	    break;

	code = ParseLine(tbuffer, &fileUser);
	if (code != 0)
	    break;

	if (id || fileUser.kind == RX_ID_KRB4)
	    count--;

	if (count < 0)
	    break;
        else
	    rx_identity_freeContents(&fileUser);
    }
    if (code == 0) {
	*identity = rx_identity_copy(&fileUser);
	rx_identity_freeContents(&fileUser);
    }

    BufioClose(bp);

    UNLOCK_GLOBAL_MUTEX;
    return code;
}
Exemplo n.º 5
0
int
afsconf_IsSuperIdentity(struct afsconf_dir *adir,
			struct rx_identity *user)
{
    bufio_p bp;
    char *tbuffer;
    struct rx_identity fileUser;
    int match;
    afs_int32 code;

    tbuffer = malloc(AFSDIR_PATH_MAX);
    if (tbuffer == NULL)
	return 0;

    UserListFileName(adir, tbuffer, AFSDIR_PATH_MAX);
    bp = BufioOpen(tbuffer, O_RDONLY, 0);
    if (!bp) {
	free(tbuffer);
	return 0;
    }
    match = 0;
    while (!match) {
	code = BufioGets(bp, tbuffer, AFSDIR_PATH_MAX);
        if (code < 0)
	    break;

	code = ParseLine(tbuffer, &fileUser);
	if (code != 0)
	   break;

	match = rx_identity_match(user, &fileUser);

	rx_identity_freeContents(&fileUser);
    }
    BufioClose(bp);
    free(tbuffer);
    return match;
}
Exemplo n.º 6
0
int
afsconf_DeleteIdentity(struct afsconf_dir *adir, struct rx_identity *user)
{
    char *filename, *nfilename;
    char *buffer;
    char *copy;
    FILE *tf;
    FILE *nf;
    int flag;
    char *tp;
    int found;
    struct stat tstat;
    struct rx_identity identity;
    afs_int32 code;

    memset(&identity, 0, sizeof(struct rx_identity));

    buffer = malloc(AFSDIR_PATH_MAX);
    if (buffer == NULL)
	return ENOMEM;
    filename = malloc(AFSDIR_PATH_MAX);
    if (filename == NULL) {
	free(buffer);
	return ENOMEM;
    }

    LOCK_GLOBAL_MUTEX;
    UserListFileName(adir, filename, AFSDIR_PATH_MAX);
#ifndef AFS_NT40_ENV
    {
	/*
	 * We attempt to fully resolve this pathname, so that the rename
	 * of the temporary file will work even if UserList is a symlink
	 * into a different filesystem.
	 */
	nfilename = malloc(AFSDIR_PATH_MAX);
	if (nfilename == NULL) {
	    UNLOCK_GLOBAL_MUTEX;
	    free(filename);
	    free(buffer);
	    return ENOMEM;
	}
	if (realpath(filename, nfilename)) {
	    free(filename);
	    filename = nfilename;
	} else {
	    free(nfilename);
	}
    }
#endif /* AFS_NT40_ENV */
    if (asprintf(&nfilename, "%s.NXX", filename) < 0) {
	UNLOCK_GLOBAL_MUTEX;
	free(filename);
	free(buffer);
	return -1;
    }
    tf = fopen(filename, "r");
    if (!tf) {
	UNLOCK_GLOBAL_MUTEX;
	free(filename);
	free(nfilename);
	free(buffer);
	return -1;
    }
    code = stat(filename, &tstat);
    if (code < 0) {
	UNLOCK_GLOBAL_MUTEX;
	free(filename);
	free(nfilename);
	free(buffer);
	return code;
    }
    nf = fopen(nfilename, "w+");
    if (!nf) {
	fclose(tf);
	UNLOCK_GLOBAL_MUTEX;
	free(filename);
	free(nfilename);
	free(buffer);
	return EIO;
    }
    flag = 0;
    found = 0;
    while (1) {
	/* check for our user id */
	tp = fgets(buffer, AFSDIR_PATH_MAX, tf);
	if (tp == NULL)
	    break;

	copy = strdup(buffer);
	if (copy == NULL) {
	    flag = 1;
	    break;
	}
	code = ParseLine(copy, &identity);
	if (code == 0 && rx_identity_match(user, &identity)) {
	    /* found the guy, don't copy to output file */
	    found = 1;
	} else {
	    /* otherwise copy original line to output */
	    fprintf(nf, "%s", buffer);
	}
	free(copy);
	rx_identity_freeContents(&identity);
    }
    fclose(tf);
    free(buffer);
    if (ferror(nf))
	flag = 1;
    if (fclose(nf) == EOF)
	flag = 1;
    if (flag == 0) {
	/* try the rename */
	flag = rk_rename(nfilename, filename);
	if (flag == 0)
	    flag = chmod(filename, tstat.st_mode);
    } else
	unlink(nfilename);

    /* finally, decide what to return to the caller */
    UNLOCK_GLOBAL_MUTEX;
    free(filename);
    free(nfilename);
    if (flag)
	return EIO;		/* something mysterious went wrong */
    if (!found)
	return ENOENT;		/* entry wasn't found, no changes made */
    return 0;			/* everything was fine */
}
Exemplo n.º 7
0
int
afsconf_DeleteIdentity(struct afsconf_dir *adir, struct rx_identity *user)
{
    char tbuffer[1024];
    char nbuffer[1024];
    char *copy;
    FILE *tf;
    FILE *nf;
    int flag;
    char *tp;
    int found;
    struct stat tstat;
    struct rx_identity identity;
    afs_int32 code;

    memset(&identity, 0, sizeof(struct rx_identity));

    LOCK_GLOBAL_MUTEX;
    UserListFileName(adir, tbuffer, sizeof tbuffer);
#ifndef AFS_NT40_ENV
    {
	/*
	 * We attempt to fully resolve this pathname, so that the rename
	 * of the temporary file will work even if UserList is a symlink
	 * into a different filesystem.
	 */
	char resolved_path[1024];

	if (realpath(tbuffer, resolved_path)) {
	    strcpy(tbuffer, resolved_path);
	}
    }
#endif /* AFS_NT40_ENV */
    tf = fopen(tbuffer, "r");
    if (!tf) {
	UNLOCK_GLOBAL_MUTEX;
	return -1;
    }
    code = stat(tbuffer, &tstat);
    if (code < 0) {
	UNLOCK_GLOBAL_MUTEX;
	return code;
    }
    strcpy(nbuffer, tbuffer);
    strcat(nbuffer, ".NXX");
    nf = fopen(nbuffer, "w+");
    if (!nf) {
	fclose(tf);
	UNLOCK_GLOBAL_MUTEX;
	return EIO;
    }
    flag = 0;
    found = 0;
    while (1) {
	/* check for our user id */
	tp = fgets(nbuffer, sizeof(nbuffer), tf);
	if (tp == NULL)
	    break;

	copy = strdup(nbuffer);
	if (copy == NULL) {
	    flag = 1;
	    break;
	}
	code = ParseLine(copy, &identity);
	if (code == 0 && rx_identity_match(user, &identity)) {
	    /* found the guy, don't copy to output file */
	    found = 1;
	} else {
	    /* otherwise copy original line to output */
	    fprintf(nf, "%s", nbuffer);
	}
	free(copy);
	rx_identity_freeContents(&identity);
    }
    fclose(tf);
    if (ferror(nf))
	flag = 1;
    if (fclose(nf) == EOF)
	flag = 1;
    strcpy(nbuffer, tbuffer);
    strcat(nbuffer, ".NXX");	/* generate new file name again */
    if (flag == 0) {
	/* try the rename */
	flag = rk_rename(nbuffer, tbuffer);
	if (flag == 0)
	    flag = chmod(tbuffer, tstat.st_mode);
    } else
	unlink(nbuffer);

    /* finally, decide what to return to the caller */
    UNLOCK_GLOBAL_MUTEX;
    if (flag)
	return EIO;		/* something mysterious went wrong */
    if (!found)
	return ENOENT;		/* entry wasn't found, no changes made */
    return 0;			/* everything was fine */
}