5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 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 SilcSocketConnection 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),
78 SILC_SERVER_CMD(connect, PRIV_CONNECT,
79 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
80 SILC_SERVER_CMD(close, PRIV_CLOSE,
81 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
82 SILC_SERVER_CMD(shutdown, PRIV_SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
88 /* Performs several checks to the command. It first checks whether this
89 command was called as pending command callback. If it was then it checks
90 whether error occurred in the command reply where the pending command
93 It also checks that the requested command includes correct amount
95 #define SILC_SERVER_COMMAND_CHECK(command, context, min, max) \
99 if (silc_server_command_pending_error_check(cmd, context2, command)) { \
100 SILC_LOG_DEBUG(("Error occurred in command reply, command not called")); \
101 silc_server_command_free(cmd); \
105 _argc = silc_argument_get_arg_num(cmd->args); \
107 SILC_LOG_DEBUG(("Not enough parameters in command")); \
108 silc_server_command_send_status_reply(cmd, command, \
109 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, \
111 silc_server_command_free(cmd); \
115 SILC_LOG_DEBUG(("Too many parameters in command")); \
116 silc_server_command_send_status_reply(cmd, command, \
117 SILC_STATUS_ERR_TOO_MANY_PARAMS, \
119 silc_server_command_free(cmd); \
124 /* Returns TRUE if the connection is registered. Unregistered connections
125 usually cannot send commands hence the check. */
127 static int silc_server_is_registered(SilcServer server,
128 SilcSocketConnection sock,
129 SilcServerCommandContext cmd,
132 SilcIDListData idata = (SilcIDListData)sock->user_data;
137 if (idata->status & SILC_IDLIST_STATUS_REGISTERED)
140 silc_server_command_send_status_reply(cmd, command,
141 SILC_STATUS_ERR_NOT_REGISTERED, 0);
145 /* Internal context to hold data when executed command with timeout. */
147 SilcServerCommandContext ctx;
148 SilcServerCommand *cmd;
149 } *SilcServerCommandTimeout;
151 /* Timeout callback to process commands with timeout for client. Client's
152 commands are always executed with timeout. */
154 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
156 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
157 SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
160 SILC_LOG_DEBUG(("Client entry is invalid"));
161 silc_server_command_free(timeout->ctx);
166 /* Update access time */
167 client->last_command = time(NULL);
169 if (!(timeout->cmd->flags & SILC_CF_REG)) {
170 SILC_LOG_DEBUG(("Calling %s command",
171 silc_get_command_name(timeout->cmd->cmd)));
172 timeout->cmd->cb(timeout->ctx, NULL);
173 } else if (silc_server_is_registered(timeout->ctx->server,
176 timeout->cmd->cmd)) {
177 SILC_LOG_DEBUG(("Calling %s command",
178 silc_get_command_name(timeout->cmd->cmd)));
179 timeout->cmd->cb(timeout->ctx, NULL);
181 SILC_LOG_DEBUG(("Client is not registered"));
182 silc_server_command_free(timeout->ctx);
188 /* Processes received command packet. */
190 void silc_server_command_process(SilcServer server,
191 SilcSocketConnection sock,
192 SilcPacketContext *packet)
194 SilcServerCommandContext ctx;
195 SilcServerCommand *cmd;
198 /* Allocate command context. This must be free'd by the
199 command routine receiving it. */
200 ctx = silc_server_command_alloc();
201 ctx->server = server;
202 ctx->sock = silc_socket_dup(sock);
203 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
205 /* Parse the command payload in the packet */
206 ctx->payload = silc_command_payload_parse(packet->buffer->data,
207 packet->buffer->len);
209 SILC_LOG_ERROR(("Bad command payload, dropped (%s:%d [%s])",
210 sock->hostname, sock->port,
211 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
212 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
213 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
215 silc_packet_context_free(packet);
216 silc_socket_free(ctx->sock);
220 ctx->args = silc_command_get_args(ctx->payload);
222 /* Get the command */
223 command = silc_command_get(ctx->payload);
224 for (cmd = silc_command_list; cmd->cb; cmd++)
225 if (cmd->cmd == command)
228 if (!cmd || !cmd->cb) {
229 SILC_LOG_DEBUG(("Unknown command %d", command));
230 silc_server_command_send_status_reply(ctx, command,
231 SILC_STATUS_ERR_UNKNOWN_COMMAND, 0);
232 silc_server_command_free(ctx);
236 /* Execute client's commands always with timeout. Normally they are
237 executed with zero (0) timeout but if client is sending command more
238 frequently than once in 2 seconds, then the timeout may be 0 to 2
240 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
241 SilcClientEntry client = (SilcClientEntry)sock->user_data;
242 SilcServerCommandTimeout timeout;
246 SILC_LOG_DEBUG(("Client entry is invalid"));
247 silc_server_command_free(ctx);
251 timeout = silc_calloc(1, sizeof(*timeout));
255 if (client->last_command && (time(NULL) - client->last_command) < 2) {
256 client->fast_command++;
259 if (client->fast_command - 2 <= 0)
260 client->fast_command = 0;
262 client->fast_command -= 2;
266 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
267 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
268 silc_schedule_task_add(server->schedule, sock->sock,
269 silc_server_command_process_timeout, timeout,
270 (client->fast_command < 3 ? 0 :
271 2 - (time(NULL) - client->last_command)),
272 (client->fast_command < 3 ? 200000 : 0),
273 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
275 silc_schedule_task_add(server->schedule, sock->sock,
276 silc_server_command_process_timeout, timeout,
277 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
281 /* Execute for server */
283 if (!(cmd->flags & SILC_CF_REG)) {
284 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
286 } else if (silc_server_is_registered(server, sock, ctx, cmd->cmd)) {
287 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
290 SILC_LOG_DEBUG(("Server is not registered"));
291 silc_server_command_free(ctx);
295 /* Allocate Command Context */
297 SilcServerCommandContext silc_server_command_alloc()
299 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
304 /* Free's the command context allocated before executing the command */
306 void silc_server_command_free(SilcServerCommandContext ctx)
309 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
311 if (ctx->users < 1) {
313 silc_command_payload_free(ctx->payload);
315 silc_packet_context_free(ctx->packet);
317 silc_socket_free(ctx->sock); /* Decrease reference counter */
322 /* Duplicate Command Context by adding reference counter. The context won't
323 be free'd untill it hits zero. */
325 SilcServerCommandContext
326 silc_server_command_dup(SilcServerCommandContext ctx)
329 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
334 /* Timeout for pending command. If reply to pending command never arrives
335 this is called to free resources. */
337 SILC_TASK_CALLBACK(silc_server_command_pending_timeout)
339 SilcServer server = app_context;
340 SilcServerCommandPending *reply = context;
341 SilcServerCommandReplyContext cmdr;
345 SILC_LOG_DEBUG(("Timeout pending command"));
347 /* Allocate temporary and bogus command reply context */
348 cmdr = silc_calloc(1, sizeof(*cmdr));
349 cmdr->server = server;
350 cmdr->ident = reply->ident;
352 /* Check for pending commands and mark to be exeucted */
354 silc_server_command_pending_check(server, reply->reply_cmd,
355 reply->ident, &cmdr->callbacks_count);
357 /* Create bogus command reply with an error inside */
359 silc_command_reply_payload_encode_va(reply->reply_cmd ? reply->reply_cmd :
360 SILC_COMMAND_RESERVED,
361 SILC_STATUS_ERR_TIMEDOUT, 0,
363 cmdr->payload = silc_command_payload_parse(tmpreply->data, tmpreply->len);
364 silc_buffer_free(tmpreply);
366 /* Call all callbacks. Same as SILC_SERVER_PENDING_EXEC macro. */
367 for (i = 0; i < cmdr->callbacks_count; i++)
368 if (cmdr->callbacks[i].callback)
369 (*cmdr->callbacks[i].callback)(cmdr->callbacks[i].context, cmdr);
371 silc_server_command_pending_del(server, reply->reply_cmd, reply->ident);
372 silc_server_command_reply_free(cmdr);
375 /* Add new pending command to be executed when reply to a command has been
376 received. The `reply_cmd' is the command that will call the `callback'
377 with `context' when reply has been received. It can be SILC_COMMAND_NONE
378 to match any command with the `ident'. If `ident' is non-zero
379 the `callback' will be executed when received reply with command
380 identifier `ident'. If there already exists pending command for the
381 specified command, ident, callback and context this function has no
384 bool silc_server_command_pending(SilcServer server,
385 SilcCommand reply_cmd,
387 SilcCommandCb callback,
390 return silc_server_command_pending_timed(server, reply_cmd, ident, callback,
394 /* Same as silc_server_command_pending with specific timeout for pending
395 commands. If the `timeout' is zero default timeout is used. */
397 bool silc_server_command_pending_timed(SilcServer server,
398 SilcCommand reply_cmd,
400 SilcCommandCb callback,
404 SilcServerCommandPending *reply;
406 /* Check whether identical pending already exists for same command,
407 ident, callback and callback context. If it does then it would be
408 error to register it again. */
409 silc_dlist_start(server->pending_commands);
410 while ((reply = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
411 if (reply->reply_cmd == reply_cmd && reply->ident == ident &&
412 reply->callback == callback && reply->context == context)
416 reply = silc_calloc(1, sizeof(*reply));
417 reply->reply_cmd = reply_cmd;
418 reply->ident = ident;
419 reply->context = context;
420 reply->callback = callback;
422 silc_schedule_task_add(server->schedule, 0,
423 silc_server_command_pending_timeout, reply,
424 timeout ? timeout : 10, 0,
425 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
426 silc_dlist_add(server->pending_commands, reply);
431 /* Deletes pending command by reply command type. */
433 void silc_server_command_pending_del(SilcServer server,
434 SilcCommand reply_cmd,
437 SilcServerCommandPending *r;
439 silc_dlist_start(server->pending_commands);
440 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
441 if ((r->reply_cmd == reply_cmd || (r->reply_cmd == SILC_COMMAND_NONE &&
443 && r->ident == ident) {
444 silc_dlist_del(server->pending_commands, r);
446 silc_schedule_task_del(server->schedule, r->timeout);
452 /* Checks for pending commands and marks callbacks to be called from
453 the command reply function. Returns TRUE if there were pending command. */
455 SilcServerCommandPendingCallbacks
456 silc_server_command_pending_check(SilcServer server,
459 SilcUInt32 *callbacks_count)
461 SilcServerCommandPending *r;
462 SilcServerCommandPendingCallbacks callbacks = NULL;
465 silc_dlist_start(server->pending_commands);
466 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
467 if ((r->reply_cmd == command || r->reply_cmd == SILC_COMMAND_NONE)
468 && r->ident == ident) {
469 callbacks = silc_realloc(callbacks, sizeof(*callbacks) * (i + 1));
470 callbacks[i].context = r->context;
471 callbacks[i].callback = r->callback;
472 r->reply_check = TRUE;
477 *callbacks_count = i;
481 /* Sends simple status message as command reply packet */
484 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
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, buffer->len, FALSE);
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,
517 SILC_LOG_DEBUG(("Sending command status %d", status));
520 silc_command_reply_payload_encode_va(command, status, 0,
521 silc_command_get_ident(cmd->payload),
522 1, arg_type, arg, arg_len);
523 silc_server_packet_send(cmd->server, cmd->sock,
524 SILC_PACKET_COMMAND_REPLY, 0,
525 buffer->data, buffer->len, FALSE);
526 silc_buffer_free(buffer);
530 silc_server_command_send_status_data2(SilcServerCommandContext cmd,
534 SilcUInt32 arg_type1,
535 const unsigned char *arg1,
537 SilcUInt32 arg_type2,
538 const unsigned char *arg2,
543 SILC_LOG_DEBUG(("Sending command status %d", status));
546 silc_command_reply_payload_encode_va(command, status, 0,
547 silc_command_get_ident(cmd->payload),
548 2, arg_type1, arg1, arg_len1,
549 arg_type2, arg2, arg_len2);
550 silc_server_packet_send(cmd->server, cmd->sock,
551 SILC_PACKET_COMMAND_REPLY, 0,
552 buffer->data, buffer->len, FALSE);
553 silc_buffer_free(buffer);
556 /* This function can be called to check whether in the command reply
557 an error occurred. This function has no effect if this is called
558 when the command function was not called as pending command callback.
559 This returns TRUE if error had occurred. */
562 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
563 SilcServerCommandReplyContext cmdr,
566 if (!cmd->pending || !cmdr)
569 if (!silc_command_get_status(cmdr->payload, NULL, NULL)) {
572 /* Send the same command reply payload */
573 silc_command_set_command(cmdr->payload, silc_command_get(cmd->payload));
574 silc_command_set_ident(cmdr->payload,
575 silc_command_get_ident(cmd->payload));
576 buffer = silc_command_payload_encode_payload(cmdr->payload);
577 silc_server_packet_send(cmd->server, cmd->sock,
578 SILC_PACKET_COMMAND_REPLY, 0,
579 buffer->data, buffer->len, FALSE);
580 silc_buffer_free(buffer);
587 /* Server side of command WHOIS. */
589 SILC_SERVER_CMD_FUNC(whois)
591 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
592 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 256);
593 silc_server_query_command(cmd->server, SILC_COMMAND_WHOIS, cmd);
594 silc_server_command_free(cmd);
597 /* Server side of command WHOWAS. */
599 SILC_SERVER_CMD_FUNC(whowas)
601 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
602 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
603 silc_server_query_command(cmd->server, SILC_COMMAND_WHOWAS, cmd);
604 silc_server_command_free(cmd);
607 /* Server side of command IDENTIFY. */
609 SILC_SERVER_CMD_FUNC(identify)
611 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
612 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 256);
613 silc_server_query_command(cmd->server, SILC_COMMAND_IDENTIFY, cmd);
614 silc_server_command_free(cmd);
617 /* Server side of command NICK. Sets nickname for user. Setting
618 nickname causes generation of a new client ID for the client. The
619 new client ID is sent to the client after changing the nickname. */
621 SILC_SERVER_CMD_FUNC(nick)
623 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
624 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
625 SilcServer server = cmd->server;
626 SilcBuffer packet, nidp, oidp = NULL;
627 SilcClientID *new_id;
629 unsigned char *nick, *nickc = NULL;
630 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
632 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
635 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
638 nick = silc_argument_get_arg_type(cmd->args, 1, &nick_len);
640 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
641 SILC_STATUS_ERR_BAD_NICKNAME, 0);
645 /* Truncate over long nicks */
646 if (nick_len > 128) {
651 /* Check for valid nickname string. This is cached, original is saved
652 in the client context. */
653 nickc = silc_identifier_check(nick, nick_len, SILC_STRING_UTF8, 128, NULL);
655 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
656 SILC_STATUS_ERR_BAD_NICKNAME, 0);
660 /* Check for same nickname */
661 if (strlen(client->nickname) == nick_len &&
662 !memcmp(client->nickname, nick, nick_len)) {
663 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
668 /* Create new Client ID */
669 if (!silc_id_create_client_id(cmd->server, cmd->server->id,
671 cmd->server->md5hash,
672 nickc, strlen(nickc), &new_id)) {
673 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
674 SILC_STATUS_ERR_BAD_NICKNAME, 0);
679 /* Send notify about nickname change to our router. We send the new
680 ID and ask to replace it with the old one. If we are router the
681 packet is broadcasted. Send NICK_CHANGE notify. */
682 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
683 SILC_BROADCAST(server), client->id,
686 /* Check if anyone is watching the old nickname */
687 if (server->server_type == SILC_ROUTER)
688 silc_server_check_watcher_list(server, client, nick,
689 SILC_NOTIFY_TYPE_NICK_CHANGE);
691 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
693 /* Remove old cache entry */
694 silc_idcache_del_by_context(server->local_list->clients, client);
696 silc_free(client->id);
699 silc_free(client->nickname);
700 client->nickname = strdup(nick);
702 /* Update client cache */
703 silc_idcache_add(server->local_list->clients, nickc,
704 client->id, (void *)client, 0, NULL);
706 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
708 /* Send NICK_CHANGE notify to the client's channels */
709 silc_server_send_notify_on_channels(server, NULL, client,
710 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
711 oidp->data, oidp->len,
712 nidp->data, nidp->len,
714 strlen(client->nickname));
716 /* Check if anyone is watching the new nickname */
717 if (server->server_type == SILC_ROUTER)
718 silc_server_check_watcher_list(server, client, NULL,
719 SILC_NOTIFY_TYPE_NICK_CHANGE);
722 /* Send the new Client ID as reply command back to client */
723 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
724 SILC_STATUS_OK, 0, ident, 2,
725 2, nidp->data, nidp->len,
727 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
728 0, packet->data, packet->len, FALSE);
730 silc_buffer_free(packet);
731 silc_buffer_free(nidp);
733 silc_buffer_free(oidp);
736 silc_server_command_free(cmd);
739 /* Sends the LIST command reply */
742 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
743 SilcChannelEntry *lch,
744 SilcUInt32 lch_count,
745 SilcChannelEntry *gch,
746 SilcUInt32 gch_count)
749 SilcBuffer packet, idp;
750 SilcChannelEntry entry;
752 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
754 unsigned char usercount[4];
756 int valid_lcount = 0, valid_rcount = 0;
758 for (i = 0; i < lch_count; i++) {
759 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
764 for (i = 0; i < gch_count; i++) {
765 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
771 if (!lch_count && !gch_count) {
772 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
777 status = SILC_STATUS_OK;
778 if ((lch_count + gch_count) > 1)
779 status = SILC_STATUS_LIST_START;
782 for (i = 0, k = 0; i < lch_count; i++) {
788 status = SILC_STATUS_LIST_ITEM;
789 if (valid_lcount > 1 && k == valid_lcount - 1 && !valid_rcount)
790 status = SILC_STATUS_LIST_END;
792 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
794 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
796 memset(usercount, 0, sizeof(usercount));
798 topic = entry->topic;
799 users = silc_hash_table_count(entry->user_list);
800 SILC_PUT32_MSB(users, usercount);
805 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
807 2, idp->data, idp->len,
808 3, entry->channel_name,
809 strlen(entry->channel_name),
810 4, topic, topic ? strlen(topic) : 0,
812 silc_server_packet_send(cmd->server, cmd->sock,
813 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
815 silc_buffer_free(packet);
816 silc_buffer_free(idp);
821 for (i = 0, k = 0; i < gch_count; i++) {
827 status = SILC_STATUS_LIST_ITEM;
828 if (valid_rcount > 1 && k == valid_rcount - 1)
829 status = SILC_STATUS_LIST_END;
831 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
833 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
835 memset(usercount, 0, sizeof(usercount));
837 topic = entry->topic;
838 users = entry->user_count;
839 SILC_PUT32_MSB(users, usercount);
844 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
846 2, idp->data, idp->len,
847 3, entry->channel_name,
848 strlen(entry->channel_name),
849 4, topic, topic ? strlen(topic) : 0,
851 silc_server_packet_send(cmd->server, cmd->sock,
852 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
854 silc_buffer_free(packet);
855 silc_buffer_free(idp);
860 /* Server side of LIST command. This lists the channel of the requested
861 server. Secret channels are not listed. */
863 SILC_SERVER_CMD_FUNC(list)
865 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
866 SilcServer server = cmd->server;
867 SilcChannelID *channel_id = NULL;
870 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
871 SilcUInt32 lch_count = 0, gch_count = 0;
873 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 1);
875 /* If we are normal server, send the command to router, since we
876 want to know all channels in the network. */
877 if (!cmd->pending && server->server_type != SILC_ROUTER &&
878 !server->standalone) {
880 SilcUInt16 old_ident;
882 old_ident = silc_command_get_ident(cmd->payload);
883 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
884 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
885 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
886 SILC_PACKET_COMMAND, cmd->packet->flags,
887 tmpbuf->data, tmpbuf->len, TRUE);
889 /* Reprocess this packet after received reply from router */
890 silc_server_command_pending(server, SILC_COMMAND_LIST,
891 silc_command_get_ident(cmd->payload),
892 silc_server_command_list,
893 silc_server_command_dup(cmd));
895 silc_command_set_ident(cmd->payload, old_ident);
896 silc_buffer_free(tmpbuf);
901 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
903 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
905 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
906 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
911 /* Get the channels from local list */
912 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
915 /* Get the channels from global list */
916 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
920 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
921 gchannels, gch_count);
923 silc_free(lchannels);
924 silc_free(gchannels);
927 silc_server_command_free(cmd);
930 /* Server side of TOPIC command. Sets topic for channel and/or returns
931 current topic to client. */
933 SILC_SERVER_CMD_FUNC(topic)
935 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
936 SilcServer server = cmd->server;
937 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
938 SilcChannelID *channel_id;
939 SilcChannelEntry channel;
940 SilcChannelClientEntry chl;
941 SilcBuffer packet, idp;
943 SilcUInt32 argc, tmp_len;
944 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
946 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
949 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
951 argc = silc_argument_get_arg_num(cmd->args);
954 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
956 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
957 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
960 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
962 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
963 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
967 /* Check whether the channel exists */
968 channel = silc_idlist_find_channel_by_id(server->local_list,
971 channel = silc_idlist_find_channel_by_id(server->global_list,
974 silc_server_command_send_status_data(cmd, SILC_COMMAND_TOPIC,
975 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
983 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
985 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
986 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
991 if (strlen(tmp) > 256) {
992 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
993 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
998 if (!silc_utf8_valid(tmp, strlen(tmp))) {
999 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
1000 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1005 /* See whether the client is on channel and has rights to change topic */
1006 if (!silc_server_client_on_channel(client, channel, &chl)) {
1007 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1008 silc_server_command_send_status_data(cmd, SILC_COMMAND_TOPIC,
1009 SILC_STATUS_ERR_NOT_ON_CHANNEL,
1010 0, 2, tmp, tmp_len);
1014 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
1015 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1016 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1017 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1018 silc_server_command_send_status_data(cmd, SILC_COMMAND_TOPIC,
1019 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
1020 0, 2, tmp, tmp_len);
1024 if (!channel->topic || strcmp(channel->topic, tmp)) {
1025 /* Set the topic for channel */
1026 silc_free(channel->topic);
1027 channel->topic = strdup(tmp);
1029 /* Send TOPIC_SET notify type to the network */
1030 silc_server_send_notify_topic_set(server, SILC_PRIMARY_ROUTE(server),
1031 SILC_BROADCAST(server), channel,
1032 client->id, SILC_ID_CLIENT,
1035 /* Send notify about topic change to all clients on the channel */
1036 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1037 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
1038 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
1039 idp->data, idp->len,
1041 strlen(channel->topic));
1042 silc_buffer_free(idp);
1046 /* Send the topic to client as reply packet */
1047 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
1048 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
1049 SILC_STATUS_OK, 0, ident, 2,
1050 2, idp->data, idp->len,
1053 strlen(channel->topic) : 0);
1054 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1055 0, packet->data, packet->len, FALSE);
1057 silc_buffer_free(packet);
1058 silc_buffer_free(idp);
1059 silc_free(channel_id);
1062 silc_server_command_free(cmd);
1065 /* Server side of INVITE command. Invites some client to join some channel.
1066 This command is also used to manage the invite list of the channel. */
1068 SILC_SERVER_CMD_FUNC(invite)
1070 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1071 SilcServer server = cmd->server;
1072 SilcSocketConnection sock = cmd->sock, dest_sock;
1073 SilcChannelClientEntry chl;
1074 SilcClientEntry sender, dest;
1075 SilcClientID *dest_id = NULL;
1076 SilcChannelEntry channel;
1077 SilcChannelID *channel_id = NULL;
1078 SilcIDListData idata;
1079 SilcArgumentPayload args;
1080 SilcHashTableList htl;
1081 SilcBuffer packet, list, tmp2;
1082 SilcBufferStruct alist;
1083 unsigned char *tmp, *atype = NULL;
1084 SilcUInt32 len, type, len2;
1085 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
1087 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
1089 /* Get Channel ID */
1090 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1092 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1093 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
1096 channel_id = silc_id_payload_parse_id(tmp, len, NULL);
1098 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1099 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
1103 /* Get the channel entry */
1104 channel = silc_idlist_find_channel_by_id(server->local_list,
1107 channel = silc_idlist_find_channel_by_id(server->global_list,
1110 silc_server_command_send_status_data(cmd, SILC_COMMAND_INVITE,
1111 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
1117 /* Check whether the sender of this command is on the channel. */
1118 sender = (SilcClientEntry)sock->user_data;
1119 if (!sender || !silc_server_client_on_channel(sender, channel, &chl)) {
1120 silc_server_command_send_status_data(cmd, SILC_COMMAND_INVITE,
1121 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
1126 /* Check whether the channel is invite-only channel. If yes then the
1127 sender of this command must be at least channel operator. */
1128 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
1129 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1130 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1131 silc_server_command_send_status_data(cmd, SILC_COMMAND_INVITE,
1132 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
1137 /* Get destination client ID */
1138 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1142 dest_id = silc_id_payload_parse_id(tmp, len, NULL);
1144 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1145 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
1149 /* Get the client entry */
1150 dest = silc_server_query_client(server, dest_id, FALSE, &resolve);
1152 if (server->server_type != SILC_SERVER || !resolve || cmd->pending) {
1153 silc_server_command_send_status_data(
1154 cmd, SILC_COMMAND_INVITE,
1155 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID, 0,
1160 /* The client info is being resolved. Reprocess this packet after
1161 receiving the reply to the query. */
1162 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1164 silc_server_command_invite,
1165 silc_server_command_dup(cmd));
1166 cmd->pending = TRUE;
1170 /* Check whether the requested client is already on the channel. */
1171 if (silc_server_client_on_channel(dest, channel, NULL)) {
1172 atype = silc_argument_get_arg_type(cmd->args, 1, &len2);
1173 silc_server_command_send_status_data2(cmd, SILC_COMMAND_INVITE,
1174 SILC_STATUS_ERR_USER_ON_CHANNEL,
1180 /* Get route to the client */
1181 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id,
1184 silc_server_command_send_status_data(cmd, SILC_COMMAND_INVITE,
1185 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1190 /* Add the client to the invite list */
1192 /* Allocate hash table for invite list if it doesn't exist yet */
1193 if (!channel->invite_list)
1194 channel->invite_list =
1195 silc_hash_table_alloc(0, silc_hash_ptr,
1197 silc_server_inviteban_destruct, channel, TRUE);
1199 /* Check if the ID is in the list already */
1200 silc_hash_table_list(channel->invite_list, &htl);
1201 while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2)) {
1202 if (type == 3 && !memcmp(tmp2->data, tmp, len)) {
1207 silc_hash_table_list_reset(&htl);
1209 /* Add new Client ID to invite list */
1211 list = silc_buffer_alloc_size(len);
1212 silc_buffer_put(list, tmp, len);
1213 silc_hash_table_add(channel->invite_list, (void *)3, list);
1216 if (!(dest->mode & SILC_UMODE_BLOCK_INVITE)) {
1217 /* Send notify to the client that is invited to the channel */
1218 SilcBuffer idp, idp2;
1219 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
1220 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
1221 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
1223 SILC_NOTIFY_TYPE_INVITE, 3,
1224 idp->data, idp->len,
1225 channel->channel_name,
1226 strlen(channel->channel_name),
1227 idp2->data, idp2->len);
1228 silc_buffer_free(idp);
1229 silc_buffer_free(idp2);
1233 /* Get the invite information */
1234 tmp = silc_argument_get_arg_type(cmd->args, 4, &len2);
1235 if (tmp && len2 > 2) {
1236 /* Parse the arguments to see they are constructed correctly */
1237 SILC_GET16_MSB(argc, tmp);
1238 args = silc_argument_payload_parse(tmp + 2, len2 - 2, argc);
1240 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1241 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1246 /* Get the type of action */
1247 atype = silc_argument_get_arg_type(cmd->args, 3, &len);
1248 if (atype && len == 1) {
1249 if (atype[0] == 0x00) {
1250 /* Allocate hash table for invite list if it doesn't exist yet */
1251 if (!channel->invite_list)
1252 channel->invite_list =
1253 silc_hash_table_alloc(0, silc_hash_ptr,
1255 silc_server_inviteban_destruct, channel,
1258 /* Check for resource limit */
1259 if (silc_hash_table_count(channel->invite_list) > 64) {
1260 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1261 SILC_STATUS_ERR_RESOURCE_LIMIT,
1267 /* Now add or delete the information. */
1268 if (!silc_server_inviteban_process(server, channel->invite_list,
1269 (SilcUInt8)atype[0], args)) {
1270 silc_server_command_send_status_reply(
1271 cmd, SILC_COMMAND_INVITE,
1272 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1277 silc_argument_payload_free(args);
1280 /* Encode invite list */
1282 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1283 list = silc_buffer_alloc_size(2);
1284 silc_buffer_format(list,
1285 SILC_STR_UI_SHORT(silc_hash_table_count(
1286 channel->invite_list)),
1288 silc_hash_table_list(channel->invite_list, &htl);
1289 while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2))
1290 list = silc_argument_payload_encode_one(list, tmp2->data, tmp2->len,
1292 silc_hash_table_list_reset(&htl);
1295 /* The notify is sent to local servers (not clients), and to network. */
1296 if (atype && tmp && len2) {
1297 silc_buffer_set(&alist, tmp, len2);
1299 /* Send to local servers if we are router */
1300 if (server->server_type == SILC_ROUTER) {
1301 SilcBuffer idp, idp2;
1302 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
1303 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
1304 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, FALSE,
1305 SILC_NOTIFY_TYPE_INVITE, 5,
1306 idp->data, idp->len,
1307 channel->channel_name,
1308 strlen(channel->channel_name),
1309 idp2->data, idp2->len,
1311 tmp ? alist.data : NULL,
1312 tmp ? alist.len : 0);
1313 silc_buffer_free(idp);
1314 silc_buffer_free(idp2);
1317 /* Send to network */
1318 silc_server_send_notify_invite(server, SILC_PRIMARY_ROUTE(server),
1319 SILC_BROADCAST(server), channel,
1321 tmp ? &alist : NULL);
1324 /* Send invite list back only if the list was modified, or no arguments
1327 argc = silc_argument_get_arg_num(cmd->args);
1330 if (silc_argument_get_arg_type(cmd->args, 3, &len))
1333 /* Send command reply */
1334 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1335 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
1336 SILC_STATUS_OK, 0, ident, 2,
1340 type && list ? list->len : 0);
1341 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
1342 packet->data, packet->len, FALSE);
1343 silc_buffer_free(packet);
1344 silc_buffer_free(list);
1348 silc_free(channel_id);
1349 silc_server_command_free(cmd);
1353 SilcSocketConnection sock;
1357 /* Quits connection to client. This gets called if client won't
1358 close the connection even when it has issued QUIT command. */
1360 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
1362 SilcServer server = app_context;
1363 QuitInternal q = (QuitInternal)context;
1365 if (q->sock->user_data) {
1366 /* Free all client specific data, such as client entry and entires
1367 on channels this client may be on. */
1368 silc_server_free_client_data(server, q->sock, q->sock->user_data,
1370 q->sock->user_data = NULL;
1373 if (!SILC_IS_DISCONNECTED(q->sock))
1374 /* Close the connection on our side */
1375 silc_server_close_connection(server, q->sock);
1377 silc_socket_free(q->sock);
1378 silc_free(q->signoff);
1382 /* Quits SILC session. This is the normal way to disconnect client. */
1384 SILC_SERVER_CMD_FUNC(quit)
1386 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1387 SilcServer server = cmd->server;
1388 SilcSocketConnection sock = cmd->sock;
1390 unsigned char *tmp = NULL;
1393 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
1395 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1399 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1403 q = silc_calloc(1, sizeof(*q));
1404 q->sock = silc_socket_dup(sock);
1405 q->signoff = tmp ? strdup(tmp) : NULL;
1407 /* We quit the connection with little timeout */
1408 silc_schedule_task_add(server->schedule, sock->sock,
1409 silc_server_command_quit_cb, (void *)q,
1410 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
1413 silc_server_command_free(cmd);
1416 /* Server side of command KILL. This command is used by router operator
1417 to remove an client from the SILC Network temporarily. */
1419 SILC_SERVER_CMD_FUNC(kill)
1421 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1422 SilcServer server = cmd->server;
1423 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1424 SilcClientEntry remote_client;
1425 SilcClientID *client_id = NULL;
1426 unsigned char *tmp, *comment, *auth;
1427 SilcUInt32 tmp_len, tmp_len2, auth_len;
1429 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 3);
1431 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
1434 /* Get authentication payload if present */
1435 auth = silc_argument_get_arg_type(cmd->args, 3, &auth_len);
1438 /* Router operator killing */
1440 /* KILL command works only on router */
1441 if (server->server_type != SILC_ROUTER) {
1442 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1443 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1447 /* Check whether client has the permissions. */
1448 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1449 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1450 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1455 /* Get the client ID */
1456 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1458 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1459 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1463 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1465 silc_server_command_send_status_data(cmd, SILC_COMMAND_KILL,
1466 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1467 0, 2, tmp, tmp_len);
1471 /* Get the client entry */
1472 remote_client = silc_idlist_find_client_by_id(server->local_list,
1473 client_id, TRUE, NULL);
1474 if (!remote_client) {
1475 remote_client = silc_idlist_find_client_by_id(server->global_list,
1476 client_id, TRUE, NULL);
1477 if (!remote_client) {
1478 silc_server_command_send_status_data(cmd, SILC_COMMAND_KILL,
1479 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1480 0, 2, tmp, tmp_len);
1486 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
1487 if (comment && tmp_len2 > 128) {
1489 comment[127] = '\0';
1492 /* If authentication data is provided then verify that killing is
1494 if (auth && auth_len) {
1495 SilcSocketConnection sock;
1497 if (!SILC_IS_LOCAL(remote_client) || !remote_client->data.public_key) {
1498 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1499 SILC_STATUS_ERR_OPERATION_ALLOWED,
1504 /* Verify the signature */
1505 if (!silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1506 remote_client->data.public_key, 0,
1507 server->sha1hash, remote_client->id,
1509 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1510 SILC_STATUS_ERR_AUTH_FAILED, 0);
1514 /* Send reply to the sender */
1515 silc_server_command_send_status_data(cmd, SILC_COMMAND_KILL,
1519 /* Do normal signoff for the destination client */
1520 sock = remote_client->connection;
1521 silc_server_remove_from_channels(server, NULL, remote_client,
1522 TRUE, (char *)"Killed", TRUE, TRUE);
1523 silc_server_free_client_data(server, NULL, remote_client, TRUE,
1525 (unsigned char *)"Killed");
1527 silc_server_close_connection(server, sock);
1529 /* Router operator killing */
1531 /* Send reply to the sender */
1532 silc_server_command_send_status_data(cmd, SILC_COMMAND_KILL,
1536 /* Check if anyone is watching this nickname */
1537 if (server->server_type == SILC_ROUTER)
1538 silc_server_check_watcher_list(server, client, NULL,
1539 SILC_NOTIFY_TYPE_KILLED);
1541 /* Now do the killing */
1542 silc_server_kill_client(server, remote_client, comment, client->id,
1547 silc_free(client_id);
1548 silc_server_command_free(cmd);
1551 /* Server side of command INFO. This sends information about us to
1552 the client. If client requested specific server we will send the
1553 command to that server. */
1555 SILC_SERVER_CMD_FUNC(info)
1557 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1558 SilcServer server = cmd->server;
1559 SilcBuffer packet, idp;
1562 char *dest_server = NULL, *server_info = NULL, *server_name;
1563 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1564 SilcServerEntry entry = NULL;
1565 SilcServerID *server_id = NULL;
1567 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
1569 /* Get server name */
1570 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
1572 /* Check server name. */
1573 dest_server = silc_identifier_check(dest_server, strlen(dest_server),
1574 SILC_STRING_UTF8, 256, &tmp_len);
1576 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1577 SILC_STATUS_ERR_BAD_SERVER, 0);
1583 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
1585 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1587 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1588 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1594 /* Check whether we have this server cached */
1595 entry = silc_idlist_find_server_by_id(server->local_list,
1596 server_id, TRUE, NULL);
1598 entry = silc_idlist_find_server_by_id(server->global_list,
1599 server_id, TRUE, NULL);
1600 if (!entry && server->server_type != SILC_SERVER) {
1601 silc_server_command_send_status_data(cmd, SILC_COMMAND_INFO,
1602 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
1603 0, 2, tmp, tmp_len);
1609 /* Some buggy servers has sent request to router about themselves. */
1610 if (server->server_type != SILC_SERVER && cmd->sock->user_data == entry)
1613 if ((!dest_server && !server_id && !entry) || (entry &&
1614 entry == server->id_entry) ||
1615 (dest_server && !cmd->pending &&
1616 !memcmp(dest_server, server->server_name, strlen(dest_server)))) {
1617 /* Send our reply */
1618 char info_string[256];
1620 memset(info_string, 0, sizeof(info_string));
1621 snprintf(info_string, sizeof(info_string),
1622 "location: %s server: %s admin: %s <%s>",
1623 server->config->server_info->location,
1624 server->config->server_info->server_type,
1625 server->config->server_info->admin,
1626 server->config->server_info->email);
1628 server_info = info_string;
1629 entry = server->id_entry;
1631 /* Check whether we have this server cached */
1632 if (!entry && dest_server) {
1633 entry = silc_idlist_find_server_by_name(server->global_list,
1634 dest_server, TRUE, NULL);
1636 entry = silc_idlist_find_server_by_name(server->local_list,
1637 dest_server, TRUE, NULL);
1641 if (!cmd->pending &&
1642 server->server_type != SILC_SERVER && entry && !entry->server_info) {
1643 /* Send to the server */
1645 SilcUInt16 old_ident;
1647 old_ident = silc_command_get_ident(cmd->payload);
1648 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1649 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1651 silc_server_packet_send(server, entry->connection,
1652 SILC_PACKET_COMMAND, cmd->packet->flags,
1653 tmpbuf->data, tmpbuf->len, TRUE);
1655 /* Reprocess this packet after received reply from router */
1656 silc_server_command_pending(server, SILC_COMMAND_INFO,
1657 silc_command_get_ident(cmd->payload),
1658 silc_server_command_info,
1659 silc_server_command_dup(cmd));
1660 cmd->pending = TRUE;
1661 silc_command_set_ident(cmd->payload, old_ident);
1662 silc_buffer_free(tmpbuf);
1666 if (!entry && !cmd->pending && !server->standalone) {
1667 /* Send to the primary router */
1669 SilcUInt16 old_ident;
1671 old_ident = silc_command_get_ident(cmd->payload);
1672 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1673 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1675 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1676 SILC_PACKET_COMMAND, cmd->packet->flags,
1677 tmpbuf->data, tmpbuf->len, TRUE);
1679 /* Reprocess this packet after received reply from router */
1680 silc_server_command_pending(server, SILC_COMMAND_INFO,
1681 silc_command_get_ident(cmd->payload),
1682 silc_server_command_info,
1683 silc_server_command_dup(cmd));
1684 cmd->pending = TRUE;
1685 silc_command_set_ident(cmd->payload, old_ident);
1686 silc_buffer_free(tmpbuf);
1691 silc_free(server_id);
1695 silc_free(dest_server);
1696 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
1697 silc_server_command_send_status_data(cmd, SILC_COMMAND_INFO,
1698 SILC_STATUS_ERR_NO_SUCH_SERVER, 0,
1700 strlen(dest_server));
1706 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1708 server_info = entry->server_info;
1709 server_name = entry->server_name;
1711 /* Send the reply */
1712 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
1713 SILC_STATUS_OK, 0, ident, 3,
1714 2, idp->data, idp->len,
1716 strlen(server_name),
1719 strlen(server_info) : 0);
1720 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
1721 packet->data, packet->len, FALSE);
1723 silc_buffer_free(packet);
1724 silc_buffer_free(idp);
1727 silc_free(dest_server);
1728 silc_server_command_free(cmd);
1731 /* Server side of command PING. This just replies to the ping. */
1733 SILC_SERVER_CMD_FUNC(ping)
1735 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1736 SilcServer server = cmd->server;
1739 SilcServerID *server_id = NULL;
1741 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PING, cmd, 1, 1);
1744 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1746 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1747 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1751 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1755 if (SILC_ID_SERVER_COMPARE(server_id, server->id)) {
1756 /* Send our reply */
1757 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1760 silc_server_command_send_status_data(cmd, SILC_COMMAND_PING,
1761 SILC_STATUS_ERR_NO_SUCH_SERVER_ID, 0,
1767 silc_free(server_id);
1768 silc_server_command_free(cmd);
1771 /* Server side of command STATS. */
1773 SILC_SERVER_CMD_FUNC(stats)
1775 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1776 SilcServer server = cmd->server;
1777 SilcServerID *server_id;
1780 SilcBuffer packet, stats;
1781 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1784 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_STATS, cmd, 1, 1);
1787 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1789 silc_server_command_send_status_reply(cmd, SILC_COMMAND_STATS,
1790 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1793 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1797 /* The ID must be ours */
1798 if (!SILC_ID_SERVER_COMPARE(server->id, server_id)) {
1799 silc_server_command_send_status_data(cmd, SILC_COMMAND_STATS,
1800 SILC_STATUS_ERR_NO_SUCH_SERVER_ID, 0,
1802 silc_free(server_id);
1805 silc_free(server_id);
1807 /* If we are router then just send everything we got. If we are normal
1808 server then we'll send this to our router to get all the latest
1809 statistical information. */
1810 if (!cmd->pending && server->server_type != SILC_ROUTER &&
1811 !server->standalone) {
1812 /* Send request to our router */
1813 SilcBuffer idp = silc_id_payload_encode(server->router->id,
1815 packet = silc_command_payload_encode_va(SILC_COMMAND_STATS,
1816 ++server->cmd_ident, 1,
1817 1, idp->data, idp->len);
1818 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1819 SILC_PACKET_COMMAND, 0, packet->data,
1820 packet->len, FALSE);
1822 /* Reprocess this packet after received reply from router */
1823 silc_server_command_pending(server, SILC_COMMAND_STATS,
1825 silc_server_command_stats,
1826 silc_server_command_dup(cmd));
1827 cmd->pending = TRUE;
1828 silc_buffer_free(packet);
1829 silc_buffer_free(idp);
1833 /* Send our reply to sender */
1834 uptime = time(NULL) - server->starttime;
1836 stats = silc_buffer_alloc_size(60);
1837 silc_buffer_format(stats,
1838 SILC_STR_UI_INT(server->starttime),
1839 SILC_STR_UI_INT(uptime),
1840 SILC_STR_UI_INT(server->stat.my_clients),
1841 SILC_STR_UI_INT(server->stat.my_channels),
1842 SILC_STR_UI_INT(server->stat.my_server_ops),
1843 SILC_STR_UI_INT(server->stat.my_router_ops),
1844 SILC_STR_UI_INT(server->stat.cell_clients),
1845 SILC_STR_UI_INT(server->stat.cell_channels),
1846 SILC_STR_UI_INT(server->stat.cell_servers),
1847 SILC_STR_UI_INT(server->stat.clients),
1848 SILC_STR_UI_INT(server->stat.channels),
1849 SILC_STR_UI_INT(server->stat.servers),
1850 SILC_STR_UI_INT(server->stat.routers),
1851 SILC_STR_UI_INT(server->stat.server_ops),
1852 SILC_STR_UI_INT(server->stat.router_ops),
1855 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_STATS,
1856 SILC_STATUS_OK, 0, ident, 2,
1858 3, stats->data, stats->len);
1859 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1860 0, packet->data, packet->len, FALSE);
1861 silc_buffer_free(packet);
1862 silc_buffer_free(stats);
1865 silc_server_command_free(cmd);
1868 /* Internal routine to join channel. The channel sent to this function
1869 has been either created or resolved from ID lists. This joins the sent
1870 client to the channel. */
1872 static void silc_server_command_join_channel(SilcServer server,
1873 SilcServerCommandContext cmd,
1874 SilcChannelEntry channel,
1875 SilcClientID *client_id,
1879 const unsigned char *auth,
1880 SilcUInt32 auth_len,
1881 const unsigned char *cauth,
1882 SilcUInt32 cauth_len)
1884 SilcSocketConnection sock = cmd->sock;
1886 SilcUInt32 tmp_len, user_count;
1887 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4], ulimit[4];
1888 SilcClientEntry client;
1889 SilcChannelClientEntry chl;
1890 SilcBuffer reply, chidp, clidp, keyp = NULL;
1891 SilcBuffer user_list, mode_list, invite_list, ban_list;
1892 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1893 char check[512], check2[512];
1894 bool founder = FALSE;
1896 SilcBuffer fkey = NULL, chpklist = NULL;
1899 SILC_LOG_DEBUG(("Joining client to channel"));
1904 /* Get the client entry */
1905 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
1906 client = (SilcClientEntry)sock->user_data;
1910 client = silc_server_query_client(server, client_id, FALSE,
1913 if (!resolve || cmd->pending) {
1914 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
1915 silc_server_command_send_status_data(
1916 cmd, SILC_COMMAND_JOIN,
1917 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID, 0,
1922 /* The client info is being resolved. Reprocess this packet after
1923 receiving the reply to the query. */
1924 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1926 silc_server_command_join,
1927 silc_server_command_dup(cmd));
1928 cmd->pending = TRUE;
1932 if (!client->data.public_key &&
1933 (auth || cauth || channel->ban_list ||
1934 (channel->mode & SILC_CHANNEL_MODE_INVITE))) {
1935 if (cmd->pending == 2)
1938 /* We must retrieve the client's public key by sending
1939 GETKEY command. Reprocess this packet after receiving the key */
1940 clidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1941 silc_server_send_command(server, cmd->sock,
1942 SILC_COMMAND_GETKEY, ++server->cmd_ident,
1943 1, 1, clidp->data, clidp->len);
1944 silc_buffer_free(clidp);
1945 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
1947 silc_server_command_join,
1948 silc_server_command_dup(cmd));
1953 cmd->pending = FALSE;
1957 * Check founder auth payload if provided. If client can gain founder
1958 * privileges it can override various conditions on joining the channel,
1959 * and can have directly the founder mode set on the channel.
1961 if (auth && auth_len && channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
1962 SilcIDListData idata = (SilcIDListData)client;
1963 SilcChannelClientEntry chl2;
1964 SilcHashTableList htl;
1966 if (channel->founder_key && idata->public_key &&
1967 silc_pkcs_public_key_compare(channel->founder_key,
1968 idata->public_key)) {
1969 /* Check whether the client is to become founder */
1970 if (silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1971 channel->founder_key, 0, server->sha1hash,
1972 client->id, SILC_ID_CLIENT)) {
1974 /* There cannot be anyone else as founder on the channel now. This
1975 client is definitely the founder due to this authentication */
1976 silc_hash_table_list(channel->user_list, &htl);
1977 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
1978 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
1979 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1980 silc_server_force_cumode_change(server, NULL, channel, chl2,
1984 silc_hash_table_list_reset(&htl);
1986 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
1993 * Check channel modes
1997 memset(check, 0, sizeof(check));
1998 memset(check2, 0, sizeof(check2));
1999 silc_strncat(check, sizeof(check),
2000 client->nickname, strlen(client->nickname));
2001 silc_strncat(check, sizeof(check), "!", 1);
2002 silc_strncat(check, sizeof(check),
2003 client->username, strlen(client->username));
2004 if (!strchr(client->username, '@')) {
2005 silc_strncat(check, sizeof(check), "@", 1);
2006 silc_strncat(check, sizeof(check),
2007 cmd->sock->hostname, strlen(cmd->sock->hostname));
2010 silc_strncat(check2, sizeof(check2),
2011 client->nickname, strlen(client->nickname));
2012 if (!strchr(client->nickname, '@')) {
2013 silc_strncat(check2, sizeof(check2), "@", 1);
2014 silc_strncat(check2, sizeof(check2),
2015 SILC_IS_LOCAL(client) ? server->server_name :
2016 client->router->server_name,
2017 SILC_IS_LOCAL(client) ? strlen(server->server_name) :
2018 strlen(client->router->server_name));
2020 silc_strncat(check2, sizeof(check2), "!", 1);
2021 silc_strncat(check2, sizeof(check2),
2022 client->username, strlen(client->username));
2023 if (!strchr(client->username, '@')) {
2024 silc_strncat(check2, sizeof(check2), "@", 1);
2025 silc_strncat(check2, sizeof(check2),
2026 cmd->sock->hostname, strlen(cmd->sock->hostname));
2029 /* Check invite list if channel is invite-only channel */
2030 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2031 if (!channel->invite_list ||
2032 !silc_hash_table_count(channel->invite_list) ||
2033 (!silc_server_inviteban_match(server, channel->invite_list,
2035 !silc_server_inviteban_match(server, channel->invite_list,
2036 2, client->data.public_key) &&
2037 !silc_server_inviteban_match(server, channel->invite_list,
2038 1, client->nickname) &&
2039 !silc_server_inviteban_match(server, channel->invite_list,
2041 !silc_server_inviteban_match(server, channel->invite_list,
2043 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2044 silc_server_command_send_status_data(cmd, SILC_COMMAND_JOIN,
2045 SILC_STATUS_ERR_NOT_INVITED, 0,
2046 2, chidp->data, chidp->len);
2047 silc_buffer_free(chidp);
2052 /* Check ban list if it exists. If the client's nickname, server,
2053 username and/or hostname is in the ban list the access to the
2054 channel is denied. */
2055 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
2056 if (silc_server_inviteban_match(server, channel->ban_list,
2058 silc_server_inviteban_match(server, channel->ban_list,
2059 2, client->data.public_key) ||
2060 silc_server_inviteban_match(server, channel->ban_list,
2061 1, client->nickname) ||
2062 silc_server_inviteban_match(server, channel->ban_list,
2064 silc_server_inviteban_match(server, channel->ban_list,
2066 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2067 silc_server_command_send_status_data(
2068 cmd, SILC_COMMAND_JOIN,
2069 SILC_STATUS_ERR_BANNED_FROM_CHANNEL, 0,
2070 2, chidp->data, chidp->len);
2071 silc_buffer_free(chidp);
2076 /* Check user count limit if set. */
2077 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
2078 if (silc_hash_table_count(channel->user_list) + 1 >
2079 channel->user_limit) {
2080 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2081 silc_server_command_send_status_data(cmd, SILC_COMMAND_JOIN,
2082 SILC_STATUS_ERR_CHANNEL_IS_FULL,
2083 0, 2, chidp->data, chidp->len);
2084 silc_buffer_free(chidp);
2090 /* Check the channel passphrase if set. */
2091 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2092 /* Get passphrase */
2093 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
2095 passphrase = silc_memdup(tmp, tmp_len);
2097 if (!passphrase || !channel->passphrase ||
2098 strlen(channel->passphrase) != strlen(passphrase) ||
2099 memcmp(passphrase, channel->passphrase, strlen(channel->passphrase))) {
2100 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2101 silc_server_command_send_status_data(cmd, SILC_COMMAND_JOIN,
2102 SILC_STATUS_ERR_BAD_PASSWORD, 0,
2103 2, chidp->data, chidp->len);
2104 silc_buffer_free(chidp);
2109 /* Verify channel authentication with channel public keys if set. */
2110 if (channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
2111 if (!silc_server_verify_channel_auth(server, channel, client->id,
2112 cauth, cauth_len)) {
2113 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2114 SILC_STATUS_ERR_PERM_DENIED, 0);
2120 * Client is allowed to join to the channel. Make it happen.
2123 /* Check whether the client already is on the channel */
2124 if (silc_server_client_on_channel(client, channel, NULL)) {
2125 clidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
2126 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2127 silc_server_command_send_status_data2(cmd, SILC_COMMAND_JOIN,
2128 SILC_STATUS_ERR_USER_ON_CHANNEL, 0,
2129 2, clidp->data, clidp->len,
2130 3, chidp->data, chidp->len);
2131 silc_buffer_free(clidp);
2132 silc_buffer_free(chidp);
2136 /* Generate new channel key as protocol dictates */
2138 if (!silc_server_create_channel_key(server, channel, 0))
2141 /* Send the channel key. This is broadcasted to the channel but is not
2142 sent to the client who is joining to the channel. */
2143 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
2144 silc_server_send_channel_key(server, NULL, channel,
2145 server->server_type == SILC_ROUTER ?
2146 FALSE : !server->standalone);
2149 /* Join the client to the channel by adding it to channel's user list.
2150 Add also the channel to client entry's channels list for fast cross-
2152 chl = silc_calloc(1, sizeof(*chl));
2154 chl->client = client;
2155 chl->channel = channel;
2156 silc_hash_table_add(channel->user_list, client, chl);
2157 silc_hash_table_add(client->channels, channel, chl);
2158 channel->user_count++;
2159 channel->disabled = FALSE;
2161 /* Get users on the channel */
2162 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2165 /* Encode Client ID Payload of the original client who wants to join */
2166 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2168 /* Encode command reply packet */
2169 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2170 SILC_PUT32_MSB(channel->mode, mode);
2171 SILC_PUT32_MSB(created, tmp2);
2172 SILC_PUT32_MSB(user_count, tmp3);
2173 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
2174 SILC_PUT32_MSB(channel->user_limit, ulimit);
2176 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2177 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2178 cipher = silc_cipher_get_name(channel->channel_key);
2179 keyp = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
2182 strlen(cipher), cipher,
2183 channel->key_len / 8, channel->key);
2187 if (channel->founder_key)
2188 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
2190 /* Encode invite list */
2192 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
2193 SilcHashTableList htl;
2195 invite_list = silc_buffer_alloc_size(2);
2196 silc_buffer_format(invite_list,
2197 SILC_STR_UI_SHORT(silc_hash_table_count(
2198 channel->invite_list)),
2201 silc_hash_table_list(channel->invite_list, &htl);
2202 while (silc_hash_table_get(&htl, (void *)&tmp_len, (void *)&reply))
2203 invite_list = silc_argument_payload_encode_one(invite_list,
2205 reply->len, tmp_len);
2206 silc_hash_table_list_reset(&htl);
2209 /* Encode ban list */
2211 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
2212 SilcHashTableList htl;
2214 ban_list = silc_buffer_alloc_size(2);
2215 silc_buffer_format(ban_list,
2216 SILC_STR_UI_SHORT(silc_hash_table_count(
2217 channel->ban_list)),
2220 silc_hash_table_list(channel->ban_list, &htl);
2221 while (silc_hash_table_get(&htl, (void *)&tmp_len, (void *)&reply))
2222 ban_list = silc_argument_payload_encode_one(ban_list,
2224 reply->len, tmp_len);
2225 silc_hash_table_list_reset(&htl);
2228 if (channel->channel_pubkeys)
2229 chpklist = silc_server_get_channel_pk_list(server, channel, FALSE, FALSE);
2232 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2233 SILC_STATUS_OK, 0, ident, 16,
2234 2, channel->channel_name,
2235 strlen(channel->channel_name),
2236 3, chidp->data, chidp->len,
2237 4, clidp->data, clidp->len,
2240 7, keyp ? keyp->data : NULL,
2241 keyp ? keyp->len : 0,
2242 8, ban_list ? ban_list->data : NULL,
2243 ban_list ? ban_list->len : 0,
2244 9, invite_list ? invite_list->data :
2246 invite_list ? invite_list->len : 0,
2249 strlen(channel->topic) : 0,
2250 11, silc_hmac_get_name(channel->hmac),
2251 strlen(silc_hmac_get_name(channel->
2254 13, user_list->data, user_list->len,
2255 14, mode_list->data,
2257 15, fkey ? fkey->data : NULL,
2258 fkey ? fkey->len : 0,
2259 16, chpklist ? chpklist->data : NULL,
2260 chpklist ? chpklist->len : 0,
2261 17, (channel->mode &
2262 SILC_CHANNEL_MODE_ULIMIT ?
2265 SILC_CHANNEL_MODE_ULIMIT ?
2266 sizeof(ulimit) : 0));
2268 /* Send command reply */
2269 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2270 reply->data, reply->len, FALSE);
2272 /* Send JOIN notify to locally connected clients on the channel. If
2273 we are normal server then router will send or have sent JOIN notify
2274 already. However since we've added the client already to our channel
2275 we'll ignore it (in packet_receive.c) so we must send it here. If
2276 we are router then this will send it to local clients and local
2278 SILC_LOG_DEBUG(("Send JOIN notify to channel"));
2279 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
2280 SILC_NOTIFY_TYPE_JOIN, 2,
2281 clidp->data, clidp->len,
2282 chidp->data, chidp->len);
2284 /* Update statistics */
2285 server->stat.my_chanclients++;
2286 if (server->server_type == SILC_ROUTER) {
2287 server->stat.cell_chanclients++;
2288 server->stat.chanclients++;
2291 if (!cmd->pending) {
2292 /* Send JOIN notify packet to our primary router */
2293 silc_server_send_notify_join(server, SILC_PRIMARY_ROUTE(server),
2294 SILC_BROADCAST(server), channel, client->id);
2297 /* Distribute the channel key to all backup routers. */
2298 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
2299 keyp->data, keyp->len, FALSE, TRUE);
2301 /* If client became founder by providing correct founder auth data
2302 notify the mode change to the channel. */
2304 SILC_PUT32_MSB(chl->mode, mode);
2305 SILC_LOG_DEBUG(("Send CUMODE_CHANGE notify to channel"));
2306 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
2307 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
2308 clidp->data, clidp->len,
2309 mode, 4, clidp->data, clidp->len,
2310 fkey ? fkey->data : NULL,
2311 fkey ? fkey->len : 0);
2315 /* Set CUMODE notify type to network */
2317 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
2318 SILC_BROADCAST(server), channel,
2319 chl->mode, client->id, SILC_ID_CLIENT,
2320 client->id, channel->founder_key);
2322 silc_buffer_free(reply);
2323 silc_buffer_free(clidp);
2324 silc_buffer_free(chidp);
2325 silc_buffer_free(keyp);
2326 silc_buffer_free(user_list);
2327 silc_buffer_free(mode_list);
2328 silc_buffer_free(fkey);
2329 silc_buffer_free(chpklist);
2330 silc_buffer_free(invite_list);
2331 silc_buffer_free(ban_list);
2335 memset(passphrase, 0, strlen(passphrase));
2336 silc_free(passphrase);
2339 /* Server side of command JOIN. Joins client into requested channel. If
2340 the channel does not exist it will be created. */
2342 SILC_SERVER_CMD_FUNC(join)
2344 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2345 SilcServer server = cmd->server;
2346 unsigned char *auth, *cauth;
2347 SilcUInt32 tmp_len, auth_len, cauth_len;
2348 char *tmp, *channel_name, *channel_namec = NULL, *cipher, *hmac;
2349 SilcChannelEntry channel;
2350 SilcUInt32 umode = 0;
2351 bool created = FALSE, create_key = TRUE;
2352 SilcClientID *client_id;
2354 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 2, 7);
2356 /* Get channel name */
2357 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2359 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2360 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2365 /* Truncate over long channel names */
2366 if (tmp_len > 256) {
2372 /* Check for valid channel name. This is cached, the original is saved
2373 in the channel context. */
2374 channel_namec = silc_channel_name_check(tmp, tmp_len, SILC_STRING_UTF8, 256,
2376 if (!channel_namec) {
2377 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2378 SILC_STATUS_ERR_BAD_CHANNEL, 0);
2382 /* Get Client ID of the client who is joining to the channel */
2383 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2385 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2386 SILC_STATUS_ERR_NO_CLIENT_ID,
2390 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
2392 silc_server_command_send_status_data(cmd, SILC_COMMAND_JOIN,
2393 SILC_STATUS_ERR_BAD_CLIENT_ID, 0,
2398 /* Get cipher, hmac name and auth payload */
2399 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
2400 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
2401 auth = silc_argument_get_arg_type(cmd->args, 6, &auth_len);
2402 cauth = silc_argument_get_arg_type(cmd->args, 7, &cauth_len);
2404 /* See if the channel exists */
2405 channel = silc_idlist_find_channel_by_name(server->local_list,
2406 channel_namec, NULL);
2408 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2409 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
2411 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2412 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2417 silc_free(client_id);
2418 client_id = silc_id_dup(entry->id, SILC_ID_CLIENT);
2421 (channel->disabled && server->server_type != SILC_ROUTER)) {
2422 /* Channel not found or not valid */
2424 /* If we are standalone server we don't have a router, we just create
2425 the channel by ourselves (unless it existed). */
2426 if (server->standalone) {
2428 channel = silc_server_create_new_channel(server, server->id, cipher,
2429 hmac, channel_name, TRUE);
2431 silc_server_command_send_status_data(
2432 cmd, SILC_COMMAND_JOIN,
2433 SILC_STATUS_ERR_UNKNOWN_ALGORITHM,
2434 0, 2, cipher, strlen(cipher));
2435 silc_free(client_id);
2439 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2445 /* The channel does not exist on our server. If we are normal server
2446 we will send JOIN command to our router which will handle the
2447 joining procedure (either creates the channel if it doesn't exist
2448 or joins the client to it). */
2449 if (server->server_type != SILC_ROUTER) {
2451 SilcUInt16 old_ident;
2453 /* If this is pending command callback then we've resolved
2454 it and it didn't work, return since we've notified the
2455 client already in the command reply callback. */
2457 silc_free(client_id);
2461 old_ident = silc_command_get_ident(cmd->payload);
2462 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2463 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2465 /* Send JOIN command to our router */
2466 silc_server_packet_send(server, (SilcSocketConnection)
2467 SILC_PRIMARY_ROUTE(server),
2468 SILC_PACKET_COMMAND, cmd->packet->flags,
2469 tmpbuf->data, tmpbuf->len, TRUE);
2471 /* Reprocess this packet after received reply from router */
2472 silc_server_command_pending(server, SILC_COMMAND_JOIN,
2473 silc_command_get_ident(cmd->payload),
2474 silc_server_command_join,
2475 silc_server_command_dup(cmd));
2476 cmd->pending = TRUE;
2477 silc_command_set_ident(cmd->payload, old_ident);
2478 silc_buffer_free(tmpbuf);
2479 silc_free(client_id);
2483 /* We are router and the channel does not seem exist so we will check
2484 our global list as well for the channel. */
2485 channel = silc_idlist_find_channel_by_name(server->global_list,
2486 channel_namec, NULL);
2488 /* Channel really does not exist, create it */
2489 channel = silc_server_create_new_channel(server, server->id, cipher,
2490 hmac, channel_name, TRUE);
2492 silc_server_command_send_status_data(
2493 cmd, SILC_COMMAND_JOIN,
2494 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
2495 2, cipher, strlen(cipher));
2496 silc_free(client_id);
2500 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2508 /* Channel not found */
2510 /* If the command came from router and we are normal server then
2511 something went wrong with the joining as the channel was not found.
2512 We can't do anything else but ignore this. */
2513 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
2514 server->server_type != SILC_ROUTER) {
2515 silc_free(client_id);
2519 /* We are router and the channel does not seem exist so we will check
2520 our global list as well for the channel. */
2521 channel = silc_idlist_find_channel_by_name(server->global_list,
2522 channel_namec, NULL);
2524 /* Channel really does not exist, create it */
2525 channel = silc_server_create_new_channel(server, server->id, cipher,
2526 hmac, channel_name, TRUE);
2528 silc_server_command_send_status_data(
2529 cmd, SILC_COMMAND_JOIN,
2530 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
2531 2, cipher, strlen(cipher));
2532 silc_free(client_id);
2536 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2543 /* Check whether the channel was created by our router */
2544 if (cmd->pending && context2) {
2545 SilcServerCommandReplyContext reply = context2;
2547 if (silc_command_get(reply->payload) == SILC_COMMAND_JOIN) {
2548 tmp = silc_argument_get_arg_type(reply->args, 6, NULL);
2549 SILC_GET32_MSB(created, tmp);
2550 if (silc_argument_get_arg_type(reply->args, 7, NULL))
2551 create_key = FALSE; /* Router returned the key already */
2553 if (silc_command_get_status(reply->payload, NULL, NULL) &&
2554 channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2555 /* Save channel passphrase, if user provided it successfully */
2558 pa = silc_argument_get_arg_type(cmd->args, 3, &pa_len);
2560 silc_free(channel->passphrase);
2561 channel->passphrase = silc_memdup(pa, pa_len);
2566 if (silc_command_get(reply->payload) == SILC_COMMAND_WHOIS &&
2567 !channel->disabled && !silc_hash_table_count(channel->user_list))
2571 /* If the channel does not have global users and is also empty the client
2572 will be the channel founder and operator. */
2573 if (!channel->disabled &&
2574 !channel->global_users && !silc_hash_table_count(channel->user_list))
2575 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2577 /* Join to the channel */
2578 silc_server_command_join_channel(server, cmd, channel, client_id,
2579 created, create_key, umode,
2580 auth, auth_len, cauth, cauth_len);
2582 silc_free(client_id);
2585 silc_free(channel_namec);
2586 silc_server_command_free(cmd);
2589 /* Server side of command MOTD. Sends server's current "message of the
2590 day" to the client. */
2592 SILC_SERVER_CMD_FUNC(motd)
2594 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2595 SilcServer server = cmd->server;
2596 SilcBuffer packet, idp;
2597 char *motd, *dest_server = NULL;
2598 SilcUInt32 motd_len;
2599 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2601 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
2603 /* Get server name */
2604 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2606 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2607 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2612 /* Check server name */
2613 dest_server = silc_identifier_check(dest_server, strlen(dest_server),
2614 SILC_STRING_UTF8, 256, NULL);
2616 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2617 SILC_STATUS_ERR_BAD_SERVER,
2622 if (!memcmp(dest_server, server->server_name, strlen(dest_server))) {
2625 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
2627 if (server->config && server->config->server_info &&
2628 server->config->server_info->motd_file) {
2630 motd = silc_file_readfile(server->config->server_info->motd_file,
2636 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2639 2, idp->data, idp->len,
2643 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2646 2, idp->data, idp->len);
2649 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2650 packet->data, packet->len, FALSE);
2651 silc_buffer_free(packet);
2652 silc_buffer_free(idp);
2654 SilcServerEntry entry;
2656 /* Check whether we have this server cached */
2657 entry = silc_idlist_find_server_by_name(server->global_list,
2658 dest_server, TRUE, NULL);
2660 entry = silc_idlist_find_server_by_name(server->local_list,
2661 dest_server, TRUE, NULL);
2664 if (server->server_type != SILC_SERVER && !cmd->pending &&
2665 entry && !entry->motd) {
2666 /* Send to the server */
2668 SilcUInt16 old_ident;
2670 old_ident = silc_command_get_ident(cmd->payload);
2671 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2672 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2674 silc_server_packet_send(server, entry->connection,
2675 SILC_PACKET_COMMAND, cmd->packet->flags,
2676 tmpbuf->data, tmpbuf->len, TRUE);
2678 /* Reprocess this packet after received reply from router */
2679 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2680 silc_command_get_ident(cmd->payload),
2681 silc_server_command_motd,
2682 silc_server_command_dup(cmd));
2683 cmd->pending = TRUE;
2684 silc_command_set_ident(cmd->payload, old_ident);
2685 silc_buffer_free(tmpbuf);
2689 /* Send to primary router only if we don't know the server
2690 * the client requested or if the server is not locally connected */
2691 if ((!entry || !(entry->data.status & SILC_IDLIST_STATUS_LOCAL))
2692 && !cmd->pending && !server->standalone) {
2693 /* Send to the primary router */
2695 SilcUInt16 old_ident;
2697 old_ident = silc_command_get_ident(cmd->payload);
2698 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2699 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2701 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2702 SILC_PACKET_COMMAND, cmd->packet->flags,
2703 tmpbuf->data, tmpbuf->len, TRUE);
2705 /* Reprocess this packet after received reply from router */
2706 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2707 silc_command_get_ident(cmd->payload),
2708 silc_server_command_motd,
2709 silc_server_command_dup(cmd));
2710 cmd->pending = TRUE;
2711 silc_command_set_ident(cmd->payload, old_ident);
2712 silc_buffer_free(tmpbuf);
2717 silc_free(dest_server);
2718 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2719 silc_server_command_send_status_data(cmd, SILC_COMMAND_MOTD,
2720 SILC_STATUS_ERR_NO_SUCH_SERVER, 0,
2722 strlen(dest_server));
2727 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2728 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2729 SILC_STATUS_OK, 0, ident, 2,
2730 2, idp->data, idp->len,
2733 strlen(entry->motd) : 0);
2734 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2735 packet->data, packet->len, FALSE);
2736 silc_buffer_free(packet);
2737 silc_buffer_free(idp);
2741 silc_free(dest_server);
2742 silc_server_command_free(cmd);
2745 /* Server side of command UMODE. Client can use this command to set/unset
2746 user mode. Client actually cannot set itself to be as server/router
2747 operator so this can be used only to unset the modes. */
2749 SILC_SERVER_CMD_FUNC(umode)
2751 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2752 SilcServer server = cmd->server;
2753 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2755 unsigned char *tmp_mask, m[4];
2756 SilcUInt32 mask = 0;
2757 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2758 bool set_mask = FALSE;
2760 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
2763 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 1, 2);
2765 /* Get the client's mode mask */
2766 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
2768 SILC_GET32_MSB(mask, tmp_mask);
2773 /* Check that mode changing is allowed. */
2774 if (!silc_server_check_umode_rights(server, client, mask)) {
2775 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2776 SILC_STATUS_ERR_PERM_DENIED, 0);
2780 /* Anonymous mode cannot be set by client */
2781 if (mask & SILC_UMODE_ANONYMOUS &&
2782 !(client->mode & SILC_UMODE_ANONYMOUS)) {
2783 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2784 SILC_STATUS_ERR_PERM_DENIED, 0);
2788 /* Update statistics */
2789 if (mask & SILC_UMODE_GONE) {
2790 if (!(client->mode & SILC_UMODE_GONE))
2791 server->stat.my_aways++;
2793 if (client->mode & SILC_UMODE_GONE)
2794 server->stat.my_aways--;
2797 /* If the client has anonymous mode set, preserve it. */
2798 if (client->mode & SILC_UMODE_ANONYMOUS)
2799 mask |= SILC_UMODE_ANONYMOUS;
2801 /* Change the mode */
2802 client->mode = mask;
2804 /* Send UMODE change to primary router */
2805 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
2806 SILC_BROADCAST(server), client->id,
2809 /* Check if anyone is watching this nickname */
2810 if (server->server_type == SILC_ROUTER)
2811 silc_server_check_watcher_list(server, client, NULL,
2812 SILC_NOTIFY_TYPE_UMODE_CHANGE);
2815 /* Send command reply to sender */
2816 SILC_PUT32_MSB(client->mode, m);
2817 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
2818 SILC_STATUS_OK, 0, ident, 1,
2820 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2821 packet->data, packet->len, FALSE);
2822 silc_buffer_free(packet);
2825 silc_server_command_free(cmd);
2828 /* Server side command of CMODE. Changes channel mode */
2830 SILC_SERVER_CMD_FUNC(cmode)
2832 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2833 SilcServer server = cmd->server;
2834 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2835 SilcIDListData idata = (SilcIDListData)client;
2836 SilcChannelID *channel_id = NULL;
2837 SilcChannelEntry channel;
2838 SilcChannelClientEntry chl;
2839 SilcBuffer packet, cidp;
2840 unsigned char *tmp, *tmp_id, *tmp_mask, *chpkdata = NULL;
2841 char *cipher = NULL, *hmac = NULL, *passphrase = NULL, ulimit[4];
2842 SilcUInt32 mode_mask = 0, old_mask = 0, tmp_len, tmp_len2, chpklen;
2843 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2844 bool set_mask = FALSE, set_chpk = FALSE;
2845 SilcPublicKey founder_key = NULL;
2846 SilcBuffer fkey = NULL, chpklist = NULL;
2847 SilcBufferStruct chpk;
2850 silc_server_command_free(cmd);
2854 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 1, 9);
2856 /* Get Channel ID */
2857 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
2859 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2860 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2861 silc_server_command_free(cmd);
2864 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2, NULL);
2866 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2867 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2868 silc_server_command_free(cmd);
2872 /* Get channel entry */
2873 channel = silc_idlist_find_channel_by_id(server->local_list,
2876 channel = silc_idlist_find_channel_by_id(server->global_list,
2879 silc_server_command_send_status_data(cmd, SILC_COMMAND_CMODE,
2880 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
2881 0, 2, tmp_id, tmp_len2);
2882 silc_free(channel_id);
2883 silc_server_command_free(cmd);
2887 old_mask = channel->mode;
2889 /* Get the channel mode mask */
2890 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2892 SILC_GET32_MSB(mode_mask, tmp_mask);
2896 /* Check whether this client is on the channel */
2897 if (!silc_server_client_on_channel(client, channel, &chl)) {
2898 silc_server_command_send_status_data(cmd, SILC_COMMAND_CMODE,
2899 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
2900 2, tmp_id, tmp_len2);
2904 /* Check that client has rights to change any requested channel modes */
2905 if (set_mask && !silc_server_check_cmode_rights(server, channel, chl,
2907 SILC_LOG_DEBUG(("Client does not have rights to change mode"));
2908 silc_server_command_send_status_data(
2909 cmd, SILC_COMMAND_CMODE,
2910 (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) ?
2911 SILC_STATUS_ERR_NO_CHANNEL_PRIV :
2912 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV), 0,
2913 2, tmp_id, tmp_len2);
2917 /* If mode mask was not sent as argument then merely return the current
2918 mode mask, founder key and channel public key list to the sender. */
2921 SILC_PUT32_MSB(channel->mode, m);
2922 if (channel->founder_key)
2923 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
2924 if (channel->channel_pubkeys)
2925 chpklist = silc_server_get_channel_pk_list(server, channel,
2928 silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
2929 SILC_STATUS_OK, 0, ident, 4,
2930 2, tmp_id, tmp_len2,
2932 4, fkey ? fkey->data : NULL,
2933 fkey ? fkey->len : 0,
2934 5, chpklist ? chpklist->data : NULL,
2935 chpklist ? chpklist->len : 0);
2936 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2937 packet->data, packet->len, FALSE);
2938 silc_buffer_free(packet);
2943 * Check the modes. Modes that requires nothing special operation are
2947 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
2948 /* Channel uses private keys to protect traffic. Client(s) has set the
2949 key locally they want to use, server does not know that key. */
2950 /* Nothing interesting to do here */
2952 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
2953 /* The mode is removed and we need to generate and distribute
2954 new channel key. Clients are not using private channel keys
2955 anymore after this. */
2957 /* Re-generate channel key */
2958 if (!silc_server_create_channel_key(server, channel, 0))
2961 /* Send the channel key. This sends it to our local clients and if
2962 we are normal server to our router as well. */
2963 silc_server_send_channel_key(server, NULL, channel,
2964 server->server_type == SILC_ROUTER ?
2965 FALSE : !server->standalone);
2967 cipher = (char *)silc_cipher_get_name(channel->channel_key);
2968 hmac = (char *)silc_hmac_get_name(channel->hmac);
2972 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
2973 /* User limit is set on channel */
2974 SilcUInt32 user_limit;
2976 /* Get user limit */
2977 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
2979 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
2980 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2981 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2985 SILC_GET32_MSB(user_limit, tmp);
2986 channel->user_limit = user_limit;
2989 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
2990 /* User limit mode is unset. Remove user limit */
2991 channel->user_limit = 0;
2994 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
2995 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
2996 /* Passphrase has been set to channel */
2998 /* Get the passphrase */
2999 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
3001 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3002 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
3006 /* Save the passphrase */
3007 passphrase = channel->passphrase = silc_memdup(tmp, strlen(tmp));
3010 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3011 /* Passphrase mode is unset. remove the passphrase */
3012 silc_free(channel->passphrase);
3013 channel->passphrase = NULL;
3017 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
3018 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
3019 /* Cipher to use protect the traffic */
3020 SilcCipher newkey, oldkey;
3023 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
3025 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3026 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
3030 /* Delete old cipher and allocate the new one */
3031 if (!silc_cipher_alloc(cipher, &newkey)) {
3032 silc_server_command_send_status_data(
3033 cmd, SILC_COMMAND_CMODE,
3034 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
3035 2, cipher, strlen(cipher));
3039 oldkey = channel->channel_key;
3040 channel->channel_key = newkey;
3042 /* Re-generate channel key */
3043 if (!silc_server_create_channel_key(server, channel, 0)) {
3044 /* We don't have new key, revert to old one */
3045 channel->channel_key = oldkey;
3049 /* Remove old channel key for good */
3050 silc_cipher_free(oldkey);
3052 /* Send the channel key. This sends it to our local clients and if
3053 we are normal server to our router as well. */
3054 silc_server_send_channel_key(server, NULL, channel,
3055 server->server_type == SILC_ROUTER ?
3056 FALSE : !server->standalone);
3059 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3060 /* Cipher mode is unset. Remove the cipher and revert back to
3062 SilcCipher newkey, oldkey;
3063 cipher = channel->cipher;
3065 /* Delete old cipher and allocate default one */
3066 if (!silc_cipher_alloc(cipher ? cipher : SILC_DEFAULT_CIPHER, &newkey)) {
3067 silc_server_command_send_status_data(
3068 cmd, SILC_COMMAND_CMODE,
3069 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
3070 2, cipher, strlen(cipher));
3074 oldkey = channel->channel_key;
3075 channel->channel_key = newkey;
3077 /* Re-generate channel key */
3078 if (!silc_server_create_channel_key(server, channel, 0)) {
3079 /* We don't have new key, revert to old one */
3080 channel->channel_key = oldkey;
3084 /* Remove old channel key for good */
3085 silc_cipher_free(oldkey);
3087 /* Send the channel key. This sends it to our local clients and if
3088 we are normal server to our router as well. */
3089 silc_server_send_channel_key(server, NULL, channel,
3090 server->server_type == SILC_ROUTER ?
3091 FALSE : !server->standalone);
3095 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
3096 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
3097 /* HMAC to use protect the traffic */
3098 unsigned char hash[32];
3102 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
3104 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3105 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
3109 /* Delete old hmac and allocate the new one */
3110 if (!silc_hmac_alloc(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, hash);
3125 silc_hmac_set_key(channel->hmac, hash,
3126 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3127 memset(hash, 0, sizeof(hash));
3130 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
3131 /* Hmac mode is unset. Remove the hmac and revert back to
3134 unsigned char hash[32];
3135 hmac = channel->hmac_name;
3137 /* Delete old hmac and allocate default one */
3138 if (!silc_hmac_alloc(hmac ? hmac : SILC_DEFAULT_HMAC, NULL, &newhmac)) {
3139 silc_server_command_send_status_data(
3140 cmd, SILC_COMMAND_CMODE,
3141 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0,
3142 2, hmac, strlen(hmac));
3146 silc_hmac_free(channel->hmac);
3147 channel->hmac = newhmac;
3149 /* Set the HMAC key out of current channel key. The client must do
3151 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
3152 channel->key_len / 8,
3154 silc_hmac_set_key(channel->hmac, hash,
3155 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3156 memset(hash, 0, sizeof(hash));
3160 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3161 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3162 /* Check if the founder public key was received */
3163 founder_key = idata->public_key;
3164 tmp = silc_argument_get_arg_type(cmd->args, 8, &tmp_len);
3166 if (!silc_pkcs_public_key_payload_decode(tmp, tmp_len, &founder_key)) {
3167 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3168 SILC_STATUS_ERR_AUTH_FAILED,
3173 /* If key was not sent and the channel mode has already founder
3174 then the key was not to be changed. */
3175 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)
3179 /* Set the founder authentication */
3180 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
3182 silc_server_command_send_status_reply(
3183 cmd, SILC_COMMAND_CMODE,
3184 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
3188 /* Verify the payload before setting the mode */
3189 if (!silc_auth_verify_data(tmp, tmp_len, SILC_AUTH_PUBLIC_KEY,
3190 founder_key, 0, server->sha1hash,
3191 client->id, SILC_ID_CLIENT)) {
3192 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3193 SILC_STATUS_ERR_AUTH_FAILED,
3198 /* Save the public key */
3199 if (channel->founder_key)
3200 silc_pkcs_public_key_free(channel->founder_key);
3201 if (silc_argument_get_arg_type(cmd->args, 8, NULL))
3202 channel->founder_key = founder_key;
3204 channel->founder_key = silc_pkcs_public_key_copy(founder_key);
3205 if (!channel->founder_key) {
3206 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3207 SILC_STATUS_ERR_AUTH_FAILED,
3212 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
3214 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3215 SILC_STATUS_ERR_AUTH_FAILED,
3217 silc_pkcs_public_key_free(channel->founder_key);
3218 channel->founder_key = NULL;
3223 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3224 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3225 if (channel->founder_key)
3226 silc_pkcs_public_key_free(channel->founder_key);
3227 channel->founder_key = NULL;
3233 if (mode_mask & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
3234 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3237 chpkdata = silc_argument_get_arg_type(cmd->args, 9, &chpklen);
3239 if (!chpkdata && channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH)
3244 /* Process the channel public key(s) */
3245 st = silc_server_set_channel_pk_list(server, NULL, channel,
3247 if (st != SILC_STATUS_OK) {
3248 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE, st, 0);
3253 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3254 if (channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) {
3255 if (channel->channel_pubkeys)
3256 silc_hash_table_free(channel->channel_pubkeys);
3257 channel->channel_pubkeys = NULL;
3264 /* Finally, set the mode */
3265 old_mask = channel->mode = mode_mask;
3267 /* Send CMODE_CHANGE notify. */
3268 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3269 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT)
3270 SILC_PUT32_MSB(channel->user_limit, ulimit);
3271 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
3272 SILC_NOTIFY_TYPE_CMODE_CHANGE, 8,
3273 cidp->data, cidp->len,
3275 cipher, cipher ? strlen(cipher) : 0,
3276 hmac, hmac ? strlen(hmac) : 0,
3277 passphrase, passphrase ?
3278 strlen(passphrase) : 0,
3279 fkey ? fkey->data : NULL,
3280 fkey ? fkey->len : 0,
3281 chpkdata ? chpkdata : NULL,
3282 chpkdata ? chpklen : 0,
3283 mode_mask & SILC_CHANNEL_MODE_ULIMIT ?
3285 mode_mask & SILC_CHANNEL_MODE_ULIMIT ?
3286 sizeof(ulimit) : 0);
3288 /* Set CMODE notify type to network */
3289 if (chpkdata && chpklen)
3290 silc_buffer_set(&chpk, chpkdata, chpklen);
3291 silc_server_send_notify_cmode(server, SILC_PRIMARY_ROUTE(server),
3292 SILC_BROADCAST(server), channel,
3293 mode_mask, client->id, SILC_ID_CLIENT,
3294 cipher, hmac, passphrase, founder_key,
3295 chpkdata ? &chpk : NULL);
3298 chpklist = silc_server_get_channel_pk_list(server, channel, FALSE, FALSE);
3300 /* Send command reply to sender */
3301 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3302 SILC_STATUS_OK, 0, ident, 5,
3303 2, tmp_id, tmp_len2,
3305 4, fkey ? fkey->data : NULL,
3306 fkey ? fkey->len : 0,
3307 5, chpklist ? chpklist->data :
3308 NULL, chpklist ? chpklist->len
3311 SILC_CHANNEL_MODE_ULIMIT ?
3314 SILC_CHANNEL_MODE_ULIMIT ?
3315 sizeof(ulimit) : 0));
3317 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3318 packet->data, packet->len, FALSE);
3320 silc_buffer_free(packet);
3321 silc_buffer_free(cidp);
3324 channel->mode = old_mask;
3325 silc_buffer_free(chpklist);
3326 silc_buffer_free(fkey);
3327 silc_free(channel_id);
3328 silc_server_command_free(cmd);
3331 /* Server side of CUMODE command. Changes client's mode on a channel. */
3333 SILC_SERVER_CMD_FUNC(cumode)
3335 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3336 SilcServer server = cmd->server;
3337 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3338 SilcChannelID *channel_id = NULL;
3339 SilcClientID *client_id = NULL;
3340 SilcChannelEntry channel;
3341 SilcClientEntry target_client;
3342 SilcChannelClientEntry chl;
3343 SilcBuffer packet, idp;
3344 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3345 SilcUInt32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3347 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3348 SilcPublicKey founder_key = NULL;
3349 SilcBuffer fkey = NULL;
3354 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
3356 /* Get Channel ID */
3357 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3359 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3360 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3363 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len, NULL);
3365 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3366 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3370 /* Get channel entry */
3371 channel = silc_idlist_find_channel_by_id(server->local_list,
3374 channel = silc_idlist_find_channel_by_id(server->global_list,
3377 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3378 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
3379 0, 2, tmp_ch_id, tmp_ch_len);
3384 /* Check whether sender is on the channel */
3385 if (!silc_server_client_on_channel(client, channel, &chl)) {
3386 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3387 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
3388 2, tmp_ch_id, tmp_ch_len);
3391 sender_mask = chl->mode;
3393 /* Get the target client's channel mode mask */
3394 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3396 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3397 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3401 SILC_GET32_MSB(target_mask, tmp_mask);
3403 /* Get target Client ID */
3404 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3406 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3407 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3410 client_id = silc_id_payload_parse_id(tmp_id, tmp_len, NULL);
3412 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3413 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3417 /* Get target client's entry */
3418 target_client = silc_idlist_find_client_by_id(server->local_list,
3419 client_id, TRUE, NULL);
3421 target_client = silc_idlist_find_client_by_id(server->global_list,
3422 client_id, TRUE, NULL);
3424 if (target_client != client &&
3425 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3426 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3427 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3428 SILC_STATUS_ERR_NOT_YOU, 0,
3429 2, tmp_ch_id, tmp_ch_len);
3433 /* Check whether target client is on the channel */
3434 if (target_client != client) {
3435 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3436 silc_server_command_send_status_data2(
3437 cmd, SILC_COMMAND_CUMODE,
3438 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL, 0,
3440 3, tmp_ch_id, tmp_ch_len);
3449 /* If the target client is founder, no one else can change their mode
3451 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && client != target_client) {
3452 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3453 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3454 0, 2, tmp_ch_id, tmp_ch_len);
3458 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3459 if (target_client != client) {
3460 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3461 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3466 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3467 /* The client tries to claim the founder rights. */
3468 unsigned char *tmp_auth;
3469 SilcUInt32 tmp_auth_len;
3470 SilcChannelClientEntry chl2;
3471 SilcHashTableList htl;
3473 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3474 !channel->founder_key) {
3475 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3476 SILC_STATUS_ERR_AUTH_FAILED, 0);
3480 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3482 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3483 SILC_STATUS_ERR_AUTH_FAILED, 0);
3487 /* Verify the authentication payload */
3488 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len, SILC_AUTH_PUBLIC_KEY,
3489 channel->founder_key, 0, server->sha1hash,
3490 client->id, SILC_ID_CLIENT)) {
3491 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3492 SILC_STATUS_ERR_AUTH_FAILED, 0);
3497 founder_key = channel->founder_key;
3498 fkey = silc_pkcs_public_key_payload_encode(founder_key);
3500 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3501 SILC_STATUS_ERR_AUTH_FAILED, 0);
3505 /* There cannot be anyone else as founder on the channel now. This
3506 client is definitely the founder due to this authentication. This
3507 is done only on router, not on server, since server cannot know
3508 whether router will accept this mode change or not. XXX This
3509 probably shouldn't be done anymore at all, may cause problems in
3510 router-router connections too (maybe just AUTH_FAILED error should
3511 be returned). -Pekka */
3512 if (server->server_type == SILC_ROUTER) {
3513 silc_hash_table_list(channel->user_list, &htl);
3514 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
3515 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
3516 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3517 silc_server_force_cumode_change(server, NULL, channel, chl2,
3521 silc_hash_table_list_reset(&htl);
3524 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
3527 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3528 if (target_client == client) {
3529 /* Remove channel founder rights from itself */
3530 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3533 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3534 SILC_STATUS_ERR_NOT_YOU, 0);
3540 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
3541 /* Promote to operator */
3542 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
3543 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3544 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3545 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3546 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3547 0, 2, tmp_ch_id, tmp_ch_len);
3551 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
3555 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
3556 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3557 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3558 silc_server_command_send_status_data(cmd, SILC_COMMAND_CUMODE,
3559 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3560 0, 2, tmp_ch_id, tmp_ch_len);
3564 /* Demote to normal user */
3565 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
3570 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3571 if (target_client != client) {
3572 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3573 SILC_STATUS_ERR_NOT_YOU, 0);
3577 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)) {
3578 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3582 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3583 if (target_client != client) {
3584 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3585 SILC_STATUS_ERR_NOT_YOU, 0);
3589 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3594 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3595 if (target_client != client) {
3596 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3597 SILC_STATUS_ERR_NOT_YOU, 0);
3601 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS)) {
3602 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3606 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3607 if (target_client != client) {
3608 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3609 SILC_STATUS_ERR_NOT_YOU, 0);
3613 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3618 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3619 if (target_client != client) {
3620 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3621 SILC_STATUS_ERR_NOT_YOU, 0);
3625 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS)) {
3626 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3630 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3631 if (target_client != client) {
3632 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3633 SILC_STATUS_ERR_NOT_YOU, 0);
3637 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3642 if (target_mask & SILC_CHANNEL_UMODE_QUIET) {
3643 if (!(chl->mode & SILC_CHANNEL_UMODE_QUIET)) {
3644 if (client == target_client) {
3645 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3646 SILC_STATUS_ERR_PERM_DENIED, 0);
3649 chl->mode |= SILC_CHANNEL_UMODE_QUIET;
3653 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
3654 if (client == target_client) {
3655 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3656 SILC_STATUS_ERR_PERM_DENIED, 0);
3659 chl->mode &= ~SILC_CHANNEL_UMODE_QUIET;
3664 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3665 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3667 /* Send notify to channel, notify only if mode was actually changed. */
3669 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
3670 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
3671 idp->data, idp->len,
3674 fkey ? fkey->data : NULL,
3675 fkey ? fkey->len : 0);
3677 /* Set CUMODE notify type to network */
3678 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
3679 SILC_BROADCAST(server), channel,
3680 target_mask, client->id, SILC_ID_CLIENT,
3681 target_client->id, founder_key);
3684 /* Send command reply to sender */
3685 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
3686 SILC_STATUS_OK, 0, ident, 3,
3688 3, tmp_ch_id, tmp_ch_len,
3689 4, tmp_id, tmp_len);
3690 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3691 packet->data, packet->len, FALSE);
3693 silc_buffer_free(packet);
3694 silc_buffer_free(idp);
3697 silc_free(channel_id);
3698 silc_free(client_id);
3699 silc_buffer_free(fkey);
3700 silc_server_command_free(cmd);
3703 /* Server side of KICK command. Kicks client out of channel. */
3705 SILC_SERVER_CMD_FUNC(kick)
3707 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3708 SilcServer server = cmd->server;
3709 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3710 SilcClientEntry target_client;
3711 SilcChannelID *channel_id;
3712 SilcClientID *client_id;
3713 SilcChannelEntry channel;
3714 SilcChannelClientEntry chl;
3715 SilcBuffer idp, packet;
3716 SilcUInt32 tmp_len, target_idp_len, clen;
3717 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3718 unsigned char *tmp, *comment, *target_idp;
3723 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
3725 /* Get Channel ID */
3726 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3728 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3729 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3732 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
3734 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3735 SILC_STATUS_ERR_BAD_CHANNEL_ID, 0,
3740 /* Get channel entry */
3741 channel = silc_idlist_find_channel_by_id(server->local_list,
3744 channel = silc_idlist_find_channel_by_id(server->local_list,
3747 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3748 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
3749 0, 2, tmp, tmp_len);
3754 /* Check whether sender is on the channel */
3755 if (!silc_server_client_on_channel(client, channel, &chl)) {
3756 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3757 SILC_STATUS_ERR_NOT_ON_CHANNEL,
3758 0, 2, tmp, tmp_len);
3762 /* Check that the kicker is channel operator or channel founder */
3763 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
3764 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3765 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3766 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3767 0, 2, tmp, tmp_len);
3771 /* Get target Client ID */
3772 target_idp = silc_argument_get_arg_type(cmd->args, 2, &target_idp_len);
3774 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3775 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3778 client_id = silc_id_payload_parse_id(target_idp, target_idp_len, NULL);
3780 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3781 SILC_STATUS_ERR_BAD_CLIENT_ID,
3782 0, 2, target_idp, target_idp_len);
3786 /* Get target client's entry */
3787 target_client = silc_idlist_find_client_by_id(server->local_list,
3788 client_id, TRUE, NULL);
3789 if (!target_client) {
3790 target_client = silc_idlist_find_client_by_id(server->global_list,
3791 client_id, TRUE, NULL);
3794 /* Check whether target client is on the channel */
3795 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3796 silc_server_command_send_status_data2(cmd, SILC_COMMAND_KICK,
3797 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL,
3798 0, 2, target_idp, target_idp_len,
3803 /* Check that the target client is not channel founder. Channel founder
3804 cannot be kicked from the channel. */
3805 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3806 silc_server_command_send_status_data(cmd, SILC_COMMAND_KICK,
3807 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3808 0, 2, tmp, tmp_len);
3813 comment = silc_argument_get_arg_type(cmd->args, 3, &clen);
3818 /* Send the reply back to the client */
3820 silc_command_reply_payload_encode_va(SILC_COMMAND_KICK,
3821 SILC_STATUS_OK, 0, ident, 2,
3823 3, target_idp, target_idp_len);
3824 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3825 packet->data, packet->len, FALSE);
3826 silc_buffer_free(packet);
3828 /* Send KICKED notify to local clients on the channel */
3829 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3830 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, TRUE,
3831 SILC_NOTIFY_TYPE_KICKED, 3,
3832 target_idp, target_idp_len,
3833 comment, comment ? strlen(comment) : 0,
3834 idp->data, idp->len);
3835 silc_buffer_free(idp);
3837 /* Send KICKED notify to primary route */
3838 silc_server_send_notify_kicked(server, SILC_PRIMARY_ROUTE(server),
3839 SILC_BROADCAST(server), channel,
3840 target_client->id, client->id, comment);
3842 /* Remove the client from channel's invite list */
3843 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
3845 silc_argument_payload_encode_one(NULL, target_idp, target_idp_len, 3);
3846 SilcArgumentPayload args =
3847 silc_argument_payload_parse(ab->data, ab->len, 1);
3849 silc_server_inviteban_process(server, channel->invite_list, 1, args);
3850 silc_buffer_free(ab);
3851 silc_argument_payload_free(args);
3854 /* Remove the client from the channel. If the channel does not exist
3855 after removing the client then the client kicked itself off the channel
3856 and we don't have to send anything after that. */
3857 if (!silc_server_remove_from_one_channel(server, NULL, channel,
3858 target_client, FALSE))
3861 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3862 /* Re-generate channel key */
3863 if (!silc_server_create_channel_key(server, channel, 0))
3866 /* Send the channel key to the channel. The key of course is not sent
3867 to the client who was kicked off the channel. */
3868 silc_server_send_channel_key(server, target_client->connection, channel,
3869 server->server_type == SILC_ROUTER ?
3870 FALSE : !server->standalone);
3874 silc_server_command_free(cmd);
3877 /* Server side of OPER command. Client uses this comand to obtain server
3878 operator privileges to this server/router. */
3880 SILC_SERVER_CMD_FUNC(oper)
3882 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3883 SilcServer server = cmd->server;
3884 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3885 unsigned char *username = NULL, *auth;
3887 SilcServerConfigAdmin *admin;
3888 SilcIDListData idata = (SilcIDListData)client;
3889 bool result = FALSE;
3890 SilcPublicKey cached_key;
3892 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3895 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
3897 /* Get the username */
3898 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3900 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3901 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3906 /* Check username */
3907 username = silc_identifier_check(username, strlen(username),
3908 SILC_STRING_UTF8, 128, &tmp_len);
3910 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3911 SILC_STATUS_ERR_BAD_USERNAME,
3916 /* Get the admin configuration */
3917 admin = silc_server_config_find_admin(server, cmd->sock->ip,
3918 username, client->nickname);
3920 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
3921 username, client->nickname);
3923 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3924 SILC_STATUS_ERR_AUTH_FAILED,
3926 SILC_LOG_INFO(("OPER authentication failed for username '%s' by "
3927 "nickname '%s' from %s", username,
3928 client->nickname, cmd->sock->hostname));
3933 /* Get the authentication payload */
3934 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3936 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3937 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3942 /* Verify the authentication data. If both passphrase and public key
3943 is set then try both of them. */
3944 if (admin->passphrase)
3945 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
3946 admin->passphrase, admin->passphrase_len,
3947 idata->hash, client->id, SILC_ID_CLIENT);
3948 if (!result && admin->publickeys) {
3949 cached_key = silc_server_get_public_key(server, admin->publickeys);
3952 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
3953 cached_key, 0, idata->hash,
3954 client->id, SILC_ID_CLIENT);
3957 /* Authentication failed */
3958 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3959 SILC_STATUS_ERR_AUTH_FAILED,
3964 /* Client is now server operator */
3965 client->mode |= SILC_UMODE_SERVER_OPERATOR;
3967 /* Update statistics */
3968 if (SILC_IS_LOCAL(client))
3969 server->stat.my_server_ops++;
3970 if (server->server_type == SILC_ROUTER)
3971 server->stat.server_ops++;
3973 /* Send UMODE change to primary router */
3974 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
3975 SILC_BROADCAST(server), client->id,
3978 /* Check if anyone is watching this nickname */
3979 if (server->server_type == SILC_ROUTER)
3980 silc_server_check_watcher_list(server, client, NULL,
3981 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3983 /* Send reply to the sender */
3984 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3988 silc_free(username);
3989 silc_server_command_free(cmd);
3992 SILC_TASK_CALLBACK(silc_server_command_detach_cb)
3994 SilcServer server = app_context;
3995 QuitInternal q = (QuitInternal)context;
3996 SilcClientID *client_id = (SilcClientID *)q->sock;
3997 SilcClientEntry client;
3998 SilcSocketConnection sock;
4000 client = silc_idlist_find_client_by_id(server->local_list, client_id,
4002 if (client && client->connection) {
4003 sock = client->connection;
4005 /* If there is pending outgoing data for the client then purge it
4006 to the network before closing connection. */
4007 silc_server_packet_queue_purge(server, sock);
4009 /* Close the connection on our side */
4010 client->router = NULL;
4011 client->connection = NULL;
4012 sock->user_data = NULL;
4013 silc_server_close_connection(server, sock);
4016 silc_free(client_id);
4020 SILC_TASK_CALLBACK(silc_server_command_detach_timeout)
4022 SilcServer server = app_context;
4023 QuitInternal q = (QuitInternal)context;
4024 SilcClientID *client_id = (SilcClientID *)q->sock;
4025 SilcClientEntry client;
4027 client = silc_idlist_find_client_by_id(server->local_list, client_id,
4029 if (client && client->mode & SILC_UMODE_DETACHED) {
4030 SILC_LOG_DEBUG(("Detach timeout"));
4031 silc_server_free_client_data(server, NULL, client, TRUE,
4035 silc_free(client_id);
4039 /* Server side of DETACH command. Detached the client from the network
4040 by closing the connection but preserving the session. */
4042 SILC_SERVER_CMD_FUNC(detach)
4044 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4045 SilcServer server = cmd->server;
4046 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4049 if (server->config->detach_disabled) {
4050 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
4051 SILC_STATUS_ERR_OPERATION_ALLOWED,
4056 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4059 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_DETACH, cmd, 0, 0);
4061 /* Remove operator privileges, since the client may resume in some
4062 other server which to it does not have operator privileges. */
4063 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
4064 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
4066 /* Send the user mode notify to notify that client is detached */
4067 client->mode |= SILC_UMODE_DETACHED;
4068 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
4069 client->data.status &= ~SILC_IDLIST_STATUS_NOATTR;
4070 client->last_command = 0;
4071 client->fast_command = 0;
4072 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
4073 SILC_BROADCAST(server), client->id,
4075 server->stat.my_detached++;
4077 /* Check if anyone is watching this nickname */
4078 if (server->server_type == SILC_ROUTER)
4079 silc_server_check_watcher_list(server, client, NULL,
4080 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4082 q = silc_calloc(1, sizeof(*q));
4083 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
4084 silc_schedule_task_add(server->schedule, 0, silc_server_command_detach_cb,
4085 q, 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
4087 if (server->config->detach_timeout) {
4088 q = silc_calloc(1, sizeof(*q));
4089 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
4090 silc_schedule_task_add(server->schedule, 0,
4091 silc_server_command_detach_timeout,
4092 q, server->config->detach_timeout * 60,
4093 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
4096 /* Send reply to the sender */
4097 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
4101 silc_server_command_free(cmd);
4104 /* Server side of WATCH command. */
4106 SILC_SERVER_CMD_FUNC(watch)
4108 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4109 SilcServer server = cmd->server;
4110 char *add_nick, *del_nick;
4111 SilcUInt32 add_nick_len, del_nick_len, tmp_len, pk_len;
4112 unsigned char hash[16], *tmp, *pk, *nick;
4113 SilcClientEntry client;
4114 SilcClientID *client_id = NULL;
4115 SilcUInt16 old_ident;
4117 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WATCH, cmd, 1, 3);
4119 if (server->server_type != SILC_ROUTER && !server->standalone) {
4120 if (!cmd->pending) {
4121 /* Send the command to router */
4124 /* If backup receives this from primary, handle it locally */
4125 if (server->server_type == SILC_BACKUP_ROUTER &&
4126 cmd->sock == SILC_PRIMARY_ROUTE(server))
4129 SILC_LOG_DEBUG(("Forwarding WATCH to router"));
4131 old_ident = silc_command_get_ident(cmd->payload);
4132 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4133 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4135 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4136 SILC_PACKET_COMMAND, cmd->packet->flags,
4137 tmpbuf->data, tmpbuf->len, TRUE);
4139 /* Reprocess this packet after received reply from router */
4140 silc_server_command_pending(server, SILC_COMMAND_WATCH,
4141 silc_command_get_ident(cmd->payload),
4142 silc_server_command_watch,
4143 silc_server_command_dup(cmd));
4144 cmd->pending = TRUE;
4145 silc_command_set_ident(cmd->payload, old_ident);
4146 silc_buffer_free(tmpbuf);
4149 SilcServerCommandReplyContext reply = context2;
4155 silc_command_get_status(reply->payload, &status, NULL);
4157 /* Backup router handles the WATCH command also. */
4158 if (server->server_type != SILC_BACKUP_ROUTER ||
4159 SILC_STATUS_IS_ERROR(status)) {
4160 /* Received reply from router, just send same data to the client. */
4161 SILC_LOG_DEBUG(("Received reply to WATCH from router"));
4162 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH, status,
4169 /* We are router and keep the watch list for local cell */
4172 /* Get the client ID */
4173 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4175 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4176 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4180 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
4182 silc_server_command_send_status_data(cmd, SILC_COMMAND_WATCH,
4183 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
4184 0, 2, tmp, tmp_len);
4188 /* Get the client entry which must be in local list */
4189 client = silc_idlist_find_client_by_id(server->local_list,
4190 client_id, TRUE, NULL);
4192 /* Backup checks global list also */
4193 if (server->server_type == SILC_BACKUP_ROUTER)
4194 client = silc_idlist_find_client_by_id(server->global_list,
4195 client_id, TRUE, NULL);
4197 silc_server_command_send_status_data(cmd, SILC_COMMAND_WATCH,
4198 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
4199 0, 2, tmp, tmp_len);
4204 /* Take public key for watching by public key */
4205 pk = silc_argument_get_arg_type(cmd->args, 4, &pk_len);
4208 add_nick = silc_argument_get_arg_type(cmd->args, 2, &add_nick_len);
4209 del_nick = silc_argument_get_arg_type(cmd->args, 3, &del_nick_len);
4210 if (!add_nick && !del_nick && !pk) {
4211 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4212 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4217 if (add_nick && add_nick_len > 128) {
4218 add_nick[128] = '\0';
4221 if (del_nick && del_nick_len > 128) {
4222 del_nick[128] = '\0';
4226 /* Add new nickname to be watched in our cell */
4228 nick = silc_identifier_check(add_nick, add_nick_len, SILC_STRING_UTF8, 128,
4231 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4232 SILC_STATUS_ERR_BAD_NICKNAME, 0);
4236 /* Hash the nick, we have the hash saved, not nicks because we can
4237 do one to one mapping to the nick from Client ID hash this way. */
4238 silc_hash_make(server->md5hash, nick, add_nick_len, hash);
4240 /* Check whether this client is already watching this nickname */
4241 if (silc_hash_table_find_by_context(server->watcher_list, hash,
4243 /* Nickname is alredy being watched for this client */
4244 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4245 SILC_STATUS_ERR_NICKNAME_IN_USE,
4251 /* Get the nickname from the watcher list and use the same key in
4252 new entries as well. If key doesn't exist then create it. */
4253 if (!silc_hash_table_find(server->watcher_list, hash, (void *)&tmp, NULL))
4254 tmp = silc_memdup(hash, CLIENTID_HASH_LEN);
4256 /* Add the client to the watcher list with the specified nickname hash. */
4257 silc_hash_table_add(server->watcher_list, tmp, client);
4261 /* Delete nickname from watch list */
4263 nick = silc_identifier_check(del_nick, del_nick_len, SILC_STRING_UTF8, 128,
4266 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4267 SILC_STATUS_ERR_BAD_NICKNAME, 0);
4271 /* Hash the nick, we have the hash saved, not nicks because we can
4272 do one to one mapping to the nick from Client ID hash this way. */
4273 silc_hash_make(server->md5hash, nick, del_nick_len, hash);
4275 /* Check that this client is watching for this nickname */
4276 if (!silc_hash_table_find_by_context(server->watcher_list, hash,
4277 client, (void *)&tmp)) {
4278 /* Nickname is alredy being watched for this client */
4279 silc_server_command_send_status_data(cmd, SILC_COMMAND_WATCH,
4280 SILC_STATUS_ERR_NO_SUCH_NICK, 0,
4281 2, nick, del_nick_len);
4286 /* Delete the nickname from the watcher list. */
4287 silc_hash_table_del_by_context(server->watcher_list, hash, client);
4289 /* Now check whether there still exists entries with this key, if not
4290 then free the key to not leak memory. */
4291 if (!silc_hash_table_find(server->watcher_list, hash, NULL, NULL))
4296 /* Add/del public key */
4299 SilcArgumentPayload pkargs;
4301 SilcPublicKey public_key, pkkey;
4304 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4305 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4310 /* Get the argument from the Argument List Payload */
4311 SILC_GET16_MSB(pkargc, pk);
4312 pkargs = silc_argument_payload_parse(pk + 2, pk_len - 2, pkargc);
4314 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4315 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4320 pk = silc_argument_get_next_arg(pkargs, &type, &pk_len);
4322 if (!silc_pkcs_public_key_payload_decode(pk, pk_len, &public_key))
4328 /* Add public key to watch list */
4330 /* Check whether this client is already watching this public key */
4331 if (silc_hash_table_find_by_context(server->watcher_list_pk,
4332 public_key, client, NULL)) {
4333 silc_pkcs_public_key_free(public_key);
4334 silc_server_command_send_status_reply(
4335 cmd, SILC_COMMAND_WATCH,
4336 SILC_STATUS_ERR_NICKNAME_IN_USE, 0);
4340 /* Get the public key from the watcher list and use the same key in
4341 new entries as well. If key doesn't exist then create it. */
4343 if (!silc_hash_table_find(server->watcher_list_pk, public_key,
4344 (void *)&pkkey, NULL))
4347 silc_pkcs_public_key_free(public_key);
4349 /* Add the client to the watcher list with the specified public
4351 silc_hash_table_add(server->watcher_list_pk, pkkey, client);
4353 } else if (type == 0x01) {
4354 /* Delete public key from watch list */
4356 /* Check that this client is watching this public key */
4357 if (silc_hash_table_find_by_context(server->watcher_list_pk,
4360 silc_pkcs_public_key_free(public_key);
4361 silc_server_command_send_status_reply(
4362 cmd, SILC_COMMAND_WATCH,
4363 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
4367 /* Delete the public key from the watcher list. */
4368 silc_hash_table_del_by_context(server->watcher_list_pk,
4369 public_key, client);
4371 /* Now check whether there still exists entries with this key, if
4372 not then free the key to not leak memory. */
4373 if (!silc_hash_table_find(server->watcher_list_pk, hash, NULL, NULL))
4374 silc_pkcs_public_key_free(pkkey);
4375 silc_pkcs_public_key_free(public_key);
4378 pk = silc_argument_get_next_arg(pkargs, &type, &pk_len);
4383 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4386 /* Distribute the watch list to backup routers too */
4387 if (server->backup) {
4389 old_ident = silc_command_get_ident(cmd->payload);
4390 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4391 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4392 silc_server_backup_send(server, cmd->sock->user_data, SILC_PACKET_COMMAND,
4393 cmd->packet->flags, tmpbuf->data, tmpbuf->len,
4395 silc_command_set_ident(cmd->payload, old_ident);
4396 silc_buffer_free(tmpbuf);
4400 silc_free(client_id);
4401 silc_server_command_free(cmd);
4404 /* Server side of SILCOPER command. Client uses this comand to obtain router
4405 operator privileges to this router. */
4407 SILC_SERVER_CMD_FUNC(silcoper)
4409 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4410 SilcServer server = cmd->server;
4411 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4412 unsigned char *username = NULL, *auth;
4414 SilcServerConfigAdmin *admin;
4415 SilcIDListData idata = (SilcIDListData)client;
4416 bool result = FALSE;
4417 SilcPublicKey cached_key;
4419 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4422 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
4424 if (server->server_type != SILC_ROUTER) {
4425 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4426 SILC_STATUS_ERR_AUTH_FAILED, 0);
4430 /* Get the username */
4431 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4433 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4434 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4439 /* Check username */
4440 username = silc_identifier_check(username, tmp_len, SILC_STRING_UTF8, 128,
4443 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4444 SILC_STATUS_ERR_BAD_USERNAME,
4449 /* Get the admin configuration */
4450 admin = silc_server_config_find_admin(server, cmd->sock->ip,
4451 username, client->nickname);
4453 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
4454 username, client->nickname);
4456 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4457 SILC_STATUS_ERR_AUTH_FAILED, 0);
4458 SILC_LOG_INFO(("SILCOPER authentication failed for username '%s' by "
4459 "nickname '%s' from %s", username,
4460 client->nickname, cmd->sock->hostname));
4465 /* Get the authentication payload */
4466 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4468 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4469 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4474 /* Verify the authentication data. If both passphrase and public key
4475 is set then try both of them. */
4476 if (admin->passphrase)
4477 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
4478 admin->passphrase, admin->passphrase_len,
4479 idata->hash, client->id, SILC_ID_CLIENT);
4480 if (!result && admin->publickeys) {
4481 cached_key = silc_server_get_public_key(server, admin->publickeys);
4484 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
4485 cached_key, 0, idata->hash,
4486 client->id, SILC_ID_CLIENT);
4489 /* Authentication failed */
4490 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4491 SILC_STATUS_ERR_AUTH_FAILED, 0);
4495 /* Client is now router operator */
4496 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4498 /* Update statistics */
4499 if (SILC_IS_LOCAL(client))
4500 server->stat.my_router_ops++;
4501 if (server->server_type == SILC_ROUTER)
4502 server->stat.router_ops++;
4504 /* Send UMODE change to primary router */
4505 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
4506 SILC_BROADCAST(server), client->id,
4509 /* Check if anyone is watching this nickname */
4510 if (server->server_type == SILC_ROUTER)
4511 silc_server_check_watcher_list(server, client, NULL,
4512 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4514 /* Send reply to the sender */
4515 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4519 silc_free(username);
4520 silc_server_command_free(cmd);
4523 /* Server side of command BAN. This is used to manage the ban list of the
4524 channel. To add clients and remove clients from the ban list. */
4526 SILC_SERVER_CMD_FUNC(ban)
4528 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4529 SilcServer server = cmd->server;
4530 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4531 SilcBuffer packet, list, tmp2;
4532 SilcChannelEntry channel;
4533 SilcChannelClientEntry chl;
4534 SilcChannelID *channel_id = NULL;
4535 unsigned char *id, *tmp, *atype = NULL;
4536 SilcUInt32 id_len, len, len2;
4537 SilcArgumentPayload args;
4538 SilcHashTableList htl;
4540 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
4541 SilcBufferStruct blist;
4543 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4546 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
4548 /* Get Channel ID */
4549 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4551 channel_id = silc_id_payload_parse_id(id, id_len, NULL);
4553 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4554 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4559 /* Get channel entry. The server must know about the channel since the
4560 client is expected to be on the channel. */
4561 channel = silc_idlist_find_channel_by_id(server->local_list,
4564 channel = silc_idlist_find_channel_by_id(server->global_list,
4567 silc_server_command_send_status_data(cmd, SILC_COMMAND_BAN,
4568 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
4574 /* Check whether this client is on the channel */
4575 if (!silc_server_client_on_channel(client, channel, &chl)) {
4576 silc_server_command_send_status_data(cmd, SILC_COMMAND_BAN,
4577 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
4582 /* The client must be at least channel operator. */
4583 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4584 silc_server_command_send_status_data(cmd, SILC_COMMAND_BAN,
4585 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0,
4590 /* Get the ban information */
4591 tmp = silc_argument_get_arg_type(cmd->args, 3, &len2);
4592 if (tmp && len2 > 2) {
4593 /* Parse the arguments to see they are constructed correctly */
4594 SILC_GET16_MSB(argc, tmp);
4595 args = silc_argument_payload_parse(tmp + 2, len2 - 2, argc);
4597 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4598 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4603 /* Get the type of action */
4604 atype = silc_argument_get_arg_type(cmd->args, 2, &len);
4605 if (atype && len == 1) {
4606 if (atype[0] == 0x00) {
4607 /* Allocate hash table for ban list if it doesn't exist yet */
4608 if (!channel->ban_list)
4610 silc_hash_table_alloc(0, silc_hash_ptr,
4612 silc_server_inviteban_destruct, channel,
4615 /* Check for resource limit */
4616 if (silc_hash_table_count(channel->ban_list) > 64) {
4617 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4618 SILC_STATUS_ERR_RESOURCE_LIMIT,
4624 /* Now add or delete the information. */
4625 if (!silc_server_inviteban_process(server, channel->ban_list,
4626 (SilcUInt8)atype[0], args)) {
4627 silc_server_command_send_status_reply(
4628 cmd, SILC_COMMAND_BAN,
4629 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4634 silc_argument_payload_free(args);
4637 /* Encode ban list */
4639 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
4640 list = silc_buffer_alloc_size(2);
4641 silc_buffer_format(list,
4642 SILC_STR_UI_SHORT(silc_hash_table_count(
4643 channel->ban_list)),
4645 silc_hash_table_list(channel->ban_list, &htl);
4646 while (silc_hash_table_get(&htl, (void *)&type, (void *)&tmp2))
4647 list = silc_argument_payload_encode_one(list, tmp2->data, tmp2->len,
4649 silc_hash_table_list_reset(&htl);
4652 /* Send BAN notify type to local servers (but not clients) and to
4654 if (atype && tmp && len2) {
4655 silc_buffer_set(&blist, tmp, len2);
4657 /* Send to local servers if we are router */
4658 if (server->server_type == SILC_ROUTER)
4659 silc_server_send_notify_to_channel(server, NULL, channel, FALSE, FALSE,
4660 SILC_NOTIFY_TYPE_BAN, 3,
4663 tmp ? blist.data : NULL,
4664 tmp ? blist.len : 0);
4666 /* Send to network. */
4667 silc_server_send_notify_ban(server, SILC_PRIMARY_ROUTE(server),
4668 SILC_BROADCAST(server), channel, atype,
4672 /* Send the reply back to the client */
4674 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4675 SILC_STATUS_OK, 0, ident, 2,
4677 3, list ? list->data : NULL,
4678 list ? list->len : 0);
4679 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4680 packet->data, packet->len, FALSE);
4682 silc_buffer_free(packet);
4683 silc_buffer_free(list);
4686 silc_free(channel_id);
4687 silc_server_command_free(cmd);
4690 /* Server side command of LEAVE. Removes client from a channel. */
4692 SILC_SERVER_CMD_FUNC(leave)
4694 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4695 SilcServer server = cmd->server;
4696 SilcSocketConnection sock = cmd->sock;
4697 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4698 SilcChannelID *id = NULL;
4699 SilcChannelEntry channel;
4703 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !id_entry)
4706 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
4708 /* Get Channel ID */
4709 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4711 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4712 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4715 id = silc_id_payload_parse_id(tmp, len, NULL);
4717 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4718 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4722 /* Get channel entry */
4723 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4725 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4727 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
4728 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
4734 /* Check whether this client is on the channel */
4735 if (!silc_server_client_on_channel(id_entry, channel, NULL)) {
4736 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
4737 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0,
4742 /* Notify routers that they should remove this client from their list
4743 of clients on the channel. Send LEAVE notify type. */
4744 silc_server_send_notify_leave(server, SILC_PRIMARY_ROUTE(server),
4745 SILC_BROADCAST(server), channel, id_entry->id);
4747 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
4748 SILC_STATUS_OK, 0, 2, tmp, len);
4750 /* Remove client from channel */
4751 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4753 /* If the channel does not exist anymore we won't send anything */
4756 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4757 /* Re-generate channel key */
4758 if (!silc_server_create_channel_key(server, channel, 0))
4761 /* Send the channel key */
4762 silc_server_send_channel_key(server, NULL, channel,
4763 server->server_type == SILC_ROUTER ?
4764 FALSE : !server->standalone);
4769 silc_server_command_free(cmd);
4772 /* Server side of command USERS. Resolves clients and their USERS currently
4773 joined on the requested channel. The list of Client ID's and their modes
4774 on the channel is sent back. */
4776 SILC_SERVER_CMD_FUNC(users)
4778 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4779 SilcServer server = cmd->server;
4780 SilcChannelEntry channel;
4781 SilcChannelID *id = NULL;
4782 SilcBuffer packet, idp;
4783 unsigned char *channel_id;
4784 SilcUInt32 channel_id_len;
4785 SilcBuffer client_id_list;
4786 SilcBuffer client_mode_list;
4787 unsigned char lc[4];
4788 SilcUInt32 list_count = 0;
4789 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4790 char *channel_name, *channel_namec = NULL;
4792 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
4794 /* Get Channel ID */
4795 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4797 /* Get channel name */
4798 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
4800 if (!channel_id && !channel_name) {
4801 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4802 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4806 /* Check channel name */
4808 channel_namec = silc_channel_name_check(channel_name, strlen(channel_name),
4809 SILC_STRING_UTF8, 256, NULL);
4810 if (!channel_namec) {
4811 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4812 SILC_STATUS_ERR_BAD_CHANNEL, 0);
4817 /* Check Channel ID */
4819 id = silc_id_payload_parse_id(channel_id, channel_id_len, NULL);
4821 silc_server_command_send_status_data(cmd, SILC_COMMAND_USERS,
4822 SILC_STATUS_ERR_BAD_CHANNEL_ID, 0,
4823 2, channel_id, channel_id_len);
4828 /* If we are server and we don't know about this channel we will send
4829 the command to our router. If we know about the channel then we also
4830 have the list of users already. */
4832 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4834 channel = silc_idlist_find_channel_by_name(server->local_list,
4835 channel_namec, NULL);
4837 if (!channel || (!server->standalone && (channel->disabled ||
4838 !channel->users_resolved))) {
4839 if (server->server_type != SILC_ROUTER && !server->standalone &&
4843 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4844 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4846 /* Send USERS command */
4847 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4848 SILC_PACKET_COMMAND, cmd->packet->flags,
4849 tmpbuf->data, tmpbuf->len, TRUE);
4851 /* Reprocess this packet after received reply */
4852 silc_server_command_pending(server, SILC_COMMAND_USERS,
4853 silc_command_get_ident(cmd->payload),
4854 silc_server_command_users,
4855 silc_server_command_dup(cmd));
4856 cmd->pending = TRUE;
4857 silc_command_set_ident(cmd->payload, ident);
4858 silc_buffer_free(tmpbuf);
4863 /* Check the global list as well. */
4865 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4867 channel = silc_idlist_find_channel_by_name(server->global_list,
4868 channel_namec, NULL);
4870 /* Channel really does not exist */
4872 silc_server_command_send_status_data(
4873 cmd, SILC_COMMAND_USERS,
4874 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID, 0,
4875 2, channel_id, channel_id_len);
4877 silc_server_command_send_status_data(
4878 cmd, SILC_COMMAND_USERS,
4879 SILC_STATUS_ERR_NO_SUCH_CHANNEL, 0,
4880 2, channel_name, strlen(channel_name));
4885 /* If the channel is private or secret do not send anything, unless the
4886 user requesting this command is on the channel or is server */
4887 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
4888 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)
4889 && !silc_server_client_on_channel(cmd->sock->user_data, channel,
4891 silc_server_command_send_status_data(cmd, SILC_COMMAND_USERS,
4892 SILC_STATUS_ERR_NO_SUCH_CHANNEL, 0,
4893 2, channel->channel_name,
4894 strlen(channel->channel_name));
4899 /* Get the users list */
4900 if (!silc_server_get_users_on_channel(server, channel, &client_id_list,
4901 &client_mode_list, &list_count)) {
4903 client_id_list = NULL;
4904 client_mode_list = NULL;
4908 SILC_PUT32_MSB(list_count, lc);
4911 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4912 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4913 SILC_STATUS_OK, 0, ident, 4,
4914 2, idp->data, idp->len,
4917 client_id_list->data : NULL,
4919 client_id_list->len : 0,
4920 5, client_mode_list ?
4921 client_mode_list->data : NULL,
4923 client_mode_list->len : 0);
4924 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4925 packet->data, packet->len, FALSE);
4927 silc_buffer_free(idp);
4928 silc_buffer_free(packet);
4930 silc_buffer_free(client_id_list);
4931 if (client_mode_list)
4932 silc_buffer_free(client_mode_list);
4936 silc_free(channel_namec);
4937 silc_server_command_free(cmd);
4940 /* Server side of command GETKEY. This fetches the client's public key
4941 from the server where to the client is connected. */
4943 SILC_SERVER_CMD_FUNC(getkey)
4945 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4946 SilcServer server = cmd->server;
4948 SilcClientEntry client;
4949 SilcServerEntry server_entry;
4950 SilcClientID *client_id = NULL;
4951 SilcServerID *server_id = NULL;
4952 SilcIDPayload idp = NULL;
4953 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4956 SilcBuffer pk = NULL;
4958 SilcPublicKey public_key;
4960 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4962 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4963 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4967 idp = silc_id_payload_parse(tmp, tmp_len);
4969 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4970 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4975 id_type = silc_id_payload_get_type(idp);
4976 if (id_type == SILC_ID_CLIENT) {
4977 client_id = silc_id_payload_get_id(idp);
4979 /* If the client is not found from local list there is no chance it
4980 would be locally connected client so send the command further. */
4981 client = silc_idlist_find_client_by_id(server->local_list,
4982 client_id, TRUE, NULL);
4984 client = silc_idlist_find_client_by_id(server->global_list,
4985 client_id, TRUE, NULL);
4987 if ((!client && !cmd->pending && !server->standalone) ||
4988 (client && !client->connection && !cmd->pending &&
4989 !(client->mode & SILC_UMODE_DETACHED)) ||
4990 (client && !client->data.public_key && !cmd->pending)) {
4992 SilcUInt16 old_ident;
4993 SilcSocketConnection dest_sock;
4995 dest_sock = silc_server_get_client_route(server, NULL, 0,
4996 client_id, NULL, NULL);
5000 old_ident = silc_command_get_ident(cmd->payload);
5001 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
5002 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
5004 silc_server_packet_send(server, dest_sock,
5005 SILC_PACKET_COMMAND, cmd->packet->flags,
5006 tmpbuf->data, tmpbuf->len, TRUE);
5008 /* Reprocess this packet after received reply from router */
5009 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
5010 silc_command_get_ident(cmd->payload),
5011 silc_server_command_getkey,
5012 silc_server_command_dup(cmd));
5013 cmd->pending = TRUE;
5014 silc_command_set_ident(cmd->payload, old_ident);
5015 silc_buffer_free(tmpbuf);
5020 silc_server_command_send_status_data(cmd, SILC_COMMAND_GETKEY,
5021 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
5022 0, 2, tmp, tmp_len);
5026 /* The client is locally connected, just get the public key and
5027 send it back. If they key does not exist then do not send it,
5028 send just OK reply */
5029 public_key = client->data.public_key;
5031 pk = silc_pkcs_public_key_payload_encode(public_key);
5032 } else if (id_type == SILC_ID_SERVER) {
5033 server_id = silc_id_payload_get_id(idp);
5035 /* If the server is not found from local list there is no chance it
5036 would be locally connected server so send the command further. */
5037 server_entry = silc_idlist_find_server_by_id(server->local_list,
5038 server_id, TRUE, NULL);
5040 server_entry = silc_idlist_find_server_by_id(server->global_list,
5041 server_id, TRUE, NULL);
5043 if (server_entry != server->id_entry &&
5044 ((!server_entry && !cmd->pending && !server->standalone) ||
5045 (server_entry && !server_entry->connection && !cmd->pending &&
5046 !server->standalone) ||
5047 (server_entry && !server_entry->data.public_key && !cmd->pending &&
5048 !server->standalone))) {
5050 SilcUInt16 old_ident;
5052 old_ident = silc_command_get_ident(cmd->payload);
5053 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
5054 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
5056 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
5057 SILC_PACKET_COMMAND, cmd->packet->flags,
5058 tmpbuf->data, tmpbuf->len, TRUE);
5060 /* Reprocess this packet after received reply from router */
5061 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
5062 silc_command_get_ident(cmd->payload),
5063 silc_server_command_getkey,
5064 silc_server_command_dup(cmd));
5065 cmd->pending = TRUE;
5066 silc_command_set_ident(cmd->payload, old_ident);
5067 silc_buffer_free(tmpbuf);
5071 if (!server_entry) {
5072 silc_server_command_send_status_data(cmd, SILC_COMMAND_GETKEY,
5073 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
5074 0, 2, tmp, tmp_len);
5078 /* If they key does not exist then do not send it, send just OK reply */
5079 public_key = (!server_entry->data.public_key ?
5080 (server_entry == server->id_entry ? server->public_key :
5081 NULL) : server_entry->data.public_key);
5083 pk = silc_pkcs_public_key_payload_encode(public_key);
5088 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5089 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
5090 SILC_STATUS_OK, 0, ident, 2,
5092 3, pk ? pk->data : NULL,
5094 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
5095 packet->data, packet->len, FALSE);
5096 silc_buffer_free(packet);
5100 silc_id_payload_free(idp);
5101 silc_buffer_free(pk);
5102 silc_free(client_id);
5103 silc_free(server_id);
5104 silc_server_command_free(cmd);
5108 /* Private range commands, specific to this implementation */
5110 /* Server side command of CONNECT. Connects us to the specified remote
5111 server or router. */
5113 SILC_SERVER_CMD_FUNC(connect)
5115 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5116 SilcServer server = cmd->server;
5117 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
5118 unsigned char *tmp, *host;
5120 SilcUInt32 port = SILC_PORT;
5122 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
5125 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CONNECT, cmd, 1, 2);
5127 /* Check whether client has the permissions. */
5128 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
5129 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
5130 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5131 SILC_STATUS_ERR_NO_SERVER_PRIV, 0);
5135 if (server->server_type == SILC_ROUTER && !server->backup_router &&
5136 client->mode & SILC_UMODE_SERVER_OPERATOR) {
5137 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5138 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
5142 /* Get the remote server */
5143 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5145 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5146 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5152 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
5154 SILC_GET32_MSB(port, tmp);
5156 /* Create the connection. It is done with timeout and is async. */
5157 silc_server_create_connection(server, host, port);
5159 /* Send reply to the sender */
5160 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5164 silc_server_command_free(cmd);
5167 /* Server side command of CLOSE. Closes connection to a specified server. */
5169 SILC_SERVER_CMD_FUNC(close)
5171 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5172 SilcServer server = cmd->server;
5173 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
5174 SilcServerEntry server_entry;
5175 SilcSocketConnection sock;
5178 unsigned char *name;
5179 SilcUInt32 port = SILC_PORT;
5181 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
5184 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CLOSE, cmd, 1, 2);
5186 /* Check whether client has the permissions. */
5187 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
5188 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
5189 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5190 SILC_STATUS_ERR_NO_SERVER_PRIV,
5195 /* Get the remote server */
5196 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5198 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5199 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5205 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
5207 SILC_GET32_MSB(port, tmp);
5209 server_entry = silc_idlist_find_server_by_conn(server->local_list,
5210 name, port, TRUE, NULL);
5212 server_entry = silc_idlist_find_server_by_conn(server->global_list,
5213 name, port, TRUE, NULL);
5214 if (!server_entry) {
5215 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5216 SILC_STATUS_ERR_NO_SERVER_ID, 0);
5220 if (server_entry == server->id_entry) {
5221 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5222 SILC_STATUS_ERR_NO_SERVER_ID, 0);
5226 /* Send reply to the sender */
5227 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5230 /* Close the connection to the server */
5231 sock = (SilcSocketConnection)server_entry->connection;
5233 if (server_entry->server_type == SILC_BACKUP_ROUTER) {
5234 server->backup_closed = TRUE;
5235 silc_server_backup_del(server, server_entry);
5238 server->backup_noswitch = TRUE;
5239 if (server->router == server_entry) {
5240 server->id_entry->router = NULL;
5241 server->router = NULL;
5242 server->standalone = TRUE;
5244 silc_server_disconnect_remote(server, sock,
5245 SILC_STATUS_ERR_BANNED_FROM_SERVER,
5246 "Closed by administrator");
5247 if (sock->user_data)
5248 silc_server_free_sock_user_data(server, sock, NULL);
5249 server->backup_noswitch = FALSE;
5252 silc_server_command_free(cmd);
5255 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
5256 active connections. */
5258 SILC_SERVER_CMD_FUNC(shutdown)
5260 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5261 SilcServer server = cmd->server;
5262 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
5264 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
5267 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_SHUTDOWN, cmd, 0, 0);
5269 /* Check whether client has the permission. */
5270 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
5271 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
5272 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
5273 SILC_STATUS_ERR_NO_SERVER_PRIV,
5278 /* Send reply to the sender */
5279 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
5282 /* Then, gracefully, or not, bring the server down. */
5283 silc_server_stop(server);
5287 silc_server_command_free(cmd);