cmd: DIR command outputs free space for the path.
[wine.git] / dlls / msv1_0 / unixlib.c
blob779250fc650a1f8d9297d4296ba5e6b1549ecbc7
1 /*
2 * Unix interface for ntlm_auth
4 * Copyright 2005, 2006 Kai Blin
5 * Copyright 2021 Hans Leidekker for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #if 0
23 #pragma makedep unix
24 #endif
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <sys/wait.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winternl.h"
36 #include "winbase.h"
37 #include "sspi.h"
39 #include "wine/debug.h"
40 #include "unixlib.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
43 WINE_DECLARE_DEBUG_CHANNEL(winediag);
45 #define INITIAL_BUFFER_SIZE 200
47 struct com_buf
49 char *buffer;
50 unsigned int size;
51 unsigned int offset;
54 static SECURITY_STATUS read_line( struct ntlm_ctx *ctx, unsigned int *offset )
56 char *newline;
57 struct com_buf *com_buf = (struct com_buf *)(ULONG_PTR)ctx->com_buf;
59 if (!com_buf)
61 if (!(com_buf = malloc( sizeof(*com_buf) ))) return SEC_E_INSUFFICIENT_MEMORY;
62 if (!(com_buf->buffer = malloc( INITIAL_BUFFER_SIZE )))
64 free( com_buf );
65 return SEC_E_INSUFFICIENT_MEMORY;
67 com_buf->size = INITIAL_BUFFER_SIZE;
68 com_buf->offset = 0;
69 ctx->com_buf = (ULONG_PTR)com_buf;
74 ssize_t size;
75 if (com_buf->offset + INITIAL_BUFFER_SIZE > com_buf->size)
77 char *buf = realloc( com_buf->buffer, com_buf->size + INITIAL_BUFFER_SIZE );
78 if (!buf) return SEC_E_INSUFFICIENT_MEMORY;
79 com_buf->size += INITIAL_BUFFER_SIZE;
80 com_buf->buffer = buf;
82 size = read( ctx->pipe_in, com_buf->buffer + com_buf->offset, com_buf->size - com_buf->offset );
83 if (size <= 0) return SEC_E_INTERNAL_ERROR;
85 com_buf->offset += size;
86 newline = memchr( com_buf->buffer, '\n', com_buf->offset );
87 } while (!newline);
89 /* if there's a newline character, and we read more than that newline, we have to store the offset so we can
90 preserve the additional data */
91 if (newline != com_buf->buffer + com_buf->offset) *offset = (com_buf->buffer + com_buf->offset) - (newline + 1);
92 else *offset = 0;
94 *newline = 0;
95 return SEC_E_OK;
98 static NTSTATUS ntlm_chat( void *args )
100 const struct chat_params *params = args;
101 struct ntlm_ctx *ctx = params->ctx;
102 struct com_buf *com_buf;
103 SECURITY_STATUS status = SEC_E_OK;
104 unsigned int offset;
106 write( ctx->pipe_out, params->buf, strlen(params->buf) );
107 write( ctx->pipe_out, "\n", 1 );
109 if ((status = read_line( ctx, &offset )) != SEC_E_OK) return status;
110 com_buf = (struct com_buf *)(ULONG_PTR)ctx->com_buf;
111 *params->retlen = strlen( com_buf->buffer );
113 if (*params->retlen > params->buflen) return SEC_E_BUFFER_TOO_SMALL;
114 if (*params->retlen < 2) return SEC_E_ILLEGAL_MESSAGE;
115 if (!strncmp( com_buf->buffer, "ERR", 3 )) return SEC_E_INVALID_TOKEN;
117 memcpy( params->buf, com_buf->buffer, *params->retlen + 1 );
119 if (!offset) com_buf->offset = 0;
120 else
122 memmove( com_buf->buffer, com_buf->buffer + com_buf->offset, offset );
123 com_buf->offset = offset;
126 return SEC_E_OK;
129 static NTSTATUS ntlm_cleanup( void *args )
131 struct ntlm_ctx *ctx = args;
132 struct com_buf *com_buf = (struct com_buf *)(ULONG_PTR)ctx->com_buf;
134 if (!ctx || (ctx->mode != MODE_CLIENT && ctx->mode != MODE_SERVER)) return STATUS_INVALID_HANDLE;
135 ctx->mode = MODE_INVALID;
137 /* closing stdin will terminate ntlm_auth */
138 close( ctx->pipe_out );
139 close( ctx->pipe_in );
141 if (ctx->pid > 0) /* reap child */
143 pid_t ret;
144 do {
145 ret = waitpid( ctx->pid, NULL, 0 );
146 } while (ret < 0 && errno == EINTR);
149 if (com_buf) free( com_buf->buffer );
150 free( com_buf );
151 return STATUS_SUCCESS;
154 static NTSTATUS ntlm_fork( void *args )
156 const struct fork_params *params = args;
157 struct ntlm_ctx *ctx = params->ctx;
158 int pipe_in[2], pipe_out[2];
160 #ifdef HAVE_PIPE2
161 if (pipe2( pipe_in, O_CLOEXEC ) < 0)
162 #endif
164 if (pipe( pipe_in ) < 0 ) return SEC_E_INTERNAL_ERROR;
165 fcntl( pipe_in[0], F_SETFD, FD_CLOEXEC );
166 fcntl( pipe_in[1], F_SETFD, FD_CLOEXEC );
168 #ifdef HAVE_PIPE2
169 if (pipe2( pipe_out, O_CLOEXEC ) < 0)
170 #endif
172 if (pipe( pipe_out ) < 0)
174 close( pipe_in[0] );
175 close( pipe_in[1] );
176 return SEC_E_INTERNAL_ERROR;
178 fcntl( pipe_out[0], F_SETFD, FD_CLOEXEC );
179 fcntl( pipe_out[1], F_SETFD, FD_CLOEXEC );
182 if (!(ctx->pid = fork())) /* child */
184 dup2( pipe_out[0], 0 );
185 close( pipe_out[0] );
186 close( pipe_out[1] );
188 dup2( pipe_in[1], 1 );
189 close( pipe_in[0] );
190 close( pipe_in[1] );
192 execvp( params->argv[0], params->argv );
194 write( 1, "BH\n", 3 );
195 _exit( 1 );
197 else
199 ctx->pipe_in = pipe_in[0];
200 close( pipe_in[1] );
201 ctx->pipe_out = pipe_out[1];
202 close( pipe_out[0] );
205 return SEC_E_OK;
208 static NTSTATUS ntlm_check_version( void *args )
210 struct ntlm_ctx ctx = { 0 };
211 char *argv[3], buf[80];
212 NTSTATUS status = STATUS_DLL_NOT_FOUND;
213 struct fork_params params = { &ctx, argv };
214 int len;
216 argv[0] = (char *)"ntlm_auth";
217 argv[1] = (char *)"--version";
218 argv[2] = NULL;
219 if (ntlm_fork( &params ) != SEC_E_OK) return status;
221 if ((len = read( ctx.pipe_in, buf, sizeof(buf) - 1 )) > 8)
223 char *newline;
225 if ((newline = memchr( buf, '\n', len ))) *newline = 0;
226 else buf[len] = 0;
228 TRACE( "detected ntlm_auth version %s\n", debugstr_a(buf) );
229 status = STATUS_SUCCESS;
232 if (status) ERR_(winediag)( "ntlm_auth was not found. Make sure that ntlm_auth >= 3.0.25 is in your path. "
233 "Usually, you can find it in the winbind package of your distribution.\n" );
234 ntlm_cleanup( &ctx );
235 return status;
238 const unixlib_entry_t __wine_unix_call_funcs[] =
240 ntlm_chat,
241 ntlm_cleanup,
242 ntlm_fork,
243 ntlm_check_version,
246 #ifdef _WIN64
248 typedef ULONG PTR32;
250 static NTSTATUS wow64_ntlm_chat( void *args )
252 struct
254 PTR32 ctx;
255 PTR32 buf;
256 UINT buflen;
257 PTR32 retlen;
258 } const *params32 = args;
260 struct chat_params params =
262 ULongToPtr(params32->ctx),
263 ULongToPtr(params32->buf),
264 params32->buflen,
265 ULongToPtr(params32->retlen)
268 return ntlm_chat( &params );
271 static NTSTATUS wow64_ntlm_fork( void *args )
273 struct
275 PTR32 ctx;
276 PTR32 argv;
277 } const *params32 = args;
279 struct fork_params params;
280 PTR32 *argv32 = ULongToPtr(params32->argv);
281 char **argv;
282 NTSTATUS ret;
283 int i, argc = 0;
285 while (argv32[argc]) argc++;
286 argv = malloc( (argc + 1) * sizeof(*argv) );
287 for (i = 0; i <= argc; i++) argv[i] = ULongToPtr( argv32[i] );
289 params.ctx = ULongToPtr(params32->ctx);
290 params.argv = argv;
291 ret = ntlm_fork( &params );
292 free( argv );
293 return ret;
296 const unixlib_entry_t __wine_unix_call_wow64_funcs[] =
298 wow64_ntlm_chat,
299 ntlm_cleanup,
300 wow64_ntlm_fork,
301 ntlm_check_version,
304 #endif /* _WIN64 */