Integrated the DKIM signer into serv_smtpclient, but disabled it
[citadel.git] / webcit / netconf.c
index 010fec575e532ac728188923b62eba46332c7791..6ff53fa5656fea83884cffef096c91ab80f8811b 100644 (file)
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <ctype.h>
-#include <string.h>
-#include "webcit.h"
-#include "child.h"
+// 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.
 
-struct sharelist {
-       struct sharelist *next;
-       char shname[256];
-};
 
+#include "webcit.h"
 
-void display_edit_node(void)
-{
-       char buf[256];
-       char node[256];
-       char sroom[256];
-
-       strcpy(node, bstr("node"));
-
-       printf("HTTP/1.0 200 OK\n");
-       output_headers(1);
-       wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=000077><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
-       wprintf("<B>Edit share list for ");
-       escputs(node);
-       wprintf("</B>\n");
-       wprintf("</FONT></TD></TR></TABLE>\n");
-
-       wprintf("<CENTER>\n");
-       wprintf("<A HREF=\"/display_share&node=");
-       urlescputs(node);
-       wprintf("\">Add a shared room</A><BR>\n");
-
-       wprintf("<A HREF=\"/display_netconf");
-       wprintf("\">Return to network configuration screen</A><BR>\n");
-
-       serv_printf("NSET roomlist|%s", node);
-       serv_gets(buf);
-       if (buf[0] == '1') {
-               wprintf("<TABLE border=0>\n");
-               while (serv_gets(buf), strcmp(buf, "000")) {
-                       extract(sroom, buf, 0);
-                       wprintf("<TR><TD><FONT SIZE=+1>");
-                       escputs(sroom);
-                       wprintf("</FONT></TD>");
-                       wprintf("<TD><A HREF=\"/display_confirm_unshare&sroom=");
-                       urlescputs(sroom);
-                       wprintf("&node=");
-                       urlescputs(node);
-                       wprintf("\">(UnShare)</A></TD>");
-                       wprintf("</TR>\n");
-               }
-               wprintf("</TABLE></CENTER>\n");
-       }
-       wDumpContent(1);
+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);
 }
 
-
-
-void display_netconf(void)
-{
-       char buf[256];
-       char node[256];
-
-       printf("HTTP/1.0 200 OK\n");
-       output_headers(1);
-       wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
-       wprintf("<B>Network configuration</B>\n");
-       wprintf("</FONT></TD></TR></TABLE>\n");
-
-       wprintf("<CENTER>");
-       wprintf("<A HREF=\"/display_add_node\">");
-       wprintf("Add a new node</A><BR>\n");
-       wprintf("</CENTER>");
-
-       wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=000077><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
-       wprintf("<B>Currently configured nodes</B>\n");
-       wprintf("</FONT></TD></TR></TABLE>\n");
-       serv_puts("NSET nodelist");
-       serv_gets(buf);
-       if (buf[0] == '1') {
-               wprintf("<CENTER><TABLE border=0>\n");
-               while (serv_gets(buf), strcmp(buf, "000")) {
-                       extract(node, buf, 0);
-                       wprintf("<TR><TD><FONT SIZE=+1>");
-                       escputs(node);
-                       wprintf("</FONT></TD>");
-                       wprintf("<TD><A HREF=\"/display_edit_node&node=");
-                       urlescputs(node);
-                       wprintf("\">(Edit)</A></TD>");
-                       wprintf("<TD><A HREF=\"/display_confirm_delete_node&node=");
-                       urlescputs(node);
-                       wprintf("\">(Delete)</A></TD>");
-                       wprintf("</TR>\n");
-               }
-               wprintf("</TABLE></CENTER>\n");
-       }
-       wDumpContent(1);
+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;
 }
 
