| 1 | /*
|
|---|
| 2 | Netdrive Samba client plugin
|
|---|
| 3 | samba library wrappers
|
|---|
| 4 | Copyright (C) netlabs.org 2003-2008
|
|---|
| 5 |
|
|---|
| 6 | This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 9 | (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | This program is distributed in the hope that it will be useful,
|
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with this program; if not, write to the Free Software
|
|---|
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "includes.h"
|
|---|
| 22 | #include "rpc_client/cli_pipe.h"
|
|---|
| 23 | #include "librpc/gen_ndr/ndr_srvsvc_c.h"
|
|---|
| 24 | #include "libsmb/libsmb.h"
|
|---|
| 25 | #include "libsmb/clirap.h"
|
|---|
| 26 | #include "param.h"
|
|---|
| 27 | #include "smb/smbXcli_base.h"
|
|---|
| 28 | #include "trans2.h"
|
|---|
| 29 | #include "smbwrp.h"
|
|---|
| 30 | #include "util.h"
|
|---|
| 31 |
|
|---|
| 32 | struct smb2_hnd {
|
|---|
| 33 | uint64_t fid_persistent;
|
|---|
| 34 | uint64_t fid_volatile;
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| 37 | /*
|
|---|
| 38 | * Wrapper for cli_errno to return not connected error on negative fd
|
|---|
| 39 | * Now returns an OS/2 return code instead of lerrno.
|
|---|
| 40 | */
|
|---|
| 41 | int os2cli_errno(cli_state * cli)
|
|---|
| 42 | {
|
|---|
| 43 | #if 0 // cli->fd not available in Samba 4.x
|
|---|
| 44 | if (cli->fd == -1)
|
|---|
| 45 | {
|
|---|
| 46 | return maperror( ENOTCONN);
|
|---|
| 47 | }
|
|---|
| 48 | #endif
|
|---|
| 49 | return maperror(cli_errno(cli));
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | void smbwrp_Logging()
|
|---|
| 53 | {
|
|---|
| 54 | char slogfile[_MAX_PATH +1] = {0};
|
|---|
| 55 | char slogfilename[] = "log.smbc";
|
|---|
| 56 | char *env = getenv("LOGFILES");
|
|---|
| 57 | if (env != NULL)
|
|---|
| 58 | {
|
|---|
| 59 | strncpy(slogfile, env, sizeof(slogfile) -1);
|
|---|
| 60 | strncat(slogfile, "\\", sizeof(slogfile) - strlen(slogfile) -1);
|
|---|
| 61 | strncat(slogfile, slogfilename, sizeof(slogfile) - strlen(slogfile) -1);
|
|---|
| 62 | }
|
|---|
| 63 | else
|
|---|
| 64 | {
|
|---|
| 65 | strncpy(slogfile, slogfilename, sizeof(slogfile) -1);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | // init samba for debug messages
|
|---|
| 69 | setup_logging(slogfile, DEBUG_FILE);
|
|---|
| 70 | lp_set_logfile(slogfile);
|
|---|
| 71 | reopen_logs();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | const char * smbwrp_getVersion()
|
|---|
| 75 | {
|
|---|
| 76 | return samba_version_string();
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | int _System smbwrp_getclisize(void)
|
|---|
| 80 | {
|
|---|
| 81 | return sizeof(struct cli_state);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /*****************************************************
|
|---|
| 85 | initialise structures
|
|---|
| 86 | *******************************************************/
|
|---|
| 87 | int _System smbwrp_init(void)
|
|---|
| 88 | {
|
|---|
| 89 | static int initialised = 0;
|
|---|
| 90 | char *p;
|
|---|
| 91 |
|
|---|
| 92 | struct loadparm_context *lp_ctx;
|
|---|
| 93 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 94 |
|
|---|
| 95 | if (initialised)
|
|---|
| 96 | {
|
|---|
| 97 | return 0;
|
|---|
| 98 | }
|
|---|
| 99 | initialised = 1;
|
|---|
| 100 |
|
|---|
| 101 | smb_init_locale();
|
|---|
| 102 |
|
|---|
| 103 | if (!lp_load_client(get_dyn_CONFIGFILE())) {
|
|---|
| 104 | debuglocal(0,("Can't load %s, defaults are used!\n",get_dyn_CONFIGFILE()));
|
|---|
| 105 | }
|
|---|
| 106 | load_interfaces();
|
|---|
| 107 |
|
|---|
| 108 | if (!init_names())
|
|---|
| 109 | {
|
|---|
| 110 | return 1;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | if (writeLog())
|
|---|
| 114 | {
|
|---|
| 115 | smbwrp_Logging();
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /*
|
|---|
| 119 | if ((p=smbw_getshared("RESOLVE_ORDER"))) {
|
|---|
| 120 | lp_set_name_resolve_order(p);
|
|---|
| 121 | }
|
|---|
| 122 | */
|
|---|
| 123 | return 0;
|
|---|
| 124 |
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | void smbwrp_initthread(void)
|
|---|
| 128 | {
|
|---|
| 129 | /*
|
|---|
| 130 | Block SIGPIPE (from lib/util_sock.c: write())
|
|---|
| 131 | It is not needed and should not stop execution
|
|---|
| 132 | */
|
|---|
| 133 | BlockSignals(True, SIGPIPE);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | #if 0
|
|---|
| 137 | /*****************************************************
|
|---|
| 138 | remove redundent stuff from a filename
|
|---|
| 139 | *******************************************************/
|
|---|
| 140 | void clean_fname(char *name)
|
|---|
| 141 | {
|
|---|
| 142 | char *p, *p2;
|
|---|
| 143 | int l;
|
|---|
| 144 | int modified = 1;
|
|---|
| 145 |
|
|---|
| 146 | if (!name) return;
|
|---|
| 147 |
|
|---|
| 148 | while (modified) {
|
|---|
| 149 | modified = 0;
|
|---|
| 150 |
|
|---|
| 151 | if ((p=strstr(name,"/./"))) {
|
|---|
| 152 | modified = 1;
|
|---|
| 153 | while (*p) {
|
|---|
| 154 | p[0] = p[2];
|
|---|
| 155 | p++;
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | if ((p=strstr(name,"//"))) {
|
|---|
| 160 | modified = 1;
|
|---|
| 161 | while (*p) {
|
|---|
| 162 | p[0] = p[1];
|
|---|
| 163 | p++;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | if (strcmp(name,"/../")==0) {
|
|---|
| 168 | modified = 1;
|
|---|
| 169 | name[1] = 0;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | if ((p=strstr(name,"/../"))) {
|
|---|
| 173 | modified = 1;
|
|---|
| 174 | for (p2=(p>name?p-1:p);p2>name;p2--) {
|
|---|
| 175 | if (p2[0] == '/') break;
|
|---|
| 176 | }
|
|---|
| 177 | while (*p2) {
|
|---|
| 178 | p2[0] = p2[3];
|
|---|
| 179 | p2++;
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | if (strcmp(name,"/..")==0) {
|
|---|
| 184 | modified = 1;
|
|---|
| 185 | name[1] = 0;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | l = strlen(name);
|
|---|
| 189 | p = l>=3?(name+l-3):name;
|
|---|
| 190 | if (strcmp(p,"/..")==0) {
|
|---|
| 191 | modified = 1;
|
|---|
| 192 | for (p2=p-1;p2>name;p2--) {
|
|---|
| 193 | if (p2[0] == '/') break;
|
|---|
| 194 | }
|
|---|
| 195 | if (p2==name) {
|
|---|
| 196 | p[0] = '/';
|
|---|
| 197 | p[1] = 0;
|
|---|
| 198 | } else {
|
|---|
| 199 | p2[0] = 0;
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | l = strlen(name);
|
|---|
| 204 | p = l>=2?(name+l-2):name;
|
|---|
| 205 | if (strcmp(p,"/.")==0) {
|
|---|
| 206 | if (p == name) {
|
|---|
| 207 | p[1] = 0;
|
|---|
| 208 | } else {
|
|---|
| 209 | p[0] = 0;
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | if (strncmp(p=name,"./",2) == 0) {
|
|---|
| 214 | modified = 1;
|
|---|
| 215 | do {
|
|---|
| 216 | p[0] = p[2];
|
|---|
| 217 | } while (*p++);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | l = strlen(p=name);
|
|---|
| 221 | if (l > 1 && p[l-1] == '/') {
|
|---|
| 222 | modified = 1;
|
|---|
| 223 | p[l-1] = 0;
|
|---|
| 224 | }
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 | #endif
|
|---|
| 228 |
|
|---|
| 229 | /*****************************************************
|
|---|
| 230 | return a connection to a server
|
|---|
| 231 | loosely based on do_connect() from libsmb/clidfs.c
|
|---|
| 232 | *******************************************************/
|
|---|
| 233 | int _System smbwrp_connect( Resource* pRes, cli_state ** cli)
|
|---|
| 234 | {
|
|---|
| 235 | smbwrp_server * srv = &pRes->srv;
|
|---|
| 236 | char * server = srv->server_name;
|
|---|
| 237 | char * share = *(srv->share_name) ? srv->share_name : "IPC$";
|
|---|
| 238 | char * workgroup = srv->workgroup;
|
|---|
| 239 | struct nmb_name called, calling;
|
|---|
| 240 | char *p, *server_n = server;
|
|---|
| 241 | struct cli_state * c;
|
|---|
| 242 | char* dev_type;
|
|---|
| 243 | int loginerror = 0;
|
|---|
| 244 | NTSTATUS status;
|
|---|
| 245 | int max_protocol = lp__client_max_protocol();
|
|---|
| 246 | int port = 0; //NBT_SMB_PORT;
|
|---|
| 247 | int name_type= 0x20;
|
|---|
| 248 | int flags = 0;
|
|---|
| 249 | enum protocol_types protocol;
|
|---|
| 250 | const char *name = NULL;
|
|---|
| 251 |
|
|---|
| 252 | if (!pRes->krb5support)
|
|---|
| 253 | debuglocal(1,"Connecting to \\\\%s:*********@%s:%s\\%s. Master %s:%d\n", srv->username, workgroup, server, share, srv->master, srv->ifmastergroup);
|
|---|
| 254 | else
|
|---|
| 255 | debuglocal(1,"Connecting to \\\\%s:%s\\%s using kerberos authentication. Master %s:%d\n", workgroup, server, share, srv->master, srv->ifmastergroup);
|
|---|
| 256 |
|
|---|
| 257 | if (pRes->krb5support) {
|
|---|
| 258 | flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | if (pRes->ntlmv1support) {
|
|---|
| 262 | lp_set_cmdline("client ntlmv2 auth","no");
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | status = cli_connect_nb(
|
|---|
| 266 | server, NULL, port, name_type, NULL,
|
|---|
| 267 | SMB_SIGNING_DEFAULT, flags, &c);
|
|---|
| 268 |
|
|---|
| 269 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 270 | debuglocal(4,"Connection to %s failed (Error %s)\n",
|
|---|
| 271 | server,
|
|---|
| 272 | nt_errstr(status));
|
|---|
| 273 | return 3;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | if (max_protocol == PROTOCOL_DEFAULT) {
|
|---|
| 277 | max_protocol = PROTOCOL_LATEST;
|
|---|
| 278 | }
|
|---|
| 279 | DEBUG(4,(" session request ok, c->timeout = %d\n",c->timeout));
|
|---|
| 280 |
|
|---|
| 281 | status = smbXcli_negprot(c->conn, c->timeout,
|
|---|
| 282 | lp_client_min_protocol(),
|
|---|
| 283 | max_protocol);
|
|---|
| 284 |
|
|---|
| 285 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 286 | debuglocal(4,"protocol negotiation failed: %s\n",
|
|---|
| 287 | nt_errstr(status));
|
|---|
| 288 | cli_shutdown(c);
|
|---|
| 289 | return status;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | protocol = smbXcli_conn_protocol(c->conn);
|
|---|
| 293 |
|
|---|
| 294 | switch (protocol) {
|
|---|
| 295 | case PROTOCOL_CORE:
|
|---|
| 296 | name = "CORE";
|
|---|
| 297 | break;
|
|---|
| 298 | case PROTOCOL_COREPLUS:
|
|---|
| 299 | name = "COREPLUS";
|
|---|
| 300 | break;
|
|---|
| 301 | case PROTOCOL_LANMAN1:
|
|---|
| 302 | name = "LANMAN1";
|
|---|
| 303 | break;
|
|---|
| 304 | case PROTOCOL_LANMAN2:
|
|---|
| 305 | name = "LANMAN2";
|
|---|
| 306 | break;
|
|---|
| 307 | case PROTOCOL_NT1:
|
|---|
| 308 | name = "NT1";
|
|---|
| 309 | break;
|
|---|
| 310 | case PROTOCOL_SMB2_02:
|
|---|
| 311 | name = "SMB2_02";
|
|---|
| 312 | break;
|
|---|
| 313 | case PROTOCOL_SMB2_10:
|
|---|
| 314 | name = "SMB2_10";
|
|---|
| 315 | break;
|
|---|
| 316 | case PROTOCOL_SMB2_22:
|
|---|
| 317 | name = "SMB2_22";
|
|---|
| 318 | break;
|
|---|
| 319 | case PROTOCOL_SMB2_24:
|
|---|
| 320 | name = "SMB2_24";
|
|---|
| 321 | break;
|
|---|
| 322 | case PROTOCOL_SMB3_00:
|
|---|
| 323 | name = "SMB3_00";
|
|---|
| 324 | break;
|
|---|
| 325 | case PROTOCOL_SMB3_02:
|
|---|
| 326 | name = "SMB3_02";
|
|---|
| 327 | break;
|
|---|
| 328 | case PROTOCOL_SMB3_10:
|
|---|
| 329 | name = "SMB3_10";
|
|---|
| 330 | break;
|
|---|
| 331 | case PROTOCOL_SMB3_11:
|
|---|
| 332 | name = "SMB3_11";
|
|---|
| 333 | break;
|
|---|
| 334 | default:
|
|---|
| 335 | name = "Unknown";
|
|---|
| 336 | break;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | debuglocal(4,"Server supports %s protocol\n", name);
|
|---|
| 340 |
|
|---|
| 341 | if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
|
|---|
| 342 | /* Ensure we ask for some initial credits. */
|
|---|
| 343 | smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | if (!NT_STATUS_IS_OK(cli_session_setup(c, srv->username,
|
|---|
| 347 | srv->password, strlen(srv->password),
|
|---|
| 348 | srv->password, strlen(srv->password),
|
|---|
| 349 | workgroup))) {
|
|---|
| 350 | debuglocal(4,"%s/******** login failed\n", srv->username);
|
|---|
| 351 | loginerror = 1; // save the login error
|
|---|
| 352 |
|
|---|
| 353 | /* try an anonymous login if it failed */
|
|---|
| 354 |
|
|---|
| 355 | /* If a password was not supplied then
|
|---|
| 356 | * try again with a null username. */
|
|---|
| 357 | if (srv->password[0] || !srv->username[0] ||
|
|---|
| 358 | pRes->krb5support ||
|
|---|
| 359 | !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
|
|---|
| 360 | "", 0,
|
|---|
| 361 | "", 0,
|
|---|
| 362 | workgroup))) {
|
|---|
| 363 | debuglocal(1,"session setup failed: %s\n",
|
|---|
| 364 | nt_errstr(status));
|
|---|
| 365 |
|
|---|
| 366 | if ((NT_STATUS_EQUAL(status,
|
|---|
| 367 | NT_STATUS_MORE_PROCESSING_REQUIRED)) ||
|
|---|
| 368 | NT_STATUS_EQUAL(status,
|
|---|
| 369 | NT_STATUS_INTERNAL_ERROR)){
|
|---|
| 370 | debuglocal(4,"did you forget to run kinit?\n");
|
|---|
| 371 | } else
|
|---|
| 372 | debuglocal(4,"Anonymous login failed\n");
|
|---|
| 373 |
|
|---|
| 374 | cli_shutdown(c);
|
|---|
| 375 | return 6;
|
|---|
| 376 | }
|
|---|
| 377 | debuglocal(4,"Anonymous login successful\n");
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 381 | debuglocal(4,"cli_init_creds() failed\n");
|
|---|
| 382 | cli_shutdown(c);
|
|---|
| 383 | // if loginerror is != 0 means normal login failed, but anonymous login worked
|
|---|
| 384 | if (loginerror !=0)
|
|---|
| 385 | return 6;
|
|---|
| 386 | else
|
|---|
| 387 | return 7;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | debuglocal(4," session setup ok. Sending tconx <%s> <********>\n", share);
|
|---|
| 391 |
|
|---|
| 392 | #if 0 /* Causes connections to fail with Samba 4.x */
|
|---|
| 393 | // YD ticket:58 we need to check resource type to avoid connecting to printers.
|
|---|
| 394 | // dev type is set to IPC for IPC$, A: for everything else (printers use LPT1:)
|
|---|
| 395 | if (!strcmp( share, "IPC$"))
|
|---|
| 396 | dev_type = "IPC";
|
|---|
| 397 | else
|
|---|
| 398 | dev_type = "A:";
|
|---|
| 399 |
|
|---|
| 400 | if (!NT_STATUS_IS_OK(cli_tcon_andx(c, share, dev_type,
|
|---|
| 401 | srv->password, strlen(srv->password)+1))) {
|
|---|
| 402 | cli_shutdown(c);
|
|---|
| 403 | // if loginerror is != 0 means normal login failed, but anonymous login worked
|
|---|
| 404 | if (loginerror !=0)
|
|---|
| 405 | return 6;
|
|---|
| 406 | else
|
|---|
| 407 | return 7;
|
|---|
| 408 | }
|
|---|
| 409 | #endif
|
|---|
| 410 |
|
|---|
| 411 | /* must be a normal share */
|
|---|
| 412 |
|
|---|
| 413 | status = cli_tree_connect(c, share, "?????",
|
|---|
| 414 | srv->password, strlen(srv->password)+1);
|
|---|
| 415 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 416 | debuglocal(4,"tree connect failed: %s\n", nt_errstr(status));
|
|---|
| 417 | cli_shutdown(c);
|
|---|
| 418 | return status;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | debuglocal(4," tconx ok.\n");
|
|---|
| 422 |
|
|---|
| 423 | // save cli_state pointer
|
|---|
| 424 | *cli = c;
|
|---|
| 425 |
|
|---|
| 426 | return 0;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | /*****************************************************
|
|---|
| 430 | close a connection to a server
|
|---|
| 431 | *******************************************************/
|
|---|
| 432 | void _System smbwrp_disconnect( Resource* pRes, cli_state * cli)
|
|---|
| 433 | {
|
|---|
| 434 | if (pRes && cli)
|
|---|
| 435 | {
|
|---|
| 436 | // this call will free all buffers, close handles and free cli mem
|
|---|
| 437 | cli_shutdown( cli);
|
|---|
| 438 | }
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 | /*****************************************************
|
|---|
| 444 | a wrapper for open()
|
|---|
| 445 | *******************************************************/
|
|---|
| 446 | int _System smbwrp_open(cli_state * cli, smbwrp_file * file)
|
|---|
| 447 | {
|
|---|
| 448 | uint16_t fd = 0;
|
|---|
| 449 |
|
|---|
| 450 | if (!cli || !file || !*file->fname)
|
|---|
| 451 | {
|
|---|
| 452 | return maperror(EINVAL);
|
|---|
| 453 | }
|
|---|
| 454 | if (file->denymode < DENY_ALL || file->denymode > DENY_NONE)
|
|---|
| 455 | {
|
|---|
| 456 | file->denymode = DENY_NONE;
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | debuglocal(4,"cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
|
|---|
| 460 | if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
|
|---|
| 461 | {
|
|---|
| 462 | return os2cli_errno(cli);
|
|---|
| 463 | }
|
|---|
| 464 | file->fd = fd;
|
|---|
| 465 | file->updatetime = 0;
|
|---|
| 466 | file->offset = 0;
|
|---|
| 467 | return 0;
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | /*****************************************************
|
|---|
| 471 | a wrapper for read()
|
|---|
| 472 | *******************************************************/
|
|---|
| 473 | int _System smbwrp_read(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
|
|---|
| 474 | {
|
|---|
| 475 | int ret;
|
|---|
| 476 |
|
|---|
| 477 | if (!cli || !file || !buf || !result)
|
|---|
| 478 | {
|
|---|
| 479 | return maperror(EINVAL);
|
|---|
| 480 | }
|
|---|
| 481 | size_t nread;
|
|---|
| 482 | *result = 0;
|
|---|
| 483 | ret = cli_read(cli, file->fd, buf, file->offset, count, &nread);
|
|---|
| 484 | if (ret == -1)
|
|---|
| 485 | {
|
|---|
| 486 | debuglocal(4," smbwrp_read - cli_read ret = %d\n",ret);
|
|---|
| 487 | return os2cli_errno(cli);
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | file->offset += nread;
|
|---|
| 491 | *result = nread;
|
|---|
| 492 | debuglocal(4," smbwrp_read successful, nread = %d, ret = %d\n",nread,ret);
|
|---|
| 493 | return 0;
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 | /*****************************************************
|
|---|
| 499 | a wrapper for write()
|
|---|
| 500 | *******************************************************/
|
|---|
| 501 | int _System smbwrp_write(cli_state * cli, smbwrp_file * file, void *buf, unsigned long count, unsigned long * result)
|
|---|
| 502 | {
|
|---|
| 503 | NTSTATUS status;
|
|---|
| 504 | size_t ret;
|
|---|
| 505 |
|
|---|
| 506 | if (!cli || !file || !buf || !result)
|
|---|
| 507 | {
|
|---|
| 508 | return maperror(EINVAL);
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | *result = 0;
|
|---|
| 512 | //debuglocal(1,("Write %x %d %lld %d", cli, file->fd, file->offset, count));
|
|---|
| 513 | status = cli_writeall(cli, file->fd, 0, buf, file->offset, count, &ret);
|
|---|
| 514 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 515 | return os2cli_errno(cli);
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | file->updatetime = 1;
|
|---|
| 519 | file->offset += ret;
|
|---|
| 520 | *result = ret;
|
|---|
| 521 | return 0;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | /*****************************************************
|
|---|
| 525 | a wrapper for close()
|
|---|
| 526 | *******************************************************/
|
|---|
| 527 | int _System smbwrp_close(cli_state * cli, smbwrp_file * file)
|
|---|
| 528 | {
|
|---|
| 529 | int rc = 0;
|
|---|
| 530 | if (!cli || !file)
|
|---|
| 531 | {
|
|---|
| 532 | return maperror(EINVAL);
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | debuglocal(4,"smpwrp_close updatetime: %d\n", file->updatetime);
|
|---|
| 536 |
|
|---|
| 537 | if (file->updatetime == 1)
|
|---|
| 538 | {
|
|---|
| 539 | file->mtime = time(NULL);
|
|---|
| 540 | debuglocal(4,"cli_close new mtime %lu\n", file->mtime);
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
|
|---|
| 544 | {
|
|---|
| 545 | rc = os2cli_errno(cli);
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | if (!rc && (file->openattr || file->mtime || file->ctime))
|
|---|
| 549 | {
|
|---|
| 550 | debuglocal(4,"Set pathinfo on close %s %08x %d %d\n", file->fname, file->openattr, file->mtime, file->ctime);
|
|---|
| 551 | if (!NT_STATUS_IS_OK(cli_setpathinfo_basic(cli, file->fname, file->ctime, 0, file->mtime, 0, file->openattr)))
|
|---|
| 552 | {
|
|---|
| 553 | debuglocal(4,"Set pathinfo on close failed %d\n", os2cli_errno(cli));
|
|---|
| 554 | //rc = os2cli_errno(cli);
|
|---|
| 555 | }
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | file->openattr = 0;
|
|---|
| 559 | file->mtime = 0;
|
|---|
| 560 | file->ctime = 0;
|
|---|
| 561 | file->updatetime = 0;
|
|---|
| 562 | file->fd = -1;
|
|---|
| 563 | file->offset = 0;
|
|---|
| 564 | *file->fname = 0;
|
|---|
| 565 | return rc;
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 | int _System smbwrp_setfilesize(cli_state * cli, smbwrp_file * file, long long newsize)
|
|---|
| 569 | {
|
|---|
| 570 | int rc = 0;
|
|---|
| 571 | if (!cli || !file)
|
|---|
| 572 | {
|
|---|
| 573 | return maperror(EINVAL);
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | debuglocal(4,"cli_setnewfilesize(%s) %lld\n", file->fname, newsize);
|
|---|
| 577 | if (!NT_STATUS_IS_OK(cli_ftruncate(cli, file->fd, newsize)))
|
|---|
| 578 | {
|
|---|
| 579 | if (newsize)
|
|---|
| 580 | {
|
|---|
| 581 | rc = os2cli_errno(cli);
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | if (!NT_STATUS_IS_OK(cli_close(cli, file->fd)))
|
|---|
| 585 | {
|
|---|
| 586 | return os2cli_errno(cli);
|
|---|
| 587 | }
|
|---|
| 588 | uint16_t fd = 0;
|
|---|
| 589 | file->fd = -1;
|
|---|
| 590 | file->offset = 0;
|
|---|
| 591 | file->openmode &= ~(O_CREAT | O_EXCL);
|
|---|
| 592 | file->openmode |= O_TRUNC;
|
|---|
| 593 | debuglocal(4,"cli_setnewfileszie : cli_open(%s) attr %08x mode %02x denymode %02x\n", file->fname, file->openattr, file->openmode, file->denymode);
|
|---|
| 594 | if (!NT_STATUS_IS_OK(cli_open(cli, file->fname, file->openmode, file->denymode, &fd)))
|
|---|
| 595 | {
|
|---|
| 596 | return os2cli_errno(cli);
|
|---|
| 597 | }
|
|---|
| 598 | file->fd = fd;
|
|---|
| 599 | }
|
|---|
| 600 | return 0;
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | /*****************************************************
|
|---|
| 604 | a wrapper for rename()
|
|---|
| 605 | *******************************************************/
|
|---|
| 606 | int _System smbwrp_rename(cli_state * cli, char *oldname, char *newname)
|
|---|
| 607 | {
|
|---|
| 608 | if (!cli || !oldname || !newname)
|
|---|
| 609 | {
|
|---|
| 610 | return maperror(EINVAL);
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | debuglocal(1,"Rename <%s> -> <%s>\n", oldname, newname);
|
|---|
| 614 | if (!NT_STATUS_IS_OK(cli_rename(cli, oldname, newname)))
|
|---|
| 615 | {
|
|---|
| 616 | return os2cli_errno(cli);
|
|---|
| 617 | }
|
|---|
| 618 | return 0;
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 |
|
|---|
| 622 | /*****************************************************
|
|---|
| 623 | a wrapper for chmod()
|
|---|
| 624 | *******************************************************/
|
|---|
| 625 | int _System smbwrp_setattr(cli_state * cli, smbwrp_fileinfo *finfo)
|
|---|
| 626 | {
|
|---|
| 627 | if (!cli || !finfo || !*finfo->fname)
|
|---|
| 628 | {
|
|---|
| 629 | return maperror(EINVAL);
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | debuglocal(4,"Setting on <%s> attr %04x, time %lu (timezone /%lu\n", finfo->fname, finfo->attr, finfo->mtime, finfo->mtime + get_time_zone(finfo->mtime));
|
|---|
| 633 | // we already have gmt time, so no need to add timezone
|
|---|
| 634 | // if (!cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime + (finfo->mtime == 0 ? 0 : get_time_zone(finfo->mtime)))
|
|---|
| 635 | if (!NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, finfo->mtime))
|
|---|
| 636 | && !NT_STATUS_IS_OK(cli_setatr(cli, finfo->fname, finfo->attr, 0)))
|
|---|
| 637 | {
|
|---|
| 638 | return os2cli_errno(cli);
|
|---|
| 639 | }
|
|---|
| 640 | return 0;
|
|---|
| 641 | }
|
|---|
| 642 |
|
|---|
| 643 | /*****************************************************
|
|---|
| 644 | a wrapper for unlink()
|
|---|
| 645 | *******************************************************/
|
|---|
| 646 | int _System smbwrp_unlink(cli_state * cli, const char *fname)
|
|---|
| 647 | {
|
|---|
| 648 | if (!cli || !fname)
|
|---|
| 649 | {
|
|---|
| 650 | return maperror(EINVAL);
|
|---|
| 651 | }
|
|---|
| 652 | #if 0
|
|---|
| 653 | if (strncmp(cli->dev, "LPT", 3) == 0)
|
|---|
| 654 | {
|
|---|
| 655 | int job = smbw_stat_printjob(cli, fname, NULL, NULL);
|
|---|
| 656 | if (job == -1)
|
|---|
| 657 | {
|
|---|
| 658 | goto failed;
|
|---|
| 659 | }
|
|---|
| 660 | if (cli_printjob_del(cli, job) != 0)
|
|---|
| 661 | {
|
|---|
| 662 | goto failed;
|
|---|
| 663 | }
|
|---|
| 664 | } else
|
|---|
| 665 | #endif
|
|---|
| 666 | if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN)))
|
|---|
| 667 | {
|
|---|
| 668 | return os2cli_errno(cli);
|
|---|
| 669 | }
|
|---|
| 670 | return 0;
|
|---|
| 671 | }
|
|---|
| 672 |
|
|---|
| 673 | /*****************************************************
|
|---|
| 674 | a wrapper for lseek()
|
|---|
| 675 | *******************************************************/
|
|---|
| 676 | int _System smbwrp_lseek(cli_state * cli, smbwrp_file * file, int whence, long long offset)
|
|---|
| 677 | {
|
|---|
| 678 | off_t size;
|
|---|
| 679 | if (!cli || !file)
|
|---|
| 680 | {
|
|---|
| 681 | return maperror(EINVAL);
|
|---|
| 682 | }
|
|---|
| 683 |
|
|---|
| 684 | debuglocal(4,"lseek %d %lld %lld\n", whence, offset, file->offset);
|
|---|
| 685 |
|
|---|
| 686 | switch (whence) {
|
|---|
| 687 | case SEEK_SET:
|
|---|
| 688 | if (offset < 0)
|
|---|
| 689 | {
|
|---|
| 690 | return maperror(EINVAL);
|
|---|
| 691 | }
|
|---|
| 692 | file->offset = offset;
|
|---|
| 693 | break;
|
|---|
| 694 | case SEEK_CUR:
|
|---|
| 695 | file->offset += offset;
|
|---|
| 696 | break;
|
|---|
| 697 | case SEEK_END:
|
|---|
| 698 | if (offset > 0)
|
|---|
| 699 | {
|
|---|
| 700 | return maperror(EINVAL);
|
|---|
| 701 | }
|
|---|
| 702 | if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
|
|---|
| 703 | NULL, &size, NULL, NULL, NULL,
|
|---|
| 704 | NULL, NULL)) &&
|
|---|
| 705 | !NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
|
|---|
| 706 | NULL, &size, NULL, NULL, NULL)))
|
|---|
| 707 | {
|
|---|
| 708 | return os2cli_errno(cli);
|
|---|
| 709 | }
|
|---|
| 710 | file->offset = size + offset;
|
|---|
| 711 | break;
|
|---|
| 712 | default: return maperror(EINVAL);
|
|---|
| 713 | }
|
|---|
| 714 |
|
|---|
| 715 | return 0;
|
|---|
| 716 | }
|
|---|
| 717 |
|
|---|
| 718 | /*****************************************************
|
|---|
| 719 | try to do a QPATHINFO and if that fails then do a getatr
|
|---|
| 720 | this is needed because win95 sometimes refuses the qpathinfo
|
|---|
| 721 | loosely based on SMBC_getatr() from source3/libsmb/libsmb_file.c
|
|---|
| 722 | *******************************************************/
|
|---|
| 723 | int _System smbwrp_getattr(smbwrp_server *srv, cli_state * cli, smbwrp_fileinfo *finfo)
|
|---|
| 724 | {
|
|---|
| 725 | SMB_INO_T ino = 0;
|
|---|
| 726 | struct timespec ctime;
|
|---|
| 727 | struct timespec mtime;
|
|---|
| 728 | struct timespec atime;
|
|---|
| 729 |
|
|---|
| 730 | if (!cli || !finfo || !*finfo->fname)
|
|---|
| 731 | {
|
|---|
| 732 | return maperror(EINVAL);
|
|---|
| 733 | }
|
|---|
| 734 | debuglocal(4,"getattr %d %d <%s>\n", smb1cli_conn_capabilities(cli->conn) & CAP_NOPATHINFO2, smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS, finfo->fname);
|
|---|
| 735 |
|
|---|
| 736 | if (NT_STATUS_IS_OK(cli_qpathinfo2(cli, finfo->fname, &ctime, &atime, &mtime, NULL,
|
|---|
| 737 | (off_t *)&finfo->size, (unsigned short *)&finfo->attr, &ino)))
|
|---|
| 738 | {
|
|---|
| 739 | finfo->attr &= 0x7F;
|
|---|
| 740 | finfo->ctime = convert_timespec_to_time_t(ctime);
|
|---|
| 741 | finfo->atime = convert_timespec_to_time_t(atime);
|
|---|
| 742 | finfo->mtime = convert_timespec_to_time_t(mtime);
|
|---|
| 743 | return 0;
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | #if 0 // cli->fd not available in Samba 4.x
|
|---|
| 747 | if (cli->fd == -1)
|
|---|
| 748 | {
|
|---|
| 749 | /* fd == -1 means the connection is broken */
|
|---|
| 750 | return maperror(ENOTCONN);
|
|---|
| 751 | }
|
|---|
| 752 | #endif
|
|---|
| 753 |
|
|---|
| 754 | debuglocal(4, "smbwrp_getattr, calling cli_qpathinfo3\n");
|
|---|
| 755 | if (NT_STATUS_IS_OK(cli_qpathinfo3(cli, finfo->fname, &ctime, &atime, &mtime, NULL,
|
|---|
| 756 | (off_t *)&finfo->size, (unsigned short *)&finfo->attr, &ino)))
|
|---|
| 757 | {
|
|---|
| 758 | finfo->attr &= 0x7F;
|
|---|
| 759 | finfo->ctime = convert_timespec_to_time_t(ctime);
|
|---|
| 760 | finfo->atime = convert_timespec_to_time_t(atime);
|
|---|
| 761 | finfo->mtime = convert_timespec_to_time_t(mtime);
|
|---|
| 762 | return 0;
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | /* If the path is not on a share (it is a workgroup or a server),
|
|---|
| 766 | * then cli_qpathinfo2 obviously fails. Return some fake information
|
|---|
| 767 | * about the directory.
|
|---|
| 768 | */
|
|---|
| 769 | if ( *srv->server_name == 0
|
|---|
| 770 | #if 0 /* Causes crashes with Samba 4.x */
|
|---|
| 771 | || (strcmp(cli->dev,"IPC") == 0)
|
|---|
| 772 | #endif
|
|---|
| 773 | || *srv->share_name == 0
|
|---|
| 774 | || (stricmp(srv->share_name,"IPC$") == 0)
|
|---|
| 775 | #if 0 /* Causes crashes with Samba 4.x */
|
|---|
| 776 | || (strncmp(cli->dev,"LPT",3) == 0)
|
|---|
| 777 | #endif
|
|---|
| 778 | )
|
|---|
| 779 | {
|
|---|
| 780 | debuglocal(4,"getattr not a share.\n");
|
|---|
| 781 | *(time_t *)&finfo->ctime = time (NULL);
|
|---|
| 782 | *(time_t *)&finfo->atime = time (NULL);
|
|---|
| 783 | *(time_t *)&finfo->mtime = time (NULL);
|
|---|
| 784 | finfo->size = 0;
|
|---|
| 785 | finfo->easize = 0;
|
|---|
| 786 | finfo->attr = aDIR;
|
|---|
| 787 | return 0;
|
|---|
| 788 | }
|
|---|
| 789 |
|
|---|
| 790 | /* if this is NT then don't bother with the getatr */
|
|---|
| 791 | if (smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS/* && !(smb1cli_conn_capabilities(cli->conn) & CAP_NOPATHINFO2)*/)
|
|---|
| 792 | {
|
|---|
| 793 | int ret = cli_errno(cli);
|
|---|
| 794 | // cli_qpathinfo* reports EINVAL when path of given file not exists
|
|---|
| 795 | // thus there is no real situation when EINVAL should be returned to
|
|---|
| 796 | // client at this point, we just replace it to ENOTDIR
|
|---|
| 797 | if (ret == EINVAL)
|
|---|
| 798 | {
|
|---|
| 799 | ret = ENOTDIR;
|
|---|
| 800 | }
|
|---|
| 801 | return maperror(ret);
|
|---|
| 802 | }
|
|---|
| 803 |
|
|---|
| 804 | if (NT_STATUS_IS_OK(cli_getatr(cli, finfo->fname, (unsigned short *)&finfo->attr, &finfo->size, (time_t *)&finfo->mtime)))
|
|---|
| 805 | {
|
|---|
| 806 | //debuglocal(2,("gotattr1 %08x <%s>\n", finfo->attr, finfo->fname));
|
|---|
| 807 | finfo->mtime -= get_time_zone(finfo->mtime);
|
|---|
| 808 | finfo->atime = finfo->atime; //was mtime
|
|---|
| 809 | finfo->ctime = finfo->ctime; //was mtime
|
|---|
| 810 | return 0;
|
|---|
| 811 | }
|
|---|
| 812 | return os2cli_errno(cli);
|
|---|
| 813 | }
|
|---|
| 814 |
|
|---|
| 815 | /*****************************************************
|
|---|
| 816 | try to do a QPATHINFO and if that fails then do a getatr
|
|---|
| 817 | this is needed because win95 sometimes refuses the qpathinfo
|
|---|
| 818 | *******************************************************/
|
|---|
| 819 | int _System smbwrp_fgetattr(cli_state * cli, smbwrp_file *file, smbwrp_fileinfo *finfo)
|
|---|
| 820 | {
|
|---|
| 821 | struct timespec ctime;
|
|---|
| 822 | struct timespec mtime;
|
|---|
| 823 | struct timespec atime;
|
|---|
| 824 | SMB_INO_T ino = 0;
|
|---|
| 825 |
|
|---|
| 826 | if (!cli || !file || !finfo)
|
|---|
| 827 | {
|
|---|
| 828 | return maperror(EINVAL);
|
|---|
| 829 | }
|
|---|
| 830 |
|
|---|
| 831 | strncpy(finfo->fname, file->fname, sizeof(finfo->fname) - 1);
|
|---|
| 832 | if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(cli, file->fd,
|
|---|
| 833 | (unsigned short *)&finfo->attr, (off_t *)&finfo->size, &ctime, &atime, &mtime, NULL,
|
|---|
| 834 | &ino)))
|
|---|
| 835 | {
|
|---|
| 836 | if (!NT_STATUS_IS_OK(cli_getattrE(cli, file->fd,
|
|---|
| 837 | (unsigned short *)&finfo->attr, (&finfo->size), (time_t *)&finfo->ctime, (time_t *)&finfo->atime, (time_t *)&finfo->mtime)))
|
|---|
| 838 | {
|
|---|
| 839 | return os2cli_errno(cli);
|
|---|
| 840 | }
|
|---|
| 841 | else
|
|---|
| 842 | {
|
|---|
| 843 | finfo->ctime -= get_time_zone(finfo->ctime);
|
|---|
| 844 | finfo->atime -= get_time_zone(finfo->atime);
|
|---|
| 845 | finfo->mtime -= get_time_zone(finfo->mtime);
|
|---|
| 846 | }
|
|---|
| 847 | }
|
|---|
| 848 | else
|
|---|
| 849 | {
|
|---|
| 850 | finfo->ctime = convert_timespec_to_time_t(ctime);
|
|---|
| 851 | finfo->atime = convert_timespec_to_time_t(atime);
|
|---|
| 852 | finfo->mtime = convert_timespec_to_time_t(mtime);
|
|---|
| 853 | }
|
|---|
| 854 |
|
|---|
| 855 | return 0;
|
|---|
| 856 | }
|
|---|
| 857 |
|
|---|
| 858 | // =============================DIRECTORY ROUTINES============================
|
|---|
| 859 |
|
|---|
| 860 | /*****************************************************
|
|---|
| 861 | add a entry to a directory listing
|
|---|
| 862 | *******************************************************/
|
|---|
| 863 | static NTSTATUS smbwrp_dir_add(const char* mnt, smbwrp_fileinfo *finfo, const char *mask, void *state)
|
|---|
| 864 | {
|
|---|
| 865 | if (state && finfo)
|
|---|
| 866 | {
|
|---|
| 867 | filelist_state * st = (filelist_state *)state;
|
|---|
| 868 | char fullname[ _MAX_PATH] = {0};
|
|---|
| 869 | debuglocal(8,"adding <%s> %d %d %d\n", finfo->fname, sizeof(st->finfo), st->datalen, sizeof(st->finfo.fname));
|
|---|
| 870 | memcpy(&st->finfo, finfo, sizeof(st->finfo));
|
|---|
| 871 | strncpy(fullname, st->dir, strlen(st->dir));
|
|---|
| 872 | strncat(fullname, finfo->fname, sizeof(fullname) - strlen(fullname) -1);
|
|---|
| 873 | strncpy(st->finfo.fname, fullname, sizeof(st->finfo.fname));
|
|---|
| 874 | getfindinfoL( st->pConn, st->plist, &st->finfo, st->ulAttribute, st->dir_mask);
|
|---|
| 875 | }
|
|---|
| 876 | }
|
|---|
| 877 |
|
|---|
| 878 | static void smbwrp_special_add(const char * name, void * state)
|
|---|
| 879 | {
|
|---|
| 880 | smbwrp_fileinfo finfo = {0};
|
|---|
| 881 |
|
|---|
| 882 | if (!name)
|
|---|
| 883 | {
|
|---|
| 884 | return;
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | ZERO_STRUCT(finfo);
|
|---|
| 888 |
|
|---|
| 889 | strncpy(finfo.fname, name, sizeof(finfo.fname) - 1);
|
|---|
| 890 | finfo.attr = aRONLY | aDIR;
|
|---|
| 891 |
|
|---|
| 892 | smbwrp_dir_add("", &finfo, NULL, state);
|
|---|
| 893 | }
|
|---|
| 894 |
|
|---|
| 895 | static void smbwrp_printjob_add(struct print_job_info *job, void * state)
|
|---|
| 896 | {
|
|---|
| 897 | smbwrp_fileinfo finfo = {0};
|
|---|
| 898 |
|
|---|
| 899 | ZERO_STRUCT(finfo);
|
|---|
| 900 |
|
|---|
| 901 | //printf("Printjob <%s>\n", job->name);
|
|---|
| 902 |
|
|---|
| 903 | strncpy(finfo.fname, job->name, sizeof(finfo.fname) - 1);
|
|---|
| 904 | finfo.mtime = job->t - get_time_zone(job->t);
|
|---|
| 905 | finfo.atime = finfo.atime; //was mtime
|
|---|
| 906 | finfo.ctime = finfo.ctime; //was mtime
|
|---|
| 907 | finfo.attr = aRONLY;
|
|---|
| 908 | finfo.size = job->size;
|
|---|
| 909 |
|
|---|
| 910 | smbwrp_dir_add("", &finfo, NULL, state);
|
|---|
| 911 | }
|
|---|
| 912 |
|
|---|
| 913 | static void smbwrp_share_add(const char *share, uint32_t type,
|
|---|
| 914 | const char *comment, void *state)
|
|---|
| 915 | {
|
|---|
| 916 | smbwrp_fileinfo finfo = {0};
|
|---|
| 917 |
|
|---|
| 918 | // strip administrative names and printers from list
|
|---|
| 919 | if (type == STYPE_PRINTQ || strcmp(share,"IPC$") == 0) return;
|
|---|
| 920 |
|
|---|
| 921 | ZERO_STRUCT(finfo);
|
|---|
| 922 |
|
|---|
| 923 | strncpy(finfo.fname, share, sizeof(finfo.fname) - 1);
|
|---|
| 924 | finfo.attr = aRONLY | aDIR;
|
|---|
| 925 |
|
|---|
| 926 | smbwrp_dir_add("", &finfo, NULL, state);
|
|---|
| 927 | }
|
|---|
| 928 |
|
|---|
| 929 | /***************************************************************
|
|---|
| 930 | Wrapper that allows SMB2 to list a directory.
|
|---|
| 931 | Synchronous only.
|
|---|
| 932 | Based on cli_smb2_list
|
|---|
| 933 | ***************************************************************/
|
|---|
| 934 |
|
|---|
| 935 | NTSTATUS list_files_smb2(struct cli_state *cli,
|
|---|
| 936 | const char *pathname,
|
|---|
| 937 | uint16_t attribute,
|
|---|
| 938 | NTSTATUS (*fn)(const char *,
|
|---|
| 939 | struct smbwrp_fileinfo *,
|
|---|
| 940 | const char *,
|
|---|
| 941 | void *),
|
|---|
| 942 | void *state)
|
|---|
| 943 | {
|
|---|
| 944 | NTSTATUS status;
|
|---|
| 945 | uint16_t fnum = 0xffff;
|
|---|
| 946 | char *parent_dir = NULL;
|
|---|
| 947 | const char *mask = NULL;
|
|---|
| 948 | struct smb2_hnd *ph = NULL;
|
|---|
| 949 | bool processed_file = false;
|
|---|
| 950 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 951 | TALLOC_CTX *subframe = NULL;
|
|---|
| 952 | bool mask_has_wild;
|
|---|
| 953 | void *dircachectx = NULL;
|
|---|
| 954 | smbwrp_fileinfo wrpfinfo;
|
|---|
| 955 |
|
|---|
| 956 | if (smbXcli_conn_has_async_calls(cli->conn)) {
|
|---|
| 957 | /*
|
|---|
| 958 | * Can't use sync call while an async call is in flight
|
|---|
| 959 | */
|
|---|
| 960 | status = NT_STATUS_INVALID_PARAMETER;
|
|---|
| 961 | goto fail;
|
|---|
| 962 | }
|
|---|
| 963 |
|
|---|
| 964 | if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
|
|---|
| 965 | status = NT_STATUS_INVALID_PARAMETER;
|
|---|
| 966 | goto fail;
|
|---|
| 967 | }
|
|---|
| 968 |
|
|---|
| 969 | /* Get the directory name. */
|
|---|
| 970 | if (!windows_parent_dirname(frame,
|
|---|
| 971 | pathname,
|
|---|
| 972 | &parent_dir,
|
|---|
| 973 | &mask)) {
|
|---|
| 974 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 975 | goto fail;
|
|---|
| 976 | }
|
|---|
| 977 |
|
|---|
| 978 | mask_has_wild = ms_has_wild(mask);
|
|---|
| 979 |
|
|---|
| 980 | status = cli_smb2_create_fnum(cli,
|
|---|
| 981 | parent_dir,
|
|---|
| 982 | 0, /* create_flags */
|
|---|
| 983 | SEC_DIR_LIST|SEC_DIR_READ_ATTRIBUTE,/* desired_access */
|
|---|
| 984 | FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
|
|---|
| 985 | FILE_SHARE_READ|FILE_SHARE_WRITE, /* share_access */
|
|---|
| 986 | FILE_OPEN, /* create_disposition */
|
|---|
| 987 | FILE_DIRECTORY_FILE, /* create_options */
|
|---|
| 988 | &fnum,
|
|---|
| 989 | NULL);
|
|---|
| 990 |
|
|---|
| 991 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 992 | goto fail;
|
|---|
| 993 | }
|
|---|
| 994 |
|
|---|
| 995 | status = map_fnum_to_smb2_handle(cli,
|
|---|
| 996 | fnum,
|
|---|
| 997 | &ph);
|
|---|
| 998 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 999 | goto fail;
|
|---|
| 1000 | }
|
|---|
| 1001 |
|
|---|
| 1002 | do {
|
|---|
| 1003 | uint8_t *dir_data = NULL;
|
|---|
| 1004 | uint32_t dir_data_length = 0;
|
|---|
| 1005 | uint32_t next_offset = 0;
|
|---|
| 1006 | subframe = talloc_stackframe();
|
|---|
| 1007 |
|
|---|
| 1008 | status = smb2cli_query_directory(cli->conn,
|
|---|
| 1009 | cli->timeout,
|
|---|
| 1010 | cli->smb2.session,
|
|---|
| 1011 | cli->smb2.tcon,
|
|---|
| 1012 | SMB2_FIND_ID_BOTH_DIRECTORY_INFO,
|
|---|
| 1013 | 0, /* flags */
|
|---|
| 1014 | 0, /* file_index */
|
|---|
| 1015 | ph->fid_persistent,
|
|---|
| 1016 | ph->fid_volatile,
|
|---|
| 1017 | mask,
|
|---|
| 1018 | 0xffff,
|
|---|
| 1019 | subframe,
|
|---|
| 1020 | &dir_data,
|
|---|
| 1021 | &dir_data_length);
|
|---|
| 1022 |
|
|---|
| 1023 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1024 | if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES)) {
|
|---|
| 1025 | break;
|
|---|
| 1026 | }
|
|---|
| 1027 | goto fail;
|
|---|
| 1028 | }
|
|---|
| 1029 |
|
|---|
| 1030 | do {
|
|---|
| 1031 | struct file_info *finfo = talloc_zero(subframe,
|
|---|
| 1032 | struct file_info);
|
|---|
| 1033 |
|
|---|
| 1034 | if (finfo == NULL) {
|
|---|
| 1035 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 1036 | goto fail;
|
|---|
| 1037 | }
|
|---|
| 1038 |
|
|---|
| 1039 | status = parse_finfo_id_both_directory_info(dir_data,
|
|---|
| 1040 | dir_data_length,
|
|---|
| 1041 | finfo,
|
|---|
| 1042 | &next_offset);
|
|---|
| 1043 |
|
|---|
| 1044 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1045 | goto fail;
|
|---|
| 1046 | }
|
|---|
| 1047 |
|
|---|
| 1048 | if (dir_check_ftype((uint32_t)finfo->mode,
|
|---|
| 1049 | (uint32_t)attribute)) {
|
|---|
| 1050 | /*
|
|---|
| 1051 | * Only process if attributes match.
|
|---|
| 1052 | * On SMB1 server does this, so on
|
|---|
| 1053 | * SMB2 we need to emulate in the
|
|---|
| 1054 | * client.
|
|---|
| 1055 | *
|
|---|
| 1056 | * https://bugzilla.samba.org/show_bug.cgi?id=10260
|
|---|
| 1057 | */
|
|---|
| 1058 | processed_file = true;
|
|---|
| 1059 |
|
|---|
| 1060 | //as samba and this client have different finfo, we need to convert
|
|---|
| 1061 | memset(&wrpfinfo, 0, sizeof(wrpfinfo));
|
|---|
| 1062 | wrpfinfo.size = finfo[0].size;
|
|---|
| 1063 | wrpfinfo.attr = finfo[0].mode;
|
|---|
| 1064 | wrpfinfo.ctime = convert_timespec_to_time_t(finfo[0].ctime_ts);
|
|---|
| 1065 | wrpfinfo.mtime = convert_timespec_to_time_t(finfo[0].mtime_ts);
|
|---|
| 1066 | wrpfinfo.atime = convert_timespec_to_time_t(finfo[0].atime_ts);
|
|---|
| 1067 | wrpfinfo.easize = finfo[0].easize;
|
|---|
| 1068 | strncpy(wrpfinfo.fname, finfo[0].name, sizeof(wrpfinfo.fname) -1);
|
|---|
| 1069 |
|
|---|
| 1070 | status = fn(cli->dfs_mountpoint,
|
|---|
| 1071 | &wrpfinfo,
|
|---|
| 1072 | pathname,
|
|---|
| 1073 | state);
|
|---|
| 1074 |
|
|---|
| 1075 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1076 | /* not sure why this is required on OS/2 */
|
|---|
| 1077 | if (status != NT_STATUS_WAIT_1)
|
|---|
| 1078 | break;
|
|---|
| 1079 | }
|
|---|
| 1080 | }
|
|---|
| 1081 |
|
|---|
| 1082 | TALLOC_FREE(finfo);
|
|---|
| 1083 |
|
|---|
| 1084 | /* Move to next entry. */
|
|---|
| 1085 | if (next_offset) {
|
|---|
| 1086 | dir_data += next_offset;
|
|---|
| 1087 | dir_data_length -= next_offset;
|
|---|
| 1088 | }
|
|---|
| 1089 | } while (next_offset != 0);
|
|---|
| 1090 |
|
|---|
| 1091 | TALLOC_FREE(subframe);
|
|---|
| 1092 |
|
|---|
| 1093 | if (!mask_has_wild) {
|
|---|
| 1094 | /*
|
|---|
| 1095 | * MacOSX 10 doesn't set STATUS_NO_MORE_FILES
|
|---|
| 1096 | * when handed a non-wildcard path. Do it
|
|---|
| 1097 | * for the server (with a non-wildcard path
|
|---|
| 1098 | * there should only ever be one file returned.
|
|---|
| 1099 | */
|
|---|
| 1100 | status = STATUS_NO_MORE_FILES;
|
|---|
| 1101 | break;
|
|---|
| 1102 | }
|
|---|
| 1103 |
|
|---|
| 1104 | } while (NT_STATUS_IS_OK(status));
|
|---|
| 1105 |
|
|---|
| 1106 | if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES)) {
|
|---|
| 1107 | status = NT_STATUS_OK;
|
|---|
| 1108 | }
|
|---|
| 1109 |
|
|---|
| 1110 | if (NT_STATUS_IS_OK(status) && !processed_file) {
|
|---|
| 1111 | /*
|
|---|
| 1112 | * In SMB1 findfirst returns NT_STATUS_NO_SUCH_FILE
|
|---|
| 1113 | * if no files match. Emulate this in the client.
|
|---|
| 1114 | */
|
|---|
| 1115 | status = NT_STATUS_NO_SUCH_FILE;
|
|---|
| 1116 | }
|
|---|
| 1117 |
|
|---|
| 1118 | fail:
|
|---|
| 1119 |
|
|---|
| 1120 | if (fnum != 0xffff) {
|
|---|
| 1121 | cli_smb2_close_fnum(cli, fnum);
|
|---|
| 1122 | }
|
|---|
| 1123 | TALLOC_FREE(subframe);
|
|---|
| 1124 | TALLOC_FREE(frame);
|
|---|
| 1125 | return status;
|
|---|
| 1126 | }
|
|---|
| 1127 |
|
|---|
| 1128 |
|
|---|
| 1129 | /****************************************************************************
|
|---|
| 1130 | Do a directory listing, calling fn on each file found.
|
|---|
| 1131 | Modified from cli_list
|
|---|
| 1132 | ****************************************************************************/
|
|---|
| 1133 | static int list_files(struct cli_state *cli, const char *mask, uint16_t attribute,
|
|---|
| 1134 | void (*fn)(const char *, smbwrp_fileinfo *, const char *,
|
|---|
| 1135 | void *), void *state)
|
|---|
| 1136 | {
|
|---|
| 1137 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 1138 | struct event_context *ev;
|
|---|
| 1139 | struct tevent_req *req;
|
|---|
| 1140 | NTSTATUS status = NT_STATUS_NO_MEMORY;
|
|---|
| 1141 | struct file_info *finfo;
|
|---|
| 1142 | size_t i, num_finfo;
|
|---|
| 1143 | uint16_t info_level;
|
|---|
| 1144 | void *dircachectx = NULL;
|
|---|
| 1145 | smbwrp_fileinfo wrpfinfo;
|
|---|
| 1146 |
|
|---|
| 1147 | if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
|
|---|
| 1148 | debuglocal(4,"SMB2 detected, calling list_files_smb2()\n");
|
|---|
| 1149 | return list_files_smb2(cli, mask, attribute, fn, state);
|
|---|
| 1150 | }
|
|---|
| 1151 |
|
|---|
| 1152 | /* Try to get the listing from cache. */
|
|---|
| 1153 | if (dircache_list_files(fn, state, &num_finfo))
|
|---|
| 1154 | {
|
|---|
| 1155 | /* Got from cache. */
|
|---|
| 1156 | return(num_finfo);
|
|---|
| 1157 | }
|
|---|
| 1158 |
|
|---|
| 1159 | if (smbXcli_conn_has_async_calls(cli->conn)) {
|
|---|
| 1160 | /*
|
|---|
| 1161 | * Can't use sync call while an async call is in flight
|
|---|
| 1162 | */
|
|---|
| 1163 | status = NT_STATUS_INVALID_PARAMETER;
|
|---|
| 1164 | goto fail;
|
|---|
| 1165 | }
|
|---|
| 1166 | ev = samba_tevent_context_init(frame);
|
|---|
| 1167 | if (ev == NULL) {
|
|---|
| 1168 | goto fail;
|
|---|
| 1169 | }
|
|---|
| 1170 |
|
|---|
| 1171 | info_level = (smb1cli_conn_capabilities(cli->conn) & CAP_NT_SMBS)
|
|---|
| 1172 | ? SMB_FIND_FILE_BOTH_DIRECTORY_INFO : SMB_FIND_EA_SIZE;
|
|---|
| 1173 |
|
|---|
| 1174 | debuglocal(4,"list_files level %d. mask <%s>\n", info_level, mask);
|
|---|
| 1175 |
|
|---|
| 1176 | req = cli_list_send(frame, ev, cli, mask, attribute, info_level);
|
|---|
| 1177 | if (req == NULL) {
|
|---|
| 1178 | goto fail;
|
|---|
| 1179 | }
|
|---|
| 1180 | if (!tevent_req_poll(req, ev)) {
|
|---|
| 1181 | status = map_nt_error_from_unix(errno);
|
|---|
| 1182 | goto fail;
|
|---|
| 1183 | }
|
|---|
| 1184 |
|
|---|
| 1185 | status = cli_list_recv(req, frame, &finfo, &num_finfo);
|
|---|
| 1186 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1187 | goto fail;
|
|---|
| 1188 | }
|
|---|
| 1189 |
|
|---|
| 1190 | dircachectx = dircache_write_begin(state, num_finfo);
|
|---|
| 1191 |
|
|---|
| 1192 | debuglocal(4,"list_files: got %d files\n", num_finfo);
|
|---|
| 1193 |
|
|---|
| 1194 |
|
|---|
| 1195 | for (i=0; i<num_finfo; i++) {
|
|---|
| 1196 | //as samba and this client have different finfo, we need to convert
|
|---|
| 1197 | memset(&wrpfinfo, 0, sizeof(wrpfinfo));
|
|---|
| 1198 | wrpfinfo.size = finfo[i].size;
|
|---|
| 1199 | wrpfinfo.attr = finfo[i].mode;
|
|---|
| 1200 | wrpfinfo.ctime = convert_timespec_to_time_t(finfo[i].ctime_ts);
|
|---|
| 1201 | wrpfinfo.mtime = convert_timespec_to_time_t(finfo[i].mtime_ts);
|
|---|
| 1202 | wrpfinfo.atime = convert_timespec_to_time_t(finfo[i].atime_ts);
|
|---|
| 1203 | wrpfinfo.easize = finfo[i].easize;
|
|---|
| 1204 | strncpy(wrpfinfo.fname, finfo[i].name, sizeof(wrpfinfo.fname) -1);
|
|---|
| 1205 |
|
|---|
| 1206 | fn(cli->dfs_mountpoint, &wrpfinfo, mask, state);
|
|---|
| 1207 | // Also add the entry to the cache.
|
|---|
| 1208 | dircache_write_entry(dircachectx, &wrpfinfo);
|
|---|
| 1209 | }
|
|---|
| 1210 |
|
|---|
| 1211 | dircache_write_end(dircachectx);
|
|---|
| 1212 | fail:
|
|---|
| 1213 | TALLOC_FREE(frame);
|
|---|
| 1214 | return num_finfo;
|
|---|
| 1215 | }
|
|---|
| 1216 |
|
|---|
| 1217 | /*****************************************************
|
|---|
| 1218 | open a directory on the server
|
|---|
| 1219 | *******************************************************/
|
|---|
| 1220 | int _System smbwrp_filelist(smbwrp_server *srv, cli_state * cli, filelist_state * state)
|
|---|
| 1221 | {
|
|---|
| 1222 | if (!srv || !cli || !state || !*state->mask)
|
|---|
| 1223 | {
|
|---|
| 1224 | return maperror(EINVAL);
|
|---|
| 1225 | }
|
|---|
| 1226 | debuglocal(1,"Filelist <%s> on master <%s> wgrp <%s> server <%s> share <%s> clidev <%s>\n", state->mask, srv->master, srv->workgroup, srv->server_name, srv->share_name, cli->dev);
|
|---|
| 1227 | if (*srv->workgroup == 0 && *srv->server_name == 0)
|
|---|
| 1228 | {
|
|---|
| 1229 | smbwrp_special_add(".", state);
|
|---|
| 1230 | smbwrp_special_add("..", state);
|
|---|
| 1231 | cli_NetServerEnum(cli, srv->master, SV_TYPE_DOMAIN_ENUM,
|
|---|
| 1232 | smbwrp_share_add, state);
|
|---|
| 1233 | } else
|
|---|
| 1234 | if (*srv->server_name == 0)
|
|---|
| 1235 | {
|
|---|
| 1236 | smbwrp_special_add(".", state);
|
|---|
| 1237 | smbwrp_special_add("..", state);
|
|---|
| 1238 |
|
|---|
| 1239 | cli_NetServerEnum(cli, srv->workgroup, SV_TYPE_ALL,
|
|---|
| 1240 | smbwrp_share_add, state);
|
|---|
| 1241 | } else
|
|---|
| 1242 | #if 0 /* Causes crashes with Samba 4.x */
|
|---|
| 1243 | if ((strcmp(cli->dev,"IPC") == 0) || *srv->share_name == 0 || (stricmp(srv->share_name,"IPC$") == 0))
|
|---|
| 1244 | #else
|
|---|
| 1245 | if (/*(strcmp(cli->dev,"IPC") == 0) ||*/ *srv->share_name == 0 || (stricmp(srv->share_name,"IPC$") == 0))
|
|---|
| 1246 | #endif
|
|---|
| 1247 | {
|
|---|
| 1248 | smbwrp_special_add(".", state);
|
|---|
| 1249 | smbwrp_special_add("..", state);
|
|---|
| 1250 |
|
|---|
| 1251 | if (net_share_enum_rpc(cli, smbwrp_share_add, state) < 0 &&
|
|---|
| 1252 | cli_RNetShareEnum(cli,smbwrp_share_add, state) < 0)
|
|---|
| 1253 | {
|
|---|
| 1254 | return os2cli_errno(cli);
|
|---|
| 1255 | }
|
|---|
| 1256 | } else
|
|---|
| 1257 | #if 0 /* Causes crashes with Samba 4.x */
|
|---|
| 1258 | if (strncmp(cli->dev,"LPT",3) == 0)
|
|---|
| 1259 | {
|
|---|
| 1260 | smbwrp_special_add(".", state);
|
|---|
| 1261 | smbwrp_special_add("..", state);
|
|---|
| 1262 | if (cli_print_queue_state(cli, smbwrp_printjob_add, state) < 0)
|
|---|
| 1263 | {
|
|---|
| 1264 | return os2cli_errno(cli);
|
|---|
| 1265 | }
|
|---|
| 1266 | }
|
|---|
| 1267 | else
|
|---|
| 1268 | #endif
|
|---|
| 1269 | {
|
|---|
| 1270 | if (list_files(cli, state->mask, aHIDDEN|aSYSTEM|aDIR,
|
|---|
| 1271 | smbwrp_dir_add, state) < 0)
|
|---|
| 1272 | {
|
|---|
| 1273 | return os2cli_errno(cli);
|
|---|
| 1274 | }
|
|---|
| 1275 | }
|
|---|
| 1276 |
|
|---|
| 1277 | return 0;
|
|---|
| 1278 | }
|
|---|
| 1279 |
|
|---|
| 1280 | /*****************************************************
|
|---|
| 1281 | a wrapper for chdir()
|
|---|
| 1282 | *******************************************************/
|
|---|
| 1283 | int _System smbwrp_chdir(smbwrp_server *srv, cli_state * cli, char *fname)
|
|---|
| 1284 | {
|
|---|
| 1285 | unsigned short mode = aDIR;
|
|---|
| 1286 | smbwrp_fileinfo finfo = {0};
|
|---|
| 1287 | if (!cli || !fname)
|
|---|
| 1288 | {
|
|---|
| 1289 | return maperror(EINVAL);
|
|---|
| 1290 | }
|
|---|
| 1291 |
|
|---|
| 1292 | strncpy(finfo.fname, fname, sizeof(finfo.fname) - 1);
|
|---|
| 1293 | if (smbwrp_getattr(srv, cli, &finfo))
|
|---|
| 1294 | {
|
|---|
| 1295 | return os2cli_errno(cli);
|
|---|
| 1296 | }
|
|---|
| 1297 |
|
|---|
| 1298 | if (!(finfo.attr & aDIR)) {
|
|---|
| 1299 | return maperror(ENOTDIR);
|
|---|
| 1300 | }
|
|---|
| 1301 |
|
|---|
| 1302 | return 0;
|
|---|
| 1303 | }
|
|---|
| 1304 |
|
|---|
| 1305 |
|
|---|
| 1306 | /*****************************************************
|
|---|
| 1307 | a wrapper for mkdir()
|
|---|
| 1308 | *******************************************************/
|
|---|
| 1309 | int _System smbwrp_mkdir(cli_state * cli, char *fname)
|
|---|
| 1310 | {
|
|---|
| 1311 | if (!cli || !fname)
|
|---|
| 1312 | {
|
|---|
| 1313 | return maperror(EINVAL);
|
|---|
| 1314 | }
|
|---|
| 1315 |
|
|---|
| 1316 | if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname)))
|
|---|
| 1317 | {
|
|---|
| 1318 | return os2cli_errno(cli);
|
|---|
| 1319 | }
|
|---|
| 1320 | return 0;
|
|---|
| 1321 | }
|
|---|
| 1322 |
|
|---|
| 1323 | /*****************************************************
|
|---|
| 1324 | a wrapper for rmdir()
|
|---|
| 1325 | *******************************************************/
|
|---|
| 1326 | int _System smbwrp_rmdir(cli_state * cli, char *fname)
|
|---|
| 1327 | {
|
|---|
| 1328 | if (!cli || !fname)
|
|---|
| 1329 | {
|
|---|
| 1330 | return maperror(EINVAL);
|
|---|
| 1331 | }
|
|---|
| 1332 |
|
|---|
| 1333 | if (!NT_STATUS_IS_OK(cli_rmdir(cli, fname)))
|
|---|
| 1334 | {
|
|---|
| 1335 | return os2cli_errno(cli);
|
|---|
| 1336 | }
|
|---|
| 1337 | return 0;
|
|---|
| 1338 | }
|
|---|
| 1339 |
|
|---|
| 1340 | /*****************************************************
|
|---|
| 1341 | set EA for a path
|
|---|
| 1342 | *******************************************************/
|
|---|
| 1343 | int _System smbwrp_setea(cli_state * cli, char *fname, char * name, unsigned char * value, int size)
|
|---|
| 1344 | {
|
|---|
| 1345 | if (!cli || !fname || !name)
|
|---|
| 1346 | {
|
|---|
| 1347 | return maperror(EINVAL);
|
|---|
| 1348 | }
|
|---|
| 1349 | if (!NT_STATUS_IS_OK(cli_set_ea_path(cli, fname, name, value, size)))
|
|---|
| 1350 | {
|
|---|
| 1351 | return os2cli_errno(cli);
|
|---|
| 1352 | }
|
|---|
| 1353 | return 0;
|
|---|
| 1354 | }
|
|---|
| 1355 |
|
|---|
| 1356 | /*****************************************************
|
|---|
| 1357 | set EA for a file
|
|---|
| 1358 | *******************************************************/
|
|---|
| 1359 | int _System smbwrp_fsetea(cli_state * cli, smbwrp_file *file, char * name, unsigned char * value, int size)
|
|---|
| 1360 | {
|
|---|
| 1361 | if (!cli || !file || !name)
|
|---|
| 1362 | {
|
|---|
| 1363 | return maperror(EINVAL);
|
|---|
| 1364 | }
|
|---|
| 1365 | if (!NT_STATUS_IS_OK(cli_set_ea_fnum(cli, file->fd, name, value, size)))
|
|---|
| 1366 | {
|
|---|
| 1367 | return os2cli_errno(cli);
|
|---|
| 1368 | }
|
|---|
| 1369 | return 0;
|
|---|
| 1370 | }
|
|---|
| 1371 |
|
|---|
| 1372 |
|
|---|
| 1373 | #pragma pack(1)
|
|---|
| 1374 | typedef struct _FEA /* fea */
|
|---|
| 1375 | {
|
|---|
| 1376 | unsigned char fEA; /* flags */
|
|---|
| 1377 | unsigned char cbName; /* name length not including NULL */
|
|---|
| 1378 | unsigned short cbValue; /* value length */
|
|---|
| 1379 | } FEA;
|
|---|
| 1380 |
|
|---|
| 1381 | typedef struct _FEALIST /* feal */
|
|---|
| 1382 | {
|
|---|
| 1383 | unsigned long cbList; /* total bytes of structure including full list */
|
|---|
| 1384 | FEA list[1]; /* variable length FEA structures */
|
|---|
| 1385 | } FEALIST;
|
|---|
| 1386 | #pragma pack()
|
|---|
| 1387 |
|
|---|
| 1388 | static int unilistea(cli_state * cli, char *fname, void * buffer, unsigned long size)
|
|---|
| 1389 | {
|
|---|
| 1390 | int fnum, i;
|
|---|
| 1391 | int gotsize = sizeof(unsigned long);
|
|---|
| 1392 | size_t num_eas;
|
|---|
| 1393 | struct ea_struct *ea_list = NULL;
|
|---|
| 1394 | TALLOC_CTX *mem_ctx;
|
|---|
| 1395 | FEA * p;
|
|---|
| 1396 | FEALIST * pfealist;
|
|---|
| 1397 | char * q;
|
|---|
| 1398 |
|
|---|
| 1399 | mem_ctx = talloc_init("%d: ealist", _gettid());
|
|---|
| 1400 | pfealist = (FEALIST *)buffer;
|
|---|
| 1401 | pfealist->cbList = 0;
|
|---|
| 1402 |
|
|---|
| 1403 | if (!NT_STATUS_IS_OK(cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)))
|
|---|
| 1404 | {
|
|---|
| 1405 | debuglocal(4,"ea_get_file list failed - %s\n", cli_errstr(cli));
|
|---|
| 1406 | talloc_destroy(mem_ctx);
|
|---|
| 1407 | return os2cli_errno(cli);
|
|---|
| 1408 | }
|
|---|
| 1409 |
|
|---|
| 1410 | debuglocal(4,"num_eas = %d\n", num_eas);
|
|---|
| 1411 |
|
|---|
| 1412 | // we will count that os/2 max EA size for file is 64kb
|
|---|
| 1413 | p = pfealist->list;
|
|---|
| 1414 | for (i = 0; i < num_eas; i++)
|
|---|
| 1415 | {
|
|---|
| 1416 | int namelen = strlen(ea_list[i].name);
|
|---|
| 1417 | debuglocal(4, "%d Got EA <%s> with namelen %d, size %d. Gross %d. Buf %d\n", i, ea_list[i].name, namelen, ea_list[i].value.length, gotsize, size);
|
|---|
| 1418 | if (namelen > 0xFF || ea_list[i].value.length > 0xFFFF)
|
|---|
| 1419 | {
|
|---|
| 1420 | debuglocal(4, "Skip EA <%s> with namelen %d, size %d\n", ea_list[i].name, namelen, ea_list[i].value.length);
|
|---|
| 1421 | continue;
|
|---|
| 1422 | }
|
|---|
| 1423 | gotsize += sizeof(FEA) + namelen + ea_list[i].value.length + 1;
|
|---|
| 1424 | if (size >= gotsize)
|
|---|
| 1425 | {
|
|---|
| 1426 | p->fEA = 0;
|
|---|
| 1427 | p->cbName = namelen;
|
|---|
| 1428 | p->cbValue = ea_list[i].value.length;
|
|---|
| 1429 | q = (char *)(p + 1);
|
|---|
| 1430 | strncpy(q, ea_list[i].name, namelen + 1);
|
|---|
| 1431 | q += namelen + 1;
|
|---|
| 1432 | memcpy(q, ea_list[i].value.data, ea_list[i].value.length);
|
|---|
| 1433 | p = (FEA *)(q + ea_list[i].value.length);
|
|---|
| 1434 | }
|
|---|
| 1435 | }
|
|---|
| 1436 | pfealist->cbList = gotsize;
|
|---|
| 1437 | debuglocal(4,"ret size = %d\n", gotsize);
|
|---|
| 1438 |
|
|---|
| 1439 | talloc_destroy(mem_ctx);
|
|---|
| 1440 | return 0;
|
|---|
| 1441 | }
|
|---|
| 1442 |
|
|---|
| 1443 | /*****************************************************
|
|---|
| 1444 | lists EA of a path
|
|---|
| 1445 | *******************************************************/
|
|---|
| 1446 | int _System smbwrp_listea(cli_state * cli, char *fname, void * buffer, unsigned long size)
|
|---|
| 1447 | {
|
|---|
| 1448 | if (!cli || !fname || !buffer)
|
|---|
| 1449 | {
|
|---|
| 1450 | return maperror(EINVAL);
|
|---|
| 1451 | }
|
|---|
| 1452 |
|
|---|
| 1453 | debuglocal(4,"EALIst for <%s>\n", fname);
|
|---|
| 1454 | return unilistea(cli, fname, buffer, size);
|
|---|
| 1455 | }
|
|---|
| 1456 |
|
|---|
| 1457 | /*****************************************************
|
|---|
| 1458 | lists EA of a file
|
|---|
| 1459 | *******************************************************/
|
|---|
| 1460 | int _System smbwrp_flistea(cli_state * cli, smbwrp_file *file, void * buffer, unsigned long size)
|
|---|
| 1461 | {
|
|---|
| 1462 | if (!cli || !file || !buffer)
|
|---|
| 1463 | {
|
|---|
| 1464 | return maperror(EINVAL);
|
|---|
| 1465 | }
|
|---|
| 1466 |
|
|---|
| 1467 | debuglocal(4,"FEALIst for <%s>\n", file->fname);
|
|---|
| 1468 | return unilistea(cli, file->fname, buffer, size);
|
|---|
| 1469 | }
|
|---|
| 1470 |
|
|---|
| 1471 | /****************************************************************************
|
|---|
| 1472 | Check the space on a device.
|
|---|
| 1473 | ****************************************************************************/
|
|---|
| 1474 | int _System smbwrp_dskattr(cli_state * cli, FSALLOCATE *pfsa)
|
|---|
| 1475 | {
|
|---|
| 1476 | uint64_t total, bsize, avail;
|
|---|
| 1477 |
|
|---|
| 1478 | if (!cli || !pfsa)
|
|---|
| 1479 | {
|
|---|
| 1480 | return maperror(EINVAL);
|
|---|
| 1481 | }
|
|---|
| 1482 |
|
|---|
| 1483 | if (!NT_STATUS_IS_OK(cli_disk_size(cli, "", &bsize, &total, &avail)))
|
|---|
| 1484 | {
|
|---|
| 1485 | debuglocal(4,"Error in cli_disk_size: %s\n",cli_errstr(cli));
|
|---|
| 1486 | return os2cli_errno(cli);
|
|---|
| 1487 | }
|
|---|
| 1488 |
|
|---|
| 1489 | debuglocal(4,"\n\t\t%" PRIu64
|
|---|
| 1490 | " blocks of size %" PRIu64
|
|---|
| 1491 | ". %" PRIu64 " blocks available\n",
|
|---|
| 1492 | total, bsize, avail);
|
|---|
| 1493 |
|
|---|
| 1494 | // YD currently Samba return it in MB!
|
|---|
| 1495 | pfsa->cSectorUnit = 1;
|
|---|
| 1496 | if (bsize >= 65536)
|
|---|
| 1497 | {
|
|---|
| 1498 | pfsa->cUnit = total*1024;
|
|---|
| 1499 | pfsa->cUnitAvail = avail*1024;
|
|---|
| 1500 | pfsa->cbSector = bsize/1024;
|
|---|
| 1501 | }
|
|---|
| 1502 | else
|
|---|
| 1503 | {
|
|---|
| 1504 | pfsa->cUnit = total;
|
|---|
| 1505 | pfsa->cUnitAvail = avail;
|
|---|
| 1506 | pfsa->cbSector = bsize;
|
|---|
| 1507 | }
|
|---|
| 1508 |
|
|---|
| 1509 | return 0;
|
|---|
| 1510 | }
|
|---|
| 1511 |
|
|---|
| 1512 | /*****************************************************
|
|---|
| 1513 | Send an echo to the server to confirm it is still alive
|
|---|
| 1514 | *******************************************************/
|
|---|
| 1515 | int _System smbwrp_echo(cli_state * cli)
|
|---|
| 1516 | {
|
|---|
| 1517 | debuglocal(4," smbwrp_echo\n");
|
|---|
| 1518 | unsigned char garbage[16];
|
|---|
| 1519 | NTSTATUS status;
|
|---|
| 1520 | if (!cli)
|
|---|
| 1521 | {
|
|---|
| 1522 | return maperror(EINVAL);
|
|---|
| 1523 | }
|
|---|
| 1524 | /* Ping the server to keep the connection alive using SMBecho. */
|
|---|
| 1525 | memset(garbage, 0xf0, sizeof(garbage));
|
|---|
| 1526 | unsigned int old_timeout = cli->timeout;
|
|---|
| 1527 | cli->timeout = 2000;// we don't want to wait 20 seconds
|
|---|
| 1528 | status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
|
|---|
| 1529 | cli->timeout = old_timeout; // reset back to previous value
|
|---|
| 1530 | if (NT_STATUS_IS_OK(status)) {
|
|---|
| 1531 | return 0;
|
|---|
| 1532 | } else {
|
|---|
| 1533 | return -1;
|
|---|
| 1534 | }
|
|---|
| 1535 | }
|
|---|