Move kadmin and ktutil to /usr/bin.
[heimdal.git] / lib / krb5 / store.c
blob21536f7b4587a59f63714cb172228fe3d0fb605b
1 /*
2 * Copyright (c) 1997-2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
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))
43 /**
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)
55 sp->flags |= flags;
58 /**
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)
70 sp->flags &= ~flags;
73 /**
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;
91 /**
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;
143 return 0;
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)
150 return 0;
151 size = sp->max_alloc / size;
152 if (size < count)
153 return HEIM_ERR_TOO_BIG;
154 return 0;
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);
209 return 0;
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)
260 sp->eof_code = 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)
276 return sp->eof_code;
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)
292 if(sp->free)
293 (*sp->free)(sp);
294 free(sp->data);
295 free(sp);
296 return 0;
300 * Copy the contnent of storage
302 * @param sp the storage to copy to a data
303 * @param data the copied data, free with krb5_data_free()
305 * @return 0 for success, or a Kerberos 5 error code on failure.
307 * @ingroup krb5_storage
310 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
311 krb5_storage_to_data(krb5_storage *sp, krb5_data *data)
313 off_t pos, size;
314 krb5_error_code ret;
316 pos = sp->seek(sp, 0, SEEK_CUR);
317 if (pos < 0)
318 return HEIM_ERR_NOT_SEEKABLE;
319 size = sp->seek(sp, 0, SEEK_END);
320 ret = size_too_large(sp, size);
321 if (ret)
322 return ret;
323 ret = krb5_data_alloc(data, size);
324 if (ret) {
325 sp->seek(sp, pos, SEEK_SET);
326 return ret;
328 if (size) {
329 sp->seek(sp, 0, SEEK_SET);
330 sp->fetch(sp, data->data, data->length);
331 sp->seek(sp, pos, SEEK_SET);
333 return 0;
336 static krb5_error_code
337 krb5_store_int(krb5_storage *sp,
338 int32_t value,
339 size_t len)
341 int ret;
342 unsigned char v[16];
344 if(len > sizeof(v))
345 return EINVAL;
346 _krb5_put_int(v, value, len);
347 ret = sp->store(sp, v, len);
348 if (ret < 0)
349 return errno;
350 if ((size_t)ret != len)
351 return sp->eof_code;
352 return 0;
356 * Store a int32 to storage, byte order is controlled by the settings
357 * on the storage, see krb5_storage_set_byteorder().
359 * @param sp the storage to write too
360 * @param value the value to store
362 * @return 0 for success, or a Kerberos 5 error code on failure.
364 * @ingroup krb5_storage
367 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
368 krb5_store_int32(krb5_storage *sp,
369 int32_t value)
371 if(BYTEORDER_IS_HOST(sp))
372 value = htonl(value);
373 else if(BYTEORDER_IS_LE(sp))
374 value = bswap32(value);
375 return krb5_store_int(sp, value, 4);
379 * Store a uint32 to storage, byte order is controlled by the settings
380 * on the storage, see krb5_storage_set_byteorder().
382 * @param sp the storage to write too
383 * @param value the value to store
385 * @return 0 for success, or a Kerberos 5 error code on failure.
387 * @ingroup krb5_storage
390 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
391 krb5_store_uint32(krb5_storage *sp,
392 uint32_t value)
394 return krb5_store_int32(sp, (int32_t)value);
397 static krb5_error_code
398 krb5_ret_int(krb5_storage *sp,
399 int32_t *value,
400 size_t len)
402 int ret;
403 unsigned char v[4];
404 unsigned long w;
405 ret = sp->fetch(sp, v, len);
406 if (ret < 0)
407 return errno;
408 if ((size_t)ret != len)
409 return sp->eof_code;
410 _krb5_get_int(v, &w, len);
411 *value = w;
412 return 0;
416 * Read a int32 from storage, byte order is controlled by the settings
417 * on the storage, see krb5_storage_set_byteorder().
419 * @param sp the storage to write too
420 * @param value the value read from the buffer
422 * @return 0 for success, or a Kerberos 5 error code on failure.
424 * @ingroup krb5_storage
427 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
428 krb5_ret_int32(krb5_storage *sp,
429 int32_t *value)
431 krb5_error_code ret = krb5_ret_int(sp, value, 4);
432 if(ret)
433 return ret;
434 if(BYTEORDER_IS_HOST(sp))
435 *value = htonl(*value);
436 else if(BYTEORDER_IS_LE(sp))
437 *value = bswap32(*value);
438 return 0;
442 * Read a uint32 from storage, byte order is controlled by the settings
443 * on the storage, see krb5_storage_set_byteorder().
445 * @param sp the storage to write too
446 * @param value the value read from the buffer
448 * @return 0 for success, or a Kerberos 5 error code on failure.
450 * @ingroup krb5_storage
453 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
454 krb5_ret_uint32(krb5_storage *sp,
455 uint32_t *value)
457 krb5_error_code ret;
458 int32_t v;
460 ret = krb5_ret_int32(sp, &v);
461 if (ret == 0)
462 *value = (uint32_t)v;
464 return ret;
468 * Store a int16 to storage, byte order is controlled by the settings
469 * on the storage, see krb5_storage_set_byteorder().
471 * @param sp the storage to write too
472 * @param value the value to store
474 * @return 0 for success, or a Kerberos 5 error code on failure.
476 * @ingroup krb5_storage
479 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
480 krb5_store_int16(krb5_storage *sp,
481 int16_t value)
483 if(BYTEORDER_IS_HOST(sp))
484 value = htons(value);
485 else if(BYTEORDER_IS_LE(sp))
486 value = bswap16(value);
487 return krb5_store_int(sp, value, 2);
491 * Store a uint16 to storage, byte order is controlled by the settings
492 * on the storage, see krb5_storage_set_byteorder().
494 * @param sp the storage to write too
495 * @param value the value to store
497 * @return 0 for success, or a Kerberos 5 error code on failure.
499 * @ingroup krb5_storage
502 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
503 krb5_store_uint16(krb5_storage *sp,
504 uint16_t value)
506 return krb5_store_int16(sp, (int16_t)value);
510 * Read a int16 from storage, byte order is controlled by the settings
511 * on the storage, see krb5_storage_set_byteorder().
513 * @param sp the storage to write too
514 * @param value the value read from the buffer
516 * @return 0 for success, or a Kerberos 5 error code on failure.
518 * @ingroup krb5_storage
521 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
522 krb5_ret_int16(krb5_storage *sp,
523 int16_t *value)
525 int32_t v;
526 int ret;
527 ret = krb5_ret_int(sp, &v, 2);
528 if(ret)
529 return ret;
530 *value = v;
531 if(BYTEORDER_IS_HOST(sp))
532 *value = htons(*value);
533 else if(BYTEORDER_IS_LE(sp))
534 *value = bswap16(*value);
535 return 0;
539 * Read a int16 from storage, byte order is controlled by the settings
540 * on the storage, see krb5_storage_set_byteorder().
542 * @param sp the storage to write too
543 * @param value the value read from the buffer
545 * @return 0 for success, or a Kerberos 5 error code on failure.
547 * @ingroup krb5_storage
550 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
551 krb5_ret_uint16(krb5_storage *sp,
552 uint16_t *value)
554 krb5_error_code ret;
555 int16_t v;
557 ret = krb5_ret_int16(sp, &v);
558 if (ret == 0)
559 *value = (uint16_t)v;
561 return ret;
565 * Store a int8 to storage.
567 * @param sp the storage to write too
568 * @param value the value to store
570 * @return 0 for success, or a Kerberos 5 error code on failure.
572 * @ingroup krb5_storage
575 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
576 krb5_store_int8(krb5_storage *sp,
577 int8_t value)
579 int ret;
581 ret = sp->store(sp, &value, sizeof(value));
582 if (ret != sizeof(value))
583 return (ret<0)?errno:sp->eof_code;
584 return 0;
588 * Store a uint8 to storage.
590 * @param sp the storage to write too
591 * @param value the value to store
593 * @return 0 for success, or a Kerberos 5 error code on failure.
595 * @ingroup krb5_storage
598 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
599 krb5_store_uint8(krb5_storage *sp,
600 uint8_t value)
602 return krb5_store_int8(sp, (int8_t)value);
606 * Read a int8 from storage
608 * @param sp the storage to write too
609 * @param value the value read from the buffer
611 * @return 0 for success, or a Kerberos 5 error code on failure.
613 * @ingroup krb5_storage
616 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
617 krb5_ret_int8(krb5_storage *sp,
618 int8_t *value)
620 int ret;
622 ret = sp->fetch(sp, value, sizeof(*value));
623 if (ret != sizeof(*value))
624 return (ret<0)?errno:sp->eof_code;
625 return 0;
629 * Read a uint8 from storage
631 * @param sp the storage to write too
632 * @param value the value read from the buffer
634 * @return 0 for success, or a Kerberos 5 error code on failure.
636 * @ingroup krb5_storage
639 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
640 krb5_ret_uint8(krb5_storage *sp,
641 uint8_t *value)
643 krb5_error_code ret;
644 int8_t v;
646 ret = krb5_ret_int8(sp, &v);
647 if (ret == 0)
648 *value = (uint8_t)v;
650 return ret;
654 * Store a data to the storage. The data is stored with an int32 as
655 * lenght plus the data (not padded).
657 * @param sp the storage buffer to write to
658 * @param data the buffer to store.
660 * @return 0 on success, a Kerberos 5 error code on failure.
662 * @ingroup krb5_storage
665 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
666 krb5_store_data(krb5_storage *sp,
667 krb5_data data)
669 int ret;
670 ret = krb5_store_int32(sp, data.length);
671 if(ret < 0)
672 return ret;
673 ret = sp->store(sp, data.data, data.length);
674 if(ret < 0)
675 return errno;
676 if((size_t)ret != data.length)
677 return sp->eof_code;
678 return 0;
682 * Parse a data from the storage.
684 * @param sp the storage buffer to read from
685 * @param data the parsed data
687 * @return 0 on success, a Kerberos 5 error code on failure.
689 * @ingroup krb5_storage
692 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
693 krb5_ret_data(krb5_storage *sp,
694 krb5_data *data)
696 int ret;
697 int32_t size;
699 ret = krb5_ret_int32(sp, &size);
700 if(ret)
701 return ret;
702 ret = size_too_large(sp, size);
703 if (ret)
704 return ret;
705 ret = krb5_data_alloc (data, size);
706 if (ret)
707 return ret;
708 if (size) {
709 ret = sp->fetch(sp, data->data, size);
710 if(ret != size) {
711 krb5_data_free(data);
712 return (ret < 0)? errno : sp->eof_code;
715 return 0;
719 * Store a string to the buffer. The data is formated as an len:uint32
720 * plus the string itself (not padded).
722 * @param sp the storage buffer to write to
723 * @param s the string to store.
725 * @return 0 on success, a Kerberos 5 error code on failure.
727 * @ingroup krb5_storage
730 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
731 krb5_store_string(krb5_storage *sp, const char *s)
733 krb5_data data;
734 data.length = strlen(s);
735 data.data = rk_UNCONST(s);
736 return krb5_store_data(sp, data);
740 * Parse a string from the storage.
742 * @param sp the storage buffer to read from
743 * @param string the parsed string
745 * @return 0 on success, a Kerberos 5 error code on failure.
747 * @ingroup krb5_storage
751 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
752 krb5_ret_string(krb5_storage *sp,
753 char **string)
755 int ret;
756 krb5_data data;
757 ret = krb5_ret_data(sp, &data);
758 if(ret)
759 return ret;
760 *string = realloc(data.data, data.length + 1);
761 if(*string == NULL){
762 free(data.data);
763 return ENOMEM;
765 (*string)[data.length] = 0;
766 return 0;
770 * Store a zero terminated string to the buffer. The data is stored
771 * one character at a time until a NUL is stored.
773 * @param sp the storage buffer to write to
774 * @param s the string to store.
776 * @return 0 on success, a Kerberos 5 error code on failure.
778 * @ingroup krb5_storage
781 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
782 krb5_store_stringz(krb5_storage *sp, const char *s)
784 size_t len = strlen(s) + 1;
785 ssize_t ret;
787 ret = sp->store(sp, s, len);
788 if(ret < 0)
789 return ret;
790 if((size_t)ret != len)
791 return sp->eof_code;
792 return 0;
796 * Parse zero terminated string from the storage.
798 * @param sp the storage buffer to read from
799 * @param string the parsed string
801 * @return 0 on success, a Kerberos 5 error code on failure.
803 * @ingroup krb5_storage
806 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
807 krb5_ret_stringz(krb5_storage *sp,
808 char **string)
810 char c;
811 char *s = NULL;
812 size_t len = 0;
813 ssize_t ret;
815 while((ret = sp->fetch(sp, &c, 1)) == 1){
816 krb5_error_code eret;
817 char *tmp;
819 len++;
820 eret = size_too_large(sp, len);
821 if (eret) {
822 free(s);
823 return eret;
825 tmp = realloc (s, len);
826 if (tmp == NULL) {
827 free (s);
828 return ENOMEM;
830 s = tmp;
831 s[len - 1] = c;
832 if(c == 0)
833 break;
835 if(ret != 1){
836 free(s);
837 if(ret == 0)
838 return sp->eof_code;
839 return ret;
841 *string = s;
842 return 0;
845 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
846 krb5_store_stringnl(krb5_storage *sp, const char *s)
848 size_t len = strlen(s);
849 ssize_t ret;
851 ret = sp->store(sp, s, len);
852 if(ret < 0)
853 return ret;
854 if((size_t)ret != len)
855 return sp->eof_code;
856 ret = sp->store(sp, "\n", 1);
857 if(ret != 1) {
858 if(ret < 0)
859 return ret;
860 else
861 return sp->eof_code;
864 return 0;
868 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
869 krb5_ret_stringnl(krb5_storage *sp,
870 char **string)
872 int expect_nl = 0;
873 char c;
874 char *s = NULL;
875 size_t len = 0;
876 ssize_t ret;
878 while((ret = sp->fetch(sp, &c, 1)) == 1){
879 krb5_error_code eret;
880 char *tmp;
882 if (c == '\r') {
883 expect_nl = 1;
884 continue;
886 if (expect_nl && c != '\n') {
887 free(s);
888 return KRB5_BADMSGTYPE;
891 len++;
892 eret = size_too_large(sp, len);
893 if (eret) {
894 free(s);
895 return eret;
897 tmp = realloc (s, len);
898 if (tmp == NULL) {
899 free (s);
900 return ENOMEM;
902 s = tmp;
903 if(c == '\n') {
904 s[len - 1] = '\0';
905 break;
907 s[len - 1] = c;
909 if(ret != 1){
910 free(s);
911 if(ret == 0)
912 return sp->eof_code;
913 return ret;
915 *string = s;
916 return 0;
920 * Write a principal block to storage.
922 * @param sp the storage buffer to write to
923 * @param p the principal block to write.
925 * @return 0 on success, a Kerberos 5 error code on failure.
927 * @ingroup krb5_storage
930 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
931 krb5_store_principal(krb5_storage *sp,
932 krb5_const_principal p)
934 size_t i;
935 int ret;
937 if(!krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE)) {
938 ret = krb5_store_int32(sp, p->name.name_type);
939 if(ret) return ret;
941 if(krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS))
942 ret = krb5_store_int32(sp, p->name.name_string.len + 1);
943 else
944 ret = krb5_store_int32(sp, p->name.name_string.len);
946 if(ret) return ret;
947 ret = krb5_store_string(sp, p->realm);
948 if(ret) return ret;
949 for(i = 0; i < p->name.name_string.len; i++){
950 ret = krb5_store_string(sp, p->name.name_string.val[i]);
951 if(ret) return ret;
953 return 0;
957 * Parse principal from the storage.
959 * @param sp the storage buffer to read from
960 * @param princ the parsed principal
962 * @return 0 on success, a Kerberos 5 error code on failure.
964 * @ingroup krb5_storage
967 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
968 krb5_ret_principal(krb5_storage *sp,
969 krb5_principal *princ)
971 int i;
972 int ret;
973 krb5_principal p;
974 int32_t type;
975 int32_t ncomp;
977 p = calloc(1, sizeof(*p));
978 if(p == NULL)
979 return ENOMEM;
981 if(krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_NO_NAME_TYPE))
982 type = KRB5_NT_UNKNOWN;
983 else if((ret = krb5_ret_int32(sp, &type))){
984 free(p);
985 return ret;
987 if((ret = krb5_ret_int32(sp, &ncomp))){
988 free(p);
989 return ret;
991 if(krb5_storage_is_flags(sp, KRB5_STORAGE_PRINCIPAL_WRONG_NUM_COMPONENTS))
992 ncomp--;
993 if (ncomp < 0) {
994 free(p);
995 return EINVAL;
997 ret = size_too_large_num(sp, ncomp, sizeof(p->name.name_string.val[0]));
998 if (ret) {
999 free(p);
1000 return ret;
1002 p->name.name_type = type;
1003 p->name.name_string.len = ncomp;
1004 ret = krb5_ret_string(sp, &p->realm);
1005 if(ret) {
1006 free(p);
1007 return ret;
1009 p->name.name_string.val = calloc(ncomp, sizeof(p->name.name_string.val[0]));
1010 if(p->name.name_string.val == NULL && ncomp != 0){
1011 free(p->realm);
1012 free(p);
1013 return ENOMEM;
1015 for(i = 0; i < ncomp; i++){
1016 ret = krb5_ret_string(sp, &p->name.name_string.val[i]);
1017 if(ret) {
1018 while (i >= 0)
1019 free(p->name.name_string.val[i--]);
1020 free(p->realm);
1021 free(p);
1022 return ret;
1025 *princ = p;
1026 return 0;
1030 * Store a keyblock to the storage.
1032 * @param sp the storage buffer to write to
1033 * @param p the keyblock to write
1035 * @return 0 on success, a Kerberos 5 error code on failure.
1037 * @ingroup krb5_storage
1040 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1041 krb5_store_keyblock(krb5_storage *sp, krb5_keyblock p)
1043 int ret;
1044 ret = krb5_store_int16(sp, p.keytype);
1045 if(ret) return ret;
1047 if(krb5_storage_is_flags(sp, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE)){
1048 /* this should really be enctype, but it is the same as
1049 keytype nowadays */
1050 ret = krb5_store_int16(sp, p.keytype);
1051 if(ret) return ret;
1054 ret = krb5_store_data(sp, p.keyvalue);
1055 return ret;
1059 * Read a keyblock from the storage.
1061 * @param sp the storage buffer to write to
1062 * @param p the keyblock read from storage, free using krb5_free_keyblock()
1064 * @return 0 on success, a Kerberos 5 error code on failure.
1066 * @ingroup krb5_storage
1069 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1070 krb5_ret_keyblock(krb5_storage *sp, krb5_keyblock *p)
1072 int ret;
1073 int16_t tmp;
1075 ret = krb5_ret_int16(sp, &tmp);
1076 if(ret) return ret;
1077 p->keytype = tmp;
1079 if(krb5_storage_is_flags(sp, KRB5_STORAGE_KEYBLOCK_KEYTYPE_TWICE)){
1080 ret = krb5_ret_int16(sp, &tmp);
1081 if(ret) return ret;
1084 ret = krb5_ret_data(sp, &p->keyvalue);
1085 return ret;
1089 * Write a times block to storage.
1091 * @param sp the storage buffer to write to
1092 * @param times the times block to write.
1094 * @return 0 on success, a Kerberos 5 error code on failure.
1096 * @ingroup krb5_storage
1099 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1100 krb5_store_times(krb5_storage *sp, krb5_times times)
1102 int ret;
1103 ret = krb5_store_int32(sp, times.authtime);
1104 if(ret) return ret;
1105 ret = krb5_store_int32(sp, times.starttime);
1106 if(ret) return ret;
1107 ret = krb5_store_int32(sp, times.endtime);
1108 if(ret) return ret;
1109 ret = krb5_store_int32(sp, times.renew_till);
1110 return ret;
1114 * Read a times block from the storage.
1116 * @param sp the storage buffer to write to
1117 * @param times the times block read from storage
1119 * @return 0 on success, a Kerberos 5 error code on failure.
1121 * @ingroup krb5_storage
1124 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1125 krb5_ret_times(krb5_storage *sp, krb5_times *times)
1127 int ret;
1128 int32_t tmp;
1129 ret = krb5_ret_int32(sp, &tmp);
1130 times->authtime = tmp;
1131 if(ret) return ret;
1132 ret = krb5_ret_int32(sp, &tmp);
1133 times->starttime = tmp;
1134 if(ret) return ret;
1135 ret = krb5_ret_int32(sp, &tmp);
1136 times->endtime = tmp;
1137 if(ret) return ret;
1138 ret = krb5_ret_int32(sp, &tmp);
1139 times->renew_till = tmp;
1140 return ret;
1144 * Write a address block to storage.
1146 * @param sp the storage buffer to write to
1147 * @param p the address block to write.
1149 * @return 0 on success, a Kerberos 5 error code on failure.
1151 * @ingroup krb5_storage
1154 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1155 krb5_store_address(krb5_storage *sp, krb5_address p)
1157 int ret;
1158 ret = krb5_store_int16(sp, p.addr_type);
1159 if(ret) return ret;
1160 ret = krb5_store_data(sp, p.address);
1161 return ret;
1165 * Read a address block from the storage.
1167 * @param sp the storage buffer to write to
1168 * @param adr the address block read from storage
1170 * @return 0 on success, a Kerberos 5 error code on failure.
1172 * @ingroup krb5_storage
1175 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1176 krb5_ret_address(krb5_storage *sp, krb5_address *adr)
1178 int16_t t;
1179 int ret;
1180 ret = krb5_ret_int16(sp, &t);
1181 if(ret) return ret;
1182 adr->addr_type = t;
1183 ret = krb5_ret_data(sp, &adr->address);
1184 return ret;
1188 * Write a addresses block to storage.
1190 * @param sp the storage buffer to write to
1191 * @param p the addresses block to write.
1193 * @return 0 on success, a Kerberos 5 error code on failure.
1195 * @ingroup krb5_storage
1198 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1199 krb5_store_addrs(krb5_storage *sp, krb5_addresses p)
1201 size_t i;
1202 int ret;
1203 ret = krb5_store_int32(sp, p.len);
1204 if(ret) return ret;
1205 for(i = 0; i<p.len; i++){
1206 ret = krb5_store_address(sp, p.val[i]);
1207 if(ret) break;
1209 return ret;
1213 * Read a addresses block from the storage.
1215 * @param sp the storage buffer to write to
1216 * @param adr the addresses block read from storage
1218 * @return 0 on success, a Kerberos 5 error code on failure.
1220 * @ingroup krb5_storage
1223 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1224 krb5_ret_addrs(krb5_storage *sp, krb5_addresses *adr)
1226 size_t i;
1227 int ret;
1228 int32_t tmp;
1230 ret = krb5_ret_int32(sp, &tmp);
1231 if(ret) return ret;
1232 ret = size_too_large_num(sp, tmp, sizeof(adr->val[0]));
1233 if (ret) return ret;
1234 adr->len = tmp;
1235 ALLOC(adr->val, adr->len);
1236 if (adr->val == NULL && adr->len != 0)
1237 return ENOMEM;
1238 for(i = 0; i < adr->len; i++){
1239 ret = krb5_ret_address(sp, &adr->val[i]);
1240 if(ret) break;
1242 return ret;
1246 * Write a auth data block to storage.
1248 * @param sp the storage buffer to write to
1249 * @param auth the auth data block to write.
1251 * @return 0 on success, a Kerberos 5 error code on failure.
1253 * @ingroup krb5_storage
1256 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1257 krb5_store_authdata(krb5_storage *sp, krb5_authdata auth)
1259 krb5_error_code ret;
1260 size_t i;
1261 ret = krb5_store_int32(sp, auth.len);
1262 if(ret) return ret;
1263 for(i = 0; i < auth.len; i++){
1264 ret = krb5_store_int16(sp, auth.val[i].ad_type);
1265 if(ret) break;
1266 ret = krb5_store_data(sp, auth.val[i].ad_data);
1267 if(ret) break;
1269 return 0;
1273 * Read a auth data from the storage.
1275 * @param sp the storage buffer to write to
1276 * @param auth the auth data block read from storage
1278 * @return 0 on success, a Kerberos 5 error code on failure.
1280 * @ingroup krb5_storage
1283 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1284 krb5_ret_authdata(krb5_storage *sp, krb5_authdata *auth)
1286 krb5_error_code ret;
1287 int32_t tmp;
1288 int16_t tmp2;
1289 int i;
1290 ret = krb5_ret_int32(sp, &tmp);
1291 if(ret) return ret;
1292 ret = size_too_large_num(sp, tmp, sizeof(auth->val[0]));
1293 if (ret) return ret;
1294 ALLOC_SEQ(auth, tmp);
1295 if (auth->val == NULL && tmp != 0)
1296 return ENOMEM;
1297 for(i = 0; i < tmp; i++){
1298 ret = krb5_ret_int16(sp, &tmp2);
1299 if(ret) break;
1300 auth->val[i].ad_type = tmp2;
1301 ret = krb5_ret_data(sp, &auth->val[i].ad_data);
1302 if(ret) break;
1304 return ret;
1307 static int32_t
1308 bitswap32(int32_t b)
1310 int32_t r = 0;
1311 int i;
1312 for (i = 0; i < 32; i++) {
1313 r = r << 1 | (b & 1);
1314 b = b >> 1;
1316 return r;
1320 * Write a credentials block to storage.
1322 * @param sp the storage buffer to write to
1323 * @param creds the creds block to write.
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_store_creds(krb5_storage *sp, krb5_creds *creds)
1333 int ret;
1335 ret = krb5_store_principal(sp, creds->client);
1336 if(ret)
1337 return ret;
1338 ret = krb5_store_principal(sp, creds->server);
1339 if(ret)
1340 return ret;
1341 ret = krb5_store_keyblock(sp, creds->session);
1342 if(ret)
1343 return ret;
1344 ret = krb5_store_times(sp, creds->times);
1345 if(ret)
1346 return ret;
1347 ret = krb5_store_int8(sp, creds->second_ticket.length != 0); /* is_skey */
1348 if(ret)
1349 return ret;
1351 if(krb5_storage_is_flags(sp, KRB5_STORAGE_CREDS_FLAGS_WRONG_BITORDER))
1352 ret = krb5_store_int32(sp, creds->flags.i);
1353 else
1354 ret = krb5_store_int32(sp, bitswap32(TicketFlags2int(creds->flags.b)));
1355 if(ret)
1356 return ret;
1358 ret = krb5_store_addrs(sp, creds->addresses);
1359 if(ret)
1360 return ret;
1361 ret = krb5_store_authdata(sp, creds->authdata);
1362 if(ret)
1363 return ret;
1364 ret = krb5_store_data(sp, creds->ticket);
1365 if(ret)
1366 return ret;
1367 ret = krb5_store_data(sp, creds->second_ticket);
1368 return ret;
1372 * Read a credentials block from the storage.
1374 * @param sp the storage buffer to write to
1375 * @param creds the credentials block read from storage
1377 * @return 0 on success, a Kerberos 5 error code on failure.
1379 * @ingroup krb5_storage
1382 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1383 krb5_ret_creds(krb5_storage *sp, krb5_creds *creds)
1385 krb5_error_code ret;
1386 int8_t dummy8;
1387 int32_t dummy32;
1389 memset(creds, 0, sizeof(*creds));
1390 ret = krb5_ret_principal (sp, &creds->client);
1391 if(ret) goto cleanup;
1392 ret = krb5_ret_principal (sp, &creds->server);
1393 if(ret) goto cleanup;
1394 ret = krb5_ret_keyblock (sp, &creds->session);
1395 if(ret) goto cleanup;
1396 ret = krb5_ret_times (sp, &creds->times);
1397 if(ret) goto cleanup;
1398 ret = krb5_ret_int8 (sp, &dummy8);
1399 if(ret) goto cleanup;
1400 ret = krb5_ret_int32 (sp, &dummy32);
1401 if(ret) goto cleanup;
1403 * Runtime detect the what is the higher bits of the bitfield. If
1404 * any of the higher bits are set in the input data, it's either a
1405 * new ticket flag (and this code need to be removed), or it's a
1406 * MIT cache (or new Heimdal cache), lets change it to our current
1407 * format.
1410 uint32_t mask = 0xffff0000;
1411 creds->flags.i = 0;
1412 creds->flags.b.anonymous = 1;
1413 if (creds->flags.i & mask)
1414 mask = ~mask;
1415 if (dummy32 & mask)
1416 dummy32 = bitswap32(dummy32);
1418 creds->flags.i = dummy32;
1419 ret = krb5_ret_addrs (sp, &creds->addresses);
1420 if(ret) goto cleanup;
1421 ret = krb5_ret_authdata (sp, &creds->authdata);
1422 if(ret) goto cleanup;
1423 ret = krb5_ret_data (sp, &creds->ticket);
1424 if(ret) goto cleanup;
1425 ret = krb5_ret_data (sp, &creds->second_ticket);
1426 cleanup:
1427 if(ret) {
1428 #if 0
1429 krb5_free_cred_contents(context, creds); /* XXX */
1430 #endif
1432 return ret;
1435 #define SC_CLIENT_PRINCIPAL 0x0001
1436 #define SC_SERVER_PRINCIPAL 0x0002
1437 #define SC_SESSION_KEY 0x0004
1438 #define SC_TICKET 0x0008
1439 #define SC_SECOND_TICKET 0x0010
1440 #define SC_AUTHDATA 0x0020
1441 #define SC_ADDRESSES 0x0040
1444 * Write a tagged credentials block to storage.
1446 * @param sp the storage buffer to write to
1447 * @param creds the creds block to write.
1449 * @return 0 on success, a Kerberos 5 error code on failure.
1451 * @ingroup krb5_storage
1454 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1455 krb5_store_creds_tag(krb5_storage *sp, krb5_creds *creds)
1457 int ret;
1458 int32_t header = 0;
1460 if (creds->client)
1461 header |= SC_CLIENT_PRINCIPAL;
1462 if (creds->server)
1463 header |= SC_SERVER_PRINCIPAL;
1464 if (creds->session.keytype != ETYPE_NULL)
1465 header |= SC_SESSION_KEY;
1466 if (creds->ticket.data)
1467 header |= SC_TICKET;
1468 if (creds->second_ticket.length)
1469 header |= SC_SECOND_TICKET;
1470 if (creds->authdata.len)
1471 header |= SC_AUTHDATA;
1472 if (creds->addresses.len)
1473 header |= SC_ADDRESSES;
1475 ret = krb5_store_int32(sp, header);
1476 if (ret)
1477 return ret;
1479 if (creds->client) {
1480 ret = krb5_store_principal(sp, creds->client);
1481 if(ret)
1482 return ret;
1485 if (creds->server) {
1486 ret = krb5_store_principal(sp, creds->server);
1487 if(ret)
1488 return ret;
1491 if (creds->session.keytype != ETYPE_NULL) {
1492 ret = krb5_store_keyblock(sp, creds->session);
1493 if(ret)
1494 return ret;
1497 ret = krb5_store_times(sp, creds->times);
1498 if(ret)
1499 return ret;
1500 ret = krb5_store_int8(sp, creds->second_ticket.length != 0); /* is_skey */
1501 if(ret)
1502 return ret;
1504 ret = krb5_store_int32(sp, bitswap32(TicketFlags2int(creds->flags.b)));
1505 if(ret)
1506 return ret;
1508 if (creds->addresses.len) {
1509 ret = krb5_store_addrs(sp, creds->addresses);
1510 if(ret)
1511 return ret;
1514 if (creds->authdata.len) {
1515 ret = krb5_store_authdata(sp, creds->authdata);
1516 if(ret)
1517 return ret;
1520 if (creds->ticket.data) {
1521 ret = krb5_store_data(sp, creds->ticket);
1522 if(ret)
1523 return ret;
1526 if (creds->second_ticket.data) {
1527 ret = krb5_store_data(sp, creds->second_ticket);
1528 if (ret)
1529 return ret;
1532 return ret;
1536 * Read a tagged credentials block from the storage.
1538 * @param sp the storage buffer to write to
1539 * @param creds the credentials block read from storage
1541 * @return 0 on success, a Kerberos 5 error code on failure.
1543 * @ingroup krb5_storage
1546 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1547 krb5_ret_creds_tag(krb5_storage *sp,
1548 krb5_creds *creds)
1550 krb5_error_code ret;
1551 int8_t dummy8;
1552 int32_t dummy32, header;
1554 memset(creds, 0, sizeof(*creds));
1556 ret = krb5_ret_int32 (sp, &header);
1557 if (ret) goto cleanup;
1559 if (header & SC_CLIENT_PRINCIPAL) {
1560 ret = krb5_ret_principal (sp, &creds->client);
1561 if(ret) goto cleanup;
1563 if (header & SC_SERVER_PRINCIPAL) {
1564 ret = krb5_ret_principal (sp, &creds->server);
1565 if(ret) goto cleanup;
1567 if (header & SC_SESSION_KEY) {
1568 ret = krb5_ret_keyblock (sp, &creds->session);
1569 if(ret) goto cleanup;
1571 ret = krb5_ret_times (sp, &creds->times);
1572 if(ret) goto cleanup;
1573 ret = krb5_ret_int8 (sp, &dummy8);
1574 if(ret) goto cleanup;
1575 ret = krb5_ret_int32 (sp, &dummy32);
1576 if(ret) goto cleanup;
1578 * Runtime detect the what is the higher bits of the bitfield. If
1579 * any of the higher bits are set in the input data, it's either a
1580 * new ticket flag (and this code need to be removed), or it's a
1581 * MIT cache (or new Heimdal cache), lets change it to our current
1582 * format.
1585 uint32_t mask = 0xffff0000;
1586 creds->flags.i = 0;
1587 creds->flags.b.anonymous = 1;
1588 if (creds->flags.i & mask)
1589 mask = ~mask;
1590 if (dummy32 & mask)
1591 dummy32 = bitswap32(dummy32);
1593 creds->flags.i = dummy32;
1594 if (header & SC_ADDRESSES) {
1595 ret = krb5_ret_addrs (sp, &creds->addresses);
1596 if(ret) goto cleanup;
1598 if (header & SC_AUTHDATA) {
1599 ret = krb5_ret_authdata (sp, &creds->authdata);
1600 if(ret) goto cleanup;
1602 if (header & SC_TICKET) {
1603 ret = krb5_ret_data (sp, &creds->ticket);
1604 if(ret) goto cleanup;
1606 if (header & SC_SECOND_TICKET) {
1607 ret = krb5_ret_data (sp, &creds->second_ticket);
1608 if(ret) goto cleanup;
1611 cleanup:
1612 if(ret) {
1613 #if 0
1614 krb5_free_cred_contents(context, creds); /* XXX */
1615 #endif
1617 return ret;