Integrated the DKIM signer into serv_smtpclient, but disabled it
[citadel.git] / webcit / netconf.c
index 79130b8db2fb5c78ea6723197f48c8d3664f0e5e..6ff53fa5656fea83884cffef096c91ab80f8811b 100644 (file)
-/* 
- * $Id$
- */
-/**
- * \defgroup NetShareConf Functions which handle network and sharing configuration.
- *
- * \ingroup CitadelConfig
- */
-/*@{*/
+// Copyright (c) 1999-2004 by the citadel.org team
+// This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
+
+
 #include "webcit.h"
 
-/**
- * \brief edit a network node
- */
-void edit_node(void) {
-       char buf[SIZ];
-       char node[SIZ];
-       char cnode[SIZ];
-       FILE *fp;
+void display_netconf(void);
+
+CtxType CTX_NODECONF = CTX_NONE;
+/*----------------------------------------------------------------------*/
+/*              Business Logic                                          */
+/*----------------------------------------------------------------------*/
+
+typedef struct _nodeconf {
+       int DeleteMe;
+       StrBuf *NodeName;
+       StrBuf *Secret;
+       StrBuf *Host;
+       StrBuf *Port;
+} NodeConf;
+
+void DeleteNodeConf(void *vNode) {
+       NodeConf *Node = (NodeConf*) vNode;
+       FreeStrBuf(&Node->NodeName);
+       FreeStrBuf(&Node->Secret);
+       FreeStrBuf(&Node->Host);
+       FreeStrBuf(&Node->Port);
+       free(Node);
+}
 
-       if (havebstr("ok_button")) {
-               strcpy(node, bstr("node") );
-               fp = tmpfile();
-               if (fp != NULL) {
-                       serv_puts("CONF getsys|application/x-citadel-ignet-config");
-                       serv_getln(buf, sizeof buf);
-                       if (buf[0] == '1') {
-                               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
-                                       extract_token(cnode, buf, 0, '|', sizeof cnode);
-                                       if (strcasecmp(node, cnode)) {
-                                               fprintf(fp, "%s\n", buf);
-                                       }
-                               }
-                       rewind(fp);
-               }
+NodeConf *NewNode(StrBuf *SerializedNode) {
+       NodeConf *Node;
+
+       if (StrLength(SerializedNode) < 8) 
+               return NULL; /* we need at least 4 pipes and some other text so its invalid. */
+       Node = (NodeConf *) malloc(sizeof(NodeConf));
+       Node->DeleteMe = 0;
+       Node->NodeName=NewStrBuf();
+       StrBufExtract_token(Node->NodeName, SerializedNode, 0, '|');
+       Node->Secret=NewStrBuf();
+       StrBufExtract_token(Node->Secret, SerializedNode, 1, '|');
+       Node->Host=NewStrBuf();
+       StrBufExtract_token(Node->Host, SerializedNode, 2, '|');
+       Node->Port=NewStrBuf();
+       StrBufExtract_token(Node->Port, SerializedNode, 3, '|');
+       return Node;
+}
 
-               serv_puts("CONF putsys|application/x-citadel-ignet-config");
-               serv_getln(buf, sizeof buf);
-               if (buf[0] == '4') {
-                       if (fp != NULL) {
-                               while (fgets(buf, sizeof buf, fp) != NULL) {
-                                       buf[strlen(buf)-1] = 0;
-                                       if (buf[0] != 0) {
-                                               serv_puts(buf);
-                                       }
-                               }
-                               fclose(fp);
-                       }
-                       serv_printf("%s|%s|%s|%s", 
-                               bstr("node"),
-                               bstr("secret"),
-                               bstr("host"),
-                               bstr("port") );
+NodeConf *HttpGetNewNode(void) {
+       NodeConf *Node;
+
+       if (!havebstr("node") || 
+           !havebstr("secret")||
+           !havebstr("host")||
+           !havebstr("port"))
+               return NULL;
+
+       Node = (NodeConf *) malloc(sizeof(NodeConf));
+       Node->DeleteMe = 0;
+       Node->NodeName = NewStrBufDup(sbstr("node"));
+       Node->Secret = NewStrBufDup(sbstr("secret"));
+       Node->Host = NewStrBufDup(sbstr("host"));
+       Node->Port = NewStrBufDup(sbstr("port"));
+       return Node;
+}
+
+void SerializeNode(NodeConf *Node, StrBuf *Buf) {
+       StrBufPrintf(Buf, "%s|%s|%s|%s", 
+                    ChrPtr(Node->NodeName),
+                    ChrPtr(Node->Secret),
+                    ChrPtr(Node->Host),
+                    ChrPtr(Node->Port));
+}
+
+
+HashList *load_netconf(StrBuf *Target, WCTemplputParams *TP) {
+       StrBuf *Buf;
+       HashList *Hash;
+       char nnn[64];
+       char buf[SIZ];
+       int nUsed;
+       NodeConf *Node;
+
+       serv_puts("CONF getsys|application/x-citadel-ignet-config");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '1') {
+               Hash = NewHash(1, NULL);
+
+               Buf = NewStrBuf();
+               while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
+                       Node = NewNode(Buf);
+                       if (Node != NULL) {
+                               nUsed = GetCount(Hash);
+                               nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
+                               Put(Hash, nnn, nUsed, Node, DeleteNodeConf); 
                        }
-                       serv_puts("000");
                }
+               FreeStrBuf(&Buf);
+               return Hash;
        }
-
-       display_netconf();
+       return NULL;
 }
 
 
-/**
- * \brief add a node
- */
-void display_add_node(void)
-{
-       output_headers(1, 1, 2, 0, 0, 0);
-       wprintf("<div id=\"banner\">\n");
-       wprintf("<h1>");
-       wprintf(_("Add a new node"));
-       wprintf("</h1>");
-       wprintf("</div>\n");
-
-       wprintf("<div id=\"content\" class=\"service\">\n");
-
-       wprintf("<FORM METHOD=\"POST\" action=\"edit_node\">\n");
-       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
-       wprintf("<CENTER><TABLE border=0>\n");
-       wprintf("<TR><TD>%s</TD>", _("Node name"));
-       wprintf("<TD><INPUT TYPE=\"text\" NAME=\"node\" MAXLENGTH=\"16\"></TD></TR>\n");
-       wprintf("<TR><TD>%s</TD>", _("Shared secret"));
-       wprintf("<TD><INPUT TYPE=\"password\" NAME=\"secret\" MAXLENGTH=\"16\"></TD></TR>\n");
-       wprintf("<TR><TD>%s</TD>", _("Host or IP address"));
-       wprintf("<TD><INPUT TYPE=\"text\" NAME=\"host\" MAXLENGTH=\"64\"></TD></TR>\n");
-       wprintf("<TR><TD>%s</TD>", _("Port number"));
-       wprintf("<TD><INPUT TYPE=\"text\" NAME=\"port\" VALUE=\"504\" MAXLENGTH=\"8\"></TD></TR>\n");
-       wprintf("</TABLE><br />");
-               wprintf("<INPUT TYPE=\"submit\" NAME=\"ok_button\" VALUE=\"%s\">", _("Add node"));
-       wprintf("&nbsp;");
-               wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
-       wprintf("</CENTER></FORM>\n");
 
-       wDumpContent(1);
+void save_net_conf(HashList *Nodelist) {
+       char buf[SIZ];
+       StrBuf *Buf;
+       HashPos *where;
+       void *vNode;
+       NodeConf *Node;
+       const char *Key;
+       long KeyLen;
+
+       serv_puts("CONF putsys|application/x-citadel-ignet-config");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '4') {
+               if ((Nodelist != NULL) && (GetCount(Nodelist) > 0)) {
+                       where = GetNewHashPos(Nodelist, 0);
+                       Buf = NewStrBuf();
+                       while (GetNextHashPos(Nodelist, where, &KeyLen, &Key, &vNode)) {
+                               Node = (NodeConf*) vNode;
+                               if (Node->DeleteMe==0) { 
+                                       SerializeNode(Node, Buf);
+                                       serv_putbuf(Buf);
+                               }
+                       }
+                       FreeStrBuf(&Buf);
+                       DeleteHashPos(&where);
+               }
+               serv_puts("000");
+       }
 }
 
-/**
- * \brief modify an existing node
- */
-void display_edit_node(void)
-{
-       char buf[512];
-       char node[256];
-       char cnode[256];
-       char csecret[256];
-       char chost[256];
-       char cport[256];
 
-       strcpy(node, bstr("node"));
 
-       output_headers(1, 1, 2, 0, 0, 0);
-       wprintf("<div id=\"banner\">\n");
-       wprintf("<h1>");
-       wprintf(_("Edit node configuration for "));
-       escputs(node);
-       wprintf("</h1>");
-       wprintf("</div>\n");
+/*----------------------------------------------------------------------*/
+/*              WEB Handlers                                            */
+/*----------------------------------------------------------------------*/
 
-       wprintf("<div id=\"content\" class=\"service\">\n");
 
-       serv_puts("CONF getsys|application/x-citadel-ignet-config");
-       serv_getln(buf, sizeof buf);
-       if (buf[0] == '1') {
-               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
-                       extract_token(cnode, buf, 0, '|', sizeof cnode);
-                       extract_token(csecret, buf, 1, '|', sizeof csecret);
-                       extract_token(chost, buf, 2, '|', sizeof chost);
-                       extract_token(cport, buf, 3, '|', sizeof cport);
-
-                       if (!strcasecmp(node, cnode)) {
-                               wprintf("<FORM METHOD=\"POST\" action=\"edit_node\">\n");
-                               wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
-                               wprintf("<CENTER><TABLE border=0>\n");
-                               wprintf("<TR><TD>");
-                               wprintf(_("Node name"));
-                               wprintf("</TD>");
-                               wprintf("<TD><INPUT TYPE=\"text\" NAME=\"node\" MAXLENGTH=\"16\" VALUE=\"%s\"></TD></TR>\n", cnode);
-                               wprintf("<TR><TD>");
-                               wprintf(_("Shared secret"));
-                               wprintf("</TD>");
-                               wprintf("<TD><INPUT TYPE=\"password\" NAME=\"secret\" MAXLENGTH=\"16\" VALUE=\"%s\"></TD></TR>\n", csecret);
-                               wprintf("<TR><TD>");
-                               wprintf(_("Host or IP address"));
-                               wprintf("</TD>");
-                               wprintf("<TD><INPUT TYPE=\"text\" NAME=\"host\" MAXLENGTH=\"64\" VALUE=\"%s\"></TD></TR>\n", chost);
-                               wprintf("<TR><TD>");
-                               wprintf(_("Port number"));
-                               wprintf("</TD>");
-                               wprintf("<TD><INPUT TYPE=\"text\" NAME=\"port\" MAXLENGTH=\"8\" VALUE=\"%s\"></TD></TR>\n", cport);
-                               wprintf("</TABLE><br />");
-                               wprintf("<INPUT TYPE=\"submit\" NAME=\"ok_button\" VALUE=\"%s\">",
-                                       _("Save changes"));
-                               wprintf("&nbsp;");
-                               wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">",
-                                       _("Cancel"));
-                               wprintf("</CENTER></FORM>\n");
-                       }
 
+/*
+ * edit a network node
+ */
+void edit_node(void) {
+       HashList *NodeConfig;
+       const StrBuf *Index;
+       NodeConf *NewNode;
+
+       if (havebstr("ok_button")) {
+               Index = sbstr("index");
+               NewNode = HttpGetNewNode();
+               if ((NewNode == NULL) || (Index == NULL)) {
+                       AppendImportantMessage(_("Invalid Parameter"), -1);
+                       url_do_template();
+                       return;
                }
+                       
+               NodeConfig = load_netconf(NULL, &NoCtx);
+               Put(NodeConfig, ChrPtr(Index), StrLength(Index), NewNode, DeleteNodeConf);
+               save_net_conf(NodeConfig);
+               DeleteHash(&NodeConfig);
        }
+       url_do_template();
+}
+
 
-       else {          /** command error getting configuration */
-               wprintf("%s<br />\n", &buf[4]);
+/*
+ * modify an existing node
+ */
+void display_edit_node(void) {
+       WCTemplputParams SubTP;
+       HashList *NodeConfig;
+       const StrBuf *Index;
+       void *vNode;
+       const StrBuf *Tmpl;
+
+       Index = sbstr("index");
+       if (Index == NULL) {
+               AppendImportantMessage(_("Invalid Parameter"), -1);
+               url_do_template();
+               return;
        }
 
-       wDumpContent(1);
+       NodeConfig = load_netconf(NULL, &NoCtx);
+       if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
+           (vNode == NULL)) {
+               AppendImportantMessage(_("Invalid Parameter"), -1);
+               url_do_template();
+               DeleteHash(&NodeConfig);
+               return;
+       }
+       StackContext(NULL, &SubTP, vNode, CTX_NODECONF, 0, NULL);
+       {
+               begin_burst();
+               Tmpl = sbstr("template");
+               output_headers(1, 0, 0, 0, 1, 0);
+               DoTemplate(SKEY(Tmpl), NULL, &SubTP);
+               end_burst();
+       }
+       UnStackContext(&SubTP);
+       DeleteHash(&NodeConfig);
+       
 }
 
 
-/**
- * \brief display all configured nodes
+/*
+ * display all configured nodes
  */
-void display_netconf(void)
-{
-       char buf[SIZ];
-       char node[SIZ];
-
-       output_headers(1, 1, 2, 0, 0, 0);
-       wprintf("<div id=\"banner\">\n");
-       wprintf("<h1>");
-       wprintf(_("Network configuration"));
-       wprintf("</h1>");
-       wprintf("</div>\n");
-
-       wprintf("<div id=\"content\" class=\"service\">\n");
-
-       wprintf("<CENTER>");
-       wprintf("<a href=\"display_add_node\">");
-       wprintf(_("Add a new node"));
-       wprintf("</A><br />\n");
-       wprintf("</CENTER>");
-
-       wprintf("<TABLE class=\"netconf_banner\"><TR><TD>");
-       wprintf("<SPAN CLASS=\"titlebar\">");
-       wprintf(_("Currently configured nodes"));
-       wprintf("</SPAN>\n");
-       wprintf("</TD></TR></TABLE>\n");
-       serv_puts("CONF getsys|application/x-citadel-ignet-config");
-       serv_getln(buf, sizeof buf);
-       if (buf[0] == '1') {
-               wprintf("<CENTER><TABLE border=0>\n");
-               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
-                       extract_token(node, buf, 0, '|', sizeof node);
-                       wprintf("<TR><TD><FONT SIZE=+1>");
-                       escputs(node);
-                       wprintf("</FONT></TD>");
-                       wprintf("<TD><a href=\"display_edit_node&node=");
-                       urlescputs(node);
-                       wprintf("\">");
-                       wprintf(_("(Edit)"));
-                       wprintf("</A></TD>");
-                       wprintf("<TD><a href=\"display_confirm_delete_node&node=");
-                       urlescputs(node);
-                       wprintf("\">");
-                       wprintf(_("(Delete)"));
-                       wprintf("</A></TD>");
-                       wprintf("</TR>\n");
-               }
-               wprintf("</TABLE></CENTER>\n");
-       }
+void display_netconf(void) {
        wDumpContent(1);
 }
 
-/**
- * \brief display the dialog to verify the deletion
+/*
+ * display the dialog to verify the deletion
  */
-void display_confirm_delete_node(void)
-{
-       char node[SIZ];
-
-       output_headers(1, 1, 2, 0, 0, 0);
-       wprintf("<div id=\"banner\">\n");
-       wprintf("<h1>");
-       wprintf(_("Confirm delete"));
-       wprintf("</h1>");
-       wprintf("</div>\n");
-
-       wprintf("<div id=\"content\" class=\"service\" >\n");
-
-       strcpy(node, bstr("node"));
-       wprintf("<CENTER>");
-       wprintf(_("Are you sure you want to delete "));
-       wprintf("<FONT SIZE=+1>");
-       escputs(node);
-       wprintf("</FONT>?<br />\n");
-       wprintf("<a href=\"delete_node&node=");
-       urlescputs(node);
-       wprintf("\">");
-       wprintf(_("Yes"));
-       wprintf("</A>&nbsp;&nbsp;&nbsp;");
-       wprintf("<a href=\"display_netconf\">");
-       wprintf(_("No"));
-       wprintf("</A><br />\n");
+void display_confirm_delete_node(void) {
        wDumpContent(1);
 }
 
-/**
- * \brief actually delete the node
+
+/*
+ * actually delete the node
  */
-void delete_node(void)
-{
-       char buf[SIZ];
-       char node[SIZ];
-       char cnode[SIZ];
-       FILE *fp;
-
-       strcpy(node, bstr("node") );
-       fp = tmpfile();
-       if (fp != NULL) {
-               serv_puts("CONF getsys|application/x-citadel-ignet-config");
-               serv_getln(buf, sizeof buf);
-               if (buf[0] == '1') {
-                       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
-                               extract_token(cnode, buf, 0, '|', sizeof cnode);
-                               if (strcasecmp(node, cnode)) {
-                                       fprintf(fp, "%s\n", buf);
-                               }
-                       }
-               }
-               rewind(fp);
-
-               serv_puts("CONF putsys|application/x-citadel-ignet-config");
-               serv_getln(buf, sizeof buf);
-               if (buf[0] == '4') {
-                       while (fgets(buf, sizeof buf, fp) != NULL) {
-                               buf[strlen(buf)-1] = 0;
-                               serv_puts(buf);
-                       }
-                       serv_puts("000");
-               }
-               fclose(fp);
+void delete_node(void) {
+       HashList *NodeConfig;
+       const StrBuf *Index;
+       NodeConf *Node;
+       void *vNode;
+
+       Index = sbstr("index");
+       if (Index == NULL) {
+               AppendImportantMessage(_("Invalid Parameter"), -1);
+               url_do_template();
+               return;
        }
 
-       display_netconf();
+       NodeConfig = load_netconf(NULL, &NoCtx);
+       if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
+           (vNode == NULL)) {
+               AppendImportantMessage(_("Invalid Parameter"), -1);
+               url_do_template();
+               DeleteHash(&NodeConfig);
+               return;
+       }
+       Node = (NodeConf *) vNode;
+       Node->DeleteMe = 1;
+               save_net_conf(NodeConfig);
+       DeleteHash(&NodeConfig);
+       
+       url_do_template();
+
+}
+
+
+void tmplput_NodeName(StrBuf *Target, WCTemplputParams *TP) {
+       NodeConf *Node = (NodeConf*) CTX(CTX_NODECONF); 
+       StrBufAppendTemplate(Target, TP, Node->NodeName, 0);
 }
 
+void tmplput_Secret(StrBuf *Target, WCTemplputParams *TP) {
+       NodeConf *Node = (NodeConf*) CTX(CTX_NODECONF);
+       StrBufAppendTemplate(Target, TP, Node->Secret, 0);
+}
+
+void tmplput_Host(StrBuf *Target, WCTemplputParams *TP) {
+       NodeConf *Node= (NodeConf*) CTX(CTX_NODECONF);
+       StrBufAppendTemplate(Target, TP, Node->Host, 0);
+}
+
+void tmplput_Port(StrBuf *Target, WCTemplputParams *TP) {
+       NodeConf *Node= (NodeConf*) CTX(CTX_NODECONF);
+       StrBufAppendTemplate(Target, TP, Node->Port, 0);
+}
 
-/*@}*/
+void 
+InitModule_NETCONF
+(void)
+{
+       RegisterCTX(CTX_NODECONF);
+       WebcitAddUrlHandler(HKEY("display_edit_node"), "", 0, display_edit_node, 0);
+
+       WebcitAddUrlHandler(HKEY("aide_ignetconf_edit_node"), "", 0, edit_node, 0);
+       WebcitAddUrlHandler(HKEY("display_netconf"), "", 0, display_netconf, 0);
+       WebcitAddUrlHandler(HKEY("display_confirm_delete_node"), "", 0, display_confirm_delete_node, 0);
+       WebcitAddUrlHandler(HKEY("delete_node"), "", 0, delete_node, 0);
+
+                                                                                          
+        RegisterNamespace("CFG:IGNET:NODE", 0, 1, tmplput_NodeName, NULL, CTX_NODECONF);
+        RegisterNamespace("CFG:IGNET:SECRET", 0, 1, tmplput_Secret, NULL, CTX_NODECONF);
+        RegisterNamespace("CFG:IGNET:HOST", 0, 1, tmplput_Host, NULL, CTX_NODECONF);
+        RegisterNamespace("CFG:IGNET:PORT", 0, 1, tmplput_Port, NULL, CTX_NODECONF);
+
+       RegisterIterator("NODECONFIG", 0, NULL, load_netconf, NULL, DeleteHash, CTX_NODECONF, CTX_NONE, IT_NOFLAG);
+}