* Begin migration to the new "rounded boxes" look
[citadel.git] / webcit / useredit.c
1 /*
2  * Administrative screen to add/change/delete user accounts
3  *
4  */
5
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include "webcit.h"
27 #include "webserver.h"
28
29
30
31
32
33 void select_user_to_edit(char *message, char *preselect)
34 {
35         char buf[SIZ];
36         char username[SIZ];
37
38         output_headers(3);      /* No room banner on this screen */
39
40         if (message != NULL) wprintf(message);
41
42         wprintf("<TABLE border=0 CELLSPACING=10><TR VALIGN=TOP><TD>\n");
43
44         svprintf("BOXTITLE", WCS_STRING, "Edit or Delete users");
45         do_template("beginbox");
46
47         wprintf("To edit an existing user account, select the user "
48                 "name from the list and click 'Edit'.<BR><BR>");
49         
50         wprintf("<CENTER>"
51                 "<FORM METHOD=\"POST\" ACTION=\"/display_edituser\">\n");
52         wprintf("<SELECT NAME=\"username\" SIZE=10>\n");
53         serv_puts("LIST");
54         serv_gets(buf);
55         if (buf[0] == '1') {
56                 while (serv_gets(buf), strcmp(buf, "000")) {
57                         extract(username, buf, 0);
58                         wprintf("<OPTION");
59                         if (preselect != NULL)
60                            if (!strcasecmp(username, preselect))
61                               wprintf(" SELECTED");
62                         wprintf(">");
63                         escputs(username);
64                         wprintf("\n");
65                 }
66         }
67         wprintf("</SELECT><BR>\n");
68
69         wprintf("<input type=submit name=sc value=\"Edit configuration\">");
70         wprintf("<input type=submit name=sc value=\"Edit address book entry\">");
71         wprintf("</FORM></CENTER>\n");
72         do_template("endbox");
73
74         wprintf("</TD><TD>");
75
76         svprintf("BOXTITLE", WCS_STRING, "Add users");
77         do_template("beginbox");
78
79         wprintf("To create a new user account, enter the desired "
80                 "user name in the box below and click 'Create'.<BR><BR>");
81
82         wprintf("<CENTER><FORM METHOD=\"POST\" ACTION=\"/create_user\">\n");
83         wprintf("New user: ");
84         wprintf("<input type=text name=username><BR>\n"
85                 "<input type=submit value=\"Create\">"
86                 "</FORM></CENTER>\n");
87
88         do_template("endbox");
89         wprintf("</TD></TR></TABLE>\n");
90
91         wDumpContent(1);
92 }
93
94
95
96 /* 
97  * Locate the message number of a user's vCard in the current room
98  */
99 long locate_user_vcard(char *username, long usernum) {
100         char buf[SIZ];
101         long vcard_msgnum = (-1L);
102         char content_type[SIZ];
103         char partnum[SIZ];
104         int already_tried_creating_one = 0;
105
106         struct stuff_t {
107                 struct stuff_t *next;
108                 long msgnum;
109         };
110
111         struct stuff_t *stuff = NULL;
112         struct stuff_t *ptr;
113
114 TRYAGAIN:
115         /* Search for the user's vCard */
116         serv_puts("MSGS ALL");
117         serv_gets(buf);
118         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
119                 ptr = malloc(sizeof(struct stuff_t));
120                 ptr->msgnum = atol(buf);
121                 ptr->next = stuff;
122                 stuff = ptr;
123         }
124
125         /* Iterate through the message list looking for vCards */
126         while (stuff != NULL) {
127                 serv_printf("MSG0 %ld|2", stuff->msgnum);
128                 serv_gets(buf);
129                 if (buf[0]=='1') {
130                         while(serv_gets(buf), strcmp(buf, "000")) {
131                                 if (!strncasecmp(buf, "part=", 5)) {
132                                         extract(partnum, &buf[5], 2);
133                                         extract(content_type, &buf[5], 4);
134                                         if (!strcasecmp(content_type,
135                                            "text/x-vcard")) {
136                                                 vcard_msgnum = stuff->msgnum;
137                                         }
138                                 }
139                         }
140                 }
141
142                 ptr = stuff->next;
143                 free(stuff);
144                 stuff = ptr;
145         }
146
147         /* If there's no vcard, create one */
148         if (vcard_msgnum < 0) if (already_tried_creating_one == 0) {
149                 already_tried_creating_one = 1;
150                 serv_puts("ENT0 1|||4");
151                 serv_gets(buf);
152                 if (buf[0] == '4') {
153                         serv_puts("Content-type: text/x-vcard");
154                         serv_puts("");
155                         serv_puts("begin:vcard");
156                         serv_puts("end:vcard");
157                         serv_puts("000");
158                 }
159                 goto TRYAGAIN;
160         }
161
162         return(vcard_msgnum);
163 }
164
165
166 /* 
167  * Display the form for editing a user's address book entry
168  */
169 void display_edit_address_book_entry(char *username, long usernum) {
170         char roomname[SIZ];
171         char buf[SIZ];
172         char error_message[SIZ];
173         long vcard_msgnum = (-1L);
174
175         /* Locate the user's config room, creating it if necessary */
176         sprintf(roomname, "%010ld.%s", usernum, USERCONFIGROOM);
177         serv_printf("GOTO %s||1", roomname);
178         serv_gets(buf);
179         if (buf[0] != '2') {
180                 serv_printf("CRE8 1|%s|5|||1|", roomname);
181                 serv_gets(buf);
182                 serv_printf("GOTO %s||1", roomname);
183                 serv_gets(buf);
184                 if (buf[0] != '2') {
185                         sprintf(error_message,
186                                 "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
187                                 "%s<BR><BR>\n", &buf[4]);
188                         select_user_to_edit(error_message, username);
189                         return;
190                 }
191         }
192
193         vcard_msgnum = locate_user_vcard(username, usernum);
194
195         if (vcard_msgnum < 0) {
196                 sprintf(error_message,
197                         "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
198                         "Could not create/edit vCard"
199                         "<BR><BR>\n"
200                 );
201                 select_user_to_edit(error_message, username);
202                 return;
203         }
204
205         do_edit_vcard(vcard_msgnum, "1", "/select_user_to_edit");
206 }
207
208
209
210
211 /*
212  * Edit a user.  If supplied_username is null, look in the "username"
213  * web variable for the name of the user to edit.
214  */
215 void display_edituser(char *supplied_username) {
216         char buf[SIZ];
217         char error_message[SIZ];
218         time_t now;
219
220         char username[SIZ];
221         char password[SIZ];
222         unsigned int flags;
223         int timescalled;
224         int msgsposted;
225         int axlevel;
226         long usernum;
227         time_t lastcall;
228         int purgedays;
229         int i;
230
231         if (supplied_username != NULL) {
232                 strcpy(username, supplied_username);
233         }
234         else {
235                 strcpy(username, bstr("username") );
236         }
237
238         serv_printf("AGUP %s", username);
239         serv_gets(buf);
240         if (buf[0] != '2') {
241                 sprintf(error_message,
242                         "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
243                         "%s<BR><BR>\n", &buf[4]);
244                 select_user_to_edit(error_message, username);
245                 return;
246         }
247
248         extract(username, &buf[4], 0);
249         extract(password, &buf[4], 1);
250         flags = extract_int(&buf[4], 2);
251         timescalled = extract_int(&buf[4], 3);
252         msgsposted = extract_int(&buf[4], 4);
253         axlevel = extract_int(&buf[4], 5);
254         usernum = extract_long(&buf[4], 6);
255         lastcall = extract_long(&buf[4], 7);
256         purgedays = extract_long(&buf[4], 8);
257
258         if (!strcmp(bstr("sc"), "Edit address book entry")) {
259                 display_edit_address_book_entry(username, usernum);
260                 return;
261         }
262
263         output_headers(3);      /* No room banner on this screen */
264         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#007700\"><TR><TD>");
265         wprintf("<SPAN CLASS=\"titlebar\">"
266                 "Edit user account: ");
267         escputs(username);
268         wprintf("</SPAN></TD></TR></TABLE>\n");
269
270         wprintf("<FORM METHOD=\"POST\" ACTION=\"/edituser\">\n"
271                 "<INPUT TYPE=\"hidden\" NAME=\"username\" VALUE=\"");
272         escputs(username);
273         wprintf("\">\n");
274
275         wprintf("<INPUT TYPE=\"hidden\" NAME=\"flags\" VALUE=\"%d\">\n", flags);
276
277         wprintf("<CENTER><TABLE>");
278
279         wprintf("<TR><TD>Password</TD><TD>"
280                 "<INPUT TYPE=\"password\" NAME=\"password\" VALUE=\"");
281         escputs(password);
282         wprintf("\" MAXLENGTH=\"20\"></TD></TR>\n");
283
284         wprintf("<TR><TD>Times logged in</TD><TD>"
285                 "<INPUT TYPE=\"text\" NAME=\"timescalled\" VALUE=\"");
286         wprintf("%d", timescalled);
287         wprintf("\" MAXLENGTH=\"6\"></TD></TR>\n");
288
289         wprintf("<TR><TD>Messages posted</TD><TD>"
290                 "<INPUT TYPE=\"text\" NAME=\"msgsposted\" VALUE=\"");
291         wprintf("%d", msgsposted);
292         wprintf("\" MAXLENGTH=\"6\"></TD></TR>\n");
293
294         wprintf("<TR><TD>Access level</TD><TD>"
295                 "<SELECT NAME=\"axlevel\">\n");
296         for (i=0; i<7; ++i) {
297                 wprintf("<OPTION ");
298                 if (axlevel == i) {
299                         wprintf("SELECTED ");
300                 }
301                 wprintf("VALUE=\"%d\">%d - %s</OPTION>\n",
302                         i, i, axdefs[i]);
303         }
304         wprintf("</SELECT></TD></TR>\n");
305
306         wprintf("<TR><TD>User ID number</TD><TD>"
307                 "<INPUT TYPE=\"text\" NAME=\"usernum\" VALUE=\"");
308         wprintf("%ld", usernum);
309         wprintf("\" MAXLENGTH=\"7\"></TD></TR>\n");
310
311         now = time(NULL);
312         wprintf("<TR><TD>Date/time of last login</TD><TD>"
313                 "<SELECT NAME=\"lastcall\">\n");
314
315         wprintf("<OPTION SELECTED VALUE=\"%ld\">", lastcall);
316         escputs(asctime(localtime(&lastcall)));
317         wprintf("</OPTION>\n");
318
319         wprintf("<OPTION VALUE=\"%ld\">", now);
320         escputs(asctime(localtime(&now)));
321         wprintf("</OPTION>\n");
322
323         wprintf("</SELECT></TD></TR>");
324
325         wprintf("<TR><TD>Auto-purge after days</TD><TD>"
326                 "<INPUT TYPE=\"text\" NAME=\"purgedays\" VALUE=\"");
327         wprintf("%d", purgedays);
328         wprintf("\" MAXLENGTH=\"5\"></TD></TR>\n");
329
330         wprintf("</TABLE>\n");
331
332         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"OK\">\n"
333                 "<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Cancel\">\n"
334                 "<BR><BR></FORM>\n");
335
336         wprintf("</CENTER>\n");
337
338         wDumpContent(1);
339
340 }
341
342
343
344 void edituser(void) {
345         char message[SIZ];
346         char buf[SIZ];
347
348         if (strcasecmp(bstr("action"), "OK")) {
349                 strcpy(message, "Edit user cancelled.");
350         }
351
352         else {
353
354                 serv_printf("ASUP %s|%s|%s|%s|%s|%s|%s|%s|%s|",
355                         bstr("username"),
356                         bstr("password"),
357                         bstr("flags"),
358                         bstr("timescalled"),
359                         bstr("msgsposted"),
360                         bstr("axlevel"),
361                         bstr("usernum"),
362                         bstr("lastcall"),
363                         bstr("purgedays")
364                 );
365                 serv_gets(buf);
366                 if (buf[0] != '2') {
367                         sprintf(message,
368                                 "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
369                                 "%s<BR><BR>\n", &buf[4]);
370                 }
371                 else {
372                         strcpy(message, "");
373                 }
374         }
375
376         select_user_to_edit(message, bstr("username"));
377 }
378
379
380
381
382 void create_user(void) {
383         char buf[SIZ];
384         char error_message[SIZ];
385         char username[SIZ];
386
387         strcpy(username, bstr("username"));
388
389         serv_printf("CREU %s", username);
390         serv_gets(buf);
391
392         if (buf[0] == '2') {
393                 sprintf(error_message, "<b>User has been created.</b>");
394                 select_user_to_edit(error_message, username);
395         }
396         else {
397                 sprintf(error_message,
398                         "<IMG SRC=\"static/error.gif\" ALIGN=CENTER>"
399                         "%s<BR><BR>\n", &buf[4]);
400                 select_user_to_edit(error_message, NULL);
401         }
402
403 }
404