2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * (5) Add functions to process supergroups:
12 * ChangeIDEntry(), RemoveFromSGEntry(),
13 * AddToSGEntry(), GetListSG2().
14 * (6) Add code to existing functions to process
16 * 2/1/98 jjm Add mdw's changes for bit mapping for supergroups
18 * 09/26/02 kwc Move depthsg definition here from ptserver.c since
19 * pt_util needs it defined also, and this file is
20 * common between the two programs.
23 #include <afsconfig.h>
24 #include <afs/param.h>
32 #include <afs/com_err.h>
33 #include <afs/cellconfig.h>
37 #include "ptprototypes.h"
39 /* Foreign cells are represented by the group system:authuser@cell*/
40 #define AUTHUSER_GROUP "system:authuser"
42 extern int restricted
;
43 extern struct ubik_dbase
*dbase
;
44 extern struct afsconf_dir
*prdir
;
47 static int inRange(struct prentry
*cellEntry
, afs_int32 aid
);
48 static afs_int32
allocNextId(struct ubik_trans
*, struct prentry
*);
49 static int AddAuthGroup(struct prentry
*tentry
, prlist
*alist
, afs_int32
*size
);
51 static char *whoami
= "ptserver";
53 int prp_user_default
= PRP_USER_DEFAULT
;
54 int prp_group_default
= PRP_GROUP_DEFAULT
;
56 #if defined(SUPERGROUPS)
60 afs_int32 depthsg
= 5; /* Maximum iterations used during IsAMemberOF */
61 afs_int32
GetListSG2(struct ubik_trans
*at
, afs_int32 gid
, prlist
* alist
,
62 afs_int32
* sizeP
, afs_int32 depth
);
64 struct map
*sg_flagged
;
67 #define NIL_MAP ((struct map *) 0)
69 /* pt_mywrite hooks into the logic that writes ubik records to disk
70 * at a very low level. It invalidates mappings in sg_flagged/sg_found.
71 * By hoooking in at this level, we ensure that a ubik file reload
72 * invalidates our incore cache.
74 * We assert records, cheaders and everything else are 0 mod 64.
75 * So, we can always see the first 64 bytes of any record written.
76 * The stuff we're interested in (flags, id) are in the first 8 bytes.
77 * so we can always tell if we're writing a group record.
80 int (*pt_save_dbase_write
)(struct ubik_dbase
*, afs_int32
, void *, afs_int32
,
84 pt_mywrite(struct ubik_dbase
*tdb
, afs_int32 fno
, void *bp
, afs_int32 pos
, afs_int32 count
)
86 afs_uint32 headersize
= ntohl(cheader
.headerSize
);
88 if (fno
== 0 && pos
+ count
> headersize
) {
103 #define xPT(p,x) ((((struct prentry *)(p))->flags & htonl(PRTYPE)) == htonl(x))
106 fprintf(stderr
, "Writing %d bytes of entry @ %#lx(+%d)\n", c
,
108 else if (c
== ENTRYSIZE
)
110 "Writing %d bytes of entry @ %#lx (%d<%s>,%d)\n",
111 c
, p
, ntohl(((struct prentry
*)cp
)->flags
),
112 xPT(cp
,PRUSER
) ? "user" : xPT(cp
,PRFREE
) ? "free" :
113 xPT(cp
,PRGRP
) ? "group" : xPT(cp
,PRCONT
) ? "cont" :
114 xPT(cp
,PRCELL
) ? "cell" : xPT(cp
,PRFOREIGN
) ? "foreign" :
115 xPT(cp
,PRINST
) ? "sub/super instance" : "?",
116 ntohl(((struct prentry
*)cp
)->id
));
119 "Writing first %d bytes of entry @ %#lx (%d<%s>,%d)\n",
120 c
, p
, ntohl(((struct prentry
*)cp
)->flags
),
121 xPT(cp
,PRUSER
) ? "user" : xPT(cp
,PRFREE
) ? "free" :
122 xPT(cp
,PRGRP
) ? "group" : xPT(cp
,PRCONT
) ? "cont" :
123 xPT(cp
,PRCELL
) ? "cell" : xPT(cp
,PRFOREIGN
) ? "foreign" :
124 xPT(cp
,PRINST
) ? "sub/super instance" : "?",
125 ntohl(((struct prentry
*)cp
)->id
));
127 fprintf(stderr
, "Writing %d bytes of entry @ %#lx\n", c
, p
);
129 if (!o
&& c
>= 8 && xPT(cp
,PRGRP
)) {
131 if (in_map(sg_found
, -ntohl(((struct prentry
*)cp
)->id
)))
132 fprintf(stderr
, "Unfound: Removing group %d\n",
133 ntohl(((struct prentry
*)cp
)->id
));
134 if (in_map(sg_flagged
, -ntohl(((struct prentry
*)cp
)->id
)))
135 fprintf(stderr
, "Unflag: Removing group %d\n",
136 ntohl(((struct prentry
*)cp
)->id
));
140 add_map(NIL_MAP
, -ntohl(((struct prentry
*)cp
)->id
)));
143 add_map(NIL_MAP
, -ntohl(((struct prentry
*)cp
)->id
)));
151 return (*pt_save_dbase_write
) (tdb
, fno
, bp
, pos
, count
);
155 * this function attaches pt_mywrite. It's called once,
156 * just after ubik_ServerInit.
162 extern struct ubik_dbase
*ubik_dbase
;
163 if (ubik_dbase
->write
!= pt_mywrite
) {
164 pt_save_dbase_write
= ubik_dbase
->write
;
165 ubik_dbase
->write
= pt_mywrite
;
169 #endif /* SUPERGROUPS */
171 /* CorrectUserName - Check to make sure a user name is OK. It must not include
172 * either a colon (or it would look like a group) or a newline (which can
173 * confuse some ptdb code, depending on the format we're reading from).
174 * This is a predicate, so it return one if name is OK and zero if name is
178 CorrectUserName(char *name
)
180 /* We accept foreign names, so we will deal with '@' later */
181 if (strchr(name
, ':') || strchr(name
, '\n'))
183 if (strlen(name
) >= PR_MAXNAMELEN
)
188 /* CorrectGroupName - Like the above but handles more complicated cases caused
189 * by including the ownership in the name. The interface works by calculating
190 * the correct name based on a given name and owner. This allows easy use by
191 * rename, which then compares the correct name with the requested new name. */
194 CorrectGroupName(struct ubik_trans
*ut
, char aname
[PR_MAXNAMELEN
], /* name for group */
195 afs_int32 cid
, /* caller id */
196 afs_int32 oid
, /* owner of group */
197 afs_int32 admin
, /* non-zero if admin */
198 char cname
[PR_MAXNAMELEN
]) /* correct name for group */
201 char *prefix
; /* ptr to group owner part */
202 char *suffix
; /* ptr to group name part */
203 char name
[PR_MAXNAMELEN
]; /* correct name for group */
204 struct prentry tentry
;
206 if (strlen(aname
) >= PR_MAXNAMELEN
)
209 /* Determine the correct prefix for the name. */
210 if (oid
== SYSADMINID
)
213 afs_int32 loc
= FindByID(ut
, oid
);
215 /* let admin create groups owned by non-existent ids (probably
216 * setting a group to own itself). Check that they look like
217 * groups (with a colon) or otherwise are good user names. */
219 strcpy(cname
, aname
);
224 code
= pr_Read(ut
, 0, loc
, &tentry
, sizeof(tentry
));
227 if (ntohl(tentry
.flags
) & PRGRP
) {
228 if ((tentry
.count
== 0) && !admin
)
230 /* terminate prefix at colon if there is one */
231 if ((prefix
= strchr(tentry
.name
, ':')))
234 prefix
= tentry
.name
;
236 /* only sysadmin allow to use 'system:' prefix */
237 if ((strcmp(prefix
, "system") == 0) && !admin
)
240 strcpy(name
, aname
); /* in case aname & cname are same */
241 suffix
= strchr(name
, ':');
242 /* let e.g. pt_util create groups with "wrong" names (like
243 * an orphan whose parent ID was reused). Check that they look like
244 * groups (with a colon) or otherwise are good user names. */
246 strcpy(cname
, aname
);
250 /* sysadmin can make groups w/o ':', but they must still look like
251 * legal user names. */
256 if (strlen(prefix
) + strlen(suffix
) >= PR_MAXNAMELEN
)
258 strcpy(cname
, prefix
);
259 strcat(cname
, suffix
);
262 /* check for legal name with either group rules or user rules */
263 if ((suffix
= strchr(cname
, ':'))) {
264 /* check for confusing characters */
265 if (strchr(cname
, '\n') || /* restrict so recreate can work */
266 strchr(suffix
+ 1, ':')) /* avoid multiple colons */
269 if (!CorrectUserName(cname
))
276 AccessOK(struct ubik_trans
*ut
, afs_int32 cid
, /* caller id */
277 struct prentry
*tentry
, /* object being accessed */
278 int mem
, /* check membership in aid, if group */
279 int any
) /* if set return true */
287 if (cid
== SYSADMINID
)
288 return 1; /* special case fileserver */
289 if (restricted
&& ((mem
== PRP_ADD_MEM
) || (mem
== PRP_REMOVE_MEM
)) && (any
== 0))
292 flags
= tentry
->flags
;
296 flags
= oid
= aid
= 0;
298 if (!(flags
& PRACCESS
)) { /* provide default access */
300 flags
|= prp_group_default
;
302 flags
|= prp_user_default
;
308 if ((cid
== oid
) || IsAMemberOf(ut
, cid
, oid
))
311 if (aid
> 0) { /* checking on a user */
314 } else if (aid
< 0) { /* checking on group */
315 if ((flags
& mem
) && IsAMemberOf(ut
, cid
, aid
))
318 /* Allow members of SYSVIEWERID to get membership and status only */
319 if (((mem
== PRP_STATUS_MEM
) || (mem
== PRP_MEMBER_MEM
)
320 || (any
== PRP_OWNED_ANY
)) && (IsAMemberOf(ut
, cid
, SYSVIEWERID
)))
322 if (IsAMemberOf(ut
, cid
, SYSADMINID
))
324 return 0; /* no access */
328 CreateEntry(struct ubik_trans
*at
, char aname
[PR_MAXNAMELEN
], afs_int32
*aid
, afs_int32 idflag
, afs_int32 flag
, afs_int32 oid
, afs_int32 creator
)
330 /* get and init a new entry */
334 struct prentry tentry
, tent
;
337 memset(&tentry
, 0, sizeof(tentry
));
339 admin
= pr_noAuth
|| IsAMemberOf(at
, creator
, SYSADMINID
);
341 if (oid
== 0 || oid
== ANONYMOUSID
) {
342 if (!admin
&& creator
== 0)
348 code
= CorrectGroupName(at
, aname
, creator
, oid
, admin
, tentry
.name
);
351 if (strcmp(aname
, tentry
.name
) != 0)
353 } else { /* non-group must not have colon */
354 if (!CorrectUserName(aname
))
356 strcpy(tentry
.name
, aname
);
359 if (FindByName(at
, aname
, &tent
))
362 newEntry
= AllocBlock(at
);
365 tentry
.createTime
= time(0);
368 tentry
.flags
= PRGRP
;
370 } else if (flag
== 0) {
372 tentry
.owner
= SYSADMINID
;
377 atsign
= strchr(aname
, '@');
379 /* A normal user or group. Pick an id for it */
383 code
= AllocID(at
, flag
, &tentry
.id
);
384 if (code
!= PRSUCCESS
)
387 } else if (flag
& PRGRP
) {
388 /* A foreign group. Its format must be AUTHUSER_GROUP@cellname
389 * Then pick an id for the group.
394 badFormat
= strcmp(AUTHUSER_GROUP
, aname
);
402 code
= AllocID(at
, flag
, &tentry
.id
);
403 if (code
!= PRSUCCESS
)
407 /* A foreign user: <name>@<cell>. The foreign user is added to
408 * its representing group. It is
412 struct prentry centry
;
414 /* To create the user <name>@<cell> the group AUTHUSER_GROUP@<cell>
417 if (asprintf(&cellGroup
, "%s%s", AUTHUSER_GROUP
, atsign
) < 0)
419 pos
= FindByName(at
, cellGroup
, ¢ry
);
423 code
= pr_Read(at
, 0, pos
, ¢ry
, sizeof(centry
));
427 /* cellid is the id of the group representing the cell */
428 tentry
.cellid
= ntohl(centry
.id
);
431 /* Check if id is good */
432 if (!inRange(¢ry
, *aid
))
436 /* Allocate an ID special for this foreign user. It is based
437 * on the representing group's id and nusers count.
439 tentry
.id
= allocNextId(at
, ¢ry
);
444 /* The foreign user will be added to the representing foreign
445 * group. The group can hold up to 30 entries.
447 if (!(ntohl(centry
.flags
) & PRQUOTA
)) {
448 centry
.flags
= htonl(ntohl(centry
.flags
) | PRQUOTA
);
449 centry
.ngroups
= htonl(30);
451 n
= ntohl(centry
.ngroups
);
452 if ((n
<= 0) && !pr_noAuth
)
454 centry
.ngroups
= htonl(n
- 1);
456 /* write updated entry for group */
457 code
= pr_Write(at
, 0, pos
, ¢ry
, sizeof(centry
));
461 /* Now add the new user entry to the database */
463 tentry
.creator
= tentry
.id
;
465 tentry
.creator
= creator
;
467 code
= pr_WriteEntry(at
, 0, newEntry
, &tentry
);
470 code
= AddToIDHash(at
, *aid
, newEntry
);
471 if (code
!= PRSUCCESS
)
473 code
= AddToNameHash(at
, aname
, newEntry
);
474 if (code
!= PRSUCCESS
)
476 if (inc_header_word(at
, foreigncount
, 1))
479 /* Now add the entry to the authuser group for this cell.
480 * We will reread the entries for the user and the group
481 * instead of modifying them before writing them in the
482 * previous steps. Although not very efficient, much simpler
484 pos
= FindByID(at
, tentry
.cellid
);
487 code
= pr_ReadEntry(at
, 0, pos
, ¢ry
);
490 code
= AddToEntry(at
, ¢ry
, pos
, *aid
);
493 /* and now the user entry */
494 pos
= FindByID(at
, *aid
);
497 code
= pr_ReadEntry(at
, 0, pos
, &tentry
);
500 code
= AddToEntry(at
, &tentry
, pos
, tentry
.cellid
);
507 /* Remember the largest group id or largest user id */
509 /* group ids are negative */
510 if (tentry
.id
< (afs_int32
) ntohl(cheader
.maxGroup
)) {
511 code
= set_header_word(at
, maxGroup
, htonl(tentry
.id
));
516 if (tentry
.id
> (afs_int32
) ntohl(cheader
.maxID
)) {
517 code
= set_header_word(at
, maxID
, htonl(tentry
.id
));
523 /* Charge the creator for this group */
525 afs_int32 loc
= FindByID(at
, creator
);
526 struct prentry centry
;
529 if (loc
) { /* this should only fail during initialization */
530 code
= pr_Read(at
, 0, loc
, ¢ry
, sizeof(centry
));
534 /* If quota is uninitialized, do it */
535 if (!(ntohl(centry
.flags
) & PRQUOTA
)) {
536 centry
.flags
= htonl(ntohl(centry
.flags
) | PRQUOTA
);
537 centry
.ngroups
= centry
.nusers
= htonl(20);
540 /* Admins don't get charged for creating a group.
541 * If in noAuth mode, you get changed for it but you
542 * are still allowed to create as many groups as you want.
544 admin
= ((creator
== SYSADMINID
)
545 || IsAMemberOf(at
, creator
, SYSADMINID
));
547 if (ntohl(centry
.ngroups
) <= 0) {
551 centry
.ngroups
= htonl(ntohl(centry
.ngroups
) - 1);
555 code
= pr_Write(at
, 0, loc
, ¢ry
, sizeof(centry
));
560 /* Initialize the quota for the user. Groups don't have their
563 tentry
.flags
|= PRQUOTA
;
564 tentry
.ngroups
= tentry
.nusers
= 20;
568 tentry
.creator
= tentry
.id
;
570 tentry
.creator
= creator
;
572 code
= pr_WriteEntry(at
, 0, newEntry
, &tentry
);
575 code
= AddToIDHash(at
, *aid
, newEntry
);
576 if (code
!= PRSUCCESS
)
578 code
= AddToNameHash(at
, aname
, newEntry
);
579 if (code
!= PRSUCCESS
)
581 if (tentry
.flags
& PRGRP
) {
582 code
= AddToOwnerChain(at
, tentry
.id
, oid
);
586 if (tentry
.flags
& PRGRP
) {
587 if (inc_header_word(at
, groupcount
, 1))
589 } else if (tentry
.flags
& PRINST
) {
590 if (inc_header_word(at
, instcount
, 1))
593 if (inc_header_word(at
, usercount
, 1))
600 /* RemoveFromEntry - remove aid from bid's entries list, freeing a continuation
601 * entry if appropriate */
604 RemoveFromEntry(struct ubik_trans
*at
, afs_int32 aid
, afs_int32 bid
)
607 struct prentry tentry
;
608 struct contentry centry
;
609 struct contentry hentry
;
616 return PRINCONSISTENT
;
617 memset(&hentry
, 0, sizeof(hentry
));
618 temp
= FindByID(at
, bid
);
621 code
= pr_ReadEntry(at
, 0, temp
, &tentry
);
624 tentry
.removeTime
= time(0);
625 for (i
= 0; i
< PRSIZE
; i
++) {
626 if (tentry
.entries
[i
] == aid
) {
627 tentry
.entries
[i
] = PRBADID
;
629 code
= pr_WriteEntry(at
, 0, temp
, &tentry
);
634 if (tentry
.entries
[i
] == 0) /* found end of list */
640 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
643 if ((centry
.id
!= bid
) || !(centry
.flags
& PRCONT
))
645 for (i
= 0; i
< COSIZE
; i
++) {
646 if (centry
.entries
[i
] == aid
) {
647 centry
.entries
[i
] = PRBADID
;
648 for (j
= 0; j
< COSIZE
; j
++)
649 if (centry
.entries
[j
] != PRBADID
650 && centry
.entries
[j
] != 0)
652 if (j
== COSIZE
) { /* can free this block */
654 tentry
.next
= centry
.next
;
656 hentry
.next
= centry
.next
;
657 code
= pr_WriteCoEntry(at
, 0, hloc
, &hentry
);
661 code
= FreeBlock(at
, nptr
);
664 } else { /* can't free it yet */
665 code
= pr_WriteCoEntry(at
, 0, nptr
, ¢ry
);
670 code
= pr_WriteEntry(at
, 0, temp
, &tentry
);
675 if (centry
.entries
[i
] == 0)
677 } /* for all coentry slots */
680 memcpy(&hentry
, ¢ry
, sizeof(centry
));
681 } /* while there are coentries */
685 #if defined(SUPERGROUPS)
686 /* ChangeIDEntry - remove aid from bid's entries list, freeing a continuation
687 * entry if appropriate */
690 ChangeIDEntry(struct ubik_trans
*at
, afs_int32 aid
, afs_int32 newid
, afs_int32 bid
)
693 struct prentry tentry
;
694 struct contentry centry
;
700 return PRINCONSISTENT
;
701 temp
= FindByID(at
, bid
);
705 code
= pr_ReadEntry(at
, 0, temp
, &tentry
);
708 for (i
= 0; i
< PRSIZE
; i
++) {
709 if (tentry
.entries
[i
] == aid
) {
710 tentry
.entries
[i
] = newid
;
711 code
= pr_WriteEntry(at
, 0, temp
, &tentry
);
716 if (tentry
.entries
[i
] == 0) { /* found end of list */
723 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
726 if ((centry
.id
!= bid
) || !(centry
.flags
& PRCONT
)) {
728 "ChangeIDEntry: bad database bid=%d centry.id=%d .flags=%d\n",
729 bid
, centry
.id
, centry
.flags
);
732 for (i
= 0; i
< COSIZE
; i
++) {
733 if (centry
.entries
[i
] == aid
) {
734 centry
.entries
[i
] = newid
;
735 for (j
= 0; j
< COSIZE
; j
++)
736 if (centry
.entries
[j
] != PRBADID
737 && centry
.entries
[j
] != 0)
739 code
= pr_WriteCoEntry(at
, 0, nptr
, ¢ry
);
744 if (centry
.entries
[i
] == 0) {
747 } /* for all coentry slots */
749 } /* while there are coentries */
753 /* #ifdef SUPERGROUPS */
754 /* RemoveFromSGEntry - remove aid from bid's supergroups list, freeing a
755 * continuation entry if appropriate */
758 RemoveFromSGEntry(struct ubik_trans
*at
, afs_int32 aid
, afs_int32 bid
)
761 struct prentry tentry
;
762 struct prentryg
*tentryg
;
763 struct contentry centry
;
764 struct contentry hentry
;
771 return PRINCONSISTENT
;
772 memset(&hentry
, 0, sizeof(hentry
));
773 temp
= FindByID(at
, bid
);
777 code
= pr_ReadEntry(at
, 0, temp
, &tentry
);
780 tentry
.removeTime
= time(NULL
);
781 tentryg
= (struct prentryg
*)&tentry
;
782 for (i
= 0; i
< SGSIZE
; i
++) {
783 if (tentryg
->supergroup
[i
] == aid
) {
784 tentryg
->supergroup
[i
] = PRBADID
;
786 code
= pr_WriteEntry(at
, 0, temp
, &tentry
);
791 if (tentryg
->supergroup
[i
] == 0) { /* found end of list */
796 nptr
= tentryg
->nextsg
;
798 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
801 if ((centry
.id
!= bid
) || !(centry
.flags
& PRCONT
)) {
803 "ChangeIDEntry: bad database bid=%d centry.id=%d .flags=%d\n",
804 bid
, centry
.id
, centry
.flags
);
807 for (i
= 0; i
< COSIZE
; i
++) {
808 if (centry
.entries
[i
] == aid
) {
809 centry
.entries
[i
] = PRBADID
;
810 for (j
= 0; j
< COSIZE
; j
++)
811 if (centry
.entries
[j
] != PRBADID
812 && centry
.entries
[j
] != 0)
814 if (j
== COSIZE
) { /* can free this block */
816 tentryg
->nextsg
= centry
.next
;
818 hentry
.next
= centry
.next
;
819 code
= pr_WriteCoEntry(at
, 0, hloc
, &hentry
);
823 code
= FreeBlock(at
, nptr
);
826 } else { /* can't free it yet */
827 code
= pr_WriteCoEntry(at
, 0, nptr
, ¢ry
);
832 code
= pr_WriteEntry(at
, 0, temp
, &tentry
);
837 if (centry
.entries
[i
] == 0) {
840 } /* for all coentry slots */
843 memcpy(&hentry
, ¢ry
, sizeof(centry
));
844 } /* while there are coentries */
848 #endif /* SUPERGROUPS */
850 /* DeleteEntry - delete the entry in tentry at loc, removing it from all
851 * groups, putting groups owned by it on orphan chain, and freeing the space */
854 DeleteEntry(struct ubik_trans
*at
, struct prentry
*tentry
, afs_int32 loc
)
857 struct contentry centry
;
861 if (strchr(tentry
->name
, '@')) {
862 if (tentry
->flags
& PRGRP
) {
863 /* If there are still foreign user accounts from that cell
864 * don't delete the group */
870 afs_int32 loc
= FindByID(at
, tentry
->cellid
);
871 struct prentry centry
;
873 code
= pr_Read(at
, 0, loc
, ¢ry
, sizeof(centry
));
876 if (ntohl(centry
.flags
) & PRQUOTA
) {
877 centry
.ngroups
= htonl(ntohl(centry
.ngroups
) + 1);
879 code
= pr_Write(at
, 0, loc
, ¢ry
, sizeof(centry
));
885 /* First remove the entire membership list */
886 for (i
= 0; i
< PRSIZE
; i
++) {
887 if (tentry
->entries
[i
] == PRBADID
)
889 if (tentry
->entries
[i
] == 0)
891 #if defined(SUPERGROUPS)
892 if ((tentry
->flags
& PRGRP
) && tentry
->entries
[i
] < 0) /* Supergroup */
893 code
= RemoveFromSGEntry(at
, tentry
->id
, tentry
->entries
[i
]);
896 code
= RemoveFromEntry(at
, tentry
->id
, tentry
->entries
[i
]);
900 #if defined(SUPERGROUPS)
902 struct prentryg
*tentryg
= (struct prentryg
*)tentry
;
904 /* Then remove the entire supergroup list */
905 for (i
= 0; i
< SGSIZE
; i
++) {
906 if (tentryg
->supergroup
[i
] == PRBADID
)
908 if (tentryg
->supergroup
[i
] == 0)
910 code
= RemoveFromEntry(at
, tentry
->id
, tentryg
->supergroup
[i
]);
915 #endif /* SUPERGROUPS */
918 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
921 for (i
= 0; i
< COSIZE
; i
++) {
922 if (centry
.entries
[i
] == PRBADID
)
924 if (centry
.entries
[i
] == 0)
926 code
= RemoveFromEntry(at
, tentry
->id
, centry
.entries
[i
]);
930 code
= FreeBlock(at
, nptr
); /* free continuation block */
936 /* Remove us from other's owned chain. Note that this will zero our owned
937 * field (on disk) so this step must follow the above step in case we are
938 * on our own owned list. */
939 if (tentry
->flags
& PRGRP
) {
941 code
= RemoveFromOwnerChain(at
, tentry
->id
, tentry
->owner
);
945 code
= RemoveFromOrphan(at
, tentry
->id
);
951 code
= RemoveFromIDHash(at
, tentry
->id
, &loc
);
952 if (code
!= PRSUCCESS
)
954 code
= RemoveFromNameHash(at
, tentry
->name
, &loc
);
955 if (code
!= PRSUCCESS
)
958 if (tentry
->flags
& PRGRP
) {
959 afs_int32 loc
= FindByID(at
, tentry
->creator
);
960 struct prentry centry
;
964 code
= pr_Read(at
, 0, loc
, ¢ry
, sizeof(centry
));
967 admin
= ((tentry
->creator
== SYSADMINID
)
968 || IsAMemberOf(at
, tentry
->creator
, SYSADMINID
));
969 if (ntohl(centry
.flags
) & PRQUOTA
) {
970 if (!(admin
&& (ntohl(centry
.ngroups
) >= 20))) {
971 centry
.ngroups
= htonl(ntohl(centry
.ngroups
) + 1);
974 code
= pr_Write(at
, 0, loc
, ¢ry
, sizeof(centry
));
980 if (tentry
->flags
& PRGRP
) {
981 if (inc_header_word(at
, groupcount
, -1))
983 } else if (tentry
->flags
& PRINST
) {
984 if (inc_header_word(at
, instcount
, -1))
987 if (strchr(tentry
->name
, '@')) {
988 if (inc_header_word(at
, foreigncount
, -1))
991 if (inc_header_word(at
, usercount
, -1))
995 code
= FreeBlock(at
, loc
);
999 /* AddToEntry - add aid to entry's entries list, alloc'ing a continuation block
1002 * Note the entry is written out by this routine. */
1005 AddToEntry(struct ubik_trans
*tt
, struct prentry
*entry
, afs_int32 loc
, afs_int32 aid
)
1009 struct contentry nentry
;
1010 struct contentry aentry
;
1012 afs_int32 last
; /* addr of last cont. block */
1013 afs_int32 first
= 0;
1015 afs_int32 slot
= -1;
1017 if (entry
->id
== aid
)
1018 return PRINCONSISTENT
;
1019 entry
->addTime
= time(0);
1020 for (i
= 0; i
< PRSIZE
; i
++) {
1021 if (entry
->entries
[i
] == aid
)
1023 if (entry
->entries
[i
] == PRBADID
) { /* remember this spot */
1026 } else if (entry
->entries
[i
] == 0) { /* end of the line */
1037 code
= pr_ReadCoEntry(tt
, 0, nptr
, &nentry
);
1041 if (!(nentry
.flags
& PRCONT
))
1043 for (i
= 0; i
< COSIZE
; i
++) {
1044 if (nentry
.entries
[i
] == aid
)
1046 if (nentry
.entries
[i
] == PRBADID
) {
1051 } else if (nentry
.entries
[i
] == 0) {
1061 if (slot
!= -1) { /* we found a place */
1063 if (first
) { /* place is in first block */
1064 entry
->entries
[slot
] = aid
;
1065 code
= pr_WriteEntry(tt
, 0, loc
, entry
);
1070 code
= pr_WriteEntry(tt
, 0, loc
, entry
);
1073 code
= pr_ReadCoEntry(tt
, 0, cloc
, &aentry
);
1076 aentry
.entries
[slot
] = aid
;
1077 code
= pr_WriteCoEntry(tt
, 0, cloc
, &aentry
);
1082 /* have to allocate a continuation block if we got here */
1083 nptr
= AllocBlock(tt
);
1085 /* then we should tack new block after last block in cont. chain */
1087 code
= pr_WriteCoEntry(tt
, 0, last
, &nentry
);
1093 memset(&aentry
, 0, sizeof(aentry
));
1094 aentry
.flags
|= PRCONT
;
1095 aentry
.id
= entry
->id
;
1097 aentry
.entries
[0] = aid
;
1098 code
= pr_WriteCoEntry(tt
, 0, nptr
, &aentry
);
1101 /* don't forget to update count, here! */
1103 code
= pr_WriteEntry(tt
, 0, loc
, entry
);
1108 #if defined(SUPERGROUPS)
1110 /* AddToSGEntry - add aid to entry's supergroup list, alloc'ing a
1111 * continuation block if needed.
1113 * Note the entry is written out by this routine. */
1116 AddToSGEntry(struct ubik_trans
*tt
, struct prentry
*entry
, afs_int32 loc
, afs_int32 aid
)
1120 struct contentry nentry
;
1121 struct contentry aentry
;
1122 struct prentryg
*entryg
;
1124 afs_int32 last
; /* addr of last cont. block */
1125 afs_int32 first
= 0;
1127 afs_int32 slot
= -1;
1129 if (entry
->id
== aid
)
1130 return PRINCONSISTENT
;
1131 entry
->addTime
= time(NULL
);
1132 entryg
= (struct prentryg
*)entry
;
1133 for (i
= 0; i
< SGSIZE
; i
++) {
1134 if (entryg
->supergroup
[i
] == aid
)
1136 if (entryg
->supergroup
[i
] == PRBADID
) { /* remember this spot */
1139 } else if (entryg
->supergroup
[i
] == 0) { /* end of the line */
1148 nptr
= entryg
->nextsg
;
1150 code
= pr_ReadCoEntry(tt
, 0, nptr
, &nentry
);
1154 if (!(nentry
.flags
& PRCONT
))
1156 for (i
= 0; i
< COSIZE
; i
++) {
1157 if (nentry
.entries
[i
] == aid
)
1159 if (nentry
.entries
[i
] == PRBADID
) {
1164 } else if (nentry
.entries
[i
] == 0) {
1174 if (slot
!= -1) { /* we found a place */
1176 if (first
) { /* place is in first block */
1177 entryg
->supergroup
[slot
] = aid
;
1178 code
= pr_WriteEntry(tt
, 0, loc
, entry
);
1183 code
= pr_WriteEntry(tt
, 0, loc
, entry
);
1186 code
= pr_ReadCoEntry(tt
, 0, cloc
, &aentry
);
1189 aentry
.entries
[slot
] = aid
;
1190 code
= pr_WriteCoEntry(tt
, 0, cloc
, &aentry
);
1195 /* have to allocate a continuation block if we got here */
1196 nptr
= AllocBlock(tt
);
1198 /* then we should tack new block after last block in cont. chain */
1200 code
= pr_WriteCoEntry(tt
, 0, last
, &nentry
);
1204 entryg
->nextsg
= nptr
;
1206 memset(&aentry
, 0, sizeof(aentry
));
1207 aentry
.flags
|= PRCONT
;
1208 aentry
.id
= entry
->id
;
1210 aentry
.entries
[0] = aid
;
1211 code
= pr_WriteCoEntry(tt
, 0, nptr
, &aentry
);
1214 /* don't forget to update count, here! */
1216 code
= pr_WriteEntry(tt
, 0, loc
, entry
);
1220 #endif /* SUPERGROUPS */
1223 AddToPRList(prlist
*alist
, int *sizeP
, afs_int32 id
)
1228 if (alist
->prlist_len
>= *sizeP
) {
1229 count
= alist
->prlist_len
+ 100;
1230 if (alist
->prlist_val
) {
1231 tmp
= realloc(alist
->prlist_val
, count
* sizeof(afs_int32
));
1233 tmp
= malloc(count
* sizeof(afs_int32
));
1237 alist
->prlist_val
= tmp
;
1240 alist
->prlist_val
[alist
->prlist_len
++] = id
;
1245 GetList(struct ubik_trans
*at
, struct prentry
*tentry
, prlist
*alist
, afs_int32 add
)
1249 struct contentry centry
;
1255 alist
->prlist_val
= 0;
1256 alist
->prlist_len
= 0;
1258 for (i
= 0; i
< PRSIZE
; i
++) {
1259 if (tentry
->entries
[i
] == PRBADID
)
1261 if (tentry
->entries
[i
] == 0)
1263 code
= AddToPRList(alist
, &size
, tentry
->entries
[i
]);
1266 #if defined(SUPERGROUPS)
1269 code
= GetListSG2(at
, tentry
->entries
[i
], alist
, &size
, depthsg
);
1275 for (nptr
= tentry
->next
; nptr
!= 0; nptr
= centry
.next
) {
1276 /* look through cont entries */
1277 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
1280 for (i
= 0; i
< COSIZE
; i
++) {
1281 if (centry
.entries
[i
] == PRBADID
)
1283 if (centry
.entries
[i
] == 0)
1285 code
= AddToPRList(alist
, &size
, centry
.entries
[i
]);
1288 #if defined(SUPERGROUPS)
1291 code
= GetListSG2(at
, centry
.entries
[i
], alist
, &size
, depthsg
);
1297 #ifndef AFS_PTHREAD_ENV
1304 if (add
) { /* this is for a CPS, so tack on appropriate stuff */
1305 if (tentry
->id
!= ANONYMOUSID
&& tentry
->id
!= ANYUSERID
) {
1306 if ((code
= AddToPRList(alist
, &size
, ANYUSERID
))
1307 || (code
= AddAuthGroup(tentry
, alist
, &size
))
1308 || (code
= AddToPRList(alist
, &size
, tentry
->id
)))
1311 if ((code
= AddToPRList(alist
, &size
, ANYUSERID
))
1312 || (code
= AddToPRList(alist
, &size
, tentry
->id
)))
1316 #ifndef AFS_PTHREAD_ENV
1317 if (alist
->prlist_len
> 100)
1320 qsort(alist
->prlist_val
, alist
->prlist_len
, sizeof(afs_int32
), IDCmp
);
1326 GetList2(struct ubik_trans
*at
, struct prentry
*tentry
, struct prentry
*tentry2
, prlist
*alist
, afs_int32 add
)
1330 struct contentry centry
;
1336 alist
->prlist_val
= 0;
1337 alist
->prlist_len
= 0;
1338 for (i
= 0; i
< PRSIZE
; i
++) {
1339 if (tentry
->entries
[i
] == PRBADID
)
1341 if (tentry
->entries
[i
] == 0)
1343 code
= AddToPRList(alist
, &size
, tentry
->entries
[i
]);
1346 #if defined(SUPERGROUPS)
1349 code
= GetListSG2(at
, tentry
->entries
[i
], alist
, &size
, depthsg
);
1355 nptr
= tentry
->next
;
1357 /* look through cont entries */
1358 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
1361 for (i
= 0; i
< COSIZE
; i
++) {
1362 if (centry
.entries
[i
] == PRBADID
)
1364 if (centry
.entries
[i
] == 0)
1366 code
= AddToPRList(alist
, &size
, centry
.entries
[i
]);
1369 #if defined(SUPERGROUPS)
1372 code
= GetListSG2(at
, centry
.entries
[i
], alist
, &size
, depthsg
);
1379 #ifndef AFS_PTHREAD_ENV
1386 for (i
= 0; i
< PRSIZE
; i
++) {
1387 if (tentry2
->entries
[i
] == PRBADID
)
1389 if (tentry2
->entries
[i
] == 0)
1391 code
= AddToPRList(alist
, &size
, tentry2
->entries
[i
]);
1397 nptr
= tentry2
->next
;
1399 /* look through cont entries */
1400 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
1403 for (i
= 0; i
< COSIZE
; i
++) {
1404 if (centry
.entries
[i
] == PRBADID
)
1406 if (centry
.entries
[i
] == 0)
1408 code
= AddToPRList(alist
, &size
, centry
.entries
[i
]);
1414 #ifndef AFS_PTHREAD_ENV
1421 if (add
) { /* this is for a CPS, so tack on appropriate stuff */
1422 if (tentry
->id
!= ANONYMOUSID
&& tentry
->id
!= ANYUSERID
) {
1423 if ((code
= AddToPRList(alist
, &size
, ANYUSERID
))
1424 || (code
= AddToPRList(alist
, &size
, AUTHUSERID
))
1425 || (code
= AddToPRList(alist
, &size
, tentry
->id
)))
1428 if ((code
= AddToPRList(alist
, &size
, ANYUSERID
))
1429 || (code
= AddToPRList(alist
, &size
, tentry
->id
)))
1433 #ifndef AFS_PTHREAD_ENV
1434 if (alist
->prlist_len
> 100)
1437 qsort(alist
->prlist_val
, alist
->prlist_len
, sizeof(afs_int32
), IDCmp
);
1441 #if defined(SUPERGROUPS)
1444 GetListSG2(struct ubik_trans
*at
, afs_int32 gid
, prlist
*alist
, afs_int32
*sizeP
, afs_int32 depth
)
1447 struct prentry tentry
;
1448 struct prentryg
*tentryg
= (struct prentryg
*)&tentry
;
1450 struct contentry centry
;
1456 int predictfound
, predictflagged
;
1461 if (!in_map(sg_flagged
, -gid
)) {
1463 fprintf(stderr
, "GetListSG2: I have not yet searched for gid=%d\n",
1465 } else if (predictflagged
= 1, in_map(sg_found
, -gid
)) {
1468 "GetListSG2: I have already searched for gid=%d, and predict success.\n",
1473 if (in_map(sg_flagged
, -gid
) && !in_map(sg_found
, -gid
)) {
1476 "GetListSG2: I have already searched for gid=%d, and predict failure.\n",
1485 temp
= FindByID(at
, gid
);
1490 code
= pr_ReadEntry(at
, 0, temp
, &tentry
);
1494 fprintf(stderr
, "GetListSG2: lookup for gid=%d [\n", gid
);
1498 for (i
= 0; i
< SGSIZE
; i
++) {
1499 if (tentryg
->supergroup
[i
] == PRBADID
)
1501 if (tentryg
->supergroup
[i
] == 0)
1505 fprintf(stderr
, "via gid=%d, added %d\n", gid
,
1506 e
.tentryg
.supergroup
[i
]);
1508 code
= AddToPRList(alist
, sizeP
, tentryg
->supergroup
[i
]);
1512 GetListSG2(at
, tentryg
->supergroup
[i
], alist
, sizeP
, depth
- 1);
1517 nptr
= tentryg
->nextsg
;
1520 /* look through cont entries */
1521 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
1524 for (i
= 0; i
< COSIZE
; i
++) {
1525 if (centry
.entries
[i
] == PRBADID
)
1527 if (centry
.entries
[i
] == 0)
1530 fprintf(stderr
, "via gid=%d, added %d\n", gid
,
1531 e
.centry
.entries
[i
]);
1533 code
= AddToPRList(alist
, sizeP
, centry
.entries
[i
]);
1536 code
= GetListSG2(at
, centry
.entries
[i
], alist
, sizeP
, depth
- 1);
1542 #ifndef AFS_PTHREAD_ENV
1549 fprintf(stderr
, "] for gid %d, done [flag=%s]\n", gid
,
1550 didsomething
? "TRUE" : "FALSE");
1551 if (predictflagged
&& didsomething
!= predictfound
)
1552 fprintf(stderr
, "**** for gid=%d, didsomething=%d predictfound=%d\n",
1553 didsomething
, predictfound
);
1556 sg_found
= add_map(sg_found
, -gid
);
1558 sg_found
= bic_map(sg_found
, add_map(NIL_MAP
, -gid
));
1559 sg_flagged
= add_map(sg_flagged
, -gid
);
1564 GetSGList(struct ubik_trans
*at
, struct prentry
*tentry
, prlist
*alist
)
1568 struct contentry centry
;
1569 struct prentryg
*tentryg
;
1575 alist
->prlist_val
= 0;
1576 alist
->prlist_len
= 0;
1578 tentryg
= (struct prentryg
*)tentry
;
1579 for (i
= 0; i
< SGSIZE
; i
++) {
1580 if (tentryg
->supergroup
[i
] == PRBADID
)
1582 if (tentryg
->supergroup
[i
] == 0)
1584 code
= AddToPRList(alist
, &size
, tentryg
->supergroup
[i
]);
1589 nptr
= tentryg
->nextsg
;
1591 /* look through cont entries */
1592 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
1595 for (i
= 0; i
< COSIZE
; i
++) {
1596 if (centry
.entries
[i
] == PRBADID
)
1598 if (centry
.entries
[i
] == 0)
1600 code
= AddToPRList(alist
, &size
, centry
.entries
[i
]);
1606 #ifndef AFS_PTHREAD_ENV
1613 #ifndef AFS_PTHREAD_ENV
1614 if (alist
->prlist_len
> 100)
1617 qsort((char *)alist
->prlist_val
, (int)alist
->prlist_len
,
1618 sizeof(afs_int32
), IDCmp
);
1621 #endif /* SUPERGROUPS */
1624 GetOwnedChain(struct ubik_trans
*ut
, afs_int32
*next
, prlist
*alist
)
1627 struct prentry tentry
;
1632 alist
->prlist_val
= 0;
1633 alist
->prlist_len
= 0;
1635 for (; *next
; *next
= ntohl(tentry
.nextOwned
)) {
1636 code
= pr_Read(ut
, 0, *next
, &tentry
, sizeof(tentry
));
1639 code
= AddToPRList(alist
, &size
, ntohl(tentry
.id
));
1640 if (alist
->prlist_len
>= PR_MAXGROUPS
) {
1646 #ifndef AFS_PTHREAD_ENV
1652 #ifndef AFS_PTHREAD_ENV
1653 if (alist
->prlist_len
> 100)
1656 qsort(alist
->prlist_val
, alist
->prlist_len
, sizeof(afs_int32
), IDCmp
);
1661 GetMax(struct ubik_trans
*at
, afs_int32
*uid
, afs_int32
*gid
)
1663 *uid
= ntohl(cheader
.maxID
);
1664 *gid
= ntohl(cheader
.maxGroup
);
1669 SetMax(struct ubik_trans
*at
, afs_int32 id
, afs_int32 flag
)
1673 cheader
.maxGroup
= htonl(id
);
1675 pr_Write(at
, 0, 16, (char *)&cheader
.maxGroup
,
1676 sizeof(cheader
.maxGroup
));
1680 cheader
.maxID
= htonl(id
);
1682 pr_Write(at
, 0, 20, (char *)&cheader
.maxID
,
1683 sizeof(cheader
.maxID
));
1691 UpdateCache(struct ubik_trans
*tt
, void *rock
)
1695 code
= pr_Read(tt
, 0, 0, (char *)&cheader
, sizeof(cheader
));
1697 afs_com_err(whoami
, code
, "Couldn't read header");
1703 read_DbHeader(struct ubik_trans
*tt
)
1705 return ubik_CheckCache(tt
, UpdateCache
, NULL
);
1711 * reads in db cache from ubik.
1713 * @param[in] ut ubik transaction
1714 * @param[out] rock opaque pointer to an int*, which on success will be set
1715 * to 1 if we need to build the database, or 0 if we do not
1717 * @return operation status
1721 Initdb_check(struct ubik_trans
*tt
, void *rock
)
1723 int *build_rock
= rock
;
1727 len
= sizeof(cheader
);
1728 code
= pr_Read(tt
, 0, 0, (char *)&cheader
, len
);
1730 afs_com_err(whoami
, code
, "couldn't read header");
1733 if ((ntohl(cheader
.version
) == PRDBVERSION
)
1734 && ntohl(cheader
.headerSize
) == sizeof(cheader
)
1735 && ntohl(cheader
.eofPtr
) != 0
1736 && FindByID(tt
, ANONYMOUSID
) != 0) {
1737 /* database exists, so we don't have to build it */
1742 /* else we need to build a database */
1750 struct ubik_trans
*tt
;
1754 /* init the database. We'll try reading it, but if we're starting
1755 * from scratch, we'll have to do a write transaction. */
1757 pr_noAuth
= afsconf_GetNoAuthFlag(prdir
);
1759 code
= ubik_BeginTransReadAny(dbase
, UBIK_READTRANS
, &tt
);
1762 code
= ubik_SetLock(tt
, 1, 1, LOCKREAD
);
1764 ubik_AbortTrans(tt
);
1768 code
= ubik_CheckCache(tt
, Initdb_check
, &build
);
1770 ubik_AbortTrans(tt
);
1775 /* Only rebuild database if the db was deleted (the header is zero) */
1776 char *bp
= (char *)&cheader
;
1778 for (i
= 0; i
< sizeof(cheader
); i
++) {
1781 afs_com_err(whoami
, code
,
1782 "Can't rebuild database because it is not empty");
1791 code
= ubik_EndTrans(tt
);
1793 if (code
|| !build
) {
1794 /* either we encountered an error, or we don't need to build the db */
1798 code
= ubik_BeginTrans(dbase
, UBIK_WRITETRANS
, &tt
);
1802 code
= ubik_SetLock(tt
, 1, 1, LOCKWRITE
);
1804 ubik_AbortTrans(tt
);
1808 /* before doing a rebuild, check again that the dbase looks bad, because
1809 * the previous check was only under a ReadAny transaction, and there could
1810 * actually have been a good database out there. Now that we have a
1811 * real write transaction, make sure things are still bad.
1813 code
= pr_Read(tt
, 0, 0, (char *)&cheader
, sizeof(cheader
));
1815 afs_com_err(whoami
, code
, "couldn't read header");
1816 ubik_AbortTrans(tt
);
1819 if ((ntohl(cheader
.version
) == PRDBVERSION
)
1820 && ntohl(cheader
.headerSize
) == sizeof(cheader
)
1821 && ntohl(cheader
.eofPtr
) != 0
1822 && FindByID(tt
, ANONYMOUSID
) != 0) {
1823 /* database exists, so we don't have to build it */
1824 code
= ubik_EndTrans(tt
);
1830 /* Initialize the database header */
1831 if ((code
= set_header_word(tt
, version
, htonl(PRDBVERSION
)))
1832 || (code
= set_header_word(tt
, headerSize
, htonl(sizeof(cheader
))))
1833 || (code
= set_header_word(tt
, eofPtr
, cheader
.headerSize
))) {
1834 afs_com_err(whoami
, code
, "couldn't write header words");
1835 ubik_AbortTrans(tt
);
1838 #define InitialGroup(id,name) do { \
1839 afs_int32 temp = (id); \
1840 afs_int32 flag = (id) < 0 ? PRGRP : 0; \
1841 code = CreateEntry \
1842 (tt, (name), &temp, /*idflag*/1, flag, SYSADMINID, SYSADMINID); \
1844 afs_com_err (whoami, code, "couldn't create %s with id %di.", \
1846 ubik_AbortTrans(tt); \
1851 InitialGroup(SYSADMINID
, "system:administrators");
1852 InitialGroup(SYSBACKUPID
, "system:backup");
1853 InitialGroup(ANYUSERID
, "system:anyuser");
1854 InitialGroup(AUTHUSERID
, "system:authuser");
1855 InitialGroup(SYSVIEWERID
, "system:ptsviewers");
1856 InitialGroup(ANONYMOUSID
, "anonymous");
1858 /* Well, we don't really want the max id set to anonymousid, so we'll set
1860 code
= set_header_word(tt
, maxID
, 0); /* correct in any byte order */
1862 afs_com_err(whoami
, code
, "couldn't reset max id");
1863 ubik_AbortTrans(tt
);
1867 code
= ubik_EndTrans(tt
);
1874 ChangeEntry(struct ubik_trans
*at
, afs_int32 aid
, afs_int32 cid
, char *name
, afs_int32 oid
, afs_int32 newid
)
1878 #if defined(SUPERGROUPS)
1882 struct contentry centry
;
1884 struct prentry tentry
, tent
;
1888 char holder
[PR_MAXNAMELEN
];
1889 char temp
[PR_MAXNAMELEN
];
1890 char oldname
[PR_MAXNAMELEN
];
1893 memset(holder
, 0, PR_MAXNAMELEN
);
1894 memset(temp
, 0, PR_MAXNAMELEN
);
1895 loc
= FindByID(at
, aid
);
1898 code
= pr_ReadEntry(at
, 0, loc
, &tentry
);
1901 if (restricted
&& !IsAMemberOf(at
, cid
, SYSADMINID
))
1903 if (tentry
.owner
!= cid
&& !IsAMemberOf(at
, cid
, SYSADMINID
)
1904 && !IsAMemberOf(at
, cid
, tentry
.owner
) && !pr_noAuth
)
1906 tentry
.changeTime
= time(0);
1907 admin
= pr_noAuth
|| IsAMemberOf(at
, cid
, SYSADMINID
);
1909 /* we're actually trying to change the id */
1910 if (newid
&& (newid
!= aid
)) {
1914 pos
= FindByID(at
, newid
);
1916 return PRIDEXIST
; /* new id already in use! */
1917 if ((aid
< 0 && newid
> 0) || (aid
> 0 && newid
< 0))
1920 /* Should check that foreign users id to change to is good: inRange() */
1922 /* if new id is not in use, rehash things */
1923 code
= RemoveFromIDHash(at
, aid
, &loc
);
1924 if (code
!= PRSUCCESS
)
1927 code
= pr_WriteEntry(at
, 0, loc
, &tentry
);
1930 code
= AddToIDHash(at
, tentry
.id
, loc
);
1934 /* get current data */
1935 code
= pr_ReadEntry(at
, 0, loc
, &tentry
);
1939 #if defined(SUPERGROUPS)
1940 if (tentry
.id
> (afs_int32
) ntohl(cheader
.maxID
))
1941 code
= set_header_word(at
, maxID
, htonl(tentry
.id
));
1945 /* need to fix up: membership
1950 for (i
= 0; i
< PRSIZE
; i
++) {
1951 if (tentry
.entries
[i
] == PRBADID
)
1953 if (tentry
.entries
[i
] == 0)
1955 if ((tentry
.flags
& PRGRP
) && tentry
.entries
[i
] < 0) { /* Supergroup */
1956 return 5; /* not yet, in short. */
1958 code
= ChangeIDEntry(at
, aid
, newid
, tentry
.entries
[i
]);
1963 for (pos
= ntohl(tentry
.owned
); pos
; pos
= nextpos
) {
1964 code
= pr_ReadEntry(at
, 0, pos
, &tent
);
1968 nextpos
= tent
.nextOwned
;
1969 code
= pr_WriteEntry(at
, 0, pos
, &tent
);
1975 #define centry (*(struct contentry*)&tent)
1976 code
= pr_ReadCoEntry(at
, 0, pos
, ¢ry
);
1977 if ((centry
.id
!= aid
)
1978 || !(centry
.flags
& PRCONT
)) {
1980 "ChangeEntry: bad database aid=%d centry.id=%d .flags=%d\n",
1981 aid
, centry
.id
, centry
.flags
);
1985 for (i
= 0; i
< COSIZE
; i
++) {
1986 if (centry
.entries
[i
] == PRBADID
)
1988 if (centry
.entries
[i
] == 0)
1990 if ((centry
.flags
& PRGRP
) && centry
.entries
[i
] < 0) { /* Supergroup */
1991 return 5; /* not yet, in short. */
1993 code
= ChangeIDEntry(at
, aid
, newid
, centry
.entries
[i
]);
1998 code
= pr_WriteCoEntry(at
, 0, pos
, ¢ry
);
2005 #else /* SUPERGROUPS */
2008 /* Also change the references from the membership list */
2009 for (i
= 0; i
< PRSIZE
; i
++) {
2010 if (tentry
.entries
[i
] == PRBADID
)
2012 if (tentry
.entries
[i
] == 0)
2014 pos
= FindByID(at
, tentry
.entries
[i
]);
2017 code
= RemoveFromEntry(at
, aid
, tentry
.entries
[i
]);
2020 code
= pr_ReadEntry(at
, 0, pos
, &tent
);
2023 code
= AddToEntry(at
, &tent
, pos
, newid
);
2027 /* Look through cont entries too. This needs to be broken into
2028 * seperate transaction so that no one transaction becomes too
2029 * large to complete.
2031 for (nptr
= tentry
.next
; nptr
; nptr
= centry
.next
) {
2032 code
= pr_ReadCoEntry(at
, 0, nptr
, ¢ry
);
2035 for (i
= 0; i
< COSIZE
; i
++) {
2036 if (centry
.entries
[i
] == PRBADID
)
2038 if (centry
.entries
[i
] == 0)
2040 pos
= FindByID(at
, centry
.entries
[i
]);
2043 code
= RemoveFromEntry(at
, aid
, centry
.entries
[i
]);
2046 code
= pr_ReadEntry(at
, 0, pos
, &tent
);
2049 code
= AddToEntry(at
, &tent
, pos
, newid
);
2054 #endif /* SUPERGROUPS */
2057 atsign
= strchr(tentry
.name
, '@'); /* check for foreign entry */
2059 /* Change the owner */
2060 if (oid
&& (oid
!= tentry
.owner
)) {
2061 /* only groups can have their owner's changed */
2062 if (!(tentry
.flags
& PRGRP
))
2066 oldowner
= tentry
.owner
;
2068 /* The entry must be written through first so Remove and Add routines
2069 * can operate on disk data */
2070 code
= pr_WriteEntry(at
, 0, loc
, &tentry
);
2074 /* switch owner chains */
2075 if (oldowner
) /* if it has an owner */
2076 code
= RemoveFromOwnerChain(at
, tentry
.id
, oldowner
);
2077 else /* must be an orphan */
2078 code
= RemoveFromOrphan(at
, tentry
.id
);
2081 code
= AddToOwnerChain(at
, tentry
.id
, tentry
.owner
);
2085 /* fix up the name */
2086 if (strlen(name
) == 0)
2088 /* get current data */
2089 code
= pr_ReadEntry(at
, 0, loc
, &tentry
);
2094 /* Change the name, if name is a ptr to tentry.name then this name change
2095 * is due to a chown, otherwise caller has specified a new name */
2096 if ((name
== tentry
.name
) || (*name
&& (strcmp(tentry
.name
, name
) != 0))) {
2097 strncpy(oldname
, tentry
.name
, PR_MAXNAMELEN
);
2098 if (tentry
.flags
& PRGRP
) {
2099 /* don't let foreign cell groups change name */
2103 if (tentry
.owner
== 0 || tentry
.owner
== ANONYMOUSID
)
2106 code
= CorrectGroupName(at
, name
, cid
, tentry
.owner
, admin
, tentry
.name
);
2110 if (name
== tentry
.name
) { /* owner fixup */
2111 if (strcmp(oldname
, tentry
.name
) == 0)
2113 } else { /* new name, caller must be correct */
2114 if (strcmp(name
, tentry
.name
) != 0)
2118 /* Allow a foreign name change only if the cellname part is
2123 newatsign
= strchr(name
, '@');
2124 if (newatsign
!= atsign
) { /* if they are the same no problem */
2125 /*if the pointers are not equal the strings better be */
2126 if ((atsign
== NULL
) || (newatsign
== NULL
)
2127 || strcmp(atsign
, newatsign
))
2130 if (!CorrectUserName(name
))
2134 pos
= FindByName(at
, name
, &tent
);
2137 code
= RemoveFromNameHash(at
, oldname
, &loc
);
2138 if (code
!= PRSUCCESS
)
2140 strncpy(tentry
.name
, name
, PR_MAXNAMELEN
);
2141 code
= pr_WriteEntry(at
, 0, loc
, &tentry
);
2144 code
= AddToNameHash(at
, tentry
.name
, loc
);
2145 if (code
!= PRSUCCESS
)
2154 allocNextId(struct ubik_trans
* at
, struct prentry
* cellEntry
)
2156 /* Id's for foreign cell entries are constructed as follows:
2157 * The 16 low order bits are the group id of the cell and the
2158 * top 16 bits identify the particular users in that cell */
2161 afs_int32 cellid
= ((ntohl(cellEntry
->id
)) & 0x0000ffff);
2163 id
= (ntohl(cellEntry
->nusers
) + 1);
2164 while (FindByID(at
, ((id
<< 16) | cellid
))) {
2170 cellEntry
->nusers
= htonl(id
);
2171 /* use the field nusers to keep
2172 * the next available id in that
2173 * foreign cell's group. Note :
2174 * It would seem more appropriate
2175 * to use ngroup for that and nusers
2176 * to enforce the quota, however pts
2177 * does not have an option to change
2178 * foreign users quota yet */
2180 id
= (id
<< 16) | cellid
;
2185 inRange(struct prentry
*cellEntry
, afs_int32 aid
)
2187 afs_uint32 id
, cellid
, groupid
;
2191 * The only thing that we want to make sure here is that
2192 * the id is in the legal range of this group. If it is
2193 * a duplicate we don't care since it will get caught
2194 * in a different check.
2197 cellid
= aid
& 0x0000ffff;
2198 groupid
= (ntohl(cellEntry
->id
)) & 0x0000ffff;
2199 if (cellid
!= groupid
)
2200 return 0; /* not in range */
2203 * if we got here we're ok but we need to update the nusers
2204 * field in order to get the id correct the next time that
2205 * we try to allocate it automatically
2209 if (id
> ntohl(cellEntry
->nusers
))
2210 cellEntry
->nusers
= htonl(id
);
2216 AddAuthGroup(struct prentry
*tentry
, prlist
*alist
, afs_int32
*size
)
2218 if (!(strchr(tentry
->name
, '@')))
2219 return (AddToPRList(alist
, size
, AUTHUSERID
));