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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "serverincludes.h"
22 #include "server_internal.h"
24 static int silc_server_is_registered(SilcServer server,
25 SilcSocketConnection sock,
26 SilcServerCommandContext cmd,
29 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
34 silc_server_command_send_status_data(SilcServerCommandContext cmd,
39 const unsigned char *arg,
42 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
43 SilcServerCommandReplyContext cmdr,
45 SILC_TASK_CALLBACK(silc_server_command_process_timeout);
47 /* Server command list. */
48 SilcServerCommand silc_command_list[] =
50 SILC_SERVER_CMD(whois, WHOIS, SILC_CF_LAG | SILC_CF_REG),
51 SILC_SERVER_CMD(whowas, WHOWAS, SILC_CF_LAG | SILC_CF_REG),
52 SILC_SERVER_CMD(identify, IDENTIFY, SILC_CF_LAG | SILC_CF_REG),
53 SILC_SERVER_CMD(nick, NICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
54 SILC_SERVER_CMD(list, LIST, SILC_CF_LAG_STRICT | SILC_CF_REG),
55 SILC_SERVER_CMD(topic, TOPIC, SILC_CF_LAG | SILC_CF_REG),
56 SILC_SERVER_CMD(invite, INVITE, SILC_CF_LAG | SILC_CF_REG),
57 SILC_SERVER_CMD(quit, QUIT, SILC_CF_LAG | SILC_CF_REG),
58 SILC_SERVER_CMD(kill, KILL, SILC_CF_LAG_STRICT | SILC_CF_REG | SILC_CF_OPER),
59 SILC_SERVER_CMD(info, INFO, SILC_CF_LAG | SILC_CF_REG),
60 SILC_SERVER_CMD(stats, STATS, SILC_CF_LAG | SILC_CF_REG),
61 SILC_SERVER_CMD(ping, PING, SILC_CF_LAG | SILC_CF_REG),
62 SILC_SERVER_CMD(oper, OPER, SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
63 SILC_SERVER_CMD(join, JOIN, SILC_CF_LAG_STRICT | SILC_CF_REG),
64 SILC_SERVER_CMD(motd, MOTD, SILC_CF_LAG | SILC_CF_REG),
65 SILC_SERVER_CMD(umode, UMODE, SILC_CF_LAG | SILC_CF_REG),
66 SILC_SERVER_CMD(cmode, CMODE, SILC_CF_LAG_STRICT | SILC_CF_REG),
67 SILC_SERVER_CMD(cumode, CUMODE, SILC_CF_LAG | SILC_CF_REG),
68 SILC_SERVER_CMD(kick, KICK, SILC_CF_LAG_STRICT | SILC_CF_REG),
69 SILC_SERVER_CMD(ban, BAN, SILC_CF_LAG_STRICT | SILC_CF_REG),
70 SILC_SERVER_CMD(detach, DETACH, SILC_CF_LAG_STRICT | SILC_CF_REG),
71 SILC_SERVER_CMD(watch, WATCH, SILC_CF_LAG | SILC_CF_REG),
72 SILC_SERVER_CMD(silcoper, SILCOPER,
73 SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER),
74 SILC_SERVER_CMD(leave, LEAVE, SILC_CF_LAG_STRICT | SILC_CF_REG),
75 SILC_SERVER_CMD(users, USERS, SILC_CF_LAG | SILC_CF_REG),
76 SILC_SERVER_CMD(getkey, GETKEY, SILC_CF_LAG | SILC_CF_REG),
78 SILC_SERVER_CMD(connect, PRIV_CONNECT,
79 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
80 SILC_SERVER_CMD(close, PRIV_CLOSE,
81 SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER),
82 SILC_SERVER_CMD(shutdown, PRIV_SHUTDOWN, SILC_CF_LAG | SILC_CF_REG |
88 /* Performs several checks to the command. It first checks whether this
89 command was called as pending command callback. If it was then it checks
90 whether error occurred in the command reply where the pending command
93 It also checks that the requested command includes correct amount
95 #define SILC_SERVER_COMMAND_CHECK(command, context, min, max) \
99 if (silc_server_command_pending_error_check(cmd, context2, command)) { \
100 SILC_LOG_DEBUG(("Error occurred in command reply, command not called")); \
101 silc_server_command_free(cmd); \
105 _argc = silc_argument_get_arg_num(cmd->args); \
107 SILC_LOG_DEBUG(("Not enough parameters in command")); \
108 silc_server_command_send_status_reply(cmd, command, \
109 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, \
111 silc_server_command_free(cmd); \
115 SILC_LOG_DEBUG(("Too many parameters in command")); \
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_LOG_DEBUG(("Client entry is invalid"));
161 silc_server_command_free(timeout->ctx);
166 /* Update access time */
167 client->last_command = time(NULL);
169 if (!(timeout->cmd->flags & SILC_CF_REG)) {
170 SILC_LOG_DEBUG(("Calling %s command",
171 silc_get_command_name(timeout->cmd->cmd)));
172 timeout->cmd->cb(timeout->ctx, NULL);
173 } else if (silc_server_is_registered(timeout->ctx->server,
176 timeout->cmd->cmd)) {
177 SILC_LOG_DEBUG(("Calling %s command",
178 silc_get_command_name(timeout->cmd->cmd)));
179 timeout->cmd->cb(timeout->ctx, NULL);
181 SILC_LOG_DEBUG(("Client is not registered"));
182 silc_server_command_free(timeout->ctx);
188 /* Processes received command packet. */
190 void silc_server_command_process(SilcServer server,
191 SilcSocketConnection sock,
192 SilcPacketContext *packet)
194 SilcServerCommandContext ctx;
195 SilcServerCommand *cmd;
198 /* Allocate command context. This must be free'd by the
199 command routine receiving it. */
200 ctx = silc_server_command_alloc();
201 ctx->server = server;
202 ctx->sock = silc_socket_dup(sock);
203 ctx->packet = silc_packet_context_dup(packet); /* Save original packet */
205 /* Parse the command payload in the packet */
206 ctx->payload = silc_command_payload_parse(packet->buffer->data,
207 packet->buffer->len);
209 SILC_LOG_ERROR(("Bad command payload, packet dropped"));
210 silc_packet_context_free(packet);
211 silc_socket_free(ctx->sock);
215 ctx->args = silc_command_get_args(ctx->payload);
217 /* Get the command */
218 command = silc_command_get(ctx->payload);
219 for (cmd = silc_command_list; cmd->cb; cmd++)
220 if (cmd->cmd == command)
223 if (!cmd || !cmd->cb) {
224 SILC_LOG_DEBUG(("Unknown command %d", command));
225 silc_server_command_send_status_reply(ctx, command,
226 SILC_STATUS_ERR_UNKNOWN_COMMAND, 0);
227 silc_server_command_free(ctx);
231 /* Execute client's commands always with timeout. Normally they are
232 executed with zero (0) timeout but if client is sending command more
233 frequently than once in 2 seconds, then the timeout may be 0 to 2
235 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
236 SilcClientEntry client = (SilcClientEntry)sock->user_data;
237 SilcServerCommandTimeout timeout;
241 SILC_LOG_DEBUG(("Client entry is invalid"));
242 silc_server_command_free(ctx);
246 timeout = silc_calloc(1, sizeof(*timeout));
250 if (client->last_command && (time(NULL) - client->last_command) < 2) {
251 client->fast_command++;
254 if (client->fast_command - 2 <= 0)
255 client->fast_command = 0;
257 client->fast_command -= 2;
261 if (!fast && ((cmd->flags & SILC_CF_LAG_STRICT) ||
262 (client->fast_command > 5 && cmd->flags & SILC_CF_LAG)))
263 silc_schedule_task_add(server->schedule, sock->sock,
264 silc_server_command_process_timeout, timeout,
265 (client->fast_command < 3 ? 0 :
266 2 - (time(NULL) - client->last_command)),
267 (client->fast_command < 3 ? 200000 : 0),
268 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
270 silc_schedule_task_add(server->schedule, sock->sock,
271 silc_server_command_process_timeout, timeout,
272 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
276 /* Execute for server */
278 if (!(cmd->flags & SILC_CF_REG)) {
279 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
281 } else if (silc_server_is_registered(server, sock, ctx, cmd->cmd)) {
282 SILC_LOG_DEBUG(("Calling %s command", silc_get_command_name(cmd->cmd)));
285 SILC_LOG_DEBUG(("Server is not registered"));
286 silc_server_command_free(ctx);
290 /* Allocate Command Context */
292 SilcServerCommandContext silc_server_command_alloc()
294 SilcServerCommandContext ctx = silc_calloc(1, sizeof(*ctx));
299 /* Free's the command context allocated before executing the command */
301 void silc_server_command_free(SilcServerCommandContext ctx)
304 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
306 if (ctx->users < 1) {
308 silc_command_payload_free(ctx->payload);
310 silc_packet_context_free(ctx->packet);
312 silc_socket_free(ctx->sock); /* Decrease reference counter */
317 /* Duplicate Command Context by adding reference counter. The context won't
318 be free'd untill it hits zero. */
320 SilcServerCommandContext
321 silc_server_command_dup(SilcServerCommandContext ctx)
324 SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
329 /* Timeout for pending command. If reply to pending command never arrives
330 this is called to free resources. */
332 SILC_TASK_CALLBACK(silc_server_command_pending_timeout)
334 SilcServer server = app_context;
335 SilcServerCommandPending *reply = context;
336 SilcServerCommandReplyContext cmdr;
340 SILC_LOG_DEBUG(("Timeout pending command"));
342 /* Allocate temporary and bogus command reply context */
343 cmdr = silc_calloc(1, sizeof(*cmdr));
344 cmdr->server = server;
345 cmdr->ident = reply->ident;
347 /* Check for pending commands and mark to be exeucted */
349 silc_server_command_pending_check(server, reply->reply_cmd,
350 reply->ident, &cmdr->callbacks_count);
352 /* Create bogus command reply with an error inside */
354 silc_command_reply_payload_encode_va(reply->reply_cmd ? reply->reply_cmd :
355 SILC_COMMAND_RESERVED,
356 SILC_STATUS_ERR_TIMEDOUT, 0,
358 cmdr->payload = silc_command_payload_parse(tmpreply->data, tmpreply->len);
359 silc_buffer_free(tmpreply);
361 /* Call all callbacks. Same as SILC_SERVER_PENDING_EXEC macro. */
362 for (i = 0; i < cmdr->callbacks_count; i++)
363 if (cmdr->callbacks[i].callback)
364 (*cmdr->callbacks[i].callback)(cmdr->callbacks[i].context, cmdr);
366 silc_server_command_pending_del(server, reply->reply_cmd, reply->ident);
367 silc_server_command_reply_free(cmdr);
370 /* Add new pending command to be executed when reply to a command has been
371 received. The `reply_cmd' is the command that will call the `callback'
372 with `context' when reply has been received. It can be SILC_COMMAND_NONE
373 to match any command with the `ident'. If `ident' is non-zero
374 the `callback' will be executed when received reply with command
375 identifier `ident'. If there already exists pending command for the
376 specified command, ident, callback and context this function has no
379 bool silc_server_command_pending(SilcServer server,
380 SilcCommand reply_cmd,
382 SilcCommandCb callback,
385 return silc_server_command_pending_timed(server, reply_cmd, ident, callback,
389 /* Same as silc_server_command_pending with specific timeout for pending
390 commands. If the `timeout' is zero default timeout is used. */
392 bool silc_server_command_pending_timed(SilcServer server,
393 SilcCommand reply_cmd,
395 SilcCommandCb callback,
399 SilcServerCommandPending *reply;
401 /* Check whether identical pending already exists for same command,
402 ident, callback and callback context. If it does then it would be
403 error to register it again. */
404 silc_dlist_start(server->pending_commands);
405 while ((reply = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
406 if (reply->reply_cmd == reply_cmd && reply->ident == ident &&
407 reply->callback == callback && reply->context == context)
411 reply = silc_calloc(1, sizeof(*reply));
412 reply->reply_cmd = reply_cmd;
413 reply->ident = ident;
414 reply->context = context;
415 reply->callback = callback;
417 silc_schedule_task_add(server->schedule, 0,
418 silc_server_command_pending_timeout, reply,
419 timeout ? timeout : 10, 0,
420 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
421 silc_dlist_add(server->pending_commands, reply);
426 /* Deletes pending command by reply command type. */
428 void silc_server_command_pending_del(SilcServer server,
429 SilcCommand reply_cmd,
432 SilcServerCommandPending *r;
434 silc_dlist_start(server->pending_commands);
435 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
436 if ((r->reply_cmd == reply_cmd || (r->reply_cmd == SILC_COMMAND_NONE &&
438 && r->ident == ident) {
439 silc_dlist_del(server->pending_commands, r);
441 silc_schedule_task_del(server->schedule, r->timeout);
447 /* Checks for pending commands and marks callbacks to be called from
448 the command reply function. Returns TRUE if there were pending command. */
450 SilcServerCommandPendingCallbacks
451 silc_server_command_pending_check(SilcServer server,
454 SilcUInt32 *callbacks_count)
456 SilcServerCommandPending *r;
457 SilcServerCommandPendingCallbacks callbacks = NULL;
460 silc_dlist_start(server->pending_commands);
461 while ((r = silc_dlist_get(server->pending_commands)) != SILC_LIST_END) {
462 if ((r->reply_cmd == command || r->reply_cmd == SILC_COMMAND_NONE)
463 && r->ident == ident) {
464 callbacks = silc_realloc(callbacks, sizeof(*callbacks) * (i + 1));
465 callbacks[i].context = r->context;
466 callbacks[i].callback = r->callback;
467 r->reply_check = TRUE;
472 *callbacks_count = i;
476 /* Sends simple status message as command reply packet */
479 silc_server_command_send_status_reply(SilcServerCommandContext cmd,
486 SILC_LOG_DEBUG(("Sending command status %d", status));
489 silc_command_reply_payload_encode_va(command, status, error,
490 silc_command_get_ident(cmd->payload),
492 silc_server_packet_send(cmd->server, cmd->sock,
493 SILC_PACKET_COMMAND_REPLY, 0,
494 buffer->data, buffer->len, FALSE);
495 silc_buffer_free(buffer);
498 /* Sends command status reply with one extra argument. The argument
499 type must be sent as argument. */
502 silc_server_command_send_status_data(SilcServerCommandContext cmd,
507 const unsigned char *arg,
512 SILC_LOG_DEBUG(("Sending command status %d", status));
515 silc_command_reply_payload_encode_va(command, status, 0,
516 silc_command_get_ident(cmd->payload),
517 1, arg_type, arg, arg_len);
518 silc_server_packet_send(cmd->server, cmd->sock,
519 SILC_PACKET_COMMAND_REPLY, 0,
520 buffer->data, buffer->len, FALSE);
521 silc_buffer_free(buffer);
524 /* This function can be called to check whether in the command reply
525 an error occurred. This function has no effect if this is called
526 when the command function was not called as pending command callback.
527 This returns TRUE if error had occurred. */
530 silc_server_command_pending_error_check(SilcServerCommandContext cmd,
531 SilcServerCommandReplyContext cmdr,
534 if (!cmd->pending || !cmdr)
537 if (!silc_command_get_status(cmdr->payload, NULL, NULL)) {
540 /* Send the same command reply payload */
541 silc_command_set_command(cmdr->payload, silc_command_get(cmd->payload));
542 silc_command_set_ident(cmdr->payload,
543 silc_command_get_ident(cmd->payload));
544 buffer = silc_command_payload_encode_payload(cmdr->payload);
545 silc_server_packet_send(cmd->server, cmd->sock,
546 SILC_PACKET_COMMAND_REPLY, 0,
547 buffer->data, buffer->len, FALSE);
548 silc_buffer_free(buffer);
555 /* Server side of command WHOIS. */
557 SILC_SERVER_CMD_FUNC(whois)
559 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
560 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOIS, cmd, 1, 256);
561 silc_server_query_command(cmd->server, SILC_COMMAND_WHOIS, cmd);
562 silc_server_command_free(cmd);
565 /* Server side of command WHOWAS. */
567 SILC_SERVER_CMD_FUNC(whowas)
569 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
570 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WHOWAS, cmd, 1, 2);
571 silc_server_query_command(cmd->server, SILC_COMMAND_WHOWAS, cmd);
572 silc_server_command_free(cmd);
575 /* Server side of command IDENTIFY. */
577 SILC_SERVER_CMD_FUNC(identify)
579 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
580 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_IDENTIFY, cmd, 1, 256);
581 silc_server_query_command(cmd->server, SILC_COMMAND_IDENTIFY, cmd);
582 silc_server_command_free(cmd);
585 /* Server side of command NICK. Sets nickname for user. Setting
586 nickname causes generation of a new client ID for the client. The
587 new client ID is sent to the client after changing the nickname. */
589 SILC_SERVER_CMD_FUNC(nick)
591 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
592 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
593 SilcServer server = cmd->server;
594 SilcBuffer packet, nidp, oidp = NULL;
595 SilcClientID *new_id;
598 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
601 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
604 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_NICK, cmd, 1, 1);
607 nick = silc_argument_get_arg_type(cmd->args, 1, &nick_len);
609 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
610 SILC_STATUS_ERR_BAD_NICKNAME, 0);
615 if (silc_server_name_bad_chars(nick, nick_len) == TRUE) {
616 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
617 SILC_STATUS_ERR_BAD_NICKNAME, 0);
621 /* Check for same nickname */
622 if (!strcmp(client->nickname, nick)) {
623 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
627 /* Create new Client ID */
628 while (!silc_id_create_client_id(cmd->server, cmd->server->id,
630 cmd->server->md5hash, nick,
634 silc_server_command_send_status_reply(cmd, SILC_COMMAND_NICK,
635 SILC_STATUS_ERR_BAD_NICKNAME, 0);
638 snprintf(&nick[strlen(nick) - 1], 1, "%d", nickfail);
641 /* Send notify about nickname change to our router. We send the new
642 ID and ask to replace it with the old one. If we are router the
643 packet is broadcasted. Send NICK_CHANGE notify. */
644 silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
645 SILC_BROADCAST(server), client->id,
648 /* Check if anyone is watching the old nickname */
649 if (server->server_type == SILC_ROUTER)
650 silc_server_check_watcher_list(server, client, nick,
651 SILC_NOTIFY_TYPE_NICK_CHANGE);
653 oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
655 /* Remove old cache entry */
656 silc_idcache_del_by_context(server->local_list->clients, client);
658 silc_free(client->id);
661 silc_free(client->nickname);
662 client->nickname = strdup(nick);
664 /* Update client cache */
665 silc_idcache_add(server->local_list->clients, client->nickname,
666 client->id, (void *)client, 0, NULL);
668 nidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
670 /* Send NICK_CHANGE notify to the client's channels */
671 silc_server_send_notify_on_channels(server, NULL, client,
672 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
673 oidp->data, oidp->len,
674 nidp->data, nidp->len,
676 strlen(client->nickname));
678 /* Check if anyone is watching the new nickname */
679 if (server->server_type == SILC_ROUTER)
680 silc_server_check_watcher_list(server, client, NULL,
681 SILC_NOTIFY_TYPE_NICK_CHANGE);
684 /* Send the new Client ID as reply command back to client */
685 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_NICK,
686 SILC_STATUS_OK, 0, ident, 2,
687 2, nidp->data, nidp->len,
688 3, nick, strlen(nick));
689 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
690 0, packet->data, packet->len, FALSE);
692 silc_buffer_free(packet);
693 silc_buffer_free(nidp);
695 silc_buffer_free(oidp);
698 silc_server_command_free(cmd);
701 /* Sends the LIST command reply */
704 silc_server_command_list_send_reply(SilcServerCommandContext cmd,
705 SilcChannelEntry *lch,
706 SilcUInt32 lch_count,
707 SilcChannelEntry *gch,
708 SilcUInt32 gch_count)
711 SilcBuffer packet, idp;
712 SilcChannelEntry entry;
714 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
716 unsigned char usercount[4];
718 int valid_lcount = 0, valid_rcount = 0;
720 for (i = 0; i < lch_count; i++) {
721 if (lch[i]->mode & SILC_CHANNEL_MODE_SECRET)
726 for (i = 0; i < gch_count; i++) {
727 if (gch[i]->mode & SILC_CHANNEL_MODE_SECRET)
733 status = SILC_STATUS_OK;
734 if ((lch_count + gch_count) > 1)
735 status = SILC_STATUS_LIST_START;
738 for (i = 0, k = 0; i < lch_count; i++) {
744 status = SILC_STATUS_LIST_ITEM;
745 if (valid_lcount > 1 && k == valid_lcount - 1 && !valid_rcount)
746 status = SILC_STATUS_LIST_END;
748 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
750 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
752 memset(usercount, 0, sizeof(usercount));
754 topic = entry->topic;
755 users = silc_hash_table_count(entry->user_list);
756 SILC_PUT32_MSB(users, usercount);
761 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
763 2, idp->data, idp->len,
764 3, entry->channel_name,
765 strlen(entry->channel_name),
766 4, topic, topic ? strlen(topic) : 0,
768 silc_server_packet_send(cmd->server, cmd->sock,
769 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
771 silc_buffer_free(packet);
772 silc_buffer_free(idp);
777 for (i = 0, k = 0; i < gch_count; i++) {
783 status = SILC_STATUS_LIST_ITEM;
784 if (valid_rcount > 1 && k == valid_rcount - 1)
785 status = SILC_STATUS_LIST_END;
787 idp = silc_id_payload_encode(entry->id, SILC_ID_CHANNEL);
789 if (entry->mode & SILC_CHANNEL_MODE_PRIVATE) {
791 memset(usercount, 0, sizeof(usercount));
793 topic = entry->topic;
794 users = entry->user_count;
795 SILC_PUT32_MSB(users, usercount);
800 silc_command_reply_payload_encode_va(SILC_COMMAND_LIST,
802 2, idp->data, idp->len,
803 3, entry->channel_name,
804 strlen(entry->channel_name),
805 4, topic, topic ? strlen(topic) : 0,
807 silc_server_packet_send(cmd->server, cmd->sock,
808 SILC_PACKET_COMMAND_REPLY, 0, packet->data,
810 silc_buffer_free(packet);
811 silc_buffer_free(idp);
816 /* Server side of LIST command. This lists the channel of the requested
817 server. Secret channels are not listed. */
819 SILC_SERVER_CMD_FUNC(list)
821 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
822 SilcServer server = cmd->server;
823 SilcChannelID *channel_id = NULL;
826 SilcChannelEntry *lchannels = NULL, *gchannels = NULL;
827 SilcUInt32 lch_count = 0, gch_count = 0;
829 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LIST, cmd, 0, 1);
831 /* If we are normal server, send the command to router, since we
832 want to know all channels in the network. */
833 if (!cmd->pending && server->server_type != SILC_ROUTER &&
834 !server->standalone) {
836 SilcUInt16 old_ident;
838 old_ident = silc_command_get_ident(cmd->payload);
839 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
840 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
841 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
842 SILC_PACKET_COMMAND, cmd->packet->flags,
843 tmpbuf->data, tmpbuf->len, TRUE);
845 /* Reprocess this packet after received reply from router */
846 silc_server_command_pending(server, SILC_COMMAND_LIST,
847 silc_command_get_ident(cmd->payload),
848 silc_server_command_list,
849 silc_server_command_dup(cmd));
851 silc_command_set_ident(cmd->payload, old_ident);
852 silc_buffer_free(tmpbuf);
857 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
859 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
861 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LIST,
862 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
867 /* Get the channels from local list */
868 lchannels = silc_idlist_get_channels(server->local_list, channel_id,
871 /* Get the channels from global list */
872 gchannels = silc_idlist_get_channels(server->global_list, channel_id,
876 silc_server_command_list_send_reply(cmd, lchannels, lch_count,
877 gchannels, gch_count);
879 silc_free(lchannels);
880 silc_free(gchannels);
883 silc_server_command_free(cmd);
886 /* Server side of TOPIC command. Sets topic for channel and/or returns
887 current topic to client. */
889 SILC_SERVER_CMD_FUNC(topic)
891 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
892 SilcServer server = cmd->server;
893 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
894 SilcChannelID *channel_id;
895 SilcChannelEntry channel;
896 SilcChannelClientEntry chl;
897 SilcBuffer packet, idp;
899 SilcUInt32 argc, tmp_len;
900 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
902 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
905 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_TOPIC, cmd, 1, 2);
907 argc = silc_argument_get_arg_num(cmd->args);
910 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
912 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
913 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
916 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
918 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
919 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
923 /* Check whether the channel exists */
924 channel = silc_idlist_find_channel_by_id(server->local_list,
927 channel = silc_idlist_find_channel_by_id(server->global_list,
930 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
931 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
939 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
941 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
942 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
947 if (strlen(tmp) > 256) {
948 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
949 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
954 /* See whether the client is on channel and has rights to change topic */
955 if (!silc_server_client_on_channel(client, channel, &chl)) {
956 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
957 SILC_STATUS_ERR_NOT_ON_CHANNEL,
962 if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
963 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
964 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
965 silc_server_command_send_status_reply(cmd, SILC_COMMAND_TOPIC,
966 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
971 if (!channel->topic || strcmp(channel->topic, tmp)) {
972 /* Set the topic for channel */
973 silc_free(channel->topic);
974 channel->topic = strdup(tmp);
976 /* Send TOPIC_SET notify type to the network */
977 silc_server_send_notify_topic_set(server, SILC_PRIMARY_ROUTE(server),
978 SILC_BROADCAST(server), channel,
979 client->id, SILC_ID_CLIENT,
982 /* Send notify about topic change to all clients on the channel */
983 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
984 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
985 SILC_NOTIFY_TYPE_TOPIC_SET, 2,
988 strlen(channel->topic));
989 silc_buffer_free(idp);
993 /* Send the topic to client as reply packet */
994 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
995 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_TOPIC,
996 SILC_STATUS_OK, 0, ident, 2,
997 2, idp->data, idp->len,
1000 strlen(channel->topic) : 0);
1001 silc_server_packet_send(cmd->server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1002 0, packet->data, packet->len, FALSE);
1004 silc_buffer_free(packet);
1005 silc_buffer_free(idp);
1006 silc_free(channel_id);
1009 silc_server_command_free(cmd);
1012 /* Server side of INVITE command. Invites some client to join some channel.
1013 This command is also used to manage the invite list of the channel. */
1015 SILC_SERVER_CMD_FUNC(invite)
1017 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1018 SilcServer server = cmd->server;
1019 SilcSocketConnection sock = cmd->sock, dest_sock;
1020 SilcChannelClientEntry chl;
1021 SilcClientEntry sender, dest;
1022 SilcClientID *dest_id = NULL;
1023 SilcChannelEntry channel;
1024 SilcChannelID *channel_id = NULL;
1025 SilcIDListData idata;
1026 SilcArgumentPayload args;
1027 SilcHashTableList htl;
1028 SilcBuffer packet, list, tmp2;
1030 SilcUInt32 len, type;
1031 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
1033 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INVITE, cmd, 1, 4);
1035 /* Get Channel ID */
1036 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1038 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1039 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
1042 channel_id = silc_id_payload_parse_id(tmp, len, NULL);
1044 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1045 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
1049 /* Get the channel entry */
1050 channel = silc_idlist_find_channel_by_id(server->local_list,
1053 channel = silc_idlist_find_channel_by_id(server->global_list,
1056 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1057 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
1063 /* Check whether the sender of this command is on the channel. */
1064 sender = (SilcClientEntry)sock->user_data;
1065 if (!sender || !silc_server_client_on_channel(sender, channel, &chl)) {
1066 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1067 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
1071 /* Check whether the channel is invite-only channel. If yes then the
1072 sender of this command must be at least channel operator. */
1073 if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
1074 !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1075 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1076 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1077 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
1082 /* Get destination client ID */
1083 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1087 dest_id = silc_id_payload_parse_id(tmp, len, NULL);
1089 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1090 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
1094 /* Get the client entry */
1095 dest = silc_server_query_client(server, dest_id, FALSE, &resolve);
1097 if (server->server_type != SILC_SERVER || !resolve || cmd->pending) {
1098 silc_server_command_send_status_reply(
1099 cmd, SILC_COMMAND_INVITE,
1100 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID, 0);
1104 /* The client info is being resolved. Reprocess this packet after
1105 receiving the reply to the query. */
1106 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1108 silc_server_command_invite,
1109 silc_server_command_dup(cmd));
1110 cmd->pending = TRUE;
1114 /* Check whether the requested client is already on the channel. */
1115 if (silc_server_client_on_channel(dest, channel, NULL)) {
1116 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1117 SILC_STATUS_ERR_USER_ON_CHANNEL,
1122 /* Get route to the client */
1123 dest_sock = silc_server_get_client_route(server, NULL, 0, dest_id,
1126 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1127 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1132 /* Add the client to the invite list */
1134 /* Allocate hash table for invite list if it doesn't exist yet */
1135 if (!channel->invite_list)
1136 channel->invite_list =
1137 silc_hash_table_alloc(0, silc_hash_ptr,
1139 silc_server_inviteban_destruct, channel, TRUE);
1141 /* Check if the ID is in the list already */
1142 silc_hash_table_list(channel->invite_list, &htl);
1143 while (silc_hash_table_get(&htl, (void **)&type, (void **)&tmp2)) {
1144 if (type == 3 && !memcmp(tmp2->data, tmp, len)) {
1149 silc_hash_table_list_reset(&htl);
1151 /* Add new Client ID to invite list */
1153 list = silc_buffer_alloc_size(len);
1154 silc_buffer_put(list, tmp, len);
1155 silc_hash_table_add(channel->invite_list, (void *)3, list);
1158 if (!(dest->mode & SILC_UMODE_BLOCK_INVITE)) {
1159 /* Send notify to the client that is invited to the channel */
1160 SilcBuffer idp, idp2;
1161 idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
1162 idp2 = silc_id_payload_encode(sender->id, SILC_ID_CLIENT);
1163 silc_server_send_notify_dest(server, dest_sock, FALSE, dest_id,
1165 SILC_NOTIFY_TYPE_INVITE, 3,
1166 idp->data, idp->len,
1167 channel->channel_name,
1168 strlen(channel->channel_name),
1169 idp2->data, idp2->len);
1170 silc_buffer_free(idp);
1171 silc_buffer_free(idp2);
1175 /* Get the invite information */
1176 tmp = silc_argument_get_arg_type(cmd->args, 4, &len);
1177 if (tmp && len > 2) {
1178 /* Parse the arguments to see they are constructed correctly */
1179 SILC_GET16_MSB(argc, tmp);
1180 args = silc_argument_payload_parse(tmp + 2, len - 2, argc);
1182 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1183 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1188 /* Get the type of action */
1189 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
1190 if (tmp && len == 1) {
1191 if (tmp[0] == 0x00) {
1192 /* Allocate hash table for invite list if it doesn't exist yet */
1193 if (!channel->invite_list)
1194 channel->invite_list =
1195 silc_hash_table_alloc(0, silc_hash_ptr,
1197 silc_server_inviteban_destruct, channel,
1200 /* Check for resource limit */
1201 if (silc_hash_table_count(channel->invite_list) > 64) {
1202 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INVITE,
1203 SILC_STATUS_ERR_RESOURCE_LIMIT,
1209 /* Now add or delete the information. */
1210 silc_server_inviteban_process(server, channel->invite_list,
1211 (SilcUInt8)tmp[0], args);
1213 silc_argument_payload_free(args);
1216 /* Encode invite list */
1218 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1219 list = silc_buffer_alloc_size(2);
1220 silc_buffer_format(list,
1221 SILC_STR_UI_SHORT(silc_hash_table_count(
1222 channel->invite_list)),
1224 silc_hash_table_list(channel->invite_list, &htl);
1225 while (silc_hash_table_get(&htl, (void **)&type, (void **)&tmp2))
1226 list = silc_argument_payload_encode_one(list, tmp2->data, tmp2->len,
1228 silc_hash_table_list_reset(&htl);
1231 /* Send notify to the primary router */
1232 silc_server_send_notify_invite(
1233 server, SILC_PRIMARY_ROUTE(server),
1234 SILC_BROADCAST(server), channel, sender->id,
1235 silc_argument_get_arg_type(cmd->args, 3, NULL),
1238 /* Send invite list back only if the list was modified, or now arguments
1241 argc = silc_argument_get_arg_num(cmd->args);
1244 if (silc_argument_get_arg_type(cmd->args, 3, &len))
1247 /* Send command reply */
1248 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1249 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INVITE,
1250 SILC_STATUS_OK, 0, ident, 2,
1254 type && list ? list->len : 0);
1255 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
1256 packet->data, packet->len, FALSE);
1257 silc_buffer_free(packet);
1258 silc_buffer_free(list);
1262 silc_free(channel_id);
1263 silc_server_command_free(cmd);
1267 SilcSocketConnection sock;
1271 /* Quits connection to client. This gets called if client won't
1272 close the connection even when it has issued QUIT command. */
1274 SILC_TASK_CALLBACK(silc_server_command_quit_cb)
1276 SilcServer server = app_context;
1277 QuitInternal q = (QuitInternal)context;
1279 /* Free all client specific data, such as client entry and entires
1280 on channels this client may be on. */
1281 silc_server_free_client_data(server, q->sock, q->sock->user_data,
1283 q->sock->user_data = NULL;
1285 /* Close the connection on our side */
1286 silc_server_close_connection(server, q->sock);
1288 silc_socket_free(q->sock);
1289 silc_free(q->signoff);
1293 /* Quits SILC session. This is the normal way to disconnect client. */
1295 SILC_SERVER_CMD_FUNC(quit)
1297 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1298 SilcServer server = cmd->server;
1299 SilcSocketConnection sock = cmd->sock;
1301 unsigned char *tmp = NULL;
1304 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_QUIT, cmd, 0, 1);
1306 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT)
1310 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
1314 q = silc_calloc(1, sizeof(*q));
1315 q->sock = silc_socket_dup(sock);
1316 q->signoff = tmp ? strdup(tmp) : NULL;
1318 /* We quit the connection with little timeout */
1319 silc_schedule_task_add(server->schedule, sock->sock,
1320 silc_server_command_quit_cb, (void *)q,
1321 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
1324 silc_server_command_free(cmd);
1327 /* Server side of command KILL. This command is used by router operator
1328 to remove an client from the SILC Network temporarily. */
1330 SILC_SERVER_CMD_FUNC(kill)
1332 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1333 SilcServer server = cmd->server;
1334 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
1335 SilcClientEntry remote_client;
1336 SilcClientID *client_id = NULL;
1337 unsigned char *tmp, *comment, *auth;
1338 SilcUInt32 tmp_len, tmp_len2, auth_len;
1340 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_KILL, cmd, 1, 3);
1342 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
1345 /* Get authentication payload if present */
1346 auth = silc_argument_get_arg_type(cmd->args, 3, &auth_len);
1349 /* Router operator killing */
1351 /* KILL command works only on router */
1352 if (server->server_type != SILC_ROUTER) {
1353 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1354 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1358 /* Check whether client has the permissions. */
1359 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1360 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1361 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
1366 /* Get the client ID */
1367 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1369 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1370 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
1374 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1376 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1377 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1382 /* Get the client entry */
1383 remote_client = silc_idlist_find_client_by_id(server->local_list,
1384 client_id, TRUE, NULL);
1385 if (!remote_client) {
1386 remote_client = silc_idlist_find_client_by_id(server->global_list,
1387 client_id, TRUE, NULL);
1388 if (!remote_client) {
1389 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1390 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1397 comment = silc_argument_get_arg_type(cmd->args, 2, &tmp_len2);
1398 if (comment && tmp_len2 > 128) {
1400 comment[127] = '\0';
1403 /* If authentication data is provided then verify that killing is
1405 if (auth && auth_len) {
1406 SilcSocketConnection sock;
1408 if (!SILC_IS_LOCAL(remote_client) || !remote_client->data.public_key) {
1409 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1410 SILC_STATUS_ERR_OPERATION_ALLOWED,
1415 /* Verify the signature */
1416 if (!silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1417 remote_client->data.public_key, 0,
1418 server->sha1hash, remote_client->id,
1420 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1421 SILC_STATUS_ERR_AUTH_FAILED,
1426 /* Send reply to the sender */
1427 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1430 /* Do normal signoff for the destination client */
1431 sock = remote_client->connection;
1432 silc_server_remove_from_channels(server, NULL, remote_client,
1433 TRUE, (char *)"Killed", TRUE, TRUE);
1434 silc_server_free_client_data(server, NULL, remote_client, TRUE,
1436 (unsigned char *)"Killed");
1438 silc_server_close_connection(server, sock);
1440 /* Router operator killing */
1442 /* Send reply to the sender */
1443 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KILL,
1446 /* Check if anyone is watching this nickname */
1447 if (server->server_type == SILC_ROUTER)
1448 silc_server_check_watcher_list(server, client, NULL,
1449 SILC_NOTIFY_TYPE_KILLED);
1451 /* Now do the killing */
1452 silc_server_kill_client(server, remote_client, comment, client->id,
1457 silc_free(client_id);
1458 silc_server_command_free(cmd);
1461 /* Server side of command INFO. This sends information about us to
1462 the client. If client requested specific server we will send the
1463 command to that server. */
1465 SILC_SERVER_CMD_FUNC(info)
1467 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1468 SilcServer server = cmd->server;
1469 SilcBuffer packet, idp;
1472 char *dest_server, *server_info = NULL, *server_name;
1473 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1474 SilcServerEntry entry = NULL;
1475 SilcServerID *server_id = NULL;
1477 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_INFO, cmd, 0, 2);
1479 /* Get server name */
1480 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
1483 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
1485 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1487 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1488 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1494 /* Check whether we have this server cached */
1495 entry = silc_idlist_find_server_by_id(server->local_list,
1496 server_id, TRUE, NULL);
1498 entry = silc_idlist_find_server_by_id(server->global_list,
1499 server_id, TRUE, NULL);
1500 if (!entry && server->server_type != SILC_SERVER) {
1501 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1502 SILC_STATUS_ERR_NO_SUCH_SERVER,
1509 /* Some buggy servers has sent request to router about themselves. */
1510 if (server->server_type != SILC_SERVER && cmd->sock->user_data == entry)
1513 if ((!dest_server && !server_id && !entry) || (entry &&
1514 entry == server->id_entry) ||
1515 (dest_server && !cmd->pending &&
1516 !strncasecmp(dest_server, server->server_name, strlen(dest_server)))) {
1517 /* Send our reply */
1518 char info_string[256];
1520 memset(info_string, 0, sizeof(info_string));
1521 snprintf(info_string, sizeof(info_string),
1522 "location: %s server: %s admin: %s <%s>",
1523 server->config->server_info->location,
1524 server->config->server_info->server_type,
1525 server->config->server_info->admin,
1526 server->config->server_info->email);
1528 server_info = info_string;
1529 entry = server->id_entry;
1531 /* Check whether we have this server cached */
1532 if (!entry && dest_server) {
1533 entry = silc_idlist_find_server_by_name(server->global_list,
1534 dest_server, TRUE, NULL);
1536 entry = silc_idlist_find_server_by_name(server->local_list,
1537 dest_server, TRUE, NULL);
1541 if (!cmd->pending &&
1542 server->server_type != SILC_SERVER && entry && !entry->server_info) {
1543 /* Send to the server */
1545 SilcUInt16 old_ident;
1547 old_ident = silc_command_get_ident(cmd->payload);
1548 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1549 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1551 silc_server_packet_send(server, entry->connection,
1552 SILC_PACKET_COMMAND, cmd->packet->flags,
1553 tmpbuf->data, tmpbuf->len, TRUE);
1555 /* Reprocess this packet after received reply from router */
1556 silc_server_command_pending(server, SILC_COMMAND_INFO,
1557 silc_command_get_ident(cmd->payload),
1558 silc_server_command_info,
1559 silc_server_command_dup(cmd));
1560 cmd->pending = TRUE;
1561 silc_command_set_ident(cmd->payload, old_ident);
1562 silc_buffer_free(tmpbuf);
1566 if (!entry && !cmd->pending && !server->standalone) {
1567 /* Send to the primary router */
1569 SilcUInt16 old_ident;
1571 old_ident = silc_command_get_ident(cmd->payload);
1572 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
1573 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
1575 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1576 SILC_PACKET_COMMAND, cmd->packet->flags,
1577 tmpbuf->data, tmpbuf->len, TRUE);
1579 /* Reprocess this packet after received reply from router */
1580 silc_server_command_pending(server, SILC_COMMAND_INFO,
1581 silc_command_get_ident(cmd->payload),
1582 silc_server_command_info,
1583 silc_server_command_dup(cmd));
1584 cmd->pending = TRUE;
1585 silc_command_set_ident(cmd->payload, old_ident);
1586 silc_buffer_free(tmpbuf);
1591 silc_free(server_id);
1594 silc_server_command_send_status_reply(cmd, SILC_COMMAND_INFO,
1595 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
1599 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
1601 server_info = entry->server_info;
1602 server_name = entry->server_name;
1604 /* Send the reply */
1605 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_INFO,
1606 SILC_STATUS_OK, 0, ident, 3,
1607 2, idp->data, idp->len,
1609 strlen(server_name),
1612 strlen(server_info) : 0);
1613 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
1614 packet->data, packet->len, FALSE);
1616 silc_buffer_free(packet);
1617 silc_buffer_free(idp);
1620 silc_server_command_free(cmd);
1623 /* Server side of command PING. This just replies to the ping. */
1625 SILC_SERVER_CMD_FUNC(ping)
1627 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1628 SilcServer server = cmd->server;
1631 SilcServerID *server_id = NULL;
1633 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PING, cmd, 1, 1);
1636 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1638 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1639 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1642 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1646 if (SILC_ID_SERVER_COMPARE(server_id, server->id)) {
1647 /* Send our reply */
1648 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1651 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1652 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
1657 silc_free(server_id);
1658 silc_server_command_free(cmd);
1661 /* Server side of command STATS. */
1663 SILC_SERVER_CMD_FUNC(stats)
1665 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
1666 SilcServer server = cmd->server;
1667 SilcServerID *server_id;
1670 SilcBuffer packet, stats;
1671 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1674 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_STATS, cmd, 1, 1);
1677 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
1679 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1680 SILC_STATUS_ERR_NO_SERVER_ID, 0);
1683 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1687 /* The ID must be ours */
1688 if (!SILC_ID_SERVER_COMPARE(server->id, server_id)) {
1689 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PING,
1690 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
1691 silc_free(server_id);
1694 silc_free(server_id);
1696 /* If we are router then just send everything we got. If we are normal
1697 server then we'll send this to our router to get all the latest
1698 statistical information. */
1699 if (!cmd->pending && server->server_type != SILC_ROUTER &&
1700 !server->standalone) {
1701 /* Send request to our router */
1702 SilcBuffer idp = silc_id_payload_encode(server->router->id,
1704 packet = silc_command_payload_encode_va(SILC_COMMAND_STATS,
1705 ++server->cmd_ident, 1,
1706 1, idp->data, idp->len);
1707 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
1708 SILC_PACKET_COMMAND, 0, packet->data,
1709 packet->len, FALSE);
1711 /* Reprocess this packet after received reply from router */
1712 silc_server_command_pending(server, SILC_COMMAND_STATS,
1714 silc_server_command_stats,
1715 silc_server_command_dup(cmd));
1716 cmd->pending = TRUE;
1717 silc_buffer_free(packet);
1718 silc_buffer_free(idp);
1722 /* Send our reply to sender */
1723 uptime = time(NULL) - server->starttime;
1725 stats = silc_buffer_alloc_size(60);
1726 silc_buffer_format(stats,
1727 SILC_STR_UI_INT(server->starttime),
1728 SILC_STR_UI_INT(uptime),
1729 SILC_STR_UI_INT(server->stat.my_clients),
1730 SILC_STR_UI_INT(server->stat.my_channels),
1731 SILC_STR_UI_INT(server->stat.my_server_ops),
1732 SILC_STR_UI_INT(server->stat.my_router_ops),
1733 SILC_STR_UI_INT(server->stat.cell_clients),
1734 SILC_STR_UI_INT(server->stat.cell_channels),
1735 SILC_STR_UI_INT(server->stat.cell_servers),
1736 SILC_STR_UI_INT(server->stat.clients),
1737 SILC_STR_UI_INT(server->stat.channels),
1738 SILC_STR_UI_INT(server->stat.servers),
1739 SILC_STR_UI_INT(server->stat.routers),
1740 SILC_STR_UI_INT(server->stat.server_ops),
1741 SILC_STR_UI_INT(server->stat.router_ops),
1744 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_STATS,
1745 SILC_STATUS_OK, 0, ident, 2,
1747 3, stats->data, stats->len);
1748 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY,
1749 0, packet->data, packet->len, FALSE);
1750 silc_buffer_free(packet);
1751 silc_buffer_free(stats);
1754 silc_server_command_free(cmd);
1757 /* Internal routine to join channel. The channel sent to this function
1758 has been either created or resolved from ID lists. This joins the sent
1759 client to the channel. */
1761 static void silc_server_command_join_channel(SilcServer server,
1762 SilcServerCommandContext cmd,
1763 SilcChannelEntry channel,
1764 SilcClientID *client_id,
1768 const unsigned char *auth,
1769 SilcUInt32 auth_len)
1771 SilcSocketConnection sock = cmd->sock;
1773 SilcUInt32 tmp_len, user_count;
1774 unsigned char *passphrase = NULL, mode[4], tmp2[4], tmp3[4];
1775 SilcClientEntry client;
1776 SilcChannelClientEntry chl;
1777 SilcBuffer reply, chidp, clidp, keyp = NULL;
1778 SilcBuffer user_list, mode_list, invite_list, ban_list;
1779 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
1780 char check[512], check2[512];
1781 bool founder = FALSE;
1783 SilcBuffer fkey = NULL;
1786 SILC_LOG_DEBUG(("Joining client to channel"));
1791 /* Get the client entry */
1792 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
1793 client = (SilcClientEntry)sock->user_data;
1797 client = silc_server_query_client(server, client_id, FALSE,
1800 if (!resolve || cmd->pending) {
1801 silc_server_command_send_status_reply(
1802 cmd, SILC_COMMAND_JOIN,
1803 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
1807 /* The client info is being resolved. Reprocess this packet after
1808 receiving the reply to the query. */
1809 silc_server_command_pending(server, SILC_COMMAND_WHOIS,
1811 silc_server_command_join,
1812 silc_server_command_dup(cmd));
1813 cmd->pending = TRUE;
1817 if (auth && auth_len && !client->data.public_key) {
1818 if (cmd->pending == 2)
1821 /* We must retrieve the detached client's public key by sending
1822 GETKEY command. Reprocess this packet after receiving the key */
1823 clidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1824 silc_server_send_command(server, cmd->sock,
1825 SILC_COMMAND_GETKEY, ++server->cmd_ident,
1826 1, 1, clidp->data, clidp->len);
1827 silc_buffer_free(clidp);
1828 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
1830 silc_server_command_join,
1831 silc_server_command_dup(cmd));
1836 cmd->pending = FALSE;
1840 * Check founder auth payload if provided. If client can gain founder
1841 * privileges it can override various conditions on joining the channel,
1842 * and can have directly the founder mode set on the channel.
1844 if (auth && auth_len && channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
1845 SilcIDListData idata = (SilcIDListData)client;
1846 SilcChannelClientEntry chl2;
1847 SilcHashTableList htl;
1849 if (channel->founder_key && idata->public_key &&
1850 silc_pkcs_public_key_compare(channel->founder_key,
1851 idata->public_key)) {
1852 /* Check whether the client is to become founder */
1853 if (silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
1854 channel->founder_key, 0, server->sha1hash,
1855 client->id, SILC_ID_CLIENT)) {
1857 /* There cannot be anyone else as founder on the channel now. This
1858 client is definitely the founder due to this authentication */
1859 silc_hash_table_list(channel->user_list, &htl);
1860 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
1861 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
1862 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
1863 silc_server_force_cumode_change(server, NULL, channel, chl2,
1867 silc_hash_table_list_reset(&htl);
1869 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
1876 * Check channel modes
1880 memset(check, 0, sizeof(check));
1881 memset(check2, 0, sizeof(check2));
1882 silc_strncat(check, sizeof(check),
1883 client->nickname, strlen(client->nickname));
1884 silc_strncat(check, sizeof(check), "!", 1);
1885 silc_strncat(check, sizeof(check),
1886 client->username, strlen(client->username));
1887 if (!strchr(client->username, '@')) {
1888 silc_strncat(check, sizeof(check), "@", 1);
1889 silc_strncat(check, sizeof(check),
1890 cmd->sock->hostname, strlen(cmd->sock->hostname));
1893 silc_strncat(check2, sizeof(check2),
1894 client->nickname, strlen(client->nickname));
1895 if (!strchr(client->nickname, '@')) {
1896 silc_strncat(check2, sizeof(check2), "@", 1);
1897 silc_strncat(check2, sizeof(check2),
1898 server->server_name, strlen(server->server_name));
1900 silc_strncat(check2, sizeof(check2), "!", 1);
1901 silc_strncat(check2, sizeof(check2),
1902 client->username, strlen(client->username));
1903 if (!strchr(client->username, '@')) {
1904 silc_strncat(check2, sizeof(check2), "@", 1);
1905 silc_strncat(check2, sizeof(check2),
1906 cmd->sock->hostname, strlen(cmd->sock->hostname));
1909 /* Check invite list if channel is invite-only channel */
1910 if (channel->mode & SILC_CHANNEL_MODE_INVITE) {
1911 if (!channel->invite_list ||
1912 !silc_hash_table_count(channel->invite_list) ||
1913 (!silc_server_inviteban_match(server, channel->invite_list,
1915 !silc_server_inviteban_match(server, channel->invite_list,
1916 2, client->data.public_key) &&
1917 !silc_server_inviteban_match(server, channel->invite_list,
1919 !silc_server_inviteban_match(server, channel->invite_list,
1921 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1922 SILC_STATUS_ERR_NOT_INVITED, 0);
1927 /* Check ban list if it exists. If the client's nickname, server,
1928 username and/or hostname is in the ban list the access to the
1929 channel is denied. */
1930 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
1931 if (silc_server_inviteban_match(server, channel->ban_list,
1933 silc_server_inviteban_match(server, channel->ban_list,
1934 2, client->data.public_key) ||
1935 silc_server_inviteban_match(server, channel->ban_list,
1937 silc_server_inviteban_match(server, channel->ban_list,
1939 silc_server_command_send_status_reply(
1940 cmd, SILC_COMMAND_JOIN,
1941 SILC_STATUS_ERR_BANNED_FROM_CHANNEL, 0);
1946 /* Check user count limit if set. */
1947 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT) {
1948 if (silc_hash_table_count(channel->user_list) + 1 >
1949 channel->user_limit) {
1950 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1951 SILC_STATUS_ERR_CHANNEL_IS_FULL,
1958 /* Check the channel passphrase if set. */
1959 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
1960 /* Get passphrase */
1961 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
1963 passphrase = silc_memdup(tmp, tmp_len);
1965 if (!passphrase || !channel->passphrase ||
1966 memcmp(passphrase, channel->passphrase, strlen(channel->passphrase))) {
1967 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1968 SILC_STATUS_ERR_BAD_PASSWORD, 0);
1974 * Client is allowed to join to the channel. Make it happen.
1977 /* Check whether the client already is on the channel */
1978 if (silc_server_client_on_channel(client, channel, NULL)) {
1979 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
1980 SILC_STATUS_ERR_USER_ON_CHANNEL, 0);
1984 /* Generate new channel key as protocol dictates */
1986 if (!silc_server_create_channel_key(server, channel, 0))
1989 /* Send the channel key. This is broadcasted to the channel but is not
1990 sent to the client who is joining to the channel. */
1991 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
1992 silc_server_send_channel_key(server, NULL, channel,
1993 server->server_type == SILC_ROUTER ?
1994 FALSE : !server->standalone);
1997 /* Join the client to the channel by adding it to channel's user list.
1998 Add also the channel to client entry's channels list for fast cross-
2000 chl = silc_calloc(1, sizeof(*chl));
2002 chl->client = client;
2003 chl->channel = channel;
2004 silc_hash_table_add(channel->user_list, client, chl);
2005 silc_hash_table_add(client->channels, channel, chl);
2006 channel->user_count++;
2007 channel->disabled = FALSE;
2009 /* Get users on the channel */
2010 silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
2013 /* Encode Client ID Payload of the original client who wants to join */
2014 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2016 /* Encode command reply packet */
2017 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
2018 SILC_PUT32_MSB(channel->mode, mode);
2019 SILC_PUT32_MSB(created, tmp2);
2020 SILC_PUT32_MSB(user_count, tmp3);
2022 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2023 tmp = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2024 cipher = silc_cipher_get_name(channel->channel_key);
2025 keyp = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
2028 strlen(cipher), cipher,
2029 channel->key_len / 8, channel->key);
2033 if (channel->founder_key)
2034 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
2036 /* Encode invite list */
2038 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
2039 SilcHashTableList htl;
2041 invite_list = silc_buffer_alloc_size(2);
2042 silc_buffer_format(invite_list,
2043 SILC_STR_UI_SHORT(silc_hash_table_count(
2044 channel->invite_list)),
2047 silc_hash_table_list(channel->invite_list, &htl);
2048 while (silc_hash_table_get(&htl, (void **)&tmp_len, (void **)&reply))
2049 invite_list = silc_argument_payload_encode_one(invite_list,
2051 reply->len, tmp_len);
2052 silc_hash_table_list_reset(&htl);
2055 /* Encode ban list */
2057 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
2058 SilcHashTableList htl;
2060 ban_list = silc_buffer_alloc_size(2);
2061 silc_buffer_format(ban_list,
2062 SILC_STR_UI_SHORT(silc_hash_table_count(
2063 channel->ban_list)),
2066 silc_hash_table_list(channel->ban_list, &htl);
2067 while (silc_hash_table_get(&htl, (void **)&tmp_len, (void **)&reply))
2068 ban_list = silc_argument_payload_encode_one(ban_list,
2070 reply->len, tmp_len);
2071 silc_hash_table_list_reset(&htl);
2075 silc_command_reply_payload_encode_va(SILC_COMMAND_JOIN,
2076 SILC_STATUS_OK, 0, ident, 14,
2077 2, channel->channel_name,
2078 strlen(channel->channel_name),
2079 3, chidp->data, chidp->len,
2080 4, clidp->data, clidp->len,
2083 7, keyp ? keyp->data : NULL,
2084 keyp ? keyp->len : 0,
2085 8, ban_list ? ban_list->data : NULL,
2086 ban_list ? ban_list->len : 0,
2087 9, invite_list ? invite_list->data :
2089 invite_list ? invite_list->len : 0,
2092 strlen(channel->topic) : 0,
2093 11, silc_hmac_get_name(channel->hmac),
2094 strlen(silc_hmac_get_name(channel->
2097 13, user_list->data, user_list->len,
2098 14, mode_list->data,
2100 15, fkey ? fkey->data : NULL,
2101 fkey ? fkey->len : 0);
2103 /* Send command reply */
2104 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
2105 reply->data, reply->len, FALSE);
2107 /* Send JOIN notify to locally connected clients on the channel. If
2108 we are normal server then router will send or have sent JOIN notify
2109 already. However since we've added the client already to our channel
2110 we'll ignore it (in packet_receive.c) so we must send it here. If
2111 we are router then this will send it to local clients and local
2113 SILC_LOG_DEBUG(("Send JOIN notify to channel"));
2114 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2115 SILC_NOTIFY_TYPE_JOIN, 2,
2116 clidp->data, clidp->len,
2117 chidp->data, chidp->len);
2119 /* Update statistics */
2120 server->stat.my_chanclients++;
2121 if (server->server_type == SILC_ROUTER) {
2122 server->stat.cell_chanclients++;
2123 server->stat.chanclients++;
2126 if (!cmd->pending) {
2127 /* Send JOIN notify packet to our primary router */
2128 silc_server_send_notify_join(server, SILC_PRIMARY_ROUTE(server),
2129 SILC_BROADCAST(server), channel, client->id);
2132 /* Distribute the channel key to all backup routers. */
2133 silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
2134 keyp->data, keyp->len, FALSE, TRUE);
2136 /* If client became founder by providing correct founder auth data
2137 notify the mode change to the channel. */
2139 SILC_PUT32_MSB(chl->mode, mode);
2140 SILC_LOG_DEBUG(("Send CUMODE_CHANGE notify to channel"));
2141 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2142 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
2143 clidp->data, clidp->len,
2144 mode, 4, clidp->data, clidp->len,
2145 fkey ? fkey->data : NULL,
2146 fkey ? fkey->len : 0);
2150 /* Set CUMODE notify type to network */
2152 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
2153 SILC_BROADCAST(server), channel,
2154 chl->mode, client->id, SILC_ID_CLIENT,
2155 client->id, channel->founder_key);
2157 silc_buffer_free(reply);
2158 silc_buffer_free(clidp);
2159 silc_buffer_free(chidp);
2160 silc_buffer_free(keyp);
2161 silc_buffer_free(user_list);
2162 silc_buffer_free(mode_list);
2163 silc_buffer_free(fkey);
2164 silc_buffer_free(invite_list);
2165 silc_buffer_free(ban_list);
2169 memset(passphrase, 0, strlen(passphrase));
2170 silc_free(passphrase);
2173 /* Server side of command JOIN. Joins client into requested channel. If
2174 the channel does not exist it will be created. */
2176 SILC_SERVER_CMD_FUNC(join)
2178 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2179 SilcServer server = cmd->server;
2180 unsigned char *auth;
2181 SilcUInt32 tmp_len, auth_len;
2182 char *tmp, *channel_name = NULL, *cipher, *hmac;
2183 SilcChannelEntry channel;
2184 SilcUInt32 umode = 0;
2185 bool created = FALSE, create_key = TRUE;
2186 SilcClientID *client_id;
2188 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_JOIN, cmd, 2, 6);
2190 /* Get channel name */
2191 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
2193 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2194 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2201 channel_name[255] = '\0';
2203 if (silc_server_name_bad_chars(channel_name, tmp_len) == TRUE) {
2204 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2205 SILC_STATUS_ERR_BAD_CHANNEL, 0);
2209 /* Get Client ID of the client who is joining to the channel */
2210 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2212 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2213 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2217 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
2219 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2220 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2225 /* Get cipher, hmac name and auth payload */
2226 cipher = silc_argument_get_arg_type(cmd->args, 4, NULL);
2227 hmac = silc_argument_get_arg_type(cmd->args, 5, NULL);
2228 auth = silc_argument_get_arg_type(cmd->args, 6, &auth_len);
2230 /* See if the channel exists */
2231 channel = silc_idlist_find_channel_by_name(server->local_list,
2232 channel_name, NULL);
2234 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
2235 SilcClientEntry entry = (SilcClientEntry)cmd->sock->user_data;
2237 silc_server_command_send_status_reply(cmd, SILC_COMMAND_JOIN,
2238 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
2243 silc_free(client_id);
2244 client_id = silc_id_dup(entry->id, SILC_ID_CLIENT);
2247 (channel->disabled && server->server_type != SILC_ROUTER)) {
2248 /* Channel not found or not valid */
2250 /* If we are standalone server we don't have a router, we just create
2251 the channel by ourselves (unless it existed). */
2252 if (server->standalone) {
2254 channel = silc_server_create_new_channel(server, server->id, cipher,
2255 hmac, channel_name, TRUE);
2257 silc_server_command_send_status_reply(
2258 cmd, SILC_COMMAND_JOIN,
2259 SILC_STATUS_ERR_UNKNOWN_ALGORITHM,
2261 silc_free(client_id);
2265 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2271 /* The channel does not exist on our server. If we are normal server
2272 we will send JOIN command to our router which will handle the
2273 joining procedure (either creates the channel if it doesn't exist
2274 or joins the client to it). */
2275 if (server->server_type != SILC_ROUTER) {
2277 SilcUInt16 old_ident;
2279 /* If this is pending command callback then we've resolved
2280 it and it didn't work, return since we've notified the
2281 client already in the command reply callback. */
2283 silc_free(client_id);
2287 old_ident = silc_command_get_ident(cmd->payload);
2288 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2289 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2291 /* Send JOIN command to our router */
2292 silc_server_packet_send(server, (SilcSocketConnection)
2293 SILC_PRIMARY_ROUTE(server),
2294 SILC_PACKET_COMMAND, cmd->packet->flags,
2295 tmpbuf->data, tmpbuf->len, TRUE);
2297 /* Reprocess this packet after received reply from router */
2298 silc_server_command_pending(server, SILC_COMMAND_JOIN,
2299 silc_command_get_ident(cmd->payload),
2300 silc_server_command_join,
2301 silc_server_command_dup(cmd));
2302 cmd->pending = TRUE;
2303 silc_command_set_ident(cmd->payload, old_ident);
2304 silc_buffer_free(tmpbuf);
2305 silc_free(client_id);
2309 /* We are router and the channel does not seem exist so we will check
2310 our global list as well for the channel. */
2311 channel = silc_idlist_find_channel_by_name(server->global_list,
2312 channel_name, NULL);
2314 /* Channel really does not exist, create it */
2315 channel = silc_server_create_new_channel(server, server->id, cipher,
2316 hmac, channel_name, TRUE);
2318 silc_server_command_send_status_reply(
2319 cmd, SILC_COMMAND_JOIN,
2320 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2321 silc_free(client_id);
2325 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2333 /* Channel not found */
2335 /* If the command came from router and we are normal server then
2336 something went wrong with the joining as the channel was not found.
2337 We can't do anything else but ignore this. */
2338 if (cmd->sock->type == SILC_SOCKET_TYPE_ROUTER ||
2339 server->server_type != SILC_ROUTER) {
2340 silc_free(client_id);
2344 /* We are router and the channel does not seem exist so we will check
2345 our global list as well for the channel. */
2346 channel = silc_idlist_find_channel_by_name(server->global_list,
2347 channel_name, NULL);
2349 /* Channel really does not exist, create it */
2350 channel = silc_server_create_new_channel(server, server->id, cipher,
2351 hmac, channel_name, TRUE);
2353 silc_server_command_send_status_reply(
2354 cmd, SILC_COMMAND_JOIN,
2355 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2356 silc_free(client_id);
2360 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2367 /* Check whether the channel was created by our router */
2368 if (cmd->pending && context2) {
2369 SilcServerCommandReplyContext reply = context2;
2371 if (silc_command_get(reply->payload) == SILC_COMMAND_JOIN) {
2372 tmp = silc_argument_get_arg_type(reply->args, 6, NULL);
2373 SILC_GET32_MSB(created, tmp);
2374 if (silc_argument_get_arg_type(reply->args, 7, NULL))
2375 create_key = FALSE; /* Router returned the key already */
2377 if (silc_command_get_status(reply->payload, NULL, NULL) &&
2378 channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2379 /* Save channel passphrase, if user provided it successfully */
2382 pa = silc_argument_get_arg_type(cmd->args, 3, &pa_len);
2384 silc_free(channel->passphrase);
2385 channel->passphrase = silc_memdup(pa, pa_len);
2390 if (silc_command_get(reply->payload) == SILC_COMMAND_WHOIS &&
2391 !channel->disabled && !silc_hash_table_count(channel->user_list))
2395 /* If the channel does not have global users and is also empty the client
2396 will be the channel founder and operator. */
2397 if (!channel->disabled &&
2398 !channel->global_users && !silc_hash_table_count(channel->user_list))
2399 umode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
2401 /* Join to the channel */
2402 silc_server_command_join_channel(server, cmd, channel, client_id,
2403 created, create_key, umode,
2406 silc_free(client_id);
2409 silc_server_command_free(cmd);
2412 /* Server side of command MOTD. Sends server's current "message of the
2413 day" to the client. */
2415 SILC_SERVER_CMD_FUNC(motd)
2417 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2418 SilcServer server = cmd->server;
2419 SilcBuffer packet, idp;
2420 char *motd, *dest_server;
2421 SilcUInt32 motd_len;
2422 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2424 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_MOTD, cmd, 1, 1);
2426 /* Get server name */
2427 dest_server = silc_argument_get_arg_type(cmd->args, 1, NULL);
2429 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2430 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
2434 if (!strncasecmp(dest_server, server->server_name, strlen(dest_server))) {
2437 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
2439 if (server->config && server->config->server_info &&
2440 server->config->server_info->motd_file) {
2442 motd = silc_file_readfile(server->config->server_info->motd_file,
2448 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2455 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2461 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2462 packet->data, packet->len, FALSE);
2463 silc_buffer_free(packet);
2464 silc_buffer_free(idp);
2466 SilcServerEntry entry;
2468 /* Check whether we have this server cached */
2469 entry = silc_idlist_find_server_by_name(server->global_list,
2470 dest_server, TRUE, NULL);
2472 entry = silc_idlist_find_server_by_name(server->local_list,
2473 dest_server, TRUE, NULL);
2476 if (server->server_type != SILC_SERVER && !cmd->pending &&
2477 entry && !entry->motd) {
2478 /* Send to the server */
2480 SilcUInt16 old_ident;
2482 old_ident = silc_command_get_ident(cmd->payload);
2483 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2484 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2486 silc_server_packet_send(server, entry->connection,
2487 SILC_PACKET_COMMAND, cmd->packet->flags,
2488 tmpbuf->data, tmpbuf->len, TRUE);
2490 /* Reprocess this packet after received reply from router */
2491 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2492 silc_command_get_ident(cmd->payload),
2493 silc_server_command_motd,
2494 silc_server_command_dup(cmd));
2495 cmd->pending = TRUE;
2496 silc_command_set_ident(cmd->payload, old_ident);
2497 silc_buffer_free(tmpbuf);
2501 if (!entry && !cmd->pending && !server->standalone) {
2502 /* Send to the primary router */
2504 SilcUInt16 old_ident;
2506 old_ident = silc_command_get_ident(cmd->payload);
2507 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
2508 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
2510 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2511 SILC_PACKET_COMMAND, cmd->packet->flags,
2512 tmpbuf->data, tmpbuf->len, TRUE);
2514 /* Reprocess this packet after received reply from router */
2515 silc_server_command_pending(server, SILC_COMMAND_MOTD,
2516 silc_command_get_ident(cmd->payload),
2517 silc_server_command_motd,
2518 silc_server_command_dup(cmd));
2519 cmd->pending = TRUE;
2520 silc_command_set_ident(cmd->payload, old_ident);
2521 silc_buffer_free(tmpbuf);
2526 silc_server_command_send_status_reply(cmd, SILC_COMMAND_MOTD,
2527 SILC_STATUS_ERR_NO_SUCH_SERVER, 0);
2531 idp = silc_id_payload_encode(server->id_entry->id, SILC_ID_SERVER);
2532 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_MOTD,
2533 SILC_STATUS_OK, 0, ident, 2,
2537 strlen(entry->motd) : 0);
2538 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2539 packet->data, packet->len, FALSE);
2540 silc_buffer_free(packet);
2541 silc_buffer_free(idp);
2545 silc_server_command_free(cmd);
2548 /* Server side of command UMODE. Client can use this command to set/unset
2549 user mode. Client actually cannot set itself to be as server/router
2550 operator so this can be used only to unset the modes. */
2552 SILC_SERVER_CMD_FUNC(umode)
2554 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2555 SilcServer server = cmd->server;
2556 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2558 unsigned char *tmp_mask, m[4];
2559 SilcUInt32 mask = 0;
2560 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2561 bool set_mask = FALSE;
2563 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
2566 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_UMODE, cmd, 1, 2);
2568 /* Get the client's mode mask */
2569 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
2571 SILC_GET32_MSB(mask, tmp_mask);
2576 /* Check that mode changing is allowed. */
2577 if (!silc_server_check_umode_rights(server, client, mask)) {
2578 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2579 SILC_STATUS_ERR_PERM_DENIED, 0);
2583 /* Anonymous mode cannot be set by client */
2584 if (mask & SILC_UMODE_ANONYMOUS) {
2585 if (!(client->mode & SILC_UMODE_ANONYMOUS)) {
2586 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2587 SILC_STATUS_ERR_PERM_DENIED, 0);
2591 if (client->mode & SILC_UMODE_ANONYMOUS) {
2592 silc_server_command_send_status_reply(cmd, SILC_COMMAND_UMODE,
2593 SILC_STATUS_ERR_PERM_DENIED, 0);
2598 /* Update statistics */
2599 if (mask & SILC_UMODE_GONE) {
2600 if (!(client->mode & SILC_UMODE_GONE))
2601 server->stat.my_aways++;
2603 if (client->mode & SILC_UMODE_GONE)
2604 server->stat.my_aways--;
2607 /* Change the mode */
2608 client->mode = mask;
2610 /* Send UMODE change to primary router */
2611 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
2612 SILC_BROADCAST(server), client->id,
2615 /* Check if anyone is watching this nickname */
2616 if (server->server_type == SILC_ROUTER)
2617 silc_server_check_watcher_list(server, client, NULL,
2618 SILC_NOTIFY_TYPE_UMODE_CHANGE);
2621 /* Send command reply to sender */
2622 SILC_PUT32_MSB(client->mode, m);
2623 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_UMODE,
2624 SILC_STATUS_OK, 0, ident, 1,
2626 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2627 packet->data, packet->len, FALSE);
2628 silc_buffer_free(packet);
2631 silc_server_command_free(cmd);
2634 /* Server side command of CMODE. Changes channel mode */
2636 SILC_SERVER_CMD_FUNC(cmode)
2638 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
2639 SilcServer server = cmd->server;
2640 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
2641 SilcIDListData idata = (SilcIDListData)client;
2642 SilcChannelID *channel_id = NULL;
2643 SilcChannelEntry channel;
2644 SilcChannelClientEntry chl;
2645 SilcBuffer packet, cidp;
2646 unsigned char *tmp, *tmp_id, *tmp_mask;
2647 char *cipher = NULL, *hmac = NULL, *passphrase = NULL;
2648 SilcUInt32 mode_mask = 0, old_mask = 0, tmp_len, tmp_len2;
2649 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
2650 bool set_mask = FALSE;
2651 SilcPublicKey founder_key = NULL;
2652 SilcBuffer fkey = NULL;
2655 silc_server_command_free(cmd);
2659 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CMODE, cmd, 1, 8);
2661 /* Get Channel ID */
2662 tmp_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_len2);
2664 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2665 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2666 silc_server_command_free(cmd);
2669 channel_id = silc_id_payload_parse_id(tmp_id, tmp_len2, NULL);
2671 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2672 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
2673 silc_server_command_free(cmd);
2677 /* Get channel entry */
2678 channel = silc_idlist_find_channel_by_id(server->local_list,
2681 channel = silc_idlist_find_channel_by_id(server->global_list,
2684 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2685 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
2687 silc_free(channel_id);
2688 silc_server_command_free(cmd);
2692 old_mask = channel->mode;
2694 /* Get the channel mode mask */
2695 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
2697 SILC_GET32_MSB(mode_mask, tmp_mask);
2701 /* Check whether this client is on the channel */
2702 if (!silc_server_client_on_channel(client, channel, &chl)) {
2703 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2704 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
2708 /* Check that client has rights to change any requested channel modes */
2709 if (set_mask && !silc_server_check_cmode_rights(server, channel, chl,
2711 SILC_LOG_DEBUG(("Client does not have rights to change mode"));
2712 silc_server_command_send_status_reply(
2713 cmd, SILC_COMMAND_CMODE,
2714 (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) ?
2715 SILC_STATUS_ERR_NO_CHANNEL_PRIV :
2716 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV), 0);
2720 /* If mode mask was not sent as argument then merely return the current
2721 mode mask to the sender. */
2724 SILC_PUT32_MSB(channel->mode, m);
2725 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
2726 SILC_STATUS_OK, 0, ident, 2,
2727 2, tmp_id, tmp_len2,
2729 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
2730 packet->data, packet->len, FALSE);
2731 silc_buffer_free(packet);
2736 * Check the modes. Modes that requires nothing special operation are
2740 if (mode_mask & SILC_CHANNEL_MODE_PRIVKEY) {
2741 /* Channel uses private keys to protect traffic. Client(s) has set the
2742 key locally they want to use, server does not know that key. */
2743 /* Nothing interesting to do here */
2745 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
2746 /* The mode is removed and we need to generate and distribute
2747 new channel key. Clients are not using private channel keys
2748 anymore after this. */
2750 /* Re-generate channel key */
2751 if (!silc_server_create_channel_key(server, channel, 0))
2754 /* Send the channel key. This sends it to our local clients and if
2755 we are normal server to our router as well. */
2756 silc_server_send_channel_key(server, NULL, channel,
2757 server->server_type == SILC_ROUTER ?
2758 FALSE : !server->standalone);
2760 cipher = (char *)silc_cipher_get_name(channel->channel_key);
2761 hmac = (char *)silc_hmac_get_name(channel->hmac);
2765 if (mode_mask & SILC_CHANNEL_MODE_ULIMIT) {
2766 /* User limit is set on channel */
2767 SilcUInt32 user_limit;
2769 /* Get user limit */
2770 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
2772 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT)) {
2773 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2774 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2778 SILC_GET32_MSB(user_limit, tmp);
2779 channel->user_limit = user_limit;
2782 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
2783 /* User limit mode is unset. Remove user limit */
2784 channel->user_limit = 0;
2787 if (mode_mask & SILC_CHANNEL_MODE_PASSPHRASE) {
2788 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
2789 /* Passphrase has been set to channel */
2791 /* Get the passphrase */
2792 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
2794 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2795 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2799 /* Save the passphrase */
2800 passphrase = channel->passphrase = silc_memdup(tmp, strlen(tmp));
2803 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
2804 /* Passphrase mode is unset. remove the passphrase */
2805 silc_free(channel->passphrase);
2806 channel->passphrase = NULL;
2810 if (mode_mask & SILC_CHANNEL_MODE_CIPHER) {
2811 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
2812 /* Cipher to use protect the traffic */
2813 SilcCipher newkey, oldkey;
2816 cipher = silc_argument_get_arg_type(cmd->args, 5, NULL);
2818 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2819 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2823 /* Delete old cipher and allocate the new one */
2824 if (!silc_cipher_alloc(cipher, &newkey)) {
2825 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2826 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2830 oldkey = channel->channel_key;
2831 channel->channel_key = newkey;
2833 /* Re-generate channel key */
2834 if (!silc_server_create_channel_key(server, channel, 0)) {
2835 /* We don't have new key, revert to old one */
2836 channel->channel_key = oldkey;
2840 /* Remove old channel key for good */
2841 silc_cipher_free(oldkey);
2843 /* Send the channel key. This sends it to our local clients and if
2844 we are normal server to our router as well. */
2845 silc_server_send_channel_key(server, NULL, channel,
2846 server->server_type == SILC_ROUTER ?
2847 FALSE : !server->standalone);
2850 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
2851 /* Cipher mode is unset. Remove the cipher and revert back to
2853 SilcCipher newkey, oldkey;
2854 cipher = channel->cipher;
2856 /* Delete old cipher and allocate default one */
2857 if (!silc_cipher_alloc(cipher ? cipher : SILC_DEFAULT_CIPHER, &newkey)) {
2858 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2859 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2863 oldkey = channel->channel_key;
2864 channel->channel_key = newkey;
2866 /* Re-generate channel key */
2867 if (!silc_server_create_channel_key(server, channel, 0)) {
2868 /* We don't have new key, revert to old one */
2869 channel->channel_key = oldkey;
2873 /* Remove old channel key for good */
2874 silc_cipher_free(oldkey);
2876 /* Send the channel key. This sends it to our local clients and if
2877 we are normal server to our router as well. */
2878 silc_server_send_channel_key(server, NULL, channel,
2879 server->server_type == SILC_ROUTER ?
2880 FALSE : !server->standalone);
2884 if (mode_mask & SILC_CHANNEL_MODE_HMAC) {
2885 if (!(channel->mode & SILC_CHANNEL_MODE_HMAC)) {
2886 /* HMAC to use protect the traffic */
2887 unsigned char hash[32];
2891 hmac = silc_argument_get_arg_type(cmd->args, 6, NULL);
2893 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2894 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2898 /* Delete old hmac and allocate the new one */
2899 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2900 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2901 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2905 silc_hmac_free(channel->hmac);
2906 channel->hmac = newhmac;
2908 /* Set the HMAC key out of current channel key. The client must do
2910 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
2911 channel->key_len / 8, hash);
2912 silc_hmac_set_key(channel->hmac, hash,
2913 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
2914 memset(hash, 0, sizeof(hash));
2917 if (channel->mode & SILC_CHANNEL_MODE_HMAC) {
2918 /* Hmac mode is unset. Remove the hmac and revert back to
2921 unsigned char hash[32];
2922 hmac = channel->hmac_name;
2924 /* Delete old hmac and allocate default one */
2925 if (!silc_hmac_alloc(hmac ? hmac : SILC_DEFAULT_HMAC, NULL, &newhmac)) {
2926 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2927 SILC_STATUS_ERR_UNKNOWN_ALGORITHM, 0);
2931 silc_hmac_free(channel->hmac);
2932 channel->hmac = newhmac;
2934 /* Set the HMAC key out of current channel key. The client must do
2936 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
2937 channel->key_len / 8,
2939 silc_hmac_set_key(channel->hmac, hash,
2940 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
2941 memset(hash, 0, sizeof(hash));
2945 if (mode_mask & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
2946 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
2947 /* Check if the founder public key was received */
2948 founder_key = idata->public_key;
2949 tmp = silc_argument_get_arg_type(cmd->args, 8, &tmp_len);
2951 if (!silc_pkcs_public_key_payload_decode(tmp, tmp_len, &founder_key)) {
2952 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2953 SILC_STATUS_ERR_AUTH_FAILED,
2958 /* If key was not sent and the channel mode has already founder
2959 then the key was not to be changed. */
2960 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)
2964 /* Set the founder authentication */
2965 tmp = silc_argument_get_arg_type(cmd->args, 7, &tmp_len);
2967 silc_server_command_send_status_reply(
2968 cmd, SILC_COMMAND_CMODE,
2969 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS, 0);
2973 /* Verify the payload before setting the mode */
2974 if (!silc_auth_verify_data(tmp, tmp_len, SILC_AUTH_PUBLIC_KEY,
2975 founder_key, 0, server->sha1hash,
2976 client->id, SILC_ID_CLIENT)) {
2977 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2978 SILC_STATUS_ERR_AUTH_FAILED,
2983 /* Save the public key */
2984 if (channel->founder_key)
2985 silc_pkcs_public_key_free(channel->founder_key);
2986 if (silc_argument_get_arg_type(cmd->args, 8, NULL))
2987 channel->founder_key = founder_key;
2989 channel->founder_key = silc_pkcs_public_key_copy(founder_key);
2990 if (!channel->founder_key) {
2991 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
2992 SILC_STATUS_ERR_AUTH_FAILED,
2997 fkey = silc_pkcs_public_key_payload_encode(channel->founder_key);
2999 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CMODE,
3000 SILC_STATUS_ERR_AUTH_FAILED,
3002 silc_pkcs_public_key_free(channel->founder_key);
3003 channel->founder_key = NULL;
3009 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3010 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
3011 if (channel->founder_key)
3012 silc_pkcs_public_key_free(channel->founder_key);
3013 channel->founder_key = NULL;
3018 /* Finally, set the mode */
3019 old_mask = channel->mode = mode_mask;
3021 /* Send CMODE_CHANGE notify. */
3022 cidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3023 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3024 SILC_NOTIFY_TYPE_CMODE_CHANGE, 6,
3025 cidp->data, cidp->len,
3027 cipher, cipher ? strlen(cipher) : 0,
3028 hmac, hmac ? strlen(hmac) : 0,
3029 passphrase, passphrase ?
3030 strlen(passphrase) : 0,
3031 fkey ? fkey->data : NULL,
3032 fkey ? fkey->len : 0);
3034 /* Set CMODE notify type to network */
3035 silc_server_send_notify_cmode(server, SILC_PRIMARY_ROUTE(server),
3036 SILC_BROADCAST(server), channel,
3037 mode_mask, client->id, SILC_ID_CLIENT,
3038 cipher, hmac, passphrase, founder_key);
3040 /* Send command reply to sender */
3041 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CMODE,
3042 SILC_STATUS_OK, 0, ident, 3,
3043 2, tmp_id, tmp_len2,
3045 4, fkey ? fkey->data : NULL,
3046 fkey ? fkey->len : 0);
3047 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3048 packet->data, packet->len, FALSE);
3050 silc_buffer_free(packet);
3051 silc_buffer_free(cidp);
3054 channel->mode = old_mask;
3055 silc_buffer_free(fkey);
3056 silc_free(channel_id);
3057 silc_server_command_free(cmd);
3060 /* Server side of CUMODE command. Changes client's mode on a channel. */
3062 SILC_SERVER_CMD_FUNC(cumode)
3064 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3065 SilcServer server = cmd->server;
3066 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3067 SilcChannelID *channel_id = NULL;
3068 SilcClientID *client_id = NULL;
3069 SilcChannelEntry channel;
3070 SilcClientEntry target_client;
3071 SilcChannelClientEntry chl;
3072 SilcBuffer packet, idp;
3073 unsigned char *tmp_id, *tmp_ch_id, *tmp_mask;
3074 SilcUInt32 target_mask, sender_mask = 0, tmp_len, tmp_ch_len;
3076 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
3077 SilcPublicKey founder_key = NULL;
3078 SilcBuffer fkey = NULL;
3083 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_CUMODE, cmd, 3, 4);
3085 /* Get Channel ID */
3086 tmp_ch_id = silc_argument_get_arg_type(cmd->args, 1, &tmp_ch_len);
3088 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3089 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3092 channel_id = silc_id_payload_parse_id(tmp_ch_id, tmp_ch_len, NULL);
3094 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3095 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3099 /* Get channel entry */
3100 channel = silc_idlist_find_channel_by_id(server->local_list,
3103 channel = silc_idlist_find_channel_by_id(server->global_list,
3106 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3107 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
3113 /* Check whether sender is on the channel */
3114 if (!silc_server_client_on_channel(client, channel, &chl)) {
3115 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3116 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
3119 sender_mask = chl->mode;
3121 /* Get the target client's channel mode mask */
3122 tmp_mask = silc_argument_get_arg_type(cmd->args, 2, NULL);
3124 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3125 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3129 SILC_GET32_MSB(target_mask, tmp_mask);
3131 /* Get target Client ID */
3132 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3134 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3135 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3138 client_id = silc_id_payload_parse_id(tmp_id, tmp_len, NULL);
3140 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3141 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3145 /* Get target client's entry */
3146 target_client = silc_idlist_find_client_by_id(server->local_list,
3147 client_id, TRUE, NULL);
3148 if (!target_client) {
3149 target_client = silc_idlist_find_client_by_id(server->global_list,
3150 client_id, TRUE, NULL);
3153 if (target_client != client &&
3154 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO) &&
3155 !(sender_mask & SILC_CHANNEL_UMODE_CHANOP)) {
3156 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3157 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
3161 /* Check whether target client is on the channel */
3162 if (target_client != client) {
3163 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3164 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3165 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL, 0);
3174 /* If the target client is founder, no one else can change their mode
3176 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && client != target_client) {
3177 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3178 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3183 if (target_mask & SILC_CHANNEL_UMODE_CHANFO) {
3184 if (target_client != client) {
3185 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3186 SILC_STATUS_ERR_NOT_YOU, 0);
3190 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3191 /* The client tries to claim the founder rights. */
3192 unsigned char *tmp_auth;
3193 SilcUInt32 tmp_auth_len;
3194 SilcChannelClientEntry chl2;
3195 SilcHashTableList htl;
3197 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
3198 !channel->founder_key) {
3199 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3200 SILC_STATUS_ERR_AUTH_FAILED, 0);
3204 tmp_auth = silc_argument_get_arg_type(cmd->args, 4, &tmp_auth_len);
3206 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3207 SILC_STATUS_ERR_AUTH_FAILED, 0);
3211 /* Verify the authentication payload */
3212 if (!silc_auth_verify_data(tmp_auth, tmp_auth_len, SILC_AUTH_PUBLIC_KEY,
3213 channel->founder_key, 0, server->sha1hash,
3214 client->id, SILC_ID_CLIENT)) {
3215 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3216 SILC_STATUS_ERR_AUTH_FAILED, 0);
3221 founder_key = channel->founder_key;
3222 fkey = silc_pkcs_public_key_payload_encode(founder_key);
3224 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3225 SILC_STATUS_ERR_AUTH_FAILED, 0);
3229 /* There cannot be anyone else as founder on the channel now. This
3230 client is definitely the founder due to this authentication */
3231 silc_hash_table_list(channel->user_list, &htl);
3232 while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
3233 if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
3234 chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3235 silc_server_force_cumode_change(server, NULL, channel, chl2,
3239 silc_hash_table_list_reset(&htl);
3241 sender_mask = chl->mode |= SILC_CHANNEL_UMODE_CHANFO;
3244 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3245 if (target_client == client) {
3246 /* Remove channel founder rights from itself */
3247 chl->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
3250 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3251 SILC_STATUS_ERR_NOT_YOU, 0);
3257 if (target_mask & SILC_CHANNEL_UMODE_CHANOP) {
3258 /* Promote to operator */
3259 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
3260 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3261 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3262 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3263 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3268 chl->mode |= SILC_CHANNEL_UMODE_CHANOP;
3272 if (chl->mode & SILC_CHANNEL_UMODE_CHANOP) {
3273 if (!(sender_mask & SILC_CHANNEL_UMODE_CHANOP) &&
3274 !(sender_mask & SILC_CHANNEL_UMODE_CHANFO)) {
3275 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3276 SILC_STATUS_ERR_NO_CHANNEL_PRIV,
3281 /* Demote to normal user */
3282 chl->mode &= ~SILC_CHANNEL_UMODE_CHANOP;
3287 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3288 if (target_client != client) {
3289 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3290 SILC_STATUS_ERR_NOT_YOU, 0);
3294 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)) {
3295 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3299 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) {
3300 if (target_client != client) {
3301 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3302 SILC_STATUS_ERR_NOT_YOU, 0);
3306 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
3311 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3312 if (target_client != client) {
3313 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3314 SILC_STATUS_ERR_NOT_YOU, 0);
3318 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS)) {
3319 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3323 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) {
3324 if (target_client != client) {
3325 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3326 SILC_STATUS_ERR_NOT_YOU, 0);
3330 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
3335 if (target_mask & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3336 if (target_client != client) {
3337 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3338 SILC_STATUS_ERR_NOT_YOU, 0);
3342 if (!(chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS)) {
3343 chl->mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3347 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) {
3348 if (target_client != client) {
3349 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3350 SILC_STATUS_ERR_NOT_YOU, 0);
3354 chl->mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
3359 if (target_mask & SILC_CHANNEL_UMODE_QUIET) {
3360 if (!(chl->mode & SILC_CHANNEL_UMODE_QUIET)) {
3361 if (client == target_client) {
3362 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3363 SILC_STATUS_ERR_PERM_DENIED, 0);
3366 chl->mode |= SILC_CHANNEL_UMODE_QUIET;
3370 if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
3371 if (client == target_client) {
3372 silc_server_command_send_status_reply(cmd, SILC_COMMAND_CUMODE,
3373 SILC_STATUS_ERR_PERM_DENIED, 0);
3376 chl->mode &= ~SILC_CHANNEL_UMODE_QUIET;
3381 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3382 tmp_id = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3384 /* Send notify to channel, notify only if mode was actually changed. */
3386 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3387 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
3388 idp->data, idp->len,
3391 fkey ? fkey->data : NULL,
3392 fkey ? fkey->len : 0);
3394 /* Set CUMODE notify type to network */
3395 silc_server_send_notify_cumode(server, SILC_PRIMARY_ROUTE(server),
3396 SILC_BROADCAST(server), channel,
3397 target_mask, client->id, SILC_ID_CLIENT,
3398 target_client->id, founder_key);
3401 /* Send command reply to sender */
3402 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_CUMODE,
3403 SILC_STATUS_OK, 0, ident, 3,
3405 3, tmp_ch_id, tmp_ch_len,
3406 4, tmp_id, tmp_len);
3407 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
3408 packet->data, packet->len, FALSE);
3410 silc_buffer_free(packet);
3411 silc_buffer_free(idp);
3414 silc_free(channel_id);
3415 silc_free(client_id);
3416 silc_buffer_free(fkey);
3417 silc_server_command_free(cmd);
3420 /* Server side of KICK command. Kicks client out of channel. */
3422 SILC_SERVER_CMD_FUNC(kick)
3424 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3425 SilcServer server = cmd->server;
3426 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3427 SilcClientEntry target_client;
3428 SilcChannelID *channel_id;
3429 SilcClientID *client_id;
3430 SilcChannelEntry channel;
3431 SilcChannelClientEntry chl;
3433 SilcUInt32 tmp_len, target_idp_len;
3434 unsigned char *tmp, *comment, *target_idp;
3439 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 3);
3441 /* Get Channel ID */
3442 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3444 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3445 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3448 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
3450 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3451 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
3455 /* Get channel entry */
3456 channel = silc_idlist_find_channel_by_id(server->local_list,
3459 channel = silc_idlist_find_channel_by_id(server->local_list,
3462 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3463 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
3469 /* Check whether sender is on the channel */
3470 if (!silc_server_client_on_channel(client, channel, &chl)) {
3471 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3472 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
3476 /* Check that the kicker is channel operator or channel founder */
3477 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
3478 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
3479 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3480 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
3484 /* Get target Client ID */
3485 target_idp = silc_argument_get_arg_type(cmd->args, 2, &target_idp_len);
3487 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3488 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3491 client_id = silc_id_payload_parse_id(target_idp, target_idp_len, NULL);
3493 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3494 SILC_STATUS_ERR_NO_CLIENT_ID, 0);
3498 /* Get target client's entry */
3499 target_client = silc_idlist_find_client_by_id(server->local_list,
3500 client_id, TRUE, NULL);
3501 if (!target_client) {
3502 target_client = silc_idlist_find_client_by_id(server->global_list,
3503 client_id, TRUE, NULL);
3506 /* Check whether target client is on the channel */
3507 if (!silc_server_client_on_channel(target_client, channel, &chl)) {
3508 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3509 SILC_STATUS_ERR_USER_NOT_ON_CHANNEL,
3514 /* Check that the target client is not channel founder. Channel founder
3515 cannot be kicked from the channel. */
3516 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
3517 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3518 SILC_STATUS_ERR_NO_CHANNEL_FOPRIV,
3525 comment = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
3529 /* Send command reply to sender */
3530 silc_server_command_send_status_reply(cmd, SILC_COMMAND_KICK,
3533 /* Send KICKED notify to local clients on the channel */
3534 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3535 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3536 SILC_NOTIFY_TYPE_KICKED, 3,
3537 target_idp, target_idp_len,
3538 comment, comment ? strlen(comment) : 0,
3539 idp->data, idp->len);
3540 silc_buffer_free(idp);
3542 /* Send KICKED notify to primary route */
3543 silc_server_send_notify_kicked(server, SILC_PRIMARY_ROUTE(server),
3544 SILC_BROADCAST(server), channel,
3545 target_client->id, client->id, comment);
3547 /* Remove the client from channel's invite list */
3548 if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
3550 silc_argument_payload_encode_one(NULL, target_idp, target_idp_len, 3);
3551 SilcArgumentPayload args =
3552 silc_argument_payload_parse(ab->data, ab->len, 1);
3553 silc_server_inviteban_process(server, channel->invite_list, 1, args);
3554 silc_buffer_free(ab);
3555 silc_argument_payload_free(args);
3558 /* Remove the client from the channel. If the channel does not exist
3559 after removing the client then the client kicked itself off the channel
3560 and we don't have to send anything after that. */
3561 if (!silc_server_remove_from_one_channel(server, NULL, channel,
3562 target_client, FALSE))
3565 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3566 /* Re-generate channel key */
3567 if (!silc_server_create_channel_key(server, channel, 0))
3570 /* Send the channel key to the channel. The key of course is not sent
3571 to the client who was kicked off the channel. */
3572 silc_server_send_channel_key(server, target_client->connection, channel,
3573 server->server_type == SILC_ROUTER ?
3574 FALSE : !server->standalone);
3578 silc_server_command_free(cmd);
3581 /* Server side of OPER command. Client uses this comand to obtain server
3582 operator privileges to this server/router. */
3584 SILC_SERVER_CMD_FUNC(oper)
3586 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3587 SilcServer server = cmd->server;
3588 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3589 unsigned char *username, *auth;
3591 SilcServerConfigAdmin *admin;
3592 SilcIDListData idata = (SilcIDListData)client;
3593 bool result = FALSE;
3594 SilcPublicKey cached_key;
3596 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3599 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_OPER, cmd, 1, 2);
3601 /* Get the username */
3602 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3604 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3605 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3610 /* Get the admin configuration */
3611 admin = silc_server_config_find_admin(server, cmd->sock->ip,
3612 username, client->nickname);
3614 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
3615 username, client->nickname);
3617 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3618 SILC_STATUS_ERR_AUTH_FAILED,
3620 SILC_LOG_INFO(("OPER authentication failed for username '%s' by "
3621 "nickname '%s' from %s", username,
3622 client->nickname, cmd->sock->hostname));
3627 /* Get the authentication payload */
3628 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
3630 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3631 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3636 /* Verify the authentication data. If both passphrase and public key
3637 is set then try both of them. */
3638 if (admin->passphrase)
3639 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
3640 admin->passphrase, admin->passphrase_len,
3641 idata->hash, client->id, SILC_ID_CLIENT);
3642 if (!result && admin->publickeys) {
3643 cached_key = silc_server_get_public_key(server, admin->publickeys);
3646 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
3647 cached_key, 0, idata->hash,
3648 client->id, SILC_ID_CLIENT);
3651 /* Authentication failed */
3652 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3653 SILC_STATUS_ERR_AUTH_FAILED,
3658 /* Client is now server operator */
3659 client->mode |= SILC_UMODE_SERVER_OPERATOR;
3661 /* Update statistics */
3662 if (SILC_IS_LOCAL(client))
3663 server->stat.my_server_ops++;
3664 if (server->server_type == SILC_ROUTER)
3665 server->stat.server_ops++;
3667 /* Send UMODE change to primary router */
3668 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
3669 SILC_BROADCAST(server), client->id,
3672 /* Check if anyone is watching this nickname */
3673 if (server->server_type == SILC_ROUTER)
3674 silc_server_check_watcher_list(server, client, NULL,
3675 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3677 /* Send reply to the sender */
3678 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
3682 silc_server_command_free(cmd);
3685 SILC_TASK_CALLBACK(silc_server_command_detach_cb)
3687 SilcServer server = app_context;
3688 QuitInternal q = (QuitInternal)context;
3689 SilcClientID *client_id = (SilcClientID *)q->sock;
3690 SilcClientEntry client;
3691 SilcSocketConnection sock;
3693 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3695 if (client && client->connection) {
3696 sock = client->connection;
3698 /* If there is pending outgoing data for the client then purge it
3699 to the network before closing connection. */
3700 silc_server_packet_queue_purge(server, sock);
3702 /* Close the connection on our side */
3703 client->router = NULL;
3704 client->connection = NULL;
3705 sock->user_data = NULL;
3706 silc_server_close_connection(server, sock);
3709 silc_free(client_id);
3713 SILC_TASK_CALLBACK(silc_server_command_detach_timeout)
3715 SilcServer server = app_context;
3716 QuitInternal q = (QuitInternal)context;
3717 SilcClientID *client_id = (SilcClientID *)q->sock;
3718 SilcClientEntry client;
3720 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3722 if (client && client->mode & SILC_UMODE_DETACHED) {
3723 SILC_LOG_DEBUG(("Detach timeout"));
3724 silc_server_free_client_data(server, NULL, client, TRUE,
3728 silc_free(client_id);
3732 /* Server side of DETACH command. Detached the client from the network
3733 by closing the connection but preserving the session. */
3735 SILC_SERVER_CMD_FUNC(detach)
3737 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3738 SilcServer server = cmd->server;
3739 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3742 if (server->config->detach_disabled) {
3743 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
3744 SILC_STATUS_ERR_OPERATION_ALLOWED,
3749 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3752 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_DETACH, cmd, 0, 0);
3754 /* Remove operator privileges, since the client may resume in some
3755 other server which to it does not have operator privileges. */
3756 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
3757 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
3759 /* Send the user mode notify to notify that client is detached */
3760 client->mode |= SILC_UMODE_DETACHED;
3761 client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
3762 client->data.status &= ~SILC_IDLIST_STATUS_NOATTR;
3763 client->last_command = 0;
3764 client->fast_command = 0;
3765 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
3766 SILC_BROADCAST(server), client->id,
3768 server->stat.my_detached++;
3770 /* Check if anyone is watching this nickname */
3771 if (server->server_type == SILC_ROUTER)
3772 silc_server_check_watcher_list(server, client, NULL,
3773 SILC_NOTIFY_TYPE_UMODE_CHANGE);
3775 q = silc_calloc(1, sizeof(*q));
3776 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
3777 silc_schedule_task_add(server->schedule, 0, silc_server_command_detach_cb,
3778 q, 0, 200000, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
3780 if (server->config->detach_timeout) {
3781 q = silc_calloc(1, sizeof(*q));
3782 q->sock = silc_id_dup(client->id, SILC_ID_CLIENT);
3783 silc_schedule_task_add(server->schedule, 0,
3784 silc_server_command_detach_timeout,
3785 q, server->config->detach_timeout * 60,
3786 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
3789 /* Send reply to the sender */
3790 silc_server_command_send_status_reply(cmd, SILC_COMMAND_DETACH,
3794 silc_server_command_free(cmd);
3797 /* Server side of WATCH command. */
3799 SILC_SERVER_CMD_FUNC(watch)
3801 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3802 SilcServer server = cmd->server;
3803 char *add_nick, *del_nick;
3804 SilcUInt32 add_nick_len, del_nick_len, tmp_len;
3806 unsigned char hash[16], *tmp;
3807 SilcClientEntry client;
3808 SilcClientID *client_id = NULL;
3810 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WATCH, cmd, 1, 3);
3812 if (server->server_type != SILC_ROUTER && !server->standalone) {
3813 if (!cmd->pending) {
3814 /* Send the command to router */
3816 SilcUInt16 old_ident;
3818 SILC_LOG_DEBUG(("Forwarding WATCH to router"));
3820 old_ident = silc_command_get_ident(cmd->payload);
3821 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
3822 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3824 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3825 SILC_PACKET_COMMAND, cmd->packet->flags,
3826 tmpbuf->data, tmpbuf->len, TRUE);
3828 /* Reprocess this packet after received reply from router */
3829 silc_server_command_pending(server, SILC_COMMAND_WATCH,
3830 silc_command_get_ident(cmd->payload),
3831 silc_server_command_watch,
3832 silc_server_command_dup(cmd));
3833 cmd->pending = TRUE;
3834 silc_command_set_ident(cmd->payload, old_ident);
3835 silc_buffer_free(tmpbuf);
3836 } else if (context2) {
3837 /* Received reply from router, just send same data to the client. */
3838 SilcServerCommandReplyContext reply = context2;
3841 SILC_LOG_DEBUG(("Received reply to WATCH from router"));
3842 silc_command_get_status(reply->payload, &status, NULL);
3843 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH, status,
3850 /* We are router and keep the watch list for local cell */
3852 /* Get the client ID */
3853 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
3855 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3856 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3860 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
3862 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3863 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
3868 /* Get the client entry which must be in local list */
3869 client = silc_idlist_find_client_by_id(server->local_list,
3870 client_id, TRUE, NULL);
3872 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3873 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
3879 add_nick = silc_argument_get_arg_type(cmd->args, 2, &add_nick_len);
3880 del_nick = silc_argument_get_arg_type(cmd->args, 3, &del_nick_len);
3881 if (!add_nick && !del_nick) {
3882 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3883 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
3888 if (add_nick && add_nick_len > 128)
3889 add_nick[128] = '\0';
3890 if (del_nick && del_nick_len > 128)
3891 del_nick[128] = '\0';
3893 memset(nick, 0, sizeof(nick));
3895 /* Add new nickname to be watched in our cell */
3897 if (silc_server_name_bad_chars(add_nick, strlen(add_nick)) == TRUE) {
3898 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3899 SILC_STATUS_ERR_BAD_NICKNAME, 0);
3903 /* Hash the nick, we have the hash saved, not nicks because we can
3904 do one to one mapping to the nick from Client ID hash this way. */
3905 silc_to_lower(add_nick, nick, sizeof(nick) - 1);
3906 silc_hash_make(server->md5hash, nick, strlen(nick), hash);
3908 /* Check whether this client is already watching this nickname */
3909 if (silc_hash_table_find_by_context(server->watcher_list, hash,
3911 /* Nickname is alredy being watched for this client */
3912 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3913 SILC_STATUS_ERR_NICKNAME_IN_USE,
3918 /* Get the nickname from the watcher list and use the same key in
3919 new entries as well. If key doesn't exist then create it. */
3920 if (!silc_hash_table_find(server->watcher_list, hash, (void **)&tmp, NULL))
3921 tmp = silc_memdup(hash, CLIENTID_HASH_LEN);
3923 /* Add the client to the watcher list with the specified nickname hash. */
3924 silc_hash_table_add(server->watcher_list, tmp, client);
3927 /* Delete nickname from watch list */
3929 if (silc_server_name_bad_chars(del_nick, strlen(del_nick)) == TRUE) {
3930 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3931 SILC_STATUS_ERR_BAD_NICKNAME, 0);
3935 /* Hash the nick, we have the hash saved, not nicks because we can
3936 do one to one mapping to the nick from Client ID hash this way. */
3937 silc_to_lower(del_nick, nick, sizeof(nick) - 1);
3938 silc_hash_make(server->md5hash, nick, strlen(nick), hash);
3940 /* Check that this client is watching for this nickname */
3941 if (!silc_hash_table_find_by_context(server->watcher_list, hash,
3942 client, (void **)&tmp)) {
3943 /* Nickname is alredy being watched for this client */
3944 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3945 SILC_STATUS_ERR_NO_SUCH_NICK, 0);
3949 /* Delete the nickname from the watcher list. */
3950 silc_hash_table_del_by_context(server->watcher_list, hash, client);
3952 /* Now check whether there still exists entries with this key, if not
3953 then free the key to not leak memory. */
3954 if (!silc_hash_table_find(server->watcher_list, hash, NULL, NULL))
3958 /* Distribute the watch list to backup routers too */
3959 if (server->backup) {
3961 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
3962 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
3963 silc_server_backup_send(server, NULL, SILC_PACKET_COMMAND,
3964 cmd->packet->flags, tmpbuf->data, tmpbuf->len,
3966 silc_buffer_free(tmpbuf);
3969 silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
3973 silc_free(client_id);
3974 silc_server_command_free(cmd);
3977 /* Server side of SILCOPER command. Client uses this comand to obtain router
3978 operator privileges to this router. */
3980 SILC_SERVER_CMD_FUNC(silcoper)
3982 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
3983 SilcServer server = cmd->server;
3984 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
3985 unsigned char *username, *auth;
3987 SilcServerConfigAdmin *admin;
3988 SilcIDListData idata = (SilcIDListData)client;
3989 bool result = FALSE;
3990 SilcPublicKey cached_key;
3992 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
3995 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_SILCOPER, cmd, 1, 2);
3997 if (server->server_type != SILC_ROUTER) {
3998 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
3999 SILC_STATUS_ERR_AUTH_FAILED, 0);
4003 /* Get the username */
4004 username = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4006 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4007 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4012 /* Get the admin configuration */
4013 admin = silc_server_config_find_admin(server, cmd->sock->ip,
4014 username, client->nickname);
4016 admin = silc_server_config_find_admin(server, cmd->sock->hostname,
4017 username, client->nickname);
4019 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4020 SILC_STATUS_ERR_AUTH_FAILED, 0);
4021 SILC_LOG_INFO(("SILCOPER authentication failed for username '%s' by "
4022 "nickname '%s' from %s", username,
4023 client->nickname, cmd->sock->hostname));
4028 /* Get the authentication payload */
4029 auth = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4031 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4032 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4037 /* Verify the authentication data. If both passphrase and public key
4038 is set then try both of them. */
4039 if (admin->passphrase)
4040 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PASSWORD,
4041 admin->passphrase, admin->passphrase_len,
4042 idata->hash, client->id, SILC_ID_CLIENT);
4043 if (!result && admin->publickeys) {
4044 cached_key = silc_server_get_public_key(server, admin->publickeys);
4047 result = silc_auth_verify_data(auth, tmp_len, SILC_AUTH_PUBLIC_KEY,
4048 cached_key, 0, idata->hash,
4049 client->id, SILC_ID_CLIENT);
4052 /* Authentication failed */
4053 silc_server_command_send_status_reply(cmd, SILC_COMMAND_OPER,
4054 SILC_STATUS_ERR_AUTH_FAILED, 0);
4058 /* Client is now router operator */
4059 client->mode |= SILC_UMODE_ROUTER_OPERATOR;
4061 /* Update statistics */
4062 if (SILC_IS_LOCAL(client))
4063 server->stat.my_router_ops++;
4064 if (server->server_type == SILC_ROUTER)
4065 server->stat.router_ops++;
4067 /* Send UMODE change to primary router */
4068 silc_server_send_notify_umode(server, SILC_PRIMARY_ROUTE(server),
4069 SILC_BROADCAST(server), client->id,
4072 /* Check if anyone is watching this nickname */
4073 if (server->server_type == SILC_ROUTER)
4074 silc_server_check_watcher_list(server, client, NULL,
4075 SILC_NOTIFY_TYPE_UMODE_CHANGE);
4077 /* Send reply to the sender */
4078 silc_server_command_send_status_reply(cmd, SILC_COMMAND_SILCOPER,
4082 silc_server_command_free(cmd);
4085 /* Server side of command BAN. This is used to manage the ban list of the
4086 channel. To add clients and remove clients from the ban list. */
4088 SILC_SERVER_CMD_FUNC(ban)
4090 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4091 SilcServer server = cmd->server;
4092 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4093 SilcBuffer packet, list, tmp2;
4094 SilcChannelEntry channel;
4095 SilcChannelClientEntry chl;
4096 SilcChannelID *channel_id = NULL;
4097 unsigned char *id, *tmp;
4098 SilcUInt32 id_len, len;
4099 SilcArgumentPayload args;
4100 SilcHashTableList htl;
4102 SilcUInt16 argc = 0, ident = silc_command_get_ident(cmd->payload);
4104 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4107 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_BAN, cmd, 0, 3);
4109 /* Get Channel ID */
4110 id = silc_argument_get_arg_type(cmd->args, 1, &id_len);
4112 channel_id = silc_id_payload_parse_id(id, id_len, NULL);
4114 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4115 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4120 /* Get channel entry. The server must know about the channel since the
4121 client is expected to be on the channel. */
4122 channel = silc_idlist_find_channel_by_id(server->local_list,
4125 channel = silc_idlist_find_channel_by_id(server->global_list,
4128 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4129 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4135 /* Check whether this client is on the channel */
4136 if (!silc_server_client_on_channel(client, channel, &chl)) {
4137 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4138 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
4142 /* The client must be at least channel operator. */
4143 if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP)) {
4144 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4145 SILC_STATUS_ERR_NO_CHANNEL_PRIV, 0);
4149 /* Get the ban information */
4150 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
4151 if (tmp && len > 2) {
4152 /* Parse the arguments to see they are constructed correctly */
4153 SILC_GET16_MSB(argc, tmp);
4154 args = silc_argument_payload_parse(tmp + 2, len - 2, argc);
4156 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4157 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4162 /* Get the type of action */
4163 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
4164 if (tmp && len == 1) {
4165 if (tmp[0] == 0x00) {
4166 /* Allocate hash table for ban list if it doesn't exist yet */
4167 if (!channel->ban_list)
4169 silc_hash_table_alloc(0, silc_hash_ptr,
4171 silc_server_inviteban_destruct, channel,
4174 /* Check for resource limit */
4175 if (silc_hash_table_count(channel->ban_list) > 64) {
4176 silc_server_command_send_status_reply(cmd, SILC_COMMAND_BAN,
4177 SILC_STATUS_ERR_RESOURCE_LIMIT,
4183 /* Now add or delete the information. */
4184 silc_server_inviteban_process(server, channel->ban_list,
4185 (SilcUInt8)tmp[0], args);
4187 silc_argument_payload_free(args);
4190 /* Encode ban list */
4192 if (channel->ban_list && silc_hash_table_count(channel->ban_list)) {
4193 list = silc_buffer_alloc_size(2);
4194 silc_buffer_format(list,
4195 SILC_STR_UI_SHORT(silc_hash_table_count(
4196 channel->ban_list)),
4198 silc_hash_table_list(channel->ban_list, &htl);
4199 while (silc_hash_table_get(&htl, (void **)&type, (void **)&tmp2))
4200 list = silc_argument_payload_encode_one(list, tmp2->data, tmp2->len,
4202 silc_hash_table_list_reset(&htl);
4205 /* Send the BAN notify type to our primary router. */
4207 silc_server_send_notify_ban(server, SILC_PRIMARY_ROUTE(server),
4208 SILC_BROADCAST(server), channel,
4209 silc_argument_get_arg_type(cmd->args, 2, NULL),
4212 /* Send the reply back to the client */
4214 silc_command_reply_payload_encode_va(SILC_COMMAND_BAN,
4215 SILC_STATUS_OK, 0, ident, 2,
4217 3, list ? list->data : NULL,
4218 list ? list->len : 0);
4219 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4220 packet->data, packet->len, FALSE);
4222 silc_buffer_free(packet);
4223 silc_buffer_free(list);
4226 silc_free(channel_id);
4227 silc_server_command_free(cmd);
4230 /* Server side command of LEAVE. Removes client from a channel. */
4232 SILC_SERVER_CMD_FUNC(leave)
4234 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4235 SilcServer server = cmd->server;
4236 SilcSocketConnection sock = cmd->sock;
4237 SilcClientEntry id_entry = (SilcClientEntry)cmd->sock->user_data;
4238 SilcChannelID *id = NULL;
4239 SilcChannelEntry channel;
4243 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !id_entry)
4246 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_LEAVE, cmd, 1, 2);
4248 /* Get Channel ID */
4249 tmp = silc_argument_get_arg_type(cmd->args, 1, &len);
4251 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4252 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4255 id = silc_id_payload_parse_id(tmp, len, NULL);
4257 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4258 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4262 /* Get channel entry */
4263 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4265 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4267 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4268 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4274 /* Check whether this client is on the channel */
4275 if (!silc_server_client_on_channel(id_entry, channel, NULL)) {
4276 silc_server_command_send_status_reply(cmd, SILC_COMMAND_LEAVE,
4277 SILC_STATUS_ERR_NOT_ON_CHANNEL, 0);
4281 /* Notify routers that they should remove this client from their list
4282 of clients on the channel. Send LEAVE notify type. */
4283 silc_server_send_notify_leave(server, SILC_PRIMARY_ROUTE(server),
4284 SILC_BROADCAST(server), channel, id_entry->id);
4286 silc_server_command_send_status_data(cmd, SILC_COMMAND_LEAVE,
4287 SILC_STATUS_OK, 0, 2, tmp, len);
4289 /* Remove client from channel */
4290 if (!silc_server_remove_from_one_channel(server, sock, channel, id_entry,
4292 /* If the channel does not exist anymore we won't send anything */
4295 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
4296 /* Re-generate channel key */
4297 if (!silc_server_create_channel_key(server, channel, 0))
4300 /* Send the channel key */
4301 silc_server_send_channel_key(server, NULL, channel,
4302 server->server_type == SILC_ROUTER ?
4303 FALSE : !server->standalone);
4308 silc_server_command_free(cmd);
4311 /* Server side of command USERS. Resolves clients and their USERS currently
4312 joined on the requested channel. The list of Client ID's and their modes
4313 on the channel is sent back. */
4315 SILC_SERVER_CMD_FUNC(users)
4317 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4318 SilcServer server = cmd->server;
4319 SilcChannelEntry channel;
4320 SilcChannelID *id = NULL;
4321 SilcBuffer packet, idp;
4322 unsigned char *channel_id;
4323 SilcUInt32 channel_id_len;
4324 SilcBuffer client_id_list;
4325 SilcBuffer client_mode_list;
4326 unsigned char lc[4];
4327 SilcUInt32 list_count = 0;
4328 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4331 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_USERS, cmd, 1, 2);
4333 /* Get Channel ID */
4334 channel_id = silc_argument_get_arg_type(cmd->args, 1, &channel_id_len);
4336 /* Get channel name */
4337 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
4339 if (!channel_id && !channel_name) {
4340 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4341 SILC_STATUS_ERR_NO_CHANNEL_ID, 0);
4346 id = silc_id_payload_parse_id(channel_id, channel_id_len, NULL);
4348 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4349 SILC_STATUS_ERR_BAD_CHANNEL_ID, 0);
4354 /* If we are server and we don't know about this channel we will send
4355 the command to our router. If we know about the channel then we also
4356 have the list of users already. */
4358 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
4360 channel = silc_idlist_find_channel_by_name(server->local_list,
4361 channel_name, NULL);
4363 if (!channel || (!server->standalone && (channel->disabled ||
4364 !channel->users_resolved))) {
4365 if (server->server_type != SILC_ROUTER && !server->standalone &&
4369 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4370 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4372 /* Send USERS command */
4373 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4374 SILC_PACKET_COMMAND, cmd->packet->flags,
4375 tmpbuf->data, tmpbuf->len, TRUE);
4377 /* Reprocess this packet after received reply */
4378 silc_server_command_pending(server, SILC_COMMAND_USERS,
4379 silc_command_get_ident(cmd->payload),
4380 silc_server_command_users,
4381 silc_server_command_dup(cmd));
4382 cmd->pending = TRUE;
4383 silc_command_set_ident(cmd->payload, ident);
4384 silc_buffer_free(tmpbuf);
4389 /* Check the global list as well. */
4391 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
4393 channel = silc_idlist_find_channel_by_name(server->global_list,
4394 channel_name, NULL);
4396 /* Channel really does not exist */
4397 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4398 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4404 /* If the channel is private or secret do not send anything, unless the
4405 user requesting this command is on the channel or is server */
4406 if (cmd->sock->type == SILC_SOCKET_TYPE_CLIENT) {
4407 if (channel->mode & (SILC_CHANNEL_MODE_PRIVATE | SILC_CHANNEL_MODE_SECRET)
4408 && !silc_server_client_on_channel(cmd->sock->user_data, channel,
4410 silc_server_command_send_status_reply(cmd, SILC_COMMAND_USERS,
4411 SILC_STATUS_ERR_NO_SUCH_CHANNEL,
4417 /* Get the users list */
4418 if (!silc_server_get_users_on_channel(server, channel, &client_id_list,
4419 &client_mode_list, &list_count)) {
4421 client_id_list = NULL;
4422 client_mode_list = NULL;
4426 SILC_PUT32_MSB(list_count, lc);
4429 idp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
4430 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_USERS,
4431 SILC_STATUS_OK, 0, ident, 4,
4432 2, idp->data, idp->len,
4435 client_id_list->data : NULL,
4437 client_id_list->len : 0,
4438 5, client_mode_list ?
4439 client_mode_list->data : NULL,
4441 client_mode_list->len : 0);
4442 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4443 packet->data, packet->len, FALSE);
4445 silc_buffer_free(idp);
4446 silc_buffer_free(packet);
4448 silc_buffer_free(client_id_list);
4449 if (client_mode_list)
4450 silc_buffer_free(client_mode_list);
4454 silc_server_command_free(cmd);
4457 /* Server side of command GETKEY. This fetches the client's public key
4458 from the server where to the client is connected. */
4460 SILC_SERVER_CMD_FUNC(getkey)
4462 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4463 SilcServer server = cmd->server;
4465 SilcClientEntry client;
4466 SilcServerEntry server_entry;
4467 SilcClientID *client_id = NULL;
4468 SilcServerID *server_id = NULL;
4469 SilcIDPayload idp = NULL;
4470 SilcUInt16 ident = silc_command_get_ident(cmd->payload);
4473 SilcBuffer pk = NULL;
4475 SilcPublicKey public_key;
4477 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4479 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4480 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4484 idp = silc_id_payload_parse(tmp, tmp_len);
4486 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4487 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4492 id_type = silc_id_payload_get_type(idp);
4493 if (id_type == SILC_ID_CLIENT) {
4494 client_id = silc_id_payload_get_id(idp);
4496 /* If the client is not found from local list there is no chance it
4497 would be locally connected client so send the command further. */
4498 client = silc_idlist_find_client_by_id(server->local_list,
4499 client_id, TRUE, NULL);
4501 client = silc_idlist_find_client_by_id(server->global_list,
4502 client_id, TRUE, NULL);
4504 if ((!client && !cmd->pending && !server->standalone) ||
4505 (client && !client->connection && !cmd->pending &&
4506 !(client->mode & SILC_UMODE_DETACHED)) ||
4507 (client && !client->data.public_key && !cmd->pending)) {
4509 SilcUInt16 old_ident;
4510 SilcSocketConnection dest_sock;
4512 dest_sock = silc_server_get_client_route(server, NULL, 0,
4513 client_id, NULL, NULL);
4517 old_ident = silc_command_get_ident(cmd->payload);
4518 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4519 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4521 silc_server_packet_send(server, dest_sock,
4522 SILC_PACKET_COMMAND, cmd->packet->flags,
4523 tmpbuf->data, tmpbuf->len, TRUE);
4525 /* Reprocess this packet after received reply from router */
4526 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4527 silc_command_get_ident(cmd->payload),
4528 silc_server_command_getkey,
4529 silc_server_command_dup(cmd));
4530 cmd->pending = TRUE;
4531 silc_command_set_ident(cmd->payload, old_ident);
4532 silc_buffer_free(tmpbuf);
4537 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4538 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
4543 /* The client is locally connected, just get the public key and
4544 send it back. If they key does not exist then do not send it,
4545 send just OK reply */
4546 public_key = client->data.public_key;
4548 pk = silc_pkcs_public_key_payload_encode(public_key);
4549 } else if (id_type == SILC_ID_SERVER) {
4550 server_id = silc_id_payload_get_id(idp);
4552 /* If the server is not found from local list there is no chance it
4553 would be locally connected server so send the command further. */
4554 server_entry = silc_idlist_find_server_by_id(server->local_list,
4555 server_id, TRUE, NULL);
4557 server_entry = silc_idlist_find_server_by_id(server->global_list,
4558 server_id, TRUE, NULL);
4560 if (server_entry != server->id_entry &&
4561 ((!server_entry && !cmd->pending && !server->standalone) ||
4562 (server_entry && !server_entry->connection && !cmd->pending &&
4563 !server->standalone) ||
4564 (server_entry && !server_entry->data.public_key && !cmd->pending &&
4565 !server->standalone))) {
4567 SilcUInt16 old_ident;
4569 old_ident = silc_command_get_ident(cmd->payload);
4570 silc_command_set_ident(cmd->payload, ++server->cmd_ident);
4571 tmpbuf = silc_command_payload_encode_payload(cmd->payload);
4573 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
4574 SILC_PACKET_COMMAND, cmd->packet->flags,
4575 tmpbuf->data, tmpbuf->len, TRUE);
4577 /* Reprocess this packet after received reply from router */
4578 silc_server_command_pending(server, SILC_COMMAND_GETKEY,
4579 silc_command_get_ident(cmd->payload),
4580 silc_server_command_getkey,
4581 silc_server_command_dup(cmd));
4582 cmd->pending = TRUE;
4583 silc_command_set_ident(cmd->payload, old_ident);
4584 silc_buffer_free(tmpbuf);
4588 if (!server_entry) {
4589 silc_server_command_send_status_reply(cmd, SILC_COMMAND_GETKEY,
4590 SILC_STATUS_ERR_NO_SUCH_SERVER_ID,
4595 /* If they key does not exist then do not send it, send just OK reply */
4596 public_key = (!server_entry->data.public_key ?
4597 (server_entry == server->id_entry ? server->public_key :
4598 NULL) : server_entry->data.public_key);
4600 pk = silc_pkcs_public_key_payload_encode(public_key);
4605 tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4606 packet = silc_command_reply_payload_encode_va(SILC_COMMAND_GETKEY,
4607 SILC_STATUS_OK, 0, ident, 2,
4609 3, pk ? pk->data : NULL,
4611 silc_server_packet_send(server, cmd->sock, SILC_PACKET_COMMAND_REPLY, 0,
4612 packet->data, packet->len, FALSE);
4613 silc_buffer_free(packet);
4617 silc_id_payload_free(idp);
4618 silc_buffer_free(pk);
4619 silc_free(client_id);
4620 silc_free(server_id);
4621 silc_server_command_free(cmd);
4625 /* Private range commands, specific to this implementation */
4627 /* Server side command of CONNECT. Connects us to the specified remote
4628 server or router. */
4630 SILC_SERVER_CMD_FUNC(connect)
4632 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4633 SilcServer server = cmd->server;
4634 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4635 unsigned char *tmp, *host;
4637 SilcUInt32 port = SILC_PORT;
4639 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4642 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CONNECT, cmd, 1, 2);
4644 /* Check whether client has the permissions. */
4645 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
4646 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
4647 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4648 SILC_STATUS_ERR_NO_SERVER_PRIV, 0);
4652 if (server->server_type == SILC_ROUTER && !server->backup_router &&
4653 client->mode & SILC_UMODE_SERVER_OPERATOR) {
4654 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4655 SILC_STATUS_ERR_NO_ROUTER_PRIV, 0);
4659 /* Get the remote server */
4660 host = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4662 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4663 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4669 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4671 SILC_GET32_MSB(port, tmp);
4673 /* Create the connection. It is done with timeout and is async. */
4674 silc_server_create_connection(server, host, port);
4676 /* Send reply to the sender */
4677 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CONNECT,
4681 silc_server_command_free(cmd);
4684 /* Server side command of CLOSE. Closes connection to a specified server. */
4686 SILC_SERVER_CMD_FUNC(close)
4688 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4689 SilcServer server = cmd->server;
4690 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4691 SilcServerEntry server_entry;
4692 SilcSocketConnection sock;
4695 unsigned char *name;
4696 SilcUInt32 port = SILC_PORT;
4698 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4701 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_CLOSE, cmd, 1, 2);
4703 /* Check whether client has the permissions. */
4704 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
4705 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
4706 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4707 SILC_STATUS_ERR_NO_SERVER_PRIV,
4712 /* Get the remote server */
4713 name = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
4715 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4716 SILC_STATUS_ERR_NOT_ENOUGH_PARAMS,
4722 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
4724 SILC_GET32_MSB(port, tmp);
4726 server_entry = silc_idlist_find_server_by_conn(server->local_list,
4727 name, port, FALSE, NULL);
4729 server_entry = silc_idlist_find_server_by_conn(server->global_list,
4730 name, port, FALSE, NULL);
4731 if (!server_entry) {
4732 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4733 SILC_STATUS_ERR_NO_SERVER_ID, 0);
4737 if (server_entry == server->id_entry) {
4738 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4739 SILC_STATUS_ERR_NO_SERVER_ID, 0);
4743 /* Send reply to the sender */
4744 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_CLOSE,
4747 /* Close the connection to the server */
4748 sock = (SilcSocketConnection)server_entry->connection;
4750 server->backup_noswitch = TRUE;
4751 if (server->router == server_entry) {
4752 server->id_entry->router = NULL;
4753 server->router = NULL;
4754 server->standalone = TRUE;
4756 silc_server_disconnect_remote(server, sock,
4757 SILC_STATUS_ERR_BANNED_FROM_SERVER,
4758 "Closed by administrator");
4759 if (sock->user_data)
4760 silc_server_free_sock_user_data(server, sock, NULL);
4761 server->backup_noswitch = FALSE;
4764 silc_server_command_free(cmd);
4767 /* Server side command of SHUTDOWN. Shutdowns the server and closes all
4768 active connections. */
4770 SILC_SERVER_CMD_FUNC(shutdown)
4772 SilcServerCommandContext cmd = (SilcServerCommandContext)context;
4773 SilcServer server = cmd->server;
4774 SilcClientEntry client = (SilcClientEntry)cmd->sock->user_data;
4776 if (cmd->sock->type != SILC_SOCKET_TYPE_CLIENT || !client)
4779 SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_PRIV_SHUTDOWN, cmd, 0, 0);
4781 /* Check whether client has the permission. */
4782 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
4783 !(client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
4784 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
4785 SILC_STATUS_ERR_NO_SERVER_PRIV,
4790 /* Send reply to the sender */
4791 silc_server_command_send_status_reply(cmd, SILC_COMMAND_PRIV_SHUTDOWN,
4794 /* Then, gracefully, or not, bring the server down. */
4795 silc_server_stop(server);
4799 silc_server_command_free(cmd);