py:dcerpc/raw_protocol: explicitly disconnect additional connections
[Samba.git] / source3 / libsmb / clierror.c
blob66dfea0654f9a5f852b839dd5f12e76568fe652d
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"
24 #include "../libcli/smb/smbXcli_base.h"
26 /***************************************************************************
27 Return an error message - either an NT error, SMB error or a RAP error.
28 Note some of the NT errors are actually warnings or "informational" errors
29 in which case they can be safely ignored.
30 ****************************************************************************/
32 const char *cli_errstr(struct cli_state *cli)
34 fstring cli_error_message;
35 char *result;
37 if (!cli->initialised) {
38 fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on uninitialized cli_stat struct!\n");
39 goto done;
42 /* Case #1: RAP error */
43 if (cli->rap_error) {
44 strlcpy(cli_error_message, win_errstr(W_ERROR(cli->rap_error)),
45 sizeof(cli_error_message));
46 goto done;
49 if (!cli_state_is_connected(cli) && NT_STATUS_IS_OK(cli->raw_status)) {
50 return nt_errstr(NT_STATUS_CONNECTION_DISCONNECTED);
53 return nt_errstr(cli->raw_status);
54 done:
55 result = talloc_strdup(talloc_tos(), cli_error_message);
56 SMB_ASSERT(result);
57 return result;
61 /****************************************************************************
62 Return the 32-bit NT status code from the last packet.
63 ****************************************************************************/
65 NTSTATUS cli_nt_error(struct cli_state *cli)
67 /* Deal with socket errors first. */
68 if (!cli_state_is_connected(cli)) {
69 return NT_STATUS_CONNECTION_DISCONNECTED;
72 if (NT_STATUS_IS_DOS(cli->raw_status)) {
73 int e_class = NT_STATUS_DOS_CLASS(cli->raw_status);
74 int code = NT_STATUS_DOS_CODE(cli->raw_status);
75 return dos_to_ntstatus(e_class, code);
78 return cli->raw_status;
82 /****************************************************************************
83 Return the DOS error from the last packet - an error class and an error
84 code.
85 ****************************************************************************/
87 void cli_dos_error(struct cli_state *cli, uint8_t *eclass, uint32_t *ecode)
89 if (!cli_state_is_connected(cli)) {
90 *eclass = ERRDOS;
91 *ecode = ERRnotconnected;
92 return;
95 if (!NT_STATUS_IS_DOS(cli->raw_status)) {
96 ntstatus_to_dos(cli->raw_status, eclass, ecode);
97 return;
100 *eclass = NT_STATUS_DOS_CLASS(cli->raw_status);
101 *ecode = NT_STATUS_DOS_CODE(cli->raw_status);
105 /* Return a UNIX errno appropriate for the error received in the last
106 packet. */
108 int cli_errno(struct cli_state *cli)
110 NTSTATUS status;
112 if (cli_is_nt_error(cli)) {
113 status = cli_nt_error(cli);
114 return map_errno_from_nt_status(status);
117 if (cli_is_dos_error(cli)) {
118 uint8_t eclass;
119 uint32_t ecode;
121 cli_dos_error(cli, &eclass, &ecode);
122 status = dos_to_ntstatus(eclass, ecode);
123 return map_errno_from_nt_status(status);
127 * Yuck! A special case for this Vista error. Since its high-order
128 * byte isn't 0xc0, it doesn't match cli_is_nt_error() above.
130 status = cli_nt_error(cli);
131 if (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT)) {
132 return EACCES;
135 /* for other cases */
136 return EINVAL;
139 /* Return true if the last packet was in error */
141 bool cli_is_error(struct cli_state *cli)
143 /* A socket error is always an error. */
144 if (!cli_state_is_connected(cli)) {
145 return true;
148 if (NT_STATUS_IS_DOS(cli->raw_status)) {
149 /* Return error if error class in non-zero */
150 uint8_t rcls = NT_STATUS_DOS_CLASS(cli->raw_status);
151 return rcls != 0;
154 return NT_STATUS_IS_ERR(cli->raw_status);
157 /* Return true if the last error was an NT error */
159 bool cli_is_nt_error(struct cli_state *cli)
161 /* A socket error is always an NT error. */
162 if (!cli_state_is_connected(cli)) {
163 return true;
166 return cli_is_error(cli) && !NT_STATUS_IS_DOS(cli->raw_status);
169 /* Return true if the last error was a DOS error */
171 bool cli_is_dos_error(struct cli_state *cli)
173 /* A socket error is always a DOS error. */
174 if (!cli_state_is_connected(cli)) {
175 return true;
178 return cli_is_error(cli) && NT_STATUS_IS_DOS(cli->raw_status);
181 bool cli_state_is_connected(struct cli_state *cli)
183 if (cli == NULL) {
184 return false;
187 if (!cli->initialised) {
188 return false;
191 return smbXcli_conn_is_connected(cli->conn);