s3:libsmb: move cli->mid to cli->smb1.mid
[Samba/vl.git] / source3 / libsmb / clierror.c
blob92c2cc28154ed97ae1ab810fab941146cc7c49e8
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
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.
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.
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/>.
22 #include "includes.h"
23 #include "libsmb/libsmb.h"
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 ****************************************************************************/
31 const char *cli_errstr(struct cli_state *cli)
33 fstring cli_error_message;
34 char *result;
36 if (!cli->initialised) {
37 fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
38 goto done;
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;
48 if (!cli_state_is_connected(cli) && NT_STATUS_IS_OK(cli->raw_status)) {
49 return nt_errstr(NT_STATUS_CONNECTION_DISCONNECTED);
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;
60 /****************************************************************************
61 Return the 32-bit NT status code from the last packet.
62 ****************************************************************************/
64 NTSTATUS cli_nt_error(struct cli_state *cli)
66 /* Deal with socket errors first. */
67 if (!cli_state_is_connected(cli)) {
68 return NT_STATUS_CONNECTION_DISCONNECTED;
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);
77 return cli->raw_status;
81 /****************************************************************************
82 Return the DOS error from the last packet - an error class and an error
83 code.
84 ****************************************************************************/
86 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
88 if (!cli_state_is_connected(cli)) {
89 *eclass = ERRDOS;
90 *ecode = ERRnotconnected;
91 return;
94 if (!NT_STATUS_IS_DOS(cli->raw_status)) {
95 ntstatus_to_dos(cli->raw_status, eclass, ecode);
96 return;
99 *eclass = NT_STATUS_DOS_CLASS(cli->raw_status);
100 *ecode = NT_STATUS_DOS_CODE(cli->raw_status);
104 /* Return a UNIX errno appropriate for the error received in the last
105 packet. */
107 int cli_errno(struct cli_state *cli)
109 NTSTATUS status;
111 if (cli_is_nt_error(cli)) {
112 status = cli_nt_error(cli);
113 return map_errno_from_nt_status(status);
116 if (cli_is_dos_error(cli)) {
117 uint8 eclass;
118 uint32 ecode;
120 cli_dos_error(cli, &eclass, &ecode);
121 status = dos_to_ntstatus(eclass, ecode);
122 return map_errno_from_nt_status(status);
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.
129 status = cli_nt_error(cli);
130 if (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT)) {
131 return EACCES;
134 /* for other cases */
135 return EINVAL;
138 /* Return true if the last packet was in error */
140 bool cli_is_error(struct cli_state *cli)
142 /* A socket error is always an error. */
143 if (!cli_state_is_connected(cli)) {
144 return true;
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;
153 return NT_STATUS_IS_ERR(cli->raw_status);
156 /* Return true if the last error was an NT error */
158 bool cli_is_nt_error(struct cli_state *cli)
160 /* A socket error is always an NT error. */
161 if (!cli_state_is_connected(cli)) {
162 return true;
165 return cli_is_error(cli) && !NT_STATUS_IS_DOS(cli->raw_status);
168 /* Return true if the last error was a DOS error */
170 bool cli_is_dos_error(struct cli_state *cli)
172 /* A socket error is always a DOS error. */
173 if (!cli_state_is_connected(cli)) {
174 return true;
177 return cli_is_error(cli) && NT_STATUS_IS_DOS(cli->raw_status);
180 bool cli_state_is_connected(struct cli_state *cli)
182 if (cli == NULL) {
183 return false;
186 if (!cli->initialised) {
187 return false;
190 if (cli->fd == -1) {
191 return false;
194 return true;
197 void cli_state_disconnect(struct cli_state *cli)
199 if (cli->fd != -1) {
200 close(cli->fd);
202 cli->fd = -1;