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