Debian Codegraph @

wdoc/Socket_Server/SOCKET_interface.html

raw
1 <HTML>
2
3 <HEAD>
4 <TITLE>ACEDB 4_8 Sockets-based Version Of The Client/Server</TITLE>
5
6 <LINK rel=stylesheet type="text/css"
7 href="../wdoc.css"
8 title="Style sheet for wdoc developer documents">
9
10 </HEAD>
11
12
13 <BODY>
14
15 <BLOCKQUOTE>
16
17 <DIV ALIGN="CENTER">
18 <IMG SRC="./acedb-logo.gif" ALT="[ACEDB 4_8 DOCUMENTATION]" align=middle>
19 <H3>
20 ACEDB Version 4_8
21 </H3>
22 </DIV>
23
24
25 <DIV ALIGN=CENTER>
26
27 <H1>
28 Interfacing to the Sockets-based Version Of The ACEDB Client/Server
29 </H1>
30
31 <ADDRESS>Originally written by<BR>
32 Ed Griffiths
33 <A href="mailto:[email protected]">&lt;[email protected]&gt</A>,
34 February 2000</ADDRESS>
35
36 </DIV>
37
38 <H3>Conventions used in this document</H3>
39
40 <P>Note that strings such as "johnbrown" should be interpreted in the sense of
41 <B>C</B> programming language strings:
42 <PRE>
43 in this document in memory
44
45 <B>"johnbrown"</B> <B>johnbrown0</B>
46 </PRE>
47 i.e. the string does not include the quotes, but <B>does</B> include a terminating
48 byte whose unsigned value is zero (the 'null' terminator).
49
50
51 <H2>Overview</H2>
52
53 <H3>Background</H3>
54
55 <P>The first implementation of the ACEDB client/server code used <CODE>RPC</CODE> for
56 inter process communication (<CODE>IPC</CODE>). This method was originally
57 chosen because it offered the promise of a simple (function call based)
58 and portable means of making requests
59 between the client and server. In practice <CODE>RPC</CODE> suffered from
60 several major disadvantages:
61 <UL>
62 <LI>although <CODE>RPC</CODE> was offered by all major unix vendors, their
63 implementations turned out to be buggy, in different ways (ironically,
64 this was especially true of Suns implementation).
65 <LI>although <CODE>RPC</CODE> offers a very simple function call interface
66 for <CODE>IPC</CODE>, error handling is not so simple and does not
67 offer the fine grained control that other methods of <CODE>IPC</CODE> do.
68 <LI><CODE>RPC</CODE> for MS Windows is not robust and does not completely follow the
69 Unix standard.
70 <LI><CODE>DCE RPC</CODE> using the DCE version of RPC would help with portability for
71 both Unix and MS Windows, but the DCE version is often either not available or not
72 available for free.
73 <LI>many other code subsytems offer packages for sockets communication (e.g. Perl) but not
74 <CODE>RPC</CODE>: using sockets makes it easier to interface to the server.
75 <LI>On unix at least, <CODE>RPC</CODE> requires several daemons to be running (portmapper,
76 inetd) and this makes the system susceptible to system set up/reboot problems.
77 </UL>
78 The decision was taken to switch to socket based <CODE>IPC</CODE> because it overcomes
79 the above difficulties. Sockets of course present their own difficulties because the interface
80 to them is more complex to program than <CODE>RPC</CODE>, it's greater flexibility/portability
81 and ubiquitousness made this a worthwhile trade off.
82
83
84 <H3>Socket Types</H3>
85
86 There are several other variants of socket based protocols apart from RPC:
87 <UL>
88 <LI>local domain
89 <LI>UDP
90 <LI>TCP
91 </UL>
92
93 <P>Local domain sockets are about 2 to 3 times as fast as UDP or TCP when run on a single
94 machine, although the server does not currently support these it may be a worthwhile
95 addition in the future.
96
97 <P>UDP is not appropriate for the ACEDB client/server as it is a "fire and forget" protocol
98 with no guarantee that messages actually arrive at their destination.
99
100 <P>TCP on the other hand is a full duplex hand-shaking protocol with error handling that
101 will reliably deliver messages or report faults.
102
103 The ACEDB sockets based server currently supports communication via TCP only.
104
105 <H3>Byte swapping</H3>
106
107 <P>We make the assumption that the only two architectures that we will encounter are
108 Most Significant Byte first (MSB) or Least Significant Byte first (LSB).
109
110 <P>The client is not required to know anything about the byte order of the host where the
111 server is running, the only action the client must take is to set the magic byte of the message header
112 so that the server will be able to detect which order the client machine uses. The server will
113 take care of any swapping required. In fact this is usually minimal, involving
114 only the message header to make sure that messages are interpreted correctly.
115
116
117 <H3><A name="PassWord">The Password System.</A></H3>
118
119 <P>Users passwords are encrypted using the MD5 algorithm. The intention is to provide
120 a level of security that is similar to UNIX logon passwords, the system is not infallible
121 but helps to deter casual misuse.
122
123 <P>The MD5 algorithm can be used to hash together any number of strings into a single
124 128 bit value that uniquely represents those strings. The server handles
125 such hashes as the 32-byte hexadecimal string version of the number, i.e. each byte in the
126 original number is turned into the two characters in the range 0-f that represent the
127 unsigned version of that byte in hexadecimal, e.g. the byte whose unsigned decimal value
128 is 90 would be "5a". (This conversion of MD5 output to a hex string is common place
129 and any package that implements MD5 will probably implement this simple transform as well.)
130
131 <P>Hence a typical hash of the userid and password would give:
132 <PRE><CODE>
133 Userid: <B>"johnbrown"</B>
134
135 Password: <B>"veryobvious"</B>
136
137 Hex string of hash: <B>"48aad355983902438c109890a52d08f3"</B>
138 </CODE></PRE>
139
140 <P>The server does this conversion for serveral reasons:
141 <UL>
142 <LI>the md5 algorithm requires strings as input and we need to use
143 some of the md5 output as input into a new md5 hash.
144 <LI>it makes all handling of the encrypted data much simpler.
145 <LI>using hex means that the string we produce consists entirely of the
146 digits 0-9 and the letters a-f (i.e. no unprintable chars).
147 <LI>the passwd hash can be kept in a plain text file in the database.
148 </UL>
149
150 <P>With md5, any number of strings can be hashed into one single "compound"
151 hash, but the <B>order</B> in which this is done is important. In the following
152 description of the hashing for the server password system it is essential to
153 get the order right. A phrase like
154 <I>"Client does an MD5 hash of the userid followed by the password"</I>
155 means do the following (in pseudo code):
156
157 <PRE><CODE>
158 MD5Init()
159 MD5Update( the_userid )
160 MD5Update( the_passwd )
161 MD5Final()
162
163 </CODE></PRE>
164
165 <P>In particular its important to note that the userid and password are always
166 combined in a separate hash, the result of this hash is then combined in a new
167 separate hash with the random value sent by the server. This is explained in
168 more detail in the following paragraphs.
169
170 <P>The sequence for normal authentification between client and server is:
171 <OL>
172 <LI>Client makes initial contact with server.
173 <LI>Server sends a 32-byte hex string of an MD5 hash of an essentially random value.
174
175 <PRE><CODE><B>"8dd9ca0ac7614ba72cf4eaa71303c46d"</B></CODE></PRE>
176
177 <LI>The client must now supply a userid and password (perhaps by prompting the user
178 for them).
179
180 <PRE><CODE>userid: <B>"johnbrown"</B> password: <B>"veryobvious"</B></CODE></PRE>
181
182 <LI>Client does an MD5 hash of the userid followed by the password
183 and creates a 32-byte hex string of this hash
184
185 <PRE><CODE><B>"48aad355983902438c109890a52d08f3"</B></CODE></PRE>
186
187 <LI>Client does a new MD5 hash of the userid/password hex string followed by
188 the servers random value and creates a 32-byte hex string of the resultant compound hash.
189
190 <PRE><CODE><B>"54d4fd5715369ebded7153d96fc665f8"</B></CODE></PRE>
191
192 <LI>Client sends the userid and this new hash to the server.
193
194 <PRE><CODE><B>"johnbrown 54d4fd5715369ebded7153d96fc665f8"</B></CODE></PRE>
195
196 <LI>Server checks client hash against its hash of the random value and the userid/password
197 hash that it holds for that userid.
198
199 <PRE><CODE>userid/password hash for "johnbrown": <B>"48aad355983902438c109890a52d08f3"</B>
200 random value for client: <B>"8dd9ca0ac7614ba72cf4eaa71303c46d"</B>
201 gives a hash of: <B>"54d4fd5715369ebded7153d96fc665f8"</B>
202 client hash was: <B>"54d4fd5715369ebded7153d96fc665f8"</B>
203
204 Hashes are identical so the client is authenticated.
205 </CODE></PRE>
206 </OL>
207
208 <P>This way all hashes are handled as normal strings, passwords are never sent over the network
209 as plain text, and its not possible for anyone listening to simply reuse the client userid/password
210 hash because its combined with a different random value for each client session.
211
212 <P>The intent here is to provide "reasonable" security, its still possible to breach this
213 system but only by someone who knows a reasonable amount about networks/MD5
214 and so on.
215
216
217
218 <H2>Client/Server Protocol</H2>
219
220 This section describes:
221
222 <UL>
223 <LI>format of messages passed between client and server
224 <LI>signing on
225 <LI>normal transactions
226 <LI>'sliced' transactions
227 <LI>client terminations
228 <LI>errors
229 <LI>unsolicited messages
230 <LI>abnormal server termination
231 <LI>time outs
232 </UL>
233
234
235 <H3>Message format</H3>
236
237 <P>All messages have the same format summarised in the below diagram:
238
239 <P align=center>
240 <IMG SRC="./msgformat.png" ALT="[Message format]" align=middle>
241 </P>
242
243 <P><B>The format crucially relies on both client and server being able to correctly
244 format 4-byte integers into the header. With the exception of the <CODE>msgType</CODE>
245 field, all the header fields are integers. Clients on machines that do not support 4-byte integers
246 will have to produce either the LSB or MSB 4-byte integer format themselves.</B>
247
248 <P>The header fields have the following content:
249
250 <DL>
251 <DT><B>Magic</B>
252 <DD>This is the first field in the message and must be set by the client to the
253 the hexadecimal value <B><CODE>0x12345678</CODE></B>. It is used by the server
254 to detect whether the client is running on a machine with a different byte order
255 from the server and hence whether the server should byte-swop the integer fields
256 in the message.
257 <DT><B>length</B>
258 <DD>This field gives the 32-bit integer length of the message body in bytes.
259 <DT><B>version</B>
260 <DD>This field gives the current protocol version, intended use is to allow changes
261 in protocol in a way that the server and client can detect. Currently this is
262 set to 1.
263 <DT><B>clientID</B>
264 <DD>This field is filled in by the server, it is a unique ID that identifies the client
265 to the server, the client should not change this.
266 <DT><B>maxBytes</B>
267 <DD>This field gives the maximum reply length to be returned by the server. The server always
268 returns the same value in this field (either the default or a user specified value taken from the
269 command line when the server was started up. The client may set the field to any value between
270 zero and the servers value and the server will return replies up to that value (smaller or larger
271 values will are replaced with the servers value).
272 <DT><B>msgType</B>
273 <DD>The most important field, this is not an integer but a string up to 30 bytes long
274 <B>including</B> the terminating <CODE>NULL</CODE> char. This string identifies
275 the type of the message, the types are described below. A string is used to try
276 to allow robust interfacing by languages other than <CODE>C</CODE>, if enums were used
277 these may be inadvertantly changed thus breaking the protocol. In addition, it's
278 easier to see how the server is replying when testing a new client if the msgType is a string.
279 </DL>
280
281 <P>The msgType field can take the following values currently:
282 <DL>
283 <DT><B>"ACESERV_MSGREQ"</B>
284 <DD>Used by the client to indicate that the message body is a command
285 for the server to execute.
286 <DT><B>"ACESERV_MSGDATA"</B>
287 <DD>Used by the client to indicate that the message body contains not
288 a command but ace format data that should be parsed into the database.
289 <DT><B>"ACESERV_MSGOK"</B>
290 <DD>Set by the server to indicate that the last request from the client
291 completed successfully.
292 <DT><B>"ACESERV_MSGENCORE"</B>
293 <DD>Set by the server to indicate that the result of a request is so large that only part
294 of it has been passed back and the client must ask the server for the rest.
295 <DT><B>"ACESERV_MSGFAIL"</B>
296 <DD>Set by the server to indicate that a request has failed, it is combined with some text
297 that gives the reason for the failure.
298 <DT><B>"ACESERV_MSGKILL"</B>
299 <DD>Set by the server to indicate that it is terminating the clients connection, it is
300 combined with some text to show the reason for the termination. After receiving this
301 message the client will not be able to communicate with the server any more because
302 the server will have closed its end of the connection.
303 </DL>
304
305 <P>Setting of these fields by the client and server is as follows:
306 <DL>
307 <DT><B>Magic</B>
308 <DD>Must be set only by the client the first time the client contacts the server. The client
309 does not need to look at this field because all byte swapping is done by the server.
310 In theory this field only needs to be looked at on the first transaction between client
311 and server but currently the server looks every time so the client must send the
312 same value every time.
313 <DT><B>length</B>
314 <DD>Must be set to the length of the body of the message
315 by the client or the server whenever they send messages to each other.
316
317 <P><B>NOTE</B> that for strings the length sent must be the length of the characters
318 in the string plus the usual terminating null char for the string.
319 <DT><B>version</B>
320 <DD>Set only by the server the first time it replies to a new client (the same value will
321 be sent by the server with each message).
322 <DT><B>clientID</B>
323 <DD>Set by the server the first time it replies to the client, the client <B>must</B> return
324 this value each time it contacts the server thereafter. The server will send
325 the same value each time it replies to the client.
326 <DT><B>maxBytes</B>
327 <DD>Set by the server the first time it replies to a new client (the same value will
328 be sent by the server with each reply).
329 <DT><B>msgType</B>
330 <DD>Must be set by the client or the server whenever they send messages to each other.
331 </DL>
332
333 <P>More detailed descriptions of how these fields are set/used are given in the following sections
334 showing the typical transactions between client and server.
335
336
337 <H3>Signing on to the server</H3>
338
339 <P>Normal "sign on" is a four message transaction with the client passing the
340 users password in an encrypted form that is unique to this particular sign on.
341 As with Unix passwords, the server stores the users password in an encrypted
342 form and the password is never sent as plain text between server and client.
343
344 <P>The client must set three fields in the header in the initial "hello" message:
345 the "magic" byte swapping number, the string length of the hello message (13),
346 the message type ("ACESERV_MSGREQ"). The message body must be set to
347 "bonjour".
348
349 <P>The server will respond with an MD5 encrypted 32-byte character string
350 which the client must use to hash with its MD5 encryption of the users userid
351 and password
352 (see <A HREF="./SOCKET_interface.html#PassWord">"The Password System"</A>
353 for more details of the encryption mechanics).
354
355 <P align=center>
356 <IMG SRC="./sigonmsg.png" ALT="[Message format]" align=middle>
357 </P>
358
359 <P>The client now responds with its hash of the users userid and password with the string
360 sent by the server and sends it back.
361
362 <P>If the server matches the hash to the one it has for the user then it will reply
363 with a welcome message ("et bonjour a vous") and the client will be successfully
364 connected.
365
366
367
368 <H3>A normal transaction</H3>
369
370 <P>To make a request the only field the client needs to <B>change</B> in the header
371 from the initial sign on is the message length field which must be set to the
372 length of the request. The message body is the request which is "find model" in this
373 case.
374
375 <P align=center>
376 <IMG SRC="./normalreq.png" ALT="[Message format]" align=middle>
377 </P>
378
379 <P>The server will then respond with a message, the client should check the
380 msgType field to see if the request succeeded. If the request succeeded the
381 message body will contain the results of the request, if it failed it will
382 contain a string which explains the failure.
383
384
385 <H3>A sliced transaction</H3>
386
387 <P>It's possible for a client to issue a request to the server which would result
388 in an enormous reply, this would then prevent other clients from interacting with
389 the server while the reply was being contructed. To avoid this the server will
390 only produce the reply a "slice" at a time. This means that the client must be
391 ready to receive the first slice and then ask the server for the next and so on.
392
393 <P>The initial client request is as normal, in the server response however the
394 msgType field is set to <B>"ACESERV_MSGENCORE"</B> to show that the response is incomplete.
395
396 <P align=center>
397 <IMG SRC="./encore.png" ALT="[Message format]" align=middle>
398 </P>
399
400 <P>The client must respond to the server with an <B>"encore"</B> request to
401 get further slices. This continues until the server replies with a message
402 where msgType is set to <B>"ACESERV_MSGOK"</B> indicating that the response
403 is now complete.
404
405 <P><B>Note</B> that the length supplied with each "encore" message is the length
406 of that slice only, it is the responsibility of the client to keep track of
407 how much data has been passed back so far.
408
409
410 <H3>Normal termination</H3>
411
412 <P>To terminate a session the client sends a normal request but with the
413 message body set to "quit", the server will then send a termination message
414 with the msgType set to <B>"ACESERV_MSGKILL"</B>. Once the client receives a
415 message with this msgType it should not try to send further messages to the
416 server because the server will have closed its end of the connection.
417
418 <P align=center>
419 <IMG SRC="./quit.png" ALT="[Message format]" align=middle>
420 </P>
421
422 <P>While the client can close a connection simply by exitting, it is cleaner to
423 send the "quit" message to the server and wait for the <B>"ACESERV_MSGKILL"</B>
424 reply.
425
426
427
428 <H3><A name="Shutdown">Normal shutdown</A></H3>
429
430
431 <P>To shutdown the server the client sends a normal request but with the
432 message body set to "shutdown" (perhaps with the "now" and "norestart" options").
433 The server will then send a termination message
434 with the msgType set to <B>"ACESERV_MSGKILL"</B> and depending on the
435 shutdown options will terminate either immediately or when the last client
436 quits.
437
438 <P align=center>
439 <IMG SRC="./shutdown.png" ALT="[Message format]" align=middle>
440 </P>
441
442 If the "now" option was specified then all other clients will be sent the termination
443 message and their connections will be closed
444 <A HREF="./SOCKET_interface.html#Unsolicited">immediately</A>.
445
446
447
448
449 <H3>Errors</H3>
450
451 <P>If the client sends a request that is in some way incorrect then the server will
452 reply with a message with msgType set to <B>"ACESERV_MSGFAIL"</B> and the message
453 body will contain some text to explain the problem. This mechanism is only used for
454 non-fatal errors, fatal errors will result in the server sending a message of type
455 <B>"ACESERV_MSGKILL"</B> and the clients connection with the server will be terminated.
456
457 <P align=center>
458 <IMG SRC="./error.png" ALT="[Message format]" align=middle>
459 </P>
460
461
462
463 <H3><A name="Unsolicited">The server can send Unsolicited Messages</A></H3>
464
465 <P>Under certain circumstances the server may send a client an unsolicited
466 <B>"ACESERV_MSGKILL"</B> message and close its connection.
467
468 <P align=center>
469 <IMG SRC="./unsolicited.png" ALT="[Message format]" align=middle>
470 </P>
471
472 <P>This can happen under the following circumstances:
473 <UL>
474 <LI>If the server is sent a request to shutdown
475 <A HREF="./SOCKET_interface.html#Shutdown">immediately</A>
476 then the server will send a termination messages to all
477 clients and the connections of those clients will be closed.
478 <LI>If a client <A HREF="./SOCKET_interface.html#Timeout">times out</A> then the server will send a termination message to
479 that client and its connection will be closed.
480 <LI>If the server <A HREF="./SOCKET_interface.html#Timeout">times out</A>
481 then the server will send a termination messages to all
482 clients and the connections of those clients will be closed.
483 </UL>
484 These are the only cases
485 where communication between client and server is not initiated by the client.
486
487
488 <H3>When the server dies</H3>
489
490 <P>If the server dies unexpectedly, perhaps because of some internal error, then the
491 client will only detect this if it is monitoring the connection to the server. In this
492 case the client will receive some notification from the operating system (e.g. a signal
493 to show a "broken pipe" on a write to the connection or perhaps a null return from
494 a read from the connection). If this happens it generally indicates that a serious error
495 has occurred in the server and that no recovery could be made or termination messages sent
496 to clients.
497
498
499 <H3><A name="Timeout">Time outs, client and server</A></H3>
500
501 <P>Clients who connect and then do not send any requests to the server for the <B>client timeout</B>
502 period (default is 600 seconds) will be sent
503 an <B>"ACESERV_MSGKILL"</B> message and their connection will be closed. Similarly if there
504 are no requests to the server at all for the <B>server timeout</B> period (default is 600 seconds),
505 then all clients will be sent an <B>"ACESERV_MSGKILL"</B> message and their connections will be closed.
506
507
508
509 <H3><A name="Admin">Format of admin commands</A></H3>
510
511 <P>As the admin commands have a slightly more complex format (they include passwords,
512 that in the acedb client are prompted for from the command line), this section
513 shows the actual format of the command strings that must be sent across to the server.
514
515 <p>For each command the syntax is given followed by an example of an actual string
516 that would be sent across the server.
517
518
519 <P><b>"user" command</b>
520
521 <pre>
522 Purpose: This command has several forms enabling an admin user
523 to create/update/delete userids.
524
525 syntax: <code>user passwd|group|new|delete userid [password group]</code>
526
527
528 Create new user:
529 <code>user new new_userid new_group user_passwd_hash</code>
530
531 e.g. "user new robbie read a9ab347d3c85d6eb12ad9f4b385ed6a5"
532
533
534 Delete user:
535 <code>user delete userid</code>
536
537 e.g. "user delete robbie"
538
539
540 Change users group:
541 <code>user group userid new_group</code>
542
543 e.g. "user group robbie write"
544
545
546 Change users password:
547 <code>user passwd userid user_passwd_hash</code>
548
549 e.g. "user passwd robbie 601ce17ce1fffbf5d457ad98a21360d5"
550 </pre>
551
552
553
554 <P><b>"global" command</b>
555
556 <pre>
557 Purpose: Change global read/write permissions for database
558
559 syntax: <code>global read|write none|passwd|world</code>
560
561
562 Change global read perms:
563 <code>global read none|passwd|world</code>
564
565 e.g. "global read none"
566
567 Change global write perms:
568 <code>global write none|passwd|world</code>
569
570 e.g. "global write world"
571 </pre>
572
573
574
575 <P><b>"domain" command</b>
576
577 <pre>
578 Purpose: This command is similar to the user command but enables the admin user
579 to create/update/delete domain names:
580
581 syntax: <code>domain group|new|delete domain-name [group]</code>
582
583 Create new domain:
584 <code>domain new new_domain new_group</code>
585
586 e.g. "domain new mynew.domain.new write"
587
588
589 Delete domain:
590 <code>domain delete domain</code>
591
592 e.g. "domain delete sanger.ac.uk"
593
594
595 Change domains group:
596 <code>domain group domain new_group</code>
597
598 e.g. "domain group ibm.com read"
599 </pre>
600
601
602 <P>
603 <HR>
604 <H3 align="center">ACEDB Version 4_8</H3>
605 <HR>
606 <ADDRESS>Ed Griffiths <A href="mailto:[email protected]">&lt;[email protected]&gt;</A></ADDRESS>
607 <!-- hhmts start -->
608 Last modified: Mon Sep 23 13:50:21 BST 2002
609 <!-- hhmts end -->
610
611
612 </BLOCKQUOTE>
613 </BODY>
614 </HTML>