File: netconf.c

package info (click to toggle)
webcit 902-dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,888 kB
  • ctags: 3,854
  • sloc: ansic: 34,145; sh: 4,455; makefile: 352; xml: 91; sed: 9
file content (306 lines) | stat: -rw-r--r-- 7,286 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#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;
}

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); 
			}
		}
		FreeStrBuf(&Buf);
		return Hash;
	}
	return NULL;
}



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");
	}
}



/*----------------------------------------------------------------------*/
/*              WEB Handlers                                            */
/*----------------------------------------------------------------------*/



/*
 * 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();
}


/*
 * 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;
	}

	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);
}

/*
 * 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;
	}

	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);
}