1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libaccess/ava.h"
#include "base/session.h"
#include "base/pblock.h"
#include "frame/req.h"
#include "frame/log.h"
#include "libadmin/libadmin.h"
#include "libaccess/avapfile.h"
#define ALLOC_SIZE 20
#define SUCCESS 0
struct parsedStruct {
char *fileName;
AVATable *avaTable;
};
typedef struct parsedStruct Parsed;
/* globals for yy_error if needed */
Session *yy_sn = NULL;
Request *yy_rq = NULL;
/*This will be a dynamic array of parsedStruct*. Re-sizing if necessary.*/
struct ParsedTable {
Parsed **parsedTable;
int numEntries;
};
char *currFile;
static struct ParsedTable parsedFiles = {NULL, 0};
extern AVATable entryTable; /*Table where entries are stored*/
extern AVAEntry tempEntry; /*Used to restore parser's state*/
extern linenum;
AVAEntry * AVAEntry_Dup(AVAEntry *entry) {
int i;
AVAEntry *newAVA = NULL;
/* copy the AVA entry */
if (entry) {
newAVA = (AVAEntry *) PERM_MALLOC(sizeof(AVAEntry));
memset(newAVA,0, sizeof(AVAEntry));
newAVA->userid = 0;
newAVA->CNEntry = 0;
newAVA->email = 0;
newAVA->locality = 0;
newAVA->state = 0;
newAVA->country = 0;
newAVA->company = 0;
newAVA->organizations = 0;
newAVA->numOrgs = 0;
if (entry->userid) newAVA->userid = PERM_STRDUP(entry->userid);
if (entry->CNEntry) newAVA->CNEntry = PERM_STRDUP(entry->CNEntry);
if (entry->email) newAVA->email = PERM_STRDUP(entry->email);
if (entry->locality) newAVA->locality = PERM_STRDUP(entry->locality);
if (entry->state) newAVA->state = PERM_STRDUP(entry->state);
if (entry->country) newAVA->country = PERM_STRDUP(entry->country);
if (entry->company) newAVA->company = PERM_STRDUP(entry->company);
if (entry->organizations) {
newAVA->organizations = PERM_MALLOC(sizeof(char *)*entry->numOrgs);
newAVA->numOrgs = entry->numOrgs;
for (i=0; i<entry->numOrgs; i++)
newAVA->organizations[i] = PERM_STRDUP (entry->organizations[i]);
}
}
return newAVA;
}
void _addAVAtoTable (AVAEntry *newAVA, AVATable *table) {
int i;
int insertIndex = -1;
if (table->numEntries%ENTRIES_ALLOCSIZE == 0) {
if (table->numEntries == 0) {
table->enteredTable =
(AVAEntry**) PERM_MALLOC (sizeof(AVAEntry*) * ENTRIES_ALLOCSIZE);
} else {
AVAEntry **temp;
temp =
PERM_MALLOC(sizeof(AVAEntry*)*(table->numEntries+ENTRIES_ALLOCSIZE));
memmove(temp, table->enteredTable, sizeof(AVAEntry*)*table->numEntries);
PERM_FREE(table->enteredTable);
table->enteredTable = temp;
}
}
for (i=table->numEntries-1; i >= 0; i--) {
if (strcmp(newAVA->userid, table->enteredTable[i]->userid) > 0) {
insertIndex = i+1;
break;
} else {
table->enteredTable[i+1] = table->enteredTable[i];
}
}
table->enteredTable[(insertIndex == -1) ? 0 : insertIndex] = newAVA;
(table->numEntries)++;
}
AVATable *AVATableDup(AVATable *table) {
AVATable *newTable = (AVATable*)PERM_MALLOC (sizeof(AVATable));
/* round the puppy so _addAVAtoTable still works */
int size = (table->numEntries + (ENTRIES_ALLOCSIZE-1))/ENTRIES_ALLOCSIZE;
int i;
newTable->enteredTable =
(AVAEntry**)PERM_MALLOC(size*ENTRIES_ALLOCSIZE*sizeof(AVAEntry *));
for (i=0; i < table->numEntries; i++) {
newTable->enteredTable[i] = AVAEntry_Dup(table->enteredTable[i]);
}
newTable->numEntries = table->numEntries;
return newTable;
}
AVAEntry *_getAVAEntry(char *groupName, AVATable *mapTable) {
char line[BIG_LINE];
int lh, rh, mid, cmp;;
if (!mapTable) {
sprintf (line, "NULL Pointer passed as mapTable when trying to get entry %s", groupName);
report_error (SYSTEM_ERROR, "File Not Found", line);
}
lh = 0;
rh = mapTable->numEntries-1;
while (lh <= rh) {
mid = lh + ((rh-lh)/2);
cmp = strcmp(groupName, mapTable->enteredTable[mid]->userid);
if (cmp == 0)
return mapTable->enteredTable[mid];
else if (cmp > 0)
lh = mid + 1;
else
rh = mid - 1;
}
return NULL;
}
AVATable *_getTable (char *fileName) {
int lh, rh, mid, cmp;
AVATable *table = NULL;
/*First checks to see if it's already been parsed*/
lh = 0;
rh = parsedFiles.numEntries-1;
while (lh <= rh) {
mid = lh + ((rh - lh)/2);
cmp = strcmp(fileName, parsedFiles.parsedTable[mid]->fileName);
if (cmp == SUCCESS) {
return parsedFiles.parsedTable[mid]->avaTable;
} else if (cmp < SUCCESS) {
rh = mid-1;
} else {
lh = mid+1;
}
}
yyin = fopen (fileName, "r");
if (yyin) {
if (!yyparse()) {
table = _wasParsed (fileName);
table->userdb = NULL;
}
fclose (yyin);
}
return table;
}
int _hasBeenParsed (char *aclFileName){
return (_getTable(aclFileName) != NULL);
}
AVATable* _wasParsed (char *inFileName) {
Parsed *newEntry;
int i;
if (!inFileName)
return NULL;
newEntry = (Parsed*) PERM_MALLOC (sizeof(Parsed));
newEntry->fileName = PERM_STRDUP (inFileName);
newEntry->avaTable = AVATableDup(&entryTable);
if (parsedFiles.numEntries % ALLOC_SIZE == 0) {
if (parsedFiles.numEntries) {
Parsed **temp;
temp = PERM_MALLOC (sizeof(Parsed*)*(parsedFiles.numEntries + ALLOC_SIZE));
if (!temp)
return NULL;
memcpy (temp, parsedFiles.parsedTable, sizeof(Parsed*)*parsedFiles.numEntries);
PERM_FREE (parsedFiles.parsedTable);
parsedFiles.parsedTable = temp;
} else {
parsedFiles.parsedTable =
(Parsed**) PERM_MALLOC (sizeof (Parsed*) * ALLOC_SIZE);
if (!parsedFiles.parsedTable)
return NULL;
}
}
for (i=parsedFiles.numEntries; i > 0; i--) {
if (strcmp(newEntry->fileName,parsedFiles.parsedTable[i-1]->fileName) < 0) {
parsedFiles.parsedTable[i] = parsedFiles.parsedTable[i-1];
} else {
break;
}
}
parsedFiles.parsedTable[i] = newEntry;
parsedFiles.numEntries++;
/*Initialize parser structures to resemble that before parse*/
entryTable.numEntries = 0;
tempEntry.country = tempEntry.company = tempEntry.CNEntry = NULL;
tempEntry.email = tempEntry.locality = tempEntry.state = NULL;
linenum = 1;
return newEntry->avaTable;
}
AVAEntry *_deleteAVAEntry (char *group, AVATable *table) {
int removeIndex;
int lh, rh, mid, cmp;
AVAEntry *entry = NULL;
if (!group || !table)
return NULL;
lh = 0;
rh = table->numEntries - 1;
while (lh <= rh) {
mid = lh + ((rh-lh)/2);
cmp = strcmp (group, table->enteredTable[mid]->userid);
if (cmp == SUCCESS) {
removeIndex = mid;
break;
} else if (cmp < SUCCESS) {
rh = mid-1;
} else {
lh = mid+1;
}
}
if (lh > rh)
return NULL;
entry = table->enteredTable[removeIndex];
memmove ((char*)(table->enteredTable)+(sizeof(AVAEntry*)*removeIndex),
(char*)(table->enteredTable)+(sizeof(AVAEntry*)*(removeIndex+1)),
(table->numEntries - removeIndex - 1)*sizeof(AVAEntry*));
(table->numEntries)--;
return entry;
}
void AVAEntry_Free (AVAEntry *entry) {
int i;
if (entry) {
if (entry->userid)
PERM_FREE (entry->userid);
if (entry->CNEntry)
PERM_FREE (entry->CNEntry);
if (entry->email)
PERM_FREE (entry->email);
if (entry->locality)
PERM_FREE (entry->locality);
if (entry->state)
PERM_FREE (entry->state);
if (entry->country)
PERM_FREE (entry->country);
if (entry->company)
PERM_FREE (entry->company);
if (entry->organizations) {
for (i=0; i<entry->numOrgs; i++)
PERM_FREE (entry->organizations[i]);
PERM_FREE(entry->organizations);
}
}
}
void PrintHeader(FILE *outfile){
fprintf (outfile,"/*This file is generated automatically by the admin server\n");
fprintf (outfile," *Any changes you make manually may be lost if other\n");
fprintf (outfile," *changes are made through the admin server.\n");
fprintf (outfile," */\n\n\n");
}
void writeOutEntry (FILE *outfile, AVAEntry *entry) {
int i;
/*What should I do if the group id is not there?*/
if (!entry || !(entry->userid))
report_error (SYSTEM_ERROR, "AVA-DB Failure",
"Bad entry passed to write out function");
fprintf (outfile,"%s: {\n", entry->userid);
if (entry->CNEntry)
fprintf (outfile,"\tCN=\"%s\"\n", entry->CNEntry);
if (entry->email)
fprintf (outfile,"\tE=\"%s\"\n", entry->email);
if (entry->company)
fprintf (outfile,"\tO=\"%s\"\n", entry->company);
if (entry->organizations) {
for (i=0; i < entry->numOrgs; i++) {
fprintf (outfile, "\tOU=\"%s\"\n", entry->organizations[i]);
}
}
if (entry->locality)
fprintf (outfile,"\tL=\"%s\"\n",entry->locality);
if (entry->state)
fprintf (outfile,"\tST=\"%s\"\n",entry->state);
if (entry->country)
fprintf (outfile,"\tC=\"%s\"\n", entry->country);
fprintf (outfile,"}\n\n\n");
}
void writeOutFile (char *authdb, AVATable *table) {
char line[BIG_LINE];
char mess[200];
FILE *newfile;
int i;
sprintf (line, "%s%c%s%c%s.%s", get_authdb_dir(), FILE_PATHSEP, authdb, FILE_PATHSEP,
AUTH_DB_FILE, AVADB_TAG);
if (!table) {
sprintf (mess, "The structure for file %s was not loaded before writing out", line);
report_error (SYSTEM_ERROR, "Internal Error", mess);
}
newfile = fopen (line, "w");
if (!newfile) {
sprintf (mess, "Could not open file %s for writing.", line);
report_error(FILE_ERROR, "No File", mess);
}
PrintHeader (newfile);
for (i=0;i < table->numEntries; i++) {
writeOutEntry (newfile, table->enteredTable[i]);
}
fclose(newfile);
}
void
logerror(char *error,int line,char *file) {
/* paranoia */
/*ava-mapping is only functin that initializes yy_sn and yy_rq*/
if ((yy_sn != NULL) && (yy_rq != NULL)) {
log_error (LOG_FAILURE, "ava-mapping", yy_sn, yy_rq,
"Parse error line %d of %s: %s", line, file, error);
} else {
char errMess[250];
sprintf (errMess, "Parse error line %d of %s: %s", line, file, error);
report_error (SYSTEM_ERROR, "Failure: Loading AVA-DB Table", errMess);
}
}
void outputAVAdbs(char *chosen) {
char *authdbdir = get_authdb_dir();
char **listings;
int i;
int numListings = 0;
int hasOptions = 0;
listings = list_auth_dbs(authdbdir);
while (listings[numListings++] != NULL);
for (i=0; listings[i] != NULL ; i++) {
if (!hasOptions) {
printf ("<select name=\"%s\"%s onChange=\"form.submit()\">",AVA_DB_SEL,
(numListings > SELECT_OVERFLOW)?"size=5":"");
hasOptions = 1;
}
printf ("<option value=\"%s\"%s>%s\n",listings[i],
(strcmp(chosen, listings[i]) == 0) ? "SELECTED":"",listings[i]);
}
if (hasOptions)
printf ("</select>\n");
else
printf ("<i><b>Insert an AVA-Database entry first</b></i>\n");/*This should never happen,
*since I never create an empty
*avadb file,
*but one never knows
*/
}
|