2 * Copyright (c) 2017 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``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 THE INSTITUTE OR CONTRIBUTORS 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
34 #include "krb5_locl.h"
35 #include "store-int.h"
42 typedef struct stdio_storage
{
47 #define F(S) (((stdio_storage*)(S)->data)->f)
48 #define POS(S) (((stdio_storage*)(S)->data)->pos)
51 stdio_fetch(krb5_storage
* sp
, void *data
, size_t size
)
53 char *cbuf
= (char *)data
;
57 /* similar pattern to net_read() to support pipes */
59 count
= fread(cbuf
, 1, rem
, F(sp
));
66 } else if (count
== 0) {
68 POS(sp
) += size
- rem
;
80 stdio_store(krb5_storage
* sp
, const void *data
, size_t size
)
82 const char *cbuf
= (const char *)data
;
86 /* similar pattern to net_write() to support pipes */
88 count
= fwrite(cbuf
, 1, rem
, F(sp
));
93 * What does it mean to have a short write when using stdio?
95 * It can't mean much. After all stdio is buffering, so
96 * earlier writes that appeared complete may have failed,
97 * and so we don't know how much we really failed to write.
115 stdio_seek(krb5_storage
* sp
, off_t offset
, int whence
)
117 int save_errno
= errno
;
119 if (whence
== SEEK_SET
&& POS(sp
) == offset
)
122 if (whence
== SEEK_CUR
&& POS(sp
) >= 0 && offset
== 0)
125 if (fseeko(F(sp
), offset
, whence
) != 0)
128 return POS(sp
) = ftello(F(sp
));
132 stdio_trunc(krb5_storage
* sp
, off_t offset
)
135 int save_errno
= errno
;
137 if (fflush(F(sp
)) == EOF
)
139 tmpoff
= ftello(F(sp
));
142 if (ftruncate(fileno(F(sp
)), offset
) == -1)
144 if (fseeko(F(sp
), 0, SEEK_END
) == -1)
146 if (fseeko(F(sp
), tmpoff
, SEEK_SET
) == -1)
154 stdio_sync(krb5_storage
* sp
)
156 if (fflush(F(sp
)) == EOF
)
158 if (fsync(fileno(F(sp
))) == -1)
164 stdio_free(krb5_storage
* sp
)
166 int save_errno
= errno
;
168 if (F(sp
) != NULL
&& fclose(F(sp
)) == 0)
174 * Open a krb5_storage using stdio for buffering.
176 * @return A krb5_storage on success, or NULL on out of memory error.
178 * @ingroup krb5_storage
180 * @sa krb5_storage_emem()
181 * @sa krb5_storage_from_fd()
182 * @sa krb5_storage_from_mem()
183 * @sa krb5_storage_from_readonly_mem()
184 * @sa krb5_storage_from_data()
185 * @sa krb5_storage_from_socket()
188 KRB5_LIB_FUNCTION krb5_storage
* KRB5_LIB_CALL
189 krb5_storage_stdio_from_fd(int fd_in
, const char *mode
)
194 int saved_errno
= errno
;
197 off
= lseek(fd_in
, 0, SEEK_CUR
);
203 * This function used to try to pass the input to
204 * _get_osfhandle() to test if the value is a HANDLE
205 * but this doesn't work because doing so throws an
206 * exception that will result in Watson being triggered
207 * to file a Windows Error Report.
217 f
= fdopen(fd
, mode
);
225 if (fseeko(f
, off
, SEEK_SET
) == -1) {
233 sp
= malloc(sizeof(krb5_storage
));
242 sp
->data
= malloc(sizeof(stdio_storage
));
243 if (sp
->data
== NULL
) {
251 sp
->eof_code
= HEIM_ERR_EOF
;
254 sp
->fetch
= stdio_fetch
;
255 sp
->store
= stdio_store
;
256 sp
->seek
= stdio_seek
;
257 sp
->trunc
= stdio_trunc
;
258 sp
->fsync
= stdio_sync
;
259 sp
->free
= stdio_free
;
260 sp
->max_alloc
= UINT_MAX
/8;