sync'ing up for 3.0alpha20 release
[sfrench/samba-autobuild/.git] / source3 / libads / ads_status.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ads (active directory) utility library
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Remus Koos 2001
6    Copyright (C) Andrew Bartlett 2001
7    
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /*
27   build a ADS_STATUS structure
28 */
29 ADS_STATUS ads_build_error(enum ads_error_type etype, 
30                            int rc, int minor_status)
31 {
32         ADS_STATUS ret;
33
34         if (etype == ADS_ERROR_NT) {
35                 DEBUG(0,("don't use ads_build_error with ADS_ERROR_NT!\n"));
36                 ret.err.rc = -1;
37                 ret.error_type = ADS_ERROR_SYSTEM;
38                 ret.minor_status = 0;
39                 return ret;     
40         }       
41                 
42         ret.err.rc = rc;
43         ret.error_type = etype;         
44         ret.minor_status = minor_status;
45         return ret;
46 }
47
48 ADS_STATUS ads_build_nt_error(enum ads_error_type etype, 
49                            NTSTATUS nt_status)
50 {
51         ADS_STATUS ret;
52
53         if (etype != ADS_ERROR_NT) {
54                 DEBUG(0,("don't use ads_build_nt_error without ADS_ERROR_NT!\n"));
55                 ret.err.rc = -1;
56                 ret.error_type = ADS_ERROR_SYSTEM;
57                 ret.minor_status = 0;
58                 return ret;     
59         }
60         ret.err.nt_status = nt_status;
61         ret.error_type = etype;         
62         ret.minor_status = 0;
63         return ret;
64 }
65
66 /*
67   do a rough conversion between ads error codes and NT status codes
68   we'll need to fill this in more
69 */
70 NTSTATUS ads_ntstatus(ADS_STATUS status)
71 {
72         if (status.error_type == ADS_ERROR_NT){
73                 return status.err.nt_status;    
74         }
75         if (ADS_ERR_OK(status)) return NT_STATUS_OK;
76         return NT_STATUS_UNSUCCESSFUL;
77 }
78
79 /*
80   return a string for an error from a ads routine
81 */
82 const char *ads_errstr(ADS_STATUS status)
83 {
84         int msg_ctx;
85         static char *ret;
86
87         SAFE_FREE(ret);
88         msg_ctx = 0;
89
90         switch (status.error_type) {
91         case ADS_ERROR_SYSTEM:
92                 return strerror(status.err.rc);
93 #ifdef HAVE_LDAP
94         case ADS_ERROR_LDAP:
95                 return ldap_err2string(status.err.rc);
96 #endif
97 #ifdef HAVE_KRB5
98         case ADS_ERROR_KRB5: 
99                 return error_message(status.err.rc);
100 #endif
101 #ifdef HAVE_GSSAPI
102         case ADS_ERROR_GSS:
103         {
104                 uint32 minor;
105                 
106                 gss_buffer_desc msg1, msg2;
107                 msg1.value = NULL;
108                 msg2.value = NULL;
109                 gss_display_status(&minor, status.err.rc, GSS_C_GSS_CODE,
110                                    GSS_C_NULL_OID, &msg_ctx, &msg1);
111                 gss_display_status(&minor, status.minor_status, GSS_C_MECH_CODE,
112                                    GSS_C_NULL_OID, &msg_ctx, &msg2);
113                 asprintf(&ret, "%s : %s", (char *)msg1.value, (char *)msg2.value);
114                 gss_release_buffer(&minor, &msg1);
115                 gss_release_buffer(&minor, &msg2);
116                 return ret;
117         }
118 #endif
119         case ADS_ERROR_NT: 
120                 return nt_errstr(ads_ntstatus(status));
121         default:
122                 return "Unknown ADS error type!? (not compiled in?)";
123         }
124
125 }
126
127