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

Diff for /mandoc/cgi.c between version 1.7 and 1.9

version 1.7, 2011/11/24 12:27:18 version 1.9, 2011/12/04 22:52:50
Line 36 
Line 36 
   
 #include "apropos_db.h"  #include "apropos_db.h"
 #include "mandoc.h"  #include "mandoc.h"
   #include "mdoc.h"
   #include "man.h"
   #include "main.h"
 #include "manpath.h"  #include "manpath.h"
   
 #ifdef __linux__  #ifdef __linux__
Line 63  struct req {
Line 66  struct req {
 };  };
   
 static  int              atou(const char *, unsigned *);  static  int              atou(const char *, unsigned *);
 static  void             format_insecure(const char *);  static  void             catman(const char *);
 static  void             format_secure(const char *);  static  void             format(const char *);
 static  void             html_print(const char *);  static  void             html_print(const char *);
 static  int              kval_decode(char *);  static  int              kval_decode(char *);
 static  void             kval_parse(struct kval **, size_t *, char *);  static  void             kval_parse(struct kval **, size_t *, char *);
Line 79  static void   resp_bad(void);
Line 82  static void   resp_bad(void);
 static  void             resp_baddb(void);  static  void             resp_baddb(void);
 static  void             resp_badexpr(const struct req *);  static  void             resp_badexpr(const struct req *);
 static  void             resp_badmanual(void);  static  void             resp_badmanual(void);
   static  void             resp_badpage(void);
 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 86  static void   resp_index(const struct req *);
Line 90  static void   resp_index(const struct req *);
 static  void             resp_search(struct res *, size_t, void *);  static  void             resp_search(struct res *, size_t, void *);
 static  void             resp_searchform(const struct req *);  static  void             resp_searchform(const struct req *);
   
 static  int               insecure = 1;  
 static  const char       *progname;  static  const char       *progname;
 static  const char       *cache;  static  const char       *cache;
 static  const char       *host;  static  const char       *host;
