1.\" Copyright (c) 1995 David Nugent <davidn@blaze.net.au> 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, is permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice immediately at the beginning of the file, without modification, 9.\" 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. This work was done expressly for inclusion into FreeBSD. Other use 14.\" is permitted provided this notation is included. 15.\" 4. Absolutely no warranty of function or purpose is made by the author 16.\" David Nugent. 17.\" 5. Modifications may be freely made to this file providing the above 18.\" conditions are met. 19.\" 20.Dd May 10, 2020 21.Dt LOGIN_CAP 3 22.Os 23.Sh NAME 24.Nm login_close , 25.Nm login_getcapbool , 26.Nm login_getcaplist , 27.Nm login_getcapnum , 28.Nm login_getcapenum , 29.Nm login_getcapstr , 30.Nm login_getcapsize , 31.Nm login_getcaptime , 32.Nm login_getclass , 33.Nm login_getclassbyname , 34.Nm login_getpath , 35.Nm login_getpwclass , 36.Nm login_getstyle , 37.Nm login_getuserclass , 38.Nm login_setcryptfmt 39.Nd "functions for accessing the login class capabilities database" 40.Sh LIBRARY 41.Lb libutil 42.Sh SYNOPSIS 43.In sys/types.h 44.In login_cap.h 45.Ft void 46.Fn login_close "login_cap_t *lc" 47.Ft login_cap_t * 48.Fn login_getclassbyname "const char *nam" "const struct passwd *pwd" 49.Ft login_cap_t * 50.Fn login_getclass "const char *nam" 51.Ft login_cap_t * 52.Fn login_getpwclass "const struct passwd *pwd" 53.Ft login_cap_t * 54.Fn login_getuserclass "const struct passwd *pwd" 55.Ft "const char *" 56.Fn login_getcapstr "login_cap_t *lc" "const char *cap" "const char *def" "const char *error" 57.Ft "const char **" 58.Fn login_getcaplist "login_cap_t *lc" "const char *cap" "const char *chars" 59.Ft "const char *" 60.Fn login_getpath "login_cap_t *lc" "const char *cap" "const char *error" 61.Ft rlim_t 62.Fn login_getcaptime "login_cap_t *lc" "const char *cap" "rlim_t def" "rlim_t error" 63.Ft rlim_t 64.Fn login_getcapnum "login_cap_t *lc" "const char *cap" "rlim_t def" "rlim_t error" 65.Ft int 66.Fn login_getcapenum "login_cap_t *lc" "const char *cap" "const char * const *values" 67.Ft rlim_t 68.Fn login_getcapsize "login_cap_t *lc" "const char *cap" "rlim_t def" "rlim_t error" 69.Ft int 70.Fn login_getcapbool "login_cap_t *lc" "const char *cap" "int def" 71.Ft "const char *" 72.Fn login_getstyle "login_cap_t *lc" "const char *style" "const char *auth" 73.Ft const char * 74.Fn login_setcryptfmt "login_cap_t *lc" "const char *def" "const char *error" 75.Sh DESCRIPTION 76These functions represent a programming interface to the login 77classes database provided in 78.Xr login.conf 5 . 79This database contains capabilities, attributes and default environment 80and accounting settings for users and programs running as specific users, 81as determined by the login class field within entries in 82.Pa /etc/master.passwd . 83.Pp 84Entries in 85.Xr login.conf 5 86consist of colon 87.Ql \&: 88separated fields, the first field in each record being one or more 89identifiers for the record (which must be unique for the entire database), 90each separated by a 91.Ql | , 92and may optionally include a description as 93the last 94.Sq name . 95Remaining fields in the record consist of keyword/data pairs. 96Long lines may be continued with a backslash within empty entries, 97with the second and subsequent lines optionally indented for readability. 98This is similar to the format used in 99.Xr termcap 5 , 100except that keywords are not limited to two significant characters, 101and are usually longer for improved readability. 102As with termcap entries, multiple records can be linked together 103(one record including another) using a field containing 104.Ql tc= Ns Va <recordid> . 105The result is that the entire record referenced by 106.Va <recordid> 107replaces the 108.Va tc= 109field at the point at which it occurs. 110See 111.Xr getcap 3 112for further details on the format and use of a capabilities database. 113.Pp 114The 115.Nm login_cap 116interface provides a convenient means of retrieving login class 117records with all 118.Va tc= 119references expanded. 120A program will typically call one of 121.Fn login_getclass , 122.Fn login_getpwclass , 123.Fn login_getuserclass 124or 125.Fn login_getclassbyname 126according to its requirements. 127Each of these functions returns a login capabilities structure, 128.Vt login_cap_t , 129which may subsequently be used to interrogate the database for 130specific values using the rest of the API. 131Once the 132.Vt login_cap_t 133is of no further use, the 134.Fn login_close 135function should be called to free all resources used. 136.Pp 137The structure of 138.Vt login_cap_t 139is defined in 140.In login_cap.h , 141as: 142.Bd -literal -offset indent 143typedef struct { 144 char *lc_class; 145 char *lc_cap; 146 char *lc_style; 147} login_cap_t; 148.Ed 149.Pp 150The 151.Fa lc_class 152member contains a pointer to the name of the login class 153retrieved. 154This may not necessarily be the same as the one requested, 155either directly via 156.Fn login_getclassbyname , 157or indirectly via a user's login record using 158.Fn login_getpwclass , 159by class name using 160.Fn login_getclass . 161If the referenced user has no login class specified in 162.Pa /etc/master.passwd , 163the class name is 164.Dv NULL 165or an empty string. 166If the class 167specified does not exist in the database, each of these 168functions will search for a record with an id of 169.Ql default , 170with that name returned in the 171.Fa lc_class 172field. 173In addition, if the referenced user has a UID of 0 (normally, 174.Ql root , 175although the user name is not considered) then 176.Fn login_getpwclass 177will search for a record with an id of 178.Ql root 179before it searches 180for the record with the id of 181.Ql default . 182.Pp 183The 184.Fa lc_cap 185field is used internally by the library to contain the 186expanded login capabilities record. 187Programs with unusual requirements may wish to use this 188with the lower-level 189.Fn getcap 190style functions to access the record directly. 191.Pp 192The 193.Fa lc_style 194field is set by the 195.Fn login_getstyle 196function to the authorisation style, according to the requirements 197of the program handling a login itself. 198.Pp 199The 200.Fn login_getclassbyname 201function is the basic means to get a 202.Vt login_cap_t 203object. 204It accepts two arguments: the first one, 205.Fa name , 206is the record identifier of the 207record to be retrieved; the second, 208.Fa pwd , 209is an optional pointer to a 210.Vt passwd 211structure. 212First of all, its arguments are used by the function 213to choose between system and user modes of operation. 214When in system mode, only the system login class database is used. 215When in user mode, the supplemental login class database in the 216user's home directory is allowed to override settings from the system 217database in a limited way as noted below. 218To minimize security implications, user mode is entered by 219.Fn login_getclassbyname 220if and only if 221.Fa name 222is 223.Dv LOGIN_MECLASS 224.Pq Ql me 225and 226.Fa pwd 227is not 228.Dv NULL . 229Otherwise system mode is chosen. 230.Pp 231In system mode, any record in the system database 232.Pa /etc/login.conf 233can be accessed, 234and a fallback to the default record is provided as follows. 235If 236.Fa name 237is 238.Dv NULL , 239an empty string, or a class that does not exist 240in the login class database, then the 241.Dv LOGIN_DEFCLASS 242record 243.Pq Ql default 244is returned instead. 245.Pp 246In user mode, only the 247.Dv LOGIN_MECLASS 248record 249.Pq Ql me 250is accessed and no fallback to the 251.Ql default 252record is provided. 253The directory specified by 254.Fa pwd->pw_dir 255is searched for 256a login database file called 257.Pa .login_conf , 258and only the 259.Ql me 260capability record 261contained within it may override the system record with the same name 262while other records are ignored. 263Using this scheme, an application can explicitly 264allow users to override a selected subset of login settings. 265To do so, the application should obtain two 266.Vt login_cap_t 267objects, one in user mode and the other in system mode, 268and then query the user object before the 269system object for login parameters that are allowed to 270be overridden by the user. 271For example, the user's 272.Pa .login_conf 273can provide a convenient way for a user to set up their preferred 274login environment before the shell is invoked on login if supported by 275.Xr login 1 . 276.Pp 277Note that access to the 278.Pa /etc/login.conf 279and 280.Pa .login_conf 281files will only be performed subject to the security checks documented in 282.Xr _secure_path 3 283for the uids 0 and 284.Fa pwd->pw_uid 285respectively. 286.Pp 287If the specified record is 288.Dv NULL , 289empty or does not exist, and the 290system has no 291.Ql default 292record available to fall back to, there is a 293memory allocation error or for some reason 294.Xr cgetent 3 295is unable to access the login capabilities database, this function 296returns 297.Dv NULL . 298.Pp 299The functions 300.Fn login_getclass , 301.Fn login_getpwclass 302and 303.Fn login_getuserclass 304retrieve the applicable login class record for the user's passwd 305entry or class name by calling 306.Fn login_getclassbyname . 307On failure, 308.Dv NULL 309is returned. 310The difference between these functions is that 311.Fn login_getuserclass 312includes the user's overriding 313.Pa .login_conf 314that exists in the user's home directory, and 315.Fn login_getpwclass 316and 317.Fn login_getclass 318restrict lookup only to the system login class database in 319.Pa /etc/login.conf . 320As explained earlier, 321.Fn login_getpwclass 322differs from 323.Fn login_getclass 324in that it allows the default class for a super-user as 325.Ql root 326if none has been specified in the password database. 327Otherwise, if the passwd pointer is 328.Dv NULL , 329or the user record 330has no login class, then the system 331.Ql default 332entry is retrieved. 333Essentially, 334.Fn login_getclass name 335is equivalent to 336.Fn login_getclassbyname name NULL 337and 338.Fn login_getuserclass pwd 339to 340.Fn login_getclassbyname LOGIN_MECLASS pwd . 341.Pp 342Once a program no longer wishes to use a 343.Vt login_cap_t 344object, 345.Fn login_close 346may be called to free all resources used by the login class. 347The 348.Fn login_close 349function may be passed a 350.Dv NULL 351pointer with no harmful side-effects. 352.Pp 353The remaining functions may be used to retrieve individual 354capability records. 355Each function takes a 356.Vt login_cap_t 357object as its first parameter, 358a capability tag as the second, and remaining parameters being 359default and error values that are returned if the capability is 360not found. 361The type of the additional parameters passed and returned depend 362on the 363.Em type 364of capability each deals with, be it a simple string, a list, 365a time value, a file or memory size value, a path (consisting of 366a colon-separated list of directories) or a boolean flag. 367The manpage for 368.Xr login.conf 5 369deals in specific tags and their type. 370.Pp 371Note that with all functions in this group, you should not call 372.Xr free 3 373on any pointers returned. 374Memory allocated during retrieval or processing of capability 375tags is automatically reused by subsequent calls to functions 376in this group, or deallocated on calling 377.Fn login_close . 378.Bl -tag -width "login_getcaplist()" 379.It Fn login_getcapstr 380This function returns a simple string capability. 381If the string is not found, then the value in 382.Fa def 383is returned as the default value, or if an error 384occurs, the value in the 385.Fa error 386parameter is returned. 387.It Fn login_getcaplist 388This function returns the value corresponding to the named 389capability tag as a list of values in a 390.Dv NULL 391terminated array. 392Within the login class database, some tags are of type 393.Vt list , 394which consist of one or more comma- or space separated 395values. 396Usually, this function is not called directly from an 397application, but is used indirectly via 398.Fn login_getstyle . 399.It Fn login_getpath 400This function returns a list of directories separated by colons 401.Ql \&: . 402Capability tags for which this function is called consist of a list of 403directories separated by spaces. 404.It Fn login_getcaptime 405This function returns a 406.Vt time value 407associated with a particular capability tag with the value expressed 408in seconds (the default), minutes, hours, days, weeks or (365 day) 409years or any combination of these. 410A suffix determines the units used: 411.Ql S 412for seconds, 413.Ql M 414for minutes, 415.Ql H 416for hours, 417.Ql D 418for days, 419.Ql W 420for weeks and 421.Ql Y 422for 365 day years. 423Case of the units suffix is ignored. 424.Pp 425Time values are normally used for setting resource, accounting and 426session limits. 427If supported by the operating system and compiler (which is true of 428.Fx ) , 429the value returned is a 430.Vt quad 431.Pq Vt long long , 432of type 433.Vt rlim_t . 434A value 435.Ql inf 436or 437.Ql infinity 438may be used to express an infinite 439value, in which case 440.Dv RLIM_INFINITY 441is returned. 442.It Fn login_getcapnum 443This function returns a numeric value for a tag, expressed either as 444.Ql tag=<value> 445or the standard 446.Fn cgetnum 447format 448.Ql tag#<value> . 449The first format should be used in preference to the second, the 450second format is provided for compatibility and consistency with the 451.Xr getcap 3 452database format where numeric types use the 453.Ql \&# 454as the delimiter for numeric values. 455If in the first format, then the value given may be 456.Ql inf 457or 458.Ql infinity 459which results in a return value of 460.Dv RLIM_INFINITY . 461If the given capability tag cannot be found, the 462.Fa def 463parameter is returned, and if an error occurs, the 464.Fa error 465parameter is returned. 466.It Fn login_getcapenum 467This function returns whether the searched capability is a string with value 468among a predefined set passed in argument 469.Fa values 470as a NULL-terminated array of strings. 471.Pp 472A non-negative value indicates a match and is the index of the capability's 473value in array 474.Fa values . 475Other possible return values are: 476.Bl -tag -width "-4" 477.It -4 478Returned if 479.Fa lc 480or 481.Fa cap 482are insufficiently initialized or invalid. 483.It -3 484Returned on allocation failure (out of memory). 485.It -2 486Returned if the capability isn't specified or its value is not a string. 487.It -1 488Returned if the capability is specified and a string but its value is not among 489.Fa values . 490.El 491.It Fn login_getcapsize 492.Fn login_getcapsize 493returns a value representing a size (typically, file or memory) 494which may be expressed as bytes (the default), 512 byte blocks, 495kilobytes, megabytes, gigabytes, and on systems that support the 496.Vt long long 497type, terabytes. 498The suffix used determines the units, and multiple values and 499units may be used in combination (e.g.\& 1m500k = 1.5 megabytes). 500A value with no suffix is interpreted as bytes, 501.Ql B 502as 512-byte blocks, 503.Ql K 504as kilobytes, 505.Ql M 506as megabytes, 507.Ql G 508as gigabytes and 509.Ql T 510as terabytes. 511Case is ignored. 512The error value is returned if there is a login capabilities database 513error, if an invalid suffix is used, or if a numeric value cannot be 514interpreted. 515.It Fn login_getcapbool 516This function returns a boolean value tied to a particular flag. 517It returns 0 if the given capability tag is not present or is 518negated by the presence of a 519.Ql tag@ 520(see 521.Xr getcap 3 522for more information on boolean flags), and returns 1 if the tag 523is found. 524.It Fn login_getstyle 525This function is used by the login authorisation system to determine 526the style of login available in a particular case. 527The function accepts three parameters, the 528.Fa lc 529entry itself and 530two optional parameters, and authorisation type 531.Fa auth 532and 533.Fa style , 534and 535applies these to determine the authorisation style that best suites 536these rules. 537.Bl -bullet 538.It 539If 540.Fa auth 541is neither 542.Dv NULL 543nor an empty string, look for a tag of type 544.Ql auth- Ns Fa <auth> 545in the capability record. 546If not present, then look for the default tag 547.Va auth= . 548.It 549If no valid authorisation list was found from the previous step, then 550default to 551.Ql passwd 552as the authorisation list. 553.It 554If 555.Fa style 556is not 557.Dv NULL 558or empty, look for it in the list of authorisation 559methods found from the previous step. 560If 561.Fa style 562is 563.Dv NULL 564or an empty string, then default to 565.Ql passwd 566authorisation. 567.It 568If 569.Fa style 570is found in the chosen list of authorisation methods, then 571return that, otherwise return 572.Dv NULL . 573.El 574.Pp 575This scheme allows the administrator to determine the types of 576authorisation methods accepted by the system, depending on the 577means by which the access occurs. 578For example, the administrator may require skey or kerberos as 579the authentication method used for access to the system via the 580network, and standard methods via direct dialup or console 581logins, significantly reducing the risk of password discovery 582by "snooping" network packets. 583.It Fn login_setcryptfmt 584The 585.Fn login_setcryptfmt 586function is used to set the 587.Xr crypt 3 588format using the 589.Va passwd_format 590configuration entry. 591If no entry is found, 592.Fa def 593is taken to be used as the fallback. 594If calling 595.Xr crypt_set_format 3 596on the specifier fails, 597.Fa error 598is returned to indicate this. 599.El 600.Sh SEE ALSO 601.Xr login 1 , 602.Xr crypt 3 , 603.Xr getcap 3 , 604.Xr login_class 3 , 605.Xr login.conf 5 , 606.Xr termcap 5 607.Sh HISTORY 608The functions 609.Fn login_close , 610.Fn login_getcapbool , 611.Fn login_getcaplist , 612.Fn login_getcapnum , 613.Fn login_getcapstr , 614.Fn login_getcapsize , 615.Fn login_getcaptime , 616.Fn login_getclass , 617.Fn login_getclassbyname , 618.Fn login_getpwclass , 619.Fn login_getstyle , 620.Fn login_getuserclass 621and 622.Fn login_setcryptfmt 623first appeared in 624.Fx 2.1.5 . 625