| 1 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 2 | /* |
| 3 | Name |
| 4 | vmnet.cpp - TADS 3 network configuration |
| 5 | Function |
| 6 | Implements the network configuration object for TADS 3. |
| 7 | Notes |
| 8 | |
| 9 | Modified |
| 10 | 04/11/10 MJRoberts - Creation |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include "t3std.h" |
| 18 | #include "os.h" |
| 19 | #include "osifcnet.h" |
| 20 | #include "vmnet.h" |
| 21 | #include "vmglob.h" |
| 22 | #include "vmerr.h" |
| 23 | #include "vmtype.h" |
| 24 | #include "vmobj.h" |
| 25 | #include "vmstr.h" |
| 26 | #include "vmlst.h" |
| 27 | #include "vmisaac.h" |
| 28 | #include "sha2.h" |
| 29 | #include "vmpredef.h" |
| 30 | #include "vmhttpreq.h" |
| 31 | #include "vmglob.h" |
| 32 | #include "vmmain.h" |
| 33 | #include "vmpredef.h" |
| 34 | #include "vmerrnum.h" |
| 35 | #include "vmrun.h" |
| 36 | #include "vmdatasrc.h" |
| 37 | |
| 38 | |
| 39 | /* ------------------------------------------------------------------------ */ |
| 40 | /* |
| 41 | * Configuration file reader |
| 42 | */ |
| 43 | |
| 44 | TadsNetConfig::~TadsNetConfig() |
| 45 | { |
| 46 | /* delete our variables */ |
| 47 | TadsNetConfigVar *cur, *nxt; |
| 48 | for (cur = first_var ; cur != 0 ; cur = nxt) |
| 49 | { |
| 50 | nxt = cur->getnext(); |
| 51 | delete cur; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * load a configuration file |
| 57 | */ |
| 58 | void TadsNetConfig::read(osfildef *fp, CVmMainClientIfc *clientifc) |
| 59 | { |
| 60 | /* read the file line by line */ |
| 61 | for (int linenum = 1 ; ; ++linenum) |
| 62 | { |
| 63 | /* read the next line */ |
| 64 | char buf[512]; |
| 65 | if (osfgets(buf, sizeof(buf), fp) == 0) |
| 66 | break; |
| 67 | |
| 68 | /* skip leading whitespace */ |
| 69 | char *p; |
| 70 | for (p = buf ; isspace(*p) ; ++p) ; |
| 71 | |
| 72 | /* skip blank lines and comments */ |
| 73 | if (*p == '\0' || *p == '#') |
| 74 | continue; |
| 75 | |
| 76 | /* the variable name is the first token on the line */ |
| 77 | char *name = p; |
| 78 | |
| 79 | /* find the end of the name: the next space or '=' */ |
| 80 | for ( ; *p != '\0' && !isspace(*p) && *p != '=' ; ++p) ; |
| 81 | char *name_end = p; |
| 82 | |
| 83 | /* skip spaces */ |
| 84 | for ( ; isspace(*p) ; ++p) ; |
| 85 | |
| 86 | /* make sure we stopped at a '=' */ |
| 87 | if (*p != '=') |
| 88 | { |
| 89 | char *msg = t3sprintf_alloc( |
| 90 | "Missing '=' in web config file at line %d", linenum); |
| 91 | clientifc->display_error(0, 0, msg, FALSE); |
| 92 | t3free(msg); |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | /* null-terminate the name */ |
| 97 | *name_end = '\0'; |
| 98 | |
| 99 | /* skip spaces after the '=' */ |
| 100 | for (++p ; isspace(*p) ; ++p) ; |
| 101 | |
| 102 | /* the value starts here */ |
| 103 | char *val = p; |
| 104 | |
| 105 | /* |
| 106 | * the value is the rest of the line, minus trailing spaces and |
| 107 | * newlines |
| 108 | */ |
| 109 | for (p += strlen(p) ; |
| 110 | p > val && (isspace(*(p-1)) |
| 111 | || *(p-1) == '\n' || *(p-1) == '\r') ; |
| 112 | --p) ; |
| 113 | |
| 114 | /* null-terminate the value */ |
| 115 | *p = '\0'; |
| 116 | |
| 117 | /* add the variable */ |
| 118 | TadsNetConfigVar *var = set(name, val); |
| 119 | |
| 120 | /* check that this is a variable name we recognize */ |
| 121 | static const char *known_vars[] = { |
| 122 | "serverid", |
| 123 | "storage.domain", |
| 124 | "storage.rootpath", |
| 125 | "storage.apikey", |
| 126 | "watchdog" |
| 127 | }; |
| 128 | |
| 129 | int found = FALSE; |
| 130 | for (size_t i = 0 ; i < countof(known_vars) ; ++i) |
| 131 | { |
| 132 | |
| 133 | if (var->matchname(known_vars[i])) |
| 134 | { |
| 135 | found = TRUE; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* warn if we didn't find it among the known variables */ |
| 141 | if (!found) |
| 142 | { |
| 143 | char *msg = t3sprintf_alloc( |
| 144 | "Warning: unknown variable name '%s' in web config file " |
| 145 | "at line %d\n", name, linenum); |
| 146 | clientifc->display_error(0, 0, msg, FALSE); |
| 147 | t3free(msg); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * set a variable |
| 154 | */ |
| 155 | TadsNetConfigVar *TadsNetConfig::set(const char *name, const char *val) |
| 156 | { |
| 157 | /* check for an existing variabel */ |
| 158 | TadsNetConfigVar *var = getvar(name); |
| 159 | if (var != 0) |
| 160 | { |
| 161 | /* found it - set the new value */ |
| 162 | var->setval(val); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | /* didn't find it - add a new variable */ |
| 167 | var = new TadsNetConfigVar(name, val); |
| 168 | if (last_var != 0) |
| 169 | last_var->setnext(var); |
| 170 | else |
| 171 | first_var = var; |
| 172 | last_var = var; |
| 173 | } |
| 174 | |
| 175 | /* return the variable */ |
| 176 | return var; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * retrieve a variable's value |
| 181 | */ |
| 182 | const char *TadsNetConfig::get(const char *name) const |
| 183 | { |
| 184 | TadsNetConfigVar *var = getvar(name); |
| 185 | return (var != 0 ? var->getval() : 0); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * look up a variable |
| 190 | */ |
| 191 | TadsNetConfigVar *TadsNetConfig::getvar(const char *name) const |
| 192 | { |
| 193 | /* scan the variable list for the given name */ |
| 194 | for (TadsNetConfigVar *cur = first_var ; cur != 0 ; cur = cur->getnext()) |
| 195 | { |
| 196 | /* if this one's name matches, return it */ |
| 197 | if (cur->matchname(name)) |
| 198 | return cur; |
| 199 | } |
| 200 | |
| 201 | /* didn't find it */ |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* ------------------------------------------------------------------------ */ |
| 206 | /* |
| 207 | * Check a storage server API reply |
| 208 | */ |
| 209 | void vmnet_check_storagesrv_reply(VMG_ int htmlstat, CVmDataSource *reply, |
| 210 | const char *headers) |
| 211 | { |
| 212 | /* retrieve and check the status code */ |
| 213 | vmnet_check_storagesrv_stat( |
| 214 | vmg_ vmnet_get_storagesrv_stat(vmg_ htmlstat, reply, headers)); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Check a storage server status code returned by |
| 219 | * vmnet_get_storagesrv_reply(). On failure, throw an error; on success, |
| 220 | * simply return. In either case, we'll free the status code buffer. |
| 221 | */ |
| 222 | void vmnet_check_storagesrv_stat(VMG_ char *stat) |
| 223 | { |
| 224 | /* if it's not "OK", throw an error */ |
| 225 | if (memcmp(stat, "OK ", 3) != 0) |
| 226 | { |
| 227 | /* |
| 228 | * Error. Push the error code/message text string as the argument |
| 229 | * to the runtime error constructor, then discard our copy of the |
| 230 | * buffer. |
| 231 | */ |
| 232 | G_interpreter->push_string(vmg_ stat); |
| 233 | t3free(stat); |
| 234 | |
| 235 | /* throw a StorageServerError */ |
| 236 | G_interpreter->throw_new_rtesub( |
| 237 | vmg_ G_predef->storage_server_error, 1, |
| 238 | VMERR_STORAGE_SERVER_ERR); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | /* success - discard the message text */ |
| 243 | t3free(stat); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Get the storage server API reply code and message. Returns an allocated |
| 249 | * buffer that the caller must free with t3free(). The first |
| 250 | * space-delimited token in the return buffer is the code, and the rest is |
| 251 | * the human-readable error message. For HTTP or network errors, the code |
| 252 | * is simply the numeric error code (positive for HTTP status codes, |
| 253 | * negative for internal network errors), with no message text. |
| 254 | */ |
| 255 | char *vmnet_get_storagesrv_stat(VMG_ int htmlstat, CVmDataSource *reply, |
| 256 | const char *headers) |
| 257 | { |
| 258 | /* check the HTML status */ |
| 259 | if (htmlstat == 200) |
| 260 | { |
| 261 | /* |
| 262 | * The HTML transaction succeeded - check the reply. Start with |
| 263 | * the headers, if provided. |
| 264 | */ |
| 265 | if (headers != 0) |
| 266 | { |
| 267 | /* find the X-IFDBStorage-Status header */ |
| 268 | for (const char *p = headers ; ; p += 2) |
| 269 | { |
| 270 | /* are we at our header? */ |
| 271 | if (memcmp(p, "X-IFDBStorage-Status:", 21) == 0) |
| 272 | { |
| 273 | /* this is our header - skip spaces and get the value */ |
| 274 | for (p += 21 ; isspace(*p) ; ++p) ; |
| 275 | |
| 276 | /* find the end of the line or end of the headers */ |
| 277 | const char *nl = strstr(p, "\r\n"); |
| 278 | if (nl == 0) |
| 279 | nl = p + strlen(p); |
| 280 | |
| 281 | /* return the message text */ |
| 282 | return lib_copy_str(p, nl - p); |
| 283 | } |
| 284 | |
| 285 | /* not our header - skip to the end of the line */ |
| 286 | p = strstr(p, "\r\n"); |
| 287 | if (p == 0) |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * We didn't find the header, so check the reply body. Read the |
| 294 | * first line of the reply, since this contains the result code. |
| 295 | */ |
| 296 | reply->seek(0, OSFSK_SET); |
| 297 | char *txt = reply->read_line_alo(); |
| 298 | |
| 299 | /* remove the trailing newline */ |
| 300 | size_t l; |
| 301 | if ((l = strlen(txt)) != 0 && txt[l-1] == '\n') |
| 302 | txt[--l] = '\0'; |
| 303 | |
| 304 | /* return the message text */ |
| 305 | return txt; |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | /* |
| 310 | * HTML or network error. Return a message containing the numeric |
| 311 | * status as the error code, with no text message. |
| 312 | */ |
| 313 | return t3sprintf_alloc("%d ", htmlstat); |
| 314 | } |
| 315 | } |