[BACK]Return to cgi.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/cgi.c between version 1.74 and 1.80

version 1.74, 2014/07/18 14:46:25 version 1.80, 2014/07/22 18:14:13
Line 71  static void   pg_noresult(const struct req *, const ch
Line 71  static void   pg_noresult(const struct req *, const ch
 static  void             pg_search(const struct req *);  static  void             pg_search(const struct req *);
 static  void             pg_searchres(const struct req *,  static  void             pg_searchres(const struct req *,
                                 struct manpage *, size_t);                                  struct manpage *, size_t);
 static  void             pg_show(const struct req *, const char *);  static  void             pg_show(struct req *, const char *);
 static  void             resp_begin_html(int, const char *);  static  void             resp_begin_html(int, const char *);
 static  void             resp_begin_http(int, const char *);  static  void             resp_begin_http(int, const char *);
 static  void             resp_end_html(void);  static  void             resp_end_html(void);
Line 79  static void   resp_searchform(const struct req *);
Line 79  static void   resp_searchform(const struct req *);
 static  void             resp_show(const struct req *, const char *);  static  void             resp_show(const struct req *, const char *);
   
 static  const char       *scriptname; /* CGI script name */  static  const char       *scriptname; /* CGI script name */
 static  const char       *httphost; /* hostname used in the URIs */  
   
 static  const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};  static  const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
 static  const char *const sec_numbers[] = {  static  const char *const sec_numbers[] = {
Line 467  resp_searchform(const struct req *req)
Line 466  resp_searchform(const struct req *req)
         puts("<!-- End search form. //-->");          puts("<!-- End search form. //-->");
 }  }
   
   static int
   validate_urifrag(const char *frag)
   {
   
           while ('\0' != *frag) {
                   if ( ! (isalnum((unsigned char)*frag) ||
                       '-' == *frag || '.' == *frag ||
                       '/' == *frag || '_' == *frag))
                           return(0);
                   frag++;
           }
           return(1);
   }
   
   static int
   validate_manpath(const struct req *req, const char* manpath)
   {
           size_t   i;
   
           if ( ! strcmp(manpath, "mandoc"))
                   return(1);
   
           for (i = 0; i < req->psz; i++)
                   if ( ! strcmp(manpath, req->p[i]))
                           return(1);
   
           return(0);
   }
   
   static int
   validate_filename(const char *file)
   {
   
           if ('.' == file[0] && '/' == file[1])
                   file += 2;
   
           return ( ! (strstr(file, "../") || strstr(file, "/..") ||
               (strncmp(file, "man", 3) && strncmp(file, "cat", 3))));
   }
   
 static void  static void
 pg_index(const struct req *req)  pg_index(const struct req *req)
 {  {
Line 524  pg_searchres(const struct req *req, struct manpage *r,
Line 563  pg_searchres(const struct req *req, struct manpage *r,
         int              prio, priouse;          int              prio, priouse;
         char             sec;          char             sec;
   
           for (i = 0; i < sz; i++) {
                   if (validate_filename(r[i].file))
                           continue;
                   fprintf(stderr, "invalid filename %s in %s database\n",
                       r[i].file, req->q.manpath);
                   pg_error_internal();
                   return;
           }
   
         if (1 == sz) {          if (1 == sz) {
                 /*                  /*
                  * If we have just one result, then jump there now                   * If we have just one result, then jump there now
Line 531  pg_searchres(const struct req *req, struct manpage *r,
Line 579  pg_searchres(const struct req *req, struct manpage *r,
                  */                   */
                 printf("Status: 303 See Other\r\n");                  printf("Status: 303 See Other\r\n");
                 printf("Location: http://%s%s/%s/%s?",                  printf("Location: http://%s%s/%s/%s?",
                     httphost, scriptname, req->q.manpath, r[0].file);                      HTTP_HOST, scriptname, req->q.manpath, r[0].file);
                 http_printquery(req);                  http_printquery(req);
                 printf("\r\n"                  printf("\r\n"
                      "Content-Type: text/html; charset=utf-8\r\n"                       "Content-Type: text/html; charset=utf-8\r\n"
Line 751  format(const struct req *req, const char *file)
Line 799  format(const struct req *req, const char *file)
                 return;                  return;
         }          }
   
         snprintf(opts, sizeof(opts),          snprintf(opts, sizeof(opts), "fragment,man=%s?"
             "fragment,man=%s?query=%%N&amp;sec=%%S",              "manpath=%s&amp;query=%%N&amp;sec=%%S&amp;arch=%s",
             scriptname);              scriptname, req->q.manpath,
               req->q.arch ? req->q.arch : "");
   
         mparse_result(mp, &mdoc, &man, NULL);          mparse_result(mp, &mdoc, &man, NULL);
         if (NULL == man && NULL == mdoc) {          if (NULL == man && NULL == mdoc) {
Line 778  format(const struct req *req, const char *file)
Line 827  format(const struct req *req, const char *file)
 static void  static void
 resp_show(const struct req *req, const char *file)  resp_show(const struct req *req, const char *file)
 {  {
         if ('.' == file[0] || '/' == file[1])  
           if ('.' == file[0] && '/' == file[1])
                 file += 2;                  file += 2;
   
         if ('c' == *file)          if ('c' == *file)
Line 788  resp_show(const struct req *req, const char *file)
Line 838  resp_show(const struct req *req, const char *file)
 }  }
   
 static void  static void
 pg_show(const struct req *req, const char *path)  pg_show(struct req *req, const char *path)
 {  {
         char            *sub;          char            *sub;
   
Line 799  pg_show(const struct req *req, const char *path)
Line 849  pg_show(const struct req *req, const char *path)
         }          }
         *sub++ = '\0';          *sub++ = '\0';
   
           if ( ! validate_manpath(req, path)) {
                   pg_error_badrequest(
                       "You specified an invalid manpath.");
                   return;
           }
   
         /*          /*
          * Begin by chdir()ing into the manpath.           * Begin by chdir()ing into the manpath.
          * This way we can pick up the database files, which are           * This way we can pick up the database files, which are
Line 806  pg_show(const struct req *req, const char *path)
Line 862  pg_show(const struct req *req, const char *path)
          */           */
   
         if (-1 == chdir(path)) {          if (-1 == chdir(path)) {
                   fprintf(stderr, "chdir %s: %s\n",
                       path, strerror(errno));
                   pg_error_internal();
                   return;
           }
   
           if ( ! validate_filename(sub)) {
                 pg_error_badrequest(                  pg_error_badrequest(
                     "You specified an invalid manpath.");                      "You specified an invalid manual file.");
                 return;                  return;
         }          }
   
           if (strcmp(path, "mandoc"))
                   req->q.manpath = path;
   
         resp_begin_html(200, NULL);          resp_begin_html(200, NULL);
         resp_searchform(req);          resp_searchform(req);
         resp_show(req, sub);          resp_show(req, sub);
Line 835  pg_search(const struct req *req)
Line 901  pg_search(const struct req *req)
          */           */
   
         if (-1 == (chdir(req->q.manpath))) {          if (-1 == (chdir(req->q.manpath))) {
                 pg_error_badrequest(                  fprintf(stderr, "chdir %s: %s\n",
                     "You specified an invalid manpath.");                      req->q.manpath, strerror(errno));
                   pg_error_internal();
                 return;                  return;
         }          }
   
Line 907  main(void)
Line 974  main(void)
         if (NULL == (scriptname = getenv("SCRIPT_NAME")))          if (NULL == (scriptname = getenv("SCRIPT_NAME")))
                 scriptname = "";                  scriptname = "";
   
         if (NULL == (httphost = getenv("HTTP_HOST")))          if ( ! validate_urifrag(scriptname)) {
                 httphost = "localhost";                  fprintf(stderr, "unsafe SCRIPT_NAME \"%s\"\n",
                       scriptname);
                   pg_error_internal();
                   return(EXIT_FAILURE);
           }
   
         /*          /*
          * First we change directory into the MAN_DIR so that           * First we change directory into the MAN_DIR so that
Line 931  main(void)
Line 1002  main(void)
         if (NULL != (querystring = getenv("QUERY_STRING")))          if (NULL != (querystring = getenv("QUERY_STRING")))
                 http_parse(&req, querystring);                  http_parse(&req, querystring);
   
           if ( ! validate_manpath(&req, req.q.manpath)) {
                   pg_error_badrequest(
                       "You specified an invalid manpath.");
                   return(EXIT_FAILURE);
           }
   
           if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) {
                   pg_error_badrequest(
                       "You specified an invalid architecture.");
                   return(EXIT_FAILURE);
           }
   
         /* Dispatch to the three different pages. */          /* Dispatch to the three different pages. */
   
         path = getenv("PATH_INFO");          path = getenv("PATH_INFO");
Line 982  pathgen(struct req *req)
Line 1065  pathgen(struct req *req)
                         dpsz--;                          dpsz--;
                 req->p = mandoc_realloc(req->p,                  req->p = mandoc_realloc(req->p,
                     (req->psz + 1) * sizeof(char *));                      (req->psz + 1) * sizeof(char *));
                 req->p[req->psz++] = mandoc_strndup(dp, dpsz);                  dp = mandoc_strndup(dp, dpsz);
                   if ( ! validate_urifrag(dp)) {
                           fprintf(stderr, "%s/manpath.conf contains "
                               "unsafe path \"%s\"\n", MAN_DIR, dp);
                           pg_error_internal();
                           exit(EXIT_FAILURE);
                   }
                   if (NULL != strchr(dp, '/')) {
                           fprintf(stderr, "%s/manpath.conf contains "
                               "path with slash \"%s\"\n", MAN_DIR, dp);
                           pg_error_internal();
                           exit(EXIT_FAILURE);
                   }
                   req->p[req->psz++] = dp;
         }          }
   
         if ( req->p == NULL ) {          if ( req->p == NULL ) {

Legend:
Removed from v.1.74  
changed lines
  Added in v.1.80

CVSweb