Fossil SCM

An early attempt at the /setup_smtp page. Partly working.

drh 2018-07-13 20:36 trunk
Commit 1e799919b8a4cdd872cc56f995ba4029a2eb6c1afe608df0b4de4ab7afdb0c10
3 files changed +10 -1 +4 -1 +84 -2
+10 -1
--- src/cgi.c
+++ src/cgi.c
@@ -887,13 +887,17 @@
887887
pLog = 0;
888888
return;
889889
}
890890
if( pLog==0 ){
891891
char zFile[50];
892
+#if defined(_WIN32)
892893
unsigned r;
893894
sqlite3_randomness(sizeof(r), &r);
894895
sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%08x.txt", r);
896
+#else
897
+ sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%05d.txt", getpid());
898
+#endif
895899
pLog = fossil_fopen(zFile, "wb");
896900
if( pLog ){
897901
fprintf(stderr, "# open log on %s\n", zFile);
898902
}else{
899903
fprintf(stderr, "# failed to open %s\n", zFile);
@@ -1470,11 +1474,16 @@
14701474
zVal[i] = 0;
14711475
for(i=0; zFieldName[i]; i++){
14721476
zFieldName[i] = fossil_tolower(zFieldName[i]);
14731477
}
14741478
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
+ }
14761485
}else if( fossil_strcmp(zFieldName,"content-length:")==0 ){
14771486
cgi_setenv("CONTENT_LENGTH", zVal);
14781487
}else if( fossil_strcmp(zFieldName,"content-type:")==0 ){
14791488
cgi_setenv("CONTENT_TYPE", zVal);
14801489
}else if( fossil_strcmp(zFieldName,"cookie:")==0 ){
14811490
--- src/cgi.c
+++ src/cgi.c
@@ -887,13 +887,17 @@
887 pLog = 0;
888 return;
889 }
890 if( pLog==0 ){
891 char zFile[50];
 
892 unsigned r;
893 sqlite3_randomness(sizeof(r), &r);
894 sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%08x.txt", r);
 
 
 
895 pLog = fossil_fopen(zFile, "wb");
896 if( pLog ){
897 fprintf(stderr, "# open log on %s\n", zFile);
898 }else{
899 fprintf(stderr, "# failed to open %s\n", zFile);
@@ -1470,11 +1474,16 @@
1470 zVal[i] = 0;
1471 for(i=0; zFieldName[i]; i++){
1472 zFieldName[i] = fossil_tolower(zFieldName[i]);
1473 }
1474 if( fossil_strcmp(zFieldName,"accept-encoding:")==0 ){
1475 cgi_setenv("HTTP_ACCEPT_ENCODING", zVal);
 
 
 
 
 
1476 }else if( fossil_strcmp(zFieldName,"content-length:")==0 ){
1477 cgi_setenv("CONTENT_LENGTH", zVal);
1478 }else if( fossil_strcmp(zFieldName,"content-type:")==0 ){
1479 cgi_setenv("CONTENT_TYPE", zVal);
1480 }else if( fossil_strcmp(zFieldName,"cookie:")==0 ){
1481
--- src/cgi.c
+++ src/cgi.c
@@ -887,13 +887,17 @@
887 pLog = 0;
888 return;
889 }
890 if( pLog==0 ){
891 char zFile[50];
892 #if defined(_WIN32)
893 unsigned r;
894 sqlite3_randomness(sizeof(r), &r);
895 sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%08x.txt", r);
896 #else
897 sqlite3_snprintf(sizeof(zFile), zFile, "httplog-%05d.txt", getpid());
898 #endif
899 pLog = fossil_fopen(zFile, "wb");
900 if( pLog ){
901 fprintf(stderr, "# open log on %s\n", zFile);
902 }else{
903 fprintf(stderr, "# failed to open %s\n", zFile);
@@ -1470,11 +1474,16 @@
1474 zVal[i] = 0;
1475 for(i=0; zFieldName[i]; i++){
1476 zFieldName[i] = fossil_tolower(zFieldName[i]);
1477 }
1478 if( fossil_strcmp(zFieldName,"accept-encoding:")==0 ){
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 }
1485 }else if( fossil_strcmp(zFieldName,"content-length:")==0 ){
1486 cgi_setenv("CONTENT_LENGTH", zVal);
1487 }else if( fossil_strcmp(zFieldName,"content-type:")==0 ){
1488 cgi_setenv("CONTENT_TYPE", zVal);
1489 }else if( fossil_strcmp(zFieldName,"cookie:")==0 ){
1490
+4 -1
--- src/db.c
+++ src/db.c
@@ -1322,12 +1322,14 @@
13221322
sqlite3_wal_checkpoint(g.dbConfig, 0);
13231323
sqlite3_close(g.dbConfig);
13241324
g.dbConfig = 0;
13251325
g.zConfigDbName = 0;
13261326
}else if( g.db && 0==iSlot ){
1327
+ int rc;
13271328
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);
13291331
g.db = 0;
13301332
g.zConfigDbName = 0;
13311333
}
13321334
}
13331335
@@ -1828,10 +1830,11 @@
18281830
18291831
if( g.db ){
18301832
int rc;
18311833
sqlite3_wal_checkpoint(g.db, 0);
18321834
rc = sqlite3_close(g.db);
1835
+ if( g.fSqlTrace ) fossil_trace("-- sqlite3_close(%d)\n", rc);
18331836
if( rc==SQLITE_BUSY && reportErrors ){
18341837
while( (pStmt = sqlite3_next_stmt(g.db, pStmt))!=0 ){
18351838
fossil_warning("unfinalized SQL statement: [%s]", sqlite3_sql(pStmt));
18361839
}
18371840
}
18381841
--- src/db.c
+++ src/db.c
@@ -1322,12 +1322,14 @@
1322 sqlite3_wal_checkpoint(g.dbConfig, 0);
1323 sqlite3_close(g.dbConfig);
1324 g.dbConfig = 0;
1325 g.zConfigDbName = 0;
1326 }else if( g.db && 0==iSlot ){
 
1327 sqlite3_wal_checkpoint(g.db, 0);
1328 sqlite3_close(g.db);
 
1329 g.db = 0;
1330 g.zConfigDbName = 0;
1331 }
1332 }
1333
@@ -1828,10 +1830,11 @@
1828
1829 if( g.db ){
1830 int rc;
1831 sqlite3_wal_checkpoint(g.db, 0);
1832 rc = sqlite3_close(g.db);
 
1833 if( rc==SQLITE_BUSY && reportErrors ){
1834 while( (pStmt = sqlite3_next_stmt(g.db, pStmt))!=0 ){
1835 fossil_warning("unfinalized SQL statement: [%s]", sqlite3_sql(pStmt));
1836 }
1837 }
1838
--- src/db.c
+++ src/db.c
@@ -1322,12 +1322,14 @@
1322 sqlite3_wal_checkpoint(g.dbConfig, 0);
1323 sqlite3_close(g.dbConfig);
1324 g.dbConfig = 0;
1325 g.zConfigDbName = 0;
1326 }else if( g.db && 0==iSlot ){
1327 int rc;
1328 sqlite3_wal_checkpoint(g.db, 0);
1329 rc = sqlite3_close(g.db);
1330 if( g.fSqlTrace ) fossil_trace("-- db_close_config(%d)\n", rc);
1331 g.db = 0;
1332 g.zConfigDbName = 0;
1333 }
1334 }
1335
@@ -1828,10 +1830,11 @@
1830
1831 if( g.db ){
1832 int rc;
1833 sqlite3_wal_checkpoint(g.db, 0);
1834 rc = sqlite3_close(g.db);
1835 if( g.fSqlTrace ) fossil_trace("-- sqlite3_close(%d)\n", rc);
1836 if( rc==SQLITE_BUSY && reportErrors ){
1837 while( (pStmt = sqlite3_next_stmt(g.db, pStmt))!=0 ){
1838 fossil_warning("unfinalized SQL statement: [%s]", sqlite3_sql(pStmt));
1839 }
1840 }
1841
+84 -2
--- src/smtp.c
+++ src/smtp.c
@@ -710,29 +710,111 @@
710710
}
711711
if( eForce==1 || !db_table_exists("repository","emailblob") ){
712712
db_multi_exec(zEmailSchema/*works-like:""*/);
713713
}
714714
}
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
+
715758
716759
/*
717760
** WEBPAGE: setup_smtp
718761
**
719762
** Administrative page for configuring and controlling inbound email and
720763
** output email queuing. This page is available to administrators
721764
** only via the /Admin/EmailServer menu.
722765
*/
723766
void setup_smtp(void){
767
+ Stmt q;
768
+ char *zErr = 0;
769
+ int iErr = 0;
724770
login_check_credentials();
725771
if( !g.perm.Setup ){
726772
login_needed(0);
727773
return;
728774
}
775
+ db_begin_transaction();
729776
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>
731812
style_footer();
813
+ fossil_free(zErr);
814
+ db_end_transaction(0);
732815
}
733
-
734816
735817
#if LOCAL_INTERFACE
736818
/*
737819
** State information for the server
738820
*/
739821
--- src/smtp.c
+++ src/smtp.c
@@ -710,29 +710,111 @@
710 }
711 if( eForce==1 || !db_table_exists("repository","emailblob") ){
712 db_multi_exec(zEmailSchema/*works-like:""*/);
713 }
714 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
715
716 /*
717 ** WEBPAGE: setup_smtp
718 **
719 ** Administrative page for configuring and controlling inbound email and
720 ** output email queuing. This page is available to administrators
721 ** only via the /Admin/EmailServer menu.
722 */
723 void setup_smtp(void){
 
 
 
724 login_check_credentials();
725 if( !g.perm.Setup ){
726 login_needed(0);
727 return;
728 }
 
729 style_header("Email Server Setup");
730 @ <i>Pending...</i>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
731 style_footer();
 
 
732 }
733
734
735 #if LOCAL_INTERFACE
736 /*
737 ** State information for the server
738 */
739
--- src/smtp.c
+++ src/smtp.c
@@ -710,29 +710,111 @@
710 }
711 if( eForce==1 || !db_table_exists("repository","emailblob") ){
712 db_multi_exec(zEmailSchema/*works-like:""*/);
713 }
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
758
759 /*
760 ** WEBPAGE: setup_smtp
761 **
762 ** Administrative page for configuring and controlling inbound email and
763 ** output email queuing. This page is available to administrators
764 ** only via the /Admin/EmailServer menu.
765 */
766 void setup_smtp(void){
767 Stmt q;
768 char *zErr = 0;
769 int iErr = 0;
770 login_check_credentials();
771 if( !g.perm.Setup ){
772 login_needed(0);
773 return;
774 }
775 db_begin_transaction();
776 style_header("Email Server Setup");
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>
812 style_footer();
813 fossil_free(zErr);
814 db_end_transaction(0);
815 }
 
816
817 #if LOCAL_INTERFACE
818 /*
819 ** State information for the server
820 */
821

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button