5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "serverincludes.h"
23 #include "server_internal.h"
25 static int silc_server_is_registered(SilcServer server,
26 SilcSocketConnection sock,
27 SilcServerCommandContext cmd,
30 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
32 SilcCommandStatus status);
34 silc_server_command_send_status_data(SilcServerCommandContext cmd,
36 SilcCommandStatus status,
41 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
42 SilcServerCommandReplyContext cmdr,
44 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
46 /* Server command list. */
47 SilcServerCommand silc_command_list[] =
49 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
50 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
51 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
53 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
54 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
55 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
56 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
57 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
58 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
59 SILC_SERVER_CMD(connect, CONNECT,
60 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
61 SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
62 SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
63 SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
64 SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
65 SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
66 SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
67 SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
68 SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
69 SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
70 SILC_SERVER_CMD(close, CLOSE,
71 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
72 SILC_SERVER_CMD(shutdown, SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
74 SILC_SERVER_CMD(silcoper, SILCOPER,
75 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
76 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
77 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
78 SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
83 /* Performs several checks to the command. It first checks whether this
84 command was called as pending command callback. If it was then it checks
85 whether error occurred in the command reply where the pending command
88 It also checks that the requested command includes correct amount
90 #define SILC_SERVER_COMMAND_CHECK(command, context, min, max) \
94 SILC_LOG_DEBUG(("Start")); \
96 if (silc_server_command_pending_error_check(cmd, context2, command)) { \
97 silc_server_command_free(cmd); \
101 _argc = silc_argument_get_arg_num(cmd->args); \
103 silc_server_command_send_status_reply(cmd, command, \
104 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS); \
105 silc_server_command_free(cmd); \
109 silc_server_command_send_status_reply(cmd, command, \
110 SILC_STATUS_ERR_TOO_MANY_PARAMS); \
111 silc_server_command_free(cmd); \
116 /* Returns TRUE if the connection is registered. Unregistered connections
117 usually cannot send commands hence the check. */
119 static int silc_server_is_registered(SilcServer server,
120 SilcSocketConnection sock,
121 SilcServerCommandContext cmd,
124 SilcIDListData idata = (SilcIDListData)sock->user_data;
125 if (idata->status & SILC_IDLIST_STATUS_REGISTERED)
128 silc_server_command_send_status_reply(cmd, command,
129 SILC_STATUS_ERR_NOT_REGISTERED);
130 silc_server_command_free(cmd);
134 /* Internal context to hold data when executed command with timeout. */
136 SilcServerCommandContext ctx;
137 SilcServerCommand *cmd;
138 } *SilcServerCommandTimeout;
140 /* Timeout callback to process commands with timeout for client. Client's
141 commands are always executed with timeout. */
143 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
145 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
146 SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
148 /* Update access time */
149 client->last_command = time(NULL);
151 if (!(timeout->cmd->flags & SILC_CF_REG))
152 timeout->cmd->cb(timeout->ctx, NULL);
153 else if (silc_server_is_registered(timeout->ctx->server,
157 timeout->cmd->cb(timeout->ctx, NULL);
162 /* Processes received command packet. */
164 void silc_server_command_process(SilcServer server,
165 SilcSocketConnection sock,
166 SilcPacketContext *packet)
168 SilcServerCommandContext ctx;
169 SilcServerCommand *cmd;
172 /* Allocate command context. This must be free'd by the
173 command routine receiving it. */
174 ctx = silc_server_command_alloc();
175 ctx->server = server;
176 ctx->sock = silc_socket_dup(sock);
177 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
179 /* Parse the command payload in the packet */
180 ctx->payload = silc_command_payload_parse(packet->buffer);
182 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
183 silc_buffer_free(packet->buffer);
184 silc_packet_context_free(packet);
185 silc_socket_free(ctx->sock);
189 ctx->args = silc_command_get_args(ctx->payload);
191 /* Get the command */
192 command = silc_command_get(ctx->payload);
193 for (cmd = silc_command_list; cmd->cb; cmd++)
194 if (cmd->cmd == command)
198 silc_server_command_send_status_reply(ctx, command,
199 SILC_STATUS_ERR_UNKNOWN_COMMAND);
200 silc_server_command_free(ctx);
204 /* Execute client's commands always with timeout. Normally they are
205 executed with zero (0) timeout but if client is sending command more
206 frequently than once in 2 seconds, then the timeout may be 0 to 2
208 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
209 SilcClientEntry client = (SilcClientEntry)sock->user_data;
210 SilcServerCommandTimeout timeout = silc_calloc(1, sizeof(*timeout));
216 if (client->last_command && (time(NULL) - client->last_command) < 2) {
217 client->fast_command++;
220 client->fast_command = ((client->fast_command - 1) <= 0 ? 0 :
221 client->fast_command--);
225 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
226 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
227 silc_schedule_task_add(server->schedule, sock->sock,
228 silc_server_command_process_timeout,
230 2 - (time(NULL) - client->last_command), 0,
232 SILC_TASK_PRI_NORMAL);
234 silc_schedule_task_add(server->schedule, sock->sock,
235 silc_server_command_process_timeout,
239 SILC_TASK_PRI_NORMAL);
243 /* Execute for server */
245 if (!(cmd->flags & SILC_CF_REG))
247 else if (silc_server_is_registered(server, sock, ctx, cmd->cmd))
251 /* Allocate Command Context */
253 SilcServerCommandContext silc_server_command_alloc()
255 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
260 /* Free's the command context allocated before executing the command */
262 void silc_server_command_free(SilcServerCommandContext ctx)
265 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
267 if (ctx->users < 1) {
269 silc_command_payload_free(ctx->payload);
271 silc_packet_context_free(ctx->packet);
273 silc_socket_free(ctx->sock); /* Decrease reference counter */
278 /* Duplicate Command Context by adding reference counter. The context won't
279 be free'd untill it hits zero. */
281 SilcServerCommandContext
282 silc_server_command_dup(SilcServerCommandContext ctx)
285 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
290 /* Add new pending command to be executed when reply to a command has been
291 received. The `reply_cmd' is the command that will call the `callback'
292 with `context' when reply has been received. It can be SILC_COMMAND_NONE
293 to match any command with the `ident'. If `ident' is non-zero
294 the `callback' will be executed when received reply with command
295 identifier `ident'. */
297 void silc_server_command_pending(SilcServer server,
298 SilcCommand reply_cmd,
300 SilcServerPendingDestructor destructor,
301 SilcCommandCb callback,
304 SilcServerCommandPending *reply;
306 reply = silc_calloc(1, sizeof(*reply));
307 reply->reply_cmd = reply_cmd;
308 reply->ident = ident;
309 reply->context = context;
310 reply->callback = callback;
311 reply->destructor = destructor;
312 silc_dlist_add(server->pending_commands, reply);
315 /* Deletes pending command by reply command type. */
317 void silc_server_command_pending_del(SilcServer server,
318 SilcCommand reply_cmd,
321 SilcServerCommandPending *r;
323 silc_dlist_start(server->pending_commands);
324 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
325 if (r->reply_cmd == reply_cmd && r->ident == ident) {
326 silc_dlist_del(server->pending_commands, r);
332 /* Checks for pending commands and marks callbacks to be called from
333 the command reply function. Returns TRUE if there were pending command. */
335 SilcServerCommandPendingCallbacks
336 silc_server_command_pending_check(SilcServer server,
337 SilcServerCommandReplyContext ctx,
340 uint32 *callbacks_count)
342 SilcServerCommandPending *r;
343 SilcServerCommandPendingCallbacks callbacks = NULL;
346 silc_dlist_start(server->pending_commands);
347 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
348 if ((r->reply_cmd == command || r->reply_cmd == SILC_COMMAND_NONE)
349 && r->ident == ident) {
350 callbacks = silc_realloc(callbacks, sizeof(*callbacks) * (i + 1));
351 callbacks[i].context = r->context;
352 callbacks[i].callback = r->callback;
353 callbacks[i].destructor = r->destructor;
359 *callbacks_count = i;
363 /* Destructor function for pending callbacks. This is called when using
364 pending commands to free the context given for the pending command. */
366 static void silc_server_command_destructor(void *context)
368 silc_server_command_free((SilcServerCommandContext)context);
371 /* Sends simple status message as command reply packet */
374 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
376 SilcCommandStatus status)
380 SILC_LOG_DEBUG(("Sending command status %d", status));
383 silc_command_reply_payload_encode_va(command, status,
384 silc_command_get_ident(cmd->payload),
386 silc_server_packet_send(cmd->server, cmd->sock,
387 SILC_PACKET_COMMAND_REPLY, 0,
388 buffer->data, buffer->len, FALSE);
389 silc_buffer_free(buffer);
392 /* Sends command status reply with one extra argument. The argument
393 type must be sent as argument. */
396 silc_server_command_send_status_data(SilcServerCommandContext cmd,
398 SilcCommandStatus status,
405 SILC_LOG_DEBUG(("Sending command status %d", status));
408 silc_command_reply_payload_encode_va(command, status,
409 silc_command_get_ident(cmd->payload),
410 1, arg_type, arg, arg_len);
411 silc_server_packet_send(cmd->server, cmd->sock,
412 SILC_PACKET_COMMAND_REPLY, 0,
413 buffer->data, buffer->len, FALSE);
414 silc_buffer_free(buffer);
417 /* This function can be called to check whether in the command reply
418 an error occurred. This function has no effect if this is called
419 when the command function was not called as pending command callback.
420 This returns TRUE if error had occurred. */
423 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
424 SilcServerCommandReplyContext cmdr,
427 SilcCommandStatus status;
429 if (!cmd->pending || !cmdr)
432 SILC_GET16_MSB(status, silc_argument_get_arg_type(cmdr->args, 1, NULL));
433 if (status != SILC_STATUS_OK &&
434 status != SILC_STATUS_LIST_START &&
435 status != SILC_STATUS_LIST_ITEM &&
436 status != SILC_STATUS_LIST_END) {
437 /* Send the error message */
438 silc_server_command_send_status_reply(cmd, command, status);
445 /******************************************************************************
449 ******************************************************************************/
452 silc_server_command_whois_parse(SilcServerCommandContext cmd,
453 SilcClientID ***client_id,
454 uint32 *client_id_count,
462 uint32 argc = silc_argument_get_arg_num(cmd->args);
465 /* If client ID is in the command it must be used instead of nickname */
466 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
468 /* No ID, get the nickname@server string and parse it. */
469 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
471 silc_parse_userfqdn(tmp, nickname, server_name);
473 silc_server_command_send_status_reply(cmd, command,
474 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
478 /* Command includes ID, we must use that. Also check whether the command
479 has more than one ID set - take them all. */
481 *client_id = silc_calloc(1, sizeof(**client_id));
482 (*client_id)[0] = silc_id_payload_parse_id(tmp, len);
483 if ((*client_id)[0] == NULL) {
484 silc_free(*client_id);
487 *client_id_count = 1;
489 /* Take all ID's from the command packet */
491 for (k = 1, i = 1; i < argc; i++) {
492 tmp = silc_argument_get_arg_type(cmd->args, i + 3, &len);
494 *client_id = silc_realloc(*client_id, sizeof(**client_id) *
495 (*client_id_count + 1));
496 (*client_id)[k] = silc_id_payload_parse_id(tmp, len);
497 if ((*client_id)[k] == NULL) {
498 /* Cleanup all and fail */
499 for (i = 0; i < *client_id_count; i++)
500 silc_free((*client_id)[i]);
501 silc_free(*client_id);
504 (*client_id_count)++;
511 /* Get the max count of reply messages allowed */
512 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
521 /* Resolve context used by both WHOIS and IDENTIFY commands */
523 SilcServerEntry router;
525 unsigned char **res_argv;
526 uint32 *res_argv_lens;
527 uint32 *res_argv_types;
529 } *SilcServerResolveContext;
532 silc_server_command_whois_check(SilcServerCommandContext cmd,
533 SilcClientEntry *clients,
534 uint32 clients_count)
536 SilcServer server = cmd->server;
537 SilcClientEntry entry;
538 SilcServerResolveContext resolve = NULL, r = NULL;
539 uint32 resolve_count = 0;
543 for (i = 0; i < clients_count; i++) {
546 if (!entry || (entry->nickname && entry->username && entry->userinfo) ||
547 !(entry->data.status & SILC_IDLIST_STATUS_REGISTERED) ||
551 /* We need to resolve this entry since it is not complete */
553 if (!cmd->pending && entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
554 /* The entry is being resolved (and we are not the resolver) so attach
555 to the command reply and we're done with this one. */
556 silc_server_command_pending(server, SILC_COMMAND_NONE,
557 entry->resolve_cmd_ident,
558 silc_server_command_destructor,
559 silc_server_command_whois,
560 silc_server_command_dup(cmd));
563 if (entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
564 /* We've resolved this and it still is not ready. We'll return
565 and are that this will be handled again after it is resolved. */
566 for (i = 0; i < resolve_count; i++) {
567 for (k = 0; k < r->res_argc; k++)
568 silc_free(r->res_argv[k]);
569 silc_free(r->res_argv);
570 silc_free(r->res_argv_lens);
571 silc_free(r->res_argv_types);
576 /* We'll resolve this client */
580 for (k = 0; k < resolve_count; k++) {
581 if (resolve[k].router == entry->router) {
588 resolve = silc_realloc(resolve, sizeof(*resolve) *
589 (resolve_count + 1));
590 r = &resolve[resolve_count];
591 memset(r, 0, sizeof(*r));
592 r->router = entry->router;
593 r->ident = ++server->cmd_ident;
597 r->res_argv = silc_realloc(r->res_argv, sizeof(*r->res_argv) *
599 r->res_argv_lens = silc_realloc(r->res_argv_lens,
600 sizeof(*r->res_argv_lens) *
602 r->res_argv_types = silc_realloc(r->res_argv_types,
603 sizeof(*r->res_argv_types) *
605 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
606 r->res_argv[r->res_argc] = silc_calloc(idp->len,
607 sizeof(**r->res_argv));
608 memcpy(r->res_argv[r->res_argc], idp->data, idp->len);
609 r->res_argv_lens[r->res_argc] = idp->len;
610 r->res_argv_types[r->res_argc] = r->res_argc + 3;
612 silc_buffer_free(idp);
614 entry->resolve_cmd_ident = r->ident;
615 entry->data.status |= SILC_IDLIST_STATUS_RESOLVING;
616 entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
621 /* Do the resolving */
622 for (i = 0; i < resolve_count; i++) {
627 /* Send WHOIS request. We send WHOIS since we're doing the requesting
628 now anyway so make it a good one. */
629 res_cmd = silc_command_payload_encode(SILC_COMMAND_WHOIS,
630 r->res_argc, r->res_argv,
634 silc_server_packet_send(server, r->router->connection,
635 SILC_PACKET_COMMAND, cmd->packet->flags,
636 res_cmd->data, res_cmd->len, FALSE);
638 /* Reprocess this packet after received reply */
639 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
641 silc_server_command_destructor,
642 silc_server_command_whois,
643 silc_server_command_dup(cmd));
646 silc_buffer_free(res_cmd);
647 for (k = 0; k < r->res_argc; k++)
648 silc_free(r->res_argv[k]);
649 silc_free(r->res_argv);
650 silc_free(r->res_argv_lens);
651 silc_free(r->res_argv_types);
660 silc_server_command_whois_send_reply(SilcServerCommandContext cmd,
661 SilcClientEntry *clients,
662 uint32 clients_count,
665 SilcServer server = cmd->server;
668 SilcBuffer packet, idp, channels;
669 SilcClientEntry entry;
670 SilcCommandStatus status;
671 uint16 ident = silc_command_get_ident(cmd->payload);
672 char nh[256], uh[256];
673 unsigned char idle[4], mode[4];
674 SilcSocketConnection hsock;
677 for (i = 0; i < clients_count; i++)
678 if (clients[i]->data.status & SILC_IDLIST_STATUS_REGISTERED)
681 if (len == 0 && clients_count) {
683 if (entry->nickname) {
684 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
685 SILC_STATUS_ERR_NO_SUCH_NICK,
687 strlen(entry->nickname));
689 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
690 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
691 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
692 2, idp->data, idp->len);
693 silc_buffer_free(idp);
699 status = SILC_STATUS_OK;
701 status = SILC_STATUS_LIST_START;
703 for (i = 0, k = 0; i < clients_count; i++) {
706 if (!(entry->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
707 if (clients_count == 1) {
708 if (entry->nickname) {
709 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
710 SILC_STATUS_ERR_NO_SUCH_NICK,
712 strlen(entry->nickname));
714 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
715 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
716 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
717 2, idp->data, idp->len);
718 silc_buffer_free(idp);
725 status = SILC_STATUS_LIST_ITEM;
727 if (clients_count > 1 && k == clients_count - 1)
728 status = SILC_STATUS_LIST_END;
730 if (count && k - 1 == count)
731 status = SILC_STATUS_LIST_END;
733 if (count && k - 1 > count)
736 /* Sanity check, however these should never fail. However, as
737 this sanity check has been added here they have failed. */
738 if (!entry->nickname || !entry->username || !entry->userinfo)
741 /* Send WHOIS reply */
742 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
743 tmp = silc_argument_get_first_arg(cmd->args, NULL);
745 memset(uh, 0, sizeof(uh));
746 memset(nh, 0, sizeof(nh));
747 memset(idle, 0, sizeof(idle));
749 strncat(nh, entry->nickname, strlen(entry->nickname));
750 if (!strchr(entry->nickname, '@')) {
752 if (entry->servername) {
753 strncat(nh, entry->servername, strlen(entry->servername));
755 len = entry->router ? strlen(entry->router->server_name) :
756 strlen(server->server_name);
757 strncat(nh, entry->router ? entry->router->server_name :
758 server->server_name, len);
762 strncat(uh, entry->username, strlen(entry->username));
763 if (!strchr(entry->username, '@')) {
765 hsock = (SilcSocketConnection)entry->connection;
766 len = strlen(hsock->hostname);
767 strncat(uh, hsock->hostname, len);
770 channels = silc_server_get_client_channel_list(server, entry);
772 SILC_PUT32_MSB(entry->mode, mode);
774 if (entry->connection) {
775 SILC_PUT32_MSB((time(NULL) - entry->data.last_receive), idle);
779 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
781 2, idp->data, idp->len,
785 strlen(entry->userinfo),
791 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
793 2, idp->data, idp->len,
797 strlen(entry->userinfo),
801 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
802 0, packet->data, packet->len, FALSE);
804 silc_buffer_free(packet);
805 silc_buffer_free(idp);
807 silc_buffer_free(channels);
814 silc_server_command_whois_process(SilcServerCommandContext cmd)
816 SilcServer server = cmd->server;
817 char *nick = NULL, *server_name = NULL;
819 SilcClientEntry *clients = NULL, entry;
820 SilcClientID **client_id = NULL;
821 uint32 client_id_count = 0, clients_count = 0;
823 bool check_global = FALSE;
825 /* Protocol dictates that we must always send the received WHOIS request
826 to our router if we are normal server, so let's do it now unless we
827 are standalone. We will not send any replies to the client until we
828 have received reply from the router. */
829 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
830 server->server_type == SILC_SERVER && !cmd->pending &&
831 !server->standalone) {
835 old_ident = silc_command_get_ident(cmd->payload);
836 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
837 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
839 /* Send WHOIS command to our router */
840 silc_server_packet_send(server, (SilcSocketConnection)
841 server->router->connection,
842 SILC_PACKET_COMMAND, cmd->packet->flags,
843 tmpbuf->data, tmpbuf->len, TRUE);
845 /* Reprocess this packet after received reply from router */
846 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
847 silc_command_get_ident(cmd->payload),
848 silc_server_command_destructor,
849 silc_server_command_whois,
850 silc_server_command_dup(cmd));
853 silc_command_set_ident(cmd->payload, old_ident);
855 silc_buffer_free(tmpbuf);
860 /* We are ready to process the command request. Let's search for the
861 requested client and send reply to the requesting client. */
863 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
865 else if (server->server_type == SILC_ROUTER)
868 /* Parse the whois request */
869 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
870 &nick, &server_name, &count,
874 /* Get all clients matching that ID or nickname from local list */
875 if (client_id_count) {
876 /* Check all Client ID's received in the command packet */
877 for (i = 0; i < client_id_count; i++) {
878 entry = silc_idlist_find_client_by_id(server->local_list,
879 client_id[i], TRUE, NULL);
880 if (!entry && check_global)
881 entry = silc_idlist_find_client_by_id(server->global_list,
882 client_id[i], TRUE, NULL);
884 clients = silc_realloc(clients, sizeof(*clients) *
885 (clients_count + 1));
886 clients[clients_count++] = entry;
890 if (!silc_idlist_get_clients_by_hash(server->local_list,
891 nick, server->md5hash,
892 &clients, &clients_count))
893 silc_idlist_get_clients_by_nickname(server->local_list,
895 &clients, &clients_count);
897 if (!silc_idlist_get_clients_by_hash(server->global_list,
898 nick, server->md5hash,
899 &clients, &clients_count))
900 silc_idlist_get_clients_by_nickname(server->global_list,
902 &clients, &clients_count);
907 /* Such client(s) really does not exist in the SILC network. */
908 if (!client_id_count) {
909 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
910 SILC_STATUS_ERR_NO_SUCH_NICK,
911 3, nick, strlen(nick));
913 SilcBuffer idp = silc_id_payload_encode(client_id[0], SILC_ID_CLIENT);
914 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
915 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
916 2, idp->data, idp->len);
917 silc_buffer_free(idp);
922 /* Router always finds the client entry if it exists in the SILC network.
923 However, it might be incomplete entry and does not include all the
924 mandatory fields that WHOIS command reply requires. Check for these and
925 make query from the server who owns the client if some fields are
927 if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
932 /* Send the command reply */
933 silc_server_command_whois_send_reply(cmd, clients, clients_count,
937 if (client_id_count) {
938 for (i = 0; i < client_id_count; i++)
939 silc_free(client_id[i]);
940 silc_free(client_id);
947 silc_free(server_name);
952 /* Server side of command WHOIS. Processes user's query and sends found
953 results as command replies back to the client. */
955 SILC_SERVER_CMD_FUNC(whois)
957 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
960 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 3328);
962 ret = silc_server_command_whois_process(cmd);
965 silc_server_command_free(cmd);
968 /******************************************************************************
972 ******************************************************************************/
975 silc_server_command_whowas_parse(SilcServerCommandContext cmd,
983 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
985 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WHOWAS,
986 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
990 /* Get the nickname@server string and parse it. */
991 silc_parse_userfqdn(tmp, nickname, server_name);
993 /* Get the max count of reply messages allowed */
994 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
1004 silc_server_command_whowas_check(SilcServerCommandContext cmd,
1005 SilcClientEntry *clients,
1006 uint32 clients_count)
1008 SilcServer server = cmd->server;
1010 SilcClientEntry entry;
1012 for (i = 0; i < clients_count; i++) {
1015 if (!entry->nickname || !entry->username) {
1022 old_ident = silc_command_get_ident(cmd->payload);
1023 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1024 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1026 /* Send WHOWAS command */
1027 silc_server_packet_send(server, entry->router->connection,
1028 SILC_PACKET_COMMAND, cmd->packet->flags,
1029 tmpbuf->data, tmpbuf->len, TRUE);
1031 /* Reprocess this packet after received reply */
1032 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
1033 silc_command_get_ident(cmd->payload),
1034 silc_server_command_destructor,
1035 silc_server_command_whowas,
1036 silc_server_command_dup(cmd));
1037 cmd->pending = TRUE;
1039 silc_command_set_ident(cmd->payload, old_ident);
1041 silc_buffer_free(tmpbuf);
1050 silc_server_command_whowas_send_reply(SilcServerCommandContext cmd,
1051 SilcClientEntry *clients,
1052 uint32 clients_count)
1054 SilcServer server = cmd->server;
1056 int i, count = 0, len;
1057 SilcBuffer packet, idp;
1058 SilcClientEntry entry = NULL;
1059 SilcCommandStatus status;
1060 uint16 ident = silc_command_get_ident(cmd->payload);
1062 char nh[256], uh[256];
1064 status = SILC_STATUS_OK;
1065 if (clients_count > 1)
1066 status = SILC_STATUS_LIST_START;
1068 for (i = 0; i < clients_count; i++) {
1071 /* We will take only clients that are not valid anymore. They are the
1072 ones that are not registered anymore but still have a ID. They
1073 have disconnected us, and thus valid for WHOWAS. */
1074 if (entry->data.status & SILC_IDLIST_STATUS_REGISTERED)
1076 if (entry->id == NULL)
1079 if (count && i - 1 == count)
1084 if (clients_count > 2)
1085 status = SILC_STATUS_LIST_ITEM;
1087 if (clients_count > 1 && i == clients_count - 1)
1088 status = SILC_STATUS_LIST_END;
1090 /* Sanity check, however these should never fail. However, as
1091 this sanity check has been added here they have failed. */
1092 if (!entry->nickname || !entry->username)
1095 /* Send WHOWAS reply */
1096 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1097 tmp = silc_argument_get_first_arg(cmd->args, NULL);
1099 memset(uh, 0, sizeof(uh));
1100 memset(nh, 0, sizeof(nh));
1102 strncat(nh, entry->nickname, strlen(entry->nickname));
1103 if (!strchr(entry->nickname, '@')) {
1104 strncat(nh, "@", 1);
1105 if (entry->servername) {
1106 strncat(nh, entry->servername, strlen(entry->servername));
1108 len = entry->router ? strlen(entry->router->server_name) :
1109 strlen(server->server_name);
1110 strncat(nh, entry->router ? entry->router->server_name :
1111 server->server_name, len);
1115 strncat(uh, entry->username, strlen(entry->username));
1116 if (!strchr(entry->username, '@')) {
1117 strncat(uh, "@", 1);
1118 strcat(uh, "*private*");
1121 if (entry->userinfo)
1123 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1125 2, idp->data, idp->len,
1129 strlen(entry->userinfo));
1132 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1134 2, idp->data, idp->len,
1138 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1139 0, packet->data, packet->len, FALSE);
1141 silc_buffer_free(packet);
1142 silc_buffer_free(idp);
1145 if (found == FALSE && entry)
1146 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1147 SILC_STATUS_ERR_NO_SUCH_NICK,
1149 strlen(entry->nickname));
1153 silc_server_command_whowas_process(SilcServerCommandContext cmd)
1155 SilcServer server = cmd->server;
1156 char *nick = NULL, *server_name = NULL;
1158 SilcClientEntry *clients = NULL;
1159 uint32 clients_count = 0;
1161 bool check_global = FALSE;
1163 /* Protocol dictates that we must always send the received WHOWAS request
1164 to our router if we are normal server, so let's do it now unless we
1165 are standalone. We will not send any replies to the client until we
1166 have received reply from the router. */
1167 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1168 server->server_type == SILC_SERVER && !cmd->pending &&
1169 !server->standalone) {
1173 old_ident = silc_command_get_ident(cmd->payload);
1174 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1175 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1177 /* Send WHOWAS command to our router */
1178 silc_server_packet_send(server, (SilcSocketConnection)
1179 server->router->connection,
1180 SILC_PACKET_COMMAND, cmd->packet->flags,
1181 tmpbuf->data, tmpbuf->len, TRUE);
1183 /* Reprocess this packet after received reply from router */
1184 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
1185 silc_command_get_ident(cmd->payload),
1186 silc_server_command_destructor,
1187 silc_server_command_whowas,
1188 silc_server_command_dup(cmd));
1189 cmd->pending = TRUE;
1191 silc_command_set_ident(cmd->payload, old_ident);
1193 silc_buffer_free(tmpbuf);
1198 /* We are ready to process the command request. Let's search for the
1199 requested client and send reply to the requesting client. */
1201 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1202 check_global = TRUE;
1203 else if (server->server_type == SILC_ROUTER)
1204 check_global = TRUE;
1206 /* Parse the whowas request */
1207 if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1210 /* Get all clients matching that nickname from local list */
1211 if (!silc_idlist_get_clients_by_nickname(server->local_list,
1213 &clients, &clients_count))
1214 silc_idlist_get_clients_by_hash(server->local_list,
1215 nick, server->md5hash,
1216 &clients, &clients_count);
1218 /* Check global list as well */
1220 if (!silc_idlist_get_clients_by_nickname(server->global_list,
1222 &clients, &clients_count))
1223 silc_idlist_get_clients_by_hash(server->global_list,
1224 nick, server->md5hash,
1225 &clients, &clients_count);
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 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1237 !silc_server_command_whowas_check(cmd, clients, clients_count)) {
1242 /* Send the command reply to the client */
1243 silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1248 silc_free(server_name);
1253 /* Server side of command WHOWAS. */
1255 SILC_SERVER_CMD_FUNC(whowas)
1257 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1260 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
1262 ret = silc_server_command_whowas_process(cmd);
1265 silc_server_command_free(cmd);
1268 /******************************************************************************
1272 ******************************************************************************/
1275 silc_server_command_identify_parse(SilcServerCommandContext cmd,
1276 SilcClientEntry **clients,
1277 uint32 *clients_count,
1278 SilcServerEntry **servers,
1279 uint32 *servers_count,
1280 SilcChannelEntry **channels,
1281 uint32 *channels_count,
1284 SilcServer server = cmd->server;
1287 uint32 argc = silc_argument_get_arg_num(cmd->args);
1289 bool check_global = FALSE;
1294 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1295 check_global = TRUE;
1296 else if (server->server_type == SILC_ROUTER)
1297 check_global = TRUE;
1299 /* If ID Payload is in the command it must be used instead of names */
1300 tmp = silc_argument_get_arg_type(cmd->args, 5, &len);
1302 /* No ID, get the names. */
1304 /* Try to get nickname@server. */
1305 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
1308 char *nick_server = NULL;
1310 silc_parse_userfqdn(tmp, &nick, &nick_server);
1312 if (!silc_idlist_get_clients_by_hash(server->local_list,
1313 nick, server->md5hash,
1314 clients, clients_count))
1315 silc_idlist_get_clients_by_nickname(server->local_list,
1317 clients, clients_count);
1319 if (!silc_idlist_get_clients_by_hash(server->global_list,
1320 nick, server->md5hash,
1321 clients, clients_count))
1322 silc_idlist_get_clients_by_nickname(server->global_list,
1324 clients, clients_count);
1328 silc_free(nick_server);
1331 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1332 SILC_STATUS_ERR_NO_SUCH_NICK,
1333 3, tmp, strlen(tmp));
1338 /* Try to get server name */
1339 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
1341 entry = silc_idlist_find_server_by_name(server->local_list,
1343 if (!entry && check_global)
1344 entry = silc_idlist_find_server_by_name(server->local_list,
1347 *servers = silc_realloc(*servers, sizeof(**servers) *
1348 (*servers_count + 1));
1349 (*servers)[(*servers_count)++] = entry;
1353 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1354 SILC_STATUS_ERR_NO_SUCH_SERVER,
1355 3, tmp, strlen(tmp));
1360 /* Try to get channel name */
1361 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
1363 entry = silc_idlist_find_channel_by_name(server->local_list,
1365 if (!entry && check_global)
1366 entry = silc_idlist_find_channel_by_name(server->local_list,
1369 *channels = silc_realloc(*channels, sizeof(**channels) *
1370 (*channels_count + 1));
1371 (*channels)[(*channels_count)++] = entry;
1375 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1376 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
1377 3, tmp, strlen(tmp));
1382 if (!(*clients) && !(*servers) && !(*channels)) {
1383 silc_server_command_send_status_reply(cmd, SILC_COMMAND_IDENTIFY,
1384 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1388 /* Command includes ID, we must use that. Also check whether the command
1389 has more than one ID set - take them all. */
1391 /* Take all ID's from the command packet */
1392 for (i = 0; i < argc; i++) {
1395 tmp = silc_argument_get_arg_type(cmd->args, i + 5, &len);
1399 idp = silc_id_payload_parse_data(tmp, len);
1401 silc_free(*clients);
1402 silc_free(*servers);
1403 silc_free(*channels);
1404 silc_server_command_send_status_reply(cmd, SILC_COMMAND_IDENTIFY,
1405 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1409 id = silc_id_payload_get_id(idp);
1411 switch (silc_id_payload_get_type(idp)) {
1413 case SILC_ID_CLIENT:
1414 entry = (void *)silc_idlist_find_client_by_id(server->local_list,
1416 if (!entry && check_global)
1417 entry = (void *)silc_idlist_find_client_by_id(server->global_list,
1420 *clients = silc_realloc(*clients, sizeof(**clients) *
1421 (*clients_count + 1));
1422 (*clients)[(*clients_count)++] = (SilcClientEntry)entry;
1424 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1425 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1432 case SILC_ID_SERVER:
1433 entry = (void *)silc_idlist_find_server_by_id(server->local_list,
1435 if (!entry && check_global)
1436 entry = (void *)silc_idlist_find_server_by_id(server->global_list,
1439 *servers = silc_realloc(*servers, sizeof(**servers) *
1440 (*servers_count + 1));
1441 (*servers)[(*servers_count)++] = (SilcServerEntry)entry;
1443 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1444 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
1450 case SILC_ID_CHANNEL:
1451 entry = (void *)silc_idlist_find_channel_by_id(server->local_list,
1453 if (!entry && check_global)
1454 entry = (void *)silc_idlist_find_channel_by_id(server->global_list,
1457 *channels = silc_realloc(*channels, sizeof(**channels) *
1458 (*channels_count + 1));
1459 (*channels)[(*channels_count)++] = (SilcChannelEntry)entry;
1461 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1462 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID,
1474 silc_free(*clients);
1475 silc_free(*servers);
1476 silc_free(*channels);
1480 /* Get the max count of reply messages allowed */
1481 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
1490 /* Checks that all mandatory fields in client entry are present. If not
1491 then send WHOIS request to the server who owns the client. We use
1492 WHOIS because we want to get as much information as possible at once. */
1495 silc_server_command_identify_check_client(SilcServerCommandContext cmd,
1496 SilcClientEntry *clients,
1497 uint32 clients_count)
1499 SilcServer server = cmd->server;
1500 SilcClientEntry entry;
1501 SilcServerResolveContext resolve = NULL, r = NULL;
1502 uint32 resolve_count = 0;
1506 for (i = 0; i < clients_count; i++) {
1509 if (!entry || entry->nickname ||
1510 !(entry->data.status & SILC_IDLIST_STATUS_REGISTERED) ||
1514 /* We need to resolve this entry since it is not complete */
1516 if (!cmd->pending && entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
1517 /* The entry is being resolved (and we are not the resolver) so attach
1518 to the command reply and we're done with this one. */
1519 silc_server_command_pending(server, SILC_COMMAND_NONE,
1520 entry->resolve_cmd_ident,
1521 silc_server_command_destructor,
1522 silc_server_command_identify,
1523 silc_server_command_dup(cmd));
1526 if (entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
1527 /* We've resolved this and it still is not ready. We'll return
1528 and are that this will be handled again after it is resolved. */
1529 for (i = 0; i < resolve_count; i++) {
1530 for (k = 0; k < r->res_argc; k++)
1531 silc_free(r->res_argv[k]);
1532 silc_free(r->res_argv);
1533 silc_free(r->res_argv_lens);
1534 silc_free(r->res_argv_types);
1539 /* We'll resolve this client */
1543 for (k = 0; k < resolve_count; k++) {
1544 if (resolve[k].router == entry->router) {
1551 resolve = silc_realloc(resolve, sizeof(*resolve) *
1552 (resolve_count + 1));
1553 r = &resolve[resolve_count];
1554 memset(r, 0, sizeof(*r));
1555 r->router = entry->router;
1556 r->ident = ++server->cmd_ident;
1560 r->res_argv = silc_realloc(r->res_argv, sizeof(*r->res_argv) *
1562 r->res_argv_lens = silc_realloc(r->res_argv_lens,
1563 sizeof(*r->res_argv_lens) *
1565 r->res_argv_types = silc_realloc(r->res_argv_types,
1566 sizeof(*r->res_argv_types) *
1568 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1569 r->res_argv[r->res_argc] = silc_calloc(idp->len,
1570 sizeof(**r->res_argv));
1571 memcpy(r->res_argv[r->res_argc], idp->data, idp->len);
1572 r->res_argv_lens[r->res_argc] = idp->len;
1573 r->res_argv_types[r->res_argc] = r->res_argc + 3;
1575 silc_buffer_free(idp);
1577 entry->resolve_cmd_ident = r->ident;
1578 entry->data.status |= SILC_IDLIST_STATUS_RESOLVING;
1579 entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
1584 /* Do the resolving */
1585 for (i = 0; i < resolve_count; i++) {
1590 /* Send WHOIS request. We send WHOIS since we're doing the requesting
1591 now anyway so make it a good one. */
1592 res_cmd = silc_command_payload_encode(SILC_COMMAND_WHOIS,
1593 r->res_argc, r->res_argv,
1597 silc_server_packet_send(server, r->router->connection,
1598 SILC_PACKET_COMMAND, cmd->packet->flags,
1599 res_cmd->data, res_cmd->len, FALSE);
1601 /* Reprocess this packet after received reply */
1602 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1604 silc_server_command_destructor,
1605 silc_server_command_identify,
1606 silc_server_command_dup(cmd));
1607 cmd->pending = TRUE;
1609 silc_buffer_free(res_cmd);
1610 for (k = 0; k < r->res_argc; k++)
1611 silc_free(r->res_argv[k]);
1612 silc_free(r->res_argv);
1613 silc_free(r->res_argv_lens);
1614 silc_free(r->res_argv_types);
1623 silc_server_command_identify_send_reply(SilcServerCommandContext cmd,
1624 SilcClientEntry *clients,
1625 uint32 clients_count,
1626 SilcServerEntry *servers,
1627 uint32 servers_count,
1628 SilcChannelEntry *channels,
1629 uint32 channels_count,
1632 SilcServer server = cmd->server;
1634 SilcBuffer packet, idp;
1635 SilcCommandStatus status;
1636 uint16 ident = silc_command_get_ident(cmd->payload);
1637 char nh[256], uh[256];
1638 SilcSocketConnection hsock;
1640 status = SILC_STATUS_OK;
1643 SilcClientEntry entry;
1646 for (i = 0; i < clients_count; i++)
1647 if (clients[i]->data.status & SILC_IDLIST_STATUS_REGISTERED)
1650 if (len == 0 && clients_count) {
1652 if (entry->nickname) {
1653 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1654 SILC_STATUS_ERR_NO_SUCH_NICK,
1656 strlen(entry->nickname));
1658 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1659 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1660 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1661 2, idp->data, idp->len);
1662 silc_buffer_free(idp);
1669 status = SILC_STATUS_LIST_START;
1671 for (i = 0, k = 0; i < clients_count; i++) {
1674 if (!(entry->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
1675 if (clients_count == 1) {
1676 SilcBuffer idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1677 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1678 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1679 2, idp->data, idp->len);
1680 silc_buffer_free(idp);
1686 status = SILC_STATUS_LIST_ITEM;
1687 if (clients_count > 1 && k == clients_count - 1
1688 && !servers_count && !channels_count)
1689 status = SILC_STATUS_LIST_END;
1690 if (count && k - 1 == count)
1691 status = SILC_STATUS_LIST_END;
1692 if (count && k - 1 > count)
1695 /* Send IDENTIFY reply */
1696 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1698 memset(uh, 0, sizeof(uh));
1699 memset(nh, 0, sizeof(nh));
1701 strncat(nh, entry->nickname, strlen(entry->nickname));
1702 if (!strchr(entry->nickname, '@')) {
1703 strncat(nh, "@", 1);
1704 if (entry->servername) {
1705 strncat(nh, entry->servername, strlen(entry->servername));
1707 len = entry->router ? strlen(entry->router->server_name) :
1708 strlen(server->server_name);
1709 strncat(nh, entry->router ? entry->router->server_name :
1710 server->server_name, len);
1714 if (!entry->username) {
1715 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1717 2, idp->data, idp->len,
1720 strncat(uh, entry->username, strlen(entry->username));
1721 if (!strchr(entry->username, '@')) {
1722 strncat(uh, "@", 1);
1723 hsock = (SilcSocketConnection)entry->connection;
1724 len = strlen(hsock->hostname);
1725 strncat(uh, hsock->hostname, len);
1728 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1730 2, idp->data, idp->len,
1735 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1736 0, packet->data, packet->len, FALSE);
1738 silc_buffer_free(packet);
1739 silc_buffer_free(idp);
1745 status = (status == SILC_STATUS_LIST_ITEM ?
1746 SILC_STATUS_LIST_ITEM : SILC_STATUS_OK);
1749 SilcServerEntry entry;
1751 if (status == SILC_STATUS_OK && servers_count > 1)
1752 status = SILC_STATUS_LIST_START;
1754 for (i = 0, k = 0; i < servers_count; i++) {
1758 status = SILC_STATUS_LIST_ITEM;
1759 if (servers_count > 1 && k == servers_count - 1 && !channels_count)
1760 status = SILC_STATUS_LIST_END;
1761 if (count && k - 1 == count)
1762 status = SILC_STATUS_LIST_END;
1763 if (count && k - 1 > count)
1766 /* Send IDENTIFY reply */
1767 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1768 if (entry->server_name) {
1770 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1772 2, idp->data, idp->len,
1773 3, entry->server_name,
1774 strlen(entry->server_name));
1776 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1778 2, idp->data, idp->len);
1781 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1782 0, packet->data, packet->len, FALSE);
1784 silc_buffer_free(packet);
1785 silc_buffer_free(idp);
1791 status = (status == SILC_STATUS_LIST_ITEM ?
1792 SILC_STATUS_LIST_ITEM : SILC_STATUS_OK);
1795 SilcChannelEntry entry;
1797 if (status == SILC_STATUS_OK && channels_count > 1)
1798 status = SILC_STATUS_LIST_START;
1800 for (i = 0, k = 0; i < channels_count; i++) {
1801 entry = channels[i];
1804 status = SILC_STATUS_LIST_ITEM;
1805 if (channels_count > 1 && k == channels_count - 1)
1806 status = SILC_STATUS_LIST_END;
1807 if (count && k - 1 == count)
1808 status = SILC_STATUS_LIST_END;
1809 if (count && k - 1 > count)
1812 /* Send IDENTIFY reply */
1813 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1814 if (entry->channel_name) {
1816 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1818 2, idp->data, idp->len,
1819 3, entry->channel_name,
1820 strlen(entry->channel_name));
1822 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1824 2, idp->data, idp->len);
1827 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1828 0, packet->data, packet->len, FALSE);
1830 silc_buffer_free(packet);
1831 silc_buffer_free(idp);
1839 silc_server_command_identify_process(SilcServerCommandContext cmd)
1841 SilcServer server = cmd->server;
1844 SilcClientEntry *clients = NULL;
1845 SilcServerEntry *servers = NULL;
1846 SilcChannelEntry *channels = NULL;
1847 uint32 clients_count = 0, servers_count = 0, channels_count = 0;
1849 /* Protocol dictates that we must always send the received IDENTIFY request
1850 to our router if we are normal server, so let's do it now unless we
1851 are standalone. We will not send any replies to the client until we
1852 have received reply from the router. */
1853 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1854 server->server_type == SILC_SERVER && !cmd->pending &&
1855 !server->standalone) {
1859 old_ident = silc_command_get_ident(cmd->payload);
1860 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
1861 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1863 /* Send IDENTIFY command to our router */
1864 silc_server_packet_send(server, (SilcSocketConnection)
1865 server->router->connection,
1866 SILC_PACKET_COMMAND, cmd->packet->flags,
1867 tmpbuf->data, tmpbuf->len, TRUE);
1869 /* Reprocess this packet after received reply from router */
1870 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
1871 silc_command_get_ident(cmd->payload),
1872 silc_server_command_destructor,
1873 silc_server_command_identify,
1874 silc_server_command_dup(cmd));
1875 cmd->pending = TRUE;
1877 silc_command_set_ident(cmd->payload, old_ident);
1879 silc_buffer_free(tmpbuf);
1884 /* We are ready to process the command request. Let's search for the
1885 requested client and send reply to the requesting client. */
1887 /* Parse the IDENTIFY request */
1888 if (!silc_server_command_identify_parse(cmd,
1889 &clients, &clients_count,
1890 &servers, &servers_count,
1891 &channels, &channels_count,
1895 /* Check that all mandatory fields are present and request those data
1896 from the server who owns the client if necessary. */
1897 if (clients && !silc_server_command_identify_check_client(cmd, clients,
1903 /* Send the command reply to the client */
1904 silc_server_command_identify_send_reply(cmd,
1905 clients, clients_count,
1906 servers, servers_count,
1907 channels, channels_count,
1913 silc_free(channels);
1918 SILC_SERVER_CMD_FUNC(identify)
1920 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1923 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 3328);
1925 ret = silc_server_command_identify_process(cmd);
1928 silc_server_command_free(cmd);
1931 /* Checks string for bad characters and returns TRUE if they are found. */
1933 static int silc_server_command_bad_chars(char *nick)
1935 if (strchr(nick, '\\')) return TRUE;
1936 if (strchr(nick, '\"')) return TRUE;
1937 if (strchr(nick, '´')) return TRUE;
1938 if (strchr(nick, '`')) return TRUE;
1939 if (strchr(nick, '\'')) return TRUE;
1940 if (strchr(nick, '*')) return TRUE;
1941 if (strchr(nick, '/')) return TRUE;
1942 if (strchr(nick, '@')) return TRUE;
1947 /* Server side of command NICK. Sets nickname for user. Setting
1948 nickname causes generation of a new client ID for the client. The
1949 new client ID is sent to the client after changing the nickname. */
1951 SILC_SERVER_CMD_FUNC(nick)
1953 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1954 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1955 SilcServer server = cmd->server;
1956 SilcBuffer packet, nidp, oidp;
1957 SilcClientID *new_id;
1959 uint16 ident = silc_command_get_ident(cmd->payload);
1962 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1965 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
1967 /* Check nickname */
1968 nick = silc_argument_get_arg_type(cmd->args, 1, NULL);
1969 if (silc_server_command_bad_chars(nick) == TRUE) {
1970 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
1971 SILC_STATUS_ERR_BAD_NICKNAME);
1975 if (strlen(nick) > 128)
1978 /* Create new Client ID */
1979 while (!silc_id_create_client_id(cmd->server, cmd->server->id,
1981 cmd->server->md5hash, nick,
1984 snprintf(&nick[strlen(nick) - 1], 1, "%d", nickfail);
1987 /* Send notify about nickname change to our router. We send the new
1988 ID and ask to replace it with the old one. If we are router the
1989 packet is broadcasted. Send NICK_CHANGE notify. */
1990 if (!server->standalone)
1991 silc_server_send_notify_nick_change(server, server->router->connection,
1992 server->server_type == SILC_SERVER ?
1993 FALSE : TRUE, client->id,
1996 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1998 /* Remove old cache entry */
1999 silc_idcache_del_by_context(server->local_list->clients, client);
2002 silc_free(client->id);
2004 /* Save the nickname as this client is our local client */
2005 silc_free(client->nickname);
2007 client->nickname = strdup(nick);
2008 client->id = new_id;
2010 /* Update client cache */
2011 silc_idcache_add(server->local_list->clients, client->nickname,
2012 client->id, (void *)client, FALSE);
2014 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2016 /* Send NICK_CHANGE notify to the client's channels */
2017 silc_server_send_notify_on_channels(server, NULL, client,
2018 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
2019 oidp->data, oidp->len,
2020 nidp->data, nidp->len);
2022 /* Send the new Client ID as reply command back to client */
2023 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
2024 SILC_STATUS_OK, ident, 1,
2025 2, nidp->data, nidp->len);
2026 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2027 0, packet->data, packet->len, FALSE);
2029 silc_buffer_free(packet);
2030 silc_buffer_free(nidp);
2031 silc_buffer_free(oidp);
2034 silc_server_command_free(cmd);
2037 /* Sends the LIST command reply */
2040 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
2041 SilcChannelEntry *lch,
2043 SilcChannelEntry *gch,
2047 SilcBuffer packet, idp;
2048 SilcChannelEntry entry;
2049 SilcCommandStatus status;
2050 uint16 ident = silc_command_get_ident(cmd->payload);
2052 unsigned char usercount[4];
2055 for (i = 0; i < lch_count; i++)
2056 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
2058 for (i = 0; i < gch_count; i++)
2059 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
2062 status = SILC_STATUS_OK;
2063 if ((lch_count + gch_count) > 1)
2064 status = SILC_STATUS_LIST_START;
2067 for (i = 0; i < lch_count; i++) {
2074 status = SILC_STATUS_LIST_ITEM;
2076 if (i == lch_count - 1 && gch_count)
2078 if (lch_count > 1 && i == lch_count - 1)
2079 status = SILC_STATUS_LIST_END;
2081 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
2083 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
2084 topic = "*private*";
2085 memset(usercount, 0, sizeof(usercount));
2087 topic = entry->topic;
2088 users = silc_hash_table_count(entry->user_list);
2089 SILC_PUT32_MSB(users, usercount);
2092 /* Send the reply */
2095 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
2097 2, idp->data, idp->len,
2098 3, entry->channel_name,
2099 strlen(entry->channel_name),
2100 4, topic, strlen(topic),
2104 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
2106 2, idp->data, idp->len,
2107 3, entry->channel_name,
2108 strlen(entry->channel_name),
2110 silc_server_packet_send(cmd->server, cmd->sock,
2111 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
2112 packet->len, FALSE);
2113 silc_buffer_free(packet);
2114 silc_buffer_free(idp);
2117 status = i ? SILC_STATUS_LIST_ITEM : SILC_STATUS_OK;
2120 for (i = 0; i < gch_count; i++) {
2127 status = SILC_STATUS_LIST_ITEM;
2129 if (gch_count > 1 && i == lch_count - 1)
2130 status = SILC_STATUS_LIST_END;
2132 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
2134 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
2135 topic = "*private*";
2136 memset(usercount, 0, sizeof(usercount));
2138 topic = entry->topic;
2139 users = silc_hash_table_count(entry->user_list);
2140 SILC_PUT32_MSB(users, usercount);
2143 /* Send the reply */
2146 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
2148 2, idp->data, idp->len,
2149 3, entry->channel_name,
2150 strlen(entry->channel_name),
2151 4, topic, strlen(topic),
2155 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
2157 2, idp->data, idp->len,
2158 3, entry->channel_name,
2159 strlen(entry->channel_name),
2161 silc_server_packet_send(cmd->server, cmd->sock,
2162 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
2163 packet->len, FALSE);
2164 silc_buffer_free(packet);
2165 silc_buffer_free(idp);
2169 /* Server side of LIST command. This lists the channel of the requested
2170 server. Secret channels are not listed. */
2172 SILC_SERVER_CMD_FUNC(list)
2174 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2175 SilcServer server = cmd->server;
2176 SilcChannelID *channel_id = NULL;
2179 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
2180 uint32 lch_count = 0, gch_count = 0;
2182 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 2);
2184 /* Get Channel ID */
2185 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2187 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2189 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
2190 SILC_STATUS_ERR_NO_CHANNEL_ID);
2195 /* Get the channels from local list */
2196 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
2199 /* Get the channels from global list if we are router */
2200 if (server->server_type == SILC_ROUTER)
2201 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
2204 /* Send the reply */
2205 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
2206 gchannels, gch_count);
2209 silc_server_command_free(cmd);
2212 /* Server side of TOPIC command. Sets topic for channel and/or returns
2213 current topic to client. */
2215 SILC_SERVER_CMD_FUNC(topic)
2217 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2218 SilcServer server = cmd->server;
2219 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2220 SilcChannelID *channel_id;
2221 SilcChannelEntry channel;
2222 SilcChannelClientEntry chl;
2223 SilcBuffer packet, idp;
2225 uint32 argc, tmp_len;
2226 uint16 ident = silc_command_get_ident(cmd->payload);
2228 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
2230 argc = silc_argument_get_arg_num(cmd->args);
2232 /* Get Channel ID */
2233 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2235 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2236 SILC_STATUS_ERR_NO_CHANNEL_ID);
2239 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
2241 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2242 SILC_STATUS_ERR_NO_CHANNEL_ID);
2246 /* Check whether the channel exists */
2247 channel = silc_idlist_find_channel_by_id(server->local_list,
2250 channel = silc_idlist_find_channel_by_id(server->global_list,
2253 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2254 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2261 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
2263 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2264 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2268 if (strlen(tmp) > 256) {
2269 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2270 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2274 /* See whether the client is on channel and has rights to change topic */
2275 if (!silc_hash_table_find(channel->user_list, client, NULL,
2277 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2278 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2282 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2283 if (channel->mode & SILC_CHANNEL_MODE_TOPIC) {
2284 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2285 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2290 /* Set the topic for channel */
2292 silc_free(channel->topic);
2293 channel->topic = strdup(tmp);
2295 /* Send TOPIC_SET notify type to the network */
2296 if (!server->standalone)
2297 silc_server_send_notify_topic_set(server, server->router->connection,
2298 server->server_type == SILC_ROUTER ?
2299 TRUE : FALSE, channel, client->id,
2302 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2304 /* Send notify about topic change to all clients on the channel */
2305 silc_server_send_notify_to_channel(server, NULL, channel, TRUE,
2306 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
2307 idp->data, idp->len,
2308 channel->topic, strlen(channel->topic));
2309 silc_buffer_free(idp);
2312 /* Send the topic to client as reply packet */
2313 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2315 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2316 SILC_STATUS_OK, ident, 2,
2317 2, idp->data, idp->len,
2319 strlen(channel->topic));
2321 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2322 SILC_STATUS_OK, ident, 1,
2323 2, idp->data, idp->len);
2324 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2325 0, packet->data, packet->len, FALSE);
2327 silc_buffer_free(packet);
2328 silc_buffer_free(idp);
2329 silc_free(channel_id);
2332 silc_server_command_free(cmd);
2335 /* Server side of INVITE command. Invites some client to join some channel.
2336 This command is also used to manage the invite list of the channel. */
2338 SILC_SERVER_CMD_FUNC(invite)
2340 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2341 SilcServer server = cmd->server;
2342 SilcSocketConnection sock = cmd->sock, dest_sock;
2343 SilcChannelClientEntry chl;
2344 SilcClientEntry sender, dest;
2345 SilcClientID *dest_id = NULL;
2346 SilcChannelEntry channel;
2347 SilcChannelID *channel_id = NULL;
2348 SilcIDListData idata;
2349 SilcBuffer idp, idp2, packet;
2350 unsigned char *tmp, *add, *del;
2352 uint16 ident = silc_command_get_ident(cmd->payload);
2354 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
2356 /* Get Channel ID */
2357 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2359 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2360 SILC_STATUS_ERR_NO_CHANNEL_ID);
2363 channel_id = silc_id_payload_parse_id(tmp, len);
2365 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2366 SILC_STATUS_ERR_NO_CHANNEL_ID);
2370 /* Get the channel entry */
2371 channel = silc_idlist_find_channel_by_id(server->local_list,
2374 channel = silc_idlist_find_channel_by_id(server->global_list,
2377 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2378 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
2383 /* Check whether the sender of this command is on the channel. */
2384 sender = (SilcClientEntry)sock->user_data;
2385 if (!silc_server_client_on_channel(sender, channel)) {
2386 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2387 SILC_STATUS_ERR_NOT_ON_CHANNEL);
2391 /* Check whether the channel is invite-only channel. If yes then the
2392 sender of this command must be at least channel operator. */
2393 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
2394 silc_hash_table_find(channel->user_list, sender, NULL, (void *)&chl);
2395 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
2396 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2397 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
2402 /* Get destination client ID */
2403 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
2407 dest_id = silc_id_payload_parse_id(tmp, len);
2409 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2410 SILC_STATUS_ERR_NO_CLIENT_ID);
2414 /* Get the client entry */
2415 dest = silc_server_get_client_resolve(server, dest_id);
2417 if (server->server_type == SILC_ROUTER) {
2418 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2419 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2423 /* The client info is being resolved. Reprocess this packet after
2424 receiving the reply to the query. */
2425 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2427 silc_server_command_destructor,
2428 silc_server_command_invite,
2429 silc_server_command_dup(cmd));
2430 cmd->pending = TRUE;
2431 silc_free(channel_id);
2436 /* Check whether the requested client is already on the channel. */
2437 if (silc_server_client_on_channel(dest, channel)) {
2438 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2439 SILC_STATUS_ERR_USER_ON_CHANNEL);
2443 /* Get route to the client */
2444 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id, &idata);
2446 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2447 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2451 memset(invite, 0, sizeof(invite));
2452 strncat(invite, dest->nickname, strlen(dest->nickname));
2453 strncat(invite, "!", 1);
2454 strncat(invite, dest->username, strlen(dest->username));
2455 if (!strchr(dest->username, '@')) {
2456 strncat(invite, "@", 1);
2457 strncat(invite, cmd->sock->hostname, strlen(cmd->sock->hostname));
2460 len = strlen(invite);
2461 if (!channel->invite_list)
2462 channel->invite_list = silc_calloc(len + 2,
2463 sizeof(*channel->invite_list));
2465 channel->invite_list = silc_realloc(channel->invite_list,
2466 sizeof(*channel->invite_list) *
2468 strlen(channel->invite_list) + 2));
2469 strncat(channel->invite_list, invite, len);
2470 strncat(channel->invite_list, ",", 1);
2472 /* Send notify to the client that is invited to the channel */
2473 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2474 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
2475 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
2477 SILC_NOTIFY_TYPE_INVITE, 3,
2478 idp->data, idp->len,
2479 channel->channel_name,
2480 strlen(channel->channel_name),
2481 idp2->data, idp2->len);
2482 silc_buffer_free(idp);
2483 silc_buffer_free(idp2);
2486 /* Add the client to the invite list of the channel */
2487 add = silc_argument_get_arg_type(cmd->args, 3, &len);
2489 if (!channel->invite_list)
2490 channel->invite_list = silc_calloc(len + 2,
2491 sizeof(*channel->invite_list));
2493 channel->invite_list = silc_realloc(channel->invite_list,
2494 sizeof(*channel->invite_list) *
2496 strlen(channel->invite_list) + 2));
2497 if (add[len - 1] == ',')
2498 add[len - 1] = '\0';
2500 strncat(channel->invite_list, add, len);
2501 strncat(channel->invite_list, ",", 1);
2504 /* Get the invite to be removed and remove it from the list */
2505 del = silc_argument_get_arg_type(cmd->args, 4, &len);
2506 if (del && channel->invite_list) {
2507 char *start, *end, *n;
2509 if (!strncmp(channel->invite_list, del,
2510 strlen(channel->invite_list) - 1)) {
2511 silc_free(channel->invite_list);
2512 channel->invite_list = NULL;
2514 start = strstr(channel->invite_list, del);
2515 if (start && strlen(start) >= len) {
2517 n = silc_calloc(strlen(channel->invite_list) - len, sizeof(*n));
2518 strncat(n, channel->invite_list, start - channel->invite_list);
2519 strncat(n, end + 1, ((channel->invite_list +
2520 strlen(channel->invite_list)) - end) - 1);
2521 silc_free(channel->invite_list);
2522 channel->invite_list = n;
2527 /* Send notify to the primary router */
2528 if (!server->standalone)
2529 silc_server_send_notify_invite(server, server->router->connection,
2530 server->server_type == SILC_ROUTER ?
2531 TRUE : FALSE, channel,
2532 sender->id, add, del);
2534 /* Send command reply */
2535 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2539 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2540 SILC_STATUS_OK, ident, 2,
2542 3, channel->invite_list,
2543 channel->invite_list ?
2544 strlen(channel->invite_list) : 0);
2547 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2548 SILC_STATUS_OK, ident, 1,
2550 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2551 packet->data, packet->len, FALSE);
2552 silc_buffer_free(packet);
2558 silc_free(channel_id);
2559 silc_server_command_free(cmd);
2564 SilcSocketConnection sock;
2568 /* Quits connection to client. This gets called if client won't
2569 close the connection even when it has issued QUIT command. */
2571 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
2573 QuitInternal q = (QuitInternal)context;
2575 /* Free all client specific data, such as client entry and entires
2576 on channels this client may be on. */
2577 silc_server_free_client_data(q->server, q->sock, q->sock->user_data,
2579 q->sock->user_data = NULL;
2581 /* Close the connection on our side */
2582 silc_server_close_connection(q->server, q->sock);
2584 silc_free(q->signoff);
2588 /* Quits SILC session. This is the normal way to disconnect client. */
2590 SILC_SERVER_CMD_FUNC(quit)
2592 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2593 SilcServer server = cmd->server;
2594 SilcSocketConnection sock = cmd->sock;
2596 unsigned char *tmp = NULL;
2599 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
2601 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2604 /* Get destination ID */
2605 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2609 q = silc_calloc(1, sizeof(*q));
2612 q->signoff = tmp ? strdup(tmp) : NULL;
2614 /* We quit the connection with little timeout */
2615 silc_schedule_task_add(server->schedule, sock->sock,
2616 silc_server_command_quit_cb, (void *)q,
2617 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2620 silc_server_command_free(cmd);
2623 /* Server side of command KILL. This command is used by router operator
2624 to remove an client from the SILC Network temporarily. */
2626 SILC_SERVER_CMD_FUNC(kill)
2628 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2629 SilcServer server = cmd->server;
2630 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2631 SilcClientEntry remote_client;
2632 SilcClientID *client_id;
2633 unsigned char *tmp, *comment;
2634 uint32 tmp_len, tmp_len2;
2636 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 2);
2638 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2641 /* KILL command works only on router */
2642 if (server->server_type != SILC_ROUTER) {
2643 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2644 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2648 /* Check whether client has the permissions. */
2649 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2650 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2651 SILC_STATUS_ERR_NO_ROUTER_PRIV);
2655 /* Get the client ID */
2656 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2658 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2659 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2662 client_id = silc_id_payload_parse_id(tmp, tmp_len);
2664 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2665 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2669 /* Get the client entry */
2670 remote_client = silc_idlist_find_client_by_id(server->local_list,
2671 client_id, TRUE, NULL);
2672 if (!remote_client) {
2673 remote_client = silc_idlist_find_client_by_id(server->global_list,
2674 client_id, TRUE, NULL);
2675 if (!remote_client) {
2676 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2677 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
2683 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
2687 /* Send reply to the sender */
2688 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2691 /* Send the KILL notify packets. First send it to the channel, then
2692 to our primary router and then directly to the client who is being
2693 killed right now. */
2695 /* Send KILLED notify to the channels. It is not sent to the client
2696 as it will be sent differently destined directly to the client and not
2698 silc_server_send_notify_on_channels(server, remote_client,
2699 remote_client, SILC_NOTIFY_TYPE_KILLED,
2702 comment, comment ? tmp_len2 : 0);
2704 /* Send KILLED notify to primary route */
2705 if (!server->standalone)
2706 silc_server_send_notify_killed(server, server->router->connection, TRUE,
2707 remote_client->id, comment);
2709 /* Send KILLED notify to the client directly */
2710 silc_server_send_notify_killed(server, remote_client->connection ?
2711 remote_client->connection :
2712 remote_client->router->connection, FALSE,
2713 remote_client->id, comment);
2715 /* Remove the client from all channels. This generates new keys to the
2716 channels as well. */
2717 silc_server_remove_from_channels(server, NULL, remote_client, FALSE,
2720 /* Remove the client entry, If it is locally connected then we will also
2721 disconnect the client here */
2722 if (remote_client->connection) {
2723 /* Remove locally conneted client */
2724 SilcSocketConnection sock = remote_client->connection;
2725 silc_server_free_client_data(server, sock, remote_client, FALSE, NULL);
2726 silc_server_close_connection(server, sock);
2728 /* Remove remote client */
2729 if (!silc_idlist_del_client(server->global_list, remote_client))
2730 silc_idlist_del_client(server->local_list, remote_client);
2734 silc_server_command_free(cmd);
2737 /* Server side of command INFO. This sends information about us to
2738 the client. If client requested specific server we will send the
2739 command to that server. */
2741 SILC_SERVER_CMD_FUNC(info)
2743 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2744 SilcServer server = cmd->server;
2745 SilcBuffer packet, idp;
2748 char *dest_server, *server_info = NULL, *server_name;
2749 uint16 ident = silc_command_get_ident(cmd->payload);
2750 SilcServerEntry entry = NULL;
2751 SilcServerID *server_id = NULL;
2753 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
2755 /* Get server name */
2756 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2759 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2761 server_id = silc_id_payload_parse_id(tmp, tmp_len);
2763 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2764 SILC_STATUS_ERR_NO_SERVER_ID);
2770 /* Check whether we have this server cached */
2771 entry = silc_idlist_find_server_by_id(server->local_list,
2772 server_id, TRUE, NULL);
2774 entry = silc_idlist_find_server_by_id(server->global_list,
2775 server_id, TRUE, NULL);
2776 if (!entry && server->server_type == SILC_ROUTER) {
2777 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2778 SILC_STATUS_ERR_NO_SUCH_SERVER);
2784 /* Some buggy servers has sent request to router about themselves. */
2785 if (server->server_type == SILC_ROUTER && cmd->sock->user_data == entry)
2788 if ((!dest_server && !server_id && !entry) || (entry &&
2789 entry == server->id_entry) ||
2790 (dest_server && !cmd->pending &&
2791 !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
2792 /* Send our reply */
2793 char info_string[256];
2795 memset(info_string, 0, sizeof(info_string));
2796 snprintf(info_string, sizeof(info_string),
2797 "location: %s server: %s admin: %s <%s>",
2798 server->config->admin_info->location,
2799 server->config->admin_info->server_type,
2800 server->config->admin_info->admin_name,
2801 server->config->admin_info->admin_email);
2803 server_info = info_string;
2804 entry = server->id_entry;
2806 /* Check whether we have this server cached */
2807 if (!entry && dest_server) {
2808 entry = silc_idlist_find_server_by_name(server->global_list,
2809 dest_server, TRUE, NULL);
2811 entry = silc_idlist_find_server_by_name(server->local_list,
2812 dest_server, TRUE, NULL);
2816 if (!cmd->pending &&
2817 server->server_type == SILC_ROUTER && entry && !entry->server_info) {
2818 /* Send to the server */
2822 old_ident = silc_command_get_ident(cmd->payload);
2823 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2824 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2826 silc_server_packet_send(server, entry->connection,
2827 SILC_PACKET_COMMAND, cmd->packet->flags,
2828 tmpbuf->data, tmpbuf->len, TRUE);
2830 /* Reprocess this packet after received reply from router */
2831 silc_server_command_pending(server, SILC_COMMAND_INFO,
2832 silc_command_get_ident(cmd->payload),
2833 silc_server_command_destructor,
2834 silc_server_command_info,
2835 silc_server_command_dup(cmd));
2836 cmd->pending = TRUE;
2837 silc_command_set_ident(cmd->payload, old_ident);
2838 silc_buffer_free(tmpbuf);
2842 if (!entry && !cmd->pending && !server->standalone) {
2843 /* Send to the primary router */
2847 old_ident = silc_command_get_ident(cmd->payload);
2848 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
2849 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2851 silc_server_packet_send(server, server->router->connection,
2852 SILC_PACKET_COMMAND, cmd->packet->flags,
2853 tmpbuf->data, tmpbuf->len, TRUE);
2855 /* Reprocess this packet after received reply from router */
2856 silc_server_command_pending(server, SILC_COMMAND_INFO,
2857 silc_command_get_ident(cmd->payload),
2858 silc_server_command_destructor,
2859 silc_server_command_info,
2860 silc_server_command_dup(cmd));
2861 cmd->pending = TRUE;
2862 silc_command_set_ident(cmd->payload, old_ident);
2863 silc_buffer_free(tmpbuf);
2869 silc_free(server_id);
2872 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2873 SILC_STATUS_ERR_NO_SUCH_SERVER);
2877 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2879 server_info = entry->server_info;
2880 server_name = entry->server_name;
2882 /* Send the reply */
2884 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2885 SILC_STATUS_OK, ident, 3,
2886 2, idp->data, idp->len,
2888 strlen(server_name),
2890 strlen(server_info));
2892 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
2893 SILC_STATUS_OK, ident, 2,
2894 2, idp->data, idp->len,
2896 strlen(server_name));
2897 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2898 packet->data, packet->len, FALSE);
2900 silc_buffer_free(packet);
2901 silc_buffer_free(idp);
2904 silc_server_command_free(cmd);
2907 /* Server side of command PING. This just replies to the ping. */
2909 SILC_SERVER_CMD_FUNC(ping)
2911 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2912 SilcServer server = cmd->server;
2917 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 1, 2);
2920 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2922 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2923 SILC_STATUS_ERR_NO_SERVER_ID);
2926 id = silc_id_str2id(tmp, len, SILC_ID_SERVER);
2930 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2931 /* Send our reply */
2932 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2935 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
2936 SILC_STATUS_ERR_NO_SUCH_SERVER);
2943 silc_server_command_free(cmd);
2946 /* Internal routine to join channel. The channel sent to this function
2947 has been either created or resolved from ID lists. This joins the sent
2948 client to the channel. */
2950 static void silc_server_command_join_channel(SilcServer server,
2951 SilcServerCommandContext cmd,
2952 SilcChannelEntry channel,
2953 SilcClientID *client_id,
2957 SilcSocketConnection sock = cmd->sock;
2959 uint32 tmp_len, user_count;
2960 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
2961 SilcClientEntry client;
2962 SilcChannelClientEntry chl;
2963 SilcBuffer reply, chidp, clidp, keyp = NULL, user_list, mode_list;
2964 uint16 ident = silc_command_get_ident(cmd->payload);
2965 char check[512], check2[512];
2967 SILC_LOG_DEBUG(("Start"));
2972 /* Get the client entry */
2973 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2974 client = (SilcClientEntry)sock->user_data;
2976 client = silc_server_get_client_resolve(server, client_id);
2981 /* The client info is being resolved. Reprocess this packet after
2982 receiving the reply to the query. */
2983 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2984 server->cmd_ident, NULL,
2985 silc_server_command_join,
2986 silc_server_command_dup(cmd));
2987 cmd->pending = TRUE;
2991 cmd->pending = FALSE;
2995 * Check channel modes
2998 memset(check, 0, sizeof(check));
2999 memset(check2, 0, sizeof(check2));
3000 strncat(check, client->nickname, strlen(client->nickname));
3001 strncat(check, "!", 1);
3002 strncat(check, client->username, strlen(client->username));
3003 if (!strchr(client->username, '@')) {
3004 strncat(check, "@", 1);
3005 strncat(check, cmd->sock->hostname, strlen(cmd->sock->hostname));
3008 strncat(check2, client->nickname, strlen(client->nickname));
3009 if (!strchr(client->nickname, '@')) {
3010 strncat(check2, "@", 1);
3011 strncat(check2, server->server_name, strlen(server->server_name));
3013 strncat(check2, "!", 1);
3014 strncat(check2, client->username, strlen(client->username));
3015 if (!strchr(client->username, '@')) {
3016 strncat(check2, "@", 1);
3017 strncat(check2, cmd->sock->hostname, strlen(cmd->sock->hostname));
3020 /* Check invite list if channel is invite-only channel */
3021 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
3022 if (!channel->invite_list ||
3023 (!silc_string_match(channel->invite_list, check) &&
3024 !silc_string_match(channel->invite_list, check2))) {
3025 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3026 SILC_STATUS_ERR_NOT_INVITED);
3031 /* Check ban list if it exists. If the client's nickname, server,
3032 username and/or hostname is in the ban list the access to the
3033 channel is denied. */
3034 if (channel->ban_list) {
3035 if (silc_string_match(channel->ban_list, check) ||
3036 silc_string_match(channel->ban_list, check2)) {
3037 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3038 SILC_STATUS_ERR_BANNED_FROM_CHANNEL);
3043 /* Get passphrase */
3044 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3046 passphrase = silc_calloc(tmp_len, sizeof(*passphrase));
3047 memcpy(passphrase, tmp, tmp_len);
3050 /* Check the channel passphrase if set. */
3051 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3052 if (!passphrase || memcmp(channel->passphrase, passphrase,
3053 strlen(channel->passphrase))) {
3054 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3055 SILC_STATUS_ERR_BAD_PASSWORD);
3060 /* Check user count limit if set. */
3061 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
3062 if (silc_hash_table_count(channel->user_list) + 1 >
3063 channel->user_limit) {
3064 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3065 SILC_STATUS_ERR_CHANNEL_IS_FULL);
3071 * Client is allowed to join to the channel. Make it happen.
3074 /* Check whether the client already is on the channel */
3075 if (silc_server_client_on_channel(client, channel)) {
3076 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3077 SILC_STATUS_ERR_USER_ON_CHANNEL);
3081 /* Generate new channel key as protocol dictates */
3082 if ((!created && silc_hash_table_count(channel->user_list) > 0) ||
3083 !channel->channel_key)
3084 if (!silc_server_create_channel_key(server, channel, 0))
3087 /* Send the channel key. This is broadcasted to the channel but is not
3088 sent to the client who is joining to the channel. */
3089 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
3090 silc_server_send_channel_key(server, NULL, channel,
3091 server->server_type == SILC_ROUTER ?
3092 FALSE : !server->standalone);
3094 /* Join the client to the channel by adding it to channel's user list.
3095 Add also the channel to client entry's channels list for fast cross-
3097 chl = silc_calloc(1, sizeof(*chl));
3099 chl->client = client;
3100 chl->channel = channel;
3101 silc_hash_table_add(channel->user_list, client, chl);
3102 silc_hash_table_add(client->channels, channel, chl);
3104 /* Get users on the channel */
3105 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
3108 /* Encode Client ID Payload of the original client who wants to join */
3109 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3111 /* Encode command reply packet */
3112 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3113 SILC_PUT32_MSB(channel->mode, mode);
3114 SILC_PUT32_MSB(created, tmp2);
3115 SILC_PUT32_MSB(user_count, tmp3);
3117 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3118 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3119 keyp = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, tmp,
3120 strlen(channel->channel_key->
3122 channel->channel_key->cipher->name,
3123 channel->key_len / 8, channel->key);
3128 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
3129 SILC_STATUS_OK, ident, 13,
3130 2, channel->channel_name,
3131 strlen(channel->channel_name),
3132 3, chidp->data, chidp->len,
3133 4, clidp->data, clidp->len,
3136 7, keyp ? keyp->data : NULL,
3137 keyp ? keyp->len : 0,
3138 8, channel->ban_list,
3140 strlen(channel->ban_list) : 0,
3141 9, channel->invite_list,
3142 channel->invite_list ?
3143 strlen(channel->invite_list) : 0,
3146 strlen(channel->topic) : 0,
3147 11, channel->hmac->hmac->name,
3148 strlen(channel->hmac->hmac->name),
3150 13, user_list->data, user_list->len,
3151 14, mode_list->data,
3154 /* Send command reply */
3155 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
3156 reply->data, reply->len, FALSE);
3158 if (!cmd->pending) {
3159 /* Send JOIN notify to locally connected clients on the channel */
3160 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3161 SILC_NOTIFY_TYPE_JOIN, 2,
3162 clidp->data, clidp->len,
3163 chidp->data, chidp->len);
3165 /* Send JOIN notify packet to our primary router */
3166 if (!server->standalone)
3167 silc_server_send_notify_join(server, server->router->connection,
3168 server->server_type == SILC_ROUTER ?
3169 TRUE : FALSE, channel, client->id);
3172 silc_buffer_free(reply);
3173 silc_buffer_free(clidp);
3174 silc_buffer_free(chidp);
3175 silc_buffer_free(keyp);
3176 silc_buffer_free(user_list);
3177 silc_buffer_free(mode_list);
3181 silc_free(passphrase);
3184 /* Server side of command JOIN. Joins client into requested channel. If
3185 the channel does not exist it will be created. */
3187 SILC_SERVER_CMD_FUNC(join)
3189 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3190 SilcServer server = cmd->server;
3192 char *tmp, *channel_name = NULL, *cipher, *hmac;
3193 SilcChannelEntry channel;
3195 int created = FALSE;
3196 SilcClientID *client_id;
3198 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 1, 4);
3200 /* Get channel name */
3201 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3203 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3204 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3209 if (strlen(channel_name) > 256)
3210 channel_name[255] = '\0';
3212 if (silc_server_command_bad_chars(channel_name) == TRUE) {
3213 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3214 SILC_STATUS_ERR_BAD_CHANNEL);
3218 /* Get Client ID of the client who is joining to the channel */
3219 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3221 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3222 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3225 client_id = silc_id_payload_parse_id(tmp, tmp_len);
3227 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3228 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3232 /* Get cipher and hmac name */
3233 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
3234 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
3236 /* See if the channel exists */
3237 channel = silc_idlist_find_channel_by_name(server->local_list,
3238 channel_name, NULL);
3240 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3241 /* If this is coming from client the Client ID in the command packet must
3242 be same as the client's ID. */
3243 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3244 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
3245 if (!SILC_ID_CLIENT_COMPARE(entry->id, client_id)) {
3246 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3247 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3252 if (!channel || !channel->id) {
3253 /* Channel not found */
3255 /* If we are standalone server we don't have a router, we just create
3256 the channel by ourselves. */
3257 if (server->standalone) {
3258 channel = silc_server_create_new_channel(server, server->id, cipher,
3259 hmac, channel_name, TRUE);
3261 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3262 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3266 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3271 /* The channel does not exist on our server. If we are normal server
3272 we will send JOIN command to our router which will handle the
3273 joining procedure (either creates the channel if it doesn't exist
3274 or joins the client to it). */
3275 if (server->server_type == SILC_SERVER) {
3279 /* If this is pending command callback then we've resolved
3280 it and it didn't work, return since we've notified the
3281 client already in the command reply callback. */
3285 old_ident = silc_command_get_ident(cmd->payload);
3286 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3287 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3289 /* Send JOIN command to our router */
3290 silc_server_packet_send(server, (SilcSocketConnection)
3291 server->router->connection,
3292 SILC_PACKET_COMMAND, cmd->packet->flags,
3293 tmpbuf->data, tmpbuf->len, TRUE);
3295 /* Reprocess this packet after received reply from router */
3296 silc_server_command_pending(server, SILC_COMMAND_JOIN,
3297 silc_command_get_ident(cmd->payload),
3298 silc_server_command_destructor,
3299 silc_server_command_join,
3300 silc_server_command_dup(cmd));
3301 cmd->pending = TRUE;
3305 /* We are router and the channel does not seem exist so we will check
3306 our global list as well for the channel. */
3307 channel = silc_idlist_find_channel_by_name(server->global_list,
3308 channel_name, NULL);
3310 /* Channel really does not exist, create it */
3311 channel = silc_server_create_new_channel(server, server->id, cipher,
3312 hmac, channel_name, TRUE);
3314 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3315 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3319 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3326 /* Channel not found */
3328 /* If the command came from router and/or we are normal server then
3329 something went wrong with the joining as the channel was not found.
3330 We can't do anything else but ignore this. */
3331 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
3332 server->server_type == SILC_SERVER)
3335 /* We are router and the channel does not seem exist so we will check
3336 our global list as well for the channel. */
3337 channel = silc_idlist_find_channel_by_name(server->global_list,
3338 channel_name, NULL);
3340 /* Channel really does not exist, create it */
3341 channel = silc_server_create_new_channel(server, server->id, cipher,
3342 hmac, channel_name, TRUE);
3344 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3345 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3349 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3355 /* If the channel does not have global users and is also empty it means the
3356 channel was created globally (by our router) and the client will be the
3357 channel founder and operator. */
3358 if (!channel->global_users && !silc_hash_table_count(channel->user_list)) {
3359 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3360 created = TRUE; /* Created globally by our router */
3363 /* Join to the channel */
3364 silc_server_command_join_channel(server, cmd, channel, client_id,
3367 silc_free(client_id);
3370 silc_server_command_free(cmd);
3373 /* Server side of command MOTD. Sends server's current "message of the
3374 day" to the client. */
3376 SILC_SERVER_CMD_FUNC(motd)
3378 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3379 SilcServer server = cmd->server;
3380 SilcBuffer packet, idp;
3381 char *motd, *dest_server;
3383 uint16 ident = silc_command_get_ident(cmd->payload);
3385 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
3387 /* Get server name */
3388 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
3390 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
3391 SILC_STATUS_ERR_NO_SUCH_SERVER);
3395 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
3398 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3400 if (server->config && server->config->motd &&
3401 server->config->motd->motd_file) {
3403 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
3408 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3409 SILC_STATUS_OK, ident, 2,
3415 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3416 SILC_STATUS_OK, ident, 1,
3420 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3421 packet->data, packet->len, FALSE);
3422 silc_buffer_free(packet);
3423 silc_buffer_free(idp);
3425 SilcServerEntry entry;
3427 /* Check whether we have this server cached */
3428 entry = silc_idlist_find_server_by_name(server->global_list,
3429 dest_server, TRUE, NULL);
3431 entry = silc_idlist_find_server_by_name(server->local_list,
3432 dest_server, TRUE, NULL);
3435 if (server->server_type == SILC_ROUTER && !cmd->pending &&
3436 entry && !entry->motd) {
3437 /* Send to the server */
3441 old_ident = silc_command_get_ident(cmd->payload);
3442 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3443 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3445 silc_server_packet_send(server, entry->connection,
3446 SILC_PACKET_COMMAND, cmd->packet->flags,
3447 tmpbuf->data, tmpbuf->len, TRUE);
3449 /* Reprocess this packet after received reply from router */
3450 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3451 silc_command_get_ident(cmd->payload),
3452 silc_server_command_destructor,
3453 silc_server_command_motd,
3454 silc_server_command_dup(cmd));
3455 cmd->pending = TRUE;
3456 silc_command_set_ident(cmd->payload, old_ident);
3457 silc_buffer_free(tmpbuf);
3461 if (!entry && !cmd->pending && !server->standalone) {
3462 /* Send to the primary router */
3466 old_ident = silc_command_get_ident(cmd->payload);
3467 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
3468 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3470 silc_server_packet_send(server, server->router->connection,
3471 SILC_PACKET_COMMAND, cmd->packet->flags,
3472 tmpbuf->data, tmpbuf->len, TRUE);
3474 /* Reprocess this packet after received reply from router */
3475 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3476 silc_command_get_ident(cmd->payload),
3477 silc_server_command_destructor,
3478 silc_server_command_motd,
3479 silc_server_command_dup(cmd));
3480 cmd->pending = TRUE;
3481 silc_command_set_ident(cmd->payload, old_ident);
3482 silc_buffer_free(tmpbuf);
3487 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3488 SILC_STATUS_ERR_NO_SUCH_SERVER);
3492 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3495 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3496 SILC_STATUS_OK, ident, 2,
3499 strlen(entry->motd));
3501 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3502 SILC_STATUS_OK, ident, 1,
3505 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3506 packet->data, packet->len, FALSE);
3507 silc_buffer_free(packet);
3508 silc_buffer_free(idp);
3512 silc_server_command_free(cmd);
3515 /* Server side of command UMODE. Client can use this command to set/unset
3516 user mode. Client actually cannot set itself to be as server/router
3517 operator so this can be used only to unset the modes. */
3519 SILC_SERVER_CMD_FUNC(umode)
3521 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3522 SilcServer server = cmd->server;
3523 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3525 unsigned char *tmp_mask;
3527 uint16 ident = silc_command_get_ident(cmd->payload);
3529 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3532 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 2, 2);
3534 /* Get the client's mode mask */
3535 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3537 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3538 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3541 SILC_GET32_MSB(mask, tmp_mask);
3547 if (mask & SILC_UMODE_SERVER_OPERATOR) {
3548 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR)) {
3549 /* Cannot operator mode */
3550 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3551 SILC_STATUS_ERR_PERM_DENIED);
3555 if (client->mode & SILC_UMODE_SERVER_OPERATOR)
3556 /* Remove the server operator rights */
3557 client->mode &= ~SILC_UMODE_SERVER_OPERATOR;
3560 if (mask & SILC_UMODE_ROUTER_OPERATOR) {
3561 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
3562 /* Cannot operator mode */
3563 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3564 SILC_STATUS_ERR_PERM_DENIED);
3568 if (client->mode & SILC_UMODE_ROUTER_OPERATOR)
3569 /* Remove the router operator rights */
3570 client->mode &= ~SILC_UMODE_ROUTER_OPERATOR;
3573 if (mask & SILC_UMODE_GONE) {
3574 client->mode |= SILC_UMODE_GONE;
3576 if (client->mode & SILC_UMODE_GONE)
3577 /* Remove the gone status */
3578 client->mode &= ~SILC_UMODE_GONE;
3581 /* Send UMODE change to primary router */
3582 if (!server->standalone)
3583 silc_server_send_notify_umode(server, server->router->connection, TRUE,
3584 client->id, client->mode);
3586 /* Send command reply to sender */
3587 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
3588 SILC_STATUS_OK, ident, 1,
3590 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3591 packet->data, packet->len, FALSE);
3592 silc_buffer_free(packet);
3595 silc_server_command_free(cmd);
3598 /* Checks that client has rights to add or remove channel modes. If any
3599 of the checks fails FALSE is returned. */
3601 int silc_server_check_cmode_rights(SilcChannelEntry channel,
3602 SilcChannelClientEntry client,
3605 int is_op = client->mode & SILC_CHANNEL_UMODE_CHANOP;
3606 int is_fo = client->mode & SILC_CHANNEL_UMODE_CHANFO;
3608 /* Check whether has rights to change anything */
3609 if (!is_op && !is_fo)
3612 /* Check whether has rights to change everything */
3616 /* We know that client is channel operator, check that they are not
3617 changing anything that requires channel founder rights. Rest of the
3618 modes are available automatically for channel operator. */
3620 if (mode & SILC_CHANNEL_MODE_PRIVKEY) {
3621 if (is_op && !is_fo)
3624 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3625 if (is_op && !is_fo)
3630 if (mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3631 if (is_op && !is_fo)
3634 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3635 if (is_op && !is_fo)
3640 if (mode & SILC_CHANNEL_MODE_CIPHER) {
3641 if (is_op && !is_fo)
3644 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3645 if (is_op && !is_fo)
3650 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3651 if (is_op && !is_fo)
3654 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3655 if (is_op && !is_fo)
3663 /* Server side command of CMODE. Changes channel mode */
3665 SILC_SERVER_CMD_FUNC(cmode)
3667 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3668 SilcServer server = cmd->server;
3669 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3670 SilcIDListData idata = (SilcIDListData)client;
3671 SilcChannelID *channel_id;
3672 SilcChannelEntry channel;
3673 SilcChannelClientEntry chl;
3674 SilcBuffer packet, cidp;
3675 unsigned char *tmp, *tmp_id, *tmp_mask;
3676 char *cipher = NULL, *hmac = NULL;
3677 uint32 mode_mask, tmp_len, tmp_len2;
3678 uint16 ident = silc_command_get_ident(cmd->payload);
3680 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 2, 7);
3682 /* Get Channel ID */
3683 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
3685 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3686 SILC_STATUS_ERR_NO_CHANNEL_ID);
3689 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2);
3691 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3692 SILC_STATUS_ERR_NO_CHANNEL_ID);
3696 /* Get the channel mode mask */
3697 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3699 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3700 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3703 SILC_GET32_MSB(mode_mask, tmp_mask);
3705 /* Get channel entry */
3706 channel = silc_idlist_find_channel_by_id(server->local_list,
3709 channel = silc_idlist_find_channel_by_id(server->global_list,
3712 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3713 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
3718 /* Check whether this client is on the channel */
3719 if (!silc_server_client_on_channel(client, channel)) {
3720 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3721 SILC_STATUS_ERR_NOT_ON_CHANNEL);
3725 /* Get entry to the channel user list */
3726 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
3728 /* Check that client has rights to change any requested channel modes */
3729 if (!silc_server_check_cmode_rights(channel, chl, mode_mask)) {
3730 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3731 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
3736 * Check the modes. Modes that requires nothing special operation are
3740 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
3741 /* Channel uses private keys to protect traffic. Client(s) has set the
3742 key locally they want to use, server does not know that key. */
3743 /* Nothing interesting to do here */
3745 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3746 /* The mode is removed and we need to generate and distribute
3747 new channel key. Clients are not using private channel keys
3748 anymore after this. */
3750 /* Re-generate channel key */
3751 if (!silc_server_create_channel_key(server, channel, 0))
3754 /* Send the channel key. This sends it to our local clients and if
3755 we are normal server to our router as well. */
3756 silc_server_send_channel_key(server, NULL, channel,
3757 server->server_type == SILC_ROUTER ?
3758 FALSE : !server->standalone);
3760 cipher = channel->channel_key->cipher->name;
3761 hmac = channel->hmac->hmac->name;
3765 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
3766 /* User limit is set on channel */
3769 /* Get user limit */
3770 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
3772 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
3773 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3774 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3778 SILC_GET32_MSB(user_limit, tmp);
3779 channel->user_limit = user_limit;
3782 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
3783 /* User limit mode is unset. Remove user limit */
3784 channel->user_limit = 0;
3787 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
3788 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
3789 /* Passphrase has been set to channel */
3791 /* Get the passphrase */
3792 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
3794 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3795 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3799 /* Save the passphrase */
3800 channel->passphrase = strdup(tmp);
3803 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3804 /* Passphrase mode is unset. remove the passphrase */
3805 if (channel->passphrase) {
3806 silc_free(channel->passphrase);
3807 channel->passphrase = NULL;
3812 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
3813 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
3814 /* Cipher to use protect the traffic */
3817 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
3819 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3820 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3824 /* Delete old cipher and allocate the new one */
3825 silc_cipher_free(channel->channel_key);
3826 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3827 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3828 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3832 /* Re-generate channel key */
3833 if (!silc_server_create_channel_key(server, channel, 0))
3836 /* Send the channel key. This sends it to our local clients and if
3837 we are normal server to our router as well. */
3838 silc_server_send_channel_key(server, NULL, channel,
3839 server->server_type == SILC_ROUTER ?
3840 FALSE : !server->standalone);
3843 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
3844 /* Cipher mode is unset. Remove the cipher and revert back to
3846 cipher = channel->cipher;
3848 /* Delete old cipher and allocate default one */
3849 silc_cipher_free(channel->channel_key);
3850 if (!silc_cipher_alloc(cipher ? cipher : SILC_DEFAULT_CIPHER,
3851 &channel->channel_key)) {
3852 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3853 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3857 /* Re-generate channel key */
3858 if (!silc_server_create_channel_key(server, channel, 0))
3861 /* Send the channel key. This sends it to our local clients and if
3862 we are normal server to our router as well. */
3863 silc_server_send_channel_key(server, NULL, channel,
3864 server->server_type == SILC_ROUTER ?
3865 FALSE : !server->standalone);
3869 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
3870 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
3871 /* HMAC to use protect the traffic */
3872 unsigned char hash[32];
3875 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
3877 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3878 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3882 /* Delete old hmac and allocate the new one */
3883 silc_hmac_free(channel->hmac);
3884 if (!silc_hmac_alloc(hmac, NULL, &channel->hmac)) {
3885 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3886 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3890 /* Set the HMAC key out of current channel key. The client must do
3892 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3894 silc_hmac_set_key(channel->hmac, hash,
3895 silc_hash_len(channel->hmac->hash));
3896 memset(hash, 0, sizeof(hash));
3899 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
3900 /* Hmac mode is unset. Remove the hmac and revert back to
3902 unsigned char hash[32];
3903 hmac = channel->hmac_name;
3905 /* Delete old hmac and allocate default one */
3906 silc_hmac_free(channel->hmac);
3907 if (!silc_hmac_alloc(hmac ? hmac : SILC_DEFAULT_HMAC, NULL,
3909 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3910 SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
3914 /* Set the HMAC key out of current channel key. The client must do
3916 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
3918 silc_hmac_set_key(channel->hmac, hash,
3919 silc_hash_len(channel->hmac->hash));
3920 memset(hash, 0, sizeof(hash));
3924 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3925 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3926 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
3927 /* Set the founder authentication */
3928 SilcAuthPayload auth;
3930 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
3932 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3933 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3937 auth = silc_auth_payload_parse(tmp, tmp_len);
3939 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3940 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
3944 /* Save the public key */
3945 tmp = silc_pkcs_public_key_encode(idata->public_key, &tmp_len);
3946 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
3949 channel->founder_method = silc_auth_get_method(auth);
3951 if (channel->founder_method == SILC_AUTH_PASSWORD) {
3952 tmp = silc_auth_get_data(auth, &tmp_len);
3953 channel->founder_passwd =
3954 silc_calloc(tmp_len + 1, sizeof(*channel->founder_passwd));
3955 memcpy(channel->founder_passwd, tmp, tmp_len);
3956 channel->founder_passwd_len = tmp_len;
3958 /* Verify the payload before setting the mode */
3959 if (!silc_auth_verify(auth, channel->founder_method,
3960 channel->founder_key, 0, idata->hash,
3961 client->id, SILC_ID_CLIENT)) {
3962 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3963 SILC_STATUS_ERR_AUTH_FAILED);
3968 silc_auth_payload_free(auth);
3972 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3973 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3974 if (channel->founder_key)
3975 silc_pkcs_public_key_free(channel->founder_key);
3976 if (channel->founder_passwd) {
3977 silc_free(channel->founder_passwd);
3978 channel->founder_passwd = NULL;
3984 /* Finally, set the mode */
3985 channel->mode = mode_mask;
3987 /* Send CMODE_CHANGE notify */
3988 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3989 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3990 SILC_NOTIFY_TYPE_CMODE_CHANGE, 4,
3991 cidp->data, cidp->len,
3993 cipher, cipher ? strlen(cipher) : 0,
3994 hmac, hmac ? strlen(hmac) : 0);
3996 /* Set CMODE notify type to network */
3997 if (!server->standalone)
3998 silc_server_send_notify_cmode(server, server->router->connection,
3999 server->server_type == SILC_ROUTER ?
4000 TRUE : FALSE, channel,
4001 mode_mask, client->id, SILC_ID_CLIENT,
4004 /* Send command reply to sender */
4005 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
4006 SILC_STATUS_OK, ident, 2,
4007 2, tmp_id, tmp_len2,
4009 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4010 packet->data, packet->len, FALSE);
4012 silc_buffer_free(packet);
4013 silc_free(channel_id);
4017 silc_server_command_free(cmd);
4020 /* Server side of CUMODE command. Changes client's mode on a channel. */
4022 SILC_SERVER_CMD_FUNC(cumode)
4024 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4025 SilcServer server = cmd->server;
4026 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4027 SilcIDListData idata = (SilcIDListData)client;
4028 SilcChannelID *channel_id;
4029 SilcClientID *client_id;
4030 SilcChannelEntry channel;
4031 SilcClientEntry target_client;
4032 SilcChannelClientEntry chl;
4033 SilcBuffer packet, idp;
4034 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
4035 uint32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
4037 uint16 ident = silc_command_get_ident(cmd->payload);
4039 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
4041 /* Get Channel ID */
4042 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
4044 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4045 SILC_STATUS_ERR_NO_CHANNEL_ID);
4048 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len);
4050 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4051 SILC_STATUS_ERR_NO_CHANNEL_ID);
4055 /* Get channel entry */
4056 channel = silc_idlist_find_channel_by_id(server->local_list,
4059 channel = silc_idlist_find_channel_by_id(server->global_list,
4062 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4063 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4068 /* Check whether sender is on the channel */
4069 if (!silc_server_client_on_channel(client, channel)) {
4070 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4071 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4075 /* Check that client has rights to change other's rights */
4076 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4077 sender_mask = chl->mode;
4079 /* Get the target client's channel mode mask */
4080 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
4082 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4083 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4086 SILC_GET32_MSB(target_mask, tmp_mask);
4088 /* Get target Client ID */
4089 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4091 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4092 SILC_STATUS_ERR_NO_CLIENT_ID);
4095 client_id = silc_id_payload_parse_id(tmp_id, tmp_len);
4097 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4098 SILC_STATUS_ERR_NO_CLIENT_ID);
4102 /* Get target client's entry */
4103 target_client = silc_idlist_find_client_by_id(server->local_list,
4104 client_id, TRUE, NULL);
4105 if (!target_client) {
4106 target_client = silc_idlist_find_client_by_id(server->global_list,
4107 client_id, TRUE, NULL);
4110 if (target_client != client &&
4111 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
4112 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
4113 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4114 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4118 /* Check whether target client is on the channel */
4119 if (target_client != client) {
4120 if (!silc_server_client_on_channel(target_client, channel)) {
4121 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4122 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
4126 /* Get entry to the channel user list */
4127 silc_hash_table_find(channel->user_list, target_client, NULL,
4135 /* If the target client is founder, no one else can change their mode
4137 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && chl->client != target_client) {
4138 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4139 SILC_STATUS_ERR_NOT_YOU);
4143 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
4144 /* The client tries to claim the founder rights. */
4145 unsigned char *tmp_auth;
4146 uint32 tmp_auth_len, auth_len;
4149 if (target_client != client) {
4150 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4151 SILC_STATUS_ERR_NOT_YOU);
4155 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
4156 !channel->founder_key) {
4157 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4158 SILC_STATUS_ERR_NOT_YOU);
4162 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
4164 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4165 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4169 auth = (channel->founder_method == SILC_AUTH_PASSWORD ?
4170 (void *)channel->founder_passwd : (void *)channel->founder_key);
4171 auth_len = (channel->founder_method == SILC_AUTH_PASSWORD ?
4172 channel->founder_passwd_len : 0);
4174 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len,
4175 channel->founder_method, auth, auth_len,
4176 idata->hash, client->id, SILC_ID_CLIENT)) {
4177 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4178 SILC_STATUS_ERR_AUTH_FAILED);
4182 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
4185 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4186 if (target_client == client) {
4187 /* Remove channel founder rights from itself */
4188 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
4191 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4192 SILC_STATUS_ERR_NOT_YOU);
4198 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
4199 /* Promote to operator */
4200 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4201 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4202 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4203 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4204 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4208 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
4212 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
4213 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4214 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4215 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4216 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4220 /* Demote to normal user */
4221 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
4226 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
4227 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4229 /* Send notify to channel, notify only if mode was actually changed. */
4231 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4232 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
4233 idp->data, idp->len,
4237 /* Set CUMODE notify type to network */
4238 if (!server->standalone)
4239 silc_server_send_notify_cumode(server, server->router->connection,
4240 server->server_type == SILC_ROUTER ?
4241 TRUE : FALSE, channel,
4242 target_mask, client->id,
4247 /* Send command reply to sender */
4248 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
4249 SILC_STATUS_OK, ident, 3,
4251 3, tmp_ch_id, tmp_ch_len,
4252 4, tmp_id, tmp_len);
4253 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4254 packet->data, packet->len, FALSE);
4256 silc_buffer_free(packet);
4257 silc_free(channel_id);
4258 silc_free(client_id);
4259 silc_buffer_free(idp);
4262 silc_server_command_free(cmd);
4265 /* Server side of KICK command. Kicks client out of channel. */
4267 SILC_SERVER_CMD_FUNC(kick)
4269 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4270 SilcServer server = cmd->server;
4271 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4272 SilcClientEntry target_client;
4273 SilcChannelID *channel_id;
4274 SilcClientID *client_id;
4275 SilcChannelEntry channel;
4276 SilcChannelClientEntry chl;
4279 unsigned char *tmp, *comment;
4281 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
4283 /* Get Channel ID */
4284 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4286 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4287 SILC_STATUS_ERR_NO_CHANNEL_ID);
4290 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
4292 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4293 SILC_STATUS_ERR_NO_CHANNEL_ID);
4297 /* Get channel entry */
4298 channel = silc_idlist_find_channel_by_id(server->local_list,
4301 channel = silc_idlist_find_channel_by_id(server->local_list,
4304 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4305 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4310 /* Check whether sender is on the channel */
4311 if (!silc_server_client_on_channel(client, channel)) {
4312 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4313 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4317 /* Check that the kicker is channel operator or channel founder */
4318 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4319 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
4320 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4321 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4325 /* Get target Client ID */
4326 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4328 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4329 SILC_STATUS_ERR_NO_CLIENT_ID);
4332 client_id = silc_id_payload_parse_id(tmp, tmp_len);
4334 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4335 SILC_STATUS_ERR_NO_CLIENT_ID);
4339 /* Get target client's entry */
4340 target_client = silc_idlist_find_client_by_id(server->local_list,
4341 client_id, TRUE, NULL);
4342 if (!target_client) {
4343 target_client = silc_idlist_find_client_by_id(server->global_list,
4344 client_id, TRUE, NULL);
4347 /* Check that the target client is not channel founder. Channel founder
4348 cannot be kicked from the channel. */
4349 silc_hash_table_find(channel->user_list, target_client, NULL, (void *)&chl);
4350 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4351 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4352 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV);
4356 /* Check whether target client is on the channel */
4357 if (!silc_server_client_on_channel(target_client, channel)) {
4358 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4359 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL);
4365 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4369 /* Send command reply to sender */
4370 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4373 /* Send KICKED notify to local clients on the channel */
4374 idp = silc_id_payload_encode(target_client->id, SILC_ID_CLIENT);
4375 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4376 SILC_NOTIFY_TYPE_KICKED,
4378 idp->data, idp->len,
4379 comment, comment ? strlen(comment) : 0);
4380 silc_buffer_free(idp);
4382 /* Remove the client from the channel. If the channel does not exist
4383 after removing the client then the client kicked itself off the channel
4384 and we don't have to send anything after that. */
4385 if (!silc_server_remove_from_one_channel(server, NULL, channel,
4386 target_client, FALSE))
4389 /* Send KICKED notify to primary route */
4390 if (!server->standalone)
4391 silc_server_send_notify_kicked(server, server->router->connection,
4392 server->server_type == SILC_ROUTER ?
4393 TRUE : FALSE, channel,
4394 target_client->id, comment);
4396 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4397 /* Re-generate channel key */
4398 if (!silc_server_create_channel_key(server, channel, 0))
4401 /* Send the channel key to the channel. The key of course is not sent
4402 to the client who was kicked off the channel. */
4403 silc_server_send_channel_key(server, target_client->connection, channel,
4404 server->server_type == SILC_ROUTER ?
4405 FALSE : !server->standalone);
4409 silc_server_command_free(cmd);
4412 /* Server side of OPER command. Client uses this comand to obtain server
4413 operator privileges to this server/router. */
4415 SILC_SERVER_CMD_FUNC(oper)
4417 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4418 SilcServer server = cmd->server;
4419 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4420 unsigned char *username, *auth;
4422 SilcServerConfigSectionAdminConnection *admin;
4423 SilcIDListData idata = (SilcIDListData)client;
4425 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
4427 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4430 /* Get the username */
4431 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4433 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4434 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4438 /* Get the admin configuration */
4439 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4440 username, client->nickname);
4442 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4443 username, client->nickname);
4445 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4446 SILC_STATUS_ERR_AUTH_FAILED);
4451 /* Get the authentication payload */
4452 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4454 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4455 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4459 /* Verify the authentication data */
4460 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4461 admin->auth_data, admin->auth_data_len,
4462 idata->hash, client->id, SILC_ID_CLIENT)) {
4463 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4464 SILC_STATUS_ERR_AUTH_FAILED);
4468 /* Client is now server operator */
4469 client->mode |= SILC_UMODE_SERVER_OPERATOR;
4471 /* Send UMODE change to primary router */
4472 if (!server->standalone)
4473 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4474 client->id, client->mode);
4476 /* Send reply to the sender */
4477 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4481 silc_server_command_free(cmd);
4484 /* Server side of SILCOPER command. Client uses this comand to obtain router
4485 operator privileges to this router. */
4487 SILC_SERVER_CMD_FUNC(silcoper)
4489 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4490 SilcServer server = cmd->server;
4491 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4492 unsigned char *username, *auth;
4494 SilcServerConfigSectionAdminConnection *admin;
4495 SilcIDListData idata = (SilcIDListData)client;
4497 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
4499 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4502 if (server->server_type == SILC_SERVER) {
4503 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4504 SILC_STATUS_ERR_AUTH_FAILED);
4508 /* Get the username */
4509 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4511 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4512 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4516 /* Get the admin configuration */
4517 admin = silc_server_config_find_admin(server->config, cmd->sock->ip,
4518 username, client->nickname);
4520 admin = silc_server_config_find_admin(server->config, cmd->sock->hostname,
4521 username, client->nickname);
4523 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4524 SILC_STATUS_ERR_AUTH_FAILED);
4529 /* Get the authentication payload */
4530 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4532 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4533 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4537 /* Verify the authentication data */
4538 if (!silc_auth_verify_data(auth, tmp_len, admin->auth_meth,
4539 admin->auth_data, admin->auth_data_len,
4540 idata->hash, client->id, SILC_ID_CLIENT)) {
4541 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4542 SILC_STATUS_ERR_AUTH_FAILED);
4546 /* Client is now router operator */
4547 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4549 /* Send UMODE change to primary router */
4550 if (!server->standalone)
4551 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4552 client->id, client->mode);
4554 /* Send reply to the sender */
4555 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4559 silc_server_command_free(cmd);
4562 /* Server side command of CONNECT. Connects us to the specified remote
4563 server or router. */
4565 SILC_SERVER_CMD_FUNC(connect)
4567 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4568 SilcServer server = cmd->server;
4569 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4570 unsigned char *tmp, *host;
4572 uint32 port = SILC_PORT;
4574 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CONNECT, cmd, 1, 2);
4576 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4579 /* Check whether client has the permissions. */
4580 if (client->mode == SILC_UMODE_NONE) {
4581 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4582 SILC_STATUS_ERR_NO_SERVER_PRIV);
4586 if (server->server_type == SILC_ROUTER &&
4587 client->mode & SILC_UMODE_SERVER_OPERATOR) {
4588 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4589 SILC_STATUS_ERR_NO_ROUTER_PRIV);
4593 /* Get the remote server */
4594 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4596 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4597 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4602 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4604 SILC_GET32_MSB(port, tmp);
4606 /* Create the connection. It is done with timeout and is async. */
4607 silc_server_create_connection(server, host, port);
4609 /* Send reply to the sender */
4610 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CONNECT,
4614 silc_server_command_free(cmd);
4617 /* Server side of command BAN. This is used to manage the ban list of the
4618 channel. To add clients and remove clients from the ban list. */
4620 SILC_SERVER_CMD_FUNC(ban)
4622 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4623 SilcServer server = cmd->server;
4624 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4626 SilcChannelEntry channel;
4627 SilcChannelClientEntry chl;
4628 SilcChannelID *channel_id = NULL;
4629 unsigned char *id, *add, *del;
4630 uint32 id_len, tmp_len;
4631 uint16 ident = silc_command_get_ident(cmd->payload);
4633 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4636 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
4638 /* Get Channel ID */
4639 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4641 channel_id = silc_id_payload_parse_id(id, id_len);
4643 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4644 SILC_STATUS_ERR_NO_CHANNEL_ID);
4649 /* Get channel entry. The server must know about the channel since the
4650 client is expected to be on the channel. */
4651 channel = silc_idlist_find_channel_by_id(server->local_list,
4654 channel = silc_idlist_find_channel_by_id(server->global_list,
4657 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4658 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4663 /* Check whether this client is on the channel */
4664 if (!silc_server_client_on_channel(client, channel)) {
4665 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4666 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4670 /* Get entry to the channel user list */
4671 silc_hash_table_find(channel->user_list, client, NULL, (void *)&chl);
4673 /* The client must be at least channel operator. */
4674 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4675 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4676 SILC_STATUS_ERR_NO_CHANNEL_PRIV);
4680 /* Get the new ban and add it to the ban list */
4681 add = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4683 if (!channel->ban_list)
4684 channel->ban_list = silc_calloc(tmp_len + 2, sizeof(*channel->ban_list));
4686 channel->ban_list = silc_realloc(channel->ban_list,
4687 sizeof(*channel->ban_list) *
4689 strlen(channel->ban_list) + 2));
4690 if (add[tmp_len - 1] == ',')
4691 add[tmp_len - 1] = '\0';
4693 strncat(channel->ban_list, add, tmp_len);
4694 strncat(channel->ban_list, ",", 1);
4697 /* Get the ban to be removed and remove it from the list */
4698 del = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4699 if (del && channel->ban_list) {
4700 char *start, *end, *n;
4702 if (!strncmp(channel->ban_list, del, strlen(channel->ban_list) - 1)) {
4703 silc_free(channel->ban_list);
4704 channel->ban_list = NULL;
4706 start = strstr(channel->ban_list, del);
4707 if (start && strlen(start) >= tmp_len) {
4708 end = start + tmp_len;
4709 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
4710 strncat(n, channel->ban_list, start - channel->ban_list);
4711 strncat(n, end + 1, ((channel->ban_list + strlen(channel->ban_list)) -
4713 silc_free(channel->ban_list);
4714 channel->ban_list = n;
4719 /* Send the BAN notify type to our primary router. */
4720 if (!server->standalone && (add || del))
4721 silc_server_send_notify_ban(server, server->router->connection,
4722 server->server_type == SILC_ROUTER ?
4723 TRUE : FALSE, channel, add, del);
4725 /* Send the reply back to the client */
4726 if (channel->ban_list)
4728 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4729 SILC_STATUS_OK, ident, 2,
4731 3, channel->ban_list,
4732 strlen(channel->ban_list) - 1);
4735 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4736 SILC_STATUS_OK, ident, 1,
4739 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4740 packet->data, packet->len, FALSE);
4742 silc_buffer_free(packet);
4746 silc_free(channel_id);
4747 silc_server_command_free(cmd);
4750 /* Server side command of CLOSE. Closes connection to a specified server. */
4752 SILC_SERVER_CMD_FUNC(close)
4754 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4755 SilcServer server = cmd->server;
4756 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4757 SilcServerEntry server_entry;
4758 SilcSocketConnection sock;
4761 unsigned char *name;
4762 uint32 port = SILC_PORT;
4764 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CLOSE, cmd, 1, 2);
4766 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4769 /* Check whether client has the permissions. */
4770 if (client->mode == SILC_UMODE_NONE) {
4771 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4772 SILC_STATUS_ERR_NO_SERVER_PRIV);
4776 /* Get the remote server */
4777 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4779 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4780 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
4785 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4787 SILC_GET32_MSB(port, tmp);
4789 server_entry = silc_idlist_find_server_by_conn(server->local_list,
4790 name, port, FALSE, NULL);
4791 if (!server_entry) {
4792 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4793 SILC_STATUS_ERR_NO_SERVER_ID);
4797 /* Send reply to the sender */
4798 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE,
4801 /* Close the connection to the server */
4802 sock = (SilcSocketConnection)server_entry->connection;
4803 silc_server_free_sock_user_data(server, sock);
4804 silc_server_close_connection(server, sock);
4807 silc_server_command_free(cmd);
4810 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4811 active connections. */
4813 SILC_SERVER_CMD_FUNC(shutdown)
4815 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4816 SilcServer server = cmd->server;
4817 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4819 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SHUTDOWN, cmd, 0, 0);
4821 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4824 /* Check whether client has the permission. */
4825 if (client->mode == SILC_UMODE_NONE) {
4826 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4827 SILC_STATUS_ERR_NO_SERVER_PRIV);
4831 /* Send reply to the sender */
4832 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SHUTDOWN,
4835 /* Then, gracefully, or not, bring the server down. */
4836 silc_server_stop(server);
4840 silc_server_command_free(cmd);
4843 /* Server side command of LEAVE. Removes client from a channel. */
4845 SILC_SERVER_CMD_FUNC(leave)
4847 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4848 SilcServer server = cmd->server;
4849 SilcSocketConnection sock = cmd->sock;
4850 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4851 SilcChannelID *id = NULL;
4852 SilcChannelEntry channel;
4856 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
4858 /* Get Channel ID */
4859 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4861 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4862 SILC_STATUS_ERR_NO_CHANNEL_ID);
4865 id = silc_id_payload_parse_id(tmp, len);
4867 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4868 SILC_STATUS_ERR_NO_CHANNEL_ID);
4872 /* Get channel entry */
4873 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4875 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4877 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4878 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
4883 /* Check whether this client is on the channel */
4884 if (!silc_server_client_on_channel(id_entry, channel)) {
4885 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4886 SILC_STATUS_ERR_NOT_ON_CHANNEL);
4890 /* Notify routers that they should remove this client from their list
4891 of clients on the channel. Send LEAVE notify type. */
4892 if (!server->standalone)
4893 silc_server_send_notify_leave(server, server->router->connection,
4894 server->server_type == SILC_ROUTER ?
4895 TRUE : FALSE, channel, id_entry->id);
4897 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4900 /* Remove client from channel */
4901 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4903 /* If the channel does not exist anymore we won't send anything */
4906 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4907 /* Re-generate channel key */
4908 if (!silc_server_create_channel_key(server, channel, 0))
4911 /* Send the channel key */
4912 silc_server_send_channel_key(server, NULL, channel,
4913 server->server_type == SILC_ROUTER ?
4914 FALSE : !server->standalone);
4920 silc_server_command_free(cmd);
4923 /* Server side of command USERS. Resolves clients and their USERS currently
4924 joined on the requested channel. The list of Client ID's and their modes
4925 on the channel is sent back. */
4927 SILC_SERVER_CMD_FUNC(users)
4929 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4930 SilcServer server = cmd->server;
4931 SilcChannelEntry channel;
4932 SilcChannelID *id = NULL;
4933 SilcBuffer packet, idp;
4934 unsigned char *channel_id;
4935 uint32 channel_id_len;
4936 SilcBuffer client_id_list;
4937 SilcBuffer client_mode_list;
4938 unsigned char lc[4];
4939 uint32 list_count = 0;
4940 uint16 ident = silc_command_get_ident(cmd->payload);
4943 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
4945 /* Get Channel ID */
4946 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4948 /* Get channel name */
4949 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
4951 if (!channel_id && !channel_name) {
4952 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4953 SILC_STATUS_ERR_NO_CHANNEL_ID);
4958 id = silc_id_payload_parse_id(channel_id, channel_id_len);
4960 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4961 SILC_STATUS_ERR_NO_CHANNEL_ID);
4966 /* If we are server and we don't know about this channel we will send
4967 the command to our router. If we know about the channel then we also
4968 have the list of users already. */
4970 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4972 channel = silc_idlist_find_channel_by_name(server->local_list,
4973 channel_name, NULL);
4976 if (server->server_type == SILC_SERVER && !server->standalone &&
4980 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
4981 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4983 /* Send USERS command */
4984 silc_server_packet_send(server, server->router->connection,
4985 SILC_PACKET_COMMAND, cmd->packet->flags,
4986 tmpbuf->data, tmpbuf->len, TRUE);
4988 /* Reprocess this packet after received reply */
4989 silc_server_command_pending(server, SILC_COMMAND_USERS,
4990 silc_command_get_ident(cmd->payload),
4991 silc_server_command_destructor,
4992 silc_server_command_users,
4993 silc_server_command_dup(cmd));
4994 cmd->pending = TRUE;
4995 silc_command_set_ident(cmd->payload, ident);
4997 silc_buffer_free(tmpbuf);
5002 /* Check the global list as well. */
5004 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
5006 channel = silc_idlist_find_channel_by_name(server->global_list,
5007 channel_name, NULL);
5009 /* Channel really does not exist */
5010 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
5011 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
5016 /* If the channel is private or secret do not send anything, unless the
5017 user requesting this command is on the channel. */
5018 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
5019 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)
5020 && !silc_server_client_on_channel(cmd->sock->user_data, channel)) {
5021 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
5022 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
5027 (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)) {
5028 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
5029 SILC_STATUS_ERR_NO_SUCH_CHANNEL);
5034 /* Get the users list */
5035 silc_server_get_users_on_channel(server, channel, &client_id_list,
5036 &client_mode_list, &list_count);
5039 SILC_PUT32_MSB(list_count, lc);
5042 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
5043 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
5044 SILC_STATUS_OK, ident, 4,
5045 2, idp->data, idp->len,
5047 4, client_id_list->data,
5048 client_id_list->len,
5049 5, client_mode_list->data,
5050 client_mode_list->len);
5051 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
5052 packet->data, packet->len, FALSE);
5054 silc_buffer_free(idp);
5055 silc_buffer_free(packet);
5056 silc_buffer_free(client_id_list);
5057 silc_buffer_free(client_mode_list);
5062 silc_server_command_free(cmd);
5065 /* Server side of command GETKEY. This fetches the client's public key
5066 from the server where to the client is connected. */
5068 SILC_SERVER_CMD_FUNC(getkey)
5070 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5071 SilcServer server = cmd->server;
5073 SilcClientEntry client;
5074 SilcServerEntry server_entry;
5075 SilcClientID *client_id = NULL;
5076 SilcServerID *server_id = NULL;
5077 SilcIDPayload idp = NULL;
5078 uint16 ident = silc_command_get_ident(cmd->payload);
5079 unsigned char *tmp, *pkdata;
5080 uint32 tmp_len, pklen;
5081 SilcBuffer pk = NULL;
5084 SILC_LOG_DEBUG(("Start"));
5086 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5088 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5089 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
5092 idp = silc_id_payload_parse_data(tmp, tmp_len);
5094 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5095 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
5099 id_type = silc_id_payload_get_type(idp);
5100 if (id_type == SILC_ID_CLIENT) {
5101 client_id = silc_id_payload_get_id(idp);
5103 /* If the client is not found from local list there is no chance it
5104 would be locally connected client so send the command further. */
5105 client = silc_idlist_find_client_by_id(server->local_list,
5106 client_id, TRUE, NULL);
5108 client = silc_idlist_find_client_by_id(server->global_list,
5109 client_id, TRUE, NULL);
5111 if ((!client && !cmd->pending && !server->standalone) ||
5112 (client && !client->connection && !cmd->pending &&
5113 !server->standalone) ||
5114 (client && !client->data.public_key && !cmd->pending &&
5115 !server->standalone)) {
5118 SilcSocketConnection dest_sock;
5120 dest_sock = silc_server_get_client_route(server, NULL, 0,
5125 old_ident = silc_command_get_ident(cmd->payload);
5126 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
5127 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
5129 silc_server_packet_send(server, dest_sock,
5130 SILC_PACKET_COMMAND, cmd->packet->flags,
5131 tmpbuf->data, tmpbuf->len, TRUE);
5133 /* Reprocess this packet after received reply from router */
5134 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
5135 silc_command_get_ident(cmd->payload),
5136 silc_server_command_destructor,
5137 silc_server_command_getkey,
5138 silc_server_command_dup(cmd));
5139 cmd->pending = TRUE;
5141 silc_command_set_ident(cmd->payload, old_ident);
5142 silc_buffer_free(tmpbuf);
5147 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5148 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
5152 /* The client is locally connected, just get the public key and
5153 send it back. If they key does not exist then do not send it,
5154 send just OK reply */
5155 if (!client->data.public_key) {
5159 tmp = silc_pkcs_public_key_encode(client->data.public_key, &tmp_len);
5160 pk = silc_buffer_alloc(4 + tmp_len);
5161 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
5162 silc_buffer_format(pk,
5163 SILC_STR_UI_SHORT(tmp_len),
5164 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
5165 SILC_STR_UI_XNSTRING(tmp, tmp_len),
5171 } else if (id_type == SILC_ID_SERVER) {
5172 server_id = silc_id_payload_get_id(idp);
5174 /* If the server is not found from local list there is no chance it
5175 would be locally connected server so send the command further. */
5176 server_entry = silc_idlist_find_server_by_id(server->local_list,
5177 server_id, TRUE, NULL);
5179 server_entry = silc_idlist_find_server_by_id(server->global_list,
5180 server_id, TRUE, NULL);
5182 if (server_entry != server->id_entry &&
5183 ((!server_entry && !cmd->pending && !server->standalone) ||
5184 (server_entry && !server_entry->connection && !cmd->pending &&
5185 !server->standalone) ||
5186 (server_entry && !server_entry->data.public_key && !cmd->pending &&
5187 !server->standalone))) {
5191 old_ident = silc_command_get_ident(cmd->payload);
5192 silc_command_set_ident(cmd->payload, silc_rng_get_rn16(server->rng));
5193 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
5195 silc_server_packet_send(server, server->router->connection,
5196 SILC_PACKET_COMMAND, cmd->packet->flags,
5197 tmpbuf->data, tmpbuf->len, TRUE);
5199 /* Reprocess this packet after received reply from router */
5200 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
5201 silc_command_get_ident(cmd->payload),
5202 silc_server_command_destructor,
5203 silc_server_command_getkey,
5204 silc_server_command_dup(cmd));
5205 cmd->pending = TRUE;
5207 silc_command_set_ident(cmd->payload, old_ident);
5208 silc_buffer_free(tmpbuf);
5212 if (!server_entry) {
5213 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5214 SILC_STATUS_ERR_NO_SUCH_SERVER_ID);
5218 /* If they key does not exist then do not send it, send just OK reply */
5219 if (!server_entry->data.public_key) {
5223 tmp = silc_pkcs_public_key_encode(server_entry->data.public_key,
5225 pk = silc_buffer_alloc(4 + tmp_len);
5226 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
5227 silc_buffer_format(pk,
5228 SILC_STR_UI_SHORT(tmp_len),
5229 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
5230 SILC_STR_UI_XNSTRING(tmp, tmp_len),
5240 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5241 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
5242 SILC_STATUS_OK, ident,
5246 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
5247 packet->data, packet->len, FALSE);
5248 silc_buffer_free(packet);
5251 silc_buffer_free(pk);
5255 silc_id_payload_free(idp);
5256 silc_free(client_id);
5257 silc_free(server_id);
5258 silc_server_command_free(cmd);