/[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.51 - (show annotations) (download) (as text)
Sat Aug 10 09:31:19 2002 UTC (17 years, 3 months ago) by poptix
Branch: MAIN
Changes since 1.50: +2 -2 lines
File MIME type: text/x-chdr
owner variable was not being used properly, potential access elevation.

1 /*
2 * chanprog.c --
3 *
4 * telling the current programmed settings
5 * initializing a lot of stuff and loading the tcl scripts
6 */
7 /*
8 * Copyright (C) 1997 Robey Pointer
9 * Copyright (C) 1999, 2000, 2001, 2002 Eggheads Development Team
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26 #ifndef lint
27 static const char rcsid[] = "$Id: chanprog.c,v 1.50 2002/05/31 04:11:37 stdarg Exp $";
28 #endif
29
30 #include "main.h"
31 #if HAVE_GETRUSAGE
32 #include <sys/resource.h>
33 #if HAVE_SYS_RUSAGE_H
34 #include <sys/rusage.h>
35 #endif
36 #endif
37 #ifdef HAVE_UNAME
38 #include <sys/utsname.h>
39 #endif
40 #include "modules.h"
41 #include "logfile.h"
42 #include "misc.h"
43 #include "users.h" /* get_user_by_handle, readuserfile */
44 #include "dccutil.h" /* dprintf_eggdrop */
45 #include "tcl.h" /* readtclprog */
46 #include "userrec.h" /* count_users, clear_userlist */
47 #include "irccmp.h" /* irccmp */
48
49 extern struct userrec *userlist;
50 extern char ver[], botnetnick[], firewall[],
51 motdfile[], userfile[], helpdir[], tempdir[],
52 moddir[], notify_new[], owner[], configfile[];
53 extern time_t now, online_since;
54 extern int backgrd, term_z, con_chan, cache_hit, cache_miss,
55 firewallport, default_flags, conmask,
56 protect_readonly, make_userfile, noshare,
57 ignore_time;
58
59 struct chanset_t *chanset = NULL; /* Channel list */
60 char admin[121] = ""; /* Admin info */
61 char origbotname[NICKLEN + 1];
62 char *botname; /* Primary botname */
63
64
65 /* Returns memberfields if the nick is in the member list.
66 */
67 memberlist *ismember(struct chanset_t *chan, char *nick)
68 {
69 register memberlist *x;
70
71 for (x = chan->channel.member; x && x->nick[0]; x = x->next)
72 if (!irccmp(x->nick, nick))
73 return x;
74 return NULL;
75 }
76
77 /* Find a chanset by channel name as the server knows it (ie !ABCDEchannel)
78 */
79 struct chanset_t *findchan(const char *name)
80 {
81 register struct chanset_t *chan;
82
83 for (chan = chanset; chan; chan = chan->next)
84 if (!irccmp(chan->name, name))
85 return chan;
86 return NULL;
87 }
88
89 /* Find a chanset by display name (ie !channel)
90 */
91 struct chanset_t *findchan_by_dname(const char *name)
92 {
93 register struct chanset_t *chan;
94
95 for (chan = chanset; chan; chan = chan->next)
96 if (!irccmp(chan->dname, name))
97 return chan;
98 return NULL;
99 }
100
101 /*
102 * "caching" functions
103 */
104
105 /* Shortcut for get_user_by_host -- might have user record in one
106 * of the channel caches.
107 */
108 struct userrec *check_chanlist(const char *host)
109 {
110 char *nick, *uhost, buf[UHOSTLEN];
111 register memberlist *m;
112 register struct chanset_t *chan;
113
114 strlcpy(buf, host, sizeof buf);
115 nick = strtok(buf, "!");
116 uhost = strtok(NULL, "!");
117 for (chan = chanset; chan; chan = chan->next)
118 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
119 if (!irccmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
120 return m->user;
121 return NULL;
122 }
123
124 /* Shortcut for get_user_by_handle -- might have user record in channels
125 */
126 struct userrec *check_chanlist_hand(const char *hand)
127 {
128 register struct chanset_t *chan;
129 register memberlist *m;
130
131 for (chan = chanset; chan; chan = chan->next)
132 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
133 if (m->user && !strcasecmp(m->user->handle, hand))
134 return m->user;
135 return NULL;
136 }
137
138 /* Clear the user pointers in the chanlists.
139 *
140 * Necessary when a hostmask is added/removed, a user is added or a new
141 * userfile is loaded.
142 */
143 void clear_chanlist(void)
144 {
145 register memberlist *m;
146 register struct chanset_t *chan;
147
148 for (chan = chanset; chan; chan = chan->next)
149 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
150 m->user = NULL;
151 }
152
153 /* Clear the user pointer of a specific nick in the chanlists.
154 *
155 * Necessary when a hostmask is added/removed, a nick changes, etc.
156 * Does not completely invalidate the channel cache like clear_chanlist().
157 */
158 void clear_chanlist_member(const char *nick)
159 {
160 register memberlist *m;
161 register struct chanset_t *chan;
162
163 for (chan = chanset; chan; chan = chan->next)
164 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
165 if (!irccmp(m->nick, nick)) {
166 m->user = NULL;
167 break;
168 }
169 }
170
171 /* If this user@host is in a channel, set it (it was null)
172 */
173 void set_chanlist(const char *host, struct userrec *rec)
174 {
175 char *nick, *uhost, buf[UHOSTLEN];
176 register memberlist *m;
177 register struct chanset_t *chan;
178
179 strlcpy(buf, host, sizeof buf);
180 nick = strtok(buf, "!");
181 uhost = strtok(NULL, "!");
182 for (chan = chanset; chan; chan = chan->next)
183 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
184 if (!irccmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
185 m->user = rec;
186 }
187
188 /* Dump status info out to dcc
189 */
190 void tell_verbose_status(int idx)
191 {
192 char s[256], s1[121], s2[81];
193 char *vers_t, *uni_t;
194 int i;
195 time_t now2, hr, min;
196 #if HAVE_GETRUSAGE
197 struct rusage ru;
198 #else
199 # if HAVE_CLOCK
200 clock_t cl;
201 # endif
202 #endif
203 #ifdef HAVE_UNAME
204 struct utsname un;
205
206 if (!uname(&un) < 0) {
207 #endif
208 vers_t = " ";
209 uni_t = _("*unknown*");
210 #ifdef HAVE_UNAME
211 } else {
212 vers_t = un.release;
213 uni_t = un.sysname;
214 }
215 #endif
216
217 i = count_users(userlist);
218 dprintf(idx, _("I am %1$s, running %2$s: "), botnetnick, ver);
219 dprintf(idx, P_("%d user\n", "%d users\n", i), i);
220 dprintf(idx, _("Running on %1$s %2$s\n"), uni_t, vers_t);
221 if (admin[0])
222 dprintf(idx, _("Admin: %s\n"), admin);
223 now2 = now - online_since;
224 s[0] = 0;
225
226 if (now2 > 86400) {
227 int days = (int) (now2 / 86400);
228
229 snprintf(s, sizeof(s), P_("%d day", "%d days", days), days);
230 strcat(s, ", ");
231 now2 -= days * 86400;
232 }
233 hr = (time_t) ((int) now2 / 3600);
234 now2 -= (hr * 3600);
235 min = (time_t) ((int) now2 / 60);
236 snprintf(&s[strlen(s)], sizeof(s) - strlen(s), "%02d:%02d", (int) hr,
237 (int) min);
238 s1[0] = 0;
239 if (backgrd)
240 strcpy(s1, _("background"));
241 else {
242 if (term_z)
243 strcpy(s1, _("terminal mode"));
244 else if (con_chan)
245 strcpy(s1, _("status mode"));
246 else
247 strcpy(s1, _("log dump mode"));
248 }
249 #if HAVE_GETRUSAGE
250 getrusage(RUSAGE_SELF, &ru);
251 hr = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) / 60);
252 min = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) - (hr * 60));
253 sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actally min/sec */
254 #else
255 # if HAVE_CLOCK
256 cl = (clock() / CLOCKS_PER_SEC);
257 hr = (int) (cl / 60);
258 min = (int) (cl - (hr * 60));
259 sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actually min/sec */
260 # else
261 sprintf(s2, "CPU ???");
262 # endif
263 #endif
264 dprintf(idx, "%s %s (%s) %s %s %4.1f%%\n", _("Online for"),
265 s, s1, s2, _("cache hit"),
266 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
267 }
268
269 /* Show all internal state variables
270 */
271 void tell_settings(int idx)
272 {
273 char s[1024];
274 struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
275
276 dprintf(idx, _("Botnet Nickname: %s\n"), botnetnick);
277 if (firewall[0])
278 dprintf(idx, _("Firewall: %1$s, port %2$d\n"), firewall, firewallport);
279 dprintf(idx, _("Userfile: %1$s Motd: %2$s\n"), userfile, motdfile);
280 dprintf(idx, _("Directories:\n"));
281 dprintf(idx, _(" Help : %s\n"), helpdir);
282 dprintf(idx, _(" Temp : %s\n"), tempdir);
283 dprintf(idx, _(" Modules : %s\n"), moddir);
284 fr.global = default_flags;
285
286 build_flags(s, &fr, NULL);
287 dprintf(idx, "%s [%s], %s: %s\n", _("New users get flags"), s,
288 _("notify"), notify_new);
289 /* FIXME PLURAL: handle it properly */
290 if (owner[0])
291 dprintf(idx, _("Permanent owner(s): %s\n"), owner);
292 dprintf(idx, _("Ignores last %d mins\n"), ignore_time);
293 }
294
295 void reaffirm_owners()
296 {
297 char *p, *q, s[121];
298 struct userrec *u;
299
300 /* Make sure default owners are +n */
301 if (owner[0]) {
302 q = owner;
303 p = strchr(q, ',');
304 while (p) {
305 strlcpy(s, q, (p - q) + 1);
306 rmspace(s);
307 u = get_user_by_handle(userlist, s);
308 if (u)
309 u->flags = sanity_check(u->flags | USER_OWNER);
310 q = p + 1;
311 p = strchr(q, ',');
312 }
313 strcpy(s, q);
314 rmspace(s);
315 u = get_user_by_handle(userlist, s);
316 if (u)
317 u->flags = sanity_check(u->flags | USER_OWNER);
318 }
319 }
320
321 void chanprog()
322 {
323 admin[0] = 0;
324 helpdir[0] = 0;
325 tempdir[0] = 0;
326 conmask = 0;
327 /* Turn off read-only variables (make them write-able) for rehash */
328 protect_readonly = 0;
329 /* Now read it */
330 if (!readtclprog(configfile))
331 fatal(_("CONFIG FILE NOT LOADED (NOT FOUND, OR ERROR)"), 0);
332 /* We should be safe now */
333 call_hook(HOOK_REHASH);
334 protect_readonly = 1;
335 if (!botnetnick[0]) {
336 strlcpy(botnetnick, origbotname, HANDLEN + 1);
337 }
338 if (!botnetnick[0])
339 fatal("I don't have a botnet nick!!\n", 0);
340 if (!userfile[0])
341 fatal(_("STARTING BOT IN USERFILE CREATION MODE.\n\
342 Telnet to the bot and enter 'NEW' as your nickname."), 0);
343 if (!readuserfile(userfile, &userlist)) {
344 if (!make_userfile) {
345 char tmp[178];
346
347 snprintf(tmp, sizeof tmp, _("USER FILE NOT FOUND! (try ./eggdrop -m %s to make one)\n"), configfile);
348 fatal(tmp, 0);
349 }
350 printf(_("\n\nSTARTING BOT IN USERFILE CREATION MODE.\n\
351 Telnet to the bot and enter 'NEW' as your nickname.\n"));
352 if (module_find("server", 0, 0))
353 printf(_("OR go to IRC and type: /msg %s hello\n"), origbotname);
354 printf(_("This will make the bot recognize you as the master.\n\n"));
355 } else if (make_userfile) {
356 make_userfile = 0;
357 printf(_("USERFILE ALREADY EXISTS (drop the -m)\n"));
358 }
359 if (helpdir[0])
360 if (helpdir[strlen(helpdir) - 1] != '/')
361 strcat(helpdir, "/");
362 if (tempdir[0])
363 if (tempdir[strlen(tempdir) - 1] != '/')
364 strcat(tempdir, "/");
365 /* Test tempdir: it's vital */
366 {
367 FILE *f;
368 char s[161], rands[8];
369
370 /* Possible file race condition solved by using a random string
371 * and the process id in the filename.
372 * FIXME: This race is only partitially fixed. We could still be
373 * overwriting an existing file / following a malicious
374 * link.
375 */
376 make_rand_str(rands, 7); /* create random string */
377 sprintf(s, "%s.test-%u-%s", tempdir, getpid(), rands);
378 f = fopen(s, "w");
379 if (f == NULL)
380 fatal(_("CANT WRITE TO TEMP DIR"), 0);
381 fclose(f);
382 unlink(s);
383 }
384 reaffirm_owners();
385 }
386
387 /* Reload the user file from disk
388 */
389 void reload()
390 {
391 FILE *f;
392
393 f = fopen(userfile, "r");
394 if (f == NULL) {
395 putlog(LOG_MISC, "*", _("Cant reload user file!"));
396 return;
397 }
398 fclose(f);
399 noshare = 1;
400 clear_userlist(userlist);
401 noshare = 0;
402 userlist = NULL;
403 if (!readuserfile(userfile, &userlist))
404 fatal(_("User file is missing!"), 0);
405 reaffirm_owners();
406 call_hook(HOOK_READ_USERFILE);
407 }
408
409 void rehash()
410 {
411 call_hook(HOOK_PRE_REHASH);
412 noshare = 1;
413 clear_userlist(userlist);
414 noshare = 0;
415 userlist = NULL;
416 chanprog();
417 }
418
419 /* Oddly enough, written by proton (Emech's coder)
420 */
421 int isowner(char *name)
422 {
423 char *pa, *pb;
424 char nl, pl;
425
426 if (!owner || !*owner)
427 return (0);
428 if (!name || !*name)
429 return (0);
430 nl = strlen(name);
431 pa = owner;
432 pb = owner;
433 while (1) {
434 while (1) {
435 if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
436 break;
437 pb++;
438 }
439 pl = (unsigned int) pb - (unsigned int) pa;
440 if (pl == nl && !strncasecmp(pa, name, nl))
441 return (1);
442 while (1) {
443 if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
444 break;
445 pb++;
446 }
447 if (*pb == 0)
448 return (0);
449 pa = pb;
450 }
451 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23