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