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