2 * Copyright (c) 1997-2008 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"
37 #define BYTEORDER_IS(SP, V) (((SP)->flags & KRB5_STORAGE_BYTEORDER_MASK) == (V))
38 #define BYTEORDER_IS_LE(SP) BYTEORDER_IS((SP), KRB5_STORAGE_BYTEORDER_LE)
39 #define BYTEORDER_IS_BE(SP) BYTEORDER_IS((SP), KRB5_STORAGE_BYTEORDER_BE)
40 #define BYTEORDER_IS_HOST(SP) (BYTEORDER_IS((SP), KRB5_STORAGE_BYTEORDER_HOST) || \
41 krb5_storage_is_flags((SP), KRB5_STORAGE_HOST_BYTEORDER))
44 * Add the flags on a storage buffer by or-ing in the flags to the buffer.
46 * @param sp the storage buffer to set the flags on
47 * @param flags the flags to set
49 * @ingroup krb5_storage
52 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
53 krb5_storage_set_flags(krb5_storage
*sp
, krb5_flags flags
)
59 * Clear the flags on a storage buffer
61 * @param sp the storage buffer to clear the flags on
62 * @param flags the flags to clear
64 * @ingroup krb5_storage
67 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
68 krb5_storage_clear_flags(krb5_storage
*sp
, krb5_flags flags
)
74 * Return true or false depending on if the storage flags is set or
75 * not. NB testing for the flag 0 always return true.
77 * @param sp the storage buffer to check flags on
78 * @param flags The flags to test for
80 * @return true if all the flags are set, false if not.
82 * @ingroup krb5_storage
85 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
86 krb5_storage_is_flags(krb5_storage
*sp
, krb5_flags flags
)
88 return (sp
->flags
& flags
) == flags
;
92 * Set the new byte order of the storage buffer.
94 * @param sp the storage buffer to set the byte order for.
95 * @param byteorder the new byte order.
97 * The byte order are: KRB5_STORAGE_BYTEORDER_BE,
98 * KRB5_STORAGE_BYTEORDER_LE and KRB5_STORAGE_BYTEORDER_HOST.
100 * @ingroup krb5_storage
103 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
104 krb5_storage_set_byteorder(krb5_storage
*sp
, krb5_flags byteorder
)
106 sp
->flags
&= ~KRB5_STORAGE_BYTEORDER_MASK
;
107 sp
->flags
|= byteorder
;
111 * Return the current byteorder for the buffer. See krb5_storage_set_byteorder() for the list or byte order contants.
113 * @ingroup krb5_storage
116 KRB5_LIB_FUNCTION krb5_flags KRB5_LIB_CALL
117 krb5_storage_get_byteorder(krb5_storage
*sp
)
119 return sp
->flags
& KRB5_STORAGE_BYTEORDER_MASK
;
123 * Set the max alloc value
125 * @param sp the storage buffer set the max allow for
126 * @param size maximum size to allocate, use 0 to remove limit
128 * @ingroup krb5_storage
131 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
132 krb5_storage_set_max_alloc(krb5_storage
*sp
, size_t size
)
134 sp
->max_alloc
= size
;
137 /* don't allocate unresonable amount of memory */
138 static krb5_error_code
139 size_too_large(krb5_storage
*sp
, size_t size
)
141 if (sp
->max_alloc
&& sp
->max_alloc
< size
)
142 return HEIM_ERR_TOO_BIG
;
146 static krb5_error_code
147 size_too_large_num(krb5_storage
*sp
, size_t count
, size_t size
)
149 if (sp
->max_alloc
== 0 || size
== 0)
151 size
= sp
->max_alloc
/ size
;
153 return HEIM_ERR_TOO_BIG
;
158 * Seek to a new offset.
160 * @param sp the storage buffer to seek in.
161 * @param offset the offset to seek
162 * @param whence relateive searching, SEEK_CUR from the current
163 * position, SEEK_END from the end, SEEK_SET absolute from the start.
165 * @return The new current offset
167 * @ingroup krb5_storage
170 KRB5_LIB_FUNCTION off_t KRB5_LIB_CALL
171 krb5_storage_seek(krb5_storage
*sp
, off_t offset
, int whence
)
173 return (*sp
->seek
)(sp
, offset
, whence
);
177 * Truncate the storage buffer in sp to offset.
179 * @param sp the storage buffer to truncate.
180 * @param offset the offset to truncate too.
182 * @return An Kerberos 5 error code.
184 * @ingroup krb5_storage
187 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
188 krb5_storage_truncate(krb5_storage
*sp
, off_t offset
)
190 return (*sp
->trunc
)(sp
, offset
);
194 * Sync the storage buffer to its backing store. If there is no
195 * backing store this function will return success.
197 * @param sp the storage buffer to sync
199 * @return A Kerberos 5 error code
201 * @ingroup krb5_storage
204 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
205 krb5_storage_fsync(krb5_storage
*sp
)
207 if (sp
->fsync
!= NULL
)
208 return sp
->fsync(sp
);
213 * Read to the storage buffer.
215 * @param sp the storage buffer to read from
216 * @param buf the buffer to store the data in
217 * @param len the length to read
219 * @return The length of data read (can be shorter then len), or negative on error.
221 * @ingroup krb5_storage
224 KRB5_LIB_FUNCTION krb5_ssize_t KRB5_LIB_CALL
225 krb5_storage_read(krb5_storage
*sp
, void *buf
, size_t len
)
227 return sp
->fetch(sp
, buf
, len
);
231 * Write to the storage buffer.
233 * @param sp the storage buffer to write to
234 * @param buf the buffer to write to the storage buffer
235 * @param len the length to write
237 * @return The length of data written (can be shorter then len), or negative on error.
239 * @ingroup krb5_storage
242 KRB5_LIB_FUNCTION krb5_ssize_t KRB5_LIB_CALL
243 krb5_storage_write(krb5_storage
*sp
, const void *buf
, size_t len
)
245 return sp
->store(sp
, buf
, len
);
249 * Set the return code that will be used when end of storage is reached.
251 * @param sp the storage
252 * @param code the error code to return on end of storage
254 * @ingroup krb5_storage
257 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
258 krb5_storage_set_eof_code(krb5_storage
*sp
, int code
)
264 * Get the return code that will be used when end of storage is reached.
266 * @param sp the storage
268 * @return storage error code
270 * @ingroup krb5_storage
273 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
274 krb5_storage_get_eof_code(krb5_storage
*sp
)
280 * Free a krb5 storage.
282 * @param sp the storage to free.
284 * @return An Kerberos 5 error code.
286 * @ingroup krb5_storage
289 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
290 krb5_storage_free(krb5_storage
*sp
)
302 * Copy the contnent of storage
304 * @param sp the storage to copy to a data
305 * @param data the copied data, free with krb5_data_free()
307 * @return 0 for success, or a Kerberos 5 error code on failure.
309 * @ingroup krb5_storage
312 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
313 krb5_storage_to_data(krb5_storage
*sp
, krb5_data
*data
)
318 pos
= sp
->seek(sp
, 0, SEEK_CUR
);
320 return HEIM_ERR_NOT_SEEKABLE
;
321 size
= sp
->seek(sp
, 0, SEEK_END
);
322 ret
= size_too_large(sp
, size
);
325 ret
= krb5_data_alloc(data
, size
);
327 sp
->seek(sp
, pos
, SEEK_SET
);
331 sp
->seek(sp
, 0, SEEK_SET
);
332 sp
->fetch(sp
, data
->data
, data
->length
);
333 sp
->seek(sp
, pos
, SEEK_SET
);
338 static krb5_error_code
339 krb5_store_int(krb5_storage
*sp
,
348 _krb5_put_int(v
, value
, len
);
349 ret
= sp
->store(sp
, v
, len
);
352 if ((size_t)ret
!= len
)
358 * Store a int32 to storage, byte order is controlled by the settings
359 * on the storage, see krb5_storage_set_byteorder().
361 * @param sp the storage to write too
362 * @param value the value to store
364 * @return 0 for success, or a Kerberos 5 error code on failure.
366 * @ingroup krb5_storage
369 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
370 krb5_store_int32(krb5_storage
*sp
,
373 if(BYTEORDER_IS_HOST(sp
))
374 value
= htonl(value
);
375 else if(BYTEORDER_IS_LE(sp
))
376 value
= bswap32(value
);
377 return krb5_store_int(sp
, value
, 4);
381 * Store a int64 to storage, byte order is controlled by the settings
382 * on the storage, see krb5_storage_set_byteorder().
384 * @param sp the storage to write too
385 * @param value the value to store
387 * @return 0 for success, or a Kerberos 5 error code on failure.
389 * @ingroup krb5_storage
392 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
393 krb5_store_int64(krb5_storage
*sp
,
396 if (BYTEORDER_IS_HOST(sp
))
397 #ifdef WORDS_BIGENDIAN
400 value
= bswap64(value
); /* There's no ntohll() */
402 else if (BYTEORDER_IS_LE(sp
))
403 value
= bswap64(value
);
404 return krb5_store_int(sp
, value
, 8);
408 * Store a uint32 to storage, byte order is controlled by the settings
409 * on the storage, see krb5_storage_set_byteorder().
411 * @param sp the storage to write too
412 * @param value the value to store
414 * @return 0 for success, or a Kerberos 5 error code on failure.
416 * @ingroup krb5_storage
419 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
420 krb5_store_uint32(krb5_storage
*sp
,
423 return krb5_store_int32(sp
, (int32_t)value
);
427 * Store a uint64 to storage, byte order is controlled by the settings
428 * on the storage, see krb5_storage_set_byteorder().
430 * @param sp the storage to write too
431 * @param value the value to store
433 * @return 0 for success, or a Kerberos 5 error code on failure.
435 * @ingroup krb5_storage
438 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
439 krb5_store_uint64(krb5_storage
*sp
,
442 return krb5_store_int64(sp
, (int64_t)value
);
445 static krb5_error_code
446 krb5_ret_int(krb5_storage
*sp
,
453 *value
= 0; /* quiets warnings */
454 ret
= sp
->fetch(sp
, v
, len
);
457 if ((size_t)ret
!= len
)
459 _krb5_get_int64(v
, &w
, len
);
465 * Read a int64 from storage, byte order is controlled by the settings
466 * on the storage, see krb5_storage_set_byteorder().
468 * @param sp the storage to write too
469 * @param value the value read from the buffer
471 * @return 0 for success, or a Kerberos 5 error code on failure.
473 * @ingroup krb5_storage
476 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
477 krb5_ret_int64(krb5_storage
*sp
,
480 krb5_error_code ret
= krb5_ret_int(sp
, value
, 8);
483 if(BYTEORDER_IS_HOST(sp
))
484 #ifdef WORDS_BIGENDIAN
487 *value
= bswap64(*value
); /* There's no ntohll() */
489 else if(BYTEORDER_IS_LE(sp
))
490 *value
= bswap64(*value
);
495 * Read a uint64 from storage, byte order is controlled by the settings
496 * on the storage, see krb5_storage_set_byteorder().
498 * @param sp the storage to write too
499 * @param value the value read from the buffer
501 * @return 0 for success, or a Kerberos 5 error code on failure.
503 * @ingroup krb5_storage
506 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
507 krb5_ret_uint64(krb5_storage
*sp
,
513 ret
= krb5_ret_int64(sp
, &v
);
515 *value
= (uint64_t)v
;
521 * Read a int32 from storage, byte order is controlled by the settings
522 * on the storage, see krb5_storage_set_byteorder().
524 * @param sp the storage to write too
525 * @param value the value read from the buffer
527 * @return 0 for success, or a Kerberos 5 error code on failure.
529 * @ingroup krb5_storage
532 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
533 krb5_ret_int32(krb5_storage
*sp
,
538 krb5_error_code ret
= krb5_ret_int(sp
, &v
, 4);
542 if (BYTEORDER_IS_HOST(sp
))
543 *value
= htonl(*value
);
544 else if (BYTEORDER_IS_LE(sp
))
545 *value
= bswap32(*value
);
550 * Read a uint32 from storage, byte order is controlled by the settings
551 * on the storage, see krb5_storage_set_byteorder().
553 * @param sp the storage to write too
554 * @param value the value read from the buffer
556 * @return 0 for success, or a Kerberos 5 error code on failure.
558 * @ingroup krb5_storage
561 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
562 krb5_ret_uint32(krb5_storage
*sp
, uint32_t *value
)
567 ret
= krb5_ret_int32(sp
, &v
);
569 *value
= (uint32_t)v
;
575 * Store a int16 to storage, byte order is controlled by the settings
576 * on the storage, see krb5_storage_set_byteorder().
578 * @param sp the storage to write too
579 * @param value the value to store
581 * @return 0 for success, or a Kerberos 5 error code on failure.
583 * @ingroup krb5_storage
586 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
587 krb5_store_int16(krb5_storage
*sp
,
590 if(BYTEORDER_IS_HOST(sp
))
591 value
= htons(value
);
592 else if(BYTEORDER_IS_LE(sp
))
593 value
= bswap16(value
);
594 return krb5_store_int(sp
, value
, 2);
598 * Store a uint16 to storage, byte order is controlled by the settings
599 * on the storage, see krb5_storage_set_byteorder().
601 * @param sp the storage to write too
602 * @param value the value to store
604 * @return 0 for success, or a Kerberos 5 error code on failure.
606 * @ingroup krb5_storage
609 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
610 krb5_store_uint16(krb5_storage
*sp
,
613 return krb5_store_int16(sp
, (int16_t)value
);
617 * Read a int16 from storage, byte order is controlled by the settings
618 * on the storage, see krb5_storage_set_byteorder().
620 * @param sp the storage to write too
621 * @param value the value read from the buffer
623 * @return 0 for success, or a Kerberos 5 error code on failure.
625 * @ingroup krb5_storage
628 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
629 krb5_ret_int16(krb5_storage
*sp
,
634 ret
= krb5_ret_int(sp
, &v
, 2);
638 if(BYTEORDER_IS_HOST(sp
))
639 *value
= htons(*value
);
640 else if(BYTEORDER_IS_LE(sp
))
641 *value
= bswap16(*value
);
646 * Read a int16 from storage, byte order is controlled by the settings
647 * on the storage, see krb5_storage_set_byteorder().
649 * @param sp the storage to write too
650 * @param value the value read from the buffer
652 * @return 0 for success, or a Kerberos 5 error code on failure.
654 * @ingroup krb5_storage
657 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
658 krb5_ret_uint16(krb5_storage
*sp
,
664 ret
= krb5_ret_int16(sp
, &v
);
666 *value
= (uint16_t)v
;
672 * Store a int8 to storage.
674 * @param sp the storage to write too
675 * @param value the value to store
677 * @return 0 for success, or a Kerberos 5 error code on failure.
679 * @ingroup krb5_storage
682 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
683 krb5_store_int8(krb5_storage
*sp
,
688 ret
= sp
->store(sp
, &value
, sizeof(value
));
689 if (ret
!= sizeof(value
))
690 return (ret
<0)?errno
:sp
->eof_code
;
695 * Store a uint8 to storage.
697 * @param sp the storage to write too
698 * @param value the value to store
700 * @return 0 for success, or a Kerberos 5 error code on failure.
702 * @ingroup krb5_storage
705 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
706 krb5_store_uint8(krb5_storage
*sp
,
709 return krb5_store_int8(sp
, (int8_t)value
);
713 * Read a int8 from storage
715 * @param sp the storage to write too
716 * @param value the value read from the buffer
718 * @return 0 for success, or a Kerberos 5 error code on failure.
720 * @ingroup krb5_storage
723 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
724 krb5_ret_int8(krb5_storage
*sp
,
729 ret
= sp
->fetch(sp
, value
, sizeof(*value
));
730 if (ret
!= sizeof(*value
))
731 return (ret
<0)?errno
:sp
->eof_code
;
736 * Read a uint8 from storage
738 * @param sp the storage to write too
739 * @param value the value read from the buffer
741 * @return 0 for success, or a Kerberos 5 error code on failure.
743 * @ingroup krb5_storage
746 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
747 krb5_ret_uint8(krb5_storage
*sp
,
753 ret
= krb5_ret_int8(sp
, &v
);
761 * Store a data to the storage. The data is stored with an int32 as
762 * lenght plus the data (not padded).
764 * @param sp the storage buffer to write to
765 * @param data the buffer to store.
767 * @return 0 on success, a Kerberos 5 error code on failure.
769 * @ingroup krb5_storage
772 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
773 krb5_store_data(krb5_storage
*sp
,
777 ret
= krb5_store_int32(sp
, data
.length
);
780 ret
= sp
->store(sp
, data
.data
, data
.length
);
783 if((size_t)ret
!= data
.length
)
789 * Parse a data from the storage.
791 * @param sp the storage buffer to read from
792 * @param data the parsed data
794 * @return 0 on success, a Kerberos 5 error code on failure.
796 * @ingroup krb5_storage
799 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
800 krb5_ret_data(krb5_storage
*sp
,
806 ret
= krb5_ret_int32(sp
, &size
);
809 ret
= size_too_large(sp
, size
);
812 ret
= krb5_data_alloc (data
, size
);
816 ret
= sp
->fetch(sp
, data
->data
, size
);
818 krb5_data_free(data
);
819 return (ret
< 0)? errno
: sp
->eof_code
;
826 * Store a string to the buffer. The data is formated as an len:uint32
827 * plus the string itself (not padded).
829 * @param sp the storage buffer to write to
830 * @param s the string to store.
832 * @return 0 on success, a Kerberos 5 error code on failure.
834 * @ingroup krb5_storage
837 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
838 krb5_store_string(krb5_storage
*sp
, const char *s
)
841 data
.length
= strlen(s
);
842 data
.data
= rk_UNCONST(s
);
843 return krb5_store_data(sp
, data
);
847 * Parse a string from the storage.
849 * @param sp the storage buffer to read from
850 * @param string the parsed string
852 * @return 0 on success, a Kerberos 5 error code on failure.
854 * @ingroup krb5_storage
858 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
859 krb5_ret_string(krb5_storage
*sp
,
864 ret
= krb5_ret_data(sp
, &data
);
867 *string
= realloc(data
.data
, data
.length
+ 1);
872 (*string
)[data
.length
] = 0;
877 * Store a zero terminated string to the buffer. The data is stored
878 * one character at a time until a NUL is stored.
880 * @param sp the storage buffer to write to
881 * @param s the string to store.
883 * @return 0 on success, a Kerberos 5 error code on failure.
885 * @ingroup krb5_storage
888 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
889 krb5_store_stringz(krb5_storage
*sp
, const char *s
)
891 size_t len
= strlen(s
) + 1;
894 ret
= sp
->store(sp
, s
, len
);
897 if((size_t)ret
!= len
)
903 * Parse zero terminated string from the storage.
905 * @param sp the storage buffer to read from
906 * @param string the parsed string
908 * @return 0 on success, a Kerberos 5 error code on failure.
910 * @ingroup krb5_storage
913 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
914 krb5_ret_stringz(krb5_storage
*sp
,
922 while((ret
= sp
->fetch(sp
, &c
, 1)) == 1){
923 krb5_error_code eret
;
927 eret
= size_too_large(sp
, len
);
932 tmp
= realloc (s
, len
);
952 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
953 krb5_store_stringnl(krb5_storage
*sp
, const char *s
)
955 size_t len
= strlen(s
);
958 ret
= sp
->store(sp
, s
, len
);
961 if((size_t)ret
!= len
)
963 ret
= sp
->store(sp
, "\n", 1);
975 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
976 krb5_ret_stringnl(krb5_storage
*sp
,
985 while((ret
= sp
->fetch(sp
, &c
, 1)) == 1){
986 krb5_error_code eret
;
993 if (expect_nl
&& c
!= '\n') {
995 return KRB5_BADMSGTYPE
;
999 eret
= size_too_large(sp
, len
);
1004 tmp
= realloc (s
, len
);
1019 return sp
->eof_code
;
1027 * Write a principal block to storage.
1029 * @param sp the storage buffer to write to
1030 * @param p the principal block to write.
1032 * @return 0 on success, a Kerberos 5 error code on failure.
1034 * @ingroup krb5_storage
1037 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1038 krb5_store_principal(krb5_storage
*sp
,
1039 krb5_const_principal p
)
1044 if(!krb5_storage_is_flags(sp
, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
)) {
1045 ret
= krb5_store_int32(sp
, p
->name
.name_type
);
1048 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
))
1049 ret
= krb5_store_int32(sp
, p
->name
.name_string
.len
+ 1);
1051 ret
= krb5_store_int32(sp
, p
->name
.name_string
.len
);
1054 ret
= krb5_store_string(sp
, p
->realm
);
1056 for(i
= 0; i
< p
->name
.name_string
.len
; i
++){
1057 ret
= krb5_store_string(sp
, p
->name
.name_string
.val
[i
]);
1064 * Parse principal from the storage.
1066 * @param sp the storage buffer to read from
1067 * @param princ the parsed principal
1069 * @return 0 on success, a Kerberos 5 error code on failure.
1071 * @ingroup krb5_storage
1074 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1075 krb5_ret_principal(krb5_storage
*sp
,
1076 krb5_principal
*princ
)
1084 p
= calloc(1, sizeof(*p
));
1088 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE
))
1089 type
= KRB5_NT_UNKNOWN
;
1090 else if((ret
= krb5_ret_int32(sp
, &type
))){
1094 if((ret
= krb5_ret_int32(sp
, &ncomp
))){
1098 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS
))
1104 ret
= size_too_large_num(sp
, ncomp
, sizeof(p
->name
.name_string
.val
[0]));
1109 p
->name
.name_type
= type
;
1110 p
->name
.name_string
.len
= ncomp
;
1111 ret
= krb5_ret_string(sp
, &p
->realm
);
1116 p
->name
.name_string
.val
= calloc(ncomp
, sizeof(p
->name
.name_string
.val
[0]));
1117 if(p
->name
.name_string
.val
== NULL
&& ncomp
!= 0){
1122 for(i
= 0; i
< ncomp
; i
++){
1123 ret
= krb5_ret_string(sp
, &p
->name
.name_string
.val
[i
]);
1126 free(p
->name
.name_string
.val
[i
--]);
1137 * Store a keyblock to the storage.
1139 * @param sp the storage buffer to write to
1140 * @param p the keyblock to write
1142 * @return 0 on success, a Kerberos 5 error code on failure.
1144 * @ingroup krb5_storage
1147 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1148 krb5_store_keyblock(krb5_storage
*sp
, krb5_keyblock p
)
1151 ret
= krb5_store_int16(sp
, p
.keytype
);
1154 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
)){
1155 /* this should really be enctype, but it is the same as
1157 ret
= krb5_store_int16(sp
, p
.keytype
);
1161 ret
= krb5_store_data(sp
, p
.keyvalue
);
1166 * Read a keyblock from the storage.
1168 * @param sp the storage buffer to write to
1169 * @param p the keyblock read from storage, free using krb5_free_keyblock()
1171 * @return 0 on success, a Kerberos 5 error code on failure.
1173 * @ingroup krb5_storage
1176 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1177 krb5_ret_keyblock(krb5_storage
*sp
, krb5_keyblock
*p
)
1182 ret
= krb5_ret_int16(sp
, &tmp
);
1186 if(krb5_storage_is_flags(sp
, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE
)){
1187 ret
= krb5_ret_int16(sp
, &tmp
);
1191 ret
= krb5_ret_data(sp
, &p
->keyvalue
);
1196 * Write a times block to storage.
1198 * @param sp the storage buffer to write to
1199 * @param times the times block to write.
1201 * @return 0 on success, a Kerberos 5 error code on failure.
1203 * @ingroup krb5_storage
1206 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1207 krb5_store_times(krb5_storage
*sp
, krb5_times times
)
1210 ret
= krb5_store_int32(sp
, times
.authtime
);
1212 ret
= krb5_store_int32(sp
, times
.starttime
);
1214 ret
= krb5_store_int32(sp
, times
.endtime
);
1216 ret
= krb5_store_int32(sp
, times
.renew_till
);
1221 * Read a times block from the storage.
1223 * @param sp the storage buffer to write to
1224 * @param times the times block read from storage
1226 * @return 0 on success, a Kerberos 5 error code on failure.
1228 * @ingroup krb5_storage
1231 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1232 krb5_ret_times(krb5_storage
*sp
, krb5_times
*times
)
1236 ret
= krb5_ret_int32(sp
, &tmp
);
1237 times
->authtime
= tmp
;
1239 ret
= krb5_ret_int32(sp
, &tmp
);
1240 times
->starttime
= tmp
;
1242 ret
= krb5_ret_int32(sp
, &tmp
);
1243 times
->endtime
= tmp
;
1245 ret
= krb5_ret_int32(sp
, &tmp
);
1246 times
->renew_till
= tmp
;
1251 * Write a address block to storage.
1253 * @param sp the storage buffer to write to
1254 * @param p the address block to write.
1256 * @return 0 on success, a Kerberos 5 error code on failure.
1258 * @ingroup krb5_storage
1261 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1262 krb5_store_address(krb5_storage
*sp
, krb5_address p
)
1265 ret
= krb5_store_int16(sp
, p
.addr_type
);
1267 ret
= krb5_store_data(sp
, p
.address
);
1272 * Read a address block from the storage.
1274 * @param sp the storage buffer to write to
1275 * @param adr the address block read from storage
1277 * @return 0 on success, a Kerberos 5 error code on failure.
1279 * @ingroup krb5_storage
1282 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1283 krb5_ret_address(krb5_storage
*sp
, krb5_address
*adr
)
1287 ret
= krb5_ret_int16(sp
, &t
);
1290 ret
= krb5_ret_data(sp
, &adr
->address
);
1295 * Write a addresses block to storage.
1297 * @param sp the storage buffer to write to
1298 * @param p the addresses block to write.
1300 * @return 0 on success, a Kerberos 5 error code on failure.
1302 * @ingroup krb5_storage
1305 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1306 krb5_store_addrs(krb5_storage
*sp
, krb5_addresses p
)
1310 ret
= krb5_store_int32(sp
, p
.len
);
1312 for(i
= 0; i
<p
.len
; i
++){
1313 ret
= krb5_store_address(sp
, p
.val
[i
]);
1320 * Read a addresses block from the storage.
1322 * @param sp the storage buffer to write to
1323 * @param adr the addresses block read from storage
1325 * @return 0 on success, a Kerberos 5 error code on failure.
1327 * @ingroup krb5_storage
1330 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1331 krb5_ret_addrs(krb5_storage
*sp
, krb5_addresses
*adr
)
1337 ret
= krb5_ret_int32(sp
, &tmp
);
1339 ret
= size_too_large_num(sp
, tmp
, sizeof(adr
->val
[0]));
1340 if (ret
) return ret
;
1342 ALLOC(adr
->val
, adr
->len
);
1343 if (adr
->val
== NULL
&& adr
->len
!= 0)
1345 for(i
= 0; i
< adr
->len
; i
++){
1346 ret
= krb5_ret_address(sp
, &adr
->val
[i
]);
1353 * Write a auth data block to storage.
1355 * @param sp the storage buffer to write to
1356 * @param auth the auth data block to write.
1358 * @return 0 on success, a Kerberos 5 error code on failure.
1360 * @ingroup krb5_storage
1363 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1364 krb5_store_authdata(krb5_storage
*sp
, krb5_authdata auth
)
1366 krb5_error_code ret
;
1368 ret
= krb5_store_int32(sp
, auth
.len
);
1370 for(i
= 0; i
< auth
.len
; i
++){
1371 ret
= krb5_store_int16(sp
, auth
.val
[i
].ad_type
);
1373 ret
= krb5_store_data(sp
, auth
.val
[i
].ad_data
);
1380 * Read a auth data from the storage.
1382 * @param sp the storage buffer to write to
1383 * @param auth the auth data block read from storage
1385 * @return 0 on success, a Kerberos 5 error code on failure.
1387 * @ingroup krb5_storage
1390 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1391 krb5_ret_authdata(krb5_storage
*sp
, krb5_authdata
*auth
)
1393 krb5_error_code ret
;
1397 ret
= krb5_ret_int32(sp
, &tmp
);
1399 ret
= size_too_large_num(sp
, tmp
, sizeof(auth
->val
[0]));
1400 if (ret
) return ret
;
1401 ALLOC_SEQ(auth
, tmp
);
1402 if (auth
->val
== NULL
&& tmp
!= 0)
1404 for(i
= 0; i
< tmp
; i
++){
1405 ret
= krb5_ret_int16(sp
, &tmp2
);
1407 auth
->val
[i
].ad_type
= tmp2
;
1408 ret
= krb5_ret_data(sp
, &auth
->val
[i
].ad_data
);
1415 bitswap32(int32_t b
)
1419 for (i
= 0; i
< 32; i
++) {
1420 r
= r
<< 1 | (b
& 1);
1427 * Write a credentials block to storage.
1429 * @param sp the storage buffer to write to
1430 * @param creds the creds block to write.
1432 * @return 0 on success, a Kerberos 5 error code on failure.
1434 * @ingroup krb5_storage
1437 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1438 krb5_store_creds(krb5_storage
*sp
, krb5_creds
*creds
)
1442 ret
= krb5_store_principal(sp
, creds
->client
);
1445 ret
= krb5_store_principal(sp
, creds
->server
);
1448 ret
= krb5_store_keyblock(sp
, creds
->session
);
1451 ret
= krb5_store_times(sp
, creds
->times
);
1454 ret
= krb5_store_int8(sp
, creds
->second_ticket
.length
!= 0); /* is_skey */
1457 ret
= krb5_store_int32(sp
, bitswap32(TicketFlags2int(creds
->flags
.b
)));
1460 ret
= krb5_store_addrs(sp
, creds
->addresses
);
1463 ret
= krb5_store_authdata(sp
, creds
->authdata
);
1466 ret
= krb5_store_data(sp
, creds
->ticket
);
1469 ret
= krb5_store_data(sp
, creds
->second_ticket
);
1474 * Read a credentials block from the storage.
1476 * @param sp the storage buffer to write to
1477 * @param creds the credentials block read from storage
1479 * @return 0 on success, a Kerberos 5 error code on failure.
1481 * @ingroup krb5_storage
1484 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1485 krb5_ret_creds(krb5_storage
*sp
, krb5_creds
*creds
)
1487 krb5_error_code ret
;
1491 memset(creds
, 0, sizeof(*creds
));
1492 ret
= krb5_ret_principal (sp
, &creds
->client
);
1493 if(ret
) goto cleanup
;
1494 ret
= krb5_ret_principal (sp
, &creds
->server
);
1495 if(ret
) goto cleanup
;
1496 ret
= krb5_ret_keyblock (sp
, &creds
->session
);
1497 if(ret
) goto cleanup
;
1498 ret
= krb5_ret_times (sp
, &creds
->times
);
1499 if(ret
) goto cleanup
;
1500 ret
= krb5_ret_int8 (sp
, &dummy8
);
1501 if(ret
) goto cleanup
;
1502 ret
= krb5_ret_int32 (sp
, &dummy32
);
1503 if(ret
) goto cleanup
;
1504 creds
->flags
.b
= int2TicketFlags(bitswap32(dummy32
));
1505 ret
= krb5_ret_addrs (sp
, &creds
->addresses
);
1506 if(ret
) goto cleanup
;
1507 ret
= krb5_ret_authdata (sp
, &creds
->authdata
);
1508 if(ret
) goto cleanup
;
1509 ret
= krb5_ret_data (sp
, &creds
->ticket
);
1510 if(ret
) goto cleanup
;
1511 ret
= krb5_ret_data (sp
, &creds
->second_ticket
);
1515 krb5_free_cred_contents(context
, creds
); /* XXX */
1521 #define SC_CLIENT_PRINCIPAL 0x0001
1522 #define SC_SERVER_PRINCIPAL 0x0002
1523 #define SC_SESSION_KEY 0x0004
1524 #define SC_TICKET 0x0008
1525 #define SC_SECOND_TICKET 0x0010
1526 #define SC_AUTHDATA 0x0020
1527 #define SC_ADDRESSES 0x0040
1530 * Write a tagged credentials block to storage.
1532 * @param sp the storage buffer to write to
1533 * @param creds the creds block to write.
1535 * @return 0 on success, a Kerberos 5 error code on failure.
1537 * @ingroup krb5_storage
1540 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1541 krb5_store_creds_tag(krb5_storage
*sp
, krb5_creds
*creds
)
1547 header
|= SC_CLIENT_PRINCIPAL
;
1549 header
|= SC_SERVER_PRINCIPAL
;
1550 if (creds
->session
.keytype
!= ETYPE_NULL
)
1551 header
|= SC_SESSION_KEY
;
1552 if (creds
->ticket
.data
)
1553 header
|= SC_TICKET
;
1554 if (creds
->second_ticket
.length
)
1555 header
|= SC_SECOND_TICKET
;
1556 if (creds
->authdata
.len
)
1557 header
|= SC_AUTHDATA
;
1558 if (creds
->addresses
.len
)
1559 header
|= SC_ADDRESSES
;
1561 ret
= krb5_store_int32(sp
, header
);
1565 if (creds
->client
) {
1566 ret
= krb5_store_principal(sp
, creds
->client
);
1571 if (creds
->server
) {
1572 ret
= krb5_store_principal(sp
, creds
->server
);
1577 if (creds
->session
.keytype
!= ETYPE_NULL
) {
1578 ret
= krb5_store_keyblock(sp
, creds
->session
);
1583 ret
= krb5_store_times(sp
, creds
->times
);
1586 ret
= krb5_store_int8(sp
, creds
->second_ticket
.length
!= 0); /* is_skey */
1590 ret
= krb5_store_int32(sp
, bitswap32(TicketFlags2int(creds
->flags
.b
)));
1594 if (creds
->addresses
.len
) {
1595 ret
= krb5_store_addrs(sp
, creds
->addresses
);
1600 if (creds
->authdata
.len
) {
1601 ret
= krb5_store_authdata(sp
, creds
->authdata
);
1606 if (creds
->ticket
.data
) {
1607 ret
= krb5_store_data(sp
, creds
->ticket
);
1612 if (creds
->second_ticket
.data
) {
1613 ret
= krb5_store_data(sp
, creds
->second_ticket
);
1622 * Read a tagged credentials block from the storage.
1624 * @param sp the storage buffer to write to
1625 * @param creds the credentials block read from storage
1627 * @return 0 on success, a Kerberos 5 error code on failure.
1629 * @ingroup krb5_storage
1632 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1633 krb5_ret_creds_tag(krb5_storage
*sp
,
1636 krb5_error_code ret
;
1638 int32_t dummy32
, header
;
1640 memset(creds
, 0, sizeof(*creds
));
1642 ret
= krb5_ret_int32 (sp
, &header
);
1643 if (ret
) goto cleanup
;
1645 if (header
& SC_CLIENT_PRINCIPAL
) {
1646 ret
= krb5_ret_principal (sp
, &creds
->client
);
1647 if(ret
) goto cleanup
;
1649 if (header
& SC_SERVER_PRINCIPAL
) {
1650 ret
= krb5_ret_principal (sp
, &creds
->server
);
1651 if(ret
) goto cleanup
;
1653 if (header
& SC_SESSION_KEY
) {
1654 ret
= krb5_ret_keyblock (sp
, &creds
->session
);
1655 if(ret
) goto cleanup
;
1657 ret
= krb5_ret_times (sp
, &creds
->times
);
1658 if(ret
) goto cleanup
;
1659 ret
= krb5_ret_int8 (sp
, &dummy8
);
1660 if(ret
) goto cleanup
;
1661 ret
= krb5_ret_int32 (sp
, &dummy32
);
1662 if(ret
) goto cleanup
;
1663 creds
->flags
.b
= int2TicketFlags(bitswap32(dummy32
));
1664 if (header
& SC_ADDRESSES
) {
1665 ret
= krb5_ret_addrs (sp
, &creds
->addresses
);
1666 if(ret
) goto cleanup
;
1668 if (header
& SC_AUTHDATA
) {
1669 ret
= krb5_ret_authdata (sp
, &creds
->authdata
);
1670 if(ret
) goto cleanup
;
1672 if (header
& SC_TICKET
) {
1673 ret
= krb5_ret_data (sp
, &creds
->ticket
);
1674 if(ret
) goto cleanup
;
1676 if (header
& SC_SECOND_TICKET
) {
1677 ret
= krb5_ret_data (sp
, &creds
->second_ticket
);
1678 if(ret
) goto cleanup
;
1684 krb5_free_cred_contents(context
, creds
); /* XXX */