Added instruction on how to build this Sphinx documentation
[krb5.git] / doc / implementor.texinfo
1 \input texinfo @c -*-texinfo-*-
2 @c
3 @c Note: the above texinfo file must include the "doubleleftarrow"
4 @c definitions added by jcb.
5 @c %**start of header
6 @c guide
7 @setfilename krb5-implement.info
8 @settitle Kerberos V5 Installation Guide
9 @setchapternewpage odd                  @c chapter begins on next odd page
10 @c @setchapternewpage on                   @c chapter begins on next page
11 @c @smallbook                              @c Format for 7" X 9.25" paper
12 @c %**end of header
13
14 @paragraphindent 0
15 @iftex
16 @parskip 6pt plus 6pt
17 @end iftex
18
19 @include definitions.texinfo
20 @c @set EDITION b7-1
21 @set EDITION [working copy]
22
23 @finalout                               @c don't print black warning boxes
24
25 @titlepage
26 @title @value{PRODUCT} Implementor's Guide
27 @subtitle Release:  @value{RELEASE}
28 @subtitle Document Edition:  @value{EDITION}
29 @subtitle Last updated:  @value{UPDATED}
30 @author @value{COMPANY}
31
32 @page
33 @vskip 0pt plus 1filll
34
35 @iftex
36 @include copyright.texinfo
37 @end iftex
38 @end titlepage
39
40 @node Top, Introduction, (dir), (dir)
41 @comment  node-name,  next,  previous,  up
42
43 @ifinfo
44 This file contains internal implementor's information for the
45 @value{RELEASE} release of @value{PRODUCT}.  
46
47 @include copyright.texinfo
48
49 @end ifinfo
50
51 @c The master menu is updated using emacs19's M-x texinfo-all-menus-update
52 @c function.  Don't forget to run M-x texinfo-every-node-update after
53 @c you add a new section or subsection, or after you've rearranged the
54 @c order of sections or subsections.  Also, don't forget to add an @node
55 @c comand before each @section or @subsection!  All you need to enter
56 @c is:
57 @c
58 @c @node New Section Name
59 @c @section New Section Name
60 @c
61 @c M-x texinfo-every-node-update will take care of calculating the
62 @c node's forward and back pointers.
63 @c
64 @c ---------------------------------------------------------------------
65
66 @menu
67 * Introduction::                
68 * Compiler and OS Requirements::  
69 * Networking::                  
70 * Thread Safety::               
71 * Shared Libraries::            
72 * Porting Issues::              
73 @end menu
74
75 @node Introduction, Compiler and OS Requirements, Top, Top
76 @chapter Introduction
77
78 This file contains internal implementor's information for
79 @value{PRODUCT}.  It is currently contains information that was removed
80 from install.texi; eventually it will have more detailed information on
81 the internals of the @value{PRODUCT}.
82
83 @node Compiler and OS Requirements, Networking, Introduction, Top
84 @chapter Compiler and OS Requirements
85
86 The basic Kerberos libraries are entirely written in C.
87 However, we do assume full ANSI C (1989) support, typical 32- or
88 64-bit twos-complement architectures (@code{char} is 8 bits,
89 @code{short} is 16 bits, @code{int} is 32 bits, byte order is 1234 or
90 4321), and a few aspects of ISO C 1999:
91
92 @itemize @bullet
93 @item
94 support for inline functions, even if the keyword isn't @code{inline}
95 @item
96 64-bit arithmetic types (needed for sequence numbers in GSSAPI)
97 @end itemize
98
99 These are handled through the internal header file
100 @file{k5-platform.h}.
101
102 We also conditionally tailor code for the GNU C compiler in a few
103 places where it helps performance or debugging, but the code should
104 still work fine with other C compilers.
105
106 Inline functions should always be specified in the code as
107 @code{static inline}, as the behavior of other forms vary across GCC
108 versions and C and C++ language standards.  We assume that static
109 copies of inline functions will not be generated if they are not
110 needed; under some compilers that behave otherwise (such as the
111 current Sun compiler as of this writing) may produce executables and
112 libraries much larger than they need to be.
113
114 On UNIX platforms, ... @i{(should outline what POSIX stuff we
115 require)}.
116
117 See also @ref{Advanced Shared Library Requirements}, for UNIX and
118 Windows systems.
119
120 Our makefiles are intended to support building from the top level with
121 a POSIX-compliant version of @code{make}, and parallel builds using
122 GNU @code{make}.  The latter sometimes comes at the cost of efficiency with
123 non-GNU versions; for example, some targets in some directories will
124 always be rebuilt with certain versions of @code{make}, even though
125 the real dependencies are not out of date, because some versions of
126 @code{make} don't understand how we're using phony intermediate
127 targets to manage building in subdirectories in parallel builds.
128 (Actually, this is more my view of how we've been doing things than
129 official policy.  ---Ken)
130
131 Some of our code uses the SUSv2/C99 functions @code{snprintf} and
132 @code{vsnprintf}.  Since the two specifications differ, we assume that
133 either specification may be followed: If truncation occurs, the return
134 value may be the untruncated output length, or it may be negative (or
135 may be zero if a zero length was supplied).  It is therefore not
136 permitted in our code to call these functions with a zero output
137 length in order to determine the desired buffer size.  A NULL output
138 pointer is not permitted.  In the header @file{k5-platform.h} we
139 provide inline definitions for some platforms where these functions do
140 not exist.
141
142 We also use the extension functions @code{asprintf} and
143 @code{vasprintf}, available on modern GNU and BSD based systems.  The
144 behaviors of these functions on errors vary between the two system
145 types -- BSD stores a NULL as the output pointer, and GNU doesn't set
146 it.  We assume either may be the case: The output pointer may be
147 unchanged or may be overwritten with NULL, and our code should thus
148 assume it may be garbage, and should be assigned to if the value
149 matters after an error is detected.  Again, @file{k5-platform.h}
150 provides workarounds for systems where these functions are not
151 defined.
152
153 Using these functions instead of plain @code{sprintf} without length
154 checking may make our code slightly less vulnerable to buffer
155 overruns.
156
157 If necessary, we may eventually use a @code{[v]asnprintf} interface
158 like that of the GNU C library, but we have not done so yet.  Do not
159 write code using that interface.
160
161 @node Networking, Thread Safety, Compiler and OS Requirements, Top
162 @chapter Networking
163
164 @menu
165 * Socket API::                  
166 * IPv6 Support::                
167 * Local Addresses::             
168 * Host Address Lookup::         
169 @end menu
170
171 @node Socket API, IPv6 Support, Networking, Networking
172 @section Socket API
173
174 Someone should describe the API subset we're allowed to use with
175 sockets, how and when to use @code{SOCKET_ERRNO}, @i{etc}.
176
177 Note that all new code doing hostname and address translation should
178 use @code{getaddrinfo} and friends.  (@xref{Host Address Lookup}.)
179
180 @node IPv6 Support, Local Addresses, Socket API, Networking
181 @section IPv6 Support
182
183 Most of the IPv6 support is keyed on the macro @code{KRB5_USE_INET6}.
184 If this macro is not defined, there should be no references to
185 @code{AF_INET6}, @code{struct sockaddr_in6}, @i{etc}.
186
187 The @code{configure} scripts will check for the existence of various
188 functions, macros and structure types to decide whether to enable the
189 IPv6 support.  You can also use the @samp{--enable-ipv6} or
190 @samp{--disable-ipv6} options to override this decision.
191
192 Regardless of the setting of @code{KRB5_USE_INET6}, some aspects of
193 the new APIs devised for IPv6 are used throughout the code, because it
194 would be too difficult maintain code for the IPv6 APIs and for the old
195 APIs at the same time.  But for backwards compatibility, we try to
196 fake them if the system libraries don't provide them, at least for
197 now.  This means we sometimes use slightly modified versions of the
198 APIs, but we try to keep the modifications as non-intrusive as
199 possible.  Macros are used to rename struct tags and function names,
200 so don't @code{#undef} any of these names.
201
202 @table @code
203
204 @item getaddrinfo
205 @itemx getnameinfo
206 @itemx freeaddrinfo
207 @itemx gai_strerror
208 @itemx struct addrinfo
209 Always include the header file @file{fake-addrinfo.h} before using
210 these.  If the native system doesn't provide them, the header file
211 will, using support functions that will call @code{gethostbyname} and
212 the like in the native libraries.  (This is similar to how the Winsock
213 2 headers work, depending on some of the predefined macros indicating
214 the target OS version, though they define the support functions
215 directly in the header, as our code used to do.)
216
217 We also provide ``wrapper'' versions on some systems where a native
218 implementation exists but the data it returns is broken in some way.
219
220 So these may not always be thread-safe, and they may not always
221 provide IPv6 support, but the API will be consistent.
222
223 @item struct sockaddr_storage
224 @itemx socklen_t
225 These are provided by @file{socket-utils.h}, if the native headers
226 don't provide them.  @code{sockaddr_storage} contains a
227 @code{sockaddr_in}, so by definition it's big enough to hold one; it
228 also has some extra padding which will probably make it big enough to
229 hold a @code{sockaddr_in6} if the resulting binary should get run on a
230 kernel with IPv6 support.
231
232 Question: Should these simply be moved into @file{port-sockets.h}?
233
234 @end table
235
236 IRIX 6.5.7 has no IPv6 support.  Of the systems most actively in the
237 MIT's Athena environment (used by MIT's Kerberos UNIX developers),
238 this is the only one without built-in IPv6 support.  In another year
239 or so we probably won't be using those systems any more, and we may
240 consider dropping support for systems without IPv6 support.
241
242 Somewhere between IRIX 6.5.14 and 6.5.16, partial IPv6 support was
243 introduced to the extent that the configuration system detects the
244 IPv6 support and attempts to use it.  Code compiles, but then upon
245 linking, one discovers that @code{in6addr_any} is not defined in any
246 system library.  The header file @file{fake-addrinfo.h} provides a
247 static copy as a workaround.  This run time IPv6 code has still not
248 been tested.
249
250 Some utility functions or macros are also provided to give a
251 convenient shorthand for some operations, and to retain compile-time
252 type checking when possible (generally using inline functions but only
253 when compiling with GCC).
254
255 @table @code
256
257 @item socklen(struct sockaddr *)
258 Returns the length of the @code{sockaddr} structure, by looking at the
259 @code{sa_len} field if it exists, or by returning the known sizes of
260 @code{AF_INET} and @code{AF_INET6} address structures.
261
262 @item sa2sin(struct sockaddr *)
263 @itemx sa2sin6(struct sockaddr *)
264 @itemx ss2sa(struct sockaddr_storage *)
265 @itemx ss2sin(struct sockaddr_storage *)
266 @itemx ss2sin6(struct sockaddr_storage *)
267 Pointer type conversions.  Use these instead of plain casts, to get
268 type checking under GCC.
269
270 @end table
271
272 @node Local Addresses, Host Address Lookup, IPv6 Support, Networking
273 @section Local Addresses
274
275 (Last update: 2005-04-21, but most of the information dates back to
276 early 2002.)
277
278 Different systems have different ways of finding the local network
279 addresses.
280
281 On Windows, @code{gethostbyname} is called on the local host name to get a
282 set of addresses.  If that fails, a UDP socket is ``connected'' to a
283 particular IPv4 address, and the local socket name is retrieved, its
284 address being treated as the one local network address.  Future
285 versions of the Windows code should be able to actually examine local
286 interfaces.
287
288 On (most?) UNIX systems, there is an @code{ioctl} called
289 @code{SIOCGIFCONF} which gets interface configuration information.
290 The behavior of this @code{ioctl} varies across UNIX systems though.
291 It takes as input a buffer to fill with data structures, but if the
292 buffer isn't big enough, the behavior isn't well defined.  Sometimes
293 you get an error, sometimes you get incomplete data.  Sometimes you
294 get a clear indication that more space was needed, sometimes not.  A
295 couple of systems have additional @code{ioctl}s that can be used to
296 determine or at least estimate the correct size for the buffer.  In
297 Solaris, Sun has introduced @code{SIOCGLIFCONF} for querying IPv6
298 addresses, and restricts @code{SIOCGIFCONF} to IPv4 only.  (** We
299 should actually check if that's true.)  They also added
300 @code{SIOCGIFNUM} and @code{SIOCGLIFNUM} for querying the number of
301 interfaces.  HP-UX 11 also has @code{SIOCGLIFCONF}, but it requires a
302 different data structure, and we haven't finished that support yet.
303
304 We (Ken Raeburn in particular) ran some tests on various systems to
305 see what would happen with buffers of various sizes from much smaller
306 to much larger than needed for the actual data.  The buffers were
307 filled with specific byte values, and then checked to see how much of
308 the buffer was actually written to.  The ``largest gap'' values listed
309 below are the largest number of bytes we've seen left unused at the
310 end of the supplied buffer when there were more entries to return.
311 These values may of coures be dependent on the configurations of the
312 particular systems we wre testing with.  (See
313 @file{lib/krb5/os/t_gifconf.c} for the test program.)
314
315 NetBSD 1.5-alpha: The returned @code{ifc_len} is the desired amount of
316 space, always.  The returned list may be truncated if there isn't
317 enough room; no overrun.  Largest gap: 43.  However, NetBSD has
318 @code{getifaddrs}, which hides all the ugliness within the C library.
319
320 BSD/OS 4.0.1 (courtesy djm): The returned @code{ifc_len} is equal to
321 or less than the supplied @code{ifc_len}.  Sometimes the entire buffer
322 is used; sometimes N-1 bytes; occasionally, the buffer must have quite
323 a bit of extra room before the next structure will be added.  Largest
324 gap: 39.
325
326 Solaris 7,8,9: Return @code{EINVAL} if the buffer space is too small
327 for all the data to be returned, including when @code{ifc_len} is 0.
328 Solaris is the only system I've found so far that actually returns an
329 error.  No gap.  However, @code{SIOCGIFNUM} may be used to query the
330 number of interfaces.
331
332 Linux 2.2.12 (Red Hat 6.1 distribution, x86), 2.4.9 (RH 7.1, x86): The
333 buffer is filled in with as many entries as will fit, and the size
334 used is returned in @code{ifc_len}.  The list is truncated if needed,
335 with no indication.  Largest gap: 31.  @emph{However}, this interface
336 does not return any IPv6 addresses.  They must be read from a file
337 under @file{/proc}.  (This appears to be what the @samp{ifconfig}
338 program does.)
339
340 IRIX 6.5.7: The buffer is filled in with as many entries as will fit
341 in N-1 bytes, and the size used is returned in @code{ifc_len}.
342 Providing exactly the desired number of bytes is inadequate; the
343 buffer must be @emph{bigger} than needed.  (E.g., 32->0, 33->32.)  The
344 returned @code{ifc_len} is always less than the supplied one.  Largest
345 gap: 32.
346
347 AIX 4.3.3: Sometimes the returned @code{ifc_len} is bigger than the
348 supplied one, but it may not be big enough for @emph{all} the
349 interfaces.  Sometimes it's smaller than the supplied value, even if
350 the returned list is truncated.  The list is filled in with as many
351 entries as will fit; no overrun.  Largest gap: 143.
352
353 Older AIX: We're told by W. David Shambroom in RT ticket 919 that
354 older versions of AIX have a bug in the @code{SIOCGIFCONF}
355 @code{ioctl} which can cause them to overrun the supplied buffer.
356 However, we don't yet have details as to which version, whether the
357 overrun amount was bounded (e.g., one @code{ifreq}'s worth) or not,
358 whether it's a real buffer overrun or someone assuming it was because
359 @code{ifc_len} was increased, @i{etc}.  Once we've got details, we can
360 try to work around the problem.
361
362 Digital UNIX 4.0F: If input @code{ifc_len} is zero, return an
363 @code{ifc_len} that's big enough to include all entries.  (Actually,
364 on our system, it appears to be larger than that by 32.)  If input
365 @code{ifc_len} is nonzero, fill in as many entries as will fit, and
366 set @code{ifc_len} accordingly.  (Tested only with buffer previously
367 filled with zeros.)
368
369 Tru64 UNIX 5.1A: Like Digital UNIX 4.0F, except the ``extra'' space
370 indicated when the input @code{ifc_len} is zero is larger.  (We got
371 400 out when 320 appeared to be needed.)
372
373 So... if the returned @code{ifc_len} is bigger than the supplied one,
374 we'll need at least that much space -- but possibly more -- to hold
375 all the results.  If the returned value is a little smaller or the
376 same, we may still need more space.
377
378 The heuristic we're using on most systems now is to keep growing the
379 buffer until the unused space is larger than an @code{ifreq} structure
380 by some safe margin.
381
382 @node Host Address Lookup,  , Local Addresses, Networking
383 @section Host Address Lookup
384
385 The traditional @code{gethostbyname} function is not thread-safe, and
386 does not support looking up IPv6 addresses, both of which are becoming
387 more important.  New standards have been in development that should
388 address both of these problems.  The most promising is
389 @code{getaddrinfo} and friends, which is part of the Austin Group and
390 UNIX 98(?) specifications.  Code in the MIT tree has mostly been
391 converted to use this interface.
392
393 @quotation
394 (Question: What about @code{inet_ntop} and @code{inet_pton}?  We're
395 not using them at the moment, but some bits of code would be
396 simplified if we were to do so, when plain addresses and not socket
397 addresses are already presented to us.)
398 @end quotation
399
400 The @code{getaddrinfo} function takes a host name and service name and
401 returns a linked list of structures indicating the address family,
402 length, and actual data in ``sockaddr'' form.  (That is, it includes a
403 pointer to a @code{sockaddr_in} or @code{sockaddr_in6} structure.)
404 Depending on options set via the @code{hints} input argument, the results
405 can be limited to a single address family (@i{e.g.}, for IPv4
406 applications), and the canonical name of the indicated host can be
407 returned.  Either the host or service can be a null pointer, in which
408 case only the other is looked up; they can also be expressed in
409 numeric form.  This interface is extensible to additional address
410 families in the future.  The returned linked list can be freed with
411 the @code{freeaddrinfo} function.
412
413 The @code{getnameinfo} function does the reverse -- given an address
414 in ``sockaddr'' form, it converts the address and port values into
415 printable forms.
416
417 Errors returned by either of these functions -- as return values, not
418 global variables -- can be translated into printable form with the
419 @code{gai_strerror} function.
420
421 Some vendors are starting to implement @code{getaddrinfo} and friends,
422 however, some of the implementations are deficient in one way or
423 another.
424
425 @table @asis
426
427 @item AIX
428 As of AIX 4.3.3, @code{getaddrinfo} returns sockaddr structures
429 without the family and length fields filled in.
430
431 @item GNU libc
432 The GNU C library, used on GNU/Linux systems, has had a few problems
433 in this area.  One version would drop some IPv4 addresses for some
434 hosts that had multiple IPv4 and IPv6 addresses.
435
436 In GNU libc 2.2.4, when the DNS is used, the name referred to by PTR
437 records for each of the addresses is looked up and stored in the
438 @code{ai_canonname} field, or the printed numeric form of the address
439 is, both of which are wrong.
440
441 @item IRIX
442 No known bugs here, but as of IRIX 6.5.7, the version we're using at
443 MIT, these functions had not been implemented.
444
445 @item Mac OS X
446 Two problems have been found with @code{getaddrinfo} on Mac OS X, at
447 least under version 10.3.  First, while @code{gethostbyname} data is
448 cached to make multiple lookups of the same name (@i{e.g.}, by
449 different parts of the code that need to know about the same server
450 host), @code{getaddrinfo} results are not cached, so multiple queries
451 mean multiple DNS requests, which means more delays if the DNS servers
452 are not close by and fast to respond.  We've implemented a cache of
453 our own to work around this, though it only applies to multiple
454 lookups in a short period of time within the same application process,
455 and it's only implemented for the Mac at the moment.
456
457 Second, the Mac libraries will generate a DNS SRV RR query; as far as
458 I [Ken] can tell this is a bug, but Apple seems to consider it a
459 feature.  (Call @code{getaddrinfo("example.com", "telnet", ...)} and
460 you get a SRV record query, but the spec on SRV records says you must
461 not use them unless the specification for the service in question says
462 to.)  Yet more network traffic for each name to look up.
463
464 @item NetBSD
465 As of NetBSD 1.5, this function is not thread-safe.  In 1.5X
466 (intermediate code snapshot between 1.5 and 1.6 releases), the
467 @code{ai_canonname} field can be empty, even if the
468 @code{AI_CANONNAME} flag was passed.  In particular, this can happen
469 if a numeric host address string is provided.  Also, numeric service
470 names appear not to work unless the stream type is given; specifying
471 the TCP protocol is not enough.
472
473 @item Tru64 UNIX
474 In Tru64 UNIX 5.0, @code{getaddrinfo} is available, but requires that
475 @code{<netdb.h>} be included before its use; that header file defines
476 @code{getaddrinfo} as a macro expanding to either @code{ogetaddrinfo}
477 or @code{ngetaddrinfo}, and apparently the symbol @code{getaddrinfo}
478 is not present in the system library, causing the @code{configure}
479 test for it to fail.  Technically speaking, I [Ken] think Compaq has
480 it wrong here, I think the symbol is supposed to be available even if
481 the application uses @code{#undef}, but I have not confirmed it in the
482 spec.
483
484 @item Windows
485 According to Windows documentation, the returned @code{ai_canonname}
486 field can be null even if the @code{AI_CANONNAME} flag is given.
487
488 @end table
489
490 For most systems where @code{getaddrinfo} returns incorrect data,
491 we've provided wrapper versions that call the system version and then
492 try to fix up the returned data.
493
494 For systems that don't provide these functions at all, we've provided
495 replacement versions that neither are thread-safe nor support IPv6,
496 but will allow us to convert the rest of our code to assume the
497 availability of @code{getaddrinfo}, rather than having to use two
498 branches everywhere, one for @code{getaddrinfo} and one for
499 @code{gethostbyname}.  These replacement functions do use
500 @code{gethostbyname} and the like; for some systems it would be
501 possible to use @code{gethostbyname2} or @code{gethostbyname_r} or
502 other such functions, to provide thread safety or IPv6 support, but
503 this has not been a priority for us, since most modern systems have
504 these functions anyways.  And if they don't, they probably don't have
505 real IPv6 support either.
506
507 Including @file{fake-addrinfo.h} will enable the wrapper or
508 replacement versions when needed.  The functions are actually defined
509 in the support library in @file{src/util/support}, added in the 1.4
510 release.
511
512 Do not assume that @code{ai_canonname} will be set when the
513 @code{AI_CANONNAME} flag is set.  Check for a null pointer before
514 using it.
515
516 @node Thread Safety, Shared Libraries, Networking, Top
517 @chapter Thread Safety
518
519 Work is still needed as this section is being written.  However, we've
520 made a lot of progress.
521
522 @menu
523 * Kerberos API Thread Safety::  
524 * Thread System Requirements::  
525 * Internal Thread API::         
526 * Thread Shim Layer Implementation::  
527 @end menu
528
529 @node Kerberos API Thread Safety, Thread System Requirements, Thread Safety, Thread Safety
530 @section Kerberos API Thread Safety
531
532 We assume that a @code{krb5_context} or a @code{krb5_auth_context}
533 will be used in only one thread at a time, and any non-opaque object
534 clearly being modified by the application code (@i{e.g.}, a
535 @code{krb5_principal} having a field replaced) is not being used in
536 another thread at the same time.
537
538 A credentials cache, key table, or replay cache object, once the C
539 object is created, may be used in multiple threads simultaneously;
540 internal locking is done by the implementations of those objects.  (We
541 assume that object destructors are invoked only when all other threads
542 are finished with the object.)  @i{(Iterators?  Probably okay now, but
543 needs review.)}  However, this doesn't mean that we've fixed any
544 problems there may be regarding simultaneous access to on-disk files
545 from multiple processes, and in fact if a process opens a disk file
546 multiple times, the same problems may come up.
547
548 Any file locking issues may become worse, actually.  UNIX file locking
549 with @code{flock} is done on a per-process basis, and closing a file
550 descriptor that was opened on a file releases any locks the process
551 may have on that file, even if they were obtained using other,
552 still-open file descriptors.  UNIX file locks are used for credentials
553 caches and keytab files; the replay cache implementation is already
554 known to be unsafe in not using file locking.
555
556 We MAY implement --- but haven't yet --- a ``fix'' whereby open files
557 are tracked by name (and per object type), and a new attempt to open
558 one gets a handle that uses the same open file descriptor, even if it
559 appears as two objects to the application.  This won't address the
560 problem of getting the same file via two names that look different,
561 but it may be ``good enough.''
562
563 GSSAPI ....
564
565 Strictly speaking, the GSSAPI specification says nothing about thread
566 safety, so for best portability, a GSSAPI application probably should
567 not assume that a GSSAPI implementation is thread-safe in any way.  On
568 the other hand, the GSSAPI specification doesn't explicitly say that
569 it's safe to use in a program that uses the Berkeley sockets API,
570 either; at some point, you have to start making some assumptions.
571
572 A GSSAPI security context, like a @code{krb5_context}, may be used
573 only in one thread at a time.  The GSSAPI specification gives precise
574 definitions of C data structures for buffers, object identifiers, OID
575 sets, and channel bindings, that do not allow for the addition of a
576 mutex.  Thus, these objects must not be modified by one thread while
577 in use by another.  (All of the GSSAPI functions that modify these
578 types of objects should be obvious; they're listed as ``modify''
579 parameters in the specification.  In fact, aside from the case of
580 @code{gss_add_oid_set_member}, they're generally output arguments,
581 with the previous value ignored.)
582
583 The function @code{gss_add_cred} can modify the
584 @code{input_cred_handle} object, if a null @code{output_cred_handle}
585 argument is supplied.  Thus, all @code{gss_cred_id_t} objects must
586 have mutexes, and all accesses (except in the functions creating or
587 destroying them) must acquire the mutex first.
588
589 Note that the use of @code{const} in the GSSAPI C bindings is not a
590 useful guide to when an object might or might not be modified.  In
591 most cases, @code{const} is applied to handle arguments, which are
592 defined as arithmetic or pointer types.  It applies to the argument
593 itself, not the data pointed to @i{if} the type is a pointer; this
594 would mean that the GSSAPI function in question cannot modify the
595 value of its handle parameter, and puts no constraints on
596 modifications to the object indicated by the handle.  And according to
597 the C type compatibility rules, the function definition can omit those
598 @code{const} qualifiers anyways.@footnote{If you're thinking that this
599 means the use of @code{const} in the GSSAPI C bindings is confusing
600 and/or useless, you're right.}
601
602
603
604
605 @node Thread System Requirements, Internal Thread API, Kerberos API Thread Safety, Thread Safety
606 @section Thread System Requirements
607
608 We support a few types of environments with regard to thread support:
609
610 @itemize @bullet
611
612 @item
613 Windows native threads.  The objects used by the Windows thread
614 support functions generally need run-time initialization; this is done
615 through the library initialization function.  (@xref{Advanced Shared
616 Library Requirements}.)
617
618 @item
619 POSIX threads, with weak reference support so we can tell whether the
620 thread code was actually linked into the current executable.  If the
621 functions aren't available, we assume the process is single-threaded
622 and ignore locks.  (We do assume that the thread support functions
623 won't show up half-way through execution of the program.)  In order to
624 support single-threaded programs wanting to load Kerberos or GSSAPI
625 modules through a plug-in mechanism, we don't list the pthread library
626 in the dependencies of our shared libraries.
627
628 @item
629 POSIX threads, with the library functions always available, even if
630 they're stub versions that behave normally but don't permit the
631 creation of new threads.
632
633 On AIX 4.3.3, we do not get weak references or useful stub functions,
634 and calling @code{dlopen} apparently causes the pthread library to get
635 loaded, so we've decided to link against the pthread library always.
636
637 On Tru64 UNIX 5.1, we again do not get weak references or useful stub
638 functions.  Rather than look for yet another approach for this one
639 platform, we decided to always link against the pthread library on
640 this platform as well.  This may break single-threaded applications
641 that load the Kerberos libraries after startup.  A clean solution,
642 even if platform-dependent, would be welcome.
643
644 @item
645 Single-threaded.  No locking is performed, any ``thread-local''
646 storage is in fact global, @i{etc}.
647
648 @end itemize
649
650 If @code{pthread_once} is not provided in functional form in the
651 default libraries, and weak references are not supported, we always
652 link against the pthread libraries.  (Tru64, AIX.)
653
654 System routines: @code{getaddrinfo} (not always implemented
655 thread-safe), @code{gethostbyname_r}, @code{gmtime_r},
656 @code{getpwnam_r} (but interfaces vary, see @file{k5-platform.h}),
657 @code{res_nsearch} (but watch for resource leaks).
658
659 Unsafe system routines: @code{setenv}, @code{setlocale}.
660
661 @node Internal Thread API, Thread Shim Layer Implementation, Thread System Requirements, Thread Safety
662 @section Internal Thread API
663
664 Some ideas were discussed on the @samp{krbdev} mailing list, and while
665 the current implementation does largely resemble the scheme Ken
666 Raeburn proposed, some details have changed.
667
668 The following macros in @file{k5-thread.h} implement a locking scheme
669 similar to POSIX threads, with fewer features.
670
671 @deftp {Data type} k5_mutex_t
672 This is the type of a mutex to be used by the Kerberos libraries.  Any
673 object of this type needs initialization.  If the object is
674 dynamically allocated, @code{k5_mutex_init} must be used; if the
675 object is allocated statically, it should be initialized at compile
676 time with @code{K5_MUTEX_PARTIAL_INITIALIZER} and then
677 @code{k5_mutex_finish_init} should be called at run time.  (In
678 general, one of these will do the work, and the other will do nothing
679 interesting, depending on the platform.  When the debugging code is
680 turned on, it will check that both were done.  However, as far as I
681 know, it should work to use just @code{k5_mutex_init} on a mutex in
682 static storage.)
683
684 The mutex may be used only within the current process.  It should not
685 be created in memory shared between processes.  (Will it work in a
686 child process after @code{fork()}?  I think so.)
687
688 The @code{k5_mutex_t} object contains more than an operating-system
689 mutex; it may also contain debugging information such as the file and
690 line number in the Kerberos code where the last mutex operation was
691 performed, information for gathering statistics on mutex usage,
692 @i{etc}., depending on compile-time options.
693
694 This type @emph{is not} a simple typedef for the native OS mutex
695 object, to prevent programmers from accidentally assuming that
696 arbitrary features of the native thread system will always be
697 available.  (If someone wishes to make use of native thread system
698 features in random library code, they'll have to go further out of
699 their way to do it, and such changes probably won't be accepted in the
700 main Kerberos code base at MIT.)
701
702 If thread support is disabled, a simple flag will be stored in place
703 of the operating-system mutex.  This flag indicates the ``locked''
704 state, and is checked in the @code{k5_mutex_lock} and
705 @code{k5_mutex_unlock} macros so that we can detect some cases of
706 improperly written code even if thread support is not built in.  The
707 other debugging fields will still be present as well.
708
709 If POSIX thread support and weak references are available, both the
710 POSIX mutex and a flag will be included; which one is used is
711 determined at run time depending on whether the thread support
712 routines are available.
713 @end deftp
714
715 @defvr Macro K5_MUTEX_PARTIAL_INITIALIZER
716 Value to be used for compile-time initialization of a mutex in static
717 storage.
718 @end defvr
719
720 @deftypefn Macro int k5_mutex_finish_init (k5_mutex_t *@var{m})
721 Finishes run-time initialization, if such is needed, of a mutex that
722 was initialized with @code{K5_MUTEX_PARTIAL_INITIALIZER}.  This macro
723 must be called before the mutex can be locked; usually this is done
724 from library initialization functions.
725 @end deftypefn
726
727 @deftypefn Macro int k5_mutex_init (k5_mutex_t *@var{m})
728 Initializes a mutex.
729 @end deftypefn
730
731 @deftypefn Macro int k5_mutex_destroy (k5_mutex_t *@var{m})
732 Destroys a mutex, whether allocated in static or heap storage.  All
733 mutexes should be destroyed before the containing storage is freed, in
734 case additional system resources have been allocated to manage them.
735 @end deftypefn
736
737 @deftypefn Macro int k5_mutex_lock (k5_mutex_t *@var{m})
738 @deftypefnx Macro int k5_mutex_unlock (k5_mutex_t *@var{m})
739 Lock or unlock a mutex, returning a system error code if an error
740 happened, or zero for success.  (Typically, the return code from
741 @code{k5_mutex_unlock} is ignored.)
742 @end deftypefn
743
744 @deftypefn Macro void k5_mutex_assert_locked (k5_mutex_t *@var{m})
745 @deftypefnx Macro void k5_mutex_assert_unlocked (k5_mutex_t *@var{m})
746 These macros may be used in functions that require that a certain
747 mutex be locked by the current thread, or not, at certain points
748 (typically on entry to the function).  They may generate error
749 messages or debugger traps, or abort the program, if the mutex is not
750 in the expected state.  Or, they may simply do nothing.
751
752 It is not required that the OS mutex interface let the application
753 code determine the state of a mutex; hence these are not specified as
754 a single macro returning the current state, to be checked with
755 @code{assert}.
756 @end deftypefn
757
758 Mutexes should be assumed not to be recursive; if a thread has the
759 mutex locked already, attempting to lock it again is an error and may
760 have unpredictable results (error return, abort, data corruption).
761 There is also no support assumed for ``trylock'' or ``lock with
762 timeout'' operations.
763
764 Kerberos library code should use the macros above, and ports to new
765 thread systems should be done through the @code{k5_os_} layer.
766 (@xref{Thread Shim Layer Implementation}.)
767
768 Thread-local storage is managed through another interface layer:
769
770 @deftp {Enumerator} k5_key_t
771 This is an enumeration type which indicates which of the per-thread
772 data objects is to be referenced.
773 @end deftp
774
775 @deftypefn Macro int k5_key_register (k5_key_t @var{key}, void (*@var{destructor})(void*))
776 Registers a thread-local storage key and a function to destroy a
777 stored object if the thread exits.  This function must be called
778 before @code{k5_setspecific} can be used.  Currently @var{destructor}
779 must not be a null pointer; note, however, that the standard library
780 function @code{free} is of the correct type to be used here if the
781 allocated data doesn't require any special cleanup besides releasing
782 one block of storage.
783 @end deftypefn
784
785 @deftypefn Macro void *k5_getspecific (k5_key_t @var{key})
786 @deftypefnx Macro int k5_setspecific (k5_key_t @var{key}, void *@var{value})
787 As with the POSIX interface, retrieve or store the value for the
788 current thread.  Storing a value may return an error indication.  If
789 an error occurs retrieving a value, @code{NULL} is returned.
790 @end deftypefn
791
792 @deftypefn Macro int k5_key_delete (k5_key_t @var{key})
793 Called to indicate that the key value will no longer be used, for
794 example if the library is in the process of being unloaded.  The
795 destructor function should be called on objects of this type currently
796 allocated in any thread.  (XXX Not implemented yet.)
797 @end deftypefn
798
799 If support functions are needed to implement any of these macros,
800 they'll be in the Kerberos support library, and any exported symbols
801 will use the @code{krb5int_} prefix.  The shorter @code{k5_} prefix is
802 just for convenience, and should not be visible to any application
803 code.
804
805 @node Thread Shim Layer Implementation,  , Internal Thread API, Thread Safety
806 @section Thread Shim Layer Implementation
807
808 Each of the @code{k5_mutex_} macros has a corresponding
809 @code{k5_os_mutex_} macro which incorporates the operating system's
810 mutex object, a flag for non-threaded systems, or both if the use of
811 the OS pthread support is left until run time.  The @code{k5_mutex_}
812 wrappers add debugging information like the file and line number of
813 the last lock or unlock call, where the mutex was created, @i{etc}.
814 There may also be statistical information gathered, such as how long a
815 thread waits for a mutex or how long the mutex is held.  This is all
816 defined in @file{k5-thread.h}.
817
818 The thread-specific storage support is defined as macros expanding to
819 similar function names with the @code{krb5int_} prefix, which
820 functions are defined in @file{util/support/threads.c}.  POSIX,
821 Windows, and non-threaded versions are defined so far.
822
823 The code for collecting and reporting statistics is also mainly in
824 that file.  Macros defined in @file{k5-thread.h} will expand to
825 include calls to these functions, if @code{DEBUG_THREADS_STATS} is
826 defined, or do nothing.
827
828 @node Shared Libraries, Porting Issues, Thread Safety, Top
829 @chapter Shared Libraries
830
831 (These sections are old -- they should get updated.)
832
833 @menu
834 * Shared Library Theory::       
835 * Advanced Shared Library Requirements::  
836 * Operating System Notes for Shared Libraries::  
837 @end menu
838
839 @node Shared Library Theory, Advanced Shared Library Requirements, Shared Libraries, Shared Libraries
840 @section Theory of How Shared Libraries are Used
841
842 An explanation of how shared libraries are implemented on a given
843 platform is too broad a topic for this manual. Instead this will touch
844 on some of the issues that the Kerberos V5 tree uses to support version
845 numbering and alternate install locations.
846
847 Normally when one builds a shared library and then links with it, the
848 name of the shared library is stored in the object
849 (i.e. libfoo.so). Most operating systems allows one to change name that
850 is referenced and we have done so, placing the version number into the
851 shared library (i.e. libfoo.so.0.1). At link time, one would reference
852 libfoo.so, but when one executes the program, the shared library loader
853 would then look for the shared library with the alternate name.  Hence
854 multiple versions of shared libraries may be supported relatively
855 easily. @footnote{Under AIX for the RISC/6000, multiple versions of
856 shared libraries are supported by combining two or more versions of the
857 shared library into one file.  The Kerberos build procedure produces
858 shared libraries with version numbers in the internal module names, so
859 that the shared libraries are compatible with this scheme.
860 Unfortunately, combining two shared libraries requires internal
861 knowledge of the AIX shared library system beyond the scope of this
862 document.  Practically speaking, only one version of AIX shared libraries
863 can be supported on a system, unless the multi-version library is
864 constructed by a programmer familiar with the AIX internals.}
865
866 All operating systems (that we have seen) provide a means for programs
867 to specify the location of shared libraries.  On different operating
868 systems, this is either specified when creating the shared library, and
869 link time, or both.@footnote{Both are necessary sometimes as the shared
870 libraries are dependent on other shared libraries.}  The build process
871 will hardwire a path to the installed destination.
872
873 @node Advanced Shared Library Requirements, Operating System Notes for Shared Libraries, Shared Library Theory, Shared Libraries
874 @section Advanced Shared Library Requirements
875
876 In order to better support some multithreading models, and permit the
877 libraries to clean up internally maintained caches of information,
878 we've imposed new requirements on the OS shared library support.
879
880 Specifically, we want the ability to run certain bits of code in a
881 thread-safe manner at library load time, on multithreading platforms
882 not supporting @code{pthread_once}, and we want the ability to run
883 cleanup code when the library is unloaded.
884
885 In general, where platforms have initialization functions, we don't
886 always get an opportunity to return an error from them.  However, the
887 system functions we call can return errors.  So a framework has been
888 built that attempts to combine the @code{pthread_once} and load-time
889 initialization approaches, and add the ability to store an error code
890 indicating success or failure of the initialization routine.
891
892 The main implementation of this framework is in @file{k5-platform.h}.
893 Some additional information, specifically the names of the
894 initialization and finalization functions, are stored in the makefiles
895 used to generate each of the UNIX libraries, in @file{win_glue.c}, and
896 somewhere in the Mac OS X support (XXX not added yet?).  How the
897 information is used depends on the platform:
898
899 @itemize @bullet
900
901 @item
902 On platforms without any thread support, a simple flag is used, on the
903 assumption that the library code will have sole control over the
904 process execution until the initialization function returns.  (It's
905 generally a bad idea to call any ``interesting'' function like
906 @code{longjmp} or Kerberos functions from signal handlers; now it's a
907 slightly worse idea.)
908
909 @item
910 On platforms supporting @code{pthread_once}, library initialization is
911 generally delayed until the point where the library code needs to
912 verify that the initialization succeeded.  If @code{pthread_once} may
913 not have been linked into the executable and we can tell (for example,
914 with weak symbol references), this is combined with the simple-flag
915 approach above.
916
917 @item
918 On Windows, the library initialization function is run from
919 @file{win_glue.c} at load time; it should complete before the
920 library's symbol table is made accessible to the calling process.
921
922 Windows note: There are limitations on what @code{DllMain} should do
923 in the initialization and finalization cases, and unfortunately we've
924 found that we do some of these things (specifically, calling
925 @code{WSAStartup} and @code{WSACleanup}, and loading other libraries
926 which do so also).  Until we can rectify this problem, there is a
927 chance that explicitly unloading an MIT Kerberos library from an
928 application (more specifically, from within the @code{DllMain} of
929 another library) may cause a deadlock.
930 @end itemize
931
932 Library finalization is similarly dependent on the platform and
933 configuration:
934
935 @itemize @bullet
936
937 @item
938 In static-library builds, since the library will be unloaded only when
939 the entire process calls @code{exit} or @code{exec} or otherwise
940 ceases to exist in its current form, freeing up memory resources in
941 the process, finalization can be skipped.
942
943 @item
944 On UNIX platforms with library finalization support in shared
945 libraries, the (OS-level) finalization function is specified to run
946 the library's (shim-level) finalization function.  If @code{gcc}'s
947 ``destructor'' attribute appears to work, we use that.
948
949 @item
950 On UNIX platforms without library finalization function support,
951 the finalization functions won't get called if the library is
952 unloaded.  Resources (probably just memory) will be leaked.
953
954 @item
955 On Windows, the finalization code is run out of @code{DllMain} in
956 @file{win_glue.c} at unload time.  See the warnings above.
957
958 @end itemize
959
960 If there are other limitations on what operations can be performed in
961 shared library initialization and finalization routines on some
962 systems, the MIT Kerberos team would appreciate specific information
963 on these limitations.
964
965 The internal interface currently used within the code of the Kerberos
966 libraries consists of four macros:
967
968 @defmac MAKE_INIT_FUNCTION (@var{fname})
969 Used at the top level of the file (@i{i.e.}, not within a function),
970 with a semicolon after it, declares @var{fname}, a function taking no
971 arguments and returning @code{int}, to be an initialization function.
972 This macro must be used before the function is declared, and it must
973 be defined in the current file as:
974 @example
975 int @var{fname} (void) @{ ... @}
976 @end example
977 This macro may define additional data and function objects,
978 and will declare @var{fname}, though it may or may not declare
979 @var{fname} as @code{static}.  (Under C rules, the declaration above
980 is compatible with a declaration of the function as @code{static}, and
981 @code{static} linkage does apply, as long as the @code{static} declaration
982 comes first.)
983
984 When the function is invoked, the return value --- zero or an error
985 code --- will be saved away, and returned any time
986 @code{CALL_INIT_FUNCTION} is used.
987
988 There can be multiple initialization functions defined this way in a
989 library.
990 @end defmac
991
992 @defmac MAKE_FINI_FUNCTION (@var{fname})
993 This is similar to @code{MAKE_INIT_FUNCTION} except that @var{fname}
994 is to be a library finalization function, called when the library is
995 no longer in use and is being unloaded from the address space.
996 @example
997 void @var{fname} (void) @{ ... @}
998 @end example
999
1000 There may be multiple finalization functions defined for a library.
1001 @end defmac
1002
1003 @deftypefn Macro int CALL_INIT_FUNCTION (@var{fname})
1004 This macro ensures that the initialization function @var{fname} is
1005 called at this point, if it has not been called already.  The macro
1006 returns an error code that indicates success (zero), an error in the
1007 OS support (@i{e.g.}, if @code{pthread_once} returns an error), or an
1008 error returned by the initialization function.
1009
1010 Currently, all uses of @code{CALL_INIT_FUNCTION} must be in the same
1011 file as the use of @code{MAKE_INIT_FUNCTION}, and must come after it.
1012 @end deftypefn
1013
1014 @deftypefn Macro int INITIALIZER_RAN (@var{fname})
1015 This macro returns non-zero iff the initialization function designated
1016 by @var{fname} (and previously declared in the current file with
1017 @code{MAKE_INIT_FUNCTION}) has been run, and returned no error
1018 indication.
1019
1020 Since the finalization function might always be invoked through linker
1021 support and initialization functions only sometimes invoked via
1022 @code{pthread_once} in other functions that may not ever be called,
1023 finalization functions should check whether the objects to be
1024 destroyed have actually been created.  This macro provides one way of
1025 doing that.
1026 @end deftypefn
1027
1028 The @file{Makefile.in} for the library must define two variables,
1029 @code{LIBINITFUNC} and @code{LIBFINIFUNC}, containing a (possibly
1030 empty) list of the names of the initialization and finalization
1031 functions for the library as built under UNIX, ordered from
1032 lowest-level (initialized first, finalized last) to highest-level.
1033 (Windows and Mac OS X builds work differently.)
1034
1035 Note that all of this assumes shared libraries.  If static linking is
1036 done, our options are a bit more limited.  We assume
1037 @code{pthread_once} is available if there is any thread support
1038 (@i{i.e.}, we don't support static linking on Windows), and we assume
1039 that finalization code would be called only when the process is
1040 exiting, at which point all resources should be freed up anyways, so
1041 it doesn't really matter whether our cleanup code gets called.  In
1042 fact, it should be more efficient if it does not.
1043
1044 While one of our goals is to be able to repeatedly load, use, and
1045 unload the MIT Kerberos libraries under a plugin architecture without
1046 memory or other resource leaks, the main goal was to provide hooks
1047 through which the library threading support could be properly
1048 initialized on various platforms.  The hooks we've added should be
1049 sufficient for each library to free up any internal caches of
1050 information at unload time, and we have added some of that support,
1051 but it is not complete at this time.
1052
1053
1054 We have also started limiting the list of exported symbols from shared
1055 libraries on some UNIX platforms, and intend to start doing symbol
1056 versioning on platforms that support it.  The symbol lists we use for
1057 UNIX at the moment are fairly all-inclusive, because we need more
1058 symbols exported than are in the lists used for Windows and Mac
1059 platforms, and we have not yet narrowed them down.  The current lists
1060 should not be taken as an indication of what we intend to export and
1061 support in the future; see @file{krb5.h} for that.
1062
1063 The export lists are stored in the directories in which each UNIX
1064 library is built, and the commands set up at configuration time by
1065 @file{shlib.conf} can specify any processing to be done on those files
1066 (@i{e.g.}, insertion of leading underscores or linker command-line
1067 arguments).
1068
1069 For some systems with somewhat non-trivial commands that need to be
1070 run to convert the export list into the proper form, file targets can be
1071 defined in @file{config/lib.in}.  
1072
1073 @node Operating System Notes for Shared Libraries,  , Advanced Shared Library Requirements, Shared Libraries
1074 @section Operating System Notes for Shared Libraries
1075
1076 From time to time users or developers suggest using GNU @code{Libtool}
1077 or some other mechanism to  generate shared libraries.  Experience
1078 with other packages suggests that Libtool tends to be difficult to
1079 debug and when it works incorrectly, patches are required to generated
1080 scripts to work around problems.  So far, the Kerberos shared library
1081 build mechanism, which sets a variety of makefile variables based on
1082 operating system type and then uses those variables in the build
1083 process has proven to be easier to debug and adequate to the task of
1084 building shared libraries for Kerberos.
1085
1086 @menu
1087 * AIX Shared Library Support::  
1088 * Alpha OSF/1 Shared Library Support::  
1089 * ELF Shared Library Support::  
1090 @end menu
1091
1092 @node AIX Shared Library Support, Alpha OSF/1 Shared Library Support, Operating System Notes for Shared Libraries, Operating System Notes for Shared Libraries
1093 @subsection AIX Shared Library Support
1094
1095         AIX specifies shared library versions by combining multiple
1096 versions into a single file.  Because of the complexity of this process,
1097 no automatic procedure for building multi-versioned shared libraries is
1098 provided. Therefore, supporting multiple versions of the Kerberos shared
1099 libraries under AIX will require significant work on the part of a
1100 programmer famiiliar with AIX internals.  
1101
1102         AIX allows a single library to be used both as a static library
1103 and as a shared library.  For this reason, the @samp{--enable-shared}
1104 switch to configure builds only shared libraries.  On other operating
1105 systems, both shared and static libraries are built when this switch is
1106 specified.  As with all other operating systems, only non-shared static
1107 libraries are built when @samp{--enable-shared} is not specified.
1108
1109         The AIX 3.2.5 linker dumps core trying to build a shared
1110 @samp{libkrb5.a} produced with the GNU C compiler.  The native AIX
1111 compiler works fine.  In addition, the AIX 4.1 linker is able to build a
1112 shared @samp{libkrb5.a} when GNU C is used.
1113
1114
1115 @node Alpha OSF/1 Shared Library Support, ELF Shared Library Support, AIX Shared Library Support, Operating System Notes for Shared Libraries
1116 @subsection Alpha OSF/1 Shared Library Support
1117
1118 Shared library support has been tested with V2.1 and higher of the
1119 operating system. Shared libraries may be compiled both with GCC and the
1120 native compiler.
1121
1122 One of the nice features on this platform is that the paths to the
1123 shared libraries is specified in the library itself without requiring
1124 that one specify the same at link time. 
1125
1126 We are using the @samp{-rpath} option to @samp{ld} to place the library
1127 load path into the executables. The one disadvantage of this is during
1128 testing where we want to make sure that we are using the build tree
1129 instead of a possibly installed library. The loader uses the contents of
1130 @samp{-rpath} before LD_LIBRARY_PATH so we must specify a dummy _RLD_ROOT
1131 and complete LD_LIBRARY_PATH in our tests.
1132
1133 The one disadvantage with the method we are using....  [What??  This
1134 never got finished!]
1135
1136 @node ELF Shared Library Support,  , Alpha OSF/1 Shared Library Support, Operating System Notes for Shared Libraries
1137 @subsection ELF Shared Library Support
1138
1139 It's tempting to add @samp{-Bsymbolic} to the commands for generating
1140 shared libraries.  We don't explicitly support overriding our
1141 (internal or public) symbol names by applications, and binding at
1142 shared library creation time would result in smaller libraries that
1143 would load faster.  However, it won't work.  At least with the GNU and
1144 Solaris linkers and runtime environments, an executable using a data
1145 symbol exported by a shared library gets a copy of that data,
1146 initialized at run time by copying, and the program will only function
1147 properly if that definition can override the one present in the shared
1148 library.  And, sadly, some of our shared libraries export variables
1149 that are directly referenced from programs.
1150
1151 @node Porting Issues,  , Shared Libraries, Top
1152 @chapter Porting Issues
1153
1154 [Snipped from email from Ken Raeburn in reply to email asking about
1155 porting MIT Kerberos to pSOS; maybe it'll be of use to someone else.]
1156
1157 > - Any Porting issues to be considered?
1158
1159 Yes.  Our build procedure currently assumes that the machine used for
1160 building is either a UNIX (or similar) system, or running Windows; it
1161 also assumes that it's a native compilation, not a cross compilation.
1162 I'm not familiar with pSOS, but assuming that you do cross compilation
1163 on another OS, how you deal with that depends on the host system.
1164
1165 UNIX host: The configure script attempts to learn a bunch of
1166 attributes about the host system (program names, availability of
1167 header files and functions and libraries) and uses them to decide how
1168 to build the krb5 libraries and programs.  Many attributes are tested
1169 by running the compiler with various options and test source files, so
1170 if you tell the configure script to run a cross compiler, it may come
1171 up with most of the right answers, if you can arrange for success and
1172 failure indications to be given at compile/link time.  (This probably
1173 wouldn't work well for VxWorks, for example, where symbol resolution
1174 is done when the object code is loaded into the OS.)
1175
1176 The configure script generates include/autoconf.h to influence
1177 whether certain calls are made or certain headers are included, and
1178 Makefile in each directory to indicate compilation options.  Each
1179 source directory has a Makefile.in, and config/pre.in and
1180 config/post.in are incorporated into each generate Makefile.  Various
1181 @@FOO@@ sequences are substituted based on the system attributes or
1182 configure options.  (Aside from always using the config/ fragments,
1183 this is typical of GNU Autoconf based software configuration.)
1184
1185 Windows host: The ``wconfig'' program generates the Makefiles in
1186 subdirectories, with config/win-pre.in and config/win-post.in used in
1187 combination with each Makefile.in, and lines starting @code{##WIN32##}
1188 are uncommented, but @code{@@FOO@@} substitutions are not done.
1189 Instead of generating @file{autoconf.h}, it's copied from
1190 @file{include/win-mac.h}, where we've hardcoded some of the parameters
1191 we care about, and just left a bunch of others out.  If you work with
1192 a Windows host, you may want to provide your own makefile fragments,
1193 and a replacement for @file{win-mac.h} or some additional data to go
1194 into it conditionalized on some preprocessor symbol for pSOS.
1195
1196 There are also places where we assume that certain header files or
1197 functions are available, because both (most) UNIX and Windows
1198 platforms (that we care about currently) provide them.  And probably a
1199 handful of places where we check for @code{_WIN32} to decide between ``the
1200 Windows way'' and ``everything else'' (i.e., UNIX); you might need to add
1201 a third branch for pSOS.  And some places where we've got hooks for
1202 Kerberos for Mac support, which you can probably ignore.
1203
1204 Our build environment assumes that Perl is available, but it's only
1205 needed in the build process, not for run time.  If Tcl is available,
1206 on UNIX, a few more programs may be built that are used for testing
1207 some interfaces, but a cross compiler should notice that it can't link
1208 against the native Tcl libraries, and configure should choose not to
1209 build those programs.
1210
1211 In the current 1.4 beta code, our library wants to find routines for
1212 making DNS queries (SRV and TXT RR queries specifically) that are
1213 outside the scope of getaddrinfo and friends.  We also look for
1214 @file{/dev/random} as a strong random number source, and text files
1215 for configuration information.  Our code assumes that allocating and
1216 reallocating lots of little (or not so little) bits of memory isn't
1217 too horribly expensive, and we don't take any special pains to keep
1218 our stack size small.  Depending how pSOS works, you may need to add
1219 to the thread support code.  (The MIT code doesn't create threads, but
1220 will do locking and such to allow multiple threads to share global
1221 data.  The code in @file{include/k5-thread.h} is, uh, kind of
1222 involved, and some pains have been taken to use macros whenever
1223 possible to allow @code{assert()} calls during debugging to report
1224 useful line numbers.)  There are probably other minor issues to deal
1225 with, I'm just making some guesses.
1226
1227 > - what type of Data formats exchanged between Client and Server?
1228
1229 If you're aiming for a server implementation only, it'll depend on the
1230 exact protocol you wish to use, but typically the Kerberos application
1231 server needs to accept the AP-REQ message and generate the AP-REP
1232 message.  Protection for the data to be transferred depends on on the
1233 application protocol.  For example, Kerberos provides some message
1234 types for encapsulating application data with or without encryption;
1235 the Kerberos mechanism for GSSAPI uses the Kerberos session key to
1236 protect application data in a different message format.
1237
1238 The server implementation would also need some secure means of getting
1239 the service principal's key stored away.
1240
1241 If you want client code support as well under pSOS, then you may have
1242 to deal with DNS queries to find the KDC,
1243 AS-REQ/AS-REP/TGS-REQ/TGS-REP message exchanges, and generating AP-REQ
1244 and accepting AP-REP messages, etc.
1245
1246 @contents
1247 @bye