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
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1e799919b8a4cdd872cc56f995ba4029a2eb6c1afe608df0b4de4ab7afdb0c10
User & Date: drh 2018-07-13 20:36:16.220
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
Unified Diff Ignore Whitespace Patch
Changes to src/cgi.c.
885
886
887
888
889
890
891

892
893
894



895
896
897
898
899
900
901
  if( z==0 ){
    if( pLog ) fclose(pLog);
    pLog = 0;
    return;
  }
  if( pLog==0 ){
    char zFile[50];

    unsigned r;
    sqlite3_randomness(sizeof(r), &r);
    sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%08x.txt", r);



    pLog = fossil_fopen(zFile, "wb");
    if( pLog ){
      fprintf(stderr, "# open log on %s\n", zFile);
    }else{
      fprintf(stderr, "# failed to open %s\n", zFile);
      return;
    }







>



>
>
>







885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
  if( z==0 ){
    if( pLog ) fclose(pLog);
    pLog = 0;
    return;
  }
  if( pLog==0 ){
    char zFile[50];
#if defined(_WIN32)
    unsigned r;
    sqlite3_randomness(sizeof(r), &r);
    sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%08x.txt", r);
#else
    sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%05d.txt", getpid());
#endif
    pLog = fossil_fopen(zFile, "wb");
    if( pLog ){
      fprintf(stderr, "# open log on %s\n", zFile);
    }else{
      fprintf(stderr, "# failed to open %s\n", zFile);
      return;
    }
1468
1469
1470
1471
1472
1473
1474




1475

1476
1477
1478
1479
1480
1481
1482
    i = strlen(zVal);
    while( i>0 && fossil_isspace(zVal[i-1]) ){ i--; }
    zVal[i] = 0;
    for(i=0; zFieldName[i]; i++){
      zFieldName[i] = fossil_tolower(zFieldName[i]);
    }
    if( fossil_strcmp(zFieldName,"accept-encoding:")==0 ){




      cgi_setenv("HTTP_ACCEPT_ENCODING", zVal);

    }else if( fossil_strcmp(zFieldName,"content-length:")==0 ){
      cgi_setenv("CONTENT_LENGTH", zVal);
    }else if( fossil_strcmp(zFieldName,"content-type:")==0 ){
      cgi_setenv("CONTENT_TYPE", zVal);
    }else if( fossil_strcmp(zFieldName,"cookie:")==0 ){
      cgi_setenv("HTTP_COOKIE", zVal);
    }else if( fossil_strcmp(zFieldName,"https:")==0 ){







>
>
>
>
|
>







1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
    i = strlen(zVal);
    while( i>0 && fossil_isspace(zVal[i-1]) ){ i--; }
    zVal[i] = 0;
    for(i=0; zFieldName[i]; i++){
      zFieldName[i] = fossil_tolower(zFieldName[i]);
    }
    if( fossil_strcmp(zFieldName,"accept-encoding:")==0 ){
      /* Hack:  Ignore the accept-encoding value (thus preventing the
      ** output from being compressed) for the "fossil test-http" command.
      ** This simplifies debugging. */
      if( g.argc<2 || fossil_strncmp(g.argv[1],"test-http",9)!=0 ){
        cgi_setenv("HTTP_ACCEPT_ENCODING", zVal);
      }
    }else if( fossil_strcmp(zFieldName,"content-length:")==0 ){
      cgi_setenv("CONTENT_LENGTH", zVal);
    }else if( fossil_strcmp(zFieldName,"content-type:")==0 ){
      cgi_setenv("CONTENT_TYPE", zVal);
    }else if( fossil_strcmp(zFieldName,"cookie:")==0 ){
      cgi_setenv("HTTP_COOKIE", zVal);
    }else if( fossil_strcmp(zFieldName,"https:")==0 ){
Changes to src/db.c.
1320
1321
1322
1323
1324
1325
1326

1327
1328

1329
1330
1331
1332
1333
1334
1335
    g.zConfigDbName = 0;
  }else if( g.dbConfig ){
    sqlite3_wal_checkpoint(g.dbConfig, 0);
    sqlite3_close(g.dbConfig);
    g.dbConfig = 0;
    g.zConfigDbName = 0;
  }else if( g.db && 0==iSlot ){

    sqlite3_wal_checkpoint(g.db, 0);
    sqlite3_close(g.db);

    g.db = 0;
    g.zConfigDbName = 0;
  }
}

/*
** Open the user database in "~/.fossil".  Create the database anew if







>

|
>







1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
    g.zConfigDbName = 0;
  }else if( g.dbConfig ){
    sqlite3_wal_checkpoint(g.dbConfig, 0);
    sqlite3_close(g.dbConfig);
    g.dbConfig = 0;
    g.zConfigDbName = 0;
  }else if( g.db && 0==iSlot ){
    int rc;
    sqlite3_wal_checkpoint(g.db, 0);
    rc = sqlite3_close(g.db);
    if( g.fSqlTrace ) fossil_trace("-- db_close_config(%d)\n", rc);
    g.db = 0;
    g.zConfigDbName = 0;
  }
}

/*
** Open the user database in "~/.fossil".  Create the database anew if
1826
1827
1828
1829
1830
1831
1832

1833
1834
1835
1836
1837
1838
1839
    }
  }

  if( g.db ){
    int rc;
    sqlite3_wal_checkpoint(g.db, 0);
    rc = sqlite3_close(g.db);

    if( rc==SQLITE_BUSY && reportErrors ){
      while( (pStmt = sqlite3_next_stmt(g.db, pStmt))!=0 ){
        fossil_warning("unfinalized SQL statement: [%s]", sqlite3_sql(pStmt));
      }
    }
    g.db = 0;
  }







>







1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
    }
  }

  if( g.db ){
    int rc;
    sqlite3_wal_checkpoint(g.db, 0);
    rc = sqlite3_close(g.db);
    if( g.fSqlTrace ) fossil_trace("-- sqlite3_close(%d)\n", rc);
    if( rc==SQLITE_BUSY && reportErrors ){
      while( (pStmt = sqlite3_next_stmt(g.db, pStmt))!=0 ){
        fossil_warning("unfinalized SQL statement: [%s]", sqlite3_sql(pStmt));
      }
    }
    g.db = 0;
  }
Changes to src/smtp.c.
708
709
710
711
712
713
714











































715
716
717
718
719
720
721
722
723



724
725
726
727
728

729




















730



731
732












733
734
735
736
737
738
739
740
  if( eForce==2 ){
    db_multi_exec(zEmailDrop/*works-like:""*/);
  }
  if( eForce==1 || !db_table_exists("repository","emailblob") ){
    db_multi_exec(zEmailSchema/*works-like:""*/);
  }
}












































