3 * @brief Runs the FCGI request loop to handle web interface requests.
5 * fcgi_stdio.h must be included before all else so the stdio function
6 * redirection works ok.
9 #include <fcgi_stdio.h>
10 #include <openssl/sha.h>
12 #include <sys/types.h>
24 /**The time period (in seconds) before the control key expires */
25 #define CONTROL_TIMEOUT 180
30 * Identifies build information and the current API version to the user.
31 * Also useful for testing that the API is running and identifying the
32 * sensors and actuators present.
33 * @param context The context to work in
34 * @param params User specified paramters: [actuators, sensors]
36 static void IdentifyHandler(FCGIContext *context, char *params)
38 bool ident_sensors = false, ident_actuators = false;
41 FCGIValue values[2] = {{"sensors", &ident_sensors, FCGI_BOOL_T},
42 {"actuators", &ident_actuators, FCGI_BOOL_T}};
43 if (!FCGI_ParseRequest(context, params, values, 2))
46 FCGI_BeginJSON(context, STATUS_OK);
47 FCGI_JSONPair("description", "MCTX3420 Server API (2013)");
48 FCGI_JSONPair("build_date", __DATE__ " " __TIME__);
50 t.tv_sec = 0; t.tv_nsec = 0;
51 clock_getres(CLOCK_MONOTONIC, &t);
52 FCGI_JSONDouble("clock_getres", TIMEVAL_TO_DOUBLE(t));
53 FCGI_JSONLong("api_version", API_VERSION);
55 bool has_control = FCGI_HasControl(context);
56 FCGI_JSONBool("logged_in", has_control);
57 FCGI_JSONPair("user_name", has_control ? context->user_name : "");
60 //Sensor and actuator information
62 FCGI_JSONKey("sensors");
63 FCGI_JSONValue("{\n\t\t");
64 for (i = 0; i < g_num_sensors; i++) {
66 FCGI_JSONValue(",\n\t\t");
68 FCGI_JSONValue("\"%d\" : \"%s\"", i, Sensor_GetName(i));
70 FCGI_JSONValue("\n\t}");
72 if (ident_actuators) {
73 FCGI_JSONKey("actuators");
74 FCGI_JSONValue("{\n\t\t");
75 for (i = 0; i < g_num_actuators; i++) {
77 FCGI_JSONValue(",\n\t\t");
79 FCGI_JSONValue("\"%d\" : \"%s\"", i, Actuator_GetName(i));
81 FCGI_JSONValue("\n\t}");
87 * Given an authorised user, attempt to set the control over the system.
88 * Modifies members in the context structure appropriately if successful.
89 * @param context The context to work in
90 * @param user_name - Name of the user
91 * @param user_type - Type of the user, passed after successful authentication
92 * @return true on success, false otherwise (eg someone else already in control)
94 bool FCGI_LockControl(FCGIContext *context, const char * user_name, UserType user_type)
97 time_t now = time(NULL);
98 bool expired = now - context->control_timestamp > CONTROL_TIMEOUT;
101 // Can't lock control if: User not actually logged in (sanity), or key is still valid and the user is not an admin
102 if (user_type == USER_UNAUTH ||
103 (user_type != USER_ADMIN && !expired && *(context->control_key) != '\0'))
106 // Release any existing control (if any)
107 FCGI_ReleaseControl(context);
110 context->control_timestamp = now;
112 // Generate a SHA1 hash for the user
114 unsigned char sha1[20];
117 SHA1_Update(&sha1ctx, &now, sizeof(now));
118 SHA1_Update(&sha1ctx, &i, sizeof(i));
119 SHA1_Final(sha1, &sha1ctx);
120 for (i = 0; i < sizeof(sha1); i++)
121 sprintf(context->control_key + i * 2, "%02x", sha1[i]);
123 // Set the IPv4 address
124 snprintf(context->control_ip, 16, "%s", getenv("REMOTE_ADDR"));
127 int uname_len = strlen(user_name);
128 i = snprintf(context->user_name, sizeof(context->user_name), "%s", user_name);
130 Log(LOGERR, "Username at %d characters too long (limit %d)",
131 uname_len, sizeof(context->user_name));
135 context->user_type = user_type;
137 // Build the user directory
138 i = snprintf(context->user_dir, sizeof(context->user_dir), "%s/%s",
139 g_options.experiment_dir, context->user_name);
140 if (i >= sizeof(context->user_dir)) {
141 Log(LOGERR, "Experiment dir too long (required %d, limit %d)",
142 i, sizeof(context->user_dir));
146 Log(LOGDEBUG, "User dir: %s", context->user_dir);
148 if (mkdir(context->user_dir, 0777) != 0 && errno != EEXIST)
150 Log(LOGERR, "Couldn't create user directory %s - %s",
151 context->user_dir, strerror(errno));
159 * Given an FCGIContext, determines if the current user (as specified by
160 * the key) has control or not. If validated, the context control_timestamp is
162 * @param context The context to work in
163 * @param key The control key to be validated.
164 * @return TRUE if authorized, FALSE if not.
166 bool FCGI_HasControl(FCGIContext *context)
168 time_t now = time(NULL);
169 int result = (now - context->control_timestamp) <= CONTROL_TIMEOUT &&
170 context->control_key[0] != '\0' &&
171 !strcmp(context->control_key, context->received_key);
173 context->control_timestamp = now; //Update the control_timestamp
180 * Revokes the current control key, if present.
181 * @param context The context to work in
183 void FCGI_ReleaseControl(FCGIContext *context)
185 *(context->control_key) = 0;
186 // Note: context->user_name should *not* be cleared
191 * Gets the control cookie
192 * @param buffer A storage buffer of exactly CONTROL_KEY_BUFSIZ length to
193 store the control key
195 void FCGI_GetControlCookie(char buffer[CONTROL_KEY_BUFSIZ])
197 const char *cookies = getenv("COOKIE_STRING");
198 const char *start = strstr(cookies, "mctxkey=");
202 size_t limit = CONTROL_KEY_BUFSIZ;
203 start += 8; //Ah, magic numbers (the length of mctxkey= - 1)
204 end = strchr(start, ';');
205 if (end != NULL && (end-start) < CONTROL_KEY_BUFSIZ) {
206 limit = (end-start) + 1;
208 snprintf(buffer, limit, "%s", start);
209 Log(LOGDEBUG, "buf: %s", buffer);
216 * Sends the control key to the user as a cookie.
217 * @param context the context to work in
218 * @param set Whether to set or unset the control cookie
220 void FCGI_SendControlCookie(FCGIContext *context, bool set) {
222 printf("Set-Cookie: mctxkey=%s\r\n", context->control_key);
224 printf("Set-Cookie: mctxkey=\r\n");
229 * Extracts a key/value pair from a request string.
230 * Note that the input is modified by this function.
231 * @param in The string from which to extract the pair
232 * @param key A pointer to a variable to hold the key string
233 * @param value A pointer to a variable to hold the value string
234 * @return A pointer to the start of the next search location, or NULL if
235 * the EOL is reached.
237 char *FCGI_KeyPair(char *in, const char **key, const char **value)
240 if (!in || !*in) { //Invalid input or string is EOL
245 //Find either = or &, whichever comes first
246 if ((ptr = strpbrk(in, "=&"))) {
247 if (*ptr == '&') { //No value specified
251 //Stopped at an '=' sign
254 if ((ptr = strchr(ptr,'&'))) {
260 } else { //No value specified and no other pair
268 * Aids in parsing request parameters.
269 * Input: The expected keys along with their type and whether or not
271 * @param context The context to work in
272 * @param params The parameter string to be parsed
273 * @param values An array of FCGIValue's that specify expected keys
274 * @param count The number of elements in 'values'.
275 * @return true If the parameter string was parsed successfully, false otherwise.
276 * Modes of failure include: Invalid a parsing error on the value,
277 * an unknown key is specified,
278 * a key/value pair is specified more than once, or
279 * not all required keys were present.
280 * If this function returns false, it is guaranteed that FCGI_RejectJSON
281 * has already been called with the appropriate description message.
283 bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], size_t count)
285 const char *key, *value;
286 char buf[BUFSIZ], *ptr;
289 while ((params = FCGI_KeyPair(params, &key, &value))) {
290 for (i = 0; i < count; i++) {
291 if (!strcmp(key, values[i].key)) {
292 FCGIValue *val = &values[i];
294 if (FCGI_RECEIVED(val->flags)) {
295 snprintf(buf, BUFSIZ, "Value already specified for '%s'.", key);
296 FCGI_RejectJSON(context, buf);
299 val->flags |= FCGI_PARAM_RECEIVED;
301 switch(FCGI_TYPE(val->flags)) {
303 if (!*value) //No value: Default true
304 *((bool*) val->value) = true;
306 *((bool*) val->value) = !!(strtol(value, &ptr, 10));
308 snprintf(buf, BUFSIZ, "Expected bool for '%s' but got '%s'", key, value);
309 FCGI_RejectJSON(context, buf);
314 case FCGI_INT_T: case FCGI_LONG_T: {
315 long parsed = strtol(value, &ptr, 10);
316 if (!*value || *ptr) {
317 snprintf(buf, BUFSIZ, "Expected int for '%s' but got '%s'", key, value);
318 FCGI_RejectJSON(context, buf);
322 if (FCGI_TYPE(val->flags) == FCGI_INT_T)
323 *((int*) val->value) = (int) parsed;
325 *((long*) val->value) = parsed;
328 *((double*) val->value) = strtod(value, &ptr);
329 if (!*value || *ptr) {
330 snprintf(buf, BUFSIZ, "Expected float for '%s' but got '%s'", key, value);
331 FCGI_RejectJSON(context, buf);
336 *((const char**) val->value) = value;
339 Fatal("Invalid type %d given", FCGI_TYPE(val->flags));
341 break; //No need to search any more
345 snprintf(buf, BUFSIZ, "Unknown key '%s' specified", key);
346 FCGI_RejectJSON(context, buf);
351 //Check that required parameters are received
352 for (i = 0; i < count; i++) {
353 if (FCGI_IS_REQUIRED(values[i].flags) && !FCGI_RECEIVED(values[i].flags)) {
354 snprintf(buf, BUFSIZ, "Key '%s' required, but was not given.", values[i].key);
355 FCGI_RejectJSON(context, buf);
363 * Begins a response to the client in JSON format.
364 * @param context The context to work in.
365 * @param status_code The status code to be returned.
367 void FCGI_BeginJSON(FCGIContext *context, StatusCodes status_code)
369 printf("Content-type: application/json; charset=utf-8\r\n\r\n");
371 printf("\t\"module\" : \"%s\"", context->current_module);
372 FCGI_JSONLong("status", status_code);
373 //Time and running statistics
375 clock_gettime(CLOCK_MONOTONIC, &now);
376 FCGI_JSONDouble("start_time", TIMEVAL_TO_DOUBLE(g_options.start_time));
377 FCGI_JSONDouble("current_time", TIMEVAL_TO_DOUBLE(now));
378 FCGI_JSONDouble("running_time", TIMEVAL_DIFF(now, g_options.start_time));
379 FCGI_JSONPair("control_state", Control_GetModeName());
383 * Generic accept response in JSON format.
384 * @param context The context to work in
385 * @param description A short description.
386 * @param cookie Optional. If given, the cookie field is set to that value.
388 void FCGI_AcceptJSON(FCGIContext *context, const char *description)
390 printf("Content-type: application/json; charset=utf-8\r\n");
392 printf("\t\"module\" : \"%s\"", context->current_module);
393 FCGI_JSONLong("status", STATUS_OK);
394 FCGI_JSONPair("description", description);
399 * Adds a key/value pair to a JSON response. The response must have already
400 * been initiated by FCGI_BeginJSON. Special characters are not escaped.
401 * @param key The key of the JSON entry
402 * @param value The value associated with the key.
404 void FCGI_JSONPair(const char *key, const char *value)
406 printf(",\r\n\t\"%s\" : \"%s\"", key, value);
410 * Similar to FCGI_JSONPair except for signed integer values.
411 * @param key The key of the JSON entry
412 * @param value The value associated with the key
414 void FCGI_JSONLong(const char *key, long value)
416 printf(",\r\n\t\"%s\" : %ld", key, value);
420 * Similar to FCGI_JsonPair except for floating point values.
421 * @param key The key of the JSON entry
422 * @param value The value associated with the key
424 void FCGI_JSONDouble(const char *key, double value)
426 printf(",\r\n\t\"%s\" : %.9f", key, value);
430 * Similar to FCGI_JsonPair except for boolean values.
431 * @param key The key of the JSON entry
432 * @param value The value associated with the key
434 void FCGI_JSONBool(const char *key, bool value)
436 printf(",\r\n\t\"%s\" : %s", key, value ? "true" : "false");
440 * Begins a JSON entry by writing the key. To be used in conjunction
441 * with FCGI_JsonValue.
442 * @param key The key of the JSON entry
444 void FCGI_JSONKey(const char *key)
446 printf(",\r\n\t\"%s\" : ", key);
450 * Ends a JSON response that was initiated by FCGI_BeginJSON.
458 * To be used when the input parameters are rejected. The return data
459 * will also have debugging information provided.
460 * @param context The context to work in
461 * @param status The status the return data should have.
462 * @param description A short description of why the input was rejected.
464 void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *description)
466 if (description == NULL)
467 description = "Unknown";
469 Log(LOGINFO, "%s: Rejected query with: %d: %s", context->current_module, status, description);
470 FCGI_BeginJSON(context, status);
471 FCGI_JSONPair("description", description);
472 FCGI_JSONLong("responsenumber", context->response_number);
473 //FCGI_JSONPair("params", getenv("QUERY_STRING")); //A bad idea if contains password but also if contains unescaped stuff
474 FCGI_JSONPair("host", getenv("SERVER_HOSTNAME"));
475 FCGI_JSONPair("user", getenv("REMOTE_USER"));
476 FCGI_JSONPair("ip", getenv("REMOTE_ADDR"));
481 * Generates a response to the client as described by the format parameter and
482 * extra arguments (exactly like printf). To be used when none of the other
483 * predefined functions will work exactly as needed. Extra care should be taken
484 * to ensure the correctness of the output.
485 * @param format The format string
486 * @param ... Any extra arguments as required by the format string.
488 void FCGI_PrintRaw(const char *format, ...)
491 va_start(list, format);
492 vprintf(format, list);
501 void FCGI_WriteBinary(void * data, size_t size, size_t num_elem)
503 Log(LOGDEBUG,"Writing!");
504 fwrite(data, size, num_elem, stdout);
508 * Escapes a string so it can be used safely.
509 * Currently escapes to ensure the validity for use as a JSON string
510 * Does not support unicode specifiers in the form of \uXXXX.
511 * @param buf The string to be escaped
512 * @return The escaped string (return value == buf)
514 char *FCGI_EscapeText(char *buf)
517 length = strlen(buf);
519 //Escape special characters. Must count down to escape properly
520 for (i = length - 1; i >= 0; i--) {
521 if (buf[i] < 0x20) { //Control characters
523 } else if (buf[i] == '"') {
524 if (i-1 >= 0 && buf[i-1] == '\\')
528 } else if (buf[i] == '\\') {
529 if (i-1 >= 0 && buf[i-1] == '\'')
539 * Main FCGI request loop that receives/responds to client requests.
540 * @param data Reserved.
541 * @returns NULL (void* required for consistency with pthreads, although at the moment this runs in the main thread anyway)
542 * TODO: Get this to exit with the rest of the program!
544 void * FCGI_RequestLoop (void *data)
546 FCGIContext context = {0};
548 Log(LOGDEBUG, "Start loop");
549 while (FCGI_Accept() >= 0) {
551 ModuleHandler module_handler = NULL;
552 char module[BUFSIZ], params[BUFSIZ];
554 //strncpy doesn't zero-truncate properly
555 snprintf(module, BUFSIZ, "%s", getenv("DOCUMENT_URI_LOCAL"));
556 snprintf(params, BUFSIZ, "%s", getenv("QUERY_STRING"));
558 FCGI_GetControlCookie(context.received_key);
559 Log(LOGDEBUG, "Got request #%d - Module %s, params %s", context.response_number, module, params);
560 Log(LOGDEBUG, "Control key: %s", context.received_key);
563 //Remove trailing slashes (if present) from module query
564 size_t lastchar = strlen(module) - 1;
565 if (lastchar > 0 && module[lastchar] == '/')
566 module[lastchar] = 0;
568 //Default to the 'identify' module if none specified
570 strcpy(module, "identify");
572 if (!strcmp("identify", module)) {
573 module_handler = IdentifyHandler;
574 } else if (!strcmp("control", module)) {
575 module_handler = Control_Handler;
576 } else if (!strcmp("sensors", module)) {
577 module_handler = Sensor_Handler;
578 } else if (!strcmp("actuators", module)) {
579 module_handler = Actuator_Handler;
580 } else if (!strcmp("image", module)) {
581 module_handler = Image_Handler;
582 } else if (!strcmp("pin", module)) {
583 module_handler = Pin_Handler; // *Debug only* pin test module
584 } else if (!strcmp("bind", module)) {
585 module_handler = Login_Handler;
586 } else if (!strcmp("unbind", module)) {
587 module_handler = Logout_Handler;
590 context.current_module = module;
591 context.response_number++;
595 if (g_options.auth_method != AUTH_NONE && module_handler != Login_Handler && module_handler != IdentifyHandler && module_handler)
596 //if (false) // Testing
598 if (!FCGI_HasControl(&context))
600 FCGI_RejectJSON(&context, "Please login. Invalid control key.");
604 //Escape all special characters.
605 //Don't escape for login (password may have special chars?)
606 FCGI_EscapeText(params);
609 module_handler(&context, params);
613 FCGI_RejectJSON(&context, "Unhandled module");
617 Log(LOGDEBUG, "Thread exiting.");
618 // NOTE: Don't call pthread_exit, because this runs in the main thread. Just return.