source: trunk/samba/source/libsmb/libsmbclient.c@ 62

Last change on this file since 62 was 62, checked in by Paul Smedley, 18 years ago

Update source to 3.0.25c level

File size: 206.2 KB
Line 
1/*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2004
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23*/
24
25#include "includes.h"
26
27#include "include/libsmb_internal.h"
28
29struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir);
30struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
31 struct smbc_dirent *dirent);
32
33/*
34 * DOS Attribute values (used internally)
35 */
36typedef struct DOS_ATTR_DESC {
37 int mode;
38 SMB_OFF_T size;
39 time_t create_time;
40 time_t access_time;
41 time_t write_time;
42 time_t change_time;
43 SMB_INO_T inode;
44} DOS_ATTR_DESC;
45
46
47/*
48 * Internal flags for extended attributes
49 */
50
51/* internal mode values */
52#define SMBC_XATTR_MODE_ADD 1
53#define SMBC_XATTR_MODE_REMOVE 2
54#define SMBC_XATTR_MODE_REMOVE_ALL 3
55#define SMBC_XATTR_MODE_SET 4
56#define SMBC_XATTR_MODE_CHOWN 5
57#define SMBC_XATTR_MODE_CHGRP 6
58
59#define CREATE_ACCESS_READ READ_CONTROL_ACCESS
60
61/*We should test for this in configure ... */
62#ifndef ENOTSUP
63#define ENOTSUP EOPNOTSUPP
64#endif
65
66/*
67 * Functions exported by libsmb_cache.c that we need here
68 */
69int smbc_default_cache_functions(SMBCCTX *context);
70
71/*
72 * check if an element is part of the list.
73 * FIXME: Does not belong here !
74 * Can anyone put this in a macro in dlinklist.h ?
75 * -- Tom
76 */
77static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
78 if (!p || !list) return False;
79 do {
80 if (p == list) return True;
81 list = list->next;
82 } while (list);
83 return False;
84}
85
86/*
87 * Find an lsa pipe handle associated with a cli struct.
88 */
89static struct rpc_pipe_client *
90find_lsa_pipe_hnd(struct cli_state *ipc_cli)
91{
92 struct rpc_pipe_client *pipe_hnd;
93
94 for (pipe_hnd = ipc_cli->pipe_list;
95 pipe_hnd;
96 pipe_hnd = pipe_hnd->next) {
97
98 if (pipe_hnd->pipe_idx == PI_LSARPC) {
99 return pipe_hnd;
100 }
101 }
102
103 return NULL;
104}
105
106static int
107smbc_close_ctx(SMBCCTX *context,
108 SMBCFILE *file);
109static off_t
110smbc_lseek_ctx(SMBCCTX *context,
111 SMBCFILE *file,
112 off_t offset,
113 int whence);
114
115extern BOOL in_client;
116
117/*
118 * Is the logging working / configfile read ?
119 */
120static int smbc_initialized = 0;
121
122static int
123hex2int( unsigned int _char )
124{
125 if ( _char >= 'A' && _char <='F')
126 return _char - 'A' + 10;
127 if ( _char >= 'a' && _char <='f')
128 return _char - 'a' + 10;
129 if ( _char >= '0' && _char <='9')
130 return _char - '0';
131 return -1;
132}
133
134/*
135 * smbc_urldecode()
136 *
137 * Convert strings of %xx to their single character equivalent. Each 'x' must
138 * be a valid hexadecimal digit, or that % sequence is left undecoded.
139 *
140 * dest may, but need not be, the same pointer as src.
141 *
142 * Returns the number of % sequences which could not be converted due to lack
143 * of two following hexadecimal digits.
144 */
145int
146smbc_urldecode(char *dest, char * src, size_t max_dest_len)
147{
148 int old_length = strlen(src);
149 int i = 0;
150 int err_count = 0;
151 pstring temp;
152 char * p;
153
154 if ( old_length == 0 ) {
155 return 0;
156 }
157
158 p = temp;
159 while ( i < old_length ) {
160 unsigned char character = src[ i++ ];
161
162 if (character == '%') {
163 int a = i+1 < old_length ? hex2int( src[i] ) : -1;
164 int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
165
166 /* Replace valid sequence */
167 if (a != -1 && b != -1) {
168
169 /* Replace valid %xx sequence with %dd */
170 character = (a * 16) + b;
171
172 if (character == '\0') {
173 break; /* Stop at %00 */
174 }
175
176 i += 2;
177 } else {
178
179 err_count++;
180 }
181 }
182
183 *p++ = character;
184 }
185
186 *p = '\0';
187
188 strncpy(dest, temp, max_dest_len - 1);
189 dest[max_dest_len - 1] = '\0';
190
191 return err_count;
192}
193
194/*
195 * smbc_urlencode()
196 *
197 * Convert any characters not specifically allowed in a URL into their %xx
198 * equivalent.
199 *
200 * Returns the remaining buffer length.
201 */
202int
203smbc_urlencode(char * dest, char * src, int max_dest_len)
204{
205 char hex[] = "0123456789ABCDEF";
206
207 for (; *src != '\0' && max_dest_len >= 3; src++) {
208
209 if ((*src < '0' &&
210 *src != '-' &&
211 *src != '.') ||
212 (*src > '9' &&
213 *src < 'A') ||
214 (*src > 'Z' &&
215 *src < 'a' &&
216 *src != '_') ||
217 (*src > 'z')) {
218 *dest++ = '%';
219 *dest++ = hex[(*src >> 4) & 0x0f];
220 *dest++ = hex[*src & 0x0f];
221 max_dest_len -= 3;
222 } else {
223 *dest++ = *src;
224 max_dest_len--;
225 }
226 }
227
228 *dest++ = '\0';
229 max_dest_len--;
230
231 return max_dest_len;
232}
233
234/*
235 * Function to parse a path and turn it into components
236 *
237 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
238 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
239 * general format ("smb:" only; we do not look for "cifs:").
240 *
241 *
242 * We accept:
243 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
244 *
245 * Meaning of URLs:
246 *
247 * smb:// Show all workgroups.
248 *
249 * The method of locating the list of workgroups varies
250 * depending upon the setting of the context variable
251 * context->options.browse_max_lmb_count. This value
252 * determine the maximum number of local master browsers to
253 * query for the list of workgroups. In order to ensure that
254 * a complete list of workgroups is obtained, all master
255 * browsers must be queried, but if there are many
256 * workgroups, the time spent querying can begin to add up.
257 * For small networks (not many workgroups), it is suggested
258 * that this variable be set to 0, indicating query all local
259 * master browsers. When the network has many workgroups, a
260 * reasonable setting for this variable might be around 3.
261 *
262 * smb://name/ if name<1D> or name<1B> exists, list servers in
263 * workgroup, else, if name<20> exists, list all shares
264 * for server ...
265 *
266 * If "options" are provided, this function returns the entire option list as a
267 * string, for later parsing by the caller. Note that currently, no options
268 * are supported.
269 */
270
271static const char *smbc_prefix = "smb:";
272
273static int
274smbc_parse_path(SMBCCTX *context,
275 const char *fname,
276 char *workgroup, int workgroup_len,
277 char *server, int server_len,
278 char *share, int share_len,
279 char *path, int path_len,
280 char *user, int user_len,
281 char *password, int password_len,
282 char *options, int options_len)
283{
284 static pstring s;
285 pstring userinfo;
286 const char *p;
287 char *q, *r;
288 int len;
289
290 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
291
292 /*
293 * Assume we wont find an authentication domain to parse, so default
294 * to the workgroup in the provided context.
295 */
296 if (workgroup != NULL) {
297 strncpy(workgroup, context->workgroup, workgroup_len - 1);
298 workgroup[workgroup_len - 1] = '\0';
299 }
300
301 if (options != NULL && options_len > 0) {
302 options[0] = (char)0;
303 }
304 pstrcpy(s, fname);
305
306 /* see if it has the right prefix */
307 len = strlen(smbc_prefix);
308 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
309 return -1; /* What about no smb: ? */
310 }
311
312 p = s + len;
313
314 /* Watch the test below, we are testing to see if we should exit */
315
316 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
317
318 DEBUG(1, ("Invalid path (does not begin with smb://"));
319 return -1;
320
321 }
322
323 p += 2; /* Skip the double slash */
324
325 /* See if any options were specified */
326 if ((q = strrchr(p, '?')) != NULL ) {
327 /* There are options. Null terminate here and point to them */
328 *q++ = '\0';
329
330 DEBUG(4, ("Found options '%s'", q));
331
332 /* Copy the options */
333 if (options != NULL && options_len > 0) {
334 safe_strcpy(options, q, options_len - 1);
335 }
336 }
337
338 if (*p == (char)0)
339 goto decoding;
340
341 if (*p == '/') {
342 int wl = strlen(context->workgroup);
343
344 if (wl > 16) {
345 wl = 16;
346 }
347
348 strncpy(server, context->workgroup, wl);
349 server[wl] = '\0';
350 return 0;
351 }
352
353 /*
354 * ok, its for us. Now parse out the server, share etc.
355 *
356 * However, we want to parse out [[domain;]user[:password]@] if it
357 * exists ...
358 */
359
360 /* check that '@' occurs before '/', if '/' exists at all */
361 q = strchr_m(p, '@');
362 r = strchr_m(p, '/');
363 if (q && (!r || q < r)) {
364 pstring username, passwd, domain;
365 const char *u = userinfo;
366
367 next_token_no_ltrim(&p, userinfo, "@", sizeof(fstring));
368
369 username[0] = passwd[0] = domain[0] = 0;
370
371 if (strchr_m(u, ';')) {
372
373 next_token_no_ltrim(&u, domain, ";", sizeof(fstring));
374
375 }
376
377 if (strchr_m(u, ':')) {
378
379 next_token_no_ltrim(&u, username, ":", sizeof(fstring));
380
381 pstrcpy(passwd, u);
382
383 }
384 else {
385
386 pstrcpy(username, u);
387
388 }
389
390 if (domain[0] && workgroup) {
391 strncpy(workgroup, domain, workgroup_len - 1);
392 workgroup[workgroup_len - 1] = '\0';
393 }
394
395 if (username[0]) {
396 strncpy(user, username, user_len - 1);
397 user[user_len - 1] = '\0';
398 }
399
400 if (passwd[0]) {
401 strncpy(password, passwd, password_len - 1);
402 password[password_len - 1] = '\0';
403 }
404
405 }
406
407 if (!next_token(&p, server, "/", sizeof(fstring))) {
408
409 return -1;
410
411 }
412
413 if (*p == (char)0) goto decoding; /* That's it ... */
414
415 if (!next_token(&p, share, "/", sizeof(fstring))) {
416
417 return -1;
418
419 }
420
421 /*
422 * Prepend a leading slash if there's a file path, as required by
423 * NetApp filers.
424 */
425 *path = '\0';
426 if (*p != '\0') {
427 *path = '/';
428 safe_strcpy(path + 1, p, path_len - 2);
429 }
430
431 all_string_sub(path, "/", "\\", 0);
432
433 decoding:
434 (void) smbc_urldecode(path, path, path_len);
435 (void) smbc_urldecode(server, server, server_len);
436 (void) smbc_urldecode(share, share, share_len);
437 (void) smbc_urldecode(user, user, user_len);
438 (void) smbc_urldecode(password, password, password_len);
439
440 return 0;
441}
442
443/*
444 * Verify that the options specified in a URL are valid
445 */
446static int
447smbc_check_options(char *server,
448 char *share,
449 char *path,
450 char *options)
451{
452 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
453 "path='%s' options='%s'\n",
454 server, share, path, options));
455
456 /* No options at all is always ok */
457 if (! *options) return 0;
458
459 /* Currently, we don't support any options. */
460 return -1;
461}
462
463/*
464 * Convert an SMB error into a UNIX error ...
465 */
466static int
467smbc_errno(SMBCCTX *context,
468 struct cli_state *c)
469{
470 int ret = cli_errno(c);
471
472 if (cli_is_dos_error(c)) {
473 uint8 eclass;
474 uint32 ecode;
475
476 cli_dos_error(c, &eclass, &ecode);
477
478 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
479 (int)eclass, (int)ecode, (int)ecode, ret));
480 } else {
481 NTSTATUS status;
482
483 status = cli_nt_error(c);
484
485 DEBUG(3,("smbc errno %s -> %d\n",
486 nt_errstr(status), ret));
487 }
488
489 return ret;
490}
491
492/*
493 * Check a server for being alive and well.
494 * returns 0 if the server is in shape. Returns 1 on error
495 *
496 * Also useable outside libsmbclient to enable external cache
497 * to do some checks too.
498 */
499static int
500smbc_check_server(SMBCCTX * context,
501 SMBCSRV * server)
502{
503 socklen_t size;
504 struct sockaddr addr;
505
506 /*
507 * Although the use of port 139 is not a guarantee that we're using
508 * netbios, we assume so. We don't want to send a keepalive packet if
509 * not netbios because it's not valid, and Vista, at least,
510 * disconnects the client on such a request.
511 */
512 if (server->cli->port == 139) {
513 /* Assuming netbios. Send a keepalive packet */
514 if ( send_keepalive(server->cli->fd) == False ) {
515 return 1;
516 }
517 } else {
518 /*
519 * Assuming not netbios. Try a different method to detect if
520 * the connection is still alive.
521 */
522 size = sizeof(addr);
523 if (getpeername(server->cli->fd, &addr, &size) == -1) {
524 return 1;
525 }
526 }
527
528 /* connection is ok */
529 return 0;
530}
531
532/*
533 * Remove a server from the cached server list it's unused.
534 * On success, 0 is returned. 1 is returned if the server could not be removed.
535 *
536 * Also useable outside libsmbclient
537 */
538int
539smbc_remove_unused_server(SMBCCTX * context,
540 SMBCSRV * srv)
541{
542 SMBCFILE * file;
543
544 /* are we being fooled ? */
545 if (!context || !context->internal ||
546 !context->internal->_initialized || !srv) return 1;
547
548
549 /* Check all open files/directories for a relation with this server */
550 for (file = context->internal->_files; file; file=file->next) {
551 if (file->srv == srv) {
552 /* Still used */
553 DEBUG(3, ("smbc_remove_usused_server: "
554 "%p still used by %p.\n",
555 srv, file));
556 return 1;
557 }
558 }
559
560 DLIST_REMOVE(context->internal->_servers, srv);
561
562 cli_shutdown(srv->cli);
563 srv->cli = NULL;
564
565 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
566
567 context->callbacks.remove_cached_srv_fn(context, srv);
568
569 SAFE_FREE(srv);
570
571 return 0;
572}
573
574static SMBCSRV *
575find_server(SMBCCTX *context,
576 const char *server,
577 const char *share,
578 fstring workgroup,
579 fstring username,
580 fstring password)
581{
582 SMBCSRV *srv;
583 int auth_called = 0;
584
585 check_server_cache:
586
587 srv = context->callbacks.get_cached_srv_fn(context, server, share,
588 workgroup, username);
589
590 if (!auth_called && !srv && (!username[0] || !password[0])) {
591 if (context->internal->_auth_fn_with_context != NULL) {
592 context->internal->_auth_fn_with_context(
593 context,
594 server, share,
595 workgroup, sizeof(fstring),
596 username, sizeof(fstring),
597 password, sizeof(fstring));
598 } else {
599 context->callbacks.auth_fn(
600 server, share,
601 workgroup, sizeof(fstring),
602 username, sizeof(fstring),
603 password, sizeof(fstring));
604 }
605
606 /*
607 * However, smbc_auth_fn may have picked up info relating to
608 * an existing connection, so try for an existing connection
609 * again ...
610 */
611 auth_called = 1;
612 goto check_server_cache;
613
614 }
615
616 if (srv) {
617 if (context->callbacks.check_server_fn(context, srv)) {
618 /*
619 * This server is no good anymore
620 * Try to remove it and check for more possible
621 * servers in the cache
622 */
623 if (context->callbacks.remove_unused_server_fn(context,
624 srv)) {
625 /*
626 * We could not remove the server completely,
627 * remove it from the cache so we will not get
628 * it again. It will be removed when the last
629 * file/dir is closed.
630 */
631 context->callbacks.remove_cached_srv_fn(context,
632 srv);
633 }
634
635 /*
636 * Maybe there are more cached connections to this
637 * server
638 */
639 goto check_server_cache;
640 }
641
642 return srv;
643 }
644
645 return NULL;
646}
647
648/*
649 * Connect to a server, possibly on an existing connection
650 *
651 * Here, what we want to do is: If the server and username
652 * match an existing connection, reuse that, otherwise, establish a
653 * new connection.
654 *
655 * If we have to create a new connection, call the auth_fn to get the
656 * info we need, unless the username and password were passed in.
657 */
658
659static SMBCSRV *
660smbc_server(SMBCCTX *context,
661 BOOL connect_if_not_found,
662 const char *server,
663 const char *share,
664 fstring workgroup,
665 fstring username,
666 fstring password)
667{
668 SMBCSRV *srv=NULL;
669 struct cli_state *c;
670 struct nmb_name called, calling;
671 const char *server_n = server;
672 pstring ipenv;
673 struct in_addr ip;
674 int tried_reverse = 0;
675 int port_try_first;
676 int port_try_next;
677 const char *username_used;
678 NTSTATUS status;
679
680 zero_ip(&ip);
681 ZERO_STRUCT(c);
682
683 if (server[0] == 0) {
684 errno = EPERM;
685 return NULL;
686 }
687
688 /* Look for a cached connection */
689 srv = find_server(context, server, share,
690 workgroup, username, password);
691
692 /*
693 * If we found a connection and we're only allowed one share per
694 * server...
695 */
696 if (srv && *share != '\0' && context->options.one_share_per_server) {
697
698 /*
699 * ... then if there's no current connection to the share,
700 * connect to it. find_server(), or rather the function
701 * pointed to by context->callbacks.get_cached_srv_fn which
702 * was called by find_server(), will have issued a tree
703 * disconnect if the requested share is not the same as the
704 * one that was already connected.
705 */
706 if (srv->cli->cnum == (uint16) -1) {
707 /* Ensure we have accurate auth info */
708 if (context->internal->_auth_fn_with_context != NULL) {
709 context->internal->_auth_fn_with_context(
710 context,
711 server, share,
712 workgroup, sizeof(fstring),
713 username, sizeof(fstring),
714 password, sizeof(fstring));
715 } else {
716 context->callbacks.auth_fn(
717 server, share,
718 workgroup, sizeof(fstring),
719 username, sizeof(fstring),
720 password, sizeof(fstring));
721 }
722
723 if (! cli_send_tconX(srv->cli, share, "?????",
724 password, strlen(password)+1)) {
725
726 errno = smbc_errno(context, srv->cli);
727 cli_shutdown(srv->cli);
728 srv->cli = NULL;
729 context->callbacks.remove_cached_srv_fn(context,
730 srv);
731 srv = NULL;
732 }
733
734 /*
735 * Regenerate the dev value since it's based on both
736 * server and share
737 */
738 if (srv) {
739 srv->dev = (dev_t)(str_checksum(server) ^
740 str_checksum(share));
741 }
742 }
743 }
744
745 /* If we have a connection... */
746 if (srv) {
747
748 /* ... then we're done here. Give 'em what they came for. */
749 return srv;
750 }
751
752 /* If we're not asked to connect when a connection doesn't exist... */
753 if (! connect_if_not_found) {
754 /* ... then we're done here. */
755 return NULL;
756 }
757
758 make_nmb_name(&calling, context->netbios_name, 0x0);
759 make_nmb_name(&called , server, 0x20);
760
761 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
762
763 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
764
765 again:
766 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
767
768 zero_ip(&ip);
769
770 /* have to open a new connection */
771 if ((c = cli_initialise()) == NULL) {
772 errno = ENOMEM;
773 return NULL;
774 }
775
776 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
777 c->use_kerberos = True;
778 }
779 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
780 c->fallback_after_kerberos = True;
781 }
782
783 c->timeout = context->timeout;
784
785 /*
786 * Force use of port 139 for first try if share is $IPC, empty, or
787 * null, so browse lists can work
788 */
789 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
790 port_try_first = 139;
791 port_try_next = 445;
792 } else {
793 port_try_first = 445;
794 port_try_next = 139;
795 }
796
797 c->port = port_try_first;
798
799 status = cli_connect(c, server_n, &ip);
800 if (!NT_STATUS_IS_OK(status)) {
801
802 /* First connection attempt failed. Try alternate port. */
803 c->port = port_try_next;
804
805 status = cli_connect(c, server_n, &ip);
806 if (!NT_STATUS_IS_OK(status)) {
807 cli_shutdown(c);
808 errno = ETIMEDOUT;
809 return NULL;
810 }
811 }
812
813 if (!cli_session_request(c, &calling, &called)) {
814 cli_shutdown(c);
815 if (strcmp(called.name, "*SMBSERVER")) {
816 make_nmb_name(&called , "*SMBSERVER", 0x20);
817 goto again;
818 } else { /* Try one more time, but ensure we don't loop */
819
820 /* Only try this if server is an IP address ... */
821
822 if (is_ipaddress(server) && !tried_reverse) {
823 fstring remote_name;
824 struct in_addr rem_ip;
825
826 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
827 DEBUG(4, ("Could not convert IP address "
828 "%s to struct in_addr\n", server));
829 errno = ETIMEDOUT;
830 return NULL;
831 }
832
833 tried_reverse++; /* Yuck */
834
835 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
836 make_nmb_name(&called, remote_name, 0x20);
837 goto again;
838 }
839 }
840 }
841 errno = ETIMEDOUT;
842 return NULL;
843 }
844
845 DEBUG(4,(" session request ok\n"));
846
847 if (!cli_negprot(c)) {
848 cli_shutdown(c);
849 errno = ETIMEDOUT;
850 return NULL;
851 }
852
853 username_used = username;
854
855 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
856 password, strlen(password),
857 password, strlen(password),
858 workgroup))) {
859
860 /* Failed. Try an anonymous login, if allowed by flags. */
861 username_used = "";
862
863 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
864 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
865 password, 1,
866 password, 0,
867 workgroup))) {
868
869 cli_shutdown(c);
870 errno = EPERM;
871 return NULL;
872 }
873 }
874
875 DEBUG(4,(" session setup ok\n"));
876
877 if (!cli_send_tconX(c, share, "?????",
878 password, strlen(password)+1)) {
879 errno = smbc_errno(context, c);
880 cli_shutdown(c);
881 return NULL;
882 }
883
884 DEBUG(4,(" tconx ok\n"));
885
886 /*
887 * Ok, we have got a nice connection
888 * Let's allocate a server structure.
889 */
890
891 srv = SMB_MALLOC_P(SMBCSRV);
892 if (!srv) {
893 errno = ENOMEM;
894 goto failed;
895 }
896
897 ZERO_STRUCTP(srv);
898 srv->cli = c;
899 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
900 srv->no_pathinfo = False;
901 srv->no_pathinfo2 = False;
902 srv->no_nt_session = False;
903
904 /* now add it to the cache (internal or external) */
905 /* Let the cache function set errno if it wants to */
906 errno = 0;
907 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
908 int saved_errno = errno;
909 DEBUG(3, (" Failed to add server to cache\n"));
910 errno = saved_errno;
911 if (errno == 0) {
912 errno = ENOMEM;
913 }
914 goto failed;
915 }
916
917 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
918 server, share, srv));
919
920 DLIST_ADD(context->internal->_servers, srv);
921 return srv;
922
923 failed:
924 cli_shutdown(c);
925 if (!srv) {
926 return NULL;
927 }
928
929 SAFE_FREE(srv);
930 return NULL;
931}
932
933/*
934 * Connect to a server for getting/setting attributes, possibly on an existing
935 * connection. This works similarly to smbc_server().
936 */
937static SMBCSRV *
938smbc_attr_server(SMBCCTX *context,
939 const char *server,
940 const char *share,
941 fstring workgroup,
942 fstring username,
943 fstring password,
944 POLICY_HND *pol)
945{
946 int flags;
947 struct in_addr ip;
948 struct cli_state *ipc_cli;
949 struct rpc_pipe_client *pipe_hnd;
950 NTSTATUS nt_status;
951 SMBCSRV *ipc_srv=NULL;
952
953 /*
954 * See if we've already created this special connection. Reference
955 * our "special" share name '*IPC$', which is an impossible real share
956 * name due to the leading asterisk.
957 */
958 ipc_srv = find_server(context, server, "*IPC$",
959 workgroup, username, password);
960 if (!ipc_srv) {
961
962 /* We didn't find a cached connection. Get the password */
963 if (*password == '\0') {
964 /* ... then retrieve it now. */
965 if (context->internal->_auth_fn_with_context != NULL) {
966 context->internal->_auth_fn_with_context(
967 context,
968 server, share,
969 workgroup, sizeof(fstring),
970 username, sizeof(fstring),
971 password, sizeof(fstring));
972 } else {
973 context->callbacks.auth_fn(
974 server, share,
975 workgroup, sizeof(fstring),
976 username, sizeof(fstring),
977 password, sizeof(fstring));
978 }
979 }
980
981 flags = 0;
982 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
983 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
984 }
985
986 zero_ip(&ip);
987 nt_status = cli_full_connection(&ipc_cli,
988 global_myname(), server,
989 &ip, 0, "IPC$", "?????",
990 username, workgroup,
991 password, flags,
992 Undefined, NULL);
993 if (! NT_STATUS_IS_OK(nt_status)) {
994 DEBUG(1,("cli_full_connection failed! (%s)\n",
995 nt_errstr(nt_status)));
996 errno = ENOTSUP;
997 return NULL;
998 }
999
1000 ipc_srv = SMB_MALLOC_P(SMBCSRV);
1001 if (!ipc_srv) {
1002 errno = ENOMEM;
1003 cli_shutdown(ipc_cli);
1004 return NULL;
1005 }
1006
1007 ZERO_STRUCTP(ipc_srv);
1008 ipc_srv->cli = ipc_cli;
1009
1010 if (pol) {
1011 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1012 PI_LSARPC,
1013 &nt_status);
1014 if (!pipe_hnd) {
1015 DEBUG(1, ("cli_nt_session_open fail!\n"));
1016 errno = ENOTSUP;
1017 cli_shutdown(ipc_srv->cli);
1018 free(ipc_srv);
1019 return NULL;
1020 }
1021
1022 /*
1023 * Some systems don't support
1024 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1025 * so we might as well do it too.
1026 */
1027
1028 nt_status = rpccli_lsa_open_policy(
1029 pipe_hnd,
1030 ipc_srv->cli->mem_ctx,
1031 True,
1032 GENERIC_EXECUTE_ACCESS,
1033 pol);
1034
1035 if (!NT_STATUS_IS_OK(nt_status)) {
1036 errno = smbc_errno(context, ipc_srv->cli);
1037 cli_shutdown(ipc_srv->cli);
1038 return NULL;
1039 }
1040 }
1041
1042 /* now add it to the cache (internal or external) */
1043
1044 errno = 0; /* let cache function set errno if it likes */
1045 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
1046 server,
1047 "*IPC$",
1048 workgroup,
1049 username)) {
1050 DEBUG(3, (" Failed to add server to cache\n"));
1051 if (errno == 0) {
1052 errno = ENOMEM;
1053 }
1054 cli_shutdown(ipc_srv->cli);
1055 free(ipc_srv);
1056 return NULL;
1057 }
1058
1059 DLIST_ADD(context->internal->_servers, ipc_srv);
1060 }
1061
1062 return ipc_srv;
1063}
1064
1065/*
1066 * Routine to open() a file ...
1067 */
1068
1069static SMBCFILE *
1070smbc_open_ctx(SMBCCTX *context,
1071 const char *fname,
1072 int flags,
1073 mode_t mode)
1074{
1075 fstring server, share, user, password, workgroup;
1076 pstring path;
1077 pstring targetpath;
1078 struct cli_state *targetcli;
1079 SMBCSRV *srv = NULL;
1080 SMBCFILE *file = NULL;
1081 int fd;
1082
1083 if (!context || !context->internal ||
1084 !context->internal->_initialized) {
1085
1086 errno = EINVAL; /* Best I can think of ... */
1087 return NULL;
1088
1089 }
1090
1091 if (!fname) {
1092
1093 errno = EINVAL;
1094 return NULL;
1095
1096 }
1097
1098 if (smbc_parse_path(context, fname,
1099 workgroup, sizeof(workgroup),
1100 server, sizeof(server),
1101 share, sizeof(share),
1102 path, sizeof(path),
1103 user, sizeof(user),
1104 password, sizeof(password),
1105 NULL, 0)) {
1106 errno = EINVAL;
1107 return NULL;
1108 }
1109
1110 if (user[0] == (char)0) fstrcpy(user, context->user);
1111
1112 srv = smbc_server(context, True,
1113 server, share, workgroup, user, password);
1114
1115 if (!srv) {
1116
1117 if (errno == EPERM) errno = EACCES;
1118 return NULL; /* smbc_server sets errno */
1119
1120 }
1121
1122 /* Hmmm, the test for a directory is suspect here ... FIXME */
1123
1124 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1125
1126 fd = -1;
1127
1128 }
1129 else {
1130
1131 file = SMB_MALLOC_P(SMBCFILE);
1132
1133 if (!file) {
1134
1135 errno = ENOMEM;
1136 return NULL;
1137
1138 }
1139
1140 ZERO_STRUCTP(file);
1141
1142 /*d_printf(">>>open: resolving %s\n", path);*/
1143 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1144 {
1145 d_printf("Could not resolve %s\n", path);
1146 SAFE_FREE(file);
1147 return NULL;
1148 }
1149 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1150
1151 if ((fd = cli_open(targetcli, targetpath, flags,
1152 context->internal->_share_mode)) < 0) {
1153
1154 /* Handle the error ... */
1155
1156 SAFE_FREE(file);
1157 errno = smbc_errno(context, targetcli);
1158 return NULL;
1159
1160 }
1161
1162 /* Fill in file struct */
1163
1164 file->cli_fd = fd;
1165 file->fname = SMB_STRDUP(fname);
1166 file->srv = srv;
1167 file->offset = 0;
1168 file->file = True;
1169
1170 DLIST_ADD(context->internal->_files, file);
1171
1172 /*
1173 * If the file was opened in O_APPEND mode, all write
1174 * operations should be appended to the file. To do that,
1175 * though, using this protocol, would require a getattrE()
1176 * call for each and every write, to determine where the end
1177 * of the file is. (There does not appear to be an append flag
1178 * in the protocol.) Rather than add all of that overhead of
1179 * retrieving the current end-of-file offset prior to each
1180 * write operation, we'll assume that most append operations
1181 * will continuously write, so we'll just set the offset to
1182 * the end of the file now and hope that's adequate.
1183 *
1184 * Note to self: If this proves inadequate, and O_APPEND
1185 * should, in some cases, be forced for each write, add a
1186 * field in the context options structure, for
1187 * "strict_append_mode" which would select between the current
1188 * behavior (if FALSE) or issuing a getattrE() prior to each
1189 * write and forcing the write to the end of the file (if
1190 * TRUE). Adding that capability will likely require adding
1191 * an "append" flag into the _SMBCFILE structure to track
1192 * whether a file was opened in O_APPEND mode. -- djl
1193 */
1194 if (flags & O_APPEND) {
1195 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1196 (void) smbc_close_ctx(context, file);
1197 errno = ENXIO;
1198 return NULL;
1199 }
1200 }
1201
1202 return file;
1203
1204 }
1205
1206 /* Check if opendir needed ... */
1207
1208 if (fd == -1) {
1209 int eno = 0;
1210
1211 eno = smbc_errno(context, srv->cli);
1212 file = context->opendir(context, fname);
1213 if (!file) errno = eno;
1214 return file;
1215
1216 }
1217
1218 errno = EINVAL; /* FIXME, correct errno ? */
1219 return NULL;
1220
1221}
1222
1223/*
1224 * Routine to create a file
1225 */
1226
1227static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1228
1229static SMBCFILE *
1230smbc_creat_ctx(SMBCCTX *context,
1231 const char *path,
1232 mode_t mode)
1233{
1234
1235 if (!context || !context->internal ||
1236 !context->internal->_initialized) {
1237
1238 errno = EINVAL;
1239 return NULL;
1240
1241 }
1242
1243 return smbc_open_ctx(context, path, creat_bits, mode);
1244}
1245
1246/*
1247 * Routine to read() a file ...
1248 */
1249
1250static ssize_t
1251smbc_read_ctx(SMBCCTX *context,
1252 SMBCFILE *file,
1253 void *buf,
1254 size_t count)
1255{
1256 int ret;
1257 fstring server, share, user, password;
1258 pstring path, targetpath;
1259 struct cli_state *targetcli;
1260
1261 /*
1262 * offset:
1263 *
1264 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1265 * appears to pass file->offset (which is type off_t) differently than
1266 * a local variable of type off_t. Using local variable "offset" in
1267 * the call to cli_read() instead of file->offset fixes a problem
1268 * retrieving data at an offset greater than 4GB.
1269 */
1270 off_t offset;
1271
1272 if (!context || !context->internal ||
1273 !context->internal->_initialized) {
1274
1275 errno = EINVAL;
1276 return -1;
1277
1278 }
1279
1280 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1281
1282 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1283
1284 errno = EBADF;
1285 return -1;
1286
1287 }
1288
1289 offset = file->offset;
1290
1291 /* Check that the buffer exists ... */
1292
1293 if (buf == NULL) {
1294
1295 errno = EINVAL;
1296 return -1;
1297
1298 }
1299
1300 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1301 if (smbc_parse_path(context, file->fname,
1302 NULL, 0,
1303 server, sizeof(server),
1304 share, sizeof(share),
1305 path, sizeof(path),
1306 user, sizeof(user),
1307 password, sizeof(password),
1308 NULL, 0)) {
1309 errno = EINVAL;
1310 return -1;
1311 }
1312
1313 /*d_printf(">>>read: resolving %s\n", path);*/
1314 if (!cli_resolve_path("", file->srv->cli, path,
1315 &targetcli, targetpath))
1316 {
1317 d_printf("Could not resolve %s\n", path);
1318 return -1;
1319 }
1320 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1321
1322 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1323
1324 if (ret < 0) {
1325
1326 errno = smbc_errno(context, targetcli);
1327 return -1;
1328
1329 }
1330
1331 file->offset += ret;
1332
1333 DEBUG(4, (" --> %d\n", ret));
1334
1335 return ret; /* Success, ret bytes of data ... */
1336
1337}
1338
1339/*
1340 * Routine to write() a file ...
1341 */
1342
1343static ssize_t
1344smbc_write_ctx(SMBCCTX *context,
1345 SMBCFILE *file,
1346 void *buf,
1347 size_t count)
1348{
1349 int ret;
1350 off_t offset;
1351 fstring server, share, user, password;
1352 pstring path, targetpath;
1353 struct cli_state *targetcli;
1354
1355 /* First check all pointers before dereferencing them */
1356
1357 if (!context || !context->internal ||
1358 !context->internal->_initialized) {
1359
1360 errno = EINVAL;
1361 return -1;
1362
1363 }
1364
1365 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1366
1367 errno = EBADF;
1368 return -1;
1369
1370 }
1371
1372 /* Check that the buffer exists ... */
1373
1374 if (buf == NULL) {
1375
1376 errno = EINVAL;
1377 return -1;
1378
1379 }
1380
1381 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1382
1383 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1384 if (smbc_parse_path(context, file->fname,
1385 NULL, 0,
1386 server, sizeof(server),
1387 share, sizeof(share),
1388 path, sizeof(path),
1389 user, sizeof(user),
1390 password, sizeof(password),
1391 NULL, 0)) {
1392 errno = EINVAL;
1393 return -1;
1394 }
1395
1396 /*d_printf(">>>write: resolving %s\n", path);*/
1397 if (!cli_resolve_path("", file->srv->cli, path,
1398 &targetcli, targetpath))
1399 {
1400 d_printf("Could not resolve %s\n", path);
1401 return -1;
1402 }
1403 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1404
1405
1406 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1407
1408 if (ret <= 0) {
1409
1410 errno = smbc_errno(context, targetcli);
1411 return -1;
1412
1413 }
1414
1415 file->offset += ret;
1416
1417 return ret; /* Success, 0 bytes of data ... */
1418}
1419
1420/*
1421 * Routine to close() a file ...
1422 */
1423
1424static int
1425smbc_close_ctx(SMBCCTX *context,
1426 SMBCFILE *file)
1427{
1428 SMBCSRV *srv;
1429 fstring server, share, user, password;
1430 pstring path, targetpath;
1431 struct cli_state *targetcli;
1432
1433 if (!context || !context->internal ||
1434 !context->internal->_initialized) {
1435
1436 errno = EINVAL;
1437 return -1;
1438
1439 }
1440
1441 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1442
1443 errno = EBADF;
1444 return -1;
1445
1446 }
1447
1448 /* IS a dir ... */
1449 if (!file->file) {
1450
1451 return context->closedir(context, file);
1452
1453 }
1454
1455 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1456 if (smbc_parse_path(context, file->fname,
1457 NULL, 0,
1458 server, sizeof(server),
1459 share, sizeof(share),
1460 path, sizeof(path),
1461 user, sizeof(user),
1462 password, sizeof(password),
1463 NULL, 0)) {
1464 errno = EINVAL;
1465 return -1;
1466 }
1467
1468 /*d_printf(">>>close: resolving %s\n", path);*/
1469 if (!cli_resolve_path("", file->srv->cli, path,
1470 &targetcli, targetpath))
1471 {
1472 d_printf("Could not resolve %s\n", path);
1473 return -1;
1474 }
1475 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1476
1477 if (!cli_close(targetcli, file->cli_fd)) {
1478
1479 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1480 file->fname));
1481 /* Deallocate slot and remove the server
1482 * from the server cache if unused */
1483 errno = smbc_errno(context, targetcli);
1484 srv = file->srv;
1485 DLIST_REMOVE(context->internal->_files, file);
1486 SAFE_FREE(file->fname);
1487 SAFE_FREE(file);
1488 context->callbacks.remove_unused_server_fn(context, srv);
1489
1490 return -1;
1491
1492 }
1493
1494 DLIST_REMOVE(context->internal->_files, file);
1495 SAFE_FREE(file->fname);
1496 SAFE_FREE(file);
1497
1498 return 0;
1499}
1500
1501/*
1502 * Get info from an SMB server on a file. Use a qpathinfo call first
1503 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1504 */
1505static BOOL
1506smbc_getatr(SMBCCTX * context,
1507 SMBCSRV *srv,
1508 char *path,
1509 uint16 *mode,
1510 SMB_OFF_T *size,
1511 struct timespec *create_time_ts,
1512 struct timespec *access_time_ts,
1513 struct timespec *write_time_ts,
1514 struct timespec *change_time_ts,
1515 SMB_INO_T *ino)
1516{
1517 pstring fixedpath;
1518 pstring targetpath;
1519 struct cli_state *targetcli;
1520 time_t write_time;
1521
1522 if (!context || !context->internal ||
1523 !context->internal->_initialized) {
1524
1525 errno = EINVAL;
1526 return -1;
1527
1528 }
1529
1530 /* path fixup for . and .. */
1531 if (strequal(path, ".") || strequal(path, ".."))
1532 pstrcpy(fixedpath, "\\");
1533 else
1534 {
1535 pstrcpy(fixedpath, path);
1536 trim_string(fixedpath, NULL, "\\..");
1537 trim_string(fixedpath, NULL, "\\.");
1538 }
1539 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1540
1541 if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
1542 {
1543 d_printf("Couldn't resolve %s\n", path);
1544 return False;
1545 }
1546
1547 if (!srv->no_pathinfo2 &&
1548 cli_qpathinfo2(targetcli, targetpath,
1549 create_time_ts,
1550 access_time_ts,
1551 write_time_ts,
1552 change_time_ts,
1553 size, mode, ino)) {
1554 return True;
1555 }
1556
1557 /* if this is NT then don't bother with the getatr */
1558 if (targetcli->capabilities & CAP_NT_SMBS) {
1559 errno = EPERM;
1560 return False;
1561 }
1562
1563 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1564
1565 struct timespec w_time_ts;
1566
1567 w_time_ts = convert_time_t_to_timespec(write_time);
1568
1569 if (write_time_ts != NULL) {
1570 *write_time_ts = w_time_ts;
1571 }
1572
1573 if (create_time_ts != NULL) {
1574 *create_time_ts = w_time_ts;
1575 }
1576
1577 if (access_time_ts != NULL) {
1578 *access_time_ts = w_time_ts;
1579 }
1580
1581 if (change_time_ts != NULL) {
1582 *change_time_ts = w_time_ts;
1583 }
1584
1585 srv->no_pathinfo2 = True;
1586 return True;
1587 }
1588
1589 errno = EPERM;
1590 return False;
1591
1592}
1593
1594/*
1595 * Set file info on an SMB server. Use setpathinfo call first. If that
1596 * fails, use setattrE..
1597 *
1598 * Access and modification time parameters are always used and must be
1599 * provided. Create time, if zero, will be determined from the actual create
1600 * time of the file. If non-zero, the create time will be set as well.
1601 *
1602 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1603 */
1604static BOOL
1605smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1606 time_t create_time,
1607 time_t access_time,
1608 time_t write_time,
1609 time_t change_time,
1610 uint16 mode)
1611{
1612 int fd;
1613 int ret;
1614
1615 /*
1616 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1617 * modern function for "new code" to be using, and it works given a
1618 * filename rather than requiring that the file be opened to have its
1619 * attributes manipulated.
1620 */
1621 if (srv->no_pathinfo ||
1622 ! cli_setpathinfo(srv->cli, path,
1623 create_time,
1624 access_time,
1625 write_time,
1626 change_time,
1627 mode)) {
1628
1629 /*
1630 * setpathinfo is not supported; go to plan B.
1631 *
1632 * cli_setatr() does not work on win98, and it also doesn't
1633 * support setting the access time (only the modification
1634 * time), so in all cases, we open the specified file and use
1635 * cli_setattrE() which should work on all OS versions, and
1636 * supports both times.
1637 */
1638
1639 /* Don't try {q,set}pathinfo() again, with this server */
1640 srv->no_pathinfo = True;
1641
1642 /* Open the file */
1643 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1644
1645 errno = smbc_errno(context, srv->cli);
1646 return -1;
1647 }
1648
1649 /* Set the new attributes */
1650 ret = cli_setattrE(srv->cli, fd,
1651 change_time,
1652 access_time,
1653 write_time);
1654
1655 /* Close the file */
1656 cli_close(srv->cli, fd);
1657
1658 /*
1659 * Unfortunately, setattrE() doesn't have a provision for
1660 * setting the access mode (attributes). We'll have to try
1661 * cli_setatr() for that, and with only this parameter, it
1662 * seems to work on win98.
1663 */
1664 if (ret && mode != (uint16) -1) {
1665 ret = cli_setatr(srv->cli, path, mode, 0);
1666 }
1667
1668 if (! ret) {
1669 errno = smbc_errno(context, srv->cli);
1670 return False;
1671 }
1672 }
1673
1674 return True;
1675}
1676
1677 /*
1678 * Routine to unlink() a file
1679 */
1680
1681static int
1682smbc_unlink_ctx(SMBCCTX *context,
1683 const char *fname)
1684{
1685 fstring server, share, user, password, workgroup;
1686 pstring path, targetpath;
1687 struct cli_state *targetcli;
1688 SMBCSRV *srv = NULL;
1689
1690 if (!context || !context->internal ||
1691 !context->internal->_initialized) {
1692
1693 errno = EINVAL; /* Best I can think of ... */
1694 return -1;
1695
1696 }
1697
1698 if (!fname) {
1699
1700 errno = EINVAL;
1701 return -1;
1702
1703 }
1704
1705 if (smbc_parse_path(context, fname,
1706 workgroup, sizeof(workgroup),
1707 server, sizeof(server),
1708 share, sizeof(share),
1709 path, sizeof(path),
1710 user, sizeof(user),
1711 password, sizeof(password),
1712 NULL, 0)) {
1713 errno = EINVAL;
1714 return -1;
1715 }
1716
1717 if (user[0] == (char)0) fstrcpy(user, context->user);
1718
1719 srv = smbc_server(context, True,
1720 server, share, workgroup, user, password);
1721
1722 if (!srv) {
1723
1724 return -1; /* smbc_server sets errno */
1725
1726 }
1727
1728 /*d_printf(">>>unlink: resolving %s\n", path);*/
1729 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1730 {
1731 d_printf("Could not resolve %s\n", path);
1732 return -1;
1733 }
1734 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1735
1736 if (!cli_unlink(targetcli, targetpath)) {
1737
1738 errno = smbc_errno(context, targetcli);
1739
1740 if (errno == EACCES) { /* Check if the file is a directory */
1741
1742 int saverr = errno;
1743 SMB_OFF_T size = 0;
1744 uint16 mode = 0;
1745 struct timespec write_time_ts;
1746 struct timespec access_time_ts;
1747 struct timespec change_time_ts;
1748 SMB_INO_T ino = 0;
1749
1750 if (!smbc_getatr(context, srv, path, &mode, &size,
1751 NULL,
1752 &access_time_ts,
1753 &write_time_ts,
1754 &change_time_ts,
1755 &ino)) {
1756
1757 /* Hmmm, bad error ... What? */
1758
1759 errno = smbc_errno(context, targetcli);
1760 return -1;
1761
1762 }
1763 else {
1764
1765 if (IS_DOS_DIR(mode))
1766 errno = EISDIR;
1767 else
1768 errno = saverr; /* Restore this */
1769
1770 }
1771 }
1772
1773 return -1;
1774
1775 }
1776
1777 return 0; /* Success ... */
1778
1779}
1780
1781/*
1782 * Routine to rename() a file
1783 */
1784
1785static int
1786smbc_rename_ctx(SMBCCTX *ocontext,
1787 const char *oname,
1788 SMBCCTX *ncontext,
1789 const char *nname)
1790{
1791 fstring server1;
1792 fstring share1;
1793 fstring server2;
1794 fstring share2;
1795 fstring user1;
1796 fstring user2;
1797 fstring password1;
1798 fstring password2;
1799 fstring workgroup;
1800 pstring path1;
1801 pstring path2;
1802 pstring targetpath1;
1803 pstring targetpath2;
1804 struct cli_state *targetcli1;
1805 struct cli_state *targetcli2;
1806 SMBCSRV *srv = NULL;
1807
1808 if (!ocontext || !ncontext ||
1809 !ocontext->internal || !ncontext->internal ||
1810 !ocontext->internal->_initialized ||
1811 !ncontext->internal->_initialized) {
1812
1813 errno = EINVAL; /* Best I can think of ... */
1814 return -1;
1815
1816 }
1817
1818 if (!oname || !nname) {
1819
1820 errno = EINVAL;
1821 return -1;
1822
1823 }
1824
1825 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1826
1827 smbc_parse_path(ocontext, oname,
1828 workgroup, sizeof(workgroup),
1829 server1, sizeof(server1),
1830 share1, sizeof(share1),
1831 path1, sizeof(path1),
1832 user1, sizeof(user1),
1833 password1, sizeof(password1),
1834 NULL, 0);
1835
1836 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1837
1838 smbc_parse_path(ncontext, nname,
1839 NULL, 0,
1840 server2, sizeof(server2),
1841 share2, sizeof(share2),
1842 path2, sizeof(path2),
1843 user2, sizeof(user2),
1844 password2, sizeof(password2),
1845 NULL, 0);
1846
1847 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1848
1849 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1850 strcmp(user1, user2)) {
1851
1852 /* Can't rename across file systems, or users?? */
1853
1854 errno = EXDEV;
1855 return -1;
1856
1857 }
1858
1859 srv = smbc_server(ocontext, True,
1860 server1, share1, workgroup, user1, password1);
1861 if (!srv) {
1862
1863 return -1;
1864
1865 }
1866
1867 /*d_printf(">>>rename: resolving %s\n", path1);*/
1868 if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
1869 {
1870 d_printf("Could not resolve %s\n", path1);
1871 return -1;
1872 }
1873 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1874 /*d_printf(">>>rename: resolving %s\n", path2);*/
1875 if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
1876 {
1877 d_printf("Could not resolve %s\n", path2);
1878 return -1;
1879 }
1880 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1881
1882 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1883 strcmp(targetcli1->share, targetcli2->share))
1884 {
1885 /* can't rename across file systems */
1886
1887 errno = EXDEV;
1888 return -1;
1889 }
1890
1891 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1892 int eno = smbc_errno(ocontext, targetcli1);
1893
1894 if (eno != EEXIST ||
1895 !cli_unlink(targetcli1, targetpath2) ||
1896 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1897
1898 errno = eno;
1899 return -1;
1900
1901 }
1902 }
1903
1904 return 0; /* Success */
1905
1906}
1907
1908/*
1909 * A routine to lseek() a file
1910 */
1911
1912static off_t
1913smbc_lseek_ctx(SMBCCTX *context,
1914 SMBCFILE *file,
1915 off_t offset,
1916 int whence)
1917{
1918 SMB_OFF_T size;
1919 fstring server, share, user, password;
1920 pstring path, targetpath;
1921 struct cli_state *targetcli;
1922
1923 if (!context || !context->internal ||
1924 !context->internal->_initialized) {
1925
1926 errno = EINVAL;
1927 return -1;
1928
1929 }
1930
1931 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1932
1933 errno = EBADF;
1934 return -1;
1935
1936 }
1937
1938 if (!file->file) {
1939
1940 errno = EINVAL;
1941 return -1; /* Can't lseek a dir ... */
1942
1943 }
1944
1945 switch (whence) {
1946 case SEEK_SET:
1947 file->offset = offset;
1948 break;
1949
1950 case SEEK_CUR:
1951 file->offset += offset;
1952 break;
1953
1954 case SEEK_END:
1955 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1956 if (smbc_parse_path(context, file->fname,
1957 NULL, 0,
1958 server, sizeof(server),
1959 share, sizeof(share),
1960 path, sizeof(path),
1961 user, sizeof(user),
1962 password, sizeof(password),
1963 NULL, 0)) {
1964
1965 errno = EINVAL;
1966 return -1;
1967 }
1968
1969 /*d_printf(">>>lseek: resolving %s\n", path);*/
1970 if (!cli_resolve_path("", file->srv->cli, path,
1971 &targetcli, targetpath))
1972 {
1973 d_printf("Could not resolve %s\n", path);
1974 return -1;
1975 }
1976 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1977
1978 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
1979 &size, NULL, NULL, NULL, NULL, NULL))
1980 {
1981 SMB_OFF_T b_size = size;
1982 if (!cli_getattrE(targetcli, file->cli_fd,
1983 NULL, &b_size, NULL, NULL, NULL))
1984 {
1985 errno = EINVAL;
1986 return -1;
1987 } else
1988 size = b_size;
1989 }
1990 file->offset = size + offset;
1991 break;
1992
1993 default:
1994 errno = EINVAL;
1995 break;
1996
1997 }
1998
1999 return file->offset;
2000
2001}
2002
2003/*
2004 * Generate an inode number from file name for those things that need it
2005 */
2006
2007static ino_t
2008smbc_inode(SMBCCTX *context,
2009 const char *name)
2010{
2011
2012 if (!context || !context->internal ||
2013 !context->internal->_initialized) {
2014
2015 errno = EINVAL;
2016 return -1;
2017
2018 }
2019
2020 if (!*name) return 2; /* FIXME, why 2 ??? */
2021 return (ino_t)str_checksum(name);
2022
2023}
2024
2025/*
2026 * Routine to put basic stat info into a stat structure ... Used by stat and
2027 * fstat below.
2028 */
2029
2030static int
2031smbc_setup_stat(SMBCCTX *context,
2032 struct stat *st,
2033 char *fname,
2034 SMB_OFF_T size,
2035 int mode)
2036{
2037
2038 st->st_mode = 0;
2039
2040 if (IS_DOS_DIR(mode)) {
2041 st->st_mode = SMBC_DIR_MODE;
2042 } else {
2043 st->st_mode = SMBC_FILE_MODE;
2044 }
2045
2046 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2047 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2048 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2049 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2050
2051 st->st_size = size;
2052#ifdef HAVE_STAT_ST_BLKSIZE
2053 st->st_blksize = 512;
2054#endif
2055#ifdef HAVE_STAT_ST_BLOCKS
2056 st->st_blocks = (size+511)/512;
2057#endif
2058 st->st_uid = getuid();
2059 st->st_gid = getgid();
2060
2061 if (IS_DOS_DIR(mode)) {
2062 st->st_nlink = 2;
2063 } else {
2064 st->st_nlink = 1;
2065 }
2066
2067 if (st->st_ino == 0) {
2068 st->st_ino = smbc_inode(context, fname);
2069 }
2070
2071 return True; /* FIXME: Is this needed ? */
2072
2073}
2074
2075/*
2076 * Routine to stat a file given a name
2077 */
2078
2079static int
2080smbc_stat_ctx(SMBCCTX *context,
2081 const char *fname,
2082 struct stat *st)
2083{
2084 SMBCSRV *srv;
2085 fstring server;
2086 fstring share;
2087 fstring user;
2088 fstring password;
2089 fstring workgroup;
2090 pstring path;
2091 struct timespec write_time_ts;
2092 struct timespec access_time_ts;
2093 struct timespec change_time_ts;
2094 SMB_OFF_T size = 0;
2095 uint16 mode = 0;
2096 SMB_INO_T ino = 0;
2097
2098 if (!context || !context->internal ||
2099 !context->internal->_initialized) {
2100
2101 errno = EINVAL; /* Best I can think of ... */
2102 return -1;
2103
2104 }
2105
2106 if (!fname) {
2107
2108 errno = EINVAL;
2109 return -1;
2110
2111 }
2112
2113 DEBUG(4, ("smbc_stat(%s)\n", fname));
2114
2115 if (smbc_parse_path(context, fname,
2116 workgroup, sizeof(workgroup),
2117 server, sizeof(server),
2118 share, sizeof(share),
2119 path, sizeof(path),
2120 user, sizeof(user),
2121 password, sizeof(password),
2122 NULL, 0)) {
2123 errno = EINVAL;
2124 return -1;
2125 }
2126
2127 if (user[0] == (char)0) fstrcpy(user, context->user);
2128
2129 srv = smbc_server(context, True,
2130 server, share, workgroup, user, password);
2131
2132 if (!srv) {
2133 return -1; /* errno set by smbc_server */
2134 }
2135
2136 if (!smbc_getatr(context, srv, path, &mode, &size,
2137 NULL,
2138 &access_time_ts,
2139 &write_time_ts,
2140 &change_time_ts,
2141 &ino)) {
2142
2143 errno = smbc_errno(context, srv->cli);
2144 return -1;
2145
2146 }
2147
2148 st->st_ino = ino;
2149
2150 smbc_setup_stat(context, st, path, size, mode);
2151
2152 set_atimespec(st, access_time_ts);
2153 set_ctimespec(st, change_time_ts);
2154 set_mtimespec(st, write_time_ts);
2155 st->st_dev = srv->dev;
2156
2157 return 0;
2158
2159}
2160
2161/*
2162 * Routine to stat a file given an fd
2163 */
2164
2165static int
2166smbc_fstat_ctx(SMBCCTX *context,
2167 SMBCFILE *file,
2168 struct stat *st)
2169{
2170 struct timespec change_time_ts;
2171 struct timespec access_time_ts;
2172 struct timespec write_time_ts;
2173 SMB_OFF_T size;
2174 uint16 mode;
2175 fstring server;
2176 fstring share;
2177 fstring user;
2178 fstring password;
2179 pstring path;
2180 pstring targetpath;
2181 struct cli_state *targetcli;
2182 SMB_INO_T ino = 0;
2183
2184 if (!context || !context->internal ||
2185 !context->internal->_initialized) {
2186
2187 errno = EINVAL;
2188 return -1;
2189
2190 }
2191
2192 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2193
2194 errno = EBADF;
2195 return -1;
2196
2197 }
2198
2199 if (!file->file) {
2200
2201 return context->fstatdir(context, file, st);
2202
2203 }
2204
2205 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2206 if (smbc_parse_path(context, file->fname,
2207 NULL, 0,
2208 server, sizeof(server),
2209 share, sizeof(share),
2210 path, sizeof(path),
2211 user, sizeof(user),
2212 password, sizeof(password),
2213 NULL, 0)) {
2214 errno = EINVAL;
2215 return -1;
2216 }
2217
2218 /*d_printf(">>>fstat: resolving %s\n", path);*/
2219 if (!cli_resolve_path("", file->srv->cli, path,
2220 &targetcli, targetpath))
2221 {
2222 d_printf("Could not resolve %s\n", path);
2223 return -1;
2224 }
2225 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2226
2227 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2228 NULL,
2229 &access_time_ts,
2230 &write_time_ts,
2231 &change_time_ts,
2232 &ino)) {
2233
2234 time_t change_time, access_time, write_time;
2235
2236 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2237 &change_time, &access_time, &write_time)) {
2238
2239 errno = EINVAL;
2240 return -1;
2241 }
2242
2243 change_time_ts = convert_time_t_to_timespec(change_time);
2244 access_time_ts = convert_time_t_to_timespec(access_time);
2245 write_time_ts = convert_time_t_to_timespec(write_time);
2246 }
2247
2248 st->st_ino = ino;
2249
2250 smbc_setup_stat(context, st, file->fname, size, mode);
2251
2252 set_atimespec(st, access_time_ts);
2253 set_ctimespec(st, change_time_ts);
2254 set_mtimespec(st, write_time_ts);
2255 st->st_dev = file->srv->dev;
2256
2257 return 0;
2258
2259}
2260
2261/*
2262 * Routine to open a directory
2263 * We accept the URL syntax explained in smbc_parse_path(), above.
2264 */
2265
2266static void
2267smbc_remove_dir(SMBCFILE *dir)
2268{
2269 struct smbc_dir_list *d,*f;
2270
2271 d = dir->dir_list;
2272 while (d) {
2273
2274 f = d; d = d->next;
2275
2276 SAFE_FREE(f->dirent);
2277 SAFE_FREE(f);
2278
2279 }
2280
2281 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2282
2283}
2284
2285static int
2286add_dirent(SMBCFILE *dir,
2287 const char *name,
2288 const char *comment,
2289 uint32 type)
2290{
2291 struct smbc_dirent *dirent;
2292 int size;
2293 int name_length = (name == NULL ? 0 : strlen(name));
2294 int comment_len = (comment == NULL ? 0 : strlen(comment));
2295
2296 /*
2297 * Allocate space for the dirent, which must be increased by the
2298 * size of the name and the comment and 1 each for the null terminator.
2299 */
2300
2301 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2302
2303 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2304
2305 if (!dirent) {
2306
2307 dir->dir_error = ENOMEM;
2308 return -1;
2309
2310 }
2311
2312 ZERO_STRUCTP(dirent);
2313
2314 if (dir->dir_list == NULL) {
2315
2316 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2317 if (!dir->dir_list) {
2318
2319 SAFE_FREE(dirent);
2320 dir->dir_error = ENOMEM;
2321 return -1;
2322
2323 }
2324 ZERO_STRUCTP(dir->dir_list);
2325
2326 dir->dir_end = dir->dir_next = dir->dir_list;
2327 }
2328 else {
2329
2330 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2331
2332 if (!dir->dir_end->next) {
2333
2334 SAFE_FREE(dirent);
2335 dir->dir_error = ENOMEM;
2336 return -1;
2337
2338 }
2339 ZERO_STRUCTP(dir->dir_end->next);
2340
2341 dir->dir_end = dir->dir_end->next;
2342 }
2343
2344 dir->dir_end->next = NULL;
2345 dir->dir_end->dirent = dirent;
2346
2347 dirent->smbc_type = type;
2348 dirent->namelen = name_length;
2349 dirent->commentlen = comment_len;
2350 dirent->dirlen = size;
2351
2352 /*
2353 * dirent->namelen + 1 includes the null (no null termination needed)
2354 * Ditto for dirent->commentlen.
2355 * The space for the two null bytes was allocated.
2356 */
2357 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2358 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2359 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2360
2361 return 0;
2362
2363}
2364
2365static void
2366list_unique_wg_fn(const char *name,
2367 uint32 type,
2368 const char *comment,
2369 void *state)
2370{
2371 SMBCFILE *dir = (SMBCFILE *)state;
2372 struct smbc_dir_list *dir_list;
2373 struct smbc_dirent *dirent;
2374 int dirent_type;
2375 int do_remove = 0;
2376
2377 dirent_type = dir->dir_type;
2378
2379 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2380
2381 /* An error occurred, what do we do? */
2382 /* FIXME: Add some code here */
2383 }
2384
2385 /* Point to the one just added */
2386 dirent = dir->dir_end->dirent;
2387
2388 /* See if this was a duplicate */
2389 for (dir_list = dir->dir_list;
2390 dir_list != dir->dir_end;
2391 dir_list = dir_list->next) {
2392 if (! do_remove &&
2393 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2394 /* Duplicate. End end of list need to be removed. */
2395 do_remove = 1;
2396 }
2397
2398 if (do_remove && dir_list->next == dir->dir_end) {
2399 /* Found the end of the list. Remove it. */
2400 dir->dir_end = dir_list;
2401 free(dir_list->next);
2402 free(dirent);
2403 dir_list->next = NULL;
2404 break;
2405 }
2406 }
2407}
2408
2409static void
2410list_fn(const char *name,
2411 uint32 type,
2412 const char *comment,
2413 void *state)
2414{
2415 SMBCFILE *dir = (SMBCFILE *)state;
2416 int dirent_type;
2417
2418 /*
2419 * We need to process the type a little ...
2420 *
2421 * Disk share = 0x00000000
2422 * Print share = 0x00000001
2423 * Comms share = 0x00000002 (obsolete?)
2424 * IPC$ share = 0x00000003
2425 *
2426 * administrative shares:
2427 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2428 */
2429
2430 if (dir->dir_type == SMBC_FILE_SHARE) {
2431
2432 switch (type) {
2433 case 0 | 0x80000000:
2434 case 0:
2435 dirent_type = SMBC_FILE_SHARE;
2436 break;
2437
2438 case 1:
2439 dirent_type = SMBC_PRINTER_SHARE;
2440 break;
2441
2442 case 2:
2443 dirent_type = SMBC_COMMS_SHARE;
2444 break;
2445
2446 case 3 | 0x80000000:
2447 case 3:
2448 dirent_type = SMBC_IPC_SHARE;
2449 break;
2450
2451 default:
2452 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2453 break;
2454 }
2455 }
2456 else {
2457 dirent_type = dir->dir_type;
2458 }
2459
2460 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2461
2462 /* An error occurred, what do we do? */
2463 /* FIXME: Add some code here */
2464
2465 }
2466}
2467
2468static void
2469dir_list_fn(const char *mnt,
2470 file_info *finfo,
2471 const char *mask,
2472 void *state)
2473{
2474
2475 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2476 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2477
2478 /* Handle an error ... */
2479
2480 /* FIXME: Add some code ... */
2481
2482 }
2483
2484}
2485
2486static int
2487net_share_enum_rpc(struct cli_state *cli,
2488 void (*fn)(const char *name,
2489 uint32 type,
2490 const char *comment,
2491 void *state),
2492 void *state)
2493{
2494 int i;
2495 WERROR result;
2496 ENUM_HND enum_hnd;
2497 uint32 info_level = 1;
2498 uint32 preferred_len = 0xffffffff;
2499 uint32 type;
2500 SRV_SHARE_INFO_CTR ctr;
2501 fstring name = "";
2502 fstring comment = "";
2503 void *mem_ctx;
2504 struct rpc_pipe_client *pipe_hnd;
2505 NTSTATUS nt_status;
2506
2507 /* Open the server service pipe */
2508 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2509 if (!pipe_hnd) {
2510 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2511 return -1;
2512 }
2513
2514 /* Allocate a context for parsing and for the entries in "ctr" */
2515 mem_ctx = talloc_init("libsmbclient: net_share_enum_rpc");
2516 if (mem_ctx == NULL) {
2517 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2518 cli_rpc_pipe_close(pipe_hnd);
2519 return -1;
2520 }
2521
2522 /* Issue the NetShareEnum RPC call and retrieve the response */
2523 init_enum_hnd(&enum_hnd, 0);
2524 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2525 mem_ctx,
2526 info_level,
2527 &ctr,
2528 preferred_len,
2529 &enum_hnd);
2530
2531 /* Was it successful? */
2532 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2533 /* Nope. Go clean up. */
2534 goto done;
2535 }
2536
2537 /* For each returned entry... */
2538 for (i = 0; i < ctr.num_entries; i++) {
2539
2540 /* pull out the share name */
2541 rpcstr_pull_unistr2_fstring(
2542 name, &ctr.share.info1[i].info_1_str.uni_netname);
2543
2544 /* pull out the share's comment */
2545 rpcstr_pull_unistr2_fstring(
2546 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2547
2548 /* Get the type value */
2549 type = ctr.share.info1[i].info_1.type;
2550
2551 /* Add this share to the list */
2552 (*fn)(name, type, comment, state);
2553 }
2554
2555done:
2556 /* Close the server service pipe */
2557 cli_rpc_pipe_close(pipe_hnd);
2558
2559 /* Free all memory which was allocated for this request */
2560 TALLOC_FREE(mem_ctx);
2561
2562 /* Tell 'em if it worked */
2563 return W_ERROR_IS_OK(result) ? 0 : -1;
2564}
2565
2566
2567
2568static SMBCFILE *
2569smbc_opendir_ctx(SMBCCTX *context,
2570 const char *fname)
2571{
2572 int saved_errno;
2573 fstring server, share, user, password, options;
2574 pstring workgroup;
2575 pstring path;
2576 uint16 mode;
2577 char *p;
2578 SMBCSRV *srv = NULL;
2579 SMBCFILE *dir = NULL;
2580 struct _smbc_callbacks *cb;
2581 struct in_addr rem_ip;
2582
2583 if (!context || !context->internal ||
2584 !context->internal->_initialized) {
2585 DEBUG(4, ("no valid context\n"));
2586 errno = EINVAL + 8192;
2587 return NULL;
2588
2589 }
2590
2591 if (!fname) {
2592 DEBUG(4, ("no valid fname\n"));
2593 errno = EINVAL + 8193;
2594 return NULL;
2595 }
2596
2597 if (smbc_parse_path(context, fname,
2598 workgroup, sizeof(workgroup),
2599 server, sizeof(server),
2600 share, sizeof(share),
2601 path, sizeof(path),
2602 user, sizeof(user),
2603 password, sizeof(password),
2604 options, sizeof(options))) {
2605 DEBUG(4, ("no valid path\n"));
2606 errno = EINVAL + 8194;
2607 return NULL;
2608 }
2609
2610 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2611 "path='%s' options='%s'\n",
2612 fname, server, share, path, options));
2613
2614 /* Ensure the options are valid */
2615 if (smbc_check_options(server, share, path, options)) {
2616 DEBUG(4, ("unacceptable options (%s)\n", options));
2617 errno = EINVAL + 8195;
2618 return NULL;
2619 }
2620
2621 if (user[0] == (char)0) fstrcpy(user, context->user);
2622
2623 dir = SMB_MALLOC_P(SMBCFILE);
2624
2625 if (!dir) {
2626
2627 errno = ENOMEM;
2628 return NULL;
2629
2630 }
2631
2632 ZERO_STRUCTP(dir);
2633
2634 dir->cli_fd = 0;
2635 dir->fname = SMB_STRDUP(fname);
2636 dir->srv = NULL;
2637 dir->offset = 0;
2638 dir->file = False;
2639 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2640
2641 if (server[0] == (char)0) {
2642
2643 int i;
2644 int count;
2645 int max_lmb_count;
2646 struct ip_service *ip_list;
2647 struct ip_service server_addr;
2648 struct user_auth_info u_info;
2649 struct cli_state *cli;
2650
2651 if (share[0] != (char)0 || path[0] != (char)0) {
2652
2653 errno = EINVAL + 8196;
2654 if (dir) {
2655 SAFE_FREE(dir->fname);
2656 SAFE_FREE(dir);
2657 }
2658 return NULL;
2659 }
2660
2661 /* Determine how many local master browsers to query */
2662 max_lmb_count = (context->options.browse_max_lmb_count == 0
2663 ? INT_MAX
2664 : context->options.browse_max_lmb_count);
2665
2666 pstrcpy(u_info.username, user);
2667 pstrcpy(u_info.password, password);
2668
2669 /*
2670 * We have server and share and path empty but options
2671 * requesting that we scan all master browsers for their list
2672 * of workgroups/domains. This implies that we must first try
2673 * broadcast queries to find all master browsers, and if that
2674 * doesn't work, then try our other methods which return only
2675 * a single master browser.
2676 */
2677
2678 ip_list = NULL;
2679 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
2680
2681 SAFE_FREE(ip_list);
2682
2683 if (!find_master_ip(workgroup, &server_addr.ip)) {
2684
2685 if (dir) {
2686 SAFE_FREE(dir->fname);
2687 SAFE_FREE(dir);
2688 }
2689 errno = ENOENT;
2690 return NULL;
2691 }
2692
2693 ip_list = &server_addr;
2694 count = 1;
2695 }
2696
2697 for (i = 0; i < count && i < max_lmb_count; i++) {
2698 DEBUG(99, ("Found master browser %d of %d: %s\n",
2699 i+1, MAX(count, max_lmb_count),
2700 inet_ntoa(ip_list[i].ip)));
2701
2702 cli = get_ipc_connect_master_ip(&ip_list[i],
2703 workgroup, &u_info);
2704 /* cli == NULL is the master browser refused to talk or
2705 could not be found */
2706 if ( !cli )
2707 continue;
2708
2709 fstrcpy(server, cli->desthost);
2710 cli_shutdown(cli);
2711
2712 DEBUG(4, ("using workgroup %s %s\n",
2713 workgroup, server));
2714
2715 /*
2716 * For each returned master browser IP address, get a
2717 * connection to IPC$ on the server if we do not
2718 * already have one, and determine the
2719 * workgroups/domains that it knows about.
2720 */
2721
2722 srv = smbc_server(context, True, server, "IPC$",
2723 workgroup, user, password);
2724 if (!srv) {
2725 continue;
2726 }
2727
2728 dir->srv = srv;
2729 dir->dir_type = SMBC_WORKGROUP;
2730
2731 /* Now, list the stuff ... */
2732
2733 if (!cli_NetServerEnum(srv->cli,
2734 workgroup,
2735 SV_TYPE_DOMAIN_ENUM,
2736 list_unique_wg_fn,
2737 (void *)dir)) {
2738 continue;
2739 }
2740 }
2741
2742 SAFE_FREE(ip_list);
2743 } else {
2744 /*
2745 * Server not an empty string ... Check the rest and see what
2746 * gives
2747 */
2748 if (*share == '\0') {
2749 if (*path != '\0') {
2750
2751 /* Should not have empty share with path */
2752 errno = EINVAL + 8197;
2753 if (dir) {
2754 SAFE_FREE(dir->fname);
2755 SAFE_FREE(dir);
2756 }
2757 return NULL;
2758
2759 }
2760
2761 /*
2762 * We don't know if <server> is really a server name
2763 * or is a workgroup/domain name. If we already have
2764 * a server structure for it, we'll use it.
2765 * Otherwise, check to see if <server><1D>,
2766 * <server><1B>, or <server><20> translates. We check
2767 * to see if <server> is an IP address first.
2768 */
2769
2770 /*
2771 * See if we have an existing server. Do not
2772 * establish a connection if one does not already
2773 * exist.
2774 */
2775 srv = smbc_server(context, False, server, "IPC$",
2776 workgroup, user, password);
2777
2778 /*
2779 * If no existing server and not an IP addr, look for
2780 * LMB or DMB
2781 */
2782 if (!srv &&
2783 !is_ipaddress(server) &&
2784 (resolve_name(server, &rem_ip, 0x1d) || /* LMB */
2785 resolve_name(server, &rem_ip, 0x1b) )) { /* DMB */
2786
2787 fstring buserver;
2788
2789 dir->dir_type = SMBC_SERVER;
2790
2791 /*
2792 * Get the backup list ...
2793 */
2794 if (!name_status_find(server, 0, 0,
2795 rem_ip, buserver)) {
2796
2797 DEBUG(0, ("Could not get name of "
2798 "local/domain master browser "
2799 "for server %s\n", server));
2800 if (dir) {
2801 SAFE_FREE(dir->fname);
2802 SAFE_FREE(dir);
2803 }
2804 errno = EPERM;
2805 return NULL;
2806
2807 }
2808
2809 /*
2810 * Get a connection to IPC$ on the server if
2811 * we do not already have one
2812 */
2813 srv = smbc_server(context, True,
2814 buserver, "IPC$",
2815 workgroup, user, password);
2816 if (!srv) {
2817 DEBUG(0, ("got no contact to IPC$\n"));
2818 if (dir) {
2819 SAFE_FREE(dir->fname);
2820 SAFE_FREE(dir);
2821 }
2822 return NULL;
2823
2824 }
2825
2826 dir->srv = srv;
2827
2828 /* Now, list the servers ... */
2829 if (!cli_NetServerEnum(srv->cli, server,
2830 0x0000FFFE, list_fn,
2831 (void *)dir)) {
2832
2833 if (dir) {
2834 SAFE_FREE(dir->fname);
2835 SAFE_FREE(dir);
2836 }
2837 return NULL;
2838 }
2839 } else if (srv ||
2840 (resolve_name(server, &rem_ip, 0x20))) {
2841
2842 /* If we hadn't found the server, get one now */
2843 if (!srv) {
2844 srv = smbc_server(context, True,
2845 server, "IPC$",
2846 workgroup,
2847 user, password);
2848 }
2849
2850 if (!srv) {
2851 if (dir) {
2852 SAFE_FREE(dir->fname);
2853 SAFE_FREE(dir);
2854 }
2855 return NULL;
2856
2857 }
2858
2859 dir->dir_type = SMBC_FILE_SHARE;
2860 dir->srv = srv;
2861
2862 /* List the shares ... */
2863
2864 if (net_share_enum_rpc(
2865 srv->cli,
2866 list_fn,
2867 (void *) dir) < 0 &&
2868 cli_RNetShareEnum(
2869 srv->cli,
2870 list_fn,
2871 (void *)dir) < 0) {
2872
2873 errno = cli_errno(srv->cli);
2874 if (dir) {
2875 SAFE_FREE(dir->fname);
2876 SAFE_FREE(dir);
2877 }
2878 return NULL;
2879
2880 }
2881 } else {
2882 /* Neither the workgroup nor server exists */
2883 errno = ECONNREFUSED;
2884 if (dir) {
2885 SAFE_FREE(dir->fname);
2886 SAFE_FREE(dir);
2887 }
2888 return NULL;
2889 }
2890
2891 }
2892 else {
2893 /*
2894 * The server and share are specified ... work from
2895 * there ...
2896 */
2897 pstring targetpath;
2898 struct cli_state *targetcli;
2899
2900 /* We connect to the server and list the directory */
2901 dir->dir_type = SMBC_FILE_SHARE;
2902
2903 srv = smbc_server(context, True, server, share,
2904 workgroup, user, password);
2905
2906 if (!srv) {
2907
2908 if (dir) {
2909 SAFE_FREE(dir->fname);
2910 SAFE_FREE(dir);
2911 }
2912 return NULL;
2913
2914 }
2915
2916 dir->srv = srv;
2917
2918 /* Now, list the files ... */
2919
2920 p = path + strlen(path);
2921 pstrcat(path, "\\*");
2922
2923 if (!cli_resolve_path("", srv->cli, path,
2924 &targetcli, targetpath))
2925 {
2926 d_printf("Could not resolve %s\n", path);
2927 if (dir) {
2928 SAFE_FREE(dir->fname);
2929 SAFE_FREE(dir);
2930 }
2931 return NULL;
2932 }
2933
2934 if (cli_list(targetcli, targetpath,
2935 aDIR | aSYSTEM | aHIDDEN,
2936 dir_list_fn, (void *)dir) < 0) {
2937
2938 if (dir) {
2939 SAFE_FREE(dir->fname);
2940 SAFE_FREE(dir);
2941 }
2942 saved_errno = smbc_errno(context, targetcli);
2943
2944 if (saved_errno == EINVAL) {
2945 /*
2946 * See if they asked to opendir something
2947 * other than a directory. If so, the
2948 * converted error value we got would have
2949 * been EINVAL rather than ENOTDIR.
2950 */
2951 *p = '\0'; /* restore original path */
2952
2953 if (smbc_getatr(context, srv, path,
2954 &mode, NULL,
2955 NULL, NULL, NULL, NULL,
2956 NULL) &&
2957 ! IS_DOS_DIR(mode)) {
2958
2959 /* It is. Correct the error value */
2960 saved_errno = ENOTDIR;
2961 }
2962 }
2963
2964 /*
2965 * If there was an error and the server is no
2966 * good any more...
2967 */
2968 cb = &context->callbacks;
2969 if (cli_is_error(targetcli) &&
2970 cb->check_server_fn(context, srv)) {
2971
2972 /* ... then remove it. */
2973 if (cb->remove_unused_server_fn(context,
2974 srv)) {
2975 /*
2976 * We could not remove the server
2977 * completely, remove it from the
2978 * cache so we will not get it
2979 * again. It will be removed when the
2980 * last file/dir is closed.
2981 */
2982 cb->remove_cached_srv_fn(context, srv);
2983 }
2984 }
2985
2986 errno = saved_errno;
2987 return NULL;
2988 }
2989 }
2990
2991 }
2992
2993 DLIST_ADD(context->internal->_files, dir);
2994 return dir;
2995
2996}
2997
2998/*
2999 * Routine to close a directory
3000 */
3001
3002static int
3003smbc_closedir_ctx(SMBCCTX *context,
3004 SMBCFILE *dir)
3005{
3006
3007 if (!context || !context->internal ||
3008 !context->internal->_initialized) {
3009
3010 errno = EINVAL;
3011 return -1;
3012
3013 }
3014
3015 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3016
3017 errno = EBADF;
3018 return -1;
3019
3020 }
3021
3022 smbc_remove_dir(dir); /* Clean it up */
3023
3024 DLIST_REMOVE(context->internal->_files, dir);
3025
3026 if (dir) {
3027
3028 SAFE_FREE(dir->fname);
3029 SAFE_FREE(dir); /* Free the space too */
3030 }
3031
3032 return 0;
3033
3034}
3035
3036static void
3037smbc_readdir_internal(SMBCCTX * context,
3038 struct smbc_dirent *dest,
3039 struct smbc_dirent *src,
3040 int max_namebuf_len)
3041{
3042 if (context->options.urlencode_readdir_entries) {
3043
3044 /* url-encode the name. get back remaining buffer space */
3045 max_namebuf_len =
3046 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3047
3048 /* We now know the name length */
3049 dest->namelen = strlen(dest->name);
3050
3051 /* Save the pointer to the beginning of the comment */
3052 dest->comment = dest->name + dest->namelen + 1;
3053
3054 /* Copy the comment */
3055 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3056 dest->comment[max_namebuf_len - 1] = '\0';
3057
3058 /* Save other fields */
3059 dest->smbc_type = src->smbc_type;
3060 dest->commentlen = strlen(dest->comment);
3061 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3062 (char *) dest);
3063 } else {
3064
3065 /* No encoding. Just copy the entry as is. */
3066 memcpy(dest, src, src->dirlen);
3067 dest->comment = (char *)(&dest->name + src->namelen + 1);
3068 }
3069
3070}
3071
3072/*
3073 * Routine to get a directory entry
3074 */
3075
3076struct smbc_dirent *
3077smbc_readdir_ctx(SMBCCTX *context,
3078 SMBCFILE *dir)
3079{
3080 int maxlen;
3081 struct smbc_dirent *dirp, *dirent;
3082
3083 /* Check that all is ok first ... */
3084
3085 if (!context || !context->internal ||
3086 !context->internal->_initialized) {
3087
3088 errno = EINVAL;
3089 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3090 return NULL;
3091
3092 }
3093
3094 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3095
3096 errno = EBADF;
3097 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3098 return NULL;
3099
3100 }
3101
3102 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3103
3104 errno = ENOTDIR;
3105 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3106 return NULL;
3107
3108 }
3109
3110 if (!dir->dir_next) {
3111 return NULL;
3112 }
3113
3114 dirent = dir->dir_next->dirent;
3115 if (!dirent) {
3116
3117 errno = ENOENT;
3118 return NULL;
3119
3120 }
3121
3122 dirp = (struct smbc_dirent *)context->internal->_dirent;
3123 maxlen = (sizeof(context->internal->_dirent) -
3124 sizeof(struct smbc_dirent));
3125
3126 smbc_readdir_internal(context, dirp, dirent, maxlen);
3127
3128 dir->dir_next = dir->dir_next->next;
3129
3130 return dirp;
3131}
3132
3133/*
3134 * Routine to get directory entries
3135 */
3136
3137static int
3138smbc_getdents_ctx(SMBCCTX *context,
3139 SMBCFILE *dir,
3140 struct smbc_dirent *dirp,
3141 int count)
3142{
3143 int rem = count;
3144 int reqd;
3145 int maxlen;
3146 char *ndir = (char *)dirp;
3147 struct smbc_dir_list *dirlist;
3148
3149 /* Check that all is ok first ... */
3150
3151 if (!context || !context->internal ||
3152 !context->internal->_initialized) {
3153
3154 errno = EINVAL;
3155 return -1;
3156
3157 }
3158
3159 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3160
3161 errno = EBADF;
3162 return -1;
3163
3164 }
3165
3166 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3167
3168 errno = ENOTDIR;
3169 return -1;
3170
3171 }
3172
3173 /*
3174 * Now, retrieve the number of entries that will fit in what was passed
3175 * We have to figure out if the info is in the list, or we need to
3176 * send a request to the server to get the info.
3177 */
3178
3179 while ((dirlist = dir->dir_next)) {
3180 struct smbc_dirent *dirent;
3181
3182 if (!dirlist->dirent) {
3183
3184 errno = ENOENT; /* Bad error */
3185 return -1;
3186
3187 }
3188
3189 /* Do urlencoding of next entry, if so selected */
3190 dirent = (struct smbc_dirent *)context->internal->_dirent;
3191 maxlen = (sizeof(context->internal->_dirent) -
3192 sizeof(struct smbc_dirent));
3193 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3194
3195 reqd = dirent->dirlen;
3196
3197 if (rem < reqd) {
3198
3199 if (rem < count) { /* We managed to copy something */
3200
3201 errno = 0;
3202 return count - rem;
3203
3204 }
3205 else { /* Nothing copied ... */
3206
3207 errno = EINVAL; /* Not enough space ... */
3208 return -1;
3209
3210 }
3211
3212 }
3213
3214 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3215
3216 ((struct smbc_dirent *)ndir)->comment =
3217 (char *)(&((struct smbc_dirent *)ndir)->name +
3218 dirent->namelen +
3219 1);
3220
3221 ndir += reqd;
3222
3223 rem -= reqd;
3224
3225 dir->dir_next = dirlist = dirlist -> next;
3226 }
3227
3228 if (rem == count)
3229 return 0;
3230 else
3231 return count - rem;
3232
3233}
3234
3235/*
3236 * Routine to create a directory ...
3237 */
3238
3239static int
3240smbc_mkdir_ctx(SMBCCTX *context,
3241 const char *fname,
3242 mode_t mode)
3243{
3244 SMBCSRV *srv;
3245 fstring server;
3246 fstring share;
3247 fstring user;
3248 fstring password;
3249 fstring workgroup;
3250 pstring path, targetpath;
3251 struct cli_state *targetcli;
3252
3253 if (!context || !context->internal ||
3254 !context->internal->_initialized) {
3255
3256 errno = EINVAL;
3257 return -1;
3258
3259 }
3260
3261 if (!fname) {
3262
3263 errno = EINVAL;
3264 return -1;
3265
3266 }
3267
3268 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3269
3270 if (smbc_parse_path(context, fname,
3271 workgroup, sizeof(workgroup),
3272 server, sizeof(server),
3273 share, sizeof(share),
3274 path, sizeof(path),
3275 user, sizeof(user),
3276 password, sizeof(password),
3277 NULL, 0)) {
3278 errno = EINVAL;
3279 return -1;
3280 }
3281
3282 if (user[0] == (char)0) fstrcpy(user, context->user);
3283
3284 srv = smbc_server(context, True,
3285 server, share, workgroup, user, password);
3286
3287 if (!srv) {
3288
3289 return -1; /* errno set by smbc_server */
3290
3291 }
3292
3293 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3294 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3295 {
3296 d_printf("Could not resolve %s\n", path);
3297 return -1;
3298 }
3299 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3300
3301 if (!cli_mkdir(targetcli, targetpath)) {
3302
3303 errno = smbc_errno(context, targetcli);
3304 return -1;
3305
3306 }
3307
3308 return 0;
3309
3310}
3311
3312/*
3313 * Our list function simply checks to see if a directory is not empty
3314 */
3315
3316static int smbc_rmdir_dirempty = True;
3317
3318static void
3319rmdir_list_fn(const char *mnt,
3320 file_info *finfo,
3321 const char *mask,
3322 void *state)
3323{
3324 if (strncmp(finfo->name, ".", 1) != 0 &&
3325 strncmp(finfo->name, "..", 2) != 0) {
3326
3327 smbc_rmdir_dirempty = False;
3328 }
3329}
3330
3331/*
3332 * Routine to remove a directory
3333 */
3334
3335static int
3336smbc_rmdir_ctx(SMBCCTX *context,
3337 const char *fname)
3338{
3339 SMBCSRV *srv;
3340 fstring server;
3341 fstring share;
3342 fstring user;
3343 fstring password;
3344 fstring workgroup;
3345 pstring path;
3346 pstring targetpath;
3347 struct cli_state *targetcli;
3348
3349 if (!context || !context->internal ||
3350 !context->internal->_initialized) {
3351
3352 errno = EINVAL;
3353 return -1;
3354
3355 }
3356
3357 if (!fname) {
3358
3359 errno = EINVAL;
3360 return -1;
3361
3362 }
3363
3364 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3365
3366 if (smbc_parse_path(context, fname,
3367 workgroup, sizeof(workgroup),
3368 server, sizeof(server),
3369 share, sizeof(share),
3370 path, sizeof(path),
3371 user, sizeof(user),
3372 password, sizeof(password),
3373 NULL, 0))
3374 {
3375 errno = EINVAL;
3376 return -1;
3377 }
3378
3379 if (user[0] == (char)0) fstrcpy(user, context->user);
3380
3381 srv = smbc_server(context, True,
3382 server, share, workgroup, user, password);
3383
3384 if (!srv) {
3385
3386 return -1; /* errno set by smbc_server */
3387
3388 }
3389
3390 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3391 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3392 {
3393 d_printf("Could not resolve %s\n", path);
3394 return -1;
3395 }
3396 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3397
3398
3399 if (!cli_rmdir(targetcli, targetpath)) {
3400
3401 errno = smbc_errno(context, targetcli);
3402
3403 if (errno == EACCES) { /* Check if the dir empty or not */
3404
3405 /* Local storage to avoid buffer overflows */
3406 pstring lpath;
3407
3408 smbc_rmdir_dirempty = True; /* Make this so ... */
3409
3410 pstrcpy(lpath, targetpath);
3411 pstrcat(lpath, "\\*");
3412
3413 if (cli_list(targetcli, lpath,
3414 aDIR | aSYSTEM | aHIDDEN,
3415 rmdir_list_fn, NULL) < 0) {
3416
3417 /* Fix errno to ignore latest error ... */
3418 DEBUG(5, ("smbc_rmdir: "
3419 "cli_list returned an error: %d\n",
3420 smbc_errno(context, targetcli)));
3421 errno = EACCES;
3422
3423 }
3424
3425 if (smbc_rmdir_dirempty)
3426 errno = EACCES;
3427 else
3428 errno = ENOTEMPTY;
3429
3430 }
3431
3432 return -1;
3433
3434 }
3435
3436 return 0;
3437
3438}
3439
3440/*
3441 * Routine to return the current directory position
3442 */
3443
3444static off_t
3445smbc_telldir_ctx(SMBCCTX *context,
3446 SMBCFILE *dir)
3447{
3448 if (!context || !context->internal ||
3449 !context->internal->_initialized) {
3450
3451 errno = EINVAL;
3452 return -1;
3453
3454 }
3455
3456 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3457
3458 errno = EBADF;
3459 return -1;
3460
3461 }
3462
3463 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3464
3465 errno = ENOTDIR;
3466 return -1;
3467
3468 }
3469
3470 /* See if we're already at the end. */
3471 if (dir->dir_next == NULL) {
3472 /* We are. */
3473 return -1;
3474 }
3475
3476 /*
3477 * We return the pointer here as the offset
3478 */
3479 return (off_t)(long)dir->dir_next->dirent;
3480}
3481
3482/*
3483 * A routine to run down the list and see if the entry is OK
3484 */
3485
3486struct smbc_dir_list *
3487smbc_check_dir_ent(struct smbc_dir_list *list,
3488 struct smbc_dirent *dirent)
3489{
3490
3491 /* Run down the list looking for what we want */
3492
3493 if (dirent) {
3494
3495 struct smbc_dir_list *tmp = list;
3496
3497 while (tmp) {
3498
3499 if (tmp->dirent == dirent)
3500 return tmp;
3501
3502 tmp = tmp->next;
3503
3504 }
3505
3506 }
3507
3508 return NULL; /* Not found, or an error */
3509
3510}
3511
3512
3513/*
3514 * Routine to seek on a directory
3515 */
3516
3517static int
3518smbc_lseekdir_ctx(SMBCCTX *context,
3519 SMBCFILE *dir,
3520 off_t offset)
3521{
3522 long int l_offset = offset; /* Handle problems of size */
3523 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3524 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3525
3526 if (!context || !context->internal ||
3527 !context->internal->_initialized) {
3528
3529 errno = EINVAL;
3530 return -1;
3531
3532 }
3533
3534 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3535
3536 errno = ENOTDIR;
3537 return -1;
3538
3539 }
3540
3541 /* Now, check what we were passed and see if it is OK ... */
3542
3543 if (dirent == NULL) { /* Seek to the begining of the list */
3544
3545 dir->dir_next = dir->dir_list;
3546 return 0;
3547
3548 }
3549
3550 if (offset == -1) { /* Seek to the end of the list */
3551 dir->dir_next = NULL;
3552 return 0;
3553 }
3554
3555 /* Now, run down the list and make sure that the entry is OK */
3556 /* This may need to be changed if we change the format of the list */
3557
3558 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3559
3560 errno = EINVAL; /* Bad entry */
3561 return -1;
3562
3563 }
3564
3565 dir->dir_next = list_ent;
3566
3567 return 0;
3568
3569}
3570
3571/*
3572 * Routine to fstat a dir
3573 */
3574
3575static int
3576smbc_fstatdir_ctx(SMBCCTX *context,
3577 SMBCFILE *dir,
3578 struct stat *st)
3579{
3580
3581 if (!context || !context->internal ||
3582 !context->internal->_initialized) {
3583
3584 errno = EINVAL;
3585 return -1;
3586
3587 }
3588
3589 /* No code yet ... */
3590
3591 return 0;
3592
3593}
3594
3595static int
3596smbc_chmod_ctx(SMBCCTX *context,
3597 const char *fname,
3598 mode_t newmode)
3599{
3600 SMBCSRV *srv;
3601 fstring server;
3602 fstring share;
3603 fstring user;
3604 fstring password;
3605 fstring workgroup;
3606 pstring path;
3607 uint16 mode;
3608
3609 if (!context || !context->internal ||
3610 !context->internal->_initialized) {
3611
3612 errno = EINVAL; /* Best I can think of ... */
3613 return -1;
3614
3615 }
3616
3617 if (!fname) {
3618
3619 errno = EINVAL;
3620 return -1;
3621
3622 }
3623
3624 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3625
3626 if (smbc_parse_path(context, fname,
3627 workgroup, sizeof(workgroup),
3628 server, sizeof(server),
3629 share, sizeof(share),
3630 path, sizeof(path),
3631 user, sizeof(user),
3632 password, sizeof(password),
3633 NULL, 0)) {
3634 errno = EINVAL;
3635 return -1;
3636 }
3637
3638 if (user[0] == (char)0) fstrcpy(user, context->user);
3639
3640 srv = smbc_server(context, True,
3641 server, share, workgroup, user, password);
3642
3643 if (!srv) {
3644 return -1; /* errno set by smbc_server */
3645 }
3646
3647 mode = 0;
3648
3649 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3650 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3651 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3652 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3653
3654 if (!cli_setatr(srv->cli, path, mode, 0)) {
3655 errno = smbc_errno(context, srv->cli);
3656 return -1;
3657 }
3658
3659 return 0;
3660}
3661
3662static int
3663smbc_utimes_ctx(SMBCCTX *context,
3664 const char *fname,
3665 struct timeval *tbuf)
3666{
3667 SMBCSRV *srv;
3668 fstring server;
3669 fstring share;
3670 fstring user;
3671 fstring password;
3672 fstring workgroup;
3673 pstring path;
3674 time_t access_time;
3675 time_t write_time;
3676
3677 if (!context || !context->internal ||
3678 !context->internal->_initialized) {
3679
3680 errno = EINVAL; /* Best I can think of ... */
3681 return -1;
3682
3683 }
3684
3685 if (!fname) {
3686
3687 errno = EINVAL;
3688 return -1;
3689
3690 }
3691
3692 if (tbuf == NULL) {
3693 access_time = write_time = time(NULL);
3694 } else {
3695 access_time = tbuf[0].tv_sec;
3696 write_time = tbuf[1].tv_sec;
3697 }
3698
3699 if (DEBUGLVL(4))
3700 {
3701 char *p;
3702 char atimebuf[32];
3703 char mtimebuf[32];
3704
3705 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3706 atimebuf[sizeof(atimebuf) - 1] = '\0';
3707 if ((p = strchr(atimebuf, '\n')) != NULL) {
3708 *p = '\0';
3709 }
3710
3711 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3712 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3713 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3714 *p = '\0';
3715 }
3716
3717 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3718 fname, atimebuf, mtimebuf);
3719 }
3720
3721 if (smbc_parse_path(context, fname,
3722 workgroup, sizeof(workgroup),
3723 server, sizeof(server),
3724 share, sizeof(share),
3725 path, sizeof(path),
3726 user, sizeof(user),
3727 password, sizeof(password),
3728 NULL, 0)) {
3729 errno = EINVAL;
3730 return -1;
3731 }
3732
3733 if (user[0] == (char)0) fstrcpy(user, context->user);
3734
3735 srv = smbc_server(context, True,
3736 server, share, workgroup, user, password);
3737
3738 if (!srv) {
3739 return -1; /* errno set by smbc_server */
3740 }
3741
3742 if (!smbc_setatr(context, srv, path,
3743 0, access_time, write_time, 0, 0)) {
3744 return -1; /* errno set by smbc_setatr */
3745 }
3746
3747 return 0;
3748}
3749
3750
3751/*
3752 * Sort ACEs according to the documentation at
3753 * http://support.microsoft.com/kb/269175, at least as far as it defines the
3754 * order.
3755 */
3756
3757static int
3758ace_compare(SEC_ACE *ace1,
3759 SEC_ACE *ace2)
3760{
3761 BOOL b1;
3762 BOOL b2;
3763
3764 /* If the ACEs are equal, we have nothing more to do. */
3765 if (sec_ace_equal(ace1, ace2)) {
3766 return 0;
3767 }
3768
3769 /* Inherited follow non-inherited */
3770 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
3771 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
3772 if (b1 != b2) {
3773 return (b1 ? 1 : -1);
3774 }
3775
3776 /*
3777 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
3778 * sort them after DENY and ALLOW.
3779 */
3780 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
3781 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
3782 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
3783 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3784 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
3785 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
3786 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
3787 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3788 if (b1 != b2) {
3789 return (b1 ? 1 : -1);
3790 }
3791
3792 /* Allowed ACEs follow denied ACEs */
3793 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
3794 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
3795 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
3796 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
3797 if (b1 != b2) {
3798 return (b1 ? 1 : -1);
3799 }
3800
3801 /*
3802 * ACEs applying to an entity's object follow those applying to the
3803 * entity itself
3804 */
3805 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
3806 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3807 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
3808 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3809 if (b1 != b2) {
3810 return (b1 ? 1 : -1);
3811 }
3812
3813 /*
3814 * If we get this far, the ACEs are similar as far as the
3815 * characteristics we typically care about (those defined by the
3816 * referenced MS document). We'll now sort by characteristics that
3817 * just seems reasonable.
3818 */
3819
3820 if (ace1->type != ace2->type) {
3821 return ace2->type - ace1->type;
3822 }
3823
3824 if (sid_compare(&ace1->trustee, &ace2->trustee)) {
3825 return sid_compare(&ace1->trustee, &ace2->trustee);
3826 }
3827
3828 if (ace1->flags != ace2->flags) {
3829 return ace1->flags - ace2->flags;
3830 }
3831
3832 if (ace1->access_mask != ace2->access_mask) {
3833 return ace1->access_mask - ace2->access_mask;
3834 }
3835
3836 if (ace1->size != ace2->size) {
3837 return ace1->size - ace2->size;
3838 }
3839
3840 return memcmp(ace1, ace2, sizeof(SEC_ACE));
3841}
3842
3843
3844static void
3845sort_acl(SEC_ACL *the_acl)
3846{
3847 uint32 i;
3848 if (!the_acl) return;
3849
3850 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
3851 QSORT_CAST ace_compare);
3852
3853 for (i=1;i<the_acl->num_aces;) {
3854 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
3855 int j;
3856 for (j=i; j<the_acl->num_aces-1; j++) {
3857 the_acl->aces[j] = the_acl->aces[j+1];
3858 }
3859 the_acl->num_aces--;
3860 } else {
3861 i++;
3862 }
3863 }
3864}
3865
3866/* convert a SID to a string, either numeric or username/group */
3867static void
3868convert_sid_to_string(struct cli_state *ipc_cli,
3869 POLICY_HND *pol,
3870 fstring str,
3871 BOOL numeric,
3872 DOM_SID *sid)
3873{
3874 char **domains = NULL;
3875 char **names = NULL;
3876 enum lsa_SidType *types = NULL;
3877 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3878 sid_to_string(str, sid);
3879
3880 if (numeric) {
3881 return; /* no lookup desired */
3882 }
3883
3884 if (!pipe_hnd) {
3885 return;
3886 }
3887
3888 /* Ask LSA to convert the sid to a name */
3889
3890 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ipc_cli->mem_ctx,
3891 pol, 1, sid, &domains,
3892 &names, &types)) ||
3893 !domains || !domains[0] || !names || !names[0]) {
3894 return;
3895 }
3896
3897 /* Converted OK */
3898
3899 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3900 domains[0], lp_winbind_separator(),
3901 names[0]);
3902}
3903
3904/* convert a string to a SID, either numeric or username/group */
3905static BOOL
3906convert_string_to_sid(struct cli_state *ipc_cli,
3907 POLICY_HND *pol,
3908 BOOL numeric,
3909 DOM_SID *sid,
3910 const char *str)
3911{
3912 enum lsa_SidType *types = NULL;
3913 DOM_SID *sids = NULL;
3914 BOOL result = True;
3915 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3916
3917 if (!pipe_hnd) {
3918 return False;
3919 }
3920
3921 if (numeric) {
3922 if (strncmp(str, "S-", 2) == 0) {
3923 return string_to_sid(sid, str);
3924 }
3925
3926 result = False;
3927 goto done;
3928 }
3929
3930 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx,
3931 pol, 1, &str, NULL, &sids,
3932 &types))) {
3933 result = False;
3934 goto done;
3935 }
3936
3937 sid_copy(sid, &sids[0]);
3938 done:
3939
3940 return result;
3941}
3942
3943
3944/* parse an ACE in the same format as print_ace() */
3945static BOOL
3946parse_ace(struct cli_state *ipc_cli,
3947 POLICY_HND *pol,
3948 SEC_ACE *ace,
3949 BOOL numeric,
3950 char *str)
3951{
3952 char *p;
3953 const char *cp;
3954 fstring tok;
3955 unsigned int atype;
3956 unsigned int aflags;
3957 unsigned int amask;
3958 DOM_SID sid;
3959 SEC_ACCESS mask;
3960 const struct perm_value *v;
3961 struct perm_value {
3962 const char *perm;
3963 uint32 mask;
3964 };
3965
3966 /* These values discovered by inspection */
3967 static const struct perm_value special_values[] = {
3968 { "R", 0x00120089 },
3969 { "W", 0x00120116 },
3970 { "X", 0x001200a0 },
3971 { "D", 0x00010000 },
3972 { "P", 0x00040000 },
3973 { "O", 0x00080000 },
3974 { NULL, 0 },
3975 };
3976
3977 static const struct perm_value standard_values[] = {
3978 { "READ", 0x001200a9 },
3979 { "CHANGE", 0x001301bf },
3980 { "FULL", 0x001f01ff },
3981 { NULL, 0 },
3982 };
3983
3984
3985 ZERO_STRUCTP(ace);
3986 p = strchr_m(str,':');
3987 if (!p) return False;
3988 *p = '\0';
3989 p++;
3990 /* Try to parse numeric form */
3991
3992 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3993 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3994 goto done;
3995 }
3996
3997 /* Try to parse text form */
3998
3999 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
4000 return False;
4001 }
4002
4003 cp = p;
4004 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
4005 return False;
4006 }
4007
4008 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
4009 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
4010 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
4011 atype = SEC_ACE_TYPE_ACCESS_DENIED;
4012 } else {
4013 return False;
4014 }
4015
4016 /* Only numeric form accepted for flags at present */
4017
4018 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
4019 sscanf(tok, "%i", &aflags))) {
4020 return False;
4021 }
4022
4023 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
4024 return False;
4025 }
4026
4027 if (strncmp(tok, "0x", 2) == 0) {
4028 if (sscanf(tok, "%i", &amask) != 1) {
4029 return False;
4030 }
4031 goto done;
4032 }
4033
4034 for (v = standard_values; v->perm; v++) {
4035 if (strcmp(tok, v->perm) == 0) {
4036 amask = v->mask;
4037 goto done;
4038 }
4039 }
4040
4041 p = tok;
4042
4043 while(*p) {
4044 BOOL found = False;
4045
4046 for (v = special_values; v->perm; v++) {
4047 if (v->perm[0] == *p) {
4048 amask |= v->mask;
4049 found = True;
4050 }
4051 }
4052
4053 if (!found) return False;
4054 p++;
4055 }
4056
4057 if (*p) {
4058 return False;
4059 }
4060
4061 done:
4062 mask = amask;
4063 init_sec_ace(ace, &sid, atype, mask, aflags);
4064 return True;
4065}
4066
4067/* add an ACE to a list of ACEs in a SEC_ACL */
4068static BOOL
4069add_ace(SEC_ACL **the_acl,
4070 SEC_ACE *ace,
4071 TALLOC_CTX *ctx)
4072{
4073 SEC_ACL *newacl;
4074 SEC_ACE *aces;
4075
4076 if (! *the_acl) {
4077 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
4078 return True;
4079 }
4080
4081 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4082 return False;
4083 }
4084 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4085 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4086 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4087 1+(*the_acl)->num_aces, aces);
4088 SAFE_FREE(aces);
4089 (*the_acl) = newacl;
4090 return True;
4091}
4092
4093
4094/* parse a ascii version of a security descriptor */
4095static SEC_DESC *
4096sec_desc_parse(TALLOC_CTX *ctx,
4097 struct cli_state *ipc_cli,
4098 POLICY_HND *pol,
4099 BOOL numeric,
4100 char *str)
4101{
4102 const char *p = str;
4103 fstring tok;
4104 SEC_DESC *ret = NULL;
4105 size_t sd_size;
4106 DOM_SID *group_sid=NULL;
4107 DOM_SID *owner_sid=NULL;
4108 SEC_ACL *dacl=NULL;
4109 int revision=1;
4110
4111 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4112
4113 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4114 revision = strtol(tok+9, NULL, 16);
4115 continue;
4116 }
4117
4118 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4119 if (owner_sid) {
4120 DEBUG(5, ("OWNER specified more than once!\n"));
4121 goto done;
4122 }
4123 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4124 if (!owner_sid ||
4125 !convert_string_to_sid(ipc_cli, pol,
4126 numeric,
4127 owner_sid, tok+6)) {
4128 DEBUG(5, ("Failed to parse owner sid\n"));
4129 goto done;
4130 }
4131 continue;
4132 }
4133
4134 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4135 if (owner_sid) {
4136 DEBUG(5, ("OWNER specified more than once!\n"));
4137 goto done;
4138 }
4139 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4140 if (!owner_sid ||
4141 !convert_string_to_sid(ipc_cli, pol,
4142 False,
4143 owner_sid, tok+7)) {
4144 DEBUG(5, ("Failed to parse owner sid\n"));
4145 goto done;
4146 }
4147 continue;
4148 }
4149
4150 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4151 if (group_sid) {
4152 DEBUG(5, ("GROUP specified more than once!\n"));
4153 goto done;
4154 }
4155 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4156 if (!group_sid ||
4157 !convert_string_to_sid(ipc_cli, pol,
4158 numeric,
4159 group_sid, tok+6)) {
4160 DEBUG(5, ("Failed to parse group sid\n"));
4161 goto done;
4162 }
4163 continue;
4164 }
4165
4166 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4167 if (group_sid) {
4168 DEBUG(5, ("GROUP specified more than once!\n"));
4169 goto done;
4170 }
4171 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4172 if (!group_sid ||
4173 !convert_string_to_sid(ipc_cli, pol,
4174 False,
4175 group_sid, tok+6)) {
4176 DEBUG(5, ("Failed to parse group sid\n"));
4177 goto done;
4178 }
4179 continue;
4180 }
4181
4182 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4183 SEC_ACE ace;
4184 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4185 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4186 goto done;
4187 }
4188 if(!add_ace(&dacl, &ace, ctx)) {
4189 DEBUG(5, ("Failed to add ACL %s\n", tok));
4190 goto done;
4191 }
4192 continue;
4193 }
4194
4195 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4196 SEC_ACE ace;
4197 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4198 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4199 goto done;
4200 }
4201 if(!add_ace(&dacl, &ace, ctx)) {
4202 DEBUG(5, ("Failed to add ACL %s\n", tok));
4203 goto done;
4204 }
4205 continue;
4206 }
4207
4208 DEBUG(5, ("Failed to parse security descriptor\n"));
4209 goto done;
4210 }
4211
4212 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4213 owner_sid, group_sid, NULL, dacl, &sd_size);
4214
4215 done:
4216 SAFE_FREE(group_sid);
4217 SAFE_FREE(owner_sid);
4218
4219 return ret;
4220}
4221
4222
4223/* Obtain the current dos attributes */
4224static DOS_ATTR_DESC *
4225dos_attr_query(SMBCCTX *context,
4226 TALLOC_CTX *ctx,
4227 const char *filename,
4228 SMBCSRV *srv)
4229{
4230 struct timespec create_time_ts;
4231 struct timespec write_time_ts;
4232 struct timespec access_time_ts;
4233 struct timespec change_time_ts;
4234 SMB_OFF_T size = 0;
4235 uint16 mode = 0;
4236 SMB_INO_T inode = 0;
4237 DOS_ATTR_DESC *ret;
4238
4239 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4240 if (!ret) {
4241 errno = ENOMEM;
4242 return NULL;
4243 }
4244
4245 /* Obtain the DOS attributes */
4246 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4247 &mode, &size,
4248 &create_time_ts,
4249 &access_time_ts,
4250 &write_time_ts,
4251 &change_time_ts,
4252 &inode)) {
4253
4254 errno = smbc_errno(context, srv->cli);
4255 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4256 return NULL;
4257
4258 }
4259
4260 ret->mode = mode;
4261 ret->size = size;
4262 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4263 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4264 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4265 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4266 ret->inode = inode;
4267
4268 return ret;
4269}
4270
4271
4272/* parse a ascii version of a security descriptor */
4273static void
4274dos_attr_parse(SMBCCTX *context,
4275 DOS_ATTR_DESC *dad,
4276 SMBCSRV *srv,
4277 char *str)
4278{
4279 int n;
4280 const char *p = str;
4281 fstring tok;
4282 struct {
4283 const char * create_time_attr;
4284 const char * access_time_attr;
4285 const char * write_time_attr;
4286 const char * change_time_attr;
4287 } attr_strings;
4288
4289 /* Determine whether to use old-style or new-style attribute names */
4290 if (context->internal->_full_time_names) {
4291 /* new-style names */
4292 attr_strings.create_time_attr = "CREATE_TIME";
4293 attr_strings.access_time_attr = "ACCESS_TIME";
4294 attr_strings.write_time_attr = "WRITE_TIME";
4295 attr_strings.change_time_attr = "CHANGE_TIME";
4296 } else {
4297 /* old-style names */
4298 attr_strings.create_time_attr = NULL;
4299 attr_strings.access_time_attr = "A_TIME";
4300 attr_strings.write_time_attr = "M_TIME";
4301 attr_strings.change_time_attr = "C_TIME";
4302 }
4303
4304 /* if this is to set the entire ACL... */
4305 if (*str == '*') {
4306 /* ... then increment past the first colon if there is one */
4307 if ((p = strchr(str, ':')) != NULL) {
4308 ++p;
4309 } else {
4310 p = str;
4311 }
4312 }
4313
4314 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4315
4316 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4317 dad->mode = strtol(tok+5, NULL, 16);
4318 continue;
4319 }
4320
4321 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4322 dad->size = (SMB_OFF_T)atof(tok+5);
4323 continue;
4324 }
4325
4326 n = strlen(attr_strings.access_time_attr);
4327 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4328 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4329 continue;
4330 }
4331
4332 n = strlen(attr_strings.change_time_attr);
4333 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4334 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4335 continue;
4336 }
4337
4338 n = strlen(attr_strings.write_time_attr);
4339 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4340 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4341 continue;
4342 }
4343
4344 if (attr_strings.create_time_attr != NULL) {
4345 n = strlen(attr_strings.create_time_attr);
4346 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4347 n) == 0) {
4348 dad->create_time = (time_t)strtol(tok+n+1,
4349 NULL, 10);
4350 continue;
4351 }
4352 }
4353
4354 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4355 dad->inode = (SMB_INO_T)atof(tok+6);
4356 continue;
4357 }
4358 }
4359}
4360
4361/*****************************************************
4362 Retrieve the acls for a file.
4363*******************************************************/
4364
4365static int
4366cacl_get(SMBCCTX *context,
4367 TALLOC_CTX *ctx,
4368 SMBCSRV *srv,
4369 struct cli_state *ipc_cli,
4370 POLICY_HND *pol,
4371 char *filename,
4372 char *attr_name,
4373 char *buf,
4374 int bufsize)
4375{
4376 uint32 i;
4377 int n = 0;
4378 int n_used;
4379 BOOL all;
4380 BOOL all_nt;
4381 BOOL all_nt_acls;
4382 BOOL all_dos;
4383 BOOL some_nt;
4384 BOOL some_dos;
4385 BOOL exclude_nt_revision = False;
4386 BOOL exclude_nt_owner = False;
4387 BOOL exclude_nt_group = False;
4388 BOOL exclude_nt_acl = False;
4389 BOOL exclude_dos_mode = False;
4390 BOOL exclude_dos_size = False;
4391 BOOL exclude_dos_create_time = False;
4392 BOOL exclude_dos_access_time = False;
4393 BOOL exclude_dos_write_time = False;
4394 BOOL exclude_dos_change_time = False;
4395 BOOL exclude_dos_inode = False;
4396 BOOL numeric = True;
4397 BOOL determine_size = (bufsize == 0);
4398 int fnum = -1;
4399 SEC_DESC *sd;
4400 fstring sidstr;
4401 fstring name_sandbox;
4402 char *name;
4403 char *pExclude;
4404 char *p;
4405 struct timespec create_time_ts;
4406 struct timespec write_time_ts;
4407 struct timespec access_time_ts;
4408 struct timespec change_time_ts;
4409 time_t create_time = (time_t)0;
4410 time_t write_time = (time_t)0;
4411 time_t access_time = (time_t)0;
4412 time_t change_time = (time_t)0;
4413 SMB_OFF_T size = 0;
4414 uint16 mode = 0;
4415 SMB_INO_T ino = 0;
4416 struct cli_state *cli = srv->cli;
4417 struct {
4418 const char * create_time_attr;
4419 const char * access_time_attr;
4420 const char * write_time_attr;
4421 const char * change_time_attr;
4422 } attr_strings;
4423 struct {
4424 const char * create_time_attr;
4425 const char * access_time_attr;
4426 const char * write_time_attr;
4427 const char * change_time_attr;
4428 } excl_attr_strings;
4429
4430 /* Determine whether to use old-style or new-style attribute names */
4431 if (context->internal->_full_time_names) {
4432 /* new-style names */
4433 attr_strings.create_time_attr = "CREATE_TIME";
4434 attr_strings.access_time_attr = "ACCESS_TIME";
4435 attr_strings.write_time_attr = "WRITE_TIME";
4436 attr_strings.change_time_attr = "CHANGE_TIME";
4437
4438 excl_attr_strings.create_time_attr = "CREATE_TIME";
4439 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4440 excl_attr_strings.write_time_attr = "WRITE_TIME";
4441 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4442 } else {
4443 /* old-style names */
4444 attr_strings.create_time_attr = NULL;
4445 attr_strings.access_time_attr = "A_TIME";
4446 attr_strings.write_time_attr = "M_TIME";
4447 attr_strings.change_time_attr = "C_TIME";
4448
4449 excl_attr_strings.create_time_attr = NULL;
4450 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4451 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4452 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4453 }
4454
4455 /* Copy name so we can strip off exclusions (if any are specified) */
4456 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4457
4458 /* Ensure name is null terminated */
4459 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4460
4461 /* Play in the sandbox */
4462 name = name_sandbox;
4463
4464 /* If there are any exclusions, point to them and mask them from name */
4465 if ((pExclude = strchr(name, '!')) != NULL)
4466 {
4467 *pExclude++ = '\0';
4468 }
4469
4470 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4471 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4472 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4473 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4474 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4475 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4476 numeric = (* (name + strlen(name) - 1) != '+');
4477
4478 /* Look for exclusions from "all" requests */
4479 if (all || all_nt || all_dos) {
4480
4481 /* Exclusions are delimited by '!' */
4482 for (;
4483 pExclude != NULL;
4484 pExclude = (p == NULL ? NULL : p + 1)) {
4485
4486 /* Find end of this exclusion name */
4487 if ((p = strchr(pExclude, '!')) != NULL)
4488 {
4489 *p = '\0';
4490 }
4491
4492 /* Which exclusion name is this? */
4493 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4494 exclude_nt_revision = True;
4495 }
4496 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4497 exclude_nt_owner = True;
4498 }
4499 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4500 exclude_nt_group = True;
4501 }
4502 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4503 exclude_nt_acl = True;
4504 }
4505 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4506 exclude_dos_mode = True;
4507 }
4508 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4509 exclude_dos_size = True;
4510 }
4511 else if (excl_attr_strings.create_time_attr != NULL &&
4512 StrCaseCmp(pExclude,
4513 excl_attr_strings.change_time_attr) == 0) {
4514 exclude_dos_create_time = True;
4515 }
4516 else if (StrCaseCmp(pExclude,
4517 excl_attr_strings.access_time_attr) == 0) {
4518 exclude_dos_access_time = True;
4519 }
4520 else if (StrCaseCmp(pExclude,
4521 excl_attr_strings.write_time_attr) == 0) {
4522 exclude_dos_write_time = True;
4523 }
4524 else if (StrCaseCmp(pExclude,
4525 excl_attr_strings.change_time_attr) == 0) {
4526 exclude_dos_change_time = True;
4527 }
4528 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4529 exclude_dos_inode = True;
4530 }
4531 else {
4532 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4533 pExclude));
4534 errno = ENOATTR;
4535 return -1;
4536 }
4537 }
4538 }
4539
4540 n_used = 0;
4541
4542 /*
4543 * If we are (possibly) talking to an NT or new system and some NT
4544 * attributes have been requested...
4545 */
4546 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4547 /* Point to the portion after "system.nt_sec_desc." */
4548 name += 19; /* if (all) this will be invalid but unused */
4549
4550 /* ... then obtain any NT attributes which were requested */
4551 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4552
4553 if (fnum == -1) {
4554 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4555 filename, cli_errstr(cli)));
4556 errno = 0;
4557 return -1;
4558 }
4559
4560 sd = cli_query_secdesc(cli, fnum, ctx);
4561
4562 if (!sd) {
4563 DEBUG(5,
4564 ("cacl_get Failed to query old descriptor\n"));
4565 errno = 0;
4566 return -1;
4567 }
4568
4569 cli_close(cli, fnum);
4570
4571 if (! exclude_nt_revision) {
4572 if (all || all_nt) {
4573 if (determine_size) {
4574 p = talloc_asprintf(ctx,
4575 "REVISION:%d",
4576 sd->revision);
4577 if (!p) {
4578 errno = ENOMEM;
4579 return -1;
4580 }
4581 n = strlen(p);
4582 } else {
4583 n = snprintf(buf, bufsize,
4584 "REVISION:%d",
4585 sd->revision);
4586 }
4587 } else if (StrCaseCmp(name, "revision") == 0) {
4588 if (determine_size) {
4589 p = talloc_asprintf(ctx, "%d",
4590 sd->revision);
4591 if (!p) {
4592 errno = ENOMEM;
4593 return -1;
4594 }
4595 n = strlen(p);
4596 } else {
4597 n = snprintf(buf, bufsize, "%d",
4598 sd->revision);
4599 }
4600 }
4601
4602 if (!determine_size && n > bufsize) {
4603 errno = ERANGE;
4604 return -1;
4605 }
4606 buf += n;
4607 n_used += n;
4608 bufsize -= n;
4609 n = 0;
4610 }
4611
4612 if (! exclude_nt_owner) {
4613 /* Get owner and group sid */
4614 if (sd->owner_sid) {
4615 convert_sid_to_string(ipc_cli, pol,
4616 sidstr,
4617 numeric,
4618 sd->owner_sid);
4619 } else {
4620 fstrcpy(sidstr, "");
4621 }
4622
4623 if (all || all_nt) {
4624 if (determine_size) {
4625 p = talloc_asprintf(ctx, ",OWNER:%s",
4626 sidstr);
4627 if (!p) {
4628 errno = ENOMEM;
4629 return -1;
4630 }
4631 n = strlen(p);
4632 } else if (sidstr[0] != '\0') {
4633 n = snprintf(buf, bufsize,
4634 ",OWNER:%s", sidstr);
4635 }
4636 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4637 if (determine_size) {
4638 p = talloc_asprintf(ctx, "%s", sidstr);
4639 if (!p) {
4640 errno = ENOMEM;
4641 return -1;
4642 }
4643 n = strlen(p);
4644 } else {
4645 n = snprintf(buf, bufsize, "%s",
4646 sidstr);
4647 }
4648 }
4649
4650 if (!determine_size && n > bufsize) {
4651 errno = ERANGE;
4652 return -1;
4653 }
4654 buf += n;
4655 n_used += n;
4656 bufsize -= n;
4657 n = 0;
4658 }
4659
4660 if (! exclude_nt_group) {
4661 if (sd->group_sid) {
4662 convert_sid_to_string(ipc_cli, pol,
4663 sidstr, numeric,
4664 sd->group_sid);
4665 } else {
4666 fstrcpy(sidstr, "");
4667 }
4668
4669 if (all || all_nt) {
4670 if (determine_size) {
4671 p = talloc_asprintf(ctx, ",GROUP:%s",
4672 sidstr);
4673 if (!p) {
4674 errno = ENOMEM;
4675 return -1;
4676 }
4677 n = strlen(p);
4678 } else if (sidstr[0] != '\0') {
4679 n = snprintf(buf, bufsize,
4680 ",GROUP:%s", sidstr);
4681 }
4682 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4683 if (determine_size) {
4684 p = talloc_asprintf(ctx, "%s", sidstr);
4685 if (!p) {
4686 errno = ENOMEM;
4687 return -1;
4688 }
4689 n = strlen(p);
4690 } else {
4691 n = snprintf(buf, bufsize,
4692 "%s", sidstr);
4693 }
4694 }
4695
4696 if (!determine_size && n > bufsize) {
4697 errno = ERANGE;
4698 return -1;
4699 }
4700 buf += n;
4701 n_used += n;
4702 bufsize -= n;
4703 n = 0;
4704 }
4705
4706 if (! exclude_nt_acl) {
4707 /* Add aces to value buffer */
4708 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4709
4710 SEC_ACE *ace = &sd->dacl->aces[i];
4711 convert_sid_to_string(ipc_cli, pol,
4712 sidstr, numeric,
4713 &ace->trustee);
4714
4715 if (all || all_nt) {
4716 if (determine_size) {
4717 p = talloc_asprintf(
4718 ctx,
4719 ",ACL:"
4720 "%s:%d/%d/0x%08x",
4721 sidstr,
4722 ace->type,
4723 ace->flags,
4724 ace->access_mask);
4725 if (!p) {
4726 errno = ENOMEM;
4727 return -1;
4728 }
4729 n = strlen(p);
4730 } else {
4731 n = snprintf(
4732 buf, bufsize,
4733 ",ACL:%s:%d/%d/0x%08x",
4734 sidstr,
4735 ace->type,
4736 ace->flags,
4737 ace->access_mask);
4738 }
4739 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4740 StrCaseCmp(name+3, sidstr) == 0) ||
4741 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4742 StrCaseCmp(name+4, sidstr) == 0)) {
4743 if (determine_size) {
4744 p = talloc_asprintf(
4745 ctx,
4746 "%d/%d/0x%08x",
4747 ace->type,
4748 ace->flags,
4749 ace->access_mask);
4750 if (!p) {
4751 errno = ENOMEM;
4752 return -1;
4753 }
4754 n = strlen(p);
4755 } else {
4756 n = snprintf(buf, bufsize,
4757 "%d/%d/0x%08x",
4758 ace->type,
4759 ace->flags,
4760 ace->access_mask);
4761 }
4762 } else if (all_nt_acls) {
4763 if (determine_size) {
4764 p = talloc_asprintf(
4765 ctx,
4766 "%s%s:%d/%d/0x%08x",
4767 i ? "," : "",
4768 sidstr,
4769 ace->type,
4770 ace->flags,
4771 ace->access_mask);
4772 if (!p) {
4773 errno = ENOMEM;
4774 return -1;
4775 }
4776 n = strlen(p);
4777 } else {
4778 n = snprintf(buf, bufsize,
4779 "%s%s:%d/%d/0x%08x",
4780 i ? "," : "",
4781 sidstr,
4782 ace->type,
4783 ace->flags,
4784 ace->access_mask);
4785 }
4786 }
4787 if (!determine_size && n > bufsize) {
4788 errno = ERANGE;
4789 return -1;
4790 }
4791 buf += n;
4792 n_used += n;
4793 bufsize -= n;
4794 n = 0;
4795 }
4796 }
4797
4798 /* Restore name pointer to its original value */
4799 name -= 19;
4800 }
4801
4802 if (all || some_dos) {
4803 /* Point to the portion after "system.dos_attr." */
4804 name += 16; /* if (all) this will be invalid but unused */
4805
4806 /* Obtain the DOS attributes */
4807 if (!smbc_getatr(context, srv, filename, &mode, &size,
4808 &create_time_ts,
4809 &access_time_ts,
4810 &write_time_ts,
4811 &change_time_ts,
4812 &ino)) {
4813
4814 errno = smbc_errno(context, srv->cli);
4815 return -1;
4816
4817 }
4818
4819 create_time = convert_timespec_to_time_t(create_time_ts);
4820 access_time = convert_timespec_to_time_t(access_time_ts);
4821 write_time = convert_timespec_to_time_t(write_time_ts);
4822 change_time = convert_timespec_to_time_t(change_time_ts);
4823
4824 if (! exclude_dos_mode) {
4825 if (all || all_dos) {
4826 if (determine_size) {
4827 p = talloc_asprintf(ctx,
4828 "%sMODE:0x%x",
4829 (ipc_cli &&
4830 (all || some_nt)
4831 ? ","
4832 : ""),
4833 mode);
4834 if (!p) {
4835 errno = ENOMEM;
4836 return -1;
4837 }
4838 n = strlen(p);
4839 } else {
4840 n = snprintf(buf, bufsize,
4841 "%sMODE:0x%x",
4842 (ipc_cli &&
4843 (all || some_nt)
4844 ? ","
4845 : ""),
4846 mode);
4847 }
4848 } else if (StrCaseCmp(name, "mode") == 0) {
4849 if (determine_size) {
4850 p = talloc_asprintf(ctx, "0x%x", mode);
4851 if (!p) {
4852 errno = ENOMEM;
4853 return -1;
4854 }
4855 n = strlen(p);
4856 } else {
4857 n = snprintf(buf, bufsize,
4858 "0x%x", mode);
4859 }
4860 }
4861
4862 if (!determine_size && n > bufsize) {
4863 errno = ERANGE;
4864 return -1;
4865 }
4866 buf += n;
4867 n_used += n;
4868 bufsize -= n;
4869 n = 0;
4870 }
4871
4872 if (! exclude_dos_size) {
4873 if (all || all_dos) {
4874 if (determine_size) {
4875 p = talloc_asprintf(
4876 ctx,
4877 ",SIZE:%.0f",
4878 (double)size);
4879 if (!p) {
4880 errno = ENOMEM;
4881 return -1;
4882 }
4883 n = strlen(p);
4884 } else {
4885 n = snprintf(buf, bufsize,
4886 ",SIZE:%.0f",
4887 (double)size);
4888 }
4889 } else if (StrCaseCmp(name, "size") == 0) {
4890 if (determine_size) {
4891 p = talloc_asprintf(
4892 ctx,
4893 "%.0f",
4894 (double)size);
4895 if (!p) {
4896 errno = ENOMEM;
4897 return -1;
4898 }
4899 n = strlen(p);
4900 } else {
4901 n = snprintf(buf, bufsize,
4902 "%.0f",
4903 (double)size);
4904 }
4905 }
4906
4907 if (!determine_size && n > bufsize) {
4908 errno = ERANGE;
4909 return -1;
4910 }
4911 buf += n;
4912 n_used += n;
4913 bufsize -= n;
4914 n = 0;
4915 }
4916
4917 if (! exclude_dos_create_time &&
4918 attr_strings.create_time_attr != NULL) {
4919 if (all || all_dos) {
4920 if (determine_size) {
4921 p = talloc_asprintf(ctx,
4922 ",%s:%lu",
4923 attr_strings.create_time_attr,
4924 create_time);
4925 if (!p) {
4926 errno = ENOMEM;
4927 return -1;
4928 }
4929 n = strlen(p);
4930 } else {
4931 n = snprintf(buf, bufsize,
4932 ",%s:%lu",
4933 attr_strings.create_time_attr,
4934 create_time);
4935 }
4936 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
4937 if (determine_size) {
4938 p = talloc_asprintf(ctx, "%lu", create_time);
4939 if (!p) {
4940 errno = ENOMEM;
4941 return -1;
4942 }
4943 n = strlen(p);
4944 } else {
4945 n = snprintf(buf, bufsize,
4946 "%lu", create_time);
4947 }
4948 }
4949
4950 if (!determine_size && n > bufsize) {
4951 errno = ERANGE;
4952 return -1;
4953 }
4954 buf += n;
4955 n_used += n;
4956 bufsize -= n;
4957 n = 0;
4958 }
4959
4960 if (! exclude_dos_access_time) {
4961 if (all || all_dos) {
4962 if (determine_size) {
4963 p = talloc_asprintf(ctx,
4964 ",%s:%lu",
4965 attr_strings.access_time_attr,
4966 access_time);
4967 if (!p) {
4968 errno = ENOMEM;
4969 return -1;
4970 }
4971 n = strlen(p);
4972 } else {
4973 n = snprintf(buf, bufsize,
4974 ",%s:%lu",
4975 attr_strings.access_time_attr,
4976 access_time);
4977 }
4978 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
4979 if (determine_size) {
4980 p = talloc_asprintf(ctx, "%lu", access_time);
4981 if (!p) {
4982 errno = ENOMEM;
4983 return -1;
4984 }
4985 n = strlen(p);
4986 } else {
4987 n = snprintf(buf, bufsize,
4988 "%lu", access_time);
4989 }
4990 }
4991
4992 if (!determine_size && n > bufsize) {
4993 errno = ERANGE;
4994 return -1;
4995 }
4996 buf += n;
4997 n_used += n;
4998 bufsize -= n;
4999 n = 0;
5000 }
5001
5002 if (! exclude_dos_write_time) {
5003 if (all || all_dos) {
5004 if (determine_size) {
5005 p = talloc_asprintf(ctx,
5006 ",%s:%lu",
5007 attr_strings.write_time_attr,
5008 write_time);
5009 if (!p) {
5010 errno = ENOMEM;
5011 return -1;
5012 }
5013 n = strlen(p);
5014 } else {
5015 n = snprintf(buf, bufsize,
5016 ",%s:%lu",
5017 attr_strings.write_time_attr,
5018 write_time);
5019 }
5020 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
5021 if (determine_size) {
5022 p = talloc_asprintf(ctx, "%lu", write_time);
5023 if (!p) {
5024 errno = ENOMEM;
5025 return -1;
5026 }
5027 n = strlen(p);
5028 } else {
5029 n = snprintf(buf, bufsize,
5030 "%lu", write_time);
5031 }
5032 }
5033
5034 if (!determine_size && n > bufsize) {
5035 errno = ERANGE;
5036 return -1;
5037 }
5038 buf += n;
5039 n_used += n;
5040 bufsize -= n;
5041 n = 0;
5042 }
5043
5044 if (! exclude_dos_change_time) {
5045 if (all || all_dos) {
5046 if (determine_size) {
5047 p = talloc_asprintf(ctx,
5048 ",%s:%lu",
5049 attr_strings.change_time_attr,
5050 change_time);
5051 if (!p) {
5052 errno = ENOMEM;
5053 return -1;
5054 }
5055 n = strlen(p);
5056 } else {
5057 n = snprintf(buf, bufsize,
5058 ",%s:%lu",
5059 attr_strings.change_time_attr,
5060 change_time);
5061 }
5062 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5063 if (determine_size) {
5064 p = talloc_asprintf(ctx, "%lu", change_time);
5065 if (!p) {
5066 errno = ENOMEM;
5067 return -1;
5068 }
5069 n = strlen(p);
5070 } else {
5071 n = snprintf(buf, bufsize,
5072 "%lu", change_time);
5073 }
5074 }
5075
5076 if (!determine_size && n > bufsize) {
5077 errno = ERANGE;
5078 return -1;
5079 }
5080 buf += n;
5081 n_used += n;
5082 bufsize -= n;
5083 n = 0;
5084 }
5085
5086 if (! exclude_dos_inode) {
5087 if (all || all_dos) {
5088 if (determine_size) {
5089 p = talloc_asprintf(
5090 ctx,
5091 ",INODE:%.0f",
5092 (double)ino);
5093 if (!p) {
5094 errno = ENOMEM;
5095 return -1;
5096 }
5097 n = strlen(p);
5098 } else {
5099 n = snprintf(buf, bufsize,
5100 ",INODE:%.0f",
5101 (double) ino);
5102 }
5103 } else if (StrCaseCmp(name, "inode") == 0) {
5104 if (determine_size) {
5105 p = talloc_asprintf(
5106 ctx,
5107 "%.0f",
5108 (double) ino);
5109 if (!p) {
5110 errno = ENOMEM;
5111 return -1;
5112 }
5113 n = strlen(p);
5114 } else {
5115 n = snprintf(buf, bufsize,
5116 "%.0f",
5117 (double) ino);
5118 }
5119 }
5120
5121 if (!determine_size && n > bufsize) {
5122 errno = ERANGE;
5123 return -1;
5124 }
5125 buf += n;
5126 n_used += n;
5127 bufsize -= n;
5128 n = 0;
5129 }
5130
5131 /* Restore name pointer to its original value */
5132 name -= 16;
5133 }
5134
5135 if (n_used == 0) {
5136 errno = ENOATTR;
5137 return -1;
5138 }
5139
5140 return n_used;
5141}
5142
5143
5144/*****************************************************
5145set the ACLs on a file given an ascii description
5146*******************************************************/
5147static int
5148cacl_set(TALLOC_CTX *ctx,
5149 struct cli_state *cli,
5150 struct cli_state *ipc_cli,
5151 POLICY_HND *pol,
5152 const char *filename,
5153 const char *the_acl,
5154 int mode,
5155 int flags)
5156{
5157 int fnum;
5158 int err = 0;
5159 SEC_DESC *sd = NULL, *old;
5160 SEC_ACL *dacl = NULL;
5161 DOM_SID *owner_sid = NULL;
5162 DOM_SID *group_sid = NULL;
5163 uint32 i, j;
5164 size_t sd_size;
5165 int ret = 0;
5166 char *p;
5167 BOOL numeric = True;
5168
5169 /* the_acl will be null for REMOVE_ALL operations */
5170 if (the_acl) {
5171 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5172 p > the_acl &&
5173 p[-1] != '+');
5174
5175 /* if this is to set the entire ACL... */
5176 if (*the_acl == '*') {
5177 /* ... then increment past the first colon */
5178 the_acl = p + 1;
5179 }
5180
5181 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5182 CONST_DISCARD(char *, the_acl));
5183
5184 if (!sd) {
5185 errno = EINVAL;
5186 return -1;
5187 }
5188 }
5189
5190 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5191 that doesn't deref sd */
5192
5193 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5194 errno = EINVAL;
5195 return -1;
5196 }
5197
5198 /* The desired access below is the only one I could find that works
5199 with NT4, W2KP and Samba */
5200
5201 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5202
5203 if (fnum == -1) {
5204 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5205 filename, cli_errstr(cli)));
5206 errno = 0;
5207 return -1;
5208 }
5209
5210 old = cli_query_secdesc(cli, fnum, ctx);
5211
5212 if (!old) {
5213 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5214 errno = 0;
5215 return -1;
5216 }
5217
5218 cli_close(cli, fnum);
5219
5220 switch (mode) {
5221 case SMBC_XATTR_MODE_REMOVE_ALL:
5222 old->dacl->num_aces = 0;
5223 dacl = old->dacl;
5224 break;
5225
5226 case SMBC_XATTR_MODE_REMOVE:
5227 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5228 BOOL found = False;
5229
5230 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5231 if (sec_ace_equal(&sd->dacl->aces[i],
5232 &old->dacl->aces[j])) {
5233 uint32 k;
5234 for (k=j; k<old->dacl->num_aces-1;k++) {
5235 old->dacl->aces[k] =
5236 old->dacl->aces[k+1];
5237 }
5238 old->dacl->num_aces--;
5239 found = True;
5240 dacl = old->dacl;
5241 break;
5242 }
5243 }
5244
5245 if (!found) {
5246 err = ENOATTR;
5247 ret = -1;
5248 goto failed;
5249 }
5250 }
5251 break;
5252
5253 case SMBC_XATTR_MODE_ADD:
5254 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5255 BOOL found = False;
5256
5257 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5258 if (sid_equal(&sd->dacl->aces[i].trustee,
5259 &old->dacl->aces[j].trustee)) {
5260 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5261 err = EEXIST;
5262 ret = -1;
5263 goto failed;
5264 }
5265 old->dacl->aces[j] = sd->dacl->aces[i];
5266 ret = -1;
5267 found = True;
5268 }
5269 }
5270
5271 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5272 err = ENOATTR;
5273 ret = -1;
5274 goto failed;
5275 }
5276
5277 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5278 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5279 }
5280 }
5281 dacl = old->dacl;
5282 break;
5283
5284 case SMBC_XATTR_MODE_SET:
5285 old = sd;
5286 owner_sid = old->owner_sid;
5287 group_sid = old->group_sid;
5288 dacl = old->dacl;
5289 break;
5290
5291 case SMBC_XATTR_MODE_CHOWN:
5292 owner_sid = sd->owner_sid;
5293 break;
5294
5295 case SMBC_XATTR_MODE_CHGRP:
5296 group_sid = sd->group_sid;
5297 break;
5298 }
5299
5300 /* Denied ACE entries must come before allowed ones */
5301 sort_acl(old->dacl);
5302
5303 /* Create new security descriptor and set it */
5304 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5305 owner_sid, group_sid, NULL, dacl, &sd_size);
5306
5307 fnum = cli_nt_create(cli, filename,
5308 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5309
5310 if (fnum == -1) {
5311 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5312 filename, cli_errstr(cli)));
5313 errno = 0;
5314 return -1;
5315 }
5316
5317 if (!cli_set_secdesc(cli, fnum, sd)) {
5318 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5319 ret = -1;
5320 }
5321
5322 /* Clean up */
5323
5324 failed:
5325 cli_close(cli, fnum);
5326
5327 if (err != 0) {
5328 errno = err;
5329 }
5330
5331 return ret;
5332}
5333
5334
5335static int
5336smbc_setxattr_ctx(SMBCCTX *context,
5337 const char *fname,
5338 const char *name,
5339 const void *value,
5340 size_t size,
5341 int flags)
5342{
5343 int ret;
5344 int ret2;
5345 SMBCSRV *srv;
5346 SMBCSRV *ipc_srv;
5347 fstring server;
5348 fstring share;
5349 fstring user;
5350 fstring password;
5351 fstring workgroup;
5352 pstring path;
5353 TALLOC_CTX *ctx;
5354 POLICY_HND pol;
5355 DOS_ATTR_DESC *dad;
5356 struct {
5357 const char * create_time_attr;
5358 const char * access_time_attr;
5359 const char * write_time_attr;
5360 const char * change_time_attr;
5361 } attr_strings;
5362
5363 if (!context || !context->internal ||
5364 !context->internal->_initialized) {
5365
5366 errno = EINVAL; /* Best I can think of ... */
5367 return -1;
5368
5369 }
5370
5371 if (!fname) {
5372
5373 errno = EINVAL;
5374 return -1;
5375
5376 }
5377
5378 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5379 fname, name, (int) size, (const char*)value));
5380
5381 if (smbc_parse_path(context, fname,
5382 workgroup, sizeof(workgroup),
5383 server, sizeof(server),
5384 share, sizeof(share),
5385 path, sizeof(path),
5386 user, sizeof(user),
5387 password, sizeof(password),
5388 NULL, 0)) {
5389 errno = EINVAL;
5390 return -1;
5391 }
5392
5393 if (user[0] == (char)0) fstrcpy(user, context->user);
5394
5395 srv = smbc_server(context, True,
5396 server, share, workgroup, user, password);
5397 if (!srv) {
5398 return -1; /* errno set by smbc_server */
5399 }
5400
5401 if (! srv->no_nt_session) {
5402 ipc_srv = smbc_attr_server(context, server, share,
5403 workgroup, user, password,
5404 &pol);
5405 if (! ipc_srv) {
5406 srv->no_nt_session = True;
5407 }
5408 } else {
5409 ipc_srv = NULL;
5410 }
5411
5412 ctx = talloc_init("smbc_setxattr");
5413 if (!ctx) {
5414 errno = ENOMEM;
5415 return -1;
5416 }
5417
5418 /*
5419 * Are they asking to set the entire set of known attributes?
5420 */
5421 if (StrCaseCmp(name, "system.*") == 0 ||
5422 StrCaseCmp(name, "system.*+") == 0) {
5423 /* Yup. */
5424 char *namevalue =
5425 talloc_asprintf(ctx, "%s:%s",
5426 name+7, (const char *) value);
5427 if (! namevalue) {
5428 errno = ENOMEM;
5429 ret = -1;
5430 return -1;
5431 }
5432
5433 if (ipc_srv) {
5434 ret = cacl_set(ctx, srv->cli,
5435 ipc_srv->cli, &pol, path,
5436 namevalue,
5437 (*namevalue == '*'
5438 ? SMBC_XATTR_MODE_SET
5439 : SMBC_XATTR_MODE_ADD),
5440 flags);
5441 } else {
5442 ret = 0;
5443 }
5444
5445 /* get a DOS Attribute Descriptor with current attributes */
5446 dad = dos_attr_query(context, ctx, path, srv);
5447 if (dad) {
5448 /* Overwrite old with new, using what was provided */
5449 dos_attr_parse(context, dad, srv, namevalue);
5450
5451 /* Set the new DOS attributes */
5452 if (! smbc_setatr(context, srv, path,
5453 dad->create_time,
5454 dad->access_time,
5455 dad->write_time,
5456 dad->change_time,
5457 dad->mode)) {
5458
5459 /* cause failure if NT failed too */
5460 dad = NULL;
5461 }
5462 }
5463
5464 /* we only fail if both NT and DOS sets failed */
5465 if (ret < 0 && ! dad) {
5466 ret = -1; /* in case dad was null */
5467 }
5468 else {
5469 ret = 0;
5470 }
5471
5472 talloc_destroy(ctx);
5473 return ret;
5474 }
5475
5476 /*
5477 * Are they asking to set an access control element or to set
5478 * the entire access control list?
5479 */
5480 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5481 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5482 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5483 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5484 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5485
5486 /* Yup. */
5487 char *namevalue =
5488 talloc_asprintf(ctx, "%s:%s",
5489 name+19, (const char *) value);
5490
5491 if (! ipc_srv) {
5492 ret = -1; /* errno set by smbc_server() */
5493 }
5494 else if (! namevalue) {
5495 errno = ENOMEM;
5496 ret = -1;
5497 } else {
5498 ret = cacl_set(ctx, srv->cli,
5499 ipc_srv->cli, &pol, path,
5500 namevalue,
5501 (*namevalue == '*'
5502 ? SMBC_XATTR_MODE_SET
5503 : SMBC_XATTR_MODE_ADD),
5504 flags);
5505 }
5506 talloc_destroy(ctx);
5507 return ret;
5508 }
5509
5510 /*
5511 * Are they asking to set the owner?
5512 */
5513 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5514 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5515
5516 /* Yup. */
5517 char *namevalue =
5518 talloc_asprintf(ctx, "%s:%s",
5519 name+19, (const char *) value);
5520
5521 if (! ipc_srv) {
5522
5523 ret = -1; /* errno set by smbc_server() */
5524 }
5525 else if (! namevalue) {
5526 errno = ENOMEM;
5527 ret = -1;
5528 } else {
5529 ret = cacl_set(ctx, srv->cli,
5530 ipc_srv->cli, &pol, path,
5531 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5532 }
5533 talloc_destroy(ctx);
5534 return ret;
5535 }
5536
5537 /*
5538 * Are they asking to set the group?
5539 */
5540 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5541 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5542
5543 /* Yup. */
5544 char *namevalue =
5545 talloc_asprintf(ctx, "%s:%s",
5546 name+19, (const char *) value);
5547
5548 if (! ipc_srv) {
5549 /* errno set by smbc_server() */
5550 ret = -1;
5551 }
5552 else if (! namevalue) {
5553 errno = ENOMEM;
5554 ret = -1;
5555 } else {
5556 ret = cacl_set(ctx, srv->cli,
5557 ipc_srv->cli, &pol, path,
5558 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5559 }
5560 talloc_destroy(ctx);
5561 return ret;
5562 }
5563
5564 /* Determine whether to use old-style or new-style attribute names */
5565 if (context->internal->_full_time_names) {
5566 /* new-style names */
5567 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5568 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5569 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5570 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5571 } else {
5572 /* old-style names */
5573 attr_strings.create_time_attr = NULL;
5574 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5575 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5576 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5577 }
5578
5579 /*
5580 * Are they asking to set a DOS attribute?
5581 */
5582 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5583 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5584 (attr_strings.create_time_attr != NULL &&
5585 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5586 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5587 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5588 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5589
5590 /* get a DOS Attribute Descriptor with current attributes */
5591 dad = dos_attr_query(context, ctx, path, srv);
5592 if (dad) {
5593 char *namevalue =
5594 talloc_asprintf(ctx, "%s:%s",
5595 name+16, (const char *) value);
5596 if (! namevalue) {
5597 errno = ENOMEM;
5598 ret = -1;
5599 } else {
5600 /* Overwrite old with provided new params */
5601 dos_attr_parse(context, dad, srv, namevalue);
5602
5603 /* Set the new DOS attributes */
5604 ret2 = smbc_setatr(context, srv, path,
5605 dad->create_time,
5606 dad->access_time,
5607 dad->write_time,
5608 dad->change_time,
5609 dad->mode);
5610
5611 /* ret2 has True (success) / False (failure) */
5612 if (ret2) {
5613 ret = 0;
5614 } else {
5615 ret = -1;
5616 }
5617 }
5618 } else {
5619 ret = -1;
5620 }
5621
5622 talloc_destroy(ctx);
5623 return ret;
5624 }
5625
5626 /* Unsupported attribute name */
5627 talloc_destroy(ctx);
5628 errno = EINVAL;
5629 return -1;
5630}
5631
5632static int
5633smbc_getxattr_ctx(SMBCCTX *context,
5634 const char *fname,
5635 const char *name,
5636 const void *value,
5637 size_t size)
5638{
5639 int ret;
5640 SMBCSRV *srv;
5641 SMBCSRV *ipc_srv;
5642 fstring server;
5643 fstring share;
5644 fstring user;
5645 fstring password;
5646 fstring workgroup;
5647 pstring path;
5648 TALLOC_CTX *ctx;
5649 POLICY_HND pol;
5650 struct {
5651 const char * create_time_attr;
5652 const char * access_time_attr;
5653 const char * write_time_attr;
5654 const char * change_time_attr;
5655 } attr_strings;
5656
5657
5658 if (!context || !context->internal ||
5659 !context->internal->_initialized) {
5660
5661 errno = EINVAL; /* Best I can think of ... */
5662 return -1;
5663
5664 }
5665
5666 if (!fname) {
5667
5668 errno = EINVAL;
5669 return -1;
5670
5671 }
5672
5673 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5674
5675 if (smbc_parse_path(context, fname,
5676 workgroup, sizeof(workgroup),
5677 server, sizeof(server),
5678 share, sizeof(share),
5679 path, sizeof(path),
5680 user, sizeof(user),
5681 password, sizeof(password),
5682 NULL, 0)) {
5683 errno = EINVAL;
5684 return -1;
5685 }
5686
5687 if (user[0] == (char)0) fstrcpy(user, context->user);
5688
5689 srv = smbc_server(context, True,
5690 server, share, workgroup, user, password);
5691 if (!srv) {
5692 return -1; /* errno set by smbc_server */
5693 }
5694
5695 if (! srv->no_nt_session) {
5696 ipc_srv = smbc_attr_server(context, server, share,
5697 workgroup, user, password,
5698 &pol);
5699 if (! ipc_srv) {
5700 srv->no_nt_session = True;
5701 }
5702 } else {
5703 ipc_srv = NULL;
5704 }
5705
5706 ctx = talloc_init("smbc:getxattr");
5707 if (!ctx) {
5708 errno = ENOMEM;
5709 return -1;
5710 }
5711
5712 /* Determine whether to use old-style or new-style attribute names */
5713 if (context->internal->_full_time_names) {
5714 /* new-style names */
5715 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5716 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5717 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5718 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5719 } else {
5720 /* old-style names */
5721 attr_strings.create_time_attr = NULL;
5722 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5723 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5724 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5725 }
5726
5727 /* Are they requesting a supported attribute? */
5728 if (StrCaseCmp(name, "system.*") == 0 ||
5729 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5730 StrCaseCmp(name, "system.*+") == 0 ||
5731 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5732 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5733 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5734 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5735 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5736 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5737 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5738 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5739 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5740 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5741 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5742 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5743 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5744 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5745 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5746 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5747 (attr_strings.create_time_attr != NULL &&
5748 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5749 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5750 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5751 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5752 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
5753
5754 /* Yup. */
5755 ret = cacl_get(context, ctx, srv,
5756 ipc_srv == NULL ? NULL : ipc_srv->cli,
5757 &pol, path,
5758 CONST_DISCARD(char *, name),
5759 CONST_DISCARD(char *, value), size);
5760 if (ret < 0 && errno == 0) {
5761 errno = smbc_errno(context, srv->cli);
5762 }
5763 talloc_destroy(ctx);
5764 return ret;
5765 }
5766
5767 /* Unsupported attribute name */
5768 talloc_destroy(ctx);
5769 errno = EINVAL;
5770 return -1;
5771}
5772
5773
5774static int
5775smbc_removexattr_ctx(SMBCCTX *context,
5776 const char *fname,
5777 const char *name)
5778{
5779 int ret;
5780 SMBCSRV *srv;
5781 SMBCSRV *ipc_srv;
5782 fstring server;
5783 fstring share;
5784 fstring user;
5785 fstring password;
5786 fstring workgroup;
5787 pstring path;
5788 TALLOC_CTX *ctx;
5789 POLICY_HND pol;
5790
5791 if (!context || !context->internal ||
5792 !context->internal->_initialized) {
5793
5794 errno = EINVAL; /* Best I can think of ... */
5795 return -1;
5796
5797 }
5798
5799 if (!fname) {
5800
5801 errno = EINVAL;
5802 return -1;
5803
5804 }
5805
5806 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
5807
5808 if (smbc_parse_path(context, fname,
5809 workgroup, sizeof(workgroup),
5810 server, sizeof(server),
5811 share, sizeof(share),
5812 path, sizeof(path),
5813 user, sizeof(user),
5814 password, sizeof(password),
5815 NULL, 0)) {
5816 errno = EINVAL;
5817 return -1;
5818 }
5819
5820 if (user[0] == (char)0) fstrcpy(user, context->user);
5821
5822 srv = smbc_server(context, True,
5823 server, share, workgroup, user, password);
5824 if (!srv) {
5825 return -1; /* errno set by smbc_server */
5826 }
5827
5828 if (! srv->no_nt_session) {
5829 ipc_srv = smbc_attr_server(context, server, share,
5830 workgroup, user, password,
5831 &pol);
5832 if (! ipc_srv) {
5833 srv->no_nt_session = True;
5834 }
5835 } else {
5836 ipc_srv = NULL;
5837 }
5838
5839 if (! ipc_srv) {
5840 return -1; /* errno set by smbc_attr_server */
5841 }
5842
5843 ctx = talloc_init("smbc_removexattr");
5844 if (!ctx) {
5845 errno = ENOMEM;
5846 return -1;
5847 }
5848
5849 /* Are they asking to set the entire ACL? */
5850 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5851 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
5852
5853 /* Yup. */
5854 ret = cacl_set(ctx, srv->cli,
5855 ipc_srv->cli, &pol, path,
5856 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
5857 talloc_destroy(ctx);
5858 return ret;
5859 }
5860
5861 /*
5862 * Are they asking to remove one or more spceific security descriptor
5863 * attributes?
5864 */
5865 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5866 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5867 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5868 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5869 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5870 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5871 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5872
5873 /* Yup. */
5874 ret = cacl_set(ctx, srv->cli,
5875 ipc_srv->cli, &pol, path,
5876 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
5877 talloc_destroy(ctx);
5878 return ret;
5879 }
5880
5881 /* Unsupported attribute name */
5882 talloc_destroy(ctx);
5883 errno = EINVAL;
5884 return -1;
5885}
5886
5887static int
5888smbc_listxattr_ctx(SMBCCTX *context,
5889 const char *fname,
5890 char *list,
5891 size_t size)
5892{
5893 /*
5894 * This isn't quite what listxattr() is supposed to do. This returns
5895 * the complete set of attribute names, always, rather than only those
5896 * attribute names which actually exist for a file. Hmmm...
5897 */
5898 const char supported_old[] =
5899 "system.*\0"
5900 "system.*+\0"
5901 "system.nt_sec_desc.revision\0"
5902 "system.nt_sec_desc.owner\0"
5903 "system.nt_sec_desc.owner+\0"
5904 "system.nt_sec_desc.group\0"
5905 "system.nt_sec_desc.group+\0"
5906 "system.nt_sec_desc.acl.*\0"
5907 "system.nt_sec_desc.acl\0"
5908 "system.nt_sec_desc.acl+\0"
5909 "system.nt_sec_desc.*\0"
5910 "system.nt_sec_desc.*+\0"
5911 "system.dos_attr.*\0"
5912 "system.dos_attr.mode\0"
5913 "system.dos_attr.c_time\0"
5914 "system.dos_attr.a_time\0"
5915 "system.dos_attr.m_time\0"
5916 ;
5917 const char supported_new[] =
5918 "system.*\0"
5919 "system.*+\0"
5920 "system.nt_sec_desc.revision\0"
5921 "system.nt_sec_desc.owner\0"
5922 "system.nt_sec_desc.owner+\0"
5923 "system.nt_sec_desc.group\0"
5924 "system.nt_sec_desc.group+\0"
5925 "system.nt_sec_desc.acl.*\0"
5926 "system.nt_sec_desc.acl\0"
5927 "system.nt_sec_desc.acl+\0"
5928 "system.nt_sec_desc.*\0"
5929 "system.nt_sec_desc.*+\0"
5930 "system.dos_attr.*\0"
5931 "system.dos_attr.mode\0"
5932 "system.dos_attr.create_time\0"
5933 "system.dos_attr.access_time\0"
5934 "system.dos_attr.write_time\0"
5935 "system.dos_attr.change_time\0"
5936 ;
5937 const char * supported;
5938
5939 if (context->internal->_full_time_names) {
5940 supported = supported_new;
5941 } else {
5942 supported = supported_old;
5943 }
5944
5945 if (size == 0) {
5946 return sizeof(supported);
5947 }
5948
5949 if (sizeof(supported) > size) {
5950 errno = ERANGE;
5951 return -1;
5952 }
5953
5954 /* this can't be strcpy() because there are embedded null characters */
5955 memcpy(list, supported, sizeof(supported));
5956 return sizeof(supported);
5957}
5958
5959
5960/*
5961 * Open a print file to be written to by other calls
5962 */
5963
5964static SMBCFILE *
5965smbc_open_print_job_ctx(SMBCCTX *context,
5966 const char *fname)
5967{
5968 fstring server;
5969 fstring share;
5970 fstring user;
5971 fstring password;
5972 pstring path;
5973
5974 if (!context || !context->internal ||
5975 !context->internal->_initialized) {
5976
5977 errno = EINVAL;
5978 return NULL;
5979
5980 }
5981
5982 if (!fname) {
5983
5984 errno = EINVAL;
5985 return NULL;
5986
5987 }
5988
5989 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
5990
5991 if (smbc_parse_path(context, fname,
5992 NULL, 0,
5993 server, sizeof(server),
5994 share, sizeof(share),
5995 path, sizeof(path),
5996 user, sizeof(user),
5997 password, sizeof(password),
5998 NULL, 0)) {
5999 errno = EINVAL;
6000 return NULL;
6001 }
6002
6003 /* What if the path is empty, or the file exists? */
6004
6005 return context->open(context, fname, O_WRONLY, 666);
6006
6007}
6008
6009/*
6010 * Routine to print a file on a remote server ...
6011 *
6012 * We open the file, which we assume to be on a remote server, and then
6013 * copy it to a print file on the share specified by printq.
6014 */
6015
6016static int
6017smbc_print_file_ctx(SMBCCTX *c_file,
6018 const char *fname,
6019 SMBCCTX *c_print,
6020 const char *printq)
6021{
6022 SMBCFILE *fid1;
6023 SMBCFILE *fid2;
6024 int bytes;
6025 int saverr;
6026 int tot_bytes = 0;
6027 char buf[4096];
6028
6029 if (!c_file || !c_file->internal->_initialized || !c_print ||
6030 !c_print->internal->_initialized) {
6031
6032 errno = EINVAL;
6033 return -1;
6034
6035 }
6036
6037 if (!fname && !printq) {
6038
6039 errno = EINVAL;
6040 return -1;
6041
6042 }
6043
6044 /* Try to open the file for reading ... */
6045
6046 if ((long)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
6047
6048 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
6049 return -1; /* smbc_open sets errno */
6050
6051 }
6052
6053 /* Now, try to open the printer file for writing */
6054
6055 if ((long)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
6056
6057 saverr = errno; /* Save errno */
6058 c_file->close_fn(c_file, fid1);
6059 errno = saverr;
6060 return -1;
6061
6062 }
6063
6064 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
6065
6066 tot_bytes += bytes;
6067
6068 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
6069
6070 saverr = errno;
6071 c_file->close_fn(c_file, fid1);
6072 c_print->close_fn(c_print, fid2);
6073 errno = saverr;
6074
6075 }
6076
6077 }
6078
6079 saverr = errno;
6080
6081 c_file->close_fn(c_file, fid1); /* We have to close these anyway */
6082 c_print->close_fn(c_print, fid2);
6083
6084 if (bytes < 0) {
6085
6086 errno = saverr;
6087 return -1;
6088
6089 }
6090
6091 return tot_bytes;
6092
6093}
6094
6095/*
6096 * Routine to list print jobs on a printer share ...
6097 */
6098
6099static int
6100smbc_list_print_jobs_ctx(SMBCCTX *context,
6101 const char *fname,
6102 smbc_list_print_job_fn fn)
6103{
6104 SMBCSRV *srv;
6105 fstring server;
6106 fstring share;
6107 fstring user;
6108 fstring password;
6109 fstring workgroup;
6110 pstring path;
6111
6112 if (!context || !context->internal ||
6113 !context->internal->_initialized) {
6114
6115 errno = EINVAL;
6116 return -1;
6117
6118 }
6119
6120 if (!fname) {
6121
6122 errno = EINVAL;
6123 return -1;
6124
6125 }
6126
6127 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6128
6129 if (smbc_parse_path(context, fname,
6130 workgroup, sizeof(workgroup),
6131 server, sizeof(server),
6132 share, sizeof(share),
6133 path, sizeof(path),
6134 user, sizeof(user),
6135 password, sizeof(password),
6136 NULL, 0)) {
6137 errno = EINVAL;
6138 return -1;
6139 }
6140
6141 if (user[0] == (char)0) fstrcpy(user, context->user);
6142
6143 srv = smbc_server(context, True,
6144 server, share, workgroup, user, password);
6145
6146 if (!srv) {
6147
6148 return -1; /* errno set by smbc_server */
6149
6150 }
6151
6152 if (cli_print_queue(srv->cli,
6153 (void (*)(struct print_job_info *))fn) < 0) {
6154
6155 errno = smbc_errno(context, srv->cli);
6156 return -1;
6157
6158 }
6159
6160 return 0;
6161
6162}
6163
6164/*
6165 * Delete a print job from a remote printer share
6166 */
6167
6168static int
6169smbc_unlink_print_job_ctx(SMBCCTX *context,
6170 const char *fname,
6171 int id)
6172{
6173 SMBCSRV *srv;
6174 fstring server;
6175 fstring share;
6176 fstring user;
6177 fstring password;
6178 fstring workgroup;
6179 pstring path;
6180 int err;
6181
6182 if (!context || !context->internal ||
6183 !context->internal->_initialized) {
6184
6185 errno = EINVAL;
6186 return -1;
6187
6188 }
6189
6190 if (!fname) {
6191
6192 errno = EINVAL;
6193 return -1;
6194
6195 }
6196
6197 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6198
6199 if (smbc_parse_path(context, fname,
6200 workgroup, sizeof(workgroup),
6201 server, sizeof(server),
6202 share, sizeof(share),
6203 path, sizeof(path),
6204 user, sizeof(user),
6205 password, sizeof(password),
6206 NULL, 0)) {
6207 errno = EINVAL;
6208 return -1;
6209 }
6210
6211 if (user[0] == (char)0) fstrcpy(user, context->user);
6212
6213 srv = smbc_server(context, True,
6214 server, share, workgroup, user, password);
6215
6216 if (!srv) {
6217
6218 return -1; /* errno set by smbc_server */
6219
6220 }
6221
6222 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6223
6224 if (err < 0)
6225 errno = smbc_errno(context, srv->cli);
6226 else if (err == ERRnosuchprintjob)
6227 errno = EINVAL;
6228 return -1;
6229
6230 }
6231
6232 return 0;
6233
6234}
6235
6236/*
6237 * Get a new empty handle to fill in with your own info
6238 */
6239SMBCCTX *
6240smbc_new_context(void)
6241{
6242 SMBCCTX *context;
6243
6244 context = SMB_MALLOC_P(SMBCCTX);
6245 if (!context) {
6246 errno = ENOMEM;
6247 return NULL;
6248 }
6249
6250 ZERO_STRUCTP(context);
6251
6252 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6253 if (!context->internal) {
6254 SAFE_FREE(context);
6255 errno = ENOMEM;
6256 return NULL;
6257 }
6258
6259 ZERO_STRUCTP(context->internal);
6260
6261
6262 /* ADD REASONABLE DEFAULTS */
6263 context->debug = 0;
6264 context->timeout = 20000; /* 20 seconds */
6265
6266 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6267 context->options.urlencode_readdir_entries = False;/* backward compat */
6268 context->options.one_share_per_server = False;/* backward compat */
6269 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6270 /* backward compat */
6271
6272 context->open = smbc_open_ctx;
6273 context->creat = smbc_creat_ctx;
6274 context->read = smbc_read_ctx;
6275 context->write = smbc_write_ctx;
6276 context->close_fn = smbc_close_ctx;
6277 context->unlink = smbc_unlink_ctx;
6278 context->rename = smbc_rename_ctx;
6279 context->lseek = smbc_lseek_ctx;
6280 context->stat = smbc_stat_ctx;
6281 context->fstat = smbc_fstat_ctx;
6282 context->opendir = smbc_opendir_ctx;
6283 context->closedir = smbc_closedir_ctx;
6284 context->readdir = smbc_readdir_ctx;
6285 context->getdents = smbc_getdents_ctx;
6286 context->mkdir = smbc_mkdir_ctx;
6287 context->rmdir = smbc_rmdir_ctx;
6288 context->telldir = smbc_telldir_ctx;
6289 context->lseekdir = smbc_lseekdir_ctx;
6290 context->fstatdir = smbc_fstatdir_ctx;
6291 context->chmod = smbc_chmod_ctx;
6292 context->utimes = smbc_utimes_ctx;
6293 context->setxattr = smbc_setxattr_ctx;
6294 context->getxattr = smbc_getxattr_ctx;
6295 context->removexattr = smbc_removexattr_ctx;
6296 context->listxattr = smbc_listxattr_ctx;
6297 context->open_print_job = smbc_open_print_job_ctx;
6298 context->print_file = smbc_print_file_ctx;
6299 context->list_print_jobs = smbc_list_print_jobs_ctx;
6300 context->unlink_print_job = smbc_unlink_print_job_ctx;
6301
6302 context->callbacks.check_server_fn = smbc_check_server;
6303 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6304
6305 smbc_default_cache_functions(context);
6306
6307 return context;
6308}
6309
6310/*
6311 * Free a context
6312 *
6313 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6314 * and thus you'll be leaking memory if not handled properly.
6315 *
6316 */
6317int
6318smbc_free_context(SMBCCTX *context,
6319 int shutdown_ctx)
6320{
6321 if (!context) {
6322 errno = EBADF;
6323 return 1;
6324 }
6325
6326 if (shutdown_ctx) {
6327 SMBCFILE * f;
6328 DEBUG(1,("Performing aggressive shutdown.\n"));
6329
6330 f = context->internal->_files;
6331 while (f) {
6332 context->close_fn(context, f);
6333 f = f->next;
6334 }
6335 context->internal->_files = NULL;
6336
6337 /* First try to remove the servers the nice way. */
6338 if (context->callbacks.purge_cached_fn(context)) {
6339 SMBCSRV * s;
6340 SMBCSRV * next;
6341 DEBUG(1, ("Could not purge all servers, "
6342 "Nice way shutdown failed.\n"));
6343 s = context->internal->_servers;
6344 while (s) {
6345 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6346 s, s->cli->fd));
6347 cli_shutdown(s->cli);
6348 context->callbacks.remove_cached_srv_fn(context,
6349 s);
6350 next = s->next;
6351 DLIST_REMOVE(context->internal->_servers, s);
6352 SAFE_FREE(s);
6353 s = next;
6354 }
6355 context->internal->_servers = NULL;
6356 }
6357 }
6358 else {
6359 /* This is the polite way */
6360 if (context->callbacks.purge_cached_fn(context)) {
6361 DEBUG(1, ("Could not purge all servers, "
6362 "free_context failed.\n"));
6363 errno = EBUSY;
6364 return 1;
6365 }
6366 if (context->internal->_servers) {
6367 DEBUG(1, ("Active servers in context, "
6368 "free_context failed.\n"));
6369 errno = EBUSY;
6370 return 1;
6371 }
6372 if (context->internal->_files) {
6373 DEBUG(1, ("Active files in context, "
6374 "free_context failed.\n"));
6375 errno = EBUSY;
6376 return 1;
6377 }
6378 }
6379
6380 /* Things we have to clean up */
6381 SAFE_FREE(context->workgroup);
6382 SAFE_FREE(context->netbios_name);
6383 SAFE_FREE(context->user);
6384
6385 DEBUG(3, ("Context %p succesfully freed\n", context));
6386 SAFE_FREE(context->internal);
6387 SAFE_FREE(context);
6388 return 0;
6389}
6390
6391
6392/*
6393 * Each time the context structure is changed, we have binary backward
6394 * compatibility issues. Instead of modifying the public portions of the
6395 * context structure to add new options, instead, we put them in the internal
6396 * portion of the context structure and provide a set function for these new
6397 * options.
6398 */
6399void
6400smbc_option_set(SMBCCTX *context,
6401 char *option_name,
6402 ... /* option_value */)
6403{
6404 va_list ap;
6405 union {
6406 int i;
6407 BOOL b;
6408 smbc_get_auth_data_with_context_fn auth_fn;
6409 void *v;
6410 } option_value;
6411
6412 va_start(ap, option_name);
6413
6414 if (strcmp(option_name, "debug_to_stderr") == 0) {
6415 /*
6416 * Log to standard error instead of standard output.
6417 */
6418 option_value.b = (BOOL) va_arg(ap, int);
6419 context->internal->_debug_stderr = option_value.b;
6420
6421 } else if (strcmp(option_name, "full_time_names") == 0) {
6422 /*
6423 * Use new-style time attribute names, e.g. WRITE_TIME rather
6424 * than the old-style names such as M_TIME. This allows also
6425 * setting/getting CREATE_TIME which was previously
6426 * unimplemented. (Note that the old C_TIME was supposed to
6427 * be CHANGE_TIME but was confused and sometimes referred to
6428 * CREATE_TIME.)
6429 */
6430 option_value.b = (BOOL) va_arg(ap, int);
6431 context->internal->_full_time_names = option_value.b;
6432
6433 } else if (strcmp(option_name, "open_share_mode") == 0) {
6434 /*
6435 * The share mode to use for files opened with
6436 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6437 */
6438 option_value.i = va_arg(ap, int);
6439 context->internal->_share_mode =
6440 (smbc_share_mode) option_value.i;
6441
6442 } else if (strcmp(option_name, "auth_function") == 0) {
6443 /*
6444 * Use the new-style authentication function which includes
6445 * the context.
6446 */
6447 option_value.auth_fn =
6448 va_arg(ap, smbc_get_auth_data_with_context_fn);
6449 context->internal->_auth_fn_with_context =
6450 option_value.auth_fn;
6451 } else if (strcmp(option_name, "user_data") == 0) {
6452 /*
6453 * Save a user data handle which may be retrieved by the user
6454 * with smbc_option_get()
6455 */
6456 option_value.v = va_arg(ap, void *);
6457 context->internal->_user_data = option_value.v;
6458 }
6459
6460 va_end(ap);
6461}
6462
6463
6464/*
6465 * Retrieve the current value of an option
6466 */
6467void *
6468smbc_option_get(SMBCCTX *context,
6469 char *option_name)
6470{
6471 if (strcmp(option_name, "debug_stderr") == 0) {
6472 /*
6473 * Log to standard error instead of standard output.
6474 */
6475#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6476 return (void *) (intptr_t) context->internal->_debug_stderr;
6477#else
6478 return (void *) context->internal->_debug_stderr;
6479#endif
6480 } else if (strcmp(option_name, "full_time_names") == 0) {
6481 /*
6482 * Use new-style time attribute names, e.g. WRITE_TIME rather
6483 * than the old-style names such as M_TIME. This allows also
6484 * setting/getting CREATE_TIME which was previously
6485 * unimplemented. (Note that the old C_TIME was supposed to
6486 * be CHANGE_TIME but was confused and sometimes referred to
6487 * CREATE_TIME.)
6488 */
6489#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6490 return (void *) (intptr_t) context->internal->_full_time_names;
6491#else
6492 return (void *) context->internal->_full_time_names;
6493#endif
6494
6495 } else if (strcmp(option_name, "auth_function") == 0) {
6496 /*
6497 * Use the new-style authentication function which includes
6498 * the context.
6499 */
6500 return (void *) context->internal->_auth_fn_with_context;
6501 } else if (strcmp(option_name, "user_data") == 0) {
6502 /*
6503 * Save a user data handle which may be retrieved by the user
6504 * with smbc_option_get()
6505 */
6506 return context->internal->_user_data;
6507 }
6508
6509 return NULL;
6510}
6511
6512
6513/*
6514 * Initialise the library etc
6515 *
6516 * We accept a struct containing handle information.
6517 * valid values for info->debug from 0 to 100,
6518 * and insist that info->fn must be non-null.
6519 */
6520SMBCCTX *
6521smbc_init_context(SMBCCTX *context)
6522{
6523 pstring conf;
6524 int pid;
6525 char *user = NULL;
6526 char *home = NULL;
6527
6528 if (!context || !context->internal) {
6529 errno = EBADF;
6530 return NULL;
6531 }
6532
6533 /* Do not initialise the same client twice */
6534 if (context->internal->_initialized) {
6535 return 0;
6536 }
6537
6538 if ((!context->callbacks.auth_fn &&
6539 !context->internal->_auth_fn_with_context) ||
6540 context->debug < 0 ||
6541 context->debug > 100) {
6542
6543 errno = EINVAL;
6544 return NULL;
6545
6546 }
6547
6548 if (!smbc_initialized) {
6549 /*
6550 * Do some library-wide intializations the first time we get
6551 * called
6552 */
6553 BOOL conf_loaded = False;
6554
6555 /* Set this to what the user wants */
6556 DEBUGLEVEL = context->debug;
6557
6558 load_case_tables();
6559
6560 setup_logging("libsmbclient", True);
6561 if (context->internal->_debug_stderr) {
6562 dbf = x_stderr;
6563 x_setbuf(x_stderr, NULL);
6564 }
6565
6566 /* Here we would open the smb.conf file if needed ... */
6567
6568 in_client = True; /* FIXME, make a param */
6569
6570 home = getenv("HOME");
6571 if (home) {
6572 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
6573 if (lp_load(conf, True, False, False, True)) {
6574 conf_loaded = True;
6575 } else {
6576 DEBUG(5, ("Could not load config file: %s\n",
6577 conf));
6578 }
6579 }
6580
6581 if (!conf_loaded) {
6582 /*
6583 * Well, if that failed, try the dyn_CONFIGFILE
6584 * Which points to the standard locn, and if that
6585 * fails, silently ignore it and use the internal
6586 * defaults ...
6587 */
6588
6589 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6590 DEBUG(5, ("Could not load config file: %s\n",
6591 dyn_CONFIGFILE));
6592 } else if (home) {
6593 /*
6594 * We loaded the global config file. Now lets
6595 * load user-specific modifications to the
6596 * global config.
6597 */
6598 slprintf(conf, sizeof(conf),
6599 "%s/.smb/smb.conf.append", home);
6600 if (!lp_load(conf, True, False, False, False)) {
6601 DEBUG(10,
6602 ("Could not append config file: "
6603 "%s\n",
6604 conf));
6605 }
6606 }
6607 }
6608
6609 load_interfaces(); /* Load the list of interfaces ... */
6610
6611 reopen_logs(); /* Get logging working ... */
6612
6613 /*
6614 * Block SIGPIPE (from lib/util_sock.c: write())
6615 * It is not needed and should not stop execution
6616 */
6617 BlockSignals(True, SIGPIPE);
6618
6619 /* Done with one-time initialisation */
6620 smbc_initialized = 1;
6621
6622 }
6623
6624 if (!context->user) {
6625 /*
6626 * FIXME: Is this the best way to get the user info?
6627 */
6628 user = getenv("USER");
6629 /* walk around as "guest" if no username can be found */
6630 if (!user) context->user = SMB_STRDUP("guest");
6631 else context->user = SMB_STRDUP(user);
6632 }
6633
6634 if (!context->netbios_name) {
6635 /*
6636 * We try to get our netbios name from the config. If that
6637 * fails we fall back on constructing our netbios name from
6638 * our hostname etc
6639 */
6640 if (global_myname()) {
6641 context->netbios_name = SMB_STRDUP(global_myname());
6642 }
6643 else {
6644 /*
6645 * Hmmm, I want to get hostname as well, but I am too
6646 * lazy for the moment
6647 */
6648 pid = sys_getpid();
6649 context->netbios_name = (char *)SMB_MALLOC(17);
6650 if (!context->netbios_name) {
6651 errno = ENOMEM;
6652 return NULL;
6653 }
6654 slprintf(context->netbios_name, 16,
6655 "smbc%s%d", context->user, pid);
6656 }
6657 }
6658
6659 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6660
6661 if (!context->workgroup) {
6662 if (lp_workgroup()) {
6663 context->workgroup = SMB_STRDUP(lp_workgroup());
6664 }
6665 else {
6666 /* TODO: Think about a decent default workgroup */
6667 context->workgroup = SMB_STRDUP("samba");
6668 }
6669 }
6670
6671 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6672
6673 /* shortest timeout is 1 second */
6674 if (context->timeout > 0 && context->timeout < 1000)
6675 context->timeout = 1000;
6676
6677 /*
6678 * FIXME: Should we check the function pointers here?
6679 */
6680
6681 context->internal->_initialized = True;
6682
6683 return context;
6684}
6685
6686
6687/* Return the verion of samba, and thus libsmbclient */
6688const char *
6689smbc_version(void)
6690{
6691 return samba_version_string();
6692}
Note: See TracBrowser for help on using the repository browser.