Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 1 | #include <config.h> |
| 2 | |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <ctype.h> |
| 5 | #include <sys/types.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 6 | #if TIME_WITH_SYS_TIME |
| 7 | # include <sys/time.h> |
| 8 | # include <time.h> |
| 9 | #else |
| 10 | # if HAVE_SYS_TIME_H |
| 11 | # include <sys/time.h> |
| 12 | # else |
| 13 | # include <time.h> |
| 14 | # endif |
| 15 | #endif |
| 16 | #if HAVE_FCNTL_H |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 18 | #endif |
| 19 | #if HAVE_UNISTD_H |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 21 | #endif |
| 22 | #if HAVE_NETINET_IN_H |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 23 | #include <netinet/in.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 24 | #endif |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 25 | #include <sys/socket.h> |
| 26 | #include <net/if.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 27 | #if HAVE_SYS_IOCTL_H |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 29 | #endif |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 30 | #include "asn1.h" |
| 31 | #include "context.h" |
| 32 | #include "system.h" |
| 33 | |
| 34 | #define TRUE 1 |
| 35 | #define FALSE 0 |
| 36 | |
| 37 | #define IDENTITY_STATE 1 |
| 38 | #define VIEW_STATE 2 |
| 39 | #define PROXY_STATE 3 |
| 40 | |
| 41 | static error_exit(str, linenumber, filename) |
| 42 | char *str; |
| 43 | int linenumber; |
| 44 | char *filename; |
| 45 | { |
| 46 | fprintf(stderr, "%s on line %d of %s\n", str, linenumber, filename); |
| 47 | exit(1); |
| 48 | } |
| 49 | |
| 50 | int |
| 51 | read_context_database(filename) |
| 52 | char *filename; |
| 53 | { |
| 54 | FILE *fp; |
| 55 | char buf[256], buf1[256], buf2[256], buf3[256]; |
| 56 | char *cp; |
| 57 | int blank, nonhex; |
| 58 | int linenumber = 0, chars = 0, clock_pos; |
| 59 | int state = IDENTITY_STATE; |
| 60 | oid contextid[64]; |
| 61 | int contextidlen; |
| 62 | int view, entityLen, time; |
| 63 | u_char entity[64]; |
| 64 | int dstParty, srcParty, proxyIdLen; |
| 65 | oid proxyId[64]; |
| 66 | char name[64]; /* friendly name */ |
| 67 | struct contextEntry *cxp, *rp; |
| 68 | u_long myaddr; |
| 69 | int diff; |
| 70 | |
| 71 | fp = fopen(filename, "r"); |
| 72 | if (fp == NULL) |
| 73 | return -1; |
| 74 | while (fgets(buf, 256, fp)){ |
| 75 | linenumber++; |
| 76 | if (strlen(buf) > 250) |
| 77 | error_exit("Line longer than 250 bytes", linenumber, filename); |
| 78 | chars += strlen(buf); |
| 79 | if (buf[0] == '#') |
| 80 | continue; |
| 81 | blank = TRUE; |
| 82 | for(cp = buf; *cp; cp++) |
| 83 | if (!isspace(*cp)){ |
| 84 | blank = FALSE; |
| 85 | break; |
| 86 | } |
| 87 | if (blank) |
| 88 | continue; |
| 89 | switch(state){ |
| 90 | case IDENTITY_STATE: |
| 91 | if (sscanf(buf, "%s %s", name, buf1) != 2) |
| 92 | error_exit("Bad parse", linenumber, filename); |
| 93 | contextidlen = 64; |
| 94 | if (!read_objid(buf1, contextid, &contextidlen)) |
| 95 | error_exit("Bad object identifier", linenumber, filename); |
| 96 | state = VIEW_STATE; |
| 97 | break; |
| 98 | case VIEW_STATE: |
| 99 | if (sscanf(buf, "%s %s %s", buf1, entity, buf3) != 3) |
| 100 | error_exit("Bad parse", linenumber, filename); |
| 101 | for(cp = buf1; *cp; cp++) |
| 102 | if (!isdigit(*cp)) |
| 103 | error_exit("Not a view index", linenumber, filename); |
| 104 | view = atoi(buf1); |
| 105 | if (!strcasecmp(entity, "Null")) |
| 106 | entityLen = 0; |
| 107 | if (!strcasecmp(buf3, "currentTime")) |
| 108 | time = CURRENTTIME; |
| 109 | else if (!strcasecmp(buf3, "restartTime")) |
| 110 | time = RESTARTTIME; |
| 111 | else |
| 112 | error_exit("Bad local time", linenumber, filename); |
| 113 | state = PROXY_STATE; |
| 114 | break; |
| 115 | case PROXY_STATE: |
| 116 | if (sscanf(buf, "%s %s %s", buf1, buf2, buf3) != 3) |
| 117 | error_exit("Bad parse", linenumber, filename); |
| 118 | for(cp = buf1; *cp; cp++) |
| 119 | if (!isdigit(*cp)) |
| 120 | error_exit("Bad destination party index", linenumber, |
| 121 | filename); |
| 122 | dstParty = atoi(buf1); |
| 123 | |
| 124 | for(cp = buf1; *cp; cp++) |
| 125 | if (!isdigit(*cp)) |
| 126 | error_exit("Bad source party index", linenumber, filename); |
| 127 | srcParty = atoi(buf2); |
| 128 | |
| 129 | proxyIdLen = 64; |
| 130 | if (!read_objid(buf3, proxyId, &proxyIdLen)) |
| 131 | error_exit("Bad object identifier", linenumber, filename); |
| 132 | |
| 133 | state = IDENTITY_STATE; |
| 134 | |
| 135 | cxp = context_getEntry(contextid, contextidlen); |
| 136 | if (!cxp) |
| 137 | cxp = context_createEntry(contextid, contextidlen); |
| 138 | rp = cxp->reserved; |
| 139 | strcpy(cxp->contextName, name); |
| 140 | myaddr = get_myaddr(); |
| 141 | /* XXX It's bogus to figure out if it is local |
| 142 | by testing the ipaddress in the context - fix this XXX */ |
| 143 | diff = ((myaddr & 0xFF000000) >> 24) ^ contextid[9]; |
| 144 | diff |= ((myaddr & 0x00FF0000) >> 16) ^ contextid[10]; |
| 145 | diff |= ((myaddr & 0x0000FF00) >> 8) ^ contextid[11]; |
| 146 | diff |= (myaddr & 0x000000FF) ^ contextid[12]; |
| 147 | if (!diff){ |
| 148 | /* context is local */ |
| 149 | cxp->contextLocal = 1; /* TRUE */ |
| 150 | } else { |
| 151 | cxp->contextLocal = 2; /* FALSE */ |
| 152 | } |
| 153 | cxp->contextViewIndex = view; |
Wes Hardaker | 7d81229 | 1995-03-09 00:09:02 +0000 | [diff] [blame] | 154 | #ifdef SVR4 |
| 155 | memmove(cxp->contextLocalEntity, entity, entityLen); |
| 156 | #else |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 157 | bcopy(entity, cxp->contextLocalEntity, entityLen); |
Wes Hardaker | 7d81229 | 1995-03-09 00:09:02 +0000 | [diff] [blame] | 158 | #endif |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 159 | cxp->contextLocalEntityLen = entityLen; |
| 160 | cxp->contextLocalTime = time; |
| 161 | cxp->contextDstPartyIndex = dstParty; |
| 162 | cxp->contextSrcPartyIndex = srcParty; |
Wes Hardaker | 7d81229 | 1995-03-09 00:09:02 +0000 | [diff] [blame] | 163 | #ifdef SVR4 |
| 164 | memmove(cxp->contextProxyContext, proxyId, proxyIdLen * sizeof(oid)); |
| 165 | #else |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 166 | bcopy(proxyId, cxp->contextProxyContext, proxyIdLen * sizeof(oid)); |
Wes Hardaker | 7d81229 | 1995-03-09 00:09:02 +0000 | [diff] [blame] | 167 | #endif |
Wes Hardaker | 5201c85 | 1994-12-14 23:43:42 +0000 | [diff] [blame] | 168 | cxp->contextProxyContextLen = proxyIdLen; |
| 169 | cxp->contextStorageType = 2; |
| 170 | cxp->contextStatus = rp->contextStatus = CONTEXTACTIVE; |
| 171 | #define CONTEXTCOMPLETE_MASK 0x03FF |
| 172 | /* all collumns - from context_vars.c XXX */ |
| 173 | cxp->contextBitMask = rp->contextBitMask = CONTEXTCOMPLETE_MASK; |
| 174 | break; |
| 175 | default: |
| 176 | error_exit("unknown state", linenumber, filename); |
| 177 | } |
| 178 | } |
| 179 | if (state != IDENTITY_STATE) |
| 180 | error_exit("Unfinished entry at EOF", linenumber, filename); |
| 181 | fclose(fp); |
| 182 | return 0; |
| 183 | } |