1 #include "lsg2.h"
2 #include <string.h>
3 
CGI_MODE(cgimode_default)4 CGI_MODE(cgimode_default)
5 {
6    if (!LMAPI->cgi_unparse_template("frontpage")) {
7       lsg2_internal_error("No Default Template");
8    }
9 }
10 
CGI_MODE(cgimode_login)11 CGI_MODE(cgimode_login)
12 {
13    char cookiebuf[BIG_BUF];
14    const char *fromaddy, *pass;
15 
16    fromaddy = LMAPI->get_var("lcgi-user");
17    pass = LMAPI->get_var("lcgi-pass");
18 
19    if (!fromaddy) {
20       lsg2_internal_error("No username provided!");
21       return;
22    }
23 
24    if(lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
25        if (LMAPI->get_var("lcgi-list")) {
26           LMAPI->set_var("lcgi-mode","listmenu",VAR_GLOBAL);
27           cgimode_listmenu();
28           return;
29        }
30        if (!LMAPI->cgi_unparse_template("mainmenu")) {
31           lsg2_internal_error("No main menu Template");
32           return;
33        }
34    }
35 
36 }
37 
CGI_MODE(cgimode_displayfile)38 CGI_MODE(cgimode_displayfile)
39 {
40    const char *fromaddy, *pass, *list;
41    char cookiebuf[BIG_BUF];
42 
43    fromaddy = LMAPI->get_var("lcgi-user");
44    pass = LMAPI->get_var("lcgi-pass");
45    list = LMAPI->get_var("list");
46 
47    if (!list) {
48       lsg2_internal_error("No list context!");
49    }
50 
51    if (!fromaddy) {
52       lsg2_internal_error("No username provided!");
53       return;
54    }
55 
56    if(LMAPI->find_cgi_tempvar("show-welcome")) {
57       char tempbuf[BIG_BUF];
58 
59       LMAPI->listdir_file(tempbuf,list,LMAPI->get_string("welcome-file"));
60       LMAPI->set_var("tlcgi-textfile",tempbuf,VAR_TEMP);
61    } else
62    if(LMAPI->find_cgi_tempvar("show-faq")) {
63       char tempbuf[BIG_BUF];
64 
65       LMAPI->listdir_file(tempbuf,list,LMAPI->get_string("faq-file"));
66       LMAPI->set_var("tlcgi-textfile",tempbuf,VAR_TEMP);
67    } else
68    if(LMAPI->find_cgi_tempvar("show-info")) {
69       char tempbuf[BIG_BUF];
70 
71       LMAPI->listdir_file(tempbuf,list,LMAPI->get_string("info-file"));
72       LMAPI->set_var("tlcgi-textfile",tempbuf,VAR_TEMP);
73    } else {
74       lsg2_internal_error("No textfile to display.");
75    }
76 
77    if(lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
78        if (!LMAPI->cgi_unparse_template("textfile")) {
79           lsg2_internal_error("No Textfile display template");
80           return;
81        }
82    }
83 }
84 
CGI_MODE(cgimode_listmenu)85 CGI_MODE(cgimode_listmenu)
86 {
87    char cookiebuf[BIG_BUF];
88    const char *fromaddy, *pass, *list;
89 
90    fromaddy = LMAPI->get_var("lcgi-user");
91    pass = LMAPI->get_var("lcgi-pass");
92    list = LMAPI->get_var("list");
93 
94    if (!list) {
95       lsg2_internal_error("No list context!");
96    }
97 
98    if (!fromaddy) {
99       lsg2_internal_error("No username provided!");
100       return;
101    }
102 
103    if(lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
104        if (!LMAPI->cgi_unparse_template("listmenu")) {
105           lsg2_internal_error("No list menu Template");
106           return;
107        }
108    }
109 }
110 
CGI_MODE(cgimode_userlist)111 CGI_MODE(cgimode_userlist)
112 {
113    char cookiebuf[BIG_BUF];
114    const char *fromaddy, *list;
115 
116    fromaddy = LMAPI->get_var("lcgi-user");
117    list = LMAPI->get_var("list");
118 
119    if (!list) {
120       lsg2_internal_error("No list context!");
121    }
122 
123    if (!fromaddy) {
124       lsg2_internal_error("No username provided!");
125       return;
126    }
127 
128    if(lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
129        const char *list;
130        struct list_user user;
131        int allow=0;
132 
133        list = LMAPI->get_string("list");
134 
135 // CHANGES RW
136 // I HATE IT! KILL! MURDER!
137 
138        if (strcmp(LMAPI->get_string("who-status"),"public") == 0)
139          { allow=1; }
140        else
141          { if ((LMAPI->user_find_list(list,LMAPI->get_string("lcgi-user"),&user)) && (strcmp(LMAPI->get_string("who-status"),"private") == 0))
142               { allow=1; }
143            else
144              { if (LMAPI->user_hasflag(&user,"ADMIN"))
145                  { allow=1; }
146              }
147          }
148 
149        if (allow == 0) {
150           if (!LMAPI->cgi_unparse_template("userlist-deny"))
151              lsg2_internal_error("No userlist-deny template.");
152           return;
153        }
154        if (!LMAPI->cgi_unparse_template("userlist")) {
155           lsg2_internal_error("No userlist Template");
156           return;
157        }
158    }
159 }
160 
CGI_MODE(cgimode_passwd)161 CGI_MODE(cgimode_passwd)
162 {
163    char cookiebuf[BIG_BUF], buf[BIG_BUF];
164    const char *fromaddy, *password, *passwd2;
165 
166    fromaddy = LMAPI->get_string("lcgi-user");
167    password = LMAPI->get_string("lcgi-pass");
168    passwd2 = LMAPI->get_string("lcgi-pass-confirm");
169 
170    if(!lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
171       return;
172    }
173 
174    if (!password) {
175       lsg2_internal_error("You must provide a password.");
176       return;
177    }
178 
179    if (passwd2) {
180       if (strcmp(passwd2,password) != 0) {
181          lsg2_internal_error("The passwords do not match.");
182          return;
183       }
184    }
185 
186    LMAPI->set_pass(fromaddy,password);
187 
188    LMAPI->buffer_printf(buf, sizeof(buf) - 1, "Password changed.", fromaddy);
189    LMAPI->set_var("tlcgi-generic-text", buf, VAR_TEMP);
190    if (!LMAPI->cgi_unparse_template("generic")) {
191       lsg2_internal_error("No generic template.");
192    }
193    return;
194 }
195 
CGI_MODE(cgimode_subscribe)196 CGI_MODE(cgimode_subscribe)
197 {
198    char cookiebuf[BIG_BUF];
199    char userfile[BIG_BUF];
200    char tempbuf[BIG_BUF];
201    const char *from;
202 
203    if (!lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
204       return;
205    }
206 
207    from = LMAPI->get_var("lcgi-user");
208 
209    LMAPI->set_var("subscribe-me",from,VAR_GLOBAL);
210    LMAPI->set_var("fromaddress",from,VAR_GLOBAL);
211    LMAPI->set_var("realsender",from,VAR_GLOBAL);
212 
213    if (LMAPI->do_hooks("PRESUB") == HOOK_RESULT_FAIL) {
214       char tempbuf[BIG_BUF];
215 
216       LMAPI->buffer_printf(tempbuf, sizeof(tempbuf) - 1, "%s.perr",
217          LMAPI->get_string("queuefile"));
218       LMAPI->set_var("tlcgi-textfile",tempbuf,VAR_TEMP);
219       if (!LMAPI->cgi_unparse_template("textfile")) {
220          lsg2_internal_error("No textfile template.");
221       }
222       LMAPI->unlink_file(tempbuf);
223       return;
224    }
225 
226    LMAPI->listdir_file(userfile,LMAPI->get_string("list"),"users");
227 
228    if (LMAPI->user_add(userfile,from)) {
229        lsg2_internal_error("Filesystem error while subscribing user.");
230        LMAPI->filesys_error(userfile);
231        LMAPI->buffer_printf(tempbuf, sizeof(tempbuf) - 1, "%s.perr",
232           LMAPI->get_string("queuefile"));
233        LMAPI->unlink_file(tempbuf);
234        return;
235    }
236    LMAPI->log_printf(0, "%s subscribed to %s\n",from,
237        LMAPI->get_string("listname"));
238 
239    LMAPI->do_hooks("POSTSUB");
240 
241    if (!LMAPI->cgi_unparse_template("subscribe")) {
242       lsg2_internal_error("No 'subscribe' template.");
243       return;
244    }
245 
246    return;
247 }
248 
CGI_MODE(cgimode_unsubscribe)249 CGI_MODE(cgimode_unsubscribe)
250 {
251    char cookiebuf[BIG_BUF];
252    char userfile[BIG_BUF];
253    const char *from;
254    char tempbuf[BIG_BUF];
255 
256    if (!lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
257       return;
258    }
259 
260    from = LMAPI->get_var("lcgi-user");
261 
262    LMAPI->set_var("subscribe-me",from,VAR_GLOBAL);
263    LMAPI->set_var("fromaddress",from,VAR_GLOBAL);
264    LMAPI->set_var("realsender",from,VAR_GLOBAL);
265 
266    if (LMAPI->do_hooks("PREUNSUB") == HOOK_RESULT_FAIL) {
267       char tempbuf[BIG_BUF];
268 
269       LMAPI->buffer_printf(tempbuf, sizeof(tempbuf) - 1, "%s.perr",
270          LMAPI->get_string("queuefile"));
271       LMAPI->set_var("tlcgi-textfile",tempbuf,VAR_TEMP);
272       if (!LMAPI->cgi_unparse_template("textfile")) {
273          lsg2_internal_error("No textfile template.");
274       }
275       LMAPI->unlink_file(tempbuf);
276       return;
277    }
278 
279    LMAPI->listdir_file(userfile,LMAPI->get_string("list"),"users");
280 
281    if (LMAPI->user_remove(userfile,from)) {
282        lsg2_internal_error("Filesystem error while unsubscribing user.");
283        LMAPI->filesys_error(userfile);
284        LMAPI->buffer_printf(tempbuf, sizeof(tempbuf) - 1, "%s.perr",
285           LMAPI->get_string("queuefile"));
286        LMAPI->unlink_file(tempbuf);
287        return;
288    }
289 
290    LMAPI->log_printf(0, "%s unsubscribed from %s\n",from,
291        LMAPI->get_string("listname"));
292 
293    LMAPI->do_hooks("POSTUNSUB");
294 
295    if (!LMAPI->cgi_unparse_template("unsubscribe")) {
296       lsg2_internal_error("No 'unsubscribe' template.");
297       return;
298    }
299 
300    return;
301 }
302 
303 
CGI_MODE(cgimode_logout)304 CGI_MODE(cgimode_logout)
305 {
306    char cookiefile[BIG_BUF];
307    char cookiebuf[BIG_BUF];
308 
309    if(!lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
310       return;
311    }
312 
313    LMAPI->buffer_printf(cookiefile, sizeof(cookiefile) - 1, "%s/SITEDATA/cookies",
314       LMAPI->get_string("lists-root"));
315    LMAPI->del_cookie(cookiefile,cookiebuf);
316 
317    LMAPI->clean_var("lcgi-cookie", VAR_ALL);
318    LMAPI->clean_var("lcgi-user", VAR_ALL);
319    LMAPI->clean_var("lcgi-list", VAR_ALL);
320    LMAPI->clean_var("list", VAR_ALL);
321 
322    LMAPI->wipe_vars(VAR_LIST);
323 
324    if (!LMAPI->cgi_unparse_template("frontpage")) {
325       lsg2_internal_error("No frontpage template.");
326    }
327    return;
328 }
329 
CGI_MODE(cgimode_flagedit)330 CGI_MODE(cgimode_flagedit)
331 {
332    char cookiebuf[BIG_BUF];
333    struct list_user user;
334    char buf[BIG_BUF];
335 
336    if (!lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
337       return;
338    }
339 
340    if (!LMAPI->get_var("lcgi-list")) {
341       lsg2_internal_error("At flag screen without list context.");
342       return;
343    }
344 
345    if (!LMAPI->get_var("lcgi-user")) {
346       lsg2_internal_error("At flag screen without user.");
347    }
348 
349    if (!LMAPI->user_find_list(LMAPI->get_string("lcgi-list"),
350                               LMAPI->get_string("lcgi-user"), &user)) {
351       LMAPI->buffer_printf(buf, sizeof(buf) - 1, "%s is not a member of %s",
352         LMAPI->get_string("lcgi-user"), LMAPI->get_string("lcgi-list"));
353       LMAPI->set_var("tlcgi-generic-text", buf, VAR_TEMP);
354       if (!LMAPI->cgi_unparse_template("generic")) {
355         lsg2_internal_error("No generic template.");
356       }
357       return;
358    }
359 
360    if (!LMAPI->cgi_unparse_template("flagedit")) {
361       lsg2_internal_error("No flagedit template.");
362    }
363    return;
364 }
365 
CGI_MODE(cgimode_setflags)366 CGI_MODE(cgimode_setflags)
367 {
368    char cookiebuf[BIG_BUF];
369    struct list_user user;
370    const char *list, *fromaddy;
371    struct listserver_flag *tflag;
372    char tbuf[BIG_BUF];
373    int isadmin;
374 
375    if (!lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
376       return;
377    }
378 
379    list = LMAPI->get_var("lcgi-list");
380    fromaddy = LMAPI->get_var("lcgi-user");
381 
382    if (!list || !fromaddy) {
383       lsg2_internal_error("Invalid state for flag setting.");
384    }
385 
386    if (!LMAPI->user_find_list(list, fromaddy, &user)) {
387       char errbuf[BIG_BUF];
388 
389       LMAPI->buffer_printf(errbuf, sizeof(errbuf) - 1, "%s is not a member of %s",
390         fromaddy, list);
391 
392       lsg2_internal_error(errbuf);
393    }
394 
395    isadmin = LMAPI->user_hasflag(&user,"ADMIN");
396 
397    tflag = LMAPI->get_flags();
398    while (tflag) {
399       if (!strcasecmp(tflag->name,"superadmin") ||
400           (tflag->admin && !isadmin) ||
401           ((tflag->admin & ADMIN_UNSAFE) ||
402            (tflag->admin & ADMIN_UNSETTABLE))) {
403          tflag = tflag->next;
404          continue;
405       }
406 
407       if (LMAPI->find_cgi_tempvar(tflag->name)) {
408          LMAPI->log_printf(18,"CGI: %s is selected.\n", tflag->name);
409          if (!LMAPI->user_hasflag(&user,tflag->name)) {
410             LMAPI->set_var("setflag-user",user.address,VAR_TEMP);
411             LMAPI->set_var("setflag-flag",tflag->name,VAR_TEMP);
412             if (LMAPI->do_hooks("SETFLAG") != HOOK_RESULT_FAIL) {
413                /* Re-read the user in case they were modified by the
414                 * hooks. */
415                LMAPI->user_find_list(list,fromaddy,&user);
416                if (!LMAPI->user_hasflag(&user,tflag->name)) {
417                   char userlistfile[BIG_BUF];
418 
419                   LMAPI->listdir_file(userlistfile, list, "users");
420                   LMAPI->user_setflag(&user,tflag->name,0);
421                   LMAPI->user_write(userlistfile, &user);
422                }
423             }
424          }
425       } else {
426          LMAPI->log_printf(18,"CGI: %s is unselected.\n", tflag->name);
427          if (LMAPI->user_hasflag(&user,tflag->name)) {
428             LMAPI->set_var("setflag-user",user.address,VAR_TEMP);
429             LMAPI->set_var("setflag-flag",tflag->name,VAR_TEMP);
430             if (LMAPI->do_hooks("UNSETFLAG") != HOOK_RESULT_FAIL) {
431                /* Re-read the user in case they were modified by the
432                 * hooks. */
433                LMAPI->user_find_list(list,fromaddy,&user);
434                if (LMAPI->user_hasflag(&user,tflag->name)) {
435                   char userlistfile[BIG_BUF];
436 
437                   LMAPI->listdir_file(userlistfile, list, "users");
438                   LMAPI->user_unsetflag(&user,tflag->name,0);
439                   LMAPI->user_write(userlistfile, &user);
440                }
441             }
442          }
443       }
444       tflag = tflag->next;
445    }
446 
447    LMAPI->buffer_printf(tbuf, sizeof(tbuf) - 1, "%s.perr",
448      LMAPI->get_string("queuefile"));
449 
450    if (LMAPI->exists_file(tbuf)) {
451       LMAPI->set_var("tlcgi-textfile",tbuf,VAR_TEMP);
452       if (!LMAPI->cgi_unparse_template("textfile")) {
453          lsg2_internal_error("No 'textfile' template.");
454       }
455       LMAPI->unlink_file(tbuf);
456    } else {
457       if (!LMAPI->cgi_unparse_template("userinfo")) {
458          lsg2_internal_error("No 'userinfo' template.");
459       }
460    }
461 }
462 
CGI_MODE(cgimode_setname)463 CGI_MODE(cgimode_setname)
464 {
465    char cookiebuf[BIG_BUF];
466    const char *list, *user, *name;
467 
468    if (!lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
469       return;
470    }
471 
472    list = LMAPI->get_var("lcgi-list");
473    user = LMAPI->get_var("lcgi-user");
474    name = LMAPI->get_var("lcgi-fullname");
475 
476    if (!list || !user || !name) {
477       lsg2_internal_error("Mode not initialized properly.");
478       return;
479    }
480 
481    LMAPI->userstat_set_stat(list,user,"realname",name);
482 
483    if (!LMAPI->cgi_unparse_template("userinfo")) {
484       lsg2_internal_error("No 'userinfo' template.");
485    }
486 
487    return;
488 }
489 
CGI_MODE(cgimode_userinfo)490 CGI_MODE(cgimode_userinfo)
491 {
492    char cookiebuf[BIG_BUF];
493    const char *list, *user;
494 
495    if (!lsg2_validate(cookiebuf, sizeof(cookiebuf) - 1)) {
496       return;
497    }
498 
499    list = LMAPI->get_var("lcgi-list");
500    user = LMAPI->get_var("lcgi-user");
501 
502    if (!list || !user) {
503       lsg2_internal_error("Mode not initialized properly.");
504       return;
505    }
506 
507    if (!LMAPI->cgi_unparse_template("userinfo")) {
508       lsg2_internal_error("No 'userinfo' template.");
509    }
510 
511    return;
512 }
513 
514