Fossil SCM

Fix the /setup_smtp page so that it edits the routing table sensibly.

drh 2018-08-17 15:38 trunk
Commit 5416287d18f9486096bd318fcbd0c6f8848ec7710c53a376a595a96783fda534
1 file changed +141 -69
+141 -69
--- src/smtp.c
+++ src/smtp.c
@@ -734,53 +734,10 @@
734734
}
735735
if( eForce==1 || !db_table_exists("repository","emailblob") ){
736736
db_multi_exec(zEmailSchema/*works-like:""*/);
737737
}
738738
}
739
-
740
-/*
741
-** Process POST change requests from the setup_smtp page
742
-*/
743
-static void handle_smtp_setup_edit(char **pzErr, int *piErr){
744
- *piErr = 0;
745
- *pzErr = 0;
746
- if( !cgi_csrf_safe(1) ) return;
747
- if( P("new")!=0 ){
748
- const char *zEAddr = PDT("eaddr","");
749
- const char *zEPolicy = PDT("epolicy","");
750
- smtp_server_schema(0);
751
- if( db_exists("SELECT 1 FROM emailroute WHERE eaddr=%Q", zEAddr) ){
752
- *pzErr = mprintf("\"%s\" already exists", zEAddr);
753
- *piErr = 100;
754
- return;
755
- }
756
- db_multi_exec("INSERT INTO emailroute(eaddr,epolicy)"
757
- "VALUES(%Q,%Q)", zEAddr, zEPolicy);
758
- return;
759
- }
760
- if( P("delete")!=0 ){
761
- const char *zEAddr = PDT("eaddr","");
762
- const char *zEPolicy = PDT("epolicy","");
763
- smtp_server_schema(0);
764
- if( zEPolicy[0] ){
765
- *pzErr = mprintf("Erase routing information to delete");
766
- *piErr = 200;
767
- return;
768
- }
769
- db_multi_exec("DELETE FROM emailroute WHERE eaddr=%Q", zEAddr);
770
- return;
771
- }
772
- if( P("change")!=0 ){
773
- const char *zEAddr = PDT("eaddr","");
774
- const char *zEPolicy = PDT("epolicy","");
775
- smtp_server_schema(0);
776
- db_multi_exec("UPDATE emailroute SET epolicy=%Q WHERE eaddr=%Q",
777
- zEPolicy, zEAddr);
778
- return;
779
- }
780
-}
781
-
782739
783740
/*
784741
** WEBPAGE: setup_smtp
785742
**
786743
** Administrative page for configuring and controlling inbound email and
@@ -787,58 +744,173 @@
787744
** output email queuing. This page is available to administrators
788745
** only via the /Admin/EmailServer menu.
789746
*/
790747
void setup_smtp(void){
791748
Stmt q;
792
- char *zErr = 0;
793
- int iErr = 0;
794749
login_check_credentials();
795750
if( !g.perm.Setup ){
796751
login_needed(0);
797752
return;
798753
}
799754
db_begin_transaction();
800755
style_header("Email Server Setup");
801
- handle_smtp_setup_edit(&zErr, &iErr);
802756
if( db_table_exists("repository","emailroute") ){
757
+ style_submenu_element("emailblob table", "%R/emailblob");
758
+ style_submenu_element("emailoutq table", "%R/emailoutq");
803759
db_prepare(&q, "SELECT eaddr, epolicy FROM emailroute ORDER BY 1");
804760
}else{
805761
db_prepare(&q, "SELECT null, null WHERE false");
806762
}
807
- @ <table class="emailroutetab">
763
+ @ <h1>Email Routing Table</h1>
764
+ @ <table class="emailroutetab" cellpadding="5" border="1" cellspacing="0">
808765
@ <thead>
809
- @ <tr><th>Email Address
810
- @ <th>Routing
811
- @ <th></th>
766
+ @ <tr>
767
+ @ <th>Email Address
768
+ @ <th>Routing
769
+ @ <th>
770
+ @ </tr>
812771
@ </thead><tbody>
813772
while( db_step(&q)==SQLITE_ROW ){
814773
const char *zEAddr = db_column_text(&q, 0);
815774
const char *zEPolicy = db_column_text(&q, 1);
816
- @ <tr><form action="%R/setup_smtp" method="POST">
817
- @ <td valign="top">%h(zEAddr)\
818
- @ <input type="hidden" name="eaddr" value="%h(zEAddr)"></td>
819
- @ <td valign="top">\
820
- @ <textarea name="epolicy" rows="3" cols="40">%h(zEPolicy)</textarea>\
821
- @ </td>
822
- @ <td valign="top">\
823
- @ <input type="submit" name="change" value="Apply Changes"><br>\
824
- @ <input type="submit" name="delete" value="Delete"></td>
825
- @ </form></tr>
775
+ @ <tr>
776
+ @ <td valign="top">%h(zEAddr)</td>
777
+ @ <td valign="top"><span style="white-space:pre;">%h(zEPolicy)</span></td>
778
+ @ <td valign="top"><form method="POST" action="%R/setup_smtp_route">
779
+ @ <input type="hidden" name="oaddr" value="%h(zEAddr)">
780
+ @ <input type="submit" value="Edit">
781
+ @ </form>
826782
}
827783
db_finalize(&q);
828
- @ <tr><form action="%R/setup_smtp" method="POST">
829
- @ <td valign="top">
830
- @ <input type="text" name="eaddr" width="30"></td>
831
- @ <td valign="top">\
832
- @ <textarea name="epolicy" rows="3" cols="40"></textarea>\
833
- @ </td>
834
- @ <td valign="top"><input type="submit" name="new" value="New"></td>
835
- @ </form></tr>
784
+ @ <tr>
785
+ @ <td colspan="3">
786
+ @ <form method="POST" action="%R/setup_smtp_route">
787
+ @ <input type="submit" value="New">
788
+ @ &larr; Add a new email address
789
+ @ </form>
790
+ @ </table>
836791
style_footer();
837
- fossil_free(zErr);
838792
db_end_transaction(0);
839793
}
794
+
795
+/*
796
+** WEBPAGE: setup_smtp_route
797
+**
798
+** Edit a single entry in the emailroute table.
799
+** Query parameters:
800
+**
801
+** eaddr=ADDR ADDR is the email address as edited.
802
+**
803
+** oaddr=ADDR The original email address prior to editing.
804
+** Omit to add a new address.
805
+**
806
+** epolicy=TXT The routing policy.
807
+*/
808
+void setup_smtp_route(void){
809
+ char *zEAddr = PT("eaddr"); /* new email address */
810
+ char *zEPolicy = PT("epolicy"); /* new routing policy */
811
+ char *zOAddr = PT("oaddr"); /* original email address */
812
+ char *zErr;
813
+ int iErr;
814
+ login_check_credentials();
815
+ if( !g.perm.Setup ){
816
+ login_needed(0);
817
+ return;
818
+ }
819
+ style_header("Email Route Editor");
820
+
821
+ if( P("edit") && cgi_csrf_safe(1) && zEAddr!=0 && zEPolicy!=0 ){
822
+ smtp_server_schema(0);
823
+ if( (zOAddr==0 || fossil_strcmp(zEAddr,zOAddr)!=0) ){
824
+ /* New or changed email address */
825
+ if( db_exists("SELECT 1 FROM emailroute WHERE eaddr=%Q",zEAddr) ){
826
+ iErr = 1;
827
+ zErr = mprintf("email address \"%h(zEAddr)\" already exists",zEAddr);
828
+ goto smtp_route_edit;
829
+ }
830
+ if( zEPolicy[0]==0 ){
831
+ iErr = 2;
832
+ zErr = mprintf("empty route");
833
+ goto smtp_route_edit;
834
+ }
835
+ }
836
+ /* If the email address has changed, or if the new policy is blank,
837
+ ** delete the old address and route information
838
+ */
839
+ db_begin_transaction();
840
+ if( (zOAddr && fossil_strcmp(zEAddr,zOAddr)!=0) || zEPolicy[0]==0 ){
841
+ db_multi_exec("DELETE FROM emailroute WHERE eaddr=%Q", zOAddr);
842
+ }
843
+ if( zEPolicy[0] ){
844
+ /* Insert the new address and route */
845
+ db_multi_exec(
846
+ "REPLACE INTO emailroute(eaddr,epolicy) VALUES(%Q,%Q)",
847
+ zEAddr, zEPolicy
848
+ );
849
+ }
850
+ db_end_transaction(0);
851
+ cgi_redirectf("%R/setup_smtp");
852
+ }
853
+ if( P("cancel")!=0 ){
854
+ cgi_redirectf("%R/setup_smtp");
855
+ }
856
+
857
+smtp_route_edit:
858
+ if( zEAddr==0 ) zEAddr = zOAddr;
859
+ if( zEPolicy==0 && db_table_exists("repository","emailroute") ){
860
+ zEPolicy = db_text(0, "SELECT epolicy FROM emailroute WHERE eaddr=%Q",
861
+ zEAddr);
862
+ }
863
+ if( zEPolicy==0 ) zEPolicy = "";
864
+ @ <form method="POST" action="%R/setup_smtp_route">
865
+ if( zOAddr ){
866
+ @ <input type="hidden" name="oaddr" value="%h(zOAddr)">
867
+ }
868
+ @ <table class="label-value">
869
+ @ <tr>
870
+ @ <th>Email Address:</th>
871
+ @ <td><input type="text" size=30 name="eaddr" value="%h(zEAddr)">
872
+ if( iErr==1 ){
873
+ @ <td><span class="generalError">&larr; %z(zErr)</span>
874
+ }
875
+ @ </tr>
876
+ if( zOAddr && fossil_strcmp(zOAddr,zEAddr)!=0 ){
877
+ @ <tr>
878
+ @ <th>Original Address:</th>
879
+ @ <td>%h(zOAddr)
880
+ @ </tr>
881
+ }
882
+ @ <tr>
883
+ @ <th>Routing:</th>
884
+ @ <td><textarea name="epolicy" rows="3" cols="40">%h(zEPolicy)</textarea>
885
+ if( iErr==2 ){
886
+ @ <td valign="top"><span class="generalError">&larr; %z(zErr)</span>
887
+ }
888
+ @ </tr>
889
+ @ <tr>
890
+ @ <td>&nbsp;
891
+ @ <td><input type="submit" name="edit" value="Apply">
892
+ @ <input type="submit" name="cancel" value="Cancel">
893
+ @ </tr>
894
+ @ </table>
895
+ @ <hr>
896
+ @ <h1>Instructions</h1>
897
+ @
898
+ @ <p>The "Routing" field consists of zero or more lines where each
899
+ @ line is an "action" followed by an "argument". Available actions:
900
+ @ <ul>
901
+ @ <li><p><b>forward</b> <i>email-address</i>
902
+ @ <p>Forward the message to <i>email-address</i>.
903
+ @ <li><p><b>mbox</b> <i>login-name</i>
904
+ @ <p>Store the message in the local mailbox for the user
905
+ @ with USER.LOGIN=<i>login-name</i>.
906
+ @ </ul>
907
+ @
908
+ @ <p>To delete a route &rarr; erase all text from the "Routing" field then
909
+ @ press the "Apply" button.
910
+ style_footer();
911
+}
840912
841913
#if LOCAL_INTERFACE
842914
/*
843915
** State information for the server
844916
*/
845917
--- src/smtp.c
+++ src/smtp.c
@@ -734,53 +734,10 @@
734 }
735 if( eForce==1 || !db_table_exists("repository","emailblob") ){
736 db_multi_exec(zEmailSchema/*works-like:""*/);
737 }
738 }
739
740 /*
741 ** Process POST change requests from the setup_smtp page
742 */
743 static void handle_smtp_setup_edit(char **pzErr, int *piErr){
744 *piErr = 0;
745 *pzErr = 0;
746 if( !cgi_csrf_safe(1) ) return;
747 if( P("new")!=0 ){
748 const char *zEAddr = PDT("eaddr","");
749 const char *zEPolicy = PDT("epolicy","");
750 smtp_server_schema(0);
751 if( db_exists("SELECT 1 FROM emailroute WHERE eaddr=%Q", zEAddr) ){
752 *pzErr = mprintf("\"%s\" already exists", zEAddr);
753 *piErr = 100;
754 return;
755 }
756 db_multi_exec("INSERT INTO emailroute(eaddr,epolicy)"
757 "VALUES(%Q,%Q)", zEAddr, zEPolicy);
758 return;
759 }
760 if( P("delete")!=0 ){
761 const char *zEAddr = PDT("eaddr","");
762 const char *zEPolicy = PDT("epolicy","");
763 smtp_server_schema(0);
764 if( zEPolicy[0] ){
765 *pzErr = mprintf("Erase routing information to delete");
766 *piErr = 200;
767 return;
768 }
769 db_multi_exec("DELETE FROM emailroute WHERE eaddr=%Q", zEAddr);
770 return;
771 }
772 if( P("change")!=0 ){
773 const char *zEAddr = PDT("eaddr","");
774 const char *zEPolicy = PDT("epolicy","");
775 smtp_server_schema(0);
776 db_multi_exec("UPDATE emailroute SET epolicy=%Q WHERE eaddr=%Q",
777 zEPolicy, zEAddr);
778 return;
779 }
780 }
781
782
783 /*
784 ** WEBPAGE: setup_smtp
785 **
786 ** Administrative page for configuring and controlling inbound email and
@@ -787,58 +744,173 @@
787 ** output email queuing. This page is available to administrators
788 ** only via the /Admin/EmailServer menu.
789 */
790 void setup_smtp(void){
791 Stmt q;
792 char *zErr = 0;
793 int iErr = 0;
794 login_check_credentials();
795 if( !g.perm.Setup ){
796 login_needed(0);
797 return;
798 }
799 db_begin_transaction();
800 style_header("Email Server Setup");
801 handle_smtp_setup_edit(&zErr, &iErr);
802 if( db_table_exists("repository","emailroute") ){
 
 
803 db_prepare(&q, "SELECT eaddr, epolicy FROM emailroute ORDER BY 1");
804 }else{
805 db_prepare(&q, "SELECT null, null WHERE false");
806 }
807 @ <table class="emailroutetab">
 
808 @ <thead>
809 @ <tr><th>Email Address
810 @ <th>Routing
811 @ <th></th>
 
 
812 @ </thead><tbody>
813 while( db_step(&q)==SQLITE_ROW ){
814 const char *zEAddr = db_column_text(&q, 0);
815 const char *zEPolicy = db_column_text(&q, 1);
816 @ <tr><form action="%R/setup_smtp" method="POST">
817 @ <td valign="top">%h(zEAddr)\
818 @ <input type="hidden" name="eaddr" value="%h(zEAddr)"></td>
819 @ <td valign="top">\
820 @ <textarea name="epolicy" rows="3" cols="40">%h(zEPolicy)</textarea>\
821 @ </td>
822 @ <td valign="top">\
823 @ <input type="submit" name="change" value="Apply Changes"><br>\
824 @ <input type="submit" name="delete" value="Delete"></td>
825 @ </form></tr>
826 }
827 db_finalize(&q);
828 @ <tr><form action="%R/setup_smtp" method="POST">
829 @ <td valign="top">
830 @ <input type="text" name="eaddr" width="30"></td>
831 @ <td valign="top">\
832 @ <textarea name="epolicy" rows="3" cols="40"></textarea>\
833 @ </td>
834 @ <td valign="top"><input type="submit" name="new" value="New"></td>
835 @ </form></tr>
836 style_footer();
837 fossil_free(zErr);
838 db_end_transaction(0);
839 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
840
841 #if LOCAL_INTERFACE
842 /*
843 ** State information for the server
844 */
845
--- src/smtp.c
+++ src/smtp.c
@@ -734,53 +734,10 @@
734 }
735 if( eForce==1 || !db_table_exists("repository","emailblob") ){
736 db_multi_exec(zEmailSchema/*works-like:""*/);
737 }
738 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
739
740 /*
741 ** WEBPAGE: setup_smtp
742 **
743 ** Administrative page for configuring and controlling inbound email and
@@ -787,58 +744,173 @@
744 ** output email queuing. This page is available to administrators
745 ** only via the /Admin/EmailServer menu.
746 */
747 void setup_smtp(void){
748 Stmt q;
 
 
749 login_check_credentials();
750 if( !g.perm.Setup ){
751 login_needed(0);
752 return;
753 }
754 db_begin_transaction();
755 style_header("Email Server Setup");
 
756 if( db_table_exists("repository","emailroute") ){
757 style_submenu_element("emailblob table", "%R/emailblob");
758 style_submenu_element("emailoutq table", "%R/emailoutq");
759 db_prepare(&q, "SELECT eaddr, epolicy FROM emailroute ORDER BY 1");
760 }else{
761 db_prepare(&q, "SELECT null, null WHERE false");
762 }
763 @ <h1>Email Routing Table</h1>
764 @ <table class="emailroutetab" cellpadding="5" border="1" cellspacing="0">
765 @ <thead>
766 @ <tr>
767 @ <th>Email Address
768 @ <th>Routing
769 @ <th>
770 @ </tr>
771 @ </thead><tbody>
772 while( db_step(&q)==SQLITE_ROW ){
773 const char *zEAddr = db_column_text(&q, 0);
774 const char *zEPolicy = db_column_text(&q, 1);
775 @ <tr>
776 @ <td valign="top">%h(zEAddr)</td>
777 @ <td valign="top"><span style="white-space:pre;">%h(zEPolicy)</span></td>
778 @ <td valign="top"><form method="POST" action="%R/setup_smtp_route">
779 @ <input type="hidden" name="oaddr" value="%h(zEAddr)">
780 @ <input type="submit" value="Edit">
781 @ </form>
 
 
 
782 }
783 db_finalize(&q);
784 @ <tr>
785 @ <td colspan="3">
786 @ <form method="POST" action="%R/setup_smtp_route">
787 @ <input type="submit" value="New">
788 @ &larr; Add a new email address
789 @ </form>
790 @ </table>
 
791 style_footer();
 
792 db_end_transaction(0);
793 }
794
795 /*
796 ** WEBPAGE: setup_smtp_route
797 **
798 ** Edit a single entry in the emailroute table.
799 ** Query parameters:
800 **
801 ** eaddr=ADDR ADDR is the email address as edited.
802 **
803 ** oaddr=ADDR The original email address prior to editing.
804 ** Omit to add a new address.
805 **
806 ** epolicy=TXT The routing policy.
807 */
808 void setup_smtp_route(void){
809 char *zEAddr = PT("eaddr"); /* new email address */
810 char *zEPolicy = PT("epolicy"); /* new routing policy */
811 char *zOAddr = PT("oaddr"); /* original email address */
812 char *zErr;
813 int iErr;
814 login_check_credentials();
815 if( !g.perm.Setup ){
816 login_needed(0);
817 return;
818 }
819 style_header("Email Route Editor");
820
821 if( P("edit") && cgi_csrf_safe(1) && zEAddr!=0 && zEPolicy!=0 ){
822 smtp_server_schema(0);
823 if( (zOAddr==0 || fossil_strcmp(zEAddr,zOAddr)!=0) ){
824 /* New or changed email address */
825 if( db_exists("SELECT 1 FROM emailroute WHERE eaddr=%Q",zEAddr) ){
826 iErr = 1;
827 zErr = mprintf("email address \"%h(zEAddr)\" already exists",zEAddr);
828 goto smtp_route_edit;
829 }
830 if( zEPolicy[0]==0 ){
831 iErr = 2;
832 zErr = mprintf("empty route");
833 goto smtp_route_edit;
834 }
835 }
836 /* If the email address has changed, or if the new policy is blank,
837 ** delete the old address and route information
838 */
839 db_begin_transaction();
840 if( (zOAddr && fossil_strcmp(zEAddr,zOAddr)!=0) || zEPolicy[0]==0 ){
841 db_multi_exec("DELETE FROM emailroute WHERE eaddr=%Q", zOAddr);
842 }
843 if( zEPolicy[0] ){
844 /* Insert the new address and route */
845 db_multi_exec(
846 "REPLACE INTO emailroute(eaddr,epolicy) VALUES(%Q,%Q)",
847 zEAddr, zEPolicy
848 );
849 }
850 db_end_transaction(0);
851 cgi_redirectf("%R/setup_smtp");
852 }
853 if( P("cancel")!=0 ){
854 cgi_redirectf("%R/setup_smtp");
855 }
856
857 smtp_route_edit:
858 if( zEAddr==0 ) zEAddr = zOAddr;
859 if( zEPolicy==0 && db_table_exists("repository","emailroute") ){
860 zEPolicy = db_text(0, "SELECT epolicy FROM emailroute WHERE eaddr=%Q",
861 zEAddr);
862 }
863 if( zEPolicy==0 ) zEPolicy = "";
864 @ <form method="POST" action="%R/setup_smtp_route">
865 if( zOAddr ){
866 @ <input type="hidden" name="oaddr" value="%h(zOAddr)">
867 }
868 @ <table class="label-value">
869 @ <tr>
870 @ <th>Email Address:</th>
871 @ <td><input type="text" size=30 name="eaddr" value="%h(zEAddr)">
872 if( iErr==1 ){
873 @ <td><span class="generalError">&larr; %z(zErr)</span>
874 }
875 @ </tr>
876 if( zOAddr && fossil_strcmp(zOAddr,zEAddr)!=0 ){
877 @ <tr>
878 @ <th>Original Address:</th>
879 @ <td>%h(zOAddr)
880 @ </tr>
881 }
882 @ <tr>
883 @ <th>Routing:</th>
884 @ <td><textarea name="epolicy" rows="3" cols="40">%h(zEPolicy)</textarea>
885 if( iErr==2 ){
886 @ <td valign="top"><span class="generalError">&larr; %z(zErr)</span>
887 }
888 @ </tr>
889 @ <tr>
890 @ <td>&nbsp;
891 @ <td><input type="submit" name="edit" value="Apply">
892 @ <input type="submit" name="cancel" value="Cancel">
893 @ </tr>
894 @ </table>
895 @ <hr>
896 @ <h1>Instructions</h1>
897 @
898 @ <p>The "Routing" field consists of zero or more lines where each
899 @ line is an "action" followed by an "argument". Available actions:
900 @ <ul>
901 @ <li><p><b>forward</b> <i>email-address</i>
902 @ <p>Forward the message to <i>email-address</i>.
903 @ <li><p><b>mbox</b> <i>login-name</i>
904 @ <p>Store the message in the local mailbox for the user
905 @ with USER.LOGIN=<i>login-name</i>.
906 @ </ul>
907 @
908 @ <p>To delete a route &rarr; erase all text from the "Routing" field then
909 @ press the "Apply" button.
910 style_footer();
911 }
912
913 #if LOCAL_INTERFACE
914 /*
915 ** State information for the server
916 */
917

Keyboard Shortcuts

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