Line 319  resp_searchform(const struct req *req)
Line 322  resp_searchform(const struct req *req)
         puts("<!-- Begin search form. //-->");          puts("<!-- Begin search form. //-->");
         printf("<FORM ACTION=\"");          printf("<FORM ACTION=\"");
         html_print(progname);          html_print(progname);
         printf("/search\" METHOD=\"get\">\n");          printf("/search.html\" METHOD=\"get\">\n");
         puts(" <FIELDSET>"                                      "\n"          puts(" <FIELDSET>"                                      "\n"
              "  <INPUT TYPE=\"submit\" VALUE=\"Search:\">");               "  <INPUT TYPE=\"submit\" VALUE=\"Search:\">");
         printf("  Terms: <INPUT TYPE=\"text\" "          printf("  Terms: <INPUT TYPE=\"text\" "
Line 347  resp_index(const struct req *req)
Line 350  resp_index(const struct req *req)
 }  }
   
 static void  static void
   resp_badpage(void)
   {
   
           resp_begin_html(404, "Not Found");
           puts("<P>Page not found.</P>");
           resp_end_html();
   }
   
   static void
 resp_badmanual(void)  resp_badmanual(void)
 {  {
   
Line 434  pg_index(const struct manpaths *ps, const struct req *
Line 446  pg_index(const struct manpaths *ps, const struct req *
 }  }
   
 static void  static void
 format_insecure(const char *file)  catman(const char *file)
 {  {
         pid_t            pid;          int              fd;
         char             cmd[MAXPATHLEN];          char             buf[BUFSIZ];
           ssize_t          ssz;
   
         strlcpy(cmd, "man=", MAXPATHLEN);          if (-1 == (fd = open(file, O_RDONLY, 0))) {
         strlcat(cmd, progname, MAXPATHLEN);                  resp_baddb();
         strlcat(cmd, "/search?expr=%N&sec=%S", MAXPATHLEN);  
   
         /* Get ready to call the child mandoc(1) process. */  
   
         if (-1 == (pid = fork()))  
                 exit(EXIT_FAILURE);  
   
         if (pid > 0) {  
                 waitpid(pid, NULL, 0);  
                 return;                  return;
         }          }
   
         dup2(STDOUT_FILENO, STDERR_FILENO);          resp_begin_http(200, NULL);
   
         puts("Content-Type: text/html; charset=utf-8\n");          while ((ssz = read(fd, buf, BUFSIZ)) > 0)
                   write(STDOUT_FILENO, buf, (size_t)ssz);
   
         fflush(stdout);          if (ssz < 0)
                   perror(file);
   
         execlp("mandoc", "mandoc", "-T",          close(fd);
                 "html", "-O", cmd, file, (char *)NULL);  
 }  }
   
 static void  static void
 format_secure(const char *file)  format(const char *file)
 {  {
         char             buf[BUFSIZ];          struct mparse   *mp;
         int              fd;          int              fd;
         ssize_t          ssz;          struct mdoc     *mdoc;
           struct man      *man;
           void            *vp;
           enum mandoclevel rc;
   
         if (-1 == (fd = open(file, O_RDONLY, 0))) {          if (-1 == (fd = open(file, O_RDONLY, 0))) {
                 resp_baddb();                  resp_baddb();
                 return;                  return;
         }          }
   
         resp_begin_http(200, NULL);          mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
           rc = mparse_readfd(mp, fd, file);
           close(fd);
   
         do {          if (rc >= MANDOCLEVEL_FATAL) {
                 ssz = read(fd, buf, BUFSIZ);                  resp_baddb();
                 if (ssz > 0)                  return;
                         write(STDOUT_FILENO, buf, ssz);          }
         } while (ssz > 0);  
   
         close(fd);          mparse_result(mp, &mdoc, &man);
           vp = html_alloc(NULL);
   
           if (NULL != mdoc) {
                   resp_begin_http(200, NULL);
                   html_mdoc(vp, mdoc);
           } else if (NULL != man) {
                   resp_begin_http(200, NULL);
                   html_man(vp, man);
           } else
                   resp_baddb();
   
           html_free(vp);
           mparse_free(mp);
 }  }
   
 static void  static void
Line 491  pg_show(const struct manpaths *ps, const struct req *r
Line 513  pg_show(const struct manpaths *ps, const struct req *r
 {  {
         char            *sub;          char            *sub;
         char             file[MAXPATHLEN];          char             file[MAXPATHLEN];
           const char      *fn, *cp;
         int              rc;          int              rc;
         unsigned int     vol, rec;          unsigned int     vol, rec;
         DB              *db;          DB              *idx;
         DBT              key, val;          DBT              key, val;
   
         if (NULL == path) {          if (NULL == path) {
Line 518  pg_show(const struct manpaths *ps, const struct req *r
Line 541  pg_show(const struct manpaths *ps, const struct req *r
   
         /* Open the index recno(3) database. */          /* Open the index recno(3) database. */
   
         db = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL);          idx = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL);
         if (NULL == db) {          if (NULL == idx) {
                 resp_baddb();                  resp_baddb();
                 return;                  return;
         }          }
Line 527  pg_show(const struct manpaths *ps, const struct req *r
Line 550  pg_show(const struct manpaths *ps, const struct req *r
         key.data = &rec;          key.data = &rec;
         key.size = 4;          key.size = 4;
   
         if (0 != (rc = (*db->get)(db, &key, &val, 0))) {          if (0 != (rc = (*idx->get)(idx, &key, &val, 0))) {
                 rc < 0 ? resp_baddb() : resp_badmanual();                  rc < 0 ? resp_baddb() : resp_badmanual();
                 (*db->close)(db);                  goto out;
                 return;  
         }          }
   
         /* Extra filename: the first nil-terminated entry. */          cp = (char *)val.data;
   
         (*db->close)(db);          if (NULL == (fn = memchr(cp, '\0', val.size)))
                   resp_baddb();
         strlcpy(file, ps->paths[vol], MAXPATHLEN);          else if (++fn - cp >= (int)val.size)
         strlcat(file, "/", MAXPATHLEN);                  resp_baddb();
         strlcat(file, (char *)val.data, MAXPATHLEN);          else if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
                   resp_baddb();
         if ( ! insecure) {          else {
                 strlcat(file, ".html", MAXPATHLEN);                  strlcpy(file, ps->paths[vol], MAXPATHLEN);
                 format_secure(file);                  strlcat(file, "/", MAXPATHLEN);
         } else                  strlcat(file, fn, MAXPATHLEN);
                 format_insecure(file);                  if (0 == strcmp(cp, "cat"))
                           catman(file);
                   else
                           format(file);
           }
   out:
           (*idx->close)(idx);
 }  }
   
 static void  static void
Line 638  main(void)
Line 666  main(void)
         if (NULL == cache)          if (NULL == cache)
                 cache = "/cache/man.cgi";                  cache = "/cache/man.cgi";
   
         if (NULL == getenv("INSECURE")) {          if (-1 == chdir(cache)) {
                 insecure = 0;                  resp_bad();
                 if (-1 == chdir(cache)) {                  return(EXIT_FAILURE);
                         resp_bad();  
                         return(EXIT_FAILURE);  
                 }  
         }          }
   
         host = getenv("HTTP_HOST");          host = getenv("HTTP_HOST");
Line 689  main(void)
Line 714  main(void)
         /* Initialise MANPATH. */          /* Initialise MANPATH. */
   
         memset(&paths, 0, sizeof(struct manpaths));          memset(&paths, 0, sizeof(struct manpaths));
         if ( ! insecure)          manpath_manconf("etc/catman.conf", &paths);
                 manpath_manconf("etc/man.conf", &paths);  
         else  
                 manpath_parse(&paths, NULL, NULL);  
   
         /* Route pages. */          /* Route pages. */
   
Line 707  main(void)
Line 729  main(void)
                 pg_show(&paths, &req, subpath);                  pg_show(&paths, &req, subpath);
                 break;                  break;
         default:          default:
                   resp_badpage();
                 break;                  break;
         }          }
   

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.9

CVSweb