155682Smarkm/*
278527Sassar * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
455682Smarkm * All rights reserved.
555682Smarkm *
655682Smarkm * Redistribution and use in source and binary forms, with or without
755682Smarkm * modification, are permitted provided that the following conditions
855682Smarkm * are met:
955682Smarkm *
1055682Smarkm * 1. Redistributions of source code must retain the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1455682Smarkm *    notice, this list of conditions and the following disclaimer in the
1555682Smarkm *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors
1855682Smarkm *    may be used to endorse or promote products derived from this software
1955682Smarkm *    without specific prior written permission.
2055682Smarkm *
2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155682Smarkm * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "krb5_locl.h"
3555682Smarkm
36178825SdfrRCSID("$Id: rd_error.c 21057 2007-06-12 17:22:31Z lha $");
3755682Smarkm
38178825Sdfrkrb5_error_code KRB5_LIB_FUNCTION
3955682Smarkmkrb5_rd_error(krb5_context context,
40178825Sdfr	      const krb5_data *msg,
4155682Smarkm	      KRB_ERROR *result)
4255682Smarkm{
4355682Smarkm
4455682Smarkm    size_t len;
4555682Smarkm    krb5_error_code ret;
4678527Sassar
4755682Smarkm    ret = decode_KRB_ERROR(msg->data, msg->length, result, &len);
48178825Sdfr    if(ret) {
49178825Sdfr	krb5_clear_error_string(context);
5055682Smarkm	return ret;
51178825Sdfr    }
5255682Smarkm    result->error_code += KRB5KDC_ERR_NONE;
5378527Sassar    return 0;
5455682Smarkm}
5555682Smarkm
56178825Sdfrvoid KRB5_LIB_FUNCTION
5755682Smarkmkrb5_free_error_contents (krb5_context context,
5855682Smarkm			  krb5_error *error)
5955682Smarkm{
6055682Smarkm    free_KRB_ERROR(error);
61178825Sdfr    memset(error, 0, sizeof(*error));
6255682Smarkm}
6355682Smarkm
64178825Sdfrvoid KRB5_LIB_FUNCTION
6555682Smarkmkrb5_free_error (krb5_context context,
6655682Smarkm		 krb5_error *error)
6755682Smarkm{
6855682Smarkm    krb5_free_error_contents (context, error);
6955682Smarkm    free (error);
7055682Smarkm}
7178527Sassar
72178825Sdfrkrb5_error_code KRB5_LIB_FUNCTION
7378527Sassarkrb5_error_from_rd_error(krb5_context context,
7478527Sassar			 const krb5_error *error,
7578527Sassar			 const krb5_creds *creds)
7678527Sassar{
7778527Sassar    krb5_error_code ret;
7878527Sassar
7978527Sassar    ret = error->error_code;
8078527Sassar    if (error->e_text != NULL) {
8178527Sassar	krb5_set_error_string(context, "%s", *error->e_text);
8278527Sassar    } else {
8378527Sassar	char clientname[256], servername[256];
8478527Sassar
8578527Sassar	if (creds != NULL) {
8678527Sassar	    krb5_unparse_name_fixed(context, creds->client,
8778527Sassar				    clientname, sizeof(clientname));
8878527Sassar	    krb5_unparse_name_fixed(context, creds->server,
8978527Sassar				    servername, sizeof(servername));
9078527Sassar	}
9178527Sassar
9278527Sassar	switch (ret) {
9378527Sassar	case KRB5KDC_ERR_NAME_EXP :
9478527Sassar	    krb5_set_error_string(context, "Client %s%s%s expired",
9578527Sassar				  creds ? "(" : "",
9678527Sassar				  creds ? clientname : "",
9778527Sassar				  creds ? ")" : "");
9878527Sassar	    break;
9978527Sassar	case KRB5KDC_ERR_SERVICE_EXP :
10078527Sassar	    krb5_set_error_string(context, "Server %s%s%s expired",
10178527Sassar				  creds ? "(" : "",
10278527Sassar				  creds ? servername : "",
10378527Sassar				  creds ? ")" : "");
10478527Sassar	    break;
10578527Sassar	case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN :
10678527Sassar	    krb5_set_error_string(context, "Client %s%s%s unknown",
10778527Sassar				  creds ? "(" : "",
10878527Sassar				  creds ? clientname : "",
10978527Sassar				  creds ? ")" : "");
11078527Sassar	    break;
11178527Sassar	case KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN :
11278527Sassar	    krb5_set_error_string(context, "Server %s%s%s unknown",
11378527Sassar				  creds ? "(" : "",
11478527Sassar				  creds ? servername : "",
11578527Sassar				  creds ? ")" : "");
11678527Sassar	    break;
11778527Sassar	default :
11878527Sassar	    krb5_clear_error_string(context);
11978527Sassar	    break;
12078527Sassar	}
12178527Sassar    }
12278527Sassar    return ret;
12378527Sassar}
124