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,
773 if (!entry && check_global)
774 entry = silc_idlist_find_client_by_id(server->global_list,
777 clients = silc_realloc(clients, sizeof(*clients) *
778 (clients_count + 1));
779 clients[clients_count++] = entry;
783 if (!silc_idlist_get_clients_by_hash(server->local_list,
784 nick, server->md5hash,
785 &clients, &clients_count))
786 silc_idlist_get_clients_by_nickname(server->local_list,
788 &clients, &clients_count);
790 if (!silc_idlist_get_clients_by_hash(server->global_list,
791 nick, server->md5hash,
792 &clients, &clients_count))
793 silc_idlist_get_clients_by_nickname(server->global_list,
795 &clients, &clients_count);
800 /* Such client(s) really does not exist in the SILC network. */
801 if (!client_id_count) {
802 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
803 SILC_STATUS_ERR_NO_SUCH_NICK,
804 3, nick, strlen(nick));
806 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
807 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
808 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
809 2, idp->data, idp->len);
810 silc_buffer_free(idp);
815 /* Router always finds the client entry if it exists in the SILC network.
816 However, it might be incomplete entry and does not include all the
817 mandatory fields that WHOIS command reply requires. Check for these and
818 make query from the server who owns the client if some fields are
820 if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
825 /* Send the command reply */
826 silc_server_command_whois_send_reply(cmd, clients, clients_count,
830 if (client_id_count) {
831 for (i = 0; i < client_id_count; i++)
832 silc_free(client_id[i]);
833 silc_free(client_id);
840 silc_free(server_name);
845 /* Server side of command WHOIS. Processes user's query and sends found
846 results as command replies back to the client. */
848 SILC_SERVER_CMD_FUNC(whois)
850 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
853 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 3328);
855 ret = silc_server_command_whois_process(cmd);
858 silc_server_command_free(cmd);
861 /******************************************************************************
865 ******************************************************************************/
868 silc_server_command_whowas_parse(SilcServerCommandContext cmd,
876 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
878 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WHOWAS,
879 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
883 /* Get the nickname@server string and parse it. */
884 if (strchr(tmp, '@')) {
885 len = strcspn(tmp, "@");
886 *nickname = silc_calloc(len + 1, sizeof(char));
887 memcpy(*nickname, tmp, len);
888 *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
889 memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
891 *nickname = strdup(tmp);
893 /* Get the max count of reply messages allowed */
894 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
904 silc_server_command_whowas_check(SilcServerCommandContext cmd,
905 SilcClientEntry *clients,
906 uint32 clients_count)
908 SilcServer server = cmd->server;
910 SilcClientEntry entry;
912 for (i = 0; i < clients_count; i++) {
915 if (!entry->nickname || !entry->username) {
922 old_ident = silc_command_get_ident(cmd->payload);
923 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
924 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
926 /* Send WHOWAS command */
927 silc_server_packet_send(server, entry->router->connection,
928 SILC_PACKET_COMMAND, cmd->packet->flags,
929 tmpbuf->data, tmpbuf->len, TRUE);
931 /* Reprocess this packet after received reply */
932 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
933 silc_command_get_ident(cmd->payload),
934 silc_server_command_destructor,
935 silc_server_command_whowas,
936 silc_server_command_dup(cmd));
939 silc_command_set_ident(cmd->payload, old_ident);
941 silc_buffer_free(tmpbuf);
950 silc_server_command_whowas_send_reply(SilcServerCommandContext cmd,
951 SilcClientEntry *clients,
952 uint32 clients_count)
954 SilcServer server = cmd->server;
956 int i, count = 0, len;
957 SilcBuffer packet, idp;
958 SilcClientEntry entry = NULL;
959 SilcCommandStatus status;
960 uint16 ident = silc_command_get_ident(cmd->payload);
962 char nh[256], uh[256];
964 status = SILC_STATUS_OK;
965 if (clients_count > 1)
966 status = SILC_STATUS_LIST_START;
968 for (i = 0; i < clients_count; i++) {
971 /* We will take only clients that are not valid anymore. They are the
972 ones that are not registered anymore but still have a ID. They
973 have disconnected us, and thus valid for WHOWAS. */
974 if (entry->data.registered == TRUE)
976 if (entry->id == NULL)
979 if (count && i - 1 == count)
984 if (clients_count > 2)
985 status = SILC_STATUS_LIST_ITEM;
987 if (clients_count > 1 && i == clients_count - 1)
988 status = SILC_STATUS_LIST_END;
990 /* Sanity check, however these should never fail. However, as
991 this sanity check has been added here they have failed. */
992 if (!entry->nickname || !entry->username)
995 /* Send WHOWAS reply */
996 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
997 tmp = silc_argument_get_first_arg(cmd->args, NULL);
999 memset(uh, 0, sizeof(uh));
1000 memset(nh, 0, sizeof(nh));
1002 strncat(nh, entry->nickname, strlen(entry->nickname));
1003 if (!strchr(entry->nickname, '@')) {
1004 strncat(nh, "@", 1);
1005 if (entry->servername) {
1006 strncat(nh, entry->servername, strlen(entry->servername));
1008 len = entry->router ? strlen(entry->router->server_name) :
1009 strlen(server->server_name);
1010 strncat(nh, entry->router ? entry->router->server_name :
1011 server->server_name, len);
1015 strncat(uh, entry->username, strlen(entry->username));
1016 if (!strchr(entry->username, '@')) {
1017 strncat(uh, "@", 1);
1018 strcat(uh, "*private*");
1021 if (entry->userinfo)
1023 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1025 2, idp->data, idp->len,
1029 strlen(entry->userinfo));
1032 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1034 2, idp->data, idp->len,
1038 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1039 0, packet->data, packet->len, FALSE);
1041 silc_buffer_free(packet);
1042 silc_buffer_free(idp);
1045 if (found == FALSE && entry)
1046 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1047 SILC_STATUS_ERR_NO_SUCH_NICK,
1049 strlen(entry->nickname));
1053 silc_server_command_whowas_process(SilcServerCommandContext cmd)
1055 SilcServer server = cmd->server;
1056 char *nick = NULL, *server_name = NULL;
1058 SilcClientEntry *clients = NULL;
1059 uint32 clients_count = 0;
1061 bool check_global = FALSE;
1063 /* Protocol dictates that we must always send the received WHOWAS request
1064 to our router if we are normal server, so let's do it now unless we
1065 are standalone. We will not send any replies to the client until we
1066 have received reply from the router. */
1067 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1068 server->server_type == SILC_SERVER && !cmd->pending &&
1069 !server->standalone) {
1073 old_ident = silc_command_get_ident(cmd->payload);
1074 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1075 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1077 /* Send WHOWAS command to our router */
1078 silc_server_packet_send(server, (SilcSocketConnection)
1079 server->router->connection,
1080 SILC_PACKET_COMMAND, cmd->packet->flags,
1081 tmpbuf->data, tmpbuf->len, TRUE);
1083 /* Reprocess this packet after received reply from router */
1084 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
1085 silc_command_get_ident(cmd->payload),
1086 silc_server_command_destructor,
1087 silc_server_command_whowas,
1088 silc_server_command_dup(cmd));
1089 cmd->pending = TRUE;
1091 silc_command_set_ident(cmd->payload, old_ident);
1093 silc_buffer_free(tmpbuf);
1098 /* We are ready to process the command request. Let's search for the
1099 requested client and send reply to the requesting client. */
1101 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1102 check_global = TRUE;
1103 else if (server->server_type == SILC_ROUTER)
1104 check_global = TRUE;
1106 /* Parse the whowas request */
1107 if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1110 /* Get all clients matching that nickname from local list */
1111 if (!silc_idlist_get_clients_by_nickname(server->local_list,
1113 &clients, &clients_count))
1114 silc_idlist_get_clients_by_hash(server->local_list,
1115 nick, server->md5hash,
1116 &clients, &clients_count);
1118 /* Check global list as well */
1120 if (!silc_idlist_get_clients_by_nickname(server->global_list,
1122 &clients, &clients_count))
1123 silc_idlist_get_clients_by_hash(server->global_list,
1124 nick, server->md5hash,
1125 &clients, &clients_count);
1129 /* Such a client really does not exist in the SILC network. */
1130 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1131 SILC_STATUS_ERR_NO_SUCH_NICK,
1132 3, nick, strlen(nick));
1136 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1137 !silc_server_command_whowas_check(cmd, clients, clients_count)) {
1142 /* Send the command reply to the client */
1143 silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1151 silc_free(server_name);
1156 /* Server side of command WHOWAS. */
1158 SILC_SERVER_CMD_FUNC(whowas)
1160 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1163 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
1165 ret = silc_server_command_whowas_process(cmd);
1168 silc_server_command_free(cmd);
1171 /******************************************************************************
1175 ******************************************************************************/
1178 silc_server_command_identify_parse(SilcServerCommandContext cmd,
1179 SilcClientEntry **clients,
1180 uint32 *clients_count,
1181 SilcServerEntry **servers,
1182 uint32 *servers_count,
1183 SilcChannelEntry **channels,
1184 uint32 *channels_count,
1187 SilcServer server = cmd->server;
1190 uint32 argc = silc_argument_get_arg_num(cmd->args);
1192 bool check_global = FALSE;
1197 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1198 check_global = TRUE;
1199 else if (server->server_type == SILC_ROUTER)
1200 check_global = TRUE;
1202 /* If ID Payload is in the command it must be used instead of names */
1203 tmp = silc_argument_get_arg_type(cmd->args, 5, &len);
1205 /* No ID, get the names. */
1207 /* Try to get nickname@server. */
1208 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
1211 char *nick_server = NULL;
1213 if (strchr(tmp, '@')) {
1214 len = strcspn(tmp, "@");
1215 nick = silc_calloc(len + 1, sizeof(char));
1216 memcpy(nick, tmp, len);
1217 nick_server = silc_calloc(strlen(tmp) - len, sizeof(char));
1218 memcpy(nick_server, tmp + len + 1, strlen(tmp) - len - 1);
1223 if (!silc_idlist_get_clients_by_hash(server->local_list,
1224 nick, server->md5hash,
1225 clients, clients_count))
1226 silc_idlist_get_clients_by_nickname(server->local_list,
1228 clients, clients_count);
1230 if (!silc_idlist_get_clients_by_hash(server->global_list,
1231 nick, server->md5hash,
1232 clients, clients_count))
1233 silc_idlist_get_clients_by_nickname(server->global_list,
1235 clients, clients_count);
1239 silc_free(nick_server);
1242 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1243 SILC_STATUS_ERR_NO_SUCH_NICK,
1244 3, tmp, strlen(tmp));
1249 /* Try to get server name */
1250 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
1252 entry = silc_idlist_find_server_by_name(server->local_list,
1254 if (!entry && check_global)
1255 entry = silc_idlist_find_server_by_name(server->local_list,
1258 *servers = silc_realloc(*servers, sizeof(**servers) *
1259 (*servers_count + 1));
1260 (*servers)[(*servers_count)++] = entry;
1264 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1265 SILC_STATUS_ERR_NO_SUCH_SERVER,
1266 3, tmp, strlen(tmp));
1271 /* Try to get channel name */
1272 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
1274 entry = silc_idlist_find_channel_by_name(server->local_list,
1276 if (!entry && check_global)
1277 entry = silc_idlist_find_channel_by_name(server->local_list,
1280 *channels = silc_realloc(*channels, sizeof(**channels) *
1281 (*channels_count + 1));
1282 (*channels)[(*channels_count)++] = entry;
1286 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1287 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
1288 3, tmp, strlen(tmp));
1293 if (!(*clients) && !(*servers) && !(*channels)) {
1294 silc_server_command_send_status_reply(cmd, SILC_COMMAND_IDENTIFY,
1295 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1299 /* Command includes ID, we must use that. Also check whether the command
1300 has more than one ID set - take them all. */
1302 /* Take all ID's from the command packet */
1303 for (i = 0; i < argc; i++) {
1306 tmp = silc_argument_get_arg_type(cmd->args, i + 5, &len);
1310 idp = silc_id_payload_parse_data(tmp, len);
1312 silc_free(*clients);
1313 silc_free(*servers);
1314 silc_free(*channels);
1315 silc_server_command_send_status_reply(cmd, SILC_COMMAND_IDENTIFY,
1316 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1320 id = silc_id_payload_get_id(idp);
1322 switch (silc_id_payload_get_type(idp)) {
1324 case SILC_ID_CLIENT:
1325 entry = (void *)silc_idlist_find_client_by_id(server->local_list,
1327 if (!entry && check_global)
1328 entry = (void *)silc_idlist_find_client_by_id(server->global_list,
1331 *clients = silc_realloc(*clients, sizeof(**clients) *
1332 (*clients_count + 1));
1333 (*clients)[(*clients_count)++] = (SilcClientEntry)entry;
1335 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1336 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1343 case SILC_ID_SERVER:
1344 entry = (void *)silc_idlist_find_server_by_id(server->local_list,
1346 if (!entry && check_global)
1347 entry = (void *)silc_idlist_find_server_by_id(server->global_list,
1350 *servers = silc_realloc(*servers, sizeof(**servers) *
1351 (*servers_count + 1));
1352 (*servers)[(*servers_count)++] = (SilcServerEntry)entry;
1354 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1355 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
1361 case SILC_ID_CHANNEL:
1362 entry = (void *)silc_idlist_find_channel_by_id(server->local_list,
1364 if (!entry && check_global)
1365 entry = (void *)silc_idlist_find_channel_by_id(server->global_list,
1368 *channels = silc_realloc(*channels, sizeof(**channels) *
1369 (*channels_count + 1));
1370 (*channels)[(*channels_count)++] = (SilcChannelEntry)entry;
1372 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1373 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
1385 silc_free(*clients);
1386 silc_free(*servers);
1387 silc_free(*channels);
1391 /* Get the max count of reply messages allowed */
1392 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
1401 /* Checks that all mandatory fields in client entry are present. If not
1402 then send WHOIS request to the server who owns the client. We use
1403 WHOIS because we want to get as much information as possible at once. */
1406 silc_server_command_identify_check_client(SilcServerCommandContext cmd,
1407 SilcClientEntry *clients,
1408 uint32 clients_count)
1410 SilcServer server = cmd->server;
1412 SilcClientEntry entry;
1414 for (i = 0; i < clients_count; i++) {
1417 if (!entry || entry->data.registered == FALSE)
1420 if (!entry->nickname) {
1427 old_ident = silc_command_get_ident(cmd->payload);
1428 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1429 silc_command_set_command(cmd->payload, SILC_COMMAND_WHOIS);
1430 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1432 /* Send WHOIS request. We send WHOIS since we're doing the requesting
1433 now anyway so make it a good one. */
1434 silc_server_packet_send(server, entry->router->connection,
1435 SILC_PACKET_COMMAND, cmd->packet->flags,
1436 tmpbuf->data, tmpbuf->len, TRUE);
1438 /* Reprocess this packet after received reply */
1439 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1440 silc_command_get_ident(cmd->payload),
1441 silc_server_command_destructor,
1442 silc_server_command_identify,
1443 silc_server_command_dup(cmd));
1445 cmd->pending = TRUE;
1447 /* Put old data back to the Command Payload we just changed */
1448 silc_command_set_ident(cmd->payload, old_ident);
1449 silc_command_set_command(cmd->payload, SILC_COMMAND_IDENTIFY);
1451 silc_buffer_free(tmpbuf);
1460 silc_server_command_identify_send_reply(SilcServerCommandContext cmd,
1461 SilcClientEntry *clients,
1462 uint32 clients_count,
1463 SilcServerEntry *servers,
1464 uint32 servers_count,
1465 SilcChannelEntry *channels,
1466 uint32 channels_count,
1469 SilcServer server = cmd->server;
1471 SilcBuffer packet, idp;
1472 SilcCommandStatus status;
1473 uint16 ident = silc_command_get_ident(cmd->payload);
1474 char nh[256], uh[256];
1475 SilcSocketConnection hsock;
1477 status = SILC_STATUS_OK;
1480 SilcClientEntry entry;
1483 for (i = 0; i < clients_count; i++)
1484 if (clients[i]->data.registered)
1488 status = SILC_STATUS_LIST_START;
1490 for (i = 0, k = 0; i < clients_count; i++) {
1493 if (entry->data.registered == FALSE) {
1494 if (clients_count == 1) {
1495 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1496 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1497 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1498 2, idp->data, idp->len);
1499 silc_buffer_free(idp);
1505 status = SILC_STATUS_LIST_ITEM;
1506 if (clients_count > 1 && k == clients_count - 1
1507 && !servers_count && !channels_count)
1508 status = SILC_STATUS_LIST_END;
1509 if (count && k - 1 == count)
1510 status = SILC_STATUS_LIST_END;
1511 if (count && k - 1 > count)
1514 /* Send IDENTIFY reply */
1515 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1517 memset(uh, 0, sizeof(uh));
1518 memset(nh, 0, sizeof(nh));
1520 strncat(nh, entry->nickname, strlen(entry->nickname));
1521 if (!strchr(entry->nickname, '@')) {
1522 strncat(nh, "@", 1);
1523 if (entry->servername) {
1524 strncat(nh, entry->servername, strlen(entry->servername));
1526 len = entry->router ? strlen(entry->router->server_name) :
1527 strlen(server->server_name);
1528 strncat(nh, entry->router ? entry->router->server_name :
1529 server->server_name, len);
1533 if (!entry->username) {
1534 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1536 2, idp->data, idp->len,
1539 strncat(uh, entry->username, strlen(entry->username));
1540 if (!strchr(entry->username, '@')) {
1541 strncat(uh, "@", 1);
1542 hsock = (SilcSocketConnection)entry->connection;
1543 len = strlen(hsock->hostname);
1544 strncat(uh, hsock->hostname, len);
1547 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1549 2, idp->data, idp->len,
1554 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1555 0, packet->data, packet->len, FALSE);
1557 silc_buffer_free(packet);
1558 silc_buffer_free(idp);
1564 status = (status == SILC_STATUS_LIST_ITEM ?
1565 SILC_STATUS_LIST_ITEM : SILC_STATUS_OK);
1568 SilcServerEntry entry;
1570 if (status == SILC_STATUS_OK && servers_count > 1)
1571 status = SILC_STATUS_LIST_START;
1573 for (i = 0, k = 0; i < servers_count; i++) {
1577 status = SILC_STATUS_LIST_ITEM;
1578 if (servers_count > 1 && k == servers_count - 1 && !channels_count)
1579 status = SILC_STATUS_LIST_END;
1580 if (count && k - 1 == count)
1581 status = SILC_STATUS_LIST_END;
1582 if (count && k - 1 > count)
1585 /* Send IDENTIFY reply */
1586 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1587 if (entry->server_name) {
1589 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1591 2, idp->data, idp->len,
1592 3, entry->server_name,
1593 strlen(entry->server_name));
1595 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1597 2, idp->data, idp->len);
1600 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1601 0, packet->data, packet->len, FALSE);
1603 silc_buffer_free(packet);
1604 silc_buffer_free(idp);
1610 status = (status == SILC_STATUS_LIST_ITEM ?
1611 SILC_STATUS_LIST_ITEM : SILC_STATUS_OK);
1614 SilcChannelEntry entry;
1616 if (status == SILC_STATUS_OK && channels_count > 1)
1617 status = SILC_STATUS_LIST_START;
1619 for (i = 0, k = 0; i < channels_count; i++) {
1620 entry = channels[i];
1623 status = SILC_STATUS_LIST_ITEM;
1624 if (channels_count > 1 && k == channels_count - 1)
1625 status = SILC_STATUS_LIST_END;
1626 if (count && k - 1 == count)
1627 status = SILC_STATUS_LIST_END;
1628 if (count && k - 1 > count)
1631 /* Send IDENTIFY reply */
1632 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1633 if (entry->channel_name) {
1635 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1637 2, idp->data, idp->len,
1638 3, entry->channel_name,
1639 strlen(entry->channel_name));
1641 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1643 2, idp->data, idp->len);
1646 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1647 0, packet->data, packet->len, FALSE);
1649 silc_buffer_free(packet);
1650 silc_buffer_free(idp);
1658 silc_server_command_identify_process(SilcServerCommandContext cmd)
1660 SilcServer server = cmd->server;
1663 SilcClientEntry *clients = NULL;
1664 SilcServerEntry *servers = NULL;
1665 SilcChannelEntry *channels = NULL;
1666 uint32 clients_count = 0, servers_count = 0, channels_count = 0;
1668 /* Protocol dictates that we must always send the received IDENTIFY request
1669 to our router if we are normal server, so let's do it now unless we
1670 are standalone. We will not send any replies to the client until we
1671 have received reply from the router. */
1672 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1673 server->server_type == SILC_SERVER && !cmd->pending &&
1674 !server->standalone) {
1678 old_ident = silc_command_get_ident(cmd->payload);
1679 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1680 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1682 /* Send IDENTIFY command to our router */
1683 silc_server_packet_send(server, (SilcSocketConnection)
1684 server->router->connection,
1685 SILC_PACKET_COMMAND, cmd->packet->flags,
1686 tmpbuf->data, tmpbuf->len, TRUE);
1688 /* Reprocess this packet after received reply from router */
1689 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
1690 silc_command_get_ident(cmd->payload),
1691 silc_server_command_destructor,
1692 silc_server_command_identify,
1693 silc_server_command_dup(cmd));
1694 cmd->pending = TRUE;
1696 silc_command_set_ident(cmd->payload, old_ident);
1698 silc_buffer_free(tmpbuf);
1703 /* We are ready to process the command request. Let's search for the
1704 requested client and send reply to the requesting client. */
1706 /* Parse the IDENTIFY request */
1707 if (!silc_server_command_identify_parse(cmd,
1708 &clients, &clients_count,
1709 &servers, &servers_count,
1710 &channels, &channels_count,
1714 /* Check that all mandatory fields are present and request those data
1715 from the server who owns the client if necessary. */
1716 if (clients && !silc_server_command_identify_check_client(cmd, clients,
1722 /* Send the command reply to the client */
1723 silc_server_command_identify_send_reply(cmd,
1724 clients, clients_count,
1725 servers, servers_count,
1726 channels, channels_count,
1732 silc_free(channels);
1737 SILC_SERVER_CMD_FUNC(identify)
1739 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1742 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 3328);
1744 ret = silc_server_command_identify_process(cmd);
1747 silc_server_command_free(cmd);
1750 /* Checks string for bad characters and returns TRUE if they are found. */
1752 static int silc_server_command_bad_chars(char *nick)
1754 if (strchr(nick, '\\')) return TRUE;
1755 if (strchr(nick, '\"')) return TRUE;
1756 if (strchr(nick, '´')) return TRUE;
1757 if (strchr(nick, '`')) return TRUE;
1758 if (strchr(nick, '\'')) return TRUE;
1759 if (strchr(nick, '*')) return TRUE;
1760 if (strchr(nick, '/')) return TRUE;
1761 if (strchr(nick, '@')) return TRUE;
1766 /* Server side of command NICK. Sets nickname for user. Setting
1767 nickname causes generation of a new client ID for the client. The
1768 new client ID is sent to the client after changing the nickname. */
1770 SILC_SERVER_CMD_FUNC(nick)
1772 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1773 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1774 SilcServer server = cmd->server;
1775 SilcBuffer packet, nidp, oidp;
1776 SilcClientID *new_id;
1778 uint16 ident = silc_command_get_ident(cmd->payload);
1780 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1783 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
1785 /* Check nickname */
1786 nick = silc_argument_get_arg_type(cmd->args, 1, NULL);
1787 if (silc_server_command_bad_chars(nick) == TRUE) {
1788 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
1789 SILC_STATUS_ERR_BAD_NICKNAME);
1793 if (strlen(nick) > 128)
1796 /* Create new Client ID */
1797 silc_id_create_client_id(cmd->server->id, cmd->server->rng,
1798 cmd->server->md5hash, nick,
1801 /* Send notify about nickname change to our router. We send the new
1802 ID and ask to replace it with the old one. If we are router the
1803 packet is broadcasted. Send NICK_CHANGE notify. */
1804 if (!server->standalone)
1805 silc_server_send_notify_nick_change(server, server->router->connection,
1806 server->server_type == SILC_SERVER ?
1807 FALSE : TRUE, client->id,
1810 /* Remove old cache entry */
1811 silc_idcache_del_by_context(server->local_list->clients, client);
1813 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1817 silc_free(client->id);
1819 /* Save the nickname as this client is our local client */
1820 if (client->nickname)
1821 silc_free(client->nickname);
1823 client->nickname = strdup(nick);
1824 client->id = new_id;
1826 /* Update client cache */
1827 silc_idcache_add(server->local_list->clients, client->nickname,
1828 client->id, (void *)client, FALSE);
1830 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1832 /* Send NICK_CHANGE notify to the client's channels */
1833 silc_server_send_notify_on_channels(server, NULL, client,
1834 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
1835 oidp->data, oidp->len,
1836 nidp->data, nidp->len);
1838 /* Send the new Client ID as reply command back to client */
1839 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
1840 SILC_STATUS_OK, ident, 1,
1841 2, nidp->data, nidp->len);
1842 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1843 0, packet->data, packet->len, FALSE);
1845 silc_buffer_free(packet);
1846 silc_buffer_free(nidp);
1847 silc_buffer_free(oidp);
1850 silc_server_command_free(cmd);
1853 /* Sends the LIST command reply */
1856 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
1857 SilcChannelEntry *lch,
1859 SilcChannelEntry *gch,
1863 SilcBuffer packet, idp;
1864 SilcChannelEntry entry;
1865 SilcCommandStatus status;
1866 uint16 ident = silc_command_get_ident(cmd->payload);
1868 unsigned char usercount[4];
1871 for (i = 0; i < lch_count; i++)
1872 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1874 for (i = 0; i < gch_count; i++)
1875 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1878 status = SILC_STATUS_OK;
1879 if ((lch_count + gch_count) > 1)
1880 status = SILC_STATUS_LIST_START;
1883 for (i = 0; i < lch_count; i++) {
1890 status = SILC_STATUS_LIST_ITEM;
1892 if (i == lch_count - 1 && gch_count)
1894 if (lch_count > 1 && i == lch_count - 1)
1895 status = SILC_STATUS_LIST_END;
1897 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1899 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1900 topic = "*private*";
1901 memset(usercount, 0, sizeof(usercount));
1903 topic = entry->topic;
1904 users = silc_hash_table_count(entry->user_list);
1905 SILC_PUT32_MSB(users, usercount);
1908 /* Send the reply */
1911 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1913 2, idp->data, idp->len,
1914 3, entry->channel_name,
1915 strlen(entry->channel_name),
1916 4, topic, strlen(topic),
1920 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1922 2, idp->data, idp->len,
1923 3, entry->channel_name,
1924 strlen(entry->channel_name),
1926 silc_server_packet_send(cmd->server, cmd->sock,
1927 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1928 packet->len, FALSE);
1929 silc_buffer_free(packet);
1930 silc_buffer_free(idp);
1933 status = i ? SILC_STATUS_LIST_ITEM : SILC_STATUS_OK;
1936 for (i = 0; i < gch_count; i++) {
1943 status = SILC_STATUS_LIST_ITEM;
1945 if (gch_count > 1 && i == lch_count - 1)
1946 status = SILC_STATUS_LIST_END;
1948 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1950 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1951 topic = "*private*";
1952 memset(usercount, 0, sizeof(usercount));
1954 topic = entry->topic;
1955 users = silc_hash_table_count(entry->user_list);
1956 SILC_PUT32_MSB(users, usercount);
1959 /* Send the reply */
1962 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1964 2, idp->data, idp->len,
1965 3, entry->channel_name,
1966 strlen(entry->channel_name),
1967 4, topic, strlen(topic),
1971 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1973 2, idp->data, idp->len,
1974 3, entry->channel_name,
1975 strlen(entry->channel_name),
1977 silc_server_packet_send(cmd->server, cmd->sock,
1978 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1979 packet->len, FALSE);
1980 silc_buffer_free(packet);
1981 silc_buffer_free(idp);
1985 /* Server side of LIST command. This lists the channel of the requested
1986 server. Secret channels are not listed. */
1988 SILC_SERVER_CMD_FUNC(list)
1990 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1991 SilcServer server = cmd->server;
1992 SilcChannelID *channel_id = NULL;
1995 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
1996 uint32 lch_count = 0, gch_count = 0;
1998 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 2);
2000 /* Get Channel ID */
2001 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2003 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2005 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
2006 SILC_STATUS_ERR_NO_CHANNEL_ID);
2011 /* Get the channels from local list */
2012 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
2015 /* Get the channels from global list if we are router */
2016 if (server->server_type == SILC_ROUTER)
2017 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
2020 /* Send the reply */
2021 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
2022 gchannels, gch_count);
2025 silc_server_command_free(cmd);
2028 /* Server side of TOPIC command. Sets topic for channel and/or returns
2029 current topic to client. */
2031 SILC_SERVER_CMD_FUNC(topic)
2033 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2034 SilcServer server = cmd->server;
2035 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2036 SilcChannelID *channel_id;
2037 SilcChannelEntry channel;
2038 SilcChannelClientEntry chl;
2039 SilcBuffer packet, idp;
2041 uint32 argc, tmp_len;
2042 uint16 ident = silc_command_get_ident(cmd->payload);
2044 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
2046 argc = silc_argument_get_arg_num(cmd->args);
2048 /* Get Channel ID */
2049 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2051 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2052 SILC_STATUS_ERR_NO_CHANNEL_ID);
2055 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2057 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2058 SILC_STATUS_ERR_NO_CHANNEL_ID);
2062 /* Check whether the channel exists */
2063 channel = silc_idlist_find_channel_by_id(server->local_list,
2066 channel = silc_idlist_find_channel_by_id(server->global_list,
2069 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2070 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2077 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
2079 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2080 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2084 if (strlen(tmp) > 256) {
2085 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2086 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2090 /* See whether the client is on channel and has rights to change topic */
2091 if (!silc_hash_table_find(channel->user_list, client, NULL,
2093 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2094 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2098 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2099 if (channel->mode & SILC_CHANNEL_MODE_TOPIC) {
2100 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2101 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2106 /* Set the topic for channel */
2108 silc_free(channel->topic);
2109 channel->topic = strdup(tmp);
2111 /* Send TOPIC_SET notify type to the network */
2112 if (!server->standalone)
2113 silc_server_send_notify_topic_set(server, server->router->connection,
2114 server->server_type == SILC_ROUTER ?
2115 TRUE : FALSE, channel, client->id,
2118 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2120 /* Send notify about topic change to all clients on the channel */
2121 silc_server_send_notify_to_channel(server, NULL, channel, TRUE,
2122 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
2123 idp->data, idp->len,
2124 channel->topic, strlen(channel->topic));
2125 silc_buffer_free(idp);
2128 /* Send the topic to client as reply packet */
2129 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2131 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2132 SILC_STATUS_OK, ident, 2,
2133 2, idp->data, idp->len,
2135 strlen(channel->topic));
2137 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2138 SILC_STATUS_OK, ident, 1,
2139 2, idp->data, idp->len);
2140 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2141 0, packet->data, packet->len, FALSE);
2143 silc_buffer_free(packet);
2144 silc_buffer_free(idp);
2145 silc_free(channel_id);
2148 silc_server_command_free(cmd);
2151 /* Server side of INVITE command. Invites some client to join some channel.
2152 This command is also used to manage the invite list of the channel. */
2154 SILC_SERVER_CMD_FUNC(invite)
2156 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2157 SilcServer server = cmd->server;
2158 SilcSocketConnection sock = cmd->sock, dest_sock;
2159 SilcChannelClientEntry chl;
2160 SilcClientEntry sender, dest;
2161 SilcClientID *dest_id = NULL;
2162 SilcChannelEntry channel;
2163 SilcChannelID *channel_id = NULL;
2164 SilcIDListData idata;
2165 SilcBuffer idp, idp2, packet;
2166 unsigned char *tmp, *add, *del;
2168 uint16 ident = silc_command_get_ident(cmd->payload);
2170 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
2172 /* Get Channel ID */
2173 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2175 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2176 SILC_STATUS_ERR_NO_CHANNEL_ID);
2179 channel_id = silc_id_payload_parse_id(tmp, len);
2181 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2182 SILC_STATUS_ERR_NO_CHANNEL_ID);
2186 /* Get the channel entry */
2187 channel = silc_idlist_find_channel_by_id(server->local_list,
2190 channel = silc_idlist_find_channel_by_id(server->global_list,
2193 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2194 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2199 /* Check whether the sender of this command is on the channel. */
2200 sender = (SilcClientEntry)sock->user_data;
2201 if (!silc_server_client_on_channel(sender, channel)) {
2202 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2203 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2207 /* Check whether the channel is invite-only channel. If yes then the
2208 sender of this command must be at least channel operator. */
2209 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2210 silc_hash_table_find(channel->user_list, sender, NULL, (void *)&chl);
2211 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2212 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2213 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2218 /* Get destination client ID */
2219 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
2223 dest_id = silc_id_payload_parse_id(tmp, len);
2225 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2226 SILC_STATUS_ERR_NO_CLIENT_ID);
2230 /* Get the client entry */
2231 dest = silc_server_get_client_resolve(server, dest_id);
2233 if (server->server_type == SILC_ROUTER) {
2234 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2235 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2239 /* The client info is being resolved. Reprocess this packet after
2240 receiving the reply to the query. */
2241 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2243 silc_server_command_destructor,
2244 silc_server_command_invite,
2245 silc_server_command_dup(cmd));
2246 cmd->pending = TRUE;
2247 silc_free(channel_id);
2252 /* Check whether the requested client is already on the channel. */
2253 if (silc_server_client_on_channel(dest, channel)) {
2254 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2255 SILC_STATUS_ERR_USER_ON_CHANNEL);
2259 /* Get route to the client */
2260 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id, &idata);
2262 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2263 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2267 memset(invite, 0, sizeof(invite));
2268 strncat(invite, dest->nickname, strlen(dest->nickname));
2269 strncat(invite, "!", 1);
2270 strncat(invite, dest->username, strlen(dest->username));
2271 if (!strchr(dest->username, '@')) {
2272 strncat(invite, "@", 1);
2273 strncat(invite, cmd->sock->hostname, strlen(cmd->sock->hostname));
2276 len = strlen(invite);
2277 if (!channel->invite_list)
2278 channel->invite_list = silc_calloc(len + 2,
2279 sizeof(*channel->invite_list));
2281 channel->invite_list = silc_realloc(channel->invite_list,
2282 sizeof(*channel->invite_list) *
2284 strlen(channel->invite_list) + 2));
2285 strncat(channel->invite_list, invite, len);
2286 strncat(channel->invite_list, ",", 1);
2288 /* Send notify to the client that is invited to the channel */
2289 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2290 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
2291 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
2293 SILC_NOTIFY_TYPE_INVITE, 3,
2294 idp->data, idp->len,
2295 channel->channel_name,
2296 strlen(channel->channel_name),
2297 idp2->data, idp2->len);
2298 silc_buffer_free(idp);
2299 silc_buffer_free(idp2);
2302 /* Add the client to the invite list of the channel */
2303 add = silc_argument_get_arg_type(cmd->args, 3, &len);
2305 if (!channel->invite_list)
2306 channel->invite_list = silc_calloc(len + 2,
2307 sizeof(*channel->invite_list));
2309 channel->invite_list = silc_realloc(channel->invite_list,
2310 sizeof(*channel->invite_list) *
2312 strlen(channel->invite_list) + 2));
2313 if (add[len - 1] == ',')
2314 add[len - 1] = '\0';
2316 strncat(channel->invite_list, add, len);
2317 strncat(channel->invite_list, ",", 1);
2320 /* Get the invite to be removed and remove it from the list */
2321 del = silc_argument_get_arg_type(cmd->args, 4, &len);
2322 if (del && channel->invite_list) {
2323 char *start, *end, *n;
2325 if (!strncmp(channel->invite_list, del,
2326 strlen(channel->invite_list) - 1)) {
2327 silc_free(channel->invite_list);
2328 channel->invite_list = NULL;
2330 start = strstr(channel->invite_list, del);
2331 if (start && strlen(start) >= len) {
2333 n = silc_calloc(strlen(channel->invite_list) - len, sizeof(*n));
2334 strncat(n, channel->invite_list, start - channel->invite_list);
2335 strncat(n, end + 1, ((channel->invite_list +
2336 strlen(channel->invite_list)) - end) - 1);
2337 silc_free(channel->invite_list);
2338 channel->invite_list = n;
2343 /* Send notify to the primary router */
2344 if (!server->standalone)
2345 silc_server_send_notify_invite(server, server->router->connection,
2346 server->server_type == SILC_ROUTER ?
2347 TRUE : FALSE, channel,
2348 sender->id, add, del);
2350 /* Send command reply */
2351 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2355 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2356 SILC_STATUS_OK, ident, 2,
2358 3, channel->invite_list,
2359 channel->invite_list ?
2360 strlen(channel->invite_list) : 0);
2363 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2364 SILC_STATUS_OK, ident, 1,
2366 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2367 packet->data, packet->len, FALSE);
2368 silc_buffer_free(packet);
2374 silc_free(channel_id);
2375 silc_server_command_free(cmd);
2380 SilcSocketConnection sock;
2384 /* Quits connection to client. This gets called if client won't
2385 close the connection even when it has issued QUIT command. */
2387 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
2389 QuitInternal q = (QuitInternal)context;
2391 /* Free all client specific data, such as client entry and entires
2392 on channels this client may be on. */
2393 silc_server_free_client_data(q->server, q->sock, q->sock->user_data,
2395 q->sock->user_data = NULL;
2397 /* Close the connection on our side */
2398 silc_server_close_connection(q->server, q->sock);
2400 silc_free(q->signoff);
2404 /* Quits SILC session. This is the normal way to disconnect client. */
2406 SILC_SERVER_CMD_FUNC(quit)
2408 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2409 SilcServer server = cmd->server;
2410 SilcSocketConnection sock = cmd->sock;
2412 unsigned char *tmp = NULL;
2415 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
2417 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2420 /* Get destination ID */
2421 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2425 q = silc_calloc(1, sizeof(*q));
2428 q->signoff = tmp ? strdup(tmp) : NULL;
2430 /* We quit the connection with little timeout */
2431 silc_schedule_task_add(server->schedule, sock->sock,
2432 silc_server_command_quit_cb, (void *)q,
2433 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2436 silc_server_command_free(cmd);
2439 /* Server side of command KILL. This command is used by router operator
2440 to remove an client from the SILC Network temporarily. */
2442 SILC_SERVER_CMD_FUNC(kill)
2444 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2445 SilcServer server = cmd->server;
2446 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2447 SilcClientEntry remote_client;
2448 SilcClientID *client_id;
2449 unsigned char *tmp, *comment;
2450 uint32 tmp_len, tmp_len2;
2452 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 2);
2454 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2457 /* KILL command works only on router */
2458 if (server->server_type != SILC_ROUTER) {
2459 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2460 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2464 /* Check whether client has the permissions. */
2465 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2466 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2467 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2471 /* Get the client ID */
2472 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2474 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2475 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2478 client_id = silc_id_payload_parse_id(tmp, tmp_len);
2480 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2481 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2485 /* Get the client entry */
2486 remote_client = silc_idlist_find_client_by_id(server->local_list,
2488 if (!remote_client) {
2489 remote_client = silc_idlist_find_client_by_id(server->global_list,
2491 if (!remote_client) {
2492 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2493 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2498 if (remote_client->data.registered == FALSE) {
2499 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2500 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2505 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
2509 /* Send reply to the sender */
2510 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2513 /* Send the KILL notify packets. First send it to the channel, then
2514 to our primary router and then directly to the client who is being
2515 killed right now. */
2517 /* Send KILLED notify to the channels. It is not sent to the client
2518 as it will be sent differently destined directly to the client and not
2520 silc_server_send_notify_on_channels(server, remote_client,
2521 remote_client, SILC_NOTIFY_TYPE_KILLED,
2524 comment, comment ? tmp_len2 : 0);
2526 /* Send KILLED notify to primary route */
2527 if (!server->standalone)
2528 silc_server_send_notify_killed(server, server->router->connection, TRUE,
2529 remote_client->id, comment);
2531 /* Send KILLED notify to the client directly */
2532 silc_server_send_notify_killed(server, remote_client->connection ?
2533 remote_client->connection :
2534 remote_client->router->connection, FALSE,
2535 remote_client->id, comment);
2537 /* Remove the client from all channels. This generates new keys to the
2538 channels as well. */
2539 silc_server_remove_from_channels(server, NULL, remote_client, FALSE,
2542 /* Remove the client entry, If it is locally connected then we will also
2543 disconnect the client here */
2544 if (remote_client->data.registered && remote_client->connection) {
2545 /* Remove locally conneted client */
2546 SilcSocketConnection sock = remote_client->connection;
2547 silc_server_free_client_data(server, sock, remote_client, FALSE, NULL);
2548 silc_server_close_connection(server, sock);
2550 /* Remove remote client */
2551 if (!silc_idlist_del_client(server->global_list, remote_client))
2552 silc_idlist_del_client(server->local_list, remote_client);
2556 silc_server_command_free(cmd);
2559 /* Server side of command INFO. This sends information about us to
2560 the client. If client requested specific server we will send the
2561 command to that server. */
2563 SILC_SERVER_CMD_FUNC(info)
2565 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2566 SilcServer server = cmd->server;
2567 SilcBuffer packet, idp;
2570 char *dest_server, *server_info = NULL, *server_name;
2571 uint16 ident = silc_command_get_ident(cmd->payload);
2572 SilcServerEntry entry = NULL;
2573 SilcServerID *server_id = NULL;
2575 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
2577 /* Get server name */
2578 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2581 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2583 server_id = silc_id_payload_parse_id(tmp, tmp_len);
2585 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2586 SILC_STATUS_ERR_NO_SERVER_ID);
2592 /* Check whether we have this server cached */
2593 entry = silc_idlist_find_server_by_id(server->local_list,
2596 entry = silc_idlist_find_server_by_id(server->global_list,
2598 if (!entry && server->server_type == SILC_ROUTER) {
2599 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2600 SILC_STATUS_ERR_NO_SUCH_SERVER);
2606 if ((!dest_server && !server_id && !entry) || (entry &&
2607 entry == server->id_entry) ||
2608 (dest_server && !cmd->pending &&
2609 !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
2610 /* Send our reply */
2611 char info_string[256];
2613 memset(info_string, 0, sizeof(info_string));
2614 snprintf(info_string, sizeof(info_string),
2615 "location: %s server: %s admin: %s <%s>",
2616 server->config->admin_info->location,
2617 server->config->admin_info->server_type,
2618 server->config->admin_info->admin_name,
2619 server->config->admin_info->admin_email);
2621 server_info = info_string;
2622 entry = server->id_entry;
2624 /* Check whether we have this server cached */
2625 if (!entry && dest_server) {
2626 entry = silc_idlist_find_server_by_name(server->global_list,
2629 entry = silc_idlist_find_server_by_name(server->local_list,
2634 if (!cmd->pending &&
2635 server->server_type == SILC_ROUTER && entry && !entry->server_info) {
2636 /* Send to the server */
2640 old_ident = silc_command_get_ident(cmd->payload);
2641 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2642 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2644 silc_server_packet_send(server, entry->connection,
2645 SILC_PACKET_COMMAND, cmd->packet->flags,
2646 tmpbuf->data, tmpbuf->len, TRUE);
2648 /* Reprocess this packet after received reply from router */
2649 silc_server_command_pending(server, SILC_COMMAND_INFO,
2650 silc_command_get_ident(cmd->payload),
2651 silc_server_command_destructor,
2652 silc_server_command_info,
2653 silc_server_command_dup(cmd));
2654 cmd->pending = TRUE;
2655 silc_command_set_ident(cmd->payload, old_ident);
2656 silc_buffer_free(tmpbuf);
2660 if (!entry && !cmd->pending && !server->standalone) {
2661 /* Send to the primary router */
2665 old_ident = silc_command_get_ident(cmd->payload);
2666 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2667 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2669 silc_server_packet_send(server, server->router->connection,
2670 SILC_PACKET_COMMAND, cmd->packet->flags,
2671 tmpbuf->data, tmpbuf->len, TRUE);
2673 /* Reprocess this packet after received reply from router */
2674 silc_server_command_pending(server, SILC_COMMAND_INFO,
2675 silc_command_get_ident(cmd->payload),
2676 silc_server_command_destructor,
2677 silc_server_command_info,
2678 silc_server_command_dup(cmd));
2679 cmd->pending = TRUE;
2680 silc_command_set_ident(cmd->payload, old_ident);
2681 silc_buffer_free(tmpbuf);
2687 silc_free(server_id);
2690 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2691 SILC_STATUS_ERR_NO_SUCH_SERVER);
2695 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2697 server_info = entry->server_info;
2698 server_name = entry->server_name;
2700 /* Send the reply */
2702 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2703 SILC_STATUS_OK, ident, 3,
2704 2, idp->data, idp->len,
2706 strlen(server_name),
2708 strlen(server_info));
2710 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2711 SILC_STATUS_OK, ident, 2,
2712 2, idp->data, idp->len,
2714 strlen(server_name));
2715 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2716 packet->data, packet->len, FALSE);
2718 silc_buffer_free(packet);
2719 silc_buffer_free(idp);
2722 silc_server_command_free(cmd);
2725 /* Server side of command PING. This just replies to the ping. */
2727 SILC_SERVER_CMD_FUNC(ping)
2729 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2730 SilcServer server = cmd->server;
2735 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 1, 2);
2738 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2740 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2741 SILC_STATUS_ERR_NO_SERVER_ID);
2744 id = silc_id_str2id(tmp, len, SILC_ID_SERVER);
2748 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2749 /* Send our reply */
2750 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2753 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2754 SILC_STATUS_ERR_NO_SUCH_SERVER);
2761 silc_server_command_free(cmd);
2764 /* Internal routine to join channel. The channel sent to this function
2765 has been either created or resolved from ID lists. This joins the sent
2766 client to the channel. */
2768 static void silc_server_command_join_channel(SilcServer server,
2769 SilcServerCommandContext cmd,
2770 SilcChannelEntry channel,
2771 SilcClientID *client_id,
2775 SilcSocketConnection sock = cmd->sock;
2777 uint32 tmp_len, user_count;
2778 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
2779 SilcClientEntry client;
2780 SilcChannelClientEntry chl;
2781 SilcBuffer reply, chidp, clidp, keyp = NULL, user_list, mode_list;
2782 uint16 ident = silc_command_get_ident(cmd->payload);
2783 char check[512], check2[512];
2785 SILC_LOG_DEBUG(("Start"));
2790 /* Get the client entry */
2791 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2792 client = (SilcClientEntry)sock->user_data;
2794 client = silc_server_get_client_resolve(server, client_id);
2799 /* The client info is being resolved. Reprocess this packet after
2800 receiving the reply to the query. */
2801 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2802 server->cmd_ident, NULL,
2803 silc_server_command_join,
2804 silc_server_command_dup(cmd));
2805 cmd->pending = TRUE;
2809 cmd->pending = FALSE;
2813 * Check channel modes
2816 memset(check, 0, sizeof(check));
2817 memset(check2, 0, sizeof(check2));
2818 strncat(check, client->nickname, strlen(client->nickname));
2819 strncat(check, "!", 1);
2820 strncat(check, client->username, strlen(client->username));
2821 if (!strchr(client->username, '@')) {
2822 strncat(check, "@", 1);
2823 strncat(check, cmd->sock->hostname, strlen(cmd->sock->hostname));
2826 strncat(check2, client->nickname, strlen(client->nickname));
2827 if (!strchr(client->nickname, '@')) {
2828 strncat(check2, "@", 1);
2829 strncat(check2, server->server_name, strlen(server->server_name));
2831 strncat(check2, "!", 1);
2832 strncat(check2, client->username, strlen(client->username));
2833 if (!strchr(client->username, '@')) {
2834 strncat(check2, "@", 1);
2835 strncat(check2, cmd->sock->hostname, strlen(cmd->sock->hostname));
2838 /* Check invite list if channel is invite-only channel */
2839 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2840 if (!channel->invite_list ||
2841 (!silc_string_match(channel->invite_list, check) &&
2842 !silc_string_match(channel->invite_list, check2))) {
2843 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2844 SILC_STATUS_ERR_NOT_INVITED);
2849 /* Check ban list if it exists. If the client's nickname, server,
2850 username and/or hostname is in the ban list the access to the
2851 channel is denied. */
2852 if (channel->ban_list) {
2853 if (silc_string_match(channel->ban_list, check) ||
2854 silc_string_match(channel->ban_list, check2)) {
2855 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2856 SILC_STATUS_ERR_BANNED_FROM_CHANNEL);
2861 /* Get passphrase */
2862 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
2864 passphrase = silc_calloc(tmp_len, sizeof(*passphrase));
2865 memcpy(passphrase, tmp, tmp_len);
2868 /* Check the channel passphrase if set. */
2869 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2870 if (!passphrase || memcmp(channel->passphrase, passphrase,
2871 strlen(channel->passphrase))) {
2872 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2873 SILC_STATUS_ERR_BAD_PASSWORD);
2878 /* Check user count limit if set. */
2879 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
2880 if (silc_hash_table_count(channel->user_list) + 1 >
2881 channel->user_limit) {
2882 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2883 SILC_STATUS_ERR_CHANNEL_IS_FULL);
2889 * Client is allowed to join to the channel. Make it happen.
2892 /* Check whether the client already is on the channel */
2893 if (silc_server_client_on_channel(client, channel)) {
2894 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2895 SILC_STATUS_ERR_USER_ON_CHANNEL);
2899 /* Generate new channel key as protocol dictates */
2900 if ((!created && silc_hash_table_count(channel->user_list) > 0) ||
2901 !channel->channel_key)
2902 silc_server_create_channel_key(server, channel, 0);
2904 /* Send the channel key. This is broadcasted to the channel but is not
2905 sent to the client who is joining to the channel. */
2906 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
2907 silc_server_send_channel_key(server, NULL, channel,
2908 server->server_type == SILC_ROUTER ?
2909 FALSE : !server->standalone);
2911 /* Join the client to the channel by adding it to channel's user list.
2912 Add also the channel to client entry's channels list for fast cross-
2914 chl = silc_calloc(1, sizeof(*chl));
2916 chl->client = client;
2917 chl->channel = channel;
2918 silc_hash_table_add(channel->user_list, client, chl);
2919 silc_hash_table_add(client->channels, channel, chl);
2921 /* Get users on the channel */
2922 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2925 /* Encode Client ID Payload of the original client who wants to join */
2926 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2928 /* Encode command reply packet */
2929 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2930 SILC_PUT32_MSB(channel->mode, mode);
2931 SILC_PUT32_MSB(created, tmp2);
2932 SILC_PUT32_MSB(user_count, tmp3);
2934 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2935 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2936 keyp = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, tmp,
2937 strlen(channel->channel_key->
2939 channel->channel_key->cipher->name,
2940 channel->key_len / 8, channel->key);
2945 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2946 SILC_STATUS_OK, ident, 13,
2947 2, channel->channel_name,
2948 strlen(channel->channel_name),
2949 3, chidp->data, chidp->len,
2950 4, clidp->data, clidp->len,
2953 7, keyp ? keyp->data : NULL,
2954 keyp ? keyp->len : 0,
2955 8, channel->ban_list,
2957 strlen(channel->ban_list) : 0,
2958 9, channel->invite_list,
2959 channel->invite_list ?
2960 strlen(channel->invite_list) : 0,
2963 strlen(channel->topic) : 0,
2964 11, channel->hmac->hmac->name,
2965 strlen(channel->hmac->hmac->name),
2967 13, user_list->data, user_list->len,
2968 14, mode_list->data,
2971 /* Send command reply */
2972 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2973 reply->data, reply->len, FALSE);
2975 if (!cmd->pending) {
2976 /* Send JOIN notify to locally connected clients on the channel */
2977 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2978 SILC_NOTIFY_TYPE_JOIN, 2,
2979 clidp->data, clidp->len,
2980 chidp->data, chidp->len);
2982 /* Send JOIN notify packet to our primary router */
2983 if (!server->standalone)
2984 silc_server_send_notify_join(server, server->router->connection,
2985 server->server_type == SILC_ROUTER ?
2986 TRUE : FALSE, channel, client->id);
2989 silc_buffer_free(reply);
2990 silc_buffer_free(clidp);
2991 silc_buffer_free(chidp);
2992 silc_buffer_free(keyp);
2993 silc_buffer_free(user_list);
2994 silc_buffer_free(mode_list);
2998 silc_free(passphrase);
3001 /* Server side of command JOIN. Joins client into requested channel. If
3002 the channel does not exist it will be created. */
3004 SILC_SERVER_CMD_FUNC(join)
3006 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3007 SilcServer server = cmd->server;
3009 char *tmp, *channel_name = NULL, *cipher, *hmac;
3010 SilcChannelEntry channel;
3012 int created = FALSE;
3013 SilcClientID *client_id;
3015 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 1, 4);
3017 /* Get channel name */
3018 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3020 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3021 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3026 if (strlen(channel_name) > 256)
3027 channel_name[255] = '\0';
3029 if (silc_server_command_bad_chars(channel_name) == TRUE) {
3030 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3031 SILC_STATUS_ERR_BAD_CHANNEL);
3035 /* Get Client ID of the client who is joining to the channel */
3036 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3038 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3039 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3042 client_id = silc_id_payload_parse_id(tmp, tmp_len);
3044 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3045 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3049 /* Get cipher and hmac name */
3050 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
3051 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
3053 /* See if the channel exists */
3054 channel = silc_idlist_find_channel_by_name(server->local_list,
3055 channel_name, NULL);
3057 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3058 /* If this is coming from client the Client ID in the command packet must
3059 be same as the client's ID. */
3060 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3061 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
3062 if (!SILC_ID_CLIENT_COMPARE(entry->id, client_id)) {
3063 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3064 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3069 if (!channel || !channel->id) {
3070 /* Channel not found */
3072 /* If we are standalone server we don't have a router, we just create
3073 the channel by ourselves. */
3074 if (server->standalone) {
3075 channel = silc_server_create_new_channel(server, server->id, cipher,
3076 hmac, channel_name, TRUE);
3078 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3079 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3083 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3088 /* The channel does not exist on our server. If we are normal server
3089 we will send JOIN command to our router which will handle the
3090 joining procedure (either creates the channel if it doesn't exist
3091 or joins the client to it). */
3092 if (server->server_type == SILC_SERVER) {
3096 /* If this is pending command callback then we've resolved
3097 it and it didn't work, return since we've notified the
3098 client already in the command reply callback. */
3102 old_ident = silc_command_get_ident(cmd->payload);
3103 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3104 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3106 /* Send JOIN command to our router */
3107 silc_server_packet_send(server, (SilcSocketConnection)
3108 server->router->connection,
3109 SILC_PACKET_COMMAND, cmd->packet->flags,
3110 tmpbuf->data, tmpbuf->len, TRUE);
3112 /* Reprocess this packet after received reply from router */
3113 silc_server_command_pending(server, SILC_COMMAND_JOIN,
3114 silc_command_get_ident(cmd->payload),
3115 silc_server_command_destructor,
3116 silc_server_command_join,
3117 silc_server_command_dup(cmd));
3118 cmd->pending = TRUE;
3122 /* We are router and the channel does not seem exist so we will check
3123 our global list as well for the channel. */
3124 channel = silc_idlist_find_channel_by_name(server->global_list,
3125 channel_name, NULL);
3127 /* Channel really does not exist, create it */
3128 channel = silc_server_create_new_channel(server, server->id, cipher,
3129 hmac, channel_name, TRUE);
3131 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3132 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3136 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3143 /* Channel not found */
3145 /* If the command came from router and/or we are normal server then
3146 something went wrong with the joining as the channel was not found.
3147 We can't do anything else but ignore this. */
3148 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
3149 server->server_type == SILC_SERVER)
3152 /* We are router and the channel does not seem exist so we will check
3153 our global list as well for the channel. */
3154 channel = silc_idlist_find_channel_by_name(server->global_list,
3155 channel_name, NULL);
3157 /* Channel really does not exist, create it */
3158 channel = silc_server_create_new_channel(server, server->id, cipher,
3159 hmac, channel_name, TRUE);
3161 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3162 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3166 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3172 /* If the channel does not have global users and is also empty it means the
3173 channel was created globally (by our router) and the client will be the
3174 channel founder and operator. */
3175 if (!channel->global_users && !silc_hash_table_count(channel->user_list)) {
3176 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3177 created = TRUE; /* Created globally by our router */
3180 /* Join to the channel */
3181 silc_server_command_join_channel(server, cmd, channel, client_id,
3184 silc_free(client_id);
3187 silc_server_command_free(cmd);
3190 /* Server side of command MOTD. Sends server's current "message of the
3191 day" to the client. */
3193 SILC_SERVER_CMD_FUNC(motd)
3195 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3196 SilcServer server = cmd->server;
3197 SilcBuffer packet, idp;
3198 char *motd, *dest_server;
3200 uint16 ident = silc_command_get_ident(cmd->payload);
3202 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
3204 /* Get server name */
3205 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
3207 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
3208 SILC_STATUS_ERR_NO_SUCH_SERVER);
3212 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
3215 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3217 if (server->config && server->config->motd &&
3218 server->config->motd->motd_file) {
3220 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
3225 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3226 SILC_STATUS_OK, ident, 2,
3232 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3233 SILC_STATUS_OK, ident, 1,
3237 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3238 packet->data, packet->len, FALSE);
3239 silc_buffer_free(packet);
3240 silc_buffer_free(idp);
3242 SilcServerEntry entry;
3244 /* Check whether we have this server cached */
3245 entry = silc_idlist_find_server_by_name(server->global_list,
3248 entry = silc_idlist_find_server_by_name(server->local_list,
3252 if (server->server_type == SILC_ROUTER && !cmd->pending &&
3253 entry && !entry->motd) {
3254 /* Send to the server */
3258 old_ident = silc_command_get_ident(cmd->payload);
3259 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3260 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3262 silc_server_packet_send(server, entry->connection,
3263 SILC_PACKET_COMMAND, cmd->packet->flags,
3264 tmpbuf->data, tmpbuf->len, TRUE);
3266 /* Reprocess this packet after received reply from router */
3267 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3268 silc_command_get_ident(cmd->payload),
3269 silc_server_command_destructor,
3270 silc_server_command_motd,
3271 silc_server_command_dup(cmd));
3272 cmd->pending = TRUE;
3273 silc_command_set_ident(cmd->payload, old_ident);
3274 silc_buffer_free(tmpbuf);
3278 if (!entry && !cmd->pending && !server->standalone) {
3279 /* Send to the primary router */
3283 old_ident = silc_command_get_ident(cmd->payload);
3284 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3285 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3287 silc_server_packet_send(server, server->router->connection,
3288 SILC_PACKET_COMMAND, cmd->packet->flags,
3289 tmpbuf->data, tmpbuf->len, TRUE);
3291 /* Reprocess this packet after received reply from router */
3292 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3293 silc_command_get_ident(cmd->payload),
3294 silc_server_command_destructor,
3295 silc_server_command_motd,
3296 silc_server_command_dup(cmd));
3297 cmd->pending = TRUE;
3298 silc_command_set_ident(cmd->payload, old_ident);
3299 silc_buffer_free(tmpbuf);
3304 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3305 SILC_STATUS_ERR_NO_SUCH_SERVER);
3309 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3312 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3313 SILC_STATUS_OK, ident, 2,
3316 strlen(entry->motd));
3318 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3319 SILC_STATUS_OK, ident, 1,
3322 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3323 packet->data, packet->len, FALSE);
3324 silc_buffer_free(packet);
3325 silc_buffer_free(idp);
3329 silc_server_command_free(cmd);
3332 /* Server side of command UMODE. Client can use this command to set/unset
3333 user mode. Client actually cannot set itself to be as server/router
3334 operator so this can be used only to unset the modes. */
3336 SILC_SERVER_CMD_FUNC(umode)
3338 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3339 SilcServer server = cmd->server;
3340 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3342 unsigned char *tmp_mask;
3344 uint16 ident = silc_command_get_ident(cmd->payload);
3346 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3349 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 2, 2);
3351 /* Get the client's mode mask */
3352 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3354 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3355 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3358 SILC_GET32_MSB(mask, tmp_mask);
3364 if (mask & SILC_UMODE_SERVER_OPERATOR) {
3365 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR)) {
3366 /* Cannot operator mode */
3367 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3368 SILC_STATUS_ERR_PERM_DENIED);
3372 if (client->mode & SILC_UMODE_SERVER_OPERATOR)
3373 /* Remove the server operator rights */
3374 client->mode &= ~SILC_UMODE_SERVER_OPERATOR;
3377 if (mask & SILC_UMODE_ROUTER_OPERATOR) {
3378 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
3379 /* Cannot operator mode */
3380 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3381 SILC_STATUS_ERR_PERM_DENIED);
3385 if (client->mode & SILC_UMODE_ROUTER_OPERATOR)
3386 /* Remove the router operator rights */
3387 client->mode &= ~SILC_UMODE_ROUTER_OPERATOR;
3390 if (mask & SILC_UMODE_GONE) {
3391 client->mode |= SILC_UMODE_GONE;
3393 if (client->mode & SILC_UMODE_GONE)
3394 /* Remove the gone status */
3395 client->mode &= ~SILC_UMODE_GONE;
3398 /* Send UMODE change to primary router */
3399 if (!server->standalone)
3400 silc_server_send_notify_umode(server, server->router->connection, TRUE,
3401 client->id, client->mode);
3403 /* Send command reply to sender */
3404 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
3405 SILC_STATUS_OK, ident, 1,
3407 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3408 packet->data, packet->len, FALSE);
3409 silc_buffer_free(packet);
3412 silc_server_command_free(cmd);
3415 /* Checks that client has rights to add or remove channel modes. If any
3416 of the checks fails FALSE is returned. */
3418 int silc_server_check_cmode_rights(SilcChannelEntry channel,
3419 SilcChannelClientEntry client,
3422 int is_op = client->mode & SILC_CHANNEL_UMODE_CHANOP;
3423 int is_fo = client->mode & SILC_CHANNEL_UMODE_CHANFO;
3425 /* Check whether has rights to change anything */
3426 if (!is_op && !is_fo)
3429 /* Check whether has rights to change everything */
3433 /* We know that client is channel operator, check that they are not
3434 changing anything that requires channel founder rights. Rest of the
3435 modes are available automatically for channel operator. */
3437 if (mode & SILC_CHANNEL_MODE_PRIVKEY) {
3438 if (is_op && !is_fo)
3441 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3442 if (is_op && !is_fo)
3447 if (mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3448 if (is_op && !is_fo)
3451 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3452 if (is_op && !is_fo)
3457 if (mode & SILC_CHANNEL_MODE_CIPHER) {
3458 if (is_op && !is_fo)
3461 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3462 if (is_op && !is_fo)
3467 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3468 if (is_op && !is_fo)
3471 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3472 if (is_op && !is_fo)
3480 /* Server side command of CMODE. Changes channel mode */
3482 SILC_SERVER_CMD_FUNC(cmode)
3484 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3485 SilcServer server = cmd->server;
3486 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3487 SilcIDListData idata = (SilcIDListData)client;
3488 SilcChannelID *channel_id;
3489 SilcChannelEntry channel;
3490 SilcChannelClientEntry chl;
3491 SilcBuffer packet, cidp;
3492 unsigned char *tmp, *tmp_id, *tmp_mask;
3493 char *cipher = NULL, *hmac = NULL;
3494 uint32 mode_mask, tmp_len, tmp_len2;
3495 uint16 ident = silc_command_get_ident(cmd->payload);
3497 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 2, 7);
3499 /* Get Channel ID */
3500 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
3502 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3503 SILC_STATUS_ERR_NO_CHANNEL_ID);
3506 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2);
3508 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3509 SILC_STATUS_ERR_NO_CHANNEL_ID);
3513 /* Get the channel mode mask */
3514 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3516 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3517 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3520 SILC_GET32_MSB(mode_mask, tmp_mask);
3522 /* Get channel entry */
3523 channel = silc_idlist_find_channel_by_id(server->local_list,
3526 channel = silc_idlist_find_channel_by_id(server->global_list,
3529 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3530 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3535 /* Check whether this client is on the channel */
3536 if (!silc_server_client_on_channel(client, channel)) {
3537 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3538 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3542 /* Get entry to the channel user list */
3543 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
3545 /* Check that client has rights to change any requested channel modes */
3546 if (!silc_server_check_cmode_rights(channel, chl, mode_mask)) {
3547 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3548 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3553 * Check the modes. Modes that requires nothing special operation are
3557 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
3558 /* Channel uses private keys to protect traffic. Client(s) has set the
3559 key locally they want to use, server does not know that key. */
3560 /* Nothing interesting to do here */
3562 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3563 /* The mode is removed and we need to generate and distribute
3564 new channel key. Clients are not using private channel keys
3565 anymore after this. */
3567 /* Re-generate channel key */
3568 silc_server_create_channel_key(server, channel, 0);
3570 /* Send the channel key. This sends it to our local clients and if
3571 we are normal server to our router as well. */
3572 silc_server_send_channel_key(server, NULL, channel,
3573 server->server_type == SILC_ROUTER ?
3574 FALSE : !server->standalone);
3576 cipher = channel->channel_key->cipher->name;
3577 hmac = channel->hmac->hmac->name;
3581 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
3582 /* User limit is set on channel */
3585 /* Get user limit */
3586 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
3588 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
3589 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3590 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3594 SILC_GET32_MSB(user_limit, tmp);
3595 channel->user_limit = user_limit;
3598 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
3599 /* User limit mode is unset. Remove user limit */
3600 channel->user_limit = 0;
3603 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
3604 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
3605 /* Passphrase has been set to channel */
3607 /* Get the passphrase */
3608 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
3610 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3611 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3615 /* Save the passphrase */
3616 channel->passphrase = strdup(tmp);
3619 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3620 /* Passphrase mode is unset. remove the passphrase */
3621 if (channel->passphrase) {
3622 silc_free(channel->passphrase);
3623 channel->passphrase = NULL;
3628 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
3629 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
3630 /* Cipher to use protect the traffic */
3633 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
3635 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3636 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3640 /* Delete old cipher and allocate the new one */
3641 silc_cipher_free(channel->channel_key);
3642 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3643 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3644 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3648 /* Re-generate channel key */
3649 silc_server_create_channel_key(server, channel, 0);
3651 /* Send the channel key. This sends it to our local clients and if
3652 we are normal server to our router as well. */
3653 silc_server_send_channel_key(server, NULL, channel,
3654 server->server_type == SILC_ROUTER ?
3655 FALSE : !server->standalone);
3658 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3659 /* Cipher mode is unset. Remove the cipher and revert back to
3661 cipher = channel->cipher;
3663 /* Delete old cipher and allocate default one */
3664 silc_cipher_free(channel->channel_key);
3665 if (!silc_cipher_alloc(cipher ? cipher : "aes-256-cbc",
3666 &channel->channel_key)) {
3667 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3668 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3672 /* Re-generate channel key */
3673 silc_server_create_channel_key(server, channel, 0);
3675 /* Send the channel key. This sends it to our local clients and if
3676 we are normal server to our router as well. */
3677 silc_server_send_channel_key(server, NULL, channel,
3678 server->server_type == SILC_ROUTER ?
3679 FALSE : !server->standalone);
3683 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
3684 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
3685 /* HMAC to use protect the traffic */
3686 unsigned char hash[32];
3689 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
3691 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3692 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3696 /* Delete old hmac and allocate the new one */
3697 silc_hmac_free(channel->hmac);
3698 if (!silc_hmac_alloc(hmac, NULL, &channel->hmac)) {
3699 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3700 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3704 /* Set the HMAC key out of current channel key. The client must do
3706 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3708 silc_hmac_set_key(channel->hmac, hash,
3709 silc_hash_len(channel->hmac->hash));
3710 memset(hash, 0, sizeof(hash));
3713 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
3714 /* Hmac mode is unset. Remove the hmac and revert back to
3716 unsigned char hash[32];
3717 hmac = channel->hmac_name;
3719 /* Delete old hmac and allocate default one */
3720 silc_hmac_free(channel->hmac);
3721 if (!silc_hmac_alloc(hmac ? hmac : "hmac-sha1-96", NULL,
3723 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3724 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3728 /* Set the HMAC key out of current channel key. The client must do
3730 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3732 silc_hmac_set_key(channel->hmac, hash,
3733 silc_hash_len(channel->hmac->hash));
3734 memset(hash, 0, sizeof(hash));
3738 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3739 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3740 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
3741 /* Set the founder authentication */
3742 SilcAuthPayload auth;
3744 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
3746 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3747 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3751 auth = silc_auth_payload_parse(tmp, tmp_len);
3753 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3754 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3758 /* Save the public key */
3759 tmp = silc_pkcs_public_key_encode(idata->public_key, &tmp_len);
3760 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
3763 channel->founder_method = silc_auth_get_method(auth);
3765 if (channel->founder_method == SILC_AUTH_PASSWORD) {
3766 tmp = silc_auth_get_data(auth, &tmp_len);
3767 channel->founder_passwd =
3768 silc_calloc(tmp_len + 1, sizeof(*channel->founder_passwd));
3769 memcpy(channel->founder_passwd, tmp, tmp_len);
3770 channel->founder_passwd_len = tmp_len;
3772 /* Verify the payload before setting the mode */
3773 if (!silc_auth_verify(auth, channel->founder_method,
3774 channel->founder_key, 0, idata->hash,
3775 client->id, SILC_ID_CLIENT)) {
3776 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3777 SILC_STATUS_ERR_AUTH_FAILED);
3782 silc_auth_payload_free(auth);
3786 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3787 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3788 if (channel->founder_key)
3789 silc_pkcs_public_key_free(channel->founder_key);
3790 if (channel->founder_passwd) {
3791 silc_free(channel->founder_passwd);
3792 channel->founder_passwd = NULL;
3798 /* Finally, set the mode */
3799 channel->mode = mode_mask;
3801 /* Send CMODE_CHANGE notify */
3802 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3803 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3804 SILC_NOTIFY_TYPE_CMODE_CHANGE, 4,
3805 cidp->data, cidp->len,
3807 cipher, cipher ? strlen(cipher) : 0,
3808 hmac, hmac ? strlen(hmac) : 0);
3810 /* Set CMODE notify type to network */
3811 if (!server->standalone)
3812 silc_server_send_notify_cmode(server, server->router->connection,
3813 server->server_type == SILC_ROUTER ?
3814 TRUE : FALSE, channel,
3815 mode_mask, client->id, SILC_ID_CLIENT,
3818 /* Send command reply to sender */
3819 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3820 SILC_STATUS_OK, ident, 2,
3821 2, tmp_id, tmp_len2,
3823 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3824 packet->data, packet->len, FALSE);
3826 silc_buffer_free(packet);
3827 silc_free(channel_id);
3831 silc_server_command_free(cmd);
3834 /* Server side of CUMODE command. Changes client's mode on a channel. */
3836 SILC_SERVER_CMD_FUNC(cumode)
3838 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3839 SilcServer server = cmd->server;
3840 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3841 SilcIDListData idata = (SilcIDListData)client;
3842 SilcChannelID *channel_id;
3843 SilcClientID *client_id;
3844 SilcChannelEntry channel;
3845 SilcClientEntry target_client;
3846 SilcChannelClientEntry chl;
3847 SilcBuffer packet, idp;
3848 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3849 uint32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3851 uint16 ident = silc_command_get_ident(cmd->payload);
3853 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
3855 /* Get Channel ID */
3856 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3858 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3859 SILC_STATUS_ERR_NO_CHANNEL_ID);
3862 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len);
3864 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3865 SILC_STATUS_ERR_NO_CHANNEL_ID);
3869 /* Get channel entry */
3870 channel = silc_idlist_find_channel_by_id(server->local_list,
3873 channel = silc_idlist_find_channel_by_id(server->global_list,
3876 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3877 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3882 /* Check whether sender is on the channel */
3883 if (!silc_server_client_on_channel(client, channel)) {
3884 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3885 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3889 /* Check that client has rights to change other's rights */
3890 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
3891 sender_mask = chl->mode;
3893 /* Get the target client's channel mode mask */
3894 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3896 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3897 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3900 SILC_GET32_MSB(target_mask, tmp_mask);
3902 /* Get target Client ID */
3903 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3905 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3906 SILC_STATUS_ERR_NO_CLIENT_ID);
3909 client_id = silc_id_payload_parse_id(tmp_id, tmp_len);
3911 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3912 SILC_STATUS_ERR_NO_CLIENT_ID);
3916 /* Get target client's entry */
3917 target_client = silc_idlist_find_client_by_id(server->local_list,
3919 if (!target_client) {
3920 target_client = silc_idlist_find_client_by_id(server->global_list,
3924 if (target_client != client &&
3925 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3926 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3927 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3928 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3932 /* Check whether target client is on the channel */
3933 if (target_client != client) {
3934 if (!silc_server_client_on_channel(target_client, channel)) {
3935 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3936 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
3940 /* Get entry to the channel user list */
3941 silc_hash_table_find(channel->user_list, target_client, NULL,
3949 /* If the target client is founder, no one else can change their mode
3951 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && chl->client != target_client) {
3952 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3953 SILC_STATUS_ERR_NOT_YOU);
3957 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3958 /* The client tries to claim the founder rights. */
3959 unsigned char *tmp_auth;
3960 uint32 tmp_auth_len, auth_len;
3963 if (target_client != client) {
3964 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3965 SILC_STATUS_ERR_NOT_YOU);
3969 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3970 !channel->founder_key) {
3971 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3972 SILC_STATUS_ERR_NOT_YOU);
3976 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3978 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3979 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3983 auth = (channel->founder_method == SILC_AUTH_PASSWORD ?
3984 (void *)channel->founder_passwd : (void *)channel->founder_key);
3985 auth_len = (channel->founder_method == SILC_AUTH_PASSWORD ?
3986 channel->founder_passwd_len : 0);
3988 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len,
3989 channel->founder_method, auth, auth_len,
3990 idata->hash, client->id, SILC_ID_CLIENT)) {
3991 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3992 SILC_STATUS_ERR_AUTH_FAILED);
3996 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
3999 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4000 if (target_client == client) {
4001 /* Remove channel founder rights from itself */
4002 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
4005 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4006 SILC_STATUS_ERR_NOT_YOU);
4012 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
4013 /* Promote to operator */
4014 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4015 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4016 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4017 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4018 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4022 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
4026 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
4027 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4028 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4029 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4030 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4034 /* Demote to normal user */
4035 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
4040 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
4041 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4043 /* Send notify to channel, notify only if mode was actually changed. */
4045 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4046 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
4047 idp->data, idp->len,
4051 /* Set CUMODE notify type to network */
4052 if (!server->standalone)
4053 silc_server_send_notify_cumode(server, server->router->connection,
4054 server->server_type == SILC_ROUTER ?
4055 TRUE : FALSE, channel,
4056 target_mask, client->id,
4061 /* Send command reply to sender */
4062 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
4063 SILC_STATUS_OK, ident, 3,
4065 3, tmp_ch_id, tmp_ch_len,
4066 4, tmp_id, tmp_len);
4067 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4068 packet->data, packet->len, FALSE);
4070 silc_buffer_free(packet);
4071 silc_free(channel_id);
4072 silc_free(client_id);
4073 silc_buffer_free(idp);
4076 silc_server_command_free(cmd);
4079 /* Server side of KICK command. Kicks client out of channel. */
4081 SILC_SERVER_CMD_FUNC(kick)
4083 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4084 SilcServer server = cmd->server;
4085 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4086 SilcClientEntry target_client;
4087 SilcChannelID *channel_id;
4088 SilcClientID *client_id;
4089 SilcChannelEntry channel;
4090 SilcChannelClientEntry chl;
4093 unsigned char *tmp, *comment;
4095 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
4097 /* Get Channel ID */
4098 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4100 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4101 SILC_STATUS_ERR_NO_CHANNEL_ID);
4104 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
4106 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4107 SILC_STATUS_ERR_NO_CHANNEL_ID);
4111 /* Get channel entry */
4112 channel = silc_idlist_find_channel_by_id(server->local_list,
4115 channel = silc_idlist_find_channel_by_id(server->local_list,
4118 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4119 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4124 /* Check whether sender is on the channel */
4125 if (!silc_server_client_on_channel(client, channel)) {
4126 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4127 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4131 /* Check that the kicker is channel operator or channel founder */
4132 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4133 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
4134 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4135 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4139 /* Get target Client ID */
4140 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4142 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4143 SILC_STATUS_ERR_NO_CLIENT_ID);
4146 client_id = silc_id_payload_parse_id(tmp, tmp_len);
4148 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4149 SILC_STATUS_ERR_NO_CLIENT_ID);
4153 /* Get target client's entry */
4154 target_client = silc_idlist_find_client_by_id(server->local_list,
4156 if (!target_client) {
4157 target_client = silc_idlist_find_client_by_id(server->global_list,
4161 /* Check that the target client is not channel founder. Channel founder
4162 cannot be kicked from the channel. */
4163 silc_hash_table_find(channel->user_list, target_client, NULL, (void *)&chl);
4164 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4165 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4166 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV);
4170 /* Check whether target client is on the channel */
4171 if (!silc_server_client_on_channel(target_client, channel)) {
4172 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4173 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
4179 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4183 /* Send command reply to sender */
4184 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4187 /* Send KICKED notify to local clients on the channel */
4188 idp = silc_id_payload_encode(target_client->id, SILC_ID_CLIENT);
4189 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4190 SILC_NOTIFY_TYPE_KICKED,
4192 idp->data, idp->len,
4193 comment, comment ? strlen(comment) : 0);
4194 silc_buffer_free(idp);
4196 /* Remove the client from the channel. If the channel does not exist
4197 after removing the client then the client kicked itself off the channel
4198 and we don't have to send anything after that. */
4199 if (!silc_server_remove_from_one_channel(server, NULL, channel,
4200 target_client, FALSE))
4203 /* Send KICKED notify to primary route */
4204 if (!server->standalone)
4205 silc_server_send_notify_kicked(server, server->router->connection,
4206 server->server_type == SILC_ROUTER ?
4207 TRUE : FALSE, channel,
4208 target_client->id, comment);
4210 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4211 /* Re-generate channel key */
4212 silc_server_create_channel_key(server, channel, 0);
4214 /* Send the channel key to the channel. The key of course is not sent
4215 to the client who was kicked off the channel. */
4216 silc_server_send_channel_key(server, target_client->connection, channel,
4217 server->server_type == SILC_ROUTER ?
4218 FALSE : !server->standalone);
4222 silc_server_command_free(cmd);
4225 /* Server side of OPER command. Client uses this comand to obtain server
4226 operator privileges to this server/router. */
4228 SILC_SERVER_CMD_FUNC(oper)
4230 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4231 SilcServer server = cmd->server;
4232 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4233 unsigned char *username, *auth;
4235 SilcServerConfigSectionAdminConnection *admin;
4236 SilcIDListData idata = (SilcIDListData)client;
4238 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
4240 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4243 /* Get the username */
4244 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4246 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4247 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4251 /* Get the admin configuration */
4252 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4253 username, client->nickname);
4255 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4256 username, client->nickname);
4258 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4259 SILC_STATUS_ERR_AUTH_FAILED);
4264 /* Get the authentication payload */
4265 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4267 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4268 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4272 /* Verify the authentication data */
4273 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4274 admin->auth_data, admin->auth_data_len,
4275 idata->hash, client->id, SILC_ID_CLIENT)) {
4276 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4277 SILC_STATUS_ERR_AUTH_FAILED);
4281 /* Client is now server operator */
4282 client->mode |= SILC_UMODE_SERVER_OPERATOR;
4284 /* Send UMODE change to primary router */
4285 if (!server->standalone)
4286 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4287 client->id, client->mode);
4289 /* Send reply to the sender */
4290 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4294 silc_server_command_free(cmd);
4297 /* Server side of SILCOPER command. Client uses this comand to obtain router
4298 operator privileges to this router. */
4300 SILC_SERVER_CMD_FUNC(silcoper)
4302 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4303 SilcServer server = cmd->server;
4304 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4305 unsigned char *username, *auth;
4307 SilcServerConfigSectionAdminConnection *admin;
4308 SilcIDListData idata = (SilcIDListData)client;
4310 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
4312 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4315 if (server->server_type == SILC_SERVER) {
4316 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4317 SILC_STATUS_ERR_AUTH_FAILED);
4321 /* Get the username */
4322 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4324 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4325 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4329 /* Get the admin configuration */
4330 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4331 username, client->nickname);
4333 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4334 username, client->nickname);
4336 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4337 SILC_STATUS_ERR_AUTH_FAILED);
4342 /* Get the authentication payload */
4343 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4345 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4346 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4350 /* Verify the authentication data */
4351 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4352 admin->auth_data, admin->auth_data_len,
4353 idata->hash, client->id, SILC_ID_CLIENT)) {
4354 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4355 SILC_STATUS_ERR_AUTH_FAILED);
4359 /* Client is now router operator */
4360 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4362 /* Send UMODE change to primary router */
4363 if (!server->standalone)
4364 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4365 client->id, client->mode);
4367 /* Send reply to the sender */
4368 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4372 silc_server_command_free(cmd);
4375 /* Server side command of CONNECT. Connects us to the specified remote
4376 server or router. */
4378 SILC_SERVER_CMD_FUNC(connect)
4380 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4381 SilcServer server = cmd->server;
4382 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4383 unsigned char *tmp, *host;
4385 uint32 port = SILC_PORT;
4387 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CONNECT, cmd, 1, 2);
4389 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4392 /* Check whether client has the permissions. */
4393 if (client->mode == SILC_UMODE_NONE) {
4394 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4395 SILC_STATUS_ERR_NO_SERVER_PRIV);
4399 if (server->server_type == SILC_ROUTER &&
4400 client->mode & SILC_UMODE_SERVER_OPERATOR) {
4401 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4402 SILC_STATUS_ERR_NO_ROUTER_PRIV);
4406 /* Get the remote server */
4407 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4409 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4410 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4415 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4417 SILC_GET32_MSB(port, tmp);
4419 /* Create the connection. It is done with timeout and is async. */
4420 silc_server_create_connection(server, host, port);
4422 /* Send reply to the sender */
4423 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4427 silc_server_command_free(cmd);
4430 /* Server side of command BAN. This is used to manage the ban list of the
4431 channel. To add clients and remove clients from the ban list. */
4433 SILC_SERVER_CMD_FUNC(ban)
4435 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4436 SilcServer server = cmd->server;
4437 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4439 SilcChannelEntry channel;
4440 SilcChannelClientEntry chl;
4441 SilcChannelID *channel_id = NULL;
4442 unsigned char *id, *add, *del;
4443 uint32 id_len, tmp_len;
4444 uint16 ident = silc_command_get_ident(cmd->payload);
4446 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4449 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
4451 /* Get Channel ID */
4452 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4454 channel_id = silc_id_payload_parse_id(id, id_len);
4456 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4457 SILC_STATUS_ERR_NO_CHANNEL_ID);
4462 /* Get channel entry. The server must know about the channel since the
4463 client is expected to be on the channel. */
4464 channel = silc_idlist_find_channel_by_id(server->local_list,
4467 channel = silc_idlist_find_channel_by_id(server->global_list,
4470 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4471 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4476 /* Check whether this client is on the channel */
4477 if (!silc_server_client_on_channel(client, channel)) {
4478 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4479 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4483 /* Get entry to the channel user list */
4484 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4486 /* The client must be at least channel operator. */
4487 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4488 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4489 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4493 /* Get the new ban and add it to the ban list */
4494 add = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4496 if (!channel->ban_list)
4497 channel->ban_list = silc_calloc(tmp_len + 2, sizeof(*channel->ban_list));
4499 channel->ban_list = silc_realloc(channel->ban_list,
4500 sizeof(*channel->ban_list) *
4502 strlen(channel->ban_list) + 2));
4503 if (add[tmp_len - 1] == ',')
4504 add[tmp_len - 1] = '\0';
4506 strncat(channel->ban_list, add, tmp_len);
4507 strncat(channel->ban_list, ",", 1);
4510 /* Get the ban to be removed and remove it from the list */
4511 del = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4512 if (del && channel->ban_list) {
4513 char *start, *end, *n;
4515 if (!strncmp(channel->ban_list, del, strlen(channel->ban_list) - 1)) {
4516 silc_free(channel->ban_list);
4517 channel->ban_list = NULL;
4519 start = strstr(channel->ban_list, del);
4520 if (start && strlen(start) >= tmp_len) {
4521 end = start + tmp_len;
4522 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
4523 strncat(n, channel->ban_list, start - channel->ban_list);
4524 strncat(n, end + 1, ((channel->ban_list + strlen(channel->ban_list)) -
4526 silc_free(channel->ban_list);
4527 channel->ban_list = n;
4532 /* Send the BAN notify type to our primary router. */
4533 if (!server->standalone && (add || del))
4534 silc_server_send_notify_ban(server, server->router->connection,
4535 server->server_type == SILC_ROUTER ?
4536 TRUE : FALSE, channel, add, del);
4538 /* Send the reply back to the client */
4539 if (channel->ban_list)
4541 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4542 SILC_STATUS_OK, ident, 2,
4544 3, channel->ban_list,
4545 strlen(channel->ban_list) - 1);
4548 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4549 SILC_STATUS_OK, ident, 1,
4552 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4553 packet->data, packet->len, FALSE);
4555 silc_buffer_free(packet);
4559 silc_free(channel_id);
4560 silc_server_command_free(cmd);
4563 /* Server side command of CLOSE. Closes connection to a specified server. */
4565 SILC_SERVER_CMD_FUNC(close)
4567 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4568 SilcServer server = cmd->server;
4569 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4570 SilcServerEntry server_entry;
4571 SilcSocketConnection sock;
4574 unsigned char *name;
4575 uint32 port = SILC_PORT;
4577 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CLOSE, cmd, 1, 2);
4579 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4582 /* Check whether client has the permissions. */
4583 if (client->mode == SILC_UMODE_NONE) {
4584 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4585 SILC_STATUS_ERR_NO_SERVER_PRIV);
4589 /* Get the remote server */
4590 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4592 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4593 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4598 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4600 SILC_GET32_MSB(port, tmp);
4602 server_entry = silc_idlist_find_server_by_conn(server->local_list,
4604 if (!server_entry) {
4605 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4606 SILC_STATUS_ERR_NO_SERVER_ID);
4610 /* Send reply to the sender */
4611 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4614 /* Close the connection to the server */
4615 sock = (SilcSocketConnection)server_entry->connection;
4616 silc_server_free_sock_user_data(server, sock);
4617 silc_server_close_connection(server, sock);
4620 silc_server_command_free(cmd);
4623 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4624 active connections. */
4626 SILC_SERVER_CMD_FUNC(shutdown)
4628 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4629 SilcServer server = cmd->server;
4630 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4632 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SHUTDOWN, cmd, 0, 0);
4634 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4637 /* Check whether client has the permission. */
4638 if (client->mode == SILC_UMODE_NONE) {
4639 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4640 SILC_STATUS_ERR_NO_SERVER_PRIV);
4644 /* Send reply to the sender */
4645 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4648 /* Then, gracefully, or not, bring the server down. */
4649 silc_server_stop(server);
4653 silc_server_command_free(cmd);
4656 /* Server side command of LEAVE. Removes client from a channel. */
4658 SILC_SERVER_CMD_FUNC(leave)
4660 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4661 SilcServer server = cmd->server;
4662 SilcSocketConnection sock = cmd->sock;
4663 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4664 SilcChannelID *id = NULL;
4665 SilcChannelEntry channel;
4669 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
4671 /* Get Channel ID */
4672 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4674 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4675 SILC_STATUS_ERR_NO_CHANNEL_ID);
4678 id = silc_id_payload_parse_id(tmp, len);
4680 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4681 SILC_STATUS_ERR_NO_CHANNEL_ID);
4685 /* Get channel entry */
4686 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4688 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4690 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4691 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4696 /* Check whether this client is on the channel */
4697 if (!silc_server_client_on_channel(id_entry, channel)) {
4698 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4699 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4703 /* Notify routers that they should remove this client from their list
4704 of clients on the channel. Send LEAVE notify type. */
4705 if (!server->standalone)
4706 silc_server_send_notify_leave(server, server->router->connection,
4707 server->server_type == SILC_ROUTER ?
4708 TRUE : FALSE, channel, id_entry->id);
4710 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4713 /* Remove client from channel */
4714 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4716 /* If the channel does not exist anymore we won't send anything */
4719 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4720 /* Re-generate channel key */
4721 silc_server_create_channel_key(server, channel, 0);
4723 /* Send the channel key */
4724 silc_server_send_channel_key(server, NULL, channel,
4725 server->server_type == SILC_ROUTER ?
4726 FALSE : !server->standalone);
4732 silc_server_command_free(cmd);
4735 /* Server side of command USERS. Resolves clients and their USERS currently
4736 joined on the requested channel. The list of Client ID's and their modes
4737 on the channel is sent back. */
4739 SILC_SERVER_CMD_FUNC(users)
4741 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4742 SilcServer server = cmd->server;
4743 SilcChannelEntry channel;
4744 SilcChannelID *id = NULL;
4745 SilcBuffer packet, idp;
4746 unsigned char *channel_id;
4747 uint32 channel_id_len;
4748 SilcBuffer client_id_list;
4749 SilcBuffer client_mode_list;
4750 unsigned char lc[4];
4751 uint32 list_count = 0;
4752 uint16 ident = silc_command_get_ident(cmd->payload);
4755 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
4757 /* Get Channel ID */
4758 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4760 /* Get channel name */
4761 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
4763 if (!channel_id && !channel_name) {
4764 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4765 SILC_STATUS_ERR_NO_CHANNEL_ID);
4770 id = silc_id_payload_parse_id(channel_id, channel_id_len);
4772 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4773 SILC_STATUS_ERR_NO_CHANNEL_ID);
4778 /* If we are server and we don't know about this channel we will send
4779 the command to our router. If we know about the channel then we also
4780 have the list of users already. */
4782 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4784 channel = silc_idlist_find_channel_by_name(server->local_list,
4785 channel_name, NULL);
4788 if (server->server_type == SILC_SERVER && !server->standalone &&
4792 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4793 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4795 /* Send USERS command */
4796 silc_server_packet_send(server, server->router->connection,
4797 SILC_PACKET_COMMAND, cmd->packet->flags,
4798 tmpbuf->data, tmpbuf->len, TRUE);
4800 /* Reprocess this packet after received reply */
4801 silc_server_command_pending(server, SILC_COMMAND_USERS,
4802 silc_command_get_ident(cmd->payload),
4803 silc_server_command_destructor,
4804 silc_server_command_users,
4805 silc_server_command_dup(cmd));
4806 cmd->pending = TRUE;
4807 silc_command_set_ident(cmd->payload, ident);
4809 silc_buffer_free(tmpbuf);
4814 /* Check the global list as well. */
4816 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4818 channel = silc_idlist_find_channel_by_name(server->global_list,
4819 channel_name, NULL);
4821 /* Channel really does not exist */
4822 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4823 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4828 /* If the channel is private or secret do not send anything */
4829 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)) {
4830 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4831 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4835 /* Get the users list */
4836 silc_server_get_users_on_channel(server, channel, &client_id_list,
4837 &client_mode_list, &list_count);
4840 SILC_PUT32_MSB(list_count, lc);
4843 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4844 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4845 SILC_STATUS_OK, ident, 4,
4846 2, idp->data, idp->len,
4848 4, client_id_list->data,
4849 client_id_list->len,
4850 5, client_mode_list->data,
4851 client_mode_list->len);
4852 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4853 packet->data, packet->len, FALSE);
4855 silc_buffer_free(idp);
4856 silc_buffer_free(packet);
4857 silc_buffer_free(client_id_list);
4858 silc_buffer_free(client_mode_list);
4863 silc_server_command_free(cmd);
4866 /* Server side of command GETKEY. This fetches the client's public key
4867 from the server where to the client is connected. */
4869 SILC_SERVER_CMD_FUNC(getkey)
4871 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4872 SilcServer server = cmd->server;
4874 SilcClientEntry client;
4875 SilcServerEntry server_entry;
4876 SilcClientID *client_id = NULL;
4877 SilcServerID *server_id = NULL;
4878 SilcIDPayload idp = NULL;
4879 uint16 ident = silc_command_get_ident(cmd->payload);
4885 SILC_LOG_DEBUG(("Start"));
4887 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4889 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4890 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4893 idp = silc_id_payload_parse_data(tmp, tmp_len);
4895 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4896 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4900 id_type = silc_id_payload_get_type(idp);
4901 if (id_type == SILC_ID_CLIENT) {
4902 client_id = silc_id_payload_get_id(idp);
4904 /* If the client is not found from local list there is no chance it
4905 would be locally connected client so send the command further. */
4906 client = silc_idlist_find_client_by_id(server->local_list,
4909 client = silc_idlist_find_client_by_id(server->global_list,
4912 if ((!client && !cmd->pending && !server->standalone) ||
4913 (client && !client->connection && !cmd->pending &&
4914 !server->standalone) ||
4915 (client && !client->data.public_key && !cmd->pending &&
4916 !server->standalone)) {
4919 SilcSocketConnection dest_sock;
4921 dest_sock = silc_server_get_client_route(server, NULL, 0,
4926 old_ident = silc_command_get_ident(cmd->payload);
4927 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4928 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4930 silc_server_packet_send(server, dest_sock,
4931 SILC_PACKET_COMMAND, cmd->packet->flags,
4932 tmpbuf->data, tmpbuf->len, TRUE);
4934 /* Reprocess this packet after received reply from router */
4935 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4936 silc_command_get_ident(cmd->payload),
4937 silc_server_command_destructor,
4938 silc_server_command_getkey,
4939 silc_server_command_dup(cmd));
4940 cmd->pending = TRUE;
4942 silc_command_set_ident(cmd->payload, old_ident);
4943 silc_buffer_free(tmpbuf);
4947 if (!client && cmd->pending) {
4948 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4949 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
4953 /* The client is locally connected, just get the public key and
4955 tmp = silc_pkcs_public_key_encode(client->data.public_key, &tmp_len);
4956 pk = silc_buffer_alloc(4 + tmp_len);
4957 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
4958 silc_buffer_format(pk,
4959 SILC_STR_UI_SHORT(tmp_len),
4960 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
4961 SILC_STR_UI_XNSTRING(tmp, tmp_len),
4965 } else if (id_type == SILC_ID_SERVER) {
4966 server_id = silc_id_payload_get_id(idp);
4968 /* If the server is not found from local list there is no chance it
4969 would be locally connected server so send the command further. */
4970 server_entry = silc_idlist_find_server_by_id(server->local_list,
4973 server_entry = silc_idlist_find_server_by_id(server->global_list,
4976 if ((!server_entry && !cmd->pending && !server->standalone) ||
4977 (server_entry && !server_entry->connection && !cmd->pending &&
4978 !server->standalone) ||
4979 (server_entry && !server_entry->data.public_key && !cmd->pending &&
4980 !server->standalone)) {
4984 old_ident = silc_command_get_ident(cmd->payload);
4985 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4986 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4988 silc_server_packet_send(server, server->router->connection,
4989 SILC_PACKET_COMMAND, cmd->packet->flags,
4990 tmpbuf->data, tmpbuf->len, TRUE);
4992 /* Reprocess this packet after received reply from router */
4993 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4994 silc_command_get_ident(cmd->payload),
4995 silc_server_command_destructor,
4996 silc_server_command_getkey,
4997 silc_server_command_dup(cmd));
4998 cmd->pending = TRUE;
5000 silc_command_set_ident(cmd->payload, old_ident);
5001 silc_buffer_free(tmpbuf);
5005 if (!server_entry && cmd->pending) {
5006 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5007 SILC_STATUS_ERR_NO_SUCH_SERVER_ID);
5011 /* The client is locally connected, just get the public key and
5013 tmp = silc_pkcs_public_key_encode(server_entry->data.public_key, &tmp_len);
5014 pk = silc_buffer_alloc(4 + tmp_len);
5015 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
5016 silc_buffer_format(pk,
5017 SILC_STR_UI_SHORT(tmp_len),
5018 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
5019 SILC_STR_UI_XNSTRING(tmp, tmp_len),
5026 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5027 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
5028 SILC_STATUS_OK, ident, 2,
5030 3, pk->data, pk->len);
5031 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
5032 packet->data, packet->len, FALSE);
5033 silc_buffer_free(packet);
5034 silc_buffer_free(pk);
5038 silc_id_payload_free(idp);
5039 silc_free(client_id);
5040 silc_free(server_id);
5041 silc_server_command_free(cmd);