/* * IRC - Internet Relay Chat, src/modules/mode.c * (C) 2005-.. The UnrealIRCd Team * * See file AUTHORS in IRC package for additional names of * the programmers. * * 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "unrealircd.h" /* Forward declarations */ int list_mode_request(Client *client, Channel *channel, const char *req); CMD_FUNC(cmd_mode); CMD_FUNC(cmd_mlock); void _do_mode(Channel *channel, Client *client, MessageTag *recv_mtags, int parc, const char *parv[], time_t sendts, int samode); void _set_mode(Channel *channel, Client *client, int parc, const char *parv[], u_int *pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3]); void _set_channel_mode(Channel *channel, char *modes, char *parameters); CMD_FUNC(_cmd_umode); /* local: */ int do_mode_char(Channel *channel, long modetype, char modechar, const char *param, u_int what, Client *client, u_int *pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3]); int do_extmode_char(Channel *channel, Cmode *handler, const char *param, u_int what, Client *client, u_int *pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3]); void do_mode_char_member_mode_new(Channel *channel, Cmode *handler, const char *param, u_int what, Client *client, u_int *pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3]); void make_mode_str(Channel *channel, Cmode_t oldem, int pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3], char *mode_buf, char *para_buf, size_t mode_buf_size, size_t para_buf_size); static char *mode_cutoff(const char *s); static void mode_cutoff2(Client *client, Channel *channel, int *parc_out, const char *parv[]); void mode_operoverride_msg(Client *client, Channel *channel, char *modebuf, char *parabuf); static int samode_in_progress = 0; ModuleHeader MOD_HEADER = { "mode", "5.0", "command /mode", "UnrealIRCd Team", "unrealircd-6", }; MOD_TEST() { MARK_AS_OFFICIAL_MODULE(modinfo); EfunctionAddVoid(modinfo->handle, EFUNC_DO_MODE, _do_mode); EfunctionAddVoid(modinfo->handle, EFUNC_SET_MODE, _set_mode); EfunctionAddVoid(modinfo->handle, EFUNC_CMD_UMODE, _cmd_umode); EfunctionAddVoid(modinfo->handle, EFUNC_SET_CHANNEL_MODE, _set_channel_mode); return MOD_SUCCESS; } MOD_INIT() { CommandAdd(modinfo->handle, "MODE", cmd_mode, MAXPARA, CMD_USER|CMD_SERVER); CommandAdd(modinfo->handle, MSG_MLOCK, cmd_mlock, MAXPARA, CMD_SERVER); MARK_AS_OFFICIAL_MODULE(modinfo); return MOD_SUCCESS; } MOD_LOAD() { return MOD_SUCCESS; } MOD_UNLOAD() { return MOD_SUCCESS; } /* * cmd_mode -- written by binary (garryb@binary.islesfan.net) * Completely rewrote it. The old mode command was 820 lines of ICKY * coding, which is a complete waste, because I wrote it in 570 lines of * *decent* coding. This is also easier to read, change, and fine-tune. Plus, * everything isn't scattered; everything's grouped where it should be. * * parv[1] - channel */ CMD_FUNC(cmd_mode) { long unsigned sendts = 0; Ban *ban; Channel *channel; /* Now, try to find the channel in question */ if (parc > 1) { if (*parv[1] == '#') { channel = find_channel(parv[1]); if (!channel) { cmd_umode(client, recv_mtags, parc, parv); return; } } else { cmd_umode(client, recv_mtags, parc, parv); return; } } else { sendnumeric(client, ERR_NEEDMOREPARAMS, "MODE"); return; } if (MyConnect(client) && !valid_channelname(parv[1])) { sendnumeric(client, ERR_NOSUCHCHANNEL, parv[1]); return; } if (parc < 3) { *modebuf = *parabuf = '\0'; modebuf[1] = '\0'; channel_modes(client, modebuf, parabuf, sizeof(modebuf), sizeof(parabuf), channel, 0); sendnumeric(client, RPL_CHANNELMODEIS, channel->name, modebuf, parabuf); sendnumeric(client, RPL_CREATIONTIME, channel->name, (long long)channel->creationtime); return; } /* List mode request? Eg: "MODE #channel b" to list all bans */ if (MyUser(client) && BadPtr(parv[3]) && list_mode_request(client, channel, parv[2])) return; opermode = 0; #ifndef NO_OPEROVERRIDE if (IsUser(client) && !IsULine(client) && !check_channel_access(client, channel, "oaq") && !check_channel_access(client, channel, "h") && ValidatePermissionsForPath("channel:override:mode",client,NULL,channel,NULL)) { sendts = 0; opermode = 1; goto aftercheck; } if (IsUser(client) && !IsULine(client) && !check_channel_access(client, channel, "oaq") && check_channel_access(client, channel, "h") && ValidatePermissionsForPath("channel:override:mode",client,NULL,channel,NULL)) { opermode = 2; goto aftercheck; } #endif if (parv[2] && (*parv[2] == '&')) { /* We don't do any bounce-mode handling anymore since UnrealIRCd 6 */ return; } if (IsServer(client) && (sendts = atol(parv[parc - 1])) && !IsULine(client) && (sendts > channel->creationtime)) { unreal_log(ULOG_INFO, "mode", "MODE_TS_IGNORED", client, "MODE change ignored for $channel from $client: " "timestamp mismatch, ours=$channel.creationtime, theirs=$their_ts", log_data_channel("channel", channel), log_data_integer("their_ts", sendts)); return; } if (IsServer(client) && !sendts && *parv[parc - 1] != '0') sendts = -1; if (IsServer(client) && sendts != -1) parc--; /* server supplied a time stamp, remove it now */ aftercheck: /* This is to prevent excess + modes. -- Syzop */ if (MyUser(client) && parv[2]) { parv[2] = mode_cutoff(parv[2]); mode_cutoff2(client, channel, &parc, parv); } /* Filter out the unprivileged FIRST. * * Now, we can actually do the mode. */ (void)do_mode(channel, client, recv_mtags, parc - 2, parv + 2, sendts, 0); /* After this don't touch 'channel' anymore, as permanent module may have destroyed the channel */ opermode = 0; /* Important since sometimes forgotten. -- Syzop */ } /** Cut off mode string (eg: +abcdfjkdsgfgs) at MAXMODEPARAMS modes. * @param s The mode string (modes only, no parameters) * @note Should only used on local clients * @returns The cleaned up string */ static char *mode_cutoff(const char *i) { static char newmodebuf[BUFSIZE]; char *o; unsigned short modesleft = MAXMODEPARAMS * 2; /* be generous... */ strlcpy(newmodebuf, i, sizeof(newmodebuf)); for (o = newmodebuf; *o && modesleft; o++) if ((*o != '-') && (*o != '+')) modesleft--; *o = '\0'; return newmodebuf; } /** Another mode cutoff routine - this one for the server-side * amplification/enlargement problem that happens with bans/exempts/invex * as explained in #2837. -- Syzop */ static void mode_cutoff2(Client *client, Channel *channel, int *parc_out, const char *parv[]) { int len, i; int parc = *parc_out; if (parc-2 <= 3) return; /* Less than 3 mode parameters? Then we don't even have to check */ /* Calculate length of MODE if it would go through fully as-is */ /* :nick!user@host MODE #channel +something param1 param2 etc... */ len = strlen(client->name) + strlen(client->user->username) + strlen(GetHost(client)) + strlen(channel->name) + 11; len += strlen(parv[2]); if (*parv[2] != '+' && *parv[2] != '-') len++; for (i = 3; parv[i]; i++) { len += strlen(parv[i]) + 1; /* (+1 for the space character) */ /* +4 is another potential amplification (per-param). * If we were smart we would only check this for b/e/I and only for * relevant cases (not for all extended), but this routine is dumb, * so we just +4 for any case where the full mask is missing. * It's better than assuming +4 for all cases, though... */ if (!match_simple("*!*@*", parv[i])) len += 4; } /* Now check if the result is acceptable... */ if (len < 510) return; /* Ok, no problem there... */ /* Ok, we have a potential problem... * we just dump the last parameter... check how much space we saved... * and try again if that did not help */ for (i = parc-1; parv[i] && (i > 3); i--) { len -= strlen(parv[i]); if (!match_simple("*!*@*", parv[i])) len -= 4; /* must adjust accordingly.. */ parv[i] = NULL; (*parc_out)--; if (len < 510) break; } /* This may be reached if like the first parameter is really insane long.. * which is no problem, as other layers (eg: ban) takes care of that. * We're done... */ } /* do_mode -- written by binary * User or server is authorized to do the mode. This takes care of * setting the mode and relaying it to other users and servers. */ void _do_mode(Channel *channel, Client *client, MessageTag *recv_mtags, int parc, const char *parv[], time_t sendts, int samode) { char pvar[MAXMODEPARAMS][MODEBUFLEN + 3]; int pcount; char tschange = 0; MessageTag *mtags = NULL; new_message(client, recv_mtags, &mtags); /* IMPORTANT: if you return, don't forget to free mtags!! */ /* Please keep the next 3 lines next to each other */ samode_in_progress = samode; set_mode(channel, client, parc, parv, &pcount, pvar); samode_in_progress = 0; if (MyConnect(client)) RunHook(HOOKTYPE_PRE_LOCAL_CHANMODE, client, channel, mtags, modebuf, parabuf, sendts, samode); else RunHook(HOOKTYPE_PRE_REMOTE_CHANMODE, client, channel, mtags, modebuf, parabuf, sendts, samode); if (IsServer(client)) { if (sendts > 0) { if (IsInvalidChannelTS(sendts)) { unreal_log(ULOG_WARNING, "mode", "MODE_INVALID_TIMESTAMP", NULL, "MODE for channel $channel has invalid timestamp $send_timestamp (from $client.name on $client.user.servername)\n" "Buffer: $modebuf $parabuf", log_data_channel("channel", channel), log_data_integer("send_timestamp", sendts), log_data_string("modebuf", modebuf), log_data_string("parabuf", parabuf)); /* Yeah, so what to do in this case? * Don't set channel->creationtime * and assume merging. */ sendts = channel->creationtime; } else if (sendts < channel->creationtime) { /* Our timestamp is wrong or this is a new channel */ tschange = 1; channel->creationtime = sendts; } if (sendts > channel->creationtime && channel->creationtime) { /* Their timestamp is wrong */ sendts = channel->creationtime; sendto_one(client, NULL, ":%s MODE %s + %lld", me.name, channel->name, (long long)channel->creationtime); } } if (sendts == -1) sendts = channel->creationtime; } if (tschange && empty_mode(modebuf)) { /* Message from the other server is an empty mode, BUT they * did change the channel->creationtime to an earlier TS * (see above "Our timestamp is wrong or this is a new channel"). * We need to relay this MODE message to all other servers * (all except from where it came from, client). */ sendto_server(client, 0, 0, NULL, ":%s MODE %s + %lld", me.id, channel->name, (long long)channel->creationtime); free_message_tags(mtags); /* Return here, as there isn't anything else to send */ return; } /* opermode for twimodesystem --sts */ #ifndef NO_OPEROVERRIDE if ((opermode == 1) && IsUser(client)) { mode_operoverride_msg(client, channel, modebuf, parabuf); sendts = 0; } #endif /* If we have nothing to do, we can stop here. */ if (empty_mode(modebuf)) { free_message_tags(mtags); return; } if (IsUser(client) && samode && MyUser(client)) { if (!sajoinmode) { char buf[512]; snprintf(buf, sizeof(buf), "%s%s%s", modebuf, *parabuf ? " " : "", parabuf); unreal_log(ULOG_INFO, "samode", "SAMODE_COMMAND", client, "Client $client used SAMODE $channel ($mode)", log_data_channel("channel", channel), log_data_string("mode", buf)); } client = &me; sendts = 0; } sendto_channel(channel, client, NULL, 0, 0, SEND_LOCAL, mtags, ":%s MODE %s %s %s", client->name, channel->name, modebuf, parabuf); if (IsServer(client) && sendts != -1) { sendto_server(client, 0, 0, mtags, ":%s MODE %s %s %s %lld", client->id, channel->name, modebuf, parabuf, (long long)sendts); } else if (samode && IsMe(client)) { /* SAMODE is a special case: always send a TS of 0 (omitting TS==desync) */ sendto_server(client, 0, 0, mtags, ":%s MODE %s %s %s 0", client->id, channel->name, modebuf, parabuf); } else { sendto_server(client, 0, 0, mtags, ":%s MODE %s %s %s", client->id, channel->name, modebuf, parabuf); /* tell them it's not a timestamp, in case the last param is a number. */ } if (MyConnect(client)) RunHook(HOOKTYPE_LOCAL_CHANMODE, client, channel, mtags, modebuf, parabuf, sendts, samode); else RunHook(HOOKTYPE_REMOTE_CHANMODE, client, channel, mtags, modebuf, parabuf, sendts, samode); /* After this, don't touch 'channel' anymore! As permanent module may have destroyed the channel. */ free_message_tags(mtags); } /* make_mode_str -- written by binary * Reconstructs the mode string, to make it look clean. mode_buf will * contain the +x-y stuff, and the parabuf will contain the parameters. */ void make_mode_str(Channel *channel, Cmode_t oldem, int pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3], char *mode_buf, char *para_buf, size_t mode_buf_size, size_t para_buf_size) { Cmode *cm; char tmpbuf[MODEBUFLEN+3], *tmpstr; char *x = mode_buf; int what, cnt, z; int i; char *m; what = 0; *tmpbuf = '\0'; *mode_buf = '\0'; *para_buf = '\0'; what = 0; /* + paramless extmodes... */ for (cm=channelmodes; cm; cm = cm->next) { if (!cm->letter || cm->paracount) continue; /* have it now and didn't have it before? */ if ((channel->mode.mode & cm->mode) && !(oldem & cm->mode)) { if (what != MODE_ADD) { *x++ = '+'; what = MODE_ADD; } *x++ = cm->letter; } } *x = '\0'; /* - extmodes (both "param modes" and paramless don't have * any params when unsetting... well, except one special type, that is (we skip those here) */ for (cm=channelmodes; cm; cm = cm->next) { if (!cm->letter || cm->unset_with_param) continue; /* don't have it now and did have it before */ if (!(channel->mode.mode & cm->mode) && (oldem & cm->mode)) { if (what != MODE_DEL) { *x++ = '-'; what = MODE_DEL; } *x++ = cm->letter; } } *x = '\0'; /* reconstruct bkov chain */ for (cnt = 0; cnt < pcount; cnt++) { if ((*(pvar[cnt]) == '+') && what != MODE_ADD) { *x++ = '+'; what = MODE_ADD; } if ((*(pvar[cnt]) == '-') && what != MODE_DEL) { *x++ = '-'; what = MODE_DEL; } *x++ = *(pvar[cnt] + 1); tmpstr = &pvar[cnt][2]; z = (MODEBUFLEN * MAXMODEPARAMS); m = para_buf; while ((*m)) { m++; } while ((*tmpstr) && ((m-para_buf) < z)) { *m = *tmpstr; m++; tmpstr++; } *m++ = ' '; *m = '\0'; } z = strlen(para_buf); if ((z > 0) && (para_buf[z - 1] == ' ')) para_buf[z - 1] = '\0'; *x = '\0'; if (*mode_buf == '\0') { *mode_buf = '+'; mode_buf++; *mode_buf = '\0'; /* Don't send empty lines. */ } return; } const char *mode_ban_handler(Client *client, Channel *channel, const char *param, int what, int extbtype, Ban **banlist) { const char *tmpstr; BanContext *b; tmpstr = clean_ban_mask(param, what, client, 0); if (BadPtr(tmpstr)) { /* Invalid ban. See if we can send an error about that (only for extbans) */ if (MyUser(client) && is_extended_ban(param)) { const char *nextbanstr; Extban *extban = findmod_by_bantype(param, &nextbanstr); BanContext *b; b = safe_alloc(sizeof(BanContext)); b->client = client; b->channel = channel; b->banstr = nextbanstr; b->is_ok_check = EXBCHK_PARAM; b->what = what; b->ban_type = extbtype; if (extban && extban->is_ok) extban->is_ok(b); safe_free(b); } return NULL; } if (MyUser(client) && is_extended_ban(param)) { /* extban: check access if needed */ const char *nextbanstr; Extban *extban = findmod_by_bantype(tmpstr, &nextbanstr); if (extban) { if ((extbtype == EXBTYPE_INVEX) && !(extban->options & EXTBOPT_INVEX)) return NULL; /* this extended ban type does not support INVEX */ if (extban->is_ok) { BanContext *b = safe_alloc(sizeof(BanContext)); b->client = client; b->channel = channel; b->what = what; b->ban_type = extbtype; b->is_ok_check = EXBCHK_ACCESS; b->banstr = nextbanstr; if (!extban->is_ok(b)) { if (ValidatePermissionsForPath("channel:override:mode:extban",client,NULL,channel,NULL)) { /* TODO: send operoverride notice */ } else { b->banstr = nextbanstr; b->is_ok_check = EXBCHK_ACCESS_ERR; extban->is_ok(b); safe_free(b); return NULL; } } b->banstr = nextbanstr; b->is_ok_check = EXBCHK_PARAM; if (!extban->is_ok(b)) { safe_free(b); return NULL; } safe_free(b); } } } if ( (what == MODE_ADD && add_listmode(banlist, client, channel, tmpstr)) || (what == MODE_DEL && del_listmode(banlist, channel, tmpstr))) { return NULL; /* already exists */ } return tmpstr; } /** Write the result of a mode change. * This is used by do_mode_char_list_mode(), do_mode_char_member_mode() * and do_extmode_char(). * The result is later used by make_mode_str() to create the * actual MODE line to be broadcasted to the channel and other servers. */ void do_mode_char_write(char pvar[MAXMODEPARAMS][MODEBUFLEN + 3], u_int *pcount, u_int what, char modeletter, const char *str) { /* Caller should have made sure there was room! */ if (*pcount >= MAXMODEPARAMS) #ifdef DEBUGMODE abort(); #else return; #endif ircsnprintf(pvar[*pcount], MODEBUFLEN + 3, "%c%c%s", (what == MODE_ADD) ? '+' : '-', modeletter, str); (*pcount)++; } int do_mode_char_list_mode(Channel *channel, long modetype, char modechar, const char *param, u_int what, Client *client, u_int *pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3]) { const char *tmpstr; /* Check if there is a parameter present */ if (!param || *pcount >= MAXMODEPARAMS) return 0; switch (modetype) { case MODE_BAN: if (!(tmpstr = mode_ban_handler(client, channel, param, what, EXBTYPE_BAN, &channel->banlist))) break; /* rejected or duplicate */ do_mode_char_write(pvar, pcount, what, modechar, tmpstr); break; case MODE_EXCEPT: if (!(tmpstr = mode_ban_handler(client, channel, param, what, EXBTYPE_EXCEPT, &channel->exlist))) break; /* rejected or duplicate */ do_mode_char_write(pvar, pcount, what, modechar, tmpstr); break; case MODE_INVEX: if (!(tmpstr = mode_ban_handler(client, channel, param, what, EXBTYPE_INVEX, &channel->invexlist))) break; /* rejected or duplicate */ do_mode_char_write(pvar, pcount, what, modechar, tmpstr); break; } return 1; } /** Check access and if granted, set the extended chanmode to the requested value in memory. * @returns amount of params eaten (0 or 1) */ int do_extmode_char(Channel *channel, Cmode *handler, const char *param, u_int what, Client *client, u_int *pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3]) { int paracnt = (what == MODE_ADD) ? handler->paracount : 0; char mode = handler->letter; int x; const char *morphed; if ((what == MODE_DEL) && handler->unset_with_param) paracnt = 1; /* there's always an exception! */ /* Expected a param and it isn't there? */ if (paracnt && (!param || (*pcount >= MAXMODEPARAMS))) return 0; /* Prevent remote users from setting local channel modes */ if (handler->local && !MyUser(client)) return paracnt; if (MyUser(client)) { x = handler->is_ok(client, channel, mode, param, EXCHK_ACCESS, what); if ((x == EX_ALWAYS_DENY) || ((x == EX_DENY) && !op_can_override("channel:override:mode:del",client,channel,handler) && !samode_in_progress)) { handler->is_ok(client, channel, mode, param, EXCHK_ACCESS_ERR, what); return paracnt; /* Denied & error msg sent */ } if ((x == EX_DENY) && !samode_in_progress) opermode = 1; /* override in progress... */ } else { /* remote user: we only need to check if we need to generate an operoverride msg */ if (!IsULine(client) && IsUser(client) && op_can_override("channel:override:mode:del",client,channel,handler) && (handler->is_ok(client, channel, mode, param, EXCHK_ACCESS, what) != EX_ALLOW)) { opermode = 1; /* override in progress... */ } } if (handler->type == CMODE_MEMBER) { do_mode_char_member_mode_new(channel, handler, param, what, client, pcount, pvar); return 1; } /* Check for multiple changes in 1 command (like +y-y+y 1 2, or +yy 1 2). */ for (x = 0; x < *pcount; x++) { if (pvar[x][1] == handler->letter) { /* this is different than the old chanmode system, coz: * "mode #chan +kkL #a #b #c" will get "+kL #a #b" which is wrong :p. * we do eat the parameter. -- Syzop */ return paracnt; } } /* w00t... a parameter mode */ if (handler->paracount) { if (what == MODE_DEL) { if (!(channel->mode.mode & handler->mode)) return paracnt; /* There's nothing to remove! */ if (handler->unset_with_param) { /* Special extended channel mode requiring a parameter on unset. * Any provided parameter is ok, the current one (that is set) will be used. */ do_mode_char_write(pvar, pcount, what, handler->letter, cm_getparameter(channel, handler->letter)); } else { /* Normal extended channel mode: deleting is just -X, no parameter. * Nothing needs to be done here. */ } } else { /* add: is the parameter ok? */ if (handler->is_ok(client, channel, mode, param, EXCHK_PARAM, what) == FALSE) return paracnt; /* rejected by is_ok */ morphed = handler->conv_param(param, client, channel); if (!morphed) return paracnt; /* rejected by conv_param */ /* is it already set at the same value? if so, ignore it. */ if (channel->mode.mode & handler->mode) { const char *now, *requested; char flag = handler->letter; now = cm_getparameter(channel, flag); requested = handler->conv_param(param, client, channel); if (now && requested && !strcmp(now, requested)) return paracnt; /* ignore... */ } do_mode_char_write(pvar, pcount, what, handler->letter, handler->conv_param(param, client, channel)); param = morphed; /* set param to converted parameter. */ } } if (what == MODE_ADD) { /* + */ channel->mode.mode |= handler->mode; if (handler->paracount) cm_putparameter(channel, handler->letter, param); RunHook(HOOKTYPE_MODECHAR_ADD, channel, (int)mode); } else { /* - */ channel->mode.mode &= ~(handler->mode); RunHook(HOOKTYPE_MODECHAR_DEL, channel, (int)mode); if (handler->paracount) cm_freeparameter(channel, handler->letter); } return paracnt; } /** Set or unset a mode on a member (eg +vhoaq/-vhoaq) */ void do_mode_char_member_mode_new(Channel *channel, Cmode *handler, const char *param, u_int what, Client *client, u_int *pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3]) { Member *member = NULL; Membership *membership = NULL; Client *target; int chasing = 0; Hook *h; char c[2]; char modechar = handler->letter; if (!(target = find_chasing(client, param, &chasing))) return; if (!target->user) return; if (!(membership = find_membership_link(target->user->channel, channel))) { sendnumeric(client, ERR_USERNOTINCHANNEL, target->name, channel->name); return; } member = find_member_link(channel->members, target); if (!member) { /* should never happen */ unreal_log(ULOG_ERROR, "mode", "BUG_FIND_MEMBER_LINK_FAILED", target, "[BUG] Client $target.details on channel $channel: " "found via find_membership_link() but NOT found via find_member_link(). " "This should never happen! Please report on https://bugs.unrealircd.org/", log_data_channel("channel", channel)); return; } if ((what == MODE_ADD) && strchr(member->member_modes, modechar)) return; /* already set */ if ((what == MODE_DEL) && !strchr(member->member_modes, modechar)) return; /* already unset */ if (what == MODE_ADD) { if (strchr(member->member_modes, modechar)) return; /* already set */ /* Set the mode */ add_member_mode_fast(member, membership, modechar); } else { if (!strchr(member->member_modes, modechar)) return; /* already unset */ del_member_mode_fast(member, membership, modechar); } /* And write out the mode */ do_mode_char_write(pvar, pcount, what, modechar, target->name); } /** In 2003 I introduced PROTOCTL CHANMODES= so remote servers (and services) * could deal with unknown "parameter eating" channel modes, minimizing desyncs. * Now, in 2015, I finally added the code to deal with this. -- Syzop */ int paracount_for_chanmode_from_server(Client *client, u_int what, char mode) { if (MyUser(client)) return 0; /* no server, we have no idea, assume 0 paracount */ if (!client->server) { /* If it's from a remote client then figure out from which "uplink" we * received this MODE. The uplink is the directly-connected-server to us * and may differ from the server the user is actually on. This is correct. */ if (!client->direction || !client->direction->server) return 0; client = client->direction; } if (client->server->features.chanmodes[0] && strchr(client->server->features.chanmodes[0], mode)) return 1; /* 1 parameter for set, 1 parameter for unset */ if (client->server->features.chanmodes[1] && strchr(client->server->features.chanmodes[1], mode)) return 1; /* 1 parameter for set, 1 parameter for unset */ if (client->server->features.chanmodes[2] && strchr(client->server->features.chanmodes[2], mode)) return (what == MODE_ADD) ? 1 : 0; /* 1 parameter for set, no parameter for unset */ if (client->server->features.chanmodes[3] && strchr(client->server->features.chanmodes[3], mode)) return 0; /* no parameter for set, no parameter for unset */ if (mode == '&') return 0; /* & indicates bounce, it is not an actual mode character */ if (mode == 'F') return (what == MODE_ADD) ? 1 : 0; /* Future compatibility */ /* If we end up here it means we have no idea if it is a parameter-eating or paramless * channel mode. That's actually pretty bad. This shouldn't happen since CHANMODES= * is sent since 2003 and the (often also required) EAUTH PROTOCTL is in there since 2010. */ unreal_log(ULOG_WARNING, "mode", "REMOTE_UNKNOWN_CHANNEL_MODE", client, "Server $client sent us an unknown channel mode $what$mode_character!", log_data_string("what", ((what == MODE_ADD) ? "+" : "-")), log_data_char("mode_character", mode)); return 0; } /** Quick way to find out parameter count for a channel mode. * Only for LOCAL mode requests. For remote modes use * paracount_for_chanmode_from_server() instead. */ int paracount_for_chanmode(u_int what, char mode) { if (me.server->features.chanmodes[0] && strchr(me.server->features.chanmodes[0], mode)) return 1; /* 1 parameter for set, 1 parameter for unset */ if (me.server->features.chanmodes[1] && strchr(me.server->features.chanmodes[1], mode)) return 1; /* 1 parameter for set, 1 parameter for unset */ if (me.server->features.chanmodes[2] && strchr(me.server->features.chanmodes[2], mode)) return (what == MODE_ADD) ? 1 : 0; /* 1 parameter for set, no parameter for unset */ if (me.server->features.chanmodes[3] && strchr(me.server->features.chanmodes[3], mode)) return 0; /* no parameter for set, no parameter for unset */ /* Not found: */ return 0; } /* set_mode * written by binary */ void _set_mode(Channel *channel, Client *client, int parc, const char *parv[], u_int *pcount, char pvar[MAXMODEPARAMS][MODEBUFLEN + 3]) { Cmode *cm = NULL; const char *curchr; const char *argument; char argumentbuf[MODEBUFLEN+1]; u_int what = MODE_ADD; long modetype = 0; int paracount = 1; #ifdef DEVELOP char *tmpo = NULL; #endif CoreChannelModeTable *tab = &corechannelmodetable[0]; CoreChannelModeTable foundat; int found = 0; int sent_mlock_warning = 0; unsigned int htrig = 0; int checkrestr = 0, warnrestr = 1; Cmode_t oldem; paracount = 1; *pcount = 0; oldem = channel->mode.mode; if (RESTRICT_CHANNELMODES && !ValidatePermissionsForPath("immune:restrict-channelmodes",client,NULL,channel,NULL)) /* "cache" this */ checkrestr = 1; for (curchr = parv[0]; *curchr; curchr++) { switch (*curchr) { case '+': what = MODE_ADD; break; case '-': what = MODE_DEL; break; default: if (MyUser(client) && channel->mode_lock && strchr(channel->mode_lock, *curchr) != NULL) { if (!IsOper(client) || find_server(SERVICES_NAME, NULL) || !ValidatePermissionsForPath("channel:override:mlock",client,NULL,channel,NULL)) { if (!sent_mlock_warning) { sendnumeric(client, ERR_MLOCKRESTRICTED, channel->name, *curchr, channel->mode_lock); sent_mlock_warning++; } continue; } } found = 0; tab = &corechannelmodetable[0]; while ((tab->mode != 0x0) && found == 0) { if (tab->flag == *curchr) { found = 1; foundat = *tab; } tab++; } if (found == 1) { modetype = foundat.mode; } else { /* Maybe in extmodes */ for (cm=channelmodes; cm; cm = cm->next) { if (cm->letter == *curchr) { found = 2; break; } } } if (found == 0) /* Mode char unknown */ { if (!MyUser(client)) paracount += paracount_for_chanmode_from_server(client, what, *curchr); else sendnumeric(client, ERR_UNKNOWNMODE, *curchr); break; } if (checkrestr && strchr(RESTRICT_CHANNELMODES, *curchr)) { if (warnrestr) { sendnotice(client, "Setting/removing of channelmode(s) '%s' has been disabled.", RESTRICT_CHANNELMODES); warnrestr = 0; } paracount += paracount_for_chanmode(what, *curchr); break; } if ((paracount < parc) && parv[paracount]) { strlcpy(argumentbuf, parv[paracount], sizeof(argumentbuf)); argument = argumentbuf; } else { argument = NULL; } #ifndef NO_OPEROVERRIDE if (found == 1) { if ((Halfop_mode(modetype) == FALSE) && opermode == 2 && htrig != 1) { /* YUCK! */ if ((foundat.flag == 'h') && argument && (find_user(argument, NULL) == client)) { /* ircop with halfop doing a -h on himself. no warning. */ } else { opermode = 0; htrig = 1; } } } else if (found == 2) { /* Extended mode: all override stuff is in do_extmode_char which will set * opermode if appropriate. -- Syzop */ } #endif /* !NO_OPEROVERRIDE */ if (found == 1) { paracount += do_mode_char_list_mode(channel, modetype, *curchr, argument, what, client, pcount, pvar); } else if (found == 2) { paracount += do_extmode_char(channel, cm, argument, what, client, pcount, pvar); } break; } /* switch(*curchr) */ } /* for loop through mode letters */ make_mode_str(channel, oldem, *pcount, pvar, modebuf, parabuf, sizeof(modebuf), sizeof(parabuf)); #ifndef NO_OPEROVERRIDE if ((htrig == 1) && IsUser(client)) { mode_operoverride_msg(client, channel, modebuf, parabuf); htrig = 0; opermode = 0; /* stop double override notices... but is this ok??? -- Syzop */ } #endif } /* * cmd_umode() added 15/10/91 By Darren Reed. * parv[1] - username to change mode for * parv[2] - modes to change */ CMD_FUNC(_cmd_umode) { Umode *um; const char *m; Client *acptr; int what; long oldumodes = 0; char oldsnomask[64]; /* (small note: keep 'what' as an int. -- Syzop). */ short rpterror = 0, umode_restrict_err = 0, chk_restrict = 0, modex_err = 0; what = MODE_ADD; if (parc < 2) { sendnumeric(client, ERR_NEEDMOREPARAMS, "MODE"); return; } if (!(acptr = find_user(parv[1], NULL))) { if (MyConnect(client)) { sendnumeric(client, ERR_NOSUCHNICK, parv[1]); } return; } if (acptr != client) { sendnumeric(client, ERR_USERSDONTMATCH); return; } if (parc < 3) { sendnumeric(client, RPL_UMODEIS, get_usermode_string(client)); if (client->user->snomask) sendnumeric(client, RPL_SNOMASK, client->user->snomask); return; } userhost_save_current(client); /* save host, in case we do any +x/-x or similar */ oldumodes = client->umodes; if (RESTRICT_USERMODES && MyUser(client) && !ValidatePermissionsForPath("immune:restrict-usermodes",client,NULL,NULL,NULL)) chk_restrict = 1; if (client->user->snomask) strlcpy(oldsnomask, client->user->snomask, sizeof(oldsnomask)); /* * parse mode change string(s) */ for (m = parv[2]; *m; m++) { if (chk_restrict && strchr(RESTRICT_USERMODES, *m)) { if (!umode_restrict_err) { sendnotice(client, "Setting/removing of usermode(s) '%s' has been disabled.", RESTRICT_USERMODES); umode_restrict_err = 1; } continue; } switch (*m) { case '+': what = MODE_ADD; break; case '-': what = MODE_DEL; break; /* we may not get these, * but they shouldnt be in default */ case ' ': case '\t': break; case 's': if (what == MODE_DEL) { if (parc >= 4 && client->user->snomask) { set_snomask(client, parv[3]); if (client->user->snomask == NULL) goto def; break; } else { set_snomask(client, NULL); goto def; } } if ((what == MODE_ADD) && IsOper(client)) { if (parc < 4) set_snomask(client, OPER_SNOMASKS); else set_snomask(client, parv[3]); goto def; } break; case 'o': case 'O': if (IsQuarantined(client->direction)) { unreal_log(ULOG_INFO, "mode", "OPER_KILLED_QUARANTINE", client, "QUARANTINE: Oper $client.detail on server $client.user.servername killed, due to quarantine"); sendto_server(NULL, 0, 0, NULL, ":%s KILL %s :Quarantined: no oper privileges allowed", me.id, client->name); exit_client(client, NULL, "Quarantined: no oper privileges allowed"); return; } /* A local user trying to set himself +o/+O is denied here. * A while later (outside this loop) it is handled as well (and +C, +N, etc too) * but we need to take care here too because it might cause problems * that's just asking for bugs! -- Syzop. */ if (MyUser(client) && (what == MODE_ADD)) /* Someone setting himself +o? Deny it. */ break; goto def; case 't': case 'x': switch (UHOST_ALLOWED) { case UHALLOW_ALWAYS: goto def; case UHALLOW_NEVER: if (MyUser(client)) { if (!modex_err) { sendnotice(client, "*** Setting %c%c is disabled", what == MODE_ADD ? '+' : '-', *m); modex_err = 1; } break; } goto def; case UHALLOW_NOCHANS: if (MyUser(client) && client->user->joined) { if (!modex_err) { sendnotice(client, "*** Setting %c%c can not be done while you are on channels", what == MODE_ADD ? '+' : '-', *m); modex_err = 1; } break; } goto def; case UHALLOW_REJOIN: /* Handled later */ goto def; } break; default: def: for (um = usermodes; um; um = um->next) { if (um->letter == *m) { if (um->allowed && !um->allowed(client,what)) break; if (what == MODE_ADD) client->umodes |= um->mode; else client->umodes &= ~um->mode; break; } } if (!um && MyConnect(client) && !rpterror) { sendnumeric(client, ERR_UMODEUNKNOWNFLAG); rpterror = 1; } break; } /* switch */ } /* for */ /* Don't let non-ircops set ircop-only modes or snomasks */ if (!ValidatePermissionsForPath("self:opermodes",client,NULL,NULL,NULL)) { if ((oldumodes & UMODE_OPER) && IsOper(client)) { /* User is an oper but does not have the self:opermodes capability. * This only happens for heavily restricted IRCOps. * Fixes bug https://bugs.unrealircd.org/view.php?id=5130 */ int i; /* MODES */ for (um = usermodes; um; um = um->next) { if (um->unset_on_deoper) { /* This is an oper mode. Is it set now and wasn't earlier? * then it needs to be stripped, as setting it is not * permitted. */ if ((client->umodes & um->mode) && !(oldumodes & um->mode)) client->umodes &= ~um->mode; /* remove */ } } /* SNOMASKS: user can delete existing but not add new ones */ if (client->user->snomask) { char rerun; do { char *p; rerun = 0; for (p = client->user->snomask; *p; p++) { if (!strchr(oldsnomask, *p)) { /* It is set now, but was not earlier? * Then it needs to be stripped, as setting is not permitted. * And re-run the loop */ delletterfromstring(client->user->snomask, *p); rerun = 1; break; } } } while(rerun); /* And make sure an empty snomask ("") becomes a NULL pointer */ if (client->user->snomask && !*client->user->snomask) remove_all_snomasks(client); } } else { /* User isn't an ircop at all. The solution is simple: */ remove_oper_privileges(client, 0); } } if (MyUser(client) && (client->umodes & UMODE_SECURE) && !IsSecure(client)) client->umodes &= ~UMODE_SECURE; /* -x translates to -xt (if applicable) */ if ((oldumodes & UMODE_HIDE) && !IsHidden(client)) client->umodes &= ~UMODE_SETHOST; /* Vhost unset = unset some other data as well */ if ((oldumodes & UMODE_SETHOST) && !IsSetHost(client)) { swhois_delete(client, "vhost", "*", &me, NULL); } /* +x or -t+x */ if ((IsHidden(client) && !(oldumodes & UMODE_HIDE)) || ((oldumodes & UMODE_SETHOST) && !IsSetHost(client) && IsHidden(client))) { if (!dontspread) sendto_server(client, PROTO_VHP, 0, NULL, ":%s SETHOST :%s", client->name, client->user->virthost); /* Set the vhost */ safe_strdup(client->user->virthost, client->user->cloakedhost); /* Notify */ userhost_changed(client); if (MyUser(client)) sendnumeric(client, RPL_HOSTHIDDEN, client->user->virthost); } /* -x */ if (!IsHidden(client) && (oldumodes & UMODE_HIDE)) { /* (Re)create the cloaked virthost, because it will be used * for ban-checking... free+recreate here because it could have * been a vhost for example. -- Syzop */ safe_strdup(client->user->virthost, client->user->cloakedhost); /* Notify */ userhost_changed(client); if (MyUser(client)) sendnumeric(client, RPL_HOSTHIDDEN, client->user->realhost); } /* * If I understand what this code is doing correctly... * If the user WAS an operator and has now set themselves -o/-O * then remove their access, d'oh! * In order to allow opers to do stuff like go +o, +h, -o and * remain +h, I moved this code below those checks. It should be * O.K. The above code just does normal access flag checks. This * only changes the operflag access level. -Cabal95 */ if ((oldumodes & UMODE_OPER) && !IsOper(client) && MyConnect(client)) { list_del(&client->special_node); if (MyUser(client)) RunHook(HOOKTYPE_LOCAL_OPER, client, 0, NULL); remove_oper_privileges(client, 0); } if (!(oldumodes & UMODE_OPER) && IsOper(client)) irccounts.operators++; /* deal with opercounts and stuff */ if ((oldumodes & UMODE_OPER) && !IsOper(client)) { irccounts.operators--; VERIFY_OPERCOUNT(client, "umode1"); } else /* YES this 'else' must be here, otherwise we can decrease twice. fixes opercount bug. */ if (!(oldumodes & UMODE_HIDEOPER) && IsHideOper(client)) { irccounts.operators--; VERIFY_OPERCOUNT(client, "umode2"); } /* end of dealing with opercounts */ if ((oldumodes & UMODE_HIDEOPER) && !IsHideOper(client)) { irccounts.operators++; } if (!(oldumodes & UMODE_INVISIBLE) && IsInvisible(client)) irccounts.invisible++; if ((oldumodes & UMODE_INVISIBLE) && !IsInvisible(client)) irccounts.invisible--; if (MyConnect(client) && !IsOper(client)) remove_oper_privileges(client, 0); /* * compare new flags with old flags and send string which * will cause servers to update correctly. */ if (oldumodes != client->umodes) RunHook(HOOKTYPE_UMODE_CHANGE, client, oldumodes, client->umodes); if (dontspread == 0) send_umode_out(client, 1, oldumodes); if (MyConnect(client) && client->user->snomask && strcmp(oldsnomask, client->user->snomask)) sendnumeric(client, RPL_SNOMASK, client->user->snomask); } CMD_FUNC(cmd_mlock) { Channel *channel = NULL; time_t t; if ((parc < 3) || BadPtr(parv[2])) return; t = (time_t) atol(parv[1]); /* Now, try to find the channel in question */ channel = find_channel(parv[2]); if (!channel) return; /* Senders' Channel t is higher, drop it. */ if (t > channel->creationtime) return; if (IsServer(client)) set_channel_mlock(client, channel, parv[3], TRUE); } void mode_operoverride_msg(Client *client, Channel *channel, char *modebuf, char *parabuf) { char buf[1024]; if (empty_mode(modebuf)) return; /* Internally we have this distinction between modebuf and parabuf, * but this makes little sense to maintain in JSON. */ snprintf(buf, sizeof(buf), "%s %s", modebuf, parabuf); unreal_log(ULOG_INFO, "operoverride", "OPEROVERRIDE_MODE", client, "OperOverride: $client.details changed channel mode of $channel to: $channel_mode", log_data_string("override_type", "mode"), log_data_string("channel_mode", buf), log_data_channel("channel", channel)); } /* Deal with information requests from local users, such as: * MODE #chan b Show the ban list * MODE #chan e Show the ban exemption list * MODE #chan I Show the invite exception list * MODE #chan q Show list of channel owners * MODE #chan a Show list of channel admins * @returns 1 if processed as a mode list (please return), * 0 if not (continue with the MODE as it likely is a set request). */ int list_mode_request(Client *client, Channel *channel, const char *req) { const char *p; Ban *ban; Member *member; for (p = req; *p; p++) if (strchr("beIqa", *p)) break; if (!*p) return 0; /* not handled, proceed with the MODE set attempt */ /* First, check access */ if (strchr("beI", *p)) { if (!IsMember(client, channel) && !ValidatePermissionsForPath("channel:see:mode:remotebanlist",client,NULL,channel,NULL)) { sendnumeric(client, ERR_NOTONCHANNEL, channel->name); return 1; /* handled */ } } else { if (!IsMember(client, channel) && !ValidatePermissionsForPath("channel:see:mode:remoteownerlist",client,NULL,channel,NULL)) { sendnumeric(client, ERR_NOTONCHANNEL, channel->name); return 1; /* handled */ } } switch(*p) { case 'b': for (ban = channel->banlist; ban; ban = ban->next) sendnumeric(client, RPL_BANLIST, channel->name, ban->banstr, ban->who, (long long)ban->when); sendnumeric(client, RPL_ENDOFBANLIST, channel->name); break; case 'e': for (ban = channel->exlist; ban; ban = ban->next) sendnumeric(client, RPL_EXLIST, channel->name, ban->banstr, ban->who, (long long)ban->when); sendnumeric(client, RPL_ENDOFEXLIST, channel->name); break; case 'I': for (ban = channel->invexlist; ban; ban = ban->next) sendnumeric(client, RPL_INVEXLIST, channel->name, ban->banstr, ban->who, (long long)ban->when); sendnumeric(client, RPL_ENDOFINVEXLIST, channel->name); break; case 'q': for (member = channel->members; member; member = member->next) if (strchr(member->member_modes, 'q')) sendnumeric(client, RPL_QLIST, channel->name, member->client->name); sendnumeric(client, RPL_ENDOFQLIST, channel->name); break; case 'a': for (member = channel->members; member; member = member->next) if (strchr(member->member_modes, 'a')) sendnumeric(client, RPL_ALIST, channel->name, member->client->name); sendnumeric(client, RPL_ENDOFALIST, channel->name); break; } return 1; /* handled */ } void _set_channel_mode(Channel *channel, char *modes, char *parameters) { char buf[512]; char *p, *param; int myparc = 1, i; char *myparv[512]; memset(&myparv, 0, sizeof(myparv)); myparv[0] = raw_strdup(modes); strlcpy(buf, parameters, sizeof(buf)); for (param = strtoken(&p, buf, " "); param; param = strtoken(&p, NULL, " ")) myparv[myparc++] = raw_strdup(param); myparv[myparc] = NULL; SetULine(&me); // hack for crash.. set ulined so no access checks. do_mode(channel, &me, NULL, myparc, (const char **)myparv, 0, 0); ClearULine(&me); // and clear it again.. for (i = 0; i < myparc; i++) safe_free(myparv[i]); }