r13316: Let the carnage begin....
[metze/samba/wip.git] / source3 / libads / krb5_errs.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Kerberos error mapping functions
4  *  Copyright (C) Guenther Deschner 2005
5  *  
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22
23 #ifdef HAVE_KRB5
24
25 static const struct {
26         int krb5_code;
27         NTSTATUS ntstatus;
28 } krb5_to_nt_status_map[] = {
29         {KRB5_CC_IO, NT_STATUS_UNEXPECTED_IO_ERROR},
30         {KRB5KDC_ERR_BADOPTION, NT_STATUS_INVALID_PARAMETER},
31         {KRB5KDC_ERR_CLIENT_REVOKED, NT_STATUS_ACCESS_DENIED},
32         {KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN, NT_STATUS_INVALID_ACCOUNT_NAME},
33         {KRB5KDC_ERR_ETYPE_NOSUPP, NT_STATUS_LOGON_FAILURE},
34 #if defined(KRB5KDC_ERR_KEY_EXPIRED) /* Heimdal */
35         {KRB5KDC_ERR_KEY_EXPIRED, NT_STATUS_PASSWORD_EXPIRED},
36 #elif defined(KRB5KDC_ERR_KEY_EXP) /* MIT */
37         {KRB5KDC_ERR_KEY_EXP, NT_STATUS_PASSWORD_EXPIRED},
38 #else 
39 #error Neither KRB5KDC_ERR_KEY_EXPIRED nor KRB5KDC_ERR_KEY_EXP available
40 #endif
41         {25, NT_STATUS_PASSWORD_EXPIRED}, /* FIXME: bug in heimdal 0.7 krb5_get_init_creds_password (Inappropriate ioctl for device (25)) */
42         {KRB5KDC_ERR_NULL_KEY, NT_STATUS_LOGON_FAILURE},
43         {KRB5KDC_ERR_POLICY, NT_STATUS_PASSWORD_RESTRICTION},
44         {KRB5KDC_ERR_PREAUTH_FAILED, NT_STATUS_LOGON_FAILURE},
45         {KRB5KDC_ERR_SERVICE_REVOKED, NT_STATUS_ACCESS_DENIED},
46         {KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN, NT_STATUS_INVALID_ACCOUNT_NAME},
47         {KRB5KDC_ERR_SUMTYPE_NOSUPP, NT_STATUS_LOGON_FAILURE},
48         {KRB5KDC_ERR_TGT_REVOKED, NT_STATUS_ACCESS_DENIED},
49         {KRB5_KDC_UNREACH, NT_STATUS_NO_LOGON_SERVERS},
50         {KRB5KRB_AP_ERR_BAD_INTEGRITY, NT_STATUS_LOGON_FAILURE},
51         {KRB5KRB_AP_ERR_MODIFIED, NT_STATUS_LOGON_FAILURE},
52         {KRB5KRB_AP_ERR_SKEW, NT_STATUS_TIME_DIFFERENCE_AT_DC},
53         {KRB5KRB_AP_ERR_TKT_EXPIRED, NT_STATUS_LOGON_FAILURE},
54         {KRB5KRB_ERR_GENERIC, NT_STATUS_UNSUCCESSFUL},
55         {KRB5KRB_ERR_RESPONSE_TOO_BIG, NT_STATUS_PROTOCOL_UNREACHABLE},
56         {0, NT_STATUS_OK}
57 };
58
59 static const struct {
60         NTSTATUS ntstatus;
61         int krb5_code;
62 } nt_status_to_krb5_map[] = {
63         {NT_STATUS_LOGON_FAILURE, KRB5KDC_ERR_PREAUTH_FAILED},
64         {NT_STATUS_NO_LOGON_SERVERS, KRB5_KDC_UNREACH},
65         {NT_STATUS_OK, 0}
66 };
67
68 /*****************************************************************************
69 convert a KRB5 error to a NT status32 code
70  *****************************************************************************/
71 NTSTATUS krb5_to_nt_status(int kerberos_error)
72 {
73         int i;
74         
75         if (kerberos_error == 0) {
76                 return NT_STATUS_OK;
77         }
78         
79         for (i=0; NT_STATUS_V(krb5_to_nt_status_map[i].ntstatus); i++) {
80                 if (kerberos_error == krb5_to_nt_status_map[i].krb5_code)
81                         return krb5_to_nt_status_map[i].ntstatus;
82         }
83
84         return NT_STATUS_UNSUCCESSFUL;
85 }
86
87 /*****************************************************************************
88 convert an NT status32 code to a KRB5 error
89  *****************************************************************************/
90 int nt_status_to_krb5(NTSTATUS nt_status)
91 {
92         int i;
93         
94         if NT_STATUS_IS_OK(nt_status) {
95                 return 0;
96         }
97         
98         for (i=0; NT_STATUS_V(nt_status_to_krb5_map[i].ntstatus); i++) {
99                 if (NT_STATUS_EQUAL(nt_status,nt_status_to_krb5_map[i].ntstatus))
100                         return nt_status_to_krb5_map[i].krb5_code;
101         }
102
103         return KRB5KRB_ERR_GENERIC;
104 }
105
106 #else 
107
108 /*****************************************************************************
109 convert a KRB5 error to a NT status32 code
110  *****************************************************************************/
111 NTSTATUS krb5_to_nt_status(int kerberos_error)
112 {
113         if (kerberos_error == 0) {
114                 return NT_STATUS_OK;
115         }
116         
117         return NT_STATUS_UNSUCCESSFUL;
118 }
119
120 /*****************************************************************************
121 convert an NT status32 code to a KRB5 error
122  *****************************************************************************/
123 int nt_status_to_krb5(NTSTATUS nt_status)
124 {
125         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_OK)) {
126                 return 0;
127         }
128         return -1; /* FIXME: what to return here ? */
129 }
130
131 #endif
132