Changeset 626f420 in code for icb.c


Ignore:
Timestamp:
Mar 4, 2014, 5:09:42 PM (11 years ago)
Author:
Mike Belopuhov <mike@…>
Branches:
master
Children:
b7bc432
Parents:
8ef8c4e
git-author:
Mike Belopuhov <mike@…> (03/04/14 17:09:08)
git-committer:
Mike Belopuhov <mike@…> (03/04/14 17:09:42)
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icb.c

    r8ef8c4e r626f420  
    11/*
    2  * Copyright (c) 2009, 2010, 2013 Mike Belopuhov
     2 * Copyright (c) 2009, 2010, 2013, 2014 Mike Belopuhov
    33 *
    44 * Permission to use, copy, modify, and distribute this software for any
     
    2323#include <syslog.h>
    2424#include <unistd.h>
     25#include <ctype.h>
    2526#include <event.h>
    2627
     
    141142 */
    142143void
    143 icb_login(struct icb_session *is, char *group, char *nick, char *client)
     144icb_login(struct icb_session *is, char *grp, char *nick, char *client)
    144145{
    145146        char *defgrp = "1";
    146147        struct icb_group *ig;
    147148        struct icb_session *s;
    148 
    149         if (!nick || strlen(nick) == 0) {
     149        char group[ICB_MAXGRPLEN];
     150
     151        if (!nick || strlen(nick) == 0 ||
     152            icb_vis(is->nick, nick, ICB_MAXNICKLEN)) {
    150153                icb_error(is, "Invalid nick");
    151154                icb_drop(is, NULL);
    152155                return;
    153156        }
    154         if (!group || strlen(group) == 0)
    155                 group = defgrp;
     157        if (!grp || strlen(grp) == 0)
     158                strlcpy(group, defgrp, ICB_MAXGRPLEN);
     159        else
     160                icb_vis(group, grp, ICB_MAXNICKLEN);
    156161        LIST_FOREACH(ig, &groups, entry) {
    157162                if (strcmp(ig->name, group) == 0)
     
    169174                        }
    170175                        icb_log(NULL, LOG_DEBUG, "%s created group %s",
    171                             nick, group);
     176                            is->nick, group);
    172177                }
    173178        }
    174179        LIST_FOREACH(s, &ig->sess, entry) {
    175                 if (strcmp(s->nick, nick) == 0) {
     180                if (strcmp(s->nick, is->nick) == 0) {
    176181                        icb_error(is, "Nick is already in use");
    177182                        icb_drop(is, NULL);
     
    181186
    182187        if (client && strlen(client) > 0)
    183                 strlcpy(is->client, client, sizeof is->client);
     188                icb_vis(is->client, client, sizeof is->client);
    184189        strlcpy(is->nick, nick, sizeof is->nick);
    185190        is->group = ig;
     
    240245 */
    241246void
    242 icb_privmsg(struct icb_session *is, char *whom, char *msg)
     247icb_privmsg(struct icb_session *is, char *to, char *msg)
    243248{
    244249        struct icb_group *ig = is->group;
    245250        struct icb_session *s;
     251        char whom[ICB_MAXNICKLEN];
     252
     253        icb_vis(whom, to, ICB_MAXNICKLEN);
    246254
    247255        LIST_FOREACH(s, &ig->sess, entry) {
     
    263271{
    264272        void (*handler)(struct icb_session *, char *);
    265 
    266         if ((handler = icb_cmd_lookup(cmd)) == NULL) {
    267                 icb_error(is, "Unsupported command: %s", cmd);
     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);
    268279                return;
    269280        }
     
    510521 *  icb_ismod: checks whether group is moderated by "is"
    511522 */
    512 int
     523inline int
    513524icb_ismod(struct icb_group *ig, struct icb_session *is)
    514525{
    515         if (ig->mod && ig->mod == is)
    516                 return (1);
    517         return (0);
     526        return (ig->mod == is);
    518527}
    519528
     
    600609        icb_send(is, buf, buflen + 1);
    601610}
     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 TracChangeset for help on using the changeset viewer.