Ejemplo n.º 1
0
afs_int32
AddToOwnerChain(struct ubik_trans *at, afs_int32 gid, afs_int32 oid)
{
    /* add entry designated by gid to owner chain of entry designated by oid */
    register afs_int32 code;
    afs_int32 loc;
    struct prentry tentry;
    struct prentry gentry;
    afs_int32 gloc;

    loc = FindByID(at, oid);
    if (!loc)
	return PRNOENT;
    code = pr_ReadEntry(at, 0, loc, &tentry);
    if (code != 0)
	return PRDBFAIL;
    if (oid == gid) {		/* added it to its own chain */
	tentry.nextOwned = tentry.owned;
	tentry.owned = loc;
    } else {
	gloc = FindByID(at, gid);
	code = pr_ReadEntry(at, 0, gloc, &gentry);
	if (code != 0)
	    return PRDBFAIL;
	gentry.nextOwned = tentry.owned;
	tentry.owned = gloc;
	code = pr_WriteEntry(at, 0, gloc, &gentry);
	if (code != 0)
	    return PRDBFAIL;
    }
    code = pr_WriteEntry(at, 0, loc, &tentry);
    if (code != 0)
	return PRDBFAIL;
    return PRSUCCESS;
}
Ejemplo n.º 2
0
afs_int32
RemoveFromOwnerChain(struct ubik_trans *at, afs_int32 gid, afs_int32 oid)
{
    afs_int32 code;
    afs_int32 nptr;
    struct prentry thisEntry;
    struct prentry thatEntry;
    struct prentry *te;		/* pointer to current (this) entry */
    struct prentry *le;		/* pointer to previous (last) entry */
    afs_int32 loc, lastLoc;

    loc = FindByID(at, oid);
    if (!loc)
	return PRNOENT;
    code = pr_ReadEntry(at, 0, loc, &thisEntry);
    if (code != 0)
	return PRDBFAIL;
    le = &thisEntry;
    lastLoc = 0;
    nptr = thisEntry.owned;
    while (nptr != 0) {
	if (nptr == lastLoc)
	    te = le;
	else {
	    if (&thisEntry == le)
		te = &thatEntry;
	    else
		te = &thisEntry;
	    code = pr_ReadEntry(at, 0, nptr, te);
	    if (code != 0)
		return PRDBFAIL;
	}
	if (te->id == gid) {
	    /* found it */
	    if (lastLoc == 0) {	/* modifying first of chain */
		le->owned = te->nextOwned;
		lastLoc = loc;	/* so we write to correct location */
	    } else
		le->nextOwned = te->nextOwned;
	    te->nextOwned = 0;
	    if (te != le) {
		code = pr_WriteEntry(at, 0, nptr, te);
		if (code != 0)
		    return PRDBFAIL;
	    }
	    code = pr_WriteEntry(at, 0, lastLoc, le);
	    if (code != 0)
		return PRDBFAIL;
	    return PRSUCCESS;
	}
	lastLoc = nptr;
	le = te;
	nptr = te->nextOwned;
    }
    return PRSUCCESS;		/* already removed? */
}
Ejemplo n.º 3
0
afs_int32
RemoveFromOrphan(struct ubik_trans *at, afs_int32 gid)
{
    /* remove gid from the orphan list */
    afs_int32 code;
    afs_int32 loc;
    afs_int32 nptr;
    struct prentry tentry;
    struct prentry bentry;

    loc = FindByID(at, gid);
    if (!loc)
	return PRNOENT;
    code = pr_ReadEntry(at, 0, loc, &tentry);
    if (code != 0)
	return PRDBFAIL;
    if (cheader.orphan == htonl(loc)) {
	cheader.orphan = htonl(tentry.nextOwned);
	tentry.nextOwned = 0;
	code =
	    pr_Write(at, 0, 32, (char *)&cheader.orphan,
		     sizeof(cheader.orphan));
	if (code != 0)
	    return PRDBFAIL;
	code = pr_WriteEntry(at, 0, loc, &tentry);
	if (code != 0)
	    return PRDBFAIL;
	return PRSUCCESS;
    }
    nptr = ntohl(cheader.orphan);
    memset(&bentry, 0, sizeof(bentry));
    loc = 0;
    while (nptr != 0) {
	code = pr_ReadEntry(at, 0, nptr, &tentry);
	if (code != 0)
	    return PRDBFAIL;
	if (gid == tentry.id) {
	    /* found it */
	    bentry.nextOwned = tentry.nextOwned;
	    tentry.nextOwned = 0;
	    code = pr_WriteEntry(at, 0, loc, &bentry);
	    if (code != 0)
		return PRDBFAIL;
	    code = pr_WriteEntry(at, 0, nptr, &tentry);
	    if (code != 0)
		return PRDBFAIL;
	    return PRSUCCESS;
	}
	loc = nptr;
	nptr = tentry.nextOwned;
	memcpy(&bentry, &tentry, sizeof(tentry));
    }
    return PRSUCCESS;
}
Ejemplo n.º 4
0
afs_int32
AddToNameHash(struct ubik_trans *tt, char *aname, afs_int32 loc)
{
    /* add to name hash */
    afs_int32 code;
    afs_int32 i;
    struct prentry tentry;

    i = NameHash(aname);
    memset(&tentry, 0, sizeof(tentry));
    code = pr_ReadEntry(tt, 0, loc, &tentry);
    if (code)
	return PRDBFAIL;
    tentry.nextName = ntohl(cheader.nameHash[i]);
    cheader.nameHash[i] = htonl(loc);
    code = pr_WriteEntry(tt, 0, loc, &tentry);
    if (code)
	return PRDBFAIL;
    code =
	pr_Write(tt, 0, 72 + i * 4, (char *)&cheader.nameHash[i],
		 sizeof(cheader.nameHash[i]));
    if (code)
	return PRDBFAIL;
    return PRSUCCESS;
}
Ejemplo n.º 5
0
afs_int32
AddToIDHash(struct ubik_trans *tt, afs_int32 aid, afs_int32 loc)
{
    /* add entry at loc designated by aid to id hash table */
    afs_int32 code;
    afs_int32 i;
    struct prentry tentry;

    if ((aid == PRBADID) || (aid == 0))
	return PRINCONSISTENT;
    i = IDHash(aid);
    memset(&tentry, 0, sizeof(tentry));
    code = pr_ReadEntry(tt, 0, loc, &tentry);
    if (code)
	return PRDBFAIL;
    tentry.nextID = ntohl(cheader.idHash[i]);
    cheader.idHash[i] = htonl(loc);
    code = pr_WriteEntry(tt, 0, loc, &tentry);
    if (code)
	return PRDBFAIL;
    code =
	pr_Write(tt, 0, 72 + HASHSIZE * 4 + i * 4, (char *)&cheader.idHash[i],
		 sizeof(cheader.idHash[i]));
    if (code)
	return PRDBFAIL;
    return PRSUCCESS;
}
Ejemplo n.º 6
0
afs_int32
RemoveFromIDHash(struct ubik_trans *tt, afs_int32 aid, afs_int32 *loc)		/* ??? in case ID hashed twice ??? */
{
    /* remove entry designated by aid from id hash table */
    afs_int32 code;
    afs_int32 current, trail, i;
    struct prentry tentry;
    struct prentry bentry;

    if ((aid == PRBADID) || (aid == 0))
	return PRINCONSISTENT;
    i = IDHash(aid);
    current = ntohl(cheader.idHash[i]);
    memset(&tentry, 0, sizeof(tentry));
    memset(&bentry, 0, sizeof(bentry));
    trail = 0;
    if (current == 0)
	return PRSUCCESS;	/* already gone */
    code = pr_ReadEntry(tt, 0, current, &tentry);
    if (code)
	return PRDBFAIL;
    while (aid != tentry.id) {
	osi_Assert(trail != current);
	trail = current;
	current = tentry.nextID;
	if (current == 0)
	    break;
	code = pr_ReadEntry(tt, 0, current, &tentry);
	if (code)
	    return PRDBFAIL;
    }
    if (current == 0)
	return PRSUCCESS;	/* we didn't find him, so he's already gone */
    if (trail == 0) {
	/* it's the first entry! */
	cheader.idHash[i] = htonl(tentry.nextID);
	code =
	    pr_Write(tt, 0, 72 + HASHSIZE * 4 + i * 4,
		     (char *)&cheader.idHash[i], sizeof(cheader.idHash[i]));
	if (code)
	    return PRDBFAIL;
    } else {
	code = pr_ReadEntry(tt, 0, trail, &bentry);
	if (code)
	    return PRDBFAIL;
	bentry.nextID = tentry.nextID;
	code = pr_WriteEntry(tt, 0, trail, &bentry);
	if (code)
	    return PRDBFAIL;
    }
    *loc = current;
    return PRSUCCESS;
}
Ejemplo n.º 7
0
afs_int32
RemoveFromNameHash(struct ubik_trans *tt, char *aname, afs_int32 *loc)
{
    /* remove from name hash */
    afs_int32 code;
    afs_int32 current, trail, i;
    struct prentry tentry;
    struct prentry bentry;

    i = NameHash(aname);
    current = ntohl(cheader.nameHash[i]);
    memset(&tentry, 0, sizeof(tentry));
    memset(&bentry, 0, sizeof(bentry));
    trail = 0;
    if (current == 0)
	return PRSUCCESS;	/* already gone */
    code = pr_ReadEntry(tt, 0, current, &tentry);
    if (code)
	return PRDBFAIL;
    while (strcmp(aname, tentry.name)) {
	osi_Assert(trail != current);
	trail = current;
	current = tentry.nextName;
	if (current == 0)
	    break;
	code = pr_ReadEntry(tt, 0, current, &tentry);
	if (code)
	    return PRDBFAIL;
    }
    if (current == 0)
	return PRSUCCESS;	/* we didn't find him, already gone */
    if (trail == 0) {
	/* it's the first entry! */
	cheader.nameHash[i] = htonl(tentry.nextName);
	code =
	    pr_Write(tt, 0, 72 + i * 4, (char *)&cheader.nameHash[i],
		     sizeof(cheader.nameHash[i]));
	if (code)
	    return PRDBFAIL;
    } else {
	code = pr_ReadEntry(tt, 0, trail, &bentry);
	if (code)
	    return PRDBFAIL;
	bentry.nextName = tentry.nextName;
	code = pr_WriteEntry(tt, 0, trail, &bentry);
	if (code)
	    return PRDBFAIL;
    }
    *loc = current;
    return PRSUCCESS;
}
Ejemplo n.º 8
0
afs_int32
FreeBlock(struct ubik_trans *at, afs_int32 pos)
{
    /* add a block of storage to the free list */
    afs_int32 code;
    struct prentry tentry;

    memset(&tentry, 0, sizeof(tentry));
    tentry.next = ntohl(cheader.freePtr);
    tentry.flags |= PRFREE;
    cheader.freePtr = htonl(pos);
    code =
	pr_Write(at, 0, 8, (char *)&cheader.freePtr, sizeof(cheader.freePtr));
    if (code != 0)
	return code;
    code = pr_WriteEntry(at, 0, pos, &tentry);
    if (code != 0)
	return code;
    return PRSUCCESS;
}
Ejemplo n.º 9
0
afs_int32
AddToOrphan(struct ubik_trans *at, afs_int32 gid)
{
    afs_int32 code;
    afs_int32 loc;
    struct prentry tentry;

    loc = FindByID(at, gid);
    if (!loc)
	return PRNOENT;
    code = pr_ReadEntry(at, 0, loc, &tentry);
    if (code != 0)
	return PRDBFAIL;
    tentry.nextOwned = ntohl(cheader.orphan);
    code = set_header_word(at, orphan, htonl(loc));
    if (code != 0)
	return PRDBFAIL;
    tentry.owner = 0;		/* so there's no confusion later */
    code = pr_WriteEntry(at, 0, loc, &tentry);
    if (code != 0)
	return PRDBFAIL;
    return PRSUCCESS;
}
Ejemplo n.º 10
0
static int
CommandProc(struct cmd_syndesc *a_as, void *arock)
{
    int i;
    long code = 0;
    long upos;
    long gpos = 0;
    struct prentry uentry, gentry;
    struct ubik_hdr *uh;
    char *dfile = 0;
    const char *pbase = AFSDIR_SERVER_PRDB_FILEPATH;
    char *pfile = NULL;
    char pbuffer[1028];
    struct cmd_parmdesc *tparm;

    tparm = a_as->parms;

    if (tparm[0].items) {
	wflag++;
	/* so we are treated as admin and can create "mis"owned
	   groups */
	pr_noAuth = 1;
    }
    if (tparm[1].items) {
	flags |= DO_USR;
    }
    if (tparm[2].items) {
	flags |= DO_GRP;
    }
    if (tparm[3].items) {
	flags |= (DO_GRP | DO_MEM);
    }
    if (tparm[4].items) {
	nflag++;
    }
    if (tparm[5].items) {
	flags |= DO_SYS;
    }
    if (tparm[6].items) {
	flags |= DO_OTR;
    }
    if (tparm[7].items) {
	pfile = tparm[7].items->data;
    }
    if (tparm[8].items) {
	dfile = tparm[8].items->data;
    }

    if (pfile == NULL) {
        snprintf(pbuffer, sizeof(pbuffer), "%s.DB0", pbase);
        pfile = pbuffer;
    }
    if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
	< 0) {
	fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
		strerror(errno));
	exit(1);
    }
    if (read(dbase_fd, buffer, HDRSIZE) < 0) {
	fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
		strerror(errno));
	exit(1);
    }

    if (dfile) {
	if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
	    fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
		    strerror(errno));
	    exit(1);
	}
    } else
	dfp = (wflag ? stdin : stdout);

    uh = (struct ubik_hdr *)buffer;
    if (ntohl(uh->magic) != UBIK_MAGIC)
	fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
		pfile, ntohl(uh->magic), UBIK_MAGIC);
    memcpy(&uv, &uh->version, sizeof(struct ubik_version));

    if (wflag && ntohl(uv.epoch) == 0 && ntohl(uv.counter) == 0) {
	uv.epoch = htonl(2); /* a ubik version of 0 or 1 has special meaning */
	memcpy(&uh->version, &uv, sizeof(struct ubik_version));
	lseek(dbase_fd, 0, SEEK_SET);
	if (write(dbase_fd, buffer, HDRSIZE) < 0) {
	    fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
		    pfile, strerror(errno));
	    exit(1);
	}
    }

    /* Now that any writeback is done, swap these */
    uv.epoch = ntohl(uv.epoch);
    uv.counter = ntohl(uv.counter);

    fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
    if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
	fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
		strerror(errno));
	exit(1);
    }

    Initdb();
    initialize_PT_error_table();

    if (wflag) {
	struct usr_list *u;
	int seenGroup = 0, id = 0, flags = 0;

	while (fgets(buffer, sizeof(buffer), dfp)) {
	    int oid, cid, quota, uid;
	    char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];

	    if (isspace(*buffer)) {
		code = sscanf(buffer, "%s %d", mem, &uid);
		if (code != 2) {
		    fprintf(stderr,
			    "Insuffient data provided for group membership\n");
		    exit(1);
		}

		if (!seenGroup) {
		    fprintf(stderr,
			    "Group member %s listed outside of group\n",
			    mem);
		    exit(1);
		}

		for (u = usr_head; u; u = u->next)
		    if (u->uid && u->uid == uid)
			break;
		if (u) {
		    /* Add user - deferred because it is probably foreign */
		    u->uid = 0;
		    if (FindByID(0, uid))
			code = PRIDEXIST;
		    else {
			if (!code
			    && (flags & (PRGRP | PRQUOTA)) ==
			    (PRGRP | PRQUOTA)) {
			    gentry.ngroups++;
			    code = pr_WriteEntry(0, 0, gpos, &gentry);
			    if (code)
				fprintf(stderr,
					"Error setting group count on %s: %s\n",
					name, afs_error_message(code));
			}
			code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
					   1 /*gflag */ ,
					   SYSADMINID /*oid */ ,
					   SYSADMINID /*cid */ );
		    }
		    if (code)
			fprintf(stderr, "Error while creating %s: %s\n",
				u->name, afs_error_message(code));
		    continue;
		}
		/* Add user to group */
		if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
		    code = PRPERM;
		} else if ((upos = FindByID(0, uid))
			   && (gpos = FindByID(0, id))) {
		    code = pr_ReadEntry(0, 0, upos, &uentry);
		    if (!code)
			code = pr_ReadEntry(0, 0, gpos, &gentry);
		    if (!code)
			code = AddToEntry(0, &gentry, gpos, uid);
		    if (!code)
			code = AddToEntry(0, &uentry, upos, id);
		} else
		    code = PRNOENT;

		if (code)
		    fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
			    name, afs_error_message(code));
	    } else {
		code = sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, &quota, &id,
			      &oid, &cid);
		if (code != 6) {
		    fprintf(stderr,
			    "Insufficient data provided for user/group\n");
		    exit(1);
		}

		seenGroup = 1;

		if (FindByID(0, id))
		    code = PRIDEXIST;
		else
		    code = CreateEntry(0, name, &id, 1 /*idflag */ ,
				       flags & PRGRP, oid, cid);
		if (code == PRBADNAM) {
		    u = malloc(sizeof(struct usr_list));
		    u->next = usr_head;
		    u->uid = id;
		    strcpy(u->name, name);
		    usr_head = u;
		} else if (code) {
		    fprintf(stderr, "Error while creating %s: %s\n", name,
			    afs_error_message(code));
		} else if ((flags & PRACCESS)
			   || (flags & (PRGRP | PRQUOTA)) ==
			   (PRGRP | PRQUOTA)) {
		    gpos = FindByID(0, id);
		    code = pr_ReadEntry(0, 0, gpos, &gentry);
		    if (!code) {
			gentry.flags = flags;
			gentry.ngroups = quota;
			code = pr_WriteEntry(0, 0, gpos, &gentry);
		    }
		    if (code)
			fprintf(stderr,
				"Error while setting flags on %s: %s\n", name,
				afs_error_message(code));
		}
	    }
	}
	for (u = usr_head; u; u = u->next)
	    if (u->uid)
		fprintf(stderr, "Error while creating %s: %s\n", u->name,
			afs_error_message(PRBADNAM));
    } else {
	for (i = 0; i < HASHSIZE; i++) {
	    upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
	    while (upos) {
		long newpos;
		newpos = display_entry(upos);
		if (newpos == upos) {
		    fprintf(stderr, "pt_util: hash error in %s chain %d\n",
			    nflag ? "name":"id", i);
		    exit(1);
		} else
		    upos = newpos;
	    }
	}
	if (flags & DO_GRP)
	    display_groups();
    }

    lseek(dbase_fd, 0, L_SET);	/* rewind to beginning of file */
    if (read(dbase_fd, buffer, HDRSIZE) < 0) {
	fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
		strerror(errno));
	exit(1);
    }
    uh = (struct ubik_hdr *)buffer;

    uh->version.epoch = ntohl(uh->version.epoch);
    uh->version.counter = ntohl(uh->version.counter);

    if ((uh->version.epoch != uv.epoch)
	|| (uh->version.counter != uv.counter)) {
	fprintf(stderr,
		"pt_util: Ubik Version number changed during execution.\n");
	fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
		uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
    }
    close(dbase_fd);
    exit(0);
}