Debian Codegraph @

modules/operserv/greplog.c

raw
1 /*
2 * Copyright (c) 2007-2008 Jilles Tjoelker
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * Searches through the logs.
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "operserv/greplog", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 static void os_cmd_greplog(sourceinfo_t *si, int parc, char *parv[]);
19
20 command_t os_greplog = { "GREPLOG", N_("Searches through the logs."), PRIV_CHAN_AUSPEX, 3, os_cmd_greplog, { .path = "oservice/greplog" } };
21
22 void _modinit(module_t *m)
23 {
24 service_named_bind_command("operserv", &os_greplog);
25 }
26
27 void _moddeinit(module_unload_intent_t intent)
28 {
29 service_named_unbind_command("operserv", &os_greplog);
30 }
31
32 #define MAXMATCHES 100
33
34 static const char *get_logfile(const unsigned int *masks)
35 {
36 logfile_t *lf;
37 int i;
38
39 for (i = 0; masks[i] != 0; i++)
40 {
41 lf = logfile_find_mask(masks[i]);
42 if (lf != NULL)
43 return lf->log_path;
44 }
45 return NULL;
46 }
47
48 static const char *get_commands_log(void)
49 {
50 const unsigned int masks[] = {
51 LG_CMD_ALL,
52 LG_CMD_ADMIN | LG_CMD_REGISTER | LG_CMD_SET | LG_CMD_DO | LG_CMD_LOGIN,
53 LG_CMD_ADMIN | LG_CMD_REGISTER | LG_CMD_SET | LG_CMD_DO,
54 LG_CMD_ADMIN | LG_CMD_REGISTER | LG_CMD_SET,
55 LG_CMD_ADMIN | LG_CMD_REGISTER,
56 LG_CMD_ADMIN,
57 0
58 };
59 return get_logfile(masks);
60 }
61
62 static const char *get_account_log(void)
63 {
64 const unsigned int masks[] = {
65 LG_CMD_REGISTER | LG_CMD_SET | LG_REGISTER,
66 LG_CMD_REGISTER | LG_REGISTER,
67 0
68 };
69 return get_logfile(masks);
70 }
71
72 /* GREPLOG <service> <mask> */
73 static void os_cmd_greplog(sourceinfo_t *si, int parc, char *parv[])
74 {
75 const char *service, *pattern, *baselog;
76 int maxdays, matches = -1, matches1, day, days, lines, linesv;
77 FILE *in;
78 char str[1024];
79 char *p, *q;
80 char logfile[256];
81 time_t t;
82 struct tm tm;
83 mowgli_list_t loglines = { NULL, NULL, 0 };
84 mowgli_node_t *n, *tn;
85
86 /* require user, channel and server auspex
87 * (channel auspex checked via in command_t)
88 */
89 if (!has_priv(si, PRIV_USER_AUSPEX))
90 {
91 command_fail(si, fault_noprivs, STR_NO_PRIVILEGE, PRIV_USER_AUSPEX);
92 return;
93 }
94 if (!has_priv(si, PRIV_SERVER_AUSPEX))
95 {
96 command_fail(si, fault_noprivs, STR_NO_PRIVILEGE, PRIV_SERVER_AUSPEX);
97 return;
98 }
99
100 if (parc < 2)
101 {
102 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "GREPLOG");
103 command_fail(si, fault_needmoreparams, _("Syntax: GREPLOG <service> <pattern> [days]"));
104 return;
105 }
106
107 service = parv[0];
108 pattern = parv[1];
109
110 if (parc >= 3)
111 {
112 days = atoi(parv[2]);
113 maxdays = !strcmp(service, "*") ? 120 : 30;
114 if (days < 0 || days > maxdays)
115 {
116 command_fail(si, fault_badparams, _("Too many days, maximum is %d."), maxdays);
117 return;
118 }
119 }
120 else
121 days = 0;
122
123 baselog = !strcmp(service, "*") ? get_account_log() : get_commands_log();
124 if (baselog == NULL)
125 {
126 command_fail(si, fault_badparams, _("There is no log file matching your request."));
127 return;
128 }
129
130 for (day = 0; day <= days; day++)
131 {
132 if (day == 0)
133 mowgli_strlcpy(logfile, baselog, sizeof logfile);
134 else
135 {
136 t = CURRTIME - day * 86400;
137 tm = *localtime(&t);
138 snprintf(logfile, sizeof logfile, "%s.%04u%02u%02u",
139 baselog, tm.tm_year + 1900,
140 tm.tm_mon + 1, tm.tm_mday);
141 }
142 in = fopen(logfile, "r");
143 if (in == NULL)
144 {
145 command_success_nodata(si, "Failed to open log file %s", logfile);
146 continue;
147 }
148 if (matches == -1)
149 matches = 0;
150 matches1 = matches;
151 lines = linesv = 0;
152 while (fgets(str, sizeof str, in) != NULL)
153 {
154 p = strchr(str, '\n');
155 if (p != NULL)
156 *p = '\0';
157 lines++;
158 p = *str == '[' ? strchr(str, ']') : NULL;
159 if (p == NULL)
160 continue;
161 p++;
162 if (*p++ != ' ')
163 continue;
164 q = strchr(p, ' ');
165 if (q == NULL)
166 continue;
167 linesv++;
168 *q = '\0';
169 if (strcmp(service, "*") && strcasecmp(service, p))
170 continue;
171 *q++ = ' ';
172 if (match(pattern, q))
173 continue;
174 matches++;
175 mowgli_node_add_head(sstrdup(str), mowgli_node_create(), &loglines);
176 if (matches > MAXMATCHES)
177 {
178 n = loglines.tail;
179 mowgli_node_delete(n, &loglines);
180 free(n->data);
181 mowgli_node_free(n);
182 }
183 }
184 fclose(in);
185 matches = matches1;
186 MOWGLI_ITER_FOREACH_SAFE(n, tn, loglines.head)
187 {
188 p = n->data;
189 matches++;
190 command_success_nodata(si, "[%d] %s", matches, p);
191 mowgli_node_delete(n, &loglines);
192 free(n->data);
193 mowgli_node_free(n);
194 }
195 if (matches == 0 && lines > linesv && lines > 0)
196 command_success_nodata(si, "Log file may be corrupted, %d/%d unexpected lines", lines - linesv, lines);
197 if (matches >= MAXMATCHES)
198 {
199 command_success_nodata(si, "Too many matches, halting search");
200 break;
201 }
202 }
203
204 logcommand(si, CMDLOG_ADMIN, "GREPLOG: \2%s\2 \2%s\2 (\2%d\2 matches)", service, pattern, matches);
205 if (matches == 0)
206 command_success_nodata(si, _("No lines matched pattern \2%s\2"), pattern);
207 else if (matches > 0)
208 command_success_nodata(si, ngettext(N_("\2%d\2 match for pattern \2%s\2"),
209 N_("\2%d\2 matches for pattern \2%s\2"), matches), matches, pattern);
210 }
211
212 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
213 * vim:ts=8
214 * vim:sw=8
215 * vim:noexpandtab
216 */