1.\"-
2.\" Copyright (c) 2001-2003 Networks Associates Technology, Inc.
3.\" Copyright (c) 2004-2011 Dag-Erling Smørgrav
4.\" All rights reserved.
5.\"
6.\" This software was developed for the FreeBSD Project by ThinkSec AS and
7.\" Network Associates Laboratories, the Security Research Division of
8.\" Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
9.\" ("CBOSS"), as part of the DARPA CHATS research program.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\"    notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\"    notice, this list of conditions and the following disclaimer in the
18.\"    documentation and/or other materials provided with the distribution.
19.\" 3. The name of the author may not be used to endorse or promote
20.\"    products derived from this software without specific prior written
21.\"    permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\" $Id$
36.\"
37.Dd May 26, 2012
38.Dt OPENPAM_READWORD 3
39.Os
40.Sh NAME
41.Nm openpam_readword
42.Nd read a word from a file, respecting shell quoting rules
43.Sh LIBRARY
44.Lb libpam
45.Sh SYNOPSIS
46.In sys/types.h
47.In stdio.h
48.In security/pam_appl.h
49.In security/openpam.h
50.Ft "char *"
51.Fn openpam_readword "FILE *f" "int *lineno" "size_t *lenp"
52.Sh DESCRIPTION
53The
54.Fn openpam_readword
55function reads the next word from a file, and
56returns it in a NUL-terminated buffer allocated with
57.Xr malloc 3 .
58.Pp
59A word is a sequence of non-whitespace characters.
60However, whitespace characters can be included in a word if quoted or
61escaped according to the following rules:
62.Bl -bullet
63.It
64An unescaped single or double quote introduces a quoted string,
65which ends when the same quote character is encountered a second
66time.
67The quotes themselves are stripped.
68.It
69Within a single- or double-quoted string, all whitespace characters,
70including the newline character, are preserved as-is.
71.It
72Outside a quoted string, a backslash escapes the next character,
73which is preserved as-is, unless that character is a newline, in
74which case it is discarded and reading continues at the beginning of
75the next line as if the backslash and newline had not been there.
76In all cases, the backslash itself is discarded.
77.It
78Within a single-quoted string, double quotes and backslashes are
79preserved as-is.
80.It
81Within a double-quoted string, a single quote is preserved as-is,
82and a backslash is preserved as-is unless used to escape a double
83quote.
84.El
85.Pp
86In addition, if the first non-whitespace character on the line is a
87hash character (#), the rest of the line is discarded.
88If a hash character occurs within a word, however, it is preserved
89as-is.
90A backslash at the end of a comment does cause line continuation.
91.Pp
92If
93.Fa lineno
94is not
95.Dv NULL ,
96the integer variable it points to is
97incremented every time a quoted or escaped newline character is read.
98.Pp
99If
100.Fa lenp
101is not
102.Dv NULL ,
103the length of the word (after quotes and
104backslashes have been removed) is stored in the variable it points to.
105.Sh RETURN VALUES
106If successful, the
107.Fn openpam_readword
108function returns a pointer to a
109dynamically allocated NUL-terminated string containing the first word
110encountered on the line.
111.Pp
112The caller is responsible for releasing the returned buffer by passing
113it to
114.Xr free 3 .
115.Pp
116If
117.Fn openpam_readword
118reaches the end of the line or file before any
119characters are copied to the word, it returns
120.Dv NULL .
121In the former
122case, the newline is pushed back to the file.
123.Pp
124If
125.Fn openpam_readword
126reaches the end of the file while a quote or
127backslash escape is in effect, it sets
128.Va errno
129to
130.Dv EINVAL
131and returns
132.Dv NULL .
133.Sh IMPLEMENTATION NOTES
134The parsing rules are intended to be equivalent to the normal POSIX
135shell quoting rules.
136Any discrepancy is a bug and should be reported to the author along
137with sample input that can be used to reproduce the error.
138.Pp
139.Sh SEE ALSO
140.Xr openpam_readline 3 ,
141.Xr openpam_readlinev 3 ,
142.Xr pam 3
143.Sh STANDARDS
144The
145.Fn openpam_readword
146function is an OpenPAM extension.
147.Sh AUTHORS
148The
149.Fn openpam_readword
150function and this manual page were
151developed by
152.An Dag-Erling Sm\(/orgrav Aq des@des.no .
153