/*
** WEBPAGE: setup_smtp
**
** Administrative page for configuring and controlling inbound email and
** output email queuing.  This page is available to administrators
** only via the /Admin/EmailServer menu.
*/
void setup_smtp(void){



  login_check_credentials();
  if( !g.perm.Setup ){
    login_needed(0);
    return;
  }

  style_header("Email Server Setup");




















  @ <i>Pending...</i>



  style_footer();
}














#if LOCAL_INTERFACE
/*
** State information for the server
*/
struct SmtpServer {
  sqlite3_int64 idTranscript; /* Transcript ID number */







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









>
>
>





>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
|







708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
  if( eForce==2 ){
    db_multi_exec(zEmailDrop/*works-like:""*/);
  }
  if( eForce==1 || !db_table_exists("repository","emailblob") ){
    db_multi_exec(zEmailSchema/*works-like:""*/);
  }
}

/*
** Process POST change requests from the setup_smtp page
*/
static void handle_smtp_setup_edit(char **pzErr, int *piErr){
  *piErr = 0;
  *pzErr = 0;
  if( !cgi_csrf_safe(1) ) return;
  if( P("new")!=0 ){
    const char *zEAddr = PDT("eaddr","");
    const char *zEPolicy = PDT("epolicy","");
    smtp_server_schema(0);
    if( db_exists("SELECT 1 FROM emailroute WHERE eaddr=%Q", zEAddr) ){
      *pzErr = mprintf("\"%s\" already exists", zEAddr);
      *piErr = 100;
      return;
    }
    db_multi_exec("INSERT INTO emailroute(eaddr,epolicy)"
                  "VALUES(%Q,%Q)", zEAddr, zEPolicy);
    return;
  }
  if( P("delete")!=0 ){
    const char *zEAddr = PDT("eaddr","");
    const char *zEPolicy = PDT("epolicy","");
    smtp_server_schema(0);
    if( zEPolicy[0] ){
      *pzErr = mprintf("Erase routing information to delete");
      *piErr = 200;
      return;
    }
    db_multi_exec("DELETE FROM emailroute WHERE eaddr=%Q", zEAddr);
    return;
  }
  if( P("change")!=0 ){
    const char *zEAddr = PDT("eaddr","");
    const char *zEPolicy = PDT("epolicy","");
    smtp_server_schema(0);
    db_multi_exec("UPDATE emailroute SET epolicy=%Q WHERE eaddr=%Q",
                   zEPolicy, zEAddr);
    return;
  }
}


/*
** WEBPAGE: setup_smtp
**
** Administrative page for configuring and controlling inbound email and
** output email queuing.  This page is available to administrators
** only via the /Admin/EmailServer menu.
*/
void setup_smtp(void){
  Stmt q;
  char *zErr = 0;
  int iErr = 0;
  login_check_credentials();
  if( !g.perm.Setup ){
    login_needed(0);
    return;
  }
  db_begin_transaction();
  style_header("Email Server Setup");
  handle_smtp_setup_edit(&zErr, &iErr);
  if( db_table_exists("repository","emailroute") ){
    db_prepare(&q, "SELECT eaddr, epolicy FROM emailroute ORDER BY 1");
  }else{
    db_prepare(&q, "SELECT null, null WHERE false");
  }
  @ <table class="emailroutetab">
  @ <thead>
  @ <tr><th>Email Address
  @ <th>Routing
  @ <th></th>
  @ </thead><tbody>
  while( db_step(&q)==SQLITE_ROW ){
    const char *zEAddr = db_column_text(&q, 0);
    const char *zEPolicy = db_column_text(&q, 1);
    @ <tr><form action="%R/setup_smtp" method="POST">
    @ <td valign="top">%h(zEAddr)\
    @ <input type="hidden" name="eaddr" value="%h(zEAddr)"></td>
    @ <td valign="top">\
    @ <textarea name="epolicy" rows="3" cols="40">%h(zEPolicy)</textarea>\
    @ </td>
    @ <td valign="top">\
    @ <input type="submit" name="change" value="Apply Changes"><br>\
    @ <input type="submit" name="delete" value="Delete"></td>
    @ </form></tr>
  }
  db_finalize(&q);
  @ <tr><form action="%R/setup_smtp" method="POST">
  @ <td valign="top">
  @ <input type="text" name="eaddr" width="30"></td>
  @ <td valign="top">\
  @ <textarea name="epolicy" rows="3" cols="40"></textarea>\
  @ </td>
  @ <td valign="top"><input type="submit" name="new" value="New"></td>
  @ </form></tr>
  style_footer();
  fossil_free(zErr);
  db_end_transaction(0);
}

#if LOCAL_INTERFACE
/*
** State information for the server
*/
struct SmtpServer {
  sqlite3_int64 idTranscript; /* Transcript ID number */