cms.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include "auth.h"
  11. #include "list.h"
  12. #include "util.h"
  13. #define h1(msg, ...) header(1,msg,__VA_ARGS__)
  14. #define err(n, msg) h1("Error %d: %s", n, msg)
  15. int logfd;
  16. static void header(int n, char* msg, ...) {
  17. char buf[MAXLEN_HEADER];
  18. va_list ap;
  19. memset(buf, 0, MAXLEN_HEADER);
  20. va_start(ap, msg);
  21. vsnprintf(buf, MAXLEN_HEADER, msg, ap);
  22. va_end(ap);
  23. printf("<h%d>%s</h%d>", n, buf, n);
  24. }
  25. static int handle_error(int err) {
  26. switch (err) {
  27. case 400: err(400, "Bad request"); break;
  28. case 403: err(403, "Forbidden"); break;
  29. case 500: err(500, "Interal error"); break;
  30. default: err(err, "Unknown"); break;
  31. }
  32. return err;
  33. }
  34. static char* handle_get() { return getenv("QUERY_STRING"); }
  35. static char* handle_post() {
  36. char* buf = malloc(MAXLEN_POST); memset(buf, 0, MAXLEN_POST);
  37. char* l = getenv("INPUT_LENGTH");
  38. if (!l) { return NULL; }
  39. int len = atol(l);
  40. if (!len) { return NULL; }
  41. //When MAXLEN_POST needs to be raised, replace code below with loop to read data in chunks
  42. len = min(len, (MAXLEN_POST-1));
  43. fread(buf, len, 1, stdin);
  44. return buf;
  45. }
  46. static void parse_request(char* request, LIST* list) {
  47. if (!strchr(request, '&')) { //Just one pair
  48. listQueue(list, request);
  49. return;
  50. }
  51. for (char* pair = strtok(request, "&"); pair; pair = strtok(NULL, "&")) {
  52. listQueue(list, (void*)pair);
  53. }
  54. }
  55. static char* getkey(char* pair) {
  56. char* k = malloc(MAXLEN_KEY);
  57. memset(k, 0, MAXLEN_KEY);
  58. int l = 0;
  59. while (l < (MAXLEN_KEY-1) && pair[l] != '=') {
  60. *k = pair[l];
  61. k++; l++;
  62. }
  63. return (k-l);
  64. }
  65. static char* getval(char* pair) {
  66. char* v = strndup(pair, MAXLEN_REQUEST);
  67. while (v && (*v != '=')) { v++; }
  68. return v;
  69. }
  70. static char* find_request(char* method) {
  71. if (!strncmp(method, "GET", 3)) { return handle_get(); }
  72. if (!strncmp(method, "POST", 3)) { return handle_post(); }
  73. return NULL;
  74. }
  75. int main() {
  76. init_prng();
  77. logfd = open("/var/www/log/log.txt", O_APPEND|O_CREAT|O_DSYNC);
  78. if (logfd == -1) { return handle_error(500); }
  79. printf("Content-type: text/html\n\n");
  80. printf("<html><body>");
  81. char* request; char* pair; char* key; char* val;
  82. char* method = getenv("REQUEST_METHOD");
  83. authdb* db = new_authdb("/var/www/data/test.db");
  84. if (!db) { return handle_error(500); }
  85. LIST* args = newList();
  86. if (!method) { return handle_error(400); }
  87. request = find_request(method);
  88. if (!request) { return handle_error(400); }
  89. logs("Full request:%s", request);
  90. parse_request(request,args);
  91. LIST_ITERATOR* i = newListIterator(args);
  92. ITERATE_LIST(pair, i) {
  93. key = getkey(pair);
  94. val = getval(pair);
  95. logs("%s=%s", key, val);
  96. } deleteListIterator(i);
  97. if (register_user(db, "admin", "password", "admin")) { logs("main: register_user failed"); }
  98. else if (user_addperm(db, "admin", "site.login")) { logs("main: user_addperm failed"); }
  99. else if (user_has_perm(db, "admin", "site.login")) { logs("main: user_has_perm failed"); }
  100. else if (user_setpass(db, "admin", "hello")) { logs("main: user_setpass failed"); }
  101. else if (user_validate(db, "admin", "hello")) { logs("main: user_validate failed"); }
  102. //Below will later be moved into a view system
  103. printf("Hello, world! DB @ [%p]<br/>", db);
  104. printf("Request method: %s<br/>", method);
  105. printf("</body></html>");
  106. close(logfd);
  107. return 0;
  108. }