xref: /minix3/lib/libc/stdio/tmpnam.3 (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras.\"	$NetBSD: tmpnam.3,v 1.17 2010/04/30 04:55:10 jruoho Exp $
2*2fe8fb19SBen Gras.\"
3*2fe8fb19SBen Gras.\" Copyright (c) 1988, 1991, 1993
4*2fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
5*2fe8fb19SBen Gras.\"
6*2fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by
7*2fe8fb19SBen Gras.\" the American National Standards Committee X3, on Information
8*2fe8fb19SBen Gras.\" Processing Systems.
9*2fe8fb19SBen Gras.\"
10*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
11*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
12*2fe8fb19SBen Gras.\" are met:
13*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
14*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
15*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
16*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
17*2fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
18*2fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
19*2fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
20*2fe8fb19SBen Gras.\"    without specific prior written permission.
21*2fe8fb19SBen Gras.\"
22*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*2fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*2fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*2fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*2fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*2fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*2fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*2fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*2fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*2fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*2fe8fb19SBen Gras.\" SUCH DAMAGE.
33*2fe8fb19SBen Gras.\"
34*2fe8fb19SBen Gras.\"     @(#)tmpnam.3	8.2 (Berkeley) 11/17/93
35*2fe8fb19SBen Gras.\"
36*2fe8fb19SBen Gras.Dd April 30, 2010
37*2fe8fb19SBen Gras.Dt TMPFILE 3
38*2fe8fb19SBen Gras.Os
39*2fe8fb19SBen Gras.Sh NAME
40*2fe8fb19SBen Gras.Nm tempnam ,
41*2fe8fb19SBen Gras.Nm tmpfile ,
42*2fe8fb19SBen Gras.Nm tmpnam
43*2fe8fb19SBen Gras.Nd temporary file routines
44*2fe8fb19SBen Gras.Sh LIBRARY
45*2fe8fb19SBen Gras.Lb libc
46*2fe8fb19SBen Gras.Sh SYNOPSIS
47*2fe8fb19SBen Gras.In stdio.h
48*2fe8fb19SBen Gras.Ft FILE *
49*2fe8fb19SBen Gras.Fn tmpfile void
50*2fe8fb19SBen Gras.Ft char *
51*2fe8fb19SBen Gras.Fn tmpnam "char *str"
52*2fe8fb19SBen Gras.Ft char *
53*2fe8fb19SBen Gras.Fn tempnam "const char *tmpdir" "const char *prefix"
54*2fe8fb19SBen Gras.Sh DESCRIPTION
55*2fe8fb19SBen GrasThe
56*2fe8fb19SBen Gras.Fn tmpfile
57*2fe8fb19SBen Grasfunction
58*2fe8fb19SBen Grasreturns a pointer to a stream associated with a file descriptor returned
59*2fe8fb19SBen Grasby the routine
60*2fe8fb19SBen Gras.Xr mkstemp 3 .
61*2fe8fb19SBen GrasThe created file is unlinked before
62*2fe8fb19SBen Gras.Fn tmpfile
63*2fe8fb19SBen Grasreturns, causing the file to be automatically deleted when the last
64*2fe8fb19SBen Grasreference to it is closed.
65*2fe8fb19SBen GrasThe file is opened with the access value
66*2fe8fb19SBen Gras.Ql w+ .
67*2fe8fb19SBen Gras.Pp
68*2fe8fb19SBen GrasThe
69*2fe8fb19SBen Gras.Fn tmpnam
70*2fe8fb19SBen Grasfunction
71*2fe8fb19SBen Grasreturns a pointer to a file name, in the
72*2fe8fb19SBen Gras.Dv P_tmpdir
73*2fe8fb19SBen Grasdirectory, which
74*2fe8fb19SBen Grasdid not reference an existing file at some indeterminate point in the
75*2fe8fb19SBen Graspast.
76*2fe8fb19SBen Gras.Dv P_tmpdir
77*2fe8fb19SBen Grasis defined in the include file
78*2fe8fb19SBen Gras.In stdio.h .
79*2fe8fb19SBen GrasIf the argument
80*2fe8fb19SBen Gras.Fa s
81*2fe8fb19SBen Grasis
82*2fe8fb19SBen Gras.Pf non- Dv NULL ,
83*2fe8fb19SBen Grasthe file name is copied to the buffer it references.
84*2fe8fb19SBen GrasOtherwise, the file name is copied to a static buffer.
85*2fe8fb19SBen GrasIn either case,
86*2fe8fb19SBen Gras.Fn tmpnam
87*2fe8fb19SBen Grasreturns a pointer to the file name.
88*2fe8fb19SBen Gras.Pp
89*2fe8fb19SBen GrasThe buffer referenced by
90*2fe8fb19SBen Gras.Fa s
91*2fe8fb19SBen Grasis expected to be at least
92*2fe8fb19SBen Gras.Dv L_tmpnam
93*2fe8fb19SBen Grasbytes in length.
94*2fe8fb19SBen Gras.Dv L_tmpnam
95*2fe8fb19SBen Grasis defined in the include file
96*2fe8fb19SBen Gras.In stdio.h .
97*2fe8fb19SBen Gras.Pp
98*2fe8fb19SBen GrasThe
99*2fe8fb19SBen Gras.Fn tempnam
100*2fe8fb19SBen Grasfunction
101*2fe8fb19SBen Grasis similar to
102*2fe8fb19SBen Gras.Fn tmpnam ,
103*2fe8fb19SBen Grasbut provides the ability to specify the directory which will
104*2fe8fb19SBen Grascontain the temporary file and the file name prefix.
105*2fe8fb19SBen Gras.Pp
106*2fe8fb19SBen GrasThe environment variable
107*2fe8fb19SBen Gras.Ev TMPDIR
108*2fe8fb19SBen Gras(if set), the argument
109*2fe8fb19SBen Gras.Fa tmpdir
110*2fe8fb19SBen Gras(if
111*2fe8fb19SBen Gras.Pf non- Dv NULL ) ,
112*2fe8fb19SBen Grasthe directory
113*2fe8fb19SBen Gras.Dv P_tmpdir ,
114*2fe8fb19SBen Grasand the directory
115*2fe8fb19SBen Gras.Pa /tmp
116*2fe8fb19SBen Grasare tried, in the listed order, as directories in which to store the
117*2fe8fb19SBen Grastemporary file.
118*2fe8fb19SBen Gras.Pp
119*2fe8fb19SBen GrasThe argument
120*2fe8fb19SBen Gras.Fa prefix ,
121*2fe8fb19SBen Grasif
122*2fe8fb19SBen Gras.Pf non- Dv NULL ,
123*2fe8fb19SBen Grasis used to specify a file name prefix, which will be the
124*2fe8fb19SBen Grasfirst part of the created file name.
125*2fe8fb19SBen Gras.Fn tempnam
126*2fe8fb19SBen Grasallocates memory in which to store the file name; the returned pointer
127*2fe8fb19SBen Grasmay be used as a subsequent argument to
128*2fe8fb19SBen Gras.Xr free 3 .
129*2fe8fb19SBen Gras.Sh RETURN VALUES
130*2fe8fb19SBen GrasThe
131*2fe8fb19SBen Gras.Fn tmpfile
132*2fe8fb19SBen Grasfunction
133*2fe8fb19SBen Grasreturns a pointer to an open file stream on success, and a
134*2fe8fb19SBen Gras.Dv NULL
135*2fe8fb19SBen Graspointer
136*2fe8fb19SBen Grason error.
137*2fe8fb19SBen Gras.Pp
138*2fe8fb19SBen GrasThe
139*2fe8fb19SBen Gras.Fn tmpnam
140*2fe8fb19SBen Grasand
141*2fe8fb19SBen Gras.Fn tempnam
142*2fe8fb19SBen Grasfunctions
143*2fe8fb19SBen Grasreturn a pointer to a file name on success, and a
144*2fe8fb19SBen Gras.Dv NULL
145*2fe8fb19SBen Graspointer
146*2fe8fb19SBen Grason error.
147*2fe8fb19SBen Gras.Sh ERRORS
148*2fe8fb19SBen GrasThe
149*2fe8fb19SBen Gras.Fn tmpfile
150*2fe8fb19SBen Grasfunction
151*2fe8fb19SBen Grasmay fail and set the global variable
152*2fe8fb19SBen Gras.Va errno
153*2fe8fb19SBen Grasfor any of the errors specified for the library functions
154*2fe8fb19SBen Gras.Xr fdopen 3
155*2fe8fb19SBen Grasor
156*2fe8fb19SBen Gras.Xr mkstemp 3 .
157*2fe8fb19SBen Gras.Pp
158*2fe8fb19SBen GrasThe
159*2fe8fb19SBen Gras.Fn tmpnam
160*2fe8fb19SBen Grasfunction
161*2fe8fb19SBen Grasmay fail and set
162*2fe8fb19SBen Gras.Va errno
163*2fe8fb19SBen Grasfor any of the errors specified for the library function
164*2fe8fb19SBen Gras.Xr mktemp 3 .
165*2fe8fb19SBen Gras.Pp
166*2fe8fb19SBen GrasThe
167*2fe8fb19SBen Gras.Fn tempnam
168*2fe8fb19SBen Grasfunction
169*2fe8fb19SBen Grasmay fail and set
170*2fe8fb19SBen Gras.Va errno
171*2fe8fb19SBen Grasfor any of the errors specified for the library functions
172*2fe8fb19SBen Gras.Xr malloc 3
173*2fe8fb19SBen Grasor
174*2fe8fb19SBen Gras.Xr mktemp 3 .
175*2fe8fb19SBen Gras.Sh SEE ALSO
176*2fe8fb19SBen Gras.Xr mkstemp 3 ,
177*2fe8fb19SBen Gras.Xr mktemp 3
178*2fe8fb19SBen Gras.Sh STANDARDS
179*2fe8fb19SBen GrasThe
180*2fe8fb19SBen Gras.Fn tmpfile
181*2fe8fb19SBen Grasand
182*2fe8fb19SBen Gras.Fn tmpnam
183*2fe8fb19SBen Grasfunctions
184*2fe8fb19SBen Grasconform to
185*2fe8fb19SBen Gras.St -ansiC .
186*2fe8fb19SBen GrasAll described functions also conform to
187*2fe8fb19SBen Gras.St -p1003.1-2001 ,
188*2fe8fb19SBen Grasalbeit the
189*2fe8fb19SBen Gras.Fn tempnam
190*2fe8fb19SBen Grasand
191*2fe8fb19SBen Gras.Fn tmpnam
192*2fe8fb19SBen Grasfunctions have been marked as obsolete in the
193*2fe8fb19SBen Gras.St -p1003.1-2008
194*2fe8fb19SBen Grasrevision.
195*2fe8fb19SBen Gras.Sh BUGS
196*2fe8fb19SBen GrasThese interfaces are provided for
197*2fe8fb19SBen Gras.At V
198*2fe8fb19SBen Grasand
199*2fe8fb19SBen Gras.Tn ANSI
200*2fe8fb19SBen Grascompatibility only.
201*2fe8fb19SBen GrasThe
202*2fe8fb19SBen Gras.Xr mkstemp 3
203*2fe8fb19SBen Grasinterface is strongly preferred.
204*2fe8fb19SBen Gras.Sh SECURITY CONSIDERATIONS
205*2fe8fb19SBen GrasThere are four important problems with these interfaces (as well as
206*2fe8fb19SBen Graswith the historic
207*2fe8fb19SBen Gras.Xr mktemp 3
208*2fe8fb19SBen Grasinterface).
209*2fe8fb19SBen GrasFirst, there is an obvious race between file name selection and file
210*2fe8fb19SBen Grascreation and deletion: the program is typically written to call
211*2fe8fb19SBen Gras.Fn tmpnam ,
212*2fe8fb19SBen Gras.Fn tempnam ,
213*2fe8fb19SBen Grasor
214*2fe8fb19SBen Gras.Xr mktemp 3 .
215*2fe8fb19SBen GrasSubsequently, the program calls
216*2fe8fb19SBen Gras.Xr open 2
217*2fe8fb19SBen Grasor
218*2fe8fb19SBen Gras.Xr fopen 3
219*2fe8fb19SBen Grasand erroneously opens a file (or symbolic link, or fifo or other
220*2fe8fb19SBen Grasdevice) that the attacker has placed in the expected file location.
221*2fe8fb19SBen GrasHence
222*2fe8fb19SBen Gras.Xr mkstemp 3
223*2fe8fb19SBen Grasis recommended, since it atomically creates the file.
224*2fe8fb19SBen Gras.Pp
225*2fe8fb19SBen GrasSecond, most historic implementations provide only a limited number
226*2fe8fb19SBen Grasof possible temporary file names (usually 26) before file names will
227*2fe8fb19SBen Grasstart being recycled.
228*2fe8fb19SBen GrasThird, the
229*2fe8fb19SBen Gras.At V
230*2fe8fb19SBen Grasimplementations of these functions (and of
231*2fe8fb19SBen Gras.Xr mktemp 3 )
232*2fe8fb19SBen Grasuse the
233*2fe8fb19SBen Gras.Xr access 2
234*2fe8fb19SBen Grassystem call to determine whether or not the temporary file may be created.
235*2fe8fb19SBen GrasThis has obvious ramifications for setuid or setgid programs, complicating
236*2fe8fb19SBen Grasthe portable use of these interfaces in such programs.
237*2fe8fb19SBen GrasFinally, there is no specification of the permissions with which the
238*2fe8fb19SBen Grastemporary files are created.
239*2fe8fb19SBen Gras.Pp
240*2fe8fb19SBen GrasThis implementation of
241*2fe8fb19SBen Gras.Fn tmpfile
242*2fe8fb19SBen Grasdoes not have these flaws,
243*2fe8fb19SBen Grasand that of
244*2fe8fb19SBen Gras.Fn tmpnam
245*2fe8fb19SBen Grasand
246*2fe8fb19SBen Gras.Fn tempnam
247*2fe8fb19SBen Grasonly have the first limitation, but portable software
248*2fe8fb19SBen Grascannot depend on that.
249*2fe8fb19SBen GrasIn particular, the
250*2fe8fb19SBen Gras.Fn tmpfile
251*2fe8fb19SBen Grasinterface should not be used in software expected to be used on other systems
252*2fe8fb19SBen Grasif there is any possibility that the user does not wish the temporary file to
253*2fe8fb19SBen Grasbe publicly readable and writable.
254*2fe8fb19SBen Gras.Pp
255*2fe8fb19SBen GrasA link-time warning will be issued if
256*2fe8fb19SBen Gras.Fn tmpnam
257*2fe8fb19SBen Grasor
258*2fe8fb19SBen Gras.Fn tempnam
259*2fe8fb19SBen Grasis used, and advises the use of
260*2fe8fb19SBen Gras.Fn mkstemp
261*2fe8fb19SBen Grasinstead.
262