source: code/icb.c@ 626f420

Last change on this file since 626f420 was 626f420, checked in by Mike Belopuhov <mike@…>, 11 years ago

Add icb_vis to escape '%' chars and do some other sanitizing

  • Property mode set to 100644
File size: 14.4 KB
Line 
1/*
2 * Copyright (c) 2009, 2010, 2013, 2014 Mike Belopuhov
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <sys/param.h>
18#include <sys/queue.h>
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <syslog.h>
24#include <unistd.h>
25#include <ctype.h>
26#include <event.h>
27
28#include "icb.h"
29#include "icbd.h"
30
31extern int creategroups;
32extern char srvname[MAXHOSTNAMELEN];
33
34void icb_command(struct icb_session *, char *, char *);
35void icb_groupmsg(struct icb_session *, char *);
36void icb_login(struct icb_session *, char *, char *, char *);
37int icb_dowho(struct icb_session *, struct icb_group *);
38int icb_modpermit(struct icb_session *);
39char *icb_nextfield(char **);
40
41/*
42 * icb_init: initializes pointers to callbacks
43 */
44void
45icb_init(struct icbd_callbacks *ic)
46{
47 icb_drop = ic->drop;
48 icb_log = ic->log;
49 icb_send = ic->send;
50
51 LIST_INIT(&groups);
52
53 if (strlen(srvname) == 0)
54 (void)gethostname(srvname, sizeof srvname);
55 /*
56 * MAXHOSTNAMELEN is usually greater than what we
57 * can actually send, hence truncation:
58 */
59 if (strlen(srvname) > 200)
60 srvname[200] = '\0';
61}
62
63/*
64 * icb_start: called upon accepting a new connection, greets new client
65 */
66void
67icb_start(struct icb_session *is)
68{
69 icb_sendfmt(is, "%c%c%c%s%c%s", ICB_M_PROTO, '1', ICB_M_SEP, srvname,
70 ICB_M_SEP, "icbd");
71 SETF(is->flags, ICB_SF_PROTOSENT);
72}
73
74/*
75 * icb_input: main input processing routine
76 */
77void
78icb_input(struct icb_session *is)
79{
80 char *msg = is->buffer;
81 char type;
82
83 is->last = getmonotime();
84 type = msg[1];
85 msg += 2;
86 if (!ISSETF(is->flags, ICB_SF_LOGGEDIN) && type != ICB_M_LOGIN) {
87 icb_error(is, "Not logged in");
88 return;
89 }
90 switch (type) {
91 case ICB_M_LOGIN: {
92 char *nick, *group, *client, *cmd;
93
94 client = icb_nextfield(&msg);
95 nick = icb_nextfield(&msg);
96 group = icb_nextfield(&msg);
97 cmd = icb_nextfield(&msg);
98 if (strlen(cmd) > 0 && cmd[0] == 'w') {
99 icb_error(is, "Command not implemented");
100 icb_drop(is, NULL);
101 return;
102 }
103 if (strlen(cmd) == 0 || strcmp(cmd, "login") != 0) {
104 icb_error(is, "Malformed login packet");
105 icb_drop(is, NULL);
106 return;
107 }
108 icb_login(is, group, nick, client);
109 break;
110 }
111 case ICB_M_OPEN: {
112 char *grpmsg;
113
114 grpmsg = icb_nextfield(&msg);
115 icb_groupmsg(is, grpmsg);
116 break;
117 }
118 case ICB_M_COMMAND: {
119 char *cmd, *arg;
120
121 cmd = icb_nextfield(&msg);
122 arg = icb_nextfield(&msg);
123 icb_command(is, cmd, arg);
124 break;
125 }
126 case ICB_M_PONG: {
127 icb_sendfmt(is, "%c", ICB_M_PING);
128 break;
129 }
130 case ICB_M_PROTO:
131 case ICB_M_NOOP:
132 /* ignore */
133 break;
134 default:
135 /* everything else is not valid */
136 icb_error(is, "Bummer. This is a bummer, man.");
137 }
138}
139
140/*
141 * icb_login: handles login ('a') packets
142 */
143void
144icb_login(struct icb_session *is, char *grp, char *nick, char *client)
145{
146 char *defgrp = "1";
147 struct icb_group *ig;
148 struct icb_session *s;
149 char group[ICB_MAXGRPLEN];
150
151 if (!nick || strlen(nick) == 0 ||
152 icb_vis(is->nick, nick, ICB_MAXNICKLEN)) {
153 icb_error(is, "Invalid nick");
154 icb_drop(is, NULL);
155 return;
156 }
157 if (!grp || strlen(grp) == 0)
158 strlcpy(group, defgrp, ICB_MAXGRPLEN);
159 else
160 icb_vis(group, grp, ICB_MAXNICKLEN);
161 LIST_FOREACH(ig, &groups, entry) {
162 if (strcmp(ig->name, group) == 0)
163 break;
164 }
165 if (ig == NULL) {
166 if (!creategroups) {
167 icb_error(is, "Invalid group %s", group);
168 icb_drop(is, NULL);
169 return;
170 } else {
171 if ((ig = icb_addgroup(is, group, NULL)) == NULL) {
172 icb_error(is, "Can't create group %s", group);
173 return;
174 }
175 icb_log(NULL, LOG_DEBUG, "%s created group %s",
176 is->nick, group);
177 }
178 }
179 LIST_FOREACH(s, &ig->sess, entry) {
180 if (strcmp(s->nick, is->nick) == 0) {
181 icb_error(is, "Nick is already in use");
182 icb_drop(is, NULL);
183 return;
184 }
185 }
186
187 if (client && strlen(client) > 0)
188 icb_vis(is->client, client, sizeof is->client);
189 strlcpy(is->nick, nick, sizeof is->nick);
190 is->group = ig;
191 is->login = time(NULL);
192 is->last = getmonotime();
193
194 /* notify group */
195 icb_status_group(ig, NULL, STATUS_SIGNON, "%s (%s@%s) entered group",
196 is->nick, is->client, is->host);
197
198 CLRF(is->flags, ICB_SF_PROTOSENT);
199 SETF(is->flags, ICB_SF_LOGGEDIN);
200
201 LIST_INSERT_HEAD(&ig->sess, is, entry);
202
203 /* acknowledge successful login */
204 icb_sendfmt(is, "%c", ICB_M_LOGIN);
205
206 /* notify user */
207 icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
208 icb_ismod(ig, is) ? " as moderator" : "");
209
210 /* send user a topic name */
211 if (strlen(ig->topic) > 0)
212 icb_status(is, STATUS_TOPIC, "Topic for %s is \"%s\"",
213 ig->name, ig->topic);
214}
215
216/*
217 * icb_groupmsg: handles open message ('b') packets
218 */
219void
220icb_groupmsg(struct icb_session *is, char *msg)
221{
222 char buf[ICB_MSGSIZE];
223 struct icb_group *ig = is->group;
224 struct icb_session *s;
225 int buflen = 1;
226
227 if (strlen(msg) == 0) {
228 icb_error(is, "Empty message");
229 return;
230 }
231
232 buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
233 is->nick, ICB_M_SEP, msg);
234 buf[0] = buflen;
235
236 LIST_FOREACH(s, &ig->sess, entry) {
237 if (s == is)
238 continue;
239 icb_send(s, buf, buflen + 1);
240 }
241}
242
243/*
244 * icb_privmsg: handles personal message ('c') packets
245 */
246void
247icb_privmsg(struct icb_session *is, char *to, char *msg)
248{
249 struct icb_group *ig = is->group;
250 struct icb_session *s;
251 char whom[ICB_MAXNICKLEN];
252
253 icb_vis(whom, to, ICB_MAXNICKLEN);
254
255 LIST_FOREACH(s, &ig->sess, entry) {
256 if (strcmp(s->nick, whom) == 0)
257 break;
258 }
259 if (!s) {
260 icb_error(is, "No such user %s", whom);
261 return;
262 }
263 icb_sendfmt(s, "%c%s%c%s", ICB_M_PERSONAL, is->nick, ICB_M_SEP, msg);
264}
265
266/*
267 * icb_command: handles command ('h') packets
268 */
269void
270icb_command(struct icb_session *is, char *cmd, char *arg)
271{
272 void (*handler)(struct icb_session *, char *);
273 char command[32]; /* XXX */
274
275 icb_vis(command, cmd, sizeof command);
276
277 if ((handler = icb_cmd_lookup(command)) == NULL) {
278 icb_error(is, "Unsupported command: %s", command);
279 return;
280 }
281 handler(is, arg);
282}
283
284/*
285 * icb_cmdout: sends out command output ('i') packets, called from the
286 * command handlers
287 */
288void
289icb_cmdout(struct icb_session *is, int type, char *outmsg)
290{
291 char *otype = NULL;
292
293 switch (type) {
294 case CMDOUT_CO:
295 otype = "co";
296 break;
297 case CMDOUT_EC:
298 otype = "ec";
299 break;
300 case CMDOUT_WG:
301 otype = "wg";
302 break;
303 case CMDOUT_WH:
304 otype = "wh";
305 break;
306 case CMDOUT_WL:
307 otype = "wl";
308 break;
309 default:
310 icb_log(is, LOG_ERR, "unknown cmdout type");
311 return;
312 }
313 if (outmsg)
314 icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
315 outmsg);
316 else
317 icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
318}
319
320/*
321 * icb_status: sends a status message ('d') to the client
322 */
323void
324icb_status(struct icb_session *is, int type, const char *fmt, ...)
325{
326 va_list ap;
327 char buf[ICB_MSGSIZE];
328 int buflen = 1;
329 static const struct {
330 int type;
331 const char *msg;
332 } msgtab[] = {
333 { STATUS_ARRIVE, "Arrive" },
334 { STATUS_BOOT, "Boot" },
335 { STATUS_DEPART, "Depart" },
336 { STATUS_HELP, "Help" },
337 { STATUS_NAME, "Name" },
338 { STATUS_NOBEEP, "No-Beep" },
339 { STATUS_NOTIFY, "Notify" },
340 { STATUS_SIGNON, "Sign-on" },
341 { STATUS_SIGNOFF, "Sign-off" },
342 { STATUS_STATUS, "Status" },
343 { STATUS_TOPIC, "Topic" },
344 { STATUS_WARNING, "Warning" },
345 { 0, NULL }
346 };
347
348 if (type < 0 || type > (int)nitems(msgtab) - 1)
349 return;
350 va_start(ap, fmt);
351 buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
352 msgtab[type].msg, ICB_M_SEP);
353 buflen += vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
354 buf[0] = buflen;
355 va_end(ap);
356 icb_send(is, buf, buflen + 1);
357}
358
359/*
360 * icb_status: sends a status message ('d') to the group except of the
361 * "ex" if it's not NULL
362 */
363void
364icb_status_group(struct icb_group *ig, struct icb_session *ex, int type,
365 const char *fmt, ...)
366{
367 char buf[ICB_MSGSIZE];
368 va_list ap;
369 struct icb_session *s;
370
371 va_start(ap, fmt);
372 (void)vsnprintf(buf, sizeof buf, fmt, ap);
373 LIST_FOREACH(s, &ig->sess, entry) {
374 if (ex && s == ex)
375 continue;
376 icb_status(s, type, buf);
377 }
378 icb_log(NULL, LOG_DEBUG, "%s", buf);
379 va_end(ap);
380}
381
382/*
383 * icb_error: sends an error message ('e') to the client
384 */
385void
386icb_error(struct icb_session *is, const char *fmt, ...)
387{
388 char buf[ICB_MSGSIZE];
389 va_list ap;
390 int buflen = 1;
391
392 va_start(ap, fmt);
393 buflen += vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
394 va_end(ap);
395 buf[0] = ++buflen; /* account for ICB_M_ERROR */
396 buf[1] = ICB_M_ERROR;
397 icb_send(is, buf, buflen + 1);
398 icb_log(is, LOG_DEBUG, "%s", buf + 2);
399}
400
401/*
402 * icb_remove: removes a session from the associated group
403 */
404void
405icb_remove(struct icb_session *is, char *reason)
406{
407 if (is->group) {
408 if (icb_ismod(is->group, is))
409 (void)icb_pass(is->group, is, NULL);
410 LIST_REMOVE(is, entry);
411 if (reason)
412 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
413 "%s (%s@%s) just left: %s", is->nick, is->client,
414 is->host, reason);
415 else
416 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
417 "%s (%s@%s) just left", is->nick, is->client,
418 is->host);
419 }
420}
421
422/*
423 * icb_addgroup: adds a new group to the list
424 */
425struct icb_group *
426icb_addgroup(struct icb_session *is, char *name, char *mpass)
427{
428 struct icb_group *ig;
429
430 if ((ig = calloc(1, sizeof *ig)) == NULL)
431 return (NULL);
432 strlcpy(ig->name, name, sizeof ig->name);
433 if (mpass)
434 strlcpy(ig->mpass, mpass, sizeof ig->mpass);
435 if (is)
436 ig->mod = is;
437 LIST_INIT(&ig->sess);
438 LIST_INSERT_HEAD(&groups, ig, entry);
439 return (ig);
440}
441
442#ifdef notused
443/*
444 * icb_delgroup: removes a group from the list
445 */
446void
447icb_delgroup(struct icb_group *ig)
448{
449 struct icb_session *s;
450
451 /* well, i guess we should kick out participants! ;-) */
452 LIST_FOREACH(s, &ig->sess, entry) {
453 icb_status(s, STATUS_WARNING, "Group dismissed");
454 s->group = NULL;
455 }
456 LIST_REMOVE(ig, entry);
457 bzero(ig, sizeof ig); /* paranoic thing, obviously */
458 free(ig);
459}
460#endif
461
462/*
463 * icb_dowho: a helper function that sends out a group header as a command
464 * output and user information in the "wl" format
465 */
466int
467icb_dowho(struct icb_session *is, struct icb_group *ig)
468{
469 char buf[ICB_MSGSIZE];
470 struct icb_session *s;
471 int nusers = 0;
472
473 icb_cmdout(is, CMDOUT_CO, " ");
474 snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
475 ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
476 strlen(ig->topic) > 0 ? ig->topic : "(None)");
477 icb_cmdout(is, CMDOUT_CO, buf);
478 LIST_FOREACH(s, &ig->sess, entry) {
479 (void)snprintf(buf, sizeof buf,
480 "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
481 icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
482 s->nick, ICB_M_SEP, getmonotime() - s->last,
483 ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
484 s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
485 icb_cmdout(is, CMDOUT_WL, buf);
486 nusers++;
487 }
488 return (nusers);
489}
490
491/*
492 * icb_who: sends a list of users of either the specified group or all
493 * groups found on the server
494 */
495void
496icb_who(struct icb_session *is, struct icb_group *ig)
497{
498 char buf[ICB_MSGSIZE];
499 struct icb_group *g;
500
501 if (!ig) {
502 int nusers = 0, ngroups = 0;
503
504 LIST_FOREACH(g, &groups, entry) {
505 nusers += icb_dowho(is, g);
506 ngroups++;
507 }
508 if (nusers > 0) {
509 (void)snprintf(buf, sizeof buf,
510 "Total: %d %s in %d %s",
511 nusers, nusers > 1 ? "users" : "user",
512 ngroups, ngroups > 1 ? "groups" : "group");
513 } else
514 (void)snprintf(buf, sizeof buf, "No users found.");
515 icb_cmdout(is, CMDOUT_CO, buf);
516 } else
517 (void)icb_dowho(is, ig);
518}
519
520/*
521 * icb_ismod: checks whether group is moderated by "is"
522 */
523inline int
524icb_ismod(struct icb_group *ig, struct icb_session *is)
525{
526 return (ig->mod == is);
527}
528
529/*
530 * icb_modpermit: checks user against the moderators table if it has
531 * been populated
532 */
533int
534icb_modpermit(struct icb_session *is)
535{
536 extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
537 extern int modtabcnt;
538
539 if (modtabcnt == 0 ||
540 bsearch(is->nick, modtab, modtabcnt, ICB_MAXNICKLEN,
541 (int (*)(const void *, const void *))strcmp))
542 return (1);
543 return (0);
544}
545
546/*
547 * icb_pass: passes moderation of group "ig" from "from" to "to",
548 * returns -1 if "from" is not a moderator, 1 if passed
549 * to "to" and 0 otherwise (no moderator or passed to the
550 * internal bot)
551 */
552int
553icb_pass(struct icb_group *ig, struct icb_session *from,
554 struct icb_session *to)
555{
556 if (ig->mod && ig->mod != from)
557 return (-1);
558 if (!from && !to)
559 return (-1);
560 if (to && !icb_modpermit(to))
561 return (-1);
562 ig->mod = to;
563 if (to)
564 icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
565 " of %s", from ? from->nick : "server", ig->name);
566 icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
567 "to %s", from ? from->nick : "server", to ? to->nick : "server");
568 return (1);
569}
570
571/*
572 * icb_nextfield: advances through a given buffer returning pointer to the
573 * beginning of the icb field or an empty string otherwise;
574 * cleans up trailing spaces
575 */
576char *
577icb_nextfield(char **buf)
578{
579 char *start = *buf;
580 char *end = NULL;
581
582 while (*buf && **buf != '\0' && **buf != ICB_M_SEP)
583 (*buf)++;
584 if (*buf && **buf == ICB_M_SEP) {
585 **buf = '\0';
586 end = *buf;
587 (*buf)++;
588 } else
589 end = *buf;
590 while (end && *(--end) == ' ' && end > start)
591 *end = '\0';
592 return (start);
593}
594
595/*
596 * icb_sendfmt: formats a string and sends it over
597 */
598void
599icb_sendfmt(struct icb_session *is, const char *fmt, ...)
600{
601 char buf[ICB_MSGSIZE];
602 va_list ap;
603 int buflen = 1;
604
605 va_start(ap, fmt);
606 buflen += vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
607 va_end(ap);
608 buf[0] = buflen;
609 icb_send(is, buf, buflen + 1);
610}
611
612/*
613 * icb_vis: strnvis-like function that escapes percentages as well
614 */
615int
616icb_vis(char *dst, const char *src, size_t dstsize)
617{
618 int si = 0, di = 0, td;
619
620 while ((size_t)di < dstsize && src[si] != '\0') {
621 if (src[si] == '%')
622 dst[di++] = '%', dst[di] = '%';
623 else if (isgraph(src[si]))
624 dst[di] = src[si];
625 else {
626 td = snprintf(&dst[di], dstsize - di,
627 "\\%03o", (unsigned char)src[si]);
628 di += td - 1;
629 }
630 si++, di++;
631 }
632 dst[MIN((size_t)di, dstsize)] = '\0';
633 return (0);
634}
Note: See TracBrowser for help on using the repository browser.