* added gcc printf format checking to wprintf
[citadel.git] / webcit / useredit.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup AdminTasks Administrative screen to add/change/delete user accounts
6  * \ingroup CitadelConfig
7  *
8  */
9 /*@{*/
10
11 #include "webcit.h"
12 #include "webserver.h"
13
14
15 /**
16  * \brief show a list of available users to edit them
17  * \param message the header message???
18  * \param preselect which user should be selected in the browser
19  */
20 void select_user_to_edit(char *message, char *preselect)
21 {
22         char buf[SIZ];
23         char username[SIZ];
24
25         output_headers(1, 1, 2, 0, 0, 0);
26         wprintf("<div id=\"banner\">\n");
27         wprintf("<img src=\"static/usermanag_48x.gif\">");
28         wprintf("<h1>");
29         wprintf(_("Edit or delete users"));
30         wprintf("</h1>");
31         wprintf("</div>");
32
33         wprintf("<div id=\"content\" class=\"service\">\n");
34
35         if (message != NULL) wprintf(message);
36
37         wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
38
39         svput("BOXTITLE", WCS_STRING, _("Add users"));
40         do_template("beginbox");
41
42         wprintf(_("To create a new user account, enter the desired "
43                 "user name in the box below and click 'Create'."));
44         wprintf("<br /><br />");
45
46         wprintf("<center><form method=\"POST\" action=\"create_user\">\n");
47         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
48         wprintf(_("New user: "));
49         wprintf("<input type=\"text\" name=\"username\"><br />\n"
50                 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
51                 "</form></center>\n", _("Create"));
52
53         do_template("endbox");
54
55         wprintf("</td><td>");
56
57         svput("BOXTITLE", WCS_STRING, _("Edit or Delete users"));
58         do_template("beginbox");
59
60         wprintf(_("To edit an existing user account, select the user "
61                 "name from the list and click 'Edit'."));
62         wprintf("<br /><br />");
63         
64         wprintf("<center>"
65                 "<form method=\"POST\" action=\"display_edituser\">\n");
66         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
67         wprintf("<select name=\"username\" size=10 style=\"width:100%%\">\n");
68         serv_puts("LIST");
69         serv_getln(buf, sizeof buf);
70         if (buf[0] == '1') {
71                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
72                         extract_token(username, buf, 0, '|', sizeof username);
73                         wprintf("<option");
74                         if (preselect != NULL)
75                            if (!strcasecmp(username, preselect))
76                               wprintf(" selected");
77                         wprintf(">");
78                         escputs(username);
79                         wprintf("\n");
80                 }
81         }
82         wprintf("</select><br />\n");
83
84         wprintf("<input type=\"submit\" name=\"edit_config_button\" value=\"%s\">", _("Edit configuration"));
85         wprintf("<input type=\"submit\" name=\"edit_abe_button\" value=\"%s\">", _("Edit address book entry"));
86         wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
87                 "onClick=\"return confirm('%s');\">", _("Delete user"), _("Delete this user?"));
88         wprintf("</form></center>\n");
89         do_template("endbox");
90
91         wprintf("</td></tr></table>\n");
92
93         wDumpContent(1);
94 }
95
96
97
98 /**
99  * \brief Locate the message number of a user's vCard in the current room
100  * \param username the plaintext name of the user
101  * \param usernum the number of the user on the citadel server
102  * \return the message id of his vcard
103  */
104 long locate_user_vcard(char *username, long usernum) {
105         char buf[SIZ];
106         long vcard_msgnum = (-1L);
107         char content_type[SIZ];
108         char partnum[SIZ];
109         int already_tried_creating_one = 0;
110
111         struct stuff_t {
112                 struct stuff_t *next;
113                 long msgnum;
114         };
115
116         struct stuff_t *stuff = NULL;
117         struct stuff_t *ptr;
118
119 TRYAGAIN:
120         /** Search for the user's vCard */
121         serv_puts("MSGS ALL");
122         serv_getln(buf, sizeof buf);
123         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
124                 ptr = malloc(sizeof(struct stuff_t));
125                 ptr->msgnum = atol(buf);
126                 ptr->next = stuff;
127                 stuff = ptr;
128         }
129
130         /** Iterate through the message list looking for vCards */
131         while (stuff != NULL) {
132                 serv_printf("MSG0 %ld|2", stuff->msgnum);
133                 serv_getln(buf, sizeof buf);
134                 if (buf[0]=='1') {
135                         while(serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
136                                 if (!strncasecmp(buf, "part=", 5)) {
137                                         extract_token(partnum, &buf[5], 2, '|', sizeof partnum);
138                                         extract_token(content_type, &buf[5], 4, '|', sizeof content_type);
139                                         if (  (!strcasecmp(content_type, "text/x-vcard"))
140                                            || (!strcasecmp(content_type, "text/vcard")) ) {
141                                                 vcard_msgnum = stuff->msgnum;
142                                         }
143                                 }
144                         }
145                 }
146
147                 ptr = stuff->next;
148                 free(stuff);
149                 stuff = ptr;
150         }
151
152         /** If there's no vcard, create one */
153         if (vcard_msgnum < 0) if (already_tried_creating_one == 0) {
154                 already_tried_creating_one = 1;
155                 serv_puts("ENT0 1|||4");
156                 serv_getln(buf, sizeof buf);
157                 if (buf[0] == '4') {
158                         serv_puts("Content-type: text/x-vcard");
159                         serv_puts("");
160                         serv_puts("begin:vcard");
161                         serv_puts("end:vcard");
162                         serv_puts("000");
163                 }
164                 goto TRYAGAIN;
165         }
166
167         return(vcard_msgnum);
168 }
169
170
171 /**
172  * \brief Display the form for editing a user's address book entry
173  * \param username the name of the user
174  * \param usernum the citadel-uid of the user
175  */
176 void display_edit_address_book_entry(char *username, long usernum) {
177         char roomname[SIZ];
178         char buf[SIZ];
179         char error_message[SIZ];
180         long vcard_msgnum = (-1L);
181
182         /** Locate the user's config room, creating it if necessary */
183         sprintf(roomname, "%010ld.%s", usernum, USERCONFIGROOM);
184         serv_printf("GOTO %s||1", roomname);
185         serv_getln(buf, sizeof buf);
186         if (buf[0] != '2') {
187                 serv_printf("CRE8 1|%s|5|||1|", roomname);
188                 serv_getln(buf, sizeof buf);
189                 serv_printf("GOTO %s||1", roomname);
190                 serv_getln(buf, sizeof buf);
191                 if (buf[0] != '2') {
192                         sprintf(error_message,
193                                 "<img src=\"static/error.gif\" align=center>"
194                                 "%s<br /><br />\n", &buf[4]);
195                         select_user_to_edit(error_message, username);
196                         return;
197                 }
198         }
199
200         vcard_msgnum = locate_user_vcard(username, usernum);
201
202         if (vcard_msgnum < 0) {
203                 sprintf(error_message,
204                         "<img src=\"static/error.gif\" align=center>%s<br /><br />\n",
205                         _("An error occurred while trying to create or edit this address book entry.")
206                 );
207                 select_user_to_edit(error_message, username);
208                 return;
209         }
210
211         do_edit_vcard(vcard_msgnum, "1", "select_user_to_edit", roomname);
212 }
213
214
215
216
217 /**
218  * \brief Edit a user.  
219  * If supplied_username is null, look in the "username"
220  * web variable for the name of the user to edit.
221  * 
222  * If "is_new" is set to nonzero, this screen will set the web variables
223  * to send the user to the vCard editor next.
224  * \param supplied_username user to look up or NULL if to search in the environment
225  * \param is_new should we create the user?
226  */
227 void display_edituser(char *supplied_username, int is_new) {
228         char buf[1024];
229         char error_message[1024];
230         time_t now;
231
232         char username[256];
233         char password[256];
234         unsigned int flags;
235         int timescalled;
236         int msgsposted;
237         int axlevel;
238         long usernum;
239         time_t lastcall;
240         int purgedays;
241         int i;
242
243         if (supplied_username != NULL) {
244                 safestrncpy(username, supplied_username, sizeof username);
245         }
246         else {
247                 safestrncpy(username, bstr("username"), sizeof username);
248         }
249
250         serv_printf("AGUP %s", username);
251         serv_getln(buf, sizeof buf);
252         if (buf[0] != '2') {
253                 sprintf(error_message,
254                         "<img src=\"static/error.gif\" align=center>"
255                         "%s<br /><br />\n", &buf[4]);
256                 select_user_to_edit(error_message, username);
257                 return;
258         }
259
260         extract_token(username, &buf[4], 0, '|', sizeof username);
261         extract_token(password, &buf[4], 1, '|', sizeof password);
262         flags = extract_int(&buf[4], 2);
263         timescalled = extract_int(&buf[4], 3);
264         msgsposted = extract_int(&buf[4], 4);
265         axlevel = extract_int(&buf[4], 5);
266         usernum = extract_long(&buf[4], 6);
267         lastcall = extract_long(&buf[4], 7);
268         purgedays = extract_long(&buf[4], 8);
269
270         if (havebstr("edit_abe_button")) {
271                 display_edit_address_book_entry(username, usernum);
272                 return;
273         }
274
275         if (havebstr("delete_button")) {
276                 delete_user(username);
277                 return;
278         }
279
280         output_headers(1, 1, 2, 0, 0, 0);
281         wprintf("<div id=\"banner\">\n");
282         wprintf("<h1>");
283         wprintf(_("Edit user account: "));
284         escputs(username);
285         wprintf("</h1>");
286         wprintf("</div>");
287
288         wprintf("<div id=\"content\" class=\"service\">\n");
289
290         wprintf("<div class=\"fix_scrollbar_bug\">"
291                 "<table class=\"useredit_background\"><tr><td>\n");
292         wprintf("<form method=\"POST\" action=\"edituser\">\n"
293                 "<input type=\"hidden\" name=\"username\" value=\"");
294         escputs(username);
295         wprintf("\">\n");
296         wprintf("<input type=\"hidden\" name=\"is_new\" value=\"%d\">\n"
297                 "<input type=\"hidden\" name=\"usernum\" value=\"%ld\">\n",
298                 is_new, usernum);
299         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
300
301         wprintf("<input type=\"hidden\" name=\"flags\" value=\"%d\">\n", flags);
302
303         wprintf("<center><table>");
304
305         wprintf("<tr><td>");
306         wprintf(_("User name:"));
307         wprintf("</td><td>"
308                 "<input type=\"text\" name=\"newname\" value=\"");
309         escputs(username);
310         wprintf("\" maxlength=\"63\"></td></tr>\n");
311
312         wprintf("<tr><td>");
313         wprintf(_("Password"));
314         wprintf("</td><td>"
315                 "<input type=\"password\" name=\"password\" value=\"");
316         escputs(password);
317         wprintf("\" maxlength=\"20\"></td></tr>\n");
318
319         wprintf("<tr><td>");
320         wprintf(_("Permission to send Internet mail"));
321         wprintf("</td><td>");
322         wprintf("<input type=\"checkbox\" name=\"inetmail\" value=\"yes\" ");
323         if (flags & US_INTERNET) {
324                 wprintf("checked ");
325         }
326         wprintf("></td></tr>\n");
327
328         wprintf("<tr><td>");
329         wprintf(_("Number of logins"));
330         wprintf("</td><td>"
331                 "<input type=\"text\" name=\"timescalled\" value=\"");
332         wprintf("%d", timescalled);
333         wprintf("\" maxlength=\"6\"></td></tr>\n");
334
335         wprintf("<tr><td>");
336         wprintf(_("Messages submitted"));
337         wprintf("</td><td>"
338                 "<input type=\"text\" name=\"msgsposted\" value=\"");
339         wprintf("%d", msgsposted);
340         wprintf("\" maxlength=\"6\"></td></tr>\n");
341
342         wprintf("<tr><td>");
343         wprintf(_("Access level"));
344         wprintf("</td><td>"
345                 "<select name=\"axlevel\">\n");
346         for (i=0; i<7; ++i) {
347                 wprintf("<option ");
348                 if (axlevel == i) {
349                         wprintf("selected ");
350                 }
351                 wprintf("value=\"%d\">%d - %s</option>\n",
352                         i, i, axdefs[i]);
353         }
354         wprintf("</select></td></tr>\n");
355
356         wprintf("<tr><td>");
357         wprintf(_("User ID number"));
358         wprintf("</td><td>"
359                 "<input type=\"text\" name=\"usernum\" value=\"");
360         wprintf("%ld", usernum);
361         wprintf("\" maxlength=\"7\"></td></tr>\n");
362
363         now = time(NULL);
364         wprintf("<tr><td>");
365         wprintf(_("Date and time of last login"));
366         wprintf("</td><td>"
367                 "<select name=\"lastcall\">\n");
368
369         wprintf("<option selected value=\"%ld\">", lastcall);
370         escputs(asctime(localtime(&lastcall)));
371         wprintf("</option>\n");
372
373         wprintf("<option value=\"%ld\">", now);
374         escputs(asctime(localtime(&now)));
375         wprintf("</option>\n");
376
377         wprintf("</select></td></tr>");
378
379         wprintf("<tr><td>");
380         wprintf(_("Auto-purge after this many days"));
381         wprintf("</td><td>"
382                 "<input type=\"text\" name=\"purgedays\" value=\"");
383         wprintf("%d", purgedays);
384         wprintf("\" maxlength=\"5\"></td></tr>\n");
385
386         wprintf("</table>\n");
387
388         wprintf("<input type=\"submit\" name=\"ok_button\" value=\"%s\">\n"
389                 "&nbsp;"
390                 "<input type=\"submit\" name=\"cancel\" value=\"%s\">\n"
391                 "<br /><br /></form>\n", _("Save changes"), _("Cancel"));
392
393         wprintf("</center>\n");
394         wprintf("</td></tr></table></div>\n");
395         wDumpContent(1);
396
397 }
398
399
400 /**
401  * \brief do the backend operation of the user edit on the server
402  */
403 void edituser(void) {
404         char message[SIZ];
405         char buf[SIZ];
406         int is_new = 0;
407         unsigned int flags = 0;
408         char *username;
409
410         is_new = ibstr("is_new");
411         safestrncpy(message, "", sizeof message);
412         username = bstr("username");
413
414         if (!havebstr("ok_button")) {
415                 safestrncpy(message, _("Changes were not saved."), sizeof message);
416         }
417         
418         else {
419                 flags = ibstr("flags");
420                 if (yesbstr("inetmail")) {
421                         flags |= US_INTERNET;
422                 }
423                 else {
424                         flags &= ~US_INTERNET ;
425                 }
426
427                 if ((havebstr("newname")) && (strcasecmp(bstr("username"), bstr("newname")))) {
428                         serv_printf("RENU %s|%s", bstr("username"), bstr("newname"));
429                         serv_getln(buf, sizeof buf);
430                         if (buf[0] != '2') {
431                                 sprintf(&message[strlen(message)],
432                                         "<img src=\"static/error.gif\" align=center>"
433                                         "%s<br /><br />\n", &buf[4]);
434                         }
435                         else {
436                                 username = bstr("newname");
437                         }
438                 }
439
440                 serv_printf("ASUP %s|%s|%d|%s|%s|%s|%s|%s|%s|",
441                         username,
442                         bstr("password"),
443                         flags,
444                         bstr("timescalled"),
445                         bstr("msgsposted"),
446                         bstr("axlevel"),
447                         bstr("usernum"),
448                         bstr("lastcall"),
449                         bstr("purgedays")
450                 );
451                 serv_getln(buf, sizeof buf);
452                 if (buf[0] != '2') {
453                         sprintf(&message[strlen(message)],
454                                 "<img src=\"static/error.gif\" align=center>"
455                                 "%s<br /><br />\n", &buf[4]);
456                 }
457         }
458
459         /**
460          * If we are in the middle of creating a new user, move on to
461          * the vCard edit screen.
462          */
463         if (is_new) {
464                 display_edit_address_book_entry(username, lbstr("usernum") );
465         }
466         else {
467                 select_user_to_edit(message, username);
468         }
469 }
470
471 /*
472  * \brief burge a user 
473  * \param username the name of the user to remove
474  */
475 void delete_user(char *username) {
476         char buf[SIZ];
477         char message[SIZ];
478
479         serv_printf("ASUP %s|0|0|0|0|0|", username);
480         serv_getln(buf, sizeof buf);
481         if (buf[0] != '2') {
482                 sprintf(message,
483                         "<img src=\"static/error.gif\" align=center>"
484                         "%s<br /><br />\n", &buf[4]);
485         }
486         else {
487                 safestrncpy(message, "", sizeof message);
488         }
489         select_user_to_edit(message, bstr("username"));
490 }
491                 
492
493
494 /**
495  * \brief create a new user
496  * take the web environment username and create it on the citadel server
497  */
498 void create_user(void) {
499         char buf[SIZ];
500         char error_message[SIZ];
501         char username[SIZ];
502
503         safestrncpy(username, bstr("username"), sizeof username);
504
505         serv_printf("CREU %s", username);
506         serv_getln(buf, sizeof buf);
507
508         if (buf[0] == '2') {
509                 sprintf(WC->ImportantMessage, _("A new user has been created."));
510                 display_edituser(username, 1);
511         }
512         else if (!strncmp(buf, "570", 3)) {
513                 sprintf(error_message,
514                         "<img src=\"static/error.gif\" align=center>"
515                         "%s<br /><br />\n",
516                         _("You are attempting to create a new user from within Citadel "
517                         "while running in host based authentication mode.  In this mode, "
518                         "you must create new users on the host system, not within Citadel.")
519                 );
520                 select_user_to_edit(error_message, NULL);
521         }
522         else {
523                 sprintf(error_message,
524                         "<img src=\"static/error.gif\" align=center>"
525                         "%s<br /><br />\n", &buf[4]);
526                 select_user_to_edit(error_message, NULL);
527         }
528
529 }
530
531
532
533 /*@}*/