These were moved to the 'techdoc' subdirectory.
[citadel.git] / citadel / techdoc / hack.txt
1  hack.doc for Citadel/UX
2  written by Art Cancro (ajc@uncnsrd.mt-kisco.ny.us)
3    
4    Much of this document is borrowed from the original hack.doc from
5 Citadel-CP/M and Citadel-86, because many of the concepts are the same.  Hats
6 off to whoever wrote the original, for a fine document that inspired the
7 implementation of Citadel for Unix. 
8  
9    Note that this document is really out of date.  It doesn't cover anything
10 about the threaded server architecture or any of the network stuff.  What is
11 covered here is the basic architecture of the databases.
12  
13    But enough of the preamble.  Here's how Citadel/UX works :)
14   
15    Here are the major databases to be discussed:
16   
17   msgmain         The big circular file that contains message text
18   quickroom       Contains room info such as room names, stats, etc.
19   fullroom        One fullrm file per room: message numbers and pointers.
20   usersupp        Contains info for each user on the system.
21  
22    The fundamental structure of the system differs greatly from the way
23 Citadels used to work.  Citadel now depends on a record manager or database
24 manager of some sort.  Thanks to the API which is in place for connecting to
25 a data store, any record manager may be used as long as it supports the
26 storage and retrieval of large binary objects (blobs) indexed by unique keys.
27 Please see database.c for more information on data store primitives.
28  
29    The message base (msgmain) is a big file of messages indexed by the message
30 number.  Messages are numbered consecutively and start with an FF (hex)
31 byte.  Except for this FF start-of-message byte, all bytes in the message
32 file have the high bit set to 0.  This means that in principle it is
33 trivial to scan through the message file and locate message N if it
34 exists, or return error.  (Complexities, as usual, crop up when we
35 try for efficiency...)
36  
37     Each room is basically just a list of message numbers.  Each time
38 we enter a new message in a room, we slide all the old message-numbers
39 down a slot, and probably the oldest one falls off the bottom (in which case
40 we must delete it from the message base).  Reading a rooms is just a matter
41 of looking up the messages one by one and sending them to the client for
42 display, printing, or whatever.
43  
44     Implementing the "new message" function is also trivial in principle:
45 we just keep track, for each caller in the userlog, of the highest-numbered
46 message which existed on the >last< call.  (Remember, message numbers are
47 simply assigned sequentially each time a message is created.  This
48 sequence is global to the entire system, not local within a room.)  If
49 we ignore all message-numbers in the room less than this, only new messages
50 will be printed.  Voila! 
51
52                 message format on disk  (msgmain)
53
54   Each message begins with an FF byte. The next byte will then be MES_NORMAL,
55 MES_ANON, or MES_ANON2, depending on whether the message in anonymous or not.
56 The next byte is either a 0 or 1. If it is 0, the message will be printed
57 with the Citadel formatter.  If it is a 1, the
58 message is printed directly to the screen, as is.  External editors generate
59 this type of message.  After these three opening bytes, the remainder of
60 the message consists of a sequence of character strings.  Each string
61 begins with a type byte indicating the meaning of the string and is
62 ended with a null.  All strings are printable ASCII: in particular,
63 all numbers are in ASCII rather than binary.  This is for simplicity,
64 both in implementing the system and in implementing other code to
65 work with the system.  For instance, a database driven off Citadel archives
66 can do wildcard matching without worrying about unpacking binary data such
67 as message ID's first.  To provide later downward compatability
68 all software should be written to IGNORE fields not currently defined.
69
70                   The type bytes currently defined are:         
71
72 BYTE    Mnemonic        Comments
73
74 T       Date/Time       A 32-bit integer containing the date and time of
75                         the message in standard UNIX format (the number
76                         of seconds since January 1, 1970 GMT).
77 P       Path            Complete path of message, as in the UseNet news
78                         standard.  A user should be able to send UUCP mail to
79                         this path. (Note that your system name will not be
80                         tacked onto this until you're sending the message to
81                         someone else)
82 I       ID on orig      A 32-bit integer containing the message ID on the
83                         system the message *originated* on.
84 #       ID on local     A 32-bit integer containing the message ID on the
85                         system the message is *currently* on (obviously this
86                         is meaningless for a message being transmitted over
87                         a network).
88 A       Author          Name of originator of message.
89 E       EmailUserID     Only present in messages originating from the Internet.
90                         It contains the username portion of the author's 
91                         e-mail address, thereby allowing replies and bounces
92                         to be properly formulated.
93 R       Recipient       Only present in Mail messages.
94 O       Room            Room of origin.
95 N       Nodename        Contains node name of system message originated on.
96 H       HumanNodeName   Contains human name of system message originated on.
97 D       Destination     Contains name of the system this message should
98                         be sent to, for mail routing (private mail only).
99 U       Subject         Optional.  Developers may choose whether they wish to
100                         generate or display subject fields.  Citadel/UX does
101                         not generate them, but it does print them when found.
102 B       Phone number    The dialup number of the system this message
103                         originated on.  This is optional, and is only
104                         defined for helping implement C86Net gateways.
105 G       Gateway domain  This field is provided solely for the implementation
106                         of C86Net gateways, and holds the C86Net domain of
107                         the system this message originated on.  Unless you're
108                         implementing such a gateway, there's no need to even
109                         bother with this field.
110 S       Special field   Only meaningful for messages being spooled over a
111                         network.  Usually means that the message isn't really
112                         a message, but rather some other network function:
113                         -> "S" followed by "FILE" (followed by a null, of
114                         course) means that the message text is actually an
115                         IGnet/Open file transfer.
116 M       Message Text    Normal ASCII, newlines seperated by CR's or LF's,
117                         null terminated as always.
118   
119                         EXAMPLE
120
121 Let <FF> be a 0xFF byte, and <0> be a null (0x00) byte.  Then a message
122 which prints as...
123
124 Apr 12, 1988 23:16 From Test User In Network Test> @lifesys (Life BBS)
125 Have a nice day!
126
127  might be stored as...
128 <FF><40><0>I12345<0>Pneighbor!lifesys!test_user<0>T576918988<0>    (continued)
129 -----------|Mesg ID#|--Message Path---------------|--Date------
130
131 AThe Test User<0>ONetwork Test<0>Nlifesys<0>HLife BBS<0>MHave a nice day!<0>
132 |-----Author-----|-Room name-----|-nodename-|Human Name-|--Message text-----
133
134  Weird things can happen if fields are missing, especially if you use the
135 networker.  But basically, the date, author, room, and nodename may be in any
136 order.  But the leading fields and the message text must remain in the same
137 place.  The H field looks better when it is placed immediately after the N
138 field.
139
140                             Networking
141
142 Citadel nodes network by sharing one or more rooms. Any Citadel node
143 can choose to share messages with any other Citadel node, through the sending
144 of spool files.  The sending system takes all messages it hasn't sent yet, and
145 spools them to the recieving system, which posts them in the rooms.
146
147 Complexities arise primarily from the possibility of densely connected
148 networks: one does not wish to accumulate multiple copies of a given
149 message, which can easily happen.  Nor does one want to see old messages
150 percolating indefinitely through the system.
151
152 This problem is handled by keeping track of the path a message has taken over
153 the network, like the UseNet news system does.  When a system sends out a
154 message, it adds its own name to the bang-path in the <P> field of the
155 message.  If no path field is present, it generates one.  
156    
157 With the path present, all the networker has to do to assure that it doesn't
158 send another system a message it's already received is check the <P>ath field
159 for that system's name somewhere in the bang path.  If it's present, the system
160 has already seen the message, so we don't send it.  (Note that the current
161 implementation does not allow for "loops" in the network -- if you build your
162 net this way you will see lots of duplicate messages.)
163
164 The above discussion should make the function of the fields reasonably clear:
165
166  o  Travelling messages need to carry original message-id, system of origin,
167     date of origin, author, and path with them, to keep reproduction and
168     cycling under control.
169
170 (Uncoincidentally) the format used to transmit messages for networking
171 purposes is precisely that used on disk, except that there may be any amount
172 of garbage between the null ending a message and the <FF> starting the next
173 one.  This allows greater compatibility if slight problems crop up. The current
174 distribution includes netproc.c, which is basically a database replicator;
175 please see network.txt on its operation and functionality (if any).
176
177                         portability problems
178  
179  At this point, most hardware-dependent stuff has been removed from the 
180 system.  On the server side, most of the OS-dependent stuff has been isolated
181 into the sysdep.c source module.  The server should compile on any POSIX
182 compliant system with a full pthreads implementation and TCP/IP support.  In
183 the future, we may try to port it to non-POSIX systems as well.
184  
185  On the client side, it's also POSIX compliant.  The client even seems to
186 build ok on non-POSIX systems with porting libraries (such as the Cygnus
187 Win32 stuff).
188   
189
190                    "Room" records (quickroom/fullroom)
191  
192 The rooms are basically indices into msgmain, the message database.
193 As noted in the overview, each is essentially an array of pointers into
194 the message file.  The pointers consist of a 32-bit message ID number
195 (we will wrap around at 32 bits for these purposes).
196
197 Since messages are numbered sequentially, the
198 set of messages existing in msgmain will always form a continuous
199 sequence at any given time.
200
201 That should be enough background to tackle a full-scale room.  From citadel.h:
202
203 STRUCT QUickroom {
204         char QRname[20];                /* Max. len is 19, plus null term   */
205         char QRpasswd[10];              /* Only valid if it's a private rm  */
206         long QRroomaide;                /* User number of room aide         */
207         long QRhighest;                 /* Highest message NUMBER in room   */
208         char QRgen;                     /* Generation number of room        */
209         unsigned QRflags;               /* See flag values below            */
210         char QRdirname[15];             /* Directory name, if applicable    */
211         char QRfloor;                   /* (not yet implemented)            */
212                 };
213
214 #define QR_BUSY         1               /* Room is being updated, WAIT      */
215 #define QR_INUSE        2               /* Set if in use, clear if avail    */
216 #define QR_PRIVATE      4               /* Set for any type of private room */
217 #define QR_PASSWORDED   8               /* Set if there's a password too    */
218 #define QR_GUESSNAME    16              /* Set if it's a guessname room     */
219 #define QR_DIRECTORY    32              /* Directory room                   */
220 #define QR_UPLOAD       64              /* Allowed to upload                */
221 #define QR_DOWNLOAD     128             /* Allowed to download              */
222 #define QR_VISDIR       256             /* Visible directory                */
223 #define QR_ANONONLY     512             /* Anonymous-Only room              */
224 #define QR_ANON2        1024            /* Anonymous-Option room            */
225 #define QR_NETWORK      2048            /* Shared network room              */
226 #define QR_PREFONLY     4096            /* Preferred users only             */
227
228 struct fullroom {
229         long FRnum[MSGSPERRM];          /* Message NUMBERS                  */
230                 };
231
232 [Note that all components start with "QR" for quickroom, to make sure we
233  don't accidentally use an offset in the wrong structure. Be very careful
234  also to get a meaningful sequence of components --
235  some C compilers don't check this sort of stuff either.]
236
237 QRgen handles the problem of rooms which have died and been reborn
238 under another name.  This will be clearer when we get to the userlog.
239 For now, just note that each room has a generation number which is
240 bumped by one each time it is recycled.
241
242 QRflags is just a bag of bits recording the status of the room.  The
243 defined bits are:
244
245 QR_BUSY         This is to insure that two processes don't update the same
246                 record at the same time, even though this hasn't been
247                 implemented yet.
248 QR_INUSE        1 if the room is valid, 0 if it is free for re-assignment.
249 QR_PRIVATE      1 if the room is not visible by default, 0 for public.
250 QR_PASSWORDED   1 if entry to the room requires a password.
251 QR_GUESSNAME    1 if the room can be reached by guessing the name.
252 QR_DIRECTORY    1 if the room is a window onto some disk/userspace, else 0.
253 QR_UPLOAD       1 if users can upload into this room, else 0.
254 QR_DOWNLOAD     1 if users can download from this room, else 0.
255 QR_VISDIR       1 if users are allowed to read the directory, else 0.
256 QR_ANONONLY     1 if all messages are to recieve the "****" anon header.
257 QR_ANON2        1 if the user will be asked if he/she wants an anon message.
258 QR_NETWORK      1 if this room is shared on a network, else 0.
259 QR_PREFONLY     1 if the room is only accessible to preferred users, else 0.
260
261 QRname is just an ASCII string (null-terminated, like all strings)
262 giving the name of the room.
263
264 QRdirname is meaningful only in QR_DIRECTORY rooms, in which case
265 it gives the directory name to window.
266
267 QRpasswd is the room's password, if it's a QR_PASSWORDED room. Note that
268 if QR_PASSWORDED or QR_GUESSNAME are set, you MUST also set QR_PRIVATE.
269 QR_PRIVATE by itself designates invitation-only. Do not EVER set all three
270 flags at the same time.
271
272 QRroomaide is the user number of the room's room-aide (or zero if the room
273 doesn't have a room aide). Note that if a user is deleted, his/her user number
274 is never used again, so you don't have to worry about a new user getting the
275 same user number and accidentally becoming a room-aide of one or more rooms.
276
277 The only field new to us in quickroom is QRhighest, recording the
278 most recent message in the room.  When we are searching for rooms with
279 messages a given caller hasn't seen, we can check this number
280 and avoid a whole lot of extra disk accesses.
281
282    The fullroom is the array of pointers into the message file. We keep one
283 file for each fullroom array to keep the quickroom file small (and access time
284 efficient). FRnum are the message numbers on disk of
285 each message in the room. (For NIL, we stick zeroes in both fields.)
286
287                         user records (usersupp)
288
289 This is the fun one.  Get some fresh air and plug in your thinking cap
290 first.  (Time, space and complexity are the usernum software rivals.
291 We've got lots of log entries times lots of messages spread over up to nnn
292 rooms to worry about, and with multitasking, disk access time is important...
293 so perforce, we opt for complexity to keep time and space in bounds.)
294
295 To understand what is happening in the log code takes a little persistence.
296 You also have to disentangle the different activities going on and
297 tackle them one by one.
298
299  o      We want to remember some random things such as terminal screen
300         size, and automatically set them up for each caller at login.
301
302  o      We want to be able to locate all new messages, and only new
303         messages, efficiently.  Messages should stay new even if it
304         takes a caller a couple of calls to get around to them.
305
306  o      We want to remember which private rooms a given caller knows
307         about, and treat them as normal rooms.  This means mostly
308         automatically seeking out those with new messages.  (Obviously,
309         we >don't< want to do this for unknown private rooms!)  This
310         has to be secure against the periodic recycling of rooms
311         between calls.
312
313  o      We want to support private mail to a caller.
314
315  o      We want to provide some protection of this information (via
316         passwords at login) and some assurance that messages are from
317         who they purport to be from (within the system -- one shouldn't
318         be able to forge messages from established users).
319
320 Lifting another page from citadel.h gives us:
321
322 struct usersupp {                       /* User record                      */
323         int USuid;                      /* uid account is logged in under   */
324         char password[20];              /* password (for BBS-only users)    */
325         long lastseen[MAXROOMS];        /* Last message seen in each room   */
326         char generation[MAXROOMS];      /* Generation # (for private rooms) */
327         char forget[MAXROOMS];          /* Forgotten generation number      */
328         long mailnum[MAILSLOTS];        /* Message #'s of each mail message */
329         long mailpos[MAILSLOTS];        /* Disk positions of each mail      */
330         unsigned flags;                 /* See US_ flags below              */
331         int screenwidth;                /* For formatting messages          */
332         int timescalled;                /* Total number of logins           */
333         int posted;                     /* Number of messages posted (ever) */
334         char fullname[26];              /* Bulletin Board name for messages */
335         char axlevel;                   /* Access level                     */
336         char spare[3];                  /* spare bytes for future use       */
337         long usernum;                   /* Eternal user number              */
338         long lastcall;                  /* Last time the user called        */
339                                 };
340
341 #define US_PERM         1               /* Permanent user; don't scroll off */
342 #define US_LASTOLD      16              /* Print last old message with new  */
343 #define US_EXPERT       32              /* Experienced user                 */
344 #define US_UNLISTED     64              /* Unlisted userlog entry           */
345 #define US_NOPROMPT     128             /* Don't prompt after each message  */
346 #define US_PREF         1024            /* Preferred user                   */
347  
348 Looks simple enough, doesn't it?  One topic at a time:
349
350  Random configuration parameters:
351 -screenwidth is the caller's screen width.  We format all messages to this
352 width, as best we can. flags is another bit-bag, recording whether we want
353 prompts, people who want to suppress the little automatic hints all through
354 the system, etc.
355  
356   Attachments, names & numbers:
357 -USuid is the uid the account was established under. For most users it will
358 be the same as BBSUID, but it won't be for users that logged in from the shell.
359 -fullname is the user's full login name.
360 -usernum is the user's ID number.  It is unique to the entire system:
361 once someone has a user number, it is never used again after the user is
362 deleted. This allows an easy way to numerically represent people.
363 -password is the user's password.
364 -axlevel is the user's access level, so we know who's an Aide, who's a problem
365 user, etc.  These are defined and listed in the system.
366
367   Feeping Creatures:
368 -timescalled is the number of times the user has called.
369 -posted is the number of messages the user has posted, public or private.
370
371   Misc stuff:
372 -lastcall holds the date and time (standard Unix format) the user called, so
373 we can purge people who haven't called in a given amount of time.
374
375   Finding new messages:
376 This is the most important.  Thus, it winds up being the most
377 elaborate.  Conceptually, what we would like to do is mark each
378 message with a bit after our caller has read it, so we can avoid
379 printing it out again next call.  Unfortunately, with lots of user
380 entries this would require adding lots of bits to each message... and
381 we'd wind up reading off disk lots of messages which would never
382 get printed.  So we resort to approximation and a small table.
383
384 The approximation comes in doing things at the granularity of
385 rooms rather than messages.  Messages in a given room are "new"
386 until we visit it, and "old" after we leave the room... whether
387 we read any of them or not.  This can actually be defended: anyone
388 who passes through a room without reading the contents probably just
389 isn't interested in the topic, and would just as soon not be dragged
390 back every visit and forced to read them.  Given that messages are
391 numbered sequentially, we can simply record the most recent message ID#
392 of each room as of the last time we visited it. Very simple.
393
394 Putting it all together, we can now compute whether a given room
395 has new messages for our current caller without going to the message base
396 index (fullroom) at all:
397
398  > We get the usersupp.lastseen[] for the room in question
399  > We compare this with the room's quickroom.QRhighest, which tells us
400    what the most recent message in the room is currently.
401
402
403              REMEMBERING WHICH PRIVATE ROOMS TO VISIT
404
405 This looks trivial at first glance -- just record one bit per room per
406 caller in the log records.  The problem is that rooms get recycled
407 periodically, and we'd rather not run through all the log entries each
408 time we do it.  So we adopt a kludge which should work 99% of the time.
409
410 As previously noted, each room has a generation number, which is bumped
411 by one each time it is recycled.  As not noted, this generation number
412 runs from 0 -> 127 (and then wraps around and starts over). 
413   When someone visits a room, we set usersupp.generation for the room
414 equal to that of the room.  This flags the room as being available.
415 If the room gets recycled, on our next visit the two generation numbers
416 will no longer match, and the room will no longer be available -- just
417 the result we're looking for.  (Naturally, if a room is public,
418 all this stuff is irrelevant.)
419
420 This leaves only the problem of an accidental matchup between the two
421 numbers giving someone access to a Forbidden Room.  We can't eliminate
422 this danger completely, but it can be reduced to insignificance for
423 most purposes.  (Just don't bet megabucks on the security of this system!)
424 Each time someone logs in, we set all "wrong" generation numbers to -1.
425 So the room must be recycled 127 times before an accidental matchup
426 can be achieved.  (We do this for all rooms, INUSE or dead, public
427 or private, since any of them may be reincarnated as a Forbidden Room.)
428
429 Thus, for someone to accidentally be led to a Forbidden Room, they
430 must establish an account on the system, then not call until some room
431 has been recycled 127 to 128 times, which room must be
432 reincarnated as a Forbidden Room, which someone must now call back
433 (having not scrolled off the userlog in the mean time) and read new
434 messages.  The last clause is about the only probable one in the sequence.
435 The danger of this is much less than the danger that someone will
436 simply guess the name of the room outright (if it's a guess-name room)
437 or some other human loophole.
438
439                      FORGOTTEN ROOMS
440
441   This is exactly the opposite of private rooms. When a user chooses to
442 forget a room, we put the room's generation number in usersupp.forget for
443 that room. When doing a <K>nown rooms list or a <G>oto, any matchups cause
444 the room to be skipped. Very simple.
445
446                      SUPPORTING PRIVATE MAIL
447
448 Can one have an elegant kludge?  This must come pretty close.
449
450 Private mail is sent and recieved in the Mail> room, which otherwise
451 behaves pretty much as any other room.  To make this work, we store
452 the actual message pointers in mailnum[] and mailpos[] in the caller's
453 log record, and then copy them into the Mail> room array whenever we
454 enter the room.  This requires a little fiddling to get things just
455 right.  We have to update quickroom[1].QRhighest at login
456 to reflect the presence or absence of new messages, for example.  And
457 make_message() has to be kludged to ask for the name of the recipient
458 of the message whenever a message is entered in Mail>.  But basically
459 it works pretty well, keeping the code and user interface simple and
460 regular.
461
462
463                    PASSWORDS AND NAME VALIDATION
464  
465   This has changed a couple of times over the course of Citadel's history.  At
466 this point it's very simple, again due to the fact that record managers are
467 used for everything.    The user file (usersupp) is indexed using the user's
468 name, converted to all lower-case.  Searching for a user, then, is easy.  We
469 just lowercase the name we're looking for and query the database.  If no
470 match is found, it is assumed that the user does not exist.
471    
472   This makes it difficult to forge messages from an existing user.  (Fine
473 point: nonprinting characters are converted to printing characters, and
474 leading, trailing, and double blanks are deleted.)