xref: /freebsd/crypto/heimdal/lib/krb5/misc.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "krb5_locl.h"
35*ae771770SStanislav Sedov #ifdef HAVE_EXECINFO_H
36*ae771770SStanislav Sedov #include <execinfo.h>
37*ae771770SStanislav Sedov #endif
38b528cefcSMark Murray 
39*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
_krb5_s4u2self_to_checksumdata(krb5_context context,const PA_S4U2Self * self,krb5_data * data)40c19800e8SDoug Rabson _krb5_s4u2self_to_checksumdata(krb5_context context,
41c19800e8SDoug Rabson 			       const PA_S4U2Self *self,
42c19800e8SDoug Rabson 			       krb5_data *data)
43c19800e8SDoug Rabson {
44c19800e8SDoug Rabson     krb5_error_code ret;
45c19800e8SDoug Rabson     krb5_ssize_t ssize;
46c19800e8SDoug Rabson     krb5_storage *sp;
47c19800e8SDoug Rabson     size_t size;
48*ae771770SStanislav Sedov     size_t i;
49c19800e8SDoug Rabson 
50c19800e8SDoug Rabson     sp = krb5_storage_emem();
51c19800e8SDoug Rabson     if (sp == NULL) {
52*ae771770SStanislav Sedov 	krb5_clear_error_message(context);
53c19800e8SDoug Rabson 	return ENOMEM;
54c19800e8SDoug Rabson     }
55c19800e8SDoug Rabson     krb5_storage_set_flags(sp, KRB5_STORAGE_BYTEORDER_LE);
56c19800e8SDoug Rabson     ret = krb5_store_int32(sp, self->name.name_type);
57c19800e8SDoug Rabson     if (ret)
58c19800e8SDoug Rabson 	goto out;
59c19800e8SDoug Rabson     for (i = 0; i < self->name.name_string.len; i++) {
60c19800e8SDoug Rabson 	size = strlen(self->name.name_string.val[i]);
61c19800e8SDoug Rabson 	ssize = krb5_storage_write(sp, self->name.name_string.val[i], size);
62*ae771770SStanislav Sedov 	if (ssize != (krb5_ssize_t)size) {
63c19800e8SDoug Rabson 	    ret = ENOMEM;
64c19800e8SDoug Rabson 	    goto out;
65c19800e8SDoug Rabson 	}
66c19800e8SDoug Rabson     }
67c19800e8SDoug Rabson     size = strlen(self->realm);
68c19800e8SDoug Rabson     ssize = krb5_storage_write(sp, self->realm, size);
69*ae771770SStanislav Sedov     if (ssize != (krb5_ssize_t)size) {
70c19800e8SDoug Rabson 	ret = ENOMEM;
71c19800e8SDoug Rabson 	goto out;
72c19800e8SDoug Rabson     }
73c19800e8SDoug Rabson     size = strlen(self->auth);
74c19800e8SDoug Rabson     ssize = krb5_storage_write(sp, self->auth, size);
75*ae771770SStanislav Sedov     if (ssize != (krb5_ssize_t)size) {
76c19800e8SDoug Rabson 	ret = ENOMEM;
77c19800e8SDoug Rabson 	goto out;
78c19800e8SDoug Rabson     }
79c19800e8SDoug Rabson 
80c19800e8SDoug Rabson     ret = krb5_storage_to_data(sp, data);
81c19800e8SDoug Rabson     krb5_storage_free(sp);
82c19800e8SDoug Rabson     return ret;
83c19800e8SDoug Rabson 
84c19800e8SDoug Rabson out:
85*ae771770SStanislav Sedov     krb5_clear_error_message(context);
86c19800e8SDoug Rabson     return ret;
87c19800e8SDoug Rabson }
88*ae771770SStanislav Sedov 
89*ae771770SStanislav Sedov krb5_error_code
krb5_enomem(krb5_context context)90*ae771770SStanislav Sedov krb5_enomem(krb5_context context)
91*ae771770SStanislav Sedov {
92*ae771770SStanislav Sedov     krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
93*ae771770SStanislav Sedov     return ENOMEM;
94*ae771770SStanislav Sedov }
95*ae771770SStanislav Sedov 
96*ae771770SStanislav Sedov void
_krb5_debug_backtrace(krb5_context context)97*ae771770SStanislav Sedov _krb5_debug_backtrace(krb5_context context)
98*ae771770SStanislav Sedov {
99*ae771770SStanislav Sedov #if defined(HAVE_BACKTRACE) && !defined(HEIMDAL_SMALLER)
100*ae771770SStanislav Sedov     void *stack[128];
101*ae771770SStanislav Sedov     char **strs = NULL;
102*ae771770SStanislav Sedov     int i, frames = backtrace(stack, sizeof(stack) / sizeof(stack[0]));
103*ae771770SStanislav Sedov     if (frames > 0)
104*ae771770SStanislav Sedov 	strs = backtrace_symbols(stack, frames);
105*ae771770SStanislav Sedov     if (strs) {
106*ae771770SStanislav Sedov 	for (i = 0; i < frames; i++)
107*ae771770SStanislav Sedov 	    _krb5_debug(context, 10, "frame %d: %s", i, strs[i]);
108*ae771770SStanislav Sedov 	free(strs);
109*ae771770SStanislav Sedov     }
110*ae771770SStanislav Sedov #endif
111*ae771770SStanislav Sedov }
112*ae771770SStanislav Sedov 
113*ae771770SStanislav Sedov krb5_error_code
_krb5_einval(krb5_context context,const char * func,unsigned long argn)114*ae771770SStanislav Sedov _krb5_einval(krb5_context context, const char *func, unsigned long argn)
115*ae771770SStanislav Sedov {
116*ae771770SStanislav Sedov #ifndef HEIMDAL_SMALLER
117*ae771770SStanislav Sedov     krb5_set_error_message(context, EINVAL,
118*ae771770SStanislav Sedov 			   N_("programmer error: invalid argument to %s argument %lu",
119*ae771770SStanislav Sedov 			      "function:line"),
120*ae771770SStanislav Sedov 			   func, argn);
121*ae771770SStanislav Sedov     if (_krb5_have_debug(context, 10)) {
122*ae771770SStanislav Sedov 	_krb5_debug(context, 10, "invalid argument to function %s argument %lu",
123*ae771770SStanislav Sedov 		    func, argn);
124*ae771770SStanislav Sedov 	_krb5_debug_backtrace(context);
125*ae771770SStanislav Sedov     }
126*ae771770SStanislav Sedov #endif
127*ae771770SStanislav Sedov     return EINVAL;
128*ae771770SStanislav Sedov }
129