summaryrefslogtreecommitdiffstats
path: root/source3/libsmb/clierror.c
blob: a53c9c4e1e8d2311dbe64d319d111c288386f459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* 
   Unix SMB/CIFS implementation.
   client error handling routines
   Copyright (C) Andrew Tridgell 1994-1998
   Copyright (C) Jelmer Vernooij 2003
   Copyright (C) Jeremy Allison 2006

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libsmb/libsmb.h"
#include "../libcli/smb/smbXcli_base.h"

/***************************************************************************
 Return an error message - either an NT error, SMB error or a RAP error.
 Note some of the NT errors are actually warnings or "informational" errors
 in which case they can be safely ignored.
****************************************************************************/

const char *cli_errstr(struct cli_state *cli)
{   
	fstring cli_error_message;
	char *result;

	if (!cli->initialised) {
		fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
		goto done;
	}

	/* Case #1: RAP error */
	if (cli->rap_error) {
		strlcpy(cli_error_message, win_errstr(W_ERROR(cli->rap_error)),
			sizeof(cli_error_message));
		goto done;
	}

	if (!cli_state_is_connected(cli) && NT_STATUS_IS_OK(cli->raw_status)) {
		return nt_errstr(NT_STATUS_CONNECTION_DISCONNECTED);
	}

	return nt_errstr(cli->raw_status);
 done:
	result = talloc_strdup(talloc_tos(), cli_error_message);
	SMB_ASSERT(result);
	return result;
}


/****************************************************************************
 Return the 32-bit NT status code from the last packet.
****************************************************************************/

NTSTATUS cli_nt_error(struct cli_state *cli)
{
	/* Deal with socket errors first. */
	if (!cli_state_is_connected(cli)) {
		return NT_STATUS_CONNECTION_DISCONNECTED;
	}

	if (NT_STATUS_IS_DOS(cli->raw_status)) {
		int e_class = NT_STATUS_DOS_CLASS(cli->raw_status);
		int code = NT_STATUS_DOS_CODE(cli->raw_status);
		return dos_to_ntstatus(e_class, code);
	}

	return cli->raw_status;
}


/****************************************************************************
 Return the DOS error from the last packet - an error class and an error
 code.
****************************************************************************/

void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
{
	if (!cli_state_is_connected(cli)) {
		*eclass = ERRDOS;
		*ecode = ERRnotconnected;
		return;
	}

	if (!NT_STATUS_IS_DOS(cli->raw_status)) {
		ntstatus_to_dos(cli->raw_status, eclass, ecode);
		return;
	}

	*eclass = NT_STATUS_DOS_CLASS(cli->raw_status);
	*ecode = NT_STATUS_DOS_CODE(cli->raw_status);
}


/* Return a UNIX errno appropriate for the error received in the last
   packet. */

int cli_errno(struct cli_state *cli)
{
	NTSTATUS status;

	if (cli_is_nt_error(cli)) {
		status = cli_nt_error(cli);
		return map_errno_from_nt_status(status);
	}

        if (cli_is_dos_error(cli)) {
                uint8 eclass;
                uint32 ecode;

                cli_dos_error(cli, &eclass, &ecode);
		status = dos_to_ntstatus(eclass, ecode);
		return map_errno_from_nt_status(status);
        }

        /*
         * Yuck!  A special case for this Vista error.  Since its high-order
         * byte isn't 0xc0, it doesn't match cli_is_nt_error() above.
         */
        status = cli_nt_error(cli);
        if (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT)) {
            return EACCES;
        }

	/* for other cases */
	return EINVAL;
}

/* Return true if the last packet was in error */

bool cli_is_error(struct cli_state *cli)
{
	/* A socket error is always an error. */
	if (!cli_state_is_connected(cli)) {
		return true;
	}

	if (NT_STATUS_IS_DOS(cli->raw_status)) {
		/* Return error if error class in non-zero */
		uint8_t rcls = NT_STATUS_DOS_CLASS(cli->raw_status);
		return rcls != 0;
	}

	return NT_STATUS_IS_ERR(cli->raw_status);
}

/* Return true if the last error was an NT error */

bool cli_is_nt_error(struct cli_state *cli)
{
	/* A socket error is always an NT error. */
	if (!cli_state_is_connected(cli)) {
		return true;
	}

	return cli_is_error(cli) && !NT_STATUS_IS_DOS(cli->raw_status);
}

/* Return true if the last error was a DOS error */

bool cli_is_dos_error(struct cli_state *cli)
{
	/* A socket error is always a DOS error. */
	if (!cli_state_is_connected(cli)) {
		return true;
	}

	return cli_is_error(cli) && NT_STATUS_IS_DOS(cli->raw_status);
}

bool cli_state_is_connected(struct cli_state *cli)
{
	if (cli == NULL) {
		return false;
	}

	if (!cli->initialised) {
		return false;
	}

	return smbXcli_conn_is_connected(cli->conn);
}