6af4a9eb99f561cc841cfdd23330dd1d05e71e66
[silc.git] / apps / silcd / idlist.h
1 /*
2
3   idlist.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2005, 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 #ifndef IDLIST_H
22 #define IDLIST_H
23
24 #include "serverconfig.h"
25
26 /* Context for holding cache information to periodically purge
27    the cache. */
28 typedef struct {
29   SilcIDCache cache;
30   SilcUInt32 timeout;
31 } *SilcIDListPurge;
32
33 /* Channel key re-key context. */
34 typedef struct {
35   SilcChannelEntry channel;
36   SilcUInt32 key_len;
37   SilcTask task;
38 } *SilcServerChannelRekey;
39
40 /* ID List Entry status flags. */
41 typedef SilcUInt8 SilcIDListStatus;
42 #define SILC_IDLIST_STATUS_NONE         0x00  /* No status */
43 #define SILC_IDLIST_STATUS_REGISTERED   0x01  /* Entry is registered */
44 #define SILC_IDLIST_STATUS_RESOLVED     0x02  /* Entry info is resolved */
45 #define SILC_IDLIST_STATUS_RESOLVING    0x04  /* Entry is being resolved
46                                                  with WHOIS or IDENTIFY */
47 #define SILC_IDLIST_STATUS_DISABLED     0x08  /* Entry is disabled */
48 #define SILC_IDLIST_STATUS_RESUMED      0x10  /* Entry is resumed */
49 #define SILC_IDLIST_STATUS_LOCAL        0x20  /* Entry locally connected */
50 #define SILC_IDLIST_STATUS_RESUME_RES   0x40  /* Entry resolved while
51                                                  resuming */
52 #define SILC_IDLIST_STATUS_NOATTR       0x80  /* Entry does not support
53                                                  attributes in WHOIS */
54
55 /*
56    Generic ID list data structure.
57
58    This structure is included in all ID list entries and it includes data
59    pointers that are common to all ID entries.  This structure is always
60    defined to the first field in the ID entries and is used to explicitly
61    type cast to this type without first explicitly casting to correct ID
62    entry type.  Hence, the ID list entry is type casted to this type to
63    get this data from the ID entry (which is usually opaque pointer).
64
65    Note that some of the fields may be NULL.
66
67 */
68 struct SilcIDListDataObject {
69   SilcConnectionType conn_type;      /* Connection type */
70   SilcServerConnection sconn;        /* Connection context */
71   SilcSKERekeyMaterial rekey;        /* Rekey material */
72   SilcHash hash;
73
74   /* Public key */
75   SilcPublicKey public_key;
76   unsigned char fingerprint[20];
77
78   long last_receive;            /* Time last received data */
79   long last_sent;               /* Time last sent data */
80
81   unsigned long created;        /* Time when entry was created */
82
83   SilcIDListStatus status;      /* Status mask of the entry */
84 };
85
86 /*
87    SILC Server entry object.
88
89    This entry holds information about servers in SILC network. However,
90    contents of this entry is highly dependent of what kind of server we are
91    (normal server or router server) and whether the entry is used as a local
92    list or a global list. These factors dictates the contents of this entry.
93
94    This entry is defined as follows:
95
96    Server type   List type      Contents
97    =======================================================================
98    server        local list     Server itself
99    server        global list    NULL
100    router        local list     All servers is the cell
101    router        global list    All servers in the SILC network
102
103    Following short description of the fields:
104
105    SilcIDListDataStruct data
106
107        Generic data structure to hold data common to all ID entries.
108
109    char *server_name
110
111        Logical name of the server. There is no limit of the length of the
112        server name. This is usually the same name as defined in DNS.
113
114    SilcUInt8 server_type
115
116        Type of the server. SILC_SERVER or SILC_ROUTER are the possible
117        choices for this.
118
119    SilcServerID *id
120
121        ID of the server. This includes all the relevant information about
122        the server SILC will ever need. These are also the informations
123        that is broadcasted between servers and routers in the SILC network.
124
125    char *server_info
126    char *motd
127
128        Server info (from INFO command) saved temporarily and motd (from
129        MOTD command) saved temporarily.
130
131    SilcServerEntry router
132
133        This is a pointer back to the server list. This is the router server
134        where this server is connected to. If this is the router itself and
135        it doesn't have a route this is NULL.
136
137    SilcCipher send_key
138    SilcCipher receive_key
139
140        Data sending and receiving keys.
141
142    void *connection
143
144        A pointer, usually, to the socket list for fast referencing to
145        the data used in connection with this server.  This may be anything
146        but as just said, this is usually pointer to the socket connection
147        list.
148
149 */
150 struct SilcServerEntryStruct {
151   /* Generic data structure. DO NOT add anything before this! */
152   SilcIDListDataStruct data;
153
154   char *server_name;
155   SilcUInt8 server_type;
156   SilcServerID *id;
157   char *server_info;
158   char *motd;
159
160   /* Pointer to the router */
161   SilcServerEntry router;
162
163   /* Connection data */
164   void *connection;
165 };
166
167 /*
168    SILC Channel Client entry structure.
169
170    This entry used only by the SilcChannelEntry object and it holds
171    information about current clients (ie. users) on channel. Following
172    short description of the fields:
173
174    SilcClientEntry client
175
176        Pointer to the client list. This is the client currently on channel.
177
178    SilcUInt32 mode
179
180        Client's current mode on the channel.
181
182    SilcChannelEntry channel
183
184        Back pointer back to channel. As this structure is also used by
185        SilcClientEntry we have this here for fast access to the channel when
186        used by SilcClientEntry.
187
188 */
189 typedef struct SilcChannelClientEntryStruct {
190   SilcClientEntry client;
191   SilcUInt32 mode;
192   SilcChannelEntry channel;
193 } *SilcChannelClientEntry;
194
195 /*
196    SILC Client entry object.
197
198    This entry holds information about connected clients ie. users in the SILC
199    network. The contents of this entrt is depended on whether we are normal
200    server or router server and whether the list is a local or global list.
201
202    This entry is defined as follows:
203
204    Server type   List type      Contents
205    =======================================================================
206    server        local list     All clients in server
207    server        global list    NULL
208    router        local list     All clients in cell
209    router        global list    All clients in SILC
210
211    Following short description of the fields:
212
213    SilcIDListDataStruct data
214
215        Generic data structure to hold data common to all ID entries.
216
217    unsigned char *nickname
218
219        The nickname of the client.  This is nickname in original format,
220        not casefolded or normalized.  However, it is checked to assure
221        that prohibited characters do not exist.  The casefolded version
222        is in the ID Cache.
223
224    char *servername
225
226        The name of the server where the client is from. MAy be NULL.
227
228    char username
229
230        Client's usename. This is defined in the following manner:
231
232        Server type   List type      Contents
233        ====================================================
234        server        local list     User's name
235        router        local list     NULL
236        router        global list    NULL
237
238        Router doesn't hold this information since it is not vital data
239        for the router. If this information is needed by the client it is
240        fetched when it is needed.
241
242    char userinfo
243
244        Information about user. This is free information and can be virtually
245        anything. This is defined in following manner:
246
247        Server type   List type      Contents
248        ====================================================
249        server        local list     User's information
250        router        local list     NULL
251        router        global list    NULL
252
253        Router doesn't hold this information since it is not vital data
254        for the router. If this information is needed by the client it is
255        fetched when it is needed.
256
257    SilcClientID *id
258
259        ID of the client. This includes all the information SILC will ever
260        need. Notice that no nickname of the user is saved anywhere. This is
261        beacuse of SilcClientID includes 88 bit hash value of the user's
262        nickname which can be used to track down specific user by their
263        nickname. Nickname is not relevant information that would need to be
264        saved as plain.
265
266    SilcUInt32 mode
267
268        Client's mode.  Client maybe for example server operator or
269        router operator (SILC operator).
270
271    long last_command
272
273        Time of last time client executed command. We are strict and will
274        not allow any command to be exeucted more than once in about
275        2 seconds. This is result of normal time().
276
277    SilcUInt8 fast_command
278
279        Counter to check command bursts.  By default, up to 5 commands
280        are allowed before limiting the execution.  See command flags
281        for more detail.
282
283    SilcServerEntry router
284
285        This is a pointer to the server list. This is the router server whose
286        cell this client is coming from. This is used to route messages to
287        this client.
288
289    SilcHashTable channels;
290
291        All the channels this client has joined.  The context saved in the
292        hash table shares memory with the channel entrys `user_list' hash
293        table.
294
295    void *connection
296
297        A pointer, usually, to the socket list for fast referencing to
298        the data used in connection with this client.  This may be anything
299        but as just said, this is usually pointer to the socket connection
300        list.
301
302    SilcUInt16 resolve_cmd_ident
303
304        Command identifier for the entry when the entry's data.status
305        is SILC_IDLIST_STATUS_RESOLVING.  If this entry is asked to be
306        resolved when the status is set then the resolver may attach to
307        this command identifier and handle the process after the resolving
308        is over.
309
310 */
311 struct SilcClientEntryStruct {
312   /* Generic data structure. DO NOT add anything before this! */
313   SilcIDListDataStruct data;
314
315   unsigned char *nickname;
316   char *servername;
317   char *username;
318   char *userinfo;
319   SilcClientID *id;
320   SilcUInt32 mode;
321
322   long last_command;
323   SilcUInt8 fast_command;
324
325   /* Requested Attributes */
326   unsigned char *attrs;
327   SilcUInt16 attrs_len;
328
329   /* Pointer to the router */
330   SilcServerEntry router;
331
332   /* All channels this client has joined */
333   SilcHashTable channels;
334
335   /* Connection data */
336   void *connection;
337
338   /* Last time updated/accessed */
339   unsigned long updated;
340
341   /* data.status is RESOLVING and this includes the resolving command
342      reply identifier. */
343   SilcUInt16 resolve_cmd_ident;
344
345   /* we need this so nobody can resume more than once at the same time -
346    * server crashes, really odd behaviour, ... */
347   SilcClientEntry resuming_client;
348 };
349
350 /*
351    SILC Channel entry object.
352
353    This entry holds information about channels in SILC network. The contents
354    of this entry is depended on whether we are normal server or router server
355    and whether the list is a local or global list.
356
357    This entry is defined as follows:
358
359    Server type   List type      Contents
360    =======================================================================
361    server        local list     All channels in server
362    server        global list    NULL
363    router        local list     All channels in cell
364    router        global list    All channels in SILC
365
366    Following short description of the fields:
367
368    char *channel_name
369
370        Logical name of the channel.  This is the original format, not
371        the casefolded or normalized.  However, this is checked to assure
372        that prohibited characters do not exist.  The casefolded version
373        is in the ID Cache.
374
375    SilcUInt32 mode
376
377        Current mode of the channel.  See lib/silccore/silcchannel.h for
378        all modes.
379
380    SilcChannelID *id
381
382        ID of the channel. This includes all the information SILC will ever
383        need.
384
385    SilcBool global_users
386
387        Boolean value to tell whether there are users outside this server
388        on this channel. This is set to TRUE if router sends message to
389        the server that there are users outside your server on your
390        channel as well. This way server knows that messages needs to be
391        sent to the router for further routing. If this is a normal
392        server and this channel is not created on this server this field
393        is always TRUE. If this server is a router this field is ignored.
394
395    char *topic
396
397        Current topic of the channel.
398
399    char *cipher
400
401        Default cipher of the channel. If this is NULL then server picks
402        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
403
404    char *hmac_name
405
406        Default hmac of the channel. If this is NULL then server picks
407        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
408
409    SilcPublicKey founder_key
410
411        If the SILC_CMODE_FOUNDER_AUTH has been set then this will include
412        the founder's public key.  When the mode and this key is set the
413        channel is also permanent channel and cannot be destroyed.
414
415    SilcHashTable user_list
416
417        All users joined on this channel.  Note that the context saved to
418        this entry shares memory with the client entrys `channels' hash
419        table.
420
421    SilcServerEntry router
422
423        This is a pointer to the server list. This is the router server
424        whose cell this channel belongs to. This is used to route messages
425        to this channel.
426
427    SilcCipher send_key
428    SilcCipher receive_key
429
430        The key of the channel (the cipher actually).
431
432    unsigned char *key
433    SilcUInt32 key_len
434
435        Raw key data of the channel key.
436
437    unsigned char iv[SILC_CIPHER_MAX_IV_SIZE]
438
439        Current initial vector. Initial vector is received always along
440        with the channel packet. By default this is filled with NULL.
441
442    SilcHmac hmac;
443
444        HMAC of the channel.
445
446    SilcServerChannelRekey rekey
447
448        Channel key re-key context.
449
450 */
451 struct SilcChannelEntryStruct {
452   char *channel_name;
453   SilcUInt32 mode;
454   SilcChannelID *id;
455   char *topic;
456   char *cipher;
457   char *hmac_name;
458   SilcPublicKey founder_key;
459   SilcHashTable channel_pubkeys;
460
461   SilcUInt32 user_limit;
462   unsigned char *passphrase;
463   SilcHashTable invite_list;
464   SilcHashTable ban_list;
465
466   /* All users on this channel */
467   SilcHashTable user_list;
468   SilcUInt32 user_count;
469
470   /* Pointer to the router */
471   SilcServerEntry router;
472
473   /* Channel keys */
474   SilcCipher send_key;
475   SilcCipher receive_key;
476   unsigned char *key;
477   SilcUInt32 key_len;
478   SilcHmac hmac;
479
480   SilcServerChannelRekey rekey;
481   unsigned long created;
482   unsigned long updated;
483
484   /* Flags */
485   unsigned int global_users : 1;
486   unsigned int disabled : 1;
487   unsigned int users_resolved : 1;
488 };
489
490 /*
491    SILC ID List object.
492
493    As for remainder these lists are defined as follows:
494
495    Entry list (cache)  Server type   List type      Contents
496    =======================================================================
497    servers             server        local list     Server itself
498    servers             server        global list    NULL
499    servers             router        local list     All servers in cell
500    servers             router        global list    All servers in SILC
501
502    clients             server        local list     All clients in server
503    clients             server        global list    NULL
504    clients             router        local list     All clients in cell
505    clients             router        global list    All clients in SILC
506
507    channels            server        local list     All channels in server
508    channels            server        global list    NULL
509    channels            router        local list     All channels in cell
510    channels            router        global list    All channels in SILC
511
512    As seen on the list normal server never defines a global list. This is
513    because of normal server don't know anything about anything global data,
514    they get it from the router if and when they need it. Routers, on the
515    other hand, always define local and global lists because routers really
516    know all the relevant data in the SILC network.
517
518    This object is used as local and global list by the server/router.
519    Above table shows how this is defined on different conditions.
520
521    This object holds pointers to the ID cache system. Every ID cache entry
522    has a specific context pointer to allocated entry (server, client or
523    channel entry).
524
525 */
526 struct SilcIDListStruct {
527   SilcIDCache servers;
528   SilcIDCache clients;
529   SilcIDCache channels;
530 };
531
532 /*
533    ID Entry for Unknown connections.
534
535    This is used during authentication phases where we still don't know
536    what kind of connection remote connection is, hence, we will use this
537    structure instead until we know what type of connection remote end is.
538
539    This is not in any list. This is always individually allocated and
540    used as such.
541
542 */
543 typedef struct {
544   /* Generic data structure. DO NOT add anything before this! */
545   SilcIDListDataStruct data;
546   SilcAsyncOperation op;
547   SilcServerConfigRef cconfig;
548   SilcServerConfigRef sconfig;
549   SilcServerConfigRef rconfig;
550   SilcServer server;
551   const char *hostname;
552   const char *ip;
553   SilcUInt16 port;
554 } *SilcUnknownEntry;
555
556 /* Prototypes */
557 void silc_idlist_add_data(void *entry, SilcIDListData idata);
558 void silc_idlist_del_data(void *entry);
559 SILC_TASK_CALLBACK(silc_idlist_purge);
560 SilcServerEntry
561 silc_idlist_add_server(SilcIDList id_list,
562                        char *server_name, int server_type,
563                        SilcServerID *id, SilcServerEntry router,
564                        void *connection);
565 SilcServerEntry
566 silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
567                               SilcBool registered, SilcIDCacheEntry *ret_entry);
568 SilcServerEntry
569 silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
570                                 SilcBool registered, SilcIDCacheEntry *ret_entry);
571 SilcServerEntry
572 silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
573                                 int port, SilcBool registered,
574                                 SilcIDCacheEntry *ret_entry);
575 SilcServerEntry
576 silc_idlist_replace_server_id(SilcIDList id_list, SilcServerID *old_id,
577                               SilcServerID *new_id);
578 int silc_idlist_del_server(SilcIDList id_list, SilcServerEntry entry);
579 void silc_idlist_server_destructor(SilcIDCache cache,
580                                    SilcIDCacheEntry entry,
581                                    void *dest_context,
582                                    void *app_context);
583 SilcClientEntry
584 silc_idlist_add_client(SilcIDList id_list, char *nickname, char *username,
585                        char *userinfo, SilcClientID *id,
586                        SilcServerEntry router, void *connection);
587 int silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry);
588 int silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
589                                         char *server,
590                                         SilcClientEntry **clients,
591                                         SilcUInt32 *clients_count);
592 int silc_idlist_get_clients_by_hash(SilcIDList id_list,
593                                     char *nickname, char *server,
594                                     SilcHash md5hash,
595                                     SilcClientEntry **clients,
596                                     SilcUInt32 *clients_count);
597 SilcClientEntry
598 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id,
599                               SilcBool registered, SilcIDCacheEntry *ret_entry);
600 SilcClientEntry
601 silc_idlist_replace_client_id(SilcServer server,
602                               SilcIDList id_list, SilcClientID *old_id,
603                               SilcClientID *new_id, const char *nickname);
604 void silc_idlist_client_destructor(SilcIDCache cache,
605                                    SilcIDCacheEntry entry,
606                                    void *dest_context,
607                                    void *app_context);
608 SilcChannelEntry
609 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
610                         SilcChannelID *id, SilcServerEntry router,
611                         SilcCipher send_key, SilcCipher receive_key,
612                         SilcHmac hmac);
613 void silc_idlist_channel_destructor(SilcIDCache cache,
614                                     SilcIDCacheEntry entry,
615                                     void *dest_context,
616                                     void *app_context);
617 int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry);
618 SilcChannelEntry
619 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name,
620                                  SilcIDCacheEntry *ret_entry);
621 SilcChannelEntry
622 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id,
623                                SilcIDCacheEntry *ret_entry);
624 SilcChannelEntry
625 silc_idlist_replace_channel_id(SilcIDList id_list, SilcChannelID *old_id,
626                                SilcChannelID *new_id);
627 SilcChannelEntry *
628 silc_idlist_get_channels(SilcIDList id_list, SilcChannelID *channel_id,
629                          SilcUInt32 *channels_count);
630
631 #endif