Filter:   InfoImg
download modules.c
Language: C
License: GPL
Copyright: (C) 2002 by the past and present ircd coders, and others.
LOC: 448
Project Info
NeoStats
Server: SourceForge
Type: cvs
...stats\neostats\NeoIRCd\src\
   .cvsignore
   .depend
   adns.c
   balloc.c
   channel.c
   channel_mode.c
   class.c
   client.c
   cloak.c
   crypt.c
   descrip.mms
   dynlink.c
   event.c
   fdlist.c
   fileio.c
   getopt.c
   hash.c
   hook.c
   hostmask.c
   irc_string.c
   ircd.c
   ircd_parser.y
   ircd_signal.c
   ircdauth.c
   kdparse.c
   linebuf.c
   list.c
   listener.c
   m_error.c
   Makefile.in
   match.c
   md5.c
   memory.c
   messages.tab
   modules.c
   motd.c
   numeric.c
   packet.c
   parse.c
   restart.c
   resv.c
   rsa.c
   s_auth.c
   s_bsd.c
   s_bsd_devpoll.c
   s_bsd_kqueue.c
   s_bsd_poll.c
   s_bsd_select.c
   s_bsd_sigio.c
   s_conf.c
   s_debug.c
   s_gline.c
   s_log.c
   s_misc.c
   s_serv.c
   s_stats.c
   s_user.c
   scache.c
   send.c
   snprintf.c
   sprintf_irc.c
   ssl.c
   tools.c
   version.c.SH
   whowas.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
 *  NeoIRCd: NeoStats Group. Based on Hybird7
 *  modules.c: A module loader.
 *
 *  Copyright (C) 2002 by the past and present ircd coders, and others.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *  $Id: modules.c,v 1.7 2003/01/29 09:28:49 fishwaldo Exp $
 */

#include "stdinc.h"


#include "modules.h"
#include "s_log.h"
#include "ircd.h"
#include "client.h"
#include "send.h"
#include "s_conf.h"
#include "handlers.h"
#include "numeric.h"
#include "parse.h"
#include "ircd_defs.h"
#include "irc_string.h"
#include "memory.h"
#include "tools.h"
#include "list.h"

/* -TimeMr14C:
 * I have moved the dl* function definitions and
 * the two functions (load_a_module / unload_a_module) to the
 * file dynlink.c 
 * And also made the necessary changes to those functions
 * to comply with shl_load and friends.
 * In this file, to keep consistency with the makefile, 
 * I added the ability to load *.sl files, too.
 * 27/02/2002
 */

#ifndef STATIC_MODULES

struct module **modlist = NULL;

static char *core_module_table[] =
{
  "m_die.s",
  "m_kick.s",
  "m_kill.s",
  "m_message.s",
  "m_mode.s",
  "m_nick.s",
  "m_part.s",
  "m_quit.s",
  "m_server.s",
  "m_sjoin.s",
  "m_squit.s",
  NULL
};

#define MODS_INCREMENT 10
int num_mods = 0;
int max_mods = MODS_INCREMENT;

static dlink_list mod_paths;

static void mo_modload(struct Client*, struct Client*, int, char**);
static void mo_modlist(struct Client*, struct Client*, int, char**);
static void mo_modreload(struct Client*, struct Client*, int, char**);
static void mo_modunload(struct Client*, struct Client*, int, char**);
static void mo_modrestart(struct Client*, struct Client*, int, char**);

struct Message modload_msgtab = {
 "MODLOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
  {m_unregistered, m_not_oper, m_ignore, mo_modload}
};

struct Message modunload_msgtab = {
 "MODUNLOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
  {m_unregistered, m_not_oper, m_ignore, mo_modunload}
};

struct Message modreload_msgtab = {
  "MODRELOAD", 0, 0, 2, 0, MFLG_SLOW, 0,
  {m_unregistered, m_not_oper, m_ignore, mo_modreload}
};

struct Message modlist_msgtab = {
 "MODLIST", 0, 0, 0, 0, MFLG_SLOW, 0,
  {m_unregistered, m_not_oper, m_ignore, mo_modlist}
};

struct Message modrestart_msgtab = {
 "MODRESTART", 0, 0, 0, 0, MFLG_SLOW, 0,
 {m_unregistered, m_not_oper, m_ignore, mo_modrestart}
};

extern struct Message error_msgtab;

void
modules_init(void)
{
	mod_add_cmd(&modload_msgtab);
	mod_add_cmd(&modunload_msgtab);
        mod_add_cmd(&modreload_msgtab);
	mod_add_cmd(&modlist_msgtab);
	mod_add_cmd(&modrestart_msgtab);
	mod_add_cmd(&error_msgtab);
}

/* mod_find_path()
 *
 * input	- path
 * output	- none
 * side effects - returns a module path from path
 */
