cgi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * uhttpd - Tiny single-threaded httpd
  3. *
  4. * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
  5. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #define _GNU_SOURCE
  20. #include <libubox/blobmsg.h>
  21. #include "uhttpd.h"
  22. static LIST_HEAD(interpreters);
  23. void uh_interpreter_add(const char *ext, const char *path)
  24. {
  25. struct interpreter *in;
  26. char *new_ext, *new_path;
  27. in = calloc_a(sizeof(*in),
  28. &new_ext, strlen(ext) + 1,
  29. &new_path, strlen(path) + 1);
  30. in->ext = strcpy(new_ext, ext);
  31. in->path = strcpy(new_path, path);
  32. list_add_tail(&in->list, &interpreters);
  33. }
  34. static void cgi_main(struct client *cl, struct path_info *pi, char *url)
  35. {
  36. const struct interpreter *ip = pi->ip;
  37. struct env_var *var;
  38. clearenv();
  39. setenv("PATH", conf.cgi_path, 1);
  40. for (var = uh_get_process_vars(cl, pi); var->name; var++) {
  41. if (!var->value)
  42. continue;
  43. setenv(var->name, var->value, 1);
  44. }
  45. if (!chdir(pi->root)) {
  46. if (ip)
  47. execl(ip->path, ip->path, pi->phys, NULL);
  48. else
  49. execl(pi->phys, pi->phys, NULL);
  50. }
  51. printf("Status: 500 Internal Server Error\r\n\r\n"
  52. "Unable to launch the requested CGI program:\n"
  53. " %s: %s\n", ip ? ip->path : pi->phys, strerror(errno));
  54. }
  55. static void cgi_handle_request(struct client *cl, char *url, struct path_info *pi)
  56. {
  57. unsigned int mode = S_IFREG | S_IXOTH;
  58. char *escaped_url;
  59. if (!pi->ip && !((pi->stat.st_mode & mode) == mode)) {
  60. escaped_url = uh_htmlescape(url);
  61. uh_client_error(cl, 403, "Forbidden",
  62. "You don't have permission to access %s on this server.",
  63. escaped_url ? escaped_url : "the url");
  64. if (escaped_url)
  65. free(escaped_url);
  66. return;
  67. }
  68. if (!uh_create_process(cl, pi, url, cgi_main)) {
  69. uh_client_error(cl, 500, "Internal Server Error",
  70. "Failed to create CGI process: %s", strerror(errno));
  71. return;
  72. }
  73. return;
  74. }
  75. static bool check_cgi_path(struct path_info *pi, const char *url)
  76. {
  77. struct interpreter *ip;
  78. const char *path = pi->phys;
  79. int path_len = strlen(path);
  80. list_for_each_entry(ip, &interpreters, list) {
  81. int len = strlen(ip->ext);
  82. if (len >= path_len)
  83. continue;
  84. if (strcmp(path + path_len - len, ip->ext) != 0)
  85. continue;
  86. pi->ip = ip;
  87. return true;
  88. }
  89. pi->ip = NULL;
  90. if (conf.cgi_docroot_path)
  91. return uh_path_match(conf.cgi_docroot_path, pi->phys);
  92. return false;
  93. }
  94. struct dispatch_handler cgi_dispatch = {
  95. .script = true,
  96. .check_path = check_cgi_path,
  97. .handle_request = cgi_handle_request,
  98. };