manual merge of fastcgi.c
[matches/MCTX3420.git] / server / fastcgi.c
1 /**
2  * @file fastcgi.c
3  * @brief Runs the FCGI request loop to handle web interface requests.
4  *
5  * fcgi_stdio.h must be included before all else so the stdio function
6  * redirection works ok.
7  */
8
9 #include <fcgi_stdio.h>
10 #include <openssl/sha.h>
11 #include <stdarg.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14
15 #include "common.h"
16 #include "sensor.h"
17 #include "actuator.h"
18 #include "control.h"
19 #include "options.h"
20 #include "image.h"
21 #include "pin_test.h"
22 #include "login.h"
23
24 /**The time period (in seconds) before the control key expires */
25 #define CONTROL_TIMEOUT 180
26
27
28
29 /**
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]
35  */ 
36 static void IdentifyHandler(FCGIContext *context, char *params)
37 {
38         bool ident_sensors = false, ident_actuators = false;
39         int i;
40
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))
44                 return;
45
46         FCGI_BeginJSON(context, STATUS_OK);
47         FCGI_JSONPair("description", "MCTX3420 Server API (2013)");
48         FCGI_JSONPair("build_date", __DATE__ " " __TIME__);
49         struct timespec t;
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);
54         
55         bool has_control = FCGI_HasControl(context);
56         FCGI_JSONBool("logged_in", has_control);
57         FCGI_JSONPair("user_name", has_control ? context->user_name : "");
58         
59
60         //Sensor and actuator information
61         if (ident_sensors) {
62                 FCGI_JSONKey("sensors");
63                 FCGI_JSONValue("{\n\t\t");
64                 for (i = 0; i < g_num_sensors; i++) {
65                         if (i > 0) {
66                                 FCGI_JSONValue(",\n\t\t");
67                         }
68                         FCGI_JSONValue("\"%d\" : \"%s\"", i, Sensor_GetName(i)); 
69                 }
70                 FCGI_JSONValue("\n\t}");
71         }
72         if (ident_actuators) {
73                 FCGI_JSONKey("actuators");
74                 FCGI_JSONValue("{\n\t\t");
75                 for (i = 0; i < g_num_actuators; i++) {
76                         if (i > 0) {
77                                 FCGI_JSONValue(",\n\t\t");
78                         }
79                         FCGI_JSONValue("\"%d\" : \"%s\"", i, Actuator_GetName(i)); 
80                 }
81                 FCGI_JSONValue("\n\t}");
82         }
83         FCGI_EndJSON();
84 }
85
86 /**
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)
93  */
94 bool FCGI_LockControl(FCGIContext *context, const char * user_name, UserType user_type) 
95 {
96         // Get current time
97         time_t now = time(NULL);
98         bool expired = now - context->control_timestamp > CONTROL_TIMEOUT;
99         int i;
100
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'))
104                 return false;
105
106         // Release any existing control (if any)
107         FCGI_ReleaseControl(context);
108
109         // Set timestamp
110         context->control_timestamp = now;
111
112         // Generate a SHA1 hash for the user
113         SHA_CTX sha1ctx;
114         unsigned char sha1[20];
115         i = rand();
116         SHA1_Init(&sha1ctx);
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]);
122
123         // Set the IPv4 address
124         snprintf(context->control_ip, 16, "%s", getenv("REMOTE_ADDR"));
125
126         // Set the user name
127         int uname_len = strlen(user_name);
128         i = snprintf(context->user_name, sizeof(context->user_name), "%s", user_name);
129         if (i < uname_len) {
130                 Log(LOGERR, "Username at %d characters too long (limit %d)", 
131                         uname_len, sizeof(context->user_name));
132                 return false; // :-(
133         }
134         // Set the user type
135         context->user_type = user_type;
136
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));
143                 return false;
144         }
145
146         Log(LOGDEBUG, "User dir: %s", context->user_dir);
147         // Create directory
148         if (mkdir(context->user_dir, 0777) != 0 && errno != EEXIST)
149         {
150                 Log(LOGERR, "Couldn't create user directory %s - %s", 
151                         context->user_dir, strerror(errno));
152                 return false; // :-(
153         }
154
155         return true; // :-)
156 }
157
158 /**
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
161  * updated.
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.
165  */
166 bool FCGI_HasControl(FCGIContext *context)
167 {
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);
172         if (result) {
173                 context->control_timestamp = now; //Update the control_timestamp
174         }
175         return result;
176 }
177
178
179 /**
180  * Revokes the current control key, if present.
181  * @param context The context to work in
182  */
183 void FCGI_ReleaseControl(FCGIContext *context)
184 {
185         *(context->control_key) = 0;
186         // Note: context->user_name should *not* be cleared
187         return;
188 }
189
190 /**
191  * Gets the control cookie
192  * @param buffer A storage buffer of exactly CONTROL_KEY_BUFSIZ length to
193                  store the control key
194  */
195 void FCGI_GetControlCookie(char buffer[CONTROL_KEY_BUFSIZ])
196 {
197         const char *cookies = getenv("COOKIE_STRING");
198         const char *start = strstr(cookies, "mctxkey=");
199
200         if (start != NULL) {
201                 const char *end;
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;
207                 }
208                 snprintf(buffer, limit, "%s", start);
209                 Log(LOGDEBUG, "buf: %s", buffer);
210         } else {
211                 *buffer = 0;
212         }
213 }
214
215 /**
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
219  */
220 void FCGI_SendControlCookie(FCGIContext *context, bool set) {
221         if (set) {
222                 printf("Set-Cookie: mctxkey=%s\r\n", context->control_key);
223         } else {
224                 printf("Set-Cookie: mctxkey=\r\n");
225         }
226 }
227
228 /**
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.
236  */
237 char *FCGI_KeyPair(char *in, const char **key, const char **value)
238 {
239         char *ptr;
240         if (!in || !*in) { //Invalid input or string is EOL
241                 return NULL;
242         }
243
244         *key = in;
245         //Find either = or &, whichever comes first
246         if ((ptr = strpbrk(in, "=&"))) {
247                 if (*ptr == '&') { //No value specified
248                         *value = ptr;
249                         *ptr++ = 0;
250                 } else {
251                         //Stopped at an '=' sign
252                         *ptr++ = 0;
253                         *value = ptr;
254                         if ((ptr = strchr(ptr,'&'))) {
255                                 *ptr++ = 0;
256                         } else {
257                                 ptr = "";
258                         }
259                 }
260         } else { //No value specified and no other pair
261                 ptr = "";
262                 *value = ptr;
263         }
264         return ptr;
265 }
266
267 /**
268  * Aids in parsing request parameters. 
269  * Input: The expected keys along with their type and whether or not
270  * they're required.
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.
282  */
283 bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], size_t count)
284 {
285         const char *key, *value;
286         char buf[BUFSIZ], *ptr;
287         size_t i;
288         
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];
293
294                                 if (FCGI_RECEIVED(val->flags)) {
295                                         snprintf(buf, BUFSIZ, "Value already specified for '%s'.", key);
296                                         FCGI_RejectJSON(context, buf);
297                                         return false;
298                                 }
299                                 val->flags |= FCGI_PARAM_RECEIVED;
300
301                                 switch(FCGI_TYPE(val->flags)) {
302                                         case FCGI_BOOL_T:
303                                                 if (!*value) //No value: Default true
304                                                         *((bool*) val->value) = true;
305                                                 else {
306                                                         *((bool*) val->value) = !!(strtol(value, &ptr, 10));
307                                                         if (*ptr) {
308                                                                 snprintf(buf, BUFSIZ, "Expected bool for '%s' but got '%s'", key, value);
309                                                                 FCGI_RejectJSON(context, buf);
310                                                                 return false;
311                                                         }
312                                                 }
313                                                 break;
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);
319                                                         return false;
320                                                 }
321
322                                                 if (FCGI_TYPE(val->flags) == FCGI_INT_T)
323                                                         *((int*) val->value) = (int) parsed;
324                                                 else
325                                                         *((long*) val->value) = parsed;
326                                         }       break;
327                                         case FCGI_DOUBLE_T:
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);
332                                                         return false;
333                                                 }
334                                                 break;
335                                         case FCGI_STRING_T:
336                                                 *((const char**) val->value) = value;
337                                                 break;
338                                         default:
339                                                 Fatal("Invalid type %d given", FCGI_TYPE(val->flags));
340                                 }
341                                 break; //No need to search any more
342                         }
343                 } //End for loop
344                 if (i == count) {
345                         snprintf(buf, BUFSIZ, "Unknown key '%s' specified", key);
346                         FCGI_RejectJSON(context, buf);
347                         return false;
348                 }
349         }
350
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);
356                         return false;
357                 }
358         }
359         return true;
360 }
361
362 /**
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.
366  */
367 void FCGI_BeginJSON(FCGIContext *context, StatusCodes status_code)
368 {
369         printf("Content-type: application/json; charset=utf-8\r\n\r\n");
370         printf("{\r\n");
371         printf("\t\"module\" : \"%s\"", context->current_module);
372         FCGI_JSONLong("status", status_code);
373         //Time and running statistics
374         struct timespec now;
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());
380 }
381
382 /**
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.
387  */
388 void FCGI_AcceptJSON(FCGIContext *context, const char *description)
389 {
390         printf("Content-type: application/json; charset=utf-8\r\n");
391         printf("\r\n{\r\n");
392         printf("\t\"module\" : \"%s\"", context->current_module);
393         FCGI_JSONLong("status", STATUS_OK);
394         FCGI_JSONPair("description", description);
395         FCGI_EndJSON();
396 }
397
398 /**
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.
403  */
404 void FCGI_JSONPair(const char *key, const char *value)
405 {
406         printf(",\r\n\t\"%s\" : \"%s\"", key, value);
407 }
408
409 /**
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
413  */
414 void FCGI_JSONLong(const char *key, long value)
415 {
416         printf(",\r\n\t\"%s\" : %ld", key, value);
417 }
418
419 /**
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
423  */
424 void FCGI_JSONDouble(const char *key, double value)
425 {
426         printf(",\r\n\t\"%s\" : %.9f", key, value);
427 }
428
429 /**
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
433  */
434 void FCGI_JSONBool(const char *key, bool value)
435 {
436         printf(",\r\n\t\"%s\" : %s", key, value ? "true" : "false");
437 }
438
439 /**
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
443  */
444 void FCGI_JSONKey(const char *key)
445 {
446         printf(",\r\n\t\"%s\" : ", key);
447 }
448
449 /**
450  * Ends a JSON response that was initiated by FCGI_BeginJSON.
451  */
452 void FCGI_EndJSON() 
453 {
454         printf("\r\n}\r\n");
455 }
456
457 /**
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.
463  */
464 void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *description)
465 {
466         if (description == NULL)
467                 description = "Unknown";
468         
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"));
477         FCGI_EndJSON();
478 }
479
480 /**
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.
487  */
488 void FCGI_PrintRaw(const char *format, ...)
489 {
490         va_list list;
491         va_start(list, format);
492         vprintf(format, list);
493         va_end(list);
494 }
495
496
497 /**
498  * Write binary data
499  * See fwrite
500  */
501 void FCGI_WriteBinary(void * data, size_t size, size_t num_elem)
502 {
503         Log(LOGDEBUG,"Writing!");
504         fwrite(data, size, num_elem, stdout);
505 }
506
507 /**
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)
513  */
514 char *FCGI_EscapeText(char *buf)
515 {
516         int length, i;
517         length = strlen(buf);
518         
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
522                         buf[i] = ' ';
523                 } else if (buf[i] == '"') {
524                         if (i-1 >= 0 && buf[i-1] == '\\') 
525                                 i--;
526                         else
527                                 buf[i] = '\'';
528                 } else if (buf[i] == '\\') {
529                         if (i-1 >= 0 && buf[i-1] == '\'')
530                                 i--;
531                         else
532                                 buf[i] = ' ';
533                 }
534         }
535         return buf;
536 }
537
538 /**
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!
543  */ 
544 void * FCGI_RequestLoop (void *data)
545 {
546         FCGIContext context = {0};
547         
548         Log(LOGDEBUG, "Start loop");
549         while (FCGI_Accept() >= 0) {
550                 
551                 ModuleHandler module_handler = NULL;
552                 char module[BUFSIZ], params[BUFSIZ];
553                 
554                 //strncpy doesn't zero-truncate properly
555                 snprintf(module, BUFSIZ, "%s", getenv("DOCUMENT_URI_LOCAL"));
556                 snprintf(params, BUFSIZ, "%s", getenv("QUERY_STRING"));
557
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);
561
562                 
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;
567
568                 //Default to the 'identify' module if none specified
569                 if (!*module) 
570                         strcpy(module, "identify");
571                 
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;
588                 }
589
590                 context.current_module = module;
591                 context.response_number++;
592                 
593                 if (module_handler) 
594                 {
595                         if (g_options.auth_method != AUTH_NONE && module_handler != Login_Handler && module_handler != IdentifyHandler && module_handler)
596                         //if (false) // Testing
597                         {
598                                 if (!FCGI_HasControl(&context))
599                                 {
600                                         FCGI_RejectJSON(&context, "Please login. Invalid control key.");
601                                         continue;       
602                                 }
603
604                                 //Escape all special characters.
605                                 //Don't escape for login (password may have special chars?)
606                                 FCGI_EscapeText(params);
607                         }
608
609                         module_handler(&context, params);
610                 } 
611                 else 
612                 {
613                         FCGI_RejectJSON(&context, "Unhandled module");
614                 }
615         }
616
617         Log(LOGDEBUG, "Thread exiting.");
618         // NOTE: Don't call pthread_exit, because this runs in the main thread. Just return.
619         return NULL;
620 }

UCC git Repository :: git.ucc.asn.au