5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 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,
35 silc_server_command_send_status_data(SilcServerCommandContext cmd,
40 const unsigned char *arg,
43 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
44 SilcServerCommandReplyContext cmdr,
46 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
48 /* Server command list. */
49 SilcServerCommand silc_command_list[] =
51 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
53 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
54 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
55 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
56 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
57 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
58 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
59 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
60 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
61 SILC_SERVER_CMD(stats, STATS, SILC_CF_LAG | SILC_CF_REG),
62 SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
63 SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
64 SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
65 SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
66 SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
67 SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
68 SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
69 SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
70 SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
71 SILC_SERVER_CMD(detach, DETACH, SILC_CF_LAG_STRICT | SILC_CF_REG),
72 SILC_SERVER_CMD(watch, WATCH, SILC_CF_LAG | SILC_CF_REG),
73 SILC_SERVER_CMD(silcoper, SILCOPER,
74 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
75 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
76 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
77 SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
79 SILC_SERVER_CMD(connect, PRIV_CONNECT,
80 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
81 SILC_SERVER_CMD(close, PRIV_CLOSE,
82 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
83 SILC_SERVER_CMD(shutdown, PRIV_SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
89 /* Performs several checks to the command. It first checks whether this
90 command was called as pending command callback. If it was then it checks
91 whether error occurred in the command reply where the pending command
94 It also checks that the requested command includes correct amount
96 #define SILC_SERVER_COMMAND_CHECK(command, context, min, max) \
100 SILC_LOG_DEBUG(("Start")); \
102 if (silc_server_command_pending_error_check(cmd, context2, command)) { \
103 silc_server_command_free(cmd); \
107 _argc = silc_argument_get_arg_num(cmd->args); \
109 silc_server_command_send_status_reply(cmd, command, \
110 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, \
112 silc_server_command_free(cmd); \
116 silc_server_command_send_status_reply(cmd, command, \
117 SILC_STATUS_ERR_TOO_MANY_PARAMS, \
119 silc_server_command_free(cmd); \
124 /* Returns TRUE if the connection is registered. Unregistered connections
125 usually cannot send commands hence the check. */
127 static int silc_server_is_registered(SilcServer server,
128 SilcSocketConnection sock,
129 SilcServerCommandContext cmd,
132 SilcIDListData idata = (SilcIDListData)sock->user_data;
137 if (idata->status & SILC_IDLIST_STATUS_REGISTERED)
140 silc_server_command_send_status_reply(cmd, command,
141 SILC_STATUS_ERR_NOT_REGISTERED, 0);
145 /* Internal context to hold data when executed command with timeout. */
147 SilcServerCommandContext ctx;
148 SilcServerCommand *cmd;
149 } *SilcServerCommandTimeout;
151 /* Timeout callback to process commands with timeout for client. Client's
152 commands are always executed with timeout. */
154 SILC_TASK_CALLBACK(silc_server_command_process_timeout)
156 SilcServerCommandTimeout timeout = (SilcServerCommandTimeout)context;
157 SilcClientEntry client = (SilcClientEntry)timeout->ctx->sock->user_data;
160 silc_server_command_free(timeout->ctx);
164 /* Update access time */
165 client->last_command = time(NULL);
167 if (!(timeout->cmd->flags & SILC_CF_REG))
168 timeout->cmd->cb(timeout->ctx, NULL);
169 else if (silc_server_is_registered(timeout->ctx->server,
173 timeout->cmd->cb(timeout->ctx, NULL);
175 silc_server_command_free(timeout->ctx);
180 /* Processes received command packet. */
182 void silc_server_command_process(SilcServer server,
183 SilcSocketConnection sock,
184 SilcPacketContext *packet)
186 SilcServerCommandContext ctx;
187 SilcServerCommand *cmd;
190 /* Allocate command context. This must be free'd by the
191 command routine receiving it. */
192 ctx = silc_server_command_alloc();
193 ctx->server = server;
194 ctx->sock = silc_socket_dup(sock);
195 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
197 /* Parse the command payload in the packet */
198 ctx->payload = silc_command_payload_parse(packet->buffer->data,
199 packet->buffer->len);
201 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
202 silc_buffer_free(packet->buffer);
203 silc_packet_context_free(packet);
204 silc_socket_free(ctx->sock);
208 ctx->args = silc_command_get_args(ctx->payload);
210 /* Get the command */
211 command = silc_command_get(ctx->payload);
212 for (cmd = silc_command_list; cmd->cb; cmd++)
213 if (cmd->cmd == command)
216 if (!cmd || !cmd->cb) {
217 silc_server_command_send_status_reply(ctx, command,
218 SILC_STATUS_ERR_UNKNOWN_COMMAND, 0);
219 silc_server_command_free(ctx);
223 /* Execute client's commands always with timeout. Normally they are
224 executed with zero (0) timeout but if client is sending command more
225 frequently than once in 2 seconds, then the timeout may be 0 to 2
227 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
228 SilcClientEntry client = (SilcClientEntry)sock->user_data;
229 SilcServerCommandTimeout timeout = silc_calloc(1, sizeof(*timeout));
235 if (client->last_command && (time(NULL) - client->last_command) < 2) {
236 client->fast_command++;
239 if (client->fast_command - 2 <= 0)
240 client->fast_command = 0;
242 client->fast_command -= 2;
246 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
247 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
248 silc_schedule_task_add(server->schedule, sock->sock,
249 silc_server_command_process_timeout, timeout,
250 (client->fast_command < 3 ? 0 :
251 2 - (time(NULL) - client->last_command)),
252 (client->fast_command < 3 ? 200000 : 0),
253 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
255 silc_schedule_task_add(server->schedule, sock->sock,
256 silc_server_command_process_timeout, timeout,
257 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
261 /* Execute for server */
263 if (!(cmd->flags & SILC_CF_REG))
265 else if (silc_server_is_registered(server, sock, ctx, cmd->cmd))
268 silc_server_command_free(ctx);
271 /* Allocate Command Context */
273 SilcServerCommandContext silc_server_command_alloc()
275 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
280 /* Free's the command context allocated before executing the command */
282 void silc_server_command_free(SilcServerCommandContext ctx)
285 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
287 if (ctx->users < 1) {
289 silc_command_payload_free(ctx->payload);
291 silc_packet_context_free(ctx->packet);
293 silc_socket_free(ctx->sock); /* Decrease reference counter */
298 /* Duplicate Command Context by adding reference counter. The context won't
299 be free'd untill it hits zero. */
301 SilcServerCommandContext
302 silc_server_command_dup(SilcServerCommandContext ctx)
305 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
310 /* Add new pending command to be executed when reply to a command has been
311 received. The `reply_cmd' is the command that will call the `callback'
312 with `context' when reply has been received. It can be SILC_COMMAND_NONE
313 to match any command with the `ident'. If `ident' is non-zero
314 the `callback' will be executed when received reply with command
315 identifier `ident'. If there already exists pending command for the
316 specified command, ident, callback and context this function has no
319 bool silc_server_command_pending(SilcServer server,
320 SilcCommand reply_cmd,
322 SilcCommandCb callback,
325 SilcServerCommandPending *reply;
327 /* Check whether identical pending already exists for same command,
328 ident, callback and callback context. If it does then it would be
329 error to register it again. */
330 silc_dlist_start(server->pending_commands);
331 while ((reply = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
332 if (reply->reply_cmd == reply_cmd && reply->ident == ident &&
333 reply->callback == callback && reply->context == context)
337 reply = silc_calloc(1, sizeof(*reply));
338 reply->reply_cmd = reply_cmd;
339 reply->ident = ident;
340 reply->context = context;
341 reply->callback = callback;
342 silc_dlist_add(server->pending_commands, reply);
347 /* Deletes pending command by reply command type. */
349 void silc_server_command_pending_del(SilcServer server,
350 SilcCommand reply_cmd,
353 SilcServerCommandPending *r;
355 silc_dlist_start(server->pending_commands);
356 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
357 if (r->reply_cmd == reply_cmd && r->ident == ident) {
358 silc_dlist_del(server->pending_commands, r);
364 /* Checks for pending commands and marks callbacks to be called from
365 the command reply function. Returns TRUE if there were pending command. */
367 SilcServerCommandPendingCallbacks
368 silc_server_command_pending_check(SilcServer server,
369 SilcServerCommandReplyContext ctx,
372 SilcUInt32 *callbacks_count)
374 SilcServerCommandPending *r;
375 SilcServerCommandPendingCallbacks callbacks = NULL;
378 silc_dlist_start(server->pending_commands);
379 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
380 if ((r->reply_cmd == command || r->reply_cmd == SILC_COMMAND_NONE)
381 && r->ident == ident) {
382 callbacks = silc_realloc(callbacks, sizeof(*callbacks) * (i + 1));
383 callbacks[i].context = r->context;
384 callbacks[i].callback = r->callback;
390 *callbacks_count = i;
394 /* Sends simple status message as command reply packet */
397 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
404 SILC_LOG_DEBUG(("Sending command status %d", status));
407 silc_command_reply_payload_encode_va(command, status, error,
408 silc_command_get_ident(cmd->payload),
410 silc_server_packet_send(cmd->server, cmd->sock,
411 SILC_PACKET_COMMAND_REPLY, 0,
412 buffer->data, buffer->len, FALSE);
413 silc_buffer_free(buffer);
416 /* Sends command status reply with one extra argument. The argument
417 type must be sent as argument. */
420 silc_server_command_send_status_data(SilcServerCommandContext cmd,
425 const unsigned char *arg,
430 SILC_LOG_DEBUG(("Sending command status %d", status));
433 silc_command_reply_payload_encode_va(command, status, 0,
434 silc_command_get_ident(cmd->payload),
435 1, arg_type, arg, arg_len);
436 silc_server_packet_send(cmd->server, cmd->sock,
437 SILC_PACKET_COMMAND_REPLY, 0,
438 buffer->data, buffer->len, FALSE);
439 silc_buffer_free(buffer);
442 /* This function can be called to check whether in the command reply
443 an error occurred. This function has no effect if this is called
444 when the command function was not called as pending command callback.
445 This returns TRUE if error had occurred. */
448 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
449 SilcServerCommandReplyContext cmdr,
452 if (!cmd->pending || !cmdr)
455 if (!silc_command_get_status(cmdr->payload, NULL, NULL)) {
458 /* Send the same command reply payload */
459 silc_command_set_ident(cmdr->payload,
460 silc_command_get_ident(cmd->payload));
461 buffer = silc_command_payload_encode_payload(cmdr->payload);
462 silc_server_packet_send(cmd->server, cmd->sock,
463 SILC_PACKET_COMMAND_REPLY, 0,
464 buffer->data, buffer->len, FALSE);
465 silc_buffer_free(buffer);
472 /******************************************************************************
476 ******************************************************************************/
485 #define ADD_ERROR(errptr, errptr_count, _id, _id_type, _index, _status) \
487 errptr = silc_realloc(errptr, sizeof(*errptr) * (errptr_count + 1)); \
490 errptr[errptr_count].id = _id; \
491 errptr[errptr_count].id_type = _id_type; \
492 errptr[errptr_count].index = _index; \
493 errptr[errptr_count].error = _status; \
498 silc_server_command_whois_parse(SilcServerCommandContext cmd,
499 SilcClientID ***client_id,
500 SilcUInt32 *client_id_count,
504 ResolveError *error_client,
505 SilcUInt32 *error_client_count)
509 SilcUInt32 argc = silc_argument_get_arg_num(cmd->args);
513 /* If client ID is in the command it must be used instead of nickname */
514 tmp = silc_argument_get_arg_type(cmd->args, 4, &len);
516 /* No ID, get the nickname@server string and parse it. */
517 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
519 silc_parse_userfqdn(tmp, nickname, server_name);
521 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WHOIS,
522 SILC_STATUS_ERR_BAD_NICKNAME, 0);
526 /* Command includes ID, we must use that. Take all ID's from the
528 for (k = 0, i = 0; i < argc; i++) {
529 tmp = silc_argument_get_arg_type(cmd->args, i + 4, &len);
532 id = silc_id_payload_parse_id(tmp, len, NULL);
534 *client_id = silc_realloc(*client_id, sizeof(**client_id) *
535 (*client_id_count + 1));
536 (*client_id)[k] = id;
537 (*client_id_count)++;
540 ADD_ERROR((*error_client), (*error_client_count), NULL, 0, i + 4,
541 SILC_STATUS_ERR_BAD_CLIENT_ID);
546 /* Get the max count of reply messages allowed */
547 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
549 SILC_GET32_MSB(*count, tmp);
556 /* Resolve context used by both WHOIS and IDENTIFY commands */
558 SilcServerEntry router;
560 unsigned char **res_argv;
561 SilcUInt32 *res_argv_lens;
562 SilcUInt32 *res_argv_types;
564 } *SilcServerResolveContext;
567 silc_server_command_whois_check(SilcServerCommandContext cmd,
568 SilcClientEntry *clients,
569 SilcUInt32 clients_count)
571 SilcServer server = cmd->server;
572 SilcClientEntry entry;
573 SilcServerResolveContext resolve = NULL, r = NULL;
574 SilcUInt32 resolve_count = 0;
578 SILC_LOG_DEBUG(("Start"));
580 for (i = 0; i < clients_count; i++) {
585 if ((entry->nickname && entry->username && entry->userinfo) ||
586 !(entry->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
590 /* If we are normal server, and we've not resolved this client from
591 router and it is global client, we'll check whether it is on some
592 channel. If not then we cannot be sure about its validity, and
593 we'll resolve it from router. */
594 if (cmd->server->server_type != SILC_SERVER || cmd->pending ||
595 entry->connection || silc_hash_table_count(entry->channels))
599 /* We need to resolve this entry since it is not complete */
601 if (!cmd->pending && entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
602 /* The entry is being resolved (and we are not the resolver) so attach
603 to the command reply and we're done with this one. */
604 silc_server_command_pending(server, SILC_COMMAND_NONE,
605 entry->resolve_cmd_ident,
606 silc_server_command_whois,
607 silc_server_command_dup(cmd));
610 if (entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
611 /* We've resolved this and it still is not ready. We'll return
612 and are that this will be handled again after it is resolved. */
613 for (i = 0; i < resolve_count; i++) {
614 for (k = 0; k < r->res_argc; k++)
615 silc_free(r->res_argv[k]);
616 silc_free(r->res_argv);
617 silc_free(r->res_argv_lens);
618 silc_free(r->res_argv_types);
623 /* We'll resolve this client */
627 for (k = 0; k < resolve_count; k++) {
628 if (resolve[k].router == entry->router) {
635 resolve = silc_realloc(resolve, sizeof(*resolve) *
636 (resolve_count + 1));
637 r = &resolve[resolve_count];
638 memset(r, 0, sizeof(*r));
639 r->router = entry->router;
640 r->ident = ++server->cmd_ident;
644 r->res_argv = silc_realloc(r->res_argv, sizeof(*r->res_argv) *
646 r->res_argv_lens = silc_realloc(r->res_argv_lens,
647 sizeof(*r->res_argv_lens) *
649 r->res_argv_types = silc_realloc(r->res_argv_types,
650 sizeof(*r->res_argv_types) *
652 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
653 r->res_argv[r->res_argc] = silc_calloc(idp->len,
654 sizeof(**r->res_argv));
655 memcpy(r->res_argv[r->res_argc], idp->data, idp->len);
656 r->res_argv_lens[r->res_argc] = idp->len;
657 r->res_argv_types[r->res_argc] = r->res_argc + 4;
659 silc_buffer_free(idp);
661 entry->resolve_cmd_ident = r->ident;
662 entry->data.status |= SILC_IDLIST_STATUS_RESOLVING;
663 entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
668 /* Do the resolving */
669 for (i = 0; i < resolve_count; i++) {
674 /* Send WHOIS request. We send WHOIS since we're doing the requesting
675 now anyway so make it a good one. */
676 res_cmd = silc_command_payload_encode(SILC_COMMAND_WHOIS,
677 r->res_argc, r->res_argv,
681 silc_server_packet_send(server, r->router->connection,
682 SILC_PACKET_COMMAND, cmd->packet->flags,
683 res_cmd->data, res_cmd->len, FALSE);
685 /* Reprocess this packet after received reply */
686 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
688 silc_server_command_whois,
689 silc_server_command_dup(cmd));
692 silc_buffer_free(res_cmd);
693 for (k = 0; k < r->res_argc; k++)
694 silc_free(r->res_argv[k]);
695 silc_free(r->res_argv);
696 silc_free(r->res_argv_lens);
697 silc_free(r->res_argv_types);
706 silc_server_command_whois_send_reply(SilcServerCommandContext cmd,
707 SilcClientEntry *clients,
708 SilcUInt32 clients_count,
710 SilcUInt32 errors_count,
711 int count, const char *nickname,
712 SilcClientID **client_ids)
714 SilcServer server = cmd->server;
716 int i, k, valid_count = clients_count;
718 SilcBuffer packet, idp, channels, umode_list = NULL;
719 SilcClientEntry entry;
721 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
722 char nh[256], uh[256];
723 unsigned char idle[4], mode[4];
724 unsigned char *fingerprint;
725 SilcSocketConnection hsock;
728 /* Process only valid clients and ignore those that are not registered.
729 This is checked with nickname only because when resolved client IDs
730 we check that they are registered earlier. */
732 for (i = 0; i < clients_count; i++)
733 if (clients[i]->data.status & SILC_IDLIST_STATUS_REGISTERED)
739 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
740 SILC_STATUS_ERR_NO_SUCH_NICK, 0,
741 3, nickname, strlen(nickname));
746 /* Start processing found clients. */
747 status = SILC_STATUS_OK;
749 status = SILC_STATUS_LIST_START;
751 for (i = 0, k = 0; i < clients_count; i++) {
757 status = SILC_STATUS_LIST_ITEM;
758 if (valid_count > 1 && k == valid_count - 1 && !errors_count)
759 status = SILC_STATUS_LIST_END;
760 if (count && k - 1 == count)
761 status = SILC_STATUS_LIST_END;
763 /* Send WHOIS reply */
764 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
765 tmp = silc_argument_get_first_arg(cmd->args, NULL);
767 memset(uh, 0, sizeof(uh));
768 memset(nh, 0, sizeof(nh));
769 memset(idle, 0, sizeof(idle));
771 strncat(nh, entry->nickname, strlen(entry->nickname));
772 if (!strchr(entry->nickname, '@')) {
774 if (entry->servername) {
775 strncat(nh, entry->servername, strlen(entry->servername));
777 len = entry->router ? strlen(entry->router->server_name) :
778 strlen(server->server_name);
779 strncat(nh, entry->router ? entry->router->server_name :
780 server->server_name, len);
784 strncat(uh, entry->username, strlen(entry->username));
785 if (!strchr(entry->username, '@') && entry->connection) {
787 hsock = (SilcSocketConnection)entry->connection;
788 len = strlen(hsock->hostname);
789 strncat(uh, hsock->hostname, len);
792 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
793 channels = silc_server_get_client_channel_list(server, entry, FALSE,
796 channels = silc_server_get_client_channel_list(server, entry, TRUE,
799 if (entry->data.fingerprint[0] != 0 && entry->data.fingerprint[1] != 0)
800 fingerprint = entry->data.fingerprint;
804 SILC_PUT32_MSB(entry->mode, mode);
805 if (entry->connection)
806 SILC_PUT32_MSB((time(NULL) - entry->data.last_receive), idle);
809 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
811 2, idp->data, idp->len,
815 strlen(entry->userinfo),
816 6, channels ? channels->data : NULL,
817 channels ? channels->len : 0,
821 fingerprint ? 20 : 0,
822 10, umode_list ? umode_list->data :
823 NULL, umode_list ? umode_list->len :
826 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
827 0, packet->data, packet->len, FALSE);
829 silc_buffer_free(packet);
830 silc_buffer_free(idp);
832 silc_buffer_free(channels);
834 silc_buffer_free(umode_list);
838 if (status == SILC_STATUS_LIST_END)
843 /* Send error replies */
844 if (status == SILC_STATUS_OK && errors_count > 1)
845 status = SILC_STATUS_LIST_START;
848 for (i = 0, k = 0; i < errors_count; i++) {
850 idp = silc_id_payload_encode(errors[i].id, SILC_ID_CLIENT);
854 tmp = silc_argument_get_arg_type(cmd->args, errors[i].index, &len);
858 status = SILC_STATUS_LIST_ITEM;
859 if (errors_count > 1 && k == errors_count - 1)
860 status = SILC_STATUS_LIST_END;
861 if (count && k - 1 == count)
862 status = SILC_STATUS_LIST_END;
865 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
866 (status == SILC_STATUS_OK ?
867 errors[i].error : status),
868 (status == SILC_STATUS_OK ?
869 0 : errors[i].error),
871 silc_buffer_free(idp);
874 if (status == SILC_STATUS_LIST_END)
881 silc_server_command_whois_send_router(SilcServerCommandContext cmd)
883 SilcServer server = cmd->server;
885 SilcUInt16 old_ident;
887 old_ident = silc_command_get_ident(cmd->payload);
888 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
889 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
891 /* Send WHOIS command to our router */
892 silc_server_packet_send(server, (SilcSocketConnection)
893 server->router->connection,
894 SILC_PACKET_COMMAND, cmd->packet->flags,
895 tmpbuf->data, tmpbuf->len, TRUE);
897 /* Reprocess this packet after received reply from router */
898 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
899 silc_command_get_ident(cmd->payload),
900 silc_server_command_whois,
901 silc_server_command_dup(cmd));
903 silc_command_set_ident(cmd->payload, old_ident);
904 silc_buffer_free(tmpbuf);
908 silc_server_command_whois_process(SilcServerCommandContext cmd)
910 SilcServer server = cmd->server;
911 char *nick = NULL, *server_name = NULL;
913 SilcClientEntry *clients = NULL, entry;
914 SilcClientID **client_id = NULL;
915 SilcUInt32 client_id_count = 0, clients_count = 0, error_client_count = 0;
916 ResolveError error_client = NULL;
918 bool check_global = FALSE;
920 /* Parse the whois request */
921 if (!silc_server_command_whois_parse(cmd, &client_id, &client_id_count,
922 &nick, &server_name, &count,
923 &error_client, &error_client_count))
926 /* Send the WHOIS request to the router only if it included nickname.
927 Since nicknames can be expanded into many clients we need to send it
928 to router. If the WHOIS included only client ID's we will check them
929 first locally since we just might have them. */
930 if (nick && !client_id_count && cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
931 server->server_type == SILC_SERVER && !cmd->pending &&
932 !server->standalone) {
933 silc_server_command_whois_send_router(cmd);
938 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
940 else if (server->server_type != SILC_SERVER)
943 /* Get all clients matching that ID or nickname from local list */
944 if (client_id_count) {
945 /* Check all Client ID's received in the command packet */
946 for (i = 0; i < client_id_count; i++) {
947 entry = silc_idlist_find_client_by_id(server->local_list,
948 client_id[i], TRUE, NULL);
949 if (!entry && check_global)
950 entry = silc_idlist_find_client_by_id(server->global_list,
951 client_id[i], TRUE, NULL);
953 clients = silc_realloc(clients, sizeof(*clients) *
954 (clients_count + 1));
955 clients[clients_count++] = entry;
957 /* If we are normal server and did not send the request first to router
958 do it now, since we do not have the Client ID information. */
959 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
960 server->server_type == SILC_SERVER && !cmd->pending &&
961 !server->standalone) {
962 silc_server_command_whois_send_router(cmd);
967 ADD_ERROR(error_client, error_client_count, client_id[i],
968 SILC_ID_CLIENT, 0, SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
972 /* Find by nickname */
973 if (!silc_idlist_get_clients_by_hash(server->local_list,
974 nick, server->md5hash,
975 &clients, &clients_count))
976 silc_idlist_get_clients_by_nickname(server->local_list,
978 &clients, &clients_count);
980 if (!silc_idlist_get_clients_by_hash(server->global_list,
981 nick, server->md5hash,
982 &clients, &clients_count))
983 silc_idlist_get_clients_by_nickname(server->global_list,
985 &clients, &clients_count);
989 if (!clients && (client_id_count || nick)) {
990 /* If we are normal server and did not send the request first to router
991 do it now, since we do not have the information. */
992 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
993 server->server_type == SILC_SERVER && !cmd->pending &&
994 !server->standalone) {
995 silc_server_command_whois_send_router(cmd);
1000 /* Such client(s) really does not exist in the SILC network. */
1001 if (!client_id_count)
1002 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOIS,
1003 SILC_STATUS_ERR_NO_SUCH_NICK, 0,
1004 3, nick, strlen(nick));
1006 silc_server_command_whois_send_reply(cmd, NULL, 0,
1007 error_client, error_client_count,
1012 /* Router always finds the client entry if it exists in the SILC network.
1013 However, it might be incomplete entry and does not include all the
1014 mandatory fields that WHOIS command reply requires. Check for these and
1015 make query from the server who owns the client if some fields are
1017 if (!silc_server_command_whois_check(cmd, clients, clients_count)) {
1022 /* Send the command reply */
1023 silc_server_command_whois_send_reply(cmd, clients, clients_count,
1024 error_client, error_client_count,
1025 count, nick, client_id);
1028 if (client_id_count) {
1029 for (i = 0; i < client_id_count; i++)
1030 silc_free(client_id[i]);
1031 silc_free(client_id);
1034 silc_free(error_client);
1036 silc_free(server_name);
1041 /* Server side of command WHOIS. Processes user's query and sends found
1042 results as command replies back to the client. */
1044 SILC_SERVER_CMD_FUNC(whois)
1046 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1049 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 3328);
1051 ret = silc_server_command_whois_process(cmd);
1052 silc_server_command_free(cmd);
1055 /******************************************************************************
1059 ******************************************************************************/
1062 silc_server_command_whowas_parse(SilcServerCommandContext cmd,
1070 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1072 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WHOWAS,
1073 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1078 /* Get the nickname@server string and parse it. */
1079 silc_parse_userfqdn(tmp, nickname, server_name);
1081 /* Get the max count of reply messages allowed */
1082 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
1084 SILC_GET32_MSB(*count, tmp);
1092 silc_server_command_whowas_check(SilcServerCommandContext cmd,
1093 SilcClientEntry *clients,
1094 SilcUInt32 clients_count)
1096 SilcServer server = cmd->server;
1098 SilcClientEntry entry;
1100 for (i = 0; i < clients_count; i++) {
1103 if (!entry->nickname || !entry->username) {
1105 SilcUInt16 old_ident;
1110 old_ident = silc_command_get_ident(cmd->payload);
1111 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1112 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1114 /* Send WHOWAS command */
1115 silc_server_packet_send(server, entry->router->connection,
1116 SILC_PACKET_COMMAND, cmd->packet->flags,
1117 tmpbuf->data, tmpbuf->len, TRUE);
1119 /* Reprocess this packet after received reply */
1120 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
1121 silc_command_get_ident(cmd->payload),
1122 silc_server_command_whowas,
1123 silc_server_command_dup(cmd));
1124 cmd->pending = TRUE;
1125 silc_command_set_ident(cmd->payload, old_ident);
1127 silc_buffer_free(tmpbuf);
1136 silc_server_command_whowas_send_reply(SilcServerCommandContext cmd,
1137 SilcClientEntry *clients,
1138 SilcUInt32 clients_count)
1140 SilcServer server = cmd->server;
1142 int i, k, count = 0, len;
1143 SilcBuffer packet, idp;
1144 SilcClientEntry entry = NULL;
1146 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1147 char nh[256], uh[256];
1150 status = SILC_STATUS_OK;
1152 /* Process only entries that are not registered anymore. */
1154 for (i = 0; i < clients_count; i++) {
1155 if (clients[i]->data.status & SILC_IDLIST_STATUS_REGISTERED)
1162 /* No valid entries found at all, just send error */
1163 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
1164 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1165 SILC_STATUS_ERR_NO_SUCH_NICK, 0,
1166 3, tmp, tmp ? strlen(tmp) : 0);
1170 if (valid_count > 1)
1171 status = SILC_STATUS_LIST_START;
1173 for (i = 0, k = 0; i < clients_count; i++) {
1179 status = SILC_STATUS_LIST_ITEM;
1180 if (valid_count > 1 && k == valid_count - 1)
1181 status = SILC_STATUS_LIST_END;
1182 if (count && k - 1 == count)
1183 status = SILC_STATUS_LIST_END;
1185 /* Send WHOWAS reply */
1186 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1187 tmp = silc_argument_get_first_arg(cmd->args, NULL);
1188 memset(uh, 0, sizeof(uh));
1189 memset(nh, 0, sizeof(nh));
1191 strncat(nh, entry->nickname, strlen(entry->nickname));
1192 if (!strchr(entry->nickname, '@')) {
1193 strncat(nh, "@", 1);
1194 if (entry->servername) {
1195 strncat(nh, entry->servername, strlen(entry->servername));
1197 len = entry->router ? strlen(entry->router->server_name) :
1198 strlen(server->server_name);
1199 strncat(nh, entry->router ? entry->router->server_name :
1200 server->server_name, len);
1204 strncat(uh, entry->username, strlen(entry->username));
1205 if (!strchr(entry->username, '@')) {
1206 strncat(uh, "@", 1);
1207 strcat(uh, "*private*");
1211 silc_command_reply_payload_encode_va(SILC_COMMAND_WHOWAS,
1212 status, 0, ident, 4,
1213 2, idp->data, idp->len,
1218 strlen(entry->userinfo) : 0);
1219 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1220 0, packet->data, packet->len, FALSE);
1222 silc_buffer_free(packet);
1223 silc_buffer_free(idp);
1225 if (status == SILC_STATUS_LIST_END)
1232 silc_server_command_whowas_process(SilcServerCommandContext cmd)
1234 SilcServer server = cmd->server;
1235 char *nick = NULL, *server_name = NULL;
1237 SilcClientEntry *clients = NULL;
1238 SilcUInt32 clients_count = 0;
1240 bool check_global = FALSE;
1242 /* Protocol dictates that we must always send the received WHOWAS request
1243 to our router if we are normal server, so let's do it now unless we
1244 are standalone. We will not send any replies to the client until we
1245 have received reply from the router. */
1246 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1247 server->server_type == SILC_SERVER && !cmd->pending &&
1248 !server->standalone) {
1250 SilcUInt16 old_ident;
1252 old_ident = silc_command_get_ident(cmd->payload);
1253 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1254 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1256 /* Send WHOWAS command to our router */
1257 silc_server_packet_send(server, (SilcSocketConnection)
1258 server->router->connection,
1259 SILC_PACKET_COMMAND, cmd->packet->flags,
1260 tmpbuf->data, tmpbuf->len, TRUE);
1262 /* Reprocess this packet after received reply from router */
1263 silc_server_command_pending(server, SILC_COMMAND_WHOWAS,
1264 silc_command_get_ident(cmd->payload),
1265 silc_server_command_whowas,
1266 silc_server_command_dup(cmd));
1267 cmd->pending = TRUE;
1268 silc_command_set_ident(cmd->payload, old_ident);
1270 silc_buffer_free(tmpbuf);
1275 /* We are ready to process the command request. Let's search for the
1276 requested client and send reply to the requesting client. */
1278 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1279 check_global = TRUE;
1280 else if (server->server_type != SILC_SERVER)
1281 check_global = TRUE;
1283 /* Parse the whowas request */
1284 if (!silc_server_command_whowas_parse(cmd, &nick, &server_name, &count))
1287 /* Get all clients matching that nickname from local list */
1288 if (!silc_idlist_get_clients_by_nickname(server->local_list,
1290 &clients, &clients_count))
1291 silc_idlist_get_clients_by_hash(server->local_list,
1292 nick, server->md5hash,
1293 &clients, &clients_count);
1295 /* Check global list as well */
1297 if (!silc_idlist_get_clients_by_nickname(server->global_list,
1299 &clients, &clients_count))
1300 silc_idlist_get_clients_by_hash(server->global_list,
1301 nick, server->md5hash,
1302 &clients, &clients_count);
1306 /* Such a client really does not exist in the SILC network. */
1307 silc_server_command_send_status_data(cmd, SILC_COMMAND_WHOWAS,
1308 SILC_STATUS_ERR_NO_SUCH_NICK, 0,
1309 3, nick, strlen(nick));
1313 if (!silc_server_command_whowas_check(cmd, clients, clients_count)) {
1318 /* Send the command reply to the client */
1319 silc_server_command_whowas_send_reply(cmd, clients, clients_count);
1324 silc_free(server_name);
1328 /* Server side of command WHOWAS. */
1330 SILC_SERVER_CMD_FUNC(whowas)
1332 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1335 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
1337 ret = silc_server_command_whowas_process(cmd);
1338 silc_server_command_free(cmd);
1341 /******************************************************************************
1345 ******************************************************************************/
1348 silc_server_command_identify_send_router(SilcServerCommandContext cmd)
1350 SilcServer server = cmd->server;
1352 SilcUInt16 old_ident;
1354 old_ident = silc_command_get_ident(cmd->payload);
1355 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1356 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1358 /* Send IDENTIFY command to our router */
1359 silc_server_packet_send(server, (SilcSocketConnection)
1360 server->router->connection,
1361 SILC_PACKET_COMMAND, cmd->packet->flags,
1362 tmpbuf->data, tmpbuf->len, TRUE);
1364 /* Reprocess this packet after received reply from router */
1365 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
1366 silc_command_get_ident(cmd->payload),
1367 silc_server_command_identify,
1368 silc_server_command_dup(cmd));
1369 cmd->pending = TRUE;
1370 silc_command_set_ident(cmd->payload, old_ident);
1371 silc_buffer_free(tmpbuf);
1375 silc_server_command_identify_parse(SilcServerCommandContext cmd,
1376 SilcClientEntry **clients,
1377 SilcUInt32 *clients_count,
1378 SilcServerEntry **servers,
1379 SilcUInt32 *servers_count,
1380 SilcChannelEntry **channels,
1381 SilcUInt32 *channels_count,
1383 ResolveError *error_id,
1384 SilcUInt32 *error_id_count)
1386 SilcServer server = cmd->server;
1389 SilcUInt32 argc = silc_argument_get_arg_num(cmd->args);
1391 bool check_global = FALSE;
1395 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT)
1396 check_global = TRUE;
1397 else if (server->server_type != SILC_SERVER)
1398 check_global = TRUE;
1400 /* If ID Payload is in the command it must be used instead of names */
1401 tmp = silc_argument_get_arg_type(cmd->args, 5, &len);
1403 /* No ID, get the names. */
1405 /* If we are normal server and have not resolved information from
1406 router yet, do so now. */
1407 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1408 server->server_type == SILC_SERVER && !cmd->pending &&
1409 !server->standalone) {
1410 silc_server_command_identify_send_router(cmd);
1414 /* Try to get nickname@server. */
1415 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
1418 char *nick_server = NULL;
1420 silc_parse_userfqdn(tmp, &nick, &nick_server);
1422 if (!silc_idlist_get_clients_by_hash(server->local_list,
1423 nick, server->md5hash,
1424 clients, clients_count))
1425 silc_idlist_get_clients_by_nickname(server->local_list,
1427 clients, clients_count);
1429 if (!silc_idlist_get_clients_by_hash(server->global_list,
1430 nick, server->md5hash,
1431 clients, clients_count))
1432 silc_idlist_get_clients_by_nickname(server->global_list,
1434 clients, clients_count);
1438 silc_free(nick_server);
1441 /* the nickname does not exist, send error reply */
1442 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1443 SILC_STATUS_ERR_NO_SUCH_NICK, 0,
1444 3, tmp, strlen(tmp));
1449 /* Try to get server name */
1450 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
1452 entry = silc_idlist_find_server_by_name(server->local_list,
1454 if (!entry && check_global)
1455 entry = silc_idlist_find_server_by_name(server->global_list,
1458 *servers = silc_realloc(*servers, sizeof(**servers) *
1459 (*servers_count + 1));
1460 (*servers)[(*servers_count)++] = entry;
1464 /* the server does not exist, send error reply */
1465 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1466 SILC_STATUS_ERR_NO_SUCH_SERVER,
1467 0, 3, tmp, strlen(tmp));
1472 /* Try to get channel name */
1473 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
1475 entry = silc_idlist_find_channel_by_name(server->local_list,
1477 if (!entry && check_global)
1478 entry = silc_idlist_find_channel_by_name(server->global_list,
1481 *channels = silc_realloc(*channels, sizeof(**channels) *
1482 (*channels_count + 1));
1483 (*channels)[(*channels_count)++] = entry;
1487 /* The channel does not exist, send error reply */
1488 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1489 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
1490 0, 3, tmp, strlen(tmp));
1495 if (!(*clients) && !(*servers) && !(*channels)) {
1496 silc_server_command_send_status_reply(cmd, SILC_COMMAND_IDENTIFY,
1497 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1502 /* Command includes ID, we must use that. Also check whether the command
1503 has more than one ID set - take them all. */
1505 /* Take all ID's from the command packet */
1506 for (i = 0; i < argc; i++) {
1509 tmp = silc_argument_get_arg_type(cmd->args, i + 5, &len);
1513 idp = silc_id_payload_parse(tmp, len);
1515 ADD_ERROR((*error_id), (*error_id_count), NULL, 0, i + 5,
1516 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1518 id = silc_id_payload_get_id(idp);
1519 switch (silc_id_payload_get_type(idp)) {
1521 case SILC_ID_CLIENT:
1522 entry = silc_idlist_find_client_by_id(server->local_list,
1524 if (!entry && check_global)
1525 entry = silc_idlist_find_client_by_id(server->global_list,
1528 *clients = silc_realloc(*clients, sizeof(**clients) *
1529 (*clients_count + 1));
1530 (*clients)[(*clients_count)++] = (SilcClientEntry)entry;
1532 /* If we are normal server and have not resolved information from
1533 router yet, do so now. */
1534 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1535 server->server_type == SILC_SERVER && !cmd->pending &&
1536 !server->standalone) {
1537 silc_server_command_identify_send_router(cmd);
1538 silc_free(*clients);
1539 silc_free(*servers);
1540 silc_free(*channels);
1541 silc_free(*error_id);
1545 ADD_ERROR((*error_id), (*error_id_count), NULL, 0, i + 5,
1546 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
1551 case SILC_ID_SERVER:
1552 entry = silc_idlist_find_server_by_id(server->local_list,
1554 if (!entry && check_global)
1555 entry = silc_idlist_find_server_by_id(server->global_list,
1558 *servers = silc_realloc(*servers, sizeof(**servers) *
1559 (*servers_count + 1));
1560 (*servers)[(*servers_count)++] = (SilcServerEntry)entry;
1562 /* If we are normal server and have not resolved information from
1563 router yet, do so now. */
1564 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1565 server->server_type == SILC_SERVER && !cmd->pending &&
1566 !server->standalone) {
1567 silc_server_command_identify_send_router(cmd);
1568 silc_free(*clients);
1569 silc_free(*servers);
1570 silc_free(*channels);
1571 silc_free(*error_id);
1575 ADD_ERROR((*error_id), (*error_id_count), NULL, 0, i + 5,
1576 SILC_STATUS_ERR_NO_SUCH_SERVER_ID);
1580 case SILC_ID_CHANNEL:
1581 entry = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1582 if (!entry && check_global)
1583 entry = silc_idlist_find_channel_by_id(server->global_list, id,
1586 *channels = silc_realloc(*channels, sizeof(**channels) *
1587 (*channels_count + 1));
1588 (*channels)[(*channels_count)++] = (SilcChannelEntry)entry;
1590 /* If we are normal server and have not resolved information from
1591 router yet, do so now. */
1592 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT &&
1593 server->server_type == SILC_SERVER && !cmd->pending &&
1594 !server->standalone) {
1595 silc_server_command_identify_send_router(cmd);
1596 silc_free(*clients);
1597 silc_free(*servers);
1598 silc_free(*channels);
1599 silc_free(*error_id);
1603 ADD_ERROR((*error_id), (*error_id_count), NULL, 0, i + 5,
1604 SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID);
1613 /* Get the max count of reply messages allowed */
1614 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
1616 SILC_GET32_MSB(*count, tmp);
1623 /* Checks that all mandatory fields in client entry are present. If not
1624 then send WHOIS request to the server who owns the client. We use
1625 WHOIS because we want to get as much information as possible at once. */
1628 silc_server_command_identify_check_client(SilcServerCommandContext cmd,
1629 SilcClientEntry *clients,
1630 SilcUInt32 clients_count)
1632 SilcServer server = cmd->server;
1633 SilcClientEntry entry;
1634 SilcServerResolveContext resolve = NULL, r = NULL;
1635 SilcUInt32 resolve_count = 0;
1639 for (i = 0; i < clients_count; i++) {
1644 if (entry->nickname ||
1645 !(entry->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
1649 /* If we are normal server, and we've not resolved this client from
1650 router and it is global client, we'll check whether it is on some
1651 channel. If not then we cannot be sure about its validity, and
1652 we'll resolve it from router. */
1653 if (cmd->server->server_type != SILC_SERVER || cmd->pending ||
1654 entry->connection || silc_hash_table_count(entry->channels))
1658 /* We need to resolve this entry since it is not complete */
1660 if (!cmd->pending && entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
1661 /* The entry is being resolved (and we are not the resolver) so attach
1662 to the command reply and we're done with this one. */
1663 silc_server_command_pending(server, SILC_COMMAND_NONE,
1664 entry->resolve_cmd_ident,
1665 silc_server_command_identify,
1666 silc_server_command_dup(cmd));
1669 if (entry->data.status & SILC_IDLIST_STATUS_RESOLVING) {
1670 /* We've resolved this and it still is not ready. We'll return
1671 and are that this will be handled again after it is resolved. */
1672 for (i = 0; i < resolve_count; i++) {
1673 for (k = 0; k < r->res_argc; k++)
1674 silc_free(r->res_argv[k]);
1675 silc_free(r->res_argv);
1676 silc_free(r->res_argv_lens);
1677 silc_free(r->res_argv_types);
1682 /* We'll resolve this client */
1686 for (k = 0; k < resolve_count; k++) {
1687 if (resolve[k].router == entry->router) {
1694 resolve = silc_realloc(resolve, sizeof(*resolve) *
1695 (resolve_count + 1));
1696 r = &resolve[resolve_count];
1697 memset(r, 0, sizeof(*r));
1698 r->router = entry->router;
1699 r->ident = ++server->cmd_ident;
1703 r->res_argv = silc_realloc(r->res_argv, sizeof(*r->res_argv) *
1705 r->res_argv_lens = silc_realloc(r->res_argv_lens,
1706 sizeof(*r->res_argv_lens) *
1708 r->res_argv_types = silc_realloc(r->res_argv_types,
1709 sizeof(*r->res_argv_types) *
1711 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1712 r->res_argv[r->res_argc] = silc_calloc(idp->len,
1713 sizeof(**r->res_argv));
1714 memcpy(r->res_argv[r->res_argc], idp->data, idp->len);
1715 r->res_argv_lens[r->res_argc] = idp->len;
1716 r->res_argv_types[r->res_argc] = r->res_argc + 4;
1718 silc_buffer_free(idp);
1720 entry->resolve_cmd_ident = r->ident;
1721 entry->data.status |= SILC_IDLIST_STATUS_RESOLVING;
1722 entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
1727 /* Do the resolving */
1728 for (i = 0; i < resolve_count; i++) {
1733 /* Send WHOIS request. We send WHOIS since we're doing the requesting
1734 now anyway so make it a good one. */
1735 res_cmd = silc_command_payload_encode(SILC_COMMAND_WHOIS,
1736 r->res_argc, r->res_argv,
1740 silc_server_packet_send(server, r->router->connection,
1741 SILC_PACKET_COMMAND, cmd->packet->flags,
1742 res_cmd->data, res_cmd->len, FALSE);
1744 /* Reprocess this packet after received reply */
1745 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1747 silc_server_command_identify,
1748 silc_server_command_dup(cmd));
1749 cmd->pending = TRUE;
1751 silc_buffer_free(res_cmd);
1752 for (k = 0; k < r->res_argc; k++)
1753 silc_free(r->res_argv[k]);
1754 silc_free(r->res_argv);
1755 silc_free(r->res_argv_lens);
1756 silc_free(r->res_argv_types);
1765 silc_server_command_identify_send_reply(SilcServerCommandContext cmd,
1766 SilcClientEntry *clients,
1767 SilcUInt32 clients_count,
1768 SilcServerEntry *servers,
1769 SilcUInt32 servers_count,
1770 SilcChannelEntry *channels,
1771 SilcUInt32 channels_count,
1772 ResolveError errors,
1773 SilcUInt32 errors_count,
1776 SilcServer server = cmd->server;
1777 int i, k, valid_count;
1779 SilcBuffer packet, idp;
1781 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1782 char nh[256], uh[256];
1783 SilcSocketConnection hsock;
1786 status = SILC_STATUS_OK;
1789 SilcClientEntry entry;
1790 valid_count = clients_count;
1792 if (silc_argument_get_arg_type(cmd->args, 1, NULL)) {
1793 /* Process only valid clients and ignore those that are not registered.
1794 This is checked with nickname only because when resolved client IDs
1795 we check that they are registered earlier. */
1797 for (i = 0; i < clients_count; i++) {
1798 if (clients[i]->data.status & SILC_IDLIST_STATUS_REGISTERED)
1805 /* No valid entries found at all, just send error */
1806 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
1807 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1808 SILC_STATUS_ERR_NO_SUCH_NICK, 0,
1809 3, tmp, tmp ? strlen(tmp) : 0);
1814 /* Process all valid client entries and send command replies */
1816 if (valid_count > 1)
1817 status = SILC_STATUS_LIST_START;
1819 for (i = 0, k = 0; i < clients_count; i++) {
1825 status = SILC_STATUS_LIST_ITEM;
1826 if (valid_count > 1 && k == valid_count - 1
1827 && !servers_count && !channels_count && !errors_count)
1828 status = SILC_STATUS_LIST_END;
1829 if (count && k - 1 == count)
1830 status = SILC_STATUS_LIST_END;
1832 /* Send IDENTIFY reply */
1834 idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
1835 memset(uh, 0, sizeof(uh));
1836 memset(nh, 0, sizeof(nh));
1837 strncat(nh, entry->nickname, strlen(entry->nickname));
1838 if (!strchr(entry->nickname, '@')) {
1839 strncat(nh, "@", 1);
1840 if (entry->servername) {
1841 strncat(nh, entry->servername, strlen(entry->servername));
1843 len = entry->router ? strlen(entry->router->server_name) :
1844 strlen(server->server_name);
1845 strncat(nh, entry->router ? entry->router->server_name :
1846 server->server_name, len);
1850 if (!entry->username) {
1851 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1852 status, 0, ident, 2,
1853 2, idp->data, idp->len,
1856 strncat(uh, entry->username, strlen(entry->username));
1857 if (!strchr(entry->username, '@') && entry->connection) {
1858 strncat(uh, "@", 1);
1859 hsock = (SilcSocketConnection)entry->connection;
1860 len = strlen(hsock->hostname);
1861 strncat(uh, hsock->hostname, len);
1864 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1865 status, 0, ident, 3,
1866 2, idp->data, idp->len,
1871 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1872 0, packet->data, packet->len, FALSE);
1874 silc_buffer_free(packet);
1875 silc_buffer_free(idp);
1877 if (status == SILC_STATUS_LIST_END)
1884 SilcServerEntry entry;
1886 if (status == SILC_STATUS_OK && servers_count > 1)
1887 status = SILC_STATUS_LIST_START;
1889 for (i = 0, k = 0; i < servers_count; i++) {
1893 status = SILC_STATUS_LIST_ITEM;
1894 if (servers_count > 1 && k == servers_count - 1 && !channels_count &&
1896 status = SILC_STATUS_LIST_END;
1897 if (count && k - 1 == count)
1898 status = SILC_STATUS_LIST_END;
1900 /* Send IDENTIFY reply */
1901 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1903 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1904 status, 0, ident, 2,
1905 2, idp->data, idp->len,
1906 3, entry->server_name,
1907 entry->server_name ?
1908 strlen(entry->server_name) : 0);
1909 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1910 0, packet->data, packet->len, FALSE);
1912 silc_buffer_free(packet);
1913 silc_buffer_free(idp);
1915 if (status == SILC_STATUS_LIST_END)
1922 SilcChannelEntry entry;
1924 if (status == SILC_STATUS_OK && channels_count > 1)
1925 status = SILC_STATUS_LIST_START;
1927 for (i = 0, k = 0; i < channels_count; i++) {
1928 entry = channels[i];
1931 status = SILC_STATUS_LIST_ITEM;
1932 if (channels_count > 1 && k == channels_count - 1 && !errors_count)
1933 status = SILC_STATUS_LIST_END;
1934 if (count && k - 1 == count)
1935 status = SILC_STATUS_LIST_END;
1937 /* Send IDENTIFY reply */
1938 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
1940 silc_command_reply_payload_encode_va(SILC_COMMAND_IDENTIFY,
1941 status, 0, ident, 2,
1942 2, idp->data, idp->len,
1943 3, entry->channel_name,
1944 entry->channel_name ?
1945 strlen(entry->channel_name): 0);
1946 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1947 0, packet->data, packet->len, FALSE);
1949 silc_buffer_free(packet);
1950 silc_buffer_free(idp);
1952 if (status == SILC_STATUS_LIST_END)
1958 /* Send error replies */
1960 if (status == SILC_STATUS_OK && errors_count > 1)
1961 status = SILC_STATUS_LIST_START;
1964 for (i = 0, k = 0; i < errors_count; i++) {
1966 idp = silc_id_payload_encode(errors[i].id, SILC_ID_CLIENT);
1970 tmp = silc_argument_get_arg_type(cmd->args, errors[i].index, &len);
1974 status = SILC_STATUS_LIST_ITEM;
1975 if (errors_count > 1 && k == errors_count - 1)
1976 status = SILC_STATUS_LIST_END;
1977 if (count && k - 1 == count)
1978 status = SILC_STATUS_LIST_END;
1981 silc_server_command_send_status_data(cmd, SILC_COMMAND_IDENTIFY,
1982 (status == SILC_STATUS_OK ?
1983 errors[i].error : status),
1984 (status == SILC_STATUS_OK ?
1985 0 : errors[i].error),
1987 silc_buffer_free(idp);
1990 if (status == SILC_STATUS_LIST_END)
1998 silc_server_command_identify_process(SilcServerCommandContext cmd)
2000 SilcUInt32 count = 0;
2002 SilcClientEntry *clients = NULL;
2003 SilcServerEntry *servers = NULL;
2004 SilcChannelEntry *channels = NULL;
2005 SilcUInt32 clients_count = 0, servers_count = 0, channels_count = 0;
2006 SilcUInt32 errors_count = 0;
2007 ResolveError errors = NULL;
2009 /* Parse the IDENTIFY request */
2010 ret = silc_server_command_identify_parse(cmd,
2011 &clients, &clients_count,
2012 &servers, &servers_count,
2013 &channels, &channels_count,
2014 &count, &errors, &errors_count);
2019 /* Check that all mandatory fields are present and request those data
2020 from the server who owns the client if necessary. */
2021 if (!silc_server_command_identify_check_client(cmd, clients,
2027 /* Send the command reply to the client */
2028 silc_server_command_identify_send_reply(cmd,
2029 clients, clients_count,
2030 servers, servers_count,
2031 channels, channels_count,
2032 errors, errors_count,
2038 silc_free(channels);
2043 SILC_SERVER_CMD_FUNC(identify)
2045 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2048 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 3328);
2050 ret = silc_server_command_identify_process(cmd);
2051 silc_server_command_free(cmd);
2054 /* Server side of command NICK. Sets nickname for user. Setting
2055 nickname causes generation of a new client ID for the client. The
2056 new client ID is sent to the client after changing the nickname. */
2058 SILC_SERVER_CMD_FUNC(nick)
2060 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2061 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2062 SilcServer server = cmd->server;
2063 SilcBuffer packet, nidp, oidp = NULL;
2064 SilcClientID *new_id;
2065 SilcUInt32 nick_len;
2067 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2070 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2073 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
2075 /* Check nickname */
2076 nick = silc_argument_get_arg_type(cmd->args, 1, &nick_len);
2079 if (silc_server_name_bad_chars(nick, nick_len) == TRUE) {
2080 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
2081 SILC_STATUS_ERR_BAD_NICKNAME, 0);
2085 /* Check for same nickname */
2086 if (!strcmp(client->nickname, nick)) {
2087 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2091 /* Create new Client ID */
2092 while (!silc_id_create_client_id(cmd->server, cmd->server->id,
2094 cmd->server->md5hash, nick,
2098 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
2099 SILC_STATUS_ERR_BAD_NICKNAME, 0);
2102 snprintf(&nick[strlen(nick) - 1], 1, "%d", nickfail);
2105 /* Send notify about nickname change to our router. We send the new
2106 ID and ask to replace it with the old one. If we are router the
2107 packet is broadcasted. Send NICK_CHANGE notify. */
2108 if (!server->standalone)
2109 silc_server_send_notify_nick_change(server, server->router->connection,
2110 server->server_type == SILC_SERVER ?
2111 FALSE : TRUE, client->id,
2114 /* Check if anyone is watching the old nickname */
2115 if (server->server_type == SILC_ROUTER)
2116 silc_server_check_watcher_list(server, client, nick,
2117 SILC_NOTIFY_TYPE_NICK_CHANGE);
2119 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2121 /* Remove old cache entry */
2122 silc_idcache_del_by_context(server->local_list->clients, client);
2124 silc_free(client->id);
2125 client->id = new_id;
2127 silc_free(client->nickname);
2128 client->nickname = strdup(nick);
2130 /* Update client cache */
2131 silc_idcache_add(server->local_list->clients, client->nickname,
2132 client->id, (void *)client, 0, NULL);
2134 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2136 /* Send NICK_CHANGE notify to the client's channels */
2137 silc_server_send_notify_on_channels(server, NULL, client,
2138 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
2139 oidp->data, oidp->len,
2140 nidp->data, nidp->len,
2142 strlen(client->nickname));
2144 /* Check if anyone is watching the new nickname */
2145 if (server->server_type == SILC_ROUTER)
2146 silc_server_check_watcher_list(server, client, NULL,
2147 SILC_NOTIFY_TYPE_NICK_CHANGE);
2150 /* Send the new Client ID as reply command back to client */
2151 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
2152 SILC_STATUS_OK, 0, ident, 2,
2153 2, nidp->data, nidp->len,
2154 3, nick, strlen(nick));
2155 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2156 0, packet->data, packet->len, FALSE);
2158 silc_buffer_free(packet);
2159 silc_buffer_free(nidp);
2161 silc_buffer_free(oidp);
2164 silc_server_command_free(cmd);
2167 /* Sends the LIST command reply */
2170 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
2171 SilcChannelEntry *lch,
2172 SilcUInt32 lch_count,
2173 SilcChannelEntry *gch,
2174 SilcUInt32 gch_count)
2177 SilcBuffer packet, idp;
2178 SilcChannelEntry entry;
2180 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2182 unsigned char usercount[4];
2184 int valid_lcount = 0, valid_rcount = 0;
2186 for (i = 0; i < lch_count; i++) {
2187 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
2192 for (i = 0; i < gch_count; i++) {
2193 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
2199 status = SILC_STATUS_OK;
2200 if ((lch_count + gch_count) > 1)
2201 status = SILC_STATUS_LIST_START;
2204 for (i = 0, k = 0; i < lch_count; i++) {
2210 status = SILC_STATUS_LIST_ITEM;
2211 if (valid_lcount > 1 && k == valid_lcount - 1 && !valid_rcount)
2212 status = SILC_STATUS_LIST_END;
2214 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
2216 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
2217 topic = "*private*";
2218 memset(usercount, 0, sizeof(usercount));
2220 topic = entry->topic;
2221 users = silc_hash_table_count(entry->user_list);
2222 SILC_PUT32_MSB(users, usercount);
2225 /* Send the reply */
2227 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
2228 status, 0, ident, 4,
2229 2, idp->data, idp->len,
2230 3, entry->channel_name,
2231 strlen(entry->channel_name),
2232 4, topic, topic ? strlen(topic) : 0,
2234 silc_server_packet_send(cmd->server, cmd->sock,
2235 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
2236 packet->len, FALSE);
2237 silc_buffer_free(packet);
2238 silc_buffer_free(idp);
2243 for (i = 0, k = 0; i < gch_count; i++) {
2249 status = SILC_STATUS_LIST_ITEM;
2250 if (valid_rcount > 1 && k == valid_rcount - 1)
2251 status = SILC_STATUS_LIST_END;
2253 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
2255 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
2256 topic = "*private*";
2257 memset(usercount, 0, sizeof(usercount));
2259 topic = entry->topic;
2260 users = entry->user_count;
2261 SILC_PUT32_MSB(users, usercount);
2264 /* Send the reply */
2266 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
2267 status, 0, ident, 4,
2268 2, idp->data, idp->len,
2269 3, entry->channel_name,
2270 strlen(entry->channel_name),
2271 4, topic, topic ? strlen(topic) : 0,
2273 silc_server_packet_send(cmd->server, cmd->sock,
2274 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
2275 packet->len, FALSE);
2276 silc_buffer_free(packet);
2277 silc_buffer_free(idp);
2282 /* Server side of LIST command. This lists the channel of the requested
2283 server. Secret channels are not listed. */
2285 SILC_SERVER_CMD_FUNC(list)
2287 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2288 SilcServer server = cmd->server;
2289 SilcChannelID *channel_id = NULL;
2292 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
2293 SilcUInt32 lch_count = 0, gch_count = 0;
2295 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 1);
2297 /* If we are normal server, send the command to router, since we
2298 want to know all channels in the network. */
2299 if (!cmd->pending && server->server_type == SILC_SERVER &&
2300 !server->standalone) {
2302 SilcUInt16 old_ident;
2304 old_ident = silc_command_get_ident(cmd->payload);
2305 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2306 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2307 silc_server_packet_send(server, server->router->connection,
2308 SILC_PACKET_COMMAND, cmd->packet->flags,
2309 tmpbuf->data, tmpbuf->len, TRUE);
2311 /* Reprocess this packet after received reply from router */
2312 silc_server_command_pending(server, SILC_COMMAND_LIST,
2313 silc_command_get_ident(cmd->payload),
2314 silc_server_command_list,
2315 silc_server_command_dup(cmd));
2316 cmd->pending = TRUE;
2317 silc_command_set_ident(cmd->payload, old_ident);
2318 silc_buffer_free(tmpbuf);
2322 /* Get Channel ID */
2323 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2325 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
2327 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
2328 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2333 /* Get the channels from local list */
2334 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
2337 /* Get the channels from global list */
2338 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
2341 /* Send the reply */
2342 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
2343 gchannels, gch_count);
2345 silc_free(lchannels);
2346 silc_free(gchannels);
2349 silc_server_command_free(cmd);
2352 /* Server side of TOPIC command. Sets topic for channel and/or returns
2353 current topic to client. */
2355 SILC_SERVER_CMD_FUNC(topic)
2357 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2358 SilcServer server = cmd->server;
2359 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2360 SilcChannelID *channel_id;
2361 SilcChannelEntry channel;
2362 SilcChannelClientEntry chl;
2363 SilcBuffer packet, idp;
2365 SilcUInt32 argc, tmp_len;
2366 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2368 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
2370 argc = silc_argument_get_arg_num(cmd->args);
2372 /* Get Channel ID */
2373 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2375 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2376 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2379 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
2381 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2382 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2386 /* Check whether the channel exists */
2387 channel = silc_idlist_find_channel_by_id(server->local_list,
2390 channel = silc_idlist_find_channel_by_id(server->global_list,
2393 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2394 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
2402 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
2404 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2405 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2410 if (strlen(tmp) > 256) {
2411 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2412 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2417 /* See whether the client is on channel and has rights to change topic */
2418 if (!silc_server_client_on_channel(client, channel, &chl)) {
2419 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2420 SILC_STATUS_ERR_NOT_ON_CHANNEL,
2425 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
2426 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
2427 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2428 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
2429 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
2434 /* Set the topic for channel */
2435 silc_free(channel->topic);
2436 channel->topic = strdup(tmp);
2438 /* Send TOPIC_SET notify type to the network */
2439 if (!server->standalone)
2440 silc_server_send_notify_topic_set(server, server->router->connection,
2441 server->server_type == SILC_ROUTER ?
2442 TRUE : FALSE, channel,
2443 client->id, SILC_ID_CLIENT,
2446 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2448 /* Send notify about topic change to all clients on the channel */
2449 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2450 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
2451 idp->data, idp->len,
2452 channel->topic, strlen(channel->topic));
2453 silc_buffer_free(idp);
2456 /* Send the topic to client as reply packet */
2457 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2458 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
2459 SILC_STATUS_OK, 0, ident, 2,
2460 2, idp->data, idp->len,
2463 strlen(channel->topic) : 0);
2464 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
2465 0, packet->data, packet->len, FALSE);
2467 silc_buffer_free(packet);
2468 silc_buffer_free(idp);
2469 silc_free(channel_id);
2472 silc_server_command_free(cmd);
2475 /* Server side of INVITE command. Invites some client to join some channel.
2476 This command is also used to manage the invite list of the channel. */
2478 SILC_SERVER_CMD_FUNC(invite)
2480 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2481 SilcServer server = cmd->server;
2482 SilcSocketConnection sock = cmd->sock, dest_sock;
2483 SilcChannelClientEntry chl;
2484 SilcClientEntry sender, dest;
2485 SilcClientID *dest_id = NULL;
2486 SilcChannelEntry channel;
2487 SilcChannelID *channel_id = NULL;
2488 SilcIDListData idata;
2489 SilcBuffer idp, idp2, packet;
2490 unsigned char *tmp, *add, *del;
2492 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2494 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
2496 /* Get Channel ID */
2497 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2499 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2500 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2503 channel_id = silc_id_payload_parse_id(tmp, len, NULL);
2505 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2506 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2510 /* Get the channel entry */
2511 channel = silc_idlist_find_channel_by_id(server->local_list,
2514 channel = silc_idlist_find_channel_by_id(server->global_list,
2517 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2518 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
2524 /* Check whether the sender of this command is on the channel. */
2525 sender = (SilcClientEntry)sock->user_data;
2526 if (!silc_server_client_on_channel(sender, channel, &chl)) {
2527 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2528 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
2532 /* Check whether the channel is invite-only channel. If yes then the
2533 sender of this command must be at least channel operator. */
2534 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
2535 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
2536 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2537 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2538 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
2543 /* Get destination client ID */
2544 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
2549 dest_id = silc_id_payload_parse_id(tmp, len, NULL);
2551 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2552 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
2556 /* Get the client entry */
2557 dest = silc_server_get_client_resolve(server, dest_id, FALSE, &resolve);
2559 if (server->server_type != SILC_SERVER || !resolve) {
2560 silc_server_command_send_status_reply(
2561 cmd, SILC_COMMAND_INVITE,
2562 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID, 0);
2566 /* The client info is being resolved. Reprocess this packet after
2567 receiving the reply to the query. */
2568 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
2570 silc_server_command_invite,
2571 silc_server_command_dup(cmd));
2572 cmd->pending = TRUE;
2573 silc_free(channel_id);
2578 /* Check whether the requested client is already on the channel. */
2579 if (silc_server_client_on_channel(dest, channel, NULL)) {
2580 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2581 SILC_STATUS_ERR_USER_ON_CHANNEL,
2586 /* Get route to the client */
2587 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id,
2590 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
2591 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
2596 memset(invite, 0, sizeof(invite));
2597 strncat(invite, dest->nickname, strlen(dest->nickname));
2598 strncat(invite, "!", 1);
2599 strncat(invite, dest->username, strlen(dest->username));
2600 if (!strchr(dest->username, '@')) {
2601 strncat(invite, "@", 1);
2602 strncat(invite, cmd->sock->hostname, strlen(cmd->sock->hostname));
2605 len = strlen(invite);
2606 if (!channel->invite_list)
2607 channel->invite_list = silc_calloc(len + 2,
2608 sizeof(*channel->invite_list));
2610 channel->invite_list = silc_realloc(channel->invite_list,
2611 sizeof(*channel->invite_list) *
2613 strlen(channel->invite_list) + 2));
2614 strncat(channel->invite_list, invite, len);
2615 strncat(channel->invite_list, ",", 1);
2617 if (!(dest->mode & SILC_UMODE_BLOCK_INVITE)) {
2618 /* Send notify to the client that is invited to the channel */
2619 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
2620 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
2621 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
2623 SILC_NOTIFY_TYPE_INVITE, 3,
2624 idp->data, idp->len,
2625 channel->channel_name,
2626 strlen(channel->channel_name),
2627 idp2->data, idp2->len);
2628 silc_buffer_free(idp);
2629 silc_buffer_free(idp2);
2633 /* Add the client to the invite list of the channel */
2634 add = silc_argument_get_arg_type(cmd->args, 3, &len);
2636 if (!channel->invite_list)
2637 channel->invite_list = silc_calloc(len + 2,
2638 sizeof(*channel->invite_list));
2640 channel->invite_list = silc_realloc(channel->invite_list,
2641 sizeof(*channel->invite_list) *
2643 strlen(channel->invite_list) + 2));
2644 if (add[len - 1] == ',')
2645 add[len - 1] = '\0';
2647 strncat(channel->invite_list, add, len);
2648 strncat(channel->invite_list, ",", 1);
2651 /* Get the invite to be removed and remove it from the list */
2652 del = silc_argument_get_arg_type(cmd->args, 4, &len);
2653 if (del && channel->invite_list) {
2654 char *start, *end, *n;
2656 if (!strncmp(channel->invite_list, del,
2657 strlen(channel->invite_list) - 1)) {
2658 silc_free(channel->invite_list);
2659 channel->invite_list = NULL;
2661 start = strstr(channel->invite_list, del);
2662 if (start && strlen(start) >= len) {
2664 n = silc_calloc(strlen(channel->invite_list) - len, sizeof(*n));
2665 strncat(n, channel->invite_list, start - channel->invite_list);
2666 strncat(n, end + 1, ((channel->invite_list +
2667 strlen(channel->invite_list)) - end) - 1);
2668 silc_free(channel->invite_list);
2669 channel->invite_list = n;
2674 /* Send notify to the primary router */
2675 if (!server->standalone)
2676 silc_server_send_notify_invite(server, server->router->connection,
2677 server->server_type == SILC_ROUTER ?
2678 TRUE : FALSE, channel,
2679 sender->id, add, del);
2681 /* Send command reply */
2682 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2686 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2687 SILC_STATUS_OK, 0, ident, 2,
2689 3, channel->invite_list,
2690 channel->invite_list ?
2691 strlen(channel->invite_list) : 0);
2694 silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
2695 SILC_STATUS_OK, 0, ident, 1,
2697 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2698 packet->data, packet->len, FALSE);
2699 silc_buffer_free(packet);
2703 silc_free(channel_id);
2704 silc_server_command_free(cmd);
2709 SilcSocketConnection sock;
2713 /* Quits connection to client. This gets called if client won't
2714 close the connection even when it has issued QUIT command. */
2716 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
2718 QuitInternal q = (QuitInternal)context;
2720 /* Free all client specific data, such as client entry and entires
2721 on channels this client may be on. */
2722 silc_server_free_client_data(q->server, q->sock, q->sock->user_data,
2724 q->sock->user_data = NULL;
2726 /* Close the connection on our side */
2727 silc_server_close_connection(q->server, q->sock);
2729 silc_free(q->signoff);
2733 /* Quits SILC session. This is the normal way to disconnect client. */
2735 SILC_SERVER_CMD_FUNC(quit)
2737 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2738 SilcServer server = cmd->server;
2739 SilcSocketConnection sock = cmd->sock;
2741 unsigned char *tmp = NULL;
2744 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
2746 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2750 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
2754 q = silc_calloc(1, sizeof(*q));
2757 q->signoff = tmp ? strdup(tmp) : NULL;
2759 /* We quit the connection with little timeout */
2760 silc_schedule_task_add(server->schedule, sock->sock,
2761 silc_server_command_quit_cb, (void *)q,
2762 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2765 silc_server_command_free(cmd);
2768 /* Server side of command KILL. This command is used by router operator
2769 to remove an client from the SILC Network temporarily. */
2771 SILC_SERVER_CMD_FUNC(kill)
2773 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2774 SilcServer server = cmd->server;
2775 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2776 SilcClientEntry remote_client;
2777 SilcClientID *client_id;
2778 unsigned char *tmp, *comment;
2779 SilcUInt32 tmp_len, tmp_len2;
2782 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 2);
2784 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
2787 /* KILL command works only on router */
2788 if (server->server_type != SILC_ROUTER) {
2789 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2790 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
2794 /* Check whether client has the permissions. */
2795 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2796 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2797 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
2801 /* Get the client ID */
2802 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2804 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2805 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2809 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
2811 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2812 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
2817 /* Get the client entry */
2818 remote_client = silc_idlist_find_client_by_id(server->local_list,
2819 client_id, TRUE, NULL);
2821 if (!remote_client) {
2822 remote_client = silc_idlist_find_client_by_id(server->global_list,
2823 client_id, TRUE, NULL);
2825 if (!remote_client) {
2826 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2827 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
2834 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
2838 /* Send reply to the sender */
2839 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
2842 /* Check if anyone is watching this nickname */
2843 if (server->server_type == SILC_ROUTER)
2844 silc_server_check_watcher_list(server, client, NULL,
2845 SILC_NOTIFY_TYPE_KILLED);
2847 /* Now do the killing */
2848 silc_server_kill_client(server, remote_client, comment, client->id,
2852 silc_server_command_free(cmd);
2855 /* Server side of command INFO. This sends information about us to
2856 the client. If client requested specific server we will send the
2857 command to that server. */
2859 SILC_SERVER_CMD_FUNC(info)
2861 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2862 SilcServer server = cmd->server;
2863 SilcBuffer packet, idp;
2866 char *dest_server, *server_info = NULL, *server_name;
2867 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2868 SilcServerEntry entry = NULL;
2869 SilcServerID *server_id = NULL;
2871 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
2873 /* Get server name */
2874 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2877 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2879 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
2881 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2882 SILC_STATUS_ERR_NO_SERVER_ID, 0);
2888 /* Check whether we have this server cached */
2889 entry = silc_idlist_find_server_by_id(server->local_list,
2890 server_id, TRUE, NULL);
2892 entry = silc_idlist_find_server_by_id(server->global_list,
2893 server_id, TRUE, NULL);
2894 if (!entry && server->server_type != SILC_SERVER) {
2895 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2896 SILC_STATUS_ERR_NO_SUCH_SERVER,
2903 /* Some buggy servers has sent request to router about themselves. */
2904 if (server->server_type != SILC_SERVER && cmd->sock->user_data == entry)
2907 if ((!dest_server && !server_id && !entry) || (entry &&
2908 entry == server->id_entry) ||
2909 (dest_server && !cmd->pending &&
2910 !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
2911 /* Send our reply */
2912 char info_string[256];
2914 memset(info_string, 0, sizeof(info_string));
2915 snprintf(info_string, sizeof(info_string),
2916 "location: %s server: %s admin: %s <%s>",
2917 server->config->server_info->location,
2918 server->config->server_info->server_type,
2919 server->config->server_info->admin,
2920 server->config->server_info->email);
2922 server_info = info_string;
2923 entry = server->id_entry;
2925 /* Check whether we have this server cached */
2926 if (!entry && dest_server) {
2927 entry = silc_idlist_find_server_by_name(server->global_list,
2928 dest_server, TRUE, NULL);
2930 entry = silc_idlist_find_server_by_name(server->local_list,
2931 dest_server, TRUE, NULL);
2935 if (!cmd->pending &&
2936 server->server_type != SILC_SERVER && entry && !entry->server_info) {
2937 /* Send to the server */
2939 SilcUInt16 old_ident;
2941 old_ident = silc_command_get_ident(cmd->payload);
2942 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2943 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2945 silc_server_packet_send(server, entry->connection,
2946 SILC_PACKET_COMMAND, cmd->packet->flags,
2947 tmpbuf->data, tmpbuf->len, TRUE);
2949 /* Reprocess this packet after received reply from router */
2950 silc_server_command_pending(server, SILC_COMMAND_INFO,
2951 silc_command_get_ident(cmd->payload),
2952 silc_server_command_info,
2953 silc_server_command_dup(cmd));
2954 cmd->pending = TRUE;
2955 silc_command_set_ident(cmd->payload, old_ident);
2956 silc_buffer_free(tmpbuf);
2960 if (!entry && !cmd->pending && !server->standalone) {
2961 /* Send to the primary router */
2963 SilcUInt16 old_ident;
2965 old_ident = silc_command_get_ident(cmd->payload);
2966 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2967 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2969 silc_server_packet_send(server, server->router->connection,
2970 SILC_PACKET_COMMAND, cmd->packet->flags,
2971 tmpbuf->data, tmpbuf->len, TRUE);
2973 /* Reprocess this packet after received reply from router */
2974 silc_server_command_pending(server, SILC_COMMAND_INFO,
2975 silc_command_get_ident(cmd->payload),
2976 silc_server_command_info,
2977 silc_server_command_dup(cmd));
2978 cmd->pending = TRUE;
2979 silc_command_set_ident(cmd->payload, old_ident);
2980 silc_buffer_free(tmpbuf);
2985 silc_free(server_id);
2988 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
2989 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
2993 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
2995 server_info = entry->server_info;
2996 server_name = entry->server_name;
2998 /* Send the reply */
2999 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
3000 SILC_STATUS_OK, 0, ident, 3,
3001 2, idp->data, idp->len,
3003 strlen(server_name),
3006 strlen(server_info) : 0);
3007 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3008 packet->data, packet->len, FALSE);
3010 silc_buffer_free(packet);
3011 silc_buffer_free(idp);
3014 silc_server_command_free(cmd);
3017 /* Server side of command PING. This just replies to the ping. */
3019 SILC_SERVER_CMD_FUNC(ping)
3021 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3022 SilcServer server = cmd->server;
3027 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 1, 2);
3030 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
3032 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
3033 SILC_STATUS_ERR_NO_SERVER_ID, 0);
3036 id = silc_id_str2id(tmp, len, SILC_ID_SERVER);
3040 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
3041 /* Send our reply */
3042 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
3045 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
3046 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
3053 silc_server_command_free(cmd);
3056 /* Server side of command STATS. */
3058 SILC_SERVER_CMD_FUNC(stats)
3060 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3061 SilcServer server = cmd->server;
3062 SilcServerID *server_id;
3065 SilcBuffer packet, stats;
3066 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3069 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_STATS, cmd, 1, 1);
3072 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3074 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3075 SILC_STATUS_ERR_NO_SERVER_ID, 0);
3078 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
3082 /* The ID must be ours */
3083 if (!SILC_ID_SERVER_COMPARE(server->id, server_id)) {
3084 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3085 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
3086 silc_free(server_id);
3089 silc_free(server_id);
3091 /* If we are router then just send everything we got. If we are normal
3092 server then we'll send this to our router to get all the latest
3093 statistical information. */
3094 if (!cmd->pending && server->server_type != SILC_ROUTER &&
3095 !server->standalone) {
3096 /* Send request to our router */
3097 SilcBuffer idp = silc_id_payload_encode(server->router->id,
3099 packet = silc_command_payload_encode_va(SILC_COMMAND_STATS,
3100 ++server->cmd_ident, 1,
3101 1, idp->data, idp->len);
3102 silc_server_packet_send(server, server->router->connection,
3103 SILC_PACKET_COMMAND, 0, packet->data,
3104 packet->len, FALSE);
3106 /* Reprocess this packet after received reply from router */
3107 silc_server_command_pending(server, SILC_COMMAND_STATS,
3109 silc_server_command_stats,
3110 silc_server_command_dup(cmd));
3111 cmd->pending = TRUE;
3112 silc_buffer_free(packet);
3113 silc_buffer_free(idp);
3117 /* Send our reply to sender */
3118 uptime = time(NULL) - server->starttime;
3120 stats = silc_buffer_alloc_size(60);
3121 silc_buffer_format(stats,
3122 SILC_STR_UI_INT(server->starttime),
3123 SILC_STR_UI_INT(uptime),
3124 SILC_STR_UI_INT(server->stat.my_clients),
3125 SILC_STR_UI_INT(server->stat.my_channels),
3126 SILC_STR_UI_INT(server->stat.my_server_ops),
3127 SILC_STR_UI_INT(server->stat.my_router_ops),
3128 SILC_STR_UI_INT(server->stat.cell_clients),
3129 SILC_STR_UI_INT(server->stat.cell_channels),
3130 SILC_STR_UI_INT(server->stat.cell_servers),
3131 SILC_STR_UI_INT(server->stat.clients),
3132 SILC_STR_UI_INT(server->stat.channels),
3133 SILC_STR_UI_INT(server->stat.servers),
3134 SILC_STR_UI_INT(server->stat.routers),
3135 SILC_STR_UI_INT(server->stat.server_ops),
3136 SILC_STR_UI_INT(server->stat.router_ops),
3139 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_STATS,
3140 SILC_STATUS_OK, 0, ident, 2,
3142 3, stats->data, stats->len);
3143 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
3144 0, packet->data, packet->len, FALSE);
3145 silc_buffer_free(packet);
3146 silc_buffer_free(stats);
3149 silc_server_command_free(cmd);
3152 /* Internal routine to join channel. The channel sent to this function
3153 has been either created or resolved from ID lists. This joins the sent
3154 client to the channel. */
3156 static void silc_server_command_join_channel(SilcServer server,
3157 SilcServerCommandContext cmd,
3158 SilcChannelEntry channel,
3159 SilcClientID *client_id,
3163 const unsigned char *auth,
3164 SilcUInt32 auth_len)
3166 SilcSocketConnection sock = cmd->sock;
3168 SilcUInt32 tmp_len, user_count;
3169 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
3170 SilcClientEntry client;
3171 SilcChannelClientEntry chl;
3172 SilcBuffer reply, chidp, clidp, keyp = NULL, user_list, mode_list;
3173 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3174 char check[512], check2[512];
3175 bool founder = FALSE;
3178 SILC_LOG_DEBUG(("Start"));
3183 /* Get the client entry */
3184 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3185 client = (SilcClientEntry)sock->user_data;
3187 client = silc_server_get_client_resolve(server, client_id, FALSE,
3194 silc_server_command_send_status_reply(
3195 cmd, SILC_COMMAND_JOIN,
3196 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
3200 /* The client info is being resolved. Reprocess this packet after
3201 receiving the reply to the query. */
3202 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3204 silc_server_command_join,
3205 silc_server_command_dup(cmd));
3206 cmd->pending = TRUE;
3210 cmd->pending = FALSE;
3214 * Check founder auth payload if provided. If client can gain founder
3215 * privileges it can override various conditions on joining the channel,
3216 * and can have directly the founder mode set on the channel.
3218 if (auth && auth_len && channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3219 SilcIDListData idata = (SilcIDListData)client;
3221 if (channel->founder_key && idata->public_key &&
3222 silc_pkcs_public_key_compare(channel->founder_key,
3223 idata->public_key)) {
3224 void *auth_data = (channel->founder_method == SILC_AUTH_PASSWORD ?
3225 (void *)channel->founder_passwd :
3226 (void *)channel->founder_key);
3227 SilcUInt32 auth_data_len =
3228 (channel->founder_method == SILC_AUTH_PASSWORD ?
3229 channel->founder_passwd_len : 0);
3231 /* Check whether the client is to become founder */
3232 if (silc_auth_verify_data(auth, auth_len, channel->founder_method,
3233 auth_data, auth_data_len,
3234 idata->hash, client->id, SILC_ID_CLIENT)) {
3235 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3242 * Check channel modes
3246 memset(check, 0, sizeof(check));
3247 memset(check2, 0, sizeof(check2));
3248 strncat(check, client->nickname, strlen(client->nickname));
3249 strncat(check, "!", 1);
3250 strncat(check, client->username, strlen(client->username));
3251 if (!strchr(client->username, '@')) {
3252 strncat(check, "@", 1);
3253 strncat(check, cmd->sock->hostname, strlen(cmd->sock->hostname));
3256 strncat(check2, client->nickname, strlen(client->nickname));
3257 if (!strchr(client->nickname, '@')) {
3258 strncat(check2, "@", 1);
3259 strncat(check2, server->server_name, strlen(server->server_name));
3261 strncat(check2, "!", 1);
3262 strncat(check2, client->username, strlen(client->username));
3263 if (!strchr(client->username, '@')) {
3264 strncat(check2, "@", 1);
3265 strncat(check2, cmd->sock->hostname, strlen(cmd->sock->hostname));
3268 /* Check invite list if channel is invite-only channel */
3269 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
3270 if (!channel->invite_list ||
3271 (!silc_string_match(channel->invite_list, check) &&
3272 !silc_string_match(channel->invite_list, check2))) {
3273 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3274 SILC_STATUS_ERR_NOT_INVITED, 0);
3279 /* Check ban list if it exists. If the client's nickname, server,
3280 username and/or hostname is in the ban list the access to the
3281 channel is denied. */
3282 if (channel->ban_list) {
3283 if (silc_string_match(channel->ban_list, check) ||
3284 silc_string_match(channel->ban_list, check2)) {
3285 silc_server_command_send_status_reply(
3286 cmd, SILC_COMMAND_JOIN,
3287 SILC_STATUS_ERR_BANNED_FROM_CHANNEL, 0);
3292 /* Check user count limit if set. */
3293 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
3294 if (silc_hash_table_count(channel->user_list) + 1 >
3295 channel->user_limit) {
3296 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3297 SILC_STATUS_ERR_CHANNEL_IS_FULL,
3304 /* Check the channel passphrase if set. */
3305 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
3306 /* Get passphrase */
3307 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3309 passphrase = silc_memdup(tmp, tmp_len);
3311 if (!passphrase || !channel->passphrase ||
3312 memcmp(passphrase, channel->passphrase, strlen(channel->passphrase))) {
3313 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3314 SILC_STATUS_ERR_BAD_PASSWORD, 0);
3320 * Client is allowed to join to the channel. Make it happen.
3323 /* Check whether the client already is on the channel */
3324 if (silc_server_client_on_channel(client, channel, NULL)) {
3325 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3326 SILC_STATUS_ERR_USER_ON_CHANNEL, 0);
3330 /* Generate new channel key as protocol dictates */
3332 if (!silc_server_create_channel_key(server, channel, 0))
3335 /* Send the channel key. This is broadcasted to the channel but is not
3336 sent to the client who is joining to the channel. */
3337 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
3338 silc_server_send_channel_key(server, NULL, channel,
3339 server->server_type == SILC_ROUTER ?
3340 FALSE : !server->standalone);
3343 /* Join the client to the channel by adding it to channel's user list.
3344 Add also the channel to client entry's channels list for fast cross-
3346 chl = silc_calloc(1, sizeof(*chl));
3348 chl->client = client;
3349 chl->channel = channel;
3350 silc_hash_table_add(channel->user_list, client, chl);
3351 silc_hash_table_add(client->channels, channel, chl);
3352 channel->user_count++;
3354 /* Get users on the channel */
3355 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
3358 /* Encode Client ID Payload of the original client who wants to join */
3359 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3361 /* Encode command reply packet */
3362 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3363 SILC_PUT32_MSB(channel->mode, mode);
3364 SILC_PUT32_MSB(created, tmp2);
3365 SILC_PUT32_MSB(user_count, tmp3);
3367 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3368 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3369 keyp = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
3372 strlen(channel->channel_key->
3374 channel->channel_key->cipher->name,
3375 channel->key_len / 8, channel->key);
3380 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
3381 SILC_STATUS_OK, 0, ident, 13,
3382 2, channel->channel_name,
3383 strlen(channel->channel_name),
3384 3, chidp->data, chidp->len,
3385 4, clidp->data, clidp->len,
3388 7, keyp ? keyp->data : NULL,
3389 keyp ? keyp->len : 0,
3390 8, channel->ban_list,
3392 strlen(channel->ban_list) : 0,
3393 9, channel->invite_list,
3394 channel->invite_list ?
3395 strlen(channel->invite_list) : 0,
3398 strlen(channel->topic) : 0,
3399 11, silc_hmac_get_name(channel->hmac),
3400 strlen(silc_hmac_get_name(channel->
3403 13, user_list->data, user_list->len,
3404 14, mode_list->data,
3407 /* Send command reply */
3408 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
3409 reply->data, reply->len, FALSE);
3411 /* Send JOIN notify to locally connected clients on the channel. If
3412 we are normal server then router will send or have sent JOIN notify
3413 already. However since we've added the client already to our channel
3414 we'll ignore it (in packet_receive.c) so we must send it here. If
3415 we are router then this will send it to local clients and local
3417 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3418 SILC_NOTIFY_TYPE_JOIN, 2,
3419 clidp->data, clidp->len,
3420 chidp->data, chidp->len);
3422 if (!cmd->pending) {
3423 /* Send JOIN notify packet to our primary router */
3424 if (!server->standalone)
3425 silc_server_send_notify_join(server, server->router->connection,
3426 server->server_type == SILC_ROUTER ?
3427 TRUE : FALSE, channel, client->id);
3430 /* Distribute the channel key to all backup routers. */
3431 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
3432 keyp->data, keyp->len, FALSE, TRUE);
3435 /* If client became founder by providing correct founder auth data
3436 notify the mode change to the channel. */
3438 SILC_PUT32_MSB(chl->mode, mode);
3439 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3440 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
3441 clidp->data, clidp->len,
3442 mode, 4, clidp->data, clidp->len);
3444 /* Set CUMODE notify type to network */
3445 if (!server->standalone)
3446 silc_server_send_notify_cumode(server, server->router->connection,
3447 server->server_type == SILC_ROUTER ?
3448 TRUE : FALSE, channel,
3449 chl->mode, client->id, SILC_ID_CLIENT,
3453 silc_buffer_free(reply);
3454 silc_buffer_free(clidp);
3455 silc_buffer_free(chidp);
3456 silc_buffer_free(keyp);
3457 silc_buffer_free(user_list);
3458 silc_buffer_free(mode_list);
3461 silc_free(passphrase);
3464 /* Server side of command JOIN. Joins client into requested channel. If
3465 the channel does not exist it will be created. */
3467 SILC_SERVER_CMD_FUNC(join)
3469 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3470 SilcServer server = cmd->server;
3471 unsigned char *auth;
3472 SilcUInt32 tmp_len, auth_len;
3473 char *tmp, *channel_name = NULL, *cipher, *hmac;
3474 SilcChannelEntry channel;
3475 SilcUInt32 umode = 0;
3476 bool created = FALSE, create_key = TRUE;
3477 SilcClientID *client_id;
3479 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 2, 6);
3481 /* Get channel name */
3482 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3484 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3485 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3492 channel_name[255] = '\0';
3494 if (silc_server_name_bad_chars(channel_name, tmp_len) == TRUE) {
3495 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3496 SILC_STATUS_ERR_BAD_CHANNEL, 0);
3500 /* Get Client ID of the client who is joining to the channel */
3501 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3503 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3504 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3508 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
3510 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
3511 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3516 /* Get cipher, hmac name and auth payload */
3517 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
3518 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
3519 auth = silc_argument_get_arg_type(cmd->args, 6, &auth_len);
3521 /* See if the channel exists */
3522 channel = silc_idlist_find_channel_by_name(server->local_list,
3523 channel_name, NULL);
3525 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
3526 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
3527 client_id = silc_id_dup(entry->id, SILC_ID_CLIENT);
3529 if (!channel || channel->disabled) {
3530 /* Channel not found */
3532 /* If we are standalone server we don't have a router, we just create
3533 the channel by ourselves. */
3534 if (server->standalone) {
3535 channel = silc_server_create_new_channel(server, server->id, cipher,
3536 hmac, channel_name, TRUE);
3538 silc_server_command_send_status_reply(
3539 cmd, SILC_COMMAND_JOIN,
3540 SILC_STATUS_ERR_UNKNOWN_ALGORITHM,
3545 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3551 /* The channel does not exist on our server. If we are normal server
3552 we will send JOIN command to our router which will handle the
3553 joining procedure (either creates the channel if it doesn't exist
3554 or joins the client to it). */
3555 if (server->server_type != SILC_ROUTER) {
3557 SilcUInt16 old_ident;
3559 /* If this is pending command callback then we've resolved
3560 it and it didn't work, return since we've notified the
3561 client already in the command reply callback. */
3565 old_ident = silc_command_get_ident(cmd->payload);
3566 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
3567 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3569 /* Send JOIN command to our router */
3570 silc_server_packet_send(server, (SilcSocketConnection)
3571 server->router->connection,
3572 SILC_PACKET_COMMAND, cmd->packet->flags,
3573 tmpbuf->data, tmpbuf->len, TRUE);
3575 /* Reprocess this packet after received reply from router */
3576 silc_server_command_pending(server, SILC_COMMAND_JOIN,
3577 silc_command_get_ident(cmd->payload),
3578 silc_server_command_join,
3579 silc_server_command_dup(cmd));
3580 cmd->pending = TRUE;
3581 silc_command_set_ident(cmd->payload, old_ident);
3582 silc_buffer_free(tmpbuf);
3586 /* We are router and the channel does not seem exist so we will check
3587 our global list as well for the channel. */
3588 channel = silc_idlist_find_channel_by_name(server->global_list,
3589 channel_name, NULL);
3591 /* Channel really does not exist, create it */
3592 channel = silc_server_create_new_channel(server, server->id, cipher,
3593 hmac, channel_name, TRUE);
3595 silc_server_command_send_status_reply(
3596 cmd, SILC_COMMAND_JOIN,
3597 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
3601 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3609 /* Channel not found */
3611 /* If the command came from router and we are normal server then
3612 something went wrong with the joining as the channel was not found.
3613 We can't do anything else but ignore this. */
3614 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
3615 server->server_type != SILC_ROUTER)
3618 /* We are router and the channel does not seem exist so we will check
3619 our global list as well for the channel. */
3620 channel = silc_idlist_find_channel_by_name(server->global_list,
3621 channel_name, NULL);
3623 /* Channel really does not exist, create it */
3624 channel = silc_server_create_new_channel(server, server->id, cipher,
3625 hmac, channel_name, TRUE);
3627 silc_server_command_send_status_reply(
3628 cmd, SILC_COMMAND_JOIN,
3629 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
3633 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3640 /* Check whether the channel was created by our router */
3641 if (cmd->pending && context2) {
3642 SilcServerCommandReplyContext reply = context2;
3644 if (silc_command_get(reply->payload) == SILC_COMMAND_JOIN) {
3645 tmp = silc_argument_get_arg_type(reply->args, 6, NULL);
3646 SILC_GET32_MSB(created, tmp);
3647 if (silc_argument_get_arg_type(reply->args, 7, NULL))
3648 create_key = FALSE; /* Router returned the key already */
3651 if (silc_command_get(reply->payload) == SILC_COMMAND_WHOIS &&
3652 !silc_hash_table_count(channel->user_list))
3656 /* If the channel does not have global users and is also empty the client
3657 will be the channel founder and operator. */
3658 if (!channel->global_users && !silc_hash_table_count(channel->user_list))
3659 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
3661 /* Join to the channel */
3662 silc_server_command_join_channel(server, cmd, channel, client_id,
3663 created, create_key, umode,
3666 silc_free(client_id);
3669 silc_server_command_free(cmd);
3672 /* Server side of command MOTD. Sends server's current "message of the
3673 day" to the client. */
3675 SILC_SERVER_CMD_FUNC(motd)
3677 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3678 SilcServer server = cmd->server;
3679 SilcBuffer packet, idp;
3680 char *motd, *dest_server;
3681 SilcUInt32 motd_len;
3682 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3684 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
3686 /* Get server name */
3687 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
3689 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
3690 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
3694 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
3697 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3699 if (server->config && server->config->server_info &&
3700 server->config->server_info->motd_file) {
3702 motd = silc_file_readfile(server->config->server_info->motd_file, &motd_len);
3707 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3714 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3720 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3721 packet->data, packet->len, FALSE);
3722 silc_buffer_free(packet);
3723 silc_buffer_free(idp);
3725 SilcServerEntry entry;
3727 /* Check whether we have this server cached */
3728 entry = silc_idlist_find_server_by_name(server->global_list,
3729 dest_server, TRUE, NULL);
3731 entry = silc_idlist_find_server_by_name(server->local_list,
3732 dest_server, TRUE, NULL);
3735 if (server->server_type != SILC_SERVER && !cmd->pending &&
3736 entry && !entry->motd) {
3737 /* Send to the server */
3739 SilcUInt16 old_ident;
3741 old_ident = silc_command_get_ident(cmd->payload);
3742 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
3743 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3745 silc_server_packet_send(server, entry->connection,
3746 SILC_PACKET_COMMAND, cmd->packet->flags,
3747 tmpbuf->data, tmpbuf->len, TRUE);
3749 /* Reprocess this packet after received reply from router */
3750 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3751 silc_command_get_ident(cmd->payload),
3752 silc_server_command_motd,
3753 silc_server_command_dup(cmd));
3754 cmd->pending = TRUE;
3755 silc_command_set_ident(cmd->payload, old_ident);
3756 silc_buffer_free(tmpbuf);
3760 if (!entry && !cmd->pending && !server->standalone) {
3761 /* Send to the primary router */
3763 SilcUInt16 old_ident;
3765 old_ident = silc_command_get_ident(cmd->payload);
3766 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
3767 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3769 silc_server_packet_send(server, server->router->connection,
3770 SILC_PACKET_COMMAND, cmd->packet->flags,
3771 tmpbuf->data, tmpbuf->len, TRUE);
3773 /* Reprocess this packet after received reply from router */
3774 silc_server_command_pending(server, SILC_COMMAND_MOTD,
3775 silc_command_get_ident(cmd->payload),
3776 silc_server_command_motd,
3777 silc_server_command_dup(cmd));
3778 cmd->pending = TRUE;
3779 silc_command_set_ident(cmd->payload, old_ident);
3780 silc_buffer_free(tmpbuf);
3785 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
3786 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
3790 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
3791 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
3792 SILC_STATUS_OK, 0, ident, 2,
3796 strlen(entry->motd) : 0);
3797 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3798 packet->data, packet->len, FALSE);
3799 silc_buffer_free(packet);
3800 silc_buffer_free(idp);
3804 silc_server_command_free(cmd);
3807 /* Server side of command UMODE. Client can use this command to set/unset
3808 user mode. Client actually cannot set itself to be as server/router
3809 operator so this can be used only to unset the modes. */
3811 SILC_SERVER_CMD_FUNC(umode)
3813 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3814 SilcServer server = cmd->server;
3815 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3817 unsigned char *tmp_mask, m[4];
3818 SilcUInt32 mask = 0;
3819 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3820 bool set_mask = FALSE;
3822 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
3825 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 1, 2);
3827 /* Get the client's mode mask */
3828 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3830 SILC_GET32_MSB(mask, tmp_mask);
3835 /* Check that mode changing is allowed. */
3836 if (!silc_server_check_umode_rights(server, client, mask)) {
3837 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3838 SILC_STATUS_ERR_PERM_DENIED, 0);
3842 /* Anonymous mode cannot be set by client */
3843 if (mask & SILC_UMODE_ANONYMOUS) {
3844 if (!(client->mode & SILC_UMODE_ANONYMOUS)) {
3845 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3846 SILC_STATUS_ERR_PERM_DENIED, 0);
3850 if (client->mode & SILC_UMODE_ANONYMOUS) {
3851 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
3852 SILC_STATUS_ERR_PERM_DENIED, 0);
3857 /* Change the mode */
3858 client->mode = mask;
3860 /* Send UMODE change to primary router */
3861 if (!server->standalone)
3862 silc_server_send_notify_umode(server, server->router->connection, TRUE,
3863 client->id, client->mode);
3865 /* Check if anyone is watching this nickname */
3866 if (server->server_type == SILC_ROUTER)
3867 silc_server_check_watcher_list(server, client, NULL,
3868 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3871 /* Send command reply to sender */
3872 SILC_PUT32_MSB(client->mode, m);
3873 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
3874 SILC_STATUS_OK, 0, ident, 1,
3876 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3877 packet->data, packet->len, FALSE);
3878 silc_buffer_free(packet);
3881 silc_server_command_free(cmd);
3884 /* Server side command of CMODE. Changes channel mode */
3886 SILC_SERVER_CMD_FUNC(cmode)
3888 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3889 SilcServer server = cmd->server;
3890 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3891 SilcIDListData idata = (SilcIDListData)client;
3892 SilcChannelID *channel_id = NULL;
3893 SilcChannelEntry channel;
3894 SilcChannelClientEntry chl;
3895 SilcBuffer packet, cidp;
3896 unsigned char *tmp, *tmp_id, *tmp_mask;
3897 char *cipher = NULL, *hmac = NULL, *passphrase = NULL;
3898 SilcUInt32 mode_mask = 0, tmp_len, tmp_len2;
3899 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3900 bool set_mask = FALSE;
3902 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 1, 7);
3904 /* Get Channel ID */
3905 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
3907 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3908 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3911 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2, NULL);
3913 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3914 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3918 /* Get the channel mode mask */
3919 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3921 SILC_GET32_MSB(mode_mask, tmp_mask);
3925 /* Get channel entry */
3926 channel = silc_idlist_find_channel_by_id(server->local_list,
3929 channel = silc_idlist_find_channel_by_id(server->global_list,
3932 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3933 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
3939 /* Check whether this client is on the channel */
3940 if (!silc_server_client_on_channel(client, channel, &chl)) {
3941 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3942 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
3946 /* Check that client has rights to change any requested channel modes */
3947 if (set_mask && !silc_server_check_cmode_rights(server, channel, chl,
3949 silc_server_command_send_status_reply(
3950 cmd, SILC_COMMAND_CMODE,
3951 (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) ?
3952 SILC_STATUS_ERR_NO_CHANNEL_PRIV :
3953 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV), 0);
3957 /* If mode mask was not sent as argument then merely return the current
3958 mode mask to the sender. */
3961 SILC_PUT32_MSB(channel->mode, m);
3962 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3963 SILC_STATUS_OK, 0, ident, 2,
3964 2, tmp_id, tmp_len2,
3966 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3967 packet->data, packet->len, FALSE);
3968 silc_buffer_free(packet);
3973 * Check the modes. Modes that requires nothing special operation are
3977 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
3978 /* Channel uses private keys to protect traffic. Client(s) has set the
3979 key locally they want to use, server does not know that key. */
3980 /* Nothing interesting to do here */
3982 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3983 /* The mode is removed and we need to generate and distribute
3984 new channel key. Clients are not using private channel keys
3985 anymore after this. */
3987 /* Re-generate channel key */
3988 if (!silc_server_create_channel_key(server, channel, 0))
3991 /* Send the channel key. This sends it to our local clients and if
3992 we are normal server to our router as well. */
3993 silc_server_send_channel_key(server, NULL, channel,
3994 server->server_type == SILC_ROUTER ?
3995 FALSE : !server->standalone);
3997 cipher = channel->channel_key->cipher->name;
3998 hmac = (char *)silc_hmac_get_name(channel->hmac);
4002 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
4003 /* User limit is set on channel */
4004 SilcUInt32 user_limit;
4006 /* Get user limit */
4007 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
4009 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
4010 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4011 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
4015 SILC_GET32_MSB(user_limit, tmp);
4016 channel->user_limit = user_limit;
4019 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
4020 /* User limit mode is unset. Remove user limit */
4021 channel->user_limit = 0;
4024 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
4025 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
4026 /* Passphrase has been set to channel */
4028 /* Get the passphrase */
4029 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
4031 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4032 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
4036 /* Save the passphrase */
4037 passphrase = channel->passphrase = silc_memdup(tmp, strlen(tmp));
4040 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
4041 /* Passphrase mode is unset. remove the passphrase */
4042 silc_free(channel->passphrase);
4043 channel->passphrase = NULL;
4047 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
4048 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
4049 /* Cipher to use protect the traffic */
4050 SilcCipher newkey, oldkey;
4053 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
4055 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4056 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
4060 /* Delete old cipher and allocate the new one */
4061 if (!silc_cipher_alloc(cipher, &newkey)) {
4062 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4063 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
4067 oldkey = channel->channel_key;
4068 channel->channel_key = newkey;
4070 /* Re-generate channel key */
4071 if (!silc_server_create_channel_key(server, channel, 0)) {
4072 /* We don't have new key, revert to old one */
4073 channel->channel_key = oldkey;
4077 /* Remove old channel key for good */
4078 silc_cipher_free(oldkey);
4080 /* Send the channel key. This sends it to our local clients and if
4081 we are normal server to our router as well. */
4082 silc_server_send_channel_key(server, NULL, channel,
4083 server->server_type == SILC_ROUTER ?
4084 FALSE : !server->standalone);
4087 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
4088 /* Cipher mode is unset. Remove the cipher and revert back to
4090 SilcCipher newkey, oldkey;
4091 cipher = channel->cipher;
4093 /* Delete old cipher and allocate default one */
4094 if (!silc_cipher_alloc(cipher ? cipher : SILC_DEFAULT_CIPHER, &newkey)) {
4095 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4096 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
4100 oldkey = channel->channel_key;
4101 channel->channel_key = newkey;
4103 /* Re-generate channel key */
4104 if (!silc_server_create_channel_key(server, channel, 0)) {
4105 /* We don't have new key, revert to old one */
4106 channel->channel_key = oldkey;
4110 /* Remove old channel key for good */
4111 silc_cipher_free(oldkey);
4113 /* Send the channel key. This sends it to our local clients and if
4114 we are normal server to our router as well. */
4115 silc_server_send_channel_key(server, NULL, channel,
4116 server->server_type == SILC_ROUTER ?
4117 FALSE : !server->standalone);
4121 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
4122 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
4123 /* HMAC to use protect the traffic */
4124 unsigned char hash[32];
4128 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
4130 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4131 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
4135 /* Delete old hmac and allocate the new one */
4136 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
4137 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4138 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
4142 silc_hmac_free(channel->hmac);
4143 channel->hmac = newhmac;
4145 /* Set the HMAC key out of current channel key. The client must do
4147 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
4148 channel->key_len / 8, hash);
4149 silc_hmac_set_key(channel->hmac, hash,
4150 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
4151 memset(hash, 0, sizeof(hash));
4154 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
4155 /* Hmac mode is unset. Remove the hmac and revert back to
4158 unsigned char hash[32];
4159 hmac = channel->hmac_name;
4161 /* Delete old hmac and allocate default one */
4162 silc_hmac_free(channel->hmac);
4163 if (!silc_hmac_alloc(hmac ? hmac : SILC_DEFAULT_HMAC, NULL, &newhmac)) {
4164 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4165 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
4169 silc_hmac_free(channel->hmac);
4170 channel->hmac = newhmac;
4172 /* Set the HMAC key out of current channel key. The client must do
4174 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
4175 channel->key_len / 8,
4177 silc_hmac_set_key(channel->hmac, hash,
4178 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
4179 memset(hash, 0, sizeof(hash));
4183 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
4184 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4185 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
4186 /* Set the founder authentication */
4187 SilcAuthPayload auth;
4189 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
4191 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4192 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
4196 auth = silc_auth_payload_parse(tmp, tmp_len);
4198 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4199 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
4203 /* Save the public key */
4204 tmp = silc_pkcs_public_key_encode(idata->public_key, &tmp_len);
4205 silc_pkcs_public_key_decode(tmp, tmp_len, &channel->founder_key);
4208 channel->founder_method = silc_auth_get_method(auth);
4210 if (channel->founder_method == SILC_AUTH_PASSWORD) {
4211 tmp = silc_auth_get_data(auth, &tmp_len);
4212 channel->founder_passwd = silc_memdup(tmp, tmp_len);
4213 channel->founder_passwd_len = tmp_len;
4215 /* Verify the payload before setting the mode */
4216 if (!silc_auth_verify(auth, channel->founder_method,
4217 channel->founder_key, 0, idata->hash,
4218 client->id, SILC_ID_CLIENT)) {
4219 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
4220 SILC_STATUS_ERR_AUTH_FAILED,
4226 silc_auth_payload_free(auth);
4230 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4231 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
4232 if (channel->founder_key)
4233 silc_pkcs_public_key_free(channel->founder_key);
4234 if (channel->founder_passwd) {
4235 silc_free(channel->founder_passwd);
4236 channel->founder_passwd = NULL;
4242 /* Finally, set the mode */
4243 channel->mode = mode_mask;
4245 /* Send CMODE_CHANGE notify. */
4246 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
4247 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4248 SILC_NOTIFY_TYPE_CMODE_CHANGE, 5,
4249 cidp->data, cidp->len,
4251 cipher, cipher ? strlen(cipher) : 0,
4252 hmac, hmac ? strlen(hmac) : 0,
4253 passphrase, passphrase ?
4254 strlen(passphrase) : 0);
4256 /* Set CMODE notify type to network */
4257 if (!server->standalone)
4258 silc_server_send_notify_cmode(server, server->router->connection,
4259 server->server_type == SILC_ROUTER ?
4260 TRUE : FALSE, channel,
4261 mode_mask, client->id, SILC_ID_CLIENT,
4262 cipher, hmac, passphrase);
4264 /* Send command reply to sender */
4265 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
4266 SILC_STATUS_OK, 0, ident, 2,
4267 2, tmp_id, tmp_len2,
4269 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4270 packet->data, packet->len, FALSE);
4272 silc_buffer_free(packet);
4273 silc_buffer_free(cidp);
4276 silc_free(channel_id);
4277 silc_server_command_free(cmd);
4280 /* Server side of CUMODE command. Changes client's mode on a channel. */
4282 SILC_SERVER_CMD_FUNC(cumode)
4284 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4285 SilcServer server = cmd->server;
4286 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4287 SilcIDListData idata = (SilcIDListData)client;
4288 SilcChannelID *channel_id;
4289 SilcClientID *client_id;
4290 SilcChannelEntry channel;
4291 SilcClientEntry target_client;
4292 SilcChannelClientEntry chl;
4293 SilcBuffer packet, idp;
4294 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
4295 SilcUInt32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
4297 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4299 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
4301 /* Get Channel ID */
4302 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
4304 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4305 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4308 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len, NULL);
4310 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4311 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4315 /* Get channel entry */
4316 channel = silc_idlist_find_channel_by_id(server->local_list,
4319 channel = silc_idlist_find_channel_by_id(server->global_list,
4322 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4323 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4329 /* Check whether sender is on the channel */
4330 if (!silc_server_client_on_channel(client, channel, &chl)) {
4331 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4332 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
4335 sender_mask = chl->mode;
4337 /* Get the target client's channel mode mask */
4338 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
4340 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4341 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4345 SILC_GET32_MSB(target_mask, tmp_mask);
4347 /* Get target Client ID */
4348 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4350 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4351 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
4354 client_id = silc_id_payload_parse_id(tmp_id, tmp_len, NULL);
4356 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4357 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
4361 /* Get target client's entry */
4362 target_client = silc_idlist_find_client_by_id(server->local_list,
4363 client_id, TRUE, NULL);
4364 if (!target_client) {
4365 target_client = silc_idlist_find_client_by_id(server->global_list,
4366 client_id, TRUE, NULL);
4369 if (target_client != client &&
4370 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
4371 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
4372 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4373 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
4377 /* Check whether target client is on the channel */
4378 if (target_client != client) {
4379 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
4380 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4381 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL, 0);
4390 /* If the target client is founder, no one else can change their mode
4392 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && client != target_client) {
4393 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4394 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
4399 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
4400 if (target_client != client) {
4401 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4402 SILC_STATUS_ERR_NOT_YOU, 0);
4406 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
4407 /* The client tries to claim the founder rights. */
4408 unsigned char *tmp_auth;
4409 SilcUInt32 tmp_auth_len, auth_len;
4412 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
4413 !channel->founder_key || !idata->public_key ||
4414 !silc_pkcs_public_key_compare(channel->founder_key,
4415 idata->public_key)) {
4416 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4417 SILC_STATUS_ERR_NOT_YOU, 0);
4421 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
4423 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4424 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
4428 auth = (channel->founder_method == SILC_AUTH_PASSWORD ?
4429 (void *)channel->founder_passwd : (void *)channel->founder_key);
4430 auth_len = (channel->founder_method == SILC_AUTH_PASSWORD ?
4431 channel->founder_passwd_len : 0);
4433 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len,
4434 channel->founder_method, auth, auth_len,
4435 idata->hash, client->id, SILC_ID_CLIENT)) {
4436 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4437 SILC_STATUS_ERR_AUTH_FAILED, 0);
4441 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
4445 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4446 if (target_client == client) {
4447 /* Remove channel founder rights from itself */
4448 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
4451 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4452 SILC_STATUS_ERR_NOT_YOU, 0);
4458 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
4459 /* Promote to operator */
4460 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4461 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4462 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4463 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4464 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
4469 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
4473 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
4474 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
4475 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
4476 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4477 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
4482 /* Demote to normal user */
4483 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
4488 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
4489 if (target_client != client) {
4490 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4491 SILC_STATUS_ERR_NOT_YOU, 0);
4495 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)) {
4496 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
4500 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
4501 if (target_client != client) {
4502 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4503 SILC_STATUS_ERR_NOT_YOU, 0);
4507 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
4512 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
4513 if (target_client != client) {
4514 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4515 SILC_STATUS_ERR_NOT_YOU, 0);
4519 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS)) {
4520 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
4524 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
4525 if (target_client != client) {
4526 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4527 SILC_STATUS_ERR_NOT_YOU, 0);
4531 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
4536 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
4537 if (target_client != client) {
4538 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4539 SILC_STATUS_ERR_NOT_YOU, 0);
4543 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS)) {
4544 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
4548 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
4549 if (target_client != client) {
4550 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
4551 SILC_STATUS_ERR_NOT_YOU, 0);
4555 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
4560 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
4561 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4563 /* Send notify to channel, notify only if mode was actually changed. */
4565 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4566 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
4567 idp->data, idp->len,
4571 /* Set CUMODE notify type to network */
4572 if (!server->standalone)
4573 silc_server_send_notify_cumode(server, server->router->connection,
4574 server->server_type == SILC_ROUTER ?
4575 TRUE : FALSE, channel,
4576 target_mask, client->id,
4581 /* Send command reply to sender */
4582 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
4583 SILC_STATUS_OK, 0, ident, 3,
4585 3, tmp_ch_id, tmp_ch_len,
4586 4, tmp_id, tmp_len);
4587 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4588 packet->data, packet->len, FALSE);
4590 silc_buffer_free(packet);
4591 silc_free(channel_id);
4592 silc_free(client_id);
4593 silc_buffer_free(idp);
4596 silc_server_command_free(cmd);
4599 /* Server side of KICK command. Kicks client out of channel. */
4601 SILC_SERVER_CMD_FUNC(kick)
4603 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4604 SilcServer server = cmd->server;
4605 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4606 SilcClientEntry target_client;
4607 SilcChannelID *channel_id;
4608 SilcClientID *client_id;
4609 SilcChannelEntry channel;
4610 SilcChannelClientEntry chl;
4612 SilcUInt32 tmp_len, target_idp_len;
4613 unsigned char *tmp, *comment, *target_idp;
4615 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
4617 /* Get Channel ID */
4618 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4620 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4621 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4624 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
4626 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4627 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4631 /* Get channel entry */
4632 channel = silc_idlist_find_channel_by_id(server->local_list,
4635 channel = silc_idlist_find_channel_by_id(server->local_list,
4638 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4639 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4645 /* Check whether sender is on the channel */
4646 if (!silc_server_client_on_channel(client, channel, &chl)) {
4647 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4648 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
4652 /* Check that the kicker is channel operator or channel founder */
4653 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
4654 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
4655 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4656 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
4660 /* Get target Client ID */
4661 target_idp = silc_argument_get_arg_type(cmd->args, 2, &target_idp_len);
4663 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4664 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
4667 client_id = silc_id_payload_parse_id(target_idp, target_idp_len, NULL);
4669 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4670 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
4674 /* Get target client's entry */
4675 target_client = silc_idlist_find_client_by_id(server->local_list,
4676 client_id, TRUE, NULL);
4677 if (!target_client) {
4678 target_client = silc_idlist_find_client_by_id(server->global_list,
4679 client_id, TRUE, NULL);
4682 /* Check whether target client is on the channel */
4683 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
4684 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4685 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL,
4690 /* Check that the target client is not channel founder. Channel founder
4691 cannot be kicked from the channel. */
4692 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
4693 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4694 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
4701 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
4705 /* Send command reply to sender */
4706 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
4709 /* Send KICKED notify to local clients on the channel */
4710 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
4711 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
4712 SILC_NOTIFY_TYPE_KICKED, 3,
4713 target_idp, target_idp_len,
4714 comment, comment ? strlen(comment) : 0,
4715 idp->data, idp->len);
4716 silc_buffer_free(idp);
4718 /* Remove the client from the channel. If the channel does not exist
4719 after removing the client then the client kicked itself off the channel
4720 and we don't have to send anything after that. */
4721 if (!silc_server_remove_from_one_channel(server, NULL, channel,
4722 target_client, FALSE))
4725 /* Send KICKED notify to primary route */
4726 if (!server->standalone)
4727 silc_server_send_notify_kicked(server, server->router->connection,
4728 server->server_type == SILC_ROUTER ?
4729 TRUE : FALSE, channel,
4730 target_client->id, client->id, comment);
4732 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4733 /* Re-generate channel key */
4734 if (!silc_server_create_channel_key(server, channel, 0))
4737 /* Send the channel key to the channel. The key of course is not sent
4738 to the client who was kicked off the channel. */
4739 silc_server_send_channel_key(server, target_client->connection, channel,
4740 server->server_type == SILC_ROUTER ?
4741 FALSE : !server->standalone);
4745 silc_server_command_free(cmd);
4748 /* Server side of OPER command. Client uses this comand to obtain server
4749 operator privileges to this server/router. */
4751 SILC_SERVER_CMD_FUNC(oper)
4753 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4754 SilcServer server = cmd->server;
4755 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4756 unsigned char *username, *auth;
4758 SilcServerConfigAdmin *admin;
4759 SilcIDListData idata = (SilcIDListData)client;
4760 bool result = FALSE;
4761 SilcPublicKey cached_key;
4763 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
4765 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4768 /* Get the username */
4769 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4771 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4772 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4777 /* Get the admin configuration */
4778 admin = silc_server_config_find_admin(server, cmd->sock->ip,
4779 username, client->nickname);
4781 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
4782 username, client->nickname);
4784 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4785 SILC_STATUS_ERR_AUTH_FAILED,
4791 /* Get the authentication payload */
4792 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4794 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4795 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4800 /* Verify the authentication data. If both passphrase and public key
4801 is set then try both of them. */
4802 if (admin->passphrase)
4803 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
4804 admin->passphrase, admin->passphrase_len,
4805 idata->hash, client->id, SILC_ID_CLIENT);
4806 if (!result && admin->publickeys) {
4807 cached_key = silc_server_get_public_key(server, admin->publickeys);
4810 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
4811 cached_key, 0, idata->hash,
4812 client->id, SILC_ID_CLIENT);
4815 /* Authentication failed */
4816 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4817 SILC_STATUS_ERR_AUTH_FAILED,
4822 /* Client is now server operator */
4823 client->mode |= SILC_UMODE_SERVER_OPERATOR;
4825 /* Update statistics */
4826 if (client->connection)
4827 server->stat.my_server_ops++;
4828 if (server->server_type == SILC_ROUTER)
4829 server->stat.server_ops++;
4831 /* Send UMODE change to primary router */
4832 if (!server->standalone)
4833 silc_server_send_notify_umode(server, server->router->connection, TRUE,
4834 client->id, client->mode);
4836 /* Check if anyone is watching this nickname */
4837 if (server->server_type == SILC_ROUTER)
4838 silc_server_check_watcher_list(server, client, NULL,
4839 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4841 /* Send reply to the sender */
4842 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4846 silc_server_command_free(cmd);
4849 SILC_TASK_CALLBACK(silc_server_command_detach_cb)
4851 QuitInternal q = (QuitInternal)context;
4852 SilcClientEntry client = (SilcClientEntry)q->sock->user_data;
4854 /* If there is pending outgoing data for the client then purge it
4855 to the network before closing connection. */
4856 silc_server_packet_queue_purge(q->server, q->sock);
4858 /* Close the connection on our side */
4859 client->router = NULL;
4860 client->connection = NULL;
4861 q->sock->user_data = NULL;
4862 silc_server_close_connection(q->server, q->sock);
4867 SILC_TASK_CALLBACK(silc_server_command_detach_timeout)
4869 QuitInternal q = (QuitInternal)context;
4870 SilcClientEntry client = (SilcClientEntry)q->sock;
4872 SILC_LOG_DEBUG(("Start"));
4874 if (client->mode & SILC_UMODE_DETACHED)
4875 silc_server_free_client_data(q->server, NULL, client, TRUE,
4880 /* Server side of DETACH command. Detached the client from the network
4881 by closing the connection but preserving the session. */
4883 SILC_SERVER_CMD_FUNC(detach)
4885 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4886 SilcServer server = cmd->server;
4887 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4890 if (server->config->detach_disabled) {
4891 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
4892 SILC_STATUS_ERR_UNKNOWN_COMMAND,
4897 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
4900 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_DETACH, cmd, 0, 0);
4902 /* Send the user mode notify to notify that client is detached */
4903 client->mode |= SILC_UMODE_DETACHED;
4904 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
4905 client->last_command = 0;
4906 client->fast_command = 0;
4907 if (!server->standalone)
4908 silc_server_send_notify_umode(server, server->router->connection,
4909 server->server_type == SILC_SERVER ?
4910 FALSE : TRUE, client->id, client->mode);
4912 /* Check if anyone is watching this nickname */
4913 if (server->server_type == SILC_ROUTER)
4914 silc_server_check_watcher_list(server, client, NULL,
4915 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4917 q = silc_calloc(1, sizeof(*q));
4919 q->sock = cmd->sock;
4920 silc_schedule_task_add(server->schedule, 0, silc_server_command_detach_cb,
4921 q, 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
4923 if (server->config->detach_timeout) {
4924 q = silc_calloc(1, sizeof(*q));
4926 q->sock = (void *)client;
4927 silc_schedule_task_add(server->schedule, 0,
4928 silc_server_command_detach_timeout,
4929 q, server->config->detach_timeout * 60,
4930 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
4933 /* Send reply to the sender */
4934 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
4938 silc_server_command_free(cmd);
4941 /* Server side of WATCH command. */
4943 SILC_SERVER_CMD_FUNC(watch)
4945 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4946 SilcServer server = cmd->server;
4947 char *add_nick, *del_nick;
4948 SilcUInt32 add_nick_len, del_nick_len, tmp_len;
4950 unsigned char hash[16], *tmp;
4951 SilcClientEntry client;
4952 SilcClientID *client_id = NULL;
4954 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WATCH, cmd, 1, 3);
4956 if (server->server_type == SILC_SERVER && !server->standalone) {
4957 if (!cmd->pending) {
4958 /* Send the command to router */
4960 SilcUInt16 old_ident;
4962 old_ident = silc_command_get_ident(cmd->payload);
4963 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4964 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4966 silc_server_packet_send(server, server->router->connection,
4967 SILC_PACKET_COMMAND, cmd->packet->flags,
4968 tmpbuf->data, tmpbuf->len, TRUE);
4970 /* Reprocess this packet after received reply from router */
4971 silc_server_command_pending(server, SILC_COMMAND_WATCH,
4972 silc_command_get_ident(cmd->payload),
4973 silc_server_command_watch,
4974 silc_server_command_dup(cmd));
4975 cmd->pending = TRUE;
4976 silc_command_set_ident(cmd->payload, old_ident);
4977 silc_buffer_free(tmpbuf);
4978 } else if (context2) {
4979 /* Received reply from router, just send same data to the client. */
4980 SilcServerCommandReplyContext reply = context2;
4982 silc_command_get_status(reply->payload, &status, NULL);
4983 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH, status,
4990 /* We are router and keep the watch list for local cell */
4992 /* Get the client ID */
4993 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4995 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
4996 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5000 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
5002 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
5003 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
5008 /* Get the client entry which must be in local list */
5009 client = silc_idlist_find_client_by_id(server->local_list,
5010 client_id, TRUE, NULL);
5012 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
5013 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
5019 add_nick = silc_argument_get_arg_type(cmd->args, 2, &add_nick_len);
5020 del_nick = silc_argument_get_arg_type(cmd->args, 3, &del_nick_len);
5021 if (!add_nick && !del_nick) {
5022 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
5023 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5028 if (add_nick && add_nick_len > 128)
5029 add_nick[128] = '\0';
5030 if (del_nick && del_nick_len > 128)
5031 del_nick[128] = '\0';
5033 memset(nick, 0, sizeof(nick));
5035 /* Add new nickname to be watched in our cell */
5037 if (silc_server_name_bad_chars(add_nick, strlen(add_nick)) == TRUE) {
5038 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
5039 SILC_STATUS_ERR_BAD_NICKNAME, 0);
5043 /* Hash the nick, we have the hash saved, not nicks because we can
5044 do one to one mapping to the nick from Client ID hash this way. */
5045 silc_to_lower(add_nick, nick, sizeof(nick) - 1);
5046 silc_hash_make(server->md5hash, nick, strlen(nick), hash);
5048 /* Check whether this client is already watching this nickname */
5049 if (silc_hash_table_find_by_context(server->watcher_list, hash,
5051 /* Nickname is alredy being watched for this client */
5052 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
5053 SILC_STATUS_ERR_NICKNAME_IN_USE,
5058 /* Get the nickname from the watcher list and use the same key in
5059 new entries as well. If key doesn't exist then create it. */
5060 if (!silc_hash_table_find(server->watcher_list, hash, (void **)&tmp, NULL))
5061 tmp = silc_memdup(hash, CLIENTID_HASH_LEN);
5063 /* Add the client to the watcher list with the specified nickname hash. */
5064 silc_hash_table_add(server->watcher_list, tmp, client);
5067 /* Delete nickname from watch list */
5069 if (silc_server_name_bad_chars(del_nick, strlen(del_nick)) == TRUE) {
5070 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
5071 SILC_STATUS_ERR_BAD_NICKNAME, 0);
5075 /* Hash the nick, we have the hash saved, not nicks because we can
5076 do one to one mapping to the nick from Client ID hash this way. */
5077 silc_to_lower(del_nick, nick, sizeof(nick) - 1);
5078 silc_hash_make(server->md5hash, nick, strlen(nick), hash);
5080 /* Check that this client is watching for this nickname */
5081 if (!silc_hash_table_find_by_context(server->watcher_list, hash,
5082 client, (void **)&tmp)) {
5083 /* Nickname is alredy being watched for this client */
5084 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
5085 SILC_STATUS_ERR_NO_SUCH_NICK, 0);
5089 /* Delete the nickname from the watcher list. */
5090 silc_hash_table_del_by_context(server->watcher_list, hash, client);
5092 /* Now check whether there still exists entries with this key, if not
5093 then free the key to not leak memory. */
5094 if (!silc_hash_table_find(server->watcher_list, hash, NULL, NULL))
5098 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
5102 silc_free(client_id);
5103 silc_server_command_free(cmd);
5106 /* Server side of SILCOPER command. Client uses this comand to obtain router
5107 operator privileges to this router. */
5109 SILC_SERVER_CMD_FUNC(silcoper)
5111 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5112 SilcServer server = cmd->server;
5113 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
5114 unsigned char *username, *auth;
5116 SilcServerConfigAdmin *admin;
5117 SilcIDListData idata = (SilcIDListData)client;
5118 bool result = FALSE;
5119 SilcPublicKey cached_key;
5121 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
5123 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
5126 if (server->server_type != SILC_ROUTER) {
5127 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
5128 SILC_STATUS_ERR_AUTH_FAILED, 0);
5132 /* Get the username */
5133 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5135 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
5136 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5141 /* Get the admin configuration */
5142 admin = silc_server_config_find_admin(server, cmd->sock->ip,
5143 username, client->nickname);
5145 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
5146 username, client->nickname);
5148 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
5149 SILC_STATUS_ERR_AUTH_FAILED, 0);
5154 /* Get the authentication payload */
5155 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
5157 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
5158 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5163 /* Verify the authentication data. If both passphrase and public key
5164 is set then try both of them. */
5165 if (admin->passphrase)
5166 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
5167 admin->passphrase, admin->passphrase_len,
5168 idata->hash, client->id, SILC_ID_CLIENT);
5169 if (!result && admin->publickeys) {
5170 cached_key = silc_server_get_public_key(server, admin->publickeys);
5173 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
5174 cached_key, 0, idata->hash,
5175 client->id, SILC_ID_CLIENT);
5178 /* Authentication failed */
5179 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
5180 SILC_STATUS_ERR_AUTH_FAILED, 0);
5184 /* Client is now router operator */
5185 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
5187 /* Update statistics */
5188 if (client->connection)
5189 server->stat.my_router_ops++;
5190 if (server->server_type == SILC_ROUTER)
5191 server->stat.router_ops++;
5193 /* Send UMODE change to primary router */
5194 if (!server->standalone)
5195 silc_server_send_notify_umode(server, server->router->connection, TRUE,
5196 client->id, client->mode);
5198 /* Check if anyone is watching this nickname */
5199 if (server->server_type == SILC_ROUTER)
5200 silc_server_check_watcher_list(server, client, NULL,
5201 SILC_NOTIFY_TYPE_UMODE_CHANGE);
5203 /* Send reply to the sender */
5204 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
5208 silc_server_command_free(cmd);
5211 /* Server side of command BAN. This is used to manage the ban list of the
5212 channel. To add clients and remove clients from the ban list. */
5214 SILC_SERVER_CMD_FUNC(ban)
5216 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5217 SilcServer server = cmd->server;
5218 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
5220 SilcChannelEntry channel;
5221 SilcChannelClientEntry chl;
5222 SilcChannelID *channel_id = NULL;
5223 unsigned char *id, *add, *del;
5224 SilcUInt32 id_len, tmp_len;
5225 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
5227 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
5230 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
5232 /* Get Channel ID */
5233 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
5235 channel_id = silc_id_payload_parse_id(id, id_len, NULL);
5237 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
5238 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
5243 /* Get channel entry. The server must know about the channel since the
5244 client is expected to be on the channel. */
5245 channel = silc_idlist_find_channel_by_id(server->local_list,
5248 channel = silc_idlist_find_channel_by_id(server->global_list,
5251 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
5252 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
5258 /* Check whether this client is on the channel */
5259 if (!silc_server_client_on_channel(client, channel, &chl)) {
5260 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
5261 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
5265 /* The client must be at least channel operator. */
5266 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
5267 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
5268 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
5272 /* Get the new ban and add it to the ban list */
5273 add = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
5275 if (!channel->ban_list)
5276 channel->ban_list = silc_calloc(tmp_len + 2, sizeof(*channel->ban_list));
5278 channel->ban_list = silc_realloc(channel->ban_list,
5279 sizeof(*channel->ban_list) *
5281 strlen(channel->ban_list) + 2));
5282 if (add[tmp_len - 1] == ',')
5283 add[tmp_len - 1] = '\0';
5285 strncat(channel->ban_list, add, tmp_len);
5286 strncat(channel->ban_list, ",", 1);
5289 /* Get the ban to be removed and remove it from the list */
5290 del = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
5291 if (del && channel->ban_list) {
5292 char *start, *end, *n;
5294 if (!strncmp(channel->ban_list, del, strlen(channel->ban_list) - 1)) {
5295 silc_free(channel->ban_list);
5296 channel->ban_list = NULL;
5298 start = strstr(channel->ban_list, del);
5299 if (start && strlen(start) >= tmp_len) {
5300 end = start + tmp_len;
5301 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
5302 strncat(n, channel->ban_list, start - channel->ban_list);
5303 strncat(n, end + 1, ((channel->ban_list + strlen(channel->ban_list)) -
5305 silc_free(channel->ban_list);
5306 channel->ban_list = n;
5311 /* Send the BAN notify type to our primary router. */
5312 if (!server->standalone && (add || del))
5313 silc_server_send_notify_ban(server, server->router->connection,
5314 server->server_type == SILC_ROUTER ?
5315 TRUE : FALSE, channel, add, del);
5317 /* Send the reply back to the client */
5319 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
5320 SILC_STATUS_OK, 0, ident, 2,
5322 3, channel->ban_list,
5324 strlen(channel->ban_list) -1 : 0);
5325 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
5326 packet->data, packet->len, FALSE);
5328 silc_buffer_free(packet);
5331 silc_free(channel_id);
5332 silc_server_command_free(cmd);
5335 /* Server side command of LEAVE. Removes client from a channel. */
5337 SILC_SERVER_CMD_FUNC(leave)
5339 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5340 SilcServer server = cmd->server;
5341 SilcSocketConnection sock = cmd->sock;
5342 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
5343 SilcChannelID *id = NULL;
5344 SilcChannelEntry channel;
5348 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
5350 /* Get Channel ID */
5351 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
5353 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
5354 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
5357 id = silc_id_payload_parse_id(tmp, len, NULL);
5359 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
5360 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
5364 /* Get channel entry */
5365 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
5367 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
5369 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
5370 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
5376 /* Check whether this client is on the channel */
5377 if (!silc_server_client_on_channel(id_entry, channel, NULL)) {
5378 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
5379 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
5383 /* Notify routers that they should remove this client from their list
5384 of clients on the channel. Send LEAVE notify type. */
5385 if (!server->standalone)
5386 silc_server_send_notify_leave(server, server->router->connection,
5387 server->server_type == SILC_ROUTER ?
5388 TRUE : FALSE, channel, id_entry->id);
5390 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
5391 SILC_STATUS_OK, 0, 2, tmp, len);
5393 /* Remove client from channel */
5394 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
5396 /* If the channel does not exist anymore we won't send anything */
5399 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
5400 /* Re-generate channel key */
5401 if (!silc_server_create_channel_key(server, channel, 0))
5404 /* Send the channel key */
5405 silc_server_send_channel_key(server, NULL, channel,
5406 server->server_type == SILC_ROUTER ?
5407 FALSE : !server->standalone);
5412 silc_server_command_free(cmd);
5415 /* Server side of command USERS. Resolves clients and their USERS currently
5416 joined on the requested channel. The list of Client ID's and their modes
5417 on the channel is sent back. */
5419 SILC_SERVER_CMD_FUNC(users)
5421 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5422 SilcServer server = cmd->server;
5423 SilcChannelEntry channel;
5424 SilcChannelID *id = NULL;
5425 SilcBuffer packet, idp;
5426 unsigned char *channel_id;
5427 SilcUInt32 channel_id_len;
5428 SilcBuffer client_id_list;
5429 SilcBuffer client_mode_list;
5430 unsigned char lc[4];
5431 SilcUInt32 list_count = 0;
5432 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
5435 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
5437 /* Get Channel ID */
5438 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
5440 /* Get channel name */
5441 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
5443 if (!channel_id && !channel_name) {
5444 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
5445 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
5450 id = silc_id_payload_parse_id(channel_id, channel_id_len, NULL);
5452 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
5453 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
5458 /* If we are server and we don't know about this channel we will send
5459 the command to our router. If we know about the channel then we also
5460 have the list of users already. */
5462 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
5464 channel = silc_idlist_find_channel_by_name(server->local_list,
5465 channel_name, NULL);
5467 if (!channel || channel->disabled || !channel->users_resolved) {
5468 if (server->server_type != SILC_ROUTER && !server->standalone &&
5472 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
5473 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
5475 /* Send USERS command */
5476 silc_server_packet_send(server, server->router->connection,
5477 SILC_PACKET_COMMAND, cmd->packet->flags,
5478 tmpbuf->data, tmpbuf->len, TRUE);
5480 /* Reprocess this packet after received reply */
5481 silc_server_command_pending(server, SILC_COMMAND_USERS,
5482 silc_command_get_ident(cmd->payload),
5483 silc_server_command_users,
5484 silc_server_command_dup(cmd));
5485 cmd->pending = TRUE;
5486 silc_command_set_ident(cmd->payload, ident);
5487 silc_buffer_free(tmpbuf);
5492 /* Check the global list as well. */
5494 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
5496 channel = silc_idlist_find_channel_by_name(server->global_list,
5497 channel_name, NULL);
5499 /* Channel really does not exist */
5500 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
5501 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
5507 /* If the channel is private or secret do not send anything, unless the
5508 user requesting this command is on the channel or is server */
5509 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
5510 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)
5511 && !silc_server_client_on_channel(cmd->sock->user_data, channel,
5513 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
5514 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
5519 /* Get the users list */
5520 silc_server_get_users_on_channel(server, channel, &client_id_list,
5521 &client_mode_list, &list_count);
5524 SILC_PUT32_MSB(list_count, lc);
5527 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
5528 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
5529 SILC_STATUS_OK, 0, ident, 4,
5530 2, idp->data, idp->len,
5532 4, client_id_list->data,
5533 client_id_list->len,
5534 5, client_mode_list->data,
5535 client_mode_list->len);
5536 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
5537 packet->data, packet->len, FALSE);
5539 silc_buffer_free(idp);
5540 silc_buffer_free(packet);
5541 silc_buffer_free(client_id_list);
5542 silc_buffer_free(client_mode_list);
5546 silc_server_command_free(cmd);
5549 /* Server side of command GETKEY. This fetches the client's public key
5550 from the server where to the client is connected. */
5552 SILC_SERVER_CMD_FUNC(getkey)
5554 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5555 SilcServer server = cmd->server;
5557 SilcClientEntry client;
5558 SilcServerEntry server_entry;
5559 SilcClientID *client_id = NULL;
5560 SilcServerID *server_id = NULL;
5561 SilcIDPayload idp = NULL;
5562 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
5563 unsigned char *tmp, *pkdata;
5564 SilcUInt32 tmp_len, pklen;
5565 SilcBuffer pk = NULL;
5567 SilcPublicKey public_key;
5569 SILC_LOG_DEBUG(("Start"));
5571 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5573 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5574 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5578 idp = silc_id_payload_parse(tmp, tmp_len);
5580 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5581 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5586 id_type = silc_id_payload_get_type(idp);
5587 if (id_type == SILC_ID_CLIENT) {
5588 client_id = silc_id_payload_get_id(idp);
5590 /* If the client is not found from local list there is no chance it
5591 would be locally connected client so send the command further. */
5592 client = silc_idlist_find_client_by_id(server->local_list,
5593 client_id, TRUE, NULL);
5595 client = silc_idlist_find_client_by_id(server->global_list,
5596 client_id, TRUE, NULL);
5598 if ((!client && !cmd->pending && !server->standalone) ||
5599 (client && !client->connection && !cmd->pending &&
5600 !(client->mode & SILC_UMODE_DETACHED)) ||
5601 (client && !client->data.public_key && !cmd->pending)) {
5603 SilcUInt16 old_ident;
5604 SilcSocketConnection dest_sock;
5606 dest_sock = silc_server_get_client_route(server, NULL, 0,
5607 client_id, NULL, NULL);
5611 old_ident = silc_command_get_ident(cmd->payload);
5612 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
5613 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
5615 silc_server_packet_send(server, dest_sock,
5616 SILC_PACKET_COMMAND, cmd->packet->flags,
5617 tmpbuf->data, tmpbuf->len, TRUE);
5619 /* Reprocess this packet after received reply from router */
5620 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
5621 silc_command_get_ident(cmd->payload),
5622 silc_server_command_getkey,
5623 silc_server_command_dup(cmd));
5624 cmd->pending = TRUE;
5625 silc_command_set_ident(cmd->payload, old_ident);
5626 silc_buffer_free(tmpbuf);
5631 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5632 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
5637 /* The client is locally connected, just get the public key and
5638 send it back. If they key does not exist then do not send it,
5639 send just OK reply */
5640 public_key = client->data.public_key;
5645 tmp = silc_pkcs_public_key_encode(public_key, &tmp_len);
5646 pk = silc_buffer_alloc(4 + tmp_len);
5647 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
5648 silc_buffer_format(pk,
5649 SILC_STR_UI_SHORT(tmp_len),
5650 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
5651 SILC_STR_UI_XNSTRING(tmp, tmp_len),
5657 } else if (id_type == SILC_ID_SERVER) {
5658 server_id = silc_id_payload_get_id(idp);
5660 /* If the server is not found from local list there is no chance it
5661 would be locally connected server so send the command further. */
5662 server_entry = silc_idlist_find_server_by_id(server->local_list,
5663 server_id, TRUE, NULL);
5665 server_entry = silc_idlist_find_server_by_id(server->global_list,
5666 server_id, TRUE, NULL);
5668 if (server_entry != server->id_entry &&
5669 ((!server_entry && !cmd->pending && !server->standalone) ||
5670 (server_entry && !server_entry->connection && !cmd->pending &&
5671 !server->standalone) ||
5672 (server_entry && !server_entry->data.public_key && !cmd->pending &&
5673 !server->standalone))) {
5675 SilcUInt16 old_ident;
5677 old_ident = silc_command_get_ident(cmd->payload);
5678 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
5679 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
5681 silc_server_packet_send(server, server->router->connection,
5682 SILC_PACKET_COMMAND, cmd->packet->flags,
5683 tmpbuf->data, tmpbuf->len, TRUE);
5685 /* Reprocess this packet after received reply from router */
5686 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
5687 silc_command_get_ident(cmd->payload),
5688 silc_server_command_getkey,
5689 silc_server_command_dup(cmd));
5690 cmd->pending = TRUE;
5691 silc_command_set_ident(cmd->payload, old_ident);
5692 silc_buffer_free(tmpbuf);
5696 if (!server_entry) {
5697 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
5698 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
5703 /* If they key does not exist then do not send it, send just OK reply */
5704 public_key = (!server_entry->data.public_key ?
5705 (server_entry == server->id_entry ? server->public_key :
5706 NULL) : server_entry->data.public_key);
5711 tmp = silc_pkcs_public_key_encode(public_key, &tmp_len);
5712 pk = silc_buffer_alloc(4 + tmp_len);
5713 silc_buffer_pull_tail(pk, SILC_BUFFER_END(pk));
5714 silc_buffer_format(pk,
5715 SILC_STR_UI_SHORT(tmp_len),
5716 SILC_STR_UI_SHORT(SILC_SKE_PK_TYPE_SILC),
5717 SILC_STR_UI_XNSTRING(tmp, tmp_len),
5727 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5728 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
5729 SILC_STATUS_OK, 0, ident,
5733 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
5734 packet->data, packet->len, FALSE);
5735 silc_buffer_free(packet);
5738 silc_buffer_free(pk);
5742 silc_id_payload_free(idp);
5743 silc_free(client_id);
5744 silc_free(server_id);
5745 silc_server_command_free(cmd);
5749 /* Private range commands, specific to this implementation */
5751 /* Server side command of CONNECT. Connects us to the specified remote
5752 server or router. */
5754 SILC_SERVER_CMD_FUNC(connect)
5756 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5757 SilcServer server = cmd->server;
5758 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
5759 unsigned char *tmp, *host;
5761 SilcUInt32 port = SILC_PORT;
5763 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CONNECT, cmd, 1, 2);
5765 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
5768 /* Check whether client has the permissions. */
5769 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
5770 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
5771 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5772 SILC_STATUS_ERR_NO_SERVER_PRIV, 0);
5776 if (server->server_type == SILC_ROUTER &&
5777 client->mode & SILC_UMODE_SERVER_OPERATOR) {
5778 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5779 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
5783 /* Get the remote server */
5784 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5786 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5787 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5793 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
5795 SILC_GET32_MSB(port, tmp);
5797 /* Create the connection. It is done with timeout and is async. */
5798 silc_server_create_connection(server, host, port);
5800 /* Send reply to the sender */
5801 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
5805 silc_server_command_free(cmd);
5808 /* Server side command of CLOSE. Closes connection to a specified server. */
5810 SILC_SERVER_CMD_FUNC(close)
5812 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5813 SilcServer server = cmd->server;
5814 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
5815 SilcServerEntry server_entry;
5816 SilcSocketConnection sock;
5819 unsigned char *name;
5820 SilcUInt32 port = SILC_PORT;
5822 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CLOSE, cmd, 1, 2);
5824 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
5827 /* Check whether client has the permissions. */
5828 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
5829 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
5830 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5831 SILC_STATUS_ERR_NO_SERVER_PRIV,
5836 /* Get the remote server */
5837 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
5839 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5840 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
5846 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
5848 SILC_GET32_MSB(port, tmp);
5850 server_entry = silc_idlist_find_server_by_conn(server->local_list,
5851 name, port, FALSE, NULL);
5853 server_entry = silc_idlist_find_server_by_conn(server->global_list,
5854 name, port, FALSE, NULL);
5855 if (!server_entry) {
5856 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5857 SILC_STATUS_ERR_NO_SERVER_ID, 0);
5861 /* Send reply to the sender */
5862 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
5865 /* Close the connection to the server */
5866 sock = (SilcSocketConnection)server_entry->connection;
5868 /* If we shutdown primary router connection manually then don't trigger
5869 any reconnect or backup router connections, by setting the router
5871 if (server->router == server_entry) {
5872 server->id_entry->router = NULL;
5873 server->router = NULL;
5874 server->standalone = TRUE;
5876 silc_server_free_sock_user_data(server, sock, NULL);
5877 silc_server_close_connection(server, sock);
5880 silc_server_command_free(cmd);
5883 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
5884 active connections. */
5886 SILC_SERVER_CMD_FUNC(shutdown)
5888 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
5889 SilcServer server = cmd->server;
5890 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
5892 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_SHUTDOWN, cmd, 0, 0);
5894 if (!client || cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
5897 /* Check whether client has the permission. */
5898 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
5899 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
5900 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
5901 SILC_STATUS_ERR_NO_SERVER_PRIV,
5906 /* Send reply to the sender */
5907 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
5910 /* Then, gracefully, or not, bring the server down. */
5911 silc_server_stop(server);
5915 silc_server_command_free(cmd);