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