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