-
-void display_confirm_unshare(void)
-{
-       char node[256];
-       char sroom[256];
-
-       printf("HTTP/1.0 200 OK\n");
-       output_headers(1);
-       wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
-       wprintf("<B>Confirm unshare</B>\n");
-       wprintf("</FONT></TD></TR></TABLE>\n");
-
-       strcpy(node, bstr("node"));
-       strcpy(sroom, bstr("sroom"));
-       wprintf("<CENTER>Are you sure you want to unshare <FONT SIZE=+1>");
-       escputs(sroom);
-       wprintf("</FONT>?<BR>\n");
-       wprintf("<A HREF=\"/unshare&node=");
-       urlescputs(node);
-       wprintf("&sroom=");
-       urlescputs(sroom);
-       wprintf("\">Yes</A>&nbsp;&nbsp;&nbsp;");
-       wprintf("<A HREF=\"/display_edit_node&node=");
-       urlescputs(node);
-       wprintf("\">No</A><BR>\n");
-       wDumpContent(1);
+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 display_confirm_delete_node(void)
-{
-       char node[256];
-
-       printf("HTTP/1.0 200 OK\n");
-       output_headers(1);
-       wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
-       wprintf("<B>Confirm delete</B>\n");
-       wprintf("</FONT></TD></TR></TABLE>\n");
-
-       strcpy(node, bstr("node"));
-       wprintf("<CENTER>Are you sure you want to delete <FONT SIZE=+1>");
-       escputs(node);
-       wprintf("</FONT>?<BR>\n");
-       wprintf("<A HREF=\"/delete_node&node=");
-       urlescputs(node);
-       wprintf("\">Yes</A>&nbsp;&nbsp;&nbsp;");
-       wprintf("<A HREF=\"/display_netconf\">No</A><BR>\n");
-       wDumpContent(1);
+void SerializeNode(NodeConf *Node, StrBuf *Buf) {
+       StrBufPrintf(Buf, "%s|%s|%s|%s", 
+                    ChrPtr(Node->NodeName),
+                    ChrPtr(Node->Secret),
+                    ChrPtr(Node->Host),
+                    ChrPtr(Node->Port));
 }
 
 
-void delete_node(void)
-{
-       char node[256];
-       char buf[256];
+HashList *load_netconf(StrBuf *Target, WCTemplputParams *TP) {
+       StrBuf *Buf;
+       HashList *Hash;
+       char nnn[64];
+       char buf[SIZ];
+       int nUsed;
+       NodeConf *Node;
 
-       strcpy(node, bstr("node"));
-       sprintf(buf, "NSET deletenode|%s", node);
-       serv_puts(buf);
-       serv_gets(buf);
+       serv_puts("CONF getsys|application/x-citadel-ignet-config");
+       serv_getln(buf, sizeof buf);
        if (buf[0] == '1') {
-               printf("HTTP/1.0 200 OK\n");
-               output_headers(1);
-               server_to_text();
-               wprintf("<A HREF=\"/display_netconf\">Back to menu</A>\n");
-               wDumpContent(1);
-       } else {
-               display_error(&buf[4]);
+               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); 
+                       }
+               }
+               FreeStrBuf(&Buf);
+               return Hash;
        }
+       return NULL;
 }
 
 
-void unshare(void)
-{
-       char node[256];
-       char sroom[256];
-       char buf[256];
-
-       strcpy(node, bstr("node"));
-       strcpy(sroom, bstr("sroom"));
-       sprintf(buf, "NSET unshare|%s|%s", node, sroom);
-       serv_puts(buf);
-       serv_gets(buf);
-       if (buf[0] == '1') {
-               printf("HTTP/1.0 200 OK\n");
-               output_headers(1);
-               server_to_text();
-               wprintf("<A HREF=\"/display_netconf\">Back to menu</A>\n");
-               wDumpContent(1);
-       } else {
-               display_error(&buf[4]);
+
+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");
        }
 }
 
 
 
-void display_add_node(void)
-{
-
-       printf("HTTP/1.0 200 OK\n");
-       output_headers(1);
-       wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=007700><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
-       wprintf("<B>Add a new node</B>\n");
-       wprintf("</FONT></TD></TR></TABLE>\n");
+/*----------------------------------------------------------------------*/
+/*              WEB Handlers                                            */
+/*----------------------------------------------------------------------*/
 
-       wprintf("<CENTER>");
-       wprintf("<FORM METHOD=\"POST\" ACTION=\"/add_node\">\n");
 
-       wprintf("Enter name of new node: ");
-       wprintf("<INPUT TYPE=\"text\" NAME=\"node\" MAXLENGTH=\"64\"><BR>\n");
 
-       wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Add\">");
-       wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
+/*
+ * edit a network node
+ */
+void edit_node(void) {
+       HashList *NodeConfig;
+       const StrBuf *Index;
+       NodeConf *NewNode;
 
-       wprintf("</FORM></CENTER>\n");
-       wDumpContent(1);
+       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();
 }
 
 
