Coverage Report

Created: 2025-11-16 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/libcli/util/errmap_unix.c
Line
Count
Source
1
/*
2
 *  Unix SMB/CIFS implementation.
3
 *  error mapping functions
4
 *  Copyright (C) Andrew Tridgell 2001
5
 *  Copyright (C) Andrew Bartlett 2001
6
 *  Copyright (C) Tim Potter 2000
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
24
/* Mapping between Unix, and NT error numbers */
25
26
static const struct {
27
  int unix_error;
28
  NTSTATUS nt_error;
29
} unix_nt_errmap[] = {
30
  { EAGAIN,       STATUS_MORE_ENTRIES },
31
  { EINTR,        STATUS_MORE_ENTRIES },
32
  { ENOBUFS,      STATUS_MORE_ENTRIES },
33
#ifdef EWOULDBLOCK
34
  { EWOULDBLOCK,  STATUS_MORE_ENTRIES },
35
#endif
36
  { EINPROGRESS,  NT_STATUS_MORE_PROCESSING_REQUIRED },
37
  { EPERM,        NT_STATUS_ACCESS_DENIED },
38
  { EACCES,       NT_STATUS_ACCESS_DENIED },
39
  { ENOENT,       NT_STATUS_OBJECT_NAME_NOT_FOUND },
40
  { ENOTDIR,      NT_STATUS_NOT_A_DIRECTORY },
41
  { EIO,          NT_STATUS_IO_DEVICE_ERROR },
42
  { EBADF,        NT_STATUS_INVALID_HANDLE },
43
  { EINVAL,       NT_STATUS_INVALID_PARAMETER },
44
  { EEXIST,       NT_STATUS_OBJECT_NAME_COLLISION},
45
  { ENFILE,       NT_STATUS_TOO_MANY_OPENED_FILES },
46
  { EMFILE,       NT_STATUS_TOO_MANY_OPENED_FILES },
47
  { ENOSPC,       NT_STATUS_DISK_FULL },
48
  { ENOTSOCK,     NT_STATUS_INVALID_HANDLE },
49
  { EFAULT,       NT_STATUS_INVALID_PARAMETER },
50
  { EMSGSIZE,     NT_STATUS_INVALID_BUFFER_SIZE },
51
  { ENOMEM,       NT_STATUS_NO_MEMORY },
52
  { EISDIR,       NT_STATUS_FILE_IS_A_DIRECTORY},
53
#ifdef EPIPE
54
  { EPIPE,        NT_STATUS_CONNECTION_DISCONNECTED },
55
#endif
56
  { EBUSY,        NT_STATUS_SHARING_VIOLATION },
57
  { ENOSYS, NT_STATUS_INVALID_SYSTEM_SERVICE },
58
#ifdef EOPNOTSUPP
59
  { EOPNOTSUPP,   NT_STATUS_NOT_SUPPORTED},
60
#endif
61
  { EMLINK,       NT_STATUS_TOO_MANY_LINKS },
62
  { ENOSYS,       NT_STATUS_NOT_SUPPORTED },
63
#ifdef ELOOP
64
  { ELOOP,        NT_STATUS_OBJECT_PATH_NOT_FOUND },
65
#endif
66
#ifdef ENODATA
67
  { ENODATA,      NT_STATUS_NOT_FOUND },
68
#endif
69
#ifdef EFTYPE
70
  { EFTYPE,       NT_STATUS_OBJECT_PATH_NOT_FOUND },
71
#endif
72
#ifdef EDQUOT
73
  { EDQUOT,       NT_STATUS_DISK_FULL }, /* Windows apps need this, not NT_STATUS_QUOTA_EXCEEDED */
74
#endif
75
#ifdef ENOTEMPTY
76
  { ENOTEMPTY,    NT_STATUS_DIRECTORY_NOT_EMPTY },
77
#endif
78
#ifdef EXDEV
79
  { EXDEV,        NT_STATUS_NOT_SAME_DEVICE },
80
#endif
81
#ifdef EROFS
82
  { EROFS,        NT_STATUS_MEDIA_WRITE_PROTECTED },
83
#endif
84
#ifdef ENAMETOOLONG
85
  { ENAMETOOLONG, NT_STATUS_NAME_TOO_LONG },
86
#endif
87
#ifdef EFBIG
88
  { EFBIG,        NT_STATUS_DISK_FULL },
89
#endif
90
#ifdef EADDRINUSE
91
  { EADDRINUSE,   NT_STATUS_ADDRESS_ALREADY_ASSOCIATED},
92
#endif
93
#ifdef ENETUNREACH
94
  { ENETUNREACH,  NT_STATUS_NETWORK_UNREACHABLE},
95
#endif
96
#ifdef EHOSTUNREACH
97
        { EHOSTUNREACH, NT_STATUS_HOST_UNREACHABLE},
98
#endif
99
#ifdef ECONNREFUSED
100
  { ECONNREFUSED, NT_STATUS_CONNECTION_REFUSED},
101
#endif
102
#ifdef EADDRNOTAVAIL
103
  { EADDRNOTAVAIL,NT_STATUS_ADDRESS_NOT_ASSOCIATED },
104
#endif
105
#ifdef ETIMEDOUT
106
  { ETIMEDOUT,    NT_STATUS_IO_TIMEOUT},
107
#endif
108
#ifdef ESOCKTNOSUPPORT
109
  { ESOCKTNOSUPPORT,NT_STATUS_INVALID_PARAMETER_MIX },
110
#endif
111
#ifdef EAFNOSUPPORT
112
  { EAFNOSUPPORT, NT_STATUS_INVALID_PARAMETER_MIX },
113
#endif
114
#ifdef ECONNABORTED
115
  { ECONNABORTED, NT_STATUS_CONNECTION_ABORTED},
116
#endif
117
#ifdef ECONNRESET
118
  { ECONNRESET,   NT_STATUS_CONNECTION_RESET},
119
#endif
120
#ifdef ENOPROTOOPT
121
  { ENOPROTOOPT,  NT_STATUS_INVALID_PARAMETER_MIX },
122
#endif
123
#ifdef ENODEV
124
  { ENODEV, NT_STATUS_NO_SUCH_DEVICE },
125
#endif
126
#ifdef ENOATTR
127
  { ENOATTR,      NT_STATUS_NOT_FOUND },
128
#endif
129
#ifdef ECANCELED
130
  { ECANCELED,    NT_STATUS_CANCELLED},
131
#endif
132
#ifdef ENOTSUP
133
        { ENOTSUP,      NT_STATUS_NOT_SUPPORTED},
134
#endif
135
#ifdef EPROTONOSUPPORT
136
  { EPROTONOSUPPORT, NT_STATUS_PROTOCOL_NOT_SUPPORTED },
137
#endif
138
139
  { 0, NT_STATUS_UNSUCCESSFUL }
140
};
141
142
143
/*********************************************************************
144
 Map an NT error code from a Unix error code.
145
*********************************************************************/
146
NTSTATUS map_nt_error_from_unix_common(int unix_error)
147
0
{
148
0
  size_t i;
149
150
  /* Look through list */
151
0
  for (i=0;i<ARRAY_SIZE(unix_nt_errmap);i++) {
152
0
    if (unix_nt_errmap[i].unix_error == unix_error) {
153
0
      return unix_nt_errmap[i].nt_error;
154
0
    }
155
0
  }
156
157
  /* Default return */
158
0
  return NT_STATUS_UNSUCCESSFUL;
159
0
}
160
161
/* Return a UNIX errno from a NT status code */
162
static const struct {
163
  NTSTATUS status;
164
  int error;
165
} nt_errno_map[] = {
166
  {NT_STATUS_ACCESS_VIOLATION, EACCES},
167
  {NT_STATUS_INVALID_HANDLE, EBADF},
168
  {NT_STATUS_ACCESS_DENIED, EACCES},
169
  {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
170
  {NT_STATUS_OBJECT_PATH_NOT_FOUND, ENOENT},
171
  {NT_STATUS_SHARING_VIOLATION, EBUSY},
172
  {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
173
  {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
174
  {NT_STATUS_PATH_NOT_COVERED, ENOENT},
175
  {NT_STATUS_UNSUCCESSFUL, EINVAL},
176
  {NT_STATUS_NOT_IMPLEMENTED, ENOSYS},
177
  {NT_STATUS_IN_PAGE_ERROR, EFAULT},
178
  {NT_STATUS_BAD_NETWORK_NAME, ENOENT},
179
#ifdef EDQUOT
180
  {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
181
  {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
182
  {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
183
  {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
184
#endif
185
#ifdef ETIME
186
  {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
187
#endif
188
  {NT_STATUS_INVALID_PARAMETER, EINVAL},
189
  {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
190
  {NT_STATUS_NO_SUCH_FILE, ENOENT},
191
#ifdef ENODATA
192
  {NT_STATUS_END_OF_FILE, ENODATA},
193
#endif
194
#ifdef ENOMEDIUM
195
  {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM},
196
  {NT_STATUS_NO_MEDIA, ENOMEDIUM},
197
#endif
198
  {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE},
199
  {NT_STATUS_NO_MEMORY, ENOMEM},
200
  {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
201
  {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
202
  {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
203
  {NT_STATUS_ACCESS_DENIED, EACCES},
204
  {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
205
  {NT_STATUS_WRONG_PASSWORD, EACCES},
206
  {NT_STATUS_LOGON_FAILURE, EACCES},
207
  {NT_STATUS_INVALID_WORKSTATION, EACCES},
208
  {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
209
  {NT_STATUS_PASSWORD_EXPIRED, EACCES},
210
  {NT_STATUS_ACCOUNT_DISABLED, EACCES},
211
  {NT_STATUS_DISK_FULL, ENOSPC},
212
  {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
213
  {NT_STATUS_PIPE_BUSY, EPIPE},
214
  {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
215
  {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
216
  {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
217
  {NT_STATUS_NOT_SUPPORTED, ENOSYS},
218
  {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
219
  {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
220
  {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
221
  {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
222
  {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
223
  {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
224
  {NT_STATUS_TOO_MANY_LINKS, EMLINK},
225
  {NT_STATUS_NETWORK_BUSY, EBUSY},
226
  {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
227
#ifdef ELIBACC
228
  {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
229
#endif
230
  {NT_STATUS_PIPE_BROKEN, EPIPE},
231
  {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
232
  {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
233
  {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
234
#ifdef EPROTO
235
  {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
236
#endif
237
  {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
238
  {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
239
  {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
240
  {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
241
  {NT_STATUS_PIPE_CONNECTED, EISCONN},
242
  {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
243
  {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
244
  {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
245
  {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
246
  {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
247
  {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
248
  {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
249
  {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
250
  {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
251
  {NT_STATUS_CONNECTION_RESET, ENETRESET},
252
#ifdef ENOTUNIQ
253
  {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
254
  {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
255
#endif
256
  {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
257
  {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
258
  {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
259
  {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
260
  {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
261
  {NT_STATUS_RETRY, EAGAIN},
262
#ifdef ENOTUNIQ
263
  {NT_STATUS_DUPLICATE_NAME, ENOTUNIQ},
264
#endif
265
#ifdef ECOMM
266
  {NT_STATUS_NET_WRITE_FAULT, ECOMM},
267
#endif
268
#ifdef EXDEV
269
  {NT_STATUS_NOT_SAME_DEVICE, EXDEV},
270
#endif
271
#ifdef ECANCELED
272
  {NT_STATUS_CANCELLED, ECANCELED},
273
#endif
274
};
275
276
/*********************************************************************
277
 Map a Unix error code from a NT error code.
278
*********************************************************************/
279
int map_errno_from_nt_status(NTSTATUS status)
280
0
{
281
0
  size_t i;
282
283
  /* Status codes without this bit set are not errors */
284
0
  if (!(NT_STATUS_V(status) & 0xc0000000)) {
285
0
    return 0;
286
0
  }
287
288
0
  for (i=0;i<ARRAY_SIZE(nt_errno_map);i++) {
289
0
    if (NT_STATUS_V(nt_errno_map[i].status) ==
290
0
          NT_STATUS_V(status)) {
291
0
      return nt_errno_map[i].error;
292
0
    }
293
0
  }
294
295
  /* for all other cases - a default code */
296
0
  return EINVAL;
297
0
}