X-Git-Url: https://code.citadel.org/?p=citadel.git;a=blobdiff_plain;f=webcit%2Fnetconf.c;h=83c391429c95aa0d7a4b10fed5706fa6facda289;hp=0f5e017e1cdb783d9334f8f2323704620b732c8a;hb=HEAD;hpb=01cdfbf78a8ccad09cfd119e9cacf53f6f618313 diff --git a/webcit/netconf.c b/webcit/netconf.c index 0f5e017e1..6ff53fa56 100644 --- a/webcit/netconf.c +++ b/webcit/netconf.c @@ -1,285 +1,296 @@ -/* - * netconf.c - * - * Functions which handle network and sharing configuration. - * - * $Id$ - */ +// 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include "webcit.h" +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); +} +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 edit_node(void) { +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]; - char node[SIZ]; - char cnode[SIZ]; - FILE *fp; - - if (!strcmp(bstr("sc"), "OK")) { - strcpy(node, bstr("node") ); - fp = tmpfile(); - if (fp != NULL) { - serv_puts("CONF getsys|application/x-citadel-ignet-config"); - serv_gets(buf); - if (buf[0] == '1') { - while (serv_gets(buf), strcmp(buf, "000")) { - extract(cnode, buf, 0); - if (strcasecmp(node, cnode)) { - fprintf(fp, "%s\n", buf); - } - } - fprintf(fp, "%s|%s|%s|%s\n", - bstr("node"), - bstr("secret"), - bstr("host"), - bstr("port") ); - } - rewind(fp); - - serv_puts("CONF putsys|application/x-citadel-ignet-config"); - serv_gets(buf); - if (buf[0] == '4') { - while (fgets(buf, sizeof buf, fp) != NULL) { - buf[strlen(buf)-1] = 0; - serv_puts(buf); - } - serv_puts("000"); + 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); } - fclose(fp); } + FreeStrBuf(&Buf); + return Hash; } - - display_netconf(); + return NULL; } -void display_add_node(void) -{ - output_headers(3); - wprintf("
"); - wprintf("Add new node"); - wprintf("
\n"); - - wprintf("
\n"); - wprintf("
\n"); - wprintf(""); - wprintf("\n"); - wprintf(""); - wprintf("\n"); - wprintf(""); - wprintf("\n"); - wprintf(""); - wprintf("\n"); - wprintf("
Node name
Shared secret
Host or IP
Port

"); - wprintf(""); - wprintf(" "); - wprintf(""); - wprintf("
\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"); + } } -void display_edit_node(void) -{ - char buf[SIZ]; - char node[SIZ]; - char cnode[SIZ]; - char csecret[SIZ]; - char chost[SIZ]; - char cport[SIZ]; - strcpy(node, bstr("node")); - output_headers(3); - wprintf("
"); - wprintf("Edit node configuration for "); - escputs(node); - wprintf("\n"); - wprintf("
\n"); +/*----------------------------------------------------------------------*/ +/* WEB Handlers */ +/*----------------------------------------------------------------------*/ + - serv_puts("CONF getsys|application/x-citadel-ignet-config"); - serv_gets(buf); - if (buf[0] == '1') { - while (serv_gets(buf), strcmp(buf, "000")) { - extract(cnode, buf, 0); - extract(csecret, buf, 1); - extract(chost, buf, 2); - extract(cport, buf, 3); - - if (!strcasecmp(node, cnode)) { - wprintf("
\n"); - wprintf("
\n"); - wprintf(""); - wprintf("\n", cnode); - wprintf(""); - wprintf("\n", csecret); - wprintf(""); - wprintf("\n", chost); - wprintf(""); - wprintf("\n", cport); - wprintf("
Node name
Shared secret
Host or IP
Port

"); - wprintf(""); - wprintf(" "); - wprintf(""); - wprintf("
\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
\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); + } +/* + * display all configured nodes + */ +void display_netconf(void) { + wDumpContent(1); +} -void display_netconf(void) -{ - char buf[SIZ]; - char node[SIZ]; +/* + * display the dialog to verify the deletion + */ +void display_confirm_delete_node(void) { + wDumpContent(1); +} - output_headers(3); - wprintf("
"); - wprintf("Network configuration\n"); - wprintf("
\n"); - wprintf("
"); - wprintf(""); - wprintf("Add a new node
\n"); - wprintf("
"); +/* + * 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("
"); - wprintf("Currently configured nodes\n"); - wprintf("
\n"); - serv_puts("CONF getsys|application/x-citadel-ignet-config"); - serv_gets(buf); - if (buf[0] == '1') { - wprintf("
\n"); - while (serv_gets(buf), strcmp(buf, "000")) { - extract(node, buf, 0); - wprintf(""); - wprintf(""); - wprintf(""); - wprintf("\n"); - } - wprintf("
"); - escputs(node); - wprintf("(Edit)(Delete)
\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; } - wDumpContent(1); + Node = (NodeConf *) vNode; + Node->DeleteMe = 1; + save_net_conf(NodeConfig); + DeleteHash(&NodeConfig); + + url_do_template(); + } -void display_confirm_delete_node(void) -{ - char node[SIZ]; - - output_headers(3); - wprintf("
"); - wprintf("Confirm delete\n"); - wprintf("
\n"); - - strcpy(node, bstr("node")); - wprintf("
Are you sure you want to delete "); - escputs(node); - wprintf("?
\n"); - wprintf("Yes   "); - wprintf("No
\n"); - wDumpContent(1); +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 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_gets(buf); - if (buf[0] == '1') { - while (serv_gets(buf), strcmp(buf, "000")) { - extract(cnode, buf, 0); - if (strcasecmp(node, cnode)) { - fprintf(fp, "%s\n", buf); - } - } - } - rewind(fp); - - serv_puts("CONF putsys|application/x-citadel-ignet-config"); - serv_gets(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); - } - - display_netconf(); +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 add_node(void) +void +InitModule_NETCONF +(void) { - char node[SIZ]; - char buf[SIZ]; - char sc[SIZ]; - - 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') { - output_headers(3); - server_to_text(); - wprintf("Back to menu\n"); - wDumpContent(1); - } else { - strcpy(WC->ImportantMessage, &buf[4]); - display_netconf(); - } - } -} + 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); +}