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