]> granicus.if.org Git - linux-pam/blob - doc/pam_appl.sgml
Relevant BUGIDs: none
[linux-pam] / doc / pam_appl.sgml
1 <!doctype linuxdoc system>
2
3 <!--
4
5  $Id$
6
7     Copyright (C) Andrew G. Morgan 1996-2001.  All rights reserved.
8
9 Redistribution and use in source (sgml) and binary (derived) forms,
10 with or without modification, are permitted provided that the
11 following conditions are met:
12
13 1. Redistributions of source code must retain the above copyright
14    notice, and the entire permission notice in its entirety,
15    including the disclaimer of warranties.
16
17 2. Redistributions in binary form must reproduce the above copyright
18    notice, this list of conditions and the following disclaimer in the
19    documentation and/or other materials provided with the distribution.
20
21 3. The name of the author may not be used to endorse or promote
22    products derived from this software without specific prior
23    written permission.
24
25 ALTERNATIVELY, this product may be distributed under the terms of the
26 GNU General Public License, in which case the provisions of the GNU
27 GPL are required INSTEAD OF the above restrictions.  (This clause is
28 necessary due to a potential bad interaction between the GNU GPL and
29 the restrictions contained in a BSD-style copyright.)
30
31 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
32 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
36 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
37 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
39 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
40 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 DAMAGE.
42
43  -->
44
45 <article>
46
47 <title>The Linux-PAM Application Developers' Guide
48 <author>Andrew G. Morgan, <tt>morgan@kernel.org</tt>
49 <date>DRAFT v0.76 2001/12/08
50 <abstract>
51 This manual documents what an application developer needs to know
52 about the <bf>Linux-PAM</bf> library. It describes how an application
53 might use the <bf>Linux-PAM</bf> library to authenticate users. In
54 addition it contains a description of the funtions to be found in
55 <tt/libpam_misc/ library, that can be used in general applications.
56 Finally, it contains some comments on PAM related security issues for
57 the application developer.
58 </abstract>
59
60 <toc>
61
62 <sect>Introduction
63
64 <sect1>Synopsis
65
66 <p>
67 For general applications that wish to use the services provided by
68 <bf/Linux-PAM/ the following is a summary of the relevant linking
69 information:
70 <tscreen>
71 <verb>
72 #include <security/pam_appl.h>
73
74 cc -o application .... -lpam -ldl
75 </verb>
76 </tscreen>
77
78 <p>
79 In addition to <tt/libpam/, there is a library of miscellaneous
80 functions that make the job of writing <em/PAM-aware/ applications
81 easier (this library is not covered in the DCE-RFC for PAM and is
82 specific to the Linux-PAM distribution):
83 <tscreen>
84 <verb>
85 ...
86 #include <security/pam_misc.h>
87
88 cc -o application .... -lpam -lpam_misc -ldl
89 </verb>
90 </tscreen>
91
92 <sect1> Description
93
94 <p>
95 <bf>Linux-PAM</bf> (Pluggable Authentication Modules for Linux) is a
96 library that enables the local system administrator to choose how
97 individual applications authenticate users.  For an overview of the
98 <bf>Linux-PAM</bf> library see the <bf/Linux-PAM/ System
99 Administrators' Guide.
100
101 <p>
102 It is the purpose of the <bf>Linux-PAM</bf> project to liberate the
103 development of privilege granting software from the development of
104 secure and appropriate authentication schemes.  This is accomplished
105 by providing a documented library of functions that an application may
106 use for all forms of user authentication management. This library
107 dynamically loads locally configured authentication modules that
108 actually perform the authentication tasks.
109
110 <p>
111 From the perspective of an application developer the information
112 contained in the local configuration of the PAM library should not be
113 important.  Indeed it is intended that an application treat the
114 functions documented here as a ``black box'' that will deal with all
115 aspects of user authentication. ``All aspects'' includes user
116 verification, account management, session initialization/termination
117 and also the resetting of passwords (<em/authentication tokens/).
118
119 <sect>Overview
120
121 <p>
122 Most service-giving applications are restricted.  In other words,
123 their service is not available to all and every prospective client.
124 Instead, the applying client must jump through a number of hoops to
125 convince the serving application that they are authorized to obtain
126 service.
127
128 The process of <em/authenticating/ a client is what PAM is designed to
129 manage.  In addition to authentication, PAM provides account
130 management, credential management, session management and
131 authentication-token (password changing) management services.  It is
132 important to realize when writing a PAM based application that these
133 services are provided in a manner that is <bf>transparent</bf> to
134 the application.  That is to say, when the application is written, no
135 assumptions can be made about <em>how</em> the client will be
136 authenticated.
137
138 <p>
139 The process of authentication is performed by the PAM library via a
140 call to <tt>pam_authenticate()</tt>.  The return value of this
141 function will indicate whether a named client (the <em>user</em>) has
142 been authenticated.  If the PAM library needs to prompt the user for
143 any information, such as their <em>name</em> or a <em>password</em>
144 then it will do so.  If the PAM library is configured to authenticate
145 the user using some silent protocol, it will do this too.  (This
146 latter case might be via some hardware interface for example.)
147
148 <p>
149 It is important to note that the application must leave all decisions
150 about when to prompt the user at the discretion of the PAM library.
151
152 <p>
153 The PAM library, however, must work equally well for different styles
154 of application.  Some applications, like the familiar <tt>login</tt>
155 and <tt>passwd</tt> are terminal based applications, exchanges of
156 information with the client in these cases is as plain text messages.
157 Graphically based applications, however, have a more sophisticated
158 interface.  They generally interact with the user via specially
159 constructed dialogue boxes.  Additionally, network based services
160 require that text messages exchanged with the client are specially
161 formatted for automated processing: one such example is <tt>ftpd</tt>
162 which prefixes each exchanged message with a numeric identifier.
163
164 <p>
165 The presentation of simple requests to a client is thus something very
166 dependent on the protocol that the serving application will use.  In
167 spite of the fact that PAM demands that it drives the whole
168 authentication process, it is not possible to leave such protocol
169 subtleties up to the PAM library.  To overcome this potential problem,
170 the application provides the PAM library with a <em>conversation</em>
171 function.  This function is called from <bf>within</bf> the PAM
172 library and enables the PAM to directly interact with the client.  The
173 sorts of things that this conversation function must be able to do are
174 prompt the user with text and/or obtain textual input from the user
175 for processing by the PAM library.  The details of this function are
176 provided in a later section.
177
178 <p>
179 For example, the conversation function may be called by the PAM library
180 with a request to prompt the user for a password.  Its job is to
181 reformat the prompt request into a form that the client will
182 understand.  In the case of <tt>ftpd</tt>, this might involve prefixing
183 the string with the number <tt>331</tt> and sending the request over
184 the network to a connected client.  The conversation function will
185 then obtain any reply and, after extracting the typed password, will
186 return this string of text to the PAM library.  Similar concerns need
187 to be addressed in the case of an X-based graphical server.
188
189 <p>
190 There are a number of issues that need to be addressed when one is
191 porting an existing application to become PAM compliant.  A section
192 below has been devoted to this: Porting legacy applications.
193
194 <p>
195 Besides authentication, PAM provides other forms of management.
196 Session management is provided with calls to
197 <tt>pam_open_session()</tt> and <tt>pam_close_session()</tt>.  What
198 these functions actually do is up to the local administrator.  But
199 typically, they could be used to log entry and exit from the system or
200 for mounting and unmounting the user's home directory.  If an
201 application provides continuous service for a period of time, it
202 should probably call these functions, first open after the user is
203 authenticated and then close when the service is terminated.
204
205 <p>
206 Account management is another area that an application developer
207 should include with a call to <tt/pam_acct_mgmt()/.  This call will
208 perform checks on the good health of the user's account (has it
209 expired etc.). One of the things this function may check is whether
210 the user's authentication token has expired - in such a case the
211 application may choose to attempt to update it with a call to
212 <tt/pam_chauthtok()/, although some applications are not suited to
213 this task (<em>ftp</em> for example) and in this case the application
214 should deny access to the user.
215
216 <p>
217 PAM is also capable of setting and deleting the users credentials with
218 the call <tt>pam_setcred()</tt>.  This function should always be
219 called after the user is authenticated and before service is offered
220 to the user.  By convention, this should be the last call to the PAM
221 library before the PAM session is opened.  What exactly a credential
222 is, is not well defined.  However, some examples are given in the
223 glossary below.
224
225 <sect>The public interface to <bf>Linux-PAM</bf>
226  
227 <p>
228 Firstly, the relevant include file for the <bf>Linux-PAM</bf> library
229 is <tt>&lt;security/pam_appl.h&gt;</tt>. It contains the definitions
230 for a number of functions. After listing these functions, we collect
231 some guiding remarks for programmers.
232
233 <sect1>What can be expected by the application
234
235 <p>
236 Below we document those functions in the <bf/Linux-PAM/ library that
237 may be called from an application.
238
239 <sect2>Initialization of Linux-PAM
240 <label id="pam-start-section">
241
242 <p>
243 <tscreen>
244 <verb>
245 extern int pam_start(const char *service_name, const char *user,
246                      const struct pam_conv *pam_conversation,
247                      pam_handle_t **pamh);
248 </verb>
249 </tscreen>
250
251 <p>
252 This is the first of the <bf>Linux-PAM</bf> functions that must be
253 called by an application. It initializes the interface and reads the
254 system configuration file, <tt>/etc/pam.conf</tt> (see the
255 <bf/Linux-PAM/ System Administrators' Guide).  Following a successful
256 return (<tt/PAM_SUCCESS/) the contents of <tt/*pamh/ is a handle that
257 provides continuity for successive calls to the <bf/Linux-PAM/
258 library.  The arguments expected by <tt/pam_start/ are as follows: the
259 <tt/service_name/ of the program, the <tt/user/name of the individual
260 to be authenticated, a pointer to an application-supplied
261 <tt/pam_conv/ structure and a pointer to a <tt/pam_handle_t/
262 <em/pointer/.
263
264 <p>
265 The <tt>pam_conv</tt> structure is discussed more fully in the section
266 <ref id="the-conversation-function" name="below">.  The
267 <tt>pam_handle_t</tt> is a <em>blind</em> structure and the
268 application should not attempt to probe it directly for information.
269 Instead the <bf>Linux-PAM</bf> library provides the functions
270 <tt>pam_set_item</tt> and <tt>pam_get_item</tt>.  These functions are
271 documented below.
272
273 <sect2>Termination of the library
274 <label id="pam-end-section">
275
276 <p>
277 <tscreen>
278 <verb>
279 extern int pam_end(pam_handle_t *pamh, int pam_status);
280 </verb>
281 </tscreen>
282
283 <p>
284 This function is the last function an application should call in the
285 <bf>Linux-PAM</bf> library.  Upon return the handle <tt/pamh/ is no
286 longer valid and all memory associated with it will be invalid (likely
287 to cause a segmentation fault if accessed).
288
289 <p>
290 Under normal conditions the argument <tt/pam_status/ has the value
291 PAM_SUCCESS, but in the event of an unsuccessful application for
292 service the appropriate <bf/Linux-PAM/ error-return value should be
293 used here. Note, <tt/pam_end()/ unconditionally shuts down the
294 authentication stack associated with the <tt/pamh/ handle. The value
295 taken by <tt/pam_status/ is used as an argument to the module specific
296 callback functions, <tt/cleanup()/ (see the <bf/Linux-PAM/ <htmlurl
297 url="pam_modules.html" name="Module Developers' Guide">). In this way,
298 the module can be given notification of the pass/fail nature of the
299 tear-down process, and perform any last minute tasks that are
300 appropriate to the module before it is unlinked.
301
302 <sect2>Setting PAM items
303 <label id="pam-set-item-section">
304
305 <p>
306 <tscreen>
307 <verb>
308 extern int pam_set_item(pam_handle_t *pamh, int item_type,
309                         const void *item);
310 </verb>
311 </tscreen>
312
313 <p>This function is used to (re)set the value of one of the following
314 <bf/item_type/s:
315
316 <p><descrip>
317 <tag><tt/PAM_SERVICE/</tag>
318
319         The service name (which identifies that PAM stack that
320         <tt/libpam/ will use to authenticate the program).
321
322 <tag><tt/PAM_USER/</tag>
323
324         The username of the entity under who's identity service will
325         be given. That is, following authentication, <tt/PAM_USER/
326         identifies the local entity that gets to use the
327         service. Note, this value can be mapped from something (eg.,
328         "<tt/anonymous/") to something else (eg. "<tt/guest119/") by
329         any module in the PAM stack. As such an application should
330         consult the value of <tt/PAM_USER/ after each call to a
331         <tt/pam_*()/ function.
332
333 <tag><tt/PAM_USER_PROMPT/</tag>
334
335         The string used when prompting for a user's name. The default
336         value for this string is ``Please enter username: ''.
337
338 <tag><tt/PAM_TTY/</tag>
339
340         The terminal name: prefixed by <tt>/dev/</tt> if it is a
341         device file; for graphical, X-based, applications the value
342         for this item should be the <tt/&dollar;DISPLAY/ variable.
343
344 <tag><tt/PAM_RUSER/</tag>
345
346         The requesting entity: user's username for a locally
347         requesting user or a remote requesting user - generally an
348         application or module will attempt to supply the value that is
349         most strongly authenticated (a local account before a remote
350         one. The level of trust in this value is embodied in the
351         actual authentication stack associated with the application,
352         so it is ultimately at the discretion of the system
353         administrator. It should generally match the current
354         <tt/PAM_RHOST/ value. That is, "<tt/PAM_RUSER@PAM_RHOST/"
355         should always identify the requesting user. In some cases,
356         <tt/PAM_RUSER/ may be NULL. In such situations, it is unclear
357         who the requesting entity is.
358
359 <tag><tt/PAM_RHOST/</tag>
360
361         The requesting hostname (the hostname of the machine from
362         which the <tt/PAM_RUSER/ entity is requesting service). That
363         is "<tt/PAM_RUSER@PAM_RHOST/" does identify the requesting
364         user.  "<tt/luser@localhost/" or "<tt/evil@evilcom.com/" are
365         valid "<tt/PAM_RUSER@PAM_RHOST/" examples. In some
366         applications, <tt/PAM_RHOST/ may be NULL. In such situations,
367         it is unclear where the authentication request is originating
368         from.
369
370 <tag><tt/PAM_CONV/</tag>
371
372         The conversation structure (see section <ref
373         id="the-conversation-function" name="below">).
374
375 <tag><tt/PAM_FAIL_DELAY/</tag> A function pointer to redirect
376         centrally managed failure delays (see section <ref
377         id="the-failure-delay-function" name="below">).
378
379 </descrip>
380
381 <p>
382 For all <tt/item_type/s, other than <tt/PAM_CONV/ and
383 <tt/PAM_FAIL_DELAY/, <tt/item/ is a pointer to a <tt>&lt;NUL&gt;</tt>
384 terminated character string.  In the case of <tt/PAM_CONV/, <tt/item/
385 points to an initialized <tt/pam_conv/ structure (see section <ref
386 id="the-conversation-function" name="below">). In the case of
387 <tt/PAM_FAIL_DELAY/, <tt/item/ is a function pointer: <tt/void
388 (*delay_fn)(int retval, unsigned usec_delay, void *appdata_ptr)/ (see
389 section <ref id="the-failure-delay-function" name="below">).
390
391 <p>
392 A successful call to this function returns <tt/PAM_SUCCESS/.  However,
393 the application should expect at least one the following errors:
394
395 <p>
396 <descrip>
397 <tag><tt/PAM_SYSTEM_ERR/</tag>
398         The <tt/pam_handle_t/ passed as a first argument to this
399         function was invalid.
400 <tag><tt/PAM_PERM_DENIED/</tag>
401         An attempt was made to replace the conversation structure with
402         a <tt/NULL/ value.
403 <tag><tt/PAM_BUF_ERR/</tag>
404         The function ran out of memory making a copy of the item.
405 <tag><tt/PAM_BAD_ITEM/</tag>
406         The application attempted to set an undefined or inaccessible
407         item.
408 </descrip>
409
410 <sect2>Getting PAM items
411 <label id="pam-get-item-section">
412
413 <p>
414 <tscreen>
415 <verb>
416 extern int pam_get_item(const pam_handle_t *pamh, int item_type,
417                         const void **item);
418 </verb>
419 </tscreen>
420
421 <p>
422 This function is used to obtain the value of the indicated
423 <tt/item_type/.  Upon successful return, <tt/*item/ contains a pointer
424 to the value of the corresponding item.  Note, this is a pointer to
425 the <em/actual/ data and should <em/not/ be <tt/free()/'ed or
426 over-written!
427
428 <p>
429 A successful call is signaled by a return value of <tt/PAM_SUCCESS/.
430 However, the application should expect one of the following errors:
431
432 <p>
433 <descrip>
434 <tag><tt/PAM_SYSTEM_ERR/</tag>
435         The <tt/pam_handle_t/ passed as a first argument to this
436         function was invalid.
437 <tag><tt/PAM_PERM_DENIED/</tag>
438         The value of <tt/item/ was <tt/NULL/.
439 <tag><tt/PAM_BAD_ITEM/</tag>
440         The application attempted to set an undefined or inaccessible
441         item.
442 </descrip>
443
444 <p>
445 In the case of an error, the contents of <tt/item/ is set to <tt/NULL/.
446
447 <sect2>Understanding errors
448 <label id="pam-strerror-section">
449
450 <p>
451 <tscreen>
452 <verb>
453 extern const char *pam_strerror(pam_handle_t *pamh, int errnum);
454 </verb>
455 </tscreen>
456
457 <p>
458 This function returns some text describing the <bf>Linux-PAM</bf>
459 error associated with the argument <tt/errnum/.  If the error is not
460 recognized ``<tt/Unknown Linux-PAM error/'' is returned.
461
462 <sect2>Planning for delays
463 <label id="the-failure-delay-function">
464
465 <p>
466 <tscreen>
467 <verb>
468 extern int pam_fail_delay(pam_handle_t *pamh, unsigned int micro_sec);
469 </verb>
470 </tscreen>
471
472 <p>
473 This function is offered by <bf/Linux-PAM/ to facilitate time delays
474 following a failed call to <tt/pam_authenticate()/ and before control
475 is returned to the application. When using this function the
476 application programmer should check if it is available with,
477 <tscreen>
478 <verb>
479 #ifdef PAM_FAIL_DELAY
480     ....
481 #endif /* PAM_FAIL_DELAY */
482 </verb>
483 </tscreen>
484
485
486 <p>
487 Generally, an application requests that a user is authenticated by
488 <bf/Linux-PAM/ through a call to <tt/pam_authenticate()/ or
489 <tt/pam_chauthtok()/.  These functions call each of the <em/stacked/
490 authentication modules listed in the relevant <bf/Linux-PAM/
491 configuration file.  As directed by this file, one of more of the
492 modules may fail causing the <tt/pam_...()/ call to return an error.
493 It is desirable for there to also be a pause before the application
494 continues. The principal reason for such a delay is security: a delay
495 acts to discourage <em/brute force/ dictionary attacks primarily, but
496 also helps hinder <em/timed/ (covert channel) attacks.
497
498 <p>
499 The <tt/pam_fail_delay()/ function provides the mechanism by which an
500 application or module can suggest a minimum delay (of <tt/micro_sec/
501 <em/micro-seconds/). <bf/Linux-PAM/ keeps a record of the longest time
502 requested with this function. Should <tt/pam_authenticate()/ fail,
503 the failing return to the application is delayed by an amount of time
504 randomly distributed (by up to 25%) about this longest value.
505
506 <p>
507 Independent of success, the delay time is reset to its zero default
508 value when <bf/Linux-PAM/ returns control to the application.
509
510 <p>
511 For applications written with a single thread that are event driven in
512 nature, <tt/libpam/ generating this delay may be undesirable. Instead,
513 the application may want to register the delay in some other way. For
514 example, in a single threaded server that serves multiple
515 authentication requests from a single event loop, the application
516 might want to simply mark a given connection as blocked until an
517 application timer expires. For this reason, <bf/Linux-PAM/ supplies
518 the <tt/PAM_FAIL_DELAY/ item. It can be queried and set with
519 <tt/pam_get_item()/ and <tt/pam_set_item()/ respectively. The value
520 used to set it should be a function pointer of the following
521 prototype:
522
523 <tscreen>
524 <verb>
525 void (*delay_fn)(int retval, unsigned usec_delay, void *appdata_ptr);
526 </verb>
527 </tscreen>
528
529 The arguments being the <tt/retval/ return code of the module stack,
530 the <tt/usec_delay/ micro-second delay that libpam is requesting and
531 the <tt/appdata_ptr/ that the application has associated with the
532 current <tt/pamh/ (<tt/pam_handle_t/). This last value was set by the
533 application when it called <tt/pam_start/ or explicitly with
534 <tt/pam_set_item(... , PAM_CONV, ...)/. Note, if <tt/PAM_FAIL_DELAY/
535 is unset (or set to <tt/NULL/), then <tt/libpam/ will perform any
536 delay.
537
538 <sect2>Authenticating the user
539
540 <p>
541 <tscreen>
542 <verb>
543 extern int pam_authenticate(pam_handle_t *pamh, int flags);
544 </verb>
545 </tscreen>
546
547 <p>
548 This function serves as an interface to the authentication mechanisms
549 of the loaded modules.  The single <em/optional/ flag, which may be
550 logically OR'd with <tt/PAM_SILENT/, takes the following value,
551
552 <p><descrip>
553
554 <tag><tt/PAM_DISALLOW_NULL_AUTHTOK/</tag>
555         Instruct the authentication modules to return
556 <tt/PAM_AUTH_ERR/ if the user does not have a registered
557 authorization token---it is set to <tt/NULL/ in the system database.
558 </descrip>
559
560 <p>
561 The value returned by this function is one of the following:
562
563 <p><descrip>
564
565 <tag><tt/PAM_AUTH_ERR/</tag>
566         The user was not authenticated
567 <tag><tt/PAM_CRED_INSUFFICIENT/</tag>
568         For some reason the application does not have sufficient
569 credentials to authenticate the user.
570 <tag><tt/PAM_AUTHINFO_UNAVAIL/</tag>
571         The modules were not able to access the authentication
572 information. This might be due to a network or hardware failure etc.
573 <tag><tt/PAM_USER_UNKNOWN/</tag>
574         The supplied username is not known to the authentication
575 service
576 <tag><tt/PAM_MAXTRIES/</tag>
577         One or more of the authentication modules has reached its
578 limit of tries authenticating the user. Do not try again.
579
580 </descrip>
581
582 <p>
583 If one or more of the authentication modules fails to load, for
584 whatever reason, this function will return <tt/PAM_ABORT/.
585
586 <sect2>Setting user credentials
587 <label id="pam-setcred-section">
588
589 <p>
590 <tscreen>
591 <verb>
592 extern int pam_setcred(pam_handle_t *pamh, int flags);
593 </verb>
594 </tscreen>
595
596 <p>
597 This function is used to set the module-specific credentials of the
598 user.  It is usually called after the user has been authenticated,
599 after the account management function has been called but before a
600 session has been opened for the user.
601
602 <p>
603 A credential is something that the user possesses.  It is some
604 property, such as a <em>Kerberos</em> ticket, or a supplementary group
605 membership that make up the uniqueness of a given user.  On a Linux
606 (or UN*X system) the user's <tt>UID</tt> and <tt>GID</tt>'s are
607 credentials too.  However, it has been decided that these properties
608 (along with the default supplementary groups of which the user is a
609 member) are credentials that should be set directly by the application
610 and not by PAM.
611
612 <p>
613 This function simply calls the <tt/pam_sm_setcred/ functions of each
614 of the loaded modules.  Valid <tt/flags/, any one of which, may be
615 logically OR'd with <tt/PAM_SILENT/, are:
616
617 <p><descrip>
618 <tag><tt/PAM_ESTABLISH_CRED/</tag>
619         Set the credentials for the authentication service,
620 <tag><tt/PAM_DELETE_CRED/</tag>
621         Delete the credentials associated with the authentication service,
622 <tag><tt/PAM_REINITIALIZE_CRED/</tag>
623         Reinitialize the user credentials, and
624 <tag><tt/PAM_REFRESH_CRED/</tag>
625         Extend the lifetime of the user credentials.
626 </descrip>
627
628 <p>
629 A successful return is signalled with <tt/PAM_SUCCESS/. Errors that
630 are especially relevant to this function are the following:
631
632 <p><descrip>
633 <tag><tt/PAM_CRED_UNAVAIL/</tag>
634         A module cannot retrieve the user's credentials.
635 <tag><tt/PAM_CRED_EXPIRED/</tag>
636         The user's credentials have expired.
637 <tag><tt/PAM_USER_UNKNOWN/</tag>
638         The user is not known to an authentication module.
639 <tag><tt/PAM_CRED_ERR/</tag>
640         A module was unable to set the credentials of the user.
641 </descrip>
642
643 <sect2>Account management
644
645 <p>
646 <tscreen>
647 <verb>
648 extern int pam_acct_mgmt(pam_handle_t *pamh, int flags);
649 </verb>
650 </tscreen>
651
652 <p>
653 This function is typically called after the user has been
654 authenticated.  It establishes whether the user's account is healthy.
655 That is to say, whether the user's account is still active and whether
656 the user is permitted to gain access to the system at this time.
657 Valid flags, any one of which, may be logically OR'd with
658 <tt/PAM_SILENT/, and are the same as those applicable to the
659 <tt/flags/ argument of <tt/pam_authenticate/.
660
661 <p>
662 This function simply calls the corresponding functions of each of the
663 loaded modules, as instructed by the configuration file,
664 <tt>/etc/pam.conf</tt>.
665
666 <p>
667 The normal response from this function is <tt/PAM_SUCCESS/, however,
668 specific failures are indicated by the following error returns:
669
670 <descrip>
671 <tag><tt/PAM_AUTHTOKEN_REQD/</tag>
672 The user <bf/is/ valid but their authentication token has
673 <em/expired/.  The correct response to this return-value is to require
674 that the user satisfies the <tt/pam_chauthtok()/ function before
675 obtaining service.  It may not be possible for some applications to do
676 this.  In such cases, the user should be denied access until such time
677 as they can update their password.
678
679 <tag><tt/PAM_ACCT_EXPIRED/</tag>
680         The user is no longer permitted to access the system.
681 <tag><tt/PAM_AUTH_ERR/</tag>
682         There was an authentication error.
683
684 <tag><tt/PAM_PERM_DENIED/</tag>
685         The user is not permitted to gain access at this time.
686 <tag><tt/PAM_USER_UNKNOWN/</tag>
687         The user is not known to a module's account management
688 component.
689
690 </descrip>
691
692 <sect2>Updating authentication tokens
693 <label id="pam-chauthtok-section">
694
695 <p>
696 <tscreen>
697 <verb>
698 extern int pam_chauthtok(pam_handle_t *pamh, const int flags);
699 </verb>
700 </tscreen>
701
702 <p>
703 This function is used to change the authentication token for a given
704 user (as indicated by the state associated with the handle,
705 <tt/pamh/). The following is a valid but optional flag which may be
706 logically OR'd with <tt/PAM_SILENT/,
707
708 <descrip>
709 <tag><tt/PAM_CHANGE_EXPIRED_AUTHTOK/</tag>
710         This argument indicates to the modules that the users
711 authentication token (password) should only be changed if it has
712 expired.
713 </descrip>
714
715 <p>
716 Note, if this argument is not passed, the application requires that
717 <em/all/ authentication tokens are to be changed.
718
719 <p>
720 <tt/PAM_SUCCESS/ is the only successful return value, valid
721 error-returns are:
722
723 <descrip>
724 <tag><tt/PAM_AUTHTOK_ERR/</tag>
725         A module was unable to obtain the new authentication token.
726         
727 <tag><tt/PAM_AUTHTOK_RECOVERY_ERR/</tag>
728         A module was unable to obtain the old authentication token.
729
730 <tag><tt/PAM_AUTHTOK_LOCK_BUSY/</tag>
731         One or more of the modules was unable to change the
732 authentication token since it is currently locked.
733         
734 <tag><tt/PAM_AUTHTOK_DISABLE_AGING/</tag>
735         Authentication token aging has been disabled for at least one
736 of the modules.
737
738 <tag><tt/PAM_PERM_DENIED/</tag>
739         Permission denied.
740
741 <tag><tt/PAM_TRY_AGAIN/</tag>
742         Not all of the modules were in a position to update the
743 authentication token(s). In such a case none of the user's
744 authentication tokens are updated.
745
746 <tag><tt/PAM_USER_UNKNOWN/</tag>
747         The user is not known to the authentication token changing
748 service.
749
750 </descrip>
751
752 <sect2>Session initialization
753 <label id="pam-open-session-section">
754
755 <p>
756 <tscreen>
757 <verb>
758 extern int pam_open_session(pam_handle_t *pamh, int flags);
759 </verb>
760 </tscreen>
761
762 <p>
763 This function is used to indicate that an authenticated session has
764 begun.  It is used to inform the modules that the user is currently in
765 a session.  It should be possible for the <bf>Linux-PAM</bf> library
766 to open a session and close the same session (see section <ref
767 id="pam-close-session-section" name="below">) from different
768 applications.
769
770 <p>
771 Currently, this function simply calls each of the corresponding
772 functions of the loaded modules. The only valid flag is
773 <tt/PAM_SILENT/ and this is, of course, <em/optional/.
774
775 <p>
776 If any of the <em/required/ loaded modules are unable to open a
777 session for the user, this function will return <tt/PAM_SESSION_ERR/.
778
779 <sect2>Terminating sessions
780 <label id="pam-close-session-section">
781
782 <p>
783 <tscreen>
784 <verb>
785 extern int pam_close_session(pam_handle_t *pamh, int flags);
786 </verb>
787 </tscreen>
788
789 <p>
790 This function is used to indicate that an authenticated session has
791 ended. It is used to inform the modules that the user is exiting a
792 session. It should be possible for the <bf>Linux-PAM</bf> library to
793 open a session and close the same session from different applications.
794
795 <p>
796 This function simply calls each of the corresponding functions of the
797 loaded modules in the same order that they were invoked with
798 <tt/pam_open_session()/.  The only valid flag is <tt/PAM_SILENT/ and
799 this is, of course, <em/optional/.
800
801 <p>
802 If any of the <em/required/ loaded modules are unable to close a
803 session for the user, this function will return <tt/PAM_SESSION_ERR/.
804
805 <sect2>Setting PAM environment variables
806 <label id="pam-putenv-section">
807
808 <p>
809 The <tt/libpam/ library associates with each PAM-handle (<tt/pamh/), a
810 set of <it/PAM environment variables/. These variables are intended to
811 hold the session environment variables that the user will inherit when
812 the session is granted and the authenticated user obtains access to
813 the requested service. For example, when <tt/login/ has finally given
814 the user a shell, the environment (as viewed with the command
815 <tt/env/) will be what <tt/libpam/ was maintaining as the PAM
816 environment for that service application. Note, these variables are not
817 the environment variables of the <tt/login/ application. This is
818 principally for two reasons: <tt/login/ may want to have an
819 environment that cannot be seen or manipulated by a user; and
820 <tt/login/ (or whatever the serving application is) may be maintaining
821 a number of parallel sessions, via different <tt/pamh/ values, at the
822 same time and a single environment may not be appropriately shared
823 between each of these. The PAM environment may contain variables
824 seeded by the applicant user's client program, for example, and as
825 such it is not appropriate for one applicant to interfere with the
826 environment of another applicant.
827
828 <p>
829 <tscreen>
830 <verb>
831 extern int pam_putenv(pam_handle_t *pamh, const char *name_value);
832 </verb>
833 </tscreen>
834
835 <p>
836 This function attempts to (re)set a <bf/Linux-PAM/ environment
837 variable. The <tt/name_value/ argument is a single <tt/NUL/ terminated
838 string of one of the following forms:
839 <descrip>
840 <tag>``<tt/NAME=value of variable/''</tag>
841
842 In this case the environment variable of the given <tt/NAME/ is set to
843 the indicated value: ``<tt/value of variable/''.  If this variable is
844 already known, it is overwritten. Otherwise it is added to the
845 <bf/Linux-PAM/ environment.
846
847 <tag>``<tt/NAME=/''</tag>
848
849 This function sets the variable to an empty value. It is listed
850 separately to indicate that this is the correct way to achieve such a
851 setting.
852
853 <tag>``<tt/NAME/''</tag>
854
855 Without an `<tt/=/' the <tt/pam_putenv()/ function will delete the
856 corresponding variable from the <bf/Linux-PAM/ environment.
857
858 </descrip>
859
860 <p>
861 Success is indicated with a return value of <tt/PAM_SUCCESS/. Failure
862 is indicated by one of the following returns:
863
864 <descrip>
865 <tag><tt/PAM_PERM_DENIED/</tag>
866         name given is a <tt/NULL/ pointer
867
868 <tag><tt/PAM_BAD_ITEM/</tag>
869         variable requested (for deletion) is not currently set
870
871 <tag><tt/PAM_ABORT/</tag>
872         the <bf/Linux-PAM/ handle, <tt/pamh/, is corrupt
873
874 <tag><tt/PAM_BUF_ERR/</tag>
875         failed to allocate memory when attempting update
876
877 </descrip>
878
879 <sect2>Getting a PAM environment variable
880 <label id="pam-getenv-section">
881
882 <p>
883 <tscreen>
884 <verb>
885 extern const char *pam_getenv(pam_handle_t *pamh, const char *name);
886 </verb>
887 </tscreen>
888
889 <p>
890 Obtain the value of the indicated <bf/Linux-PAM/ environment
891 variable. On error, internal failure or the unavailability of the
892 given variable (unspecified), this function simply returns <tt/NULL/.
893
894 <sect2>Getting the PAM environment
895 <label id="pam-getenvlist-section">
896
897 <p>
898 <tscreen>
899 <verb>
900 extern const char * const *pam_getenvlist(pam_handle_t *pamh);
901 </verb>
902 </tscreen>
903
904 <p>
905 The PAM environment variables (see section <ref
906 id="pam-putenv-section" name="above">) are a complete set of enviroment
907 variables that are associated with a PAM-handle (<tt/pamh/). They
908 represent the contents of the <it/regular/ environment variables of
909 the authenticated user when service is granted.
910
911 <p>
912 Th function, <tt>pam_getenvlist()</tt> returns a pointer to a complete,
913 <tt/malloc()/'d, copy of the PAM environment.  It is a pointer to a
914 duplicated list of environment variables.  It should be noted that
915 this memory will never be <tt/free()'d/ by <tt/libpam/.  Once obtained
916 by a call to <tt/pam_getenvlist()/, <bf>it is the responsibility of
917 the calling application</bf> to <tt/free()/ this memory.
918
919 <p>
920 The format of the memory is a <tt/malloc()/'d array of <tt/char */
921 pointers, the last element of which is set to <tt/NULL/. Each of the
922 non-<tt/NULL/ entries in this array point to a <tt/NUL/ terminated and
923 <tt/malloc()/'d <tt/char/ string of the form:
924 <tt/"/<it/name/<tt/=/<it/value/<tt/"/.
925
926 <p>
927 It is by design, and not a coincidence, that the format and contents
928 of the returned array matches that required for the third argument of
929 the <tt/execle(3)/ function call.
930
931 <sect1>What is expected of an application
932
933 <sect2>The conversation function
934 <label id="the-conversation-function">
935
936 <p>
937 An application must provide a ``conversation function''. It is used
938 for direct communication between a loaded module and the application
939 and will typically provide a means for the module to prompt the user
940 for a password etc. . The structure, <tt/pam_conv/, is defined by
941 including <tt>&lt;security/pam_appl.h&gt</tt>; to be,
942
943 <p>
944 <tscreen>
945 <verb>
946 struct pam_conv {
947     int (*conv)(int num_msg,
948         const struct pam_message **msg,
949         struct pam_response **resp,
950         void *appdata_ptr);
951     void *appdata_ptr;
952 };
953 </verb>
954 </tscreen>
955
956 <p>
957 It is initialized by the application before it is passed to the
958 library.  The <em/contents/ of this structure are attached to the
959 <tt/*pamh/ handle.  The point of this argument is to provide a
960 mechanism for any loaded module to interact directly with the
961 application program. This is why it is called a <em/conversation/
962 structure.
963
964 <p>
965 When a module calls the referenced <tt/conv()/ function, the argument
966 <tt/*appdata_ptr/ is set to the second element of this structure.
967
968 <p>
969 The other arguments of a call to <tt/conv()/ concern the information
970 exchanged by module and application. That is to say, <tt/num_msg/
971 holds the length of the array of pointers, <tt/msg/. After a
972 successful return, the pointer <tt/*resp/ points to an array of
973 <tt/pam_response/ structures, holding the application supplied text.
974 Note, <tt/*resp/ is an <tt/struct pam_response/ array and <em/not/ an
975 array of pointers.
976
977 <p>
978 The message (from the module to the application) passing structure is
979 defined by <tt>&lt;security/pam_appl.h&gt;</tt> as:
980
981 <p>
982 <tscreen>
983 <verb>
984 struct pam_message {
985     int msg_style;
986     const char *msg;
987 };
988 </verb>
989 </tscreen>
990
991 <p>
992 Valid choices for <tt/msg_style/ are:
993
994 <p><descrip>
995 <tag><tt/PAM_PROMPT_ECHO_OFF/</tag>
996         Obtain a string without echoing any text
997 <tag><tt/PAM_PROMPT_ECHO_ON/</tag>
998         Obtain a string whilst echoing text
999 <tag><tt/PAM_ERROR_MSG/</tag>
1000         Display an error
1001 <tag><tt/PAM_TEXT_INFO/</tag>
1002         Display some text.
1003 </descrip>
1004
1005 <p>
1006 The point of having an array of messages is that it becomes possible
1007 to pass a number of things to the application in a single call from
1008 the module. It can also be convenient for the application that related
1009 things come at once: a windows based application can then present a
1010 single form with many messages/prompts on at once.
1011
1012 <p>
1013 In passing, it is worth noting that there is a descrepency between the
1014 way Linux-PAM handles the <tt/const struct pam_message **msg/
1015 conversation function argument from the way that Solaris' PAM (and
1016 derivitives, known to include HP/UX, <em/are there others?/)
1017 does. Linux-PAM interprets the <tt/msg/ argument as entirely
1018 equivalent to the following prototype <tt/const struct pam_message
1019 *msg[]/ (which, in spirit, is consistent with the commonly used
1020 prototypes for <tt/argv/ argument to the familiar <tt/main()/
1021 function: <tt/char **argv/; and <tt/char *argv[]/). Said another way
1022 Linux-PAM interprets the <tt/msg/ argument as a pointer to an array of
1023 <tt/num_meg/ read only 'struct pam_message' <em/pointers/.  Solaris'
1024 PAM implementation interprets this argument as a pointer to a pointer
1025 to an array of <tt/num_meg/ <tt/pam_message/ structures.  Fortunately,
1026 perhaps, for most module/application developers when <tt/num_msg/ has
1027 a value of one these two definitions are entirely
1028 equivalent. Unfortunately, casually raising this number to two has led
1029 to unanticipated compatibility problems.
1030
1031 <p>
1032 For what its worth the two known module writer work-arounds for trying
1033 to maintain source level compatibility with both PAM implementations
1034 are:
1035 <itemize>
1036 <item> never call the conversation function with <tt/num_msg/ greater
1037 than one.
1038 <item> set up <tt/msg/ as doubly referenced so both types of
1039 conversation function can find the messages. That is, make
1040 <p><tscreen>
1041 <verb>
1042 msg[n] = & (( *msg )[n])
1043 </verb>
1044 </tscreen>
1045 </itemize>
1046 <p>
1047 The response (from the application to the module) passing structure is
1048 defined by including <tt>&lt;security/pam_appl.h&gt;</tt> as:
1049
1050 <p><tscreen><verb>
1051 struct pam_response {
1052     char *resp;
1053     int resp_retcode;
1054 };
1055 </verb></tscreen>
1056
1057 <p>
1058 Currently, there are no definitions for <tt/resp_retcode/ values; the
1059 normal value is <tt/0/.
1060
1061 <p>
1062 Prior to the 0.59 release of Linux-PAM, the length of the returned
1063 <tt/pam_response/ array was equal to the number of <em/prompts/ (types
1064 <tt/PAM_PROMPT_ECHO_OFF/ and <tt/PAM_PROMPT_ECHO_ON/) in the
1065 <tt/pam_message/ array with which the conversation function was
1066 called.  This meant that it was not always necessary for the module to
1067 <tt/free(3)/ the responses if the conversation function was only used
1068 to display some text.
1069
1070 <p>
1071 Post Linux-PAM-0.59.  The number of responses is always equal to the
1072 <tt/num_msg/ conversation function argument.  This is slightly easier
1073 to program but does require that the response array is <tt/free(3)/'d
1074 after every call to the conversation function.  The index of the
1075 responses corresponds directly to the prompt index in the
1076 <tt/pam_message/ array.
1077
1078 <p>
1079 The maximum length of the <tt/pam_msg.msg/ and <tt/pam_response.resp/
1080 character strings is <tt/PAM_MAX_MSG_SIZE/.  (This is not enforced by
1081 Linux-PAM.)
1082
1083 <p>
1084 <tt/PAM_SUCCESS/ is the expected return value of this
1085 function.  However, should an error occur the application should not
1086 set <tt/*resp/ but simply return <tt/PAM_CONV_ERR/.
1087
1088 <p>
1089 Note, if an application wishes to use two conversation functions, it
1090 should activate the second with a call to <tt/pam_set_item()/.
1091
1092 <p>
1093 <bf>Notes:</bf> New item types are being added to the conversation
1094 protocol.  Currently Linux-PAM supports: <tt>PAM_BINARY_PROMPT</tt>
1095 and <tt>PAM_BINARY_MSG</tt>.  These two are intended for server-client
1096 hidden information exchange and may be used as an interface for
1097 maching-machine authentication.
1098
1099 <sect1>Programming notes
1100
1101 <p>
1102 Note, all of the authentication service function calls accept the
1103 token <tt/PAM_SILENT/, which instructs the modules to not send
1104 messages to the application. This token can be logically OR'd with any
1105 one of the permitted tokens specific to the individual function calls.
1106 <tt/PAM_SILENT/ does not override the prompting of the user for
1107 passwords etc., it only stops informative messages from being
1108 generated.
1109
1110 <sect>Security issues of <bf>Linux-PAM</bf>
1111
1112 <p>
1113 PAM, from the perspective of an application, is a convenient API for
1114 authenticating users. PAM modules generally have no increased
1115 privilege over that possessed by the application that is making use of
1116 it. For this reason, the application must take ultimate responsibility
1117 for protecting the environment in which PAM operates.
1118
1119 <p>
1120 A poorly (or maliciously) written application can defeat any
1121 <bf/Linux-PAM/ module's authentication mechanisms by simply ignoring
1122 it's return values.  It is the applications task and responsibility to
1123 grant privileges and access to services.  The <bf/Linux-PAM/ library
1124 simply assumes the responsibility of <em/authenticating/ the user;
1125 ascertaining that the user <em/is/ who they say they are.  Care should
1126 be taken to anticipate all of the documented behavior of the
1127 <bf/Linux-PAM/ library functions.  A failure to do this will most
1128 certainly lead to a future security breach.
1129
1130 <sect1>Care about standard library calls
1131
1132 <p>
1133 In general, writers of authorization-granting applications should
1134 assume that each module is likely to call any or <em/all/ `libc'
1135 functions.  For `libc' functions that return pointers to
1136 static/dynamically allocated structures (ie. the library allocates the
1137 memory and the user is not expected to `<tt/free()/' it) any module
1138 call to this function is likely to corrupt a pointer previously
1139 obtained by the application.  The application programmer should either
1140 re-call such a `libc' function after a call to the <bf/Linux-PAM/
1141 library, or copy the structure contents to some safe area of memory
1142 before passing control to the <bf/Linux-PAM/ library.
1143
1144 <p>
1145 Two important function classes that fall into this category are
1146 <tt>getpwnam(3)</tt> and <tt>syslog(3)</tt>.
1147
1148 <sect1>Choice of a service name
1149
1150 <p>
1151 When picking the <em/service-name/ that corresponds to the first entry
1152 in the <bf/Linux-PAM/ configuration file, the application programmer
1153 should <bf/avoid/ the temptation of choosing something related to
1154 <tt/argv[0]/.  It is a trivial matter for any user to invoke any
1155 application on a system under a different name and this should not be
1156 permitted to cause a security breach.
1157
1158 <p>
1159 In general, this is always the right advice if the program is setuid,
1160 or otherwise more privileged than the user that invokes it. In some
1161 cases, avoiding this advice is convenient, but as an author of such an
1162 application, you should consider well the ways in which your program
1163 will be installed and used. (Its often the case that programs are not
1164 intended to be setuid, but end up being installed that way for
1165 convenience. If your program falls into this category, don't fall into
1166 the trap of making this mistake.)
1167
1168 <p>
1169 To invoke some <tt/target/ application by another name, the user may
1170 symbolically link the target application with the desired name.  To be
1171 precise all the user need do is,
1172 <tscreen>
1173 <verb>
1174 ln -s /target/application ./preferred_name
1175 </verb>
1176 </tscreen>
1177 and then <em/run/ <tt>./preferred_name</tt>
1178
1179 <p>
1180 By studying the <bf/Linux-PAM/ configuration file(s), an attacker can
1181 choose the <tt/preferred_name/ to be that of a service enjoying
1182 minimal protection; for example a game which uses <bf/Linux-PAM/ to
1183 restrict access to certain hours of the day.  If the service-name were
1184 to be linked to the filename under which the service was invoked, it
1185 is clear that the user is effectively in the position of dictating
1186 which authentication scheme the service uses.  Needless to say, this
1187 is not a secure situation.
1188
1189 <p>
1190 The conclusion is that the application developer should carefully
1191 define the service-name of an application. The safest thing is to make
1192 it a single hard-wired name.
1193
1194 <sect1>The conversation function
1195
1196 <p>
1197 Care should be taken to ensure that the <tt/conv()/ function is
1198 robust. Such a function is provided in the library <tt/libpam_misc/
1199 (see <ref id="libpam-misc-section" name="below">).
1200
1201 <sect1>The identity of the user
1202
1203 <p>
1204 The <bf/Linux-PAM/ modules will need to determine the identity of the
1205 user who requests a service, and the identity of the user who grants
1206 the service.  These two users will seldom be the same.  Indeed there
1207 is generally a third user identity to be considered, the new (assumed)
1208 identity of the user once the service is granted.
1209
1210 <p>
1211 The need for keeping tabs on these identities is clearly an issue of
1212 security.  One convention that is actively used by some modules is
1213 that the identity of the user requesting a service should be the
1214 current <tt/uid/ (userid) of the running process; the identity of the
1215 privilege granting user is the <tt/euid/ (effective userid) of the
1216 running process; the identity of the user, under whose name the
1217 service will be executed, is given by the contents of the
1218 <tt/PAM_USER/ <tt/pam_get_item(3)/. Note, modules can change the
1219 values of <tt/PAM_USER/ and <tt/PAM_RUSER/ during any of the
1220 <tt/pam_*()/ library calls. For this reason, the application should
1221 take care to use the <tt/pam_get_item()/ every time it wishes to
1222 establish who the authenticated user is (or will currently be).
1223
1224 <p>
1225 For network-serving databases and other applications that provide
1226 their own security model (independent of the OS kernel) the above
1227 scheme is insufficient to identify the requesting user.
1228
1229 <p>
1230 A more portable solution to storing the identity of the requesting
1231 user is to use the <tt/PAM_RUSER/ <tt/pam_get_item(3)/. The
1232 application should supply this value before attempting to authenticate
1233 the user with <tt/pam_authenticate()/. How well this name can be
1234 trusted will ultimately be at the discretion of the local
1235 administrator (who configures PAM for your application) and a selected
1236 module may attempt to override the value where it can obtain more
1237 reliable data. If an application is unable to determine the identity
1238 of the requesting entity/user, it should not call <tt/pam_set_item(3)/
1239 to set <tt/PAM_RUSER/.
1240
1241 <p>
1242 In addition to the <tt/PAM_RUSER/ item, the application should supply
1243 the <tt/PAM_RHOST/ (<em/requesting host/) item. As a general rule, the
1244 following convention for its value can be assumed: <tt/&lt;unset&gt;/
1245 = unknown; <tt/localhost/ = invoked directly from the local system;
1246 <em/other.place.xyz/ = some component of the user's connection
1247 originates from this remote/requesting host. At present, PAM has no
1248 established convention for indicating whether the application supports
1249 a trusted path to communication from this host.
1250
1251 <sect1>Sufficient resources
1252
1253 <p>
1254 Care should be taken to ensure that the proper execution of an
1255 application is not compromised by a lack of system resources.  If an
1256 application is unable to open sufficient files to perform its service,
1257 it should fail gracefully, or request additional resources.
1258 Specifically, the quantities manipulated by the <tt/setrlimit(2)/
1259 family of commands should be taken into consideration.
1260
1261 <p>
1262 This is also true of conversation prompts. The application should not
1263 accept prompts of arbitrary length with out checking for resource
1264 allocation failure and dealing with such extreme conditions gracefully
1265 and in a mannor that preserves the PAM API. Such tolerance may be
1266 especially important when attempting to track a malicious adversary.
1267
1268 <sect>A library of miscellaneous helper functions
1269 <label id="libpam-misc-section">
1270
1271 <p>
1272 To aid the work of the application developer a library of
1273 miscellaneous functions is provided.  It is called <tt/libpam_misc/,
1274 and contains functions for allocating memory (securely), a text based
1275 conversation function, and routines for enhancing the standard
1276 PAM-environment variable support.
1277
1278 <sect1>Requirements
1279
1280 <p>
1281 The functions, structures and macros, made available by this library
1282 can be defined by including <tt>&lt;security/pam_misc.h&gt;</tt>.  It
1283 should be noted that this library is specific to <bf/Linux-PAM/ and is
1284 not referred to in the defining DCE-RFC (see <ref id="bibliography"
1285 name="the bibliography">) below.
1286
1287 <sect1>Macros supplied
1288
1289 <sect2>Safe duplication of strings
1290
1291 <p>
1292 <tscreen>
1293 <verb>
1294 x_strdup(const char *s)
1295 </verb>
1296 </tscreen>
1297
1298 <p>
1299 This macro is a replacement for the <tt/xstrdup()/ function that was
1300 present in earlier versions of the library and which clashed horribly
1301 with a number of applications. It returns a duplicate copy of the
1302 <tt/NUL/ terminated string, <tt/s/. <tt/NULL/ is returned if there is
1303 insufficient memory available for the duplicate or if <tt/s/ is
1304 <tt/NULL/ to begin with.
1305
1306 <sect1>Functions supplied
1307
1308 <sect2>A text based conversation function
1309
1310 <p>
1311 <tscreen>
1312 <verb>
1313 extern int misc_conv(int num_msg, const struct pam_message **msgm,
1314                      struct pam_response **response, void *appdata_ptr);
1315 </verb>
1316 </tscreen>
1317
1318 <p>
1319 This is a function that will prompt the user with the appropriate
1320 comments and obtain the appropriate inputs as directed by
1321 authentication modules.
1322
1323 <p>
1324 In addition to simply slotting into the appropriate <tt/struct
1325 pam_conv/, this function provides some time-out facilities.  The
1326 function exports five variables that can be used by an application
1327 programmer to limit the amount of time this conversation function will
1328 spend waiting for the user to type something.
1329
1330 <p>
1331 The five variables are as follows:
1332 <descrip>
1333 <tag><tt>extern time_t pam_misc_conv_warn_time;</tt></tag>
1334
1335 This variable contains the <em/time/ (as returned by <tt/time()/) that
1336 the user should be first warned that the clock is ticking. By default
1337 it has the value <tt/0/, which indicates that no such warning will be
1338 given. The application may set its value to sometime in the future,
1339 but this should be done prior to passing control to the <bf/Linux-PAM/
1340 library.
1341
1342 <tag><tt>extern const char *pam_misc_conv_warn_line;</tt></tag>
1343
1344 Used in conjuction with <tt/pam_misc_conv_warn_time/, this variable is
1345 a pointer to the string that will be displayed when it becomes time to
1346 warn the user that the timeout is approaching. Its default value is
1347 ``..&bsol;a.Time is running out...&bsol;n'', but this can be changed
1348 by the application prior to passing control to <bf/Linux-PAM/.
1349
1350 <tag><tt>extern time_t pam_misc_conv_die_time;</tt></tag>
1351
1352 This variable contains the <em/time/ (as returned by <tt/time()/) that
1353 the conversation will time out. By default it has the value <tt/0/,
1354 which indicates that the conversation function will not timeout. The
1355 application may set its value to sometime in the future, this should
1356 be done prior to passing control to the <bf/Linux-PAM/ library.
1357
1358 <tag><tt>extern const char *pam_misc_conv_die_line;</tt></tag>
1359
1360 Used in conjuction with <tt/pam_misc_conv_die_time/, this variable is
1361 a pointer to the string that will be displayed when the conversation
1362 times out. Its default value is ``..&bsol;a.Sorry, your time is
1363 up!&bsol;n'', but this can be changed by the application prior to
1364 passing control to <bf/Linux-PAM/.
1365
1366 <tag><tt>extern int pam_misc_conv_died;</tt></tag>
1367
1368 Following a return from the <bf/Linux-PAM/ libraray, the value of this
1369 variable indicates whether the conversation has timed out. A value of
1370 <tt/1/ indicates the time-out occurred.
1371
1372 </descrip>
1373
1374 <p>
1375 The following two function pointers are available for supporting binary
1376 prompts in the conversation function. They are optimized for the
1377 current incarnation of the <tt/libpamc/ library and are subject to
1378 change.
1379 <descrip>
1380 <tag><tt>extern int (*pam_binary_handler_fn)(void *appdata, pamc_bp_t
1381 *prompt_p);</tt></tag>
1382
1383 This function pointer is initialized to <tt/NULL/ but can be filled
1384 with a function that provides machine-machine (hidden) message
1385 exchange.  It is intended for use with hidden authentication protocols
1386 such as RSA or Diffie-Hellman key exchanges.  (This is still under
1387 development.)
1388
1389 <tag><tt>extern int (*pam_binary_handler_free)(void *appdata,
1390 pamc_bp_t *delete_me);</tt></tag>
1391
1392 This function pointer is initialized to <tt/PAM_BP_RENEW(delete_me, 0,
1393 0)/, but can be redefined as desired by the application.
1394
1395 </descrip>
1396
1397 <sect2>Transcribing an environment to that of Linux-PAM
1398 <p>
1399 <tscreen>
1400 <verb>
1401 extern int pam_misc_paste_env(pam_handle_t *pamh,
1402                               const char * const * user_env);
1403 </verb>
1404 </tscreen>
1405
1406 This function takes the supplied list of environment pointers and
1407 <em/uploads/ its contents to the <bf/Linux-PAM/ environment. Success
1408 is indicated by <tt/PAM_SUCCESS/.
1409
1410 <sect2>Liberating a locally saved environment
1411 <p>
1412 <tscreen>
1413 <verb>
1414 extern char **pam_misc_drop_env(char **env);
1415 </verb>
1416 </tscreen>
1417
1418 This function is defined to complement the <tt/pam_getenvlist()/
1419 function.  It liberates the memory associated with <tt/env/,
1420 <em/overwriting/ with <tt/0/ all memory before <tt/free()/ing it.
1421
1422 <sect2>BSD like Linux-PAM environment variable setting
1423 <p>
1424 <tscreen>
1425 <verb>
1426 extern int pam_misc_setenv(pam_handle_t *pamh, const char *name,
1427                            const char *value, int readonly);
1428 </verb>
1429 </tscreen>
1430
1431 This function performs a task equivalent to <tt/pam_putenv()/, its
1432 syntax is, however, more like the BSD style function; <tt/setenv()/.
1433 The <tt/name/ and <tt/value/ are concatenated with an ``<tt/=/'' to
1434 form a <tt/name_value/ and passed to <tt/pam_putenv()/. If, however,
1435 the <bf/Linux-PAM/ variable is already set, the replacement will only
1436 be applied if the last argument, <tt/readonly/, is zero.
1437
1438 <sect>Porting legacy applications
1439
1440 <p>
1441 The following is extracted from an email.  I'll tidy it up later.
1442
1443 <p>
1444 The point of PAM is that the application is not supposed to have any
1445 idea how the attached authentication modules will choose to
1446 authenticate the user.  So all they can do is provide a conversation
1447 function that will talk directly to the user(client) on the modules'
1448 behalf.
1449
1450 <p>
1451 Consider the case that you plug a retinal scanner into the login
1452 program.  In this situation the user would be prompted: "please look
1453 into the scanner".  No username or password would be needed - all this
1454 information could be deduced from the scan and a database lookup.  The
1455 point is that the retinal scanner is an ideal task for a "module".
1456
1457 <p>
1458 While it is true that a pop-daemon program is designed with the POP
1459 protocol in mind and no-one ever considered attaching a retinal
1460 scanner to it, it is also the case that the "clean" PAM'ification of
1461 such a daemon would allow for the possibility of a scanner module
1462 being be attached to it.  The point being that the "standard"
1463 pop-authentication protocol(s) [which will be needed to satisfy
1464 inflexible/legacy clients] would be supported by inserting an
1465 appropriate pam_qpopper module(s).  However, having rewritten popd
1466 once in this way any new protocols can be implemented in-situ.
1467
1468 <p>
1469 One simple test of a ported application would be to insert the
1470 <tt/pam_permit/ module and see if the application demands you type a
1471 password...  In such a case, <tt/xlock/ would fail to lock the
1472 terminal - or would at best be a screen-saver, ftp would give password
1473 free access to all etc..  Neither of these is a very secure thing to
1474 do, but they do illustrate how much flexibility PAM puts in the hands
1475 of the local admin.
1476
1477 <p>
1478 The key issue, in doing things correctly, is identifying what is part
1479 of the authentication procedure (how many passwords etc..) the
1480 exchange protocol (prefixes to prompts etc., numbers like 331 in the
1481 case of ftpd) and what is part of the service that the application
1482 delivers.  PAM really needs to have total control in the
1483 authentication "procedure", the conversation function should only
1484 deal with reformatting user prompts and extracting responses from raw
1485 input.
1486
1487 <sect>Glossary of PAM related terms
1488
1489 <p>
1490 The following are a list of terms used within this document.
1491
1492 <p>
1493 <descrip>
1494
1495 <tag>Authentication token</tag>
1496 Generally, this is a password.  However, a user can authenticate
1497 him/herself in a variety of ways.  Updating the user's authentication
1498 token thus corresponds to <em>refreshing</em> the object they use to
1499 authenticate themself with the system.  The word password is avoided
1500 to keep open the possibility that the authentication involves a
1501 retinal scan or other non-textual mode of challenge/response.
1502
1503 <tag>Credentials</tag>
1504 Having successfully authenticated the user, PAM is able to establish
1505 certain characteristics/attributes of the user.  These are termed
1506 <em>credentials</em>.  Examples of which are group memberships to
1507 perform privileged tasks with, and <em>tickets</em> in the form of
1508 environment variables etc. .  Some user-credentials, such as the
1509 user's UID and GID (plus default group memberships) are not deemed to
1510 be PAM-credentials.  It is the responsibility of the application to
1511 grant these directly.
1512
1513 </descrip>
1514
1515 <sect>An example application
1516
1517 <p>
1518 To get a flavor of the way a <tt/Linux-PAM/ application is written we
1519 include the following example. It prompts the user for their password
1520 and indicates whether their account is valid on the standard output,
1521 its return code also indicates the success (<tt/0/ for success; <tt/1/
1522 for failure).
1523
1524 <p>
1525 <tscreen>
1526 <verb>
1527 /*
1528   This program was contributed by Shane Watts
1529   [modifications by AGM]
1530
1531   You need to add the following (or equivalent) to the /etc/pam.conf file.
1532   # check authorization
1533   check_user   auth       required     /usr/lib/security/pam_unix_auth.so
1534   check_user   account    required     /usr/lib/security/pam_unix_acct.so
1535  */
1536
1537 #include <security/pam_appl.h>
1538 #include <security/pam_misc.h>
1539 #include <stdio.h>
1540
1541 static struct pam_conv conv = {
1542     misc_conv,
1543     NULL
1544 };
1545
1546 int main(int argc, char *argv[])
1547 {
1548     pam_handle_t *pamh=NULL;
1549     int retval;
1550     const char *user="nobody";
1551
1552     if(argc == 2) {
1553         user = argv[1];
1554     }
1555
1556     if(argc > 2) {
1557         fprintf(stderr, "Usage: check_user [username]\n");
1558         exit(1);
1559     }
1560
1561     retval = pam_start("check_user", user, &ero;conv, &ero;pamh);
1562         
1563     if (retval == PAM_SUCCESS)
1564         retval = pam_authenticate(pamh, 0);    /* is user really user? */
1565
1566     if (retval == PAM_SUCCESS)
1567         retval = pam_acct_mgmt(pamh, 0);       /* permitted access? */
1568
1569     /* This is where we have been authorized or not. */
1570
1571     if (retval == PAM_SUCCESS) {
1572         fprintf(stdout, "Authenticated\n");
1573     } else {
1574         fprintf(stdout, "Not Authenticated\n");
1575     }
1576
1577     if (pam_end(pamh,retval) != PAM_SUCCESS) {     /* close Linux-PAM */
1578         pamh = NULL;
1579         fprintf(stderr, "check_user: failed to release authenticator\n");
1580         exit(1);
1581     }
1582
1583     return ( retval == PAM_SUCCESS ? 0:1 );       /* indicate success */
1584 }
1585 </verb>
1586 </tscreen>
1587
1588 <sect>Files
1589
1590 <p><descrip>
1591
1592 <tag><tt>/usr/include/security/pam_appl.h</tt></tag>
1593
1594 header file for <bf/Linux-PAM/ applications interface
1595
1596 <tag><tt>/usr/include/security/pam_misc.h</tt></tag>
1597
1598 header file for useful library functions for making applications
1599 easier to write
1600
1601 <tag><tt>/usr/lib/libpam.so.*</tt></tag>
1602
1603 the shared library providing applications with access to
1604 <bf/Linux-PAM/.
1605
1606 <tag><tt>/etc/pam.conf</tt></tag>
1607
1608 the <bf/Linux-PAM/ configuration file.
1609
1610 <tag><tt>/usr/lib/security/pam_*.so</tt></tag>
1611
1612 the primary location for <bf/Linux-PAM/ dynamically loadable object
1613 files; the modules.
1614
1615 </descrip>
1616
1617 <sect>See also
1618 <label id="bibliography">
1619
1620 <p><itemize>
1621
1622 <item>The <bf/Linux-PAM/
1623 <htmlurl url="pam.html" name="System Administrators' Guide">.
1624
1625 <item>The <bf/Linux-PAM/
1626 <htmlurl url="pam_modules.html" name="Module Writers' Guide">.
1627
1628 <item>The V. Samar and R. Schemers (SunSoft), ``UNIFIED LOGIN WITH
1629 PLUGGABLE AUTHENTICATION MODULES'', Open Software Foundation Request
1630 For Comments 86.0, October 1995.
1631
1632 </itemize>
1633
1634 <sect>Notes
1635
1636 <p>
1637 I intend to put development comments here... like ``at the moment
1638 this isn't actually supported''. At release time what ever is in
1639 this section will be placed in the Bugs section below! :)
1640
1641 <p>
1642 <itemize>
1643
1644 <item> <tt/pam_strerror()/ should be internationalized....
1645
1646 <item>
1647 Note, the <tt/resp_retcode/ of struct <tt/pam_message/, has no
1648 purpose at the moment. Ideas/suggestions welcome!
1649
1650 <item> more security issues are required....
1651
1652 </itemize>
1653
1654 <sect>Author/acknowledgments
1655
1656 <p>
1657 This document was written by Andrew G. Morgan
1658 (morgan@kernel.org) with many contributions from
1659 <!-- insert credits here -->
1660 <!--
1661  an sgml list of people to credit for their contributions to Linux-PAM
1662  $Id$
1663   -->
1664 Chris Adams,
1665 Peter Allgeyer,
1666 Tim Baverstock,
1667 Tim Berger,
1668 Craig S. Bell,
1669 Derrick J. Brashear,
1670 Ben Buxton,
1671 Seth Chaiklin,
1672 Oliver Crow,
1673 Chris Dent,
1674 Marc Ewing,
1675 Cristian Gafton,
1676 Emmanuel Galanos,
1677 Brad M. Garcia,
1678 Eric Hester,
1679 Roger Hu,
1680 Eric Jacksch,
1681 Michael K. Johnson,
1682 David Kinchlea,
1683 Olaf Kirch,
1684 Marcin Korzonek,
1685 Stephen Langasek,
1686 Nicolai Langfeldt,
1687 Elliot Lee,
1688 Luke Kenneth Casson Leighton,
1689 Al Longyear,
1690 Ingo Luetkebohle,
1691 Marek Michalkiewicz,
1692 Robert Milkowski,
1693 Aleph One,
1694 Martin Pool,
1695 Sean Reifschneider,
1696 Jan Rekorajski,
1697 Erik Troan,
1698 Theodore Ts'o,
1699 Jeff Uphoff,
1700 Myles Uyema,
1701 Savochkin Andrey Vladimirovich,
1702 Ronald Wahl,
1703 David Wood,
1704 John Wilmes,
1705 Joseph S. D. Yao
1706 and
1707 Alex O.  Yuriev.
1708
1709 <p>
1710 Thanks are also due to Sun Microsystems, especially to Vipin Samar and
1711 Charlie Lai for their advice. At an early stage in the development of
1712 <bf/Linux-PAM/, Sun graciously made the documentation for their
1713 implementation of PAM available. This act greatly accelerated the
1714 development of <bf/Linux-PAM/.
1715
1716 <sect>Bugs/omissions
1717
1718 <p>
1719 This manual is hopelessly unfinished. Only a partial list of people is
1720 credited for all the good work they have done.
1721
1722 <sect>Copyright information for this document
1723
1724 <p>
1725 Copyright (c) Andrew G. Morgan 1996-9,2000-1.  All rights reserved.
1726 <newline>
1727 Email: <tt>&lt;morgan@kernel.org&gt;</tt>
1728
1729 <p>
1730 Redistribution and use in source and binary forms, with or without
1731 modification, are permitted provided that the following conditions are
1732 met:
1733
1734 <p>
1735 <itemize>
1736
1737 <item>
1738 1. Redistributions of source code must retain the above copyright
1739    notice, and the entire permission notice in its entirety,
1740    including the disclaimer of warranties.
1741
1742 <item>
1743 2. Redistributions in binary form must reproduce the above copyright
1744    notice, this list of conditions and the following disclaimer in the
1745    documentation and/or other materials provided with the distribution.
1746
1747 <item>
1748 3. The name of the author may not be used to endorse or promote
1749    products derived from this software without specific prior
1750    written permission.
1751
1752 </itemize>
1753
1754 <p>
1755 <bf/Alternatively/, this product may be distributed under the terms of
1756 the GNU General Public License (GPL), in which case the provisions of
1757 the GNU GPL are required <bf/instead of/ the above restrictions.
1758 (This clause is necessary due to a potential bad interaction between
1759 the GNU GPL and the restrictions contained in a BSD-style copyright.)
1760
1761 <p>
1762 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
1763 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1764 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1765 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1766 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
1767 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
1768 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
1769 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
1770 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
1771 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1772 DAMAGE.
1773
1774 <p>
1775 <tt>$Id$</tt>
1776
1777 </article>