static struct module_path *
mod_find_path(char *path)
{
  dlink_node *pathst = mod_paths.head;
  struct module_path *mpath;
  
  if (!pathst)
    return NULL;

  for (; pathst; pathst = pathst->next)
  {
    mpath = (struct module_path *)pathst->data;

    if (!strcmp(path, mpath->path))
      return mpath;
  }
  
  return NULL;
}

/* mod_add_path
 *
 * input	- path
 * ouput	- 
 * side effects - adds path to list
 */
void
mod_add_path(char *path)
{
  struct module_path *pathst;
  dlink_node *node;
  
  if (mod_find_path(path))
    return;

  pathst = MyMalloc (sizeof (struct module_path));
  node = make_dlink_node();
  
  strcpy(pathst->path, path);
  dlinkAdd(pathst, node, &mod_paths);
}

/* mod_clear_paths()
 *
 * input	-
 * output	-
 * side effects - clear the lists of paths
 */
void
mod_clear_paths(void)
{
  struct module_path *pathst;
  dlink_node *node, *next;

  for(node = mod_paths.head; node; node = next)
    {
      next = node->next;
      pathst = (struct module_path *)node->data;
      dlinkDelete(node, &mod_paths);
      free_dlink_node(node);
      MyFree(pathst);
    }
}

/* irc_basename
 *
 * input	-
 * output	-
 * side effects -
 */
char *
irc_basename(char *path)
{
  char *mod_basename = MyMalloc (strlen (path) + 1);
  char *s;

  if (!(s = strrchr (path, '/')))
    s = path;
  else
    s++;

  (void)strcpy (mod_basename, s);
  return mod_basename;
}

/* findmodule_byname
 *
 * input        -
 * output       -
 * side effects -
 */

int 
findmodule_byname (char *name)
{
  int i;

  for (i = 0; i < num_mods; i++) 
    {
      if (!irccmp (modlist[i]->name, name))
	return i;
    }
  return -1;
}

/* load_all_modules()
 *
 * input        -
 * output       -
 * side effects -
 */
void
load_all_modules (int warn)
{
  DIR            *system_module_dir = NULL;
  struct dirent  *ldirent = NULL;
  char            module_fq_name[PATH_MAX + 1];
  int             len;

  modules_init();
  
  modlist = (struct module **)MyMalloc ( sizeof (struct module) *
                                         (MODS_INCREMENT));

  max_mods = MODS_INCREMENT;

  system_module_dir = opendir (AUTOMODPATH);

  if (system_module_dir == NULL)
    {
      ilog (L_WARN, "Could not load modules from %s: %s",
	   AUTOMODPATH, strerror (errno));
      return;
    }

  while ((ldirent = readdir (system_module_dir)) != NULL)
    {
      len = strlen(ldirent->d_name);

      /* On HPUX, we have *.sl as shared library extension
       * -TimeMr14C */
      
      if ((len > 3) && 
          (ldirent->d_name[len-3] == '.') &&
          (ldirent->d_name[len-2] == 's') &&
          ((ldirent->d_name[len-1] == 'o') || (ldirent->d_name[len-1] == 'l')))
	{
	  (void)sprintf (module_fq_name, "%s/%s",  AUTOMODPATH,
			 ldirent->d_name);
	  (void)load_a_module (module_fq_name, warn, 0);
	}
    }

  (void)closedir (system_module_dir);
}

/* load_core_modules()
 *
 * input        -
 * output       -
 * side effects - core modules are loaded, if any fail, kill ircd
 */
void
load_core_modules(int warn)
{
  char module_name[MAXPATHLEN];
  int i, hpux = 0;

#ifdef HAVE_SHL_LOAD
  hpux = 1;
#endif

  for(i = 0; core_module_table[i]; i++)
  {
    sprintf(module_name, "%s/%s%c",
            MODPATH, core_module_table[i], hpux ? 'l' : 'o');
	    
    if(load_a_module(module_name, warn, 1) == -1)
    {
      ilog(L_CRIT, "Error loading core module %s%c: terminating ircd", 
           core_module_table[i], hpux ? 'l' : 'o');
      exit(0);
    }
  }
}

/* load_one_module()
 *
 * input        -
 * output       -
 * side effects -
 */
int
load_one_module (char *path, int coremodule)
{
  char modpath[MAXPATHLEN];
  dlink_node *pathst;
  struct module_path *mpath;
	
  struct stat statbuf;
  
  for (pathst = mod_paths.head; pathst; pathst = pathst->next)
    {
      mpath = (struct module_path *)pathst->data;
      
      sprintf(modpath, "%s/%s", mpath->path, path);
      if((strstr(modpath, "../") == NULL) && (strstr(modpath, "/..") == NULL)) 
	 {
	    if (stat(modpath, &statbuf) == 0)
	      {
		 if(S_ISREG(statbuf.st_mode))
		    {
		       /* Regular files only please */
		       if (coremodule)
		         return load_a_module(modpath, 1, 1);
		       else
		         return load_a_module(modpath, 1, 0);
		    }
	      }
	    
	 }
    }
   
  sendto_realops_flags(FLAGS_ALL, L_ALL,
                       "Cannot locate module %s", path);
  ilog(L_WARN, "Cannot locate module %s", path);
  return -1;
}
		

