summaryrefslogblamecommitdiffstats
path: root/cpukit/httpd/umui.c
blob: 971b059bc79fc2e30dc358480c75ce47b6e58551 (plain) (tree)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
































































































































































































































































































































































































































































































































































































































































                                                                                                                 
/*
 * umui.c -- User Management GoForm Processing
 *
 * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved.
 *
 * See the file "license.txt" for usage and redistribution license requirements
 *
 *	$Id$
 */

/******************************** Description *********************************/

/*
 *	This module provides GoForm functions for User management
 */

/********************************* Includes ***********************************/

#include	"wsIntrn.h"
#include	"um.h"

/********************************* Defines ************************************/

#define		NONE_OPTION		T("<NONE>")
#define		MSG_START		T("<body><h2>")
#define		MSG_END			T("</h2></body>")

/**************************** Forward Declarations ****************************/

static void		formAddUser(webs_t wp, char_t *path, char_t *query);
static void		formDeleteUser(webs_t wp, char_t *path, char_t *query);
static void		formDisplayUser(webs_t wp, char_t *path, char_t *query);
static int		aspGenerateUserList(int eid, webs_t wp, 
									int argc, char_t **argv);

static void		formAddGroup(webs_t wp, char_t *path, char_t *query);
static void		formDeleteGroup(webs_t wp, char_t *path, char_t *query);
static int		aspGenerateGroupList(int eid, webs_t wp, 
									 int argc, char_t **argv);

static void		formAddAccessLimit(webs_t wp, char_t *path, char_t *query);
static void		formDeleteAccessLimit(webs_t wp, char_t *path, char_t *query);
static int		aspGenerateAccessLimitList(int eid, webs_t wp, 
										   int argc, char_t **argv);

static int		aspGenerateAccessMethodList(int eid, webs_t wp, 
											int argc, char_t **argv);
static int		aspGeneratePrivilegeList(int eid, webs_t wp, 
										 int argc, char_t **argv);

static void		formSaveUserManagement(webs_t wp, char_t *path, char_t *query);
static void		formLoadUserManagement(webs_t wp, char_t *path, char_t *query);

static void		websMsgStart(webs_t wp);
static void		websMsgEnd(webs_t wp);

/*********************************** Code *************************************/
/*
 *	Set up the User Management form handlers
 */

void formDefineUserMgmt(void)
{
	websAspDefine(T("MakeGroupList"), aspGenerateGroupList);
	websAspDefine(T("MakeUserList"), aspGenerateUserList);
	websAspDefine(T("MakeAccessLimitList"), aspGenerateAccessLimitList);
	websAspDefine(T("MakeAccessMethodList"), aspGenerateAccessMethodList);
	websAspDefine(T("MakePrivilegeList"), aspGeneratePrivilegeList);

	websFormDefine(T("AddUser"), formAddUser);
	websFormDefine(T("DeleteUser"), formDeleteUser);
	websFormDefine(T("DisplayUser"), formDisplayUser);
	websFormDefine(T("AddGroup"), formAddGroup);
	websFormDefine(T("DeleteGroup"), formDeleteGroup);
	websFormDefine(T("AddAccessLimit"), formAddAccessLimit);
	websFormDefine(T("DeleteAccessLimit"), formDeleteAccessLimit);

	websFormDefine(T("SaveUserManagement"), formSaveUserManagement);
	websFormDefine(T("LoadUserManagement"), formLoadUserManagement);
}

/******************************************************************************/
/*
 *  Add a user
 */

