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