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/>.
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
;
36 if (!cli
->initialised
) {
37 fstrcpy(cli_error_message
, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
41 /* Case #1: RAP error */
43 strlcpy(cli_error_message
, win_errstr(W_ERROR(cli
->rap_error
)),
44 sizeof(cli_error_message
));
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
);
54 result
= talloc_strdup(talloc_tos(), cli_error_message
);
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
84 ****************************************************************************/
86 void cli_dos_error(struct cli_state
*cli
, uint8
*eclass
, uint32
*ecode
)
88 if (!cli_state_is_connected(cli
)) {
90 *ecode
= ERRnotconnected
;
94 if (!NT_STATUS_IS_DOS(cli
->raw_status
)) {
95 ntstatus_to_dos(cli
->raw_status
, eclass
, ecode
);
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
107 int cli_errno(struct cli_state
*cli
)
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
)) {
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
)) {
134 /* for other cases */
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
)) {
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
);
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
)) {
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
)) {
177 return cli_is_error(cli
) && NT_STATUS_IS_DOS(cli
->raw_status
);
180 bool cli_state_is_connected(struct cli_state
*cli
)
186 if (!cli
->initialised
) {
197 void cli_state_disconnect(struct cli_state
*cli
)