1 1.1 elric /* $NetBSD: mk_error.c,v 1.2 2017/01/28 21:31:49 christos Exp $ */ 2 1.1 elric 3 1.1 elric /* 4 1.1 elric * Copyright (c) 1997 - 2003 Kungliga Tekniska Hgskolan 5 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden). 6 1.1 elric * All rights reserved. 7 1.1 elric * 8 1.1 elric * Redistribution and use in source and binary forms, with or without 9 1.1 elric * modification, are permitted provided that the following conditions 10 1.1 elric * are met: 11 1.1 elric * 12 1.1 elric * 1. Redistributions of source code must retain the above copyright 13 1.1 elric * notice, this list of conditions and the following disclaimer. 14 1.1 elric * 15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 elric * notice, this list of conditions and the following disclaimer in the 17 1.1 elric * documentation and/or other materials provided with the distribution. 18 1.1 elric * 19 1.1 elric * 3. Neither the name of the Institute nor the names of its contributors 20 1.1 elric * may be used to endorse or promote products derived from this software 21 1.1 elric * without specific prior written permission. 22 1.1 elric * 23 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 1.1 elric * SUCH DAMAGE. 34 1.1 elric */ 35 1.1 elric 36 1.1 elric #include "krb5_locl.h" 37 1.1 elric 38 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 39 1.2 christos krb5_mk_error_ext(krb5_context context, 40 1.2 christos krb5_error_code error_code, 41 1.2 christos const char *e_text, 42 1.2 christos const krb5_data *e_data, 43 1.2 christos const krb5_principal server, 44 1.2 christos const PrincipalName *client_name, 45 1.2 christos const Realm *client_realm, 46 1.2 christos time_t *client_time, 47 1.2 christos int *client_usec, 48 1.2 christos krb5_data *reply) 49 1.1 elric { 50 1.1 elric const char *e_text2 = NULL; 51 1.1 elric KRB_ERROR msg; 52 1.1 elric krb5_timestamp sec; 53 1.1 elric int32_t usec; 54 1.2 christos size_t len = 0; 55 1.1 elric krb5_error_code ret = 0; 56 1.1 elric 57 1.1 elric krb5_us_timeofday (context, &sec, &usec); 58 1.1 elric 59 1.1 elric memset(&msg, 0, sizeof(msg)); 60 1.1 elric msg.pvno = 5; 61 1.1 elric msg.msg_type = krb_error; 62 1.1 elric msg.stime = sec; 63 1.1 elric msg.susec = usec; 64 1.1 elric msg.ctime = client_time; 65 1.1 elric msg.cusec = client_usec; 66 1.1 elric /* Make sure we only send `protocol' error codes */ 67 1.1 elric if(error_code < KRB5KDC_ERR_NONE || error_code >= KRB5_ERR_RCSID) { 68 1.1 elric if(e_text == NULL) 69 1.1 elric e_text = e_text2 = krb5_get_error_message(context, error_code); 70 1.1 elric error_code = KRB5KRB_ERR_GENERIC; 71 1.1 elric } 72 1.1 elric msg.error_code = error_code - KRB5KDC_ERR_NONE; 73 1.1 elric if (e_text) 74 1.1 elric msg.e_text = rk_UNCONST(&e_text); 75 1.1 elric if (e_data) 76 1.1 elric msg.e_data = rk_UNCONST(e_data); 77 1.1 elric if(server){ 78 1.1 elric msg.realm = server->realm; 79 1.1 elric msg.sname = server->name; 80 1.1 elric }else{ 81 1.2 christos static char unspec[] = "<unspecified realm>"; 82 1.2 christos msg.realm = unspec; 83 1.1 elric } 84 1.2 christos msg.crealm = rk_UNCONST(client_realm); 85 1.2 christos msg.cname = rk_UNCONST(client_name); 86 1.1 elric 87 1.1 elric ASN1_MALLOC_ENCODE(KRB_ERROR, reply->data, reply->length, &msg, &len, ret); 88 1.1 elric if (e_text2) 89 1.1 elric krb5_free_error_message(context, e_text2); 90 1.1 elric if (ret) 91 1.1 elric return ret; 92 1.1 elric if(reply->length != len) 93 1.1 elric krb5_abortx(context, "internal error in ASN.1 encoder"); 94 1.1 elric return 0; 95 1.1 elric } 96 1.2 christos 97 1.2 christos KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 98 1.2 christos krb5_mk_error(krb5_context context, 99 1.2 christos krb5_error_code error_code, 100 1.2 christos const char *e_text, 101 1.2 christos const krb5_data *e_data, 102 1.2 christos const krb5_principal client, 103 1.2 christos const krb5_principal server, 104 1.2 christos time_t *client_time, 105 1.2 christos int *client_usec, 106 1.2 christos krb5_data *reply) 107 1.2 christos { 108 1.2 christos const PrincipalName *client_name = NULL; 109 1.2 christos const Realm *client_realm = NULL; 110 1.2 christos 111 1.2 christos if (client) { 112 1.2 christos client_realm = &client->realm; 113 1.2 christos client_name = &client->name; 114 1.2 christos } 115 1.2 christos 116 1.2 christos return krb5_mk_error_ext(context, error_code, e_text, e_data, 117 1.2 christos server, client_name, client_realm, 118 1.2 christos client_time, client_usec, reply); 119 1.2 christos } 120