155682Smarkm/*
2233294Sstas * Copyright (c) 1997 - 2001 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
555682Smarkm *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
955682Smarkm *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
2055682Smarkm *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "krb5_locl.h"
3555682Smarkm
36233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
3755682Smarkmkrb5_rd_error(krb5_context context,
38178825Sdfr	      const krb5_data *msg,
3955682Smarkm	      KRB_ERROR *result)
4055682Smarkm{
41233294Sstas
4255682Smarkm    size_t len;
4355682Smarkm    krb5_error_code ret;
4478527Sassar
4555682Smarkm    ret = decode_KRB_ERROR(msg->data, msg->length, result, &len);
46178825Sdfr    if(ret) {
47233294Sstas	krb5_clear_error_message(context);
4855682Smarkm	return ret;
49178825Sdfr    }
5055682Smarkm    result->error_code += KRB5KDC_ERR_NONE;
5178527Sassar    return 0;
5255682Smarkm}
5355682Smarkm
54233294SstasKRB5_LIB_FUNCTION void KRB5_LIB_CALL
5555682Smarkmkrb5_free_error_contents (krb5_context context,
5655682Smarkm			  krb5_error *error)
5755682Smarkm{
5855682Smarkm    free_KRB_ERROR(error);
59178825Sdfr    memset(error, 0, sizeof(*error));
6055682Smarkm}
6155682Smarkm
62233294SstasKRB5_LIB_FUNCTION void KRB5_LIB_CALL
6355682Smarkmkrb5_free_error (krb5_context context,
6455682Smarkm		 krb5_error *error)
6555682Smarkm{
6655682Smarkm    krb5_free_error_contents (context, error);
6755682Smarkm    free (error);
6855682Smarkm}
6978527Sassar
70233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
7178527Sassarkrb5_error_from_rd_error(krb5_context context,
7278527Sassar			 const krb5_error *error,
7378527Sassar			 const krb5_creds *creds)
7478527Sassar{
7578527Sassar    krb5_error_code ret;
7678527Sassar
7778527Sassar    ret = error->error_code;
7878527Sassar    if (error->e_text != NULL) {
79233294Sstas	krb5_set_error_message(context, ret, "%s", *error->e_text);
8078527Sassar    } else {
8178527Sassar	char clientname[256], servername[256];
8278527Sassar
8378527Sassar	if (creds != NULL) {
8478527Sassar	    krb5_unparse_name_fixed(context, creds->client,
8578527Sassar				    clientname, sizeof(clientname));
8678527Sassar	    krb5_unparse_name_fixed(context, creds->server,
8778527Sassar				    servername, sizeof(servername));
8878527Sassar	}
8978527Sassar
9078527Sassar	switch (ret) {
9178527Sassar	case KRB5KDC_ERR_NAME_EXP :
92233294Sstas	    krb5_set_error_message(context, ret,
93233294Sstas				   N_("Client %s%s%s expired", ""),
94233294Sstas				   creds ? "(" : "",
95233294Sstas				   creds ? clientname : "",
96233294Sstas				   creds ? ")" : "");
9778527Sassar	    break;
9878527Sassar	case KRB5KDC_ERR_SERVICE_EXP :
99233294Sstas	    krb5_set_error_message(context, ret,
100233294Sstas				   N_("Server %s%s%s expired", ""),
101233294Sstas				   creds ? "(" : "",
102233294Sstas				   creds ? servername : "",
103233294Sstas				   creds ? ")" : "");
10478527Sassar	    break;
10578527Sassar	case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN :
106233294Sstas	    krb5_set_error_message(context, ret,
107233294Sstas				   N_("Client %s%s%s unknown", ""),
108233294Sstas				   creds ? "(" : "",
109233294Sstas				   creds ? clientname : "",
110233294Sstas				   creds ? ")" : "");
11178527Sassar	    break;
11278527Sassar	case KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN :
113233294Sstas	    krb5_set_error_message(context, ret,
114233294Sstas				   N_("Server %s%s%s unknown", ""),
115233294Sstas				   creds ? "(" : "",
116233294Sstas				   creds ? servername : "",
117233294Sstas				   creds ? ")" : "");
11878527Sassar	    break;
11978527Sassar	default :
120233294Sstas	    krb5_clear_error_message(context);
12178527Sassar	    break;
12278527Sassar	}
12378527Sassar    }
12478527Sassar    return ret;
12578527Sassar}
126