5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "serverincludes.h"
23 #include "server_internal.h"
25 static int silc_server_is_registered(SilcServer server,
26 SilcSocketConnection sock,
27 SilcServerCommandContext cmd,
30 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
32 SilcCommandStatus status);
34 silc_server_command_send_status_data(SilcServerCommandContext cmd,
36 SilcCommandStatus status,
41 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
42 SilcServerCommandReplyContext cmdr,
44 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
46 /* Server command list. */
47 SilcServerCommand silc_command_list[] =
49 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
50 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
51 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
53 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
54 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
55 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
56 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
57 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
58 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
59 SILC_SERVER_CMD(connect, CONNECT,
60 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
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(close, CLOSE,
71 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
72 SILC_SERVER_CMD(shutdown, SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
74 SILC_SERVER_CMD(silcoper, SILCOPER,
75 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
76 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
77 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
78 SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
83 /* Performs several checks to the command. It first checks whether this
84 command was called as pending command callback. If it was then it checks
85 whether error occurred in the command reply where the pending command
88 It also checks that the requested command includes correct amount
90 #define SILC_SERVER_COMMAND_CHECK(command, context, min, max) \
94 SILC_LOG_DEBUG(("Start")); \
96 if (silc_server_command_pending_error_check(cmd, context2, command)) { \
97 silc_server_command_free(cmd); \
101 _argc = silc_argument_get_arg_num(cmd->args); \
103 silc_server_command_send_status_reply(cmd, command, \
104 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS); \
105 silc_server_command_free(cmd); \
109 silc_server_command_send_status_reply(cmd, command, \
110 SILC_STATUS_ERR_TOO_MANY_PARAMS); \
111 silc_server_command_free(cmd); \
116 /* Returns TRUE if the connection is registered. Unregistered connections
117 usually cannot send commands hence the check. */
119 static int silc_server_is_registered(SilcServer server,
120 SilcSocketConnection sock,
121 SilcServerCommandContext cmd,
124 SilcIDListData idata = (SilcIDListData)sock->user_data;
125 if (idata->registered)
128 silc_server_command_send_status_reply(cmd, command,
129 SILC_STATUS_ERR_NOT_REGISTERED);
130 silc_server_command_free(cmd);
134 /* Internal context to hold data when executed command with timeout. */
136 SilcServerCommandContext ctx;
137 SilcServerCommand *cmd;
138 } *SilcServerCommandTimeout;
140 /* Timeout callback to process commands with timeout for client. Client's
141 commands are always executed with timeout. */
143 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
145 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
146 SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
148 /* Update access time */
149 client->last_command = time(NULL);
151 if (!(timeout->cmd->flags & SILC_CF_REG))
152 timeout->cmd->cb(timeout->ctx, NULL);
153 else if (silc_server_is_registered(timeout->ctx->server,
157 timeout->cmd->cb(timeout->ctx, NULL);
162 /* Processes received command packet. */
164 void silc_server_command_process(SilcServer server,
165 SilcSocketConnection sock,
166 SilcPacketContext *packet)
168 SilcServerCommandContext ctx;
169 SilcServerCommand *cmd;
172 /* Allocate command context. This must be free'd by the
173 command routine receiving it. */
174 ctx = silc_server_command_alloc();
175 ctx->server = server;
176 ctx->sock = silc_socket_dup(sock);
177 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
179 /* Parse the command payload in the packet */
180 ctx->payload = silc_command_payload_parse(packet->buffer);
182 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
183 silc_buffer_free(packet->buffer);
184 silc_packet_context_free(packet);
185 silc_socket_free(ctx->sock);
189 ctx->args = silc_command_get_args(ctx->payload);
191 /* Get the command */
192 command = silc_command_get(ctx->payload);
193 for (cmd = silc_command_list; cmd->cb; cmd++)
194 if (cmd->cmd == command)
198 silc_server_command_send_status_reply(ctx, command,
199 SILC_STATUS_ERR_UNKNOWN_COMMAND);
200 silc_server_command_free(ctx);
204 /* Execute client's commands always with timeout. Normally they are
205 executed with zero (0) timeout but if client is sending command more
206 frequently than once in 2 seconds, then the timeout may be 0 to 2
208 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
209 SilcClientEntry client = (SilcClientEntry)sock->user_data;
210 SilcServerCommandTimeout timeout = silc_calloc(1, sizeof(*timeout));
216 if (client->last_command && (time(NULL) - client->last_command) < 2) {
217 client->fast_command++;
220 client->fast_command = ((client->fast_command - 1) <= 0 ? 0 :
221 client->fast_command--);
225 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
226 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
227 silc_schedule_task_add(server->schedule, sock->sock,
228 silc_server_command_process_timeout,
230 2 - (time(NULL) - client->last_command), 0,
232 SILC_TASK_PRI_NORMAL);
234 silc_schedule_task_add(server->schedule, sock->sock,
235 silc_server_command_process_timeout,
239 SILC_TASK_PRI_NORMAL);
243 /* Execute for server */
245 if (!(cmd->flags & SILC_CF_REG))
247 else if (silc_server_is_registered(server, sock, ctx, cmd->cmd))
251 /* Allocate Command Context */
253 SilcServerCommandContext silc_server_command_alloc()
255 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
260 /* Free's the command context allocated before executing the command */
262 void silc_server_command_free(SilcServerCommandContext ctx)
265 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
267 if (ctx->users < 1) {
269 silc_command_payload_free(ctx->payload);
271 silc_packet_context_free(ctx->packet);
273 silc_socket_free(ctx->sock); /* Decrease reference counter */
278 /* Duplicate Command Context by adding reference counter. The context won't
279 be free'd untill it hits zero. */
281 SilcServerCommandContext
282 silc_server_command_dup(SilcServerCommandContext ctx)
285 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
290 /* Add new pending command to be executed when reply to a command has been
291 received. The `reply_cmd' is the command that will call the `callback'
292 with `context' when reply has been received. If `ident' is non-zero
293 the `callback' will be executed when received reply with command
294 identifier `ident'. */
296 void silc_server_command_pending(SilcServer server,
297 SilcCommand reply_cmd,
299 SilcServerPendingDestructor destructor,
300 SilcCommandCb callback,
303 SilcServerCommandPending *reply;
305 reply = silc_calloc(1, sizeof(*reply));
306 reply->reply_cmd = reply_cmd;
307 reply->ident = ident;
308 reply->context = context;
309 reply->callback = callback;
310 reply->destructor = destructor;
311 silc_dlist_add(server->pending_commands, reply);
314 /* Deletes pending command by reply command type. */
316 void silc_server_command_pending_del(SilcServer server,
317 SilcCommand reply_cmd,
320 SilcServerCommandPending *r;
322 silc_dlist_start(server->pending_commands);
323 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
324 if (r->reply_cmd == reply_cmd && r->ident == ident) {
325 silc_dlist_del(server->pending_commands, r);
331 /* Checks for pending commands and marks callbacks to be called from
332 the command reply function. Returns TRUE if there were pending command. */
334 int silc_server_command_pending_check(SilcServer server,
335 SilcServerCommandReplyContext ctx,
339 SilcServerCommandPending *r;
341 silc_dlist_start(server->pending_commands);
342 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
343 if (r->reply_cmd == command && r->ident == ident) {
344 ctx->context = r->context;
345 ctx->callback = r->callback;
346 ctx->destructor = r->destructor;
355 /* Destructor function for pending callbacks. This is called when using
356 pending commands to free the context given for the pending command. */
358 static void silc_server_command_destructor(void *context)
360 silc_server_command_free((SilcServerCommandContext)context);
363 /* Sends simple status message as command reply packet */
366 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
368 SilcCommandStatus status)
372 SILC_LOG_DEBUG(("Sending command status %d", status));
375 silc_command_reply_payload_encode_va(command, status,
376 silc_command_get_ident(cmd->payload),
378 silc_server_packet_send(cmd->server, cmd->sock,
379 SILC_PACKET_COMMAND_REPLY, 0,
380 buffer->data, buffer->len, FALSE);
381 silc_buffer_free(buffer);
384 /* Sends command status reply with one extra argument. The argument
385 type must be sent as argument. */
388 silc_server_command_send_status_data(SilcServerCommandContext cmd,
390 SilcCommandStatus status,
397 SILC_LOG_DEBUG(("Sending command status %d", status));
400 silc_command_reply_payload_encode_va(command, status,
401 silc_command_get_ident(cmd->payload),
402 1, arg_type, arg, arg_len);
403 silc_server_packet_send(cmd->server, cmd->sock,
404 SILC_PACKET_COMMAND_REPLY, 0,
405 buffer->data, buffer->len, FALSE);
406 silc_buffer_free(buffer);
409 /* This function can be called to check whether in the command reply
410 an error occurred. This function has no effect if this is called
411 when the command function was not called as pending command callback.
412 This returns TRUE if error had occurred. */
415 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
416 SilcServerCommandReplyContext cmdr,
419 SilcCommandStatus status;
421 if (!cmd->pending || !cmdr)
424 SILC_GET16_MSB(status, silc_argument_get_arg_type(cmdr->args, 1, NULL));
425 if (status != SILC_STATUS_OK &&
426 status != SILC_STATUS_LIST_START &&
427 status != SILC_STATUS_LIST_ITEM &&
428 status != SILC_STATUS_LIST_END) {
429 /* Send the error message */
430 silc_server_command_send_status_reply(cmd, command, status);
437 /******************************************************************************
441 ******************************************************************************/
444 silc_server_command_whois_parse(SilcServerCommandContext cmd,
445 SilcClientID ***client_id,
446 uint32 *client_id_count,
454 uint32 argc = silc_argument_get_arg_num(cmd->args);
457 /* If client ID is in the command it must be used instead of nickname */
458 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
460 /* No ID, get the nickname@server string and parse it. */
461 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
463 if (strchr(tmp, '@')) {
464 len = strcspn(tmp, "@");
465 *nickname = silc_calloc(len + 1, sizeof(char));
466 memcpy(*nickname, tmp, len);
467 *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
468 memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
470 *nickname = strdup(tmp);
473 silc_server_command_send_status_reply(cmd, command,
474 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
478 /* Command includes ID, we must use that. Also check whether the command
479 has more than one ID set - take them all. */
481 *client_id = silc_calloc(1, sizeof(**client_id));
482 (*client_id)[0] = silc_id_payload_parse_id(tmp, len);
483 if ((*client_id)[0] == NULL) {
484 silc_free(*client_id);
487 *client_id_count = 1;
489 /* Take all ID's from the command packet */
491 for (k = 1, i = 1; i < argc; i++) {
492 tmp = silc_argument_get_arg_type(cmd->args, i + 3, &len);
494 *client_id = silc_realloc(*client_id, sizeof(**client_id) *
495 (*client_id_count + 1));
496 (*client_id)[k] = silc_id_payload_parse_id(tmp, len);
497 if ((*client_id)[k] == NULL) {
498 /* Cleanup all and fail */
499 for (i = 0; i < *client_id_count; i++)
500 silc_free((*client_id)[i]);
501 silc_free(*client_id);
504 (*client_id_count)++;
511 /* Get the max count of reply messages allowed */
512 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
522 silc_server_command_whois_check(SilcServerCommandContext cmd,
523 SilcClientEntry *clients,
524 uint32 clients_count)
526 SilcServer server = cmd->server;
528 SilcClientEntry entry;
530 for (i = 0; i < clients_count; i++) {
533 if (!entry || entry->data.registered == FALSE)
536 if (!entry->nickname || !entry->username || !entry->userinfo) {
543 old_ident = silc_command_get_ident(cmd->payload);
544 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
545 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
547 /* Send WHOIS command */
548 silc_server_packet_send(server, entry->router->connection,
549 SILC_PACKET_COMMAND, cmd->packet->flags,
550 tmpbuf->data, tmpbuf->len, TRUE);
552 /* Reprocess this packet after received reply */
553 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
554 silc_command_get_ident(cmd->payload),
555 silc_server_command_destructor,
556 silc_server_command_whois,
557 silc_server_command_dup(cmd));
560 silc_command_set_ident(cmd->payload, old_ident);
562 silc_buffer_free(tmpbuf);
571 silc_server_command_whois_send_reply(SilcServerCommandContext cmd,
572 SilcClientEntry *clients,
573 uint32 clients_count,
576 SilcServer server = cmd->server;
579 SilcBuffer packet, idp, channels;
580 SilcClientEntry entry;
581 SilcCommandStatus status;
582 uint16 ident = silc_command_get_ident(cmd->payload);
583 char nh[256], uh[256];
584 unsigned char idle[4], mode[4];
585 SilcSocketConnection hsock;
588 for (i = 0; i < clients_count; i++)
589 if (clients[i]->data.registered)
592 status = SILC_STATUS_OK;
594 status = SILC_STATUS_LIST_START;
596 for (i = 0, k = 0; i < clients_count; i++) {
599 if (entry->data.registered == FALSE) {
600 if (clients_count == 1) {
601 if (entry->nickname) {
602 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
603 SILC_STATUS_ERR_NO_SUCH_NICK,
605 strlen(entry->nickname));
607 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
608 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
609 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
610 2, idp->data, idp->len);
611 silc_buffer_free(idp);
618 status = SILC_STATUS_LIST_ITEM;
620 if (clients_count > 1 && k == clients_count - 1)
621 status = SILC_STATUS_LIST_END;
623 if (count && k - 1 == count)
624 status = SILC_STATUS_LIST_END;
626 if (count && k - 1 > count)
629 /* Sanity check, however these should never fail. However, as
630 this sanity check has been added here they have failed. */
631 if (!entry->nickname || !entry->username || !entry->userinfo)
634 /* Send WHOIS reply */
635 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
636 tmp = silc_argument_get_first_arg(cmd->args, NULL);
638 memset(uh, 0, sizeof(uh));
639 memset(nh, 0, sizeof(nh));
640 memset(idle, 0, sizeof(idle));
642 strncat(nh, entry->nickname, strlen(entry->nickname));
643 if (!strchr(entry->nickname, '@')) {
645 if (entry->servername) {
646 strncat(nh, entry->servername, strlen(entry->servername));
648 len = entry->router ? strlen(entry->router->server_name) :
649 strlen(server->server_name);
650 strncat(nh, entry->router ? entry->router->server_name :
651 server->server_name, len);
655 strncat(uh, entry->username, strlen(entry->username));
656 if (!strchr(entry->username, '@')) {
658 hsock = (SilcSocketConnection)entry->connection;
659 len = strlen(hsock->hostname);
660 strncat(uh, hsock->hostname, len);
663 channels = silc_server_get_client_channel_list(server, entry);
665 SILC_PUT32_MSB(entry->mode, mode);
667 if (entry->connection) {
668 SILC_PUT32_MSB((time(NULL) - entry->data.last_receive), idle);
672 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
674 2, idp->data, idp->len,
678 strlen(entry->userinfo),
684 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
686 2, idp->data, idp->len,
690 strlen(entry->userinfo),
694 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
695 0, packet->data, packet->len, FALSE);
697 silc_buffer_free(packet);
698 silc_buffer_free(idp);
700 silc_buffer_free(channels);
707 silc_server_command_whois_process(SilcServerCommandContext cmd)
709 SilcServer server = cmd->server;
710 char *nick = NULL, *server_name = NULL;
712 SilcClientEntry *clients = NULL, entry;
713 SilcClientID **client_id = NULL;
714 uint32 client_id_count = 0, clients_count = 0;
716 bool check_global = FALSE;
718 /* Protocol dictates that we must always send the received WHOIS request
719 to our router if we are normal server, so let's do it now unless we
720 are standalone. We will not send any replies to the client until we
721 have received reply from the router. */
722 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
723 server->server_type == SILC_SERVER && !cmd->pending &&
724 !server->standalone) {
728 old_ident = silc_command_get_ident(cmd->payload);
729 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
730 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
732 /* Send WHOIS command to our router */
733 silc_server_packet_send(server, (SilcSocketConnection)
734 server->router->connection,
735 SILC_PACKET_COMMAND, cmd->packet->flags,
736 tmpbuf->data, tmpbuf->len, TRUE);
738 /* Reprocess this packet after received reply from router */
739 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
740 silc_command_get_ident(cmd->payload),
741 silc_server_command_destructor,
742 silc_server_command_whois,
743 silc_server_command_dup(cmd));
746 silc_command_set_ident(cmd->payload, old_ident);
748 silc_buffer_free(tmpbuf);
753 /* We are ready to process the command request. Let's search for the
754 requested client and send reply to the requesting client. */
756 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
758 else if (server->server_type == SILC_ROUTER)
761 /* Parse the whois request */
762 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
763 &nick, &server_name, &count,
767 /* Get all clients matching that ID or nickname from local list */
768 if (client_id_count) {
769 /* Check all Client ID's received in the command packet */
770 for (i = 0; i < client_id_count; i++) {
771 entry = silc_idlist_find_client_by_id(server->local_list,
772 client_id[i], TRUE, NULL);
773 if (!entry && check_global)
774 entry = silc_idlist_find_client_by_id(server->global_list,
775 client_id[i], TRUE, NULL);
777 clients = silc_realloc(clients, sizeof(*clients) *
778 (clients_count + 1));
779 clients[clients_count++] = entry;
783 if (!silc_idlist_get_clients_by_hash(server->local_list,
784 nick, server->md5hash,
785 &clients, &clients_count))
786 silc_idlist_get_clients_by_nickname(server->local_list,
788 &clients, &clients_count);
790 if (!silc_idlist_get_clients_by_hash(server->global_list,
791 nick, server->md5hash,
792 &clients, &clients_count))
793 silc_idlist_get_clients_by_nickname(server->global_list,
795 &clients, &clients_count);
800 /* Such client(s) really does not exist in the SILC network. */
801 if (!client_id_count) {
802 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
803 SILC_STATUS_ERR_NO_SUCH_NICK,
804 3, nick, strlen(nick));
806 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
807 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
808 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
809 2, idp->data, idp->len);
810 silc_buffer_free(idp);
815 /* Router always finds the client entry if it exists in the SILC network.
816 However, it might be incomplete entry and does not include all the
817 mandatory fields that WHOIS command reply requires. Check for these and
818 make query from the server who owns the client if some fields are
820 if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
825 /* Send the command reply */
826 silc_server_command_whois_send_reply(cmd, clients, clients_count,
830 if (client_id_count) {
831 for (i = 0; i < client_id_count; i++)
832 silc_free(client_id[i]);
833 silc_free(client_id);
840 silc_free(server_name);
845 /* Server side of command WHOIS. Processes user's query and sends found
846 results as command replies back to the client. */
848 SILC_SERVER_CMD_FUNC(whois)
850 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
853 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 3328);
855 ret = silc_server_command_whois_process(cmd);
858 silc_server_command_free(cmd);
861 /******************************************************************************
865 ******************************************************************************/
868 silc_server_command_whowas_parse(SilcServerCommandContext cmd,
876 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
878 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WHOWAS,
879 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
883 /* Get the nickname@server string and parse it. */
884 if (strchr(tmp, '@')) {
885 len = strcspn(tmp, "@");
886 *nickname = silc_calloc(len + 1, sizeof(char));
887 memcpy(*nickname, tmp, len);
888 *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
889 memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
891 *nickname = strdup(tmp);
893 /* Get the max count of reply messages allowed */
894 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
904 silc_server_command_whowas_check(SilcServerCommandContext cmd,
905 SilcClientEntry *clients,
906 uint32 clients_count)
908 SilcServer server = cmd->server;
910 SilcClientEntry entry;
912 for (i = 0; i < clients_count; i++) {
915 if (!entry->nickname || !entry->username) {
922 old_ident = silc_command_get_ident(cmd->payload);
923 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
924 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
926 /* Send WHOWAS command */
927 silc_server_packet_send(server, entry->router->connection,
928 SILC_PACKET_COMMAND, cmd->packet->flags,
929 tmpbuf->data, tmpbuf->len, TRUE);
931 /* Reprocess this packet after received reply */
932 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
933 silc_command_get_ident(cmd->payload),
934 silc_server_command_destructor,
935 silc_server_command_whowas,
936 silc_server_command_dup(cmd));
939 silc_command_set_ident(cmd->payload, old_ident);
941 silc_buffer_free(tmpbuf);
950 silc_server_command_whowas_send_reply(SilcServerCommandContext cmd,
951 SilcClientEntry *clients,
952 uint32 clients_count)
954 SilcServer server = cmd->server;
956 int i, count = 0, len;
957 SilcBuffer packet, idp;
958 SilcClientEntry entry = NULL;
959 SilcCommandStatus status;
960 uint16 ident = silc_command_get_ident(cmd->payload);
962 char nh[256], uh[256];
964 status = SILC_STATUS_OK;
965 if (clients_count > 1)
966 status = SILC_STATUS_LIST_START;
968 for (i = 0; i < clients_count; i++) {
971 /* We will take only clients that are not valid anymore. They are the
972 ones that are not registered anymore but still have a ID. They
973 have disconnected us, and thus valid for WHOWAS. */
974 if (entry->data.registered == TRUE)
976 if (entry->id == NULL)
979 if (count && i - 1 == count)
984 if (clients_count > 2)
985 status = SILC_STATUS_LIST_ITEM;
987 if (clients_count > 1 && i == clients_count - 1)
988 status = SILC_STATUS_LIST_END;
990 /* Sanity check, however these should never fail. However, as
991 this sanity check has been added here they have failed. */
992 if (!entry->nickname || !entry->username)
995 /* Send WHOWAS reply */
996 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
997 tmp = silc_argument_get_first_arg(cmd->args, NULL);
999 memset(uh, 0, sizeof(uh));
1000 memset(nh, 0, sizeof(nh));
1002 strncat(nh, entry->nickname, strlen(entry->nickname));
1003 if (!strchr(entry->nickname, '@')) {
1004 strncat(nh, "@", 1);
1005 if (entry->servername) {
1006 strncat(nh, entry->servername, strlen(entry->servername));
1008 len = entry->router ? strlen(entry->router->server_name) :
1009 strlen(server->server_name);
1010 strncat(nh, entry->router ? entry->router->server_name :
1011 server->server_name, len);
1015 strncat(uh, entry->username, strlen(entry->username));
1016 if (!strchr(entry->username, '@')) {
1017 strncat(uh, "@", 1);
1018 strcat(uh, "*private*");
1021 if (entry->userinfo)
1023 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1025 2, idp->data, idp->len,
1029 strlen(entry->userinfo));
1032 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1034 2, idp->data, idp->len,
1038 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1039 0, packet->data, packet->len, FALSE);
1041 silc_buffer_free(packet);
1042 silc_buffer_free(idp);
1045 if (found == FALSE && entry)
1046 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1047 SILC_STATUS_ERR_NO_SUCH_NICK,
1049 strlen(entry->nickname));
1053 silc_server_command_whowas_process(SilcServerCommandContext cmd)
1055 SilcServer server = cmd->server;
1056 char *nick = NULL, *server_name = NULL;
1058 SilcClientEntry *clients = NULL;
1059 uint32 clients_count = 0;
1061 bool check_global = FALSE;
1063 /* Protocol dictates that we must always send the received WHOWAS request
1064 to our router if we are normal server, so let's do it now unless we
1065 are standalone. We will not send any replies to the client until we
1066 have received reply from the router. */
1067 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1068 server->server_type == SILC_SERVER && !cmd->pending &&
1069 !server->standalone) {
1073 old_ident = silc_command_get_ident(cmd->payload);
1074 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1075 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1077 /* Send WHOWAS command to our router */
1078 silc_server_packet_send(server, (SilcSocketConnection)
1079 server->router->connection,
1080 SILC_PACKET_COMMAND, cmd->packet->flags,
1081 tmpbuf->data, tmpbuf->len, TRUE);
1083 /* Reprocess this packet after received reply from router */
1084 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
1085 silc_command_get_ident(cmd->payload),
1086 silc_server_command_destructor,
1087 silc_server_command_whowas,
1088 silc_server_command_dup(cmd));
1089 cmd->pending = TRUE;
1091 silc_command_set_ident(cmd->payload, old_ident);
1093 silc_buffer_free(tmpbuf);
1098 /* We are ready to process the command request. Let's search for the
1099 requested client and send reply to the requesting client. */
1101 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1102 check_global = TRUE;
1103 else if (server->server_type == SILC_ROUTER)
1104 check_global = TRUE;
1106 /* Parse the whowas request */
1107 if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1110 /* Get all clients matching that nickname from local list */
1111 if (!silc_idlist_get_clients_by_nickname(server->local_list,
1113 &clients, &clients_count))
1114 silc_idlist_get_clients_by_hash(server->local_list,
1115 nick, server->md5hash,
1116 &clients, &clients_count);
1118 /* Check global list as well */
1120 if (!silc_idlist_get_clients_by_nickname(server->global_list,
1122 &clients, &clients_count))
1123 silc_idlist_get_clients_by_hash(server->global_list,
1124 nick, server->md5hash,
1125 &clients, &clients_count);
1129 /* Such a client really does not exist in the SILC network. */
1130 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1131 SILC_STATUS_ERR_NO_SUCH_NICK,
1132 3, nick, strlen(nick));
1136 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1137 !silc_server_command_whowas_check(cmd, clients, clients_count)) {
1142 /* Send the command reply to the client */
1143 silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1151 silc_free(server_name);
1156 /* Server side of command WHOWAS. */
1158 SILC_SERVER_CMD_FUNC(whowas)
1160 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1163 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
1165 ret = silc_server_command_whowas_process(cmd);
1168 silc_server_command_free(cmd);
1171 /******************************************************************************
1175 ******************************************************************************/
1178 silc_server_command_identify_parse(SilcServerCommandContext cmd,
1179 SilcClientEntry **clients,
1180 uint32 *clients_count,
1181 SilcServerEntry **servers,
1182 uint32 *servers_count,
1183 SilcChannelEntry **channels,
1184 uint32 *channels_count,
1187 SilcServer server = cmd->server;
1190 uint32 argc = silc_argument_get_arg_num(cmd->args);
1192 bool check_global = FALSE;
1197 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1198 check_global = TRUE;
1199 else if (server->server_type == SILC_ROUTER)
1200 check_global = TRUE;
1202 /* If ID Payload is in the command it must be used instead of names */
1203 tmp = silc_argument_get_arg_type(cmd->args, 5, &len);
1205 /* No ID, get the names. */
1207 /* Try to get nickname@server. */
1208 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
1211 char *nick_server = NULL;
1213 if (strchr(tmp, '@')) {
1214 len = strcspn(tmp, "@");
1215 nick = silc_calloc(len + 1, sizeof(char));
1216 memcpy(nick, tmp, len);
1217 nick_server = silc_calloc(strlen(tmp) - len, sizeof(char));
1218 memcpy(nick_server, tmp + len + 1, strlen(tmp) - len - 1);
1223 if (!silc_idlist_get_clients_by_hash(server->local_list,
1224 nick, server->md5hash,
1225 clients, clients_count))
1226 silc_idlist_get_clients_by_nickname(server->local_list,
1228 clients, clients_count);
1230 if (!silc_idlist_get_clients_by_hash(server->global_list,
1231 nick, server->md5hash,
1232 clients, clients_count))
1233 silc_idlist_get_clients_by_nickname(server->global_list,
1235 clients, clients_count);
1239 silc_free(nick_server);
1242 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1243 SILC_STATUS_ERR_NO_SUCH_NICK,
1244 3, tmp, strlen(tmp));
1249 /* Try to get server name */
1250 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
1252 entry = silc_idlist_find_server_by_name(server->local_list,
1254 if (!entry && check_global)
1255 entry = silc_idlist_find_server_by_name(server->local_list,
1258 *servers = silc_realloc(*servers, sizeof(**servers) *
1259 (*servers_count + 1));
1260 (*servers)[(*servers_count)++] = entry;
1264 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1265 SILC_STATUS_ERR_NO_SUCH_SERVER,
1266 3, tmp, strlen(tmp));
1271 /* Try to get channel name */
1272 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
1274 entry = silc_idlist_find_channel_by_name(server->local_list,
1276 if (!entry && check_global)
1277 entry = silc_idlist_find_channel_by_name(server->local_list,
1280 *channels = silc_realloc(*channels, sizeof(**channels) *
1281 (*channels_count + 1));
1282 (*channels)[(*channels_count)++] = entry;
1286 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1287 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
1288 3, tmp, strlen(tmp));
1293 if (!(*clients) && !(*servers) && !(*channels)) {
1294 silc_server_command_send_status_reply(cmd, SILC_COMMAND_IDENTIFY,
1295 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1299 /* Command includes ID, we must use that. Also check whether the command
1300 has more than one ID set - take them all. */
1302 /* Take all ID's from the command packet */
1303 for (i = 0; i < argc; i++) {
1306 tmp = silc_argument_get_arg_type(cmd->args, i + 5, &len);
1310 idp = silc_id_payload_parse_data(tmp, len);
1312 silc_free(*clients);
1313 silc_free(*servers);
1314 silc_free(*channels);
1315 silc_server_command_send_status_reply(cmd, SILC_COMMAND_IDENTIFY,
1316 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1320 id = silc_id_payload_get_id(idp);
1322 switch (silc_id_payload_get_type(idp)) {
1324 case SILC_ID_CLIENT:
1325 entry = (void *)silc_idlist_find_client_by_id(server->local_list,
1327 if (!entry && check_global)
1328 entry = (void *)silc_idlist_find_client_by_id(server->global_list,
1331 *clients = silc_realloc(*clients, sizeof(**clients) *
1332 (*clients_count + 1));
1333 (*clients)[(*clients_count)++] = (SilcClientEntry)entry;
1335 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1336 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1343 case SILC_ID_SERVER:
1344 entry = (void *)silc_idlist_find_server_by_id(server->local_list,
1346 if (!entry && check_global)
1347 entry = (void *)silc_idlist_find_server_by_id(server->global_list,
1350 *servers = silc_realloc(*servers, sizeof(**servers) *
1351 (*servers_count + 1));
1352 (*servers)[(*servers_count)++] = (SilcServerEntry)entry;
1354 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1355 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
1361 case SILC_ID_CHANNEL:
1362 entry = (void *)silc_idlist_find_channel_by_id(server->local_list,
1364 if (!entry && check_global)
1365 entry = (void *)silc_idlist_find_channel_by_id(server->global_list,
1368 *channels = silc_realloc(*channels, sizeof(**channels) *
1369 (*channels_count + 1));
1370 (*channels)[(*channels_count)++] = (SilcChannelEntry)entry;
1372 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1373 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
1385 silc_free(*clients);
1386 silc_free(*servers);
1387 silc_free(*channels);
1391 /* Get the max count of reply messages allowed */
1392 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
1401 /* Checks that all mandatory fields in client entry are present. If not
1402 then send WHOIS request to the server who owns the client. We use
1403 WHOIS because we want to get as much information as possible at once. */
1406 silc_server_command_identify_check_client(SilcServerCommandContext cmd,
1407 SilcClientEntry *clients,
1408 uint32 clients_count)
1410 SilcServer server = cmd->server;
1412 SilcClientEntry entry;
1414 for (i = 0; i < clients_count; i++) {
1417 if (!entry || entry->data.registered == FALSE)
1420 if (!entry->nickname) {
1427 old_ident = silc_command_get_ident(cmd->payload);
1428 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1429 silc_command_set_command(cmd->payload, SILC_COMMAND_WHOIS);
1430 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1432 /* Send WHOIS request. We send WHOIS since we're doing the requesting
1433 now anyway so make it a good one. */
1434 silc_server_packet_send(server, entry->router->connection,
1435 SILC_PACKET_COMMAND, cmd->packet->flags,
1436 tmpbuf->data, tmpbuf->len, TRUE);
1438 /* Reprocess this packet after received reply */
1439 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1440 silc_command_get_ident(cmd->payload),
1441 silc_server_command_destructor,
1442 silc_server_command_identify,
1443 silc_server_command_dup(cmd));
1445 cmd->pending = TRUE;
1447 /* Put old data back to the Command Payload we just changed */
1448 silc_command_set_ident(cmd->payload, old_ident);
1449 silc_command_set_command(cmd->payload, SILC_COMMAND_IDENTIFY);
1451 silc_buffer_free(tmpbuf);
1460 silc_server_command_identify_send_reply(SilcServerCommandContext cmd,
1461 SilcClientEntry *clients,
1462 uint32 clients_count,
1463 SilcServerEntry *servers,
1464 uint32 servers_count,
1465 SilcChannelEntry *channels,
1466 uint32 channels_count,
1469 SilcServer server = cmd->server;
1471 SilcBuffer packet, idp;
1472 SilcCommandStatus status;
1473 uint16 ident = silc_command_get_ident(cmd->payload);
1474 char nh[256], uh[256];
1475 SilcSocketConnection hsock;
1477 status = SILC_STATUS_OK;
1480 SilcClientEntry entry;
1483 for (i = 0; i < clients_count; i++)
1484 if (clients[i]->data.registered)
1488 status = SILC_STATUS_LIST_START;
1490 for (i = 0, k = 0; i < clients_count; i++) {
1493 if (entry->data.registered == FALSE) {
1494 if (clients_count == 1) {
1495 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1496 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1497 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1498 2, idp->data, idp->len);
1499 silc_buffer_free(idp);
1505 status = SILC_STATUS_LIST_ITEM;
1506 if (clients_count > 1 && k == clients_count - 1
1507 && !servers_count && !channels_count)
1508 status = SILC_STATUS_LIST_END;
1509 if (count && k - 1 == count)
1510 status = SILC_STATUS_LIST_END;
1511 if (count && k - 1 > count)
1514 /* Send IDENTIFY reply */
1515 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1517 memset(uh, 0, sizeof(uh));
1518 memset(nh, 0, sizeof(nh));
1520 strncat(nh, entry->nickname, strlen(entry->nickname));
1521 if (!strchr(entry->nickname, '@')) {
1522 strncat(nh, "@", 1);
1523 if (entry->servername) {
1524 strncat(nh, entry->servername, strlen(entry->servername));
1526 len = entry->router ? strlen(entry->router->server_name) :
1527 strlen(server->server_name);
1528 strncat(nh, entry->router ? entry->router->server_name :
1529 server->server_name, len);
1533 if (!entry->username) {
1534 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1536 2, idp->data, idp->len,
1539 strncat(uh, entry->username, strlen(entry->username));
1540 if (!strchr(entry->username, '@')) {
1541 strncat(uh, "@", 1);
1542 hsock = (SilcSocketConnection)entry->connection;
1543 len = strlen(hsock->hostname);
1544 strncat(uh, hsock->hostname, len);
1547 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1549 2, idp->data, idp->len,
1554 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1555 0, packet->data, packet->len, FALSE);
1557 silc_buffer_free(packet);
1558 silc_buffer_free(idp);
1564 status = (status == SILC_STATUS_LIST_ITEM ?
1565 SILC_STATUS_LIST_ITEM : SILC_STATUS_OK);
1568 SilcServerEntry entry;
1570 if (status == SILC_STATUS_OK && servers_count > 1)
1571 status = SILC_STATUS_LIST_START;
1573 for (i = 0, k = 0; i < servers_count; i++) {
1577 status = SILC_STATUS_LIST_ITEM;
1578 if (servers_count > 1 && k == servers_count - 1 && !channels_count)
1579 status = SILC_STATUS_LIST_END;
1580 if (count && k - 1 == count)
1581 status = SILC_STATUS_LIST_END;
1582 if (count && k - 1 > count)
1585 /* Send IDENTIFY reply */
1586 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1587 if (entry->server_name) {
1589 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1591 2, idp->data, idp->len,
1592 3, entry->server_name,
1593 strlen(entry->server_name));
1595 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1597 2, idp->data, idp->len);
1600 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1601 0, packet->data, packet->len, FALSE);
1603 silc_buffer_free(packet);
1604 silc_buffer_free(idp);
1610 status = (status == SILC_STATUS_LIST_ITEM ?
1611 SILC_STATUS_LIST_ITEM : SILC_STATUS_OK);
1614 SilcChannelEntry entry;
1616 if (status == SILC_STATUS_OK && channels_count > 1)
1617 status = SILC_STATUS_LIST_START;
1619 for (i = 0, k = 0; i < channels_count; i++) {
1620 entry = channels[i];
1623 status = SILC_STATUS_LIST_ITEM;
1624 if (channels_count > 1 && k == channels_count - 1)
1625 status = SILC_STATUS_LIST_END;
1626 if (count && k - 1 == count)
1627 status = SILC_STATUS_LIST_END;
1628 if (count && k - 1 > count)
1631 /* Send IDENTIFY reply */
1632 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1633 if (entry->channel_name) {
1635 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1637 2, idp->data, idp->len,
1638 3, entry->channel_name,
1639 strlen(entry->channel_name));
1641 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1643 2, idp->data, idp->len);
1646 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1647 0, packet->data, packet->len, FALSE);
1649 silc_buffer_free(packet);
1650 silc_buffer_free(idp);
1658 silc_server_command_identify_process(SilcServerCommandContext cmd)
1660 SilcServer server = cmd->server;
1663 SilcClientEntry *clients = NULL;
1664 SilcServerEntry *servers = NULL;
1665 SilcChannelEntry *channels = NULL;
1666 uint32 clients_count = 0, servers_count = 0, channels_count = 0;
1668 /* Protocol dictates that we must always send the received IDENTIFY request
1669 to our router if we are normal server, so let's do it now unless we
1670 are standalone. We will not send any replies to the client until we
1671 have received reply from the router. */
1672 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1673 server->server_type == SILC_SERVER && !cmd->pending &&
1674 !server->standalone) {
1678 old_ident = silc_command_get_ident(cmd->payload);
1679 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1680 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1682 /* Send IDENTIFY command to our router */
1683 silc_server_packet_send(server, (SilcSocketConnection)
1684 server->router->connection,
1685 SILC_PACKET_COMMAND, cmd->packet->flags,
1686 tmpbuf->data, tmpbuf->len, TRUE);
1688 /* Reprocess this packet after received reply from router */
1689 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
1690 silc_command_get_ident(cmd->payload),
1691 silc_server_command_destructor,
1692 silc_server_command_identify,
1693 silc_server_command_dup(cmd));
1694 cmd->pending = TRUE;
1696 silc_command_set_ident(cmd->payload, old_ident);
1698 silc_buffer_free(tmpbuf);
1703 /* We are ready to process the command request. Let's search for the
1704 requested client and send reply to the requesting client. */
1706 /* Parse the IDENTIFY request */
1707 if (!silc_server_command_identify_parse(cmd,
1708 &clients, &clients_count,
1709 &servers, &servers_count,
1710 &channels, &channels_count,
1714 /* Check that all mandatory fields are present and request those data
1715 from the server who owns the client if necessary. */
1716 if (clients && !silc_server_command_identify_check_client(cmd, clients,
1722 /* Send the command reply to the client */
1723 silc_server_command_identify_send_reply(cmd,
1724 clients, clients_count,
1725 servers, servers_count,
1726 channels, channels_count,
1732 silc_free(channels);
1737 SILC_SERVER_CMD_FUNC(identify)
1739 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1742 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 3328);
1744 ret = silc_server_command_identify_process(cmd);
1747 silc_server_command_free(cmd);
1750 /* Checks string for bad characters and returns TRUE if they are found. */
1752 static int silc_server_command_bad_chars(char *nick)
1754 if (strchr(nick, '\\')) return TRUE;
1755 if (strchr(nick, '\"')) return TRUE;
1756 if (strchr(nick, '´')) return TRUE;
1757 if (strchr(nick, '`')) return TRUE;
1758 if (strchr(nick, '\'')) return TRUE;
1759 if (strchr(nick, '*')) return TRUE;
1760 if (strchr(nick, '/')) return TRUE;
1761 if (strchr(nick, '@')) return TRUE;
1766 /* Server side of command NICK. Sets nickname for user. Setting
1767 nickname causes generation of a new client ID for the client. The
1768 new client ID is sent to the client after changing the nickname. */
1770 SILC_SERVER_CMD_FUNC(nick)
1772 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1773 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1774 SilcServer server = cmd->server;
1775 SilcBuffer packet, nidp, oidp;
1776 SilcClientID *new_id;
1778 uint16 ident = silc_command_get_ident(cmd->payload);
1780 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1783 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
1785 /* Check nickname */
1786 nick = silc_argument_get_arg_type(cmd->args, 1, NULL);
1787 if (silc_server_command_bad_chars(nick) == TRUE) {
1788 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
1789 SILC_STATUS_ERR_BAD_NICKNAME);
1793 if (strlen(nick) > 128)
1796 /* Create new Client ID */
1797 silc_id_create_client_id(cmd->server->id, cmd->server->rng,
1798 cmd->server->md5hash, nick,
1801 /* Send notify about nickname change to our router. We send the new
1802 ID and ask to replace it with the old one. If we are router the
1803 packet is broadcasted. Send NICK_CHANGE notify. */
1804 if (!server->standalone)
1805 silc_server_send_notify_nick_change(server, server->router->connection,
1806 server->server_type == SILC_SERVER ?
1807 FALSE : TRUE, client->id,
1810 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1812 /* Remove old cache entry */
1813 silc_idcache_del_by_context(server->local_list->clients, client);
1816 silc_free(client->id);
1818 /* Save the nickname as this client is our local client */
1819 silc_free(client->nickname);
1821 client->nickname = strdup(nick);
1822 client->id = new_id;
1824 /* Update client cache */
1825 silc_idcache_add(server->local_list->clients, client->nickname,
1826 client->id, (void *)client, FALSE);
1828 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1830 /* Send NICK_CHANGE notify to the client's channels */
1831 silc_server_send_notify_on_channels(server, NULL, client,
1832 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
1833 oidp->data, oidp->len,
1834 nidp->data, nidp->len);
1836 /* Send the new Client ID as reply command back to client */
1837 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
1838 SILC_STATUS_OK, ident, 1,
1839 2, nidp->data, nidp->len);
1840 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1841 0, packet->data, packet->len, FALSE);
1843 silc_buffer_free(packet);
1844 silc_buffer_free(nidp);
1845 silc_buffer_free(oidp);
1848 silc_server_command_free(cmd);
1851 /* Sends the LIST command reply */
1854 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
1855 SilcChannelEntry *lch,
1857 SilcChannelEntry *gch,
1861 SilcBuffer packet, idp;
1862 SilcChannelEntry entry;
1863 SilcCommandStatus status;
1864 uint16 ident = silc_command_get_ident(cmd->payload);
1866 unsigned char usercount[4];
1869 for (i = 0; i < lch_count; i++)
1870 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1872 for (i = 0; i < gch_count; i++)
1873 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1876 status = SILC_STATUS_OK;
1877 if ((lch_count + gch_count) > 1)
1878 status = SILC_STATUS_LIST_START;
1881 for (i = 0; i < lch_count; i++) {
1888 status = SILC_STATUS_LIST_ITEM;
1890 if (i == lch_count - 1 && gch_count)
1892 if (lch_count > 1 && i == lch_count - 1)
1893 status = SILC_STATUS_LIST_END;
1895 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1897 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1898 topic = "*private*";
1899 memset(usercount, 0, sizeof(usercount));
1901 topic = entry->topic;
1902 users = silc_hash_table_count(entry->user_list);
1903 SILC_PUT32_MSB(users, usercount);
1906 /* Send the reply */
1909 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1911 2, idp->data, idp->len,
1912 3, entry->channel_name,
1913 strlen(entry->channel_name),
1914 4, topic, strlen(topic),
1918 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1920 2, idp->data, idp->len,
1921 3, entry->channel_name,
1922 strlen(entry->channel_name),
1924 silc_server_packet_send(cmd->server, cmd->sock,
1925 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1926 packet->len, FALSE);
1927 silc_buffer_free(packet);
1928 silc_buffer_free(idp);
1931 status = i ? SILC_STATUS_LIST_ITEM : SILC_STATUS_OK;
1934 for (i = 0; i < gch_count; i++) {
1941 status = SILC_STATUS_LIST_ITEM;
1943 if (gch_count > 1 && i == lch_count - 1)
1944 status = SILC_STATUS_LIST_END;
1946 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1948 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1949 topic = "*private*";
1950 memset(usercount, 0, sizeof(usercount));
1952 topic = entry->topic;
1953 users = silc_hash_table_count(entry->user_list);
1954 SILC_PUT32_MSB(users, usercount);
1957 /* Send the reply */
1960 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1962 2, idp->data, idp->len,
1963 3, entry->channel_name,
1964 strlen(entry->channel_name),
1965 4, topic, strlen(topic),
1969 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1971 2, idp->data, idp->len,
1972 3, entry->channel_name,
1973 strlen(entry->channel_name),
1975 silc_server_packet_send(cmd->server, cmd->sock,
1976 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1977 packet->len, FALSE);
1978 silc_buffer_free(packet);
1979 silc_buffer_free(idp);
1983 /* Server side of LIST command. This lists the channel of the requested
1984 server. Secret channels are not listed. */
1986 SILC_SERVER_CMD_FUNC(list)
1988 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1989 SilcServer server = cmd->server;
1990 SilcChannelID *channel_id = NULL;
1993 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
1994 uint32 lch_count = 0, gch_count = 0;
1996 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 2);
1998 /* Get Channel ID */
1999 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2001 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2003 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
2004 SILC_STATUS_ERR_NO_CHANNEL_ID);
2009 /* Get the channels from local list */
2010 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
2013 /* Get the channels from global list if we are router */
2014 if (server->server_type == SILC_ROUTER)
2015 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
2018 /* Send the reply */
2019 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
2020 gchannels, gch_count);
2023 silc_server_command_free(cmd);
2026 /* Server side of TOPIC command. Sets topic for channel and/or returns
2027 current topic to client. */
2029 SILC_SERVER_CMD_FUNC(topic)
2031 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2032 SilcServer server = cmd->server;
2033 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2034 SilcChannelID *channel_id;
2035 SilcChannelEntry channel;
2036 SilcChannelClientEntry chl;
2037 SilcBuffer packet, idp;
2039 uint32 argc, tmp_len;
2040 uint16 ident = silc_command_get_ident(cmd->payload);
2042 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
2044 argc = silc_argument_get_arg_num(cmd->args);
2046 /* Get Channel ID */
2047 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2049 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2050 SILC_STATUS_ERR_NO_CHANNEL_ID);
2053 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2055 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2056 SILC_STATUS_ERR_NO_CHANNEL_ID);
2060 /* Check whether the channel exists */
2061 channel = silc_idlist_find_channel_by_id(server->local_list,
2064 channel = silc_idlist_find_channel_by_id(server->global_list,
2067 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2068 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2075 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
2077 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2078 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2082 if (strlen(tmp) > 256) {
2083 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2084 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2088 /* See whether the client is on channel and has rights to change topic */
2089 if (!silc_hash_table_find(channel->user_list, client, NULL,
2091 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2092 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2096 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2097 if (channel->mode & SILC_CHANNEL_MODE_TOPIC) {
2098 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2099 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2104 /* Set the topic for channel */
2106 silc_free(channel->topic);
2107 channel->topic = strdup(tmp);
2109 /* Send TOPIC_SET notify type to the network */
2110 if (!server->standalone)
2111 silc_server_send_notify_topic_set(server, server->router->connection,
2112 server->server_type == SILC_ROUTER ?
2113 TRUE : FALSE, channel, client->id,
2116 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2118 /* Send notify about topic change to all clients on the channel */
2119 silc_server_send_notify_to_channel(server, NULL, channel, TRUE,
2120 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
2121 idp->data, idp->len,
2122 channel->topic, strlen(channel->topic));
2123 silc_buffer_free(idp);
2126 /* Send the topic to client as reply packet */
2127 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2129 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2130 SILC_STATUS_OK, ident, 2,
2131 2, idp->data, idp->len,
2133 strlen(channel->topic));
2135 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2136 SILC_STATUS_OK, ident, 1,
2137 2, idp->data, idp->len);
2138 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2139 0, packet->data, packet->len, FALSE);
2141 silc_buffer_free(packet);
2142 silc_buffer_free(idp);
2143 silc_free(channel_id);
2146 silc_server_command_free(cmd);
2149 /* Server side of INVITE command. Invites some client to join some channel.
2150 This command is also used to manage the invite list of the channel. */
2152 SILC_SERVER_CMD_FUNC(invite)
2154 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2155 SilcServer server = cmd->server;
2156 SilcSocketConnection sock = cmd->sock, dest_sock;
2157 SilcChannelClientEntry chl;
2158 SilcClientEntry sender, dest;
2159 SilcClientID *dest_id = NULL;
2160 SilcChannelEntry channel;
2161 SilcChannelID *channel_id = NULL;
2162 SilcIDListData idata;
2163 SilcBuffer idp, idp2, packet;
2164 unsigned char *tmp, *add, *del;
2166 uint16 ident = silc_command_get_ident(cmd->payload);
2168 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
2170 /* Get Channel ID */
2171 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2173 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2174 SILC_STATUS_ERR_NO_CHANNEL_ID);
2177 channel_id = silc_id_payload_parse_id(tmp, len);
2179 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2180 SILC_STATUS_ERR_NO_CHANNEL_ID);
2184 /* Get the channel entry */
2185 channel = silc_idlist_find_channel_by_id(server->local_list,
2188 channel = silc_idlist_find_channel_by_id(server->global_list,
2191 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2192 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2197 /* Check whether the sender of this command is on the channel. */
2198 sender = (SilcClientEntry)sock->user_data;
2199 if (!silc_server_client_on_channel(sender, channel)) {
2200 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2201 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2205 /* Check whether the channel is invite-only channel. If yes then the
2206 sender of this command must be at least channel operator. */
2207 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2208 silc_hash_table_find(channel->user_list, sender, NULL, (void *)&chl);
2209 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2210 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2211 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2216 /* Get destination client ID */
2217 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
2221 dest_id = silc_id_payload_parse_id(tmp, len);
2223 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2224 SILC_STATUS_ERR_NO_CLIENT_ID);
2228 /* Get the client entry */
2229 dest = silc_server_get_client_resolve(server, dest_id);
2231 if (server->server_type == SILC_ROUTER) {
2232 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2233 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2237 /* The client info is being resolved. Reprocess this packet after
2238 receiving the reply to the query. */
2239 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2241 silc_server_command_destructor,
2242 silc_server_command_invite,
2243 silc_server_command_dup(cmd));
2244 cmd->pending = TRUE;
2245 silc_free(channel_id);
2250 /* Check whether the requested client is already on the channel. */
2251 if (silc_server_client_on_channel(dest, channel)) {
2252 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2253 SILC_STATUS_ERR_USER_ON_CHANNEL);
2257 /* Get route to the client */
2258 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id, &idata);
2260 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2261 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2265 memset(invite, 0, sizeof(invite));
2266 strncat(invite, dest->nickname, strlen(dest->nickname));
2267 strncat(invite, "!", 1);
2268 strncat(invite, dest->username, strlen(dest->username));
2269 if (!strchr(dest->username, '@')) {
2270 strncat(invite, "@", 1);
2271 strncat(invite, cmd->sock->hostname, strlen(cmd->sock->hostname));
2274 len = strlen(invite);
2275 if (!channel->invite_list)
2276 channel->invite_list = silc_calloc(len + 2,
2277 sizeof(*channel->invite_list));
2279 channel->invite_list = silc_realloc(channel->invite_list,
2280 sizeof(*channel->invite_list) *
2282 strlen(channel->invite_list) + 2));
2283 strncat(channel->invite_list, invite, len);
2284 strncat(channel->invite_list, ",", 1);
2286 /* Send notify to the client that is invited to the channel */
2287 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2288 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
2289 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
2291 SILC_NOTIFY_TYPE_INVITE, 3,
2292 idp->data, idp->len,
2293 channel->channel_name,
2294 strlen(channel->channel_name),
2295 idp2->data, idp2->len);
2296 silc_buffer_free(idp);
2297 silc_buffer_free(idp2);
2300 /* Add the client to the invite list of the channel */
2301 add = silc_argument_get_arg_type(cmd->args, 3, &len);
2303 if (!channel->invite_list)
2304 channel->invite_list = silc_calloc(len + 2,
2305 sizeof(*channel->invite_list));
2307 channel->invite_list = silc_realloc(channel->invite_list,
2308 sizeof(*channel->invite_list) *
2310 strlen(channel->invite_list) + 2));
2311 if (add[len - 1] == ',')
2312 add[len - 1] = '\0';
2314 strncat(channel->invite_list, add, len);
2315 strncat(channel->invite_list, ",", 1);
2318 /* Get the invite to be removed and remove it from the list */
2319 del = silc_argument_get_arg_type(cmd->args, 4, &len);
2320 if (del && channel->invite_list) {
2321 char *start, *end, *n;
2323 if (!strncmp(channel->invite_list, del,
2324 strlen(channel->invite_list) - 1)) {
2325 silc_free(channel->invite_list);
2326 channel->invite_list = NULL;
2328 start = strstr(channel->invite_list, del);
2329 if (start && strlen(start) >= len) {
2331 n = silc_calloc(strlen(channel->invite_list) - len, sizeof(*n));
2332 strncat(n, channel->invite_list, start - channel->invite_list);
2333 strncat(n, end + 1, ((channel->invite_list +
2334 strlen(channel->invite_list)) - end) - 1);
2335 silc_free(channel->invite_list);
2336 channel->invite_list = n;
2341 /* Send notify to the primary router */
2342 if (!server->standalone)
2343 silc_server_send_notify_invite(server, server->router->connection,
2344 server->server_type == SILC_ROUTER ?
2345 TRUE : FALSE, channel,
2346 sender->id, add, del);
2348 /* Send command reply */
2349 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2353 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2354 SILC_STATUS_OK, ident, 2,
2356 3, channel->invite_list,
2357 channel->invite_list ?
2358 strlen(channel->invite_list) : 0);
2361 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2362 SILC_STATUS_OK, ident, 1,
2364 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2365 packet->data, packet->len, FALSE);
2366 silc_buffer_free(packet);
2372 silc_free(channel_id);
2373 silc_server_command_free(cmd);
2378 SilcSocketConnection sock;
2382 /* Quits connection to client. This gets called if client won't
2383 close the connection even when it has issued QUIT command. */
2385 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
2387 QuitInternal q = (QuitInternal)context;
2389 /* Free all client specific data, such as client entry and entires
2390 on channels this client may be on. */
2391 silc_server_free_client_data(q->server, q->sock, q->sock->user_data,
2393 q->sock->user_data = NULL;
2395 /* Close the connection on our side */
2396 silc_server_close_connection(q->server, q->sock);
2398 silc_free(q->signoff);
2402 /* Quits SILC session. This is the normal way to disconnect client. */
2404 SILC_SERVER_CMD_FUNC(quit)
2406 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2407 SilcServer server = cmd->server;
2408 SilcSocketConnection sock = cmd->sock;
2410 unsigned char *tmp = NULL;
2413 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
2415 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2418 /* Get destination ID */
2419 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2423 q = silc_calloc(1, sizeof(*q));
2426 q->signoff = tmp ? strdup(tmp) : NULL;
2428 /* We quit the connection with little timeout */
2429 silc_schedule_task_add(server->schedule, sock->sock,
2430 silc_server_command_quit_cb, (void *)q,
2431 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2434 silc_server_command_free(cmd);
2437 /* Server side of command KILL. This command is used by router operator
2438 to remove an client from the SILC Network temporarily. */
2440 SILC_SERVER_CMD_FUNC(kill)
2442 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2443 SilcServer server = cmd->server;
2444 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2445 SilcClientEntry remote_client;
2446 SilcClientID *client_id;
2447 unsigned char *tmp, *comment;
2448 uint32 tmp_len, tmp_len2;
2450 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 2);
2452 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2455 /* KILL command works only on router */
2456 if (server->server_type != SILC_ROUTER) {
2457 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2458 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2462 /* Check whether client has the permissions. */
2463 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2464 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2465 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2469 /* Get the client ID */
2470 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2472 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2473 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2476 client_id = silc_id_payload_parse_id(tmp, tmp_len);
2478 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2479 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2483 /* Get the client entry */
2484 remote_client = silc_idlist_find_client_by_id(server->local_list,
2485 client_id, TRUE, NULL);
2486 if (!remote_client) {
2487 remote_client = silc_idlist_find_client_by_id(server->global_list,
2488 client_id, TRUE, NULL);
2489 if (!remote_client) {
2490 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2491 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2497 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
2501 /* Send reply to the sender */
2502 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2505 /* Send the KILL notify packets. First send it to the channel, then
2506 to our primary router and then directly to the client who is being
2507 killed right now. */
2509 /* Send KILLED notify to the channels. It is not sent to the client
2510 as it will be sent differently destined directly to the client and not
2512 silc_server_send_notify_on_channels(server, remote_client,
2513 remote_client, SILC_NOTIFY_TYPE_KILLED,
2516 comment, comment ? tmp_len2 : 0);
2518 /* Send KILLED notify to primary route */
2519 if (!server->standalone)
2520 silc_server_send_notify_killed(server, server->router->connection, TRUE,
2521 remote_client->id, comment);
2523 /* Send KILLED notify to the client directly */
2524 silc_server_send_notify_killed(server, remote_client->connection ?
2525 remote_client->connection :
2526 remote_client->router->connection, FALSE,
2527 remote_client->id, comment);
2529 /* Remove the client from all channels. This generates new keys to the
2530 channels as well. */
2531 silc_server_remove_from_channels(server, NULL, remote_client, FALSE,
2534 /* Remove the client entry, If it is locally connected then we will also
2535 disconnect the client here */
2536 if (remote_client->data.registered && remote_client->connection) {
2537 /* Remove locally conneted client */
2538 SilcSocketConnection sock = remote_client->connection;
2539 silc_server_free_client_data(server, sock, remote_client, FALSE, NULL);
2540 silc_server_close_connection(server, sock);
2542 /* Remove remote client */
2543 if (!silc_idlist_del_client(server->global_list, remote_client))
2544 silc_idlist_del_client(server->local_list, remote_client);
2548 silc_server_command_free(cmd);
2551 /* Server side of command INFO. This sends information about us to
2552 the client. If client requested specific server we will send the
2553 command to that server. */
2555 SILC_SERVER_CMD_FUNC(info)
2557 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2558 SilcServer server = cmd->server;
2559 SilcBuffer packet, idp;
2562 char *dest_server, *server_info = NULL, *server_name;
2563 uint16 ident = silc_command_get_ident(cmd->payload);
2564 SilcServerEntry entry = NULL;
2565 SilcServerID *server_id = NULL;
2567 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
2569 /* Get server name */
2570 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2573 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2575 server_id = silc_id_payload_parse_id(tmp, tmp_len);
2577 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2578 SILC_STATUS_ERR_NO_SERVER_ID);
2584 /* Check whether we have this server cached */
2585 entry = silc_idlist_find_server_by_id(server->local_list,
2586 server_id, TRUE, NULL);
2588 entry = silc_idlist_find_server_by_id(server->global_list,
2589 server_id, TRUE, NULL);
2590 if (!entry && server->server_type == SILC_ROUTER) {
2591 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2592 SILC_STATUS_ERR_NO_SUCH_SERVER);
2598 if ((!dest_server && !server_id && !entry) || (entry &&
2599 entry == server->id_entry) ||
2600 (dest_server && !cmd->pending &&
2601 !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
2602 /* Send our reply */
2603 char info_string[256];
2605 memset(info_string, 0, sizeof(info_string));
2606 snprintf(info_string, sizeof(info_string),
2607 "location: %s server: %s admin: %s <%s>",
2608 server->config->admin_info->location,
2609 server->config->admin_info->server_type,
2610 server->config->admin_info->admin_name,
2611 server->config->admin_info->admin_email);
2613 server_info = info_string;
2614 entry = server->id_entry;
2616 /* Check whether we have this server cached */
2617 if (!entry && dest_server) {
2618 entry = silc_idlist_find_server_by_name(server->global_list,
2619 dest_server, TRUE, NULL);
2621 entry = silc_idlist_find_server_by_name(server->local_list,
2622 dest_server, TRUE, NULL);
2626 if (!cmd->pending &&
2627 server->server_type == SILC_ROUTER && entry && !entry->server_info) {
2628 /* Send to the server */
2632 old_ident = silc_command_get_ident(cmd->payload);
2633 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2634 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2636 silc_server_packet_send(server, entry->connection,
2637 SILC_PACKET_COMMAND, cmd->packet->flags,
2638 tmpbuf->data, tmpbuf->len, TRUE);
2640 /* Reprocess this packet after received reply from router */
2641 silc_server_command_pending(server, SILC_COMMAND_INFO,
2642 silc_command_get_ident(cmd->payload),
2643 silc_server_command_destructor,
2644 silc_server_command_info,
2645 silc_server_command_dup(cmd));
2646 cmd->pending = TRUE;
2647 silc_command_set_ident(cmd->payload, old_ident);
2648 silc_buffer_free(tmpbuf);
2652 if (!entry && !cmd->pending && !server->standalone) {
2653 /* Send to the primary router */
2657 old_ident = silc_command_get_ident(cmd->payload);
2658 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2659 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2661 silc_server_packet_send(server, server->router->connection,
2662 SILC_PACKET_COMMAND, cmd->packet->flags,
2663 tmpbuf->data, tmpbuf->len, TRUE);
2665 /* Reprocess this packet after received reply from router */
2666 silc_server_command_pending(server, SILC_COMMAND_INFO,
2667 silc_command_get_ident(cmd->payload),
2668 silc_server_command_destructor,
2669 silc_server_command_info,
2670 silc_server_command_dup(cmd));
2671 cmd->pending = TRUE;
2672 silc_command_set_ident(cmd->payload, old_ident);
2673 silc_buffer_free(tmpbuf);
2679 silc_free(server_id);
2682 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2683 SILC_STATUS_ERR_NO_SUCH_SERVER);
2687 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2689 server_info = entry->server_info;
2690 server_name = entry->server_name;
2692 /* Send the reply */
2694 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2695 SILC_STATUS_OK, ident, 3,
2696 2, idp->data, idp->len,
2698 strlen(server_name),
2700 strlen(server_info));
2702 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2703 SILC_STATUS_OK, ident, 2,
2704 2, idp->data, idp->len,
2706 strlen(server_name));
2707 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2708 packet->data, packet->len, FALSE);
2710 silc_buffer_free(packet);
2711 silc_buffer_free(idp);
2714 silc_server_command_free(cmd);
2717 /* Server side of command PING. This just replies to the ping. */
2719 SILC_SERVER_CMD_FUNC(ping)
2721 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2722 SilcServer server = cmd->server;
2727 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 1, 2);
2730 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2732 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2733 SILC_STATUS_ERR_NO_SERVER_ID);
2736 id = silc_id_str2id(tmp, len, SILC_ID_SERVER);
2740 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2741 /* Send our reply */
2742 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2745 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2746 SILC_STATUS_ERR_NO_SUCH_SERVER);
2753 silc_server_command_free(cmd);
2756 /* Internal routine to join channel. The channel sent to this function
2757 has been either created or resolved from ID lists. This joins the sent
2758 client to the channel. */
2760 static void silc_server_command_join_channel(SilcServer server,
2761 SilcServerCommandContext cmd,
2762 SilcChannelEntry channel,
2763 SilcClientID *client_id,
2767 SilcSocketConnection sock = cmd->sock;
2769 uint32 tmp_len, user_count;
2770 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
2771 SilcClientEntry client;
2772 SilcChannelClientEntry chl;
2773 SilcBuffer reply, chidp, clidp, keyp = NULL, user_list, mode_list;
2774 uint16 ident = silc_command_get_ident(cmd->payload);
2775 char check[512], check2[512];
2777 SILC_LOG_DEBUG(("Start"));
2782 /* Get the client entry */
2783 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2784 client = (SilcClientEntry)sock->user_data;
2786 client = silc_server_get_client_resolve(server, client_id);
2791 /* The client info is being resolved. Reprocess this packet after
2792 receiving the reply to the query. */
2793 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2794 server->cmd_ident, NULL,
2795 silc_server_command_join,
2796 silc_server_command_dup(cmd));
2797 cmd->pending = TRUE;
2801 cmd->pending = FALSE;
2805 * Check channel modes
2808 memset(check, 0, sizeof(check));
2809 memset(check2, 0, sizeof(check2));
2810 strncat(check, client->nickname, strlen(client->nickname));
2811 strncat(check, "!", 1);
2812 strncat(check, client->username, strlen(client->username));
2813 if (!strchr(client->username, '@')) {
2814 strncat(check, "@", 1);
2815 strncat(check, cmd->sock->hostname, strlen(cmd->sock->hostname));
2818 strncat(check2, client->nickname, strlen(client->nickname));
2819 if (!strchr(client->nickname, '@')) {
2820 strncat(check2, "@", 1);
2821 strncat(check2, server->server_name, strlen(server->server_name));
2823 strncat(check2, "!", 1);
2824 strncat(check2, client->username, strlen(client->username));
2825 if (!strchr(client->username, '@')) {
2826 strncat(check2, "@", 1);
2827 strncat(check2, cmd->sock->hostname, strlen(cmd->sock->hostname));
2830 /* Check invite list if channel is invite-only channel */
2831 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2832 if (!channel->invite_list ||
2833 (!silc_string_match(channel->invite_list, check) &&
2834 !silc_string_match(channel->invite_list, check2))) {
2835 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2836 SILC_STATUS_ERR_NOT_INVITED);
2841 /* Check ban list if it exists. If the client's nickname, server,
2842 username and/or hostname is in the ban list the access to the
2843 channel is denied. */
2844 if (channel->ban_list) {
2845 if (silc_string_match(channel->ban_list, check) ||
2846 silc_string_match(channel->ban_list, check2)) {
2847 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2848 SILC_STATUS_ERR_BANNED_FROM_CHANNEL);
2853 /* Get passphrase */
2854 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
2856 passphrase = silc_calloc(tmp_len, sizeof(*passphrase));
2857 memcpy(passphrase, tmp, tmp_len);
2860 /* Check the channel passphrase if set. */
2861 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2862 if (!passphrase || memcmp(channel->passphrase, passphrase,
2863 strlen(channel->passphrase))) {
2864 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2865 SILC_STATUS_ERR_BAD_PASSWORD);
2870 /* Check user count limit if set. */
2871 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
2872 if (silc_hash_table_count(channel->user_list) + 1 >
2873 channel->user_limit) {
2874 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2875 SILC_STATUS_ERR_CHANNEL_IS_FULL);
2881 * Client is allowed to join to the channel. Make it happen.
2884 /* Check whether the client already is on the channel */
2885 if (silc_server_client_on_channel(client, channel)) {
2886 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2887 SILC_STATUS_ERR_USER_ON_CHANNEL);
2891 /* Generate new channel key as protocol dictates */
2892 if ((!created && silc_hash_table_count(channel->user_list) > 0) ||
2893 !channel->channel_key)
2894 if (!silc_server_create_channel_key(server, channel, 0))
2897 /* Send the channel key. This is broadcasted to the channel but is not
2898 sent to the client who is joining to the channel. */
2899 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
2900 silc_server_send_channel_key(server, NULL, channel,
2901 server->server_type == SILC_ROUTER ?
2902 FALSE : !server->standalone);
2904 /* Join the client to the channel by adding it to channel's user list.
2905 Add also the channel to client entry's channels list for fast cross-
2907 chl = silc_calloc(1, sizeof(*chl));
2909 chl->client = client;
2910 chl->channel = channel;
2911 silc_hash_table_add(channel->user_list, client, chl);
2912 silc_hash_table_add(client->channels, channel, chl);
2914 /* Get users on the channel */
2915 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2918 /* Encode Client ID Payload of the original client who wants to join */
2919 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2921 /* Encode command reply packet */
2922 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2923 SILC_PUT32_MSB(channel->mode, mode);
2924 SILC_PUT32_MSB(created, tmp2);
2925 SILC_PUT32_MSB(user_count, tmp3);
2927 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2928 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2929 keyp = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, tmp,
2930 strlen(channel->channel_key->
2932 channel->channel_key->cipher->name,
2933 channel->key_len / 8, channel->key);
2938 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2939 SILC_STATUS_OK, ident, 13,
2940 2, channel->channel_name,
2941 strlen(channel->channel_name),
2942 3, chidp->data, chidp->len,
2943 4, clidp->data, clidp->len,
2946 7, keyp ? keyp->data : NULL,
2947 keyp ? keyp->len : 0,
2948 8, channel->ban_list,
2950 strlen(channel->ban_list) : 0,
2951 9, channel->invite_list,
2952 channel->invite_list ?
2953 strlen(channel->invite_list) : 0,
2956 strlen(channel->topic) : 0,
2957 11, channel->hmac->hmac->name,
2958 strlen(channel->hmac->hmac->name),
2960 13, user_list->data, user_list->len,
2961 14, mode_list->data,
2964 /* Send command reply */
2965 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2966 reply->data, reply->len, FALSE);
2968 if (!cmd->pending) {
2969 /* Send JOIN notify to locally connected clients on the channel */
2970 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2971 SILC_NOTIFY_TYPE_JOIN, 2,
2972 clidp->data, clidp->len,
2973 chidp->data, chidp->len);
2975 /* Send JOIN notify packet to our primary router */
2976 if (!server->standalone)
2977 silc_server_send_notify_join(server, server->router->connection,
2978 server->server_type == SILC_ROUTER ?
2979 TRUE : FALSE, channel, client->id);
2982 silc_buffer_free(reply);
2983 silc_buffer_free(clidp);
2984 silc_buffer_free(chidp);
2985 silc_buffer_free(keyp);
2986 silc_buffer_free(user_list);
2987 silc_buffer_free(mode_list);
2991 silc_free(passphrase);
2994 /* Server side of command JOIN. Joins client into requested channel. If
2995 the channel does not exist it will be created. */
2997 SILC_SERVER_CMD_FUNC(join)
2999 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3000 SilcServer server = cmd->server;
3002 char *tmp, *channel_name = NULL, *cipher, *hmac;
3003 SilcChannelEntry channel;
3005 int created = FALSE;
3006 SilcClientID *client_id;
3008 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 1, 4);
3010 /* Get channel name */
3011 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3013 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3014 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3019 if (strlen(channel_name) > 256)
3020 channel_name[255] = '\0';
3022 if (silc_server_command_bad_chars(channel_name) == TRUE) {
3023 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3024 SILC_STATUS_ERR_BAD_CHANNEL);
3028 /* Get Client ID of the client who is joining to the channel */
3029 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3031 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3032 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3035 client_id = silc_id_payload_parse_id(tmp, tmp_len);
3037 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3038 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3042 /* Get cipher and hmac name */
3043 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
3044 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
3046 /* See if the channel exists */
3047 channel = silc_idlist_find_channel_by_name(server->local_list,
3048 channel_name, NULL);
3050 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3051 /* If this is coming from client the Client ID in the command packet must
3052 be same as the client's ID. */
3053 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3054 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
3055 if (!SILC_ID_CLIENT_COMPARE(entry->id, client_id)) {
3056 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3057 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3062 if (!channel || !channel->id) {
3063 /* Channel not found */
3065 /* If we are standalone server we don't have a router, we just create
3066 the channel by ourselves. */
3067 if (server->standalone) {
3068 channel = silc_server_create_new_channel(server, server->id, cipher,
3069 hmac, channel_name, TRUE);
3071 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3072 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3076 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3081 /* The channel does not exist on our server. If we are normal server
3082 we will send JOIN command to our router which will handle the
3083 joining procedure (either creates the channel if it doesn't exist
3084 or joins the client to it). */
3085 if (server->server_type == SILC_SERVER) {
3089 /* If this is pending command callback then we've resolved
3090 it and it didn't work, return since we've notified the
3091 client already in the command reply callback. */
3095 old_ident = silc_command_get_ident(cmd->payload);
3096 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3097 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3099 /* Send JOIN command to our router */
3100 silc_server_packet_send(server, (SilcSocketConnection)
3101 server->router->connection,
3102 SILC_PACKET_COMMAND, cmd->packet->flags,
3103 tmpbuf->data, tmpbuf->len, TRUE);
3105 /* Reprocess this packet after received reply from router */
3106 silc_server_command_pending(server, SILC_COMMAND_JOIN,
3107 silc_command_get_ident(cmd->payload),
3108 silc_server_command_destructor,
3109 silc_server_command_join,
3110 silc_server_command_dup(cmd));
3111 cmd->pending = TRUE;
3115 /* We are router and the channel does not seem exist so we will check
3116 our global list as well for the channel. */
3117 channel = silc_idlist_find_channel_by_name(server->global_list,
3118 channel_name, NULL);
3120 /* Channel really does not exist, create it */
3121 channel = silc_server_create_new_channel(server, server->id, cipher,
3122 hmac, channel_name, TRUE);
3124 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3125 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3129 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3136 /* Channel not found */
3138 /* If the command came from router and/or we are normal server then
3139 something went wrong with the joining as the channel was not found.
3140 We can't do anything else but ignore this. */
3141 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
3142 server->server_type == SILC_SERVER)
3145 /* We are router and the channel does not seem exist so we will check
3146 our global list as well for the channel. */
3147 channel = silc_idlist_find_channel_by_name(server->global_list,
3148 channel_name, NULL);
3150 /* Channel really does not exist, create it */
3151 channel = silc_server_create_new_channel(server, server->id, cipher,
3152 hmac, channel_name, TRUE);
3154 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3155 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3159 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3165 /* If the channel does not have global users and is also empty it means the
3166 channel was created globally (by our router) and the client will be the
3167 channel founder and operator. */
3168 if (!channel->global_users && !silc_hash_table_count(channel->user_list)) {
3169 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3170 created = TRUE; /* Created globally by our router */
3173 /* Join to the channel */
3174 silc_server_command_join_channel(server, cmd, channel, client_id,
3177 silc_free(client_id);
3180 silc_server_command_free(cmd);
3183 /* Server side of command MOTD. Sends server's current "message of the
3184 day" to the client. */
3186 SILC_SERVER_CMD_FUNC(motd)
3188 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3189 SilcServer server = cmd->server;
3190 SilcBuffer packet, idp;
3191 char *motd, *dest_server;
3193 uint16 ident = silc_command_get_ident(cmd->payload);
3195 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
3197 /* Get server name */
3198 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
3200 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
3201 SILC_STATUS_ERR_NO_SUCH_SERVER);
3205 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
3208 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3210 if (server->config && server->config->motd &&
3211 server->config->motd->motd_file) {
3213 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
3218 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3219 SILC_STATUS_OK, ident, 2,
3225 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3226 SILC_STATUS_OK, ident, 1,
3230 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3231 packet->data, packet->len, FALSE);
3232 silc_buffer_free(packet);
3233 silc_buffer_free(idp);
3235 SilcServerEntry entry;
3237 /* Check whether we have this server cached */
3238 entry = silc_idlist_find_server_by_name(server->global_list,
3239 dest_server, TRUE, NULL);
3241 entry = silc_idlist_find_server_by_name(server->local_list,
3242 dest_server, TRUE, NULL);
3245 if (server->server_type == SILC_ROUTER && !cmd->pending &&
3246 entry && !entry->motd) {
3247 /* Send to the server */
3251 old_ident = silc_command_get_ident(cmd->payload);
3252 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3253 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3255 silc_server_packet_send(server, entry->connection,
3256 SILC_PACKET_COMMAND, cmd->packet->flags,
3257 tmpbuf->data, tmpbuf->len, TRUE);
3259 /* Reprocess this packet after received reply from router */
3260 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3261 silc_command_get_ident(cmd->payload),
3262 silc_server_command_destructor,
3263 silc_server_command_motd,
3264 silc_server_command_dup(cmd));
3265 cmd->pending = TRUE;
3266 silc_command_set_ident(cmd->payload, old_ident);
3267 silc_buffer_free(tmpbuf);
3271 if (!entry && !cmd->pending && !server->standalone) {
3272 /* Send to the primary router */
3276 old_ident = silc_command_get_ident(cmd->payload);
3277 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3278 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3280 silc_server_packet_send(server, server->router->connection,
3281 SILC_PACKET_COMMAND, cmd->packet->flags,
3282 tmpbuf->data, tmpbuf->len, TRUE);
3284 /* Reprocess this packet after received reply from router */
3285 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3286 silc_command_get_ident(cmd->payload),
3287 silc_server_command_destructor,
3288 silc_server_command_motd,
3289 silc_server_command_dup(cmd));
3290 cmd->pending = TRUE;
3291 silc_command_set_ident(cmd->payload, old_ident);
3292 silc_buffer_free(tmpbuf);
3297 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3298 SILC_STATUS_ERR_NO_SUCH_SERVER);
3302 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3305 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3306 SILC_STATUS_OK, ident, 2,
3309 strlen(entry->motd));
3311 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3312 SILC_STATUS_OK, ident, 1,
3315 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3316 packet->data, packet->len, FALSE);
3317 silc_buffer_free(packet);
3318 silc_buffer_free(idp);
3322 silc_server_command_free(cmd);
3325 /* Server side of command UMODE. Client can use this command to set/unset
3326 user mode. Client actually cannot set itself to be as server/router
3327 operator so this can be used only to unset the modes. */
3329 SILC_SERVER_CMD_FUNC(umode)
3331 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3332 SilcServer server = cmd->server;
3333 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3335 unsigned char *tmp_mask;
3337 uint16 ident = silc_command_get_ident(cmd->payload);
3339 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3342 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 2, 2);
3344 /* Get the client's mode mask */
3345 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3347 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3348 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3351 SILC_GET32_MSB(mask, tmp_mask);
3357 if (mask & SILC_UMODE_SERVER_OPERATOR) {
3358 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR)) {
3359 /* Cannot operator mode */
3360 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3361 SILC_STATUS_ERR_PERM_DENIED);
3365 if (client->mode & SILC_UMODE_SERVER_OPERATOR)
3366 /* Remove the server operator rights */
3367 client->mode &= ~SILC_UMODE_SERVER_OPERATOR;
3370 if (mask & SILC_UMODE_ROUTER_OPERATOR) {
3371 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
3372 /* Cannot operator mode */
3373 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3374 SILC_STATUS_ERR_PERM_DENIED);
3378 if (client->mode & SILC_UMODE_ROUTER_OPERATOR)
3379 /* Remove the router operator rights */
3380 client->mode &= ~SILC_UMODE_ROUTER_OPERATOR;
3383 if (mask & SILC_UMODE_GONE) {
3384 client->mode |= SILC_UMODE_GONE;
3386 if (client->mode & SILC_UMODE_GONE)
3387 /* Remove the gone status */
3388 client->mode &= ~SILC_UMODE_GONE;
3391 /* Send UMODE change to primary router */
3392 if (!server->standalone)
3393 silc_server_send_notify_umode(server, server->router->connection, TRUE,
3394 client->id, client->mode);
3396 /* Send command reply to sender */
3397 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
3398 SILC_STATUS_OK, ident, 1,
3400 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3401 packet->data, packet->len, FALSE);
3402 silc_buffer_free(packet);
3405 silc_server_command_free(cmd);
3408 /* Checks that client has rights to add or remove channel modes. If any
3409 of the checks fails FALSE is returned. */
3411 int silc_server_check_cmode_rights(SilcChannelEntry channel,
3412 SilcChannelClientEntry client,
3415 int is_op = client->mode & SILC_CHANNEL_UMODE_CHANOP;
3416 int is_fo = client->mode & SILC_CHANNEL_UMODE_CHANFO;
3418 /* Check whether has rights to change anything */
3419 if (!is_op && !is_fo)
3422 /* Check whether has rights to change everything */
3426 /* We know that client is channel operator, check that they are not
3427 changing anything that requires channel founder rights. Rest of the
3428 modes are available automatically for channel operator. */
3430 if (mode & SILC_CHANNEL_MODE_PRIVKEY) {
3431 if (is_op && !is_fo)
3434 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3435 if (is_op && !is_fo)
3440 if (mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3441 if (is_op && !is_fo)
3444 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3445 if (is_op && !is_fo)
3450 if (mode & SILC_CHANNEL_MODE_CIPHER) {
3451 if (is_op && !is_fo)
3454 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3455 if (is_op && !is_fo)
3460 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3461 if (is_op && !is_fo)
3464 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3465 if (is_op && !is_fo)
3473 /* Server side command of CMODE. Changes channel mode */
3475 SILC_SERVER_CMD_FUNC(cmode)
3477 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3478 SilcServer server = cmd->server;
3479 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3480 SilcIDListData idata = (SilcIDListData)client;
3481 SilcChannelID *channel_id;
3482 SilcChannelEntry channel;
3483 SilcChannelClientEntry chl;
3484 SilcBuffer packet, cidp;
3485 unsigned char *tmp, *tmp_id, *tmp_mask;
3486 char *cipher = NULL, *hmac = NULL;
3487 uint32 mode_mask, tmp_len, tmp_len2;
3488 uint16 ident = silc_command_get_ident(cmd->payload);
3490 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 2, 7);
3492 /* Get Channel ID */
3493 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
3495 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3496 SILC_STATUS_ERR_NO_CHANNEL_ID);
3499 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2);
3501 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3502 SILC_STATUS_ERR_NO_CHANNEL_ID);
3506 /* Get the channel mode mask */
3507 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3509 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3510 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3513 SILC_GET32_MSB(mode_mask, tmp_mask);
3515 /* Get channel entry */
3516 channel = silc_idlist_find_channel_by_id(server->local_list,
3519 channel = silc_idlist_find_channel_by_id(server->global_list,
3522 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3523 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3528 /* Check whether this client is on the channel */
3529 if (!silc_server_client_on_channel(client, channel)) {
3530 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3531 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3535 /* Get entry to the channel user list */
3536 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
3538 /* Check that client has rights to change any requested channel modes */
3539 if (!silc_server_check_cmode_rights(channel, chl, mode_mask)) {
3540 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3541 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3546 * Check the modes. Modes that requires nothing special operation are
3550 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
3551 /* Channel uses private keys to protect traffic. Client(s) has set the
3552 key locally they want to use, server does not know that key. */
3553 /* Nothing interesting to do here */
3555 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3556 /* The mode is removed and we need to generate and distribute
3557 new channel key. Clients are not using private channel keys
3558 anymore after this. */
3560 /* Re-generate channel key */
3561 if (!silc_server_create_channel_key(server, channel, 0))
3564 /* Send the channel key. This sends it to our local clients and if
3565 we are normal server to our router as well. */
3566 silc_server_send_channel_key(server, NULL, channel,
3567 server->server_type == SILC_ROUTER ?
3568 FALSE : !server->standalone);
3570 cipher = channel->channel_key->cipher->name;
3571 hmac = channel->hmac->hmac->name;
3575 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
3576 /* User limit is set on channel */
3579 /* Get user limit */
3580 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
3582 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
3583 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3584 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3588 SILC_GET32_MSB(user_limit, tmp);
3589 channel->user_limit = user_limit;
3592 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
3593 /* User limit mode is unset. Remove user limit */
3594 channel->user_limit = 0;
3597 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
3598 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
3599 /* Passphrase has been set to channel */
3601 /* Get the passphrase */
3602 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
3604 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3605 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3609 /* Save the passphrase */
3610 channel->passphrase = strdup(tmp);
3613 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3614 /* Passphrase mode is unset. remove the passphrase */
3615 if (channel->passphrase) {
3616 silc_free(channel->passphrase);
3617 channel->passphrase = NULL;
3622 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
3623 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
3624 /* Cipher to use protect the traffic */
3627 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
3629 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3630 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3634 /* Delete old cipher and allocate the new one */
3635 silc_cipher_free(channel->channel_key);
3636 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3637 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3638 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3642 /* Re-generate channel key */
3643 if (!silc_server_create_channel_key(server, channel, 0))
3646 /* Send the channel key. This sends it to our local clients and if
3647 we are normal server to our router as well. */
3648 silc_server_send_channel_key(server, NULL, channel,
3649 server->server_type == SILC_ROUTER ?
3650 FALSE : !server->standalone);
3653 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3654 /* Cipher mode is unset. Remove the cipher and revert back to
3656 cipher = channel->cipher;
3658 /* Delete old cipher and allocate default one */
3659 silc_cipher_free(channel->channel_key);
3660 if (!silc_cipher_alloc(cipher ? cipher : "aes-256-cbc",
3661 &channel->channel_key)) {
3662 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3663 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3667 /* Re-generate channel key */
3668 if (!silc_server_create_channel_key(server, channel, 0))
3671 /* Send the channel key. This sends it to our local clients and if
3672 we are normal server to our router as well. */
3673 silc_server_send_channel_key(server, NULL, channel,
3674 server->server_type == SILC_ROUTER ?
3675 FALSE : !server->standalone);
3679 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
3680 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
3681 /* HMAC to use protect the traffic */
3682 unsigned char hash[32];
3685 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
3687 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3688 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3692 /* Delete old hmac and allocate the new one */
3693 silc_hmac_free(channel->hmac);
3694 if (!silc_hmac_alloc(hmac, NULL, &channel->hmac)) {
3695 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3696 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3700 /* Set the HMAC key out of current channel key. The client must do
3702 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3704 silc_hmac_set_key(channel->hmac, hash,
3705 silc_hash_len(channel->hmac->hash));
3706 memset(hash, 0, sizeof(hash));
3709 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
3710 /* Hmac mode is unset. Remove the hmac and revert back to
3712 unsigned char hash[32];
3713 hmac = channel->hmac_name;
3715 /* Delete old hmac and allocate default one */
3716 silc_hmac_free(channel->hmac);
3717 if (!silc_hmac_alloc(hmac ? hmac : "hmac-sha1-96", NULL,
3719 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3720 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3724 /* Set the HMAC key out of current channel key. The client must do
3726 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3728 silc_hmac_set_key(channel->hmac, hash,
3729 silc_hash_len(channel->hmac->hash));
3730 memset(hash, 0, sizeof(hash));
3734 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3735 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3736 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
3737 /* Set the founder authentication */
3738 SilcAuthPayload auth;
3740 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
3742 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3743 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3747 auth = silc_auth_payload_parse(tmp, tmp_len);
3749 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3750 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3754 /* Save the public key */
3755 tmp = silc_pkcs_public_key_encode(idata->public_key, &tmp_len);
3756 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
3759 channel->founder_method = silc_auth_get_method(auth);
3761 if (channel->founder_method == SILC_AUTH_PASSWORD) {
3762 tmp = silc_auth_get_data(auth, &tmp_len);
3763 channel->founder_passwd =
3764 silc_calloc(tmp_len + 1, sizeof(*channel->founder_passwd));
3765 memcpy(channel->founder_passwd, tmp, tmp_len);
3766 channel->founder_passwd_len = tmp_len;
3768 /* Verify the payload before setting the mode */
3769 if (!silc_auth_verify(auth, channel->founder_method,
3770 channel->founder_key, 0, idata->hash,
3771 client->id, SILC_ID_CLIENT)) {
3772 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3773 SILC_STATUS_ERR_AUTH_FAILED);
3778 silc_auth_payload_free(auth);
3782 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3783 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3784 if (channel->founder_key)
3785 silc_pkcs_public_key_free(channel->founder_key);
3786 if (channel->founder_passwd) {
3787 silc_free(channel->founder_passwd);
3788 channel->founder_passwd = NULL;
3794 /* Finally, set the mode */
3795 channel->mode = mode_mask;
3797 /* Send CMODE_CHANGE notify */
3798 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3799 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3800 SILC_NOTIFY_TYPE_CMODE_CHANGE, 4,
3801 cidp->data, cidp->len,
3803 cipher, cipher ? strlen(cipher) : 0,
3804 hmac, hmac ? strlen(hmac) : 0);
3806 /* Set CMODE notify type to network */
3807 if (!server->standalone)
3808 silc_server_send_notify_cmode(server, server->router->connection,
3809 server->server_type == SILC_ROUTER ?
3810 TRUE : FALSE, channel,
3811 mode_mask, client->id, SILC_ID_CLIENT,
3814 /* Send command reply to sender */
3815 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3816 SILC_STATUS_OK, ident, 2,
3817 2, tmp_id, tmp_len2,
3819 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3820 packet->data, packet->len, FALSE);
3822 silc_buffer_free(packet);
3823 silc_free(channel_id);
3827 silc_server_command_free(cmd);
3830 /* Server side of CUMODE command. Changes client's mode on a channel. */
3832 SILC_SERVER_CMD_FUNC(cumode)
3834 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3835 SilcServer server = cmd->server;
3836 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3837 SilcIDListData idata = (SilcIDListData)client;
3838 SilcChannelID *channel_id;
3839 SilcClientID *client_id;
3840 SilcChannelEntry channel;
3841 SilcClientEntry target_client;
3842 SilcChannelClientEntry chl;
3843 SilcBuffer packet, idp;
3844 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3845 uint32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3847 uint16 ident = silc_command_get_ident(cmd->payload);
3849 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
3851 /* Get Channel ID */
3852 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3854 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3855 SILC_STATUS_ERR_NO_CHANNEL_ID);
3858 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len);
3860 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3861 SILC_STATUS_ERR_NO_CHANNEL_ID);
3865 /* Get channel entry */
3866 channel = silc_idlist_find_channel_by_id(server->local_list,
3869 channel = silc_idlist_find_channel_by_id(server->global_list,
3872 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3873 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3878 /* Check whether sender is on the channel */
3879 if (!silc_server_client_on_channel(client, channel)) {
3880 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3881 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3885 /* Check that client has rights to change other's rights */
3886 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
3887 sender_mask = chl->mode;
3889 /* Get the target client's channel mode mask */
3890 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3892 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3893 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3896 SILC_GET32_MSB(target_mask, tmp_mask);
3898 /* Get target Client ID */
3899 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3901 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3902 SILC_STATUS_ERR_NO_CLIENT_ID);
3905 client_id = silc_id_payload_parse_id(tmp_id, tmp_len);
3907 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3908 SILC_STATUS_ERR_NO_CLIENT_ID);
3912 /* Get target client's entry */
3913 target_client = silc_idlist_find_client_by_id(server->local_list,
3914 client_id, TRUE, NULL);
3915 if (!target_client) {
3916 target_client = silc_idlist_find_client_by_id(server->global_list,
3917 client_id, TRUE, NULL);
3920 if (target_client != client &&
3921 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3922 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3923 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3924 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3928 /* Check whether target client is on the channel */
3929 if (target_client != client) {
3930 if (!silc_server_client_on_channel(target_client, channel)) {
3931 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3932 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
3936 /* Get entry to the channel user list */
3937 silc_hash_table_find(channel->user_list, target_client, NULL,
3945 /* If the target client is founder, no one else can change their mode
3947 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && chl->client != target_client) {
3948 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3949 SILC_STATUS_ERR_NOT_YOU);
3953 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3954 /* The client tries to claim the founder rights. */
3955 unsigned char *tmp_auth;
3956 uint32 tmp_auth_len, auth_len;
3959 if (target_client != client) {
3960 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3961 SILC_STATUS_ERR_NOT_YOU);
3965 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3966 !channel->founder_key) {
3967 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3968 SILC_STATUS_ERR_NOT_YOU);
3972 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3974 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3975 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3979 auth = (channel->founder_method == SILC_AUTH_PASSWORD ?
3980 (void *)channel->founder_passwd : (void *)channel->founder_key);
3981 auth_len = (channel->founder_method == SILC_AUTH_PASSWORD ?
3982 channel->founder_passwd_len : 0);
3984 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len,
3985 channel->founder_method, auth, auth_len,
3986 idata->hash, client->id, SILC_ID_CLIENT)) {
3987 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3988 SILC_STATUS_ERR_AUTH_FAILED);
3992 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
3995 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3996 if (target_client == client) {
3997 /* Remove channel founder rights from itself */
3998 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
4001 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4002 SILC_STATUS_ERR_NOT_YOU);
4008 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
4009 /* Promote to operator */
4010 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4011 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4012 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4013 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4014 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4018 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
4022 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
4023 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4024 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4025 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4026 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4030 /* Demote to normal user */
4031 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
4036 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
4037 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4039 /* Send notify to channel, notify only if mode was actually changed. */
4041 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4042 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
4043 idp->data, idp->len,
4047 /* Set CUMODE notify type to network */
4048 if (!server->standalone)
4049 silc_server_send_notify_cumode(server, server->router->connection,
4050 server->server_type == SILC_ROUTER ?
4051 TRUE : FALSE, channel,
4052 target_mask, client->id,
4057 /* Send command reply to sender */
4058 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
4059 SILC_STATUS_OK, ident, 3,
4061 3, tmp_ch_id, tmp_ch_len,
4062 4, tmp_id, tmp_len);
4063 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4064 packet->data, packet->len, FALSE);
4066 silc_buffer_free(packet);
4067 silc_free(channel_id);
4068 silc_free(client_id);
4069 silc_buffer_free(idp);
4072 silc_server_command_free(cmd);
4075 /* Server side of KICK command. Kicks client out of channel. */
4077 SILC_SERVER_CMD_FUNC(kick)
4079 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4080 SilcServer server = cmd->server;
4081 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4082 SilcClientEntry target_client;
4083 SilcChannelID *channel_id;
4084 SilcClientID *client_id;
4085 SilcChannelEntry channel;
4086 SilcChannelClientEntry chl;
4089 unsigned char *tmp, *comment;
4091 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
4093 /* Get Channel ID */
4094 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4096 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4097 SILC_STATUS_ERR_NO_CHANNEL_ID);
4100 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
4102 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4103 SILC_STATUS_ERR_NO_CHANNEL_ID);
4107 /* Get channel entry */
4108 channel = silc_idlist_find_channel_by_id(server->local_list,
4111 channel = silc_idlist_find_channel_by_id(server->local_list,
4114 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4115 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4120 /* Check whether sender is on the channel */
4121 if (!silc_server_client_on_channel(client, channel)) {
4122 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4123 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4127 /* Check that the kicker is channel operator or channel founder */
4128 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4129 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
4130 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4131 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4135 /* Get target Client ID */
4136 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4138 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4139 SILC_STATUS_ERR_NO_CLIENT_ID);
4142 client_id = silc_id_payload_parse_id(tmp, tmp_len);
4144 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4145 SILC_STATUS_ERR_NO_CLIENT_ID);
4149 /* Get target client's entry */
4150 target_client = silc_idlist_find_client_by_id(server->local_list,
4151 client_id, TRUE, NULL);
4152 if (!target_client) {
4153 target_client = silc_idlist_find_client_by_id(server->global_list,
4154 client_id, TRUE, NULL);
4157 /* Check that the target client is not channel founder. Channel founder
4158 cannot be kicked from the channel. */
4159 silc_hash_table_find(channel->user_list, target_client, NULL, (void *)&chl);
4160 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4161 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4162 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV);
4166 /* Check whether target client is on the channel */
4167 if (!silc_server_client_on_channel(target_client, channel)) {
4168 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4169 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
4175 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4179 /* Send command reply to sender */
4180 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4183 /* Send KICKED notify to local clients on the channel */
4184 idp = silc_id_payload_encode(target_client->id, SILC_ID_CLIENT);
4185 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4186 SILC_NOTIFY_TYPE_KICKED,
4188 idp->data, idp->len,
4189 comment, comment ? strlen(comment) : 0);
4190 silc_buffer_free(idp);
4192 /* Remove the client from the channel. If the channel does not exist
4193 after removing the client then the client kicked itself off the channel
4194 and we don't have to send anything after that. */
4195 if (!silc_server_remove_from_one_channel(server, NULL, channel,
4196 target_client, FALSE))
4199 /* Send KICKED notify to primary route */
4200 if (!server->standalone)
4201 silc_server_send_notify_kicked(server, server->router->connection,
4202 server->server_type == SILC_ROUTER ?
4203 TRUE : FALSE, channel,
4204 target_client->id, comment);
4206 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4207 /* Re-generate channel key */
4208 if (!silc_server_create_channel_key(server, channel, 0))
4211 /* Send the channel key to the channel. The key of course is not sent
4212 to the client who was kicked off the channel. */
4213 silc_server_send_channel_key(server, target_client->connection, channel,
4214 server->server_type == SILC_ROUTER ?
4215 FALSE : !server->standalone);
4219 silc_server_command_free(cmd);
4222 /* Server side of OPER command. Client uses this comand to obtain server
4223 operator privileges to this server/router. */
4225 SILC_SERVER_CMD_FUNC(oper)
4227 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4228 SilcServer server = cmd->server;
4229 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4230 unsigned char *username, *auth;
4232 SilcServerConfigSectionAdminConnection *admin;
4233 SilcIDListData idata = (SilcIDListData)client;
4235 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
4237 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4240 /* Get the username */
4241 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4243 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4244 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4248 /* Get the admin configuration */
4249 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4250 username, client->nickname);
4252 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4253 username, client->nickname);
4255 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4256 SILC_STATUS_ERR_AUTH_FAILED);
4261 /* Get the authentication payload */
4262 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4264 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4265 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4269 /* Verify the authentication data */
4270 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4271 admin->auth_data, admin->auth_data_len,
4272 idata->hash, client->id, SILC_ID_CLIENT)) {
4273 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4274 SILC_STATUS_ERR_AUTH_FAILED);
4278 /* Client is now server operator */
4279 client->mode |= SILC_UMODE_SERVER_OPERATOR;
4281 /* Send UMODE change to primary router */
4282 if (!server->standalone)
4283 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4284 client->id, client->mode);
4286 /* Send reply to the sender */
4287 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4291 silc_server_command_free(cmd);
4294 /* Server side of SILCOPER command. Client uses this comand to obtain router
4295 operator privileges to this router. */
4297 SILC_SERVER_CMD_FUNC(silcoper)
4299 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4300 SilcServer server = cmd->server;
4301 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4302 unsigned char *username, *auth;
4304 SilcServerConfigSectionAdminConnection *admin;
4305 SilcIDListData idata = (SilcIDListData)client;
4307 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
4309 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4312 if (server->server_type == SILC_SERVER) {
4313 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4314 SILC_STATUS_ERR_AUTH_FAILED);
4318 /* Get the username */
4319 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4321 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4322 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4326 /* Get the admin configuration */
4327 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4328 username, client->nickname);
4330 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4331 username, client->nickname);
4333 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4334 SILC_STATUS_ERR_AUTH_FAILED);
4339 /* Get the authentication payload */
4340 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4342 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4343 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4347 /* Verify the authentication data */
4348 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4349 admin->auth_data, admin->auth_data_len,
4350 idata->hash, client->id, SILC_ID_CLIENT)) {
4351 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4352 SILC_STATUS_ERR_AUTH_FAILED);
4356 /* Client is now router operator */
4357 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4359 /* Send UMODE change to primary router */
4360 if (!server->standalone)
4361 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4362 client->id, client->mode);
4364 /* Send reply to the sender */
4365 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4369 silc_server_command_free(cmd);
4372 /* Server side command of CONNECT. Connects us to the specified remote
4373 server or router. */
4375 SILC_SERVER_CMD_FUNC(connect)
4377 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4378 SilcServer server = cmd->server;
4379 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4380 unsigned char *tmp, *host;
4382 uint32 port = SILC_PORT;
4384 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CONNECT, cmd, 1, 2);
4386 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4389 /* Check whether client has the permissions. */
4390 if (client->mode == SILC_UMODE_NONE) {
4391 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4392 SILC_STATUS_ERR_NO_SERVER_PRIV);
4396 if (server->server_type == SILC_ROUTER &&
4397 client->mode & SILC_UMODE_SERVER_OPERATOR) {
4398 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4399 SILC_STATUS_ERR_NO_ROUTER_PRIV);
4403 /* Get the remote server */
4404 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4406 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4407 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4412 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4414 SILC_GET32_MSB(port, tmp);
4416 /* Create the connection. It is done with timeout and is async. */
4417 silc_server_create_connection(server, host, port);
4419 /* Send reply to the sender */
4420 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4424 silc_server_command_free(cmd);
4427 /* Server side of command BAN. This is used to manage the ban list of the
4428 channel. To add clients and remove clients from the ban list. */
4430 SILC_SERVER_CMD_FUNC(ban)
4432 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4433 SilcServer server = cmd->server;
4434 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4436 SilcChannelEntry channel;
4437 SilcChannelClientEntry chl;
4438 SilcChannelID *channel_id = NULL;
4439 unsigned char *id, *add, *del;
4440 uint32 id_len, tmp_len;
4441 uint16 ident = silc_command_get_ident(cmd->payload);
4443 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4446 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
4448 /* Get Channel ID */
4449 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4451 channel_id = silc_id_payload_parse_id(id, id_len);
4453 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4454 SILC_STATUS_ERR_NO_CHANNEL_ID);
4459 /* Get channel entry. The server must know about the channel since the
4460 client is expected to be on the channel. */
4461 channel = silc_idlist_find_channel_by_id(server->local_list,
4464 channel = silc_idlist_find_channel_by_id(server->global_list,
4467 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4468 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4473 /* Check whether this client is on the channel */
4474 if (!silc_server_client_on_channel(client, channel)) {
4475 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4476 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4480 /* Get entry to the channel user list */
4481 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4483 /* The client must be at least channel operator. */
4484 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4485 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4486 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4490 /* Get the new ban and add it to the ban list */
4491 add = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4493 if (!channel->ban_list)
4494 channel->ban_list = silc_calloc(tmp_len + 2, sizeof(*channel->ban_list));
4496 channel->ban_list = silc_realloc(channel->ban_list,
4497 sizeof(*channel->ban_list) *
4499 strlen(channel->ban_list) + 2));
4500 if (add[tmp_len - 1] == ',')
4501 add[tmp_len - 1] = '\0';
4503 strncat(channel->ban_list, add, tmp_len);
4504 strncat(channel->ban_list, ",", 1);
4507 /* Get the ban to be removed and remove it from the list */
4508 del = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4509 if (del && channel->ban_list) {
4510 char *start, *end, *n;
4512 if (!strncmp(channel->ban_list, del, strlen(channel->ban_list) - 1)) {
4513 silc_free(channel->ban_list);
4514 channel->ban_list = NULL;
4516 start = strstr(channel->ban_list, del);
4517 if (start && strlen(start) >= tmp_len) {
4518 end = start + tmp_len;
4519 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
4520 strncat(n, channel->ban_list, start - channel->ban_list);
4521 strncat(n, end + 1, ((channel->ban_list + strlen(channel->ban_list)) -
4523 silc_free(channel->ban_list);
4524 channel->ban_list = n;
4529 /* Send the BAN notify type to our primary router. */
4530 if (!server->standalone && (add || del))
4531 silc_server_send_notify_ban(server, server->router->connection,
4532 server->server_type == SILC_ROUTER ?
4533 TRUE : FALSE, channel, add, del);
4535 /* Send the reply back to the client */
4536 if (channel->ban_list)
4538 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4539 SILC_STATUS_OK, ident, 2,
4541 3, channel->ban_list,
4542 strlen(channel->ban_list) - 1);
4545 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4546 SILC_STATUS_OK, ident, 1,
4549 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4550 packet->data, packet->len, FALSE);
4552 silc_buffer_free(packet);
4556 silc_free(channel_id);
4557 silc_server_command_free(cmd);
4560 /* Server side command of CLOSE. Closes connection to a specified server. */
4562 SILC_SERVER_CMD_FUNC(close)
4564 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4565 SilcServer server = cmd->server;
4566 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4567 SilcServerEntry server_entry;
4568 SilcSocketConnection sock;
4571 unsigned char *name;
4572 uint32 port = SILC_PORT;
4574 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CLOSE, cmd, 1, 2);
4576 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4579 /* Check whether client has the permissions. */
4580 if (client->mode == SILC_UMODE_NONE) {
4581 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4582 SILC_STATUS_ERR_NO_SERVER_PRIV);
4586 /* Get the remote server */
4587 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4589 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4590 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4595 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4597 SILC_GET32_MSB(port, tmp);
4599 server_entry = silc_idlist_find_server_by_conn(server->local_list,
4600 name, port, FALSE, NULL);
4601 if (!server_entry) {
4602 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4603 SILC_STATUS_ERR_NO_SERVER_ID);
4607 /* Send reply to the sender */
4608 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4611 /* Close the connection to the server */
4612 sock = (SilcSocketConnection)server_entry->connection;
4613 if (sock->user_data)
4614 silc_server_free_sock_user_data(server, sock);
4615 silc_server_close_connection(server, sock);
4618 silc_server_command_free(cmd);
4621 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4622 active connections. */
4624 SILC_SERVER_CMD_FUNC(shutdown)
4626 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4627 SilcServer server = cmd->server;
4628 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4630 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SHUTDOWN, cmd, 0, 0);
4632 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4635 /* Check whether client has the permission. */
4636 if (client->mode == SILC_UMODE_NONE) {
4637 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4638 SILC_STATUS_ERR_NO_SERVER_PRIV);
4642 /* Send reply to the sender */
4643 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4646 /* Then, gracefully, or not, bring the server down. */
4647 silc_server_stop(server);
4651 silc_server_command_free(cmd);
4654 /* Server side command of LEAVE. Removes client from a channel. */
4656 SILC_SERVER_CMD_FUNC(leave)
4658 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4659 SilcServer server = cmd->server;
4660 SilcSocketConnection sock = cmd->sock;
4661 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4662 SilcChannelID *id = NULL;
4663 SilcChannelEntry channel;
4667 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
4669 /* Get Channel ID */
4670 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4672 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4673 SILC_STATUS_ERR_NO_CHANNEL_ID);
4676 id = silc_id_payload_parse_id(tmp, len);
4678 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4679 SILC_STATUS_ERR_NO_CHANNEL_ID);
4683 /* Get channel entry */
4684 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4686 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4688 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4689 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4694 /* Check whether this client is on the channel */
4695 if (!silc_server_client_on_channel(id_entry, channel)) {
4696 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4697 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4701 /* Notify routers that they should remove this client from their list
4702 of clients on the channel. Send LEAVE notify type. */
4703 if (!server->standalone)
4704 silc_server_send_notify_leave(server, server->router->connection,
4705 server->server_type == SILC_ROUTER ?
4706 TRUE : FALSE, channel, id_entry->id);
4708 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4711 /* Remove client from channel */
4712 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4714 /* If the channel does not exist anymore we won't send anything */
4717 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4718 /* Re-generate channel key */
4719 if (!silc_server_create_channel_key(server, channel, 0))
4722 /* Send the channel key */
4723 silc_server_send_channel_key(server, NULL, channel,
4724 server->server_type == SILC_ROUTER ?
4725 FALSE : !server->standalone);
4731 silc_server_command_free(cmd);
4734 /* Server side of command USERS. Resolves clients and their USERS currently
4735 joined on the requested channel. The list of Client ID's and their modes
4736 on the channel is sent back. */
4738 SILC_SERVER_CMD_FUNC(users)
4740 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4741 SilcServer server = cmd->server;
4742 SilcChannelEntry channel;
4743 SilcChannelID *id = NULL;
4744 SilcBuffer packet, idp;
4745 unsigned char *channel_id;
4746 uint32 channel_id_len;
4747 SilcBuffer client_id_list;
4748 SilcBuffer client_mode_list;
4749 unsigned char lc[4];
4750 uint32 list_count = 0;
4751 uint16 ident = silc_command_get_ident(cmd->payload);
4754 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
4756 /* Get Channel ID */
4757 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4759 /* Get channel name */
4760 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
4762 if (!channel_id && !channel_name) {
4763 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4764 SILC_STATUS_ERR_NO_CHANNEL_ID);
4769 id = silc_id_payload_parse_id(channel_id, channel_id_len);
4771 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4772 SILC_STATUS_ERR_NO_CHANNEL_ID);
4777 /* If we are server and we don't know about this channel we will send
4778 the command to our router. If we know about the channel then we also
4779 have the list of users already. */
4781 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4783 channel = silc_idlist_find_channel_by_name(server->local_list,
4784 channel_name, NULL);
4787 if (server->server_type == SILC_SERVER && !server->standalone &&
4791 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4792 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4794 /* Send USERS command */
4795 silc_server_packet_send(server, server->router->connection,
4796 SILC_PACKET_COMMAND, cmd->packet->flags,
4797 tmpbuf->data, tmpbuf->len, TRUE);
4799 /* Reprocess this packet after received reply */
4800 silc_server_command_pending(server, SILC_COMMAND_USERS,
4801 silc_command_get_ident(cmd->payload),
4802 silc_server_command_destructor,
4803 silc_server_command_users,
4804 silc_server_command_dup(cmd));
4805 cmd->pending = TRUE;
4806 silc_command_set_ident(cmd->payload, ident);
4808 silc_buffer_free(tmpbuf);
4813 /* Check the global list as well. */
4815 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4817 channel = silc_idlist_find_channel_by_name(server->global_list,
4818 channel_name, NULL);
4820 /* Channel really does not exist */
4821 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4822 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4827 /* If the channel is private or secret do not send anything */
4828 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)) {
4829 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4830 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4834 /* Get the users list */
4835 silc_server_get_users_on_channel(server, channel, &client_id_list,
4836 &client_mode_list, &list_count);
4839 SILC_PUT32_MSB(list_count, lc);
4842 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4843 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4844 SILC_STATUS_OK, ident, 4,
4845 2, idp->data, idp->len,
4847 4, client_id_list->data,
4848 client_id_list->len,
4849 5, client_mode_list->data,
4850 client_mode_list->len);
4851 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4852 packet->data, packet->len, FALSE);
4854 silc_buffer_free(idp);
4855 silc_buffer_free(packet);
4856 silc_buffer_free(client_id_list);
4857 silc_buffer_free(client_mode_list);
4862 silc_server_command_free(cmd);
4865 /* Server side of command GETKEY. This fetches the client's public key
4866 from the server where to the client is connected. */
4868 SILC_SERVER_CMD_FUNC(getkey)
4870 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4871 SilcServer server = cmd->server;
4873 SilcClientEntry client;
4874 SilcServerEntry server_entry;
4875 SilcClientID *client_id = NULL;
4876 SilcServerID *server_id = NULL;
4877 SilcIDPayload idp = NULL;
4878 uint16 ident = silc_command_get_ident(cmd->payload);
4884 SILC_LOG_DEBUG(("Start"));
4886 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4888 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4889 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4892 idp = silc_id_payload_parse_data(tmp, tmp_len);
4894 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4895 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4899 id_type = silc_id_payload_get_type(idp);
4900 if (id_type == SILC_ID_CLIENT) {
4901 client_id = silc_id_payload_get_id(idp);
4903 /* If the client is not found from local list there is no chance it
4904 would be locally connected client so send the command further. */
4905 client = silc_idlist_find_client_by_id(server->local_list,
4906 client_id, TRUE, NULL);
4908 client = silc_idlist_find_client_by_id(server->global_list,
4909 client_id, TRUE, NULL);
4911 if ((!client && !cmd->pending && !server->standalone) ||
4912 (client && !client->connection && !cmd->pending &&
4913 !server->standalone) ||
4914 (client && !client->data.public_key && !cmd->pending &&
4915 !server->standalone)) {
4918 SilcSocketConnection dest_sock;
4920 dest_sock = silc_server_get_client_route(server, NULL, 0,
4925 old_ident = silc_command_get_ident(cmd->payload);
4926 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4927 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4929 silc_server_packet_send(server, dest_sock,
4930 SILC_PACKET_COMMAND, cmd->packet->flags,
4931 tmpbuf->data, tmpbuf->len, TRUE);
4933 /* Reprocess this packet after received reply from router */
4934 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4935 silc_command_get_ident(cmd->payload),
4936 silc_server_command_destructor,
4937 silc_server_command_getkey,
4938 silc_server_command_dup(cmd));
4939 cmd->pending = TRUE;
4941 silc_command_set_ident(cmd->payload, old_ident);
4942 silc_buffer_free(tmpbuf);
4946 if (!client && cmd->pending) {
4947 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4948 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
4952 /* The client is locally connected, just get the public key and
4954 tmp = silc_pkcs_public_key_encode(client->data.public_key, &tmp_len);
4955 pk = silc_buffer_alloc(4 + tmp_len);
4956 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
4957 silc_buffer_format(pk,
4958 SILC_STR_UI_SHORT(tmp_len),
4959 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
4960 SILC_STR_UI_XNSTRING(tmp, tmp_len),
4964 } else if (id_type == SILC_ID_SERVER) {
4965 server_id = silc_id_payload_get_id(idp);
4967 /* If the server is not found from local list there is no chance it
4968 would be locally connected server so send the command further. */
4969 server_entry = silc_idlist_find_server_by_id(server->local_list,
4970 server_id, TRUE, NULL);
4972 server_entry = silc_idlist_find_server_by_id(server->global_list,
4973 server_id, TRUE, NULL);
4975 if ((!server_entry && !cmd->pending && !server->standalone) ||
4976 (server_entry && !server_entry->connection && !cmd->pending &&
4977 !server->standalone) ||
4978 (server_entry && !server_entry->data.public_key && !cmd->pending &&
4979 !server->standalone)) {
4983 old_ident = silc_command_get_ident(cmd->payload);
4984 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4985 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4987 silc_server_packet_send(server, server->router->connection,
4988 SILC_PACKET_COMMAND, cmd->packet->flags,
4989 tmpbuf->data, tmpbuf->len, TRUE);
4991 /* Reprocess this packet after received reply from router */
4992 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4993 silc_command_get_ident(cmd->payload),
4994 silc_server_command_destructor,
4995 silc_server_command_getkey,
4996 silc_server_command_dup(cmd));
4997 cmd->pending = TRUE;
4999 silc_command_set_ident(cmd->payload, old_ident);
5000 silc_buffer_free(tmpbuf);
5004 if (!server_entry && cmd->pending) {
5005 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5006 SILC_STATUS_ERR_NO_SUCH_SERVER_ID);
5010 /* The client is locally connected, just get the public key and
5012 tmp = silc_pkcs_public_key_encode(server_entry->data.public_key, &tmp_len);
5013 pk = silc_buffer_alloc(4 + tmp_len);
5014 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
5015 silc_buffer_format(pk,
5016 SILC_STR_UI_SHORT(tmp_len),
5017 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
5018 SILC_STR_UI_XNSTRING(tmp, tmp_len),
5025 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5026 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
5027 SILC_STATUS_OK, ident, 2,
5029 3, pk->data, pk->len);
5030 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
5031 packet->data, packet->len, FALSE);
5032 silc_buffer_free(packet);
5033 silc_buffer_free(pk);
5037 silc_id_payload_free(idp);
5038 silc_free(client_id);
5039 silc_free(server_id);
5040 silc_server_command_free(cmd);