Fossil

Check-in [1e799919]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:An early attempt at the /setup_smtp page. Partly working.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:1e799919b8a4cdd872cc56f995ba4029a2eb6c1afe608df0b4de4ab7afdb0c10
User & Date: drh 2018-07-13 20:36:16
Context
2018-07-13
21:36
Improved process debugging for "fossil ui" and "fossil server". Sanely close the open database connection upon receiving SIGPIPE. check-in: 83b171bc user: drh tags: trunk
20:36
An early attempt at the /setup_smtp page. Partly working. check-in: 1e799919 user: drh tags: trunk
18:40
Attempts to obtain the IPv6 address of the peer do not seem to work. Fallback to getting the IPv4 address until we figure this out. check-in: cf94d5a0 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/cgi.c.

   885    885     if( z==0 ){
   886    886       if( pLog ) fclose(pLog);
   887    887       pLog = 0;
   888    888       return;
   889    889     }
   890    890     if( pLog==0 ){
   891    891       char zFile[50];
          892  +#if defined(_WIN32)
   892    893       unsigned r;
   893    894       sqlite3_randomness(sizeof(r), &r);
   894    895       sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%08x.txt", r);
          896  +#else
          897  +    sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%05d.txt", getpid());
          898  +#endif
   895    899       pLog = fossil_fopen(zFile, "wb");
   896    900       if( pLog ){
   897    901         fprintf(stderr, "# open log on %s\n", zFile);
   898    902       }else{
   899    903         fprintf(stderr, "# failed to open %s\n", zFile);
   900    904         return;
   901    905       }
................................................................................
  1468   1472       i = strlen(zVal);
  1469   1473       while( i>0 && fossil_isspace(zVal[i-1]) ){ i--; }
  1470   1474       zVal[i] = 0;
  1471   1475       for(i=0; zFieldName[i]; i++){
  1472   1476         zFieldName[i] = fossil_tolower(zFieldName[i]);
  1473   1477       }
  1474   1478       if( fossil_strcmp(zFieldName,"accept-encoding:")==0 ){
  1475         -      cgi_setenv("HTTP_ACCEPT_ENCODING", zVal);
         1479  +      /* Hack:  Ignore the accept-encoding value (thus preventing the
         1480  +      ** output from being compressed) for the "fossil test-http" command.
         1481  +      ** This simplifies debugging. */
         1482  +      if( g.argc<2 || fossil_strncmp(g.argv[1],"test-http",9)!=0 ){
         1483  +        cgi_setenv("HTTP_ACCEPT_ENCODING", zVal);
         1484  +      }
  1476   1485       }else if( fossil_strcmp(zFieldName,"content-length:")==0 ){
  1477   1486         cgi_setenv("CONTENT_LENGTH", zVal);
  1478   1487       }else if( fossil_strcmp(zFieldName,"content-type:")==0 ){
  1479   1488         cgi_setenv("CONTENT_TYPE", zVal);
  1480   1489       }else if( fossil_strcmp(zFieldName,"cookie:")==0 ){
  1481   1490         cgi_setenv("HTTP_COOKIE", zVal);
  1482   1491       }else if( fossil_strcmp(zFieldName,"https:")==0 ){

Changes to src/db.c.

  1320   1320       g.zConfigDbName = 0;
  1321   1321     }else if( g.dbConfig ){
  1322   1322       sqlite3_wal_checkpoint(g.dbConfig, 0);
  1323   1323       sqlite3_close(g.dbConfig);
  1324   1324       g.dbConfig = 0;
  1325   1325       g.zConfigDbName = 0;
  1326   1326     }else if( g.db && 0==iSlot ){
         1327  +    int rc;
  1327   1328       sqlite3_wal_checkpoint(g.db, 0);
  1328         -    sqlite3_close(g.db);
         1329  +    rc = sqlite3_close(g.db);
         1330  +    if( g.fSqlTrace ) fossil_trace("-- db_close_config(%d)\n", rc);
  1329   1331       g.db = 0;
  1330   1332       g.zConfigDbName = 0;
  1331   1333     }
  1332   1334   }
  1333   1335   
  1334   1336   /*
  1335   1337   ** Open the user database in "~/.fossil".  Create the database anew if
................................................................................
  1826   1828       }
  1827   1829     }
  1828   1830   
  1829   1831     if( g.db ){
  1830   1832       int rc;
  1831   1833       sqlite3_wal_checkpoint(g.db, 0);
  1832   1834       rc = sqlite3_close(g.db);
         1835  +    if( g.fSqlTrace ) fossil_trace("-- sqlite3_close(%d)\n", rc);
  1833   1836       if( rc==SQLITE_BUSY && reportErrors ){
  1834   1837         while( (pStmt = sqlite3_next_stmt(g.db, pStmt))!=0 ){
  1835   1838           fossil_warning("unfinalized SQL statement: [%s]", sqlite3_sql(pStmt));
  1836   1839         }
  1837   1840       }
  1838   1841       g.db = 0;
  1839   1842     }

Changes to src/smtp.c.

   708    708     if( eForce==2 ){
   709    709       db_multi_exec(zEmailDrop/*works-like:""*/);
   710    710     }
   711    711     if( eForce==1 || !db_table_exists("repository","emailblob") ){
   712    712       db_multi_exec(zEmailSchema/*works-like:""*/);
   713    713     }
   714    714   }
          715  +
          716  +/*
          717  +** Process POST change requests from the setup_smtp page
          718  +*/
          719  +static void handle_smtp_setup_edit(char **pzErr, int *piErr){
          720  +  *piErr = 0;
          721  +  *pzErr = 0;
          722  +  if( !cgi_csrf_safe(1) ) return;
          723  +  if( P("new")!=0 ){
          724  +    const char *zEAddr = PDT("eaddr","");
          725  +    const char *zEPolicy = PDT("epolicy","");
          726  +    smtp_server_schema(0);
          727  +    if( db_exists("SELECT 1 FROM emailroute WHERE eaddr=%Q", zEAddr) ){
          728  +      *pzErr = mprintf("\"%s\" already exists", zEAddr);
          729  +      *piErr = 100;
          730  +      return;
          731  +    }
          732  +    db_multi_exec("INSERT INTO emailroute(eaddr,epolicy)"
          733  +                  "VALUES(%Q,%Q)", zEAddr, zEPolicy);
          734  +    return;
          735  +  }
          736  +  if( P("delete")!=0 ){
          737  +    const char *zEAddr = PDT("eaddr","");
          738  +    const char *zEPolicy = PDT("epolicy","");
          739  +    smtp_server_schema(0);
          740  +    if( zEPolicy[0] ){
          741  +      *pzErr = mprintf("Erase routing information to delete");
          742  +      *piErr = 200;
          743  +      return;
          744  +    }
          745  +    db_multi_exec("DELETE FROM emailroute WHERE eaddr=%Q", zEAddr);
          746  +    return;
          747  +  }
          748  +  if( P("change")!=0 ){
          749  +    const char *zEAddr = PDT("eaddr","");
          750  +    const char *zEPolicy = PDT("epolicy","");
          751  +    smtp_server_schema(0);
          752  +    db_multi_exec("UPDATE emailroute SET epolicy=%Q WHERE eaddr=%Q",
          753  +                   zEPolicy, zEAddr);
          754  +    return;
          755  +  }
          756  +}
          757  +
   715    758   
   716    759   /*
   717    760   ** WEBPAGE: setup_smtp
   718    761   **
   719    762   ** Administrative page for configuring and controlling inbound email and
   720    763   ** output email queuing.  This page is available to administrators
   721    764   ** only via the /Admin/EmailServer menu.
   722    765   */
   723    766   void setup_smtp(void){
          767  +  Stmt q;
          768  +  char *zErr = 0;
          769  +  int iErr = 0;
   724    770     login_check_credentials();
   725    771     if( !g.perm.Setup ){
   726    772       login_needed(0);
   727    773       return;
   728    774     }
          775  +  db_begin_transaction();
   729    776     style_header("Email Server Setup");
   730         -  @ <i>Pending...</i>
          777  +  handle_smtp_setup_edit(&zErr, &iErr);
          778  +  if( db_table_exists("repository","emailroute") ){
          779  +    db_prepare(&q, "SELECT eaddr, epolicy FROM emailroute ORDER BY 1");
          780  +  }else{
          781  +    db_prepare(&q, "SELECT null, null WHERE false");
          782  +  }
          783  +  @ <table class="emailroutetab">
          784  +  @ <thead>
          785  +  @ <tr><th>Email Address
          786  +  @ <th>Routing
          787  +  @ <th></th>
          788  +  @ </thead><tbody>
          789  +  while( db_step(&q)==SQLITE_ROW ){
          790  +    const char *zEAddr = db_column_text(&q, 0);
          791  +    const char *zEPolicy = db_column_text(&q, 1);
          792  +    @ <tr><form action="%R/setup_smtp" method="POST">
          793  +    @ <td valign="top">%h(zEAddr)\
          794  +    @ <input type="hidden" name="eaddr" value="%h(zEAddr)"></td>
          795  +    @ <td valign="top">\
          796  +    @ <textarea name="epolicy" rows="3" cols="40">%h(zEPolicy)</textarea>\
          797  +    @ </td>
          798  +    @ <td valign="top">\
          799  +    @ <input type="submit" name="change" value="Apply Changes"><br>\
          800  +    @ <input type="submit" name="delete" value="Delete"></td>
          801  +    @ </form></tr>
          802  +  }
          803  +  db_finalize(&q);
          804  +  @ <tr><form action="%R/setup_smtp" method="POST">
          805  +  @ <td valign="top">
          806  +  @ <input type="text" name="eaddr" width="30"></td>
          807  +  @ <td valign="top">\
          808  +  @ <textarea name="epolicy" rows="3" cols="40"></textarea>\
          809  +  @ </td>
          810  +  @ <td valign="top"><input type="submit" name="new" value="New"></td>
          811  +  @ </form></tr>
   731    812     style_footer();
          813  +  fossil_free(zErr);
          814  +  db_end_transaction(0);
   732    815   }
   733         -
   734    816   
   735    817   #if LOCAL_INTERFACE
   736    818   /*
   737    819   ** State information for the server
   738    820   */
   739    821   struct SmtpServer {
   740    822     sqlite3_int64 idTranscript; /* Transcript ID number */