5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "serverincludes.h"
23 #include "server_internal.h"
25 static int silc_server_is_registered(SilcServer server,
26 SilcSocketConnection sock,
27 SilcServerCommandContext cmd,
30 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
32 SilcCommandStatus status);
34 silc_server_command_send_status_data(SilcServerCommandContext cmd,
36 SilcCommandStatus status,
41 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
42 SilcServerCommandReplyContext cmdr,
44 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
46 /* Server command list. */
47 SilcServerCommand silc_command_list[] =
49 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
50 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
51 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
53 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
54 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
55 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
56 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
57 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
58 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
59 SILC_SERVER_CMD(connect, CONNECT,
60 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
61 SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
62 SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
63 SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
64 SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
65 SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
66 SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
67 SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
68 SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
69 SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
70 SILC_SERVER_CMD(close, CLOSE,
71 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
72 SILC_SERVER_CMD(shutdown, SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
74 SILC_SERVER_CMD(silcoper, SILCOPER,
75 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
76 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
77 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
78 SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
83 /* Performs several checks to the command. It first checks whether this
84 command was called as pending command callback. If it was then it checks
85 whether error occurred in the command reply where the pending command
88 It also checks that the requested command includes correct amount
90 #define SILC_SERVER_COMMAND_CHECK(command, context, min, max) \
94 SILC_LOG_DEBUG(("Start")); \
96 if (silc_server_command_pending_error_check(cmd, context2, command)) { \
97 silc_server_command_free(cmd); \
101 _argc = silc_argument_get_arg_num(cmd->args); \
103 silc_server_command_send_status_reply(cmd, command, \
104 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS); \
105 silc_server_command_free(cmd); \
109 silc_server_command_send_status_reply(cmd, command, \
110 SILC_STATUS_ERR_TOO_MANY_PARAMS); \
111 silc_server_command_free(cmd); \
116 /* Returns TRUE if the connection is registered. Unregistered connections
117 usually cannot send commands hence the check. */
119 static int silc_server_is_registered(SilcServer server,
120 SilcSocketConnection sock,
121 SilcServerCommandContext cmd,
124 SilcIDListData idata = (SilcIDListData)sock->user_data;
125 if (idata->registered)
128 silc_server_command_send_status_reply(cmd, command,
129 SILC_STATUS_ERR_NOT_REGISTERED);
130 silc_server_command_free(cmd);
134 /* Internal context to hold data when executed command with timeout. */
136 SilcServerCommandContext ctx;
137 SilcServerCommand *cmd;
138 } *SilcServerCommandTimeout;
140 /* Timeout callback to process commands with timeout for client. Client's
141 commands are always executed with timeout. */
143 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
145 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
146 SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
148 /* Update access time */
149 client->last_command = time(NULL);
151 if (!(timeout->cmd->flags & SILC_CF_REG))
152 timeout->cmd->cb(timeout->ctx, NULL);
153 else if (silc_server_is_registered(timeout->ctx->server,
157 timeout->cmd->cb(timeout->ctx, NULL);
162 /* Processes received command packet. */
164 void silc_server_command_process(SilcServer server,
165 SilcSocketConnection sock,
166 SilcPacketContext *packet)
168 SilcServerCommandContext ctx;
169 SilcServerCommand *cmd;
172 /* Allocate command context. This must be free'd by the
173 command routine receiving it. */
174 ctx = silc_server_command_alloc();
175 ctx->server = server;
176 ctx->sock = silc_socket_dup(sock);
177 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
179 /* Parse the command payload in the packet */
180 ctx->payload = silc_command_payload_parse(packet->buffer);
182 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
183 silc_buffer_free(packet->buffer);
184 silc_packet_context_free(packet);
185 silc_socket_free(ctx->sock);
189 ctx->args = silc_command_get_args(ctx->payload);
191 /* Get the command */
192 command = silc_command_get(ctx->payload);
193 for (cmd = silc_command_list; cmd->cb; cmd++)
194 if (cmd->cmd == command)
198 silc_server_command_send_status_reply(ctx, command,
199 SILC_STATUS_ERR_UNKNOWN_COMMAND);
200 silc_server_command_free(ctx);
204 /* Execute client's commands always with timeout. Normally they are
205 executed with zero (0) timeout but if client is sending command more
206 frequently than once in 2 seconds, then the timeout may be 0 to 2
208 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
209 SilcClientEntry client = (SilcClientEntry)sock->user_data;
210 SilcServerCommandTimeout timeout = silc_calloc(1, sizeof(*timeout));
216 if (client->last_command && (time(NULL) - client->last_command) < 2) {
217 client->fast_command++;
220 client->fast_command = ((client->fast_command - 1) <= 0 ? 0 :
221 client->fast_command--);
225 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
226 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
227 silc_schedule_task_add(server->schedule, sock->sock,
228 silc_server_command_process_timeout,
230 2 - (time(NULL) - client->last_command), 0,
232 SILC_TASK_PRI_NORMAL);
234 silc_schedule_task_add(server->schedule, sock->sock,
235 silc_server_command_process_timeout,
239 SILC_TASK_PRI_NORMAL);
243 /* Execute for server */
245 if (!(cmd->flags & SILC_CF_REG))
247 else if (silc_server_is_registered(server, sock, ctx, cmd->cmd))
251 /* Allocate Command Context */
253 SilcServerCommandContext silc_server_command_alloc()
255 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
260 /* Free's the command context allocated before executing the command */
262 void silc_server_command_free(SilcServerCommandContext ctx)
265 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
267 if (ctx->users < 1) {
269 silc_command_payload_free(ctx->payload);
271 silc_packet_context_free(ctx->packet);
273 silc_socket_free(ctx->sock); /* Decrease reference counter */
278 /* Duplicate Command Context by adding reference counter. The context won't
279 be free'd untill it hits zero. */
281 SilcServerCommandContext
282 silc_server_command_dup(SilcServerCommandContext ctx)
285 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
290 /* Add new pending command to be executed when reply to a command has been
291 received. The `reply_cmd' is the command that will call the `callback'
292 with `context' when reply has been received. If `ident' is non-zero
293 the `callback' will be executed when received reply with command
294 identifier `ident'. */
296 void silc_server_command_pending(SilcServer server,
297 SilcCommand reply_cmd,
299 SilcServerPendingDestructor destructor,
300 SilcCommandCb callback,
303 SilcServerCommandPending *reply;
305 reply = silc_calloc(1, sizeof(*reply));
306 reply->reply_cmd = reply_cmd;
307 reply->ident = ident;
308 reply->context = context;
309 reply->callback = callback;
310 reply->destructor = destructor;
311 silc_dlist_add(server->pending_commands, reply);
314 /* Deletes pending command by reply command type. */
316 void silc_server_command_pending_del(SilcServer server,
317 SilcCommand reply_cmd,
320 SilcServerCommandPending *r;
322 silc_dlist_start(server->pending_commands);
323 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
324 if (r->reply_cmd == reply_cmd && r->ident == ident) {
325 silc_dlist_del(server->pending_commands, r);
331 /* Checks for pending commands and marks callbacks to be called from
332 the command reply function. Returns TRUE if there were pending command. */
334 int silc_server_command_pending_check(SilcServer server,
335 SilcServerCommandReplyContext ctx,
339 SilcServerCommandPending *r;
341 silc_dlist_start(server->pending_commands);
342 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
343 if (r->reply_cmd == command && r->ident == ident) {
344 ctx->context = r->context;
345 ctx->callback = r->callback;
346 ctx->destructor = r->destructor;
355 /* Destructor function for pending callbacks. This is called when using
356 pending commands to free the context given for the pending command. */
358 static void silc_server_command_destructor(void *context)
360 silc_server_command_free((SilcServerCommandContext)context);
363 /* Sends simple status message as command reply packet */
366 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
368 SilcCommandStatus status)
372 SILC_LOG_DEBUG(("Sending command status %d", status));
375 silc_command_reply_payload_encode_va(command, status,
376 silc_command_get_ident(cmd->payload),
378 silc_server_packet_send(cmd->server, cmd->sock,
379 SILC_PACKET_COMMAND_REPLY, 0,
380 buffer->data, buffer->len, FALSE);
381 silc_buffer_free(buffer);
384 /* Sends command status reply with one extra argument. The argument
385 type must be sent as argument. */
388 silc_server_command_send_status_data(SilcServerCommandContext cmd,
390 SilcCommandStatus status,
397 SILC_LOG_DEBUG(("Sending command status %d", status));
400 silc_command_reply_payload_encode_va(command, status,
401 silc_command_get_ident(cmd->payload),
402 1, arg_type, arg, arg_len);
403 silc_server_packet_send(cmd->server, cmd->sock,
404 SILC_PACKET_COMMAND_REPLY, 0,
405 buffer->data, buffer->len, FALSE);
406 silc_buffer_free(buffer);
409 /* This function can be called to check whether in the command reply
410 an error occurred. This function has no effect if this is called
411 when the command function was not called as pending command callback.
412 This returns TRUE if error had occurred. */
415 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
416 SilcServerCommandReplyContext cmdr,
419 SilcCommandStatus status;
421 if (!cmd->pending || !cmdr)
424 SILC_GET16_MSB(status, silc_argument_get_arg_type(cmdr->args, 1, NULL));
425 if (status != SILC_STATUS_OK &&
426 status != SILC_STATUS_LIST_START &&
427 status != SILC_STATUS_LIST_ITEM &&
428 status != SILC_STATUS_LIST_END) {
429 /* Send the error message */
430 silc_server_command_send_status_reply(cmd, command, status);
437 /******************************************************************************
441 ******************************************************************************/
444 silc_server_command_whois_parse(SilcServerCommandContext cmd,
445 SilcClientID ***client_id,
446 uint32 *client_id_count,
454 uint32 argc = silc_argument_get_arg_num(cmd->args);
457 /* If client ID is in the command it must be used instead of nickname */
458 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
460 /* No ID, get the nickname@server string and parse it. */
461 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
463 if (strchr(tmp, '@')) {
464 len = strcspn(tmp, "@");
465 *nickname = silc_calloc(len + 1, sizeof(char));
466 memcpy(*nickname, tmp, len);
467 *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
468 memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
470 *nickname = strdup(tmp);
473 silc_server_command_send_status_reply(cmd, command,
474 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
478 /* Command includes ID, we must use that. Also check whether the command
479 has more than one ID set - take them all. */
481 *client_id = silc_calloc(1, sizeof(**client_id));
482 (*client_id)[0] = silc_id_payload_parse_id(tmp, len);
483 if ((*client_id)[0] == NULL) {
484 silc_free(*client_id);
487 *client_id_count = 1;
489 /* Take all ID's from the command packet */
491 for (k = 1, i = 1; i < argc; i++) {
492 tmp = silc_argument_get_arg_type(cmd->args, i + 3, &len);
494 *client_id = silc_realloc(*client_id, sizeof(**client_id) *
495 (*client_id_count + 1));
496 (*client_id)[k] = silc_id_payload_parse_id(tmp, len);
497 if ((*client_id)[k] == NULL) {
498 /* Cleanup all and fail */
499 for (i = 0; i < *client_id_count; i++)
500 silc_free((*client_id)[i]);
501 silc_free(*client_id);
504 (*client_id_count)++;
511 /* Get the max count of reply messages allowed */
512 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
522 silc_server_command_whois_check(SilcServerCommandContext cmd,
523 SilcClientEntry *clients,
524 uint32 clients_count)
526 SilcServer server = cmd->server;
528 SilcClientEntry entry;
530 for (i = 0; i < clients_count; i++) {
533 if (!entry || entry->data.registered == FALSE)
536 if (!entry->nickname || !entry->username || !entry->userinfo) {
543 old_ident = silc_command_get_ident(cmd->payload);
544 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
545 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
547 /* Send WHOIS command */
548 silc_server_packet_send(server, entry->router->connection,
549 SILC_PACKET_COMMAND, cmd->packet->flags,
550 tmpbuf->data, tmpbuf->len, TRUE);
552 /* Reprocess this packet after received reply */
553 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
554 silc_command_get_ident(cmd->payload),
555 silc_server_command_destructor,
556 silc_server_command_whois,
557 silc_server_command_dup(cmd));
560 silc_command_set_ident(cmd->payload, old_ident);
562 silc_buffer_free(tmpbuf);
571 silc_server_command_whois_send_reply(SilcServerCommandContext cmd,
572 SilcClientEntry *clients,
573 uint32 clients_count,
576 SilcServer server = cmd->server;
579 SilcBuffer packet, idp, channels;
580 SilcClientEntry entry;
581 SilcCommandStatus status;
582 uint16 ident = silc_command_get_ident(cmd->payload);
583 char nh[256], uh[256];
584 unsigned char idle[4], mode[4];
585 SilcSocketConnection hsock;
588 for (i = 0; i < clients_count; i++)
589 if (clients[i]->data.registered)
592 status = SILC_STATUS_OK;
594 status = SILC_STATUS_LIST_START;
596 for (i = 0, k = 0; i < clients_count; i++) {
599 if (entry->data.registered == FALSE) {
600 if (clients_count == 1) {
601 if (entry->nickname) {
602 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
603 SILC_STATUS_ERR_NO_SUCH_NICK,
605 strlen(entry->nickname));
607 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
608 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
609 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
610 2, idp->data, idp->len);
611 silc_buffer_free(idp);
618 status = SILC_STATUS_LIST_ITEM;
620 if (clients_count > 1 && k == clients_count - 1)
621 status = SILC_STATUS_LIST_END;
623 if (count && k - 1 == count)
624 status = SILC_STATUS_LIST_END;
626 if (count && k - 1 > count)
629 /* Sanity check, however these should never fail. However, as
630 this sanity check has been added here they have failed. */
631 if (!entry->nickname || !entry->username || !entry->userinfo)
634 /* Send WHOIS reply */
635 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
636 tmp = silc_argument_get_first_arg(cmd->args, NULL);
638 memset(uh, 0, sizeof(uh));
639 memset(nh, 0, sizeof(nh));
640 memset(idle, 0, sizeof(idle));
642 strncat(nh, entry->nickname, strlen(entry->nickname));
643 if (!strchr(entry->nickname, '@')) {
645 if (entry->servername) {
646 strncat(nh, entry->servername, strlen(entry->servername));
648 len = entry->router ? strlen(entry->router->server_name) :
649 strlen(server->server_name);
650 strncat(nh, entry->router ? entry->router->server_name :
651 server->server_name, len);
655 strncat(uh, entry->username, strlen(entry->username));
656 if (!strchr(entry->username, '@')) {
658 hsock = (SilcSocketConnection)entry->connection;
659 len = strlen(hsock->hostname);
660 strncat(uh, hsock->hostname, len);
663 channels = silc_server_get_client_channel_list(server, entry);
665 SILC_PUT32_MSB(entry->mode, mode);
667 if (entry->connection) {
668 SILC_PUT32_MSB((time(NULL) - entry->data.last_receive), idle);
672 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
674 2, idp->data, idp->len,
678 strlen(entry->userinfo),
684 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
686 2, idp->data, idp->len,
690 strlen(entry->userinfo),
694 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
695 0, packet->data, packet->len, FALSE);
697 silc_buffer_free(packet);
698 silc_buffer_free(idp);
700 silc_buffer_free(channels);
707 silc_server_command_whois_process(SilcServerCommandContext cmd)
709 SilcServer server = cmd->server;
710 char *nick = NULL, *server_name = NULL;
712 SilcClientEntry *clients = NULL, entry;
713 SilcClientID **client_id = NULL;
714 uint32 client_id_count = 0, clients_count = 0;
716 bool check_global = FALSE;
718 /* Protocol dictates that we must always send the received WHOIS request
719 to our router if we are normal server, so let's do it now unless we
720 are standalone. We will not send any replies to the client until we
721 have received reply from the router. */
722 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
723 server->server_type == SILC_SERVER && !cmd->pending &&
724 !server->standalone) {
728 old_ident = silc_command_get_ident(cmd->payload);
729 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
730 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
732 /* Send WHOIS command to our router */
733 silc_server_packet_send(server, (SilcSocketConnection)
734 server->router->connection,
735 SILC_PACKET_COMMAND, cmd->packet->flags,
736 tmpbuf->data, tmpbuf->len, TRUE);
738 /* Reprocess this packet after received reply from router */
739 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
740 silc_command_get_ident(cmd->payload),
741 silc_server_command_destructor,
742 silc_server_command_whois,
743 silc_server_command_dup(cmd));
746 silc_command_set_ident(cmd->payload, old_ident);
748 silc_buffer_free(tmpbuf);
753 /* We are ready to process the command request. Let's search for the
754 requested client and send reply to the requesting client. */
756 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
758 else if (server->server_type == SILC_ROUTER)
761 /* Parse the whois request */
762 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
763 &nick, &server_name, &count,
767 /* Get all clients matching that ID or nickname from local list */
768 if (client_id_count) {
769 /* Check all Client ID's received in the command packet */
770 for (i = 0; i < client_id_count; i++) {
771 entry = silc_idlist_find_client_by_id(server->local_list,
772 client_id[i], TRUE, NULL);
773 if (!entry && check_global)
774 entry = silc_idlist_find_client_by_id(server->global_list,
775 client_id[i], TRUE, NULL);
777 clients = silc_realloc(clients, sizeof(*clients) *
778 (clients_count + 1));
779 clients[clients_count++] = entry;
783 if (!silc_idlist_get_clients_by_hash(server->local_list,
784 nick, server->md5hash,
785 &clients, &clients_count))
786 silc_idlist_get_clients_by_nickname(server->local_list,
788 &clients, &clients_count);
790 if (!silc_idlist_get_clients_by_hash(server->global_list,
791 nick, server->md5hash,
792 &clients, &clients_count))
793 silc_idlist_get_clients_by_nickname(server->global_list,
795 &clients, &clients_count);
800 /* Such client(s) really does not exist in the SILC network. */
801 if (!client_id_count) {
802 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
803 SILC_STATUS_ERR_NO_SUCH_NICK,
804 3, nick, strlen(nick));
806 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
807 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
808 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
809 2, idp->data, idp->len);
810 silc_buffer_free(idp);
815 /* Router always finds the client entry if it exists in the SILC network.
816 However, it might be incomplete entry and does not include all the
817 mandatory fields that WHOIS command reply requires. Check for these and
818 make query from the server who owns the client if some fields are
820 if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
825 /* Send the command reply */
826 silc_server_command_whois_send_reply(cmd, clients, clients_count,
830 if (client_id_count) {
831 for (i = 0; i < client_id_count; i++)
832 silc_free(client_id[i]);
833 silc_free(client_id);
840 silc_free(server_name);
845 /* Server side of command WHOIS. Processes user's query and sends found
846 results as command replies back to the client. */
848 SILC_SERVER_CMD_FUNC(whois)
850 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
853 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 3328);
855 ret = silc_server_command_whois_process(cmd);
858 silc_server_command_free(cmd);
861 /******************************************************************************
865 ******************************************************************************/
868 silc_server_command_whowas_parse(SilcServerCommandContext cmd,
876 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
878 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WHOWAS,
879 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
883 /* Get the nickname@server string and parse it. */
884 if (strchr(tmp, '@')) {
885 len = strcspn(tmp, "@");
886 *nickname = silc_calloc(len + 1, sizeof(char));
887 memcpy(*nickname, tmp, len);
888 *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
889 memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
891 *nickname = strdup(tmp);
893 /* Get the max count of reply messages allowed */
894 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
904 silc_server_command_whowas_check(SilcServerCommandContext cmd,
905 SilcClientEntry *clients,
906 uint32 clients_count)
908 SilcServer server = cmd->server;
910 SilcClientEntry entry;
912 for (i = 0; i < clients_count; i++) {
915 if (!entry->nickname || !entry->username) {
922 old_ident = silc_command_get_ident(cmd->payload);
923 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
924 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
926 /* Send WHOWAS command */
927 silc_server_packet_send(server, entry->router->connection,
928 SILC_PACKET_COMMAND, cmd->packet->flags,
929 tmpbuf->data, tmpbuf->len, TRUE);
931 /* Reprocess this packet after received reply */
932 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
933 silc_command_get_ident(cmd->payload),
934 silc_server_command_destructor,
935 silc_server_command_whowas,
936 silc_server_command_dup(cmd));
939 silc_command_set_ident(cmd->payload, old_ident);
941 silc_buffer_free(tmpbuf);
950 silc_server_command_whowas_send_reply(SilcServerCommandContext cmd,
951 SilcClientEntry *clients,
952 uint32 clients_count)
954 SilcServer server = cmd->server;
956 int i, count = 0, len;
957 SilcBuffer packet, idp;
958 SilcClientEntry entry = NULL;
959 SilcCommandStatus status;
960 uint16 ident = silc_command_get_ident(cmd->payload);
962 char nh[256], uh[256];
964 status = SILC_STATUS_OK;
965 if (clients_count > 1)
966 status = SILC_STATUS_LIST_START;
968 for (i = 0; i < clients_count; i++) {
971 /* We will take only clients that are not valid anymore. They are the
972 ones that are not registered anymore but still have a ID. They
973 have disconnected us, and thus valid for WHOWAS. */
974 if (entry->data.registered == TRUE)
976 if (entry->id == NULL)
979 if (count && i - 1 == count)
984 if (clients_count > 2)
985 status = SILC_STATUS_LIST_ITEM;
987 if (clients_count > 1 && i == clients_count - 1)
988 status = SILC_STATUS_LIST_END;
990 /* Sanity check, however these should never fail. However, as
991 this sanity check has been added here they have failed. */
992 if (!entry->nickname || !entry->username)
995 /* Send WHOWAS reply */
996 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
997 tmp = silc_argument_get_first_arg(cmd->args, NULL);
999 memset(uh, 0, sizeof(uh));
1000 memset(nh, 0, sizeof(nh));
1002 strncat(nh, entry->nickname, strlen(entry->nickname));
1003 if (!strchr(entry->nickname, '@')) {
1004 strncat(nh, "@", 1);
1005 if (entry->servername) {
1006 strncat(nh, entry->servername, strlen(entry->servername));
1008 len = entry->router ? strlen(entry->router->server_name) :
1009 strlen(server->server_name);
1010 strncat(nh, entry->router ? entry->router->server_name :
1011 server->server_name, len);
1015 strncat(uh, entry->username, strlen(entry->username));
1016 if (!strchr(entry->username, '@')) {
1017 strncat(uh, "@", 1);
1018 strcat(uh, "*private*");
1021 if (entry->userinfo)
1023 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1025 2, idp->data, idp->len,
1029 strlen(entry->userinfo));
1032 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1034 2, idp->data, idp->len,
1038 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1039 0, packet->data, packet->len, FALSE);
1041 silc_buffer_free(packet);
1042 silc_buffer_free(idp);
1045 if (found == FALSE && entry)
1046 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1047 SILC_STATUS_ERR_NO_SUCH_NICK,
1049 strlen(entry->nickname));
1053 silc_server_command_whowas_process(SilcServerCommandContext cmd)
1055 SilcServer server = cmd->server;
1056 char *nick = NULL, *server_name = NULL;
1058 SilcClientEntry *clients = NULL;
1059 uint32 clients_count = 0;
1061 bool check_global = FALSE;
1063 /* Protocol dictates that we must always send the received WHOWAS request
1064 to our router if we are normal server, so let's do it now unless we
1065 are standalone. We will not send any replies to the client until we
1066 have received reply from the router. */
1067 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1068 server->server_type == SILC_SERVER && !cmd->pending &&
1069 !server->standalone) {
1073 old_ident = silc_command_get_ident(cmd->payload);
1074 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1075 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1077 /* Send WHOWAS command to our router */
1078 silc_server_packet_send(server, (SilcSocketConnection)
1079 server->router->connection,
1080 SILC_PACKET_COMMAND, cmd->packet->flags,
1081 tmpbuf->data, tmpbuf->len, TRUE);
1083 /* Reprocess this packet after received reply from router */
1084 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
1085 silc_command_get_ident(cmd->payload),
1086 silc_server_command_destructor,
1087 silc_server_command_whowas,
1088 silc_server_command_dup(cmd));
1089 cmd->pending = TRUE;
1091 silc_command_set_ident(cmd->payload, old_ident);
1093 silc_buffer_free(tmpbuf);
1098 /* We are ready to process the command request. Let's search for the
1099 requested client and send reply to the requesting client. */
1101 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1102 check_global = TRUE;
1103 else if (server->server_type == SILC_ROUTER)
1104 check_global = TRUE;
1106 /* Parse the whowas request */
1107 if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1110 /* Get all clients matching that nickname from local list */
1111 if (!silc_idlist_get_clients_by_nickname(server->local_list,
1113 &clients, &clients_count))
1114 silc_idlist_get_clients_by_hash(server->local_list,
1115 nick, server->md5hash,
1116 &clients, &clients_count);
1118 /* Check global list as well */
1120 if (!silc_idlist_get_clients_by_nickname(server->global_list,
1122 &clients, &clients_count))
1123 silc_idlist_get_clients_by_hash(server->global_list,
1124 nick, server->md5hash,
1125 &clients, &clients_count);
1129 /* Such a client really does not exist in the SILC network. */
1130 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1131 SILC_STATUS_ERR_NO_SUCH_NICK,
1132 3, nick, strlen(nick));
1136 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1137 !silc_server_command_whowas_check(cmd, clients, clients_count)) {
1142 /* Send the command reply to the client */
1143 silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1151 silc_free(server_name);
1156 /* Server side of command WHOWAS. */
1158 SILC_SERVER_CMD_FUNC(whowas)
1160 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1163 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
1165 ret = silc_server_command_whowas_process(cmd);
1168 silc_server_command_free(cmd);
1171 /******************************************************************************
1175 ******************************************************************************/
1178 silc_server_command_identify_parse(SilcServerCommandContext cmd,
1179 SilcClientEntry **clients,
1180 uint32 *clients_count,
1181 SilcServerEntry **servers,
1182 uint32 *servers_count,
1183 SilcChannelEntry **channels,
1184 uint32 *channels_count,
1187 SilcServer server = cmd->server;
1190 uint32 argc = silc_argument_get_arg_num(cmd->args);
1192 bool check_global = FALSE;
1197 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1198 check_global = TRUE;
1199 else if (server->server_type == SILC_ROUTER)
1200 check_global = TRUE;
1202 /* If ID Payload is in the command it must be used instead of names */
1203 tmp = silc_argument_get_arg_type(cmd->args, 5, &len);
1205 /* No ID, get the names. */
1207 /* Try to get nickname@server. */
1208 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
1211 char *nick_server = NULL;
1213 if (strchr(tmp, '@')) {
1214 len = strcspn(tmp, "@");
1215 nick = silc_calloc(len + 1, sizeof(char));
1216 memcpy(nick, tmp, len);
1217 nick_server = silc_calloc(strlen(tmp) - len, sizeof(char));
1218 memcpy(nick_server, tmp + len + 1, strlen(tmp) - len - 1);
1223 if (!silc_idlist_get_clients_by_hash(server->local_list,
1224 nick, server->md5hash,
1225 clients, clients_count))
1226 silc_idlist_get_clients_by_nickname(server->local_list,
1228 clients, clients_count);
1230 if (!silc_idlist_get_clients_by_hash(server->global_list,
1231 nick, server->md5hash,
1232 clients, clients_count))
1233 silc_idlist_get_clients_by_nickname(server->global_list,
1235 clients, clients_count);
1239 silc_free(nick_server);
1242 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1243 SILC_STATUS_ERR_NO_SUCH_NICK,
1244 3, tmp, strlen(tmp));
1249 /* Try to get server name */
1250 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
1252 entry = silc_idlist_find_server_by_name(server->local_list,
1254 if (!entry && check_global)
1255 entry = silc_idlist_find_server_by_name(server->local_list,
1258 *servers = silc_realloc(*servers, sizeof(**servers) *
1259 (*servers_count + 1));
1260 (*servers)[(*servers_count)++] = entry;
1264 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1265 SILC_STATUS_ERR_NO_SUCH_SERVER,
1266 3, tmp, strlen(tmp));
1271 /* Try to get channel name */
1272 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
1274 entry = silc_idlist_find_channel_by_name(server->local_list,
1276 if (!entry && check_global)
1277 entry = silc_idlist_find_channel_by_name(server->local_list,
1280 *channels = silc_realloc(*channels, sizeof(**channels) *
1281 (*channels_count + 1));
1282 (*channels)[(*channels_count)++] = entry;
1286 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1287 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
1288 3, tmp, strlen(tmp));
1293 if (!(*clients) && !(*servers) && !(*channels)) {
1294 silc_server_command_send_status_reply(cmd, SILC_COMMAND_IDENTIFY,
1295 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1299 /* Command includes ID, we must use that. Also check whether the command
1300 has more than one ID set - take them all. */
1302 /* Take all ID's from the command packet */
1303 for (i = 0; i < argc; i++) {
1306 tmp = silc_argument_get_arg_type(cmd->args, i + 5, &len);
1310 idp = silc_id_payload_parse_data(tmp, len);
1312 silc_free(*clients);
1313 silc_free(*servers);
1314 silc_free(*channels);
1315 silc_server_command_send_status_reply(cmd, SILC_COMMAND_IDENTIFY,
1316 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1320 id = silc_id_payload_get_id(idp);
1322 switch (silc_id_payload_get_type(idp)) {
1324 case SILC_ID_CLIENT:
1325 entry = (void *)silc_idlist_find_client_by_id(server->local_list,
1327 if (!entry && check_global)
1328 entry = (void *)silc_idlist_find_client_by_id(server->global_list,
1331 *clients = silc_realloc(*clients, sizeof(**clients) *
1332 (*clients_count + 1));
1333 (*clients)[(*clients_count)++] = (SilcClientEntry)entry;
1335 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1336 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1343 case SILC_ID_SERVER:
1344 entry = (void *)silc_idlist_find_server_by_id(server->local_list,
1346 if (!entry && check_global)
1347 entry = (void *)silc_idlist_find_server_by_id(server->global_list,
1350 *servers = silc_realloc(*servers, sizeof(**servers) *
1351 (*servers_count + 1));
1352 (*servers)[(*servers_count)++] = (SilcServerEntry)entry;
1354 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1355 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
1361 case SILC_ID_CHANNEL:
1362 entry = (void *)silc_idlist_find_channel_by_id(server->local_list,
1364 if (!entry && check_global)
1365 entry = (void *)silc_idlist_find_channel_by_id(server->global_list,
1368 *channels = silc_realloc(*channels, sizeof(**channels) *
1369 (*channels_count + 1));
1370 (*channels)[(*channels_count)++] = (SilcChannelEntry)entry;
1372 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1373 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
1385 silc_free(*clients);
1386 silc_free(*servers);
1387 silc_free(*channels);
1391 /* Get the max count of reply messages allowed */
1392 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
1401 /* Checks that all mandatory fields in client entry are present. If not
1402 then send WHOIS request to the server who owns the client. We use
1403 WHOIS because we want to get as much information as possible at once. */
1406 silc_server_command_identify_check_client(SilcServerCommandContext cmd,
1407 SilcClientEntry *clients,
1408 uint32 clients_count)
1410 SilcServer server = cmd->server;
1412 SilcClientEntry entry;
1414 for (i = 0; i < clients_count; i++) {
1417 if (!entry || entry->data.registered == FALSE)
1420 if (!entry->nickname) {
1427 old_ident = silc_command_get_ident(cmd->payload);
1428 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1429 silc_command_set_command(cmd->payload, SILC_COMMAND_WHOIS);
1430 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1432 /* Send WHOIS request. We send WHOIS since we're doing the requesting
1433 now anyway so make it a good one. */
1434 silc_server_packet_send(server, entry->router->connection,
1435 SILC_PACKET_COMMAND, cmd->packet->flags,
1436 tmpbuf->data, tmpbuf->len, TRUE);
1438 /* Reprocess this packet after received reply */
1439 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1440 silc_command_get_ident(cmd->payload),
1441 silc_server_command_destructor,
1442 silc_server_command_identify,
1443 silc_server_command_dup(cmd));
1445 cmd->pending = TRUE;
1447 /* Put old data back to the Command Payload we just changed */
1448 silc_command_set_ident(cmd->payload, old_ident);
1449 silc_command_set_command(cmd->payload, SILC_COMMAND_IDENTIFY);
1451 silc_buffer_free(tmpbuf);
1460 silc_server_command_identify_send_reply(SilcServerCommandContext cmd,
1461 SilcClientEntry *clients,
1462 uint32 clients_count,
1463 SilcServerEntry *servers,
1464 uint32 servers_count,
1465 SilcChannelEntry *channels,
1466 uint32 channels_count,
1469 SilcServer server = cmd->server;
1471 SilcBuffer packet, idp;
1472 SilcCommandStatus status;
1473 uint16 ident = silc_command_get_ident(cmd->payload);
1474 char nh[256], uh[256];
1475 SilcSocketConnection hsock;
1477 status = SILC_STATUS_OK;
1480 SilcClientEntry entry;
1483 for (i = 0; i < clients_count; i++)
1484 if (clients[i]->data.registered)
1488 status = SILC_STATUS_LIST_START;
1490 for (i = 0, k = 0; i < clients_count; i++) {
1493 if (entry->data.registered == FALSE) {
1494 if (clients_count == 1) {
1495 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1496 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1497 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1498 2, idp->data, idp->len);
1499 silc_buffer_free(idp);
1505 status = SILC_STATUS_LIST_ITEM;
1506 if (clients_count > 1 && k == clients_count - 1
1507 && !servers_count && !channels_count)
1508 status = SILC_STATUS_LIST_END;
1509 if (count && k - 1 == count)
1510 status = SILC_STATUS_LIST_END;
1511 if (count && k - 1 > count)
1514 /* Send IDENTIFY reply */
1515 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1517 memset(uh, 0, sizeof(uh));
1518 memset(nh, 0, sizeof(nh));
1520 strncat(nh, entry->nickname, strlen(entry->nickname));
1521 if (!strchr(entry->nickname, '@')) {
1522 strncat(nh, "@", 1);
1523 if (entry->servername) {
1524 strncat(nh, entry->servername, strlen(entry->servername));
1526 len = entry->router ? strlen(entry->router->server_name) :
1527 strlen(server->server_name);
1528 strncat(nh, entry->router ? entry->router->server_name :
1529 server->server_name, len);
1533 if (!entry->username) {
1534 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1536 2, idp->data, idp->len,
1539 strncat(uh, entry->username, strlen(entry->username));
1540 if (!strchr(entry->username, '@')) {
1541 strncat(uh, "@", 1);
1542 hsock = (SilcSocketConnection)entry->connection;
1543 len = strlen(hsock->hostname);
1544 strncat(uh, hsock->hostname, len);
1547 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1549 2, idp->data, idp->len,
1554 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1555 0, packet->data, packet->len, FALSE);
1557 silc_buffer_free(packet);
1558 silc_buffer_free(idp);
1564 status = (status == SILC_STATUS_LIST_ITEM ?
1565 SILC_STATUS_LIST_ITEM : SILC_STATUS_OK);
1568 SilcServerEntry entry;
1570 if (status == SILC_STATUS_OK && servers_count > 1)
1571 status = SILC_STATUS_LIST_START;
1573 for (i = 0, k = 0; i < servers_count; i++) {
1577 status = SILC_STATUS_LIST_ITEM;
1578 if (servers_count > 1 && k == servers_count - 1 && !channels_count)
1579 status = SILC_STATUS_LIST_END;
1580 if (count && k - 1 == count)
1581 status = SILC_STATUS_LIST_END;
1582 if (count && k - 1 > count)
1585 /* Send IDENTIFY reply */
1586 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1587 if (entry->server_name) {
1589 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1591 2, idp->data, idp->len,
1592 3, entry->server_name,
1593 strlen(entry->server_name));
1595 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1597 2, idp->data, idp->len);
1600 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1601 0, packet->data, packet->len, FALSE);
1603 silc_buffer_free(packet);
1604 silc_buffer_free(idp);
1610 status = (status == SILC_STATUS_LIST_ITEM ?
1611 SILC_STATUS_LIST_ITEM : SILC_STATUS_OK);
1614 SilcChannelEntry entry;
1616 if (status == SILC_STATUS_OK && channels_count > 1)
1617 status = SILC_STATUS_LIST_START;
1619 for (i = 0, k = 0; i < channels_count; i++) {
1620 entry = channels[i];
1623 status = SILC_STATUS_LIST_ITEM;
1624 if (channels_count > 1 && k == channels_count - 1)
1625 status = SILC_STATUS_LIST_END;
1626 if (count && k - 1 == count)
1627 status = SILC_STATUS_LIST_END;
1628 if (count && k - 1 > count)
1631 /* Send IDENTIFY reply */
1632 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1633 if (entry->channel_name) {
1635 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1637 2, idp->data, idp->len,
1638 3, entry->channel_name,
1639 strlen(entry->channel_name));
1641 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1643 2, idp->data, idp->len);
1646 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1647 0, packet->data, packet->len, FALSE);
1649 silc_buffer_free(packet);
1650 silc_buffer_free(idp);
1658 silc_server_command_identify_process(SilcServerCommandContext cmd)
1660 SilcServer server = cmd->server;
1663 SilcClientEntry *clients = NULL;
1664 SilcServerEntry *servers = NULL;
1665 SilcChannelEntry *channels = NULL;
1666 uint32 clients_count = 0, servers_count = 0, channels_count = 0;
1668 /* Protocol dictates that we must always send the received IDENTIFY request
1669 to our router if we are normal server, so let's do it now unless we
1670 are standalone. We will not send any replies to the client until we
1671 have received reply from the router. */
1672 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1673 server->server_type == SILC_SERVER && !cmd->pending &&
1674 !server->standalone) {
1678 old_ident = silc_command_get_ident(cmd->payload);
1679 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1680 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1682 /* Send IDENTIFY command to our router */
1683 silc_server_packet_send(server, (SilcSocketConnection)
1684 server->router->connection,
1685 SILC_PACKET_COMMAND, cmd->packet->flags,
1686 tmpbuf->data, tmpbuf->len, TRUE);
1688 /* Reprocess this packet after received reply from router */
1689 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
1690 silc_command_get_ident(cmd->payload),
1691 silc_server_command_destructor,
1692 silc_server_command_identify,
1693 silc_server_command_dup(cmd));
1694 cmd->pending = TRUE;
1696 silc_command_set_ident(cmd->payload, old_ident);
1698 silc_buffer_free(tmpbuf);
1703 /* We are ready to process the command request. Let's search for the
1704 requested client and send reply to the requesting client. */
1706 /* Parse the IDENTIFY request */
1707 if (!silc_server_command_identify_parse(cmd,
1708 &clients, &clients_count,
1709 &servers, &servers_count,
1710 &channels, &channels_count,
1714 /* Check that all mandatory fields are present and request those data
1715 from the server who owns the client if necessary. */
1716 if (clients && !silc_server_command_identify_check_client(cmd, clients,
1722 /* Send the command reply to the client */
1723 silc_server_command_identify_send_reply(cmd,
1724 clients, clients_count,
1725 servers, servers_count,
1726 channels, channels_count,
1732 silc_free(channels);
1737 SILC_SERVER_CMD_FUNC(identify)
1739 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1742 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 3328);
1744 ret = silc_server_command_identify_process(cmd);
1747 silc_server_command_free(cmd);
1750 /* Checks string for bad characters and returns TRUE if they are found. */
1752 static int silc_server_command_bad_chars(char *nick)
1754 if (strchr(nick, '\\')) return TRUE;
1755 if (strchr(nick, '\"')) return TRUE;
1756 if (strchr(nick, '´')) return TRUE;
1757 if (strchr(nick, '`')) return TRUE;
1758 if (strchr(nick, '\'')) return TRUE;
1759 if (strchr(nick, '*')) return TRUE;
1760 if (strchr(nick, '/')) return TRUE;
1761 if (strchr(nick, '@')) return TRUE;
1766 /* Server side of command NICK. Sets nickname for user. Setting
1767 nickname causes generation of a new client ID for the client. The
1768 new client ID is sent to the client after changing the nickname. */
1770 SILC_SERVER_CMD_FUNC(nick)
1772 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1773 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1774 SilcServer server = cmd->server;
1775 SilcBuffer packet, nidp, oidp;
1776 SilcClientID *new_id;
1778 uint16 ident = silc_command_get_ident(cmd->payload);
1781 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1784 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
1786 /* Check nickname */
1787 nick = silc_argument_get_arg_type(cmd->args, 1, NULL);
1788 if (silc_server_command_bad_chars(nick) == TRUE) {
1789 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
1790 SILC_STATUS_ERR_BAD_NICKNAME);
1794 if (strlen(nick) > 128)
1797 /* Create new Client ID */
1798 while (!silc_id_create_client_id(cmd->server, cmd->server->id,
1800 cmd->server->md5hash, nick,
1803 snprintf(&nick[strlen(nick) - 1], 1, "%d", nickfail);
1806 /* Send notify about nickname change to our router. We send the new
1807 ID and ask to replace it with the old one. If we are router the
1808 packet is broadcasted. Send NICK_CHANGE notify. */
1809 if (!server->standalone)
1810 silc_server_send_notify_nick_change(server, server->router->connection,
1811 server->server_type == SILC_SERVER ?
1812 FALSE : TRUE, client->id,
1815 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1817 /* Remove old cache entry */
1818 silc_idcache_del_by_context(server->local_list->clients, client);
1821 silc_free(client->id);
1823 /* Save the nickname as this client is our local client */
1824 silc_free(client->nickname);
1826 client->nickname = strdup(nick);
1827 client->id = new_id;
1829 /* Update client cache */
1830 silc_idcache_add(server->local_list->clients, client->nickname,
1831 client->id, (void *)client, FALSE);
1833 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1835 /* Send NICK_CHANGE notify to the client's channels */
1836 silc_server_send_notify_on_channels(server, NULL, client,
1837 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
1838 oidp->data, oidp->len,
1839 nidp->data, nidp->len);
1841 /* Send the new Client ID as reply command back to client */
1842 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
1843 SILC_STATUS_OK, ident, 1,
1844 2, nidp->data, nidp->len);
1845 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1846 0, packet->data, packet->len, FALSE);
1848 silc_buffer_free(packet);
1849 silc_buffer_free(nidp);
1850 silc_buffer_free(oidp);
1853 silc_server_command_free(cmd);
1856 /* Sends the LIST command reply */
1859 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
1860 SilcChannelEntry *lch,
1862 SilcChannelEntry *gch,
1866 SilcBuffer packet, idp;
1867 SilcChannelEntry entry;
1868 SilcCommandStatus status;
1869 uint16 ident = silc_command_get_ident(cmd->payload);
1871 unsigned char usercount[4];
1874 for (i = 0; i < lch_count; i++)
1875 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1877 for (i = 0; i < gch_count; i++)
1878 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1881 status = SILC_STATUS_OK;
1882 if ((lch_count + gch_count) > 1)
1883 status = SILC_STATUS_LIST_START;
1886 for (i = 0; i < lch_count; i++) {
1893 status = SILC_STATUS_LIST_ITEM;
1895 if (i == lch_count - 1 && gch_count)
1897 if (lch_count > 1 && i == lch_count - 1)
1898 status = SILC_STATUS_LIST_END;
1900 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1902 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1903 topic = "*private*";
1904 memset(usercount, 0, sizeof(usercount));
1906 topic = entry->topic;
1907 users = silc_hash_table_count(entry->user_list);
1908 SILC_PUT32_MSB(users, usercount);
1911 /* Send the reply */
1914 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1916 2, idp->data, idp->len,
1917 3, entry->channel_name,
1918 strlen(entry->channel_name),
1919 4, topic, strlen(topic),
1923 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1925 2, idp->data, idp->len,
1926 3, entry->channel_name,
1927 strlen(entry->channel_name),
1929 silc_server_packet_send(cmd->server, cmd->sock,
1930 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1931 packet->len, FALSE);
1932 silc_buffer_free(packet);
1933 silc_buffer_free(idp);
1936 status = i ? SILC_STATUS_LIST_ITEM : SILC_STATUS_OK;
1939 for (i = 0; i < gch_count; i++) {
1946 status = SILC_STATUS_LIST_ITEM;
1948 if (gch_count > 1 && i == lch_count - 1)
1949 status = SILC_STATUS_LIST_END;
1951 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1953 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1954 topic = "*private*";
1955 memset(usercount, 0, sizeof(usercount));
1957 topic = entry->topic;
1958 users = silc_hash_table_count(entry->user_list);
1959 SILC_PUT32_MSB(users, usercount);
1962 /* Send the reply */
1965 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1967 2, idp->data, idp->len,
1968 3, entry->channel_name,
1969 strlen(entry->channel_name),
1970 4, topic, strlen(topic),
1974 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1976 2, idp->data, idp->len,
1977 3, entry->channel_name,
1978 strlen(entry->channel_name),
1980 silc_server_packet_send(cmd->server, cmd->sock,
1981 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1982 packet->len, FALSE);
1983 silc_buffer_free(packet);
1984 silc_buffer_free(idp);
1988 /* Server side of LIST command. This lists the channel of the requested
1989 server. Secret channels are not listed. */
1991 SILC_SERVER_CMD_FUNC(list)
1993 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1994 SilcServer server = cmd->server;
1995 SilcChannelID *channel_id = NULL;
1998 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
1999 uint32 lch_count = 0, gch_count = 0;
2001 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 2);
2003 /* Get Channel ID */
2004 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2006 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2008 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
2009 SILC_STATUS_ERR_NO_CHANNEL_ID);
2014 /* Get the channels from local list */
2015 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
2018 /* Get the channels from global list if we are router */
2019 if (server->server_type == SILC_ROUTER)
2020 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
2023 /* Send the reply */
2024 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
2025 gchannels, gch_count);
2028 silc_server_command_free(cmd);
2031 /* Server side of TOPIC command. Sets topic for channel and/or returns
2032 current topic to client. */
2034 SILC_SERVER_CMD_FUNC(topic)
2036 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2037 SilcServer server = cmd->server;
2038 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2039 SilcChannelID *channel_id;
2040 SilcChannelEntry channel;
2041 SilcChannelClientEntry chl;
2042 SilcBuffer packet, idp;
2044 uint32 argc, tmp_len;
2045 uint16 ident = silc_command_get_ident(cmd->payload);
2047 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
2049 argc = silc_argument_get_arg_num(cmd->args);
2051 /* Get Channel ID */
2052 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2054 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2055 SILC_STATUS_ERR_NO_CHANNEL_ID);
2058 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2060 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2061 SILC_STATUS_ERR_NO_CHANNEL_ID);
2065 /* Check whether the channel exists */
2066 channel = silc_idlist_find_channel_by_id(server->local_list,
2069 channel = silc_idlist_find_channel_by_id(server->global_list,
2072 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2073 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2080 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
2082 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2083 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2087 if (strlen(tmp) > 256) {
2088 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2089 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2093 /* See whether the client is on channel and has rights to change topic */
2094 if (!silc_hash_table_find(channel->user_list, client, NULL,
2096 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2097 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2101 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2102 if (channel->mode & SILC_CHANNEL_MODE_TOPIC) {
2103 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2104 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2109 /* Set the topic for channel */
2111 silc_free(channel->topic);
2112 channel->topic = strdup(tmp);
2114 /* Send TOPIC_SET notify type to the network */
2115 if (!server->standalone)
2116 silc_server_send_notify_topic_set(server, server->router->connection,
2117 server->server_type == SILC_ROUTER ?
2118 TRUE : FALSE, channel, client->id,
2121 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2123 /* Send notify about topic change to all clients on the channel */
2124 silc_server_send_notify_to_channel(server, NULL, channel, TRUE,
2125 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
2126 idp->data, idp->len,
2127 channel->topic, strlen(channel->topic));
2128 silc_buffer_free(idp);
2131 /* Send the topic to client as reply packet */
2132 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2134 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2135 SILC_STATUS_OK, ident, 2,
2136 2, idp->data, idp->len,
2138 strlen(channel->topic));
2140 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2141 SILC_STATUS_OK, ident, 1,
2142 2, idp->data, idp->len);
2143 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2144 0, packet->data, packet->len, FALSE);
2146 silc_buffer_free(packet);
2147 silc_buffer_free(idp);
2148 silc_free(channel_id);
2151 silc_server_command_free(cmd);
2154 /* Server side of INVITE command. Invites some client to join some channel.
2155 This command is also used to manage the invite list of the channel. */
2157 SILC_SERVER_CMD_FUNC(invite)
2159 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2160 SilcServer server = cmd->server;
2161 SilcSocketConnection sock = cmd->sock, dest_sock;
2162 SilcChannelClientEntry chl;
2163 SilcClientEntry sender, dest;
2164 SilcClientID *dest_id = NULL;
2165 SilcChannelEntry channel;
2166 SilcChannelID *channel_id = NULL;
2167 SilcIDListData idata;
2168 SilcBuffer idp, idp2, packet;
2169 unsigned char *tmp, *add, *del;
2171 uint16 ident = silc_command_get_ident(cmd->payload);
2173 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
2175 /* Get Channel ID */
2176 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2178 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2179 SILC_STATUS_ERR_NO_CHANNEL_ID);
2182 channel_id = silc_id_payload_parse_id(tmp, len);
2184 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2185 SILC_STATUS_ERR_NO_CHANNEL_ID);
2189 /* Get the channel entry */
2190 channel = silc_idlist_find_channel_by_id(server->local_list,
2193 channel = silc_idlist_find_channel_by_id(server->global_list,
2196 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2197 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2202 /* Check whether the sender of this command is on the channel. */
2203 sender = (SilcClientEntry)sock->user_data;
2204 if (!silc_server_client_on_channel(sender, channel)) {
2205 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2206 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2210 /* Check whether the channel is invite-only channel. If yes then the
2211 sender of this command must be at least channel operator. */
2212 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2213 silc_hash_table_find(channel->user_list, sender, NULL, (void *)&chl);
2214 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2215 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2216 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2221 /* Get destination client ID */
2222 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
2226 dest_id = silc_id_payload_parse_id(tmp, len);
2228 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2229 SILC_STATUS_ERR_NO_CLIENT_ID);
2233 /* Get the client entry */
2234 dest = silc_server_get_client_resolve(server, dest_id);
2236 if (server->server_type == SILC_ROUTER) {
2237 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2238 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2242 /* The client info is being resolved. Reprocess this packet after
2243 receiving the reply to the query. */
2244 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2246 silc_server_command_destructor,
2247 silc_server_command_invite,
2248 silc_server_command_dup(cmd));
2249 cmd->pending = TRUE;
2250 silc_free(channel_id);
2255 /* Check whether the requested client is already on the channel. */
2256 if (silc_server_client_on_channel(dest, channel)) {
2257 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2258 SILC_STATUS_ERR_USER_ON_CHANNEL);
2262 /* Get route to the client */
2263 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id, &idata);
2265 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2266 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2270 memset(invite, 0, sizeof(invite));
2271 strncat(invite, dest->nickname, strlen(dest->nickname));
2272 strncat(invite, "!", 1);
2273 strncat(invite, dest->username, strlen(dest->username));
2274 if (!strchr(dest->username, '@')) {
2275 strncat(invite, "@", 1);
2276 strncat(invite, cmd->sock->hostname, strlen(cmd->sock->hostname));
2279 len = strlen(invite);
2280 if (!channel->invite_list)
2281 channel->invite_list = silc_calloc(len + 2,
2282 sizeof(*channel->invite_list));
2284 channel->invite_list = silc_realloc(channel->invite_list,
2285 sizeof(*channel->invite_list) *
2287 strlen(channel->invite_list) + 2));
2288 strncat(channel->invite_list, invite, len);
2289 strncat(channel->invite_list, ",", 1);
2291 /* Send notify to the client that is invited to the channel */
2292 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2293 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
2294 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
2296 SILC_NOTIFY_TYPE_INVITE, 3,
2297 idp->data, idp->len,
2298 channel->channel_name,
2299 strlen(channel->channel_name),
2300 idp2->data, idp2->len);
2301 silc_buffer_free(idp);
2302 silc_buffer_free(idp2);
2305 /* Add the client to the invite list of the channel */
2306 add = silc_argument_get_arg_type(cmd->args, 3, &len);
2308 if (!channel->invite_list)
2309 channel->invite_list = silc_calloc(len + 2,
2310 sizeof(*channel->invite_list));
2312 channel->invite_list = silc_realloc(channel->invite_list,
2313 sizeof(*channel->invite_list) *
2315 strlen(channel->invite_list) + 2));
2316 if (add[len - 1] == ',')
2317 add[len - 1] = '\0';
2319 strncat(channel->invite_list, add, len);
2320 strncat(channel->invite_list, ",", 1);
2323 /* Get the invite to be removed and remove it from the list */
2324 del = silc_argument_get_arg_type(cmd->args, 4, &len);
2325 if (del && channel->invite_list) {
2326 char *start, *end, *n;
2328 if (!strncmp(channel->invite_list, del,
2329 strlen(channel->invite_list) - 1)) {
2330 silc_free(channel->invite_list);
2331 channel->invite_list = NULL;
2333 start = strstr(channel->invite_list, del);
2334 if (start && strlen(start) >= len) {
2336 n = silc_calloc(strlen(channel->invite_list) - len, sizeof(*n));
2337 strncat(n, channel->invite_list, start - channel->invite_list);
2338 strncat(n, end + 1, ((channel->invite_list +
2339 strlen(channel->invite_list)) - end) - 1);
2340 silc_free(channel->invite_list);
2341 channel->invite_list = n;
2346 /* Send notify to the primary router */
2347 if (!server->standalone)
2348 silc_server_send_notify_invite(server, server->router->connection,
2349 server->server_type == SILC_ROUTER ?
2350 TRUE : FALSE, channel,
2351 sender->id, add, del);
2353 /* Send command reply */
2354 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2358 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2359 SILC_STATUS_OK, ident, 2,
2361 3, channel->invite_list,
2362 channel->invite_list ?
2363 strlen(channel->invite_list) : 0);
2366 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2367 SILC_STATUS_OK, ident, 1,
2369 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2370 packet->data, packet->len, FALSE);
2371 silc_buffer_free(packet);
2377 silc_free(channel_id);
2378 silc_server_command_free(cmd);
2383 SilcSocketConnection sock;
2387 /* Quits connection to client. This gets called if client won't
2388 close the connection even when it has issued QUIT command. */
2390 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
2392 QuitInternal q = (QuitInternal)context;
2394 /* Free all client specific data, such as client entry and entires
2395 on channels this client may be on. */
2396 silc_server_free_client_data(q->server, q->sock, q->sock->user_data,
2398 q->sock->user_data = NULL;
2400 /* Close the connection on our side */
2401 silc_server_close_connection(q->server, q->sock);
2403 silc_free(q->signoff);
2407 /* Quits SILC session. This is the normal way to disconnect client. */
2409 SILC_SERVER_CMD_FUNC(quit)
2411 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2412 SilcServer server = cmd->server;
2413 SilcSocketConnection sock = cmd->sock;
2415 unsigned char *tmp = NULL;
2418 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
2420 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2423 /* Get destination ID */
2424 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2428 q = silc_calloc(1, sizeof(*q));
2431 q->signoff = tmp ? strdup(tmp) : NULL;
2433 /* We quit the connection with little timeout */
2434 silc_schedule_task_add(server->schedule, sock->sock,
2435 silc_server_command_quit_cb, (void *)q,
2436 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2439 silc_server_command_free(cmd);
2442 /* Server side of command KILL. This command is used by router operator
2443 to remove an client from the SILC Network temporarily. */
2445 SILC_SERVER_CMD_FUNC(kill)
2447 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2448 SilcServer server = cmd->server;
2449 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2450 SilcClientEntry remote_client;
2451 SilcClientID *client_id;
2452 unsigned char *tmp, *comment;
2453 uint32 tmp_len, tmp_len2;
2455 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 2);
2457 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2460 /* KILL command works only on router */
2461 if (server->server_type != SILC_ROUTER) {
2462 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2463 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2467 /* Check whether client has the permissions. */
2468 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2469 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2470 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2474 /* Get the client ID */
2475 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2477 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2478 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2481 client_id = silc_id_payload_parse_id(tmp, tmp_len);
2483 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2484 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2488 /* Get the client entry */
2489 remote_client = silc_idlist_find_client_by_id(server->local_list,
2490 client_id, TRUE, NULL);
2491 if (!remote_client) {
2492 remote_client = silc_idlist_find_client_by_id(server->global_list,
2493 client_id, TRUE, NULL);
2494 if (!remote_client) {
2495 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2496 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2502 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
2506 /* Send reply to the sender */
2507 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2510 /* Send the KILL notify packets. First send it to the channel, then
2511 to our primary router and then directly to the client who is being
2512 killed right now. */
2514 /* Send KILLED notify to the channels. It is not sent to the client
2515 as it will be sent differently destined directly to the client and not
2517 silc_server_send_notify_on_channels(server, remote_client,
2518 remote_client, SILC_NOTIFY_TYPE_KILLED,
2521 comment, comment ? tmp_len2 : 0);
2523 /* Send KILLED notify to primary route */
2524 if (!server->standalone)
2525 silc_server_send_notify_killed(server, server->router->connection, TRUE,
2526 remote_client->id, comment);
2528 /* Send KILLED notify to the client directly */
2529 silc_server_send_notify_killed(server, remote_client->connection ?
2530 remote_client->connection :
2531 remote_client->router->connection, FALSE,
2532 remote_client->id, comment);
2534 /* Remove the client from all channels. This generates new keys to the
2535 channels as well. */
2536 silc_server_remove_from_channels(server, NULL, remote_client, FALSE,
2539 /* Remove the client entry, If it is locally connected then we will also
2540 disconnect the client here */
2541 if (remote_client->connection) {
2542 /* Remove locally conneted client */
2543 SilcSocketConnection sock = remote_client->connection;
2544 silc_server_free_client_data(server, sock, remote_client, FALSE, NULL);
2545 silc_server_close_connection(server, sock);
2547 /* Remove remote client */
2548 if (!silc_idlist_del_client(server->global_list, remote_client))
2549 silc_idlist_del_client(server->local_list, remote_client);
2553 silc_server_command_free(cmd);
2556 /* Server side of command INFO. This sends information about us to
2557 the client. If client requested specific server we will send the
2558 command to that server. */
2560 SILC_SERVER_CMD_FUNC(info)
2562 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2563 SilcServer server = cmd->server;
2564 SilcBuffer packet, idp;
2567 char *dest_server, *server_info = NULL, *server_name;
2568 uint16 ident = silc_command_get_ident(cmd->payload);
2569 SilcServerEntry entry = NULL;
2570 SilcServerID *server_id = NULL;
2572 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
2574 /* Get server name */
2575 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2578 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2580 server_id = silc_id_payload_parse_id(tmp, tmp_len);
2582 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2583 SILC_STATUS_ERR_NO_SERVER_ID);
2589 /* Check whether we have this server cached */
2590 entry = silc_idlist_find_server_by_id(server->local_list,
2591 server_id, TRUE, NULL);
2593 entry = silc_idlist_find_server_by_id(server->global_list,
2594 server_id, TRUE, NULL);
2595 if (!entry && server->server_type == SILC_ROUTER) {
2596 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2597 SILC_STATUS_ERR_NO_SUCH_SERVER);
2603 /* Some buggy servers has sent request to router about themselves. */
2604 if (server->server_type == SILC_ROUTER && cmd->sock->user_data == entry)
2607 if ((!dest_server && !server_id && !entry) || (entry &&
2608 entry == server->id_entry) ||
2609 (dest_server && !cmd->pending &&
2610 !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
2611 /* Send our reply */
2612 char info_string[256];
2614 memset(info_string, 0, sizeof(info_string));
2615 snprintf(info_string, sizeof(info_string),
2616 "location: %s server: %s admin: %s <%s>",
2617 server->config->admin_info->location,
2618 server->config->admin_info->server_type,
2619 server->config->admin_info->admin_name,
2620 server->config->admin_info->admin_email);
2622 server_info = info_string;
2623 entry = server->id_entry;
2625 /* Check whether we have this server cached */
2626 if (!entry && dest_server) {
2627 entry = silc_idlist_find_server_by_name(server->global_list,
2628 dest_server, TRUE, NULL);
2630 entry = silc_idlist_find_server_by_name(server->local_list,
2631 dest_server, TRUE, NULL);
2635 if (!cmd->pending &&
2636 server->server_type == SILC_ROUTER && entry && !entry->server_info) {
2637 /* Send to the server */
2641 old_ident = silc_command_get_ident(cmd->payload);
2642 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2643 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2645 silc_server_packet_send(server, entry->connection,
2646 SILC_PACKET_COMMAND, cmd->packet->flags,
2647 tmpbuf->data, tmpbuf->len, TRUE);
2649 /* Reprocess this packet after received reply from router */
2650 silc_server_command_pending(server, SILC_COMMAND_INFO,
2651 silc_command_get_ident(cmd->payload),
2652 silc_server_command_destructor,
2653 silc_server_command_info,
2654 silc_server_command_dup(cmd));
2655 cmd->pending = TRUE;
2656 silc_command_set_ident(cmd->payload, old_ident);
2657 silc_buffer_free(tmpbuf);
2661 if (!entry && !cmd->pending && !server->standalone) {
2662 /* Send to the primary router */
2666 old_ident = silc_command_get_ident(cmd->payload);
2667 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2668 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2670 silc_server_packet_send(server, server->router->connection,
2671 SILC_PACKET_COMMAND, cmd->packet->flags,
2672 tmpbuf->data, tmpbuf->len, TRUE);
2674 /* Reprocess this packet after received reply from router */
2675 silc_server_command_pending(server, SILC_COMMAND_INFO,
2676 silc_command_get_ident(cmd->payload),
2677 silc_server_command_destructor,
2678 silc_server_command_info,
2679 silc_server_command_dup(cmd));
2680 cmd->pending = TRUE;
2681 silc_command_set_ident(cmd->payload, old_ident);
2682 silc_buffer_free(tmpbuf);
2688 silc_free(server_id);
2691 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2692 SILC_STATUS_ERR_NO_SUCH_SERVER);
2696 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2698 server_info = entry->server_info;
2699 server_name = entry->server_name;
2701 /* Send the reply */
2703 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2704 SILC_STATUS_OK, ident, 3,
2705 2, idp->data, idp->len,
2707 strlen(server_name),
2709 strlen(server_info));
2711 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2712 SILC_STATUS_OK, ident, 2,
2713 2, idp->data, idp->len,
2715 strlen(server_name));
2716 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2717 packet->data, packet->len, FALSE);
2719 silc_buffer_free(packet);
2720 silc_buffer_free(idp);
2723 silc_server_command_free(cmd);
2726 /* Server side of command PING. This just replies to the ping. */
2728 SILC_SERVER_CMD_FUNC(ping)
2730 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2731 SilcServer server = cmd->server;
2736 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 1, 2);
2739 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2741 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2742 SILC_STATUS_ERR_NO_SERVER_ID);
2745 id = silc_id_str2id(tmp, len, SILC_ID_SERVER);
2749 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2750 /* Send our reply */
2751 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2754 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2755 SILC_STATUS_ERR_NO_SUCH_SERVER);
2762 silc_server_command_free(cmd);
2765 /* Internal routine to join channel. The channel sent to this function
2766 has been either created or resolved from ID lists. This joins the sent
2767 client to the channel. */
2769 static void silc_server_command_join_channel(SilcServer server,
2770 SilcServerCommandContext cmd,
2771 SilcChannelEntry channel,
2772 SilcClientID *client_id,
2776 SilcSocketConnection sock = cmd->sock;
2778 uint32 tmp_len, user_count;
2779 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
2780 SilcClientEntry client;
2781 SilcChannelClientEntry chl;
2782 SilcBuffer reply, chidp, clidp, keyp = NULL, user_list, mode_list;
2783 uint16 ident = silc_command_get_ident(cmd->payload);
2784 char check[512], check2[512];
2786 SILC_LOG_DEBUG(("Start"));
2791 /* Get the client entry */
2792 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2793 client = (SilcClientEntry)sock->user_data;
2795 client = silc_server_get_client_resolve(server, client_id);
2800 /* The client info is being resolved. Reprocess this packet after
2801 receiving the reply to the query. */
2802 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2803 server->cmd_ident, NULL,
2804 silc_server_command_join,
2805 silc_server_command_dup(cmd));
2806 cmd->pending = TRUE;
2810 cmd->pending = FALSE;
2814 * Check channel modes
2817 memset(check, 0, sizeof(check));
2818 memset(check2, 0, sizeof(check2));
2819 strncat(check, client->nickname, strlen(client->nickname));
2820 strncat(check, "!", 1);
2821 strncat(check, client->username, strlen(client->username));
2822 if (!strchr(client->username, '@')) {
2823 strncat(check, "@", 1);
2824 strncat(check, cmd->sock->hostname, strlen(cmd->sock->hostname));
2827 strncat(check2, client->nickname, strlen(client->nickname));
2828 if (!strchr(client->nickname, '@')) {
2829 strncat(check2, "@", 1);
2830 strncat(check2, server->server_name, strlen(server->server_name));
2832 strncat(check2, "!", 1);
2833 strncat(check2, client->username, strlen(client->username));
2834 if (!strchr(client->username, '@')) {
2835 strncat(check2, "@", 1);
2836 strncat(check2, cmd->sock->hostname, strlen(cmd->sock->hostname));
2839 /* Check invite list if channel is invite-only channel */
2840 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2841 if (!channel->invite_list ||
2842 (!silc_string_match(channel->invite_list, check) &&
2843 !silc_string_match(channel->invite_list, check2))) {
2844 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2845 SILC_STATUS_ERR_NOT_INVITED);
2850 /* Check ban list if it exists. If the client's nickname, server,
2851 username and/or hostname is in the ban list the access to the
2852 channel is denied. */
2853 if (channel->ban_list) {
2854 if (silc_string_match(channel->ban_list, check) ||
2855 silc_string_match(channel->ban_list, check2)) {
2856 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2857 SILC_STATUS_ERR_BANNED_FROM_CHANNEL);
2862 /* Get passphrase */
2863 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
2865 passphrase = silc_calloc(tmp_len, sizeof(*passphrase));
2866 memcpy(passphrase, tmp, tmp_len);
2869 /* Check the channel passphrase if set. */
2870 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2871 if (!passphrase || memcmp(channel->passphrase, passphrase,
2872 strlen(channel->passphrase))) {
2873 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2874 SILC_STATUS_ERR_BAD_PASSWORD);
2879 /* Check user count limit if set. */
2880 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
2881 if (silc_hash_table_count(channel->user_list) + 1 >
2882 channel->user_limit) {
2883 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2884 SILC_STATUS_ERR_CHANNEL_IS_FULL);
2890 * Client is allowed to join to the channel. Make it happen.
2893 /* Check whether the client already is on the channel */
2894 if (silc_server_client_on_channel(client, channel)) {
2895 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2896 SILC_STATUS_ERR_USER_ON_CHANNEL);
2900 /* Generate new channel key as protocol dictates */
2901 if ((!created && silc_hash_table_count(channel->user_list) > 0) ||
2902 !channel->channel_key)
2903 if (!silc_server_create_channel_key(server, channel, 0))
2906 /* Send the channel key. This is broadcasted to the channel but is not
2907 sent to the client who is joining to the channel. */
2908 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
2909 silc_server_send_channel_key(server, NULL, channel,
2910 server->server_type == SILC_ROUTER ?
2911 FALSE : !server->standalone);
2913 /* Join the client to the channel by adding it to channel's user list.
2914 Add also the channel to client entry's channels list for fast cross-
2916 chl = silc_calloc(1, sizeof(*chl));
2918 chl->client = client;
2919 chl->channel = channel;
2920 silc_hash_table_add(channel->user_list, client, chl);
2921 silc_hash_table_add(client->channels, channel, chl);
2923 /* Get users on the channel */
2924 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2927 /* Encode Client ID Payload of the original client who wants to join */
2928 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2930 /* Encode command reply packet */
2931 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2932 SILC_PUT32_MSB(channel->mode, mode);
2933 SILC_PUT32_MSB(created, tmp2);
2934 SILC_PUT32_MSB(user_count, tmp3);
2936 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2937 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2938 keyp = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, tmp,
2939 strlen(channel->channel_key->
2941 channel->channel_key->cipher->name,
2942 channel->key_len / 8, channel->key);
2947 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2948 SILC_STATUS_OK, ident, 13,
2949 2, channel->channel_name,
2950 strlen(channel->channel_name),
2951 3, chidp->data, chidp->len,
2952 4, clidp->data, clidp->len,
2955 7, keyp ? keyp->data : NULL,
2956 keyp ? keyp->len : 0,
2957 8, channel->ban_list,
2959 strlen(channel->ban_list) : 0,
2960 9, channel->invite_list,
2961 channel->invite_list ?
2962 strlen(channel->invite_list) : 0,
2965 strlen(channel->topic) : 0,
2966 11, channel->hmac->hmac->name,
2967 strlen(channel->hmac->hmac->name),
2969 13, user_list->data, user_list->len,
2970 14, mode_list->data,
2973 /* Send command reply */
2974 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2975 reply->data, reply->len, FALSE);
2977 if (!cmd->pending) {
2978 /* Send JOIN notify to locally connected clients on the channel */
2979 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2980 SILC_NOTIFY_TYPE_JOIN, 2,
2981 clidp->data, clidp->len,
2982 chidp->data, chidp->len);
2984 /* Send JOIN notify packet to our primary router */
2985 if (!server->standalone)
2986 silc_server_send_notify_join(server, server->router->connection,
2987 server->server_type == SILC_ROUTER ?
2988 TRUE : FALSE, channel, client->id);
2991 silc_buffer_free(reply);
2992 silc_buffer_free(clidp);
2993 silc_buffer_free(chidp);
2994 silc_buffer_free(keyp);
2995 silc_buffer_free(user_list);
2996 silc_buffer_free(mode_list);
3000 silc_free(passphrase);
3003 /* Server side of command JOIN. Joins client into requested channel. If
3004 the channel does not exist it will be created. */
3006 SILC_SERVER_CMD_FUNC(join)
3008 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3009 SilcServer server = cmd->server;
3011 char *tmp, *channel_name = NULL, *cipher, *hmac;
3012 SilcChannelEntry channel;
3014 int created = FALSE;
3015 SilcClientID *client_id;
3017 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 1, 4);
3019 /* Get channel name */
3020 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3022 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3023 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3028 if (strlen(channel_name) > 256)
3029 channel_name[255] = '\0';
3031 if (silc_server_command_bad_chars(channel_name) == TRUE) {
3032 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3033 SILC_STATUS_ERR_BAD_CHANNEL);
3037 /* Get Client ID of the client who is joining to the channel */
3038 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3040 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3041 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3044 client_id = silc_id_payload_parse_id(tmp, tmp_len);
3046 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3047 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3051 /* Get cipher and hmac name */
3052 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
3053 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
3055 /* See if the channel exists */
3056 channel = silc_idlist_find_channel_by_name(server->local_list,
3057 channel_name, NULL);
3059 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3060 /* If this is coming from client the Client ID in the command packet must
3061 be same as the client's ID. */
3062 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3063 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
3064 if (!SILC_ID_CLIENT_COMPARE(entry->id, client_id)) {
3065 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3066 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3071 if (!channel || !channel->id) {
3072 /* Channel not found */
3074 /* If we are standalone server we don't have a router, we just create
3075 the channel by ourselves. */
3076 if (server->standalone) {
3077 channel = silc_server_create_new_channel(server, server->id, cipher,
3078 hmac, channel_name, TRUE);
3080 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3081 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3085 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3090 /* The channel does not exist on our server. If we are normal server
3091 we will send JOIN command to our router which will handle the
3092 joining procedure (either creates the channel if it doesn't exist
3093 or joins the client to it). */
3094 if (server->server_type == SILC_SERVER) {
3098 /* If this is pending command callback then we've resolved
3099 it and it didn't work, return since we've notified the
3100 client already in the command reply callback. */
3104 old_ident = silc_command_get_ident(cmd->payload);
3105 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3106 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3108 /* Send JOIN command to our router */
3109 silc_server_packet_send(server, (SilcSocketConnection)
3110 server->router->connection,
3111 SILC_PACKET_COMMAND, cmd->packet->flags,
3112 tmpbuf->data, tmpbuf->len, TRUE);
3114 /* Reprocess this packet after received reply from router */
3115 silc_server_command_pending(server, SILC_COMMAND_JOIN,
3116 silc_command_get_ident(cmd->payload),
3117 silc_server_command_destructor,
3118 silc_server_command_join,
3119 silc_server_command_dup(cmd));
3120 cmd->pending = TRUE;
3124 /* We are router and the channel does not seem exist so we will check
3125 our global list as well for the channel. */
3126 channel = silc_idlist_find_channel_by_name(server->global_list,
3127 channel_name, NULL);
3129 /* Channel really does not exist, create it */
3130 channel = silc_server_create_new_channel(server, server->id, cipher,
3131 hmac, channel_name, TRUE);
3133 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3134 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3138 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3145 /* Channel not found */
3147 /* If the command came from router and/or we are normal server then
3148 something went wrong with the joining as the channel was not found.
3149 We can't do anything else but ignore this. */
3150 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
3151 server->server_type == SILC_SERVER)
3154 /* We are router and the channel does not seem exist so we will check
3155 our global list as well for the channel. */
3156 channel = silc_idlist_find_channel_by_name(server->global_list,
3157 channel_name, NULL);
3159 /* Channel really does not exist, create it */
3160 channel = silc_server_create_new_channel(server, server->id, cipher,
3161 hmac, channel_name, TRUE);
3163 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3164 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3168 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3174 /* If the channel does not have global users and is also empty it means the
3175 channel was created globally (by our router) and the client will be the
3176 channel founder and operator. */
3177 if (!channel->global_users && !silc_hash_table_count(channel->user_list)) {
3178 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3179 created = TRUE; /* Created globally by our router */
3182 /* Join to the channel */
3183 silc_server_command_join_channel(server, cmd, channel, client_id,
3186 silc_free(client_id);
3189 silc_server_command_free(cmd);
3192 /* Server side of command MOTD. Sends server's current "message of the
3193 day" to the client. */
3195 SILC_SERVER_CMD_FUNC(motd)
3197 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3198 SilcServer server = cmd->server;
3199 SilcBuffer packet, idp;
3200 char *motd, *dest_server;
3202 uint16 ident = silc_command_get_ident(cmd->payload);
3204 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
3206 /* Get server name */
3207 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
3209 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
3210 SILC_STATUS_ERR_NO_SUCH_SERVER);
3214 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
3217 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3219 if (server->config && server->config->motd &&
3220 server->config->motd->motd_file) {
3222 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
3227 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3228 SILC_STATUS_OK, ident, 2,
3234 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3235 SILC_STATUS_OK, ident, 1,
3239 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3240 packet->data, packet->len, FALSE);
3241 silc_buffer_free(packet);
3242 silc_buffer_free(idp);
3244 SilcServerEntry entry;
3246 /* Check whether we have this server cached */
3247 entry = silc_idlist_find_server_by_name(server->global_list,
3248 dest_server, TRUE, NULL);
3250 entry = silc_idlist_find_server_by_name(server->local_list,
3251 dest_server, TRUE, NULL);
3254 if (server->server_type == SILC_ROUTER && !cmd->pending &&
3255 entry && !entry->motd) {
3256 /* Send to the server */
3260 old_ident = silc_command_get_ident(cmd->payload);
3261 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3262 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3264 silc_server_packet_send(server, entry->connection,
3265 SILC_PACKET_COMMAND, cmd->packet->flags,
3266 tmpbuf->data, tmpbuf->len, TRUE);
3268 /* Reprocess this packet after received reply from router */
3269 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3270 silc_command_get_ident(cmd->payload),
3271 silc_server_command_destructor,
3272 silc_server_command_motd,
3273 silc_server_command_dup(cmd));
3274 cmd->pending = TRUE;
3275 silc_command_set_ident(cmd->payload, old_ident);
3276 silc_buffer_free(tmpbuf);
3280 if (!entry && !cmd->pending && !server->standalone) {
3281 /* Send to the primary router */
3285 old_ident = silc_command_get_ident(cmd->payload);
3286 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3287 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3289 silc_server_packet_send(server, server->router->connection,
3290 SILC_PACKET_COMMAND, cmd->packet->flags,
3291 tmpbuf->data, tmpbuf->len, TRUE);
3293 /* Reprocess this packet after received reply from router */
3294 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3295 silc_command_get_ident(cmd->payload),
3296 silc_server_command_destructor,
3297 silc_server_command_motd,
3298 silc_server_command_dup(cmd));
3299 cmd->pending = TRUE;
3300 silc_command_set_ident(cmd->payload, old_ident);
3301 silc_buffer_free(tmpbuf);
3306 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3307 SILC_STATUS_ERR_NO_SUCH_SERVER);
3311 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3314 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3315 SILC_STATUS_OK, ident, 2,
3318 strlen(entry->motd));
3320 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3321 SILC_STATUS_OK, ident, 1,
3324 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3325 packet->data, packet->len, FALSE);
3326 silc_buffer_free(packet);
3327 silc_buffer_free(idp);
3331 silc_server_command_free(cmd);
3334 /* Server side of command UMODE. Client can use this command to set/unset
3335 user mode. Client actually cannot set itself to be as server/router
3336 operator so this can be used only to unset the modes. */
3338 SILC_SERVER_CMD_FUNC(umode)
3340 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3341 SilcServer server = cmd->server;
3342 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3344 unsigned char *tmp_mask;
3346 uint16 ident = silc_command_get_ident(cmd->payload);
3348 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3351 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 2, 2);
3353 /* Get the client's mode mask */
3354 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3356 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3357 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3360 SILC_GET32_MSB(mask, tmp_mask);
3366 if (mask & SILC_UMODE_SERVER_OPERATOR) {
3367 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR)) {
3368 /* Cannot operator mode */
3369 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3370 SILC_STATUS_ERR_PERM_DENIED);
3374 if (client->mode & SILC_UMODE_SERVER_OPERATOR)
3375 /* Remove the server operator rights */
3376 client->mode &= ~SILC_UMODE_SERVER_OPERATOR;
3379 if (mask & SILC_UMODE_ROUTER_OPERATOR) {
3380 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
3381 /* Cannot operator mode */
3382 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3383 SILC_STATUS_ERR_PERM_DENIED);
3387 if (client->mode & SILC_UMODE_ROUTER_OPERATOR)
3388 /* Remove the router operator rights */
3389 client->mode &= ~SILC_UMODE_ROUTER_OPERATOR;
3392 if (mask & SILC_UMODE_GONE) {
3393 client->mode |= SILC_UMODE_GONE;
3395 if (client->mode & SILC_UMODE_GONE)
3396 /* Remove the gone status */
3397 client->mode &= ~SILC_UMODE_GONE;
3400 /* Send UMODE change to primary router */
3401 if (!server->standalone)
3402 silc_server_send_notify_umode(server, server->router->connection, TRUE,
3403 client->id, client->mode);
3405 /* Send command reply to sender */
3406 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
3407 SILC_STATUS_OK, ident, 1,
3409 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3410 packet->data, packet->len, FALSE);
3411 silc_buffer_free(packet);
3414 silc_server_command_free(cmd);
3417 /* Checks that client has rights to add or remove channel modes. If any
3418 of the checks fails FALSE is returned. */
3420 int silc_server_check_cmode_rights(SilcChannelEntry channel,
3421 SilcChannelClientEntry client,
3424 int is_op = client->mode & SILC_CHANNEL_UMODE_CHANOP;
3425 int is_fo = client->mode & SILC_CHANNEL_UMODE_CHANFO;
3427 /* Check whether has rights to change anything */
3428 if (!is_op && !is_fo)
3431 /* Check whether has rights to change everything */
3435 /* We know that client is channel operator, check that they are not
3436 changing anything that requires channel founder rights. Rest of the
3437 modes are available automatically for channel operator. */
3439 if (mode & SILC_CHANNEL_MODE_PRIVKEY) {
3440 if (is_op && !is_fo)
3443 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3444 if (is_op && !is_fo)
3449 if (mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3450 if (is_op && !is_fo)
3453 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3454 if (is_op && !is_fo)
3459 if (mode & SILC_CHANNEL_MODE_CIPHER) {
3460 if (is_op && !is_fo)
3463 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3464 if (is_op && !is_fo)
3469 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3470 if (is_op && !is_fo)
3473 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3474 if (is_op && !is_fo)
3482 /* Server side command of CMODE. Changes channel mode */
3484 SILC_SERVER_CMD_FUNC(cmode)
3486 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3487 SilcServer server = cmd->server;
3488 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3489 SilcIDListData idata = (SilcIDListData)client;
3490 SilcChannelID *channel_id;
3491 SilcChannelEntry channel;
3492 SilcChannelClientEntry chl;
3493 SilcBuffer packet, cidp;
3494 unsigned char *tmp, *tmp_id, *tmp_mask;
3495 char *cipher = NULL, *hmac = NULL;
3496 uint32 mode_mask, tmp_len, tmp_len2;
3497 uint16 ident = silc_command_get_ident(cmd->payload);
3499 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 2, 7);
3501 /* Get Channel ID */
3502 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
3504 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3505 SILC_STATUS_ERR_NO_CHANNEL_ID);
3508 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2);
3510 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3511 SILC_STATUS_ERR_NO_CHANNEL_ID);
3515 /* Get the channel mode mask */
3516 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3518 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3519 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3522 SILC_GET32_MSB(mode_mask, tmp_mask);
3524 /* Get channel entry */
3525 channel = silc_idlist_find_channel_by_id(server->local_list,
3528 channel = silc_idlist_find_channel_by_id(server->global_list,
3531 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3532 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3537 /* Check whether this client is on the channel */
3538 if (!silc_server_client_on_channel(client, channel)) {
3539 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3540 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3544 /* Get entry to the channel user list */
3545 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
3547 /* Check that client has rights to change any requested channel modes */
3548 if (!silc_server_check_cmode_rights(channel, chl, mode_mask)) {
3549 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3550 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3555 * Check the modes. Modes that requires nothing special operation are
3559 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
3560 /* Channel uses private keys to protect traffic. Client(s) has set the
3561 key locally they want to use, server does not know that key. */
3562 /* Nothing interesting to do here */
3564 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3565 /* The mode is removed and we need to generate and distribute
3566 new channel key. Clients are not using private channel keys
3567 anymore after this. */
3569 /* Re-generate channel key */
3570 if (!silc_server_create_channel_key(server, channel, 0))
3573 /* Send the channel key. This sends it to our local clients and if
3574 we are normal server to our router as well. */
3575 silc_server_send_channel_key(server, NULL, channel,
3576 server->server_type == SILC_ROUTER ?
3577 FALSE : !server->standalone);
3579 cipher = channel->channel_key->cipher->name;
3580 hmac = channel->hmac->hmac->name;
3584 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
3585 /* User limit is set on channel */
3588 /* Get user limit */
3589 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
3591 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
3592 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3593 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3597 SILC_GET32_MSB(user_limit, tmp);
3598 channel->user_limit = user_limit;
3601 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
3602 /* User limit mode is unset. Remove user limit */
3603 channel->user_limit = 0;
3606 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
3607 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
3608 /* Passphrase has been set to channel */
3610 /* Get the passphrase */
3611 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
3613 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3614 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3618 /* Save the passphrase */
3619 channel->passphrase = strdup(tmp);
3622 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3623 /* Passphrase mode is unset. remove the passphrase */
3624 if (channel->passphrase) {
3625 silc_free(channel->passphrase);
3626 channel->passphrase = NULL;
3631 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
3632 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
3633 /* Cipher to use protect the traffic */
3636 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
3638 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3639 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3643 /* Delete old cipher and allocate the new one */
3644 silc_cipher_free(channel->channel_key);
3645 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3646 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3647 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3651 /* Re-generate channel key */
3652 if (!silc_server_create_channel_key(server, channel, 0))
3655 /* Send the channel key. This sends it to our local clients and if
3656 we are normal server to our router as well. */
3657 silc_server_send_channel_key(server, NULL, channel,
3658 server->server_type == SILC_ROUTER ?
3659 FALSE : !server->standalone);
3662 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3663 /* Cipher mode is unset. Remove the cipher and revert back to
3665 cipher = channel->cipher;
3667 /* Delete old cipher and allocate default one */
3668 silc_cipher_free(channel->channel_key);
3669 if (!silc_cipher_alloc(cipher ? cipher : "aes-256-cbc",
3670 &channel->channel_key)) {
3671 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3672 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3676 /* Re-generate channel key */
3677 if (!silc_server_create_channel_key(server, channel, 0))
3680 /* Send the channel key. This sends it to our local clients and if
3681 we are normal server to our router as well. */
3682 silc_server_send_channel_key(server, NULL, channel,
3683 server->server_type == SILC_ROUTER ?
3684 FALSE : !server->standalone);
3688 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
3689 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
3690 /* HMAC to use protect the traffic */
3691 unsigned char hash[32];
3694 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
3696 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3697 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3701 /* Delete old hmac and allocate the new one */
3702 silc_hmac_free(channel->hmac);
3703 if (!silc_hmac_alloc(hmac, NULL, &channel->hmac)) {
3704 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3705 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3709 /* Set the HMAC key out of current channel key. The client must do
3711 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3713 silc_hmac_set_key(channel->hmac, hash,
3714 silc_hash_len(channel->hmac->hash));
3715 memset(hash, 0, sizeof(hash));
3718 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
3719 /* Hmac mode is unset. Remove the hmac and revert back to
3721 unsigned char hash[32];
3722 hmac = channel->hmac_name;
3724 /* Delete old hmac and allocate default one */
3725 silc_hmac_free(channel->hmac);
3726 if (!silc_hmac_alloc(hmac ? hmac : "hmac-sha1-96", NULL,
3728 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3729 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3733 /* Set the HMAC key out of current channel key. The client must do
3735 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3737 silc_hmac_set_key(channel->hmac, hash,
3738 silc_hash_len(channel->hmac->hash));
3739 memset(hash, 0, sizeof(hash));
3743 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3744 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3745 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
3746 /* Set the founder authentication */
3747 SilcAuthPayload auth;
3749 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
3751 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3752 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3756 auth = silc_auth_payload_parse(tmp, tmp_len);
3758 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3759 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3763 /* Save the public key */
3764 tmp = silc_pkcs_public_key_encode(idata->public_key, &tmp_len);
3765 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
3768 channel->founder_method = silc_auth_get_method(auth);
3770 if (channel->founder_method == SILC_AUTH_PASSWORD) {
3771 tmp = silc_auth_get_data(auth, &tmp_len);
3772 channel->founder_passwd =
3773 silc_calloc(tmp_len + 1, sizeof(*channel->founder_passwd));
3774 memcpy(channel->founder_passwd, tmp, tmp_len);
3775 channel->founder_passwd_len = tmp_len;
3777 /* Verify the payload before setting the mode */
3778 if (!silc_auth_verify(auth, channel->founder_method,
3779 channel->founder_key, 0, idata->hash,
3780 client->id, SILC_ID_CLIENT)) {
3781 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3782 SILC_STATUS_ERR_AUTH_FAILED);
3787 silc_auth_payload_free(auth);
3791 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3792 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3793 if (channel->founder_key)
3794 silc_pkcs_public_key_free(channel->founder_key);
3795 if (channel->founder_passwd) {
3796 silc_free(channel->founder_passwd);
3797 channel->founder_passwd = NULL;
3803 /* Finally, set the mode */
3804 channel->mode = mode_mask;
3806 /* Send CMODE_CHANGE notify */
3807 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3808 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3809 SILC_NOTIFY_TYPE_CMODE_CHANGE, 4,
3810 cidp->data, cidp->len,
3812 cipher, cipher ? strlen(cipher) : 0,
3813 hmac, hmac ? strlen(hmac) : 0);
3815 /* Set CMODE notify type to network */
3816 if (!server->standalone)
3817 silc_server_send_notify_cmode(server, server->router->connection,
3818 server->server_type == SILC_ROUTER ?
3819 TRUE : FALSE, channel,
3820 mode_mask, client->id, SILC_ID_CLIENT,
3823 /* Send command reply to sender */
3824 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3825 SILC_STATUS_OK, ident, 2,
3826 2, tmp_id, tmp_len2,
3828 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3829 packet->data, packet->len, FALSE);
3831 silc_buffer_free(packet);
3832 silc_free(channel_id);
3836 silc_server_command_free(cmd);
3839 /* Server side of CUMODE command. Changes client's mode on a channel. */
3841 SILC_SERVER_CMD_FUNC(cumode)
3843 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3844 SilcServer server = cmd->server;
3845 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3846 SilcIDListData idata = (SilcIDListData)client;
3847 SilcChannelID *channel_id;
3848 SilcClientID *client_id;
3849 SilcChannelEntry channel;
3850 SilcClientEntry target_client;
3851 SilcChannelClientEntry chl;
3852 SilcBuffer packet, idp;
3853 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3854 uint32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3856 uint16 ident = silc_command_get_ident(cmd->payload);
3858 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
3860 /* Get Channel ID */
3861 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3863 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3864 SILC_STATUS_ERR_NO_CHANNEL_ID);
3867 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len);
3869 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3870 SILC_STATUS_ERR_NO_CHANNEL_ID);
3874 /* Get channel entry */
3875 channel = silc_idlist_find_channel_by_id(server->local_list,
3878 channel = silc_idlist_find_channel_by_id(server->global_list,
3881 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3882 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3887 /* Check whether sender is on the channel */
3888 if (!silc_server_client_on_channel(client, channel)) {
3889 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3890 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3894 /* Check that client has rights to change other's rights */
3895 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
3896 sender_mask = chl->mode;
3898 /* Get the target client's channel mode mask */
3899 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3901 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3902 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3905 SILC_GET32_MSB(target_mask, tmp_mask);
3907 /* Get target Client ID */
3908 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3910 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3911 SILC_STATUS_ERR_NO_CLIENT_ID);
3914 client_id = silc_id_payload_parse_id(tmp_id, tmp_len);
3916 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3917 SILC_STATUS_ERR_NO_CLIENT_ID);
3921 /* Get target client's entry */
3922 target_client = silc_idlist_find_client_by_id(server->local_list,
3923 client_id, TRUE, NULL);
3924 if (!target_client) {
3925 target_client = silc_idlist_find_client_by_id(server->global_list,
3926 client_id, TRUE, NULL);
3929 if (target_client != client &&
3930 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3931 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3932 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3933 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3937 /* Check whether target client is on the channel */
3938 if (target_client != client) {
3939 if (!silc_server_client_on_channel(target_client, channel)) {
3940 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3941 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
3945 /* Get entry to the channel user list */
3946 silc_hash_table_find(channel->user_list, target_client, NULL,
3954 /* If the target client is founder, no one else can change their mode
3956 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && chl->client != target_client) {
3957 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3958 SILC_STATUS_ERR_NOT_YOU);
3962 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3963 /* The client tries to claim the founder rights. */
3964 unsigned char *tmp_auth;
3965 uint32 tmp_auth_len, auth_len;
3968 if (target_client != client) {
3969 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3970 SILC_STATUS_ERR_NOT_YOU);
3974 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3975 !channel->founder_key) {
3976 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3977 SILC_STATUS_ERR_NOT_YOU);
3981 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3983 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3984 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3988 auth = (channel->founder_method == SILC_AUTH_PASSWORD ?
3989 (void *)channel->founder_passwd : (void *)channel->founder_key);
3990 auth_len = (channel->founder_method == SILC_AUTH_PASSWORD ?
3991 channel->founder_passwd_len : 0);
3993 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len,
3994 channel->founder_method, auth, auth_len,
3995 idata->hash, client->id, SILC_ID_CLIENT)) {
3996 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3997 SILC_STATUS_ERR_AUTH_FAILED);
4001 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
4004 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4005 if (target_client == client) {
4006 /* Remove channel founder rights from itself */
4007 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
4010 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4011 SILC_STATUS_ERR_NOT_YOU);
4017 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
4018 /* Promote to operator */
4019 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4020 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4021 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4022 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4023 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4027 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
4031 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
4032 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4033 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4034 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4035 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4039 /* Demote to normal user */
4040 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
4045 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
4046 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4048 /* Send notify to channel, notify only if mode was actually changed. */
4050 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4051 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
4052 idp->data, idp->len,
4056 /* Set CUMODE notify type to network */
4057 if (!server->standalone)
4058 silc_server_send_notify_cumode(server, server->router->connection,
4059 server->server_type == SILC_ROUTER ?
4060 TRUE : FALSE, channel,
4061 target_mask, client->id,
4066 /* Send command reply to sender */
4067 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
4068 SILC_STATUS_OK, ident, 3,
4070 3, tmp_ch_id, tmp_ch_len,
4071 4, tmp_id, tmp_len);
4072 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4073 packet->data, packet->len, FALSE);
4075 silc_buffer_free(packet);
4076 silc_free(channel_id);
4077 silc_free(client_id);
4078 silc_buffer_free(idp);
4081 silc_server_command_free(cmd);
4084 /* Server side of KICK command. Kicks client out of channel. */
4086 SILC_SERVER_CMD_FUNC(kick)
4088 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4089 SilcServer server = cmd->server;
4090 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4091 SilcClientEntry target_client;
4092 SilcChannelID *channel_id;
4093 SilcClientID *client_id;
4094 SilcChannelEntry channel;
4095 SilcChannelClientEntry chl;
4098 unsigned char *tmp, *comment;
4100 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
4102 /* Get Channel ID */
4103 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4105 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4106 SILC_STATUS_ERR_NO_CHANNEL_ID);
4109 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
4111 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4112 SILC_STATUS_ERR_NO_CHANNEL_ID);
4116 /* Get channel entry */
4117 channel = silc_idlist_find_channel_by_id(server->local_list,
4120 channel = silc_idlist_find_channel_by_id(server->local_list,
4123 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4124 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4129 /* Check whether sender is on the channel */
4130 if (!silc_server_client_on_channel(client, channel)) {
4131 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4132 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4136 /* Check that the kicker is channel operator or channel founder */
4137 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4138 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
4139 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4140 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4144 /* Get target Client ID */
4145 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4147 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4148 SILC_STATUS_ERR_NO_CLIENT_ID);
4151 client_id = silc_id_payload_parse_id(tmp, tmp_len);
4153 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4154 SILC_STATUS_ERR_NO_CLIENT_ID);
4158 /* Get target client's entry */
4159 target_client = silc_idlist_find_client_by_id(server->local_list,
4160 client_id, TRUE, NULL);
4161 if (!target_client) {
4162 target_client = silc_idlist_find_client_by_id(server->global_list,
4163 client_id, TRUE, NULL);
4166 /* Check that the target client is not channel founder. Channel founder
4167 cannot be kicked from the channel. */
4168 silc_hash_table_find(channel->user_list, target_client, NULL, (void *)&chl);
4169 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4170 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4171 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV);
4175 /* Check whether target client is on the channel */
4176 if (!silc_server_client_on_channel(target_client, channel)) {
4177 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4178 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
4184 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4188 /* Send command reply to sender */
4189 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4192 /* Send KICKED notify to local clients on the channel */
4193 idp = silc_id_payload_encode(target_client->id, SILC_ID_CLIENT);
4194 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4195 SILC_NOTIFY_TYPE_KICKED,
4197 idp->data, idp->len,
4198 comment, comment ? strlen(comment) : 0);
4199 silc_buffer_free(idp);
4201 /* Remove the client from the channel. If the channel does not exist
4202 after removing the client then the client kicked itself off the channel
4203 and we don't have to send anything after that. */
4204 if (!silc_server_remove_from_one_channel(server, NULL, channel,
4205 target_client, FALSE))
4208 /* Send KICKED notify to primary route */
4209 if (!server->standalone)
4210 silc_server_send_notify_kicked(server, server->router->connection,
4211 server->server_type == SILC_ROUTER ?
4212 TRUE : FALSE, channel,
4213 target_client->id, comment);
4215 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4216 /* Re-generate channel key */
4217 if (!silc_server_create_channel_key(server, channel, 0))
4220 /* Send the channel key to the channel. The key of course is not sent
4221 to the client who was kicked off the channel. */
4222 silc_server_send_channel_key(server, target_client->connection, channel,
4223 server->server_type == SILC_ROUTER ?
4224 FALSE : !server->standalone);
4228 silc_server_command_free(cmd);
4231 /* Server side of OPER command. Client uses this comand to obtain server
4232 operator privileges to this server/router. */
4234 SILC_SERVER_CMD_FUNC(oper)
4236 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4237 SilcServer server = cmd->server;
4238 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4239 unsigned char *username, *auth;
4241 SilcServerConfigSectionAdminConnection *admin;
4242 SilcIDListData idata = (SilcIDListData)client;
4244 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
4246 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4249 /* Get the username */
4250 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4252 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4253 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4257 /* Get the admin configuration */
4258 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4259 username, client->nickname);
4261 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4262 username, client->nickname);
4264 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4265 SILC_STATUS_ERR_AUTH_FAILED);
4270 /* Get the authentication payload */
4271 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4273 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4274 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4278 /* Verify the authentication data */
4279 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4280 admin->auth_data, admin->auth_data_len,
4281 idata->hash, client->id, SILC_ID_CLIENT)) {
4282 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4283 SILC_STATUS_ERR_AUTH_FAILED);
4287 /* Client is now server operator */
4288 client->mode |= SILC_UMODE_SERVER_OPERATOR;
4290 /* Send UMODE change to primary router */
4291 if (!server->standalone)
4292 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4293 client->id, client->mode);
4295 /* Send reply to the sender */
4296 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4300 silc_server_command_free(cmd);
4303 /* Server side of SILCOPER command. Client uses this comand to obtain router
4304 operator privileges to this router. */
4306 SILC_SERVER_CMD_FUNC(silcoper)
4308 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4309 SilcServer server = cmd->server;
4310 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4311 unsigned char *username, *auth;
4313 SilcServerConfigSectionAdminConnection *admin;
4314 SilcIDListData idata = (SilcIDListData)client;
4316 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
4318 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4321 if (server->server_type == SILC_SERVER) {
4322 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4323 SILC_STATUS_ERR_AUTH_FAILED);
4327 /* Get the username */
4328 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4330 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4331 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4335 /* Get the admin configuration */
4336 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4337 username, client->nickname);
4339 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4340 username, client->nickname);
4342 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4343 SILC_STATUS_ERR_AUTH_FAILED);
4348 /* Get the authentication payload */
4349 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4351 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4352 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4356 /* Verify the authentication data */
4357 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4358 admin->auth_data, admin->auth_data_len,
4359 idata->hash, client->id, SILC_ID_CLIENT)) {
4360 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4361 SILC_STATUS_ERR_AUTH_FAILED);
4365 /* Client is now router operator */
4366 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4368 /* Send UMODE change to primary router */
4369 if (!server->standalone)
4370 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4371 client->id, client->mode);
4373 /* Send reply to the sender */
4374 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4378 silc_server_command_free(cmd);
4381 /* Server side command of CONNECT. Connects us to the specified remote
4382 server or router. */
4384 SILC_SERVER_CMD_FUNC(connect)
4386 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4387 SilcServer server = cmd->server;
4388 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4389 unsigned char *tmp, *host;
4391 uint32 port = SILC_PORT;
4393 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CONNECT, cmd, 1, 2);
4395 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4398 /* Check whether client has the permissions. */
4399 if (client->mode == SILC_UMODE_NONE) {
4400 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4401 SILC_STATUS_ERR_NO_SERVER_PRIV);
4405 if (server->server_type == SILC_ROUTER &&
4406 client->mode & SILC_UMODE_SERVER_OPERATOR) {
4407 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4408 SILC_STATUS_ERR_NO_ROUTER_PRIV);
4412 /* Get the remote server */
4413 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4415 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4416 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4421 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4423 SILC_GET32_MSB(port, tmp);
4425 /* Create the connection. It is done with timeout and is async. */
4426 silc_server_create_connection(server, host, port);
4428 /* Send reply to the sender */
4429 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4433 silc_server_command_free(cmd);
4436 /* Server side of command BAN. This is used to manage the ban list of the
4437 channel. To add clients and remove clients from the ban list. */
4439 SILC_SERVER_CMD_FUNC(ban)
4441 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4442 SilcServer server = cmd->server;
4443 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4445 SilcChannelEntry channel;
4446 SilcChannelClientEntry chl;
4447 SilcChannelID *channel_id = NULL;
4448 unsigned char *id, *add, *del;
4449 uint32 id_len, tmp_len;
4450 uint16 ident = silc_command_get_ident(cmd->payload);
4452 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4455 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
4457 /* Get Channel ID */
4458 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4460 channel_id = silc_id_payload_parse_id(id, id_len);
4462 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4463 SILC_STATUS_ERR_NO_CHANNEL_ID);
4468 /* Get channel entry. The server must know about the channel since the
4469 client is expected to be on the channel. */
4470 channel = silc_idlist_find_channel_by_id(server->local_list,
4473 channel = silc_idlist_find_channel_by_id(server->global_list,
4476 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4477 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4482 /* Check whether this client is on the channel */
4483 if (!silc_server_client_on_channel(client, channel)) {
4484 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4485 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4489 /* Get entry to the channel user list */
4490 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4492 /* The client must be at least channel operator. */
4493 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4494 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4495 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4499 /* Get the new ban and add it to the ban list */
4500 add = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4502 if (!channel->ban_list)
4503 channel->ban_list = silc_calloc(tmp_len + 2, sizeof(*channel->ban_list));
4505 channel->ban_list = silc_realloc(channel->ban_list,
4506 sizeof(*channel->ban_list) *
4508 strlen(channel->ban_list) + 2));
4509 if (add[tmp_len - 1] == ',')
4510 add[tmp_len - 1] = '\0';
4512 strncat(channel->ban_list, add, tmp_len);
4513 strncat(channel->ban_list, ",", 1);
4516 /* Get the ban to be removed and remove it from the list */
4517 del = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4518 if (del && channel->ban_list) {
4519 char *start, *end, *n;
4521 if (!strncmp(channel->ban_list, del, strlen(channel->ban_list) - 1)) {
4522 silc_free(channel->ban_list);
4523 channel->ban_list = NULL;
4525 start = strstr(channel->ban_list, del);
4526 if (start && strlen(start) >= tmp_len) {
4527 end = start + tmp_len;
4528 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
4529 strncat(n, channel->ban_list, start - channel->ban_list);
4530 strncat(n, end + 1, ((channel->ban_list + strlen(channel->ban_list)) -
4532 silc_free(channel->ban_list);
4533 channel->ban_list = n;
4538 /* Send the BAN notify type to our primary router. */
4539 if (!server->standalone && (add || del))
4540 silc_server_send_notify_ban(server, server->router->connection,
4541 server->server_type == SILC_ROUTER ?
4542 TRUE : FALSE, channel, add, del);
4544 /* Send the reply back to the client */
4545 if (channel->ban_list)
4547 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4548 SILC_STATUS_OK, ident, 2,
4550 3, channel->ban_list,
4551 strlen(channel->ban_list) - 1);
4554 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4555 SILC_STATUS_OK, ident, 1,
4558 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4559 packet->data, packet->len, FALSE);
4561 silc_buffer_free(packet);
4565 silc_free(channel_id);
4566 silc_server_command_free(cmd);
4569 /* Server side command of CLOSE. Closes connection to a specified server. */
4571 SILC_SERVER_CMD_FUNC(close)
4573 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4574 SilcServer server = cmd->server;
4575 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4576 SilcServerEntry server_entry;
4577 SilcSocketConnection sock;
4580 unsigned char *name;
4581 uint32 port = SILC_PORT;
4583 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CLOSE, cmd, 1, 2);
4585 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4588 /* Check whether client has the permissions. */
4589 if (client->mode == SILC_UMODE_NONE) {
4590 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4591 SILC_STATUS_ERR_NO_SERVER_PRIV);
4595 /* Get the remote server */
4596 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4598 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4599 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4604 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4606 SILC_GET32_MSB(port, tmp);
4608 server_entry = silc_idlist_find_server_by_conn(server->local_list,
4609 name, port, FALSE, NULL);
4610 if (!server_entry) {
4611 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4612 SILC_STATUS_ERR_NO_SERVER_ID);
4616 /* Send reply to the sender */
4617 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4620 /* Close the connection to the server */
4621 sock = (SilcSocketConnection)server_entry->connection;
4622 silc_server_free_sock_user_data(server, sock);
4623 silc_server_close_connection(server, sock);
4626 silc_server_command_free(cmd);
4629 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4630 active connections. */
4632 SILC_SERVER_CMD_FUNC(shutdown)
4634 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4635 SilcServer server = cmd->server;
4636 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4638 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SHUTDOWN, cmd, 0, 0);
4640 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4643 /* Check whether client has the permission. */
4644 if (client->mode == SILC_UMODE_NONE) {
4645 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4646 SILC_STATUS_ERR_NO_SERVER_PRIV);
4650 /* Send reply to the sender */
4651 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4654 /* Then, gracefully, or not, bring the server down. */
4655 silc_server_stop(server);
4659 silc_server_command_free(cmd);
4662 /* Server side command of LEAVE. Removes client from a channel. */
4664 SILC_SERVER_CMD_FUNC(leave)
4666 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4667 SilcServer server = cmd->server;
4668 SilcSocketConnection sock = cmd->sock;
4669 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4670 SilcChannelID *id = NULL;
4671 SilcChannelEntry channel;
4675 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
4677 /* Get Channel ID */
4678 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4680 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4681 SILC_STATUS_ERR_NO_CHANNEL_ID);
4684 id = silc_id_payload_parse_id(tmp, len);
4686 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4687 SILC_STATUS_ERR_NO_CHANNEL_ID);
4691 /* Get channel entry */
4692 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4694 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4696 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4697 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4702 /* Check whether this client is on the channel */
4703 if (!silc_server_client_on_channel(id_entry, channel)) {
4704 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4705 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4709 /* Notify routers that they should remove this client from their list
4710 of clients on the channel. Send LEAVE notify type. */
4711 if (!server->standalone)
4712 silc_server_send_notify_leave(server, server->router->connection,
4713 server->server_type == SILC_ROUTER ?
4714 TRUE : FALSE, channel, id_entry->id);
4716 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4719 /* Remove client from channel */
4720 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4722 /* If the channel does not exist anymore we won't send anything */
4725 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4726 /* Re-generate channel key */
4727 if (!silc_server_create_channel_key(server, channel, 0))
4730 /* Send the channel key */
4731 silc_server_send_channel_key(server, NULL, channel,
4732 server->server_type == SILC_ROUTER ?
4733 FALSE : !server->standalone);
4739 silc_server_command_free(cmd);
4742 /* Server side of command USERS. Resolves clients and their USERS currently
4743 joined on the requested channel. The list of Client ID's and their modes
4744 on the channel is sent back. */
4746 SILC_SERVER_CMD_FUNC(users)
4748 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4749 SilcServer server = cmd->server;
4750 SilcChannelEntry channel;
4751 SilcChannelID *id = NULL;
4752 SilcBuffer packet, idp;
4753 unsigned char *channel_id;
4754 uint32 channel_id_len;
4755 SilcBuffer client_id_list;
4756 SilcBuffer client_mode_list;
4757 unsigned char lc[4];
4758 uint32 list_count = 0;
4759 uint16 ident = silc_command_get_ident(cmd->payload);
4762 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
4764 /* Get Channel ID */
4765 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4767 /* Get channel name */
4768 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
4770 if (!channel_id && !channel_name) {
4771 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4772 SILC_STATUS_ERR_NO_CHANNEL_ID);
4777 id = silc_id_payload_parse_id(channel_id, channel_id_len);
4779 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4780 SILC_STATUS_ERR_NO_CHANNEL_ID);
4785 /* If we are server and we don't know about this channel we will send
4786 the command to our router. If we know about the channel then we also
4787 have the list of users already. */
4789 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4791 channel = silc_idlist_find_channel_by_name(server->local_list,
4792 channel_name, NULL);
4795 if (server->server_type == SILC_SERVER && !server->standalone &&
4799 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4800 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4802 /* Send USERS command */
4803 silc_server_packet_send(server, server->router->connection,
4804 SILC_PACKET_COMMAND, cmd->packet->flags,
4805 tmpbuf->data, tmpbuf->len, TRUE);
4807 /* Reprocess this packet after received reply */
4808 silc_server_command_pending(server, SILC_COMMAND_USERS,
4809 silc_command_get_ident(cmd->payload),
4810 silc_server_command_destructor,
4811 silc_server_command_users,
4812 silc_server_command_dup(cmd));
4813 cmd->pending = TRUE;
4814 silc_command_set_ident(cmd->payload, ident);
4816 silc_buffer_free(tmpbuf);
4821 /* Check the global list as well. */
4823 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4825 channel = silc_idlist_find_channel_by_name(server->global_list,
4826 channel_name, NULL);
4828 /* Channel really does not exist */
4829 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4830 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4835 /* If the channel is private or secret do not send anything */
4836 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)) {
4837 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4838 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4842 /* Get the users list */
4843 silc_server_get_users_on_channel(server, channel, &client_id_list,
4844 &client_mode_list, &list_count);
4847 SILC_PUT32_MSB(list_count, lc);
4850 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4851 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4852 SILC_STATUS_OK, ident, 4,
4853 2, idp->data, idp->len,
4855 4, client_id_list->data,
4856 client_id_list->len,
4857 5, client_mode_list->data,
4858 client_mode_list->len);
4859 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4860 packet->data, packet->len, FALSE);
4862 silc_buffer_free(idp);
4863 silc_buffer_free(packet);
4864 silc_buffer_free(client_id_list);
4865 silc_buffer_free(client_mode_list);
4870 silc_server_command_free(cmd);
4873 /* Server side of command GETKEY. This fetches the client's public key
4874 from the server where to the client is connected. */
4876 SILC_SERVER_CMD_FUNC(getkey)
4878 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4879 SilcServer server = cmd->server;
4881 SilcClientEntry client;
4882 SilcServerEntry server_entry;
4883 SilcClientID *client_id = NULL;
4884 SilcServerID *server_id = NULL;
4885 SilcIDPayload idp = NULL;
4886 uint16 ident = silc_command_get_ident(cmd->payload);
4887 unsigned char *tmp, *pkdata;
4888 uint32 tmp_len, pklen;
4889 SilcBuffer pk = NULL;
4892 SILC_LOG_DEBUG(("Start"));
4894 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4896 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4897 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4900 idp = silc_id_payload_parse_data(tmp, tmp_len);
4902 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4903 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4907 id_type = silc_id_payload_get_type(idp);
4908 if (id_type == SILC_ID_CLIENT) {
4909 client_id = silc_id_payload_get_id(idp);
4911 /* If the client is not found from local list there is no chance it
4912 would be locally connected client so send the command further. */
4913 client = silc_idlist_find_client_by_id(server->local_list,
4914 client_id, TRUE, NULL);
4916 client = silc_idlist_find_client_by_id(server->global_list,
4917 client_id, TRUE, NULL);
4919 if ((!client && !cmd->pending && !server->standalone) ||
4920 (client && !client->connection && !cmd->pending &&
4921 !server->standalone) ||
4922 (client && !client->data.public_key && !cmd->pending &&
4923 !server->standalone)) {
4926 SilcSocketConnection dest_sock;
4928 dest_sock = silc_server_get_client_route(server, NULL, 0,
4933 old_ident = silc_command_get_ident(cmd->payload);
4934 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4935 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4937 silc_server_packet_send(server, dest_sock,
4938 SILC_PACKET_COMMAND, cmd->packet->flags,
4939 tmpbuf->data, tmpbuf->len, TRUE);
4941 /* Reprocess this packet after received reply from router */
4942 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4943 silc_command_get_ident(cmd->payload),
4944 silc_server_command_destructor,
4945 silc_server_command_getkey,
4946 silc_server_command_dup(cmd));
4947 cmd->pending = TRUE;
4949 silc_command_set_ident(cmd->payload, old_ident);
4950 silc_buffer_free(tmpbuf);
4955 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4956 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
4960 /* The client is locally connected, just get the public key and
4962 tmp = silc_pkcs_public_key_encode(client->data.public_key, &tmp_len);
4963 pk = silc_buffer_alloc(4 + tmp_len);
4964 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
4965 silc_buffer_format(pk,
4966 SILC_STR_UI_SHORT(tmp_len),
4967 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
4968 SILC_STR_UI_XNSTRING(tmp, tmp_len),
4973 } else if (id_type == SILC_ID_SERVER) {
4974 server_id = silc_id_payload_get_id(idp);
4976 /* If the server is not found from local list there is no chance it
4977 would be locally connected server so send the command further. */
4978 server_entry = silc_idlist_find_server_by_id(server->local_list,
4979 server_id, TRUE, NULL);
4981 server_entry = silc_idlist_find_server_by_id(server->global_list,
4982 server_id, TRUE, NULL);
4984 if (server_entry != server->id_entry &&
4985 ((!server_entry && !cmd->pending && !server->standalone) ||
4986 (server_entry && !server_entry->connection && !cmd->pending &&
4987 !server->standalone) ||
4988 (server_entry && !server_entry->data.public_key && !cmd->pending &&
4989 !server->standalone))) {
4993 old_ident = silc_command_get_ident(cmd->payload);
4994 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4995 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4997 silc_server_packet_send(server, server->router->connection,
4998 SILC_PACKET_COMMAND, cmd->packet->flags,
4999 tmpbuf->data, tmpbuf->len, TRUE);
5001 /* Reprocess this packet after received reply from router */
5002 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
5003 silc_command_get_ident(cmd->payload),
5004 silc_server_command_destructor,
5005 silc_server_command_getkey,
5006 silc_server_command_dup(cmd));
5007 cmd->pending = TRUE;
5009 silc_command_set_ident(cmd->payload, old_ident);
5010 silc_buffer_free(tmpbuf);
5014 if (!server_entry) {
5015 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5016 SILC_STATUS_ERR_NO_SUCH_SERVER_ID);
5020 /* If they key does not exist then do not send it, send just OK reply */
5021 if (!server_entry->data.public_key) {
5025 tmp = silc_pkcs_public_key_encode(server_entry->data.public_key,
5027 pk = silc_buffer_alloc(4 + tmp_len);
5028 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
5029 silc_buffer_format(pk,
5030 SILC_STR_UI_SHORT(tmp_len),
5031 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
5032 SILC_STR_UI_XNSTRING(tmp, tmp_len),
5042 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5043 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
5044 SILC_STATUS_OK, ident,
5048 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
5049 packet->data, packet->len, FALSE);
5050 silc_buffer_free(packet);
5053 silc_buffer_free(pk);
5057 silc_id_payload_free(idp);
5058 silc_free(client_id);
5059 silc_free(server_id);
5060 silc_server_command_free(cmd);