static void formAddUser(webs_t wp, char_t *path, char_t *query)
{
	char_t	*userid, *pass1, *pass2, *group, *enabled, *ok;
	bool_t bDisable;
	int	nCheck;

	a_assert(wp);

	userid = websGetVar(wp, T("user"), T("")); 
	pass1 = websGetVar(wp, T("password"), T("")); 
	pass2 = websGetVar(wp, T("passconf"), T("")); 
	group = websGetVar(wp, T("group"), T("")); 
	enabled = websGetVar(wp, T("enabled"), T("")); 
	ok = websGetVar(wp, T("ok"), T("")); 

	websHeader(wp);
	websMsgStart(wp);

	if (gstricmp(ok, T("ok")) != 0) {
		websWrite(wp, T("Add User Cancelled"));
	} else if (gstrcmp(pass1, pass2) != 0) {
		websWrite(wp, T("Confirmation Password did not match."));
	} else {
		if (enabled && *enabled && (gstrcmp(enabled, T("on")) == 0)) {
			bDisable = FALSE;
		} else {
			bDisable = TRUE;
		}

		nCheck = umAddUser(userid, pass1, group, 0, bDisable);
		if (nCheck != 0) {
			char_t * strError;

			switch (nCheck) {
			case UM_ERR_DUPLICATE:
				strError = T("User already exists.");
				break;

			case UM_ERR_BAD_NAME:
				strError = T("Invalid user name.");
				break;

			case UM_ERR_BAD_PASSWORD:
				strError = T("Invalid password.");
				break;

			case UM_ERR_NOT_FOUND:
				strError = T("Invalid or unselected group.");
				break;

			default:
				strError = T("Error writing user record.");
				break;
			}

			websWrite(wp, T("Unable to add user, \"%s\".  %s"),
				userid, strError);
		} else {
			websWrite(wp, T("User, \"%s\" was successfully added."),
				userid);
		}
	}

	websMsgEnd(wp);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *  Delete a user
 */

static void formDeleteUser(webs_t wp, char_t *path, char_t *query)
{
	char_t	*userid, *ok;

	a_assert(wp);

	userid = websGetVar(wp, T("user"), T("")); 
	ok = websGetVar(wp, T("ok"), T("")); 

	websHeader(wp);
	websMsgStart(wp);

	if (gstricmp(ok, T("ok")) != 0) {
		websWrite(wp, T("Delete User Cancelled"));
	} else if (umUserExists(userid) == FALSE) {
		websWrite(wp, T("ERROR: User \"%s\" not found"), userid);
	} else if (umGetUserProtected(userid)) {
		websWrite(wp, T("ERROR: User, \"%s\" is delete-protected."), userid);
	} else if (umDeleteUser(userid) != 0) {
		websWrite(wp, T("ERROR: Unable to delete user, \"%s\" "), userid);
	} else {
		websWrite(wp, T("User, \"%s\" was successfully deleted."), userid);
	}

	websMsgEnd(wp);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *  Display the user info
 */

static void formDisplayUser(webs_t wp, char_t *path, char_t *query)
{
	char_t	*userid, *ok, *temp;
	bool_t	enabled;

	a_assert(wp);

	userid = websGetVar(wp, T("user"), T("")); 
	ok = websGetVar(wp, T("ok"), T("")); 

	websHeader(wp);
	websWrite(wp, T("<body>"));

	if (gstricmp(ok, T("ok")) != 0) {
		websWrite(wp, T("Display User Cancelled"));
	} else if (umUserExists(userid) == FALSE) {
		websWrite(wp, T("ERROR: User <b>%s</b> not found.\n"), userid);
	} else {
		websWrite(wp, T("<h2>User ID: <b>%s</b></h2>\n"), userid);
		temp = umGetUserGroup(userid);
		websWrite(wp, T("<h3>User Group: <b>%s</b></h3>\n"), temp);
		enabled = umGetUserEnabled(userid);
		websWrite(wp, T("<h3>Enabled: <b>%d</b></h3>\n"), enabled);
	}

	websWrite(wp, T("</body>\n"));
	websFooter(wp);
	websDone(wp, 200);
}


/******************************************************************************/
/*
 *  Generate HTML to create a list box containing the users
 */

static int aspGenerateUserList(int eid, webs_t wp, int argc, char_t **argv)
{
	char_t	*userid;
	int		row, nBytesSent, nBytes;

	a_assert(wp);

	nBytes = websWrite(wp, 
		T("<SELECT NAME=\"user\" SIZE=\"3\" TITLE=\"Select a User\">"));
	row = 0;
	userid = umGetFirstUser();
	nBytesSent = 0;

	while (userid && (nBytes > 0)) {
		nBytes = websWrite(wp, T("<OPTION VALUE=\"%s\">%s\n"), 
			userid, userid);
		userid = umGetNextUser(userid);
		nBytesSent += nBytes;
	}

	nBytesSent += websWrite(wp, T("</SELECT>"));

	return nBytesSent;
}

/******************************************************************************/
/*
 *  Add a group
 */

static void formAddGroup(webs_t wp, char_t *path, char_t *query)
{
	char_t			*group, *enabled, *privilege, *method, *ok, *pChar;
	int				nCheck;
	short			priv;
	accessMeth_t	am;
	bool_t			bDisable;

	a_assert(wp);

	group = websGetVar(wp, T("group"), T("")); 
	method = websGetVar(wp, T("method"), T("")); 
	enabled = websGetVar(wp, T("enabled"), T("")); 
	privilege = websGetVar(wp, T("privilege"), T("")); 
	ok = websGetVar(wp, T("ok"), T("")); 

	websHeader(wp);
	websMsgStart(wp);

	if (gstricmp(ok, T("ok")) != 0) {
		websWrite(wp, T("Add Group Cancelled."));
	} else if ((group == NULL) || (*group == 0)) {
		websWrite(wp, T("No Group Name was entered."));
	} else if (umGroupExists(group)) {
		websWrite(wp, T("ERROR: Group, \"%s\" already exists."), group);
	} else {
		if (privilege && *privilege) {
/*
 *			privilege is a mulitple <SELECT> var, and must be parsed.
 *			Values for these variables are space delimited.
 */
			priv = 0;
			for (pChar = privilege; *pChar; pChar++) {
				if (*pChar == ' ') {
					*pChar = '\0';
					priv |= gatoi(privilege);
					*pChar = ' ';
					privilege = pChar + 1;
				}
			}
			priv |= gatoi(privilege);
		} else {
			priv = 0;
		}

		if (method && *method) {
			am = (accessMeth_t) gatoi(method);
		} else {
			am = AM_FULL;
		}

		if (enabled && *enabled && (gstrcmp(enabled, T("on")) == 0)) {
			bDisable = FALSE;
		} else {
			bDisable = TRUE;
		}

		nCheck = umAddGroup(group, priv, am, 0, bDisable);
		if (nCheck != 0) {
			websWrite(wp, T("Unable to add group, \"%s\", code: %d "),
				group, nCheck);
		} else {
			websWrite(wp, T("Group, \"%s\" was successfully added."), 
				group);
		}
	}

	websMsgEnd(wp);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *  Delete a group
 */

static void formDeleteGroup(webs_t wp, char_t *path, char_t *query)
{
	char_t	*group, *ok;

	a_assert(wp);

	group = websGetVar(wp, T("group"), T("")); 
	ok = websGetVar(wp, T("ok"), T("")); 

	websHeader(wp);
	websMsgStart(wp);

	if (gstricmp(ok, T("ok")) != 0) {
		websWrite(wp, T("Delete Group Cancelled."));
	} else if ((group == NULL) || (*group == '\0')) {
		websWrite(wp, T("ERROR: No group was selected."));
	} else if (umGetGroupProtected(group)) {
		websWrite(wp, T("ERROR: Group, \"%s\" is delete-protected."), group);
	} else if (umGetGroupInUse(group)) {
		websWrite(wp, T("ERROR: Group, \"%s\" is being used."),	group);
	} else if (umDeleteGroup(group) != 0) {
		websWrite(wp, T("ERROR: Unable to delete group, \"%s\" "), group);
	} else {
		websWrite(wp, T("Group, \"%s\" was successfully deleted."), group);
	}

	websMsgEnd(wp);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *  Generate HTML to create a list box containing the groups
 */

static int aspGenerateGroupList(int eid, webs_t wp, int argc, char_t **argv)
{
	char_t	*group;
	int		row, nBytesSent, nBytes;

	a_assert(wp);

	row = 0;
	nBytesSent = 0;
	nBytes = websWrite(wp, 
		T("<SELECT NAME=\"group\" SIZE=\"3\" TITLE=\"Select a Group\">"));
/*
 *  Add a special "<NONE>" element to allow de-selection
 */
	nBytes = websWrite(wp, T("<OPTION VALUE=\"\">[NONE]\n"));

	group = umGetFirstGroup();
	while (group && (nBytes > 0)) {
		nBytes = websWrite(wp, T("<OPTION VALUE=\"%s\">%s\n"), group, group);
		group = umGetNextGroup(group);
		nBytesSent += nBytes;
	}

	nBytesSent += websWrite(wp, T("</SELECT>"));

	return nBytesSent;
}

/******************************************************************************/
/*
 *  Add an access limit
 */

static void formAddAccessLimit(webs_t wp, char_t *path, char_t *query)
{
	char_t			*url, *method, *group, *secure, *ok;
	int				nCheck;
	accessMeth_t	am;
	short			nSecure;

	a_assert(wp);

	url = websGetVar(wp, T("url"), T("")); 
	group = websGetVar(wp, T("group"), T("")); 
	method = websGetVar(wp, T("method"), T("")); 
	secure = websGetVar(wp, T("secure"), T("")); 
	ok = websGetVar(wp, T("ok"), T("")); 

	websHeader(wp);
	websMsgStart(wp);

	if (gstricmp(ok, T("ok")) != 0) {
		websWrite(wp, T("Add Access Limit Cancelled."));
	} else if ((url == NULL) || (*url == 0)) {
		websWrite(wp, T("ERROR:  No URL was entered."));
	} else if (umAccessLimitExists(url)) {
		websWrite(wp, T("ERROR:  An Access Limit for [%s] already exists."),
			url);
	} else {
		if (method && *method) {
			am = (accessMeth_t) gatoi(method);
		} else {
			am = AM_FULL;
		}

		if (secure && *secure) {
			nSecure = (short) gatoi(secure);
		} else {
			nSecure = 0;
		}

		nCheck = umAddAccessLimit(url, am, nSecure, group);
		if (nCheck != 0) {
			websWrite(wp, T("Unable to add Access Limit for [%s]"),	url);
		} else {
			websWrite(wp, T("Access limit for [%s], was successfully added."),
				url);
		}
	}

	websMsgEnd(wp);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *  Delete an Access Limit
 */

static void formDeleteAccessLimit(webs_t wp, char_t *path, char_t *query)
{
	char_t	*url, *ok;

	a_assert(wp);

	url = websGetVar(wp, T("url"), T("")); 
	ok = websGetVar(wp, T("ok"), T("")); 

	websHeader(wp);
	websMsgStart(wp);

	if (gstricmp(ok, T("ok")) != 0) {
		websWrite(wp, T("Delete Access Limit Cancelled"));
	} else if (umDeleteAccessLimit(url) != 0) {
		websWrite(wp, T("ERROR: Unable to delete Access Limit for [%s]"), 
			url);
	} else {
		websWrite(wp, T("Access Limit for [%s], was successfully deleted."), 
			url);
	}

	websMsgEnd(wp);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *  Generate HTML to create a list box containing the access limits
 */

static int aspGenerateAccessLimitList(int eid, webs_t wp, 
									  int argc, char_t **argv)
{
	char_t	*url;
	int		row, nBytesSent, nBytes;

	a_assert(wp);

	row = nBytesSent = 0;
	url = umGetFirstAccessLimit();
	nBytes = websWrite(wp, 
		T("<SELECT NAME=\"url\" SIZE=\"3\" TITLE=\"Select a URL\">"));

	while (url && (nBytes > 0)) {
		nBytes = websWrite(wp, T("<OPTION VALUE=\"%s\">%s\n"), url, url);
		url = umGetNextAccessLimit(url);
		nBytesSent += nBytes;
	}

	nBytesSent += websWrite(wp, T("</SELECT>"));

	return nBytesSent;
}

/******************************************************************************/
/*
 *  Generate HTML to create a list box containing the access methods
 */

static int aspGenerateAccessMethodList(int eid, webs_t wp, 
									   int argc, char_t **argv)
{
	int		nBytes;

	a_assert(wp);

	nBytes = websWrite(wp, 
		T("<SELECT NAME=\"method\" SIZE=\"3\" TITLE=\"Select a Method\">"));
	nBytes += websWrite(wp, T("<OPTION VALUE=\"%d\">FULL ACCESS\n"), 
		AM_FULL);
	nBytes += websWrite(wp, T("<OPTION VALUE=\"%d\">BASIC ACCESS\n"), 
		AM_BASIC);
	nBytes += websWrite(wp, T("<OPTION VALUE=\"%d\" SELECTED>DIGEST ACCESS\n"), 
		AM_DIGEST);
	nBytes += websWrite(wp, T("<OPTION VALUE=\"%d\">NO ACCESS\n"), 
		AM_NONE);
	nBytes += websWrite(wp, T("</SELECT>"));

	return nBytes;
}
/******************************************************************************/
/*
 *  Generate HTML to create a list box containing privileges
 */

static int aspGeneratePrivilegeList(int eid, webs_t wp, 
									int argc, char_t **argv)
{
	int		nBytes;

	a_assert(wp);

	nBytes = websWrite(wp, T("<SELECT NAME=\"privilege\" SIZE=\"3\" "));
	nBytes += websWrite(wp, T("MULTIPLE TITLE=\"Choose Privileges\">"));
	nBytes += websWrite(wp, T("<OPTION VALUE=\"%d\">READ\n"), PRIV_READ);
	nBytes += websWrite(wp, T("<OPTION VALUE=\"%d\">EXECUTE\n"), PRIV_WRITE);
	nBytes += websWrite(wp, T("<OPTION VALUE=\"%d\">ADMINISTRATE\n"), 
		PRIV_ADMIN);
	nBytes += websWrite(wp, T("</SELECT>"));

	return nBytes;
}

/******************************************************************************/
/*
 *  Save the user management configuration to a file
 */

static void formSaveUserManagement(webs_t wp, char_t *path, char_t *query)
{
	char_t	*ok;

	a_assert(wp);

	ok = websGetVar(wp, T("ok"), T("")); 

	websHeader(wp);
	websMsgStart(wp);

	if (gstricmp(ok, T("ok")) != 0) {
		websWrite(wp, T("Save Cancelled."));
	} else if (umCommit(NULL) != 0) {
		websWrite(wp, T("ERROR: Unable to save user configuration."));
	} else {
		websWrite(wp, T("User configuration was saved successfully."));
	}

	websMsgEnd(wp);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *  Load the user management configuration from a file
 */

static void formLoadUserManagement(webs_t wp, char_t *path, char_t *query)
{
	char_t	*ok;

	a_assert(wp);

	ok = websGetVar(wp, T("ok"), T("")); 

	websHeader(wp);
	websMsgStart(wp);

	if (gstricmp(ok, T("ok")) != 0) {
		websWrite(wp, T("Load Cancelled."));
	} else if (umRestore(NULL) != 0) {
		websWrite(wp, T("ERROR: Unable to load user configuration."));
	} else {
		websWrite(wp, T("User configuration was re-loaded successfully."));
	}

	websMsgEnd(wp);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *  Message start and end convenience functions
 */

static void	websMsgStart(webs_t wp)
{
	websWrite(wp, MSG_START);
}

static void	websMsgEnd(webs_t wp)
{
	websWrite(wp, MSG_END);
}

/******************************************************************************/