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