aboutsummaryrefslogtreecommitdiffstats
path: root/src/opercmd.c
blob: 0cae2ad021bc7a4ba88c0454199a4b1c1e6c6d03 (plain) (blame)
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
/*
 *  Copyright (c) 2002 Erik Fears
 *  Copyright (c) 2014-2018 ircd-hybrid development team
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 *  USA
 */

#include "setup.h"

#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>

#include "list.h"
#include "options.h"
#include "irc.h"
#include "log.h"
#include "main.h"
#include "config.h"
#include "opercmd.h"
#include "scan.h"
#include "memory.h"
#include "stats.h"


static list_t COMMANDS;  /* List of active commands */


/* cmd_check
 *
 *    Start a manual scan on given IP address/hostname.
 *
 * Parameters:
 *    param: Parameters of the command
 *    target: channel command was sent to
 *
 */
static void
cmd_check(char *param, const char *target)
{
  scan_manual(param, target);
}

/* cmd_stat
 *
 *   Send output of stats to channel.
 *
 * Parameters:
 *    param: Parameters of the command
 *    target: channel command was sent to
 */
static void
cmd_stats(char *param, const char *target)
{
  stats_output(target);
}

/* cmd_fdstat
 *
 *   Send output of stats to channel.
 *
 * Parameters:
 *    param: Parameters of the command
 *    target: channel command was sent to
 */
static void
cmd_fdstat(char *param, const char *target)
{
  fdstats_output(target);
}

/* command_create
 *
 *    Create a Command struct.
 *
 * Parameters:
 *    type: Index in COMMAND_TABLE
 *    param: Parameters to the command (NULL if there are not any)
 *    irc_nick: Nickname of user that initiated the command
 *    target: Target channel (target is ALWAYS a channel)
 *
 * Return:
 *    Pointer to new Command
 */
static struct Command *
command_create(const struct OperCommandHash *tab, const char *param, const char *irc_nick,
               const char *target)
{
  struct Command *command = xcalloc(sizeof(*command));

  if (param)
    command->param = xstrdup(param);

  command->tab = tab;
  command->irc_nick = xstrdup(irc_nick);
  command->target = target;

  time(&command->added);

  return command;
}

/* command_free
 *
 *   Free a command struct
 *
 * Parameters:
 *   command: Command struct to free
 *
 * Return: NONE
 */
static void
command_free(struct Command *command)
{
  if (command->param)
    xfree(command->param);

  xfree(command->irc_nick);
  xfree(command);
}

/* command_parse
 *
 *    Parse a command to hopm (sent to a channel hopm is on). The command is parsed
 *    from the parameters, and if it is a known command it is stored in a queue. A
 *    userhost is performed on the user to check if they are an IRC operator. When
 *    a reply is returned (command_userhost), the command will be executed.
 *
 * Parameters:
 *    command: Command sent (including parameters)
 *    target: Channel command was sent to (we only got this far if there was only one recipient)
 *    source_p: Operator (hopefully) that sent the command.
 *
 */
void
command_parse(const char *command, const char *target, const char *source_p)
{
  char *param;  /* Parsed parameters */
  static const struct OperCommandHash COMMAND_TABLE[] =
  {
    { .command = "CHECK",  .handler = cmd_check  },
    { .command = "SCAN",   .handler = cmd_check  },
    { .command = "STATS",  .handler = cmd_stats  },
    { .command = "FDSTAT", .handler = cmd_fdstat },
    { .command = NULL }
  };

  if (OPT_DEBUG)
    log_printf("COMMAND -> Parsing command (%s) from %s [%s]",
               command, source_p, target);

  /* Only allow OptionsItem.command_queue_size commands in the queue */
  if (LIST_SIZE(&COMMANDS) >= OptionsItem.command_queue_size)
    return;

  /*
   * Parameter is the first character in command after the first space.
   * 'param' will be NULL if:
   * 1. There was no space
   * 2. There was a space but it was the last character in command, in which case
   *    param = '\0'
   */

  /* Skip past the botname/!all */
  command = strchr(command, ' ');

  /*
   * There is no command OR there is at least nothing
   * past that first space.
   */
  if (command == NULL || *++command == '\0')
    return;

  /* Find the parameters */
  param = strchr(command, ' ');

  if (param)
  {
    *param = '\0';
    param++;
  }

  log_printf("COMMAND -> parsed [%s] [%s]", command, param ? param : "");

  /* Lookup the command in the table */
  for (const struct OperCommandHash *tab = COMMAND_TABLE; tab->command; ++tab)
  {
    if (strcasecmp(command, tab->command) == 0)
    {
      /* Queue this command */
      struct Command *cmd = command_create(tab, param, source_p, target);

      list_add(cmd, &cmd->node, &COMMANDS);
      break;
    }
  }

  irc_send("USERHOST %s", source_p);
}

/* command_timer
 *
 *    Perform ~1 second actions.
 *
 * Parameters: NONE
 *
 * Return: NONE
 *
 */
void
command_timer(void)
{
  static unsigned int interval;
  node_t *node, *node_next;
  time_t present;

  /* Only perform command removal every OptionsItem.command_interval seconds */
  if (interval++ < OptionsItem.command_interval)
    return;
  else
    interval = 0;

  time(&present);

  LIST_FOREACH_SAFE(node, node_next, COMMANDS.head)
  {
    struct Command *command = node->data;

    if ((present - command->added) > OptionsItem.command_timeout)
    {
      list_remove(&command->node, &COMMANDS);
      command_free(command);  /* Cleanup the command */
    }
    else  /* Since the queue is in order, it's also ordered by time, no nodes after this will be timed out */
      return;
  }
}

/* command_userhost
 *
 *    A 302 reply was received. The reply is parsed to check if the
 *    user was an operator. If so any commands they had queued are
 *    executed.
 *
 * Parameters:
 *    reply: Reply to USERHOST    (ex: :grifferz*=+goats@pc-62-30-219-54-pb.blueyonder.co.uk)
 *
 * Return: NONE
 *
 */
void
command_userhost(const char *reply)
{
  node_t *node, *node_next;
  char *tmp;
  int oper = 0;

  tmp = strchr(reply, '=');

  /* They quit, ignore it */
  if (tmp == NULL)
    return;

  /* Operators have a * flag in a USERHOST reply */
  if (*(tmp - 1) == '*')
    oper = 1;

  /* Null terminate it so tmp = the oper's nick */
  if (oper)
    *(--tmp) = '\0';
  else
    *(tmp) = '\0';

  /* Find any queued commands that match this user */
  LIST_FOREACH_SAFE(node, node_next, COMMANDS.head)
  {
    struct Command *command = node->data;

    if (strcmp(command->irc_nick, reply) == 0)
    {
      if (oper)
        command->tab->handler(command->param, command->target);

      list_remove(&command->node, &COMMANDS);
      command_free(command);  /* Cleanup the command */
    }
  }
}