1: #include "parms.h"
2: #include "structs.h"
3:
4: #ifdef RCSIDENT
5: static char rcsid[] = "$Header: /usr/local/src/usenet/notes/src/RCS/perms.c,v 1.8 88/11/10 21:49:23 paul Exp $";
6: #endif RCSIDENT
7:
8: /*
9: * getperms - opens and reads the permission file to
10: * determine the access rights for this user
11: *
12: * Uses a struture similar to PLATO's. A list is scanned
13: * until a match is found. The access rights at that point
14: * are then awarded to the user. In the event that no
15: * match is found, no access rights are granted.
16: *
17: * Original Coding: Ray Essick December 1981
18: */
19: #include <grp.h>
20: /*
21: * Make sure that the name NGROUPS is defined.
22: * pre-4.1a probably doesn't define either.
23: * 4.1a uses the NGRPS constant
24: * 4.2 uses the NGROUPS constant
25: * We make sure that NGROUPS is defined and use that in the
26: * generic portion of the routine. The emulation for V7, 4.1, 4.1a
27: * and such use the NGRPS or whatever is appropriate.
28: */
29: #if defined(NGRPS) && ! defined(NGROUPS)
30: #define NGROUPS NGRPS /* name change! */
31: #endif
32: #ifndef NGROUPS
33: #define NGROUPS 1 /* could be better */
34: #endif NGROUPS
35:
36: static char *gnameptr[NGROUPS]; /* point to names */
37: static int ngroups = 0; /* true if have gids */
38:
39: getperms (io, sysflag, name)
40: struct io_f *io;
41: int sysflag; /* true for remote system */
42: char *name;
43: {
44:
45: register int i;
46: FILE * acs, *fopen ();
47: struct group *gr,
48: *getgrgid ();
49: char fn[WDLEN];
50: struct perm_f entry;
51: int hisperms; /* built up perms */
52: int given; /* if assigned perms */
53:
54: if (sysflag == 0 && globuid == Notesuid) /* "notes" omnipotent */
55: {
56: io -> access = READOK + RESPOK + WRITOK + DRCTOK;/* all */
57: /*
58: * should I just set it to -1 or something that turns on
59: * all the bits? or leave it with the defined bits only?
60: */
61: return;
62: }
63:
64: if (sysflag == 0 && ngroups == 0)
65: {
66: register int i,
67: j; /* temp loop stuff */
68: int gidset[NGROUPS]; /* hold gid list */
69:
70: ngroups = NGROUPS; /* max allowed */
71: /*
72: * NOTE that the getgroups system call doesn't behave as
73: * documented in the 4.2 manual. The manual says to call it
74: * ret=getgroups(&ngroups,&gidset) where ngroups is value-result.
75: * and ret is 0 on success. Actual implementation works as below.
76: */
77: if ((ngroups = getgroups (ngroups, &gidset)) >= 0)/* worked */
78: {
79: for (i = 0, j = 0; i < ngroups; i++) /* get names */
80: {
81: if ((gr = getgrgid (gidset[i])) == NULL)
82: continue; /* bogus, skip it */
83: gnameptr[j++] = strsave (gr -> gr_name);/* save it */
84: }
85: ngroups = j; /* save count */
86: }
87: }
88: io -> access = 0; /* default null */
89: hisperms = 0; /* set up mask */
90: given = 0;
91:
92: sprintf (fn, "%s/%s/%s", io -> basedir, io -> nf, ACCESS);
93: x ((acs = fopen (fn, "r")) == NULL, "getperms: no list");
94: while (fread (&entry, sizeof entry, 1, acs) == 1)
95: {
96: if ((sysflag != 0) && (entry.ptype != PERMSYSTEM))
97: continue; /* looking for systems */
98: if ((sysflag == 0) && (entry.ptype == PERMSYSTEM))
99: continue; /* users != systems */
100:
101: if (strcmp (entry.name, "Other") == 0)
102: {
103: if (!given) /* he hasn't matched */
104: hisperms = entry.perms; /* give him these */
105: goto gotit; /* and exit ... */
106: }
107: switch (entry.ptype)
108: {
109: case PERMUSER:
110: if (strcmp (name, entry.name) == 0)
111: {
112: hisperms = entry.perms;
113: goto gotit;
114: }
115: break;
116:
117: case PERMGROUP: /* a group entry */
118: for (i = 0; i < ngroups; i++) /* check all */
119: if (strcmp (entry.name, gnameptr[i]) == 0)
120: {
121: hisperms |= entry.perms; /* OR them in */
122: given++; /* mark as such */
123: break;
124: }
125: break;
126:
127: case PERMSYSTEM:
128: if (strcmp (name, entry.name) == 0)
129: {
130: hisperms = entry.perms;
131: given++;
132: goto gotit;
133: }
134: break;
135:
136: default: /* bad access list */
137: x (1, "getperms: bad list");
138: }
139: }
140: gotit:
141: fclose (acs); /* close the access file */
142: io -> access = hisperms; /* what we built */
143:
144: return;
145: }
146:
147: /*
148: * Some compatibility routines to let us use the fancy 4.2 Bsd
149: * getgroups() system call while running 4.1, 4.1a or V7 kernels.
150: * I've undoubtedly missed some kernels.
151: */
152:
153:
154: /*
155: * getgroups
156: *
157: * Returns an integer and a set of groups.
158: * Emulates the 4.2 getgroups command under 4.1a Bsd
159: *
160: * Stolen mostly from the 4.1a command "groups".
161: *
162: * Ray Essick, January 1984
163: *
164: */
165:
166: #if defined (BSD41A)
167:
168: getgroups (ngroups, gidset)
169: int ngroups;
170: int *gidset;
171: {
172: register int i;
173: register int maxback; /* most to user */
174: int grps[NGRPS / (sizeof (int) * 8)]; /* NGRPS=NGROUPS */
175:
176: setgrp (0, grps); /* get groups */
177: maxback = ngroups; /* save limit */
178: ngroups = 0; /* start empty */
179: for (i = 0; i < NGRPS && ngroups < maxback; i++) /* for each */
180: if (grps[i / (sizeof (int) * 8)] & (1 << (i % (sizeof (int) * 8))))
181: {
182: *gidset++ = i; /* save the group */
183: ngroups++; /* and count */
184: }
185: return (ngroups);
186: }
187: #endif defined (BSD41A)
188:
189: /*
190: * The V7 and 4.1 version of this system call. Also serves well
191: * for the 2.8 Bsd kernels and probably for the more recent BTL
192: * kernels. 2.9BSD had groups as of the Seismo/Harvard update tape SMS
193: * This could be extended to read from /etc/groups to actually give
194: * the user all groups he is permitted.
195: */
196:
197: #if defined (V7) || defined (BSD41) || defined (BSD28) || defined (USG)
198: getgroups (ngroups, gidset) /* simple V7 one */
199: int ngroups;
200: int *gidset;
201: {
202: *gidset = getgid () & GIDMASK;
203: ngroups = 1;
204: return (1);
205: }
206: #endif
Defined functions
Defined variables
rcsid
defined in line
5;
never used
Defined macros