grrr...fix reverse chronological order
[dragonfly.git] / lib / libcr / rpc / PSD.doc / rpc.rfc.ms
1 .\"
2 .\" Must use  --  tbl  --  with this one
3 .\"
4 .\" @(#)rpc.rfc.ms      2.2 88/08/05 4.0 RPCSRC
5 .\" $FreeBSD: src/lib/libc/rpc/PSD.doc/rpc.rfc.ms,v 1.1.14.1 2000/11/24 09:36:30 ru Exp $
6 .\" $DragonFly: src/lib/libcr/rpc/PSD.doc/Attic/rpc.rfc.ms,v 1.2 2003/06/17 04:26:45 dillon Exp $
7 .\"
8 .so stubs
9 .de BT
10 .if \\n%=1 .tl ''- % -''
11 ..
12 .ND
13 .\" prevent excess underlining in nroff
14 .if n .fp 2 R
15 .OH 'Remote Procedure Calls: Protocol Specification''Page %'
16 .EH 'Page %''Remote Procedure Calls: Protocol Specification'
17 .if \n%=1 .bp
18 .SH
19 \&Remote Procedure Calls: Protocol Specification
20 .LP
21 .NH 0
22 \&Status of this Memo
23 .LP
24 Note: This chapter specifies a protocol that Sun Microsystems, Inc.,
25 and others are using.  
26 It has been designated RFC1050 by the ARPA Network
27 Information Center.
28 .LP
29 .NH 1
30 \&Introduction
31 .LP
32 This chapter specifies  a  message protocol  used in implementing
33 Sun's Remote Procedure Call (RPC) package.  (The message protocol is
34 specified with the External Data Representation (XDR) language.
35 See the
36 .I "External Data Representation Standard: Protocol Specification"
37 for the details.  Here, we assume that  the  reader is familiar  
38 with XDR and do not attempt to justify it or its uses).  The paper
39 by Birrell and Nelson [1]  is recommended as an  excellent background
40 to  and justification of RPC.
41 .NH 2
42 \&Terminology
43 .LP
44 This chapter discusses servers, services, programs, procedures,
45 clients, and versions.  A server is a piece of software where network
46 services are implemented.  A network service is a collection of one
47 or more remote programs.  A remote program implements one or more
48 remote procedures; the procedures, their parameters, and results are
49 documented in the specific program's protocol specification (see the
50 \fIPort Mapper Program Protocol\fP\, below, for an example).  Network
51 clients are pieces of software that initiate remote procedure calls
52 to services.  A server may support more than one version of a remote
53 program in order to be forward compatible with changing protocols.
54 .LP
55 For example, a network file service may be composed of two programs.
56 One program may deal with high-level applications such as file system
57 access control and locking.  The other may deal with low-level file
58 IO and have procedures like "read" and "write".  A client machine of
59 the network file service would call the procedures associated with
60 the two programs of the service on behalf of some user on the client
61 machine.
62 .NH 2
63 \&The RPC Model
64 .LP
65 The remote procedure call model is similar to the local procedure
66 call model.  In the local case, the caller places arguments to a
67 procedure in some well-specified location (such as a result
68 register).  It then transfers control to the procedure, and
69 eventually gains back control.  At that point, the results of the
70 procedure are extracted from the well-specified location, and the
71 caller continues execution.
72 .LP
73 The remote procedure call is similar, in that one thread of control
74 logically winds through two processes\(emone is the caller's process,
75 the other is a server's process.  That is, the caller process sends a
76 call message to the server process and waits (blocks) for a reply
77 message.  The call message contains the procedure's parameters, among
78 other things.  The reply message contains the procedure's results,
79 among other things.  Once the reply message is received, the results
80 of the procedure are extracted, and caller's execution is resumed.
81 .LP
82 On the server side, a process is dormant awaiting the arrival of a
83 call message.  When one arrives, the server process extracts the
84 procedure's parameters, computes the results, sends a reply message,
85 and then awaits the next call message.
86 .LP
87 Note that in this model, only one of the two processes is active at
88 any given time.  However, this model is only given as an example.
89 The RPC protocol makes no restrictions on the concurrency model
90 implemented, and others are possible.  For example, an implementation
91 may choose to have RPC calls be asynchronous, so that the client may
92 do useful work while waiting for the reply from the server.  Another
93 possibility is to have the server create a task to process an
94 incoming request, so that the server can be free to receive other
95 requests.
96 .NH 2
97 \&Transports and Semantics
98 .LP
99 The RPC protocol is independent of transport protocols.  That is, RPC
100 does not care how a message is passed from one process to another.
101 The protocol deals only with specification and interpretation of
102 messages.
103 .LP
104 It is important to point out that RPC does not try to implement any
105 kind of reliability and that the application must be aware of the
106 type of transport protocol underneath RPC.  If it knows it is running
107 on top of a reliable transport such as TCP/IP[6], then most of the
108 work is already done for it.  On the other hand, if it is running on
109 top of an unreliable transport such as UDP/IP[7], it must implement
110 is own retransmission and time-out policy as the RPC layer does not
111 provide this service.
112 .LP
113 Because of transport independence, the RPC protocol does not attach
114 specific semantics to the remote procedures or their execution.
115 Semantics can be inferred from (but should be explicitly specified
116 by) the underlying transport protocol.  For example, consider RPC
117 running on top of an unreliable transport such as UDP/IP.  If an
118 application retransmits RPC messages after short time-outs, the only
119 thing it can infer if it receives no reply is that the procedure was
120 executed zero or more times.  If it does receive a reply, then it can
121 infer that the procedure was executed at least once.
122 .LP
123 A server may wish to remember previously granted requests from a
124 client and not regrant them in order to insure some degree of
125 execute-at-most-once semantics.  A server can do this by taking
126 advantage of the transaction ID that is packaged with every RPC
127 request.  The main use of this transaction is by the client RPC layer
128 in matching replies to requests.  However, a client application may
129 choose to reuse its previous transaction ID when retransmitting a
130 request.  The server application, knowing this fact, may choose to
131 remember this ID after granting a request and not regrant requests
132 with the same ID in order to achieve some degree of
133 execute-at-most-once semantics.  The server is not allowed to examine
134 this ID in any other way except as a test for equality.
135 .LP
136 On the other hand, if using a reliable transport such as TCP/IP, the
137 application can infer from a reply message that the procedure was
138 executed exactly once, but if it receives no reply message, it cannot
139 assume the remote procedure was not executed.  Note that even if a
140 connection-oriented protocol like TCP is used, an application still
141 needs time-outs and reconnection to handle server crashes.
142 .LP
143 There are other possibilities for transports besides datagram- or
144 connection-oriented protocols.  For example, a request-reply protocol
145 such as VMTP[2] is perhaps the most natural transport for RPC.
146 .SH
147 .I
148 NOTE:  At Sun, RPC is currently implemented on top of both TCP/IP
149 and UDP/IP transports.
150 .LP
151 .NH 2
152 \&Binding and Rendezvous Independence
153 .LP
154 The act of binding a client to a service is NOT part of the remote
155 procedure call specification.  This important and necessary function
156 is left up to some higher-level software.  (The software may use RPC
157 itself\(emsee the \fIPort Mapper Program Protocol\fP\, below).
158 .LP
159 Implementors should think of the RPC protocol as the jump-subroutine
160 instruction ("JSR") of a network; the loader (binder) makes JSR
161 useful, and the loader itself uses JSR to accomplish its task.
162 Likewise, the network makes RPC useful, using RPC to accomplish this
163 task.
164 .NH 2
165 \&Authentication
166 .LP
167 The RPC protocol provides the fields necessary for a client to
168 identify itself to a service and vice-versa.  Security and access
169 control mechanisms can be built on top of the message authentication.
170 Several different authentication protocols can be supported.  A field
171 in the RPC header indicates which protocol is being used.  More
172 information on specific authentication protocols can be found in the
173 \fIAuthentication Protocols\fP\,
174 below.
175 .KS
176 .NH 1
177 \&RPC Protocol Requirements
178 .LP
179 The RPC protocol must provide for the following:
180 .IP  1.
181 Unique specification of a procedure to be called.
182 .IP  2.
183 Provisions for matching response messages to request messages.
184 .KE
185 .IP  3.
186 Provisions for authenticating the caller to service and vice-versa.
187 .LP
188 Besides these requirements, features that detect the following are
189 worth supporting because of protocol roll-over errors, implementation
190 bugs, user error, and network administration:
191 .IP  1.
192 RPC protocol mismatches.
193 .IP  2.
194 Remote program protocol version mismatches.
195 .IP  3.
196 Protocol errors (such as misspecification of a procedure's parameters).
197 .IP  4.
198 Reasons why remote authentication failed.
199 .IP  5.
200 Any other reasons why the desired procedure was not called.
201 .NH 2
202 \&Programs and Procedures
203 .LP
204 The RPC call message has three unsigned fields:  remote program
205 number, remote program version number, and remote procedure number.
206 The three fields uniquely identify the procedure to be called.
207 Program numbers are administered by some central authority (like
208 Sun).  Once an implementor has a program number, he can implement his
209 remote program; the first implementation would most likely have the
210 version number of 1.  Because most new protocols evolve into better,
211 stable, and mature protocols, a version field of the call message
212 identifies which version of the protocol the caller is using.
213 Version numbers make speaking old and new protocols through the same
214 server process possible.
215 .LP
216 The procedure number identifies the procedure to be called.  These
217 numbers are documented in the specific program's protocol
218 specification.  For example, a file service's protocol specification
219 may state that its procedure number 5 is "read" and procedure number
220 12 is "write".
221 .LP
222 Just as remote program protocols may change over several versions,
223 the actual RPC message protocol could also change.  Therefore, the
224 call message also has in it the RPC version number, which is always
225 equal to two for the version of RPC described here.
226 .LP
227 The reply message to a request  message  has enough  information to
228 distinguish the following error conditions:
229 .IP  1.
230 The remote implementation of RPC does speak protocol version 2.
231 The lowest and highest supported RPC version numbers are returned.
232 .IP  2.
233 The remote program is not available on the remote system.
234 .IP  3.
235 The remote program does not support the requested version number.
236 The lowest and highest supported remote program version numbers are
237 returned.
238 .IP  4.
239 The requested procedure number does not exist.  (This is usually a
240 caller side protocol or programming error.)
241 .IP  5.
242 The parameters to the remote procedure appear to be garbage from the
243 server's point of view.  (Again, this is usually caused by a
244 disagreement about the protocol between client and service.)
245 .NH 2
246 \&Authentication
247 .LP
248 Provisions for authentication of caller to service and vice-versa are
249 provided as a part of the RPC protocol.  The call message has two
250 authentication fields, the credentials and verifier.  The reply
251 message has one authentication field, the response verifier.  The RPC
252 protocol specification defines all three fields to be the following
253 opaque type:
254 .DS
255 .ft CW
256 .vs 11
257 enum auth_flavor {
258     AUTH_NULL        = 0,
259     AUTH_UNIX        = 1,
260     AUTH_SHORT       = 2,
261     AUTH_DES         = 3
262     /* \fIand more to be defined\fP */
263 };
264
265 struct opaque_auth {
266     auth_flavor flavor;
267     opaque body<400>;
268 };
269 .DE
270 .LP
271 In simple English, any
272 .I opaque_auth 
273 structure is an 
274 .I auth_flavor 
275 enumeration followed by bytes which are  opaque to the RPC protocol
276 implementation.
277 .LP
278 The interpretation and semantics  of the data contained  within the
279 authentication   fields  is specified  by  individual,  independent
280 authentication  protocol specifications.   (See 
281 \fIAuthentication Protocols\fP\,
282 below, for definitions of the various authentication protocols.)
283 .LP
284 If authentication parameters were   rejected, the  response message
285 contains information stating why they were rejected.
286 .NH 2
287 \&Program Number Assignment
288 .LP
289 Program numbers are given out in groups of
290 .I 0x20000000 
291 (decimal 536870912) according to the following chart:
292 .TS
293 box tab (&) ;
294 lfI lfI
295 rfL cfI .
296 Program Numbers&Description
297 _
298 .sp .5
299 0 - 1fffffff&Defined by Sun
300 20000000 - 3fffffff&Defined by user
301 40000000 - 5fffffff&Transient
302 60000000 - 7fffffff&Reserved
303 80000000 - 9fffffff&Reserved
304 a0000000 - bfffffff&Reserved
305 c0000000 - dfffffff&Reserved
306 e0000000 - ffffffff&Reserved
307 .TE
308 .LP
309 The first group is a range of numbers administered by Sun
310 Microsystems and should be identical for all sites.  The second range
311 is for applications peculiar to a particular site.  This range is
312 intended primarily for debugging new programs.  When a site develops
313 an application that might be of general interest, that application
314 should be given an assigned number in the first range.  The third
315 group is for applications that generate program numbers dynamically.
316 The final groups are reserved for future use, and should not be used.
317 .NH 2
318 \&Other Uses of the RPC Protocol
319 .LP
320 The intended use of this protocol is for calling remote procedures.
321 That is, each call message is matched with a response message.
322 However, the protocol itself is a message-passing protocol with which
323 other (non-RPC) protocols can be implemented.  Sun currently uses, or
324 perhaps abuses, the RPC message protocol for the following two
325 (non-RPC) protocols:  batching (or pipelining) and broadcast RPC.
326 These two protocols are discussed but not defined below.
327 .NH 3
328 \&Batching
329 .LP
330 Batching allows a client to send an arbitrarily large sequence of
331 call messages to a server; batching typically uses reliable byte
332 stream protocols (like TCP/IP) for its transport.  In the case of
333 batching, the client never waits for a reply from the server, and the
334 server does not send replies to batch requests.  A sequence of batch
335 calls is usually terminated by a legitimate RPC in order to flush the
336 pipeline (with positive acknowledgement).
337 .NH 3
338 \&Broadcast RPC
339 .LP
340 In broadcast RPC-based protocols, the client sends a broadcast packet
341 to the network and waits for numerous replies.  Broadcast RPC uses
342 unreliable, packet-based protocols (like UDP/IP) as its transports.
343 Servers that support broadcast protocols only respond when the
344 request is successfully processed, and are silent in the face of
345 errors.  Broadcast RPC uses the Port Mapper RPC service to achieve
346 its semantics.  See the \fIPort Mapper Program Protocol\fP\, below,
347 for more information.
348 .KS
349 .NH 1
350 \&The RPC Message Protocol
351 .LP
352 This section defines the RPC message protocol in the XDR data
353 description language.  The message is defined in a top-down style.
354 .ie t .DS
355 .el .DS L
356 .ft CW
357 enum msg_type {
358         CALL  = 0,
359         REPLY = 1
360 };
361
362 .ft I
363 /*
364 * A reply to a call message can take on two forms:
365 * The message was either accepted or rejected.
366 */
367 .ft CW
368 enum reply_stat {
369         MSG_ACCEPTED = 0,
370         MSG_DENIED   = 1
371 };
372
373 .ft I
374 /*
375 * Given that a call message was accepted,  the following is the
376 * status of an attempt to call a remote procedure.
377 */
378 .ft CW
379 enum accept_stat {
380         SUCCESS       = 0, /* \fIRPC executed successfully       \fP*/
381         PROG_UNAVAIL  = 1, /* \fIremote hasn't exported program  \fP*/
382         PROG_MISMATCH = 2, /* \fIremote can't support version #  \fP*/
383         PROC_UNAVAIL  = 3, /* \fIprogram can't support procedure \fP*/
384         GARBAGE_ARGS  = 4  /* \fIprocedure can't decode params   \fP*/
385 };
386 .DE
387 .ie t .DS
388 .el .DS L
389 .ft I
390 /*
391 * Reasons why a call message was rejected:
392 */
393 .ft CW
394 enum reject_stat {
395         RPC_MISMATCH = 0, /* \fIRPC version number != 2          \fP*/
396         AUTH_ERROR = 1    /* \fIremote can't authenticate caller \fP*/
397 };
398
399 .ft I
400 /*
401 * Why authentication failed:
402 */
403 .ft CW
404 enum auth_stat {
405         AUTH_BADCRED      = 1,  /* \fIbad credentials \fP*/
406         AUTH_REJECTEDCRED = 2,  /* \fIclient must begin new session \fP*/
407         AUTH_BADVERF      = 3,  /* \fIbad verifier \fP*/
408         AUTH_REJECTEDVERF = 4,  /* \fIverifier expired or replayed  \fP*/
409         AUTH_TOOWEAK      = 5   /* \fIrejected for security reasons \fP*/
410 };
411 .DE
412 .KE
413 .ie t .DS
414 .el .DS L
415 .ft I
416 /*
417 * The  RPC  message: 
418 * All   messages  start with   a transaction  identifier,  xid,
419 * followed  by a  two-armed  discriminated union.   The union's
420 * discriminant is a  msg_type which switches to  one of the two
421 * types   of the message.   The xid  of a \fIREPLY\fP  message always
422 * matches  that of the initiating \fICALL\fP   message.   NB: The xid
423 * field is only  used for clients  matching reply messages with
424 * call messages  or for servers detecting  retransmissions; the
425 * service side  cannot treat this id  as any type   of sequence
426 * number.
427 */
428 .ft CW
429 struct rpc_msg {
430         unsigned int xid;
431         union switch (msg_type mtype) {
432                 case CALL:
433                         call_body cbody;
434                 case REPLY:  
435                         reply_body rbody;
436         } body;
437 };
438 .DE
439 .ie t .DS
440 .el .DS L
441 .ft I
442 /*
443 * Body of an RPC request call: 
444 * In version 2 of the  RPC protocol specification, rpcvers must
445 * be equal to 2.  The  fields prog,  vers, and proc specify the
446 * remote program, its version number, and the  procedure within
447 * the remote program to be called.  After these  fields are two
448 * authentication  parameters: cred (authentication credentials)
449 * and verf  (authentication verifier).  The  two authentication
450 * parameters are   followed by  the  parameters  to  the remote
451 * procedure,  which  are specified  by  the  specific   program
452 * protocol.
453 */
454 .ft CW
455 struct call_body {
456         unsigned int rpcvers;  /* \fImust be equal to two (2) \fP*/
457         unsigned int prog;
458         unsigned int vers;
459         unsigned int proc;
460         opaque_auth cred;
461         opaque_auth verf;
462         /* \fIprocedure specific parameters start here \fP*/
463 };
464 .DE
465 .ie t .DS
466 .el .DS L
467 .ft I
468 /*
469 * Body of a reply to an RPC request:
470 * The call message was either accepted or rejected.
471 */
472 .ft CW
473 union reply_body switch (reply_stat stat) {
474         case MSG_ACCEPTED:  
475                 accepted_reply areply;
476         case MSG_DENIED:  
477                 rejected_reply rreply;
478 } reply;
479 .DE
480 .ie t .DS
481 .el .DS L
482 .ft I
483 /*
484 * Reply to   an RPC request  that  was accepted  by the server:
485 * there could be an error even though the request was accepted.
486 * The first field is an authentication verifier that the server
487 * generates in order to  validate itself  to the caller.  It is
488 * followed by    a  union whose     discriminant  is   an  enum
489 * accept_stat.  The  \fISUCCESS\fP  arm of    the union  is  protocol
490 * specific.  The \fIPROG_UNAVAIL\fP, \fIPROC_UNAVAIL\fP, and \fIGARBAGE_ARGP\fP
491 * arms of the union are void.   The \fIPROG_MISMATCH\fP arm specifies
492 * the lowest and highest version numbers of the  remote program
493 * supported by the server.
494 */
495 .ft CW
496 struct accepted_reply {
497         opaque_auth verf;
498         union switch (accept_stat stat) {
499                 case SUCCESS:
500                         opaque results[0];
501                         /* \fIprocedure-specific results start here\fP */
502                 case PROG_MISMATCH:
503                         struct {
504                                 unsigned int low;
505                                 unsigned int high;
506                         } mismatch_info;
507                 default:
508 .ft I
509                         /*
510                         * Void.  Cases include \fIPROG_UNAVAIL, PROC_UNAVAIL\fP,
511                         * and \fIGARBAGE_ARGS\fP.
512                         */
513 .ft CW
514                         void;
515         } reply_data;
516 };
517 .DE
518 .ie t .DS
519 .el .DS L
520 .ft I
521 /*
522 * Reply to an RPC request that was rejected by the server: 
523 * The request  can   be rejected for   two reasons:  either the
524 * server   is not  running a   compatible  version  of the  RPC
525 * protocol    (\fIRPC_MISMATCH\fP), or    the  server   refuses    to
526 * authenticate the  caller  (\fIAUTH_ERROR\fP).  In  case of  an  RPC
527 * version mismatch,  the server returns the  lowest and highest
528 * supported    RPC  version    numbers.  In   case   of refused
529 * authentication, failure status is returned.
530 */
531 .ft CW
532 union rejected_reply switch (reject_stat stat) {
533         case RPC_MISMATCH:
534                 struct {
535                         unsigned int low;
536                         unsigned int high;
537                 } mismatch_info;
538         case AUTH_ERROR: 
539                 auth_stat stat;
540 };
541 .DE
542 .NH 1
543 \&Authentication Protocols
544 .LP
545 As previously stated, authentication parameters are opaque, but
546 open-ended to the rest of the RPC protocol.  This section defines
547 some "flavors" of authentication implemented at (and supported by)
548 Sun.  Other sites are free to invent new authentication types, with
549 the same rules of flavor number assignment as there is for program
550 number assignment.
551 .NH 2
552 \&Null Authentication
553 .LP
554 Often calls must be made where the caller does not know who he is or
555 the server does not care who the caller is.  In this case, the flavor
556 value (the discriminant of the \fIopaque_auth\fP's union) of the RPC
557 message's credentials, verifier, and response verifier is
558 .I AUTH_NULL .
559 The  bytes of the opaque_auth's body  are undefined.
560 It is recommended that the opaque length be zero.
561 .NH 2
562 \&UNIX Authentication
563 .LP
564 The caller of a remote procedure may wish to identify himself as he
565 is identified on a UNIX system.  The  value of the credential's
566 discriminant of an RPC call  message is  
567 .I AUTH_UNIX .
568 The bytes of
569 the credential's opaque body encode the following structure:
570 .DS
571 .ft CW
572 struct auth_unix {
573         unsigned int stamp;
574         string machinename<255>;
575         unsigned int uid;
576         unsigned int gid;
577         unsigned int gids<10>;
578 };
579 .DE
580 The 
581 .I stamp 
582 is an  arbitrary    ID which the  caller machine   may
583 generate.  The 
584 .I machinename 
585 is the  name of the  caller's machine (like  "krypton").  The 
586 .I uid 
587 is  the caller's effective user  ID.  The  
588 .I gid 
589 is  the caller's effective  group  ID.  The 
590 .I gids 
591 is  a
592 counted array of groups which contain the caller as  a member.  The
593 verifier accompanying the  credentials  should  be  of  
594 .I AUTH_NULL
595 (defined above).
596 .LP
597 The value of the discriminant of  the response verifier received in
598 the  reply  message  from  the    server  may   be   
599 .I AUTH_NULL 
600 or
601 .I AUTH_SHORT .
602 In  the  case  of 
603 .I AUTH_SHORT ,
604 the bytes of the response verifier's string encode an opaque
605 structure.  This new opaque structure may now be passed to the server
606 instead of the original
607 .I AUTH_UNIX
608 flavor credentials.  The server keeps a cache which maps shorthand
609 opaque structures (passed back by way of an
610 .I AUTH_SHORT
611 style response verifier) to the original credentials of the caller.
612 The caller can save network bandwidth and server cpu cycles by using
613 the new credentials.
614 .LP
615 The server may flush the shorthand opaque structure at any time.  If
616 this happens, the remote procedure call message will be rejected due
617 to an authentication error.  The reason for the failure will be
618 .I AUTH_REJECTEDCRED .
619 At this point, the caller may wish to try the original
620 .I AUTH_UNIX
621 style of credentials.
622 .KS
623 .NH 2
624 \&DES Authentication
625 .LP
626 UNIX authentication suffers from two major problems:
627 .IP  1.
628 The naming is too UNIX-system oriented.
629 .IP  2.
630 There is no verifier, so credentials can easily be faked.
631 .LP
632 DES authentication attempts to fix these two problems.
633 .KE
634 .NH 3
635 \&Naming
636 .LP
637 The first problem is handled by addressing the caller by a simple
638 string of characters instead of by an operating system specific
639 integer.  This string of characters is known as the "netname" or
640 network name of the caller.  The server is not allowed to interpret
641 the contents of the caller's name in any other way except to
642 identify the caller.  Thus, netnames should be unique for every
643 caller in the internet.
644 .LP
645 It is up to each operating system's implementation of DES
646 authentication to generate netnames for its users that insure this
647 uniqueness when they call upon remote servers.  Operating systems
648 already know how to distinguish users local to their systems.  It is
649 usually a simple matter to extend this mechanism to the network.
650 For example, a UNIX user at Sun with a user ID of 515 might be
651 assigned the following netname: "unix.515@sun.com".  This netname
652 contains three items that serve to insure it is unique.  Going
653 backwards, there is only one naming domain called "sun.com" in the
654 internet.  Within this domain, there is only one UNIX user with
655 user ID 515.  However, there may be another user on another
656 operating system, for example VMS, within the same naming domain
657 that, by coincidence, happens to have the same user ID.  To insure
658 that these two users can be distinguished we add the operating
659 system name.  So one user is "unix.515@sun.com" and the other is
660 "vms.515@sun.com".
661 .LP
662 The first field is actually a naming method rather than an
663 operating system name.  It just happens that today there is almost
664 a one-to-one correspondence between naming methods and operating
665 systems.  If the world could agree on a naming standard, the first
666 field could be the name of that standard, instead of an operating
667 system name.
668 .LP
669 .NH 3
670 \&DES Authentication Verifiers
671 .LP
672 Unlike UNIX authentication, DES authentication does have a verifier
673 so the server can validate the client's credential (and
674 vice-versa).  The contents of this verifier is primarily an
675 encrypted timestamp.  The server can decrypt this timestamp, and if
676 it is close to what the real time is, then the client must have
677 encrypted it correctly.  The only way the client could encrypt it
678 correctly is to know the "conversation key" of the RPC session.  And
679 if the client knows the conversation key, then it must be the real
680 client.
681 .LP
682 The conversation key is a DES [5] key which the client generates
683 and notifies the server of in its first RPC call.  The conversation
684 key is encrypted using a public key scheme in this first
685 transaction.  The particular public key scheme used in DES
686 authentication is Diffie-Hellman [3] with 192-bit keys.  The
687 details of this encryption method are described later.
688 .LP
689 The client and the server need the same notion of the current time
690 in order for all of this to work.  If network time synchronization
691 cannot be guaranteed, then client can synchronize with the server
692 before beginning the conversation, perhaps by consulting the
693 Internet Time Server (TIME[4]).
694 .LP
695 The way a server determines if a client timestamp is valid is
696 somewhat complicated.  For any other transaction but the first, the
697 server just checks for two things:
698 .IP  1.
699 the timestamp is greater than the one previously seen from the
700 same client.
701 .IP  2.
702 the timestamp has not expired.
703 .LP
704 A timestamp is expired if the server's time is later than the sum
705 of the client's timestamp plus what is known as the client's
706 "window".  The "window" is a number the client passes (encrypted)
707 to the server in its first transaction.  You can think of it as a
708 lifetime for the credential.
709 .LP
710 This explains everything but the first transaction.  In the first
711 transaction, the server checks only that the timestamp has not
712 expired.  If this was all that was done though, then it would be
713 quite easy for the client to send random data in place of the
714 timestamp with a fairly good chance of succeeding.  As an added
715 check, the client sends an encrypted item in the first transaction
716 known as the "window verifier" which must be equal to the window
717 minus 1, or the server will reject the credential.
718 .LP
719 The client too must check the verifier returned from the server to
720 be sure it is legitimate.  The server sends back to the client the
721 encrypted timestamp it received from the client, minus one second.
722 If the client gets anything different than this, it will reject it.
723 .LP
724 .NH 3
725 \&Nicknames and Clock Synchronization
726 .LP
727 After the first transaction, the server's DES authentication
728 subsystem returns in its verifier to the client an integer
729 "nickname" which the client may use in its further transactions
730 instead of passing its netname, encrypted DES key and window every
731 time.  The nickname is most likely an index into a table on the
732 server which stores for each client its netname, decrypted DES key
733 and window.
734 .LP
735 Though they originally were synchronized, the client's and server's
736 clocks can get out of sync again.  When this happens the client RPC
737 subsystem most likely will get back
738 .I RPC_AUTHERROR 
739 at which point it should resynchronize.
740 .LP
741 A client may still get the
742 .I RPC_AUTHERROR 
743 error even though it is
744 synchronized with the server.  The reason is that the server's
745 nickname table is a limited size, and it may flush entries whenever
746 it wants.  A client should resend its original credential in this
747 case and the server will give it a new nickname.  If a server
748 crashes, the entire nickname table gets flushed, and all clients
749 will have to resend their original credentials.
750 .KS
751 .NH 3
752 \&DES Authentication Protocol (in XDR language)
753 .ie t .DS
754 .el .DS L
755 .ft I
756 /*
757 * There are two kinds of credentials: one in which the client uses
758 * its full network name, and one in which it uses its "nickname"
759 * (just an unsigned integer) given to it by the server.  The
760 * client must use its fullname in its first transaction with the
761 * server, in which the server will return to the client its
762 * nickname.  The client may use its nickname in all further
763 * transactions with the server.  There is no requirement to use the
764 * nickname, but it is wise to use it for performance reasons.
765 */
766 .ft CW
767 enum authdes_namekind {
768         ADN_FULLNAME = 0,
769         ADN_NICKNAME = 1
770 };
771
772 .ft I
773 /*
774 * A 64-bit block of encrypted DES data
775 */
776 .ft CW
777 typedef opaque des_block[8];
778
779 .ft I
780 /*
781 * Maximum length of a network user's name
782 */
783 .ft CW
784 const MAXNETNAMELEN = 255;
785
786 .ft I
787 /*
788 * A fullname contains the network name of the client, an encrypted
789 * conversation key and the window.  The window is actually a
790 * lifetime for the credential.  If the time indicated in the
791 * verifier timestamp plus the window has past, then the server
792 * should expire the request and not grant it.  To insure that
793 * requests are not replayed, the server should insist that
794 * timestamps are greater than the previous one seen, unless it is
795 * the first transaction.  In the first transaction, the server
796 * checks instead that the window verifier is one less than the
797 * window.
798 */
799 .ft CW
800 struct authdes_fullname {
801 string name<MAXNETNAMELEN>;  /* \fIname of client \f(CW*/
802 des_block key;               /* \fIPK encrypted conversation key \f(CW*/
803 unsigned int window;         /* \fIencrypted window \f(CW*/
804 };
805
806 .ft I
807 /*
808 * A credential is either a fullname or a nickname
809 */
810 .ft CW
811 union authdes_cred switch (authdes_namekind adc_namekind) {
812         case ADN_FULLNAME:
813                 authdes_fullname adc_fullname;
814         case ADN_NICKNAME:
815                 unsigned int adc_nickname;
816 };
817
818 .ft I
819 /*
820 * A timestamp encodes the time since midnight, January 1, 1970.
821 */
822 .ft CW
823 struct timestamp {
824         unsigned int seconds;    /* \fIseconds \fP*/
825         unsigned int useconds;   /* \fIand microseconds \fP*/
826 };
827
828 .ft I
829 /*
830 * Verifier: client variety
831 * The window verifier is only used in the first transaction.  In
832 * conjunction with a fullname credential, these items are packed
833 * into the following structure before being encrypted:
834 *
835 * \f(CWstruct {\fP
836 *     \f(CWadv_timestamp;            \fP-- one DES block
837 *     \f(CWadc_fullname.window;      \fP-- one half DES block
838 *     \f(CWadv_winverf;              \fP-- one half DES block
839 * \f(CW}\fP
840 * This structure is encrypted using CBC mode encryption with an
841 * input vector of zero.  All other encryptions of timestamps use
842 * ECB mode encryption.
843 */
844 .ft CW
845 struct authdes_verf_clnt {
846         timestamp adv_timestamp;    /* \fIencrypted timestamp       \fP*/
847         unsigned int adv_winverf;   /* \fIencrypted window verifier \fP*/
848 };
849
850 .ft I
851 /*
852 * Verifier: server variety
853 * The server returns (encrypted) the same timestamp the client
854 * gave it minus one second.  It also tells the client its nickname
855 * to be used in future transactions (unencrypted).
856 */
857 .ft CW
858 struct authdes_verf_svr {
859 timestamp adv_timeverf;     /* \fIencrypted verifier      \fP*/
860 unsigned int adv_nickname;  /* \fInew nickname for client \fP*/
861 };
862 .DE
863 .KE
864 .NH 3
865 \&Diffie-Hellman Encryption
866 .LP
867 In this scheme, there are two constants,
868 .I BASE 
869 and
870 .I MODULUS .
871 The
872 particular values Sun has chosen for these for the DES
873 authentication protocol are:
874 .ie t .DS
875 .el .DS L
876 .ft CW
877 const BASE = 3;
878 const MODULUS = 
879         "d4a0ba0250b6fd2ec626e7efd637df76c716e22d0944b88b"; /* \fIhex \fP*/
880 .DE
881 .ft R
882 The way this scheme works is best explained by an example.  Suppose
883 there are two people "A" and "B" who want to send encrypted
884 messages to each other.  So, A and B both generate "secret" keys at
885 random which they do not reveal to anyone.  Let these keys be
886 represented as SK(A) and SK(B).  They also publish in a public
887 directory their "public" keys.  These keys are computed as follows:
888 .ie t .DS
889 .el .DS L
890 .ft CW
891 PK(A) = ( BASE ** SK(A) ) mod MODULUS
892 PK(B) = ( BASE ** SK(B) ) mod MODULUS
893 .DE
894 .ft R
895 The "**" notation is used here to represent exponentiation.  Now,
896 both A and B can arrive at the "common" key between them,
897 represented here as CK(A, B), without revealing their secret keys.
898 .LP
899 A computes:
900 .ie t .DS
901 .el .DS L
902 .ft CW
903 CK(A, B) = ( PK(B) ** SK(A)) mod MODULUS
904 .DE
905 .ft R
906 while B computes:
907 .ie t .DS
908 .el .DS L
909 .ft CW
910 CK(A, B) = ( PK(A) ** SK(B)) mod MODULUS
911 .DE
912 .ft R
913 These two can be shown to be equivalent:
914 .ie t .DS
915 .el .DS L
916 .ft CW
917 (PK(B) ** SK(A)) mod MODULUS = (PK(A) ** SK(B)) mod MODULUS
918 .DE
919 .ft R
920 We drop the "mod MODULUS" parts and assume modulo arithmetic to
921 simplify things:
922 .ie t .DS
923 .el .DS L
924 .ft CW
925 PK(B) ** SK(A) = PK(A) ** SK(B)
926 .DE
927 .ft R
928 Then, replace PK(B) by what B computed earlier and likewise for
929 PK(A).
930 .ie t .DS
931 .el .DS L
932 .ft CW
933 ((BASE ** SK(B)) ** SK(A) = (BASE ** SK(A)) ** SK(B)
934 .DE
935 .ft R
936 which leads to:
937 .ie t .DS
938 .el .DS L
939 .ft CW
940 BASE ** (SK(A) * SK(B)) = BASE ** (SK(A) * SK(B))
941 .DE
942 .ft R
943 This common key CK(A, B) is not used to encrypt the timestamps used
944 in the protocol.  Rather, it is used only to encrypt a conversation
945 key which is then used to encrypt the timestamps.  The reason for
946 doing this is to use the common key as little as possible, for fear
947 that it could be broken.  Breaking the conversation key is a far
948 less serious offense, since conversations are relatively
949 short-lived.
950 .LP
951 The conversation key is encrypted using 56-bit DES keys, yet the
952 common key is 192 bits.  To reduce the number of bits, 56 bits are
953 selected from the common key as follows.  The middle-most 8-bytes
954 are selected from the common key, and then parity is added to the
955 lower order bit of each byte, producing a 56-bit key with 8 bits of
956 parity.
957 .KS
958 .NH 1
959 \&Record Marking Standard
960 .LP
961 When RPC messages are passed on top of a byte stream protocol (like
962 TCP/IP), it is necessary, or at least desirable, to delimit one
963 message from another in order to detect and possibly recover from
964 user protocol errors.  This is called record marking (RM).  Sun uses
965 this RM/TCP/IP transport for passing RPC messages on TCP streams.
966 One RPC message fits into one RM record.
967 .LP
968 A record is composed of one or more record fragments.  A record
969 fragment is a four-byte header followed by 0 to (2**31) - 1 bytes of
970 fragment data.  The bytes encode an unsigned binary number; as with
971 XDR integers, the byte order is from highest to lowest.  The number
972 encodes two values\(ema boolean which indicates whether the fragment
973 is the last fragment of the record (bit value 1 implies the fragment
974 is the last fragment) and a 31-bit unsigned binary value which is the
975 length in bytes of the fragment's data.  The boolean value is the
976 highest-order bit of the header; the length is the 31 low-order bits.
977 (Note that this record specification is NOT in XDR standard form!)
978 .KE
979 .KS
980 .NH 1
981 \&The RPC Language
982 .LP
983 Just as there was a need to describe the XDR data-types in a formal
984 language, there is also need to describe the procedures that operate
985 on these XDR data-types in a formal language as well.  We use the RPC
986 Language for this purpose.  It is an extension to the XDR language.
987 The following example is used to describe the essence of the
988 language.
989 .NH 2
990 \&An Example Service Described in the RPC Language
991 .LP
992 Here is an example of the specification of a simple ping program.
993 .ie t .DS
994 .el .DS L
995 .vs 11
996 .ft I
997 /*
998 * Simple ping program
999 */
1000 .ft CW
1001 program PING_PROG {
1002         /* \fILatest and greatest version\fP */
1003         version PING_VERS_PINGBACK {
1004         void 
1005         PINGPROC_NULL(void) = 0;
1006
1007 .ft I
1008         /*
1009         * Ping the caller, return the round-trip time
1010         * (in microseconds). Returns -1 if the operation
1011         * timed out.
1012         */
1013 .ft CW
1014         int
1015         PINGPROC_PINGBACK(void) = 1;        
1016 } = 2;     
1017
1018 .ft I
1019 /*
1020 * Original version
1021 */
1022 .ft CW
1023 version PING_VERS_ORIG {
1024         void 
1025         PINGPROC_NULL(void) = 0;
1026         } = 1;
1027 } = 1;
1028
1029 const PING_VERS = 2;      /* \fIlatest version \fP*/
1030 .vs
1031 .DE
1032 .KE
1033 .LP
1034 The first version described is
1035 .I PING_VERS_PINGBACK
1036 with  two procedures,   
1037 .I PINGPROC_NULL 
1038 and 
1039 .I PINGPROC_PINGBACK .
1040 .I PINGPROC_NULL 
1041 takes no arguments and returns no results, but it is useful for
1042 computing round-trip times from the client to the server and back
1043 again.  By convention, procedure 0 of any RPC protocol should have
1044 the same semantics, and never require any kind of authentication.
1045 The second procedure is used for the client to have the server do a
1046 reverse ping operation back to the client, and it returns the amount
1047 of time (in microseconds) that the operation used.  The next version,
1048 .I PING_VERS_ORIG ,
1049 is the original version of the protocol
1050 and it does not contain
1051 .I PINGPROC_PINGBACK
1052 procedure. It  is useful
1053 for compatibility  with old client  programs,  and as  this program
1054 matures it may be dropped from the protocol entirely.
1055 .KS
1056 .NH 2
1057 \&The RPC Language Specification
1058 .LP
1059 The  RPC language is identical to  the XDR language, except for the
1060 added definition of a
1061 .I program-def 
1062 described below.
1063 .DS
1064 .ft CW
1065 program-def:
1066         "program" identifier "{"
1067                 version-def 
1068                 version-def *
1069         "}" "=" constant ";"
1070
1071 version-def:
1072         "version" identifier "{"
1073                 procedure-def
1074                 procedure-def *
1075         "}" "=" constant ";"
1076
1077 procedure-def:
1078         type-specifier identifier "(" type-specifier ")"
1079         "=" constant ";"
1080 .DE
1081 .KE
1082 .NH 2
1083 \&Syntax Notes
1084 .IP  1.
1085 The following keywords  are  added  and   cannot  be used   as
1086 identifiers: "program" and "version";
1087 .IP  2.
1088 A version name cannot occur more than once within the  scope of
1089 a program definition. Nor can a version number occur more than once
1090 within the scope of a program definition.
1091 .IP  3.
1092 A procedure name cannot occur  more than once within  the scope
1093 of a version definition. Nor can a procedure number occur more than
1094 once within the scope of version definition.
1095 .IP  4.
1096 Program identifiers are in the same name space as  constant and
1097 type identifiers.
1098 .IP  5.
1099 Only unsigned constants can  be assigned to programs, versions
1100 and procedures.
1101 .NH 1
1102 \&Port Mapper Program Protocol
1103 .LP
1104 The port mapper program maps RPC program and version numbers to
1105 transport-specific port numbers.  This program makes dynamic binding
1106 of remote programs possible.
1107 .LP
1108 This is desirable because the range of reserved port numbers is very
1109 small and the number of potential remote programs is very large.  By
1110 running only the port mapper on a reserved port, the port numbers of
1111 other remote programs can be ascertained by querying the port mapper.
1112 .LP
1113 The port mapper also aids in broadcast RPC.  A given RPC program will
1114 usually have different port number bindings on different machines, so
1115 there is no way to directly broadcast to all of these programs.  The
1116 port mapper, however, does have a fixed port number.  So, to
1117 broadcast to a given program, the client actually sends its message
1118 to the port mapper located at the broadcast address.  Each port
1119 mapper that picks up the broadcast then calls the local service
1120 specified by the client.  When the port mapper gets the reply from
1121 the local service, it sends the reply on back to the client.
1122 .KS
1123 .NH 2
1124 \&Port Mapper Protocol Specification (in RPC Language)
1125 .ie t .DS
1126 .el .DS L
1127 .ft CW
1128 .vs 11
1129 const PMAP_PORT = 111;      /* \fIportmapper port number \fP*/
1130
1131 .ft I
1132 /*
1133 * A mapping of (program, version, protocol) to port number
1134 */
1135 .ft CW
1136 struct mapping {
1137         unsigned int prog;
1138         unsigned int vers;
1139         unsigned int prot;
1140         unsigned int port;
1141 };
1142
1143 .ft I
1144 /* 
1145 * Supported values for the "prot" field
1146 */
1147 .ft CW
1148 const IPPROTO_TCP = 6;      /* \fIprotocol number for TCP/IP \fP*/
1149 const IPPROTO_UDP = 17;     /* \fIprotocol number for UDP/IP \fP*/
1150
1151 .ft I
1152 /*
1153 * A list of mappings
1154 */
1155 .ft CW
1156 struct *pmaplist {
1157         mapping map;
1158         pmaplist next;
1159 };
1160 .vs
1161 .DE
1162 .ie t .DS
1163 .el .DS L
1164 .vs 11
1165 .ft I
1166 /*
1167 * Arguments to callit
1168 */
1169 .ft CW
1170 struct call_args {
1171         unsigned int prog;
1172         unsigned int vers;
1173         unsigned int proc;
1174         opaque args<>;
1175 };  
1176
1177 .ft I
1178 /*
1179 * Results of callit
1180 */
1181 .ft CW
1182 struct call_result {
1183         unsigned int port;
1184         opaque res<>;
1185 };
1186 .vs
1187 .DE
1188 .KE
1189 .ie t .DS
1190 .el .DS L
1191 .vs 11
1192 .ft I
1193 /*
1194 * Port mapper procedures
1195 */
1196 .ft CW
1197 program PMAP_PROG {
1198         version PMAP_VERS {
1199                 void 
1200                 PMAPPROC_NULL(void)         = 0;
1201
1202                 bool
1203                 PMAPPROC_SET(mapping)       = 1;
1204
1205                 bool
1206                 PMAPPROC_UNSET(mapping)     = 2;
1207
1208                 unsigned int
1209                 PMAPPROC_GETPORT(mapping)   = 3;
1210
1211                 pmaplist
1212                 PMAPPROC_DUMP(void)         = 4;
1213
1214                 call_result
1215                 PMAPPROC_CALLIT(call_args)  = 5;
1216         } = 2;
1217 } = 100000;
1218 .vs
1219 .DE
1220 .NH 2
1221 \&Port Mapper Operation
1222 .LP
1223 The portmapper program currently supports two protocols (UDP/IP and
1224 TCP/IP).  The portmapper is contacted by talking to it on assigned
1225 port number 111 (SUNRPC [8]) on either of these protocols.  The
1226 following is a description of each of the portmapper procedures:
1227 .IP \fBPMAPPROC_NULL:\fP
1228 This procedure does no work.  By convention, procedure zero of any
1229 protocol takes no parameters and returns no results.
1230 .IP \fBPMAPPROC_SET:\fP
1231 When a program first becomes available on a machine, it registers
1232 itself with the port mapper program on the same machine.  The program
1233 passes its program number "prog", version number "vers", transport
1234 protocol number "prot", and the port "port" on which it awaits
1235 service request.  The procedure returns a boolean response whose
1236 value is
1237 .I TRUE
1238 if the procedure successfully established the mapping and 
1239 .I FALSE 
1240 otherwise.  The procedure refuses to establish
1241 a mapping if one already exists for the tuple "(prog, vers, prot)".
1242 .IP \fBPMAPPROC_UNSET:\fP
1243 When a program becomes unavailable, it should unregister itself with
1244 the port mapper program on the same machine.  The parameters and
1245 results have meanings identical to those of
1246 .I PMAPPROC_SET .
1247 The protocol and port number fields of the argument are ignored.
1248 .IP \fBPMAPPROC_GETPORT:\fP
1249 Given a program number "prog", version number "vers", and transport
1250 protocol number "prot", this procedure returns the port number on
1251 which the program is awaiting call requests.  A port value of zeros
1252 means the program has not been registered.  The "port" field of the
1253 argument is ignored.
1254 .IP \fBPMAPPROC_DUMP:\fP
1255 This procedure enumerates all entries in the port mapper's database.
1256 The procedure takes no parameters and returns a list of program,
1257 version, protocol, and port values.
1258 .IP \fBPMAPPROC_CALLIT:\fP
1259 This procedure allows a caller to call another remote procedure on
1260 the same machine without knowing the remote procedure's port number.
1261 It is intended for supporting broadcasts to arbitrary remote programs
1262 via the well-known port mapper's port.  The parameters "prog",
1263 "vers", "proc", and the bytes of "args" are the program number,
1264 version number, procedure number, and parameters of the remote
1265 procedure.
1266 .LP
1267 .B Note:
1268 .RS
1269 .IP  1.
1270 This procedure only sends a response if the procedure was
1271 successfully executed and is silent (no response) otherwise.
1272 .IP  2.
1273 The port mapper communicates with the remote program using UDP/IP
1274 only.
1275 .RE
1276 .LP
1277 The procedure returns the remote program's port number, and the bytes
1278 of results are the results of the remote procedure.
1279 .bp
1280 .NH 1
1281 \&References
1282 .LP
1283 [1]  Birrell, Andrew D. & Nelson, Bruce Jay; "Implementing Remote
1284 Procedure Calls"; XEROX CSL-83-7, October 1983.
1285 .LP
1286 [2]  Cheriton, D.; "VMTP:  Versatile Message Transaction Protocol",
1287 Preliminary Version 0.3; Stanford University, January 1987.
1288 .LP
1289 [3]  Diffie & Hellman; "New Directions in Cryptography"; IEEE
1290 Transactions on Information Theory IT-22, November 1976.
1291 .LP
1292 [4]  Harrenstien, K.; "Time Server", RFC 738; Information Sciences
1293 Institute, October 1977.
1294 .LP
1295 [5]  National Bureau of Standards; "Data Encryption Standard"; Federal
1296 Information Processing Standards Publication 46, January 1977.
1297 .LP
1298 [6]  Postel, J.; "Transmission Control Protocol - DARPA Internet
1299 Program Protocol Specification", RFC 793; Information Sciences
1300 Institute, September 1981.
1301 .LP
1302 [7]  Postel, J.; "User Datagram Protocol", RFC 768; Information Sciences
1303 Institute, August 1980.
1304 .LP
1305 [8]  Reynolds, J.  & Postel, J.; "Assigned Numbers", RFC 923; Information
1306 Sciences Institute, October 1984.