/[cvs]/eggdrop1.9/src/chanprog.c
ViewVC logotype

Contents of /eggdrop1.9/src/chanprog.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.19 - (show annotations) (download) (as text)
Thu Nov 16 02:49:14 2000 UTC (18 years, 8 months ago) by guppy
Branch: MAIN
Changes since 1.18: +7 -3 lines
File MIME type: text/x-chdr
guppy: my nouserfile patch, time to release 1.6.0

1 /*
2 * chanprog.c -- handles:
3 * rmspace()
4 * maintaining the server list
5 * revenge punishment
6 * timers, utimers
7 * telling the current programmed settings
8 * initializing a lot of stuff and loading the tcl scripts
9 *
10 * $Id: chanprog.c,v 1.18 2000/10/19 16:32:20 fabian Exp $
11 */
12 /*
13 * Copyright (C) 1997 Robey Pointer
14 * Copyright (C) 1999, 2000 Eggheads
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
31 #include "main.h"
32 #if HAVE_GETRUSAGE
33 #include <sys/resource.h>
34 #if HAVE_SYS_RUSAGE_H
35 #include <sys/rusage.h>
36 #endif
37 #endif
38 #ifdef HAVE_UNAME
39 #include <sys/utsname.h>
40 #endif
41 #include "modules.h"
42
43 extern struct userrec *userlist;
44 extern log_t *logs;
45 extern Tcl_Interp *interp;
46 extern char ver[], botnetnick[], firewall[],
47 motdfile[], userfile[], helpdir[], tempdir[],
48 moddir[], notify_new[], owner[], configfile[];
49 extern time_t now, online_since;
50 extern int backgrd, term_z, con_chan, cache_hit, cache_miss,
51 firewallport, default_flags, max_logs, conmask,
52 protect_readonly, make_userfile, noshare,
53 ignore_time;
54
55 tcl_timer_t *timer = NULL; /* Minutely timer */
56 tcl_timer_t *utimer = NULL; /* Secondly timer */
57 unsigned long timer_id = 1; /* Next timer of any sort will
58 have this number */
59 struct chanset_t *chanset = NULL; /* Channel list */
60 char admin[121] = ""; /* Admin info */
61 char origbotname[NICKLEN + 1];
62 char botname[NICKLEN + 1]; /* Primary botname */
63
64
65 /* Remove space characters from beginning and end of string
66 * (more efficent by Fred1)
67 */
68 void rmspace(char *s)
69 {
70 #define whitespace(c) (((c) == 32) || ((c) == 9) || ((c) == 13) || ((c) == 10))
71 char *p;
72
73 if (*s == '\0')
74 return;
75
76 /* Wipe end of string */
77 for (p = s + strlen(s) - 1; ((whitespace(*p)) && (p >= s)); p--);
78 if (p != s + strlen(s) - 1)
79 *(p + 1) = 0;
80 for (p = s; ((whitespace(*p)) && (*p)); p++);
81 if (p != s)
82 strcpy(s, p);
83 }
84
85 /* Returns memberfields if the nick is in the member list.
86 */
87 memberlist *ismember(struct chanset_t *chan, char *nick)
88 {
89 register memberlist *x;
90
91 for (x = chan->channel.member; x && x->nick[0]; x = x->next)
92 if (!rfc_casecmp(x->nick, nick))
93 return x;
94 return NULL;
95 }
96
97 /* Find a chanset by channel name as the server knows it (ie !ABCDEchannel)
98 */
99 struct chanset_t *findchan(const char *name)
100 {
101 register struct chanset_t *chan;
102
103 for (chan = chanset; chan; chan = chan->next)
104 if (!rfc_casecmp(chan->name, name))
105 return chan;
106 return NULL;
107 }
108
109 /* Find a chanset by display name (ie !channel)
110 */
111 struct chanset_t *findchan_by_dname(const char *name)
112 {
113 register struct chanset_t *chan;
114
115 for (chan = chanset; chan; chan = chan->next)
116 if (!rfc_casecmp(chan->dname, name))
117 return chan;
118 return NULL;
119 }
120
121 /*
122 * "caching" functions
123 */
124
125 /* Shortcut for get_user_by_host -- might have user record in one
126 * of the channel caches.
127 */
128 struct userrec *check_chanlist(const char *host)
129 {
130 char *nick, *uhost, buf[UHOSTLEN];
131 register memberlist *m;
132 register struct chanset_t *chan;
133
134 strncpy(buf, host, UHOSTMAX), buf[UHOSTMAX] = 0;
135 uhost = buf;
136 nick = splitnick(&uhost);
137 for (chan = chanset; chan; chan = chan->next)
138 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
139 if (!rfc_casecmp(nick, m->nick) && !egg_strcasecmp(uhost, m->userhost))
140 return m->user;
141 return NULL;
142 }
143
144 /* Shortcut for get_user_by_handle -- might have user record in channels
145 */
146 struct userrec *check_chanlist_hand(const char *hand)
147 {
148 register struct chanset_t *chan;
149 register memberlist *m;
150
151 for (chan = chanset; chan; chan = chan->next)
152 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
153 if (m->user && !egg_strcasecmp(m->user->handle, hand))
154 return m->user;
155 return NULL;
156 }
157
158 /* Clear the user pointers in the chanlists.
159 *
160 * Necessary when a hostmask is added/removed, a user is added or a new
161 * userfile is loaded.
162 */
163 void clear_chanlist(void)
164 {
165 register memberlist *m;
166 register struct chanset_t *chan;
167
168 for (chan = chanset; chan; chan = chan->next)
169 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
170 m->user = NULL;
171 }
172
173 /* Clear the user pointer of a specific nick in the chanlists.
174 *
175 * Necessary when a hostmask is added/removed, a nick changes, etc.
176 * Does not completely invalidate the channel cache like clear_chanlist().
177 */
178 void clear_chanlist_member(const char *nick)
179 {
180 register memberlist *m;
181 register struct chanset_t *chan;
182
183 for (chan = chanset; chan; chan = chan->next)
184 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
185 if (!rfc_casecmp(m->nick, nick)) {
186 m->user = NULL;
187 break;
188 }
189 }
190
191 /* If this user@host is in a channel, set it (it was null)
192 */
193 void set_chanlist(const char *host, struct userrec *rec)
194 {
195 char *nick, *uhost, buf[UHOSTLEN];
196 register memberlist *m;
197 register struct chanset_t *chan;
198
199 strncpy(buf, host, UHOSTMAX), buf[UHOSTMAX] = 0;
200 uhost = buf;
201 nick = splitnick(&uhost);
202 for (chan = chanset; chan; chan = chan->next)
203 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
204 if (!rfc_casecmp(nick, m->nick) && !egg_strcasecmp(uhost, m->userhost))
205 m->user = rec;
206 }
207
208 /* Calculate the memory we should be using
209 */
210 int expmem_chanprog()
211 {
212 register int tot = 0;
213 register tcl_timer_t *t;
214
215 Context;
216 for (t = timer; t; t = t->next)
217 tot += sizeof(tcl_timer_t) + strlen(t->cmd) + 1;
218 for (t = utimer; t; t = t->next)
219 tot += sizeof(tcl_timer_t) + strlen(t->cmd) + 1;
220 return tot;
221 }
222
223 /* Dump uptime info out to dcc (guppy 9Jan99)
224 */
225 void tell_verbose_uptime(int idx)
226 {
227 char s[256], s1[121];
228 time_t now2, hr, min;
229
230 now2 = now - online_since;
231 s[0] = 0;
232 if (now2 > 86400) {
233 /* days */
234 sprintf(s, "%d day", (int) (now2 / 86400));
235 if ((int) (now2 / 86400) >= 2)
236 strcat(s, "s");
237 strcat(s, ", ");
238 now2 -= (((int) (now2 / 86400)) * 86400);
239 }
240 hr = (time_t) ((int) now2 / 3600);
241 now2 -= (hr * 3600);
242 min = (time_t) ((int) now2 / 60);
243 sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
244 s1[0] = 0;
245 if (backgrd)
246 strcpy(s1, MISC_BACKGROUND);
247 else {
248 if (term_z)
249 strcpy(s1, MISC_TERMMODE);
250 else if (con_chan)
251 strcpy(s1, MISC_STATMODE);
252 else
253 strcpy(s1, MISC_LOGMODE);
254 }
255 dprintf(idx, "%s %s (%s)\n", MISC_ONLINEFOR, s, s1);
256 }
257
258 /* Dump status info out to dcc
259 */
260 void tell_verbose_status(int idx)
261 {
262 char s[256], s1[121], s2[81];
263 char *vers_t, *uni_t;
264 int i;
265 time_t now2, hr, min;
266 #if HAVE_GETRUSAGE
267 struct rusage ru;
268 #else
269 # if HAVE_CLOCK
270 clock_t cl;
271 # endif
272 #endif
273 #ifdef HAVE_UNAME
274 struct utsname un;
275
276 if (!uname(&un) < 0) {
277 #endif
278 vers_t = " ";
279 uni_t = "*unknown*";
280 #ifdef HAVE_UNAME
281 } else {
282 vers_t = un.release;
283 uni_t = un.sysname;
284 }
285 #endif
286
287 i = count_users(userlist);
288 dprintf(idx, "I am %s, running %s: %d user%s (mem: %uk)\n",
289 botnetnick, ver, i, i == 1 ? "" : "s",
290 (int) (expected_memory() / 1024));
291 dprintf(idx, "Running on %s %s\n", uni_t, vers_t);
292 if (admin[0])
293 dprintf(idx, "Admin: %s\n", admin);
294 now2 = now - online_since;
295 s[0] = 0;
296 if (now2 > 86400) {
297 /* days */
298 sprintf(s, "%d day", (int) (now2 / 86400));
299 if ((int) (now2 / 86400) >= 2)
300 strcat(s, "s");
301 strcat(s, ", ");
302 now2 -= (((int) (now2 / 86400)) * 86400);
303 }
304 hr = (time_t) ((int) now2 / 3600);
305 now2 -= (hr * 3600);
306 min = (time_t) ((int) now2 / 60);
307 sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
308 s1[0] = 0;
309 if (backgrd)
310 strcpy(s1, MISC_BACKGROUND);
311 else {
312 if (term_z)
313 strcpy(s1, MISC_TERMMODE);
314 else if (con_chan)
315 strcpy(s1, MISC_STATMODE);
316 else
317 strcpy(s1, MISC_LOGMODE);
318 }
319 #if HAVE_GETRUSAGE
320 getrusage(RUSAGE_SELF, &ru);
321 hr = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) / 60);
322 min = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) - (hr * 60));
323 sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actally min/sec */
324 #else
325 # if HAVE_CLOCK
326 cl = (clock() / CLOCKS_PER_SEC);
327 hr = (int) (cl / 60);
328 min = (int) (cl - (hr * 60));
329 sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actually min/sec */
330 # else
331 sprintf(s2, "CPU ???");
332 # endif
333 #endif
334 dprintf(idx, "%s %s (%s) %s %s %4.1f%%\n", MISC_ONLINEFOR,
335 s, s1, s2, MISC_CACHEHIT,
336 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
337 strcpy(s, "info library");
338 if ((interp) && (Tcl_Eval(interp, s) == TCL_OK))
339 dprintf(idx, "%s %s\n", MISC_TCLLIBVER, interp->result);
340 }
341
342 /* Show all internal state variables
343 */
344 void tell_settings(int idx)
345 {
346 char s[1024];
347 int i;
348 struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
349
350 dprintf(idx, "Botnet Nickname: %s\n", botnetnick);
351 if (firewall[0])
352 dprintf(idx, "Firewall: %s, port %d\n", firewall, firewallport);
353 dprintf(idx, "Userfile: %s Motd: %s\n", userfile, motdfile);
354 dprintf(idx, "Directories:\n");
355 dprintf(idx, " Help : %s\n", helpdir);
356 dprintf(idx, " Temp : %s\n", tempdir);
357 #ifndef STATIC
358 dprintf(idx, " Modules : %s\n", moddir);
359 #endif
360 fr.global = default_flags;
361
362 build_flags(s, &fr, NULL);
363 dprintf(idx, "%s [%s], %s: %s\n", MISC_NEWUSERFLAGS, s,
364 MISC_NOTIFY, notify_new);
365 if (owner[0])
366 dprintf(idx, "%s: %s\n", MISC_PERMOWNER, owner);
367 for (i = 0; i < max_logs; i++)
368 if (logs[i].filename != NULL) {
369 dprintf(idx, "Logfile #%d: %s on %s (%s: %s)\n", i + 1,
370 logs[i].filename, logs[i].chname,
371 masktype(logs[i].mask), maskname(logs[i].mask));
372 }
373 dprintf(idx, "Ignores last %d mins\n", ignore_time);
374 }
375
376 void reaffirm_owners()
377 {
378 char *p, *q, s[121];
379 struct userrec *u;
380
381 /* Make sure default owners are +n */
382 if (owner[0]) {
383 q = owner;
384 p = strchr(q, ',');
385 while (p) {
386 strncpy(s, q, p - q);
387 s[p - q] = 0;
388 rmspace(s);
389 u = get_user_by_handle(userlist, s);
390 if (u)
391 u->flags = sanity_check(u->flags | USER_OWNER);
392 q = p + 1;
393 p = strchr(q, ',');
394 }
395 strcpy(s, q);
396 rmspace(s);
397 u = get_user_by_handle(userlist, s);
398 if (u)
399 u->flags = sanity_check(u->flags | USER_OWNER);
400 }
401 }
402
403 void chanprog()
404 {
405 int i;
406
407 admin[0] = 0;
408 helpdir[0] = 0;
409 tempdir[0] = 0;
410 for (i = 0; i < max_logs; i++)
411 logs[i].flags |= LF_EXPIRING;
412 conmask = 0;
413 /* Turn off read-only variables (make them write-able) for rehash */
414 protect_readonly = 0;
415 /* Now read it */
416 Context;
417 if (!readtclprog(configfile))
418 fatal(MISC_NOCONFIGFILE, 0);
419 for (i = 0; i < max_logs; i++) {
420 if (logs[i].flags & LF_EXPIRING) {
421 if (logs[i].filename != NULL) {
422 nfree(logs[i].filename);
423 logs[i].filename = NULL;
424 }
425 if (logs[i].chname != NULL) {
426 nfree(logs[i].chname);
427 logs[i].chname = NULL;
428 }
429 if (logs[i].f != NULL) {
430 fclose(logs[i].f);
431 logs[i].f = NULL;
432 }
433 logs[i].mask = 0;
434 logs[i].flags = 0;
435 }
436 }
437 /* We should be safe now */
438 call_hook(HOOK_REHASH);
439 Context;
440 protect_readonly = 1;
441 if (!userfile[0])
442 fatal(MISC_NOUSERFILE2, 0);
443 if ((int) getuid() == 0) {
444 /* Perhaps you should make it run something innocent here ;)
445 * like rm -rf /etc :) */
446 printf("\n\n%s\n", MISC_ROOTWARN);
447 }
448 if (!readuserfile(userfile, &userlist)) {
449 if (!make_userfile) {
450 char tmp[178];
451
452 egg_snprintf(tmp, sizeof tmp, MISC_NOUSERFILE, configfile);
453 fatal(tmp, 0);
454 }
455 printf("\n\n%s\n", MISC_NOUSERFILE2);
456 if (module_find("server", 0, 0))
457 printf(MISC_USERFCREATE1, origbotname);
458 printf("%s\n\n", MISC_USERFCREATE2);
459 } else if (make_userfile) {
460 make_userfile = 0;
461 printf("%s\n", MISC_USERFEXISTS);
462 }
463 Context;
464 if (helpdir[0])
465 if (helpdir[strlen(helpdir) - 1] != '/')
466 strcat(helpdir, "/");
467 if (tempdir[0])
468 if (tempdir[strlen(tempdir) - 1] != '/')
469 strcat(tempdir, "/");
470 if (!botnetnick[0]) {
471 strncpy(botnetnick, origbotname, HANDLEN);
472 botnetnick[HANDLEN] = 0;
473 }
474 if (!botnetnick[0])
475 fatal("I don't have a botnet nick!!\n", 0);
476 Context;
477 /* Test tempdir: it's vital */
478 {
479 FILE *f;
480 char s[161], rands[8];
481
482 /* Possible file race condition solved by using a random string
483 * and the process id in the filename.
484 * FIXME: This race is only partitially fixed. We could still be
485 * overwriting an existing file / following a malicious
486 * link.
487 */
488 make_rand_str(rands, 7); /* create random string */
489 sprintf(s, "%s.test-%u-%s", tempdir, getpid(), rands);
490 f = fopen(s, "w");
491 if (f == NULL)
492 fatal(MISC_CANTWRITETEMP, 0);
493 fclose(f);
494 unlink(s);
495 }
496 Context;
497 reaffirm_owners();
498 }
499
500 /* Reload the user file from disk
501 */
502 void reload()
503 {
504 FILE *f;
505
506 f = fopen(userfile, "r");
507 if (f == NULL) {
508 putlog(LOG_MISC, "*", MISC_CANTRELOADUSER);
509 return;
510 }
511 fclose(f);
512 noshare = 1;
513 clear_userlist(userlist);
514 noshare = 0;
515 userlist = NULL;
516 if (!readuserfile(userfile, &userlist))
517 fatal(MISC_MISSINGUSERF, 0);
518 Context;
519 reaffirm_owners();
520 call_hook(HOOK_READ_USERFILE);
521 }
522
523 void rehash()
524 {
525 call_hook(HOOK_PRE_REHASH);
526 noshare = 1;
527 clear_userlist(userlist);
528 noshare = 0;
529 userlist = NULL;
530 chanprog();
531 }
532
533 /*
534 * Brief venture into timers
535 */
536
537 /* Add a timer
538 */
539 unsigned long add_timer(tcl_timer_t **stack, int elapse, char *cmd,
540 unsigned long prev_id)
541 {
542 tcl_timer_t *old = (*stack);
543
544 *stack = (tcl_timer_t *) nmalloc(sizeof(tcl_timer_t));
545 (*stack)->next = old;
546 (*stack)->mins = elapse;
547 (*stack)->cmd = (char *) nmalloc(strlen(cmd) + 1);
548 strcpy((*stack)->cmd, cmd);
549 /* If it's just being added back and already had an id,
550 * don't create a new one.
551 */
552 if (prev_id > 0)
553 (*stack)->id = prev_id;
554 else
555 (*stack)->id = timer_id++;
556 return (*stack)->id;
557 }
558
559 /* Remove a timer, by id
560 */
561 int remove_timer(tcl_timer_t **stack, unsigned long id)
562 {
563 tcl_timer_t *old;
564 int ok = 0;
565
566 while (*stack) {
567 if ((*stack)->id == id) {
568 ok++;
569 old = *stack;
570 *stack = ((*stack)->next);
571 nfree(old->cmd);
572 nfree(old);
573 } else
574 stack = &((*stack)->next);
575 }
576 return ok;
577 }
578
579 /* Check timers, execute the ones that have expired.
580 */
581 void do_check_timers(tcl_timer_t **stack)
582 {
583 tcl_timer_t *mark = *stack, *old = NULL;
584 char x[30];
585
586 /* New timers could be added by a Tcl script inside a current timer
587 * so i'll just clear out the timer list completely, and add any
588 * unexpired timers back on.
589 */
590 *stack = NULL;
591 while (mark) {
592 if (mark->mins > 0)
593 mark->mins--;
594 old = mark;
595 mark = mark->next;
596 if (old->mins == 0) {
597 simple_sprintf(x, "timer%d", old->id);
598 do_tcl(x, old->cmd);
599 nfree(old->cmd);
600 nfree(old);
601 } else {
602 old->next = *stack;
603 *stack = old;
604 }
605 }
606 }
607
608 /* Wipe all timers.
609 */
610 void wipe_timers(Tcl_Interp *irp, tcl_timer_t **stack)
611 {
612 tcl_timer_t *mark = *stack, *old;
613
614 while (mark) {
615 old = mark;
616 mark = mark->next;
617 nfree(old->cmd);
618 nfree(old);
619 }
620 *stack = NULL;
621 }
622
623 /* Return list of timers
624 */
625 void list_timers(Tcl_Interp *irp, tcl_timer_t *stack)
626 {
627 tcl_timer_t *mark = stack;
628 char mins[10], id[20], *argv[3], *x;
629
630 while (mark != NULL) {
631 sprintf(mins, "%u", mark->mins);
632 sprintf(id, "timer%lu", mark->id);
633 argv[0] = mins;
634 argv[1] = mark->cmd;
635 argv[2] = id;
636 x = Tcl_Merge(3, argv);
637 Tcl_AppendElement(irp, x);
638 Tcl_Free((char *) x);
639 mark = mark->next;
640 }
641 }
642
643 /* Oddly enough, written by proton (Emech's coder)
644 */
645 int isowner(char *name)
646 {
647 char *pa, *pb;
648 char nl, pl;
649
650 if (!owner || !*owner)
651 return (0);
652 if (!name || !*name)
653 return (0);
654 nl = strlen(name);
655 pa = owner;
656 pb = owner;
657 while (1) {
658 while (1) {
659 if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
660 break;
661 pb++;
662 }
663 pl = (unsigned int) pb - (unsigned int) pa;
664 if (pl == nl && !egg_strncasecmp(pa, name, nl))
665 return (1);
666 while (1) {
667 if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
668 break;
669 pb++;
670 }
671 if (*pb == 0)
672 return (0);
673 pa = pb;
674 }
675 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23