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,
37 unsigned int arg_type,
39 unsigned int arg_len);
40 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
42 /* Server command list. */
43 SilcServerCommand silc_command_list[] =
45 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
46 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
47 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
48 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
49 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
50 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
51 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
53 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
54 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
55 SILC_SERVER_CMD(connect, CONNECT,
56 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
57 SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
58 SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
59 SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
60 SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
61 SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
62 SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
63 SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
64 SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
65 SILC_SERVER_CMD(restart, RESTART,
66 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
67 SILC_SERVER_CMD(close, CLOSE,
68 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
69 SILC_SERVER_CMD(shutdown, SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
71 SILC_SERVER_CMD(silcoper, SILCOPER,
72 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
73 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
74 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
75 SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
80 #define SILC_SERVER_COMMAND_CHECK_ARGC(command, context, min, max) \
82 unsigned int _argc = silc_argument_get_arg_num(cmd->args); \
84 SILC_LOG_DEBUG(("Start")); \
87 silc_server_command_send_status_reply(cmd, command, \
88 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS); \
89 silc_server_command_free(cmd); \
93 silc_server_command_send_status_reply(cmd, command, \
94 SILC_STATUS_ERR_TOO_MANY_PARAMS); \
95 silc_server_command_free(cmd); \
100 /* Returns TRUE if the connection is registered. Unregistered connections
101 usually cannot send commands hence the check. */
103 static int silc_server_is_registered(SilcServer server,
104 SilcSocketConnection sock,
105 SilcServerCommandContext cmd,
108 SilcIDListData idata = (SilcIDListData)sock->user_data;
109 if (idata->registered)
112 silc_server_command_send_status_reply(cmd, command,
113 SILC_STATUS_ERR_NOT_REGISTERED);
114 silc_server_command_free(cmd);
118 /* Internal context to hold data when executed command with timeout. */
120 SilcServerCommandContext ctx;
121 SilcServerCommand *cmd;
122 } *SilcServerCommandTimeout;
124 /* Timeout callback to process commands with timeout for client. Client's
125 commands are always executed with timeout. */
127 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
129 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
130 SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
132 /* Update access time */
133 client->last_command = time(NULL);
135 if (!(timeout->cmd->flags & SILC_CF_REG))
136 timeout->cmd->cb(timeout->ctx);
137 else if (silc_server_is_registered(timeout->ctx->server,
141 timeout->cmd->cb(timeout->ctx);
146 /* Processes received command packet. */
148 void silc_server_command_process(SilcServer server,
149 SilcSocketConnection sock,
150 SilcPacketContext *packet)
152 SilcServerCommandContext ctx;
153 SilcServerCommand *cmd;
156 /* Allocate command context. This must be free'd by the
157 command routine receiving it. */
158 ctx = silc_server_command_alloc();
159 ctx->server = server;
160 ctx->sock = silc_socket_dup(sock);
161 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
163 /* Parse the command payload in the packet */
164 ctx->payload = silc_command_payload_parse(packet->buffer);
166 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
167 silc_buffer_free(packet->buffer);
168 silc_packet_context_free(packet);
169 silc_socket_free(ctx->sock);
173 ctx->args = silc_command_get_args(ctx->payload);
175 /* Get the command */
176 command = silc_command_get(ctx->payload);
177 for (cmd = silc_command_list; cmd->cb; cmd++)
178 if (cmd->cmd == command)
182 silc_server_command_send_status_reply(ctx, command,
183 SILC_STATUS_ERR_UNKNOWN_COMMAND);
184 silc_server_command_free(ctx);
188 /* Execute client's commands always with timeout. Normally they are
189 executed with zero (0) timeout but if client is sending command more
190 frequently than once in 2 seconds, then the timeout may be 0 to 2
192 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
193 SilcClientEntry client = (SilcClientEntry)sock->user_data;
194 SilcServerCommandTimeout timeout = silc_calloc(1, sizeof(*timeout));
200 if (client->last_command && (time(NULL) - client->last_command) < 2) {
201 client->fast_command++;
204 client->fast_command = ((client->fast_command - 1) <= 0 ? 0 :
205 client->fast_command--);
209 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
210 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
211 silc_task_register(server->timeout_queue, sock->sock,
212 silc_server_command_process_timeout,
214 2 - (time(NULL) - client->last_command), 0,
216 SILC_TASK_PRI_NORMAL);
218 silc_task_register(server->timeout_queue, sock->sock,
219 silc_server_command_process_timeout,
223 SILC_TASK_PRI_NORMAL);
227 /* Execute for server */
229 if (!(cmd->flags & SILC_CF_REG))
231 else if (silc_server_is_registered(server, sock, ctx, cmd->cmd))
235 /* Allocate Command Context */
237 SilcServerCommandContext silc_server_command_alloc()
239 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
244 /* Free's the command context allocated before executing the command */
246 void silc_server_command_free(SilcServerCommandContext ctx)
249 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
251 if (ctx->users < 1) {
253 silc_command_free_payload(ctx->payload);
255 silc_packet_context_free(ctx->packet);
257 silc_socket_free(ctx->sock); /* Decrease reference counter */
262 /* Duplicate Command Context by adding reference counter. The context won't
263 be free'd untill it hits zero. */
265 SilcServerCommandContext
266 silc_server_command_dup(SilcServerCommandContext ctx)
269 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
274 /* Add new pending command to be executed when reply to a command has been
275 received. The `reply_cmd' is the command that will call the `callback'
276 with `context' when reply has been received. If `ident' is non-zero
277 the `callback' will be executed when received reply with command
278 identifier `ident'. */
280 void silc_server_command_pending(SilcServer server,
281 SilcCommand reply_cmd,
282 unsigned short ident,
283 SilcServerPendingDestructor destructor,
284 SilcCommandCb callback,
287 SilcServerCommandPending *reply;
289 reply = silc_calloc(1, sizeof(*reply));
290 reply->reply_cmd = reply_cmd;
291 reply->ident = ident;
292 reply->context = context;
293 reply->callback = callback;
294 reply->destructor = destructor;
295 silc_dlist_add(server->pending_commands, reply);
298 /* Deletes pending command by reply command type. */
300 void silc_server_command_pending_del(SilcServer server,
301 SilcCommand reply_cmd,
302 unsigned short ident)
304 SilcServerCommandPending *r;
306 silc_dlist_start(server->pending_commands);
307 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
308 if (r->reply_cmd == reply_cmd && r->ident == ident) {
309 silc_dlist_del(server->pending_commands, r);
315 /* Checks for pending commands and marks callbacks to be called from
316 the command reply function. Returns TRUE if there were pending command. */
318 int silc_server_command_pending_check(SilcServer server,
319 SilcServerCommandReplyContext ctx,
321 unsigned short ident)
323 SilcServerCommandPending *r;
325 silc_dlist_start(server->pending_commands);
326 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
327 if (r->reply_cmd == command && r->ident == ident) {
328 ctx->context = r->context;
329 ctx->callback = r->callback;
330 ctx->destructor = r->destructor;
339 /* Destructor function for pending callbacks. This is called when using
340 pending commands to free the context given for the pending command. */
342 static void silc_server_command_destructor(void *context)
344 silc_server_command_free((SilcServerCommandContext)context);
347 /* Sends simple status message as command reply packet */
350 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
352 SilcCommandStatus status)
356 SILC_LOG_DEBUG(("Sending command status %d", status));
359 silc_command_reply_payload_encode_va(command, status,
360 silc_command_get_ident(cmd->payload),
362 silc_server_packet_send(cmd->server, cmd->sock,
363 SILC_PACKET_COMMAND_REPLY, 0,
364 buffer->data, buffer->len, FALSE);
365 silc_buffer_free(buffer);
368 /* Sends command status reply with one extra argument. The argument
369 type must be sent as argument. */
372 silc_server_command_send_status_data(SilcServerCommandContext cmd,
374 SilcCommandStatus status,
375 unsigned int arg_type,
377 unsigned int arg_len)
381 SILC_LOG_DEBUG(("Sending command status %d", status));
384 silc_command_reply_payload_encode_va(command, status,
385 silc_command_get_ident(cmd->payload),
386 1, arg_type, arg, arg_len);
387 silc_server_packet_send(cmd->server, cmd->sock,
388 SILC_PACKET_COMMAND_REPLY, 0,
389 buffer->data, buffer->len, FALSE);
390 silc_buffer_free(buffer);
393 /******************************************************************************
397 ******************************************************************************/
400 silc_server_command_whois_parse(SilcServerCommandContext cmd,
401 SilcClientID ***client_id,
402 unsigned int *client_id_count,
410 unsigned int argc = silc_argument_get_arg_num(cmd->args);
413 /* If client ID is in the command it must be used instead of nickname */
414 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
416 /* No ID, get the nickname@server string and parse it. */
417 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
419 if (strchr(tmp, '@')) {
420 len = strcspn(tmp, "@");
421 *nickname = silc_calloc(len + 1, sizeof(char));
422 memcpy(*nickname, tmp, len);
423 *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
424 memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
426 *nickname = strdup(tmp);
429 silc_server_command_send_status_reply(cmd, command,
430 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
434 /* Command includes ID, we must use that. Also check whether the command
435 has more than one ID set - take them all. */
437 *client_id = silc_calloc(1, sizeof(**client_id));
438 (*client_id)[0] = silc_id_payload_parse_id(tmp, len);
439 if ((*client_id)[0] == NULL) {
440 silc_free(*client_id);
443 *client_id_count = 1;
445 /* Take all ID's from the command packet */
447 for (k = 1, i = 1; i < argc; i++) {
448 tmp = silc_argument_get_arg_type(cmd->args, i + 3, &len);
450 *client_id = silc_realloc(*client_id, sizeof(**client_id) *
451 (*client_id_count + 1));
452 (*client_id)[k] = silc_id_payload_parse_id(tmp, len);
453 if ((*client_id)[k] == NULL) {
454 /* Cleanup all and fail */
455 for (i = 0; i < *client_id_count; i++)
456 silc_free((*client_id)[i]);
457 silc_free(*client_id);
460 (*client_id_count)++;
466 /* Command includes ID, use that */
469 /* Get the max count of reply messages allowed */
470 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
480 silc_server_command_whois_check(SilcServerCommandContext cmd,
481 SilcClientEntry *clients,
482 unsigned int clients_count)
484 SilcServer server = cmd->server;
486 SilcClientEntry entry;
488 for (i = 0; i < clients_count; i++) {
491 if (entry->data.registered == FALSE)
494 if (!entry->nickname || !entry->username || !entry->userinfo) {
496 unsigned short old_ident;
501 old_ident = silc_command_get_ident(cmd->payload);
502 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
503 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
505 /* Send WHOIS command */
506 silc_server_packet_send(server, entry->router->connection,
507 SILC_PACKET_COMMAND, cmd->packet->flags,
508 tmpbuf->data, tmpbuf->len, TRUE);
510 /* Reprocess this packet after received reply */
511 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
512 silc_command_get_ident(cmd->payload),
513 silc_server_command_destructor,
514 silc_server_command_whois,
515 silc_server_command_dup(cmd));
518 silc_command_set_ident(cmd->payload, old_ident);
520 silc_buffer_free(tmpbuf);
529 silc_server_command_whois_send_reply(SilcServerCommandContext cmd,
530 SilcClientEntry *clients,
531 unsigned int clients_count)
533 SilcServer server = cmd->server;
535 int i, count = 0, len;
536 SilcBuffer packet, idp, channels;
537 SilcClientEntry entry;
538 SilcCommandStatus status;
539 unsigned short ident = silc_command_get_ident(cmd->payload);
540 char nh[128], uh[128];
541 unsigned char idle[4], mode[4];
542 SilcSocketConnection hsock;
544 status = SILC_STATUS_OK;
545 if (clients_count > 1)
546 status = SILC_STATUS_LIST_START;
548 for (i = 0; i < clients_count; i++) {
551 if (entry->data.registered == FALSE) {
552 if (clients_count == 1) {
553 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
554 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
555 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
556 2, idp->data, idp->len);
557 silc_buffer_free(idp);
562 if (count && i - 1 == count)
566 status = SILC_STATUS_LIST_ITEM;
568 if (clients_count > 1 && i == clients_count - 1)
569 status = SILC_STATUS_LIST_END;
571 /* Sanity check, however these should never fail. However, as
572 this sanity check has been added here they have failed. */
573 if (!entry->nickname || !entry->username || !entry->userinfo)
576 /* Send WHOIS reply */
577 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
578 tmp = silc_argument_get_first_arg(cmd->args, NULL);
580 memset(uh, 0, sizeof(uh));
581 memset(nh, 0, sizeof(nh));
582 memset(idle, 0, sizeof(idle));
584 strncat(nh, entry->nickname, strlen(entry->nickname));
585 if (!strchr(entry->nickname, '@')) {
587 len = entry->router ? strlen(entry->router->server_name) :
588 strlen(server->server_name);
589 strncat(nh, entry->router ? entry->router->server_name :
590 server->server_name, len);
593 strncat(uh, entry->username, strlen(entry->username));
594 if (!strchr(entry->username, '@')) {
596 hsock = (SilcSocketConnection)entry->connection;
597 len = strlen(hsock->hostname);
598 strncat(uh, hsock->hostname, len);
601 channels = silc_server_get_client_channel_list(server, entry);
603 SILC_PUT32_MSB(entry->mode, mode);
605 if (entry->connection) {
606 SILC_PUT32_MSB((time(NULL) - entry->data.last_receive), idle);
610 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
612 2, idp->data, idp->len,
616 strlen(entry->userinfo),
622 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
624 2, idp->data, idp->len,
628 strlen(entry->userinfo),
632 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
633 0, packet->data, packet->len, FALSE);
635 silc_buffer_free(packet);
636 silc_buffer_free(idp);
638 silc_buffer_free(channels);
643 silc_server_command_whois_from_client(SilcServerCommandContext cmd)
645 SilcServer server = cmd->server;
646 char *nick = NULL, *server_name = NULL;
647 int count = 0, clients_count = 0;
648 SilcClientEntry *clients = NULL, entry;
649 SilcClientID **client_id = NULL;
650 unsigned int client_id_count = 0;
653 /* Protocol dictates that we must always send the received WHOIS request
654 to our router if we are normal server, so let's do it now unless we
655 are standalone. We will not send any replies to the client until we
656 have received reply from the router. */
657 if (server->server_type == SILC_SERVER && !cmd->pending &&
658 !server->standalone) {
660 unsigned short old_ident;
662 old_ident = silc_command_get_ident(cmd->payload);
663 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
664 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
666 /* Send WHOIS command to our router */
667 silc_server_packet_send(server, (SilcSocketConnection)
668 server->router->connection,
669 SILC_PACKET_COMMAND, cmd->packet->flags,
670 tmpbuf->data, tmpbuf->len, TRUE);
672 /* Reprocess this packet after received reply from router */
673 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
674 silc_command_get_ident(cmd->payload),
675 silc_server_command_destructor,
676 silc_server_command_whois,
677 silc_server_command_dup(cmd));
680 silc_command_set_ident(cmd->payload, old_ident);
682 silc_buffer_free(tmpbuf);
687 /* We are ready to process the command request. Let's search for the
688 requested client and send reply to the requesting client. */
690 /* Parse the whois request */
691 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
692 &nick, &server_name, &count,
696 /* Get all clients matching that ID or nickname from local list */
697 if (client_id_count) {
698 /* Check all Client ID's received in the command packet */
699 for (i = 0; i < client_id_count; i++) {
700 entry = silc_idlist_find_client_by_id(server->local_list,
703 clients = silc_realloc(clients, sizeof(*clients) *
704 (clients_count + 1));
705 clients[clients_count++] = entry;
709 clients = silc_idlist_get_clients_by_hash(server->local_list,
710 nick, server->md5hash,
713 clients = silc_idlist_get_clients_by_nickname(server->local_list,
718 /* Check global list as well */
720 if (client_id_count) {
721 /* Check all Client ID's received in the command packet */
722 for (i = 0; i < client_id_count; i++) {
723 entry = silc_idlist_find_client_by_id(server->global_list,
726 clients = silc_realloc(clients, sizeof(*clients) *
727 (clients_count + 1));
728 clients[clients_count++] = entry;
732 clients = silc_idlist_get_clients_by_hash(server->global_list,
733 nick, server->md5hash,
736 clients = silc_idlist_get_clients_by_nickname(server->global_list,
743 /* Such client(s) really does not exist in the SILC network. */
744 if (!client_id_count) {
745 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
746 SILC_STATUS_ERR_NO_SUCH_NICK,
747 3, nick, strlen(nick));
749 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
750 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
751 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
752 2, idp->data, idp->len);
753 silc_buffer_free(idp);
758 /* Router always finds the client entry if it exists in the SILC network.
759 However, it might be incomplete entry and does not include all the
760 mandatory fields that WHOIS command reply requires. Check for these and
761 make query from the server who owns the client if some fields are
763 if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
768 /* Send the command reply to the client */
769 silc_server_command_whois_send_reply(cmd, clients, clients_count);
772 if (client_id_count) {
773 for (i = 0; i < client_id_count; i++)
774 silc_free(client_id[i]);
775 silc_free(client_id);
782 silc_free(server_name);
788 silc_server_command_whois_from_server(SilcServerCommandContext cmd)
790 SilcServer server = cmd->server;
791 char *nick = NULL, *server_name = NULL;
792 int count = 0, clients_count = 0;
793 SilcClientEntry *clients = NULL, entry;
794 SilcClientID **client_id = NULL;
795 unsigned int client_id_count = 0;
798 /* Parse the whois request */
799 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
800 &nick, &server_name, &count,
804 /* Process the command request. Let's search for the requested client and
805 send reply to the requesting server. */
807 if (client_id_count) {
808 /* Check all Client ID's received in the command packet */
809 for (i = 0; i < client_id_count; i++) {
810 entry = silc_idlist_find_client_by_id(server->local_list,
813 clients = silc_realloc(clients, sizeof(*clients) *
814 (clients_count + 1));
815 clients[clients_count++] = entry;
819 clients = silc_idlist_get_clients_by_hash(server->local_list,
820 nick, server->md5hash,
823 clients = silc_idlist_get_clients_by_nickname(server->local_list,
828 /* If we are router we will check our global list as well. */
829 if (!clients && server->server_type == SILC_ROUTER) {
830 if (client_id_count) {
831 /* Check all Client ID's received in the command packet */
832 for (i = 0; i < client_id_count; i++) {
833 entry = silc_idlist_find_client_by_id(server->global_list,
836 clients = silc_realloc(clients, sizeof(*clients) *
837 (clients_count + 1));
838 clients[clients_count++] = entry;
842 clients = silc_idlist_get_clients_by_hash(server->global_list,
843 nick, server->md5hash,
846 clients = silc_idlist_get_clients_by_nickname(server->global_list,
853 /* Such a client really does not exist in the SILC network. */
854 if (!client_id_count) {
855 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
856 SILC_STATUS_ERR_NO_SUCH_NICK,
857 3, nick, strlen(nick));
859 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
860 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
861 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
862 2, idp->data, idp->len);
863 silc_buffer_free(idp);
868 /* Router always finds the client entry if it exists in the SILC network.
869 However, it might be incomplete entry and does not include all the
870 mandatory fields that WHOIS command reply requires. Check for these and
871 make query from the server who owns the client if some fields are
873 if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
878 /* Send the command reply to the client */
879 silc_server_command_whois_send_reply(cmd, clients, clients_count);
882 if (client_id_count) {
883 for (i = 0; i < client_id_count; i++)
884 silc_free(client_id[i]);
885 silc_free(client_id);
892 silc_free(server_name);
897 /* Server side of command WHOIS. Processes user's query and sends found
898 results as command replies back to the client. */
900 SILC_SERVER_CMD_FUNC(whois)
902 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
905 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_WHOIS, cmd, 1, 3328);
907 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
908 ret = silc_server_command_whois_from_client(cmd);
909 else if ((cmd->sock->type == SILC_SOCKET_TYPE_SERVER) ||
910 (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER))
911 ret = silc_server_command_whois_from_server(cmd);
914 silc_server_command_free(cmd);
917 /******************************************************************************
921 ******************************************************************************/
924 silc_server_command_whowas_parse(SilcServerCommandContext cmd,
932 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
934 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WHOWAS,
935 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
939 /* Get the nickname@server string and parse it. */
940 if (strchr(tmp, '@')) {
941 len = strcspn(tmp, "@");
942 *nickname = silc_calloc(len + 1, sizeof(char));
943 memcpy(*nickname, tmp, len);
944 *server_name = silc_calloc(strlen(tmp) - len, sizeof(char));
945 memcpy(*server_name, tmp + len + 1, strlen(tmp) - len - 1);
947 *nickname = strdup(tmp);
949 /* Get the max count of reply messages allowed */
950 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
960 silc_server_command_whowas_check(SilcServerCommandContext cmd,
961 SilcClientEntry *clients,
962 unsigned int clients_count)
964 SilcServer server = cmd->server;
966 SilcClientEntry entry;
968 for (i = 0; i < clients_count; i++) {
971 if (!entry->nickname || !entry->username) {
973 unsigned short old_ident;
978 old_ident = silc_command_get_ident(cmd->payload);
979 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
980 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
982 /* Send WHOWAS command */
983 silc_server_packet_send(server, entry->router->connection,
984 SILC_PACKET_COMMAND, cmd->packet->flags,
985 tmpbuf->data, tmpbuf->len, TRUE);
987 /* Reprocess this packet after received reply */
988 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
989 silc_command_get_ident(cmd->payload),
990 silc_server_command_destructor,
991 silc_server_command_whowas,
992 silc_server_command_dup(cmd));
995 silc_command_set_ident(cmd->payload, old_ident);
997 silc_buffer_free(tmpbuf);
1006 silc_server_command_whowas_send_reply(SilcServerCommandContext cmd,
1007 SilcClientEntry *clients,
1008 unsigned int clients_count)
1010 SilcServer server = cmd->server;
1012 int i, count = 0, len;
1013 SilcBuffer packet, idp;
1014 SilcClientEntry entry = NULL;
1015 SilcCommandStatus status;
1016 unsigned short ident = silc_command_get_ident(cmd->payload);
1018 char nh[256], uh[256];
1020 status = SILC_STATUS_OK;
1021 if (clients_count > 1)
1022 status = SILC_STATUS_LIST_START;
1024 for (i = 0; i < clients_count; i++) {
1027 /* We will take only clients that are not valid anymore. They are the
1028 ones that are not registered anymore but still have a ID. They
1029 have disconnected us, and thus valid for WHOWAS. */
1030 if (entry->data.registered == TRUE)
1032 if (entry->id == NULL)
1035 if (count && i - 1 == count)
1040 if (clients_count > 2)
1041 status = SILC_STATUS_LIST_ITEM;
1043 if (clients_count > 1 && i == clients_count - 1)
1044 status = SILC_STATUS_LIST_END;
1046 /* Sanity check, however these should never fail. However, as
1047 this sanity check has been added here they have failed. */
1048 if (!entry->nickname || !entry->username)
1051 /* Send WHOWAS reply */
1052 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1053 tmp = silc_argument_get_first_arg(cmd->args, NULL);
1055 memset(uh, 0, sizeof(uh));
1056 memset(nh, 0, sizeof(nh));
1058 strncat(nh, entry->nickname, strlen(entry->nickname));
1059 if (!strchr(entry->nickname, '@')) {
1060 strncat(nh, "@", 1);
1061 len = entry->router ? strlen(entry->router->server_name) :
1062 strlen(server->server_name);
1063 strncat(nh, entry->router ? entry->router->server_name :
1064 server->server_name, len);
1067 strncat(uh, entry->username, strlen(entry->username));
1068 if (!strchr(entry->username, '@')) {
1069 strncat(uh, "@", 1);
1070 strcat(uh, "*private*");
1073 if (entry->userinfo)
1075 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1077 2, idp->data, idp->len,
1081 strlen(entry->userinfo));
1084 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1086 2, idp->data, idp->len,
1090 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1091 0, packet->data, packet->len, FALSE);
1093 silc_buffer_free(packet);
1094 silc_buffer_free(idp);
1097 if (found == FALSE && entry)
1098 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1099 SILC_STATUS_ERR_NO_SUCH_NICK,
1101 strlen(entry->nickname));
1105 silc_server_command_whowas_from_client(SilcServerCommandContext cmd)
1107 SilcServer server = cmd->server;
1108 char *nick = NULL, *server_name = NULL;
1109 int count = 0, clients_count = 0;
1110 SilcClientEntry *clients = NULL;
1113 /* Protocol dictates that we must always send the received WHOWAS request
1114 to our router if we are normal server, so let's do it now unless we
1115 are standalone. We will not send any replies to the client until we
1116 have received reply from the router. */
1117 if (server->server_type == SILC_SERVER &&
1118 !cmd->pending && !server->standalone) {
1120 unsigned short old_ident;
1122 old_ident = silc_command_get_ident(cmd->payload);
1123 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1124 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1126 /* Send WHOWAS command to our router */
1127 silc_server_packet_send(server, (SilcSocketConnection)
1128 server->router->connection,
1129 SILC_PACKET_COMMAND, cmd->packet->flags,
1130 tmpbuf->data, tmpbuf->len, TRUE);
1132 /* Reprocess this packet after received reply from router */
1133 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
1134 silc_command_get_ident(cmd->payload),
1135 silc_server_command_destructor,
1136 silc_server_command_whowas,
1137 silc_server_command_dup(cmd));
1138 cmd->pending = TRUE;
1140 silc_command_set_ident(cmd->payload, old_ident);
1142 silc_buffer_free(tmpbuf);
1147 /* We are ready to process the command request. Let's search for the
1148 requested client and send reply to the requesting client. */
1150 /* Parse the whowas request */
1151 if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1154 /* Get all clients matching that nickname from local list */
1155 clients = silc_idlist_get_clients_by_nickname(server->local_list,
1159 clients = silc_idlist_get_clients_by_hash(server->local_list,
1160 nick, server->md5hash,
1163 /* Check global list as well */
1165 clients = silc_idlist_get_clients_by_nickname(server->global_list,
1169 clients = silc_idlist_get_clients_by_hash(server->global_list,
1170 nick, server->md5hash,
1174 if (!silc_server_command_whowas_check(cmd, clients, clients_count)) {
1179 /* Send the command reply to the client */
1180 silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1188 silc_free(server_name);
1194 silc_server_command_whowas_from_server(SilcServerCommandContext cmd)
1196 SilcServer server = cmd->server;
1197 char *nick = NULL, *server_name = NULL;
1198 int count = 0, clients_count = 0;
1199 SilcClientEntry *clients = NULL;
1202 /* Parse the whowas request */
1203 if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1206 /* Process the command request. Let's search for the requested client and
1207 send reply to the requesting server. */
1209 clients = silc_idlist_get_clients_by_nickname(server->local_list,
1213 clients = silc_idlist_get_clients_by_hash(server->local_list,
1214 nick, server->md5hash,
1217 /* If we are router we will check our global list as well. */
1218 if (!clients && server->server_type == SILC_ROUTER) {
1219 clients = silc_idlist_get_clients_by_nickname(server->global_list,
1223 clients = silc_idlist_get_clients_by_hash(server->global_list,
1224 nick, server->md5hash,
1229 /* Such a client really does not exist in the SILC network. */
1230 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1231 SILC_STATUS_ERR_NO_SUCH_NICK,
1232 3, nick, strlen(nick));
1236 /* Send the command reply to the client */
1237 silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1245 silc_free(server_name);
1250 /* Server side of command WHOWAS. */
1252 SILC_SERVER_CMD_FUNC(whowas)
1254 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1257 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_WHOWAS, cmd, 1, 2);
1259 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1260 ret = silc_server_command_whowas_from_client(cmd);
1261 else if ((cmd->sock->type == SILC_SOCKET_TYPE_SERVER) ||
1262 (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER))
1263 ret = silc_server_command_whowas_from_server(cmd);
1266 silc_server_command_free(cmd);
1269 /******************************************************************************
1273 ******************************************************************************/
1275 /* Checks that all mandatory fields are present. If not then send WHOIS
1276 request to the server who owns the client. We use WHOIS because we want
1277 to get as much information as possible at once. */
1280 silc_server_command_identify_check(SilcServerCommandContext cmd,
1281 SilcClientEntry *clients,
1282 unsigned int clients_count)
1284 SilcServer server = cmd->server;
1286 SilcClientEntry entry;
1288 for (i = 0; i < clients_count; i++) {
1291 if (entry->data.registered == FALSE)
1294 if (!entry->nickname) {
1296 unsigned short old_ident;
1301 old_ident = silc_command_get_ident(cmd->payload);
1302 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1303 silc_command_set_command(cmd->payload, SILC_COMMAND_WHOIS);
1304 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1306 /* Send WHOIS request. We send WHOIS since we're doing the requesting
1307 now anyway so make it a good one. */
1308 silc_server_packet_send(server, entry->router->connection,
1309 SILC_PACKET_COMMAND, cmd->packet->flags,
1310 tmpbuf->data, tmpbuf->len, TRUE);
1312 /* Reprocess this packet after received reply */
1313 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1314 silc_command_get_ident(cmd->payload),
1315 silc_server_command_destructor,
1316 silc_server_command_identify,
1317 silc_server_command_dup(cmd));
1319 cmd->pending = TRUE;
1321 /* Put old data back to the Command Payload we just changed */
1322 silc_command_set_ident(cmd->payload, old_ident);
1323 silc_command_set_command(cmd->payload, SILC_COMMAND_IDENTIFY);
1325 silc_buffer_free(tmpbuf);
1334 silc_server_command_identify_send_reply(SilcServerCommandContext cmd,
1335 SilcClientEntry *clients,
1336 unsigned int clients_count)
1338 SilcServer server = cmd->server;
1340 int i, count = 0, len;
1341 SilcBuffer packet, idp;
1342 SilcClientEntry entry;
1343 SilcCommandStatus status;
1344 unsigned short ident = silc_command_get_ident(cmd->payload);
1346 status = SILC_STATUS_OK;
1347 if (clients_count > 1)
1348 status = SILC_STATUS_LIST_START;
1350 for (i = 0; i < clients_count; i++) {
1353 if (entry->data.registered == FALSE) {
1354 if (clients_count == 1) {
1355 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1356 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1357 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1358 2, idp->data, idp->len);
1359 silc_buffer_free(idp);
1364 if (count && i - 1 == count)
1368 status = SILC_STATUS_LIST_ITEM;
1370 if (clients_count > 1 && i == clients_count - 1)
1371 status = SILC_STATUS_LIST_END;
1373 /* Send IDENTIFY reply */
1374 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1375 tmp = silc_argument_get_first_arg(cmd->args, NULL);
1379 char nh[256], uh[256];
1380 SilcSocketConnection hsock;
1382 memset(uh, 0, sizeof(uh));
1383 memset(nh, 0, sizeof(nh));
1385 strncat(nh, entry->nickname, strlen(entry->nickname));
1386 if (!strchr(entry->nickname, '@')) {
1387 strncat(nh, "@", 1);
1388 len = entry->router ? strlen(entry->router->server_name) :
1389 strlen(server->server_name);
1390 strncat(nh, entry->router ? entry->router->server_name :
1391 server->server_name, len);
1394 if (!entry->username) {
1395 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1397 2, idp->data, idp->len,
1400 strncat(uh, entry->username, strlen(entry->username));
1401 if (!strchr(entry->username, '@')) {
1402 strncat(uh, "@", 1);
1403 hsock = (SilcSocketConnection)entry->connection;
1404 len = strlen(hsock->hostname);
1405 strncat(uh, hsock->hostname, len);
1408 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1410 2, idp->data, idp->len,
1415 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1416 0, packet->data, packet->len, FALSE);
1418 silc_buffer_free(packet);
1419 silc_buffer_free(idp);
1425 silc_server_command_identify_from_client(SilcServerCommandContext cmd)
1427 SilcServer server = cmd->server;
1428 char *nick = NULL, *server_name = NULL;
1429 int count = 0, clients_count = 0;
1430 SilcClientEntry *clients = NULL, entry;
1431 SilcClientID **client_id = NULL;
1432 unsigned int client_id_count = 0;
1435 /* Protocol dictates that we must always send the received IDENTIFY request
1436 to our router if we are normal server, so let's do it now unless we
1437 are standalone. We will not send any replies to the client until we
1438 have received reply from the router. */
1439 if (server->server_type == SILC_SERVER &&
1440 !cmd->pending && !server->standalone) {
1442 unsigned short old_ident;
1444 old_ident = silc_command_get_ident(cmd->payload);
1445 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1446 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1448 /* Send IDENTIFY command to our router */
1449 silc_server_packet_send(server, (SilcSocketConnection)
1450 server->router->connection,
1451 SILC_PACKET_COMMAND, cmd->packet->flags,
1452 tmpbuf->data, tmpbuf->len, TRUE);
1454 /* Reprocess this packet after received reply from router */
1455 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
1456 silc_command_get_ident(cmd->payload),
1457 silc_server_command_destructor,
1458 silc_server_command_identify,
1459 silc_server_command_dup(cmd));
1460 cmd->pending = TRUE;
1462 silc_command_set_ident(cmd->payload, old_ident);
1464 silc_buffer_free(tmpbuf);
1469 /* We are ready to process the command request. Let's search for the
1470 requested client and send reply to the requesting client. */
1472 /* Parse the IDENTIFY request */
1473 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
1474 &nick, &server_name, &count,
1475 SILC_COMMAND_IDENTIFY))
1478 /* Get all clients matching that ID or nickname from local list */
1479 if (client_id_count) {
1480 /* Check all Client ID's received in the command packet */
1481 for (i = 0; i < client_id_count; i++) {
1482 entry = silc_idlist_find_client_by_id(server->local_list,
1483 client_id[i], NULL);
1485 clients = silc_realloc(clients, sizeof(*clients) *
1486 (clients_count + 1));
1487 clients[clients_count++] = entry;
1491 clients = silc_idlist_get_clients_by_nickname(server->local_list,
1495 clients = silc_idlist_get_clients_by_hash(server->local_list,
1496 nick, server->md5hash,
1500 /* Check global list as well */
1502 if (client_id_count) {
1503 /* Check all Client ID's received in the command packet */
1504 for (i = 0; i < client_id_count; i++) {
1505 entry = silc_idlist_find_client_by_id(server->global_list,
1506 client_id[i], NULL);
1508 clients = silc_realloc(clients, sizeof(*clients) *
1509 (clients_count + 1));
1510 clients[clients_count++] = entry;
1514 clients = silc_idlist_get_clients_by_nickname(server->global_list,
1518 clients = silc_idlist_get_clients_by_hash(server->global_list,
1519 nick, server->md5hash,
1525 /* Such a client really does not exist in the SILC network. */
1526 if (!client_id_count) {
1527 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1528 SILC_STATUS_ERR_NO_SUCH_NICK,
1529 3, nick, strlen(nick));
1531 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
1532 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1533 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1534 2, idp->data, idp->len);
1535 silc_buffer_free(idp);
1540 /* Check that all mandatory fields are present and request those data
1541 from the server who owns the client if necessary. */
1542 if (!silc_server_command_identify_check(cmd, clients, clients_count)) {
1547 /* Send the command reply to the client */
1548 silc_server_command_identify_send_reply(cmd, clients, clients_count);
1551 if (client_id_count) {
1552 for (i = 0; i < client_id_count; i++)
1553 silc_free(client_id[i]);
1554 silc_free(client_id);
1561 silc_free(server_name);
1567 silc_server_command_identify_from_server(SilcServerCommandContext cmd)
1569 SilcServer server = cmd->server;
1570 char *nick = NULL, *server_name = NULL;
1571 int count = 0, clients_count = 0;
1572 SilcClientEntry *clients = NULL, entry;
1573 SilcClientID **client_id = NULL;
1574 unsigned int client_id_count = 0;
1577 /* Parse the IDENTIFY request */
1578 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
1579 &nick, &server_name, &count,
1580 SILC_COMMAND_IDENTIFY))
1583 /* Process the command request. Let's search for the requested client and
1584 send reply to the requesting server. */
1586 if (client_id_count) {
1587 /* Check all Client ID's received in the command packet */
1588 for (i = 0; i < client_id_count; i++) {
1589 entry = silc_idlist_find_client_by_id(server->local_list,
1590 client_id[i], NULL);
1592 clients = silc_realloc(clients, sizeof(*clients) *
1593 (clients_count + 1));
1594 clients[clients_count++] = entry;
1598 clients = silc_idlist_get_clients_by_nickname(server->local_list,
1602 clients = silc_idlist_get_clients_by_hash(server->local_list,
1603 nick, server->md5hash,
1607 /* If we are router we will check our global list as well. */
1608 if (!clients && server->server_type == SILC_ROUTER) {
1609 if (client_id_count) {
1610 /* Check all Client ID's received in the command packet */
1611 for (i = 0; i < client_id_count; i++) {
1612 entry = silc_idlist_find_client_by_id(server->global_list,
1613 client_id[i], NULL);
1615 clients = silc_realloc(clients, sizeof(*clients) *
1616 (clients_count + 1));
1617 clients[clients_count++] = entry;
1621 clients = silc_idlist_get_clients_by_nickname(server->global_list,
1625 clients = silc_idlist_get_clients_by_hash(server->global_list,
1626 nick, server->md5hash,
1632 /* Such a client really does not exist in the SILC network. */
1633 if (!client_id_count) {
1634 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1635 SILC_STATUS_ERR_NO_SUCH_NICK,
1636 3, nick, strlen(nick));
1638 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
1639 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1640 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1641 2, idp->data, idp->len);
1642 silc_buffer_free(idp);
1647 /* Check that all mandatory fields are present and request those data
1648 from the server who owns the client if necessary. */
1649 if (!silc_server_command_identify_check(cmd, clients, clients_count)) {
1654 /* Send the command reply */
1655 silc_server_command_identify_send_reply(cmd, clients, clients_count);
1658 if (client_id_count) {
1659 for (i = 0; i < client_id_count; i++)
1660 silc_free(client_id[i]);
1661 silc_free(client_id);
1668 silc_free(server_name);
1673 SILC_SERVER_CMD_FUNC(identify)
1675 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1678 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_IDENTIFY, cmd, 1, 3328);
1680 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1681 ret = silc_server_command_identify_from_client(cmd);
1682 else if ((cmd->sock->type == SILC_SOCKET_TYPE_SERVER) |
1683 (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER))
1684 ret = silc_server_command_identify_from_server(cmd);
1687 silc_server_command_free(cmd);
1690 /* Checks string for bad characters and returns TRUE if they are found. */
1692 static int silc_server_command_bad_chars(char *nick)
1694 if (strchr(nick, '\\')) return TRUE;
1695 if (strchr(nick, '\"')) return TRUE;
1696 if (strchr(nick, '´')) return TRUE;
1697 if (strchr(nick, '`')) return TRUE;
1698 if (strchr(nick, '\'')) return TRUE;
1699 if (strchr(nick, '*')) return TRUE;
1700 if (strchr(nick, '/')) return TRUE;
1701 if (strchr(nick, '@')) return TRUE;
1706 /* Server side of command NICK. Sets nickname for user. Setting
1707 nickname causes generation of a new client ID for the client. The
1708 new client ID is sent to the client after changing the nickname. */
1710 SILC_SERVER_CMD_FUNC(nick)
1712 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1713 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1714 SilcServer server = cmd->server;
1715 SilcBuffer packet, nidp, oidp;
1716 SilcClientID *new_id;
1718 unsigned short ident = silc_command_get_ident(cmd->payload);
1720 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1723 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_NICK, cmd, 1, 1);
1725 /* Check nickname */
1726 nick = silc_argument_get_arg_type(cmd->args, 1, NULL);
1727 if (silc_server_command_bad_chars(nick) == TRUE) {
1728 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
1729 SILC_STATUS_ERR_BAD_NICKNAME);
1733 if (strlen(nick) > 128)
1736 /* Create new Client ID */
1737 silc_id_create_client_id(cmd->server->id, cmd->server->rng,
1738 cmd->server->md5hash, nick,
1741 /* Send notify about nickname change to our router. We send the new
1742 ID and ask to replace it with the old one. If we are router the
1743 packet is broadcasted. Send NICK_CHANGE notify. */
1744 if (!server->standalone)
1745 silc_server_send_notify_nick_change(server, server->router->connection,
1746 server->server_type == SILC_SERVER ?
1747 FALSE : TRUE, client->id,
1748 new_id, SILC_ID_CLIENT_LEN);
1750 /* Remove old cache entry */
1751 silc_idcache_del_by_id(server->local_list->clients, SILC_ID_CLIENT,
1754 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1758 memset(client->id, 0, SILC_ID_CLIENT_LEN);
1759 silc_free(client->id);
1762 /* Save the nickname as this client is our local client */
1763 if (client->nickname)
1764 silc_free(client->nickname);
1766 client->nickname = strdup(nick);
1767 client->id = new_id;
1769 /* Update client cache */
1770 silc_idcache_add(server->local_list->clients, client->nickname,
1771 strlen(client->nickname), SILC_ID_CLIENT, client->id,
1772 (void *)client, TRUE, FALSE);
1774 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1776 /* Send NICK_CHANGE notify to the client's channels */
1777 silc_server_send_notify_on_channels(server, NULL, client,
1778 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
1779 oidp->data, oidp->len,
1780 nidp->data, nidp->len);
1782 /* Send the new Client ID as reply command back to client */
1783 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
1784 SILC_STATUS_OK, ident, 1,
1785 2, nidp->data, nidp->len);
1786 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1787 0, packet->data, packet->len, FALSE);
1789 silc_buffer_free(packet);
1790 silc_buffer_free(nidp);
1791 silc_buffer_free(oidp);
1794 silc_server_command_free(cmd);
1797 /* Sends the LIST command reply */
1800 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
1801 SilcChannelEntry *lch,
1802 unsigned int lch_count,
1803 SilcChannelEntry *gch,
1804 unsigned int gch_count)
1807 SilcBuffer packet, idp;
1808 SilcChannelEntry entry;
1809 SilcCommandStatus status;
1810 unsigned short ident = silc_command_get_ident(cmd->payload);
1812 unsigned char usercount[4];
1815 for (i = 0; i < lch_count; i++)
1816 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1818 for (i = 0; i < gch_count; i++)
1819 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
1822 status = SILC_STATUS_OK;
1823 if ((lch_count + gch_count) > 1)
1824 status = SILC_STATUS_LIST_START;
1827 for (i = 0; i < lch_count; i++) {
1834 status = SILC_STATUS_LIST_ITEM;
1836 if (i == lch_count - 1 && gch_count)
1838 if (lch_count > 1 && i == lch_count - 1)
1839 status = SILC_STATUS_LIST_END;
1841 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1843 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1844 topic = "*private*";
1845 memset(usercount, 0, sizeof(usercount));
1847 topic = entry->topic;
1848 users = silc_list_count(entry->user_list);
1849 SILC_PUT32_MSB(users, usercount);
1852 /* Send the reply */
1855 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1857 2, idp->data, idp->len,
1858 3, entry->channel_name,
1859 strlen(entry->channel_name),
1860 4, topic, strlen(topic),
1864 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1866 2, idp->data, idp->len,
1867 3, entry->channel_name,
1868 strlen(entry->channel_name),
1870 silc_server_packet_send(cmd->server, cmd->sock,
1871 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1872 packet->len, FALSE);
1873 silc_buffer_free(packet);
1874 silc_buffer_free(idp);
1877 status = i ? SILC_STATUS_LIST_ITEM : SILC_STATUS_OK;
1880 for (i = 0; i < gch_count; i++) {
1887 status = SILC_STATUS_LIST_ITEM;
1889 if (gch_count > 1 && i == lch_count - 1)
1890 status = SILC_STATUS_LIST_END;
1892 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1894 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
1895 topic = "*private*";
1896 memset(usercount, 0, sizeof(usercount));
1898 topic = entry->topic;
1899 users = silc_list_count(entry->user_list);
1900 SILC_PUT32_MSB(users, usercount);
1903 /* Send the reply */
1906 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1908 2, idp->data, idp->len,
1909 3, entry->channel_name,
1910 strlen(entry->channel_name),
1911 4, topic, strlen(topic),
1915 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
1917 2, idp->data, idp->len,
1918 3, entry->channel_name,
1919 strlen(entry->channel_name),
1921 silc_server_packet_send(cmd->server, cmd->sock,
1922 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
1923 packet->len, FALSE);
1924 silc_buffer_free(packet);
1925 silc_buffer_free(idp);
1929 /* Server side of LIST command. This lists the channel of the requested
1930 server. Secret channels are not listed. */
1932 SILC_SERVER_CMD_FUNC(list)
1934 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1935 SilcServer server = cmd->server;
1936 SilcChannelID *channel_id = NULL;
1938 unsigned int tmp_len;
1939 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
1940 unsigned int lch_count = 0, gch_count = 0;
1942 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_LIST, cmd, 0, 2);
1944 /* Get Channel ID */
1945 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1947 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1949 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
1950 SILC_STATUS_ERR_NO_CHANNEL_ID);
1955 /* Get the channels from local list */
1956 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
1959 /* Get the channels from global list if we are router */
1960 if (server->server_type == SILC_ROUTER)
1961 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
1964 /* Send the reply */
1965 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
1966 gchannels, gch_count);
1969 silc_server_command_free(cmd);
1972 /* Server side of TOPIC command. Sets topic for channel and/or returns
1973 current topic to client. */
1975 SILC_SERVER_CMD_FUNC(topic)
1977 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1978 SilcServer server = cmd->server;
1979 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1980 SilcChannelID *channel_id;
1981 SilcChannelEntry channel;
1982 SilcChannelClientEntry chl;
1983 SilcBuffer packet, idp;
1985 unsigned int argc, tmp_len;
1986 unsigned short ident = silc_command_get_ident(cmd->payload);
1988 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_TOPIC, cmd, 1, 2);
1990 argc = silc_argument_get_arg_num(cmd->args);
1992 /* Get Channel ID */
1993 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1995 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
1996 SILC_STATUS_ERR_NO_CHANNEL_ID);
1999 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2001 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2002 SILC_STATUS_ERR_NO_CHANNEL_ID);
2006 /* Check whether the channel exists */
2007 channel = silc_idlist_find_channel_by_id(server->local_list,
2010 channel = silc_idlist_find_channel_by_id(server->global_list,
2013 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2014 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2021 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
2023 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2024 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2028 if (strlen(tmp) > 256) {
2029 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2030 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2034 /* See whether has rights to change topic */
2035 silc_list_start(channel->user_list);
2036 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
2037 if (chl->client == client)
2040 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2041 if (channel->mode & SILC_CHANNEL_MODE_TOPIC) {
2042 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2043 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2048 /* Set the topic for channel */
2050 silc_free(channel->topic);
2051 channel->topic = strdup(tmp);
2053 /* Send TOPIC_SET notify type to the network */
2054 if (!server->standalone)
2055 silc_server_send_notify_topic_set(server, server->router->connection,
2056 server->server_type == SILC_ROUTER ?
2057 TRUE : FALSE, channel, client->id,
2058 SILC_ID_CLIENT_LEN, channel->topic);
2060 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2062 /* Send notify about topic change to all clients on the channel */
2063 silc_server_send_notify_to_channel(server, NULL, channel, TRUE,
2064 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
2065 idp->data, idp->len,
2066 channel->topic, strlen(channel->topic));
2067 silc_buffer_free(idp);
2070 /* Send the topic to client as reply packet */
2071 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2073 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2074 SILC_STATUS_OK, ident, 2,
2075 2, idp->data, idp->len,
2077 strlen(channel->topic));
2079 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2080 SILC_STATUS_OK, ident, 1,
2081 2, idp->data, idp->len);
2082 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2083 0, packet->data, packet->len, FALSE);
2085 silc_buffer_free(packet);
2086 silc_buffer_free(idp);
2087 silc_free(channel_id);
2090 silc_server_command_free(cmd);
2093 /* Server side of INVITE command. Invites some client to join some channel. */
2095 SILC_SERVER_CMD_FUNC(invite)
2097 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2098 SilcServer server = cmd->server;
2099 SilcSocketConnection sock = cmd->sock, dest_sock;
2100 SilcClientEntry sender, dest;
2101 SilcClientID *dest_id;
2102 SilcChannelEntry channel;
2103 SilcChannelID *channel_id;
2108 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_INVITE, cmd, 1, 2);
2110 /* Get destination ID */
2111 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2113 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2114 SILC_STATUS_ERR_NO_CLIENT_ID);
2117 dest_id = silc_id_payload_parse_id(tmp, len);
2119 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2120 SILC_STATUS_ERR_NO_CLIENT_ID);
2124 /* Get Channel ID */
2125 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
2127 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2128 SILC_STATUS_ERR_NO_CHANNEL_ID);
2131 channel_id = silc_id_payload_parse_id(tmp, len);
2133 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2134 SILC_STATUS_ERR_NO_CHANNEL_ID);
2138 /* Check whether the channel exists */
2139 channel = silc_idlist_find_channel_by_id(server->local_list,
2142 channel = silc_idlist_find_channel_by_id(server->global_list,
2145 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2146 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2151 /* Check whether the sender of this command is on the channel. */
2152 sender = (SilcClientEntry)sock->user_data;
2153 if (!silc_server_client_on_channel(sender, channel)) {
2154 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2155 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2159 /* Check whether the channel is invite-only channel. If yes then the
2160 sender of this command must be at least channel operator. */
2161 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2162 SilcChannelClientEntry chl;
2164 silc_list_start(channel->user_list);
2165 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
2166 if (chl->client == sender) {
2167 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2168 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2169 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2176 /* Find the connection data for the destination. If it is local we will
2177 send it directly otherwise we will send it to router for routing. */
2178 dest = silc_idlist_find_client_by_id(server->local_list, dest_id, NULL);
2180 dest_sock = (SilcSocketConnection)dest->connection;
2182 dest_sock = silc_server_route_get(server, dest_id, SILC_ID_CLIENT);
2184 /* Check whether the requested client is already on the channel. */
2185 /* XXX if we are normal server we don't know about global clients on
2186 the channel thus we must request it (USERS command), check from
2187 local cache as well. */
2188 if (silc_server_client_on_channel(dest, channel)) {
2189 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2190 SILC_STATUS_ERR_USER_ON_CHANNEL);
2194 sidp = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
2196 /* Send notify to the client that is invited to the channel */
2197 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
2199 SILC_NOTIFY_TYPE_INVITE, 2,
2200 sidp->data, sidp->len, tmp, len);
2202 /* Send command reply */
2203 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2206 silc_buffer_free(sidp);
2209 silc_server_command_free(cmd);
2214 SilcSocketConnection sock;
2218 /* Quits connection to client. This gets called if client won't
2219 close the connection even when it has issued QUIT command. */
2221 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
2223 QuitInternal q = (QuitInternal)context;
2225 /* Free all client specific data, such as client entry and entires
2226 on channels this client may be on. */
2227 silc_server_free_client_data(q->server, q->sock, q->sock->user_data,
2229 q->sock->user_data = NULL;
2231 /* Close the connection on our side */
2232 silc_server_close_connection(q->server, q->sock);
2234 silc_free(q->signoff);
2238 /* Quits SILC session. This is the normal way to disconnect client. */
2240 SILC_SERVER_CMD_FUNC(quit)
2242 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2243 SilcServer server = cmd->server;
2244 SilcSocketConnection sock = cmd->sock;
2246 unsigned char *tmp = NULL;
2247 unsigned int len = 0;
2249 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_QUIT, cmd, 0, 1);
2251 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2254 /* Get destination ID */
2255 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2259 q = silc_calloc(1, sizeof(*q));
2262 q->signoff = tmp ? strdup(tmp) : NULL;
2264 /* We quit the connection with little timeout */
2265 silc_task_register(server->timeout_queue, sock->sock,
2266 silc_server_command_quit_cb, (void *)q,
2267 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2270 silc_server_command_free(cmd);
2273 /* Server side of command KILL. This command is used by router operator
2274 to remove an client from the SILC Network temporarily. */
2276 SILC_SERVER_CMD_FUNC(kill)
2278 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2279 SilcServer server = cmd->server;
2280 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2281 SilcClientEntry remote_client;
2282 SilcClientID *client_id;
2283 unsigned char *tmp, *comment;
2284 unsigned int tmp_len, tmp_len2;
2286 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_KILL, cmd, 1, 2);
2288 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2291 /* KILL command works only on router */
2292 if (server->server_type != SILC_ROUTER) {
2293 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2294 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2298 /* Check whether client has the permissions. */
2299 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2300 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2301 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2305 /* Get the client ID */
2306 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2308 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2309 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2312 client_id = silc_id_payload_parse_id(tmp, tmp_len);
2314 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2315 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2319 /* Get the client entry */
2320 remote_client = silc_idlist_find_client_by_id(server->local_list,
2322 if (!remote_client) {
2323 remote_client = silc_idlist_find_client_by_id(server->global_list,
2325 if (!remote_client) {
2326 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2327 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2333 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
2337 /* Send reply to the sender */
2338 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2341 /* Send the KILL notify packets. First send it to the channel, then
2342 to our primary router and then directly to the client who is being
2343 killed right now. */
2345 /* Send KILLED notify to the channels. It is not sent to the client
2346 as it will be sent differently destined directly to the client and not
2348 silc_server_send_notify_on_channels(server, remote_client,
2349 remote_client, SILC_NOTIFY_TYPE_KILLED,
2352 comment, comment ? tmp_len2 : 0);
2354 /* Send KILLED notify to primary route */
2355 if (!server->standalone)
2356 silc_server_send_notify_killed(server, server->router->connection, TRUE,
2357 remote_client->id, SILC_ID_CLIENT_LEN,
2360 /* Send KILLED notify to the client directly */
2361 silc_server_send_notify_killed(server, remote_client->connection ?
2362 remote_client->connection :
2363 remote_client->router->connection, FALSE,
2364 remote_client->id, SILC_ID_CLIENT_LEN,
2367 /* Remove the client from all channels. This generates new keys to the
2368 channels as well. */
2369 silc_server_remove_from_channels(server, NULL, remote_client, FALSE,
2372 /* Remove the client entry, If it is locally connected then we will also
2373 disconnect the client here */
2374 if (remote_client->data.registered && remote_client->connection) {
2375 /* Remove locally conneted client */
2376 SilcSocketConnection sock = remote_client->connection;
2377 silc_server_free_client_data(server, sock, remote_client, FALSE, NULL);
2378 silc_server_close_connection(server, sock);
2380 /* Remove remote client */
2381 if (!silc_idlist_del_client(server->global_list, remote_client))
2382 silc_idlist_del_client(server->local_list, remote_client);
2386 silc_server_command_free(cmd);
2389 /* Server side of command INFO. This sends information about us to
2390 the client. If client requested specific server we will send the
2391 command to that server. */
2393 SILC_SERVER_CMD_FUNC(info)
2395 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2396 SilcServer server = cmd->server;
2397 SilcBuffer packet, idp;
2398 char *dest_server, *server_info = NULL, *server_name;
2399 unsigned short ident = silc_command_get_ident(cmd->payload);
2400 SilcServerEntry entry = NULL;
2402 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_INFO, cmd, 1, 1);
2404 /* Get server name */
2405 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2407 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2408 SILC_STATUS_ERR_NO_SUCH_SERVER);
2412 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
2413 /* Send our reply */
2414 char info_string[256];
2416 memset(info_string, 0, sizeof(info_string));
2417 snprintf(info_string, sizeof(info_string),
2418 "location: %s server: %s admin: %s <%s>",
2419 server->config->admin_info->location,
2420 server->config->admin_info->server_type,
2421 server->config->admin_info->admin_name,
2422 server->config->admin_info->admin_email);
2424 server_info = info_string;
2425 entry = server->id_entry;
2427 /* Check whether we have this server cached */
2428 entry = silc_idlist_find_server_by_name(server->global_list,
2431 entry = silc_idlist_find_server_by_name(server->local_list,
2435 if (server->server_type == SILC_ROUTER && entry && !entry->server_info) {
2436 /* Send to the server */
2438 unsigned short old_ident;
2440 old_ident = silc_command_get_ident(cmd->payload);
2441 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2442 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2444 silc_server_packet_send(server, entry->connection,
2445 SILC_PACKET_COMMAND, cmd->packet->flags,
2446 tmpbuf->data, tmpbuf->len, TRUE);
2448 /* Reprocess this packet after received reply from router */
2449 silc_server_command_pending(server, SILC_COMMAND_INFO,
2450 silc_command_get_ident(cmd->payload),
2451 silc_server_command_destructor,
2452 silc_server_command_info,
2453 silc_server_command_dup(cmd));
2454 cmd->pending = TRUE;
2455 silc_command_set_ident(cmd->payload, old_ident);
2456 silc_buffer_free(tmpbuf);
2460 if (!entry && !cmd->pending && !server->standalone) {
2461 /* Send to the primary router */
2463 unsigned short old_ident;
2465 old_ident = silc_command_get_ident(cmd->payload);
2466 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2467 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2469 silc_server_packet_send(server, server->router->connection,
2470 SILC_PACKET_COMMAND, cmd->packet->flags,
2471 tmpbuf->data, tmpbuf->len, TRUE);
2473 /* Reprocess this packet after received reply from router */
2474 silc_server_command_pending(server, SILC_COMMAND_INFO,
2475 silc_command_get_ident(cmd->payload),
2476 silc_server_command_destructor,
2477 silc_server_command_info,
2478 silc_server_command_dup(cmd));
2479 cmd->pending = TRUE;
2480 silc_command_set_ident(cmd->payload, old_ident);
2481 silc_buffer_free(tmpbuf);
2487 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2488 SILC_STATUS_ERR_NO_SUCH_SERVER);
2492 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2494 server_info = entry->server_info;
2495 server_name = dest_server;
2497 /* Send the reply */
2498 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2499 SILC_STATUS_OK, ident, 3,
2500 2, idp->data, idp->len,
2502 strlen(server_name),
2504 strlen(server_info));
2505 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2506 packet->data, packet->len, FALSE);
2508 silc_buffer_free(packet);
2509 silc_buffer_free(idp);
2512 silc_server_command_free(cmd);
2515 /* Server side of command PING. This just replies to the ping. */
2517 SILC_SERVER_CMD_FUNC(ping)
2519 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2520 SilcServer server = cmd->server;
2525 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_INFO, cmd, 1, 2);
2528 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2530 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2531 SILC_STATUS_ERR_NO_SERVER_ID);
2534 id = silc_id_str2id(tmp, len, SILC_ID_SERVER);
2538 if (!SILC_ID_SERVER_COMPARE(id, server->id)) {
2539 /* Send our reply */
2540 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2543 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2544 SILC_STATUS_ERR_NO_SUCH_SERVER);
2551 silc_server_command_free(cmd);
2554 /* Internal routine to join channel. The channel sent to this function
2555 has been either created or resolved from ID lists. This joins the sent
2556 client to the channel. */
2558 static void silc_server_command_join_channel(SilcServer server,
2559 SilcServerCommandContext cmd,
2560 SilcChannelEntry channel,
2561 SilcClientID *client_id,
2565 SilcSocketConnection sock = cmd->sock;
2567 unsigned int tmp_len, user_count;
2568 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
2569 SilcClientEntry client;
2570 SilcChannelClientEntry chl;
2571 SilcBuffer reply, chidp, clidp, keyp, user_list, mode_list;
2572 unsigned short ident = silc_command_get_ident(cmd->payload);
2575 SILC_LOG_DEBUG(("Start"));
2580 /* Get the client entry */
2581 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2582 client = (SilcClientEntry)sock->user_data;
2584 client = silc_idlist_find_client_by_id(server->local_list, client_id,
2591 * Check channel modes
2594 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2595 strncat(check, client->nickname, strlen(client->nickname));
2596 if (!strchr(client->nickname, '@')) {
2597 strncat(check, "@", 1);
2598 strncat(check, server->server_name, strlen(server->server_name));
2600 strncat(check, "!", 1);
2601 strncat(check, client->username, strlen(client->username));
2602 if (!strchr(client->username, '@')) {
2603 strncat(check, "@", 1);
2604 strncat(check, cmd->sock->hostname, strlen(cmd->sock->hostname));
2608 /* Check invite list if channel is invite-only channel */
2609 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
2610 channel->mode & SILC_CHANNEL_MODE_INVITE && channel->invite_list) {
2611 if (!silc_string_match(channel->invite_list, check)) {
2612 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2613 SILC_STATUS_ERR_NOT_INVITED);
2618 /* Check ban list if it exists. If the client's nickname, server,
2619 username and/or hostname is in the ban list the access to the
2620 channel is denied. */
2621 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT && channel->ban_list) {
2622 if (silc_string_match(channel->ban_list, check)) {
2623 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2624 SILC_STATUS_ERR_BANNED_FROM_CHANNEL);
2629 /* Get passphrase */
2630 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
2632 passphrase = silc_calloc(tmp_len, sizeof(*passphrase));
2633 memcpy(passphrase, tmp, tmp_len);
2636 /* Check the channel passphrase if set. */
2637 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2638 if (!passphrase || memcmp(channel->passphrase, passphrase,
2639 strlen(channel->passphrase))) {
2640 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2641 SILC_STATUS_ERR_BAD_PASSWORD);
2646 /* Check user count limit if set. */
2647 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
2648 if (silc_list_count(channel->user_list) + 1 >
2649 channel->user_limit) {
2650 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2651 SILC_STATUS_ERR_CHANNEL_IS_FULL);
2657 * Client is allowed to join to the channel. Make it happen.
2660 /* Check whether the client already is on the channel */
2661 if (silc_server_client_on_channel(client, channel)) {
2662 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2663 SILC_STATUS_ERR_USER_ON_CHANNEL);
2667 /* Generate new channel key as protocol dictates */
2668 if ((!created && silc_list_count(channel->user_list) > 0) ||
2669 !channel->channel_key)
2670 silc_server_create_channel_key(server, channel, 0);
2672 /* Send the channel key. This is broadcasted to the channel but is not
2673 sent to the client who is joining to the channel. */
2674 silc_server_send_channel_key(server, NULL, channel,
2675 server->server_type == SILC_ROUTER ?
2676 FALSE : !server->standalone);
2678 /* Join the client to the channel by adding it to channel's user list.
2679 Add also the channel to client entry's channels list for fast cross-
2681 chl = silc_calloc(1, sizeof(*chl));
2683 chl->client = client;
2684 chl->channel = channel;
2685 silc_list_add(channel->user_list, chl);
2686 silc_list_add(client->channels, chl);
2688 /* Get users on the channel */
2689 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2692 /* Encode Client ID Payload of the original client who wants to join */
2693 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2695 /* Encode command reply packet */
2696 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2697 SILC_PUT32_MSB(channel->mode, mode);
2698 SILC_PUT32_MSB(created, tmp2);
2699 SILC_PUT32_MSB(user_count, tmp3);
2700 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2701 keyp = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, tmp,
2702 strlen(channel->channel_key->
2704 channel->channel_key->cipher->name,
2705 channel->key_len / 8, channel->key);
2708 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2709 SILC_STATUS_OK, ident, 13,
2710 2, channel->channel_name,
2711 strlen(channel->channel_name),
2712 3, chidp->data, chidp->len,
2713 4, clidp->data, clidp->len,
2716 7, keyp->data, keyp->len,
2717 8, channel->ban_list,
2719 strlen(channel->ban_list) : 0,
2720 9, channel->invite_list,
2721 channel->invite_list ?
2722 strlen(channel->invite_list) : 0,
2725 strlen(channel->topic) : 0,
2726 11, channel->hmac->hmac->name,
2727 strlen(channel->hmac->hmac->name),
2729 13, user_list->data, user_list->len,
2730 14, mode_list->data,
2733 /* Send command reply */
2734 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2735 reply->data, reply->len, FALSE);
2737 if (!cmd->pending) {
2738 /* Send JOIN notify to locally connected clients on the channel */
2739 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2740 SILC_NOTIFY_TYPE_JOIN, 2,
2741 clidp->data, clidp->len,
2742 chidp->data, chidp->len);
2744 /* Send JOIN notify packet to our primary router */
2745 if (!server->standalone)
2746 silc_server_send_notify_join(server, server->router->connection,
2747 server->server_type == SILC_ROUTER ?
2748 TRUE : FALSE, channel, client->id,
2749 SILC_ID_CLIENT_LEN);
2752 silc_buffer_free(reply);
2753 silc_buffer_free(clidp);
2754 silc_buffer_free(chidp);
2755 silc_buffer_free(keyp);
2756 silc_buffer_free(user_list);
2757 silc_buffer_free(mode_list);
2761 silc_free(passphrase);
2764 /* Server side of command JOIN. Joins client into requested channel. If
2765 the channel does not exist it will be created. */
2767 SILC_SERVER_CMD_FUNC(join)
2769 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2770 SilcServer server = cmd->server;
2772 char *tmp, *channel_name = NULL, *cipher, *hmac;
2773 SilcChannelEntry channel;
2774 unsigned int umode = 0;
2775 int created = FALSE;
2776 SilcClientID *client_id;
2778 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_JOIN, cmd, 1, 4);
2780 /* Get channel name */
2781 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2783 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2784 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2789 if (strlen(channel_name) > 256)
2790 channel_name[255] = '\0';
2792 if (silc_server_command_bad_chars(channel_name) == TRUE) {
2793 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2794 SILC_STATUS_ERR_BAD_CHANNEL);
2795 silc_free(channel_name);
2799 /* Get Client ID of the client who is joining to the channel */
2800 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2802 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2803 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2806 client_id = silc_id_payload_parse_id(tmp, tmp_len);
2808 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2809 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2813 /* Get cipher and hmac name */
2814 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
2815 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
2817 /* See if the channel exists */
2818 channel = silc_idlist_find_channel_by_name(server->local_list,
2819 channel_name, NULL);
2821 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2822 /* If this is coming from client the Client ID in the command packet must
2823 be same as the client's ID. */
2824 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2825 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
2826 if (SILC_ID_CLIENT_COMPARE(entry->id, client_id)) {
2827 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2828 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2834 /* Channel not found */
2836 /* If we are standalone server we don't have a router, we just create
2837 the channel by ourselves. */
2838 if (server->standalone) {
2839 channel = silc_server_create_new_channel(server, server->id, cipher,
2840 hmac, channel_name, TRUE);
2841 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2846 /* The channel does not exist on our server. If we are normal server
2847 we will send JOIN command to our router which will handle the
2848 joining procedure (either creates the channel if it doesn't exist
2849 or joins the client to it). */
2850 if (server->server_type == SILC_SERVER) {
2852 unsigned short old_ident;
2854 old_ident = silc_command_get_ident(cmd->payload);
2855 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2856 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2858 /* Send JOIN command to our router */
2859 silc_server_packet_send(server, (SilcSocketConnection)
2860 server->router->connection,
2861 SILC_PACKET_COMMAND, cmd->packet->flags,
2862 tmpbuf->data, tmpbuf->len, TRUE);
2864 /* Reprocess this packet after received reply from router */
2865 silc_server_command_pending(server, SILC_COMMAND_JOIN,
2866 silc_command_get_ident(cmd->payload),
2867 silc_server_command_destructor,
2868 silc_server_command_join,
2869 silc_server_command_dup(cmd));
2870 cmd->pending = TRUE;
2874 /* We are router and the channel does not seem exist so we will check
2875 our global list as well for the channel. */
2876 channel = silc_idlist_find_channel_by_name(server->global_list,
2877 channel_name, NULL);
2879 /* Channel really does not exist, create it */
2880 channel = silc_server_create_new_channel(server, server->id, cipher,
2881 hmac, channel_name, TRUE);
2882 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2889 /* Channel not found */
2891 /* If the command came from router and/or we are normal server then
2892 something went wrong with the joining as the channel was not found.
2893 We can't do anything else but ignore this. */
2894 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
2895 server->server_type == SILC_SERVER)
2898 /* We are router and the channel does not seem exist so we will check
2899 our global list as well for the channel. */
2900 channel = silc_idlist_find_channel_by_name(server->global_list,
2901 channel_name, NULL);
2903 /* Channel really does not exist, create it */
2904 channel = silc_server_create_new_channel(server, server->id, cipher,
2905 hmac, channel_name, TRUE);
2906 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2912 /* If the channel does not have global users and is also empty it means the
2913 channel was created globally (by our router) and the client will be the
2914 channel founder and operator. */
2915 if (!channel->global_users && silc_list_count(channel->user_list) == 0) {
2916 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2917 created = TRUE; /* Created globally by our router */
2920 /* Join to the channel */
2921 silc_server_command_join_channel(server, cmd, channel, client_id,
2924 silc_free(client_id);
2927 silc_server_command_free(cmd);
2930 /* Server side of command MOTD. Sends server's current "message of the
2931 day" to the client. */
2933 SILC_SERVER_CMD_FUNC(motd)
2935 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2936 SilcServer server = cmd->server;
2937 SilcBuffer packet, idp;
2938 char *motd, *dest_server;
2940 unsigned short ident = silc_command_get_ident(cmd->payload);
2942 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_MOTD, cmd, 1, 1);
2944 /* Get server name */
2945 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2947 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2948 SILC_STATUS_ERR_NO_SUCH_SERVER);
2952 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
2955 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
2957 if (server->config && server->config->motd &&
2958 server->config->motd->motd_file) {
2960 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
2965 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2966 SILC_STATUS_OK, ident, 2,
2972 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2973 SILC_STATUS_OK, ident, 1,
2977 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2978 packet->data, packet->len, FALSE);
2979 silc_buffer_free(packet);
2980 silc_buffer_free(idp);
2982 SilcServerEntry entry;
2984 /* Check whether we have this server cached */
2985 entry = silc_idlist_find_server_by_name(server->global_list,
2988 entry = silc_idlist_find_server_by_name(server->local_list,
2992 if (server->server_type == SILC_ROUTER && !cmd->pending &&
2993 entry && !entry->motd) {
2994 /* Send to the server */
2996 unsigned short old_ident;
2998 old_ident = silc_command_get_ident(cmd->payload);
2999 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3000 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3002 silc_server_packet_send(server, entry->connection,
3003 SILC_PACKET_COMMAND, cmd->packet->flags,
3004 tmpbuf->data, tmpbuf->len, TRUE);
3006 /* Reprocess this packet after received reply from router */
3007 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3008 silc_command_get_ident(cmd->payload),
3009 silc_server_command_destructor,
3010 silc_server_command_motd,
3011 silc_server_command_dup(cmd));
3012 cmd->pending = TRUE;
3013 silc_command_set_ident(cmd->payload, old_ident);
3014 silc_buffer_free(tmpbuf);
3018 if (!entry && !cmd->pending && !server->standalone) {
3019 /* Send to the primary router */
3021 unsigned short old_ident;
3023 old_ident = silc_command_get_ident(cmd->payload);
3024 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3025 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3027 silc_server_packet_send(server, server->router->connection,
3028 SILC_PACKET_COMMAND, cmd->packet->flags,
3029 tmpbuf->data, tmpbuf->len, TRUE);
3031 /* Reprocess this packet after received reply from router */
3032 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3033 silc_command_get_ident(cmd->payload),
3034 silc_server_command_destructor,
3035 silc_server_command_motd,
3036 silc_server_command_dup(cmd));
3037 cmd->pending = TRUE;
3038 silc_command_set_ident(cmd->payload, old_ident);
3039 silc_buffer_free(tmpbuf);
3044 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3045 SILC_STATUS_ERR_NO_SUCH_SERVER);
3049 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3052 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3053 SILC_STATUS_OK, ident, 2,
3056 strlen(entry->motd));
3058 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3059 SILC_STATUS_OK, ident, 1,
3062 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3063 packet->data, packet->len, FALSE);
3064 silc_buffer_free(packet);
3065 silc_buffer_free(idp);
3069 silc_server_command_free(cmd);
3072 /* Server side of command UMODE. Client can use this command to set/unset
3073 user mode. Client actually cannot set itself to be as server/router
3074 operator so this can be used only to unset the modes. */
3076 SILC_SERVER_CMD_FUNC(umode)
3078 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3079 SilcServer server = cmd->server;
3080 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3082 unsigned char *tmp_mask;
3084 unsigned short ident = silc_command_get_ident(cmd->payload);
3086 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3089 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_UMODE, cmd, 2, 2);
3091 /* Get the client's mode mask */
3092 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3094 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3095 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3098 SILC_GET32_MSB(mask, tmp_mask);
3104 if (mask & SILC_UMODE_SERVER_OPERATOR) {
3105 /* Cannot operator mode */
3106 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3107 SILC_STATUS_ERR_PERM_DENIED);
3110 if (client->mode & SILC_UMODE_SERVER_OPERATOR)
3111 /* Remove the server operator rights */
3112 client->mode &= ~SILC_UMODE_SERVER_OPERATOR;
3115 if (mask & SILC_UMODE_ROUTER_OPERATOR) {
3116 /* Cannot operator mode */
3117 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3118 SILC_STATUS_ERR_PERM_DENIED);
3121 if (client->mode & SILC_UMODE_ROUTER_OPERATOR)
3122 /* Remove the router operator rights */
3123 client->mode &= ~SILC_UMODE_ROUTER_OPERATOR;
3126 /* Send UMODE change to primary router */
3127 if (!server->standalone)
3128 silc_server_send_notify_umode(server, server->router->connection, TRUE,
3129 client->id, SILC_ID_CLIENT_LEN,
3132 /* Send command reply to sender */
3133 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
3134 SILC_STATUS_OK, ident, 1,
3136 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3137 packet->data, packet->len, FALSE);
3138 silc_buffer_free(packet);
3141 silc_server_command_free(cmd);
3144 /* Checks that client has rights to add or remove channel modes. If any
3145 of the checks fails FALSE is returned. */
3147 int silc_server_check_cmode_rights(SilcChannelEntry channel,
3148 SilcChannelClientEntry client,
3151 int is_op = client->mode & SILC_CHANNEL_UMODE_CHANOP;
3152 int is_fo = client->mode & SILC_CHANNEL_UMODE_CHANFO;
3154 /* Check whether has rights to change anything */
3155 if (!is_op && !is_fo)
3158 /* Check whether has rights to change everything */
3162 /* We know that client is channel operator, check that they are not
3163 changing anything that requires channel founder rights. Rest of the
3164 modes are available automatically for channel operator. */
3166 if (mode & SILC_CHANNEL_MODE_PRIVKEY) {
3167 if (is_op && !is_fo)
3170 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3171 if (is_op && !is_fo)
3176 if (mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3177 if (is_op && !is_fo)
3180 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3181 if (is_op && !is_fo)
3186 if (mode & SILC_CHANNEL_MODE_CIPHER) {
3187 if (is_op && !is_fo)
3190 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3191 if (is_op && !is_fo)
3199 /* Server side command of CMODE. Changes channel mode */
3201 SILC_SERVER_CMD_FUNC(cmode)
3203 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3204 SilcServer server = cmd->server;
3205 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3206 SilcChannelID *channel_id;
3207 SilcChannelEntry channel;
3208 SilcChannelClientEntry chl;
3209 SilcBuffer packet, cidp;
3210 unsigned char *tmp, *tmp_id, *tmp_mask;
3211 unsigned int argc, mode_mask, tmp_len, tmp_len2;
3212 unsigned short ident = silc_command_get_ident(cmd->payload);
3214 SILC_LOG_DEBUG(("Start"));
3216 argc = silc_argument_get_arg_num(cmd->args);
3218 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3219 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3223 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3224 SILC_STATUS_ERR_TOO_MANY_PARAMS);
3228 /* Get Channel ID */
3229 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
3231 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3232 SILC_STATUS_ERR_NO_CHANNEL_ID);
3235 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2);
3237 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3238 SILC_STATUS_ERR_NO_CHANNEL_ID);
3242 /* Get the channel mode mask */
3243 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3245 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3246 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3249 SILC_GET32_MSB(mode_mask, tmp_mask);
3251 /* Get channel entry */
3252 channel = silc_idlist_find_channel_by_id(server->local_list,
3255 channel = silc_idlist_find_channel_by_id(server->global_list,
3258 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3259 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3264 /* Check whether this client is on the channel */
3265 if (!silc_server_client_on_channel(client, channel)) {
3266 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3267 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3271 /* Get entry to the channel user list */
3272 silc_list_start(channel->user_list);
3273 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
3274 if (chl->client == client)
3277 /* Check that client has rights to change any requested channel modes */
3278 if (!silc_server_check_cmode_rights(channel, chl, mode_mask)) {
3279 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3280 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3285 * Check the modes. Modes that requires nothing special operation are
3289 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
3290 /* Channel uses private keys to protect traffic. Client(s) has set the
3291 key locally they want to use, server does not know that key. */
3292 /* Nothing interesting to do here now */
3294 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3295 /* The mode is removed and we need to generate and distribute
3296 new channel key. Clients are not using private channel keys
3297 anymore after this. */
3299 /* XXX Duplicated code, make own function for this!! LEAVE uses this
3302 /* Re-generate channel key */
3303 silc_server_create_channel_key(server, channel, 0);
3305 /* Encode channel key payload to be distributed on the channel */
3307 silc_channel_key_payload_encode(tmp_len2, tmp_id,
3308 strlen(channel->channel_key->
3310 channel->channel_key->cipher->name,
3311 channel->key_len / 8, channel->key);
3313 /* If we are normal server then we will send it to our router. If we
3314 are router we will send it to all local servers that has clients on
3316 if (server->server_type == SILC_SERVER) {
3317 if (!server->standalone)
3318 silc_server_packet_send(server,
3319 cmd->server->router->connection,
3320 SILC_PACKET_CHANNEL_KEY, 0, packet->data,
3326 /* Send to locally connected clients on the channel */
3327 silc_server_packet_send_local_channel(server, channel,
3328 SILC_PACKET_CHANNEL_KEY, 0,
3329 packet->data, packet->len, FALSE);
3330 silc_buffer_free(packet);
3334 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
3335 /* User limit is set on channel */
3336 unsigned int user_limit;
3338 /* Get user limit */
3339 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
3341 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
3342 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3343 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3347 SILC_GET32_MSB(user_limit, tmp);
3348 channel->user_limit = user_limit;
3351 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
3352 /* User limit mode is unset. Remove user limit */
3353 channel->user_limit = 0;
3356 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
3357 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
3358 /* Passphrase has been set to channel */
3360 /* Get the passphrase */
3361 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
3363 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3364 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3368 /* Save the passphrase */
3369 channel->passphrase = strdup(tmp);
3372 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3373 /* Passphrase mode is unset. remove the passphrase */
3374 if (channel->passphrase) {
3375 silc_free(channel->passphrase);
3376 channel->passphrase = NULL;
3381 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
3382 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
3383 /* Cipher to use protect the traffic */
3384 unsigned int key_len;
3387 tmp = silc_argument_get_arg_type(cmd->args, 8, NULL);
3389 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3390 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3394 /* XXX Duplicated code, make own function for this!! */
3396 /* Delete old cipher and allocate the new one */
3397 silc_cipher_free(channel->channel_key);
3398 if (!silc_cipher_alloc(tmp, &channel->channel_key)) {
3399 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3400 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3403 key_len = silc_cipher_get_key_len(channel->channel_key) / 8;
3405 /* Re-generate channel key */
3406 silc_server_create_channel_key(server, channel, key_len);
3408 /* Encode channel key payload to be distributed on the channel */
3410 silc_channel_key_payload_encode(tmp_len2, tmp_id,
3411 strlen(channel->channel_key->
3413 channel->channel_key->cipher->name,
3414 channel->key_len / 8, channel->key);
3416 /* If we are normal server then we will send it to our router. If we
3417 are router we will send it to all local servers that has clients on
3419 if (server->server_type == SILC_SERVER) {
3420 if (!server->standalone)
3421 silc_server_packet_send(server,
3422 cmd->server->router->connection,
3423 SILC_PACKET_CHANNEL_KEY, 0, packet->data,
3429 /* Send to locally connected clients on the channel */
3430 silc_server_packet_send_local_channel(server, channel,
3431 SILC_PACKET_CHANNEL_KEY, 0,
3432 packet->data, packet->len, FALSE);
3433 silc_buffer_free(packet);
3436 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3437 /* Cipher mode is unset. Remove the cipher and revert back to
3439 char *cipher = channel->channel_key->cipher->name;
3441 /* Generate new cipher and key for the channel */
3443 /* XXX Duplicated code, make own function for this!! */
3445 /* Delete old cipher and allocate default one */
3446 silc_cipher_free(channel->channel_key);
3447 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3448 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3449 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3453 /* Re-generate channel key */
3454 silc_server_create_channel_key(server, channel, 0);
3456 /* Encode channel key payload to be distributed on the channel */
3458 silc_channel_key_payload_encode(tmp_len2, tmp_id,
3459 strlen(channel->channel_key->
3461 channel->channel_key->cipher->name,
3462 channel->key_len / 8, channel->key);
3464 /* If we are normal server then we will send it to our router. If we
3465 are router we will send it to all local servers that has clients on
3467 if (server->server_type == SILC_SERVER) {
3468 if (!server->standalone)
3469 silc_server_packet_send(server,
3470 cmd->server->router->connection,
3471 SILC_PACKET_CHANNEL_KEY, 0, packet->data,
3477 /* Send to locally connected clients on the channel */
3478 silc_server_packet_send_local_channel(server, channel,
3479 SILC_PACKET_CHANNEL_KEY, 0,
3480 packet->data, packet->len, FALSE);
3481 silc_buffer_free(packet);
3485 /* Finally, set the mode */
3486 channel->mode = mode_mask;
3488 /* Send CMODE_CHANGE notify */
3489 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3490 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3491 SILC_NOTIFY_TYPE_CMODE_CHANGE, 2,
3492 cidp->data, cidp->len,
3495 /* Set CMODE notify type to network */
3496 if (!server->standalone)
3497 silc_server_send_notify_cmode(server, server->router->connection,
3498 server->server_type == SILC_ROUTER ?
3499 TRUE : FALSE, channel,
3500 mode_mask, client->id, SILC_ID_CLIENT_LEN);
3502 /* Send command reply to sender */
3503 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3504 SILC_STATUS_OK, ident, 1,
3506 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3507 packet->data, packet->len, FALSE);
3509 silc_buffer_free(packet);
3510 silc_free(channel_id);
3514 silc_server_command_free(cmd);
3517 /* Server side of CUMODE command. Changes client's mode on a channel. */
3519 SILC_SERVER_CMD_FUNC(cumode)
3521 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3522 SilcServer server = cmd->server;
3523 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3524 SilcChannelID *channel_id;
3525 SilcClientID *client_id;
3526 SilcChannelEntry channel;
3527 SilcClientEntry target_client;
3528 SilcChannelClientEntry chl;
3529 SilcBuffer packet, idp;
3530 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3531 unsigned int target_mask, sender_mask, tmp_len, tmp_ch_len;
3533 unsigned short ident = silc_command_get_ident(cmd->payload);
3535 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CUMODE, cmd, 3, 3);
3537 /* Get Channel ID */
3538 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3540 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3541 SILC_STATUS_ERR_NO_CHANNEL_ID);
3544 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len);
3546 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3547 SILC_STATUS_ERR_NO_CHANNEL_ID);
3551 /* Get channel entry */
3552 channel = silc_idlist_find_channel_by_id(server->local_list,
3555 channel = silc_idlist_find_channel_by_id(server->global_list,
3558 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3559 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3564 /* Check whether sender is on the channel */
3565 if (!silc_server_client_on_channel(client, channel)) {
3566 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3567 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3571 /* Check that client has rights to change other's rights */
3572 silc_list_start(channel->user_list);
3573 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
3574 if (chl->client == client) {
3575 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
3576 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
3577 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3578 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3582 sender_mask = chl->mode;
3587 /* Get the target client's channel mode mask */
3588 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3590 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3591 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3594 SILC_GET32_MSB(target_mask, tmp_mask);
3596 /* Get target Client ID */
3597 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3599 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3600 SILC_STATUS_ERR_NO_CLIENT_ID);
3603 client_id = silc_id_payload_parse_id(tmp_id, tmp_len);
3605 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3606 SILC_STATUS_ERR_NO_CLIENT_ID);
3610 /* Get target client's entry */
3611 target_client = silc_idlist_find_client_by_id(server->local_list,
3613 if (!target_client) {
3614 target_client = silc_idlist_find_client_by_id(server->global_list,
3618 /* Check whether target client is on the channel */
3619 if (!silc_server_client_on_channel(target_client, channel)) {
3620 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3621 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
3625 /* Get entry to the channel user list */
3626 silc_list_start(channel->user_list);
3627 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
3628 if (chl->client == target_client)
3635 /* If the target client is founder, no one else can change their mode
3637 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && chl->client != target_client) {
3638 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3639 SILC_STATUS_ERR_NOT_YOU);
3643 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3644 /* Cannot promote anyone to channel founder */
3645 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3646 SILC_STATUS_ERR_NOT_YOU);
3649 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3650 if (target_client == client) {
3651 /* Remove channel founder rights from itself */
3652 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3655 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3656 SILC_STATUS_ERR_NOT_YOU);
3662 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
3663 /* Promote to operator */
3664 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
3665 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
3669 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
3670 /* Demote to normal user */
3671 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
3676 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3678 /* Send notify to channel, notify only if mode was actually changed. */
3680 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3681 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
3682 idp->data, idp->len,
3686 /* Set CUMODE notify type to network */
3687 if (!server->standalone)
3688 silc_server_send_notify_cumode(server, server->router->connection,
3689 server->server_type == SILC_ROUTER ?
3690 TRUE : FALSE, channel,
3691 target_mask, client->id,
3694 SILC_ID_CLIENT_LEN);
3697 /* Send command reply to sender */
3698 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
3699 SILC_STATUS_OK, ident, 2,
3701 3, tmp_id, tmp_len);
3702 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3703 packet->data, packet->len, FALSE);
3705 silc_buffer_free(packet);
3706 silc_free(channel_id);
3707 silc_free(client_id);
3708 silc_buffer_free(idp);
3711 silc_server_command_free(cmd);
3714 /* Server side of KICK command. Kicks client out of channel. */
3716 SILC_SERVER_CMD_FUNC(kick)
3718 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3719 SilcServer server = cmd->server;
3720 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3721 SilcClientEntry target_client;
3722 SilcChannelID *channel_id;
3723 SilcClientID *client_id;
3724 SilcChannelEntry channel;
3725 SilcChannelClientEntry chl;
3727 unsigned int tmp_len;
3728 unsigned char *tmp, *comment;
3730 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_LEAVE, cmd, 1, 3);
3732 /* Get Channel ID */
3733 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3735 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3736 SILC_STATUS_ERR_NO_CHANNEL_ID);
3739 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
3741 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3742 SILC_STATUS_ERR_NO_CHANNEL_ID);
3746 /* Get channel entry */
3747 channel = silc_idlist_find_channel_by_id(server->local_list,
3750 channel = silc_idlist_find_channel_by_id(server->local_list,
3753 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3754 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3759 /* Check whether sender is on the channel */
3760 if (!silc_server_client_on_channel(client, channel)) {
3761 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3762 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3766 /* Check that the kicker is channel operator or channel founder */
3767 silc_list_start(channel->user_list);
3768 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
3769 if (chl->client == client) {
3770 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
3771 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3772 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3779 /* Get target Client ID */
3780 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3782 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3783 SILC_STATUS_ERR_NO_CLIENT_ID);
3786 client_id = silc_id_payload_parse_id(tmp, tmp_len);
3788 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3789 SILC_STATUS_ERR_NO_CLIENT_ID);
3793 /* Get target client's entry */
3794 target_client = silc_idlist_find_client_by_id(server->local_list,
3796 if (!target_client) {
3797 target_client = silc_idlist_find_client_by_id(server->global_list,
3801 /* Check that the target client is not channel founder. Channel founder
3802 cannot be kicked from the channel. */
3803 silc_list_start(channel->user_list);
3804 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
3805 if (chl->client == target_client) {
3806 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3807 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3808 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV);
3815 /* Check whether target client is on the channel */
3816 if (!silc_server_client_on_channel(target_client, channel)) {
3817 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3818 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
3824 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3828 /* Send command reply to sender */
3829 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3832 /* Send KICKED notify to local clients on the channel */
3833 idp = silc_id_payload_encode(target_client->id, SILC_ID_CLIENT);
3834 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3835 SILC_NOTIFY_TYPE_KICKED,
3837 idp->data, idp->len,
3838 comment, comment ? strlen(comment) : 0);
3839 silc_buffer_free(idp);
3841 /* Remove the client from the channel. If the channel does not exist
3842 after removing the client then the client kicked itself off the channel
3843 and we don't have to send anything after that. */
3844 if (!silc_server_remove_from_one_channel(server, NULL, channel,
3845 target_client, FALSE))
3848 /* Send KICKED notify to primary route */
3849 if (!server->standalone)
3850 silc_server_send_notify_kicked(server, server->router->connection,
3851 server->server_type == SILC_ROUTER ?
3852 TRUE : FALSE, channel,
3853 target_client->id, SILC_ID_CLIENT_LEN,
3856 /* Re-generate channel key */
3857 silc_server_create_channel_key(server, channel, 0);
3859 /* Send the channel key to the channel. The key of course is not sent
3860 to the client who was kicked off the channel. */
3861 silc_server_send_channel_key(server, target_client->connection, channel,
3862 server->server_type == SILC_ROUTER ?
3863 FALSE : !server->standalone);
3866 silc_server_command_free(cmd);
3869 /* Server side of OPER command. Client uses this comand to obtain server
3870 operator privileges to this server/router. */
3872 SILC_SERVER_CMD_FUNC(oper)
3874 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3875 SilcServer server = cmd->server;
3876 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3877 unsigned char *username, *auth;
3878 unsigned int tmp_len;
3879 SilcServerConfigSectionAdminConnection *admin;
3880 SilcIDListData idata = (SilcIDListData)client;
3882 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_OPER, cmd, 1, 2);
3884 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3887 /* Get the username */
3888 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3890 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3891 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3895 /* Get the admin configuration */
3896 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
3897 username, client->nickname);
3899 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
3900 username, client->nickname);
3902 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3903 SILC_STATUS_ERR_AUTH_FAILED);
3908 /* Get the authentication payload */
3909 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3911 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3912 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3916 /* Verify the authentication data */
3917 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
3918 admin->auth_data, admin->auth_data_len,
3919 idata->hash, client->id, SILC_ID_CLIENT)) {
3920 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3921 SILC_STATUS_ERR_AUTH_FAILED);
3925 /* Client is now server operator */
3926 client->mode |= SILC_UMODE_SERVER_OPERATOR;
3928 /* Send UMODE change to primary router */
3929 if (!server->standalone)
3930 silc_server_send_notify_umode(server, server->router->connection, TRUE,
3931 client->id, SILC_ID_CLIENT_LEN,
3934 /* Send reply to the sender */
3935 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3939 silc_server_command_free(cmd);
3942 /* Server side of SILCOPER command. Client uses this comand to obtain router
3943 operator privileges to this router. */
3945 SILC_SERVER_CMD_FUNC(silcoper)
3947 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3948 SilcServer server = cmd->server;
3949 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3950 unsigned char *username, *auth;
3951 unsigned int tmp_len;
3952 SilcServerConfigSectionAdminConnection *admin;
3953 SilcIDListData idata = (SilcIDListData)client;
3955 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_SILCOPER, cmd, 1, 2);
3957 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3960 /* Get the username */
3961 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3963 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
3964 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3968 /* Get the admin configuration */
3969 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
3970 username, client->nickname);
3972 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
3973 username, client->nickname);
3975 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
3976 SILC_STATUS_ERR_AUTH_FAILED);
3981 /* Get the authentication payload */
3982 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3984 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
3985 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3989 /* Verify the authentication data */
3990 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
3991 admin->auth_data, admin->auth_data_len,
3992 idata->hash, client->id, SILC_ID_CLIENT)) {
3993 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
3994 SILC_STATUS_ERR_AUTH_FAILED);
3998 /* Client is now router operator */
3999 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4001 /* Send UMODE change to primary router */
4002 if (!server->standalone)
4003 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4004 client->id, SILC_ID_CLIENT_LEN,
4007 /* Send reply to the sender */
4008 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4012 silc_server_command_free(cmd);
4015 /* Server side command of CONNECT. Connects us to the specified remote
4016 server or router. */
4018 SILC_SERVER_CMD_FUNC(connect)
4020 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4021 SilcServer server = cmd->server;
4022 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4023 unsigned char *tmp, *host;
4024 unsigned int tmp_len;
4025 unsigned int port = SILC_PORT;
4027 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CONNECT, cmd, 1, 2);
4029 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4032 /* Check whether client has the permissions. */
4033 if (client->mode == SILC_UMODE_NONE) {
4034 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4035 SILC_STATUS_ERR_NO_SERVER_PRIV);
4039 if (server->server_type == SILC_ROUTER &&
4040 client->mode & SILC_UMODE_SERVER_OPERATOR) {
4041 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4042 SILC_STATUS_ERR_NO_ROUTER_PRIV);
4046 /* Get the remote server */
4047 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4049 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4050 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4055 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4057 SILC_GET32_MSB(port, tmp);
4059 /* Create the connection. It is done with timeout and is async. */
4060 silc_server_create_connection(server, host, port);
4062 /* Send reply to the sender */
4063 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4067 silc_server_command_free(cmd);
4070 SILC_SERVER_CMD_FUNC(restart)
4074 /* Server side command of CLOSE. Closes connection to a specified server. */
4076 SILC_SERVER_CMD_FUNC(close)
4078 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4079 SilcServer server = cmd->server;
4080 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4081 SilcServerEntry server_entry;
4082 SilcSocketConnection sock;
4084 unsigned int tmp_len;
4085 unsigned char *name;
4086 unsigned int port = SILC_PORT;
4088 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_CLOSE, cmd, 1, 2);
4090 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4093 /* Check whether client has the permissions. */
4094 if (client->mode == SILC_UMODE_NONE) {
4095 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4096 SILC_STATUS_ERR_NO_SERVER_PRIV);
4100 /* Get the remote server */
4101 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4103 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4104 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4109 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4111 SILC_GET32_MSB(port, tmp);
4113 server_entry = silc_idlist_find_server_by_conn(server->local_list,
4115 if (!server_entry) {
4116 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4117 SILC_STATUS_ERR_NO_SERVER_ID);
4121 /* Send reply to the sender */
4122 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4125 /* Close the connection to the server */
4126 sock = (SilcSocketConnection)server_entry->connection;
4127 silc_server_free_sock_user_data(server, sock);
4128 silc_server_close_connection(server, sock);
4131 silc_server_command_free(cmd);
4134 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4135 active connections. */
4137 SILC_SERVER_CMD_FUNC(shutdown)
4139 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4140 SilcServer server = cmd->server;
4141 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4143 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_SHUTDOWN, cmd, 0, 0);
4145 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4148 /* Check whether client has the permission. */
4149 if (client->mode == SILC_UMODE_NONE) {
4150 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4151 SILC_STATUS_ERR_NO_SERVER_PRIV);
4155 /* Send reply to the sender */
4156 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4159 /* Then, gracefully, or not, bring the server down. */
4160 silc_server_stop(server);
4164 silc_server_command_free(cmd);
4167 /* Server side command of LEAVE. Removes client from a channel. */
4169 SILC_SERVER_CMD_FUNC(leave)
4171 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4172 SilcServer server = cmd->server;
4173 SilcSocketConnection sock = cmd->sock;
4174 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4176 SilcChannelEntry channel;
4178 unsigned int i, len;
4181 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_LEAVE, cmd, 1, 2);
4183 /* Get Channel ID */
4184 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4186 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4187 SILC_STATUS_ERR_NO_CHANNEL_ID);
4190 id = silc_id_payload_parse_id(tmp, len);
4192 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4193 SILC_STATUS_ERR_NO_CHANNEL_ID);
4197 /* Get channel entry */
4198 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4200 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4202 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4203 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4208 /* Check whether this client is on the channel */
4209 if (!silc_server_client_on_channel(id_entry, channel)) {
4210 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4211 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4215 /* Notify routers that they should remove this client from their list
4216 of clients on the channel. Send LEAVE notify type. */
4217 if (!server->standalone)
4218 silc_server_send_notify_leave(server, server->router->connection,
4219 server->server_type == SILC_ROUTER ?
4220 TRUE : FALSE, channel, id_entry->id,
4221 SILC_ID_CLIENT_LEN);
4223 /* Remove client from channel */
4224 i = silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4226 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4229 /* If the channel does not exist anymore we won't send anything */
4233 /* Re-generate channel key */
4234 silc_server_create_channel_key(server, channel, 0);
4236 /* Encode channel key payload to be distributed on the channel */
4238 silc_channel_key_payload_encode(len, tmp,
4239 strlen(channel->channel_key->cipher->name),
4240 channel->channel_key->cipher->name,
4241 channel->key_len / 8, channel->key);
4243 /* If we are normal server then we will send it to our router. If we
4244 are router we will send it to all local servers that has clients on
4246 if (server->server_type == SILC_SERVER) {
4247 if (!server->standalone)
4248 silc_server_packet_send(server,
4249 cmd->server->router->connection,
4250 SILC_PACKET_CHANNEL_KEY, 0, packet->data,
4251 packet->len, FALSE);
4256 /* Send to locally connected clients on the channel */
4257 silc_server_packet_send_local_channel(server, channel,
4258 SILC_PACKET_CHANNEL_KEY, 0,
4259 packet->data, packet->len, FALSE);
4261 silc_buffer_free(packet);
4265 silc_server_command_free(cmd);
4268 /* Server side of command USERS. Resolves clients and their USERS currently
4269 joined on the requested channel. The list of Client ID's and their modes
4270 on the channel is sent back. */
4272 SILC_SERVER_CMD_FUNC(users)
4274 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4275 SilcServer server = cmd->server;
4276 SilcChannelEntry channel;
4279 unsigned char *channel_id;
4280 unsigned int channel_id_len;
4281 SilcBuffer client_id_list;
4282 SilcBuffer client_mode_list;
4283 unsigned char lc[4];
4284 unsigned int list_count = 0;
4285 unsigned short ident = silc_command_get_ident(cmd->payload);
4287 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_USERS, cmd, 1, 1);
4289 /* Get Channel ID */
4290 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4292 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4293 SILC_STATUS_ERR_NO_CHANNEL_ID);
4296 id = silc_id_payload_parse_id(channel_id, channel_id_len);
4298 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4299 SILC_STATUS_ERR_NO_CHANNEL_ID);
4303 /* If we are server and we don't know about this channel we will send
4304 the command to our router. If we know about the channel then we also
4305 have the list of users already. */
4306 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4308 if (server->server_type == SILC_SERVER && !server->standalone &&
4312 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4313 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4315 /* Send USERS command */
4316 silc_server_packet_send(server, server->router->connection,
4317 SILC_PACKET_COMMAND, cmd->packet->flags,
4318 tmpbuf->data, tmpbuf->len, TRUE);
4320 /* Reprocess this packet after received reply */
4321 silc_server_command_pending(server, SILC_COMMAND_USERS,
4322 silc_command_get_ident(cmd->payload),
4323 silc_server_command_destructor,
4324 silc_server_command_users,
4325 silc_server_command_dup(cmd));
4326 cmd->pending = TRUE;
4327 silc_command_set_ident(cmd->payload, ident);
4329 silc_buffer_free(tmpbuf);
4334 /* We are router and we will check the global list as well. */
4335 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4337 /* Channel really does not exist */
4338 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4339 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4344 /* Get the users list */
4345 silc_server_get_users_on_channel(server, channel, &client_id_list,
4346 &client_mode_list, &list_count);
4349 SILC_PUT32_MSB(list_count, lc);
4352 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4353 SILC_STATUS_OK, ident, 4,
4354 2, channel_id, channel_id_len,
4356 4, client_id_list->data,
4357 client_id_list->len,
4358 5, client_mode_list->data,
4359 client_mode_list->len);
4360 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4361 packet->data, packet->len, FALSE);
4363 silc_buffer_free(packet);
4364 silc_buffer_free(client_id_list);
4365 silc_buffer_free(client_mode_list);
4369 silc_server_command_free(cmd);
4372 /* Server side of command BAN. This is used to manage the ban list of the
4373 channel. To add clients and remove clients from the ban list. */
4375 SILC_SERVER_CMD_FUNC(ban)
4377 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4378 SilcServer server = cmd->server;
4379 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4381 SilcChannelEntry channel;
4382 SilcChannelClientEntry chl;
4383 SilcChannelID *channel_id = NULL;
4384 unsigned char *id, *add, *del;
4385 unsigned int id_len, tmp_len;
4386 unsigned short ident = silc_command_get_ident(cmd->payload);
4388 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4391 SILC_SERVER_COMMAND_CHECK_ARGC(SILC_COMMAND_BAN, cmd, 0, 3);
4393 /* Get Channel ID */
4394 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4396 channel_id = silc_id_payload_parse_id(id, id_len);
4398 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4399 SILC_STATUS_ERR_NO_CHANNEL_ID);
4404 /* Get channel entry. The server must know about the channel since the
4405 client is expected to be on the channel. */
4406 channel = silc_idlist_find_channel_by_id(server->local_list,
4409 channel = silc_idlist_find_channel_by_id(server->global_list,
4412 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4413 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4418 /* Check whether this client is on the channel */
4419 if (!silc_server_client_on_channel(client, channel)) {
4420 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4421 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4425 /* Get entry to the channel user list */
4426 silc_list_start(channel->user_list);
4427 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
4428 if (chl->client == client)
4431 /* The client must be at least channel operator. */
4432 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4433 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4434 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4438 /* Get the new ban and add it to the ban list */
4439 add = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4441 if (!channel->ban_list)
4442 channel->ban_list = silc_calloc(tmp_len + 2, sizeof(*channel->ban_list));
4444 channel->ban_list = silc_realloc(channel->ban_list,
4445 sizeof(*channel->ban_list) *
4447 strlen(channel->ban_list) + 2));
4448 strncat(channel->ban_list, add, tmp_len);
4449 strncat(channel->ban_list, ",", 1);
4452 /* Get the ban to be removed and remove it from the list */
4453 del = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4454 if (del && channel->ban_list) {
4455 char *start, *end, *n;
4457 if (!strncmp(channel->ban_list, del, strlen(channel->ban_list) - 1)) {
4458 silc_free(channel->ban_list);
4459 channel->ban_list = NULL;
4463 start = strstr(channel->ban_list, del);
4464 if (start && strlen(start) >= tmp_len) {
4465 end = start + tmp_len;
4466 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
4467 strncat(n, channel->ban_list, start - channel->ban_list);
4468 strncat(n, end + 1, ((channel->ban_list + strlen(channel->ban_list)) -
4470 silc_free(channel->ban_list);
4471 channel->ban_list = n;
4477 /* Send the BAN notify type to our primary router. */
4478 if (!server->standalone && (add || del))
4479 silc_server_send_notify_ban(server, server->router->connection,
4480 server->server_type == SILC_ROUTER ?
4481 TRUE : FALSE, channel, add, del);
4483 /* Send the reply back to the client */
4484 if (channel->ban_list)
4486 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4487 SILC_STATUS_OK, ident, 2,
4489 3, channel->ban_list,
4490 strlen(channel->ban_list) - 1);
4493 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4494 SILC_STATUS_OK, ident, 1,
4497 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4498 packet->data, packet->len, FALSE);
4500 silc_buffer_free(packet);
4504 silc_free(channel_id);
4505 silc_server_command_free(cmd);