/* load a module .. */
static void
mo_modload (struct Client *client_p, struct Client *source_p, int parc, char **parv)
{
  char *m_bn;

  if (!IsOperAdmin(source_p))
  {
    sendto_one(source_p, ":%s NOTICE %s :You have no A flag", me.name, parv[0]);
    return;
  }

  m_bn = irc_basename (parv[1]);

  if (findmodule_byname (m_bn) != -1)
  {
    sendto_one (source_p, ":%s NOTICE %s :Module %s is already loaded",
                me.name, source_p->name, m_bn);
    MyFree(m_bn);
    return;
  }

  load_one_module (parv[1], 0);

  MyFree(m_bn);
}


/* unload a module .. */
static void
mo_modunload (struct Client *client_p, struct Client *source_p, int parc, char **parv)
{
  char *m_bn;
  int modindex;

  if (!IsOperAdmin (source_p))
  {
    sendto_one (source_p, ":%s NOTICE %s :You have no A flag",
                me.name, parv[0]);
    return;
  }

  m_bn = irc_basename (parv[1]);

  if((modindex = findmodule_byname (m_bn)) == -1)
  {
    sendto_one (source_p, ":%s NOTICE %s :Module %s is not loaded",
                me.name, source_p->name, m_bn);
    MyFree (m_bn);
    return;
  }

  if(modlist[modindex]->core == 1)
  {
    sendto_one(source_p, 
               ":%s NOTICE %s :Module %s is a core module and may not be unloaded",
	       me.name, source_p->name, m_bn);
    MyFree(m_bn);
    return;
  }

  if(unload_one_module (m_bn, 1) == -1)
  {
    sendto_one (source_p, ":%s NOTICE %s :Module %s is not loaded",
                me.name, source_p->name, m_bn);
  }
  MyFree (m_bn);
}

/* unload and load in one! */
static void
mo_modreload (struct Client *client_p, struct Client *source_p, int parc, char **parv)
{
  char *m_bn;
  int modindex;
  int check_core;

  if (!IsOperAdmin (source_p))
    {
      sendto_one (source_p, ":%s NOTICE %s :You have no A flag",
                  me.name, parv[0]);
      return;
    }

  m_bn = irc_basename (parv[1]);

  if((modindex = findmodule_byname(m_bn)) == -1)
    {
      sendto_one (source_p, ":%s NOTICE %s :Module %s is not loaded",
                  me.name, source_p->name, m_bn);
      MyFree (m_bn);
      return;
    }

  check_core = modlist[modindex]->core;

  if(unload_one_module (m_bn, 1) == -1)
    {
      sendto_one (source_p, ":%s NOTICE %s :Module %s is not loaded",
                  me.name, source_p->name, m_bn);
      MyFree (m_bn);
      return;
    }

  if((load_one_module(parv[1], check_core) == -1) && check_core)
  {
    sendto_realops_flags(FLAGS_ALL|FLAGS_REMOTE, L_ALL,
                         "Error reloading core module: %s: terminating ircd",
			 parv[1]);
    ilog(L_CRIT, "Error loading core module %s: terminating ircd", parv[1]);
    exit(0);
  }

  MyFree(m_bn);
}

/* list modules .. */
static void
mo_modlist (struct Client *client_p, struct Client *source_p, int parc, char **parv)
{
  int i;

  if (!IsOperAdmin (source_p))
  {
    sendto_one (source_p, ":%s NOTICE %s :You have no A flag",
                me.name, parv[0]);
    return;
  }

  for(i = 0; i < num_mods; i++ )
  {
    if(parc>1)
    {
      if(match(parv[1],modlist[i]->name))
      {
        sendto_one(source_p, form_str(RPL_MODLIST), me.name, parv[0],
                   modlist[i]->name, modlist[i]->address,
                   modlist[i]->version, modlist[i]->core?"(core)":"");
      }
    }
    else
    {
      sendto_one(source_p, form_str(RPL_MODLIST), me.name, parv[0],
                 modlist[i]->name, modlist[i]->address,
                 modlist[i]->version, modlist[i]->core?"(core)":"");
    }
  }
  
  sendto_one(source_p, form_str(RPL_ENDOFMODLIST), me.name, parv[0]);
}

/* unload and reload all modules */
static void
mo_modrestart (struct Client *client_p, struct Client *source_p, int parc, char **parv)

