1
0

avapfile.c 11 KB

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