Changeset 626f420 in code


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

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • cmd.c

    r8ef8c4e r626f420  
    2222#include <syslog.h>
    2323#include <unistd.h>
     24#include <vis.h>
    2425#include <event.h>
    2526
     
    7980        struct icb_group *ig = is->group;
    8081        struct icb_session *s;
     82        char whom[ICB_MAXNICKLEN];
    8183
    8284        if (strlen(arg) == 0) {
     
    8587        }
    8688
     89        icb_vis(whom, arg, ICB_MAXNICKLEN);
     90
    8791        LIST_FOREACH(s, &ig->sess, entry) {
    88                 if (strcmp(s->nick, arg) == 0)
     92                if (strcmp(s->nick, whom) == 0)
    8993                        break;
    9094        }
    9195        if (s == NULL) {
    92                 icb_status(is, STATUS_NOTIFY, "%s is not signed on", arg);
     96                icb_status(is, STATUS_NOTIFY, "%s is not signed on", whom);
    9397                return;
    9498        }
     
    110114        struct icb_group *ig;
    111115        struct icb_session *s;
     116        char whom[ICB_MAXNICKLEN];
    112117
    113118        /* to boot or not to boot, that is the question */
     
    119124        }
    120125
     126        if (strlen(whom) == 0) {
     127                icb_error(is, "Invalid user");
     128                return;
     129        }
     130
     131        icb_vis(whom, arg, ICB_MAXNICKLEN);
     132
    121133        /* who would be a target then? */
    122134        LIST_FOREACH(s, &ig->sess, entry) {
    123                 if (strcmp(s->nick, arg) == 0)
     135                if (strcmp(s->nick, whom) == 0)
    124136                        break;
    125137        }
     
    140152        struct icb_group *ig;
    141153        struct icb_session *s;
     154        char group[ICB_MAXGRPLEN];
    142155        int changing = 0;
    143156
     
    147160        }
    148161
     162        icb_vis(group, arg, ICB_MAXGRPLEN);
     163
    149164        LIST_FOREACH(ig, &groups, entry) {
    150                 if (strcmp(ig->name, arg) == 0)
     165                if (strcmp(ig->name, group) == 0)
    151166                        break;
    152167        }
     
    156171                        return;
    157172                } else {
    158                         if ((ig = icb_addgroup(is, arg, NULL)) == NULL) {
     173                        if ((ig = icb_addgroup(is, group, NULL)) == NULL) {
    159174                                icb_error(is, "Can't create group");
    160175                                return;
    161176                        }
    162177                        icb_log(NULL, LOG_DEBUG, "%s created group %s",
    163                             is->nick, arg);
     178                            is->nick, group);
    164179                }
    165180        }
     
    208223        struct icb_group *ig = is->group;
    209224        struct icb_session *s;
     225        char nick[ICB_MAXNICKLEN];
    210226
    211227        if (strlen(arg) == 0) {
     
    221237        if (strlen(arg) > ICB_MAXNICKLEN)
    222238                arg[ICB_MAXNICKLEN - 1] = '\0';
     239        icb_vis(nick, arg, ICB_MAXNICKLEN);
    223240        LIST_FOREACH(s, &ig->sess, entry) {
    224                 if (strcmp(s->nick, arg) == 0) {
     241                if (strcmp(s->nick, nick) == 0) {
    225242                        icb_error(is, "Nick is already in use");
    226243                        return;
     
    228245        }
    229246        icb_status_group(ig, NULL, STATUS_NAME,
    230             "%s changed nickname to %s", is->nick, arg);
    231         strlcpy(is->nick, arg, sizeof is->nick);
     247            "%s changed nickname to %s", is->nick, nick);
     248        strlcpy(is->nick, nick, sizeof is->nick);
    232249}
    233250
     
    284301        struct icb_group *ig = is->group;
    285302        struct icb_session *s;
     303        char whom[ICB_MAXNICKLEN];
    286304
    287305        if (!ig->mod) {         /* if there is no mod, let anyone grab it */
     
    294312                        return;
    295313                }
     314                icb_vis(whom, arg, ICB_MAXNICKLEN);
    296315                LIST_FOREACH(s, &ig->sess, entry) {
    297                         if (strcmp(s->nick, arg) == 0)
     316                        if (strcmp(s->nick, whom) == 0)
    298317                                break;
    299318                }
     
    311330{
    312331        struct icb_group *ig = is->group;
     332        char topic[ICB_MAXTOPICLEN];
    313333
    314334        if (strlen(arg) == 0) { /* querying the topic */
     
    324344                        return;
    325345                }
    326                 strlcpy(ig->topic, arg, sizeof ig->topic);
     346                icb_vis(topic, arg, ICB_MAXTOPICLEN);
     347                strlcpy(ig->topic, topic, sizeof ig->topic);
    327348                icb_status_group(ig, NULL, STATUS_TOPIC,
    328349                    "%s changed the topic to \"%s\"", is->nick, ig->topic);
     
    334355{
    335356        struct icb_group *ig;
     357        char group[ICB_MAXGRPLEN];
    336358
    337359        if (strlen(arg) == 0)
    338360                return icb_who(is, NULL);
    339361
     362        icb_vis(group, arg, ICB_MAXGRPLEN);
    340363        LIST_FOREACH(ig, &groups, entry) {
    341                 if (strcmp(ig->name, arg) == 0)
     364                if (strcmp(ig->name, group) == 0)
    342365                        break;
    343366        }
    344367        if (ig == NULL) {
    345                 icb_error(is, "The group %s doesn't exist.", arg);
     368                icb_error(is, "The group %s doesn't exist.", group);
    346369                return;
    347370        }
  • dns.c

    r8ef8c4e r626f420  
    11/*
    2  * Copyright (c) 2013 Mike Belopuhov
     2 * Copyright (c) 2014 Mike Belopuhov
    33 * Copyright (c) 2009 Michael Shalayeff
    44 * All rights reserved.
     
    3939void dns_done(int, short, void *);
    4040int dns_pipe;
    41 
    4241
    4342struct icbd_dnsquery {
  • 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}
  • icb.h

    r8ef8c4e r626f420  
    122122/* icb.c */
    123123struct icb_group *icb_addgroup(struct icb_session *, char *, char *);
    124 void icb_cmdout(struct icb_session *, int, char *);
    125 void icb_delgroup(struct icb_group *);
    126 void icb_error(struct icb_session *, const char *, ...);
    127 void icb_init(struct icbd_callbacks *);
    128 void icb_input(struct icb_session *);
    129 int  icb_ismod(struct icb_group *, struct icb_session *);
    130 int  icb_modpermit(struct icb_session *);
    131 int  icb_pass(struct icb_group *, struct icb_session *, struct icb_session *);
    132 void icb_privmsg(struct icb_session *, char *, char *);
    133 void icb_remove(struct icb_session *, char *);
    134 void icb_sendfmt(struct icb_session *, const char *, ...);
    135 void icb_start(struct icb_session *);
    136 void icb_status(struct icb_session *, int, const char *, ...);
    137 void icb_status_group(struct icb_group *, struct icb_session *, int ,
    138          const char *, ...);
    139 void icb_who(struct icb_session *, struct icb_group *);
     124void             icb_cmdout(struct icb_session *, int, char *);
     125void             icb_delgroup(struct icb_group *);
     126void             icb_error(struct icb_session *, const char *, ...);
     127void             icb_init(struct icbd_callbacks *);
     128void             icb_input(struct icb_session *);
     129inline int       icb_ismod(struct icb_group *, struct icb_session *);
     130int              icb_modpermit(struct icb_session *);
     131int              icb_pass(struct icb_group *, struct icb_session *,
     132                     struct icb_session *);
     133void             icb_privmsg(struct icb_session *, char *, char *);
     134void             icb_remove(struct icb_session *, char *);
     135void             icb_sendfmt(struct icb_session *, const char *, ...);
     136void             icb_start(struct icb_session *);
     137void             icb_status(struct icb_session *, int, const char *, ...);
     138void             icb_status_group(struct icb_group *, struct icb_session *,
     139                    int, const char *, ...);
     140void             icb_who(struct icb_session *, struct icb_group *);
     141int              icb_vis(char *, const char *, size_t);
    140142
    141143/* callbacks from icbd.c */
    142 void (*icb_drop)(struct icb_session *, char *);
    143 void (*icb_log)(struct icb_session *, int, const char *, ...);
    144 void (*icb_send)(struct icb_session *, char *, ssize_t);
     144void            (*icb_drop)(struct icb_session *, char *);
     145void            (*icb_log)(struct icb_session *, int, const char *, ...);
     146void            (*icb_send)(struct icb_session *, char *, ssize_t);
Note: See TracChangeset for help on using the changeset viewer.