{
  int modnum;

  if (!IsOperAdmin (source_p))
  {
    sendto_one (source_p, ":%s NOTICE %s :You have no A flag",
                me.name, parv[0]);
    return;
  }

  sendto_one(source_p, ":%s NOTICE %s :Reloading all modules",
             me.name, parv[0]);

  modnum = num_mods;
  while (num_mods)
     unload_one_module(modlist[0]->name, 0);

  load_all_modules(0);
  load_core_modules(0);
  rehash(0);
  
  sendto_realops_flags(FLAGS_ALL, L_ALL,
              "Module Restart: %d modules unloaded, %d modules loaded",
			modnum, num_mods);
  ilog(L_WARN, "Module Restart: %d modules unloaded, %d modules loaded",
      modnum, num_mods);
}

#else /* STATIC_MODULES */

/* load_all_modules()
 *
 * input        -
 * output       -
 * side effects - all the msgtabs are added for static modules
 */
void
load_all_modules(int warn)
{
	mod_add_cmd(&error_msgtab);
	mod_add_cmd(&accept_msgtab);
	mod_add_cmd(&admin_msgtab);
	mod_add_cmd(&away_msgtab);
	mod_add_cmd(&capab_msgtab);
	mod_add_cmd(&cburst_msgtab);
	mod_add_cmd(&client_msgtab);
	mod_add_cmd(&close_msgtab);
	mod_add_cmd(&connect_msgtab);
#ifdef HAVE_LIBCRYPTO
	mod_add_cmd(&challenge_msgtab);
        mod_add_cmd(&cryptlink_msgtab);
#endif
        mod_add_cmd(&die_msgtab);
	mod_add_cmd(&dmem_msgtab);
	mod_add_cmd(&drop_msgtab);
	mod_add_cmd(&eob_msgtab);
	mod_add_cmd(&gline_msgtab);
	mod_add_cmd(&help_msgtab);
	mod_add_cmd(&info_msgtab);
	mod_add_cmd(&invite_msgtab);
	mod_add_cmd(&ison_msgtab);
	mod_add_cmd(&join_msgtab);
	mod_add_cmd(&kick_msgtab);
	mod_add_cmd(&kill_msgtab);
	mod_add_cmd(&kline_msgtab);
	mod_add_cmd(&dline_msgtab);
	mod_add_cmd(&knock_msgtab);
	mod_add_cmd(&knockll_msgtab);
	mod_add_cmd(&links_msgtab);
	mod_add_cmd(&list_msgtab);
	mod_add_cmd(&lljoin_msgtab);
	mod_add_cmd(&llnick_msgtab);
	mod_add_cmd(&locops_msgtab);
	mod_add_cmd(&lusers_msgtab);
	mod_add_cmd(&privmsg_msgtab);
	mod_add_cmd(&notice_msgtab);
	mod_add_cmd(&mode_msgtab);
	mod_add_cmd(&motd_msgtab);
	mod_add_cmd(&names_msgtab);
	mod_add_cmd(&nburst_msgtab);
	mod_add_cmd(&nick_msgtab);
	mod_add_cmd(&oper_msgtab);
	mod_add_cmd(&operwall_msgtab);
	mod_add_cmd(&part_msgtab);
	mod_add_cmd(&pass_msgtab);
	mod_add_cmd(&ping_msgtab);
	mod_add_cmd(&pong_msgtab);
	mod_add_cmd(&post_msgtab);
	mod_add_cmd(&get_msgtab);
        mod_add_cmd(&put_msgtab);
	mod_add_cmd(&quit_msgtab);
	mod_add_cmd(&rehash_msgtab);
	mod_add_cmd(&restart_msgtab);
	mod_add_cmd(&resv_msgtab);  
	mod_add_cmd(&server_msgtab);
	mod_add_cmd(&set_msgtab);
	mod_add_cmd(&sjoin_msgtab);
	mod_add_cmd(&squit_msgtab);
	mod_add_cmd(&stats_msgtab);
	mod_add_cmd(&svinfo_msgtab);
	mod_add_cmd(&testline_msgtab);
	mod_add_cmd(&time_msgtab);
	mod_add_cmd(&topic_msgtab);
	mod_add_cmd(&trace_msgtab);
	mod_add_cmd(&msgtabs[0]);
	mod_add_cmd(&msgtabs[1]);
	mod_add_cmd(&msgtabs[2]);
	mod_add_cmd(&unresv_msgtab);
	mod_add_cmd(&user_msgtab);
	mod_add_cmd(&userhost_msgtab);
	mod_add_cmd(&users_msgtab);
	mod_add_cmd(&version_msgtab);
	mod_add_cmd(&wallops_msgtab);
	mod_add_cmd(&who_msgtab);
	mod_add_cmd(&whois_msgtab);
	mod_add_cmd(&whowas_msgtab);
}

#endif /* STATIC_MODULES */