s3:libsmb: move cli_state->fd to cli_state->conn.fd
[kai/samba.git] / source3 / libsmb / clierror.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client error handling routines
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jelmer Vernooij 2003
6    Copyright (C) Jeremy Allison 2006
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libsmb/libsmb.h"
24
25 /***************************************************************************
26  Return an error message - either an NT error, SMB error or a RAP error.
27  Note some of the NT errors are actually warnings or "informational" errors
28  in which case they can be safely ignored.
29 ****************************************************************************/
30
31 const char *cli_errstr(struct cli_state *cli)
32 {   
33         fstring cli_error_message;
34         char *result;
35
36         if (!cli->initialised) {
37                 fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
38                 goto done;
39         }
40
41         /* Case #1: RAP error */
42         if (cli->rap_error) {
43                 strlcpy(cli_error_message, win_errstr(W_ERROR(cli->rap_error)),
44                         sizeof(cli_error_message));
45                 goto done;
46         }
47
48         if (!cli_state_is_connected(cli) && NT_STATUS_IS_OK(cli->raw_status)) {
49                 return nt_errstr(NT_STATUS_CONNECTION_DISCONNECTED);
50         }
51
52         return nt_errstr(cli->raw_status);
53  done:
54         result = talloc_strdup(talloc_tos(), cli_error_message);
55         SMB_ASSERT(result);
56         return result;
57 }
58
59
60 /****************************************************************************
61  Return the 32-bit NT status code from the last packet.
62 ****************************************************************************/
63
64 NTSTATUS cli_nt_error(struct cli_state *cli)
65 {
66         /* Deal with socket errors first. */
67         if (!cli_state_is_connected(cli)) {
68                 return NT_STATUS_CONNECTION_DISCONNECTED;
69         }
70
71         if (NT_STATUS_IS_DOS(cli->raw_status)) {
72                 int e_class = NT_STATUS_DOS_CLASS(cli->raw_status);
73                 int code = NT_STATUS_DOS_CODE(cli->raw_status);
74                 return dos_to_ntstatus(e_class, code);
75         }
76
77         return cli->raw_status;
78 }
79
80
81 /****************************************************************************
82  Return the DOS error from the last packet - an error class and an error
83  code.
84 ****************************************************************************/
85
86 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
87 {
88         if (!cli_state_is_connected(cli)) {
89                 *eclass = ERRDOS;
90                 *ecode = ERRnotconnected;
91                 return;
92         }
93
94         if (!NT_STATUS_IS_DOS(cli->raw_status)) {
95                 ntstatus_to_dos(cli->raw_status, eclass, ecode);
96                 return;
97         }
98
99         *eclass = NT_STATUS_DOS_CLASS(cli->raw_status);
100         *ecode = NT_STATUS_DOS_CODE(cli->raw_status);
101 }
102
103
104 /* Return a UNIX errno appropriate for the error received in the last
105    packet. */
106
107 int cli_errno(struct cli_state *cli)
108 {
109         NTSTATUS status;
110
111         if (cli_is_nt_error(cli)) {
112                 status = cli_nt_error(cli);
113                 return map_errno_from_nt_status(status);
114         }
115
116         if (cli_is_dos_error(cli)) {
117                 uint8 eclass;
118                 uint32 ecode;
119
120                 cli_dos_error(cli, &eclass, &ecode);
121                 status = dos_to_ntstatus(eclass, ecode);
122                 return map_errno_from_nt_status(status);
123         }
124
125         /*
126          * Yuck!  A special case for this Vista error.  Since its high-order
127          * byte isn't 0xc0, it doesn't match cli_is_nt_error() above.
128          */
129         status = cli_nt_error(cli);
130         if (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT)) {
131             return EACCES;
132         }
133
134         /* for other cases */
135         return EINVAL;
136 }
137
138 /* Return true if the last packet was in error */
139
140 bool cli_is_error(struct cli_state *cli)
141 {
142         /* A socket error is always an error. */
143         if (!cli_state_is_connected(cli)) {
144                 return true;
145         }
146
147         if (NT_STATUS_IS_DOS(cli->raw_status)) {
148                 /* Return error if error class in non-zero */
149                 uint8_t rcls = NT_STATUS_DOS_CLASS(cli->raw_status);
150                 return rcls != 0;
151         }
152
153         return NT_STATUS_IS_ERR(cli->raw_status);
154 }
155
156 /* Return true if the last error was an NT error */
157
158 bool cli_is_nt_error(struct cli_state *cli)
159 {
160         /* A socket error is always an NT error. */
161         if (!cli_state_is_connected(cli)) {
162                 return true;
163         }
164
165         return cli_is_error(cli) && !NT_STATUS_IS_DOS(cli->raw_status);
166 }
167
168 /* Return true if the last error was a DOS error */
169
170 bool cli_is_dos_error(struct cli_state *cli)
171 {
172         /* A socket error is always a DOS error. */
173         if (!cli_state_is_connected(cli)) {
174                 return true;
175         }
176
177         return cli_is_error(cli) && NT_STATUS_IS_DOS(cli->raw_status);
178 }
179
180 bool cli_state_is_connected(struct cli_state *cli)
181 {
182         if (cli == NULL) {
183                 return false;
184         }
185
186         if (!cli->initialised) {
187                 return false;
188         }
189
190         if (cli->conn.fd == -1) {
191                 return false;
192         }
193
194         return true;
195 }
196
197 void cli_state_disconnect(struct cli_state *cli)
198 {
199         if (cli->conn.fd != -1) {
200                 close(cli->conn.fd);
201         }
202         cli->conn.fd = -1;
203 }