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,
40 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
42 /* Server command list. */
43 SilcServerCommand silc_command_list[] =
45 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
46 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
47 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
48 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
49 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
50 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
51 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
53 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
54 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
55 SILC_SERVER_CMD(connect, CONNECT,
56 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
57 SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
58 SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
59 SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
60 SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
61 SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
62 SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
63 SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
64 SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
65 SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
66 SILC_SERVER_CMD(close, CLOSE,
67 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
68 SILC_SERVER_CMD(shutdown, SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
70 SILC_SERVER_CMD(silcoper, SILCOPER,
71 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
72 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
73 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
74 SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
79 #define SILC_SERVER_COMMAND_CHECK_ARGC(command, context, min, max) \
81 uint32 _argc = silc_argument_get_arg_num(cmd->args); \
83 SILC_LOG_DEBUG(("Start")); \
86 silc_server_command_send_status_reply(cmd, command, \
87 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS); \
88 silc_server_command_free(cmd); \
92 silc_server_command_send_status_reply(cmd, command, \
93 SILC_STATUS_ERR_TOO_MANY_PARAMS); \
94 silc_server_command_free(cmd); \
99 /* Returns TRUE if the connection is registered. Unregistered connections
100 usually cannot send commands hence the check. */
102 static int silc_server_is_registered(SilcServer server,
103 SilcSocketConnection sock,
104 SilcServerCommandContext cmd,
107 SilcIDListData idata = (SilcIDListData)sock->user_data;
108 if (idata->registered)
111 silc_server_command_send_status_reply(cmd, command,
112 SILC_STATUS_ERR_NOT_REGISTERED);
113 silc_server_command_free(cmd);
117 /* Internal context to hold data when executed command with timeout. */
119 SilcServerCommandContext ctx;
120 SilcServerCommand *cmd;
121 } *SilcServerCommandTimeout;
123 /* Timeout callback to process commands with timeout for client. Client's
124 commands are always executed with timeout. */
126 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
128 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
129 SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
131 /* Update access time */
132 client->last_command = time(NULL);
134 if (!(timeout->cmd->flags & SILC_CF_REG))
135 timeout->cmd->cb(timeout->ctx);
136 else if (silc_server_is_registered(timeout->ctx->server,
140 timeout->cmd->cb(timeout->ctx);
145 /* Processes received command packet. */
147 void silc_server_command_process(SilcServer server,
148 SilcSocketConnection sock,
149 SilcPacketContext *packet)
151 SilcServerCommandContext ctx;
152 SilcServerCommand *cmd;
155 /* Allocate command context. This must be free'd by the
156 command routine receiving it. */
157 ctx = silc_server_command_alloc();
158 ctx->server = server;
159 ctx->sock = silc_socket_dup(sock);
160 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
162 /* Parse the command payload in the packet */
163 ctx->payload = silc_command_payload_parse(packet->buffer);
165 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
166 silc_buffer_free(packet->buffer);
167 silc_packet_context_free(packet);
168 silc_socket_free(ctx->sock);
172 ctx->args = silc_command_get_args(ctx->payload);
174 /* Get the command */
175 command = silc_command_get(ctx->payload);
176 for (cmd = silc_command_list; cmd->cb; cmd++)
177 if (cmd->cmd == command)
181 silc_server_command_send_status_reply(ctx, command,
182 SILC_STATUS_ERR_UNKNOWN_COMMAND);
183 silc_server_command_free(ctx);
187 /* Execute client's commands always with timeout. Normally they are
188 executed with zero (0) timeout but if client is sending command more
189 frequently than once in 2 seconds, then the timeout may be 0 to 2
191 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
192 SilcClientEntry client = (SilcClientEntry)sock->user_data;
193 SilcServerCommandTimeout timeout = silc_calloc(1, sizeof(*timeout));
199 if (client->last_command && (time(NULL) - client->last_command) < 2) {
200 client->fast_command++;
203 client->fast_command = ((client->fast_command - 1) <= 0 ? 0 :
204 client->fast_command--);
208 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
209 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
210 silc_task_register(server->timeout_queue, sock->sock,
211 silc_server_command_process_timeout,
213 2 - (time(NULL) - client->last_command), 0,
215 SILC_TASK_PRI_NORMAL);
217 silc_task_register(server->timeout_queue, sock->sock,
218 silc_server_command_process_timeout,
222 SILC_TASK_PRI_NORMAL);
226 /* Execute for server */
228 if (!(cmd->flags & SILC_CF_REG))
230 else if (silc_server_is_registered(server, sock, ctx, cmd->cmd))
234 /* Allocate Command Context */
236 SilcServerCommandContext silc_server_command_alloc()
238 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
243 /* Free's the command context allocated before executing the command */
245 void silc_server_command_free(SilcServerCommandContext ctx)
248 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
250 if (ctx->users < 1) {
252 silc_command_free_payload(ctx->payload);
254 silc_packet_context_free(ctx->packet);
256 silc_socket_free(ctx->sock); /* Decrease reference counter */
261 /* Duplicate Command Context by adding reference counter. The context won't
262 be free'd untill it hits zero. */
264 SilcServerCommandContext
265 silc_server_command_dup(SilcServerCommandContext ctx)
268 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
273 /* Add new pending command to be executed when reply to a command has been
274 received. The `reply_cmd' is the command that will call the `callback'
275 with `context' when reply has been received. If `ident' is non-zero
276 the `callback' will be executed when received reply with command
277 identifier `ident'. */
279 void silc_server_command_pending(SilcServer server,
280 SilcCommand reply_cmd,
282 SilcServerPendingDestructor destructor,
283 SilcCommandCb callback,
286 SilcServerCommandPending *reply;
288 reply = silc_calloc(1, sizeof(*reply));
289 reply->reply_cmd = reply_cmd;
290 reply->ident = ident;
291 reply->context = context;
292 reply->callback = callback;
293 reply->destructor = destructor;
294 silc_dlist_add(server->pending_commands, reply);
297 /* Deletes pending command by reply command type. */
299 void silc_server_command_pending_del(SilcServer server,
300 SilcCommand reply_cmd,
303 SilcServerCommandPending *r;
305 silc_dlist_start(server->pending_commands);
306 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
307 if (r->reply_cmd == reply_cmd && r->ident == ident) {
308 silc_dlist_del(server->pending_commands, r);
314 /* Checks for pending commands and marks callbacks to be called from
315 the command reply function. Returns TRUE if there were pending command. */
317 int silc_server_command_pending_check(SilcServer server,
318 SilcServerCommandReplyContext ctx,
322 SilcServerCommandPending *r;
324 silc_dlist_start(server->pending_commands);
325 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
326 if (r->reply_cmd == command && r->ident == ident) {
327 ctx->context = r->context;
328 ctx->callback = r->callback;
329 ctx->destructor = r->destructor;
338 /* Destructor function for pending callbacks. This is called when using
339 pending commands to free the context given for the pending command. */
341 static void silc_server_command_destructor(void *context)
343 silc_server_command_free((SilcServerCommandContext)context);
346 /* Sends simple status message as command reply packet */
349 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
351 SilcCommandStatus status)
355 SILC_LOG_DEBUG(("Sending command status %d", status));
358 silc_command_reply_payload_encode_va(command, status,
359 silc_command_get_ident(cmd->payload),
361 silc_server_packet_send(cmd->server, cmd->sock,
362 SILC_PACKET_COMMAND_REPLY, 0,
363 buffer->data, buffer->len, FALSE);
364 silc_buffer_free(buffer);
367 /* Sends command status reply with one extra argument. The argument
368 type must be sent as argument. */
371 silc_server_command_send_status_data(SilcServerCommandContext cmd,
373 SilcCommandStatus status,
380 SILC_LOG_DEBUG(("Sending command status %d", status));
383 silc_command_reply_payload_encode_va(command, status,
384 silc_command_get_ident(cmd->payload),
385 1, arg_type, arg, arg_len);
386 silc_server_packet_send(cmd->server, cmd->sock,
387 SILC_PACKET_COMMAND_REPLY, 0,
388 buffer->data, buffer->len, FALSE);
389 silc_buffer_free(buffer);
392 /******************************************************************************
396 ******************************************************************************/
399 silc_server_command_whois_parse(SilcServerCommandContext cmd,
400 SilcClientID ***client_id,
401 uint32 *client_id_count,
409 uint32 argc = silc_argument_get_arg_num(cmd->args);
412 /* If client ID is in the command it must be used instead of nickname */
413 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
415 /* No ID, get the nickname@server string and parse it. */
416 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
418 if (strchr(tmp, '@')) {
419 len = strcspn(tmp, "@");
420 *nickname = silc_calloc(len + 1, sizeof(char));
421 memcpy(*nickname, tmp, len);
422 *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
423 memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
425 *nickname = strdup(tmp);
428 silc_server_command_send_status_reply(cmd, command,
429 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
433 /* Command includes ID, we must use that. Also check whether the command
434 has more than one ID set - take them all. */
436 *client_id = silc_calloc(1, sizeof(**client_id));
437 (*client_id)[0] = silc_id_payload_parse_id(tmp, len);
438 if ((*client_id)[0] == NULL) {
439 silc_free(*client_id);
442 *client_id_count = 1;
444 /* Take all ID's from the command packet */
446 for (k = 1, i = 1; i < argc; i++) {
447 tmp = silc_argument_get_arg_type(cmd->args, i + 3, &len);
449 *client_id = silc_realloc(*client_id, sizeof(**client_id) *
450 (*client_id_count + 1));
451 (*client_id)[k] = silc_id_payload_parse_id(tmp, len);
452 if ((*client_id)[k] == NULL) {
453 /* Cleanup all and fail */
454 for (i = 0; i < *client_id_count; i++)
455 silc_free((*client_id)[i]);
456 silc_free(*client_id);
459 (*client_id_count)++;
465 /* Command includes ID, use that */
468 /* Get the max count of reply messages allowed */
469 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
479 silc_server_command_whois_check(SilcServerCommandContext cmd,
480 SilcClientEntry *clients,
481 uint32 clients_count)
483 SilcServer server = cmd->server;
485 SilcClientEntry entry;
487 for (i = 0; i < clients_count; i++) {
490 if (!entry || entry->data.registered == FALSE)
493 if (!entry->nickname || !entry->username || !entry->userinfo) {
500 old_ident = silc_command_get_ident(cmd->payload);
501 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
502 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
504 /* Send WHOIS command */
505 silc_server_packet_send(server, entry->router->connection,
506 SILC_PACKET_COMMAND, cmd->packet->flags,
507 tmpbuf->data, tmpbuf->len, TRUE);
509 /* Reprocess this packet after received reply */
510 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
511 silc_command_get_ident(cmd->payload),
512 silc_server_command_destructor,
513 silc_server_command_whois,
514 silc_server_command_dup(cmd));
517 silc_command_set_ident(cmd->payload, old_ident);
519 silc_buffer_free(tmpbuf);
528 silc_server_command_whois_send_reply(SilcServerCommandContext cmd,
529 SilcClientEntry *clients,
530 uint32 clients_count,
533 SilcServer server = cmd->server;
536 SilcBuffer packet, idp, channels;
537 SilcClientEntry entry;
538 SilcCommandStatus status;
539 uint16 ident = silc_command_get_ident(cmd->payload);
540 char nh[128], uh[128];
541 unsigned char idle[4], mode[4];
542 SilcSocketConnection hsock;
545 for (i = 0; i < clients_count; i++)
546 if (clients[i]->data.registered)
549 status = SILC_STATUS_OK;
551 status = SILC_STATUS_LIST_START;
553 for (i = 0, k = 0; i < clients_count; i++) {
556 if (entry->data.registered == FALSE) {
557 if (clients_count == 1) {
558 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
559 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
560 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
561 2, idp->data, idp->len);
562 silc_buffer_free(idp);
568 status = SILC_STATUS_LIST_ITEM;
570 if (clients_count > 1 && k == clients_count - 1)
571 status = SILC_STATUS_LIST_END;
573 if (count && k - 1 == count)
574 status = SILC_STATUS_LIST_END;
576 if (count && k - 1 > count)
579 /* Sanity check, however these should never fail. However, as
580 this sanity check has been added here they have failed. */
581 if (!entry->nickname || !entry->username || !entry->userinfo)
584 /* Send WHOIS reply */
585 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
586 tmp = silc_argument_get_first_arg(cmd->args, NULL);
588 memset(uh, 0, sizeof(uh));
589 memset(nh, 0, sizeof(nh));
590 memset(idle, 0, sizeof(idle));
592 strncat(nh, entry->nickname, strlen(entry->nickname));
593 if (!strchr(entry->nickname, '@')) {
595 len = entry->router ? strlen(entry->router->server_name) :
596 strlen(server->server_name);
597 strncat(nh, entry->router ? entry->router->server_name :
598 server->server_name, len);
601 strncat(uh, entry->username, strlen(entry->username));
602 if (!strchr(entry->username, '@')) {
604 hsock = (SilcSocketConnection)entry->connection;
605 len = strlen(hsock->hostname);
606 strncat(uh, hsock->hostname, len);
609 channels = silc_server_get_client_channel_list(server, entry);
611 SILC_PUT32_MSB(entry->mode, mode);
613 if (entry->connection) {
614 SILC_PUT32_MSB((time(NULL) - entry->data.last_receive), idle);
618 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
620 2, idp->data, idp->len,
624 strlen(entry->userinfo),
630 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
632 2, idp->data, idp->len,
636 strlen(entry->userinfo),
640 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
641 0, packet->data, packet->len, FALSE);
643 silc_buffer_free(packet);
644 silc_buffer_free(idp);
646 silc_buffer_free(channels);
653 silc_server_command_whois_from_client(SilcServerCommandContext cmd)
655 SilcServer server = cmd->server;
656 char *nick = NULL, *server_name = NULL;
658 SilcClientEntry *clients = NULL, entry;
659 SilcClientID **client_id = NULL;
660 uint32 client_id_count = 0, clients_count = 0;
663 /* Protocol dictates that we must always send the received WHOIS request
664 to our router if we are normal server, so let's do it now unless we
665 are standalone. We will not send any replies to the client until we
666 have received reply from the router. */
667 if (server->server_type == SILC_SERVER && !cmd->pending &&
668 !server->standalone) {
672 old_ident = silc_command_get_ident(cmd->payload);
673 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
674 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
676 /* Send WHOIS command to our router */
677 silc_server_packet_send(server, (SilcSocketConnection)
678 server->router->connection,
679 SILC_PACKET_COMMAND, cmd->packet->flags,
680 tmpbuf->data, tmpbuf->len, TRUE);
682 /* Reprocess this packet after received reply from router */
683 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
684 silc_command_get_ident(cmd->payload),
685 silc_server_command_destructor,
686 silc_server_command_whois,
687 silc_server_command_dup(cmd));
690 silc_command_set_ident(cmd->payload, old_ident);
692 silc_buffer_free(tmpbuf);
697 /* We are ready to process the command request. Let's search for the
698 requested client and send reply to the requesting client. */
700 /* Parse the whois request */
701 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
702 &nick, &server_name, &count,
706 /* Get all clients matching that ID or nickname from local list */
707 if (client_id_count) {
708 /* Check all Client ID's received in the command packet */
709 for (i = 0; i < client_id_count; i++) {
710 entry = silc_idlist_find_client_by_id(server->local_list,
713 clients = silc_realloc(clients, sizeof(*clients) *
714 (clients_count + 1));
715 clients[clients_count++] = entry;
719 if (!silc_idlist_get_clients_by_hash(server->local_list,
720 nick, server->md5hash,
721 &clients, &clients_count))
722 silc_idlist_get_clients_by_nickname(server->local_list,
724 &clients, &clients_count);
727 /* Check global list as well */
728 if (client_id_count) {
729 /* Check all Client ID's received in the command packet */
730 for (i = 0; i < client_id_count; i++) {
731 entry = silc_idlist_find_client_by_id(server->global_list,
734 clients = silc_realloc(clients, sizeof(*clients) *
735 (clients_count + 1));
736 clients[clients_count++] = entry;
740 if (!silc_idlist_get_clients_by_hash(server->global_list,
741 nick, server->md5hash,
742 &clients, &clients_count))
743 silc_idlist_get_clients_by_nickname(server->global_list,
745 &clients, &clients_count);
749 /* Such client(s) really does not exist in the SILC network. */
750 if (!client_id_count) {
751 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
752 SILC_STATUS_ERR_NO_SUCH_NICK,
753 3, nick, strlen(nick));
755 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
756 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
757 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
758 2, idp->data, idp->len);
759 silc_buffer_free(idp);
764 /* Router always finds the client entry if it exists in the SILC network.
765 However, it might be incomplete entry and does not include all the
766 mandatory fields that WHOIS command reply requires. Check for these and
767 make query from the server who owns the client if some fields are
769 if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
774 /* Send the command reply to the client */
775 silc_server_command_whois_send_reply(cmd, clients, clients_count,
779 if (client_id_count) {
780 for (i = 0; i < client_id_count; i++)
781 silc_free(client_id[i]);
782 silc_free(client_id);
789 silc_free(server_name);
795 silc_server_command_whois_from_server(SilcServerCommandContext cmd)
797 SilcServer server = cmd->server;
798 char *nick = NULL, *server_name = NULL;
800 SilcClientEntry *clients = NULL, entry;
801 SilcClientID **client_id = NULL;
802 uint32 client_id_count = 0, clients_count = 0;
805 /* Parse the whois request */
806 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
807 &nick, &server_name, &count,
811 /* Process the command request. Let's search for the requested client and
812 send reply to the requesting server. */
814 if (client_id_count) {
815 /* Check all Client ID's received in the command packet */
816 for (i = 0; i < client_id_count; i++) {
817 entry = silc_idlist_find_client_by_id(server->local_list,
820 clients = silc_realloc(clients, sizeof(*clients) *
821 (clients_count + 1));
822 clients[clients_count++] = entry;
826 if (!silc_idlist_get_clients_by_hash(server->local_list,
827 nick, server->md5hash,
828 &clients, &clients_count))
829 silc_idlist_get_clients_by_nickname(server->local_list,
831 &clients, &clients_count);
834 /* If we are router we will check our global list as well. */
835 if (server->server_type == SILC_ROUTER) {
836 if (client_id_count) {
837 /* Check all Client ID's received in the command packet */
838 for (i = 0; i < client_id_count; i++) {
839 entry = silc_idlist_find_client_by_id(server->global_list,
842 clients = silc_realloc(clients, sizeof(*clients) *
843 (clients_count + 1));
844 clients[clients_count++] = entry;
848 if (!silc_idlist_get_clients_by_hash(server->global_list,
849 nick, server->md5hash,
850 &clients, &clients_count))
851 silc_idlist_get_clients_by_nickname(server->global_list,
853 &clients, &clients_count);
858 /* Such a client really does not exist in the SILC network. */
859 if (!client_id_count) {
860 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
861 SILC_STATUS_ERR_NO_SUCH_NICK,
862 3, nick, strlen(nick));
864 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
865 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
866 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
867 2, idp->data, idp->len);
868 silc_buffer_free(idp);
873 /* Router always finds the client entry if it exists in the SILC network.
874 However, it might be incomplete entry and does not include all the
875 mandatory fields that WHOIS command reply requires. Check for these and
876 make query from the server who owns the client if some fields are
878 if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
883 /* Send the command reply to the client */
884 silc_server_command_whois_send_reply(cmd, clients, clients_count,
888 if (client_id_count) {
889 for (i = 0; i < client_id_count; i++)
890 silc_free(client_id[i]);
891 silc_free(client_id);
898 silc_free(server_name);
903 /* Server side of command WHOIS. Processes user's query and sends found
904 results as command replies back to the client. */
906 SILC_SERVER_CMD_FUNC(whois)
908 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
911 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_WHOIS, cmd, 1, 3328);
913 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
914 ret = silc_server_command_whois_from_client(cmd);
915 else if ((cmd->sock->type == SILC_SOCKET_TYPE_SERVER) ||
916 (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER))
917 ret = silc_server_command_whois_from_server(cmd);
920 silc_server_command_free(cmd);
923 /******************************************************************************
927 ******************************************************************************/
930 silc_server_command_whowas_parse(SilcServerCommandContext cmd,
938 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
940 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WHOWAS,
941 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
945 /* Get the nickname@server string and parse it. */
946 if (strchr(tmp, '@')) {
947 len = strcspn(tmp, "@");
948 *nickname = silc_calloc(len + 1, sizeof(char));
949 memcpy(*nickname, tmp, len);
950 *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
951 memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
953 *nickname = strdup(tmp);
955 /* Get the max count of reply messages allowed */
956 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
966 silc_server_command_whowas_check(SilcServerCommandContext cmd,
967 SilcClientEntry *clients,
968 uint32 clients_count)
970 SilcServer server = cmd->server;
972 SilcClientEntry entry;
974 for (i = 0; i < clients_count; i++) {
977 if (!entry->nickname || !entry->username) {
984 old_ident = silc_command_get_ident(cmd->payload);
985 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
986 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
988 /* Send WHOWAS command */
989 silc_server_packet_send(server, entry->router->connection,
990 SILC_PACKET_COMMAND, cmd->packet->flags,
991 tmpbuf->data, tmpbuf->len, TRUE);
993 /* Reprocess this packet after received reply */
994 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
995 silc_command_get_ident(cmd->payload),
996 silc_server_command_destructor,
997 silc_server_command_whowas,
998 silc_server_command_dup(cmd));
1001 silc_command_set_ident(cmd->payload, old_ident);
1003 silc_buffer_free(tmpbuf);
1012 silc_server_command_whowas_send_reply(SilcServerCommandContext cmd,
1013 SilcClientEntry *clients,
1014 uint32 clients_count)
1016 SilcServer server = cmd->server;
1018 int i, count = 0, len;
1019 SilcBuffer packet, idp;
1020 SilcClientEntry entry = NULL;
1021 SilcCommandStatus status;
1022 uint16 ident = silc_command_get_ident(cmd->payload);
1024 char nh[256], uh[256];
1026 status = SILC_STATUS_OK;
1027 if (clients_count > 1)
1028 status = SILC_STATUS_LIST_START;
1030 for (i = 0; i < clients_count; i++) {
1033 /* We will take only clients that are not valid anymore. They are the
1034 ones that are not registered anymore but still have a ID. They
1035 have disconnected us, and thus valid for WHOWAS. */
1036 if (entry->data.registered == TRUE)
1038 if (entry->id == NULL)
1041 if (count && i - 1 == count)
1046 if (clients_count > 2)
1047 status = SILC_STATUS_LIST_ITEM;
1049 if (clients_count > 1 && i == clients_count - 1)
1050 status = SILC_STATUS_LIST_END;
1052 /* Sanity check, however these should never fail. However, as
1053 this sanity check has been added here they have failed. */
1054 if (!entry->nickname || !entry->username)
1057 /* Send WHOWAS reply */
1058 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1059 tmp = silc_argument_get_first_arg(cmd->args, NULL);
1061 memset(uh, 0, sizeof(uh));
1062 memset(nh, 0, sizeof(nh));
1064 strncat(nh, entry->nickname, strlen(entry->nickname));
1065 if (!strchr(entry->nickname, '@')) {
1066 strncat(nh, "@", 1);
1067 len = entry->router ? strlen(entry->router->server_name) :
1068 strlen(server->server_name);
1069 strncat(nh, entry->router ? entry->router->server_name :
1070 server->server_name, len);
1073 strncat(uh, entry->username, strlen(entry->username));
1074 if (!strchr(entry->username, '@')) {
1075 strncat(uh, "@", 1);
1076 strcat(uh, "*private*");
1079 if (entry->userinfo)
1081 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1083 2, idp->data, idp->len,
1087 strlen(entry->userinfo));
1090 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1092 2, idp->data, idp->len,
1096 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1097 0, packet->data, packet->len, FALSE);
1099 silc_buffer_free(packet);
1100 silc_buffer_free(idp);
1103 if (found == FALSE && entry)
1104 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1105 SILC_STATUS_ERR_NO_SUCH_NICK,
1107 strlen(entry->nickname));
1111 silc_server_command_whowas_from_client(SilcServerCommandContext cmd)
1113 SilcServer server = cmd->server;
1114 char *nick = NULL, *server_name = NULL;
1116 SilcClientEntry *clients = NULL;
1117 uint32 clients_count = 0;
1120 /* Protocol dictates that we must always send the received WHOWAS request
1121 to our router if we are normal server, so let's do it now unless we
1122 are standalone. We will not send any replies to the client until we
1123 have received reply from the router. */
1124 if (server->server_type == SILC_SERVER &&
1125 !cmd->pending && !server->standalone) {
1129 old_ident = silc_command_get_ident(cmd->payload);
1130 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1131 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1133 /* Send WHOWAS command to our router */
1134 silc_server_packet_send(server, (SilcSocketConnection)
1135 server->router->connection,
1136 SILC_PACKET_COMMAND, cmd->packet->flags,
1137 tmpbuf->data, tmpbuf->len, TRUE);
1139 /* Reprocess this packet after received reply from router */
1140 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
1141 silc_command_get_ident(cmd->payload),
1142 silc_server_command_destructor,
1143 silc_server_command_whowas,
1144 silc_server_command_dup(cmd));
1145 cmd->pending = TRUE;
1147 silc_command_set_ident(cmd->payload, old_ident);
1149 silc_buffer_free(tmpbuf);
1154 /* We are ready to process the command request. Let's search for the
1155 requested client and send reply to the requesting client. */
1157 /* Parse the whowas request */
1158 if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1161 /* Get all clients matching that nickname from local list */
1162 if (!silc_idlist_get_clients_by_nickname(server->local_list,
1164 &clients, &clients_count))
1165 silc_idlist_get_clients_by_hash(server->local_list,
1166 nick, server->md5hash,
1167 &clients, &clients_count);
1169 /* Check global list as well */
1170 if (!silc_idlist_get_clients_by_nickname(server->global_list,
1172 &clients, &clients_count))
1173 silc_idlist_get_clients_by_hash(server->global_list,
1174 nick, server->md5hash,
1175 &clients, &clients_count);
1177 if (!silc_server_command_whowas_check(cmd, clients, clients_count)) {
1182 /* Send the command reply to the client */
1183 silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1191 silc_free(server_name);
1197 silc_server_command_whowas_from_server(SilcServerCommandContext cmd)
1199 SilcServer server = cmd->server;
1200 char *nick = NULL, *server_name = NULL;
1202 SilcClientEntry *clients = NULL;
1203 uint32 clients_count = 0;
1206 /* Parse the whowas request */
1207 if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1210 /* Process the command request. Let's search for the requested client and
1211 send reply to the requesting server. */
1213 if (!silc_idlist_get_clients_by_nickname(server->local_list,
1215 &clients, &clients_count))
1216 silc_idlist_get_clients_by_hash(server->local_list,
1217 nick, server->md5hash,
1218 &clients, &clients_count);
1220 /* If we are router we will check our global list as well. */
1221 if (server->server_type == SILC_ROUTER) {
1222 if (!silc_idlist_get_clients_by_nickname(server->global_list,
1224 &clients, &clients_count))
1225 silc_idlist_get_clients_by_hash(server->global_list,
1226 nick, server->md5hash,
1227 &clients, &clients_count);
1231 /* Such a client really does not exist in the SILC network. */
1232 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1233 SILC_STATUS_ERR_NO_SUCH_NICK,
1234 3, nick, strlen(nick));
1238 /* Send the command reply to the client */
1239 silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1247 silc_free(server_name);
1252 /* Server side of command WHOWAS. */
1254 SILC_SERVER_CMD_FUNC(whowas)
1256 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1259 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_WHOWAS, cmd, 1, 2);
1261 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1262 ret = silc_server_command_whowas_from_client(cmd);
1263 else if ((cmd->sock->type == SILC_SOCKET_TYPE_SERVER) ||
1264 (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER))
1265 ret = silc_server_command_whowas_from_server(cmd);
1268 silc_server_command_free(cmd);
1271 /******************************************************************************
1275 ******************************************************************************/
1277 /* Checks that all mandatory fields are present. If not then send WHOIS
1278 request to the server who owns the client. We use WHOIS because we want
1279 to get as much information as possible at once. */
1282 silc_server_command_identify_check(SilcServerCommandContext cmd,
1283 SilcClientEntry *clients,
1284 uint32 clients_count)
1286 SilcServer server = cmd->server;
1288 SilcClientEntry entry;
1290 for (i = 0; i < clients_count; i++) {
1293 if (!entry || entry->data.registered == FALSE)
1296 if (!entry->nickname) {
1303 old_ident = silc_command_get_ident(cmd->payload);
1304 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1305 silc_command_set_command(cmd->payload, SILC_COMMAND_WHOIS);
1306 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1308 /* Send WHOIS request. We send WHOIS since we're doing the requesting
1309 now anyway so make it a good one. */
1310 silc_server_packet_send(server, entry->router->connection,
1311 SILC_PACKET_COMMAND, cmd->packet->flags,
1312 tmpbuf->data, tmpbuf->len, TRUE);
1314 /* Reprocess this packet after received reply */
1315 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1316 silc_command_get_ident(cmd->payload),
1317 silc_server_command_destructor,
1318 silc_server_command_identify,
1319 silc_server_command_dup(cmd));
1321 cmd->pending = TRUE;
1323 /* Put old data back to the Command Payload we just changed */
1324 silc_command_set_ident(cmd->payload, old_ident);
1325 silc_command_set_command(cmd->payload, SILC_COMMAND_IDENTIFY);
1327 silc_buffer_free(tmpbuf);
1336 silc_server_command_identify_send_reply(SilcServerCommandContext cmd,
1337 SilcClientEntry *clients,
1338 uint32 clients_count,
1341 SilcServer server = cmd->server;
1344 SilcBuffer packet, idp;
1345 SilcClientEntry entry;
1346 SilcCommandStatus status;
1347 uint16 ident = silc_command_get_ident(cmd->payload);
1348 char nh[256], uh[256];
1349 SilcSocketConnection hsock;
1352 for (i = 0; i < clients_count; i++)
1353 if (clients[i]->data.registered)
1356 status = SILC_STATUS_OK;
1358 status = SILC_STATUS_LIST_START;
1360 for (i = 0, k = 0; i < clients_count; i++) {
1363 if (entry->data.registered == FALSE) {
1364 if (clients_count == 1) {
1365 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1366 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1367 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1368 2, idp->data, idp->len);
1369 silc_buffer_free(idp);
1375 status = SILC_STATUS_LIST_ITEM;
1377 if (clients_count > 1 && k == clients_count - 1)
1378 status = SILC_STATUS_LIST_END;
1380 if (count && k - 1 == count)
1381 status = SILC_STATUS_LIST_END;
1383 if (count && k - 1 > count)
1386 /* Send IDENTIFY reply */
1387 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1388 tmp = silc_argument_get_first_arg(cmd->args, NULL);
1390 memset(uh, 0, sizeof(uh));
1391 memset(nh, 0, sizeof(nh));
1393 strncat(nh, entry->nickname, strlen(entry->nickname));
1394 if (!strchr(entry->nickname, '@')) {
1395 strncat(nh, "@", 1);
1396 len = entry->router ? strlen(entry->router->server_name) :
1397 strlen(server->server_name);
1398 strncat(nh, entry->router ? entry->router->server_name :
1399 server->server_name, len);
1402 if (!entry->username) {
1403 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1405 2, idp->data, idp->len,
1408 strncat(uh, entry->username, strlen(entry->username));
1409 if (!strchr(entry->username, '@')) {
1410 strncat(uh, "@", 1);
1411 hsock = (SilcSocketConnection)entry->connection;
1412 len = strlen(hsock->hostname);
1413 strncat(uh, hsock->hostname, len);
1416 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1418 2, idp->data, idp->len,
1423 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1424 0, packet->data, packet->len, FALSE);
1426 silc_buffer_free(packet);
1427 silc_buffer_free(idp);
1434 silc_server_command_identify_from_client(SilcServerCommandContext cmd)
1436 SilcServer server = cmd->server;
1437 char *nick = NULL, *server_name = NULL;
1439 SilcClientEntry *clients = NULL, entry;
1440 SilcClientID **client_id = NULL;
1441 uint32 client_id_count = 0, clients_count = 0;
1444 /* Protocol dictates that we must always send the received IDENTIFY request
1445 to our router if we are normal server, so let's do it now unless we
1446 are standalone. We will not send any replies to the client until we
1447 have received reply from the router. */
1448 if (server->server_type == SILC_SERVER &&
1449 !cmd->pending && !server->standalone) {
1453 old_ident = silc_command_get_ident(cmd->payload);
1454 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1455 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1457 /* Send IDENTIFY command to our router */
1458 silc_server_packet_send(server, (SilcSocketConnection)
1459 server->router->connection,
1460 SILC_PACKET_COMMAND, cmd->packet->flags,
1461 tmpbuf->data, tmpbuf->len, TRUE);
1463 /* Reprocess this packet after received reply from router */
1464 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
1465 silc_command_get_ident(cmd->payload),
1466 silc_server_command_destructor,
1467 silc_server_command_identify,
1468 silc_server_command_dup(cmd));
1469 cmd->pending = TRUE;
1471 silc_command_set_ident(cmd->payload, old_ident);
1473 silc_buffer_free(tmpbuf);
1478 /* We are ready to process the command request. Let's search for the
1479 requested client and send reply to the requesting client. */
1481 /* Parse the IDENTIFY request */
1482 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
1483 &nick, &server_name, &count,
1484 SILC_COMMAND_IDENTIFY))
1487 /* Get all clients matching that ID or nickname from local list */
1488 if (client_id_count) {
1489 /* Check all Client ID's received in the command packet */
1490 for (i = 0; i < client_id_count; i++) {
1491 entry = silc_idlist_find_client_by_id(server->local_list,
1492 client_id[i], NULL);
1494 clients = silc_realloc(clients, sizeof(*clients) *
1495 (clients_count + 1));
1496 clients[clients_count++] = entry;
1500 if (!silc_idlist_get_clients_by_hash(server->local_list,
1501 nick, server->md5hash,
1502 &clients, &clients_count))
1503 silc_idlist_get_clients_by_nickname(server->local_list,
1505 &clients, &clients_count);
1508 /* Check global list as well */
1509 if (client_id_count) {
1510 /* Check all Client ID's received in the command packet */
1511 for (i = 0; i < client_id_count; i++) {
1512 entry = silc_idlist_find_client_by_id(server->global_list,
1513 client_id[i], NULL);
1515 clients = silc_realloc(clients, sizeof(*clients) *
1516 (clients_count + 1));
1517 clients[clients_count++] = entry;
1521 if (!silc_idlist_get_clients_by_hash(server->global_list,
1522 nick, server->md5hash,
1523 &clients, &clients_count))
1524 silc_idlist_get_clients_by_nickname(server->global_list,
1526 &clients, &clients_count);
1530 /* Such a client really does not exist in the SILC network. */
1531 if (!client_id_count) {
1532 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1533 SILC_STATUS_ERR_NO_SUCH_NICK,
1534 3, nick, strlen(nick));
1536 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
1537 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1538 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1539 2, idp->data, idp->len);
1540 silc_buffer_free(idp);
1545 /* Check that all mandatory fields are present and request those data
1546 from the server who owns the client if necessary. */
1547 if (!silc_server_command_identify_check(cmd, clients, clients_count)) {
1552 /* Send the command reply to the client */
1553 silc_server_command_identify_send_reply(cmd, clients, clients_count,
1557 if (client_id_count) {
1558 for (i = 0; i < client_id_count; i++)
1559 silc_free(client_id[i]);
1560 silc_free(client_id);
1567 silc_free(server_name);
1573 silc_server_command_identify_from_server(SilcServerCommandContext cmd)
1575 SilcServer server = cmd->server;
1576 char *nick = NULL, *server_name = NULL;
1578 SilcClientEntry *clients = NULL, entry;
1579 SilcClientID **client_id = NULL;
1580 uint32 client_id_count = 0, clients_count = 0;
1583 /* Parse the IDENTIFY request */
1584 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
1585 &nick, &server_name, &count,
1586 SILC_COMMAND_IDENTIFY))
1589 /* Process the command request. Let's search for the requested client and
1590 send reply to the requesting server. */
1592 if (client_id_count) {
1593 /* Check all Client ID's received in the command packet */
1594 for (i = 0; i < client_id_count; i++) {
1595 entry = silc_idlist_find_client_by_id(server->local_list,
1596 client_id[i], NULL);
1598 clients = silc_realloc(clients, sizeof(*clients) *
1599 (clients_count + 1));
1600 clients[clients_count++] = entry;
1604 if (!silc_idlist_get_clients_by_hash(server->local_list,
1605 nick, server->md5hash,
1606 &clients, &clients_count))
1607 silc_idlist_get_clients_by_nickname(server->local_list,
1609 &clients, &clients_count);
1612 /* If we are router we will check our global list as well. */
1613 if (server->server_type == SILC_ROUTER) {
1614 if (client_id_count) {
1615 /* Check all Client ID's received in the command packet */
1616 for (i = 0; i < client_id_count; i++) {
1617 entry = silc_idlist_find_client_by_id(server->global_list,
1618 client_id[i], NULL);
1620 clients = silc_realloc(clients, sizeof(*clients) *
1621 (clients_count + 1));
1622 clients[clients_count++] = entry;
1626 if (!silc_idlist_get_clients_by_hash(server->global_list,
1627 nick, server->md5hash,
1628 &clients, &clients_count))
1629 silc_idlist_get_clients_by_nickname(server->global_list,
1631 &clients, &clients_count);
1636 /* Such a client really does not exist in the SILC network. */
1637 if (!client_id_count) {
1638 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1639 SILC_STATUS_ERR_NO_SUCH_NICK,
1640 3, nick, strlen(nick));
1642 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
1643 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1644 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1645 2, idp->data, idp->len);
1646 silc_buffer_free(idp);
1651 /* Check that all mandatory fields are present and request those data
1652 from the server who owns the client if necessary. */
1653 if (!silc_server_command_identify_check(cmd, clients, clients_count)) {
1658 /* Send the command reply */
1659 silc_server_command_identify_send_reply(cmd, clients, clients_count, count);
1662 if (client_id_count) {
1663 for (i = 0; i < client_id_count; i++)
1664 silc_free(client_id[i]);
1665 silc_free(client_id);
1672 silc_free(server_name);
1677 SILC_SERVER_CMD_FUNC(identify)
1679 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1682 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_IDENTIFY, cmd, 1, 3328);
1684 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1685 ret = silc_server_command_identify_from_client(cmd);
1686 else if ((cmd->sock->type == SILC_SOCKET_TYPE_SERVER) |
1687 (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER))
1688 ret = silc_server_command_identify_from_server(cmd);
1691 silc_server_command_free(cmd);
1694 /* Checks string for bad characters and returns TRUE if they are found. */
1696 static int silc_server_command_bad_chars(char *nick)
1698 if (strchr(nick, '\\')) return TRUE;
1699 if (strchr(nick, '\"')) return TRUE;
1700 if (strchr(nick, '´')) return TRUE;
1701 if (strchr(nick, '`')) return TRUE;
1702 if (strchr(nick, '\'')) return TRUE;
1703 if (strchr(nick, '*')) return TRUE;
1704 if (strchr(nick, '/')) return TRUE;
1705 if (strchr(nick, '@')) return TRUE;
1710 /* Server side of command NICK. Sets nickname for user. Setting
1711 nickname causes generation of a new client ID for the client. The
1712 new client ID is sent to the client after changing the nickname. */
1714 SILC_SERVER_CMD_FUNC(nick)
1716 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1717 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1718 SilcServer server = cmd->server;
1719 SilcBuffer packet, nidp, oidp;
1720 SilcClientID *new_id;
1722 uint16 ident = silc_command_get_ident(cmd->payload);
1724 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1727 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_NICK, cmd, 1, 1);
1729 /* Check nickname */
1730 nick = silc_argument_get_arg_type(cmd->args, 1, NULL);
1731 if (silc_server_command_bad_chars(nick) == TRUE) {
1732 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
1733 SILC_STATUS_ERR_BAD_NICKNAME);
1737 if (strlen(nick) > 128)
1740 /* Create new Client ID */
1741 silc_id_create_client_id(cmd->server->id, cmd->server->rng,
1742 cmd->server->md5hash, nick,
1745 /* Send notify about nickname change to our router. We send the new
1746 ID and ask to replace it with the old one. If we are router the
1747 packet is broadcasted. Send NICK_CHANGE notify. */
1748 if (!server->standalone)
1749 silc_server_send_notify_nick_change(server, server->router->connection,
1750 server->server_type == SILC_SERVER ?
1751 FALSE : TRUE, client->id,
1752 new_id, SILC_ID_CLIENT_LEN);
1754 /* Remove old cache entry */
1755 silc_idcache_del_by_id(server->local_list->clients, SILC_ID_CLIENT,
1758 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1762 memset(client->id, 0, SILC_ID_CLIENT_LEN);
1763 silc_free(client->id);
1766 /* Save the nickname as this client is our local client */
1767 if (client->nickname)
1768 silc_free(client->nickname);
1770 client->nickname = strdup(nick);
1771 client->id = new_id;
1773 /* Update client cache */
1774 silc_idcache_add(server->local_list->clients, client->nickname,
1775 strlen(client->nickname), SILC_ID_CLIENT, client->id,
1776 (void *)client, TRUE, FALSE);
1778 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1780 /* Send NICK_CHANGE notify to the client's channels */
1781 silc_server_send_notify_on_channels(server, NULL, client,
1782 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
1783 oidp->data, oidp->len,
1784 nidp->data, nidp->len);
1786 /* Send the new Client ID as reply command back to client */
1787 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
1788 SILC_STATUS_OK, ident, 1,
1789 2, nidp->data, nidp->len);
1790 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1791 0, packet->data, packet->len, FALSE);
1793 silc_buffer_free(packet);
1794 silc_buffer_free(nidp);
1795 silc_buffer_free(oidp);
1798 silc_server_command_free(cmd);
1801 /* Sends the LIST command reply */
1804 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
1805 SilcChannelEntry *lch,
1807 SilcChannelEntry *gch,
1811 SilcBuffer packet, idp;
1812 SilcChannelEntry entry;
1813 SilcCommandStatus status;
1814 uint16 ident = silc_command_get_ident(cmd->payload);
1816 unsigned char usercount[4];
1819 for (i = 0; i < lch_count; i++)
1820 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1822 for (i = 0; i < gch_count; i++)
1823 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1826 status = SILC_STATUS_OK;
1827 if ((lch_count + gch_count) > 1)
1828 status = SILC_STATUS_LIST_START;
1831 for (i = 0; i < lch_count; i++) {
1838 status = SILC_STATUS_LIST_ITEM;
1840 if (i == lch_count - 1 && gch_count)
1842 if (lch_count > 1 && i == lch_count - 1)
1843 status = SILC_STATUS_LIST_END;
1845 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1847 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1848 topic = "*private*";
1849 memset(usercount, 0, sizeof(usercount));
1851 topic = entry->topic;
1852 users = silc_list_count(entry->user_list);
1853 SILC_PUT32_MSB(users, usercount);
1856 /* Send the reply */
1859 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1861 2, idp->data, idp->len,
1862 3, entry->channel_name,
1863 strlen(entry->channel_name),
1864 4, topic, strlen(topic),
1868 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1870 2, idp->data, idp->len,
1871 3, entry->channel_name,
1872 strlen(entry->channel_name),
1874 silc_server_packet_send(cmd->server, cmd->sock,
1875 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1876 packet->len, FALSE);
1877 silc_buffer_free(packet);
1878 silc_buffer_free(idp);
1881 status = i ? SILC_STATUS_LIST_ITEM : SILC_STATUS_OK;
1884 for (i = 0; i < gch_count; i++) {
1891 status = SILC_STATUS_LIST_ITEM;
1893 if (gch_count > 1 && i == lch_count - 1)
1894 status = SILC_STATUS_LIST_END;
1896 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1898 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1899 topic = "*private*";
1900 memset(usercount, 0, sizeof(usercount));
1902 topic = entry->topic;
1903 users = silc_list_count(entry->user_list);
1904 SILC_PUT32_MSB(users, usercount);
1907 /* Send the reply */
1910 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1912 2, idp->data, idp->len,
1913 3, entry->channel_name,
1914 strlen(entry->channel_name),
1915 4, topic, strlen(topic),
1919 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1921 2, idp->data, idp->len,
1922 3, entry->channel_name,
1923 strlen(entry->channel_name),
1925 silc_server_packet_send(cmd->server, cmd->sock,
1926 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1927 packet->len, FALSE);
1928 silc_buffer_free(packet);
1929 silc_buffer_free(idp);
1933 /* Server side of LIST command. This lists the channel of the requested
1934 server. Secret channels are not listed. */
1936 SILC_SERVER_CMD_FUNC(list)
1938 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1939 SilcServer server = cmd->server;
1940 SilcChannelID *channel_id = NULL;
1943 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
1944 uint32 lch_count = 0, gch_count = 0;
1946 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_LIST, cmd, 0, 2);
1948 /* Get Channel ID */
1949 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1951 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1953 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
1954 SILC_STATUS_ERR_NO_CHANNEL_ID);
1959 /* Get the channels from local list */
1960 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
1963 /* Get the channels from global list if we are router */
1964 if (server->server_type == SILC_ROUTER)
1965 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
1968 /* Send the reply */
1969 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
1970 gchannels, gch_count);
1973 silc_server_command_free(cmd);
1976 /* Server side of TOPIC command. Sets topic for channel and/or returns
1977 current topic to client. */
1979 SILC_SERVER_CMD_FUNC(topic)
1981 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1982 SilcServer server = cmd->server;
1983 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1984 SilcChannelID *channel_id;
1985 SilcChannelEntry channel;
1986 SilcChannelClientEntry chl;
1987 SilcBuffer packet, idp;
1989 uint32 argc, tmp_len;
1990 uint16 ident = silc_command_get_ident(cmd->payload);
1992 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_TOPIC, cmd, 1, 2);
1994 argc = silc_argument_get_arg_num(cmd->args);
1996 /* Get Channel ID */
1997 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1999 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2000 SILC_STATUS_ERR_NO_CHANNEL_ID);
2003 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2005 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2006 SILC_STATUS_ERR_NO_CHANNEL_ID);
2010 /* Check whether the channel exists */
2011 channel = silc_idlist_find_channel_by_id(server->local_list,
2014 channel = silc_idlist_find_channel_by_id(server->global_list,
2017 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2018 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2025 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
2027 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2028 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2032 if (strlen(tmp) > 256) {
2033 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2034 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2038 /* See whether has rights to change topic */
2039 silc_list_start(channel->user_list);
2040 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
2041 if (chl->client == client)
2044 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2045 if (channel->mode & SILC_CHANNEL_MODE_TOPIC) {
2046 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2047 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2052 /* Set the topic for channel */
2054 silc_free(channel->topic);
2055 channel->topic = strdup(tmp);
2057 /* Send TOPIC_SET notify type to the network */
2058 if (!server->standalone)
2059 silc_server_send_notify_topic_set(server, server->router->connection,
2060 server->server_type == SILC_ROUTER ?
2061 TRUE : FALSE, channel, client->id,
2062 SILC_ID_CLIENT_LEN, channel->topic);
2064 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2066 /* Send notify about topic change to all clients on the channel */
2067 silc_server_send_notify_to_channel(server, NULL, channel, TRUE,
2068 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
2069 idp->data, idp->len,
2070 channel->topic, strlen(channel->topic));
2071 silc_buffer_free(idp);
2074 /* Send the topic to client as reply packet */
2075 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2077 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2078 SILC_STATUS_OK, ident, 2,
2079 2, idp->data, idp->len,
2081 strlen(channel->topic));
2083 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2084 SILC_STATUS_OK, ident, 1,
2085 2, idp->data, idp->len);
2086 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2087 0, packet->data, packet->len, FALSE);
2089 silc_buffer_free(packet);
2090 silc_buffer_free(idp);
2091 silc_free(channel_id);
2094 silc_server_command_free(cmd);
2097 /* Server side of INVITE command. Invites some client to join some channel.
2098 This command is also used to manage the invite list of the channel. */
2100 SILC_SERVER_CMD_FUNC(invite)
2102 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2103 SilcServer server = cmd->server;
2104 SilcSocketConnection sock = cmd->sock, dest_sock;
2105 SilcChannelClientEntry chl;
2106 SilcClientEntry sender, dest;
2107 SilcClientID *dest_id = NULL;
2108 SilcChannelEntry channel;
2109 SilcChannelID *channel_id = NULL;
2110 SilcIDListData idata;
2111 SilcBuffer idp, idp2, packet;
2112 unsigned char *tmp, *add, *del;
2114 uint16 ident = silc_command_get_ident(cmd->payload);
2116 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_INVITE, cmd, 1, 4);
2118 /* Get Channel ID */
2119 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2121 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2122 SILC_STATUS_ERR_NO_CHANNEL_ID);
2125 channel_id = silc_id_payload_parse_id(tmp, len);
2127 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2128 SILC_STATUS_ERR_NO_CHANNEL_ID);
2132 /* Get the channel entry */
2133 channel = silc_idlist_find_channel_by_id(server->local_list,
2136 channel = silc_idlist_find_channel_by_id(server->global_list,
2139 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2140 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2145 /* Check whether the sender of this command is on the channel. */
2146 sender = (SilcClientEntry)sock->user_data;
2147 if (!silc_server_client_on_channel(sender, channel)) {
2148 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2149 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2153 /* Check whether the channel is invite-only channel. If yes then the
2154 sender of this command must be at least channel operator. */
2155 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2156 silc_list_start(channel->user_list);
2157 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
2158 if (chl->client == sender) {
2159 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2160 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2161 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2168 /* Get destination client ID */
2169 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
2173 dest_id = silc_id_payload_parse_id(tmp, len);
2175 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2176 SILC_STATUS_ERR_NO_CLIENT_ID);
2180 /* Get the client entry */
2181 dest = silc_server_get_client_resolve(server, dest_id);
2183 if (server->server_type == SILC_ROUTER) {
2184 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2185 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2189 /* The client info is being resolved. Reprocess this packet after
2190 receiving the reply to the query. */
2191 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2193 silc_server_command_destructor,
2194 silc_server_command_invite,
2195 silc_server_command_dup(cmd));
2196 cmd->pending = TRUE;
2197 silc_free(channel_id);
2202 /* Check whether the requested client is already on the channel. */
2203 if (silc_server_client_on_channel(dest, channel)) {
2204 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2205 SILC_STATUS_ERR_USER_ON_CHANNEL);
2209 /* Get route to the client */
2210 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id, &idata);
2212 memset(invite, 0, sizeof(invite));
2213 strncat(invite, dest->nickname, strlen(dest->nickname));
2214 strncat(invite, "!", 1);
2215 strncat(invite, dest->username, strlen(dest->username));
2216 if (!strchr(dest->username, '@')) {
2217 strncat(invite, "@", 1);
2218 strncat(invite, cmd->sock->hostname, strlen(cmd->sock->hostname));
2221 len = strlen(invite);
2222 if (!channel->invite_list)
2223 channel->invite_list = silc_calloc(len + 2,
2224 sizeof(*channel->invite_list));
2226 channel->invite_list = silc_realloc(channel->invite_list,
2227 sizeof(*channel->invite_list) *
2229 strlen(channel->invite_list) + 2));
2230 strncat(channel->invite_list, invite, len);
2231 strncat(channel->invite_list, ",", 1);
2233 /* Send notify to the client that is invited to the channel */
2234 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2235 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
2236 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
2238 SILC_NOTIFY_TYPE_INVITE, 3,
2239 idp->data, idp->len,
2240 channel->channel_name,
2241 strlen(channel->channel_name),
2242 idp2->data, idp2->len);
2243 silc_buffer_free(idp);
2244 silc_buffer_free(idp2);
2247 /* Add the client to the invite list of the channel */
2248 add = silc_argument_get_arg_type(cmd->args, 3, &len);
2250 if (!channel->invite_list)
2251 channel->invite_list = silc_calloc(len + 2,
2252 sizeof(*channel->invite_list));
2254 channel->invite_list = silc_realloc(channel->invite_list,
2255 sizeof(*channel->invite_list) *
2257 strlen(channel->invite_list) + 2));
2258 if (add[len - 1] == ',')
2259 add[len - 1] = '\0';
2261 strncat(channel->invite_list, add, len);
2262 strncat(channel->invite_list, ",", 1);
2265 /* Get the invite to be removed and remove it from the list */
2266 del = silc_argument_get_arg_type(cmd->args, 4, &len);
2267 if (del && channel->invite_list) {
2268 char *start, *end, *n;
2270 if (!strncmp(channel->invite_list, del,
2271 strlen(channel->invite_list) - 1)) {
2272 silc_free(channel->invite_list);
2273 channel->invite_list = NULL;
2275 start = strstr(channel->invite_list, del);
2276 if (start && strlen(start) >= len) {
2278 n = silc_calloc(strlen(channel->invite_list) - len, sizeof(*n));
2279 strncat(n, channel->invite_list, start - channel->invite_list);
2280 strncat(n, end + 1, ((channel->invite_list +
2281 strlen(channel->invite_list)) - end) - 1);
2282 silc_free(channel->invite_list);
2283 channel->invite_list = n;
2288 /* Send notify to the primary router */
2289 if (!server->standalone)
2290 silc_server_send_notify_invite(server, server->router->connection,
2291 server->server_type == SILC_ROUTER ?
2292 TRUE : FALSE, channel,
2293 sender->id, SILC_ID_CLIENT_LEN,
2296 /* Send command reply */
2297 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2298 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2299 SILC_STATUS_OK, ident, 2,
2301 3, channel->invite_list,
2302 channel->invite_list ?
2303 strlen(channel->invite_list) :
2305 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2306 packet->data, packet->len, FALSE);
2307 silc_buffer_free(packet);
2313 silc_free(channel_id);
2314 silc_server_command_free(cmd);
2319 SilcSocketConnection sock;
2323 /* Quits connection to client. This gets called if client won't
2324 close the connection even when it has issued QUIT command. */
2326 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
2328 QuitInternal q = (QuitInternal)context;
2330 /* Free all client specific data, such as client entry and entires
2331 on channels this client may be on. */
2332 silc_server_free_client_data(q->server, q->sock, q->sock->user_data,
2334 q->sock->user_data = NULL;
2336 /* Close the connection on our side */
2337 silc_server_close_connection(q->server, q->sock);
2339 silc_free(q->signoff);
2343 /* Quits SILC session. This is the normal way to disconnect client. */
2345 SILC_SERVER_CMD_FUNC(quit)
2347 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2348 SilcServer server = cmd->server;
2349 SilcSocketConnection sock = cmd->sock;
2351 unsigned char *tmp = NULL;
2354 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_QUIT, cmd, 0, 1);
2356 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2359 /* Get destination ID */
2360 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2364 q = silc_calloc(1, sizeof(*q));
2367 q->signoff = tmp ? strdup(tmp) : NULL;
2369 /* We quit the connection with little timeout */
2370 silc_task_register(server->timeout_queue, sock->sock,
2371 silc_server_command_quit_cb, (void *)q,
2372 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2375 silc_server_command_free(cmd);
2378 /* Server side of command KILL. This command is used by router operator
2379 to remove an client from the SILC Network temporarily. */
2381 SILC_SERVER_CMD_FUNC(kill)
2383 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2384 SilcServer server = cmd->server;
2385 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2386 SilcClientEntry remote_client;
2387 SilcClientID *client_id;
2388 unsigned char *tmp, *comment;
2389 uint32 tmp_len, tmp_len2;
2391 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_KILL, cmd, 1, 2);
2393 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2396 /* KILL command works only on router */
2397 if (server->server_type != SILC_ROUTER) {
2398 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2399 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2403 /* Check whether client has the permissions. */
2404 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2405 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2406 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2410 /* Get the client ID */
2411 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2413 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2414 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2417 client_id = silc_id_payload_parse_id(tmp, tmp_len);
2419 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2420 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2424 /* Get the client entry */
2425 remote_client = silc_idlist_find_client_by_id(server->local_list,
2427 if (!remote_client) {
2428 remote_client = silc_idlist_find_client_by_id(server->global_list,
2430 if (!remote_client) {
2431 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2432 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2438 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
2442 /* Send reply to the sender */
2443 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2446 /* Send the KILL notify packets. First send it to the channel, then
2447 to our primary router and then directly to the client who is being
2448 killed right now. */
2450 /* Send KILLED notify to the channels. It is not sent to the client
2451 as it will be sent differently destined directly to the client and not
2453 silc_server_send_notify_on_channels(server, remote_client,
2454 remote_client, SILC_NOTIFY_TYPE_KILLED,
2457 comment, comment ? tmp_len2 : 0);
2459 /* Send KILLED notify to primary route */
2460 if (!server->standalone)
2461 silc_server_send_notify_killed(server, server->router->connection, TRUE,
2462 remote_client->id, SILC_ID_CLIENT_LEN,
2465 /* Send KILLED notify to the client directly */
2466 silc_server_send_notify_killed(server, remote_client->connection ?
2467 remote_client->connection :
2468 remote_client->router->connection, FALSE,
2469 remote_client->id, SILC_ID_CLIENT_LEN,
2472 /* Remove the client from all channels. This generates new keys to the
2473 channels as well. */
2474 silc_server_remove_from_channels(server, NULL, remote_client, FALSE,
2477 /* Remove the client entry, If it is locally connected then we will also
2478 disconnect the client here */
2479 if (remote_client->data.registered && remote_client->connection) {
2480 /* Remove locally conneted client */
2481 SilcSocketConnection sock = remote_client->connection;
2482 silc_server_free_client_data(server, sock, remote_client, FALSE, NULL);
2483 silc_server_close_connection(server, sock);
2485 /* Remove remote client */
2486 if (!silc_idlist_del_client(server->global_list, remote_client))
2487 silc_idlist_del_client(server->local_list, remote_client);
2491 silc_server_command_free(cmd);
2494 /* Server side of command INFO. This sends information about us to
2495 the client. If client requested specific server we will send the
2496 command to that server. */
2498 SILC_SERVER_CMD_FUNC(info)
2500 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2501 SilcServer server = cmd->server;
2502 SilcBuffer packet, idp;
2505 char *dest_server, *server_info = NULL, *server_name;
2506 uint16 ident = silc_command_get_ident(cmd->payload);
2507 SilcServerEntry entry = NULL;
2508 SilcServerID *server_id = NULL;
2510 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_INFO, cmd, 0, 2);
2512 /* Get server name */
2513 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2516 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2518 server_id = silc_id_payload_parse_id(tmp, tmp_len);
2520 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2521 SILC_STATUS_ERR_NO_SERVER_ID);
2527 /* Check whether we have this server cached */
2528 entry = silc_idlist_find_server_by_id(server->local_list,
2531 entry = silc_idlist_find_server_by_id(server->global_list,
2533 if (!entry && server->server_type == SILC_ROUTER) {
2534 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2535 SILC_STATUS_ERR_NO_SUCH_SERVER);
2541 if ((!dest_server && !server_id) ||
2542 (dest_server && !cmd->pending &&
2543 !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
2544 /* Send our reply */
2545 char info_string[256];
2547 memset(info_string, 0, sizeof(info_string));
2548 snprintf(info_string, sizeof(info_string),
2549 "location: %s server: %s admin: %s <%s>",
2550 server->config->admin_info->location,
2551 server->config->admin_info->server_type,
2552 server->config->admin_info->admin_name,
2553 server->config->admin_info->admin_email);
2555 server_info = info_string;
2556 entry = server->id_entry;
2558 /* Check whether we have this server cached */
2559 if (!entry && dest_server) {
2560 entry = silc_idlist_find_server_by_name(server->global_list,
2563 entry = silc_idlist_find_server_by_name(server->local_list,
2568 if (!cmd->pending &&
2569 server->server_type == SILC_ROUTER && entry && !entry->server_info) {
2570 /* Send to the server */
2574 old_ident = silc_command_get_ident(cmd->payload);
2575 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2576 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2578 silc_server_packet_send(server, entry->connection,
2579 SILC_PACKET_COMMAND, cmd->packet->flags,
2580 tmpbuf->data, tmpbuf->len, TRUE);
2582 /* Reprocess this packet after received reply from router */
2583 silc_server_command_pending(server, SILC_COMMAND_INFO,
2584 silc_command_get_ident(cmd->payload),
2585 silc_server_command_destructor,
2586 silc_server_command_info,
2587 silc_server_command_dup(cmd));
2588 cmd->pending = TRUE;
2589 silc_command_set_ident(cmd->payload, old_ident);
2590 silc_buffer_free(tmpbuf);
2594 if (!entry && !cmd->pending && !server->standalone) {
2595 /* Send to the primary router */
2599 old_ident = silc_command_get_ident(cmd->payload);
2600 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2601 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2603 silc_server_packet_send(server, server->router->connection,
2604 SILC_PACKET_COMMAND, cmd->packet->flags,
2605 tmpbuf->data, tmpbuf->len, TRUE);
2607 /* Reprocess this packet after received reply from router */
2608 silc_server_command_pending(server, SILC_COMMAND_INFO,
2609 silc_command_get_ident(cmd->payload),
2610 silc_server_command_destructor,
2611 silc_server_command_info,
2612 silc_server_command_dup(cmd));
2613 cmd->pending = TRUE;
2614 silc_command_set_ident(cmd->payload, old_ident);
2615 silc_buffer_free(tmpbuf);
2621 silc_free(server_id);
2624 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2625 SILC_STATUS_ERR_NO_SUCH_SERVER);
2629 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2631 server_info = entry->server_info;
2632 server_name = entry->server_name;
2634 /* Send the reply */
2635 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2636 SILC_STATUS_OK, ident, 3,
2637 2, idp->data, idp->len,
2639 strlen(server_name),
2641 strlen(server_info));
2642 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2643 packet->data, packet->len, FALSE);
2645 silc_buffer_free(packet);
2646 silc_buffer_free(idp);
2649 silc_server_command_free(cmd);
2652 /* Server side of command PING. This just replies to the ping. */
2654 SILC_SERVER_CMD_FUNC(ping)
2656 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2657 SilcServer server = cmd->server;
2662 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_INFO, cmd, 1, 2);
2665 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2667 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2668 SILC_STATUS_ERR_NO_SERVER_ID);
2671 id = silc_id_str2id(tmp, len, SILC_ID_SERVER);
2675 if (!SILC_ID_SERVER_COMPARE(id, server->id)) {
2676 /* Send our reply */
2677 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2680 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2681 SILC_STATUS_ERR_NO_SUCH_SERVER);
2688 silc_server_command_free(cmd);
2691 /* Internal routine to join channel. The channel sent to this function
2692 has been either created or resolved from ID lists. This joins the sent
2693 client to the channel. */
2695 static void silc_server_command_join_channel(SilcServer server,
2696 SilcServerCommandContext cmd,
2697 SilcChannelEntry channel,
2698 SilcClientID *client_id,
2702 SilcSocketConnection sock = cmd->sock;
2704 uint32 tmp_len, user_count;
2705 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
2706 SilcClientEntry client;
2707 SilcChannelClientEntry chl;
2708 SilcBuffer reply, chidp, clidp, keyp = NULL, user_list, mode_list;
2709 uint16 ident = silc_command_get_ident(cmd->payload);
2712 SILC_LOG_DEBUG(("Start"));
2717 /* Get the client entry */
2718 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2719 client = (SilcClientEntry)sock->user_data;
2721 client = silc_idlist_find_client_by_id(server->local_list, client_id,
2728 * Check channel modes
2731 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2732 strncat(check, client->nickname, strlen(client->nickname));
2733 if (!strchr(client->nickname, '@')) {
2734 strncat(check, "@", 1);
2735 strncat(check, server->server_name, strlen(server->server_name));
2737 strncat(check, "!", 1);
2738 strncat(check, client->username, strlen(client->username));
2739 if (!strchr(client->username, '@')) {
2740 strncat(check, "@", 1);
2741 strncat(check, cmd->sock->hostname, strlen(cmd->sock->hostname));
2745 /* Check invite list if channel is invite-only channel */
2746 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
2747 channel->mode & SILC_CHANNEL_MODE_INVITE) {
2748 if (!channel->invite_list) {
2749 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2750 SILC_STATUS_ERR_NOT_INVITED);
2754 if (!silc_string_match(channel->invite_list, check)) {
2755 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2756 SILC_STATUS_ERR_NOT_INVITED);
2761 /* Check ban list if it exists. If the client's nickname, server,
2762 username and/or hostname is in the ban list the access to the
2763 channel is denied. */
2764 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT && channel->ban_list) {
2765 if (silc_string_match(channel->ban_list, check)) {
2766 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2767 SILC_STATUS_ERR_BANNED_FROM_CHANNEL);
2772 /* Get passphrase */
2773 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
2775 passphrase = silc_calloc(tmp_len, sizeof(*passphrase));
2776 memcpy(passphrase, tmp, tmp_len);
2779 /* Check the channel passphrase if set. */
2780 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2781 if (!passphrase || memcmp(channel->passphrase, passphrase,
2782 strlen(channel->passphrase))) {
2783 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2784 SILC_STATUS_ERR_BAD_PASSWORD);
2789 /* Check user count limit if set. */
2790 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
2791 if (silc_list_count(channel->user_list) + 1 >
2792 channel->user_limit) {
2793 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2794 SILC_STATUS_ERR_CHANNEL_IS_FULL);
2800 * Client is allowed to join to the channel. Make it happen.
2803 /* Check whether the client already is on the channel */
2804 if (silc_server_client_on_channel(client, channel)) {
2805 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2806 SILC_STATUS_ERR_USER_ON_CHANNEL);
2810 /* Generate new channel key as protocol dictates */
2811 if ((!created && silc_list_count(channel->user_list) > 0) ||
2812 !channel->channel_key)
2813 silc_server_create_channel_key(server, channel, 0);
2815 /* Send the channel key. This is broadcasted to the channel but is not
2816 sent to the client who is joining to the channel. */
2817 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
2818 silc_server_send_channel_key(server, NULL, channel,
2819 server->server_type == SILC_ROUTER ?
2820 FALSE : !server->standalone);
2822 /* Join the client to the channel by adding it to channel's user list.
2823 Add also the channel to client entry's channels list for fast cross-
2825 chl = silc_calloc(1, sizeof(*chl));
2827 chl->client = client;
2828 chl->channel = channel;
2829 silc_list_add(channel->user_list, chl);
2830 silc_list_add(client->channels, chl);
2832 /* Get users on the channel */
2833 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2836 /* Encode Client ID Payload of the original client who wants to join */
2837 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2839 /* Encode command reply packet */
2840 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2841 SILC_PUT32_MSB(channel->mode, mode);
2842 SILC_PUT32_MSB(created, tmp2);
2843 SILC_PUT32_MSB(user_count, tmp3);
2845 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2846 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2847 keyp = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, tmp,
2848 strlen(channel->channel_key->
2850 channel->channel_key->cipher->name,
2851 channel->key_len / 8, channel->key);
2856 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2857 SILC_STATUS_OK, ident, 13,
2858 2, channel->channel_name,
2859 strlen(channel->channel_name),
2860 3, chidp->data, chidp->len,
2861 4, clidp->data, clidp->len,
2864 7, keyp ? keyp->data : NULL,
2865 keyp ? keyp->len : 0,
2866 8, channel->ban_list,
2868 strlen(channel->ban_list) : 0,
2869 9, channel->invite_list,
2870 channel->invite_list ?
2871 strlen(channel->invite_list) : 0,
2874 strlen(channel->topic) : 0,
2875 11, channel->hmac->hmac->name,
2876 strlen(channel->hmac->hmac->name),
2878 13, user_list->data, user_list->len,
2879 14, mode_list->data,
2882 /* Send command reply */
2883 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2884 reply->data, reply->len, FALSE);
2886 if (!cmd->pending) {
2887 /* Send JOIN notify to locally connected clients on the channel */
2888 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2889 SILC_NOTIFY_TYPE_JOIN, 2,
2890 clidp->data, clidp->len,
2891 chidp->data, chidp->len);
2893 /* Send JOIN notify packet to our primary router */
2894 if (!server->standalone)
2895 silc_server_send_notify_join(server, server->router->connection,
2896 server->server_type == SILC_ROUTER ?
2897 TRUE : FALSE, channel, client->id,
2898 SILC_ID_CLIENT_LEN);
2901 silc_buffer_free(reply);
2902 silc_buffer_free(clidp);
2903 silc_buffer_free(chidp);
2904 silc_buffer_free(keyp);
2905 silc_buffer_free(user_list);
2906 silc_buffer_free(mode_list);
2910 silc_free(passphrase);
2913 /* Server side of command JOIN. Joins client into requested channel. If
2914 the channel does not exist it will be created. */
2916 SILC_SERVER_CMD_FUNC(join)
2918 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2919 SilcServer server = cmd->server;
2921 char *tmp, *channel_name = NULL, *cipher, *hmac;
2922 SilcChannelEntry channel;
2924 int created = FALSE;
2925 SilcClientID *client_id;
2927 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_JOIN, cmd, 1, 4);
2929 /* Get channel name */
2930 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2932 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2933 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2938 if (strlen(channel_name) > 256)
2939 channel_name[255] = '\0';
2941 if (silc_server_command_bad_chars(channel_name) == TRUE) {
2942 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2943 SILC_STATUS_ERR_BAD_CHANNEL);
2944 silc_free(channel_name);
2948 /* Get Client ID of the client who is joining to the channel */
2949 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2951 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2952 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2955 client_id = silc_id_payload_parse_id(tmp, tmp_len);
2957 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2958 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2962 /* Get cipher and hmac name */
2963 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
2964 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
2966 /* See if the channel exists */
2967 channel = silc_idlist_find_channel_by_name(server->local_list,
2968 channel_name, NULL);
2970 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2971 /* If this is coming from client the Client ID in the command packet must
2972 be same as the client's ID. */
2973 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2974 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
2975 if (SILC_ID_CLIENT_COMPARE(entry->id, client_id)) {
2976 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2977 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2982 if (!channel || !channel->id) {
2983 /* Channel not found */
2985 /* If we are standalone server we don't have a router, we just create
2986 the channel by ourselves. */
2987 if (server->standalone) {
2988 channel = silc_server_create_new_channel(server, server->id, cipher,
2989 hmac, channel_name, TRUE);
2991 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2992 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
2996 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3001 /* The channel does not exist on our server. If we are normal server
3002 we will send JOIN command to our router which will handle the
3003 joining procedure (either creates the channel if it doesn't exist
3004 or joins the client to it). */
3005 if (server->server_type == SILC_SERVER) {
3009 old_ident = silc_command_get_ident(cmd->payload);
3010 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3011 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3013 /* Send JOIN command to our router */
3014 silc_server_packet_send(server, (SilcSocketConnection)
3015 server->router->connection,
3016 SILC_PACKET_COMMAND, cmd->packet->flags,
3017 tmpbuf->data, tmpbuf->len, TRUE);
3019 /* Reprocess this packet after received reply from router */
3020 silc_server_command_pending(server, SILC_COMMAND_JOIN,
3021 silc_command_get_ident(cmd->payload),
3022 silc_server_command_destructor,
3023 silc_server_command_join,
3024 silc_server_command_dup(cmd));
3025 cmd->pending = TRUE;
3029 /* We are router and the channel does not seem exist so we will check
3030 our global list as well for the channel. */
3031 channel = silc_idlist_find_channel_by_name(server->global_list,
3032 channel_name, NULL);
3034 /* Channel really does not exist, create it */
3035 channel = silc_server_create_new_channel(server, server->id, cipher,
3036 hmac, channel_name, TRUE);
3038 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3039 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3043 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3050 /* Channel not found */
3052 /* If the command came from router and/or we are normal server then
3053 something went wrong with the joining as the channel was not found.
3054 We can't do anything else but ignore this. */
3055 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
3056 server->server_type == SILC_SERVER)
3059 /* We are router and the channel does not seem exist so we will check
3060 our global list as well for the channel. */
3061 channel = silc_idlist_find_channel_by_name(server->global_list,
3062 channel_name, NULL);
3064 /* Channel really does not exist, create it */
3065 channel = silc_server_create_new_channel(server, server->id, cipher,
3066 hmac, channel_name, TRUE);
3068 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3069 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3073 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3079 /* If the channel does not have global users and is also empty it means the
3080 channel was created globally (by our router) and the client will be the
3081 channel founder and operator. */
3082 if (!channel->global_users && silc_list_count(channel->user_list) == 0) {
3083 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3084 created = TRUE; /* Created globally by our router */
3087 /* Join to the channel */
3088 silc_server_command_join_channel(server, cmd, channel, client_id,
3091 silc_free(client_id);
3094 silc_server_command_free(cmd);
3097 /* Server side of command MOTD. Sends server's current "message of the
3098 day" to the client. */
3100 SILC_SERVER_CMD_FUNC(motd)
3102 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3103 SilcServer server = cmd->server;
3104 SilcBuffer packet, idp;
3105 char *motd, *dest_server;
3107 uint16 ident = silc_command_get_ident(cmd->payload);
3109 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_MOTD, cmd, 1, 1);
3111 /* Get server name */
3112 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
3114 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
3115 SILC_STATUS_ERR_NO_SUCH_SERVER);
3119 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
3122 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3124 if (server->config && server->config->motd &&
3125 server->config->motd->motd_file) {
3127 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
3132 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3133 SILC_STATUS_OK, ident, 2,
3139 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3140 SILC_STATUS_OK, ident, 1,
3144 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3145 packet->data, packet->len, FALSE);
3146 silc_buffer_free(packet);
3147 silc_buffer_free(idp);
3149 SilcServerEntry entry;
3151 /* Check whether we have this server cached */
3152 entry = silc_idlist_find_server_by_name(server->global_list,
3155 entry = silc_idlist_find_server_by_name(server->local_list,
3159 if (server->server_type == SILC_ROUTER && !cmd->pending &&
3160 entry && !entry->motd) {
3161 /* Send to the server */
3165 old_ident = silc_command_get_ident(cmd->payload);
3166 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3167 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3169 silc_server_packet_send(server, entry->connection,
3170 SILC_PACKET_COMMAND, cmd->packet->flags,
3171 tmpbuf->data, tmpbuf->len, TRUE);
3173 /* Reprocess this packet after received reply from router */
3174 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3175 silc_command_get_ident(cmd->payload),
3176 silc_server_command_destructor,
3177 silc_server_command_motd,
3178 silc_server_command_dup(cmd));
3179 cmd->pending = TRUE;
3180 silc_command_set_ident(cmd->payload, old_ident);
3181 silc_buffer_free(tmpbuf);
3185 if (!entry && !cmd->pending && !server->standalone) {
3186 /* Send to the primary router */
3190 old_ident = silc_command_get_ident(cmd->payload);
3191 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3192 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3194 silc_server_packet_send(server, server->router->connection,
3195 SILC_PACKET_COMMAND, cmd->packet->flags,
3196 tmpbuf->data, tmpbuf->len, TRUE);
3198 /* Reprocess this packet after received reply from router */
3199 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3200 silc_command_get_ident(cmd->payload),
3201 silc_server_command_destructor,
3202 silc_server_command_motd,
3203 silc_server_command_dup(cmd));
3204 cmd->pending = TRUE;
3205 silc_command_set_ident(cmd->payload, old_ident);
3206 silc_buffer_free(tmpbuf);
3211 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3212 SILC_STATUS_ERR_NO_SUCH_SERVER);
3216 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3219 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3220 SILC_STATUS_OK, ident, 2,
3223 strlen(entry->motd));
3225 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3226 SILC_STATUS_OK, ident, 1,
3229 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3230 packet->data, packet->len, FALSE);
3231 silc_buffer_free(packet);
3232 silc_buffer_free(idp);
3236 silc_server_command_free(cmd);
3239 /* Server side of command UMODE. Client can use this command to set/unset
3240 user mode. Client actually cannot set itself to be as server/router
3241 operator so this can be used only to unset the modes. */
3243 SILC_SERVER_CMD_FUNC(umode)
3245 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3246 SilcServer server = cmd->server;
3247 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3249 unsigned char *tmp_mask;
3251 uint16 ident = silc_command_get_ident(cmd->payload);
3253 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3256 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_UMODE, cmd, 2, 2);
3258 /* Get the client's mode mask */
3259 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3261 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3262 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3265 SILC_GET32_MSB(mask, tmp_mask);
3271 if (mask & SILC_UMODE_SERVER_OPERATOR) {
3272 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR)) {
3273 /* Cannot operator mode */
3274 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3275 SILC_STATUS_ERR_PERM_DENIED);
3279 if (client->mode & SILC_UMODE_SERVER_OPERATOR)
3280 /* Remove the server operator rights */
3281 client->mode &= ~SILC_UMODE_SERVER_OPERATOR;
3284 if (mask & SILC_UMODE_ROUTER_OPERATOR) {
3285 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
3286 /* Cannot operator mode */
3287 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3288 SILC_STATUS_ERR_PERM_DENIED);
3292 if (client->mode & SILC_UMODE_ROUTER_OPERATOR)
3293 /* Remove the router operator rights */
3294 client->mode &= ~SILC_UMODE_ROUTER_OPERATOR;
3297 if (mask & SILC_UMODE_GONE) {
3298 client->mode |= SILC_UMODE_GONE;
3300 if (client->mode & SILC_UMODE_GONE)
3301 /* Remove the gone status */
3302 client->mode &= ~SILC_UMODE_GONE;
3305 /* Send UMODE change to primary router */
3306 if (!server->standalone)
3307 silc_server_send_notify_umode(server, server->router->connection, TRUE,
3308 client->id, SILC_ID_CLIENT_LEN,
3311 /* Send command reply to sender */
3312 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
3313 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);
3320 silc_server_command_free(cmd);
3323 /* Checks that client has rights to add or remove channel modes. If any
3324 of the checks fails FALSE is returned. */
3326 int silc_server_check_cmode_rights(SilcChannelEntry channel,
3327 SilcChannelClientEntry client,
3330 int is_op = client->mode & SILC_CHANNEL_UMODE_CHANOP;
3331 int is_fo = client->mode & SILC_CHANNEL_UMODE_CHANFO;
3333 /* Check whether has rights to change anything */
3334 if (!is_op && !is_fo)
3337 /* Check whether has rights to change everything */
3341 /* We know that client is channel operator, check that they are not
3342 changing anything that requires channel founder rights. Rest of the
3343 modes are available automatically for channel operator. */
3345 if (mode & SILC_CHANNEL_MODE_PRIVKEY) {
3346 if (is_op && !is_fo)
3349 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3350 if (is_op && !is_fo)
3355 if (mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3356 if (is_op && !is_fo)
3359 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3360 if (is_op && !is_fo)
3365 if (mode & SILC_CHANNEL_MODE_CIPHER) {
3366 if (is_op && !is_fo)
3369 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3370 if (is_op && !is_fo)
3375 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3376 if (is_op && !is_fo)
3379 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3380 if (is_op && !is_fo)
3388 /* Server side command of CMODE. Changes channel mode */
3390 SILC_SERVER_CMD_FUNC(cmode)
3392 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3393 SilcServer server = cmd->server;
3394 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3395 SilcIDListData idata = (SilcIDListData)client;
3396 SilcChannelID *channel_id;
3397 SilcChannelEntry channel;
3398 SilcChannelClientEntry chl;
3399 SilcBuffer packet, cidp;
3400 unsigned char *tmp, *tmp_id, *tmp_mask;
3401 char *cipher = NULL, *hmac = NULL;
3402 uint32 mode_mask, tmp_len, tmp_len2;
3403 uint16 ident = silc_command_get_ident(cmd->payload);
3405 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CMODE, cmd, 2, 7);
3407 /* Get Channel ID */
3408 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
3410 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3411 SILC_STATUS_ERR_NO_CHANNEL_ID);
3414 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2);
3416 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3417 SILC_STATUS_ERR_NO_CHANNEL_ID);
3421 /* Get the channel mode mask */
3422 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3424 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3425 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3428 SILC_GET32_MSB(mode_mask, tmp_mask);
3430 /* Get channel entry */
3431 channel = silc_idlist_find_channel_by_id(server->local_list,
3434 channel = silc_idlist_find_channel_by_id(server->global_list,
3437 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3438 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3443 /* Check whether this client is on the channel */
3444 if (!silc_server_client_on_channel(client, channel)) {
3445 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3446 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3450 /* Get entry to the channel user list */
3451 silc_list_start(channel->user_list);
3452 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
3453 if (chl->client == client)
3456 /* Check that client has rights to change any requested channel modes */
3457 if (!silc_server_check_cmode_rights(channel, chl, mode_mask)) {
3458 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3459 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3464 * Check the modes. Modes that requires nothing special operation are
3468 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
3469 /* Channel uses private keys to protect traffic. Client(s) has set the
3470 key locally they want to use, server does not know that key. */
3471 /* Nothing interesting to do here */
3473 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3474 /* The mode is removed and we need to generate and distribute
3475 new channel key. Clients are not using private channel keys
3476 anymore after this. */
3478 /* Re-generate channel key */
3479 silc_server_create_channel_key(server, channel, 0);
3481 /* Send the channel key. This sends it to our local clients and if
3482 we are normal server to our router as well. */
3483 silc_server_send_channel_key(server, NULL, channel,
3484 server->server_type == SILC_ROUTER ?
3485 FALSE : !server->standalone);
3487 cipher = channel->channel_key->cipher->name;
3488 hmac = channel->hmac->hmac->name;
3492 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
3493 /* User limit is set on channel */
3496 /* Get user limit */
3497 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
3499 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
3500 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3501 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3505 SILC_GET32_MSB(user_limit, tmp);
3506 channel->user_limit = user_limit;
3509 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
3510 /* User limit mode is unset. Remove user limit */
3511 channel->user_limit = 0;
3514 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
3515 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
3516 /* Passphrase has been set to channel */
3518 /* Get the passphrase */
3519 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
3521 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3522 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3526 /* Save the passphrase */
3527 channel->passphrase = strdup(tmp);
3530 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3531 /* Passphrase mode is unset. remove the passphrase */
3532 if (channel->passphrase) {
3533 silc_free(channel->passphrase);
3534 channel->passphrase = NULL;
3539 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
3540 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
3541 /* Cipher to use protect the traffic */
3544 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
3546 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3547 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3551 /* Delete old cipher and allocate the new one */
3552 silc_cipher_free(channel->channel_key);
3553 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3554 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3555 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3559 /* Re-generate channel key */
3560 silc_server_create_channel_key(server, channel, 0);
3562 /* Send the channel key. This sends it to our local clients and if
3563 we are normal server to our router as well. */
3564 silc_server_send_channel_key(server, NULL, channel,
3565 server->server_type == SILC_ROUTER ?
3566 FALSE : !server->standalone);
3569 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3570 /* Cipher mode is unset. Remove the cipher and revert back to
3572 cipher = channel->cipher;
3574 /* Delete old cipher and allocate default one */
3575 silc_cipher_free(channel->channel_key);
3576 if (!silc_cipher_alloc(cipher ? cipher : "aes-256-cbc",
3577 &channel->channel_key)) {
3578 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3579 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3583 /* Re-generate channel key */
3584 silc_server_create_channel_key(server, channel, 0);
3586 /* Send the channel key. This sends it to our local clients and if
3587 we are normal server to our router as well. */
3588 silc_server_send_channel_key(server, NULL, channel,
3589 server->server_type == SILC_ROUTER ?
3590 FALSE : !server->standalone);
3594 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
3595 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
3596 /* HMAC to use protect the traffic */
3597 unsigned char hash[32];
3600 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
3602 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3603 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3607 /* Delete old hmac and allocate the new one */
3608 silc_hmac_free(channel->hmac);
3609 if (!silc_hmac_alloc(hmac, NULL, &channel->hmac)) {
3610 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3611 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3615 /* Set the HMAC key out of current channel key. The client must do
3617 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3619 silc_hmac_set_key(channel->hmac, hash,
3620 silc_hash_len(channel->hmac->hash));
3621 memset(hash, 0, sizeof(hash));
3624 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
3625 /* Hmac mode is unset. Remove the hmac and revert back to
3627 unsigned char hash[32];
3628 hmac = channel->hmac_name;
3630 /* Delete old hmac and allocate default one */
3631 silc_hmac_free(channel->hmac);
3632 if (!silc_hmac_alloc(hmac ? hmac : "hmac-sha1-96", NULL,
3634 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3635 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3639 /* Set the HMAC key out of current channel key. The client must do
3641 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3643 silc_hmac_set_key(channel->hmac, hash,
3644 silc_hash_len(channel->hmac->hash));
3645 memset(hash, 0, sizeof(hash));
3649 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3650 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3651 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
3652 /* Set the founder authentication */
3653 SilcAuthPayload auth;
3655 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
3657 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3658 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3662 auth = silc_auth_payload_parse(tmp, tmp_len);
3664 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3665 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3669 /* Save the public key */
3670 tmp = silc_pkcs_public_key_encode(idata->public_key, &tmp_len);
3671 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
3674 channel->founder_method = silc_auth_get_method(auth);
3676 if (channel->founder_method == SILC_AUTH_PASSWORD) {
3677 tmp = silc_auth_get_data(auth, &tmp_len);
3678 channel->founder_passwd =
3679 silc_calloc(tmp_len + 1, sizeof(*channel->founder_passwd));
3680 memcpy(channel->founder_passwd, tmp, tmp_len);
3681 channel->founder_passwd_len = tmp_len;
3684 silc_auth_payload_free(auth);
3688 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3689 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3690 if (channel->founder_key)
3691 silc_pkcs_public_key_free(channel->founder_key);
3692 if (channel->founder_passwd) {
3693 silc_free(channel->founder_passwd);
3694 channel->founder_passwd = NULL;
3700 /* Finally, set the mode */
3701 channel->mode = mode_mask;
3703 /* Send CMODE_CHANGE notify */
3704 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3705 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3706 SILC_NOTIFY_TYPE_CMODE_CHANGE, 4,
3707 cidp->data, cidp->len,
3709 cipher, cipher ? strlen(cipher) : 0,
3710 hmac, hmac ? strlen(hmac) : 0);
3712 /* Set CMODE notify type to network */
3713 if (!server->standalone)
3714 silc_server_send_notify_cmode(server, server->router->connection,
3715 server->server_type == SILC_ROUTER ?
3716 TRUE : FALSE, channel,
3717 mode_mask, client->id, SILC_ID_CLIENT,
3721 /* Send command reply to sender */
3722 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3723 SILC_STATUS_OK, ident, 1,
3725 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3726 packet->data, packet->len, FALSE);
3728 silc_buffer_free(packet);
3729 silc_free(channel_id);
3733 silc_server_command_free(cmd);
3736 /* Server side of CUMODE command. Changes client's mode on a channel. */
3738 SILC_SERVER_CMD_FUNC(cumode)
3740 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3741 SilcServer server = cmd->server;
3742 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3743 SilcIDListData idata = (SilcIDListData)client;
3744 SilcChannelID *channel_id;
3745 SilcClientID *client_id;
3746 SilcChannelEntry channel;
3747 SilcClientEntry target_client;
3748 SilcChannelClientEntry chl;
3749 SilcBuffer packet, idp;
3750 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3751 uint32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3753 uint16 ident = silc_command_get_ident(cmd->payload);
3755 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CUMODE, cmd, 3, 4);
3757 /* Get Channel ID */
3758 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3760 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3761 SILC_STATUS_ERR_NO_CHANNEL_ID);
3764 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len);
3766 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3767 SILC_STATUS_ERR_NO_CHANNEL_ID);
3771 /* Get channel entry */
3772 channel = silc_idlist_find_channel_by_id(server->local_list,
3775 channel = silc_idlist_find_channel_by_id(server->global_list,
3778 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3779 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3784 /* Check whether sender is on the channel */
3785 if (!silc_server_client_on_channel(client, channel)) {
3786 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3787 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3791 /* Check that client has rights to change other's rights */
3792 silc_list_start(channel->user_list);
3793 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
3794 if (chl->client == client) {
3795 sender_mask = chl->mode;
3800 /* Get the target client's channel mode mask */
3801 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3803 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3804 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3807 SILC_GET32_MSB(target_mask, tmp_mask);
3809 /* Get target Client ID */
3810 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3812 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3813 SILC_STATUS_ERR_NO_CLIENT_ID);
3816 client_id = silc_id_payload_parse_id(tmp_id, tmp_len);
3818 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3819 SILC_STATUS_ERR_NO_CLIENT_ID);
3823 /* Get target client's entry */
3824 target_client = silc_idlist_find_client_by_id(server->local_list,
3826 if (!target_client) {
3827 target_client = silc_idlist_find_client_by_id(server->global_list,
3831 if (target_client != client &&
3832 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3833 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3834 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3835 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3839 /* Check whether target client is on the channel */
3840 if (target_client != client) {
3841 if (!silc_server_client_on_channel(target_client, channel)) {
3842 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3843 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
3847 /* Get entry to the channel user list */
3848 silc_list_start(channel->user_list);
3849 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
3850 if (chl->client == target_client)
3858 /* If the target client is founder, no one else can change their mode
3860 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && chl->client != target_client) {
3861 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3862 SILC_STATUS_ERR_NOT_YOU);
3866 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3867 /* The client tries to claim the founder rights. */
3868 unsigned char *tmp_auth;
3869 uint32 tmp_auth_len, auth_len;
3872 if (target_client != client) {
3873 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3874 SILC_STATUS_ERR_NOT_YOU);
3878 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3879 !channel->founder_key) {
3880 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3881 SILC_STATUS_ERR_NOT_YOU);
3885 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3887 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3888 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3892 auth = (channel->founder_method == SILC_AUTH_PASSWORD ?
3893 (void *)channel->founder_passwd : (void *)channel->founder_key);
3894 auth_len = (channel->founder_method == SILC_AUTH_PASSWORD ?
3895 channel->founder_passwd_len : 0);
3897 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len,
3898 channel->founder_method, auth, auth_len,
3899 idata->hash, client->id, SILC_ID_CLIENT)) {
3900 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3901 SILC_STATUS_ERR_AUTH_FAILED);
3905 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
3908 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3909 if (target_client == client) {
3910 /* Remove channel founder rights from itself */
3911 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3914 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3915 SILC_STATUS_ERR_NOT_YOU);
3921 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
3922 /* Promote to operator */
3923 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
3924 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3925 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3926 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3927 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3931 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
3935 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
3936 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3937 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3938 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3939 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3943 /* Demote to normal user */
3944 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
3949 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3950 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3952 /* Send notify to channel, notify only if mode was actually changed. */
3954 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3955 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
3956 idp->data, idp->len,
3960 /* Set CUMODE notify type to network */
3961 if (!server->standalone)
3962 silc_server_send_notify_cumode(server, server->router->connection,
3963 server->server_type == SILC_ROUTER ?
3964 TRUE : FALSE, channel,
3965 target_mask, client->id,
3968 SILC_ID_CLIENT_LEN);
3971 /* Send command reply to sender */
3972 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
3973 SILC_STATUS_OK, ident, 2,
3975 3, tmp_id, tmp_len);
3976 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3977 packet->data, packet->len, FALSE);
3979 silc_buffer_free(packet);
3980 silc_free(channel_id);
3981 silc_free(client_id);
3982 silc_buffer_free(idp);
3985 silc_server_command_free(cmd);
3988 /* Server side of KICK command. Kicks client out of channel. */
3990 SILC_SERVER_CMD_FUNC(kick)
3992 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3993 SilcServer server = cmd->server;
3994 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3995 SilcClientEntry target_client;
3996 SilcChannelID *channel_id;
3997 SilcClientID *client_id;
3998 SilcChannelEntry channel;
3999 SilcChannelClientEntry chl;
4002 unsigned char *tmp, *comment;
4004 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_LEAVE, cmd, 1, 3);
4006 /* Get Channel ID */
4007 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4009 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4010 SILC_STATUS_ERR_NO_CHANNEL_ID);
4013 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
4015 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4016 SILC_STATUS_ERR_NO_CHANNEL_ID);
4020 /* Get channel entry */
4021 channel = silc_idlist_find_channel_by_id(server->local_list,
4024 channel = silc_idlist_find_channel_by_id(server->local_list,
4027 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4028 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4033 /* Check whether sender is on the channel */
4034 if (!silc_server_client_on_channel(client, channel)) {
4035 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4036 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4040 /* Check that the kicker is channel operator or channel founder */
4041 silc_list_start(channel->user_list);
4042 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
4043 if (chl->client == client) {
4044 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
4045 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4046 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4053 /* Get target Client ID */
4054 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4056 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4057 SILC_STATUS_ERR_NO_CLIENT_ID);
4060 client_id = silc_id_payload_parse_id(tmp, tmp_len);
4062 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4063 SILC_STATUS_ERR_NO_CLIENT_ID);
4067 /* Get target client's entry */
4068 target_client = silc_idlist_find_client_by_id(server->local_list,
4070 if (!target_client) {
4071 target_client = silc_idlist_find_client_by_id(server->global_list,
4075 /* Check that the target client is not channel founder. Channel founder
4076 cannot be kicked from the channel. */
4077 silc_list_start(channel->user_list);
4078 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
4079 if (chl->client == target_client) {
4080 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4081 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4082 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV);
4089 /* Check whether target client is on the channel */
4090 if (!silc_server_client_on_channel(target_client, channel)) {
4091 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4092 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
4098 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4102 /* Send command reply to sender */
4103 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4106 /* Send KICKED notify to local clients on the channel */
4107 idp = silc_id_payload_encode(target_client->id, SILC_ID_CLIENT);
4108 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4109 SILC_NOTIFY_TYPE_KICKED,
4111 idp->data, idp->len,
4112 comment, comment ? strlen(comment) : 0);
4113 silc_buffer_free(idp);
4115 /* Remove the client from the channel. If the channel does not exist
4116 after removing the client then the client kicked itself off the channel
4117 and we don't have to send anything after that. */
4118 if (!silc_server_remove_from_one_channel(server, NULL, channel,
4119 target_client, FALSE))
4122 /* Send KICKED notify to primary route */
4123 if (!server->standalone)
4124 silc_server_send_notify_kicked(server, server->router->connection,
4125 server->server_type == SILC_ROUTER ?
4126 TRUE : FALSE, channel,
4127 target_client->id, SILC_ID_CLIENT_LEN,
4130 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4131 /* Re-generate channel key */
4132 silc_server_create_channel_key(server, channel, 0);
4134 /* Send the channel key to the channel. The key of course is not sent
4135 to the client who was kicked off the channel. */
4136 silc_server_send_channel_key(server, target_client->connection, channel,
4137 server->server_type == SILC_ROUTER ?
4138 FALSE : !server->standalone);
4142 silc_server_command_free(cmd);
4145 /* Server side of OPER command. Client uses this comand to obtain server
4146 operator privileges to this server/router. */
4148 SILC_SERVER_CMD_FUNC(oper)
4150 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4151 SilcServer server = cmd->server;
4152 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4153 unsigned char *username, *auth;
4155 SilcServerConfigSectionAdminConnection *admin;
4156 SilcIDListData idata = (SilcIDListData)client;
4158 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_OPER, cmd, 1, 2);
4160 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4163 /* Get the username */
4164 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4166 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4167 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4171 /* Get the admin configuration */
4172 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4173 username, client->nickname);
4175 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4176 username, client->nickname);
4178 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4179 SILC_STATUS_ERR_AUTH_FAILED);
4184 /* Get the authentication payload */
4185 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4187 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4188 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4192 /* Verify the authentication data */
4193 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4194 admin->auth_data, admin->auth_data_len,
4195 idata->hash, client->id, SILC_ID_CLIENT)) {
4196 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4197 SILC_STATUS_ERR_AUTH_FAILED);
4201 /* Client is now server operator */
4202 client->mode |= SILC_UMODE_SERVER_OPERATOR;
4204 /* Send UMODE change to primary router */
4205 if (!server->standalone)
4206 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4207 client->id, SILC_ID_CLIENT_LEN,
4210 /* Send reply to the sender */
4211 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4215 silc_server_command_free(cmd);
4218 /* Server side of SILCOPER command. Client uses this comand to obtain router
4219 operator privileges to this router. */
4221 SILC_SERVER_CMD_FUNC(silcoper)
4223 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4224 SilcServer server = cmd->server;
4225 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4226 unsigned char *username, *auth;
4228 SilcServerConfigSectionAdminConnection *admin;
4229 SilcIDListData idata = (SilcIDListData)client;
4231 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_SILCOPER, cmd, 1, 2);
4233 if (server->server_type == SILC_SERVER)
4236 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4239 /* Get the username */
4240 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4242 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4243 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4247 /* Get the admin configuration */
4248 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4249 username, client->nickname);
4251 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4252 username, client->nickname);
4254 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4255 SILC_STATUS_ERR_AUTH_FAILED);
4260 /* Get the authentication payload */
4261 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4263 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4264 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4268 /* Verify the authentication data */
4269 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4270 admin->auth_data, admin->auth_data_len,
4271 idata->hash, client->id, SILC_ID_CLIENT)) {
4272 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4273 SILC_STATUS_ERR_AUTH_FAILED);
4277 /* Client is now router operator */
4278 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4280 /* Send UMODE change to primary router */
4281 if (!server->standalone)
4282 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4283 client->id, SILC_ID_CLIENT_LEN,
4286 /* Send reply to the sender */
4287 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4291 silc_server_command_free(cmd);
4294 /* Server side command of CONNECT. Connects us to the specified remote
4295 server or router. */
4297 SILC_SERVER_CMD_FUNC(connect)
4299 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4300 SilcServer server = cmd->server;
4301 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4302 unsigned char *tmp, *host;
4304 uint32 port = SILC_PORT;
4306 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CONNECT, cmd, 1, 2);
4308 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4311 /* Check whether client has the permissions. */
4312 if (client->mode == SILC_UMODE_NONE) {
4313 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4314 SILC_STATUS_ERR_NO_SERVER_PRIV);
4318 if (server->server_type == SILC_ROUTER &&
4319 client->mode & SILC_UMODE_SERVER_OPERATOR) {
4320 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4321 SILC_STATUS_ERR_NO_ROUTER_PRIV);
4325 /* Get the remote server */
4326 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4328 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4329 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4334 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4336 SILC_GET32_MSB(port, tmp);
4338 /* Create the connection. It is done with timeout and is async. */
4339 silc_server_create_connection(server, host, port);
4341 /* Send reply to the sender */
4342 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4346 silc_server_command_free(cmd);
4349 /* Server side of command BAN. This is used to manage the ban list of the
4350 channel. To add clients and remove clients from the ban list. */
4352 SILC_SERVER_CMD_FUNC(ban)
4354 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4355 SilcServer server = cmd->server;
4356 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4358 SilcChannelEntry channel;
4359 SilcChannelClientEntry chl;
4360 SilcChannelID *channel_id = NULL;
4361 unsigned char *id, *add, *del;
4362 uint32 id_len, tmp_len;
4363 uint16 ident = silc_command_get_ident(cmd->payload);
4365 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4368 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_BAN, cmd, 0, 3);
4370 /* Get Channel ID */
4371 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4373 channel_id = silc_id_payload_parse_id(id, id_len);
4375 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4376 SILC_STATUS_ERR_NO_CHANNEL_ID);
4381 /* Get channel entry. The server must know about the channel since the
4382 client is expected to be on the channel. */
4383 channel = silc_idlist_find_channel_by_id(server->local_list,
4386 channel = silc_idlist_find_channel_by_id(server->global_list,
4389 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4390 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4395 /* Check whether this client is on the channel */
4396 if (!silc_server_client_on_channel(client, channel)) {
4397 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4398 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4402 /* Get entry to the channel user list */
4403 silc_list_start(channel->user_list);
4404 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
4405 if (chl->client == client)
4408 /* The client must be at least channel operator. */
4409 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4410 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4411 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4415 /* Get the new ban and add it to the ban list */
4416 add = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4418 if (!channel->ban_list)
4419 channel->ban_list = silc_calloc(tmp_len + 2, sizeof(*channel->ban_list));
4421 channel->ban_list = silc_realloc(channel->ban_list,
4422 sizeof(*channel->ban_list) *
4424 strlen(channel->ban_list) + 2));
4425 if (add[tmp_len - 1] == ',')
4426 add[tmp_len - 1] = '\0';
4428 strncat(channel->ban_list, add, tmp_len);
4429 strncat(channel->ban_list, ",", 1);
4432 /* Get the ban to be removed and remove it from the list */
4433 del = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4434 if (del && channel->ban_list) {
4435 char *start, *end, *n;
4437 if (!strncmp(channel->ban_list, del, strlen(channel->ban_list) - 1)) {
4438 silc_free(channel->ban_list);
4439 channel->ban_list = NULL;
4441 start = strstr(channel->ban_list, del);
4442 if (start && strlen(start) >= tmp_len) {
4443 end = start + tmp_len;
4444 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
4445 strncat(n, channel->ban_list, start - channel->ban_list);
4446 strncat(n, end + 1, ((channel->ban_list + strlen(channel->ban_list)) -
4448 silc_free(channel->ban_list);
4449 channel->ban_list = n;
4454 /* Send the BAN notify type to our primary router. */
4455 if (!server->standalone && (add || del))
4456 silc_server_send_notify_ban(server, server->router->connection,
4457 server->server_type == SILC_ROUTER ?
4458 TRUE : FALSE, channel, add, del);
4460 /* Send the reply back to the client */
4461 if (channel->ban_list)
4463 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4464 SILC_STATUS_OK, ident, 2,
4466 3, channel->ban_list,
4467 strlen(channel->ban_list) - 1);
4470 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4471 SILC_STATUS_OK, ident, 1,
4474 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4475 packet->data, packet->len, FALSE);
4477 silc_buffer_free(packet);
4481 silc_free(channel_id);
4482 silc_server_command_free(cmd);
4485 /* Server side command of CLOSE. Closes connection to a specified server. */
4487 SILC_SERVER_CMD_FUNC(close)
4489 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4490 SilcServer server = cmd->server;
4491 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4492 SilcServerEntry server_entry;
4493 SilcSocketConnection sock;
4496 unsigned char *name;
4497 uint32 port = SILC_PORT;
4499 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CLOSE, cmd, 1, 2);
4501 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4504 /* Check whether client has the permissions. */
4505 if (client->mode == SILC_UMODE_NONE) {
4506 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4507 SILC_STATUS_ERR_NO_SERVER_PRIV);
4511 /* Get the remote server */
4512 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4514 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4515 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4520 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4522 SILC_GET32_MSB(port, tmp);
4524 server_entry = silc_idlist_find_server_by_conn(server->local_list,
4526 if (!server_entry) {
4527 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4528 SILC_STATUS_ERR_NO_SERVER_ID);
4532 /* Send reply to the sender */
4533 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4536 /* Close the connection to the server */
4537 sock = (SilcSocketConnection)server_entry->connection;
4538 silc_server_free_sock_user_data(server, sock);
4539 silc_server_close_connection(server, sock);
4542 silc_server_command_free(cmd);
4545 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4546 active connections. */
4548 SILC_SERVER_CMD_FUNC(shutdown)
4550 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4551 SilcServer server = cmd->server;
4552 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4554 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_SHUTDOWN, cmd, 0, 0);
4556 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4559 /* Check whether client has the permission. */
4560 if (client->mode == SILC_UMODE_NONE) {
4561 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4562 SILC_STATUS_ERR_NO_SERVER_PRIV);
4566 /* Send reply to the sender */
4567 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4570 /* Then, gracefully, or not, bring the server down. */
4571 silc_server_stop(server);
4575 silc_server_command_free(cmd);
4578 /* Server side command of LEAVE. Removes client from a channel. */
4580 SILC_SERVER_CMD_FUNC(leave)
4582 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4583 SilcServer server = cmd->server;
4584 SilcSocketConnection sock = cmd->sock;
4585 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4586 SilcChannelID *id = NULL;
4587 SilcChannelEntry channel;
4591 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_LEAVE, cmd, 1, 2);
4593 /* Get Channel ID */
4594 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4596 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4597 SILC_STATUS_ERR_NO_CHANNEL_ID);
4600 id = silc_id_payload_parse_id(tmp, len);
4602 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4603 SILC_STATUS_ERR_NO_CHANNEL_ID);
4607 /* Get channel entry */
4608 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4610 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4612 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4613 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4618 /* Check whether this client is on the channel */
4619 if (!silc_server_client_on_channel(id_entry, channel)) {
4620 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4621 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4625 /* Notify routers that they should remove this client from their list
4626 of clients on the channel. Send LEAVE notify type. */
4627 if (!server->standalone)
4628 silc_server_send_notify_leave(server, server->router->connection,
4629 server->server_type == SILC_ROUTER ?
4630 TRUE : FALSE, channel, id_entry->id,
4631 SILC_ID_CLIENT_LEN);
4633 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4636 /* Remove client from channel */
4637 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4639 /* If the channel does not exist anymore we won't send anything */
4642 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4643 /* Re-generate channel key */
4644 silc_server_create_channel_key(server, channel, 0);
4646 /* Send the channel key */
4647 silc_server_send_channel_key(server, NULL, channel,
4648 server->server_type == SILC_ROUTER ?
4649 FALSE : !server->standalone);
4657 silc_server_command_free(cmd);
4660 /* Server side of command USERS. Resolves clients and their USERS currently
4661 joined on the requested channel. The list of Client ID's and their modes
4662 on the channel is sent back. */
4664 SILC_SERVER_CMD_FUNC(users)
4666 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4667 SilcServer server = cmd->server;
4668 SilcChannelEntry channel;
4671 unsigned char *channel_id;
4672 uint32 channel_id_len;
4673 SilcBuffer client_id_list;
4674 SilcBuffer client_mode_list;
4675 unsigned char lc[4];
4676 uint32 list_count = 0;
4677 uint16 ident = silc_command_get_ident(cmd->payload);
4679 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_USERS, cmd, 1, 1);
4681 /* Get Channel ID */
4682 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4684 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4685 SILC_STATUS_ERR_NO_CHANNEL_ID);
4688 id = silc_id_payload_parse_id(channel_id, channel_id_len);
4690 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4691 SILC_STATUS_ERR_NO_CHANNEL_ID);
4695 /* If we are server and we don't know about this channel we will send
4696 the command to our router. If we know about the channel then we also
4697 have the list of users already. */
4698 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4700 if (server->server_type == SILC_SERVER && !server->standalone &&
4704 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4705 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4707 /* Send USERS command */
4708 silc_server_packet_send(server, server->router->connection,
4709 SILC_PACKET_COMMAND, cmd->packet->flags,
4710 tmpbuf->data, tmpbuf->len, TRUE);
4712 /* Reprocess this packet after received reply */
4713 silc_server_command_pending(server, SILC_COMMAND_USERS,
4714 silc_command_get_ident(cmd->payload),
4715 silc_server_command_destructor,
4716 silc_server_command_users,
4717 silc_server_command_dup(cmd));
4718 cmd->pending = TRUE;
4719 silc_command_set_ident(cmd->payload, ident);
4721 silc_buffer_free(tmpbuf);
4726 /* We are router and we will check the global list as well. */
4727 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4729 /* Channel really does not exist */
4730 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4731 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4736 /* Get the users list */
4737 silc_server_get_users_on_channel(server, channel, &client_id_list,
4738 &client_mode_list, &list_count);
4741 SILC_PUT32_MSB(list_count, lc);
4744 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4745 SILC_STATUS_OK, ident, 4,
4746 2, channel_id, channel_id_len,
4748 4, client_id_list->data,
4749 client_id_list->len,
4750 5, client_mode_list->data,
4751 client_mode_list->len);
4752 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4753 packet->data, packet->len, FALSE);
4755 silc_buffer_free(packet);
4756 silc_buffer_free(client_id_list);
4757 silc_buffer_free(client_mode_list);
4761 silc_server_command_free(cmd);
4764 /* Server side of command GETKEY. This fetches the client's public key
4765 from the server where to the client is connected. */
4767 SILC_SERVER_CMD_FUNC(getkey)
4769 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4770 SilcServer server = cmd->server;
4772 SilcClientEntry client;
4773 SilcServerEntry server_entry;
4774 SilcClientID *client_id = NULL;
4775 SilcServerID *server_id = NULL;
4776 SilcIDPayload idp = NULL;
4777 uint16 ident = silc_command_get_ident(cmd->payload);
4783 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4785 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4786 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4789 idp = silc_id_payload_parse_data(tmp, tmp_len);
4791 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4792 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4796 id_type = silc_id_payload_get_type(idp);
4797 if (id_type == SILC_ID_CLIENT) {
4798 client_id = silc_id_payload_get_id(idp);
4800 /* If the client is not found from local list there is no chance it
4801 would be locally connected client so send the command further. */
4802 client = silc_idlist_find_client_by_id(server->local_list,
4805 if ((!client && !cmd->pending && !server->standalone) ||
4806 (client && !client->connection)) {
4809 SilcSocketConnection dest_sock;
4811 dest_sock = silc_server_get_client_route(server, NULL, 0,
4816 old_ident = silc_command_get_ident(cmd->payload);
4817 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4818 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4820 silc_server_packet_send(server, dest_sock,
4821 SILC_PACKET_COMMAND, cmd->packet->flags,
4822 tmpbuf->data, tmpbuf->len, TRUE);
4824 /* Reprocess this packet after received reply from router */
4825 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4826 silc_command_get_ident(cmd->payload),
4827 silc_server_command_destructor,
4828 silc_server_command_getkey,
4829 silc_server_command_dup(cmd));
4830 cmd->pending = TRUE;
4832 silc_command_set_ident(cmd->payload, old_ident);
4833 silc_buffer_free(tmpbuf);
4837 if (!client && cmd->pending) {
4838 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4839 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
4843 /* The client is locally connected, just get the public key and
4845 tmp = silc_pkcs_public_key_encode(client->data.public_key, &tmp_len);
4846 pk = silc_buffer_alloc(4 + tmp_len);
4847 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
4848 silc_buffer_format(pk,
4849 SILC_STR_UI_SHORT(tmp_len),
4850 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
4851 SILC_STR_UI_XNSTRING(tmp, tmp_len),
4855 } else if (id_type == SILC_ID_SERVER) {
4856 server_id = silc_id_payload_get_id(idp);
4858 /* If the server is not found from local list there is no chance it
4859 would be locally connected server so send the command further. */
4860 server_entry = silc_idlist_find_server_by_id(server->local_list,
4863 if ((!server_entry && !cmd->pending && !server->standalone) ||
4864 (server_entry && !server_entry->connection)) {
4868 old_ident = silc_command_get_ident(cmd->payload);
4869 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4870 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4872 silc_server_packet_send(server, server->router->connection,
4873 SILC_PACKET_COMMAND, cmd->packet->flags,
4874 tmpbuf->data, tmpbuf->len, TRUE);
4876 /* Reprocess this packet after received reply from router */
4877 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4878 silc_command_get_ident(cmd->payload),
4879 silc_server_command_destructor,
4880 silc_server_command_getkey,
4881 silc_server_command_dup(cmd));
4882 cmd->pending = TRUE;
4884 silc_command_set_ident(cmd->payload, old_ident);
4885 silc_buffer_free(tmpbuf);
4889 if (!server_entry && cmd->pending) {
4890 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4891 SILC_STATUS_ERR_NO_SUCH_SERVER_ID);
4895 /* The client is locally connected, just get the public key and
4897 tmp = silc_pkcs_public_key_encode(server_entry->data.public_key, &tmp_len);
4898 pk = silc_buffer_alloc(4 + tmp_len);
4899 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
4900 silc_buffer_format(pk,
4901 SILC_STR_UI_SHORT(tmp_len),
4902 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
4903 SILC_STR_UI_XNSTRING(tmp, tmp_len),
4910 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4911 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
4912 SILC_STATUS_OK, ident, 2,
4914 3, pk->data, pk->len);
4915 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4916 packet->data, packet->len, FALSE);
4917 silc_buffer_free(packet);
4918 silc_buffer_free(pk);
4922 silc_id_payload_free(idp);
4923 silc_free(client_id);
4924 silc_free(server_id);
4925 silc_server_command_free(cmd);