xref: /openbsd-src/lib/libc/gen/auth_subr.3 (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1.\"	$OpenBSD: auth_subr.3,v 1.18 2008/04/04 17:42:39 millert Exp $
2.\"
3.\" Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\" 3. All advertising materials mentioning features or use of this software
14.\"    must display the following acknowledgement:
15.\"	This product includes software developed by Berkeley Software Design,
16.\"	Inc.
17.\" 4. The name of Berkeley Software Design, Inc.  may not be used to endorse
18.\"    or promote products derived from this software without specific prior
19.\"    written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.\"	BSDI $From: auth_subr.3,v 2.5 2000/03/30 19:11:27 polk Exp $
34.Dd $Mdocdate: April 4 2008 $
35.Dt BSD_AUTH 3
36.Os
37.Sh NAME
38.Nm auth_open ,
39.Nm auth_call ,
40.Nm auth_challenge ,
41.Nm auth_check_change ,
42.Nm auth_check_expire ,
43.Nm auth_clean ,
44.Nm auth_close ,
45.Nm auth_clrenv ,
46.Nm auth_clroption ,
47.Nm auth_clroptions ,
48.Nm auth_getitem ,
49.Nm auth_getpwd ,
50.Nm auth_getstate ,
51.Nm auth_getvalue ,
52.Nm auth_set_va_list ,
53.Nm auth_setdata ,
54.Nm auth_setenv ,
55.Nm auth_setitem ,
56.Nm auth_setoption ,
57.Nm auth_setpwd ,
58.Nm auth_setstate
59.Nd interface to the BSD Authentication system
60.Sh SYNOPSIS
61.Fd #include <login_cap.h>
62.Fd #include <bsd_auth.h>
63.Ft auth_session_t *
64.Fn auth_open "void"
65.Ft int
66.Fn auth_close "auth_session_t *as"
67.Ft int
68.Fn auth_call "auth_session_t *as" "char *path" "..."
69.Ft char *
70.Fn auth_challenge "auth_session_t *as"
71.Ft quad_t
72.Fn auth_check_change "auth_session_t *as"
73.Ft quad_t
74.Fn auth_check_expire "auth_session_t *as"
75.Ft void
76.Fn auth_clean "auth_session_t *as"
77.Ft void
78.Fn auth_clrenv "auth_session_t *as"
79.Ft void
80.Fn auth_clroption "auth_session_t * as" "char *name"
81.Ft void
82.Fn auth_clroptions "auth_session_t *as"
83.Ft char *
84.Fn auth_getitem "auth_session_t *as" "auth_item_t item"
85.Ft struct passwd *
86.Fn auth_getpwd "auth_session_t *as"
87.Ft int
88.Fn auth_getstate "auth_session_t *as"
89.Ft char *
90.Fn auth_getvalue "auth_session_t *as" "char *what"
91.Ft void
92.Fn auth_set_va_list "auth_session_t *as" "va_list ap"
93.Ft int
94.Fn auth_setdata "auth_session_t *as" "void *ptr" "size_t len"
95.Ft void
96.Fn auth_setenv "auth_session_t *as"
97.Ft int
98.Fn auth_setitem "auth_session_t *as" "auth_item_t item" "char *value"
99.Ft int
100.Fn auth_setoption "auth_session_t *as" "char *name" "char *value"
101.Ft int
102.Fn auth_setpwd "auth_session_t *as" "struct passwd *pwd"
103.Ft void
104.Fn auth_setstate "auth_session_t *as" "int state"
105.Sh DESCRIPTION
106These functions provide the lower level interface to the BSD
107Authentication system.
108They all operate on a BSD Authentication session pointer,
109.Fa as ,
110which is returned by
111.Fn auth_open .
112The session pointer
113must be passed to all other BSD Authentication functions called.
114The
115.Fn auth_open
116function returns
117.Dv NULL
118if it was unable to allocate memory for the session.
119The session is terminated by the
120.Fn auth_close
121function,
122which also sets any environment variables requested by the login script
123(assuming the user was not rejected) or removes files created by the
124login script if the authentication was not successful.
125It returns the final state of the authentication request.
126A return value of 0 implies the user was not authenticated.
127A non-zero return value is made up of 1 or more of the following values
128ORed together:
129.Bl -tag -width AUTH_ROOTOKAYXX
130.It Dv AUTH_OKAY
131The user was authenticated.
132.It Dv AUTH_ROOTOKAY
133The user was authenticated with a root instance.
134.It Dv AUTH_SECURE
135The user was authenticated via a mechanism which is not subject to
136eavesdropping attacks (such as provided by token cards).
137.El
138.Pp
139The full state of the session is returned by the
140.Fn auth_getstate
141function.
142In addition to the values above, it also may contain the bits:
143.Bl -tag -width AUTH_ROOTOKAYXX
144.It Dv AUTH_SILENT
145Do not report an error, the user was not authenticated for access and
146was not expected to be.
147This is returned by login scripts that allow changing of the user's password,
148for instance.
149This value is stripped off for normal returns.
150.It Dv AUTH_CHALLENGE
151The user was not authenticated for access and a challenge was issued.
152The challenge should be displayed to the user, a response retrieved,
153and the result verified.
154This value is stripped off for normal returns.
155.It Dv AUTH_EXPIRED
156The user's account has expired.
157.It Dv AUTH_PWEXPIRED
158The user's password has expired and needs to be changed.
159.El
160.Pp
161A session may be cleaned
162by calling
163.Fn auth_clean .
164This function removes any files created by a login script in this
165session and clears all state associated with this session, with the
166exception of the option settings.
167It is not necessary to call
168.Fn auth_clean
169if
170.Fn auth_close
171is called.
172.Pp
173The remaining functions are described in alphabetical order.
174.Pp
175The fundamental function for doing BSD Authentication is
176.Fn auth_call .
177In addition to the pointer to the BSD Authentication session, it takes
178the following parameters:
179.Bl -tag -width indent
180.It Ar path
181The full path name of the login script to run.
182The call will fail if
183.Ar path
184does not pass the requirements of the
185.Xr secure_path 3
186function.
187.It Ar ...
188The remaining arguments, which should be of type
189.Vt char *
190and terminated with a
191.Dv NULL ,
192are passed to the login script at the end of the command line.
193.El
194.Pp
195The
196.Fn auth_call
197function, after verifying the
198.Ar path ,
199creates a bi-directional pipe (socketpair) which is located on
200file descriptor 3 for the child (the login script).
201This is known as the
202.Dq back channel .
203The actual command line passed to the child is made up of
2043 parts.
205The parameters passed to
206.Fn auth_call
207following
208.Ar path
209have appended to them any arguments specified by the
210.Fn auth_set_va_list
211function.
212These are typically the variable arguments passed to the function
213that calls
214.Fn auth_call .
215Any option values set by the
216.Fn auth_setoption
217function are inserted between the first argument (the command
218name) and the second argument with a preceding
219.Fl v
220flag.
221The name and value are separated by an
222.Sq = :
223.Pp
224.D1 Fl v Ar name=value
225.Pp
226Once the login script has been spawned, any data specified by the
227.Fn auth_setdata
228is written to the back channel.
229Multiple blocks of data may have been specified and they will be sent
230in the same order they were specified.
231As the data is sent, the storage for the data is zeroed out and then freed
232(the data is zeroed out since it may contain sensitive information,
233such as a password).
234Once any data is written out,
235.Fn auth_call
236reads up to 8192 bytes of data from the back channel.
237The state of the session is determined from this data (see
238.Xr login.conf 5
239for details).
240If the login script exits with a 0 and does not specify any return state
241on the back channel, the state prior to the call to
242.Fn auth_call
243is retained.
244.Pp
245Note that while
246.Fn auth_call
247will zero out the copies it makes of sensitive information, such as plain text
248passwords, after it is sent, it is the responsibility of the
249caller to zero out the original copies of this sensitive information.
250Due to the mechanics of the
251.Fn auth_call
252function, this data must be zeroed
253.Em before
254.Fn auth_call
255is called.
256The safest place to zero out sensitive information is immediately
257after it has been passed to
258.Fn auth_setdata .
259.Pp
260The back channel data may also contain a file descriptor passed back
261from the login script.
262If this is the case, the login script will first send back the string
263.Dq fd
264to indicate that a file descriptor will be the next data item.
265The file descriptor will be passed back to the next invocation of
266the login script with a number specified by the
267.Fl v Ar fd
268option.
269This is used to implement stateful challenge/response schemes that require
270a persistent connection during the challenge and response.
271The copy of the descriptor in the parent process is closed when the
272child is running to prevent deadlock when file locking is used.
273The descriptor is also closed by a call to
274.Fn auth_close
275or
276.Fn auth_clean .
277.Pp
278The data read from the back channel is also used by the
279.Fn auth_getvalue
280and
281.Fn auth_close
282functions.
283Subsequent calls to
284.Fn auth_call
285will cause this data to be lost and overwritten with the new data read
286from the new call.
287.Pp
288The environment passed to the login script by
289.Fn auth_call
290only contains two values:
291.Ev PATH
292and
293.Ev SHELL .
294The
295.Ev PATH
296is set to the default path
297.Pa ( /bin
298and
299.Pa /usr/bin )
300while the
301.Ev SHELL
302is set to the default system shell
303.Pq Pa /bin/sh .
304.Pp
305The
306.Fn auth_challenge
307function queries the login script defined by the current
308.Ar style
309for a challenge for the user specified by
310.Ar name .
311(See below for the setting of the
312.Ar style
313and
314.Ar name ) .
315It internally uses the
316.Fn auth_call
317function.
318The generated challenge is returned.
319.Dv NULL
320is returned on error or if no challenge was generated.
321The challenge can also be extracted by the
322.Fn auth_getchallenge
323function, which simply returns the last challenge generated
324for this session.
325.Pp
326The
327.Fn auth_check_change
328and
329.Fn auth_check_expire
330functions check the password expiration (change) and account expiration
331times.
332They return 0 if no change or expiration time is set for the account.
333They return a negative value of how many seconds have passed since
334the password or account expired.
335In this case the state of the session is marked with either
336.Li AUTH_PWEXPIRED
337or
338.Li AUTH_EXPIRED
339as well as clearing any bits which would indicate the authentication was
340successful.
341If the password or account has not expired, they return the number of
342seconds left until the account does expire.
343The return value of -1 can either indicate the password or account
344just expired or that no password entry was set for the current session.
345.Pp
346The
347.Fn auth_clrenv
348function clears any requests set by a login script for
349environment variables to be set.
350.Pp
351The
352.Fn auth_clroption
353function clears the previously set option
354.Fa name .
355.Pp
356The
357.Fn auth_clroptions
358function clears all previously set options.
359.Pp
360The
361.Fn auth_getitem
362function returns the value of
363.Fa item .
364The
365.Fa item
366may be one of:
367.Bl -tag -width AUTH_INTERACTIVE
368.It Dv AUTH_CHALLENGE
369The latest challenge, if any, set for the session.
370.It Dv AUTH_CLASS
371The class of the user, as defined by the
372.Pa /etc/login.conf
373file.
374This value is not directly used by BSD Authentication, rather, it is
375passed to the login scripts for their possible use.
376.It Dv AUTH_INTERACTIVE
377If set to any value, then the session is tagged as interactive.
378If not set, the session is not interactive.
379When the value is requested it is always either
380.Dv NULL
381or
382.Dq True .
383The auth subroutines may choose to provide additional information to
384standard output or standard error when the session is interactive.
385There is no functional change in the operation of the subroutines.
386.It Dv AUTH_NAME
387The name of the user being authenticated.
388The name should include the instance, if any, that is being requested.
389.It Dv AUTH_SERVICE
390The service requesting the authentication.
391Initially it is set to the default service which provides the traditional
392interactive service.
393.It Dv AUTH_STYLE
394The style of authentication being performed, as defined by the
395.Pa /etc/login.conf
396file.
397The style determines which login script should actually be used.
398.El
399.Pp
400The value returned points to private memory and should not be
401freed by the caller.
402.Pp
403The
404.Fn auth_getvalue
405function returns the value, if any, associated with the specified internal
406variable
407.Ar what .
408These variables are set by login scripts.
409When a new login script is run
410(by the
411.Fn auth_call
412function)
413the values from the previous login script are lost.
414(See
415.Xr login.conf 5
416for details on internal variables.)
417.Pp
418The
419.Fn auth_set_va_list
420function establishes a variable argument list to be used by the
421.Fn auth_call
422function.
423It is intended to be used by functions which need to call
424.Fn auth_call
425but take a variable number of arguments themselves.
426Since the arguments are not copied, the call to
427.Fn auth_call
428must be placed within the scope of
429.Fa ap .
430The
431.Fn auth_call
432function will call
433.Xr va_end 3
434on
435.Fa ap .
436.Pp
437The
438.Fn auth_setdata
439function makes a copy of
440.Fa len
441bytes of data pointed to by
442.Fa ptr
443for use by
444.Fn auth_call .
445The data will be passed on the back channel to the next login script called.
446.Pp
447The
448.Fn auth_setenv
449function adds/deletes any environment variables requested by the
450login script to the current environment.
451.Pp
452The
453.Fn auth_setitem
454function assigns
455.Fa value
456to the specified
457.Fa item .
458The items are described above with the
459.Fn auth_getitem
460function.
461In addition, if
462.Fa value
463is
464.Dv NULL ,
465the
466.Fa item
467is cleared.
468If
469.Fa value
470is
471.Dv NULL
472and
473.Fa item
474is
475.Li AUTH_ALL
476then all items are cleared.
477.Pp
478The
479.Fn auth_setoption
480function requests that the option
481.Fa name
482be set with the value of
483.Fa value
484when a script is executed by
485.Fn auth_call .
486The actual arguments to the script will be placed at the beginning
487of the argument vector.
488For each option two arguments will be issued:
489.Li -v name=value .
490.Pp
491The function
492.Fn auth_setpwd
493establishes the password file entry for the authentication session.
494If the name has already been set by
495.Fn auth_setitem
496then the
497.Fa pwd
498argument may be
499.Dv NULL ,
500else it must be the password entry to use.
501.Pp
502The function
503.Fn auth_getpwd
504retrieves the saved password file entry for the authentication session.
505If no entry has been saved (either explicitly via
506.Fn auth_setpwd
507or implicitly via
508.Fn auth_check_expire
509or
510.Fn auth_check_change )
511it returns
512.Dv NULL .
513Note that the memory containing the password file entry is freed by
514a call to
515.Fn auth_close
516or
517.Fn auth_clean .
518.Pp
519The function
520.Fn auth_setstate
521sets the sessions state to
522.Fa state .
523Typically this is either
524.Li AUTH_OKAY
525or 0.
526.Sh SEE ALSO
527.Xr authenticate 3 ,
528.Xr login_cap 3 ,
529.Xr pw_dup 3 ,
530.Xr login.conf 5
531.Sh CAVEATS
532The
533.Fn auth_check_change
534and
535.Fn auth_check_expire
536functions call
537.Fn getpwnam
538or
539.Fn getpwuid ,
540overwriting the static storage used by the
541.Xr getpwent 3
542family of routines.
543The calling program must either make a local copy of the passwd struct
544pointer via the
545.Xr pw_dup 3
546function or use the
547.Xr auth_setpwd 3
548function to copy the passwd struct into
549.Fa as .
550