5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005, 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "serverincludes.h"
22 #include "server_internal.h"
24 static int silc_server_is_registered(SilcServer server,
25 SilcPacketStream sock,
26 SilcServerCommandContext cmd,
29 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
34 silc_server_command_send_status_data(SilcServerCommandContext cmd,
39 const unsigned char *arg,
42 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
43 SilcServerCommandReplyContext cmdr,
45 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
47 /* Server command list. */
48 SilcServerCommand silc_command_list[] =
50 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
51 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
53 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
54 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
55 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
56 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
57 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
58 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
59 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
60 SILC_SERVER_CMD(stats, STATS, SILC_CF_LAG | SILC_CF_REG),
61 SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
62 SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
63 SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
64 SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
65 SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
66 SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
67 SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
68 SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
69 SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
70 SILC_SERVER_CMD(detach, DETACH, SILC_CF_LAG_STRICT | SILC_CF_REG),
71 SILC_SERVER_CMD(watch, WATCH, SILC_CF_LAG | SILC_CF_REG),
72 SILC_SERVER_CMD(silcoper, SILCOPER,
73 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
74 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
75 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
76 SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
77 SILC_SERVER_CMD(service, SERVICE, SILC_CF_LAG_STRICT | SILC_CF_REG),
79 SILC_SERVER_CMD(connect, PRIV_CONNECT,
80 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
81 SILC_SERVER_CMD(close, PRIV_CLOSE,
82 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
83 SILC_SERVER_CMD(shutdown, PRIV_SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
89 /* Performs several checks to the command. It first checks whether this
90 command was called as pending command callback. If it was then it checks
91 whether error occurred in the command reply where the pending command
94 It also checks that the requested command includes correct amount
96 #define SILC_SERVER_COMMAND_CHECK(command, context, min, max) \
100 if (silc_server_command_pending_error_check(cmd, context2, command)) { \
101 SILC_LOG_DEBUG(("Error occurred in command reply, command not called")); \
102 silc_server_command_free(cmd); \
106 _argc = silc_argument_get_arg_num(cmd->args); \
108 SILC_LOG_DEBUG(("Not enough parameters in command")); \
109 silc_server_command_send_status_reply(cmd, command, \
110 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, \
112 silc_server_command_free(cmd); \
116 SILC_LOG_DEBUG(("Too many parameters in command")); \
117 silc_server_command_send_status_reply(cmd, command, \
118 SILC_STATUS_ERR_TOO_MANY_PARAMS, \
120 silc_server_command_free(cmd); \
125 /* Returns TRUE if the connection is registered. Unregistered connections
126 usually cannot send commands hence the check. */
128 static int silc_server_is_registered(SilcServer server,
129 SilcPacketStream sock,
130 SilcServerCommandContext cmd,
133 SilcIDListData idata = silc_packet_get_context(sock);
138 if (idata->status & SILC_IDLIST_STATUS_REGISTERED)
141 silc_server_command_send_status_reply(cmd, command,
142 SILC_STATUS_ERR_NOT_REGISTERED, 0);
146 /* Internal context to hold data when executed command with timeout. */
148 SilcServerCommandContext ctx;
149 SilcServerCommand *cmd;
150 } *SilcServerCommandTimeout;
152 /* Timeout callback to process commands with timeout for client. Client's
153 commands are always executed with timeout. */
155 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
157 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
158 SilcClientEntry client = silc_packet_get_context(timeout->ctx->sock);
161 SILC_LOG_DEBUG(("Client entry is invalid"));
162 silc_server_command_free(timeout->ctx);
167 /* Update access time */
168 client->last_command = time(NULL);
170 if (!(timeout->cmd->flags & SILC_CF_REG)) {
171 SILC_LOG_DEBUG(("Calling %s command",
172 silc_get_command_name(timeout->cmd->cmd)));
173 timeout->cmd->cb(timeout->ctx, NULL);
174 } else if (silc_server_is_registered(timeout->ctx->server,
177 timeout->cmd->cmd)) {
178 SILC_LOG_DEBUG(("Calling %s command",
179 silc_get_command_name(timeout->cmd->cmd)));
180 timeout->cmd->cb(timeout->ctx, NULL);
182 SILC_LOG_DEBUG(("Client is not registered"));
183 silc_server_command_free(timeout->ctx);
189 /* Processes received command packet. */
191 void silc_server_command_process(SilcServer server,
192 SilcPacketStream sock,
195 SilcIDListData idata = silc_packet_get_context(sock);
196 SilcServerCommandContext ctx;
197 SilcServerCommand *cmd;
203 /* Allocate command context. This must be free'd by the
204 command routine receiving it. */
205 ctx = silc_server_command_alloc();
206 ctx->server = server;
208 ctx->packet = packet; /* Save original packet */
209 silc_packet_stream_ref(sock);
211 /* Parse the command payload in the packet */
212 ctx->payload = silc_command_payload_parse(packet->buffer.data,
213 silc_buffer_len(&packet->buffer));
215 SILC_LOG_ERROR(("Bad command payload"));
216 silc_packet_free(packet);
217 silc_packet_stream_unref(ctx->sock);
221 ctx->args = silc_command_get_args(ctx->payload);
223 /* Get the command */
224 command = silc_command_get(ctx->payload);
225 for (cmd = silc_command_list; cmd->cb; cmd++)
226 if (cmd->cmd == command)
229 if (!cmd || !cmd->cb) {
230 SILC_LOG_DEBUG(("Unknown command %d", command));
231 silc_server_command_send_status_reply(ctx, command,
232 SILC_STATUS_ERR_UNKNOWN_COMMAND, 0);
233 silc_packet_free(packet);
234 silc_packet_stream_unref(ctx->sock);
239 /* Execute client's commands always with timeout. Normally they are
240 executed with zero (0) timeout but if client is sending command more
241 frequently than once in 2 seconds, then the timeout may be 0 to 2
243 if (idata->conn_type == SILC_CONN_CLIENT) {
244 SilcClientEntry client = silc_packet_get_context(sock);
245 SilcServerCommandTimeout timeout;
248 timeout = silc_calloc(1, sizeof(*timeout));
252 if (client->last_command && (time(NULL) - client->last_command) < 2) {
253 client->fast_command++;
256 if (client->fast_command - 2 <= 0)
257 client->fast_command = 0;
259 client->fast_command -= 2;
263 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
264 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
265 silc_schedule_task_add_timeout(
267 silc_server_command_process_timeout, timeout,
268 (client->fast_command < 3 ? 0 :
269 2 - (time(NULL) - client->last_command)),
270 (client->fast_command < 3 ? 200000 : 0));
272 silc_schedule_task_add_timeout(server->schedule,
273 silc_server_command_process_timeout,
278 /* Execute for server */
280 if (!(cmd->flags & SILC_CF_REG)) {
281 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
283 } else if (silc_server_is_registered(server, sock, ctx, cmd->cmd)) {
284 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
287 SILC_LOG_DEBUG(("Server is not registered"));
288 silc_server_command_free(ctx);
292 /* Allocate Command Context */
294 SilcServerCommandContext silc_server_command_alloc()
296 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
301 /* Free's the command context allocated before executing the command */
303 void silc_server_command_free(SilcServerCommandContext ctx)
306 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
308 if (ctx->users < 1) {
310 silc_command_payload_free(ctx->payload);
312 silc_packet_free(ctx->packet);
314 silc_packet_stream_unref(ctx->sock);
319 /* Duplicate Command Context by adding reference counter. The context won't
320 be free'd untill it hits zero. */
322 SilcServerCommandContext
323 silc_server_command_dup(SilcServerCommandContext ctx)
326 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
331 /* Timeout for pending command. If reply to pending command never arrives
332 this is called to free resources. */
334 SILC_TASK_CALLBACK(silc_server_command_pending_timeout)
336 SilcServer server = app_context;
337 SilcServerCommandPending *reply = context;
338 SilcServerCommandReplyContext cmdr;
342 SILC_LOG_DEBUG(("Timeout pending command"));
344 /* Allocate temporary and bogus command reply context */
345 cmdr = silc_calloc(1, sizeof(*cmdr));
346 cmdr->server = server;
347 cmdr->ident = reply->ident;
349 /* Check for pending commands and mark to be exeucted */
351 silc_server_command_pending_check(server, reply->reply_cmd,
352 reply->ident, &cmdr->callbacks_count);
354 /* Create bogus command reply with an error inside */
356 silc_command_reply_payload_encode_va(reply->reply_cmd ? reply->reply_cmd :
357 SILC_COMMAND_RESERVED,
358 SILC_STATUS_ERR_TIMEDOUT, 0,
360 cmdr->payload = silc_command_payload_parse(tmpreply->data,
361 silc_buffer_len(tmpreply));
362 silc_buffer_free(tmpreply);
364 /* Call all callbacks. Same as SILC_SERVER_PENDING_EXEC macro. */
365 for (i = 0; i < cmdr->callbacks_count; i++)
366 if (cmdr->callbacks[i].callback)
367 (*cmdr->callbacks[i].callback)(cmdr->callbacks[i].context, cmdr);
369 silc_server_command_pending_del(server, reply->reply_cmd, reply->ident);
370 silc_server_command_reply_free(cmdr);
373 /* Add new pending command to be executed when reply to a command has been
374 received. The `reply_cmd' is the command that will call the `callback'
375 with `context' when reply has been received. It can be SILC_COMMAND_NONE
376 to match any command with the `ident'. If `ident' is non-zero
377 the `callback' will be executed when received reply with command
378 identifier `ident'. If there already exists pending command for the
379 specified command, ident, callback and context this function has no
382 SilcBool silc_server_command_pending(SilcServer server,
383 SilcCommand reply_cmd,
385 SilcCommandCb callback,
388 return silc_server_command_pending_timed(server, reply_cmd, ident, callback,
392 /* Same as silc_server_command_pending with specific timeout for pending
393 commands. If the `timeout' is zero default timeout is used. */
395 SilcBool silc_server_command_pending_timed(SilcServer server,
396 SilcCommand reply_cmd,
398 SilcCommandCb callback,
402 SilcServerCommandPending *reply;
404 /* Check whether identical pending already exists for same command,
405 ident, callback and callback context. If it does then it would be
406 error to register it again. */
407 silc_dlist_start(server->pending_commands);
408 while ((reply = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
409 if (reply->reply_cmd == reply_cmd && reply->ident == ident &&
410 reply->callback == callback && reply->context == context)
414 reply = silc_calloc(1, sizeof(*reply));
415 reply->reply_cmd = reply_cmd;
416 reply->ident = ident;
417 reply->context = context;
418 reply->callback = callback;
420 silc_schedule_task_add_timeout(server->schedule,
421 silc_server_command_pending_timeout, reply,
422 timeout ? timeout : 12, 0);
423 silc_dlist_add(server->pending_commands, reply);
428 /* Deletes pending command by reply command type. */
430 void silc_server_command_pending_del(SilcServer server,
431 SilcCommand reply_cmd,
434 SilcServerCommandPending *r;
436 silc_dlist_start(server->pending_commands);
437 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
438 if ((r->reply_cmd == reply_cmd || (r->reply_cmd == SILC_COMMAND_NONE &&
440 && r->ident == ident) {
441 silc_dlist_del(server->pending_commands, r);
443 silc_schedule_task_del(server->schedule, r->timeout);
449 /* Checks for pending commands and marks callbacks to be called from
450 the command reply function. Returns TRUE if there were pending command. */
452 SilcServerCommandPendingCallbacks
453 silc_server_command_pending_check(SilcServer server,
456 SilcUInt32 *callbacks_count)
458 SilcServerCommandPending *r;
459 SilcServerCommandPendingCallbacks callbacks = NULL;
462 silc_dlist_start(server->pending_commands);
463 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
464 if ((r->reply_cmd == command || r->reply_cmd == SILC_COMMAND_NONE)
465 && r->ident == ident) {
466 callbacks = silc_realloc(callbacks, sizeof(*callbacks) * (i + 1));
467 callbacks[i].context = r->context;
468 callbacks[i].callback = r->callback;
469 r->reply_check = TRUE;
474 *callbacks_count = i;
478 /* Sends simple status message as command reply packet */
481 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
489 cmd->server->stat.commands_sent++;
491 SILC_LOG_DEBUG(("Sending command status %d", status));
494 silc_command_reply_payload_encode_va(command, status, error,
495 silc_command_get_ident(cmd->payload),
497 silc_server_packet_send(cmd->server, cmd->sock,
498 SILC_PACKET_COMMAND_REPLY, 0,
499 buffer->data, silc_buffer_len(buffer));
500 silc_buffer_free(buffer);
503 /* Sends command status reply with one extra argument. The argument
504 type must be sent as argument. */
507 silc_server_command_send_status_data(SilcServerCommandContext cmd,
512 const unsigned char *arg,
518 cmd->server->stat.commands_sent++;
520 SILC_LOG_DEBUG(("Sending command status %d", status));
523 silc_command_reply_payload_encode_va(command, status, 0,
524 silc_command_get_ident(cmd->payload),
525 1, arg_type, arg, arg_len);
526 silc_server_packet_send(cmd->server, cmd->sock,
527 SILC_PACKET_COMMAND_REPLY, 0,
528 buffer->data, silc_buffer_len(buffer));
529 silc_buffer_free(buffer);
533 silc_server_command_send_status_data2(SilcServerCommandContext cmd,
537 SilcUInt32 arg_type1,
538 const unsigned char *arg1,
540 SilcUInt32 arg_type2,
541 const unsigned char *arg2,
547 cmd->server->stat.commands_sent++;
549 SILC_LOG_DEBUG(("Sending command status %d", status));
552 silc_command_reply_payload_encode_va(command, status, 0,
553 silc_command_get_ident(cmd->payload),
554 2, arg_type1, arg1, arg_len1,
555 arg_type2, arg2, arg_len2);
556 silc_server_packet_send(cmd->server, cmd->sock,
557 SILC_PACKET_COMMAND_REPLY, 0,
558 buffer->data, silc_buffer_len(buffer));
559 silc_buffer_free(buffer);
562 /* This function can be called to check whether in the command reply
563 an error occurred. This function has no effect if this is called
564 when the command function was not called as pending command callback.
565 This returns TRUE if error had occurred. */
568 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
569 SilcServerCommandReplyContext cmdr,
572 if (!cmd->pending || !cmdr)
575 if (!silc_command_get_status(cmdr->payload, NULL, NULL)) {
579 cmd->server->stat.commands_sent++;
581 /* Send the same command reply payload */
582 silc_command_set_command(cmdr->payload, silc_command_get(cmd->payload));
583 silc_command_set_ident(cmdr->payload,
584 silc_command_get_ident(cmd->payload));
585 buffer = silc_command_payload_encode_payload(cmdr->payload);
586 silc_server_packet_send(cmd->server, cmd->sock,
587 SILC_PACKET_COMMAND_REPLY, 0,
588 buffer->data, silc_buffer_len(buffer));
589 silc_buffer_free(buffer);
596 /* Server side of command WHOIS. */
598 SILC_SERVER_CMD_FUNC(whois)
600 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
601 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 256);
602 silc_server_query_command(cmd->server, SILC_COMMAND_WHOIS, cmd);
603 silc_server_command_free(cmd);
606 /* Server side of command WHOWAS. */
608 SILC_SERVER_CMD_FUNC(whowas)
610 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
611 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
612 silc_server_query_command(cmd->server, SILC_COMMAND_WHOWAS, cmd);
613 silc_server_command_free(cmd);
616 /* Server side of command IDENTIFY. */
618 SILC_SERVER_CMD_FUNC(identify)
620 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
621 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 256);
622 silc_server_query_command(cmd->server, SILC_COMMAND_IDENTIFY, cmd);
623 silc_server_command_free(cmd);
626 /* Server side of command NICK. Sets nickname for user. Setting
627 nickname causes generation of a new client ID for the client. The
628 new client ID is sent to the client after changing the nickname. */
630 SILC_SERVER_CMD_FUNC(nick)
632 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
633 SilcClientEntry client = silc_packet_get_context(cmd->sock);
634 SilcServer server = cmd->server;
635 SilcBuffer nidp, oidp = NULL;
636 SilcClientID *new_id;
638 unsigned char *nick, *nickc = NULL;
639 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
641 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
644 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
647 nick = silc_argument_get_arg_type(cmd->args, 1, &nick_len);
649 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
650 SILC_STATUS_ERR_BAD_NICKNAME, 0);
654 /* Truncate over long nicks */
655 if (nick_len > 128) {
660 /* Check for valid nickname string. This is cached, original is saved
661 in the client context. */
662 nickc = silc_identifier_check(nick, nick_len, SILC_STRING_UTF8, 128, NULL);
664 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
665 SILC_STATUS_ERR_BAD_NICKNAME, 0);
669 /* Check for same nickname */
670 if (strlen(client->nickname) == nick_len &&
671 !memcmp(client->nickname, nick, nick_len)) {
672 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
677 /* Create new Client ID */
678 if (!silc_id_create_client_id(cmd->server, cmd->server->id,
680 cmd->server->md5hash,
681 nickc, strlen(nickc), &new_id)) {
682 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
683 SILC_STATUS_ERR_BAD_NICKNAME, 0);
688 /* Send notify about nickname change to our router. We send the new
689 ID and ask to replace it with the old one. If we are router the
690 packet is broadcasted. Send NICK_CHANGE notify. */
691 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
692 SILC_BROADCAST(server), client->id,
695 /* Check if anyone is watching the old nickname */
696 if (server->server_type == SILC_ROUTER)
697 silc_server_check_watcher_list(server, client, nick,
698 SILC_NOTIFY_TYPE_NICK_CHANGE);
700 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
702 /* Remove old cache entry */
703 silc_idcache_del_by_context(server->local_list->clients, client, NULL);
705 silc_free(client->id);
708 silc_free(client->nickname);
709 client->nickname = strdup(nick);
711 /* Update client cache */
712 silc_idcache_add(server->local_list->clients, nickc,
713 client->id, (void *)client);
715 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
717 /* Send NICK_CHANGE notify to the client's channels */
718 silc_server_send_notify_on_channels(server, NULL, client,
719 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
720 oidp->data, silc_buffer_len(oidp),
721 nidp->data, silc_buffer_len(nidp),
723 strlen(client->nickname));
725 /* Check if anyone is watching the new nickname */
726 if (server->server_type == SILC_ROUTER)
727 silc_server_check_watcher_list(server, client, NULL,
728 SILC_NOTIFY_TYPE_NICK_CHANGE);
731 /* Send the new Client ID as reply command back to client */
732 silc_server_send_command_reply(cmd->server, cmd->sock,
734 SILC_STATUS_OK, 0, ident, 2,
735 2, nidp->data, silc_buffer_len(nidp),
737 silc_buffer_free(nidp);
739 silc_buffer_free(oidp);
742 silc_server_command_free(cmd);
745 /* Sends the LIST command reply */
748 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
749 SilcChannelEntry *lch,
750 SilcUInt32 lch_count,
751 SilcChannelEntry *gch,
752 SilcUInt32 gch_count)
756 SilcChannelEntry entry;
758 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
760 unsigned char usercount[4];
762 int valid_lcount = 0, valid_rcount = 0;
764 for (i = 0; i < lch_count; i++) {
765 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
770 for (i = 0; i < gch_count; i++) {
771 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
777 if (!lch_count && !gch_count) {
778 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
783 status = SILC_STATUS_OK;
784 if ((lch_count + gch_count) > 1)
785 status = SILC_STATUS_LIST_START;
788 for (i = 0, k = 0; i < lch_count; i++) {
794 status = SILC_STATUS_LIST_ITEM;
795 if (valid_lcount > 1 && k == valid_lcount - 1 && !valid_rcount)
796 status = SILC_STATUS_LIST_END;
798 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
800 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
802 memset(usercount, 0, sizeof(usercount));
804 topic = entry->topic;
805 users = silc_hash_table_count(entry->user_list);
806 SILC_PUT32_MSB(users, usercount);
810 silc_server_send_command_reply(cmd->server, cmd->sock, SILC_COMMAND_LIST,
812 2, idp->data, silc_buffer_len(idp),
813 3, entry->channel_name,
814 strlen(entry->channel_name),
815 4, topic, topic ? strlen(topic) : 0,
817 silc_buffer_free(idp);
822 for (i = 0, k = 0; i < gch_count; i++) {
828 status = SILC_STATUS_LIST_ITEM;
829 if (valid_rcount > 1 && k == valid_rcount - 1)
830 status = SILC_STATUS_LIST_END;
832 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
834 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
836 memset(usercount, 0, sizeof(usercount));
838 topic = entry->topic;
839 users = entry->user_count;
840 SILC_PUT32_MSB(users, usercount);
844 silc_server_send_command_reply(cmd->server, cmd->sock, SILC_COMMAND_LIST,
846 2, idp->data, silc_buffer_len(idp),
847 3, entry->channel_name,
848 strlen(entry->channel_name),
849 4, topic, topic ? strlen(topic) : 0,
851 silc_buffer_free(idp);
856 /* Server side of LIST command. This lists the channel of the requested
857 server. Secret channels are not listed. */
859 SILC_SERVER_CMD_FUNC(list)
861 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
862 SilcServer server = cmd->server;
864 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
865 SilcUInt32 lch_count = 0, gch_count = 0;
867 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 1);
869 /* If we are normal server, send the command to router, since we
870 want to know all channels in the network. */
871 if (!cmd->pending && server->server_type != SILC_ROUTER &&
872 !server->standalone) {
874 SilcUInt16 old_ident;
877 cmd->server->stat.commands_sent++;
879 old_ident = silc_command_get_ident(cmd->payload);
880 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
881 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
882 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
883 SILC_PACKET_COMMAND, cmd->packet->flags,
884 tmpbuf->data, silc_buffer_len(tmpbuf));
886 /* Reprocess this packet after received reply from router */
887 silc_server_command_pending(server, SILC_COMMAND_LIST,
888 silc_command_get_ident(cmd->payload),
889 silc_server_command_list,
890 silc_server_command_dup(cmd));
892 silc_command_set_ident(cmd->payload, old_ident);
893 silc_buffer_free(tmpbuf);
898 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
899 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
900 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
904 /* Get the channels from local list */
905 lchannels = silc_idlist_get_channels(server->local_list, SILC_ID_GET_ID(id),
908 /* Get the channels from global list */
909 gchannels = silc_idlist_get_channels(server->global_list, SILC_ID_GET_ID(id),
913 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
914 gchannels, gch_count);
916 silc_free(lchannels);
917 silc_free(gchannels);
920 silc_server_command_free(cmd);
923 /* Server side of TOPIC command. Sets topic for channel and/or returns
924 current topic to client. */
926 SILC_SERVER_CMD_FUNC(topic)
928 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
929 SilcServer server = cmd->server;
930 SilcClientEntry client = silc_packet_get_context(cmd->sock);
932 SilcChannelEntry channel;
933 SilcChannelClientEntry chl;
936 SilcUInt32 argc, tmp_len;
937 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
939 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
942 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
944 argc = silc_argument_get_arg_num(cmd->args);
947 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
948 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
949 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
953 /* Check whether the channel exists */
954 channel = silc_idlist_find_channel_by_id(server->local_list,
955 SILC_ID_GET_ID(id), NULL);
957 channel = silc_idlist_find_channel_by_id(server->global_list,
958 SILC_ID_GET_ID(id), NULL);
960 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
961 silc_server_command_send_status_data(cmd, SILC_COMMAND_TOPIC,
962 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
970 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
972 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
973 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
979 tmp[tmp_len - 1] = '\0';
982 if (!silc_utf8_valid(tmp, tmp_len)) {
983 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
984 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
989 /* See whether the client is on channel and has rights to change topic */
990 if (!silc_server_client_on_channel(client, channel, &chl)) {
991 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
992 silc_server_command_send_status_data(cmd, SILC_COMMAND_TOPIC,
993 SILC_STATUS_ERR_NOT_ON_CHANNEL,
998 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
999 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1000 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1001 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1002 silc_server_command_send_status_data(cmd, SILC_COMMAND_TOPIC,
1003 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
1004 0, 2, tmp, tmp_len);
1008 if (!channel->topic || strcmp(channel->topic, tmp)) {
1009 /* Set the topic for channel */
1010 silc_free(channel->topic);
1011 channel->topic = strdup(tmp);
1013 /* Send TOPIC_SET notify type to the network */
1014 silc_server_send_notify_topic_set(server, SILC_PRIMARY_ROUTE(server),
1015 SILC_BROADCAST(server), channel,
1016 client->id, SILC_ID_CLIENT,
1019 /* Send notify about topic change to all clients on the channel */
1020 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1021 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
1022 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
1023 idp->data, silc_buffer_len(idp),
1025 strlen(channel->topic));
1026 silc_buffer_free(idp);
1030 /* Send the topic to client as reply packet */
1031 idp = silc_id_payload_encode(SILC_ID_GET_ID(id), SILC_ID_CHANNEL);
1032 silc_server_send_command_reply(cmd->server, cmd->sock, SILC_COMMAND_TOPIC,
1033 SILC_STATUS_OK, 0, ident, 2,
1034 2, idp->data, silc_buffer_len(idp),
1037 strlen(channel->topic) : 0);
1038 silc_buffer_free(idp);
1041 silc_server_command_free(cmd);
1044 /* Server side of INVITE command. Invites some client to join some channel.
1045 This command is also used to manage the invite list of the channel. */
1047 SILC_SERVER_CMD_FUNC(invite)
1049 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1050 SilcServer server = cmd->server;
1051 SilcPacketStream sock = cmd->sock, dest_sock;
1052 SilcChannelClientEntry chl;
1053 SilcClientEntry sender, dest;
1054 SilcChannelEntry channel;
1056 SilcIDListData idata;
1057 SilcArgumentPayload args;
1058 SilcHashTableList htl;
1059 SilcBuffer list, tmp2;
1060 SilcBufferStruct alist;
1061 unsigned char *tmp, *atype = NULL;
1062 SilcUInt32 len, type, len2;
1063 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
1065 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
1067 /* Get Channel ID */
1068 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
1069 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1070 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
1074 /* Get the channel entry */
1075 channel = silc_idlist_find_channel_by_id(server->local_list,
1076 SILC_ID_GET_ID(id), NULL);
1078 channel = silc_idlist_find_channel_by_id(server->global_list,
1079 SILC_ID_GET_ID(id), NULL);
1081 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1082 silc_server_command_send_status_data(cmd, SILC_COMMAND_INVITE,
1083 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
1089 /* Check whether the sender of this command is on the channel. */
1090 sender = silc_packet_get_context(sock);
1091 if (!sender || !silc_server_client_on_channel(sender, channel, &chl)) {
1092 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1093 silc_server_command_send_status_data(cmd, SILC_COMMAND_INVITE,
1094 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
1099 /* Check whether the channel is invite-only channel. If yes then the
1100 sender of this command must be at least channel operator. */
1101 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
1102 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1103 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1104 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1105 silc_server_command_send_status_data(cmd, SILC_COMMAND_INVITE,
1106 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
1111 /* Get destination client ID */
1112 if (silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id2, NULL)) {
1115 /* Get the client entry */
1116 dest = silc_server_query_client(server, SILC_ID_GET_ID(id2),
1119 if (server->server_type != SILC_SERVER || !resolve || cmd->pending) {
1120 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1121 silc_server_command_send_status_data(
1122 cmd, SILC_COMMAND_INVITE,
1123 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID, 0,
1128 /* The client info is being resolved. Reprocess this packet after
1129 receiving the reply to the query. */
1130 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1132 silc_server_command_invite,
1133 silc_server_command_dup(cmd));
1134 cmd->pending = TRUE;
1138 /* Check whether the requested client is already on the channel. */
1139 if (silc_server_client_on_channel(dest, channel, NULL)) {
1140 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1141 atype = silc_argument_get_arg_type(cmd->args, 1, &len2);
1142 silc_server_command_send_status_data2(cmd, SILC_COMMAND_INVITE,
1143 SILC_STATUS_ERR_USER_ON_CHANNEL,
1149 /* Get route to the client */
1150 dest_sock = silc_server_get_client_route(server, NULL, 0,
1151 SILC_ID_GET_ID(id2),
1154 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1155 silc_server_command_send_status_data(cmd, SILC_COMMAND_INVITE,
1156 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1161 /* Add the client to the invite list */
1163 /* Allocate hash table for invite list if it doesn't exist yet */
1164 if (!channel->invite_list)
1165 channel->invite_list =
1166 silc_hash_table_alloc(0, silc_hash_ptr,
1168 silc_server_inviteban_destruct, channel, TRUE);
1170 /* Check if the ID is in the list already */
1171 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1172 silc_hash_table_list(channel->invite_list, &htl);
1173 while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2)) {
1174 if (type == 3 && !memcmp(tmp2->data, tmp, len)) {
1179 silc_hash_table_list_reset(&htl);
1181 /* Add new Client ID to invite list */
1183 list = silc_buffer_alloc_size(len);
1184 silc_buffer_put(list, tmp, len);
1185 silc_hash_table_add(channel->invite_list, (void *)3, list);
1188 if (!(dest->mode & SILC_UMODE_BLOCK_INVITE)) {
1189 /* Send notify to the client that is invited to the channel */
1190 SilcBuffer idp, idp2;
1191 idp = silc_id_payload_encode(SILC_ID_GET_ID(id), SILC_ID_CHANNEL);
1192 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
1193 silc_server_send_notify_dest(server, dest_sock, FALSE,
1194 SILC_ID_GET_ID(id2), SILC_ID_CLIENT,
1195 SILC_NOTIFY_TYPE_INVITE, 3,
1196 idp->data, silc_buffer_len(idp),
1197 channel->channel_name,
1198 strlen(channel->channel_name),
1199 idp2->data, silc_buffer_len(idp2));
1200 silc_buffer_free(idp);
1201 silc_buffer_free(idp2);
1205 /* Get the invite information */
1206 tmp = silc_argument_get_arg_type(cmd->args, 4, &len2);
1207 if (tmp && len2 > 2) {
1208 /* Parse the arguments to see they are constructed correctly */
1209 SILC_GET16_MSB(argc, tmp);
1210 args = silc_argument_payload_parse(tmp + 2, len2 - 2, argc);
1212 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1213 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1218 /* Get the type of action */
1219 atype = silc_argument_get_arg_type(cmd->args, 3, &len);
1220 if (atype && len == 1) {
1221 if (atype[0] == 0x00) {
1222 /* Allocate hash table for invite list if it doesn't exist yet */
1223 if (!channel->invite_list)
1224 channel->invite_list =
1225 silc_hash_table_alloc(0, silc_hash_ptr,
1227 silc_server_inviteban_destruct, channel,
1230 /* Check for resource limit */
1231 if (silc_hash_table_count(channel->invite_list) > 64) {
1232 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1233 SILC_STATUS_ERR_RESOURCE_LIMIT,
1239 /* Now add or delete the information. */
1240 if (!silc_server_inviteban_process(server, channel->invite_list,
1241 (SilcUInt8)atype[0], args)) {
1242 silc_server_command_send_status_reply(
1243 cmd, SILC_COMMAND_INVITE,
1244 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1249 silc_argument_payload_free(args);
1252 /* Encode invite list */
1254 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1255 list = silc_buffer_alloc_size(2);
1256 silc_buffer_format(list,
1257 SILC_STR_UI_SHORT(silc_hash_table_count(
1258 channel->invite_list)),
1260 silc_hash_table_list(channel->invite_list, &htl);
1261 while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2))
1262 list = silc_argument_payload_encode_one(list, tmp2->data,
1263 silc_buffer_len(tmp2), type);
1264 silc_hash_table_list_reset(&htl);
1267 /* The notify is sent to local servers (not clients), and to network. */
1268 if (atype && tmp && len2) {
1269 silc_buffer_set(&alist, tmp, len2);
1271 /* Send to local servers if we are router */
1272 if (server->server_type == SILC_ROUTER) {
1273 SilcBuffer idp, idp2;
1274 idp = silc_id_payload_encode(SILC_ID_GET_ID(id), SILC_ID_CHANNEL);
1275 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
1276 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, FALSE,
1277 SILC_NOTIFY_TYPE_INVITE, 5,
1278 idp->data, silc_buffer_len(idp),
1279 channel->channel_name,
1280 strlen(channel->channel_name),
1281 idp2->data, silc_buffer_len(idp2),
1283 tmp ? alist.data : NULL,
1284 tmp ? silc_buffer_len(&alist) : 0);
1285 silc_buffer_free(idp);
1286 silc_buffer_free(idp2);
1289 /* Send to network */
1290 silc_server_send_notify_invite(server, SILC_PRIMARY_ROUTE(server),
1291 SILC_BROADCAST(server), channel,
1293 tmp ? &alist : NULL);
1296 /* Send invite list back only if the list was modified, or no arguments
1299 argc = silc_argument_get_arg_num(cmd->args);
1302 if (silc_argument_get_arg_type(cmd->args, 3, &len))
1305 /* Send command reply */
1306 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1307 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_INVITE,
1308 SILC_STATUS_OK, 0, ident, 2,
1312 type && list ? silc_buffer_len(list) : 0);
1313 silc_buffer_free(list);
1316 silc_server_command_free(cmd);
1320 SilcPacketStream sock;
1324 /* Quits connection to client. This gets called if client won't
1325 close the connection even when it has issued QUIT command. */
1327 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
1329 SilcServer server = app_context;
1330 QuitInternal q = (QuitInternal)context;
1331 SilcClientEntry client = silc_packet_get_context(q->sock);
1334 /* Free all client specific data, such as client entry and entires
1335 on channels this client may be on. */
1336 silc_server_free_client_data(server, q->sock, client,
1338 silc_packet_set_context(q->sock, NULL);
1341 silc_packet_stream_unref(q->sock);
1342 silc_free(q->signoff);
1346 /* Quits SILC session. This is the normal way to disconnect client. */
1348 SILC_SERVER_CMD_FUNC(quit)
1350 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1351 SilcServer server = cmd->server;
1352 SilcPacketStream sock = cmd->sock;
1353 SilcClientEntry client = silc_packet_get_context(sock);
1355 unsigned char *tmp = NULL;
1358 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
1360 if (client->data.conn_type != SILC_CONN_CLIENT)
1364 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1368 q = silc_calloc(1, sizeof(*q));
1370 q->signoff = tmp ? strdup(tmp) : NULL;
1371 silc_packet_stream_ref(q->sock);
1373 /* We quit the connection with little timeout */
1374 silc_schedule_task_add_timeout(server->schedule,
1375 silc_server_command_quit_cb, (void *)q,
1379 silc_server_command_free(cmd);
1382 /* Server side of command KILL. This command is used by router operator
1383 to remove an client from the SILC Network temporarily. */
1385 SILC_SERVER_CMD_FUNC(kill)
1387 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1388 SilcServer server = cmd->server;
1389 SilcClientEntry client = silc_packet_get_context(cmd->sock);
1390 SilcClientEntry remote_client;
1392 unsigned char *tmp, *comment, *auth;
1393 SilcUInt32 tmp_len, tmp_len2, auth_len;
1395 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 3);
1397 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
1400 /* Get authentication payload if present */
1401 auth = silc_argument_get_arg_type(cmd->args, 3, &auth_len);
1404 /* Router operator killing */
1406 /* KILL command works only on router */
1407 if (server->server_type != SILC_ROUTER) {
1408 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1409 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1413 /* Check whether client has the permissions. */
1414 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1415 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1416 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1421 /* Get the client ID */
1422 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
1423 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1424 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
1428 /* Get the client entry */
1429 remote_client = silc_idlist_find_client_by_id(server->local_list,
1432 if (!remote_client) {
1433 remote_client = silc_idlist_find_client_by_id(server->global_list,
1436 if (!remote_client) {
1437 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1438 silc_server_command_send_status_data(cmd, SILC_COMMAND_KILL,
1439 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1440 0, 2, tmp, tmp_len);
1446 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
1447 if (comment && tmp_len2 > 128) {
1449 comment[127] = '\0';
1452 /* If authentication data is provided then verify that killing is
1454 if (auth && auth_len) {
1455 SilcPacketStream sock;
1457 if (!SILC_IS_LOCAL(remote_client) || !remote_client->data.public_key) {
1458 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1459 SILC_STATUS_ERR_OPERATION_ALLOWED,
1464 /* Verify the signature */
1465 if (!silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1466 remote_client->data.public_key, 0,
1467 server->sha1hash, remote_client->id,
1469 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1470 SILC_STATUS_ERR_AUTH_FAILED, 0);
1474 /* Send reply to the sender */
1475 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1476 silc_server_command_send_status_data(cmd, SILC_COMMAND_KILL,
1480 /* Do normal signoff for the destination client */
1481 sock = remote_client->connection;
1482 silc_server_remove_from_channels(server, NULL, remote_client,
1483 TRUE, (char *)"Killed", TRUE, TRUE);
1484 silc_server_free_client_data(server, NULL, remote_client, TRUE,
1486 (unsigned char *)"Killed");
1488 silc_server_close_connection(server, sock);
1490 /* Router operator killing */
1492 /* Send reply to the sender */
1493 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1494 silc_server_command_send_status_data(cmd, SILC_COMMAND_KILL,
1498 /* Check if anyone is watching this nickname */
1499 if (server->server_type == SILC_ROUTER)
1500 silc_server_check_watcher_list(server, client, NULL,
1501 SILC_NOTIFY_TYPE_KILLED);
1503 /* Now do the killing */
1504 silc_server_kill_client(server, remote_client, comment, client->id,
1509 silc_server_command_free(cmd);
1512 /* Server side of command INFO. This sends information about us to
1513 the client. If client requested specific server we will send the
1514 command to that server. */
1516 SILC_SERVER_CMD_FUNC(info)
1518 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1519 SilcServer server = cmd->server;
1523 char *dest_server = NULL, *server_info = NULL, *server_name;
1524 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1525 SilcServerEntry entry = NULL;
1528 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
1530 /* Get server name */
1531 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
1533 /* Check server name. */
1534 dest_server = silc_identifier_check(dest_server, strlen(dest_server),
1535 SILC_STRING_UTF8, 256, &tmp_len);
1537 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1538 SILC_STATUS_ERR_BAD_SERVER, 0);
1544 if (silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1545 /* Check whether we have this server cached */
1546 entry = silc_idlist_find_server_by_id(server->local_list,
1547 SILC_ID_GET_ID(id), TRUE, NULL);
1549 entry = silc_idlist_find_server_by_id(server->global_list,
1550 SILC_ID_GET_ID(id), TRUE, NULL);
1551 if (!entry && server->server_type != SILC_SERVER) {
1552 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
1553 silc_server_command_send_status_data(cmd, SILC_COMMAND_INFO,
1554 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
1555 0, 2, tmp, tmp_len);
1561 if ((!dest_server && !entry) || (entry && entry == server->id_entry) ||
1562 (dest_server && !cmd->pending &&
1563 !memcmp(dest_server, server->server_name, strlen(dest_server)))) {
1564 /* Send our reply */
1565 char info_string[256];
1567 memset(info_string, 0, sizeof(info_string));
1568 snprintf(info_string, sizeof(info_string),
1569 "location: %s server: %s admin: %s <%s>",
1570 server->config->server_info->location,
1571 server->config->server_info->server_type,
1572 server->config->server_info->admin,
1573 server->config->server_info->email);
1575 server_info = info_string;
1576 entry = server->id_entry;
1578 /* Check whether we have this server cached */
1579 if (!entry && dest_server) {
1580 entry = silc_idlist_find_server_by_name(server->global_list,
1581 dest_server, TRUE, NULL);
1583 entry = silc_idlist_find_server_by_name(server->local_list,
1584 dest_server, TRUE, NULL);
1588 if (!cmd->pending &&
1589 server->server_type != SILC_SERVER && entry && !entry->server_info) {
1590 /* Send to the server */
1592 SilcUInt16 old_ident;
1595 cmd->server->stat.commands_sent++;
1597 old_ident = silc_command_get_ident(cmd->payload);
1598 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1599 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1601 silc_server_packet_send(server, entry->connection,
1602 SILC_PACKET_COMMAND, cmd->packet->flags,
1603 tmpbuf->data, silc_buffer_len(tmpbuf));
1605 /* Reprocess this packet after received reply from router */
1606 silc_server_command_pending(server, SILC_COMMAND_INFO,
1607 silc_command_get_ident(cmd->payload),
1608 silc_server_command_info,
1609 silc_server_command_dup(cmd));
1610 cmd->pending = TRUE;
1611 silc_command_set_ident(cmd->payload, old_ident);
1612 silc_buffer_free(tmpbuf);
1616 if (!entry && !cmd->pending && !server->standalone) {
1617 /* Send to the primary router */
1619 SilcUInt16 old_ident;
1622 cmd->server->stat.commands_sent++;
1624 old_ident = silc_command_get_ident(cmd->payload);
1625 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1626 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1628 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1629 SILC_PACKET_COMMAND, cmd->packet->flags,
1630 tmpbuf->data, silc_buffer_len(tmpbuf));
1632 /* Reprocess this packet after received reply from router */
1633 silc_server_command_pending(server, SILC_COMMAND_INFO,
1634 silc_command_get_ident(cmd->payload),
1635 silc_server_command_info,
1636 silc_server_command_dup(cmd));
1637 cmd->pending = TRUE;
1638 silc_command_set_ident(cmd->payload, old_ident);
1639 silc_buffer_free(tmpbuf);
1646 silc_free(dest_server);
1647 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
1648 silc_server_command_send_status_data(cmd, SILC_COMMAND_INFO,
1649 SILC_STATUS_ERR_NO_SUCH_SERVER, 0,
1651 strlen(dest_server));
1657 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1659 server_info = entry->server_info;
1660 server_name = entry->server_name;
1662 /* Send the reply */
1663 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_INFO,
1664 SILC_STATUS_OK, 0, ident, 3,
1665 2, idp->data, silc_buffer_len(idp),
1667 strlen(server_name),
1670 strlen(server_info) : 0);
1671 silc_buffer_free(idp);
1674 silc_free(dest_server);
1675 silc_server_command_free(cmd);
1678 /* Server side of command PING. This just replies to the ping. */
1680 SILC_SERVER_CMD_FUNC(ping)
1682 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1683 SilcServer server = cmd->server;
1688 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PING, cmd, 1, 1);
1691 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
1692 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1693 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1698 if (SILC_ID_SERVER_COMPARE(SILC_ID_GET_ID(id), server->id)) {
1699 /* Send our reply */
1700 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1703 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1704 silc_server_command_send_status_data(cmd, SILC_COMMAND_PING,
1705 SILC_STATUS_ERR_NO_SUCH_SERVER_ID, 0,
1711 silc_server_command_free(cmd);
1714 /* Server side of command STATS. */
1716 SILC_SERVER_CMD_FUNC(stats)
1718 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1719 SilcServer server = cmd->server;
1723 SilcBuffer packet, stats;
1724 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1727 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_STATS, cmd, 1, 1);
1730 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
1731 silc_server_command_send_status_reply(cmd, SILC_COMMAND_STATS,
1732 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1736 /* The ID must be ours */
1737 if (!SILC_ID_SERVER_COMPARE(server->id, SILC_ID_GET_ID(id))) {
1738 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1739 silc_server_command_send_status_data(cmd, SILC_COMMAND_STATS,
1740 SILC_STATUS_ERR_NO_SUCH_SERVER_ID, 0,
1745 /* If we are router then just send everything we got. If we are normal
1746 server then we'll send this to our router to get all the latest
1747 statistical information. */
1748 if (!cmd->pending && server->server_type != SILC_ROUTER &&
1749 !server->standalone) {
1753 cmd->server->stat.commands_sent++;
1755 /* Send request to our router */
1756 idp = silc_id_payload_encode(server->router->id,
1758 packet = silc_command_payload_encode_va(SILC_COMMAND_STATS,
1759 ++server->cmd_ident, 1,
1761 silc_buffer_len(idp));
1762 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1763 SILC_PACKET_COMMAND, 0, packet->data,
1764 silc_buffer_len(packet));
1766 /* Reprocess this packet after received reply from router */
1767 silc_server_command_pending(server, SILC_COMMAND_STATS,
1769 silc_server_command_stats,
1770 silc_server_command_dup(cmd));
1771 cmd->pending = TRUE;
1772 silc_buffer_free(packet);
1773 silc_buffer_free(idp);
1777 /* Send our reply to sender */
1778 uptime = time(NULL) - server->starttime;
1780 stats = silc_buffer_alloc_size(60);
1781 silc_buffer_format(stats,
1782 SILC_STR_UI_INT(server->starttime),
1783 SILC_STR_UI_INT(uptime),
1784 SILC_STR_UI_INT(server->stat.my_clients),
1785 SILC_STR_UI_INT(server->stat.my_channels),
1786 SILC_STR_UI_INT(server->stat.my_server_ops),
1787 SILC_STR_UI_INT(server->stat.my_router_ops),
1788 SILC_STR_UI_INT(server->stat.cell_clients),
1789 SILC_STR_UI_INT(server->stat.cell_channels),
1790 SILC_STR_UI_INT(server->stat.cell_servers),
1791 SILC_STR_UI_INT(server->stat.clients),
1792 SILC_STR_UI_INT(server->stat.channels),
1793 SILC_STR_UI_INT(server->stat.servers),
1794 SILC_STR_UI_INT(server->stat.routers),
1795 SILC_STR_UI_INT(server->stat.server_ops),
1796 SILC_STR_UI_INT(server->stat.router_ops),
1799 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1800 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_STATS,
1801 SILC_STATUS_OK, 0, ident, 2,
1803 3, stats->data, silc_buffer_len(stats));
1804 silc_buffer_free(stats);
1807 silc_server_command_free(cmd);
1810 /* Internal routine to join channel. The channel sent to this function
1811 has been either created or resolved from ID lists. This joins the sent
1812 client to the channel. */
1814 static void silc_server_command_join_channel(SilcServer server,
1815 SilcServerCommandContext cmd,
1816 SilcChannelEntry channel,
1817 SilcClientID *client_id,
1819 SilcBool create_key,
1821 const unsigned char *auth,
1822 SilcUInt32 auth_len,
1823 const unsigned char *cauth,
1824 SilcUInt32 cauth_len)
1826 SilcPacketStream sock = cmd->sock;
1827 SilcIDListData idata = silc_packet_get_context(sock);
1829 SilcUInt32 tmp_len, user_count;
1830 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4], ulimit[4];
1831 SilcClientEntry client;
1832 SilcChannelClientEntry chl;
1833 SilcBuffer reply, chidp, clidp, keyp = NULL;
1834 SilcBuffer user_list, mode_list, invite_list, ban_list;
1835 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1836 char check[512], check2[512];
1837 SilcBool founder = FALSE;
1839 SilcBuffer fkey = NULL, chpklist = NULL;
1840 const char *cipher, *hostname, *ip;
1842 SILC_LOG_DEBUG(("Joining client to channel"));
1847 silc_socket_stream_get_info(sock, NULL, &hostname, &ip, NULL);
1849 /* Get the client entry */
1850 if (idata->conn_type == SILC_CONN_CLIENT) {
1851 client = (SilcClientEntry)idata;
1855 client = silc_server_query_client(server, client_id, FALSE,
1858 if (!resolve || cmd->pending) {
1859 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
1860 silc_server_command_send_status_data(
1861 cmd, SILC_COMMAND_JOIN,
1862 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID, 0,
1867 /* The client info is being resolved. Reprocess this packet after
1868 receiving the reply to the query. */
1869 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1871 silc_server_command_join,
1872 silc_server_command_dup(cmd));
1873 cmd->pending = TRUE;
1877 if (!client->data.public_key &&
1878 (auth || cauth || channel->ban_list ||
1879 (channel->mode & SILC_CHANNEL_MODE_INVITE))) {
1880 if (cmd->pending == 2)
1883 /* We must retrieve the client's public key by sending
1884 GETKEY command. Reprocess this packet after receiving the key */
1885 clidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1886 silc_server_send_command(server, cmd->sock,
1887 SILC_COMMAND_GETKEY, ++server->cmd_ident,
1888 1, 1, clidp->data, silc_buffer_len(clidp));
1889 silc_buffer_free(clidp);
1890 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
1892 silc_server_command_join,
1893 silc_server_command_dup(cmd));
1898 cmd->pending = FALSE;
1902 * Check founder auth payload if provided. If client can gain founder
1903 * privileges it can override various conditions on joining the channel,
1904 * and can have directly the founder mode set on the channel.
1906 if (auth && auth_len && channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
1907 SilcIDListData idata = (SilcIDListData)client;
1908 SilcChannelClientEntry chl2;
1909 SilcHashTableList htl;
1911 if (channel->founder_key && idata->public_key &&
1912 silc_pkcs_public_key_compare(channel->founder_key,
1913 idata->public_key)) {
1914 /* Check whether the client is to become founder */
1915 if (silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1916 channel->founder_key, 0, server->sha1hash,
1917 client->id, SILC_ID_CLIENT)) {
1919 /* There cannot be anyone else as founder on the channel now. This
1920 client is definitely the founder due to this authentication */
1921 silc_hash_table_list(channel->user_list, &htl);
1922 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
1923 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
1924 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1925 silc_server_force_cumode_change(server, NULL, channel, chl2,
1929 silc_hash_table_list_reset(&htl);
1931 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
1938 * Check channel modes
1942 memset(check, 0, sizeof(check));
1943 memset(check2, 0, sizeof(check2));
1944 silc_strncat(check, sizeof(check),
1945 client->nickname, strlen(client->nickname));
1946 silc_strncat(check, sizeof(check), "!", 1);
1947 silc_strncat(check, sizeof(check),
1948 client->username, strlen(client->username));
1949 if (!strchr(client->username, '@')) {
1950 silc_strncat(check, sizeof(check), "@", 1);
1951 silc_strncat(check, sizeof(check),
1952 hostname, strlen(hostname));
1955 silc_strncat(check2, sizeof(check2),
1956 client->nickname, strlen(client->nickname));
1957 if (!strchr(client->nickname, '@')) {
1958 silc_strncat(check2, sizeof(check2), "@", 1);
1959 silc_strncat(check2, sizeof(check2),
1960 SILC_IS_LOCAL(client) ? server->server_name :
1961 client->router->server_name,
1962 SILC_IS_LOCAL(client) ? strlen(server->server_name) :
1963 strlen(client->router->server_name));
1965 silc_strncat(check2, sizeof(check2), "!", 1);
1966 silc_strncat(check2, sizeof(check2),
1967 client->username, strlen(client->username));
1968 if (!strchr(client->username, '@')) {
1969 silc_strncat(check2, sizeof(check2), "@", 1);
1970 silc_strncat(check2, sizeof(check2),
1971 hostname, strlen(hostname));
1974 /* Check invite list if channel is invite-only channel */
1975 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
1976 if (!channel->invite_list ||
1977 !silc_hash_table_count(channel->invite_list) ||
1978 (!silc_server_inviteban_match(server, channel->invite_list,
1980 !silc_server_inviteban_match(server, channel->invite_list,
1981 2, client->data.public_key) &&
1982 !silc_server_inviteban_match(server, channel->invite_list,
1983 1, client->nickname) &&
1984 !silc_server_inviteban_match(server, channel->invite_list,
1986 !silc_server_inviteban_match(server, channel->invite_list,
1988 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
1989 silc_server_command_send_status_data(cmd, SILC_COMMAND_JOIN,
1990 SILC_STATUS_ERR_NOT_INVITED, 0,
1992 silc_buffer_len(chidp));
1993 silc_buffer_free(chidp);
1998 /* Check ban list if it exists. If the client's nickname, server,
1999 username and/or hostname is in the ban list the access to the
2000 channel is denied. */
2001 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
2002 if (silc_server_inviteban_match(server, channel->ban_list,
2004 silc_server_inviteban_match(server, channel->ban_list,
2005 2, client->data.public_key) ||
2006 silc_server_inviteban_match(server, channel->ban_list,
2007 1, client->nickname) ||
2008 silc_server_inviteban_match(server, channel->ban_list,
2010 silc_server_inviteban_match(server, channel->ban_list,
2012 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2013 silc_server_command_send_status_data(
2014 cmd, SILC_COMMAND_JOIN,
2015 SILC_STATUS_ERR_BANNED_FROM_CHANNEL, 0,
2017 silc_buffer_len(chidp));
2018 silc_buffer_free(chidp);
2023 /* Check user count limit if set. */
2024 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
2025 if (silc_hash_table_count(channel->user_list) + 1 >
2026 channel->user_limit) {
2027 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2028 silc_server_command_send_status_data(cmd, SILC_COMMAND_JOIN,
2029 SILC_STATUS_ERR_CHANNEL_IS_FULL,
2031 silc_buffer_len(chidp));
2032 silc_buffer_free(chidp);
2038 /* Check the channel passphrase if set. */
2039 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2040 /* Get passphrase */
2041 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
2043 passphrase = silc_memdup(tmp, tmp_len);
2045 if (!passphrase || !channel->passphrase ||
2046 strlen(channel->passphrase) != strlen(passphrase) ||
2047 memcmp(passphrase, channel->passphrase, strlen(channel->passphrase))) {
2048 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2049 silc_server_command_send_status_data(cmd, SILC_COMMAND_JOIN,
2050 SILC_STATUS_ERR_BAD_PASSWORD, 0,
2052 silc_buffer_len(chidp));
2053 silc_buffer_free(chidp);
2058 /* Verify channel authentication with channel public keys if set. */
2059 if (channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
2060 if (!silc_server_verify_channel_auth(server, channel, client->id,
2061 cauth, cauth_len)) {
2062 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2063 SILC_STATUS_ERR_PERM_DENIED, 0);
2069 * Client is allowed to join to the channel. Make it happen.
2072 /* Check whether the client already is on the channel */
2073 if (silc_server_client_on_channel(client, channel, NULL)) {
2074 clidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
2075 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2076 silc_server_command_send_status_data2(cmd, SILC_COMMAND_JOIN,
2077 SILC_STATUS_ERR_USER_ON_CHANNEL, 0,
2079 silc_buffer_len(clidp),
2081 silc_buffer_len(chidp));
2082 silc_buffer_free(clidp);
2083 silc_buffer_free(chidp);
2087 /* Generate new channel key as protocol dictates */
2089 if (!silc_server_create_channel_key(server, channel, 0))
2092 /* Send the channel key. This is broadcasted to the channel but is not
2093 sent to the client who is joining to the channel. */
2094 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
2095 silc_server_send_channel_key(server, NULL, channel,
2096 server->server_type == SILC_ROUTER ?
2097 FALSE : !server->standalone);
2100 /* Join the client to the channel by adding it to channel's user list.
2101 Add also the channel to client entry's channels list for fast cross-
2103 chl = silc_calloc(1, sizeof(*chl));
2105 chl->client = client;
2106 chl->channel = channel;
2107 silc_hash_table_add(channel->user_list, client, chl);
2108 silc_hash_table_add(client->channels, channel, chl);
2109 channel->user_count++;
2110 channel->disabled = FALSE;
2112 /* Get users on the channel */
2113 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2116 /* Encode Client ID Payload of the original client who wants to join */
2117 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2119 /* Encode command reply packet */
2120 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2121 SILC_PUT32_MSB(channel->mode, mode);
2122 SILC_PUT32_MSB(created, tmp2);
2123 SILC_PUT32_MSB(user_count, tmp3);
2124 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
2125 SILC_PUT32_MSB(channel->user_limit, ulimit);
2127 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2128 unsigned char cid[32];
2130 silc_id_id2str(channel->id, SILC_ID_CHANNEL, cid, sizeof(cid), &cid_len);
2131 cipher = silc_cipher_get_name(channel->send_key);
2132 keyp = silc_channel_key_payload_encode(cid_len, cid,
2133 strlen(cipher), cipher,
2134 channel->key_len / 8, channel->key);
2137 if (channel->founder_key)
2138 fkey = silc_public_key_payload_encode(channel->founder_key);
2140 /* Encode invite list */
2142 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
2143 SilcHashTableList htl;
2145 invite_list = silc_buffer_alloc_size(2);
2146 silc_buffer_format(invite_list,
2147 SILC_STR_UI_SHORT(silc_hash_table_count(
2148 channel->invite_list)),
2151 silc_hash_table_list(channel->invite_list, &htl);
2152 while (silc_hash_table_get(&htl, (void *)&tmp_len, (void *)&reply))
2153 invite_list = silc_argument_payload_encode_one(invite_list,
2155 silc_buffer_len(reply), tmp_len);
2156 silc_hash_table_list_reset(&htl);
2159 /* Encode ban list */
2161 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
2162 SilcHashTableList htl;
2164 ban_list = silc_buffer_alloc_size(2);
2165 silc_buffer_format(ban_list,
2166 SILC_STR_UI_SHORT(silc_hash_table_count(
2167 channel->ban_list)),
2170 silc_hash_table_list(channel->ban_list, &htl);
2171 while (silc_hash_table_get(&htl, (void *)&tmp_len, (void *)&reply))
2172 ban_list = silc_argument_payload_encode_one(ban_list,
2174 silc_buffer_len(reply), tmp_len);
2175 silc_hash_table_list_reset(&htl);
2178 if (channel->channel_pubkeys)
2179 chpklist = silc_server_get_channel_pk_list(server, channel, FALSE, FALSE);
2182 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2183 SILC_STATUS_OK, 0, ident, 16,
2184 2, channel->channel_name,
2185 strlen(channel->channel_name),
2187 silc_buffer_len(chidp),
2189 silc_buffer_len(clidp),
2192 7, keyp ? keyp->data : NULL,
2193 keyp ? silc_buffer_len(keyp) : 0,
2194 8, ban_list ? ban_list->data : NULL,
2196 silc_buffer_len(ban_list): 0,
2197 9, invite_list ? invite_list->data :
2200 silc_buffer_len(invite_list) : 0,
2203 strlen(channel->topic) : 0,
2204 11, silc_hmac_get_name(channel->hmac),
2205 strlen(silc_hmac_get_name(channel->
2208 13, user_list->data,
2209 silc_buffer_len(user_list),
2210 14, mode_list->data,
2211 silc_buffer_len(mode_list),
2212 15, fkey ? fkey->data : NULL,
2213 fkey ? silc_buffer_len(fkey) : 0,
2214 16, chpklist ? chpklist->data : NULL,
2215 chpklist ? silc_buffer_len(chpklist) : 0,
2216 17, (channel->mode &
2217 SILC_CHANNEL_MODE_ULIMIT ?
2220 SILC_CHANNEL_MODE_ULIMIT ?
2221 sizeof(ulimit) : 0));
2223 /* Send command reply */
2224 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2225 reply->data, silc_buffer_len(reply));
2228 cmd->server->stat.commands_sent++;
2230 /* Send JOIN notify to locally connected clients on the channel. If
2231 we are normal server then router will send or have sent JOIN notify
2232 already. However since we've added the client already to our channel
2233 we'll ignore it (in packet_receive.c) so we must send it here. If
2234 we are router then this will send it to local clients and local
2236 SILC_LOG_DEBUG(("Send JOIN notify to channel"));
2237 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
2238 SILC_NOTIFY_TYPE_JOIN, 2,
2239 clidp->data, silc_buffer_len(clidp),
2240 chidp->data, silc_buffer_len(chidp));
2242 /* Update statistics */
2243 server->stat.my_chanclients++;
2244 if (server->server_type == SILC_ROUTER) {
2245 server->stat.cell_chanclients++;
2246 server->stat.chanclients++;
2249 if (!cmd->pending) {
2250 /* Send JOIN notify packet to our primary router */
2251 silc_server_send_notify_join(server, SILC_PRIMARY_ROUTE(server),
2252 SILC_BROADCAST(server), channel, client->id);
2255 /* Distribute the channel key to all backup routers. */
2256 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
2257 keyp->data, silc_buffer_len(keyp), FALSE, TRUE);
2259 /* If client became founder by providing correct founder auth data
2260 notify the mode change to the channel. */
2262 SILC_PUT32_MSB(chl->mode, mode);
2263 SILC_LOG_DEBUG(("Send CUMODE_CHANGE notify to channel"));
2264 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
2265 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
2267 silc_buffer_len(clidp),
2268 mode, 4, clidp->data,
2269 silc_buffer_len(clidp),
2270 fkey ? fkey->data : NULL,
2271 fkey ? silc_buffer_len(fkey) : 0);
2275 /* Set CUMODE notify type to network */
2277 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
2278 SILC_BROADCAST(server), channel,
2279 chl->mode, client->id, SILC_ID_CLIENT,
2280 client->id, channel->founder_key);
2282 silc_buffer_free(reply);
2283 silc_buffer_free(clidp);
2284 silc_buffer_free(chidp);
2285 silc_buffer_free(keyp);
2286 silc_buffer_free(user_list);
2287 silc_buffer_free(mode_list);
2288 silc_buffer_free(fkey);
2289 silc_buffer_free(chpklist);
2290 silc_buffer_free(invite_list);
2291 silc_buffer_free(ban_list);
2295 memset(passphrase, 0, strlen(passphrase));
2296 silc_free(passphrase);
2299 /* Server side of command JOIN. Joins client into requested channel. If
2300 the channel does not exist it will be created. */
2302 SILC_SERVER_CMD_FUNC(join)
2304 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2305 SilcServer server = cmd->server;
2306 SilcIDListData idata = silc_packet_get_context(cmd->sock);
2307 unsigned char *auth, *cauth;
2308 SilcUInt32 tmp_len, auth_len, cauth_len;
2309 char *tmp, *channel_name, *channel_namec = NULL, *cipher, *hmac;
2310 SilcChannelEntry channel;
2311 SilcUInt32 umode = 0;
2312 SilcBool created = FALSE, create_key = TRUE;
2315 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 2, 7);
2317 /* Get channel name */
2318 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2320 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2321 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2326 /* Truncate over long channel names */
2327 if (tmp_len > 256) {
2328 tmp[tmp_len - 1] = '\0';
2333 /* Check for valid channel name. This is cached, the original is saved
2334 in the channel context. */
2335 channel_namec = silc_channel_name_check(tmp, tmp_len, SILC_STRING_UTF8, 256,
2337 if (!channel_namec) {
2338 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2339 SILC_STATUS_ERR_BAD_CHANNEL, 0);
2343 /* Get Client ID of the client who is joining to the channel */
2344 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
2345 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2346 SILC_STATUS_ERR_NO_CLIENT_ID,
2350 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2352 /* Get cipher, hmac name and auth payload */
2353 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
2354 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
2355 auth = silc_argument_get_arg_type(cmd->args, 6, &auth_len);
2356 cauth = silc_argument_get_arg_type(cmd->args, 7, &cauth_len);
2358 /* See if the channel exists */
2359 channel = silc_idlist_find_channel_by_name(server->local_list,
2360 channel_namec, NULL);
2362 if (idata->conn_type == SILC_CONN_CLIENT) {
2363 SilcClientEntry entry = (SilcClientEntry)idata;
2365 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2366 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2371 #ifndef SILC_DIST_INPLACE
2372 /* Limit how many channels client can join */
2373 if (!cmd->pending && entry->channels &&
2374 silc_hash_table_count(entry->channels) >=
2375 server->config->param.chlimit) {
2376 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2377 SILC_STATUS_ERR_RESOURCE_LIMIT,
2381 #endif /* SILC_DIST_INPLACE */
2384 (channel->disabled && server->server_type != SILC_ROUTER)) {
2385 /* Channel not found or not valid */
2387 /* If we are standalone server we don't have a router, we just create
2388 the channel by ourselves (unless it existed). */
2389 if (server->standalone) {
2391 channel = silc_server_create_new_channel(server, server->id, cipher,
2392 hmac, channel_name, TRUE);
2394 silc_server_command_send_status_data(
2395 cmd, SILC_COMMAND_JOIN,
2396 SILC_STATUS_ERR_UNKNOWN_ALGORITHM,
2397 0, 2, cipher, strlen(cipher));
2401 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2407 /* The channel does not exist on our server. If we are normal server
2408 we will send JOIN command to our router which will handle the
2409 joining procedure (either creates the channel if it doesn't exist
2410 or joins the client to it). */
2411 if (server->server_type != SILC_ROUTER) {
2413 SilcUInt16 old_ident;
2415 /* If this is pending command callback then we've resolved
2416 it and it didn't work, return since we've notified the
2417 client already in the command reply callback. */
2422 cmd->server->stat.commands_sent++;
2424 old_ident = silc_command_get_ident(cmd->payload);
2425 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2426 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2428 /* Send JOIN command to our router */
2429 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2430 SILC_PACKET_COMMAND, cmd->packet->flags,
2431 tmpbuf->data, silc_buffer_len(tmpbuf));
2433 /* Reprocess this packet after received reply from router */
2434 silc_server_command_pending(server, SILC_COMMAND_JOIN,
2435 silc_command_get_ident(cmd->payload),
2436 silc_server_command_join,
2437 silc_server_command_dup(cmd));
2438 cmd->pending = TRUE;
2439 silc_command_set_ident(cmd->payload, old_ident);
2440 silc_buffer_free(tmpbuf);
2444 /* We are router and the channel does not seem exist so we will check
2445 our global list as well for the channel. */
2446 channel = silc_idlist_find_channel_by_name(server->global_list,
2447 channel_namec, NULL);
2449 /* Channel really does not exist, create it */
2450 channel = silc_server_create_new_channel(server, server->id, cipher,
2451 hmac, channel_name, TRUE);
2453 silc_server_command_send_status_data(
2454 cmd, SILC_COMMAND_JOIN,
2455 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
2456 2, cipher, strlen(cipher));
2460 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2468 /* Channel not found */
2470 /* If the command came from router and we are normal server then
2471 something went wrong with the joining as the channel was not found.
2472 We can't do anything else but ignore this. */
2473 if (idata->conn_type == SILC_CONN_ROUTER ||
2474 server->server_type != SILC_ROUTER)
2477 /* We are router and the channel does not seem exist so we will check
2478 our global list as well for the channel. */
2479 channel = silc_idlist_find_channel_by_name(server->global_list,
2480 channel_namec, NULL);
2482 /* Channel really does not exist, create it */
2483 channel = silc_server_create_new_channel(server, server->id, cipher,
2484 hmac, channel_name, TRUE);
2486 silc_server_command_send_status_data(
2487 cmd, SILC_COMMAND_JOIN,
2488 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
2489 2, cipher, strlen(cipher));
2493 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2500 /* Check whether the channel was created by our router */
2501 if (cmd->pending && context2) {
2502 SilcServerCommandReplyContext reply = context2;
2504 if (silc_command_get(reply->payload) == SILC_COMMAND_JOIN) {
2505 tmp = silc_argument_get_arg_type(reply->args, 6, NULL);
2506 SILC_GET32_MSB(created, tmp);
2507 if (silc_argument_get_arg_type(reply->args, 7, NULL))
2508 create_key = FALSE; /* Router returned the key already */
2510 if (silc_command_get_status(reply->payload, NULL, NULL) &&
2511 channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2512 /* Save channel passphrase, if user provided it successfully */
2515 pa = silc_argument_get_arg_type(cmd->args, 3, &pa_len);
2517 silc_free(channel->passphrase);
2518 channel->passphrase = silc_memdup(pa, pa_len);
2523 if (silc_command_get(reply->payload) == SILC_COMMAND_WHOIS &&
2524 !channel->disabled && !silc_hash_table_count(channel->user_list))
2528 /* If the channel does not have global users and is also empty the client
2529 will be the channel founder and operator. */
2530 if (!channel->disabled &&
2531 !channel->global_users && !silc_hash_table_count(channel->user_list))
2532 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2534 /* Join to the channel */
2535 silc_server_command_join_channel(server, cmd, channel, SILC_ID_GET_ID(id),
2536 created, create_key, umode,
2537 auth, auth_len, cauth, cauth_len);
2540 silc_free(channel_namec);
2541 silc_server_command_free(cmd);
2544 /* Server side of command MOTD. Sends server's current "message of the
2545 day" to the client. */
2547 SILC_SERVER_CMD_FUNC(motd)
2549 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2550 SilcServer server = cmd->server;
2552 char *motd, *dest_server = NULL;
2553 SilcUInt32 motd_len;
2554 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2556 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
2558 /* Get server name */
2559 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2561 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2562 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2567 /* Check server name */
2568 dest_server = silc_identifier_check(dest_server, strlen(dest_server),
2569 SILC_STRING_UTF8, 256, NULL);
2571 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2572 SILC_STATUS_ERR_BAD_SERVER,
2577 if (!memcmp(dest_server, server->server_name, strlen(dest_server))) {
2580 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
2582 if (server->config && server->config->server_info &&
2583 server->config->server_info->motd_file) {
2585 motd = silc_file_readfile(server->config->server_info->motd_file,
2589 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_MOTD,
2590 SILC_STATUS_OK, 0, ident, 1,
2591 2, idp->data, silc_buffer_len(idp));
2596 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_MOTD,
2597 SILC_STATUS_OK, 0, ident, 2,
2598 2, idp->data, silc_buffer_len(idp),
2602 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_MOTD,
2603 SILC_STATUS_OK, 0, ident, 1,
2604 2, idp->data, silc_buffer_len(idp));
2606 silc_buffer_free(idp);
2608 SilcServerEntry entry;
2610 /* Check whether we have this server cached */
2611 entry = silc_idlist_find_server_by_name(server->global_list,
2612 dest_server, TRUE, NULL);
2614 entry = silc_idlist_find_server_by_name(server->local_list,
2615 dest_server, TRUE, NULL);
2618 if (server->server_type != SILC_SERVER && !cmd->pending &&
2619 entry && !entry->motd) {
2620 /* Send to the server */
2622 SilcUInt16 old_ident;
2625 cmd->server->stat.commands_sent++;
2627 old_ident = silc_command_get_ident(cmd->payload);
2628 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2629 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2631 silc_server_packet_send(server, entry->connection,
2632 SILC_PACKET_COMMAND, cmd->packet->flags,
2633 tmpbuf->data, silc_buffer_len(tmpbuf));
2635 /* Reprocess this packet after received reply from router */
2636 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2637 silc_command_get_ident(cmd->payload),
2638 silc_server_command_motd,
2639 silc_server_command_dup(cmd));
2640 cmd->pending = TRUE;
2641 silc_command_set_ident(cmd->payload, old_ident);
2642 silc_buffer_free(tmpbuf);
2646 /* Send to primary router only if we don't know the server
2647 * the client requested or if the server is not locally connected */
2648 if ((!entry || !(entry->data.status & SILC_IDLIST_STATUS_LOCAL))
2649 && !cmd->pending && !server->standalone) {
2650 /* Send to the primary router */
2652 SilcUInt16 old_ident;
2655 cmd->server->stat.commands_sent++;
2657 old_ident = silc_command_get_ident(cmd->payload);
2658 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2659 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2661 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2662 SILC_PACKET_COMMAND, cmd->packet->flags,
2663 tmpbuf->data, silc_buffer_len(tmpbuf));
2665 /* Reprocess this packet after received reply from router */
2666 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2667 silc_command_get_ident(cmd->payload),
2668 silc_server_command_motd,
2669 silc_server_command_dup(cmd));
2670 cmd->pending = TRUE;
2671 silc_command_set_ident(cmd->payload, old_ident);
2672 silc_buffer_free(tmpbuf);
2677 silc_free(dest_server);
2678 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2679 silc_server_command_send_status_data(cmd, SILC_COMMAND_MOTD,
2680 SILC_STATUS_ERR_NO_SUCH_SERVER, 0,
2682 strlen(dest_server));
2687 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2688 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_MOTD,
2689 SILC_STATUS_OK, 0, ident, 2,
2690 2, idp->data, silc_buffer_len(idp),
2693 strlen(entry->motd) : 0);
2694 silc_buffer_free(idp);
2698 silc_free(dest_server);
2699 silc_server_command_free(cmd);
2702 /* Server side of command UMODE. Client can use this command to set/unset
2703 user mode. Client actually cannot set itself to be as server/router
2704 operator so this can be used only to unset the modes. */
2706 SILC_SERVER_CMD_FUNC(umode)
2708 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2709 SilcServer server = cmd->server;
2710 SilcClientEntry client = silc_packet_get_context(cmd->sock);
2711 unsigned char *tmp_mask, m[4];
2712 SilcUInt32 mask = 0;
2713 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2714 SilcBool set_mask = FALSE;
2716 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
2719 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 1, 2);
2721 /* Get the client's mode mask */
2722 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
2724 SILC_GET32_MSB(mask, tmp_mask);
2729 /* Check that mode changing is allowed. */
2730 if (!silc_server_check_umode_rights(server, client, mask)) {
2731 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2732 SILC_STATUS_ERR_PERM_DENIED, 0);
2736 /* Anonymous mode cannot be set by client */
2737 if (mask & SILC_UMODE_ANONYMOUS &&
2738 !(client->mode & SILC_UMODE_ANONYMOUS)) {
2739 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2740 SILC_STATUS_ERR_PERM_DENIED, 0);
2744 /* Update statistics */
2745 if (mask & SILC_UMODE_GONE) {
2746 if (!(client->mode & SILC_UMODE_GONE))
2747 server->stat.my_aways++;
2749 if (client->mode & SILC_UMODE_GONE)
2750 server->stat.my_aways--;
2753 /* If the client has anonymous mode set, preserve it. */
2754 if (client->mode & SILC_UMODE_ANONYMOUS)
2755 mask |= SILC_UMODE_ANONYMOUS;
2757 /* Change the mode */
2758 client->mode = mask;
2760 /* Send UMODE change to primary router */
2761 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
2762 SILC_BROADCAST(server), client->id,
2765 /* Check if anyone is watching this nickname */
2766 if (server->server_type == SILC_ROUTER)
2767 silc_server_check_watcher_list(server, client, NULL,
2768 SILC_NOTIFY_TYPE_UMODE_CHANGE);
2771 /* Send command reply to sender */
2772 SILC_PUT32_MSB(client->mode, m);
2773 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_UMODE,
2774 SILC_STATUS_OK, 0, ident, 1,
2778 silc_server_command_free(cmd);
2781 /* Server side command of CMODE. Changes channel mode */
2783 SILC_SERVER_CMD_FUNC(cmode)
2785 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2786 SilcServer server = cmd->server;
2787 SilcClientEntry client = silc_packet_get_context(cmd->sock);
2788 SilcIDListData idata = (SilcIDListData)client;
2790 SilcChannelEntry channel;
2791 SilcChannelClientEntry chl;
2793 unsigned char *tmp, *tmp_id, *tmp_mask, *chpkdata = NULL;
2794 char *cipher = NULL, *hmac = NULL, *passphrase = NULL, ulimit[4];
2795 SilcUInt32 mode_mask = 0, old_mask = 0, tmp_len, tmp_len2, chpklen;
2796 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2797 SilcBool set_mask = FALSE, set_chpk = FALSE;
2798 SilcPublicKey founder_key = NULL;
2799 SilcBuffer fkey = NULL, chpklist = NULL;
2800 SilcBufferStruct chpk;
2803 silc_server_command_free(cmd);
2807 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 1, 9);
2809 /* Get Channel ID */
2810 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
2811 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2812 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2813 silc_server_command_free(cmd);
2817 /* Get channel entry */
2818 channel = silc_idlist_find_channel_by_id(server->local_list,
2819 SILC_ID_GET_ID(id), NULL);
2821 channel = silc_idlist_find_channel_by_id(server->global_list,
2822 SILC_ID_GET_ID(id), NULL);
2824 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
2825 silc_server_command_send_status_data(cmd, SILC_COMMAND_CMODE,
2826 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
2827 0, 2, tmp_id, tmp_len2);
2828 silc_server_command_free(cmd);
2832 old_mask = channel->mode;
2834 /* Get the channel mode mask */
2835 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2837 SILC_GET32_MSB(mode_mask, tmp_mask);
2841 /* Check whether this client is on the channel */
2842 if (!silc_server_client_on_channel(client, channel, &chl)) {
2843 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
2844 silc_server_command_send_status_data(cmd, SILC_COMMAND_CMODE,
2845 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
2846 2, tmp_id, tmp_len2);
2850 /* Check that client has rights to change any requested channel modes */
2851 if (set_mask && !silc_server_check_cmode_rights(server, channel, chl,
2853 SILC_LOG_DEBUG(("Client does not have rights to change mode"));
2854 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
2855 silc_server_command_send_status_data(
2856 cmd, SILC_COMMAND_CMODE,
2857 (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) ?
2858 SILC_STATUS_ERR_NO_CHANNEL_PRIV :
2859 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV), 0,
2860 2, tmp_id, tmp_len2);
2864 /* If mode mask was not sent as argument then merely return the current
2865 mode mask, founder key and channel public key list to the sender. */
2868 SILC_PUT32_MSB(channel->mode, m);
2869 if (channel->founder_key)
2870 fkey = silc_public_key_payload_encode(channel->founder_key);
2871 if (channel->channel_pubkeys)
2872 chpklist = silc_server_get_channel_pk_list(server, channel,
2874 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
2875 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_CMODE,
2876 SILC_STATUS_OK, 0, ident, 4,
2877 2, tmp_id, tmp_len2,
2879 4, fkey ? fkey->data : NULL,
2880 fkey ? silc_buffer_len(fkey) : 0,
2881 5, chpklist ? chpklist->data : NULL,
2882 chpklist ? silc_buffer_len(chpklist) : 0);
2887 * Check the modes. Modes that requires nothing special operation are
2891 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
2892 /* Channel uses private keys to protect traffic. Client(s) has set the
2893 key locally they want to use, server does not know that key. */
2894 /* Nothing interesting to do here */
2896 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
2897 /* The mode is removed and we need to generate and distribute
2898 new channel key. Clients are not using private channel keys
2899 anymore after this. */
2901 /* if we don't remove the flag from the mode
2902 * silc_server_create_channel_key won't create a new key */
2903 channel->mode &= ~SILC_CHANNEL_MODE_PRIVKEY;
2905 /* Re-generate channel key */
2906 if (!silc_server_create_channel_key(server, channel, 0))
2909 /* Send the channel key. This sends it to our local clients and if
2910 we are normal server to our router as well. */
2911 silc_server_send_channel_key(server, NULL, channel,
2912 server->server_type == SILC_ROUTER ?
2913 FALSE : !server->standalone);
2915 cipher = (char *)silc_cipher_get_name(channel->send_key);
2916 hmac = (char *)silc_hmac_get_name(channel->hmac);
2920 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
2921 /* User limit is set on channel */
2922 SilcUInt32 user_limit;
2924 /* Get user limit */
2925 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
2927 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
2928 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2929 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2933 SILC_GET32_MSB(user_limit, tmp);
2934 channel->user_limit = user_limit;
2937 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
2938 /* User limit mode is unset. Remove user limit */
2939 channel->user_limit = 0;
2942 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
2943 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
2944 /* Passphrase has been set to channel */
2946 /* Get the passphrase */
2947 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
2949 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2950 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2954 /* Save the passphrase */
2955 passphrase = channel->passphrase = silc_memdup(tmp, strlen(tmp));
2958 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2959 /* Passphrase mode is unset. remove the passphrase */
2960 silc_free(channel->passphrase);
2961 channel->passphrase = NULL;
2965 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
2966 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
2967 /* Cipher to use protect the traffic */
2968 SilcCipher send_key, receive_key, olds, oldr;
2971 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
2973 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2974 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2978 /* Delete old cipher and allocate the new one */
2979 if (!silc_cipher_alloc(cipher, &send_key)) {
2980 silc_server_command_send_status_data(
2981 cmd, SILC_COMMAND_CMODE,
2982 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
2983 2, cipher, strlen(cipher));
2986 if (!silc_cipher_alloc(cipher, &receive_key)) {
2987 silc_server_command_send_status_data(
2988 cmd, SILC_COMMAND_CMODE,
2989 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
2990 2, cipher, strlen(cipher));
2994 olds = channel->send_key;
2995 oldr = channel->receive_key;
2996 channel->send_key = send_key;
2997 channel->receive_key = receive_key;
2999 /* Re-generate channel key */
3000 if (!silc_server_create_channel_key(server, channel, 0)) {
3001 /* We don't have new key, revert to old one */
3002 channel->send_key = olds;
3003 channel->receive_key = oldr;
3007 /* Remove old channel key for good */
3008 silc_cipher_free(olds);
3009 silc_cipher_free(oldr);
3011 /* Send the channel key. This sends it to our local clients and if
3012 we are normal server to our router as well. */
3013 silc_server_send_channel_key(server, NULL, channel,
3014 server->server_type == SILC_ROUTER ?
3015 FALSE : !server->standalone);
3018 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3019 /* Cipher mode is unset. Remove the cipher and revert back to
3021 SilcCipher send_key, receive_key, olds, oldr;
3022 cipher = channel->cipher;
3024 /* Delete old cipher and allocate default one */
3025 if (!silc_cipher_alloc(cipher ? cipher : SILC_DEFAULT_CIPHER,
3027 silc_server_command_send_status_data(
3028 cmd, SILC_COMMAND_CMODE,
3029 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
3030 2, cipher, strlen(cipher));
3033 if (!silc_cipher_alloc(cipher ? cipher : SILC_DEFAULT_CIPHER,
3035 silc_server_command_send_status_data(
3036 cmd, SILC_COMMAND_CMODE,
3037 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
3038 2, cipher, strlen(cipher));
3042 olds = channel->send_key;
3043 oldr = channel->receive_key;
3044 channel->send_key = send_key;
3045 channel->receive_key = receive_key;
3047 /* Re-generate channel key */
3048 if (!silc_server_create_channel_key(server, channel, 0)) {
3049 /* We don't have new key, revert to old one */
3050 channel->send_key = olds;
3051 channel->receive_key = oldr;
3055 /* Remove old channel key for good */
3056 silc_cipher_free(olds);
3057 silc_cipher_free(oldr);
3059 /* Send the channel key. This sends it to our local clients and if
3060 we are normal server to our router as well. */
3061 silc_server_send_channel_key(server, NULL, channel,
3062 server->server_type == SILC_ROUTER ?
3063 FALSE : !server->standalone);
3067 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
3068 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
3069 /* HMAC to use protect the traffic */
3070 unsigned char hash[SILC_HASH_MAXLEN];
3074 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
3076 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3077 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
3081 /* Delete old hmac and allocate the new one */
3082 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
3083 silc_server_command_send_status_data(
3084 cmd, SILC_COMMAND_CMODE,
3085 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
3086 2, hmac, strlen(hmac));
3090 silc_hmac_free(channel->hmac);
3091 channel->hmac = newhmac;
3093 /* Set the HMAC key out of current channel key. The client must do
3095 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
3096 channel->key_len / 8, hash);
3097 silc_hmac_set_key(channel->hmac, hash,
3098 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3099 memset(hash, 0, sizeof(hash));
3102 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
3103 /* Hmac mode is unset. Remove the hmac and revert back to
3106 unsigned char hash[SILC_HASH_MAXLEN];
3107 hmac = channel->hmac_name;
3109 /* Delete old hmac and allocate default one */
3110 if (!silc_hmac_alloc(hmac ? hmac : SILC_DEFAULT_HMAC, NULL, &newhmac)) {
3111 silc_server_command_send_status_data(
3112 cmd, SILC_COMMAND_CMODE,
3113 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
3114 2, hmac, strlen(hmac));
3118 silc_hmac_free(channel->hmac);
3119 channel->hmac = newhmac;
3121 /* Set the HMAC key out of current channel key. The client must do
3123 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
3124 channel->key_len / 8,
3126 silc_hmac_set_key(channel->hmac, hash,
3127 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3128 memset(hash, 0, sizeof(hash));
3132 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3133 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3134 /* Check if the founder public key was received */
3135 founder_key = idata->public_key;
3136 tmp = silc_argument_get_arg_type(cmd->args, 8, &tmp_len);
3138 if (!silc_public_key_payload_decode(tmp, tmp_len, &founder_key)) {
3139 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3140 SILC_STATUS_ERR_AUTH_FAILED,
3145 /* If key was not sent and the channel mode has already founder
3146 then the key was not to be changed. */
3147 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)
3151 /* Set the founder authentication */
3152 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
3154 silc_server_command_send_status_reply(
3155 cmd, SILC_COMMAND_CMODE,
3156 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
3160 /* Verify the payload before setting the mode */
3161 if (!silc_auth_verify_data(tmp, tmp_len, SILC_AUTH_PUBLIC_KEY,
3162 founder_key, 0, server->sha1hash,
3163 client->id, SILC_ID_CLIENT)) {
3164 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3165 SILC_STATUS_ERR_AUTH_FAILED,
3170 /* Save the public key */
3171 if (channel->founder_key)
3172 silc_pkcs_public_key_free(channel->founder_key);
3173 if (silc_argument_get_arg_type(cmd->args, 8, NULL))
3174 channel->founder_key = founder_key;
3176 channel->founder_key = silc_pkcs_public_key_copy(founder_key);
3177 if (!channel->founder_key) {
3178 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3179 SILC_STATUS_ERR_AUTH_FAILED,
3184 fkey = silc_public_key_payload_encode(channel->founder_key);
3186 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3187 SILC_STATUS_ERR_AUTH_FAILED,
3189 silc_pkcs_public_key_free(channel->founder_key);
3190 channel->founder_key = NULL;
3195 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3196 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3197 if (channel->founder_key)
3198 silc_pkcs_public_key_free(channel->founder_key);
3199 channel->founder_key = NULL;
3205 if (mode_mask & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
3206 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3209 chpkdata = silc_argument_get_arg_type(cmd->args, 9, &chpklen);
3211 if (!chpkdata && channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH)
3216 /* Process the channel public key(s) */
3217 st = silc_server_set_channel_pk_list(server, NULL, channel,
3219 if (st != SILC_STATUS_OK) {
3220 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE, st, 0);
3225 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3226 if (channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
3227 if (channel->channel_pubkeys)
3228 silc_hash_table_free(channel->channel_pubkeys);
3229 channel->channel_pubkeys = NULL;
3236 /* Finally, set the mode */
3237 old_mask = channel->mode = mode_mask;
3239 /* Send CMODE_CHANGE notify. */
3240 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3241 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT)
3242 SILC_PUT32_MSB(channel->user_limit, ulimit);
3243 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
3244 SILC_NOTIFY_TYPE_CMODE_CHANGE, 8,
3245 cidp->data, silc_buffer_len(cidp),
3247 cipher, cipher ? strlen(cipher) : 0,
3248 hmac, hmac ? strlen(hmac) : 0,
3249 passphrase, passphrase ?
3250 strlen(passphrase) : 0,
3251 fkey ? fkey->data : NULL,
3252 fkey ? silc_buffer_len(fkey) : 0,
3253 chpkdata ? chpkdata : NULL,
3254 chpkdata ? chpklen : 0,
3255 mode_mask & SILC_CHANNEL_MODE_ULIMIT ?
3257 mode_mask & SILC_CHANNEL_MODE_ULIMIT ?
3258 sizeof(ulimit) : 0);
3260 /* Set CMODE notify type to network */
3261 if (chpkdata && chpklen)
3262 silc_buffer_set(&chpk, chpkdata, chpklen);
3263 silc_server_send_notify_cmode(server, SILC_PRIMARY_ROUTE(server),
3264 SILC_BROADCAST(server), channel,
3265 mode_mask, client->id, SILC_ID_CLIENT,
3266 cipher, hmac, passphrase, founder_key,
3267 chpkdata ? &chpk : NULL);
3270 chpklist = silc_server_get_channel_pk_list(server, channel, FALSE, FALSE);
3272 /* Send command reply to sender */
3273 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
3274 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_CMODE,
3275 SILC_STATUS_OK, 0, ident, 5,
3276 2, tmp_id, tmp_len2,
3278 4, fkey ? fkey->data : NULL,
3279 fkey ? silc_buffer_len(fkey) : 0,
3280 5, chpklist ? chpklist->data :
3281 NULL, chpklist ? silc_buffer_len(chpklist)
3284 SILC_CHANNEL_MODE_ULIMIT ?
3287 SILC_CHANNEL_MODE_ULIMIT ?
3288 sizeof(ulimit) : 0));
3289 silc_buffer_free(cidp);
3292 channel->mode = old_mask;
3293 silc_buffer_free(chpklist);
3294 silc_buffer_free(fkey);
3295 silc_server_command_free(cmd);
3298 /* Server side of CUMODE command. Changes client's mode on a channel. */
3300 SILC_SERVER_CMD_FUNC(cumode)
3302 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3303 SilcServer server = cmd->server;
3304 SilcClientEntry client = silc_packet_get_context(cmd->sock);
3306 SilcChannelEntry channel;
3307 SilcClientEntry target_client;
3308 SilcChannelClientEntry chl;
3310 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3311 SilcUInt32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3313 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3314 SilcPublicKey founder_key = NULL;
3315 SilcBuffer fkey = NULL;
3320 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
3322 /* Get Channel ID */
3323 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
3324 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3325 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3329 /* Get channel entry */
3330 channel = silc_idlist_find_channel_by_id(server->local_list,
3331 SILC_ID_GET_ID(id), NULL);
3333 channel = silc_idlist_find_channel_by_id(server->global_list,
3334 SILC_ID_GET_ID(id), NULL);
3336 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3337 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3338 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
3339 0, 2, tmp_ch_id, tmp_ch_len);
3344 /* Check whether sender is on the channel */
3345 if (!silc_server_client_on_channel(client, channel, &chl)) {
3346 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3347 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3348 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
3349 2, tmp_ch_id, tmp_ch_len);
3352 sender_mask = chl->mode;
3354 /* Get the target client's channel mode mask */
3355 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3357 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3358 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3362 SILC_GET32_MSB(target_mask, tmp_mask);
3364 /* Get target Client ID */
3365 if (!silc_argument_get_decoded(cmd->args, 3, SILC_ARGUMENT_ID, &id2, NULL)) {
3366 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3367 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3371 /* Get target client's entry */
3372 target_client = silc_idlist_find_client_by_id(server->local_list,
3373 SILC_ID_GET_ID(id2),
3376 target_client = silc_idlist_find_client_by_id(server->global_list,
3377 SILC_ID_GET_ID(id2),
3380 if (target_client != client &&
3381 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3382 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3383 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3384 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3385 SILC_STATUS_ERR_NOT_YOU, 0,
3386 2, tmp_ch_id, tmp_ch_len);
3390 /* Check whether target client is on the channel */
3391 if (target_client != client) {
3392 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3393 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3394 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3395 silc_server_command_send_status_data2(
3396 cmd, SILC_COMMAND_CUMODE,
3397 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL, 0,
3399 3, tmp_ch_id, tmp_ch_len);
3408 /* If the target client is founder, no one else can change their mode
3410 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && client != target_client) {
3411 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3412 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3413 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3414 0, 2, tmp_ch_id, tmp_ch_len);
3418 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3419 if (target_client != client) {
3420 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3421 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3426 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3427 /* The client tries to claim the founder rights. */
3428 unsigned char *tmp_auth;
3429 SilcUInt32 tmp_auth_len;
3430 SilcChannelClientEntry chl2;
3431 SilcHashTableList htl;
3433 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3434 !channel->founder_key) {
3435 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3436 SILC_STATUS_ERR_AUTH_FAILED, 0);
3440 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3442 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3443 SILC_STATUS_ERR_AUTH_FAILED, 0);
3447 /* Verify the authentication payload */
3448 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len, SILC_AUTH_PUBLIC_KEY,
3449 channel->founder_key, 0, server->sha1hash,
3450 client->id, SILC_ID_CLIENT)) {
3451 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3452 SILC_STATUS_ERR_AUTH_FAILED, 0);
3457 founder_key = channel->founder_key;
3458 fkey = silc_public_key_payload_encode(founder_key);
3460 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3461 SILC_STATUS_ERR_AUTH_FAILED, 0);
3465 /* There cannot be anyone else as founder on the channel now. This
3466 client is definitely the founder due to this authentication. This
3467 is done only on router, not on server, since server cannot know
3468 whether router will accept this mode change or not. XXX This
3469 probably shouldn't be done anymore at all, may cause problems in
3470 router-router connections too (maybe just AUTH_FAILED error should
3471 be returned). -Pekka */
3472 if (server->server_type == SILC_ROUTER) {
3473 silc_hash_table_list(channel->user_list, &htl);
3474 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
3475 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
3476 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3477 silc_server_force_cumode_change(server, NULL, channel, chl2,
3481 silc_hash_table_list_reset(&htl);
3484 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
3487 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3488 if (target_client == client) {
3489 /* Remove channel founder rights from itself */
3490 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3493 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3494 SILC_STATUS_ERR_NOT_YOU, 0);
3500 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
3501 /* Promote to operator */
3502 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
3503 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3504 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3505 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3506 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3507 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3508 0, 2, tmp_ch_id, tmp_ch_len);
3512 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
3516 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
3517 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3518 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3519 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3520 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3521 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3522 0, 2, tmp_ch_id, tmp_ch_len);
3526 /* Demote to normal user */
3527 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
3532 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3533 if (target_client != client) {
3534 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3535 SILC_STATUS_ERR_NOT_YOU, 0);
3539 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)) {
3540 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3544 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3545 if (target_client != client) {
3546 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3547 SILC_STATUS_ERR_NOT_YOU, 0);
3551 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3556 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3557 if (target_client != client) {
3558 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3559 SILC_STATUS_ERR_NOT_YOU, 0);
3563 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS)) {
3564 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3568 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3569 if (target_client != client) {
3570 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3571 SILC_STATUS_ERR_NOT_YOU, 0);
3575 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3580 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3581 if (target_client != client) {
3582 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3583 SILC_STATUS_ERR_NOT_YOU, 0);
3587 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS)) {
3588 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3592 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3593 if (target_client != client) {
3594 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3595 SILC_STATUS_ERR_NOT_YOU, 0);
3599 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3604 if (target_mask & SILC_CHANNEL_UMODE_QUIET) {
3605 if (!(chl->mode & SILC_CHANNEL_UMODE_QUIET)) {
3606 if (client == target_client) {
3607 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3608 SILC_STATUS_ERR_PERM_DENIED, 0);
3611 chl->mode |= SILC_CHANNEL_UMODE_QUIET;
3615 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
3616 if (client == target_client) {
3617 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3618 SILC_STATUS_ERR_PERM_DENIED, 0);
3621 chl->mode &= ~SILC_CHANNEL_UMODE_QUIET;
3626 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3627 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3628 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3630 /* Send notify to channel, notify only if mode was actually changed. */
3632 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
3633 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
3634 idp->data, silc_buffer_len(idp),
3637 fkey ? fkey->data : NULL,
3638 fkey ? silc_buffer_len(fkey) : 0);
3640 /* Set CUMODE notify type to network */
3641 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
3642 SILC_BROADCAST(server), channel,
3643 target_mask, client->id, SILC_ID_CLIENT,
3644 target_client->id, founder_key);
3647 /* Send command reply to sender */
3648 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_CUMODE,
3649 SILC_STATUS_OK, 0, ident, 3,
3651 3, tmp_ch_id, tmp_ch_len,
3652 4, tmp_id, tmp_len);
3653 silc_buffer_free(idp);
3656 silc_buffer_free(fkey);
3657 silc_server_command_free(cmd);
3660 /* Server side of KICK command. Kicks client out of channel. */
3662 SILC_SERVER_CMD_FUNC(kick)
3664 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3665 SilcServer server = cmd->server;
3666 SilcClientEntry client = silc_packet_get_context(cmd->sock);
3667 SilcClientEntry target_client;
3669 SilcChannelEntry channel;
3670 SilcChannelClientEntry chl;
3672 SilcUInt32 tmp_len, target_idp_len, clen;
3673 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3674 unsigned char *tmp, *comment, *target_idp;
3679 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
3681 /* Get Channel ID */
3682 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
3683 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3684 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3688 /* Get channel entry */
3689 channel = silc_idlist_find_channel_by_id(server->local_list,
3690 SILC_ID_GET_ID(id), NULL);
3692 channel = silc_idlist_find_channel_by_id(server->local_list,
3693 SILC_ID_GET_ID(id), NULL);
3695 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3696 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3697 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
3698 0, 2, tmp, tmp_len);
3703 /* Check whether sender is on the channel */
3704 if (!silc_server_client_on_channel(client, channel, &chl)) {
3705 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3706 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3707 SILC_STATUS_ERR_NOT_ON_CHANNEL,
3708 0, 2, tmp, tmp_len);
3712 /* Check that the kicker is channel operator or channel founder */
3713 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
3714 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3715 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3716 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3717 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3718 0, 2, tmp, tmp_len);
3722 /* Get target Client ID */
3723 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id2, NULL)) {
3724 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3725 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3729 /* Get target client's entry */
3730 target_client = silc_idlist_find_client_by_id(server->local_list,
3731 SILC_ID_GET_ID(id2),
3734 target_client = silc_idlist_find_client_by_id(server->global_list,
3735 SILC_ID_GET_ID(id2),
3738 /* Check whether target client is on the channel */
3739 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3740 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3741 target_idp = silc_argument_get_arg_type(cmd->args, 2, &target_idp_len);
3742 silc_server_command_send_status_data2(cmd, SILC_COMMAND_KICK,
3743 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL,
3744 0, 2, target_idp, target_idp_len,
3749 /* Check that the target client is not channel founder. Channel founder
3750 cannot be kicked from the channel. */
3751 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3752 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3753 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3754 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3755 0, 2, tmp, tmp_len);
3760 comment = silc_argument_get_arg_type(cmd->args, 3, &clen);
3764 /* Send the reply back to the client */
3765 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3766 target_idp = silc_argument_get_arg_type(cmd->args, 2, &target_idp_len);
3767 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_KICK,
3768 SILC_STATUS_OK, 0, ident, 2,
3770 3, target_idp, target_idp_len);
3772 /* Send KICKED notify to local clients on the channel */
3773 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3774 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
3775 SILC_NOTIFY_TYPE_KICKED, 3,
3776 target_idp, target_idp_len,
3777 comment, comment ? strlen(comment) : 0,
3778 idp->data, silc_buffer_len(idp));
3779 silc_buffer_free(idp);
3781 /* Send KICKED notify to primary route */
3782 silc_server_send_notify_kicked(server, SILC_PRIMARY_ROUTE(server),
3783 SILC_BROADCAST(server), channel,
3784 target_client->id, client->id, comment);
3786 /* Remove the client from channel's invite list */
3787 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
3789 silc_argument_payload_encode_one(NULL, target_idp, target_idp_len, 3);
3790 SilcArgumentPayload args =
3791 silc_argument_payload_parse(ab->data, silc_buffer_len(ab), 1);
3793 silc_server_inviteban_process(server, channel->invite_list, 1, args);
3794 silc_buffer_free(ab);
3795 silc_argument_payload_free(args);
3798 /* Remove the client from the channel. If the channel does not exist
3799 after removing the client then the client kicked itself off the channel
3800 and we don't have to send anything after that. */
3801 if (!silc_server_remove_from_one_channel(server, NULL, channel,
3802 target_client, FALSE))
3805 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3806 /* Re-generate channel key */
3807 if (!silc_server_create_channel_key(server, channel, 0))
3810 /* Send the channel key to the channel. The key of course is not sent
3811 to the client who was kicked off the channel. */
3812 silc_server_send_channel_key(server, target_client->connection, channel,
3813 server->server_type == SILC_ROUTER ?
3814 FALSE : !server->standalone);
3818 silc_server_command_free(cmd);
3821 /* Server side of OPER command. Client uses this comand to obtain server
3822 operator privileges to this server/router. */
3824 SILC_SERVER_CMD_FUNC(oper)
3826 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3827 SilcServer server = cmd->server;
3828 SilcClientEntry client = silc_packet_get_context(cmd->sock);
3829 unsigned char *username = NULL, *auth;
3831 SilcServerConfigAdmin *admin;
3832 SilcIDListData idata = (SilcIDListData)client;
3833 SilcBool result = FALSE;
3834 SilcPublicKey cached_key;
3835 const char *hostname, *ip;
3837 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
3840 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
3842 silc_socket_stream_get_info(cmd->sock, NULL, &hostname, &ip, NULL);
3844 /* Get the username */
3845 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3847 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3848 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3853 /* Check username */
3854 username = silc_identifier_check(username, strlen(username),
3855 SILC_STRING_UTF8, 128, &tmp_len);
3857 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3858 SILC_STATUS_ERR_BAD_USERNAME,
3863 /* Get the admin configuration */
3864 admin = silc_server_config_find_admin(server, (char *)ip,
3865 username, client->nickname);
3867 admin = silc_server_config_find_admin(server, (char *)hostname,
3868 username, client->nickname);
3870 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3871 SILC_STATUS_ERR_AUTH_FAILED,
3873 SILC_LOG_INFO(("OPER authentication failed for username '%s' by "
3874 "nickname '%s' from %s", username,
3875 client->nickname, hostname));
3880 /* Get the authentication payload */
3881 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3883 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3884 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3889 /* Verify the authentication data. If both passphrase and public key
3890 is set then try both of them. */
3891 if (admin->passphrase)
3892 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
3893 admin->passphrase, admin->passphrase_len,
3894 idata->hash, client->id, SILC_ID_CLIENT);
3895 if (!result && admin->publickeys) {
3897 silc_server_get_public_key(server,
3898 SILC_SKR_USAGE_SERVICE_AUTHORIZATION, admin);
3901 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
3902 cached_key, 0, idata->hash,
3903 client->id, SILC_ID_CLIENT);
3906 /* Authentication failed */
3907 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3908 SILC_STATUS_ERR_AUTH_FAILED,
3913 /* Client is now server operator */
3914 client->mode |= SILC_UMODE_SERVER_OPERATOR;
3916 /* Update statistics */
3917 if (SILC_IS_LOCAL(client))
3918 server->stat.my_server_ops++;
3919 if (server->server_type == SILC_ROUTER)
3920 server->stat.server_ops++;
3922 /* Send UMODE change to primary router */
3923 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
3924 SILC_BROADCAST(server), client->id,
3927 /* Check if anyone is watching this nickname */
3928 if (server->server_type == SILC_ROUTER)
3929 silc_server_check_watcher_list(server, client, NULL,
3930 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3932 /* Send reply to the sender */
3933 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3937 silc_free(username);
3938 silc_server_command_free(cmd);
3941 SILC_TASK_CALLBACK(silc_server_command_detach_cb)
3943 SilcServer server = app_context;
3944 QuitInternal q = (QuitInternal)context;
3945 SilcClientID *client_id = (SilcClientID *)q->sock;
3946 SilcClientEntry client;
3947 SilcPacketStream sock;
3949 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3951 if (client && client->connection) {
3952 sock = client->connection;
3954 /* Close the connection on our side */
3955 client->router = NULL;
3956 client->connection = NULL;
3957 silc_server_close_connection(server, sock);
3960 silc_free(client_id);
3964 SILC_TASK_CALLBACK(silc_server_command_detach_timeout)
3966 SilcServer server = app_context;
3967 QuitInternal q = (QuitInternal)context;
3968 SilcClientID *client_id = (SilcClientID *)q->sock;
3969 SilcClientEntry client;
3971 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3973 if (client && client->mode & SILC_UMODE_DETACHED) {
3974 SILC_LOG_DEBUG(("Detach timeout"));
3975 silc_server_free_client_data(server, NULL, client, TRUE,
3979 silc_free(client_id);
3983 /* Server side of DETACH command. Detached the client from the network
3984 by closing the connection but preserving the session. */
3986 SILC_SERVER_CMD_FUNC(detach)
3988 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3989 SilcServer server = cmd->server;
3990 SilcClientEntry client = silc_packet_get_context(cmd->sock);
3993 if (server->config->detach_disabled) {
3994 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
3995 SILC_STATUS_ERR_OPERATION_ALLOWED,
4000 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
4003 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_DETACH, cmd, 0, 0);
4005 /* Remove operator privileges, since the client may resume in some
4006 other server which to it does not have operator privileges. */
4007 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
4008 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
4010 /* Send the user mode notify to notify that client is detached */
4011 client->mode |= SILC_UMODE_DETACHED;
4012 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
4013 client->data.status &= ~SILC_IDLIST_STATUS_NOATTR;
4014 client->last_command = 0;
4015 client->fast_command = 0;
4016 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
4017 SILC_BROADCAST(server), client->id,
4019 server->stat.my_detached++;
4021 /* Check if anyone is watching this nickname */
4022 if (server->server_type == SILC_ROUTER)
4023 silc_server_check_watcher_list(server, client, NULL,
4024 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4026 q = silc_calloc(1, sizeof(*q));
4027 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
4028 silc_schedule_task_add_timeout(server->schedule,
4029 silc_server_command_detach_cb,
4032 if (server->config->detach_timeout) {
4033 q = silc_calloc(1, sizeof(*q));
4034 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
4035 silc_schedule_task_add_timeout(server->schedule,
4036 silc_server_command_detach_timeout,
4037 q, server->config->detach_timeout * 60, 0);
4040 /* Send reply to the sender */
4041 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
4045 silc_server_command_free(cmd);
4048 /* Server side of WATCH command. */
4050 SILC_SERVER_CMD_FUNC(watch)
4052 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4053 SilcServer server = cmd->server;
4054 char *add_nick, *del_nick;
4055 SilcUInt32 add_nick_len, del_nick_len, tmp_len, pk_len;
4056 unsigned char hash[SILC_HASH_MAXLEN], *tmp, *pk, *nick;
4057 SilcClientEntry client;
4059 SilcUInt16 old_ident;
4061 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WATCH, cmd, 1, 3);
4063 if (server->server_type != SILC_ROUTER && !server->standalone) {
4064 if (!cmd->pending) {
4065 /* Send the command to router */
4068 /* If backup receives this from primary, handle it locally */
4069 if (server->server_type == SILC_BACKUP_ROUTER &&
4070 cmd->sock == SILC_PRIMARY_ROUTE(server))
4073 SILC_LOG_DEBUG(("Forwarding WATCH to router"));
4076 cmd->server->stat.commands_sent++;
4078 old_ident = silc_command_get_ident(cmd->payload);
4079 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4080 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4082 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4083 SILC_PACKET_COMMAND, cmd->packet->flags,
4084 tmpbuf->data, silc_buffer_len(tmpbuf));
4086 /* Reprocess this packet after received reply from router */
4087 silc_server_command_pending(server, SILC_COMMAND_WATCH,
4088 silc_command_get_ident(cmd->payload),
4089 silc_server_command_watch,
4090 silc_server_command_dup(cmd));
4091 cmd->pending = TRUE;
4092 silc_command_set_ident(cmd->payload, old_ident);
4093 silc_buffer_free(tmpbuf);
4096 SilcServerCommandReplyContext reply = context2;
4102 silc_command_get_status(reply->payload, &status, NULL);
4104 /* Backup router handles the WATCH command also. */
4105 if (server->server_type != SILC_BACKUP_ROUTER ||
4106 SILC_STATUS_IS_ERROR(status)) {
4107 /* Received reply from router, just send same data to the client. */
4108 SILC_LOG_DEBUG(("Received reply to WATCH from router"));
4109 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH, status,
4116 /* We are router and keep the watch list for local cell */
4119 /* Get the client ID */
4120 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
4121 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4122 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4127 /* Get the client entry which must be in local list */
4128 client = silc_idlist_find_client_by_id(server->local_list,
4129 SILC_ID_GET_ID(id), TRUE, NULL);
4131 /* Backup checks global list also */
4132 if (server->server_type == SILC_BACKUP_ROUTER)
4133 client = silc_idlist_find_client_by_id(server->global_list,
4134 SILC_ID_GET_ID(id), TRUE, NULL);
4136 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4137 silc_server_command_send_status_data(cmd, SILC_COMMAND_WATCH,
4138 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
4139 0, 2, tmp, tmp_len);
4144 /* Take public key for watching by public key */
4145 pk = silc_argument_get_arg_type(cmd->args, 4, &pk_len);
4148 add_nick = silc_argument_get_arg_type(cmd->args, 2, &add_nick_len);
4149 del_nick = silc_argument_get_arg_type(cmd->args, 3, &del_nick_len);
4150 if (!add_nick && !del_nick && !pk) {
4151 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4152 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4157 if (add_nick && add_nick_len > 128) {
4158 add_nick[128] = '\0';
4161 if (del_nick && del_nick_len > 128) {
4162 del_nick[128] = '\0';
4166 /* Add new nickname to be watched in our cell */
4168 nick = silc_identifier_check(add_nick, add_nick_len, SILC_STRING_UTF8, 128,
4171 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4172 SILC_STATUS_ERR_BAD_NICKNAME, 0);
4176 /* Hash the nick, we have the hash saved, not nicks because we can
4177 do one to one mapping to the nick from Client ID hash this way. */
4178 silc_hash_make(server->md5hash, nick, add_nick_len, hash);
4180 /* Check whether this client is already watching this nickname */
4181 if (silc_hash_table_find_by_context(server->watcher_list, hash,
4183 /* Nickname is alredy being watched for this client */
4184 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4185 SILC_STATUS_ERR_NICKNAME_IN_USE,
4191 /* Get the nickname from the watcher list and use the same key in
4192 new entries as well. If key doesn't exist then create it. */
4193 if (!silc_hash_table_find(server->watcher_list, hash, (void *)&tmp, NULL))
4194 tmp = silc_memdup(hash, CLIENTID_HASH_LEN);
4196 /* Add the client to the watcher list with the specified nickname hash. */
4197 silc_hash_table_add(server->watcher_list, tmp, client);
4201 /* Delete nickname from watch list */
4203 nick = silc_identifier_check(del_nick, del_nick_len, SILC_STRING_UTF8, 128,
4206 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4207 SILC_STATUS_ERR_BAD_NICKNAME, 0);
4211 /* Hash the nick, we have the hash saved, not nicks because we can
4212 do one to one mapping to the nick from Client ID hash this way. */
4213 silc_hash_make(server->md5hash, nick, del_nick_len, hash);
4215 /* Check that this client is watching for this nickname */
4216 if (!silc_hash_table_find_by_context(server->watcher_list, hash,
4217 client, (void *)&tmp)) {
4218 /* Nickname is alredy being watched for this client */
4219 silc_server_command_send_status_data(cmd, SILC_COMMAND_WATCH,
4220 SILC_STATUS_ERR_NO_SUCH_NICK, 0,
4221 2, nick, del_nick_len);
4226 /* Delete the nickname from the watcher list. */
4227 silc_hash_table_del_by_context(server->watcher_list, hash, client);
4229 /* Now check whether there still exists entries with this key, if not
4230 then free the key to not leak memory. */
4231 if (!silc_hash_table_find(server->watcher_list, hash, NULL, NULL))
4236 /* Add/del public key */
4239 SilcArgumentPayload pkargs;
4241 SilcPublicKey public_key, pkkey;
4244 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4245 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4250 /* Get the argument from the Argument List Payload */
4251 SILC_GET16_MSB(pkargc, pk);
4252 pkargs = silc_argument_payload_parse(pk + 2, pk_len - 2, pkargc);
4254 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4255 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4260 pk = silc_argument_get_next_arg(pkargs, &type, &pk_len);
4262 if (!silc_public_key_payload_decode(pk, pk_len, &public_key))
4268 /* Add public key to watch list */
4270 /* Check whether this client is already watching this public key */
4271 if (silc_hash_table_find_by_context(server->watcher_list_pk,
4272 public_key, client, NULL)) {
4273 silc_pkcs_public_key_free(public_key);
4274 silc_server_command_send_status_reply(
4275 cmd, SILC_COMMAND_WATCH,
4276 SILC_STATUS_ERR_NICKNAME_IN_USE, 0);
4280 /* Get the public key from the watcher list and use the same key in
4281 new entries as well. If key doesn't exist then create it. */
4283 if (!silc_hash_table_find(server->watcher_list_pk, public_key,
4284 (void *)&pkkey, NULL))
4287 silc_pkcs_public_key_free(public_key);
4289 /* Add the client to the watcher list with the specified public
4291 silc_hash_table_add(server->watcher_list_pk, pkkey, client);
4293 } else if (type == 0x01) {
4294 /* Delete public key from watch list */
4296 /* Check that this client is watching this public key */
4297 if (silc_hash_table_find_by_context(server->watcher_list_pk,
4300 silc_pkcs_public_key_free(public_key);
4301 silc_server_command_send_status_reply(
4302 cmd, SILC_COMMAND_WATCH,
4303 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
4307 /* Delete the public key from the watcher list. */
4308 silc_hash_table_del_by_context(server->watcher_list_pk,
4309 public_key, client);
4311 /* Now check whether there still exists entries with this key, if
4312 not then free the key to not leak memory. */
4313 if (!silc_hash_table_find(server->watcher_list_pk, hash, NULL, NULL))
4314 silc_pkcs_public_key_free(pkkey);
4315 silc_pkcs_public_key_free(public_key);
4318 pk = silc_argument_get_next_arg(pkargs, &type, &pk_len);
4323 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4326 /* Distribute the watch list to backup routers too */
4327 if (server->backup) {
4331 cmd->server->stat.commands_sent++;
4333 old_ident = silc_command_get_ident(cmd->payload);
4334 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4335 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4336 silc_server_backup_send(server, silc_packet_get_context(cmd->sock),
4337 SILC_PACKET_COMMAND,
4338 cmd->packet->flags, tmpbuf->data,
4339 silc_buffer_len(tmpbuf),
4341 silc_command_set_ident(cmd->payload, old_ident);
4342 silc_buffer_free(tmpbuf);
4346 silc_server_command_free(cmd);
4349 /* Server side of SILCOPER command. Client uses this comand to obtain router
4350 operator privileges to this router. */
4352 SILC_SERVER_CMD_FUNC(silcoper)
4354 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4355 SilcServer server = cmd->server;
4356 SilcClientEntry client = silc_packet_get_context(cmd->sock);
4357 unsigned char *username = NULL, *auth;
4359 SilcServerConfigAdmin *admin;
4360 SilcIDListData idata = (SilcIDListData)client;
4361 SilcBool result = FALSE;
4362 SilcPublicKey cached_key;
4363 const char *hostname, *ip;
4365 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
4368 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
4370 silc_socket_stream_get_info(cmd->sock, NULL, &hostname, &ip, NULL);
4372 if (server->server_type != SILC_ROUTER) {
4373 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4374 SILC_STATUS_ERR_AUTH_FAILED, 0);
4378 /* Get the username */
4379 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4381 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4382 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4387 /* Check username */
4388 username = silc_identifier_check(username, tmp_len, SILC_STRING_UTF8, 128,
4391 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4392 SILC_STATUS_ERR_BAD_USERNAME,
4397 /* Get the admin configuration */
4398 admin = silc_server_config_find_admin(server, (char *)ip,
4399 username, client->nickname);
4401 admin = silc_server_config_find_admin(server, (char *)hostname,
4402 username, client->nickname);
4404 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4405 SILC_STATUS_ERR_AUTH_FAILED, 0);
4406 SILC_LOG_INFO(("SILCOPER authentication failed for username '%s' by "
4407 "nickname '%s' from %s", username,
4408 client->nickname, hostname));
4413 /* Get the authentication payload */
4414 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4416 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4417 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4422 /* Verify the authentication data. If both passphrase and public key
4423 is set then try both of them. */
4424 if (admin->passphrase)
4425 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
4426 admin->passphrase, admin->passphrase_len,
4427 idata->hash, client->id, SILC_ID_CLIENT);
4428 if (!result && admin->publickeys) {
4430 silc_server_get_public_key(server,
4431 SILC_SKR_USAGE_SERVICE_AUTHORIZATION, admin);
4434 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
4435 cached_key, 0, idata->hash,
4436 client->id, SILC_ID_CLIENT);
4439 /* Authentication failed */
4440 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4441 SILC_STATUS_ERR_AUTH_FAILED, 0);
4445 /* Client is now router operator */
4446 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4448 /* Update statistics */
4449 if (SILC_IS_LOCAL(client))
4450 server->stat.my_router_ops++;
4451 if (server->server_type == SILC_ROUTER)
4452 server->stat.router_ops++;
4454 /* Send UMODE change to primary router */
4455 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
4456 SILC_BROADCAST(server), client->id,
4459 /* Check if anyone is watching this nickname */
4460 if (server->server_type == SILC_ROUTER)
4461 silc_server_check_watcher_list(server, client, NULL,
4462 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4464 /* Send reply to the sender */
4465 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4469 silc_free(username);
4470 silc_server_command_free(cmd);
4473 /* Server side of command BAN. This is used to manage the ban list of the
4474 channel. To add clients and remove clients from the ban list. */
4476 SILC_SERVER_CMD_FUNC(ban)
4478 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4479 SilcServer server = cmd->server;
4480 SilcClientEntry client = silc_packet_get_context(cmd->sock);
4481 SilcBuffer list, tmp2;
4482 SilcChannelEntry channel;
4483 SilcChannelClientEntry chl;
4485 unsigned char *tmp_id, *tmp, *atype = NULL;
4486 SilcUInt32 id_len, len, len2;
4487 SilcArgumentPayload args;
4488 SilcHashTableList htl;
4490 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
4491 SilcBufferStruct blist;
4493 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
4496 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
4498 /* Get Channel ID */
4499 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
4500 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4501 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4505 /* Get channel entry. The server must know about the channel since the
4506 client is expected to be on the channel. */
4507 channel = silc_idlist_find_channel_by_id(server->local_list,
4508 SILC_ID_GET_ID(id), NULL);
4510 channel = silc_idlist_find_channel_by_id(server->global_list,
4511 SILC_ID_GET_ID(id), NULL);
4513 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4514 silc_server_command_send_status_data(
4515 cmd, SILC_COMMAND_BAN,
4516 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
4517 0, 2, tmp_id, id_len);
4522 /* Check whether this client is on the channel */
4523 if (!silc_server_client_on_channel(client, channel, &chl)) {
4524 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4525 silc_server_command_send_status_data(cmd, SILC_COMMAND_BAN,
4526 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
4531 /* The client must be at least channel operator. */
4532 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4533 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4534 silc_server_command_send_status_data(cmd, SILC_COMMAND_BAN,
4535 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0,
4540 /* Get the ban information */
4541 tmp = silc_argument_get_arg_type(cmd->args, 3, &len2);
4542 if (tmp && len2 > 2) {
4543 /* Parse the arguments to see they are constructed correctly */
4544 SILC_GET16_MSB(argc, tmp);
4545 args = silc_argument_payload_parse(tmp + 2, len2 - 2, argc);
4547 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4548 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4553 /* Get the type of action */
4554 atype = silc_argument_get_arg_type(cmd->args, 2, &len);
4555 if (atype && len == 1) {
4556 if (atype[0] == 0x00) {
4557 /* Allocate hash table for ban list if it doesn't exist yet */
4558 if (!channel->ban_list)
4560 silc_hash_table_alloc(0, silc_hash_ptr,
4562 silc_server_inviteban_destruct, channel,
4565 /* Check for resource limit */
4566 if (silc_hash_table_count(channel->ban_list) > 64) {
4567 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4568 SILC_STATUS_ERR_RESOURCE_LIMIT,
4574 /* Now add or delete the information. */
4575 if (!silc_server_inviteban_process(server, channel->ban_list,
4576 (SilcUInt8)atype[0], args)) {
4577 silc_server_command_send_status_reply(
4578 cmd, SILC_COMMAND_BAN,
4579 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4584 silc_argument_payload_free(args);
4587 /* Encode ban list */
4589 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
4590 list = silc_buffer_alloc_size(2);
4591 silc_buffer_format(list,
4592 SILC_STR_UI_SHORT(silc_hash_table_count(
4593 channel->ban_list)),
4595 silc_hash_table_list(channel->ban_list, &htl);
4596 while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2))
4597 list = silc_argument_payload_encode_one(list, tmp2->data, silc_buffer_len(tmp2),
4599 silc_hash_table_list_reset(&htl);
4602 /* Send BAN notify type to local servers (but not clients) and to
4604 if (atype && tmp && len2) {
4605 silc_buffer_set(&blist, tmp, len2);
4607 /* Send to local servers if we are router */
4608 if (server->server_type == SILC_ROUTER)
4609 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, FALSE,
4610 SILC_NOTIFY_TYPE_BAN, 3,
4613 tmp ? blist.data : NULL,
4614 tmp ? silc_buffer_len(&blist) : 0);
4616 /* Send to network. */
4617 silc_server_send_notify_ban(server, SILC_PRIMARY_ROUTE(server),
4618 SILC_BROADCAST(server), channel, atype,
4622 /* Send the reply back to the client */
4623 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_BAN,
4624 SILC_STATUS_OK, 0, ident, 2,
4626 3, list ? list->data : NULL,
4627 list ? silc_buffer_len(list) : 0);
4628 silc_buffer_free(list);
4631 silc_server_command_free(cmd);
4634 /* Server side command of LEAVE. Removes client from a channel. */
4636 SILC_SERVER_CMD_FUNC(leave)
4638 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4639 SilcServer server = cmd->server;
4640 SilcPacketStream sock = cmd->sock;
4641 SilcClientEntry id_entry = silc_packet_get_context(cmd->sock);
4643 SilcChannelEntry channel;
4647 if (id_entry->data.conn_type != SILC_CONN_CLIENT || !id_entry)
4650 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
4652 /* Get Channel ID */
4653 if (!silc_argument_get_decoded(cmd->args, 1, SILC_ARGUMENT_ID, &id, NULL)) {
4654 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4655 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4659 /* Get channel entry */
4660 channel = silc_idlist_find_channel_by_id(server->local_list,
4661 SILC_ID_GET_ID(id), NULL);
4663 channel = silc_idlist_find_channel_by_id(server->global_list,
4664 SILC_ID_GET_ID(id), NULL);
4666 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4667 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
4668 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
4674 /* Check whether this client is on the channel */
4675 if (!silc_server_client_on_channel(id_entry, channel, NULL)) {
4676 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4677 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
4678 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
4683 /* Notify routers that they should remove this client from their list
4684 of clients on the channel. Send LEAVE notify type. */
4685 silc_server_send_notify_leave(server, SILC_PRIMARY_ROUTE(server),
4686 SILC_BROADCAST(server), channel, id_entry->id);
4688 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4689 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
4690 SILC_STATUS_OK, 0, 2, tmp, len);
4692 /* Remove client from channel */
4693 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4695 /* If the channel does not exist anymore we won't send anything */
4698 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4699 /* Re-generate channel key */
4700 if (!silc_server_create_channel_key(server, channel, 0))
4703 /* Send the channel key */
4704 silc_server_send_channel_key(server, NULL, channel,
4705 server->server_type == SILC_ROUTER ?
4706 FALSE : !server->standalone);
4710 silc_server_command_free(cmd);
4713 /* Server side of command USERS. Resolves clients and their USERS currently
4714 joined on the requested channel. The list of Client ID's and their modes
4715 on the channel is sent back. */
4717 SILC_SERVER_CMD_FUNC(users)
4719 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4720 SilcServer server = cmd->server;
4721 SilcIDListData idata = silc_packet_get_context(cmd->sock);
4722 SilcChannelEntry channel;
4725 unsigned char *channel_id;
4726 SilcUInt32 channel_id_len;
4727 SilcBuffer client_id_list;
4728 SilcBuffer client_mode_list;
4729 unsigned char lc[4];
4730 SilcUInt32 list_count = 0;
4731 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4732 char *channel_name, *channel_namec = NULL;
4734 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
4736 /* Get Channel ID */
4737 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4739 /* Get channel name */
4740 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
4742 if (!channel_id && !channel_name) {
4743 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4744 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4748 /* Check channel name */
4750 channel_namec = silc_channel_name_check(channel_name, strlen(channel_name),
4751 SILC_STRING_UTF8, 256, NULL);
4752 if (!channel_namec) {
4753 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4754 SILC_STATUS_ERR_BAD_CHANNEL, 0);
4759 /* Check Channel ID */
4761 if (!silc_id_payload_parse_id(channel_id, channel_id_len, &id)) {
4762 silc_server_command_send_status_data(cmd, SILC_COMMAND_USERS,
4763 SILC_STATUS_ERR_BAD_CHANNEL_ID, 0,
4764 2, channel_id, channel_id_len);
4769 /* If we are server and we don't know about this channel we will send
4770 the command to our router. If we know about the channel then we also
4771 have the list of users already. */
4773 channel = silc_idlist_find_channel_by_id(server->local_list,
4774 SILC_ID_GET_ID(id), NULL);
4776 channel = silc_idlist_find_channel_by_name(server->local_list,
4777 channel_namec, NULL);
4779 if (!channel || (!server->standalone && (channel->disabled ||
4780 !channel->users_resolved))) {
4781 if (server->server_type != SILC_ROUTER && !server->standalone &&
4786 cmd->server->stat.commands_sent++;
4788 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4789 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4791 /* Send USERS command */
4792 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4793 SILC_PACKET_COMMAND, cmd->packet->flags,
4794 tmpbuf->data, silc_buffer_len(tmpbuf));
4796 /* Reprocess this packet after received reply */
4797 silc_server_command_pending(server, SILC_COMMAND_USERS,
4798 silc_command_get_ident(cmd->payload),
4799 silc_server_command_users,
4800 silc_server_command_dup(cmd));
4801 cmd->pending = TRUE;
4802 silc_command_set_ident(cmd->payload, ident);
4803 silc_buffer_free(tmpbuf);
4807 /* Check the global list as well. */
4809 channel = silc_idlist_find_channel_by_id(server->global_list,
4810 SILC_ID_GET_ID(id), NULL);
4812 channel = silc_idlist_find_channel_by_name(server->global_list,
4813 channel_namec, NULL);
4815 /* Channel really does not exist */
4817 silc_server_command_send_status_data(
4818 cmd, SILC_COMMAND_USERS,
4819 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID, 0,
4820 2, channel_id, channel_id_len);
4822 silc_server_command_send_status_data(
4823 cmd, SILC_COMMAND_USERS,
4824 SILC_STATUS_ERR_NO_SUCH_CHANNEL, 0,
4825 2, channel_name, strlen(channel_name));
4830 /* If the channel is private or secret do not send anything, unless the
4831 user requesting this command is on the channel or is server */
4832 if (idata->conn_type == SILC_CONN_CLIENT) {
4833 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)
4834 && !silc_server_client_on_channel((SilcClientEntry)idata, channel,
4836 silc_server_command_send_status_data(cmd, SILC_COMMAND_USERS,
4837 SILC_STATUS_ERR_NO_SUCH_CHANNEL, 0,
4838 2, channel->channel_name,
4839 strlen(channel->channel_name));
4844 /* Get the users list */
4845 if (!silc_server_get_users_on_channel(server, channel, &client_id_list,
4846 &client_mode_list, &list_count)) {
4848 client_id_list = NULL;
4849 client_mode_list = NULL;
4853 SILC_PUT32_MSB(list_count, lc);
4856 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4857 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_USERS,
4858 SILC_STATUS_OK, 0, ident, 4,
4859 2, idp->data, silc_buffer_len(idp),
4862 client_id_list->data : NULL,
4864 silc_buffer_len(client_id_list) : 0,
4865 5, client_mode_list ?
4866 client_mode_list->data : NULL,
4868 silc_buffer_len(client_mode_list) : 0);
4869 silc_buffer_free(idp);
4871 silc_buffer_free(client_id_list);
4872 if (client_mode_list)
4873 silc_buffer_free(client_mode_list);
4876 silc_free(channel_namec);
4877 silc_server_command_free(cmd);
4880 /* Server side of command GETKEY. This fetches the client's public key
4881 from the server where to the client is connected. */
4883 SILC_SERVER_CMD_FUNC(getkey)
4885 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4886 SilcServer server = cmd->server;
4887 SilcClientEntry client;
4888 SilcServerEntry server_entry;
4889 SilcClientID client_id;
4890 SilcServerID server_id;
4891 SilcIDPayload idp = NULL;
4892 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4895 SilcBuffer pk = NULL;
4897 SilcPublicKey public_key;
4899 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4901 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4902 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4906 idp = silc_id_payload_parse(tmp, tmp_len);
4908 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4909 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4914 id_type = silc_id_payload_get_type(idp);
4915 if (id_type == SILC_ID_CLIENT) {
4916 silc_id_payload_get_id(idp, &client_id, sizeof(client_id));
4918 /* If the client is not found from local list there is no chance it
4919 would be locally connected client so send the command further. */
4920 client = silc_idlist_find_client_by_id(server->local_list,
4921 &client_id, TRUE, NULL);
4923 client = silc_idlist_find_client_by_id(server->global_list,
4924 &client_id, TRUE, NULL);
4926 if ((!client && !cmd->pending && !server->standalone) ||
4927 (client && !client->connection && !cmd->pending &&
4928 !(client->mode & SILC_UMODE_DETACHED)) ||
4929 (client && !client->data.public_key && !cmd->pending)) {
4931 SilcUInt16 old_ident;
4932 SilcPacketStream dest_sock;
4934 dest_sock = silc_server_get_client_route(server, NULL, 0,
4935 &client_id, NULL, NULL);
4940 cmd->server->stat.commands_sent++;
4942 old_ident = silc_command_get_ident(cmd->payload);
4943 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4944 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4946 silc_server_packet_send(server, dest_sock,
4947 SILC_PACKET_COMMAND, cmd->packet->flags,
4948 tmpbuf->data, silc_buffer_len(tmpbuf));
4950 /* Reprocess this packet after received reply from router */
4951 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4952 silc_command_get_ident(cmd->payload),
4953 silc_server_command_getkey,
4954 silc_server_command_dup(cmd));
4955 cmd->pending = TRUE;
4956 silc_command_set_ident(cmd->payload, old_ident);
4957 silc_buffer_free(tmpbuf);
4962 silc_server_command_send_status_data(cmd, SILC_COMMAND_GETKEY,
4963 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
4964 0, 2, tmp, tmp_len);
4968 /* The client is locally connected, just get the public key and
4969 send it back. If they key does not exist then do not send it,
4970 send just OK reply */
4971 public_key = client->data.public_key;
4973 pk = silc_public_key_payload_encode(public_key);
4974 } else if (id_type == SILC_ID_SERVER) {
4975 silc_id_payload_get_id(idp, &server_id, sizeof(server_id));
4977 /* If the server is not found from local list there is no chance it
4978 would be locally connected server so send the command further. */
4979 server_entry = silc_idlist_find_server_by_id(server->local_list,
4980 &server_id, TRUE, NULL);
4982 server_entry = silc_idlist_find_server_by_id(server->global_list,
4983 &server_id, TRUE, NULL);
4985 if (server_entry != server->id_entry &&
4986 ((!server_entry && !cmd->pending && !server->standalone) ||
4987 (server_entry && !server_entry->connection && !cmd->pending &&
4988 !server->standalone) ||
4989 (server_entry && !server_entry->data.public_key && !cmd->pending &&
4990 !server->standalone))) {
4992 SilcUInt16 old_ident;
4995 cmd->server->stat.commands_sent++;
4997 old_ident = silc_command_get_ident(cmd->payload);
4998 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4999 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
5001 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
5002 SILC_PACKET_COMMAND, cmd->packet->flags,
5003 tmpbuf->data, silc_buffer_len(tmpbuf));
5005 /* Reprocess this packet after received reply from router */
5006 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
5007 silc_command_get_ident(cmd->payload),
5008 silc_server_command_getkey,
5009 silc_server_command_dup(cmd));
5010 cmd->pending = TRUE;
5011 silc_command_set_ident(cmd->payload, old_ident);
5012 silc_buffer_free(tmpbuf);
5016 if (!server_entry) {
5017 silc_server_command_send_status_data(cmd, SILC_COMMAND_GETKEY,
5018 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
5019 0, 2, tmp, tmp_len);
5023 /* If they key does not exist then do not send it, send just OK reply */
5024 public_key = (!server_entry->data.public_key ?
5025 (server_entry == server->id_entry ? server->public_key :
5026 NULL) : server_entry->data.public_key);
5028 pk = silc_public_key_payload_encode(public_key);
5033 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5034 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_GETKEY,
5035 SILC_STATUS_OK, 0, ident, 2,
5037 3, pk ? pk->data : NULL,
5038 pk ? silc_buffer_len(pk) : 0);
5042 silc_id_payload_free(idp);
5043 silc_buffer_free(pk);
5044 silc_server_command_free(cmd);
5047 /* Server side of command SERVICE. */
5048 /* XXX currently this just sends empty reply back */
5050 SILC_SERVER_CMD_FUNC(service)
5052 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5053 SilcServer server = cmd->server;
5054 SilcUInt32 tmp_len, auth_len;
5055 unsigned char *service_name, *auth;
5056 SilcBool send_list = FALSE;
5057 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
5059 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SERVICE, cmd, 0, 256);
5061 /* Get requested service */
5062 service_name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5063 if (service_name && tmp_len) {
5064 /* Verify service name */
5065 if (!silc_identifier_verify(service_name, tmp_len,
5066 SILC_STRING_UTF8, 256)) {
5067 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SERVICE,
5068 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5074 /* Get authentication payload if present */
5075 auth = silc_argument_get_arg_type(cmd->args, 2, &auth_len);
5083 /* Send our service list back */
5084 silc_server_send_command_reply(server, cmd->sock, SILC_COMMAND_SERVICE,
5085 SILC_STATUS_OK, 0, ident, 0);
5088 silc_server_command_free(cmd);
5092 /* Private range commands, specific to this implementation */
5094 /* Server side command of CONNECT. Connects us to the specified remote
5095 server or router. */
5097 SILC_SERVER_CMD_FUNC(connect)
5099 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5100 SilcServer server = cmd->server;
5101 SilcClientEntry client = silc_packet_get_context(cmd->sock);
5102 unsigned char *tmp, *host;
5104 SilcUInt32 port = SILC_PORT;
5106 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
5109 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CONNECT, cmd, 1, 2);
5111 /* Check whether client has the permissions. */
5112 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
5113 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
5114 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5115 SILC_STATUS_ERR_NO_SERVER_PRIV, 0);
5119 if (server->server_type == SILC_ROUTER && !server->backup_router &&
5120 client->mode & SILC_UMODE_SERVER_OPERATOR) {
5121 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5122 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
5126 /* Get the remote server */
5127 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5129 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5130 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5136 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
5138 SILC_GET32_MSB(port, tmp);
5140 /* Create the connection. It is done with timeout and is async. */
5141 silc_server_create_connection(server, FALSE, host, port, NULL, NULL);
5143 /* Send reply to the sender */
5144 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5148 silc_server_command_free(cmd);
5151 /* Server side command of CLOSE. Closes connection to a specified server. */
5153 SILC_SERVER_CMD_FUNC(close)
5155 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5156 SilcServer server = cmd->server;
5157 SilcClientEntry client = silc_packet_get_context(cmd->sock);
5158 SilcServerEntry server_entry;
5159 SilcPacketStream sock;
5162 unsigned char *name;
5163 SilcUInt32 port = SILC_PORT;
5165 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
5168 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CLOSE, cmd, 1, 2);
5170 /* Check whether client has the permissions. */
5171 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
5172 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
5173 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5174 SILC_STATUS_ERR_NO_SERVER_PRIV,
5179 /* Get the remote server */
5180 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5182 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5183 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5189 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
5191 SILC_GET32_MSB(port, tmp);
5193 server_entry = silc_idlist_find_server_by_conn(server->local_list,
5194 name, port, TRUE, NULL);
5196 server_entry = silc_idlist_find_server_by_conn(server->global_list,
5197 name, port, TRUE, NULL);
5198 if (!server_entry) {
5199 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5200 SILC_STATUS_ERR_NO_SERVER_ID, 0);
5204 if (server_entry == server->id_entry) {
5205 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5206 SILC_STATUS_ERR_NO_SERVER_ID, 0);
5210 /* Send reply to the sender */
5211 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5214 /* Close the connection to the server */
5215 sock = server_entry->connection;
5217 if (server_entry->server_type == SILC_BACKUP_ROUTER) {
5218 server->backup_closed = TRUE;
5219 silc_server_backup_del(server, server_entry);
5222 server->backup_noswitch = TRUE;
5223 if (server->router == server_entry) {
5224 server->id_entry->router = NULL;
5225 server->router = NULL;
5226 server->standalone = TRUE;
5228 silc_server_disconnect_remote(server, sock,
5229 SILC_STATUS_ERR_BANNED_FROM_SERVER,
5230 "Closed by administrator");
5231 silc_server_free_sock_user_data(server, sock, NULL);
5232 server->backup_noswitch = FALSE;
5235 silc_server_command_free(cmd);
5238 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
5239 active connections. */
5241 SILC_SERVER_CMD_FUNC(shutdown)
5243 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5244 SilcServer server = cmd->server;
5245 SilcClientEntry client = silc_packet_get_context(cmd->sock);
5247 if (client->data.conn_type != SILC_CONN_CLIENT || !client)
5250 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_SHUTDOWN, cmd, 0, 0);
5252 /* Check whether client has the permission. */
5253 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
5254 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
5255 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
5256 SILC_STATUS_ERR_NO_SERVER_PRIV,
5261 /* Send reply to the sender */
5262 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
5265 /* Then, gracefully, or not, bring the server down. */
5266 silc_server_stop(server);
5270 silc_server_command_free(cmd);