+/*
+ * 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;
+       }
 
-void add_node(void)
-{
-       char node[256];
-       char buf[256];
-       char sc[256];
-
-       strcpy(node, bstr("node"));
-       strcpy(sc, bstr("sc"));
-
-       if (!strcmp(sc, "Add")) {
-               sprintf(buf, "NSET addnode|%s", node);
-               serv_puts(buf);
-               serv_gets(buf);
-               if (buf[0] == '1') {
-                       printf("HTTP/1.0 200 OK\n");
-                       output_headers(1);
-                       server_to_text();
-                       wprintf("<A HREF=\"/display_netconf\">Back to menu</A>\n");
-                       wDumpContent(1);
-               } else {
-                       display_error(&buf[4]);
-               }
+       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);
+       
 }
 
 
+/*
+ * display all configured nodes
+ */
+void display_netconf(void) {
+       wDumpContent(1);
+}
 
-void display_share(void)
-{
-       char buf[256];
-       char node[256];
-       char sroom[256];
-       struct sharelist *shlist = NULL;
-       struct sharelist *shptr;
-       int already_shared;
-
-       strcpy(node, bstr("node"));
-
-       printf("HTTP/1.0 200 OK\n");
-       output_headers(1);
-       wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=007700><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
-       wprintf("<B>Add a shared room</B>\n");
-       wprintf("</FONT></TD></TR></TABLE>\n");
-
-       wprintf("<CENTER>");
-       wprintf("<FORM METHOD=\"POST\" ACTION=\"/share\">\n");
-       wprintf("<INPUT TYPE=\"hidden\" NAME=\"node\" VALUE=\"");
-       urlescputs(node);
-       wprintf("\">\n");
-
-       sprintf(buf, "NSET roomlist|%s", node);
-       serv_puts(buf);
-       serv_gets(buf);
-       if (buf[0] == '1') {
-               while (serv_gets(buf), strcmp(buf, "000")) {
-                       shptr = (struct sharelist *)
-                           malloc(sizeof(struct sharelist));
-                       shptr->next = shlist;
-                       extract(shptr->shname, buf, 0);
-                       shlist = shptr;
-               }
+/*
+ * display the dialog to verify the deletion
+ */
+void display_confirm_delete_node(void) {
+       wDumpContent(1);
+}
+
+
+/*
+ * actually delete the node
+ */
+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;
        }
-       wprintf("<SELECT NAME=\"sroom\" SIZE=5 WIDTH=30>\n");
-       serv_puts("LKRA");
-       serv_gets(buf);
-       if (buf[0] == '1') {
-               while (serv_gets(buf), strcmp(buf, "000")) {
-                       extract(sroom, buf, 0);
-                       already_shared = 0;
-                       for (shptr = shlist; shptr != NULL; shptr = shptr->next) {
-                               if (!strcasecmp(sroom, shptr->shname))
-                                       already_shared = 1;
-                       }
 
-                       if (already_shared == 0) {
-                               wprintf("<OPTION>");
-                               escputs(sroom);
-                               wprintf("\n");
-                       }
-               }
+       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;
        }
-       wprintf("</SELECT>\n");
-       wprintf("<BR>\n");
+       Node = (NodeConf *) vNode;
+       Node->DeleteMe = 1;
+               save_net_conf(NodeConfig);
+       DeleteHash(&NodeConfig);
+       
+       url_do_template();
 
-       wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Share\">");
-       wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
+}
 
-       wprintf("</FORM></CENTER>\n");
-       wDumpContent(1);
 
-       /* free the list */
-       while (shlist != NULL) {
-               shptr = shlist->next;
-               free(shlist);
-               shlist = shptr;
-       }
+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 share(void)
+void 
+InitModule_NETCONF
+(void)
 {
-       char node[256];
-       char buf[256];
-       char sc[256];
-       char sroom[256];
-
-       strcpy(node, bstr("node"));
-       strcpy(sc, bstr("sc"));
-       strcpy(sroom, bstr("sroom"));
-
-       if (!strcmp(sc, "Share")) {
-               sprintf(buf, "NSET share|%s|%s", node, sroom);
-               serv_puts(buf);
-               serv_gets(buf);
-               if (buf[0] == '1') {
-                       printf("HTTP/1.0 200 OK\n");
-                       output_headers(1);
-                       server_to_text();
-                       wprintf("<A HREF=\"/display_netconf\">Back to menu</A>\n");
-                       wDumpContent(1);
-               } else {
-                       display_error(&buf[4]);
-               }
+       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);
 }