r22914: - Fixes bug 4599. A missing <code>if</code> statement forced subseqeuent
[Samba/bb.git] / source / libsmb / libsmbclient.c
blob32f195547fccc9b72691da2f3da1d13b664969a1
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
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.
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.
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.
25 #include "includes.h"
27 #include "include/libsmb_internal.h"
29 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir);
30 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
31 struct smbc_dirent *dirent);
34 * DOS Attribute values (used internally)
36 typedef 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;
48 * Internal flags for extended attributes
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
59 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
61 /*We should test for this in configure ... */
62 #ifndef ENOTSUP
63 #define ENOTSUP EOPNOTSUPP
64 #endif
67 * Functions exported by libsmb_cache.c that we need here
69 int smbc_default_cache_functions(SMBCCTX *context);
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
77 static 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;
87 * Find an lsa pipe handle associated with a cli struct.
89 static struct rpc_pipe_client *
90 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
92 struct rpc_pipe_client *pipe_hnd;
94 for (pipe_hnd = ipc_cli->pipe_list;
95 pipe_hnd;
96 pipe_hnd = pipe_hnd->next) {
98 if (pipe_hnd->pipe_idx == PI_LSARPC) {
99 return pipe_hnd;
103 return NULL;
106 static int
107 smbc_close_ctx(SMBCCTX *context,
108 SMBCFILE *file);
109 static off_t
110 smbc_lseek_ctx(SMBCCTX *context,
111 SMBCFILE *file,
112 off_t offset,
113 int whence);
115 extern BOOL in_client;
118 * Is the logging working / configfile read ?
120 static int smbc_initialized = 0;
122 static int
123 hex2int( unsigned int _char )
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;
135 * smbc_urldecode()
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.
140 * dest may, but need not be, the same pointer as src.
142 * Returns the number of % sequences which could not be converted due to lack
143 * of two following hexadecimal digits.
146 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
148 int old_length = strlen(src);
149 int i = 0;
150 int err_count = 0;
151 pstring temp;
152 char * p;
154 if ( old_length == 0 ) {
155 return 0;
158 p = temp;
159 while ( i < old_length ) {
160 unsigned char character = src[ i++ ];
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;
166 /* Replace valid sequence */
167 if (a != -1 && b != -1) {
169 /* Replace valid %xx sequence with %dd */
170 character = (a * 16) + b;
172 if (character == '\0') {
173 break; /* Stop at %00 */
176 i += 2;
177 } else {
179 err_count++;
183 *p++ = character;
186 *p = '\0';
188 strncpy(dest, temp, max_dest_len - 1);
189 dest[max_dest_len - 1] = '\0';
191 return err_count;
195 * smbc_urlencode()
197 * Convert any characters not specifically allowed in a URL into their %xx
198 * equivalent.
200 * Returns the remaining buffer length.
203 smbc_urlencode(char * dest, char * src, int max_dest_len)
205 char hex[] = "0123456789ABCDEF";
207 for (; *src != '\0' && max_dest_len >= 3; src++) {
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--;
228 *dest++ = '\0';
229 max_dest_len--;
231 return max_dest_len;
235 * Function to parse a path and turn it into components
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:").
242 * We accept:
243 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
245 * Meaning of URLs:
247 * smb:// Show all workgroups.
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.
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 ...
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.
271 static const char *smbc_prefix = "smb:";
273 static int
274 smbc_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)
284 static pstring s;
285 pstring userinfo;
286 const char *p;
287 char *q, *r;
288 int len;
290 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
293 * Assume we wont find an authentication domain to parse, so default
294 * to the workgroup in the provided context.
296 if (workgroup != NULL) {
297 strncpy(workgroup, context->workgroup, workgroup_len - 1);
298 workgroup[workgroup_len - 1] = '\0';
301 if (options != NULL && options_len > 0) {
302 options[0] = (char)0;
304 pstrcpy(s, fname);
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: ? */
312 p = s + len;
314 /* Watch the test below, we are testing to see if we should exit */
316 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
318 DEBUG(1, ("Invalid path (does not begin with smb://"));
319 return -1;
323 p += 2; /* Skip the double slash */
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';
330 DEBUG(4, ("Found options '%s'", q));
332 /* Copy the options */
333 if (options != NULL && options_len > 0) {
334 safe_strcpy(options, q, options_len - 1);
338 if (*p == (char)0)
339 goto decoding;
341 if (*p == '/') {
342 int wl = strlen(context->workgroup);
344 if (wl > 16) {
345 wl = 16;
348 strncpy(server, context->workgroup, wl);
349 server[wl] = '\0';
350 return 0;
354 * ok, its for us. Now parse out the server, share etc.
356 * However, we want to parse out [[domain;]user[:password]@] if it
357 * exists ...
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;
367 next_token_no_ltrim(&p, userinfo, "@", sizeof(fstring));
369 username[0] = passwd[0] = domain[0] = 0;
371 if (strchr_m(u, ';')) {
373 next_token_no_ltrim(&u, domain, ";", sizeof(fstring));
377 if (strchr_m(u, ':')) {
379 next_token_no_ltrim(&u, username, ":", sizeof(fstring));
381 pstrcpy(passwd, u);
384 else {
386 pstrcpy(username, u);
390 if (domain[0] && workgroup) {
391 strncpy(workgroup, domain, workgroup_len - 1);
392 workgroup[workgroup_len - 1] = '\0';
395 if (username[0]) {
396 strncpy(user, username, user_len - 1);
397 user[user_len - 1] = '\0';
400 if (passwd[0]) {
401 strncpy(password, passwd, password_len - 1);
402 password[password_len - 1] = '\0';
407 if (!next_token(&p, server, "/", sizeof(fstring))) {
409 return -1;
413 if (*p == (char)0) goto decoding; /* That's it ... */
415 if (!next_token(&p, share, "/", sizeof(fstring))) {
417 return -1;
422 * Prepend a leading slash if there's a file path, as required by
423 * NetApp filers.
425 *path = '\0';
426 if (*p != '\0') {
427 *path = '/';
428 safe_strcpy(path + 1, p, path_len - 2);
431 all_string_sub(path, "/", "\\", 0);
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);
440 return 0;
444 * Verify that the options specified in a URL are valid
446 static int
447 smbc_check_options(char *server,
448 char *share,
449 char *path,
450 char *options)
452 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
453 "path='%s' options='%s'\n",
454 server, share, path, options));
456 /* No options at all is always ok */
457 if (! *options) return 0;
459 /* Currently, we don't support any options. */
460 return -1;
464 * Convert an SMB error into a UNIX error ...
466 static int
467 smbc_errno(SMBCCTX *context,
468 struct cli_state *c)
470 int ret = cli_errno(c);
472 if (cli_is_dos_error(c)) {
473 uint8 eclass;
474 uint32 ecode;
476 cli_dos_error(c, &eclass, &ecode);
478 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
479 (int)eclass, (int)ecode, (int)ecode, ret));
480 } else {
481 NTSTATUS status;
483 status = cli_nt_error(c);
485 DEBUG(3,("smbc errno %s -> %d\n",
486 nt_errstr(status), ret));
489 return ret;
493 * Check a server for being alive and well.
494 * returns 0 if the server is in shape. Returns 1 on error
496 * Also useable outside libsmbclient to enable external cache
497 * to do some checks too.
499 static int
500 smbc_check_server(SMBCCTX * context,
501 SMBCSRV * server)
503 size_t size;
504 struct sockaddr addr;
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.
512 if (server->cli->port == 139) {
513 /* Assuming netbios. Send a keepalive packet */
514 if ( send_keepalive(server->cli->fd) == False ) {
515 return 1;
517 } else {
519 * Assuming not netbios. Try a different method to detect if
520 * the connection is still alive.
522 size = sizeof(addr);
523 if (getpeername(server->cli->fd, &addr, &size) == -1) {
524 return 1;
528 /* connection is ok */
529 return 0;
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.
536 * Also useable outside libsmbclient
539 smbc_remove_unused_server(SMBCCTX * context,
540 SMBCSRV * srv)
542 SMBCFILE * file;
544 /* are we being fooled ? */
545 if (!context || !context->internal ||
546 !context->internal->_initialized || !srv) return 1;
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;
560 DLIST_REMOVE(context->internal->_servers, srv);
562 cli_shutdown(srv->cli);
563 srv->cli = NULL;
565 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
567 context->callbacks.remove_cached_srv_fn(context, srv);
569 SAFE_FREE(srv);
571 return 0;
574 static SMBCSRV *
575 find_server(SMBCCTX *context,
576 const char *server,
577 const char *share,
578 fstring workgroup,
579 fstring username,
580 fstring password)
582 SMBCSRV *srv;
583 int auth_called = 0;
585 check_server_cache:
587 srv = context->callbacks.get_cached_srv_fn(context, server, share,
588 workgroup, username);
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));
607 * However, smbc_auth_fn may have picked up info relating to
608 * an existing connection, so try for an existing connection
609 * again ...
611 auth_called = 1;
612 goto check_server_cache;
616 if (srv) {
617 if (context->callbacks.check_server_fn(context, srv)) {
619 * This server is no good anymore
620 * Try to remove it and check for more possible
621 * servers in the cache
623 if (context->callbacks.remove_unused_server_fn(context,
624 srv)) {
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.
631 context->callbacks.remove_cached_srv_fn(context,
632 srv);
636 * Maybe there are more cached connections to this
637 * server
639 goto check_server_cache;
642 return srv;
645 return NULL;
649 * Connect to a server, possibly on an existing connection
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.
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.
659 static SMBCSRV *
660 smbc_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)
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;
679 zero_ip(&ip);
680 ZERO_STRUCT(c);
682 if (server[0] == 0) {
683 errno = EPERM;
684 return NULL;
687 /* Look for a cached connection */
688 srv = find_server(context, server, share,
689 workgroup, username, password);
692 * If we found a connection and we're only allowed one share per
693 * server...
695 if (srv && *share != '\0' && context->options.one_share_per_server) {
698 * ... then if there's no current connection to the share,
699 * connect to it. find_server(), or rather the function
700 * pointed to by context->callbacks.get_cached_srv_fn which
701 * was called by find_server(), will have issued a tree
702 * disconnect if the requested share is not the same as the
703 * one that was already connected.
705 if (srv->cli->cnum == (uint16) -1) {
706 /* Ensure we have accurate auth info */
707 if (context->internal->_auth_fn_with_context != NULL) {
708 context->internal->_auth_fn_with_context(
709 context,
710 server, share,
711 workgroup, sizeof(fstring),
712 username, sizeof(fstring),
713 password, sizeof(fstring));
714 } else {
715 context->callbacks.auth_fn(
716 server, share,
717 workgroup, sizeof(fstring),
718 username, sizeof(fstring),
719 password, sizeof(fstring));
722 if (! cli_send_tconX(srv->cli, share, "?????",
723 password, strlen(password)+1)) {
725 errno = smbc_errno(context, srv->cli);
726 cli_shutdown(srv->cli);
727 srv->cli = NULL;
728 context->callbacks.remove_cached_srv_fn(context,
729 srv);
730 srv = NULL;
734 * Regenerate the dev value since it's based on both
735 * server and share
737 if (srv) {
738 srv->dev = (dev_t)(str_checksum(server) ^
739 str_checksum(share));
744 /* If we have a connection... */
745 if (srv) {
747 /* ... then we're done here. Give 'em what they came for. */
748 return srv;
751 /* If we're not asked to connect when a connection doesn't exist... */
752 if (! connect_if_not_found) {
753 /* ... then we're done here. */
754 return NULL;
757 make_nmb_name(&calling, context->netbios_name, 0x0);
758 make_nmb_name(&called , server, 0x20);
760 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
762 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
764 again:
765 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
767 zero_ip(&ip);
769 /* have to open a new connection */
770 if ((c = cli_initialise()) == NULL) {
771 errno = ENOMEM;
772 return NULL;
775 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
776 c->use_kerberos = True;
778 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
779 c->fallback_after_kerberos = True;
782 c->timeout = context->timeout;
785 * Force use of port 139 for first try if share is $IPC, empty, or
786 * null, so browse lists can work
788 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
789 port_try_first = 139;
790 port_try_next = 445;
791 } else {
792 port_try_first = 445;
793 port_try_next = 139;
796 c->port = port_try_first;
798 if (!cli_connect(c, server_n, &ip)) {
800 /* First connection attempt failed. Try alternate port. */
801 c->port = port_try_next;
803 if (!cli_connect(c, server_n, &ip)) {
804 cli_shutdown(c);
805 errno = ETIMEDOUT;
806 return NULL;
810 if (!cli_session_request(c, &calling, &called)) {
811 cli_shutdown(c);
812 if (strcmp(called.name, "*SMBSERVER")) {
813 make_nmb_name(&called , "*SMBSERVER", 0x20);
814 goto again;
815 } else { /* Try one more time, but ensure we don't loop */
817 /* Only try this if server is an IP address ... */
819 if (is_ipaddress(server) && !tried_reverse) {
820 fstring remote_name;
821 struct in_addr rem_ip;
823 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
824 DEBUG(4, ("Could not convert IP address "
825 "%s to struct in_addr\n", server));
826 errno = ETIMEDOUT;
827 return NULL;
830 tried_reverse++; /* Yuck */
832 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
833 make_nmb_name(&called, remote_name, 0x20);
834 goto again;
838 errno = ETIMEDOUT;
839 return NULL;
842 DEBUG(4,(" session request ok\n"));
844 if (!cli_negprot(c)) {
845 cli_shutdown(c);
846 errno = ETIMEDOUT;
847 return NULL;
850 username_used = username;
852 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
853 password, strlen(password),
854 password, strlen(password),
855 workgroup))) {
857 /* Failed. Try an anonymous login, if allowed by flags. */
858 username_used = "";
860 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
861 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
862 password, 1,
863 password, 0,
864 workgroup))) {
866 cli_shutdown(c);
867 errno = EPERM;
868 return NULL;
872 DEBUG(4,(" session setup ok\n"));
874 if (!cli_send_tconX(c, share, "?????",
875 password, strlen(password)+1)) {
876 errno = smbc_errno(context, c);
877 cli_shutdown(c);
878 return NULL;
881 DEBUG(4,(" tconx ok\n"));
884 * Ok, we have got a nice connection
885 * Let's allocate a server structure.
888 srv = SMB_MALLOC_P(SMBCSRV);
889 if (!srv) {
890 errno = ENOMEM;
891 goto failed;
894 ZERO_STRUCTP(srv);
895 srv->cli = c;
896 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
897 srv->no_pathinfo = False;
898 srv->no_pathinfo2 = False;
899 srv->no_nt_session = False;
901 /* now add it to the cache (internal or external) */
902 /* Let the cache function set errno if it wants to */
903 errno = 0;
904 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
905 int saved_errno = errno;
906 DEBUG(3, (" Failed to add server to cache\n"));
907 errno = saved_errno;
908 if (errno == 0) {
909 errno = ENOMEM;
911 goto failed;
914 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
915 server, share, srv));
917 DLIST_ADD(context->internal->_servers, srv);
918 return srv;
920 failed:
921 cli_shutdown(c);
922 if (!srv) {
923 return NULL;
926 SAFE_FREE(srv);
927 return NULL;
931 * Connect to a server for getting/setting attributes, possibly on an existing
932 * connection. This works similarly to smbc_server().
934 static SMBCSRV *
935 smbc_attr_server(SMBCCTX *context,
936 const char *server,
937 const char *share,
938 fstring workgroup,
939 fstring username,
940 fstring password,
941 POLICY_HND *pol)
943 int flags;
944 struct in_addr ip;
945 struct cli_state *ipc_cli;
946 struct rpc_pipe_client *pipe_hnd;
947 NTSTATUS nt_status;
948 SMBCSRV *ipc_srv=NULL;
951 * See if we've already created this special connection. Reference
952 * our "special" share name '*IPC$', which is an impossible real share
953 * name due to the leading asterisk.
955 ipc_srv = find_server(context, server, "*IPC$",
956 workgroup, username, password);
957 if (!ipc_srv) {
959 /* We didn't find a cached connection. Get the password */
960 if (*password == '\0') {
961 /* ... then retrieve it now. */
962 if (context->internal->_auth_fn_with_context != NULL) {
963 context->internal->_auth_fn_with_context(
964 context,
965 server, share,
966 workgroup, sizeof(fstring),
967 username, sizeof(fstring),
968 password, sizeof(fstring));
969 } else {
970 context->callbacks.auth_fn(
971 server, share,
972 workgroup, sizeof(fstring),
973 username, sizeof(fstring),
974 password, sizeof(fstring));
978 flags = 0;
979 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
980 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
983 zero_ip(&ip);
984 nt_status = cli_full_connection(&ipc_cli,
985 global_myname(), server,
986 &ip, 0, "IPC$", "?????",
987 username, workgroup,
988 password, flags,
989 Undefined, NULL);
990 if (! NT_STATUS_IS_OK(nt_status)) {
991 DEBUG(1,("cli_full_connection failed! (%s)\n",
992 nt_errstr(nt_status)));
993 errno = ENOTSUP;
994 return NULL;
997 ipc_srv = SMB_MALLOC_P(SMBCSRV);
998 if (!ipc_srv) {
999 errno = ENOMEM;
1000 cli_shutdown(ipc_cli);
1001 return NULL;
1004 ZERO_STRUCTP(ipc_srv);
1005 ipc_srv->cli = ipc_cli;
1007 if (pol) {
1008 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1009 PI_LSARPC,
1010 &nt_status);
1011 if (!pipe_hnd) {
1012 DEBUG(1, ("cli_nt_session_open fail!\n"));
1013 errno = ENOTSUP;
1014 cli_shutdown(ipc_srv->cli);
1015 free(ipc_srv);
1016 return NULL;
1020 * Some systems don't support
1021 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1022 * so we might as well do it too.
1025 nt_status = rpccli_lsa_open_policy(
1026 pipe_hnd,
1027 ipc_srv->cli->mem_ctx,
1028 True,
1029 GENERIC_EXECUTE_ACCESS,
1030 pol);
1032 if (!NT_STATUS_IS_OK(nt_status)) {
1033 errno = smbc_errno(context, ipc_srv->cli);
1034 cli_shutdown(ipc_srv->cli);
1035 return NULL;
1039 /* now add it to the cache (internal or external) */
1041 errno = 0; /* let cache function set errno if it likes */
1042 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
1043 server,
1044 "*IPC$",
1045 workgroup,
1046 username)) {
1047 DEBUG(3, (" Failed to add server to cache\n"));
1048 if (errno == 0) {
1049 errno = ENOMEM;
1051 cli_shutdown(ipc_srv->cli);
1052 free(ipc_srv);
1053 return NULL;
1056 DLIST_ADD(context->internal->_servers, ipc_srv);
1059 return ipc_srv;
1063 * Routine to open() a file ...
1066 static SMBCFILE *
1067 smbc_open_ctx(SMBCCTX *context,
1068 const char *fname,
1069 int flags,
1070 mode_t mode)
1072 fstring server, share, user, password, workgroup;
1073 pstring path;
1074 pstring targetpath;
1075 struct cli_state *targetcli;
1076 SMBCSRV *srv = NULL;
1077 SMBCFILE *file = NULL;
1078 int fd;
1080 if (!context || !context->internal ||
1081 !context->internal->_initialized) {
1083 errno = EINVAL; /* Best I can think of ... */
1084 return NULL;
1088 if (!fname) {
1090 errno = EINVAL;
1091 return NULL;
1095 if (smbc_parse_path(context, fname,
1096 workgroup, sizeof(workgroup),
1097 server, sizeof(server),
1098 share, sizeof(share),
1099 path, sizeof(path),
1100 user, sizeof(user),
1101 password, sizeof(password),
1102 NULL, 0)) {
1103 errno = EINVAL;
1104 return NULL;
1107 if (user[0] == (char)0) fstrcpy(user, context->user);
1109 srv = smbc_server(context, True,
1110 server, share, workgroup, user, password);
1112 if (!srv) {
1114 if (errno == EPERM) errno = EACCES;
1115 return NULL; /* smbc_server sets errno */
1119 /* Hmmm, the test for a directory is suspect here ... FIXME */
1121 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1123 fd = -1;
1126 else {
1128 file = SMB_MALLOC_P(SMBCFILE);
1130 if (!file) {
1132 errno = ENOMEM;
1133 return NULL;
1137 ZERO_STRUCTP(file);
1139 /*d_printf(">>>open: resolving %s\n", path);*/
1140 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1142 d_printf("Could not resolve %s\n", path);
1143 SAFE_FREE(file);
1144 return NULL;
1146 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1148 if ((fd = cli_open(targetcli, targetpath, flags,
1149 context->internal->_share_mode)) < 0) {
1151 /* Handle the error ... */
1153 SAFE_FREE(file);
1154 errno = smbc_errno(context, targetcli);
1155 return NULL;
1159 /* Fill in file struct */
1161 file->cli_fd = fd;
1162 file->fname = SMB_STRDUP(fname);
1163 file->srv = srv;
1164 file->offset = 0;
1165 file->file = True;
1167 DLIST_ADD(context->internal->_files, file);
1170 * If the file was opened in O_APPEND mode, all write
1171 * operations should be appended to the file. To do that,
1172 * though, using this protocol, would require a getattrE()
1173 * call for each and every write, to determine where the end
1174 * of the file is. (There does not appear to be an append flag
1175 * in the protocol.) Rather than add all of that overhead of
1176 * retrieving the current end-of-file offset prior to each
1177 * write operation, we'll assume that most append operations
1178 * will continuously write, so we'll just set the offset to
1179 * the end of the file now and hope that's adequate.
1181 * Note to self: If this proves inadequate, and O_APPEND
1182 * should, in some cases, be forced for each write, add a
1183 * field in the context options structure, for
1184 * "strict_append_mode" which would select between the current
1185 * behavior (if FALSE) or issuing a getattrE() prior to each
1186 * write and forcing the write to the end of the file (if
1187 * TRUE). Adding that capability will likely require adding
1188 * an "append" flag into the _SMBCFILE structure to track
1189 * whether a file was opened in O_APPEND mode. -- djl
1191 if (flags & O_APPEND) {
1192 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1193 (void) smbc_close_ctx(context, file);
1194 errno = ENXIO;
1195 return NULL;
1199 return file;
1203 /* Check if opendir needed ... */
1205 if (fd == -1) {
1206 int eno = 0;
1208 eno = smbc_errno(context, srv->cli);
1209 file = context->opendir(context, fname);
1210 if (!file) errno = eno;
1211 return file;
1215 errno = EINVAL; /* FIXME, correct errno ? */
1216 return NULL;
1221 * Routine to create a file
1224 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1226 static SMBCFILE *
1227 smbc_creat_ctx(SMBCCTX *context,
1228 const char *path,
1229 mode_t mode)
1232 if (!context || !context->internal ||
1233 !context->internal->_initialized) {
1235 errno = EINVAL;
1236 return NULL;
1240 return smbc_open_ctx(context, path, creat_bits, mode);
1244 * Routine to read() a file ...
1247 static ssize_t
1248 smbc_read_ctx(SMBCCTX *context,
1249 SMBCFILE *file,
1250 void *buf,
1251 size_t count)
1253 int ret;
1254 fstring server, share, user, password;
1255 pstring path, targetpath;
1256 struct cli_state *targetcli;
1259 * offset:
1261 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1262 * appears to pass file->offset (which is type off_t) differently than
1263 * a local variable of type off_t. Using local variable "offset" in
1264 * the call to cli_read() instead of file->offset fixes a problem
1265 * retrieving data at an offset greater than 4GB.
1267 off_t offset;
1269 if (!context || !context->internal ||
1270 !context->internal->_initialized) {
1272 errno = EINVAL;
1273 return -1;
1277 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1279 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1281 errno = EBADF;
1282 return -1;
1286 offset = file->offset;
1288 /* Check that the buffer exists ... */
1290 if (buf == NULL) {
1292 errno = EINVAL;
1293 return -1;
1297 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1298 if (smbc_parse_path(context, file->fname,
1299 NULL, 0,
1300 server, sizeof(server),
1301 share, sizeof(share),
1302 path, sizeof(path),
1303 user, sizeof(user),
1304 password, sizeof(password),
1305 NULL, 0)) {
1306 errno = EINVAL;
1307 return -1;
1310 /*d_printf(">>>read: resolving %s\n", path);*/
1311 if (!cli_resolve_path("", file->srv->cli, path,
1312 &targetcli, targetpath))
1314 d_printf("Could not resolve %s\n", path);
1315 return -1;
1317 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1319 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1321 if (ret < 0) {
1323 errno = smbc_errno(context, targetcli);
1324 return -1;
1328 file->offset += ret;
1330 DEBUG(4, (" --> %d\n", ret));
1332 return ret; /* Success, ret bytes of data ... */
1337 * Routine to write() a file ...
1340 static ssize_t
1341 smbc_write_ctx(SMBCCTX *context,
1342 SMBCFILE *file,
1343 void *buf,
1344 size_t count)
1346 int ret;
1347 off_t offset;
1348 fstring server, share, user, password;
1349 pstring path, targetpath;
1350 struct cli_state *targetcli;
1352 /* First check all pointers before dereferencing them */
1354 if (!context || !context->internal ||
1355 !context->internal->_initialized) {
1357 errno = EINVAL;
1358 return -1;
1362 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1364 errno = EBADF;
1365 return -1;
1369 /* Check that the buffer exists ... */
1371 if (buf == NULL) {
1373 errno = EINVAL;
1374 return -1;
1378 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1380 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1381 if (smbc_parse_path(context, file->fname,
1382 NULL, 0,
1383 server, sizeof(server),
1384 share, sizeof(share),
1385 path, sizeof(path),
1386 user, sizeof(user),
1387 password, sizeof(password),
1388 NULL, 0)) {
1389 errno = EINVAL;
1390 return -1;
1393 /*d_printf(">>>write: resolving %s\n", path);*/
1394 if (!cli_resolve_path("", file->srv->cli, path,
1395 &targetcli, targetpath))
1397 d_printf("Could not resolve %s\n", path);
1398 return -1;
1400 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1403 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1405 if (ret <= 0) {
1407 errno = smbc_errno(context, targetcli);
1408 return -1;
1412 file->offset += ret;
1414 return ret; /* Success, 0 bytes of data ... */
1418 * Routine to close() a file ...
1421 static int
1422 smbc_close_ctx(SMBCCTX *context,
1423 SMBCFILE *file)
1425 SMBCSRV *srv;
1426 fstring server, share, user, password;
1427 pstring path, targetpath;
1428 struct cli_state *targetcli;
1430 if (!context || !context->internal ||
1431 !context->internal->_initialized) {
1433 errno = EINVAL;
1434 return -1;
1438 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1440 errno = EBADF;
1441 return -1;
1445 /* IS a dir ... */
1446 if (!file->file) {
1448 return context->closedir(context, file);
1452 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1453 if (smbc_parse_path(context, file->fname,
1454 NULL, 0,
1455 server, sizeof(server),
1456 share, sizeof(share),
1457 path, sizeof(path),
1458 user, sizeof(user),
1459 password, sizeof(password),
1460 NULL, 0)) {
1461 errno = EINVAL;
1462 return -1;
1465 /*d_printf(">>>close: resolving %s\n", path);*/
1466 if (!cli_resolve_path("", file->srv->cli, path,
1467 &targetcli, targetpath))
1469 d_printf("Could not resolve %s\n", path);
1470 return -1;
1472 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1474 if (!cli_close(targetcli, file->cli_fd)) {
1476 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1477 file->fname));
1478 /* Deallocate slot and remove the server
1479 * from the server cache if unused */
1480 errno = smbc_errno(context, targetcli);
1481 srv = file->srv;
1482 DLIST_REMOVE(context->internal->_files, file);
1483 SAFE_FREE(file->fname);
1484 SAFE_FREE(file);
1485 context->callbacks.remove_unused_server_fn(context, srv);
1487 return -1;
1491 DLIST_REMOVE(context->internal->_files, file);
1492 SAFE_FREE(file->fname);
1493 SAFE_FREE(file);
1495 return 0;
1499 * Get info from an SMB server on a file. Use a qpathinfo call first
1500 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1502 static BOOL
1503 smbc_getatr(SMBCCTX * context,
1504 SMBCSRV *srv,
1505 char *path,
1506 uint16 *mode,
1507 SMB_OFF_T *size,
1508 struct timespec *create_time_ts,
1509 struct timespec *access_time_ts,
1510 struct timespec *write_time_ts,
1511 struct timespec *change_time_ts,
1512 SMB_INO_T *ino)
1514 pstring fixedpath;
1515 pstring targetpath;
1516 struct cli_state *targetcli;
1517 time_t write_time;
1519 if (!context || !context->internal ||
1520 !context->internal->_initialized) {
1522 errno = EINVAL;
1523 return -1;
1527 /* path fixup for . and .. */
1528 if (strequal(path, ".") || strequal(path, ".."))
1529 pstrcpy(fixedpath, "\\");
1530 else
1532 pstrcpy(fixedpath, path);
1533 trim_string(fixedpath, NULL, "\\..");
1534 trim_string(fixedpath, NULL, "\\.");
1536 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1538 if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
1540 d_printf("Couldn't resolve %s\n", path);
1541 return False;
1544 if (!srv->no_pathinfo2 &&
1545 cli_qpathinfo2(targetcli, targetpath,
1546 create_time_ts,
1547 access_time_ts,
1548 write_time_ts,
1549 change_time_ts,
1550 size, mode, ino)) {
1551 return True;
1554 /* if this is NT then don't bother with the getatr */
1555 if (targetcli->capabilities & CAP_NT_SMBS) {
1556 errno = EPERM;
1557 return False;
1560 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1562 struct timespec w_time_ts;
1564 w_time_ts = convert_time_t_to_timespec(write_time);
1566 if (write_time_ts != NULL) {
1567 *write_time_ts = w_time_ts;
1570 if (create_time_ts != NULL) {
1571 *create_time_ts = w_time_ts;
1574 if (access_time_ts != NULL) {
1575 *access_time_ts = w_time_ts;
1578 if (change_time_ts != NULL) {
1579 *change_time_ts = w_time_ts;
1582 srv->no_pathinfo2 = True;
1583 return True;
1586 errno = EPERM;
1587 return False;
1592 * Set file info on an SMB server. Use setpathinfo call first. If that
1593 * fails, use setattrE..
1595 * Access and modification time parameters are always used and must be
1596 * provided. Create time, if zero, will be determined from the actual create
1597 * time of the file. If non-zero, the create time will be set as well.
1599 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1601 static BOOL
1602 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1603 time_t create_time,
1604 time_t access_time,
1605 time_t write_time,
1606 time_t change_time,
1607 uint16 mode)
1609 int fd;
1610 int ret;
1613 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1614 * modern function for "new code" to be using, and it works given a
1615 * filename rather than requiring that the file be opened to have its
1616 * attributes manipulated.
1618 if (srv->no_pathinfo ||
1619 ! cli_setpathinfo(srv->cli, path,
1620 create_time,
1621 access_time,
1622 write_time,
1623 change_time,
1624 mode)) {
1627 * setpathinfo is not supported; go to plan B.
1629 * cli_setatr() does not work on win98, and it also doesn't
1630 * support setting the access time (only the modification
1631 * time), so in all cases, we open the specified file and use
1632 * cli_setattrE() which should work on all OS versions, and
1633 * supports both times.
1636 /* Don't try {q,set}pathinfo() again, with this server */
1637 srv->no_pathinfo = True;
1639 /* Open the file */
1640 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1642 errno = smbc_errno(context, srv->cli);
1643 return -1;
1646 /* Set the new attributes */
1647 ret = cli_setattrE(srv->cli, fd,
1648 change_time,
1649 access_time,
1650 write_time);
1652 /* Close the file */
1653 cli_close(srv->cli, fd);
1656 * Unfortunately, setattrE() doesn't have a provision for
1657 * setting the access mode (attributes). We'll have to try
1658 * cli_setatr() for that, and with only this parameter, it
1659 * seems to work on win98.
1661 if (ret && mode != (uint16) -1) {
1662 ret = cli_setatr(srv->cli, path, mode, 0);
1665 if (! ret) {
1666 errno = smbc_errno(context, srv->cli);
1667 return False;
1671 return True;
1675 * Routine to unlink() a file
1678 static int
1679 smbc_unlink_ctx(SMBCCTX *context,
1680 const char *fname)
1682 fstring server, share, user, password, workgroup;
1683 pstring path, targetpath;
1684 struct cli_state *targetcli;
1685 SMBCSRV *srv = NULL;
1687 if (!context || !context->internal ||
1688 !context->internal->_initialized) {
1690 errno = EINVAL; /* Best I can think of ... */
1691 return -1;
1695 if (!fname) {
1697 errno = EINVAL;
1698 return -1;
1702 if (smbc_parse_path(context, fname,
1703 workgroup, sizeof(workgroup),
1704 server, sizeof(server),
1705 share, sizeof(share),
1706 path, sizeof(path),
1707 user, sizeof(user),
1708 password, sizeof(password),
1709 NULL, 0)) {
1710 errno = EINVAL;
1711 return -1;
1714 if (user[0] == (char)0) fstrcpy(user, context->user);
1716 srv = smbc_server(context, True,
1717 server, share, workgroup, user, password);
1719 if (!srv) {
1721 return -1; /* smbc_server sets errno */
1725 /*d_printf(">>>unlink: resolving %s\n", path);*/
1726 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1728 d_printf("Could not resolve %s\n", path);
1729 return -1;
1731 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1733 if (!cli_unlink(targetcli, targetpath)) {
1735 errno = smbc_errno(context, targetcli);
1737 if (errno == EACCES) { /* Check if the file is a directory */
1739 int saverr = errno;
1740 SMB_OFF_T size = 0;
1741 uint16 mode = 0;
1742 struct timespec write_time_ts;
1743 struct timespec access_time_ts;
1744 struct timespec change_time_ts;
1745 SMB_INO_T ino = 0;
1747 if (!smbc_getatr(context, srv, path, &mode, &size,
1748 NULL,
1749 &access_time_ts,
1750 &write_time_ts,
1751 &change_time_ts,
1752 &ino)) {
1754 /* Hmmm, bad error ... What? */
1756 errno = smbc_errno(context, targetcli);
1757 return -1;
1760 else {
1762 if (IS_DOS_DIR(mode))
1763 errno = EISDIR;
1764 else
1765 errno = saverr; /* Restore this */
1770 return -1;
1774 return 0; /* Success ... */
1779 * Routine to rename() a file
1782 static int
1783 smbc_rename_ctx(SMBCCTX *ocontext,
1784 const char *oname,
1785 SMBCCTX *ncontext,
1786 const char *nname)
1788 fstring server1;
1789 fstring share1;
1790 fstring server2;
1791 fstring share2;
1792 fstring user1;
1793 fstring user2;
1794 fstring password1;
1795 fstring password2;
1796 fstring workgroup;
1797 pstring path1;
1798 pstring path2;
1799 pstring targetpath1;
1800 pstring targetpath2;
1801 struct cli_state *targetcli1;
1802 struct cli_state *targetcli2;
1803 SMBCSRV *srv = NULL;
1805 if (!ocontext || !ncontext ||
1806 !ocontext->internal || !ncontext->internal ||
1807 !ocontext->internal->_initialized ||
1808 !ncontext->internal->_initialized) {
1810 errno = EINVAL; /* Best I can think of ... */
1811 return -1;
1815 if (!oname || !nname) {
1817 errno = EINVAL;
1818 return -1;
1822 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1824 smbc_parse_path(ocontext, oname,
1825 workgroup, sizeof(workgroup),
1826 server1, sizeof(server1),
1827 share1, sizeof(share1),
1828 path1, sizeof(path1),
1829 user1, sizeof(user1),
1830 password1, sizeof(password1),
1831 NULL, 0);
1833 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1835 smbc_parse_path(ncontext, nname,
1836 NULL, 0,
1837 server2, sizeof(server2),
1838 share2, sizeof(share2),
1839 path2, sizeof(path2),
1840 user2, sizeof(user2),
1841 password2, sizeof(password2),
1842 NULL, 0);
1844 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1846 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1847 strcmp(user1, user2)) {
1849 /* Can't rename across file systems, or users?? */
1851 errno = EXDEV;
1852 return -1;
1856 srv = smbc_server(ocontext, True,
1857 server1, share1, workgroup, user1, password1);
1858 if (!srv) {
1860 return -1;
1864 /*d_printf(">>>rename: resolving %s\n", path1);*/
1865 if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
1867 d_printf("Could not resolve %s\n", path1);
1868 return -1;
1870 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1871 /*d_printf(">>>rename: resolving %s\n", path2);*/
1872 if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
1874 d_printf("Could not resolve %s\n", path2);
1875 return -1;
1877 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1879 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1880 strcmp(targetcli1->share, targetcli2->share))
1882 /* can't rename across file systems */
1884 errno = EXDEV;
1885 return -1;
1888 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1889 int eno = smbc_errno(ocontext, targetcli1);
1891 if (eno != EEXIST ||
1892 !cli_unlink(targetcli1, targetpath2) ||
1893 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1895 errno = eno;
1896 return -1;
1901 return 0; /* Success */
1906 * A routine to lseek() a file
1909 static off_t
1910 smbc_lseek_ctx(SMBCCTX *context,
1911 SMBCFILE *file,
1912 off_t offset,
1913 int whence)
1915 SMB_OFF_T size;
1916 fstring server, share, user, password;
1917 pstring path, targetpath;
1918 struct cli_state *targetcli;
1920 if (!context || !context->internal ||
1921 !context->internal->_initialized) {
1923 errno = EINVAL;
1924 return -1;
1928 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1930 errno = EBADF;
1931 return -1;
1935 if (!file->file) {
1937 errno = EINVAL;
1938 return -1; /* Can't lseek a dir ... */
1942 switch (whence) {
1943 case SEEK_SET:
1944 file->offset = offset;
1945 break;
1947 case SEEK_CUR:
1948 file->offset += offset;
1949 break;
1951 case SEEK_END:
1952 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1953 if (smbc_parse_path(context, file->fname,
1954 NULL, 0,
1955 server, sizeof(server),
1956 share, sizeof(share),
1957 path, sizeof(path),
1958 user, sizeof(user),
1959 password, sizeof(password),
1960 NULL, 0)) {
1962 errno = EINVAL;
1963 return -1;
1966 /*d_printf(">>>lseek: resolving %s\n", path);*/
1967 if (!cli_resolve_path("", file->srv->cli, path,
1968 &targetcli, targetpath))
1970 d_printf("Could not resolve %s\n", path);
1971 return -1;
1973 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1975 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
1976 &size, NULL, NULL, NULL, NULL, NULL))
1978 SMB_OFF_T b_size = size;
1979 if (!cli_getattrE(targetcli, file->cli_fd,
1980 NULL, &b_size, NULL, NULL, NULL))
1982 errno = EINVAL;
1983 return -1;
1984 } else
1985 size = b_size;
1987 file->offset = size + offset;
1988 break;
1990 default:
1991 errno = EINVAL;
1992 break;
1996 return file->offset;
2001 * Generate an inode number from file name for those things that need it
2004 static ino_t
2005 smbc_inode(SMBCCTX *context,
2006 const char *name)
2009 if (!context || !context->internal ||
2010 !context->internal->_initialized) {
2012 errno = EINVAL;
2013 return -1;
2017 if (!*name) return 2; /* FIXME, why 2 ??? */
2018 return (ino_t)str_checksum(name);
2023 * Routine to put basic stat info into a stat structure ... Used by stat and
2024 * fstat below.
2027 static int
2028 smbc_setup_stat(SMBCCTX *context,
2029 struct stat *st,
2030 char *fname,
2031 SMB_OFF_T size,
2032 int mode)
2035 st->st_mode = 0;
2037 if (IS_DOS_DIR(mode)) {
2038 st->st_mode = SMBC_DIR_MODE;
2039 } else {
2040 st->st_mode = SMBC_FILE_MODE;
2043 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2044 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2045 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2046 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2048 st->st_size = size;
2049 #ifdef HAVE_STAT_ST_BLKSIZE
2050 st->st_blksize = 512;
2051 #endif
2052 #ifdef HAVE_STAT_ST_BLOCKS
2053 st->st_blocks = (size+511)/512;
2054 #endif
2055 st->st_uid = getuid();
2056 st->st_gid = getgid();
2058 if (IS_DOS_DIR(mode)) {
2059 st->st_nlink = 2;
2060 } else {
2061 st->st_nlink = 1;
2064 if (st->st_ino == 0) {
2065 st->st_ino = smbc_inode(context, fname);
2068 return True; /* FIXME: Is this needed ? */
2073 * Routine to stat a file given a name
2076 static int
2077 smbc_stat_ctx(SMBCCTX *context,
2078 const char *fname,
2079 struct stat *st)
2081 SMBCSRV *srv;
2082 fstring server;
2083 fstring share;
2084 fstring user;
2085 fstring password;
2086 fstring workgroup;
2087 pstring path;
2088 struct timespec write_time_ts;
2089 struct timespec access_time_ts;
2090 struct timespec change_time_ts;
2091 SMB_OFF_T size = 0;
2092 uint16 mode = 0;
2093 SMB_INO_T ino = 0;
2095 if (!context || !context->internal ||
2096 !context->internal->_initialized) {
2098 errno = EINVAL; /* Best I can think of ... */
2099 return -1;
2103 if (!fname) {
2105 errno = EINVAL;
2106 return -1;
2110 DEBUG(4, ("smbc_stat(%s)\n", fname));
2112 if (smbc_parse_path(context, fname,
2113 workgroup, sizeof(workgroup),
2114 server, sizeof(server),
2115 share, sizeof(share),
2116 path, sizeof(path),
2117 user, sizeof(user),
2118 password, sizeof(password),
2119 NULL, 0)) {
2120 errno = EINVAL;
2121 return -1;
2124 if (user[0] == (char)0) fstrcpy(user, context->user);
2126 srv = smbc_server(context, True,
2127 server, share, workgroup, user, password);
2129 if (!srv) {
2130 return -1; /* errno set by smbc_server */
2133 if (!smbc_getatr(context, srv, path, &mode, &size,
2134 NULL,
2135 &access_time_ts,
2136 &write_time_ts,
2137 &change_time_ts,
2138 &ino)) {
2140 errno = smbc_errno(context, srv->cli);
2141 return -1;
2145 st->st_ino = ino;
2147 smbc_setup_stat(context, st, path, size, mode);
2149 set_atimespec(st, access_time_ts);
2150 set_ctimespec(st, change_time_ts);
2151 set_mtimespec(st, write_time_ts);
2152 st->st_dev = srv->dev;
2154 return 0;
2159 * Routine to stat a file given an fd
2162 static int
2163 smbc_fstat_ctx(SMBCCTX *context,
2164 SMBCFILE *file,
2165 struct stat *st)
2167 struct timespec change_time_ts;
2168 struct timespec access_time_ts;
2169 struct timespec write_time_ts;
2170 SMB_OFF_T size;
2171 uint16 mode;
2172 fstring server;
2173 fstring share;
2174 fstring user;
2175 fstring password;
2176 pstring path;
2177 pstring targetpath;
2178 struct cli_state *targetcli;
2179 SMB_INO_T ino = 0;
2181 if (!context || !context->internal ||
2182 !context->internal->_initialized) {
2184 errno = EINVAL;
2185 return -1;
2189 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2191 errno = EBADF;
2192 return -1;
2196 if (!file->file) {
2198 return context->fstatdir(context, file, st);
2202 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2203 if (smbc_parse_path(context, file->fname,
2204 NULL, 0,
2205 server, sizeof(server),
2206 share, sizeof(share),
2207 path, sizeof(path),
2208 user, sizeof(user),
2209 password, sizeof(password),
2210 NULL, 0)) {
2211 errno = EINVAL;
2212 return -1;
2215 /*d_printf(">>>fstat: resolving %s\n", path);*/
2216 if (!cli_resolve_path("", file->srv->cli, path,
2217 &targetcli, targetpath))
2219 d_printf("Could not resolve %s\n", path);
2220 return -1;
2222 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2224 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2225 NULL,
2226 &access_time_ts,
2227 &write_time_ts,
2228 &change_time_ts,
2229 &ino)) {
2231 time_t change_time, access_time, write_time;
2233 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2234 &change_time, &access_time, &write_time)) {
2236 errno = EINVAL;
2237 return -1;
2240 change_time_ts = convert_time_t_to_timespec(change_time);
2241 access_time_ts = convert_time_t_to_timespec(access_time);
2242 write_time_ts = convert_time_t_to_timespec(write_time);
2245 st->st_ino = ino;
2247 smbc_setup_stat(context, st, file->fname, size, mode);
2249 set_atimespec(st, access_time_ts);
2250 set_ctimespec(st, change_time_ts);
2251 set_mtimespec(st, write_time_ts);
2252 st->st_dev = file->srv->dev;
2254 return 0;
2259 * Routine to open a directory
2260 * We accept the URL syntax explained in smbc_parse_path(), above.
2263 static void
2264 smbc_remove_dir(SMBCFILE *dir)
2266 struct smbc_dir_list *d,*f;
2268 d = dir->dir_list;
2269 while (d) {
2271 f = d; d = d->next;
2273 SAFE_FREE(f->dirent);
2274 SAFE_FREE(f);
2278 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2282 static int
2283 add_dirent(SMBCFILE *dir,
2284 const char *name,
2285 const char *comment,
2286 uint32 type)
2288 struct smbc_dirent *dirent;
2289 int size;
2290 int name_length = (name == NULL ? 0 : strlen(name));
2291 int comment_len = (comment == NULL ? 0 : strlen(comment));
2294 * Allocate space for the dirent, which must be increased by the
2295 * size of the name and the comment and 1 each for the null terminator.
2298 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2300 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2302 if (!dirent) {
2304 dir->dir_error = ENOMEM;
2305 return -1;
2309 ZERO_STRUCTP(dirent);
2311 if (dir->dir_list == NULL) {
2313 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2314 if (!dir->dir_list) {
2316 SAFE_FREE(dirent);
2317 dir->dir_error = ENOMEM;
2318 return -1;
2321 ZERO_STRUCTP(dir->dir_list);
2323 dir->dir_end = dir->dir_next = dir->dir_list;
2325 else {
2327 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2329 if (!dir->dir_end->next) {
2331 SAFE_FREE(dirent);
2332 dir->dir_error = ENOMEM;
2333 return -1;
2336 ZERO_STRUCTP(dir->dir_end->next);
2338 dir->dir_end = dir->dir_end->next;
2341 dir->dir_end->next = NULL;
2342 dir->dir_end->dirent = dirent;
2344 dirent->smbc_type = type;
2345 dirent->namelen = name_length;
2346 dirent->commentlen = comment_len;
2347 dirent->dirlen = size;
2350 * dirent->namelen + 1 includes the null (no null termination needed)
2351 * Ditto for dirent->commentlen.
2352 * The space for the two null bytes was allocated.
2354 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2355 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2356 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2358 return 0;
2362 static void
2363 list_unique_wg_fn(const char *name,
2364 uint32 type,
2365 const char *comment,
2366 void *state)
2368 SMBCFILE *dir = (SMBCFILE *)state;
2369 struct smbc_dir_list *dir_list;
2370 struct smbc_dirent *dirent;
2371 int dirent_type;
2372 int do_remove = 0;
2374 dirent_type = dir->dir_type;
2376 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2378 /* An error occurred, what do we do? */
2379 /* FIXME: Add some code here */
2382 /* Point to the one just added */
2383 dirent = dir->dir_end->dirent;
2385 /* See if this was a duplicate */
2386 for (dir_list = dir->dir_list;
2387 dir_list != dir->dir_end;
2388 dir_list = dir_list->next) {
2389 if (! do_remove &&
2390 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2391 /* Duplicate. End end of list need to be removed. */
2392 do_remove = 1;
2395 if (do_remove && dir_list->next == dir->dir_end) {
2396 /* Found the end of the list. Remove it. */
2397 dir->dir_end = dir_list;
2398 free(dir_list->next);
2399 free(dirent);
2400 dir_list->next = NULL;
2401 break;
2406 static void
2407 list_fn(const char *name,
2408 uint32 type,
2409 const char *comment,
2410 void *state)
2412 SMBCFILE *dir = (SMBCFILE *)state;
2413 int dirent_type;
2416 * We need to process the type a little ...
2418 * Disk share = 0x00000000
2419 * Print share = 0x00000001
2420 * Comms share = 0x00000002 (obsolete?)
2421 * IPC$ share = 0x00000003
2423 * administrative shares:
2424 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2427 if (dir->dir_type == SMBC_FILE_SHARE) {
2429 switch (type) {
2430 case 0 | 0x80000000:
2431 case 0:
2432 dirent_type = SMBC_FILE_SHARE;
2433 break;
2435 case 1:
2436 dirent_type = SMBC_PRINTER_SHARE;
2437 break;
2439 case 2:
2440 dirent_type = SMBC_COMMS_SHARE;
2441 break;
2443 case 3 | 0x80000000:
2444 case 3:
2445 dirent_type = SMBC_IPC_SHARE;
2446 break;
2448 default:
2449 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2450 break;
2453 else {
2454 dirent_type = dir->dir_type;
2457 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2459 /* An error occurred, what do we do? */
2460 /* FIXME: Add some code here */
2465 static void
2466 dir_list_fn(const char *mnt,
2467 file_info *finfo,
2468 const char *mask,
2469 void *state)
2472 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2473 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2475 /* Handle an error ... */
2477 /* FIXME: Add some code ... */
2483 static int
2484 net_share_enum_rpc(struct cli_state *cli,
2485 void (*fn)(const char *name,
2486 uint32 type,
2487 const char *comment,
2488 void *state),
2489 void *state)
2491 int i;
2492 NTSTATUS result;
2493 uint32 enum_hnd;
2494 uint32 info_level = 1;
2495 uint32 preferred_len = 0xffffffff;
2496 struct srvsvc_NetShareCtr1 ctr1;
2497 union srvsvc_NetShareCtr ctr;
2498 void *mem_ctx;
2499 struct rpc_pipe_client *pipe_hnd;
2500 uint32 numentries;
2501 NTSTATUS nt_status;
2503 /* Open the server service pipe */
2504 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2505 if (!pipe_hnd) {
2506 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2507 return -1;
2510 /* Allocate a context for parsing and for the entries in "ctr" */
2511 mem_ctx = talloc_init("libsmbclient: net_share_enum_rpc");
2512 if (mem_ctx == NULL) {
2513 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2514 cli_rpc_pipe_close(pipe_hnd);
2515 return -1;
2518 ZERO_STRUCT(ctr1);
2519 ctr.ctr1 = &ctr1;
2521 /* Issue the NetShareEnum RPC call and retrieve the response */
2522 enum_hnd = 0;
2523 result = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
2524 &info_level, &ctr, preferred_len,
2525 &numentries, &enum_hnd);
2527 /* Was it successful? */
2528 if (!NT_STATUS_IS_OK(result) || numentries == 0) {
2529 /* Nope. Go clean up. */
2530 goto done;
2533 /* For each returned entry... */
2534 for (i = 0; i < numentries; i++) {
2536 /* Add this share to the list */
2537 (*fn)(ctr.ctr1->array[i].name,
2538 ctr.ctr1->array[i].type,
2539 ctr.ctr1->array[i].comment, state);
2542 done:
2543 /* Close the server service pipe */
2544 cli_rpc_pipe_close(pipe_hnd);
2546 /* Free all memory which was allocated for this request */
2547 TALLOC_FREE(mem_ctx);
2549 /* Tell 'em if it worked */
2550 return NT_STATUS_IS_OK(result) ? 0 : -1;
2555 static SMBCFILE *
2556 smbc_opendir_ctx(SMBCCTX *context,
2557 const char *fname)
2559 int saved_errno;
2560 fstring server, share, user, password, options;
2561 pstring workgroup;
2562 pstring path;
2563 uint16 mode;
2564 char *p;
2565 SMBCSRV *srv = NULL;
2566 SMBCFILE *dir = NULL;
2567 struct _smbc_callbacks *cb;
2568 struct in_addr rem_ip;
2570 if (!context || !context->internal ||
2571 !context->internal->_initialized) {
2572 DEBUG(4, ("no valid context\n"));
2573 errno = EINVAL + 8192;
2574 return NULL;
2578 if (!fname) {
2579 DEBUG(4, ("no valid fname\n"));
2580 errno = EINVAL + 8193;
2581 return NULL;
2584 if (smbc_parse_path(context, fname,
2585 workgroup, sizeof(workgroup),
2586 server, sizeof(server),
2587 share, sizeof(share),
2588 path, sizeof(path),
2589 user, sizeof(user),
2590 password, sizeof(password),
2591 options, sizeof(options))) {
2592 DEBUG(4, ("no valid path\n"));
2593 errno = EINVAL + 8194;
2594 return NULL;
2597 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2598 "path='%s' options='%s'\n",
2599 fname, server, share, path, options));
2601 /* Ensure the options are valid */
2602 if (smbc_check_options(server, share, path, options)) {
2603 DEBUG(4, ("unacceptable options (%s)\n", options));
2604 errno = EINVAL + 8195;
2605 return NULL;
2608 if (user[0] == (char)0) fstrcpy(user, context->user);
2610 dir = SMB_MALLOC_P(SMBCFILE);
2612 if (!dir) {
2614 errno = ENOMEM;
2615 return NULL;
2619 ZERO_STRUCTP(dir);
2621 dir->cli_fd = 0;
2622 dir->fname = SMB_STRDUP(fname);
2623 dir->srv = NULL;
2624 dir->offset = 0;
2625 dir->file = False;
2626 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2628 if (server[0] == (char)0) {
2630 int i;
2631 int count;
2632 int max_lmb_count;
2633 struct ip_service *ip_list;
2634 struct ip_service server_addr;
2635 struct user_auth_info u_info;
2636 struct cli_state *cli;
2638 if (share[0] != (char)0 || path[0] != (char)0) {
2640 errno = EINVAL + 8196;
2641 if (dir) {
2642 SAFE_FREE(dir->fname);
2643 SAFE_FREE(dir);
2645 return NULL;
2648 /* Determine how many local master browsers to query */
2649 max_lmb_count = (context->options.browse_max_lmb_count == 0
2650 ? INT_MAX
2651 : context->options.browse_max_lmb_count);
2653 pstrcpy(u_info.username, user);
2654 pstrcpy(u_info.password, password);
2657 * We have server and share and path empty but options
2658 * requesting that we scan all master browsers for their list
2659 * of workgroups/domains. This implies that we must first try
2660 * broadcast queries to find all master browsers, and if that
2661 * doesn't work, then try our other methods which return only
2662 * a single master browser.
2665 ip_list = NULL;
2666 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
2668 SAFE_FREE(ip_list);
2670 if (!find_master_ip(workgroup, &server_addr.ip)) {
2672 if (dir) {
2673 SAFE_FREE(dir->fname);
2674 SAFE_FREE(dir);
2676 errno = ENOENT;
2677 return NULL;
2680 ip_list = &server_addr;
2681 count = 1;
2684 for (i = 0; i < count && i < max_lmb_count; i++) {
2685 DEBUG(99, ("Found master browser %d of %d: %s\n",
2686 i+1, MAX(count, max_lmb_count),
2687 inet_ntoa(ip_list[i].ip)));
2689 cli = get_ipc_connect_master_ip(&ip_list[i],
2690 workgroup, &u_info);
2691 /* cli == NULL is the master browser refused to talk or
2692 could not be found */
2693 if ( !cli )
2694 continue;
2696 fstrcpy(server, cli->desthost);
2697 cli_shutdown(cli);
2699 DEBUG(4, ("using workgroup %s %s\n",
2700 workgroup, server));
2703 * For each returned master browser IP address, get a
2704 * connection to IPC$ on the server if we do not
2705 * already have one, and determine the
2706 * workgroups/domains that it knows about.
2709 srv = smbc_server(context, True, server, "IPC$",
2710 workgroup, user, password);
2711 if (!srv) {
2712 continue;
2715 dir->srv = srv;
2716 dir->dir_type = SMBC_WORKGROUP;
2718 /* Now, list the stuff ... */
2720 if (!cli_NetServerEnum(srv->cli,
2721 workgroup,
2722 SV_TYPE_DOMAIN_ENUM,
2723 list_unique_wg_fn,
2724 (void *)dir)) {
2725 continue;
2729 SAFE_FREE(ip_list);
2730 } else {
2732 * Server not an empty string ... Check the rest and see what
2733 * gives
2735 if (*share == '\0') {
2736 if (*path != '\0') {
2738 /* Should not have empty share with path */
2739 errno = EINVAL + 8197;
2740 if (dir) {
2741 SAFE_FREE(dir->fname);
2742 SAFE_FREE(dir);
2744 return NULL;
2749 * We don't know if <server> is really a server name
2750 * or is a workgroup/domain name. If we already have
2751 * a server structure for it, we'll use it.
2752 * Otherwise, check to see if <server><1D>,
2753 * <server><1B>, or <server><20> translates. We check
2754 * to see if <server> is an IP address first.
2758 * See if we have an existing server. Do not
2759 * establish a connection if one does not already
2760 * exist.
2762 srv = smbc_server(context, False, server, "IPC$",
2763 workgroup, user, password);
2766 * If no existing server and not an IP addr, look for
2767 * LMB or DMB
2769 if (!srv &&
2770 !is_ipaddress(server) &&
2771 (resolve_name(server, &rem_ip, 0x1d) || /* LMB */
2772 resolve_name(server, &rem_ip, 0x1b) )) { /* DMB */
2774 fstring buserver;
2776 dir->dir_type = SMBC_SERVER;
2779 * Get the backup list ...
2781 if (!name_status_find(server, 0, 0,
2782 rem_ip, buserver)) {
2784 DEBUG(0, ("Could not get name of "
2785 "local/domain master browser "
2786 "for server %s\n", server));
2787 if (dir) {
2788 SAFE_FREE(dir->fname);
2789 SAFE_FREE(dir);
2791 errno = EPERM;
2792 return NULL;
2797 * Get a connection to IPC$ on the server if
2798 * we do not already have one
2800 srv = smbc_server(context, True,
2801 buserver, "IPC$",
2802 workgroup, user, password);
2803 if (!srv) {
2804 DEBUG(0, ("got no contact to IPC$\n"));
2805 if (dir) {
2806 SAFE_FREE(dir->fname);
2807 SAFE_FREE(dir);
2809 return NULL;
2813 dir->srv = srv;
2815 /* Now, list the servers ... */
2816 if (!cli_NetServerEnum(srv->cli, server,
2817 0x0000FFFE, list_fn,
2818 (void *)dir)) {
2820 if (dir) {
2821 SAFE_FREE(dir->fname);
2822 SAFE_FREE(dir);
2824 return NULL;
2826 } else if (srv ||
2827 (resolve_name(server, &rem_ip, 0x20))) {
2829 /* If we hadn't found the server, get one now */
2830 if (!srv) {
2831 srv = smbc_server(context, True,
2832 server, "IPC$",
2833 workgroup,
2834 user, password);
2837 if (!srv) {
2838 if (dir) {
2839 SAFE_FREE(dir->fname);
2840 SAFE_FREE(dir);
2842 return NULL;
2846 dir->dir_type = SMBC_FILE_SHARE;
2847 dir->srv = srv;
2849 /* List the shares ... */
2851 if (net_share_enum_rpc(
2852 srv->cli,
2853 list_fn,
2854 (void *) dir) < 0 &&
2855 cli_RNetShareEnum(
2856 srv->cli,
2857 list_fn,
2858 (void *)dir) < 0) {
2860 errno = cli_errno(srv->cli);
2861 if (dir) {
2862 SAFE_FREE(dir->fname);
2863 SAFE_FREE(dir);
2865 return NULL;
2868 } else {
2869 /* Neither the workgroup nor server exists */
2870 errno = ECONNREFUSED;
2871 if (dir) {
2872 SAFE_FREE(dir->fname);
2873 SAFE_FREE(dir);
2875 return NULL;
2879 else {
2881 * The server and share are specified ... work from
2882 * there ...
2884 pstring targetpath;
2885 struct cli_state *targetcli;
2887 /* We connect to the server and list the directory */
2888 dir->dir_type = SMBC_FILE_SHARE;
2890 srv = smbc_server(context, True, server, share,
2891 workgroup, user, password);
2893 if (!srv) {
2895 if (dir) {
2896 SAFE_FREE(dir->fname);
2897 SAFE_FREE(dir);
2899 return NULL;
2903 dir->srv = srv;
2905 /* Now, list the files ... */
2907 p = path + strlen(path);
2908 pstrcat(path, "\\*");
2910 if (!cli_resolve_path("", srv->cli, path,
2911 &targetcli, targetpath))
2913 d_printf("Could not resolve %s\n", path);
2914 if (dir) {
2915 SAFE_FREE(dir->fname);
2916 SAFE_FREE(dir);
2918 return NULL;
2921 if (cli_list(targetcli, targetpath,
2922 aDIR | aSYSTEM | aHIDDEN,
2923 dir_list_fn, (void *)dir) < 0) {
2925 if (dir) {
2926 SAFE_FREE(dir->fname);
2927 SAFE_FREE(dir);
2929 saved_errno = smbc_errno(context, targetcli);
2931 if (saved_errno == EINVAL) {
2933 * See if they asked to opendir something
2934 * other than a directory. If so, the
2935 * converted error value we got would have
2936 * been EINVAL rather than ENOTDIR.
2938 *p = '\0'; /* restore original path */
2940 if (smbc_getatr(context, srv, path,
2941 &mode, NULL,
2942 NULL, NULL, NULL, NULL,
2943 NULL) &&
2944 ! IS_DOS_DIR(mode)) {
2946 /* It is. Correct the error value */
2947 saved_errno = ENOTDIR;
2952 * If there was an error and the server is no
2953 * good any more...
2955 cb = &context->callbacks;
2956 if (cli_is_error(targetcli) &&
2957 cb->check_server_fn(context, srv)) {
2959 /* ... then remove it. */
2960 if (cb->remove_unused_server_fn(context,
2961 srv)) {
2963 * We could not remove the server
2964 * completely, remove it from the
2965 * cache so we will not get it
2966 * again. It will be removed when the
2967 * last file/dir is closed.
2969 cb->remove_cached_srv_fn(context, srv);
2973 errno = saved_errno;
2974 return NULL;
2980 DLIST_ADD(context->internal->_files, dir);
2981 return dir;
2986 * Routine to close a directory
2989 static int
2990 smbc_closedir_ctx(SMBCCTX *context,
2991 SMBCFILE *dir)
2994 if (!context || !context->internal ||
2995 !context->internal->_initialized) {
2997 errno = EINVAL;
2998 return -1;
3002 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3004 errno = EBADF;
3005 return -1;
3009 smbc_remove_dir(dir); /* Clean it up */
3011 DLIST_REMOVE(context->internal->_files, dir);
3013 if (dir) {
3015 SAFE_FREE(dir->fname);
3016 SAFE_FREE(dir); /* Free the space too */
3019 return 0;
3023 static void
3024 smbc_readdir_internal(SMBCCTX * context,
3025 struct smbc_dirent *dest,
3026 struct smbc_dirent *src,
3027 int max_namebuf_len)
3029 if (context->options.urlencode_readdir_entries) {
3031 /* url-encode the name. get back remaining buffer space */
3032 max_namebuf_len =
3033 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3035 /* We now know the name length */
3036 dest->namelen = strlen(dest->name);
3038 /* Save the pointer to the beginning of the comment */
3039 dest->comment = dest->name + dest->namelen + 1;
3041 /* Copy the comment */
3042 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3043 dest->comment[max_namebuf_len - 1] = '\0';
3045 /* Save other fields */
3046 dest->smbc_type = src->smbc_type;
3047 dest->commentlen = strlen(dest->comment);
3048 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3049 (char *) dest);
3050 } else {
3052 /* No encoding. Just copy the entry as is. */
3053 memcpy(dest, src, src->dirlen);
3054 dest->comment = (char *)(&dest->name + src->namelen + 1);
3060 * Routine to get a directory entry
3063 struct smbc_dirent *
3064 smbc_readdir_ctx(SMBCCTX *context,
3065 SMBCFILE *dir)
3067 int maxlen;
3068 struct smbc_dirent *dirp, *dirent;
3070 /* Check that all is ok first ... */
3072 if (!context || !context->internal ||
3073 !context->internal->_initialized) {
3075 errno = EINVAL;
3076 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3077 return NULL;
3081 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3083 errno = EBADF;
3084 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3085 return NULL;
3089 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3091 errno = ENOTDIR;
3092 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3093 return NULL;
3097 if (!dir->dir_next) {
3098 return NULL;
3101 dirent = dir->dir_next->dirent;
3102 if (!dirent) {
3104 errno = ENOENT;
3105 return NULL;
3109 dirp = (struct smbc_dirent *)context->internal->_dirent;
3110 maxlen = (sizeof(context->internal->_dirent) -
3111 sizeof(struct smbc_dirent));
3113 smbc_readdir_internal(context, dirp, dirent, maxlen);
3115 dir->dir_next = dir->dir_next->next;
3117 return dirp;
3121 * Routine to get directory entries
3124 static int
3125 smbc_getdents_ctx(SMBCCTX *context,
3126 SMBCFILE *dir,
3127 struct smbc_dirent *dirp,
3128 int count)
3130 int rem = count;
3131 int reqd;
3132 int maxlen;
3133 char *ndir = (char *)dirp;
3134 struct smbc_dir_list *dirlist;
3136 /* Check that all is ok first ... */
3138 if (!context || !context->internal ||
3139 !context->internal->_initialized) {
3141 errno = EINVAL;
3142 return -1;
3146 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3148 errno = EBADF;
3149 return -1;
3153 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3155 errno = ENOTDIR;
3156 return -1;
3161 * Now, retrieve the number of entries that will fit in what was passed
3162 * We have to figure out if the info is in the list, or we need to
3163 * send a request to the server to get the info.
3166 while ((dirlist = dir->dir_next)) {
3167 struct smbc_dirent *dirent;
3169 if (!dirlist->dirent) {
3171 errno = ENOENT; /* Bad error */
3172 return -1;
3176 /* Do urlencoding of next entry, if so selected */
3177 dirent = (struct smbc_dirent *)context->internal->_dirent;
3178 maxlen = (sizeof(context->internal->_dirent) -
3179 sizeof(struct smbc_dirent));
3180 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3182 reqd = dirent->dirlen;
3184 if (rem < reqd) {
3186 if (rem < count) { /* We managed to copy something */
3188 errno = 0;
3189 return count - rem;
3192 else { /* Nothing copied ... */
3194 errno = EINVAL; /* Not enough space ... */
3195 return -1;
3201 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3203 ((struct smbc_dirent *)ndir)->comment =
3204 (char *)(&((struct smbc_dirent *)ndir)->name +
3205 dirent->namelen +
3208 ndir += reqd;
3210 rem -= reqd;
3212 dir->dir_next = dirlist = dirlist -> next;
3215 if (rem == count)
3216 return 0;
3217 else
3218 return count - rem;
3223 * Routine to create a directory ...
3226 static int
3227 smbc_mkdir_ctx(SMBCCTX *context,
3228 const char *fname,
3229 mode_t mode)
3231 SMBCSRV *srv;
3232 fstring server;
3233 fstring share;
3234 fstring user;
3235 fstring password;
3236 fstring workgroup;
3237 pstring path, targetpath;
3238 struct cli_state *targetcli;
3240 if (!context || !context->internal ||
3241 !context->internal->_initialized) {
3243 errno = EINVAL;
3244 return -1;
3248 if (!fname) {
3250 errno = EINVAL;
3251 return -1;
3255 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3257 if (smbc_parse_path(context, fname,
3258 workgroup, sizeof(workgroup),
3259 server, sizeof(server),
3260 share, sizeof(share),
3261 path, sizeof(path),
3262 user, sizeof(user),
3263 password, sizeof(password),
3264 NULL, 0)) {
3265 errno = EINVAL;
3266 return -1;
3269 if (user[0] == (char)0) fstrcpy(user, context->user);
3271 srv = smbc_server(context, True,
3272 server, share, workgroup, user, password);
3274 if (!srv) {
3276 return -1; /* errno set by smbc_server */
3280 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3281 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3283 d_printf("Could not resolve %s\n", path);
3284 return -1;
3286 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3288 if (!cli_mkdir(targetcli, targetpath)) {
3290 errno = smbc_errno(context, targetcli);
3291 return -1;
3295 return 0;
3300 * Our list function simply checks to see if a directory is not empty
3303 static int smbc_rmdir_dirempty = True;
3305 static void
3306 rmdir_list_fn(const char *mnt,
3307 file_info *finfo,
3308 const char *mask,
3309 void *state)
3311 if (strncmp(finfo->name, ".", 1) != 0 &&
3312 strncmp(finfo->name, "..", 2) != 0) {
3314 smbc_rmdir_dirempty = False;
3319 * Routine to remove a directory
3322 static int
3323 smbc_rmdir_ctx(SMBCCTX *context,
3324 const char *fname)
3326 SMBCSRV *srv;
3327 fstring server;
3328 fstring share;
3329 fstring user;
3330 fstring password;
3331 fstring workgroup;
3332 pstring path;
3333 pstring targetpath;
3334 struct cli_state *targetcli;
3336 if (!context || !context->internal ||
3337 !context->internal->_initialized) {
3339 errno = EINVAL;
3340 return -1;
3344 if (!fname) {
3346 errno = EINVAL;
3347 return -1;
3351 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3353 if (smbc_parse_path(context, fname,
3354 workgroup, sizeof(workgroup),
3355 server, sizeof(server),
3356 share, sizeof(share),
3357 path, sizeof(path),
3358 user, sizeof(user),
3359 password, sizeof(password),
3360 NULL, 0))
3362 errno = EINVAL;
3363 return -1;
3366 if (user[0] == (char)0) fstrcpy(user, context->user);
3368 srv = smbc_server(context, True,
3369 server, share, workgroup, user, password);
3371 if (!srv) {
3373 return -1; /* errno set by smbc_server */
3377 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3378 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3380 d_printf("Could not resolve %s\n", path);
3381 return -1;
3383 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3386 if (!cli_rmdir(targetcli, targetpath)) {
3388 errno = smbc_errno(context, targetcli);
3390 if (errno == EACCES) { /* Check if the dir empty or not */
3392 /* Local storage to avoid buffer overflows */
3393 pstring lpath;
3395 smbc_rmdir_dirempty = True; /* Make this so ... */
3397 pstrcpy(lpath, targetpath);
3398 pstrcat(lpath, "\\*");
3400 if (cli_list(targetcli, lpath,
3401 aDIR | aSYSTEM | aHIDDEN,
3402 rmdir_list_fn, NULL) < 0) {
3404 /* Fix errno to ignore latest error ... */
3405 DEBUG(5, ("smbc_rmdir: "
3406 "cli_list returned an error: %d\n",
3407 smbc_errno(context, targetcli)));
3408 errno = EACCES;
3412 if (smbc_rmdir_dirempty)
3413 errno = EACCES;
3414 else
3415 errno = ENOTEMPTY;
3419 return -1;
3423 return 0;
3428 * Routine to return the current directory position
3431 static off_t
3432 smbc_telldir_ctx(SMBCCTX *context,
3433 SMBCFILE *dir)
3435 off_t ret_val; /* Squash warnings about cast */
3437 if (!context || !context->internal ||
3438 !context->internal->_initialized) {
3440 errno = EINVAL;
3441 return -1;
3445 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3447 errno = EBADF;
3448 return -1;
3452 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3454 errno = ENOTDIR;
3455 return -1;
3460 * We return the pointer here as the offset
3462 ret_val = (off_t)(long)dir->dir_next;
3463 return ret_val;
3468 * A routine to run down the list and see if the entry is OK
3471 struct smbc_dir_list *
3472 smbc_check_dir_ent(struct smbc_dir_list *list,
3473 struct smbc_dirent *dirent)
3476 /* Run down the list looking for what we want */
3478 if (dirent) {
3480 struct smbc_dir_list *tmp = list;
3482 while (tmp) {
3484 if (tmp->dirent == dirent)
3485 return tmp;
3487 tmp = tmp->next;
3493 return NULL; /* Not found, or an error */
3499 * Routine to seek on a directory
3502 static int
3503 smbc_lseekdir_ctx(SMBCCTX *context,
3504 SMBCFILE *dir,
3505 off_t offset)
3507 long int l_offset = offset; /* Handle problems of size */
3508 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3509 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3511 if (!context || !context->internal ||
3512 !context->internal->_initialized) {
3514 errno = EINVAL;
3515 return -1;
3519 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3521 errno = ENOTDIR;
3522 return -1;
3526 /* Now, check what we were passed and see if it is OK ... */
3528 if (dirent == NULL) { /* Seek to the begining of the list */
3530 dir->dir_next = dir->dir_list;
3531 return 0;
3535 /* Now, run down the list and make sure that the entry is OK */
3536 /* This may need to be changed if we change the format of the list */
3538 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3540 errno = EINVAL; /* Bad entry */
3541 return -1;
3545 dir->dir_next = list_ent;
3547 return 0;
3552 * Routine to fstat a dir
3555 static int
3556 smbc_fstatdir_ctx(SMBCCTX *context,
3557 SMBCFILE *dir,
3558 struct stat *st)
3561 if (!context || !context->internal ||
3562 !context->internal->_initialized) {
3564 errno = EINVAL;
3565 return -1;
3569 /* No code yet ... */
3571 return 0;
3575 static int
3576 smbc_chmod_ctx(SMBCCTX *context,
3577 const char *fname,
3578 mode_t newmode)
3580 SMBCSRV *srv;
3581 fstring server;
3582 fstring share;
3583 fstring user;
3584 fstring password;
3585 fstring workgroup;
3586 pstring path;
3587 uint16 mode;
3589 if (!context || !context->internal ||
3590 !context->internal->_initialized) {
3592 errno = EINVAL; /* Best I can think of ... */
3593 return -1;
3597 if (!fname) {
3599 errno = EINVAL;
3600 return -1;
3604 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3606 if (smbc_parse_path(context, fname,
3607 workgroup, sizeof(workgroup),
3608 server, sizeof(server),
3609 share, sizeof(share),
3610 path, sizeof(path),
3611 user, sizeof(user),
3612 password, sizeof(password),
3613 NULL, 0)) {
3614 errno = EINVAL;
3615 return -1;
3618 if (user[0] == (char)0) fstrcpy(user, context->user);
3620 srv = smbc_server(context, True,
3621 server, share, workgroup, user, password);
3623 if (!srv) {
3624 return -1; /* errno set by smbc_server */
3627 mode = 0;
3629 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3630 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3631 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3632 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3634 if (!cli_setatr(srv->cli, path, mode, 0)) {
3635 errno = smbc_errno(context, srv->cli);
3636 return -1;
3639 return 0;
3642 static int
3643 smbc_utimes_ctx(SMBCCTX *context,
3644 const char *fname,
3645 struct timeval *tbuf)
3647 SMBCSRV *srv;
3648 fstring server;
3649 fstring share;
3650 fstring user;
3651 fstring password;
3652 fstring workgroup;
3653 pstring path;
3654 time_t access_time;
3655 time_t write_time;
3657 if (!context || !context->internal ||
3658 !context->internal->_initialized) {
3660 errno = EINVAL; /* Best I can think of ... */
3661 return -1;
3665 if (!fname) {
3667 errno = EINVAL;
3668 return -1;
3672 if (tbuf == NULL) {
3673 access_time = write_time = time(NULL);
3674 } else {
3675 access_time = tbuf[0].tv_sec;
3676 write_time = tbuf[1].tv_sec;
3679 if (DEBUGLVL(4))
3681 char *p;
3682 char atimebuf[32];
3683 char mtimebuf[32];
3685 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3686 atimebuf[sizeof(atimebuf) - 1] = '\0';
3687 if ((p = strchr(atimebuf, '\n')) != NULL) {
3688 *p = '\0';
3691 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3692 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3693 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3694 *p = '\0';
3697 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3698 fname, atimebuf, mtimebuf);
3701 if (smbc_parse_path(context, fname,
3702 workgroup, sizeof(workgroup),
3703 server, sizeof(server),
3704 share, sizeof(share),
3705 path, sizeof(path),
3706 user, sizeof(user),
3707 password, sizeof(password),
3708 NULL, 0)) {
3709 errno = EINVAL;
3710 return -1;
3713 if (user[0] == (char)0) fstrcpy(user, context->user);
3715 srv = smbc_server(context, True,
3716 server, share, workgroup, user, password);
3718 if (!srv) {
3719 return -1; /* errno set by smbc_server */
3722 if (!smbc_setatr(context, srv, path,
3723 0, access_time, write_time, 0, 0)) {
3724 return -1; /* errno set by smbc_setatr */
3727 return 0;
3731 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
3732 However NT4 gives a "The information may have been modified by a
3733 computer running Windows NT 5.0" if denied ACEs do not appear before
3734 allowed ACEs. */
3736 static int
3737 ace_compare(SEC_ACE *ace1,
3738 SEC_ACE *ace2)
3740 if (sec_ace_equal(ace1, ace2))
3741 return 0;
3743 if (ace1->type != ace2->type)
3744 return ace2->type - ace1->type;
3746 if (sid_compare(&ace1->trustee, &ace2->trustee))
3747 return sid_compare(&ace1->trustee, &ace2->trustee);
3749 if (ace1->flags != ace2->flags)
3750 return ace1->flags - ace2->flags;
3752 if (ace1->access_mask != ace2->access_mask)
3753 return ace1->access_mask - ace2->access_mask;
3755 if (ace1->size != ace2->size)
3756 return ace1->size - ace2->size;
3758 return memcmp(ace1, ace2, sizeof(SEC_ACE));
3762 static void
3763 sort_acl(SEC_ACL *the_acl)
3765 uint32 i;
3766 if (!the_acl) return;
3768 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
3769 QSORT_CAST ace_compare);
3771 for (i=1;i<the_acl->num_aces;) {
3772 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
3773 int j;
3774 for (j=i; j<the_acl->num_aces-1; j++) {
3775 the_acl->aces[j] = the_acl->aces[j+1];
3777 the_acl->num_aces--;
3778 } else {
3779 i++;
3784 /* convert a SID to a string, either numeric or username/group */
3785 static void
3786 convert_sid_to_string(struct cli_state *ipc_cli,
3787 POLICY_HND *pol,
3788 fstring str,
3789 BOOL numeric,
3790 DOM_SID *sid)
3792 char **domains = NULL;
3793 char **names = NULL;
3794 enum lsa_SidType *types = NULL;
3795 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3796 sid_to_string(str, sid);
3798 if (numeric) {
3799 return; /* no lookup desired */
3802 if (!pipe_hnd) {
3803 return;
3806 /* Ask LSA to convert the sid to a name */
3808 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ipc_cli->mem_ctx,
3809 pol, 1, sid, &domains,
3810 &names, &types)) ||
3811 !domains || !domains[0] || !names || !names[0]) {
3812 return;
3815 /* Converted OK */
3817 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3818 domains[0], lp_winbind_separator(),
3819 names[0]);
3822 /* convert a string to a SID, either numeric or username/group */
3823 static BOOL
3824 convert_string_to_sid(struct cli_state *ipc_cli,
3825 POLICY_HND *pol,
3826 BOOL numeric,
3827 DOM_SID *sid,
3828 const char *str)
3830 enum lsa_SidType *types = NULL;
3831 DOM_SID *sids = NULL;
3832 BOOL result = True;
3833 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3835 if (!pipe_hnd) {
3836 return False;
3839 if (numeric) {
3840 if (strncmp(str, "S-", 2) == 0) {
3841 return string_to_sid(sid, str);
3844 result = False;
3845 goto done;
3848 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx,
3849 pol, 1, &str, NULL, &sids,
3850 &types))) {
3851 result = False;
3852 goto done;
3855 sid_copy(sid, &sids[0]);
3856 done:
3858 return result;
3862 /* parse an ACE in the same format as print_ace() */
3863 static BOOL
3864 parse_ace(struct cli_state *ipc_cli,
3865 POLICY_HND *pol,
3866 SEC_ACE *ace,
3867 BOOL numeric,
3868 char *str)
3870 char *p;
3871 const char *cp;
3872 fstring tok;
3873 unsigned int atype;
3874 unsigned int aflags;
3875 unsigned int amask;
3876 DOM_SID sid;
3877 SEC_ACCESS mask;
3878 const struct perm_value *v;
3879 struct perm_value {
3880 const char *perm;
3881 uint32 mask;
3884 /* These values discovered by inspection */
3885 static const struct perm_value special_values[] = {
3886 { "R", 0x00120089 },
3887 { "W", 0x00120116 },
3888 { "X", 0x001200a0 },
3889 { "D", 0x00010000 },
3890 { "P", 0x00040000 },
3891 { "O", 0x00080000 },
3892 { NULL, 0 },
3895 static const struct perm_value standard_values[] = {
3896 { "READ", 0x001200a9 },
3897 { "CHANGE", 0x001301bf },
3898 { "FULL", 0x001f01ff },
3899 { NULL, 0 },
3903 ZERO_STRUCTP(ace);
3904 p = strchr_m(str,':');
3905 if (!p) return False;
3906 *p = '\0';
3907 p++;
3908 /* Try to parse numeric form */
3910 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3911 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3912 goto done;
3915 /* Try to parse text form */
3917 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3918 return False;
3921 cp = p;
3922 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3923 return False;
3926 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3927 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3928 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3929 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3930 } else {
3931 return False;
3934 /* Only numeric form accepted for flags at present */
3936 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3937 sscanf(tok, "%i", &aflags))) {
3938 return False;
3941 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3942 return False;
3945 if (strncmp(tok, "0x", 2) == 0) {
3946 if (sscanf(tok, "%i", &amask) != 1) {
3947 return False;
3949 goto done;
3952 for (v = standard_values; v->perm; v++) {
3953 if (strcmp(tok, v->perm) == 0) {
3954 amask = v->mask;
3955 goto done;
3959 p = tok;
3961 while(*p) {
3962 BOOL found = False;
3964 for (v = special_values; v->perm; v++) {
3965 if (v->perm[0] == *p) {
3966 amask |= v->mask;
3967 found = True;
3971 if (!found) return False;
3972 p++;
3975 if (*p) {
3976 return False;
3979 done:
3980 mask = amask;
3981 init_sec_ace(ace, &sid, atype, mask, aflags);
3982 return True;
3985 /* add an ACE to a list of ACEs in a SEC_ACL */
3986 static BOOL
3987 add_ace(SEC_ACL **the_acl,
3988 SEC_ACE *ace,
3989 TALLOC_CTX *ctx)
3991 SEC_ACL *newacl;
3992 SEC_ACE *aces;
3994 if (! *the_acl) {
3995 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
3996 return True;
3999 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4000 return False;
4002 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4003 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4004 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4005 1+(*the_acl)->num_aces, aces);
4006 SAFE_FREE(aces);
4007 (*the_acl) = newacl;
4008 return True;
4012 /* parse a ascii version of a security descriptor */
4013 static SEC_DESC *
4014 sec_desc_parse(TALLOC_CTX *ctx,
4015 struct cli_state *ipc_cli,
4016 POLICY_HND *pol,
4017 BOOL numeric,
4018 char *str)
4020 const char *p = str;
4021 fstring tok;
4022 SEC_DESC *ret = NULL;
4023 size_t sd_size;
4024 DOM_SID *grp_sid=NULL;
4025 DOM_SID *owner_sid=NULL;
4026 SEC_ACL *dacl=NULL;
4027 int revision=1;
4029 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4031 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4032 revision = strtol(tok+9, NULL, 16);
4033 continue;
4036 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4037 if (owner_sid) {
4038 DEBUG(5, ("OWNER specified more than once!\n"));
4039 goto done;
4041 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4042 if (!owner_sid ||
4043 !convert_string_to_sid(ipc_cli, pol,
4044 numeric,
4045 owner_sid, tok+6)) {
4046 DEBUG(5, ("Failed to parse owner sid\n"));
4047 goto done;
4049 continue;
4052 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4053 if (owner_sid) {
4054 DEBUG(5, ("OWNER specified more than once!\n"));
4055 goto done;
4057 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4058 if (!owner_sid ||
4059 !convert_string_to_sid(ipc_cli, pol,
4060 False,
4061 owner_sid, tok+7)) {
4062 DEBUG(5, ("Failed to parse owner sid\n"));
4063 goto done;
4065 continue;
4068 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4069 if (grp_sid) {
4070 DEBUG(5, ("GROUP specified more than once!\n"));
4071 goto done;
4073 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4074 if (!grp_sid ||
4075 !convert_string_to_sid(ipc_cli, pol,
4076 numeric,
4077 grp_sid, tok+6)) {
4078 DEBUG(5, ("Failed to parse group sid\n"));
4079 goto done;
4081 continue;
4084 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4085 if (grp_sid) {
4086 DEBUG(5, ("GROUP specified more than once!\n"));
4087 goto done;
4089 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4090 if (!grp_sid ||
4091 !convert_string_to_sid(ipc_cli, pol,
4092 False,
4093 grp_sid, tok+6)) {
4094 DEBUG(5, ("Failed to parse group sid\n"));
4095 goto done;
4097 continue;
4100 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4101 SEC_ACE ace;
4102 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4103 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4104 goto done;
4106 if(!add_ace(&dacl, &ace, ctx)) {
4107 DEBUG(5, ("Failed to add ACL %s\n", tok));
4108 goto done;
4110 continue;
4113 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4114 SEC_ACE ace;
4115 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4116 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4117 goto done;
4119 if(!add_ace(&dacl, &ace, ctx)) {
4120 DEBUG(5, ("Failed to add ACL %s\n", tok));
4121 goto done;
4123 continue;
4126 DEBUG(5, ("Failed to parse security descriptor\n"));
4127 goto done;
4130 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4131 owner_sid, grp_sid, NULL, dacl, &sd_size);
4133 done:
4134 SAFE_FREE(grp_sid);
4135 SAFE_FREE(owner_sid);
4137 return ret;
4141 /* Obtain the current dos attributes */
4142 static DOS_ATTR_DESC *
4143 dos_attr_query(SMBCCTX *context,
4144 TALLOC_CTX *ctx,
4145 const char *filename,
4146 SMBCSRV *srv)
4148 struct timespec create_time_ts;
4149 struct timespec write_time_ts;
4150 struct timespec access_time_ts;
4151 struct timespec change_time_ts;
4152 SMB_OFF_T size = 0;
4153 uint16 mode = 0;
4154 SMB_INO_T inode = 0;
4155 DOS_ATTR_DESC *ret;
4157 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4158 if (!ret) {
4159 errno = ENOMEM;
4160 return NULL;
4163 /* Obtain the DOS attributes */
4164 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4165 &mode, &size,
4166 &create_time_ts,
4167 &access_time_ts,
4168 &write_time_ts,
4169 &change_time_ts,
4170 &inode)) {
4172 errno = smbc_errno(context, srv->cli);
4173 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4174 return NULL;
4178 ret->mode = mode;
4179 ret->size = size;
4180 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4181 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4182 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4183 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4184 ret->inode = inode;
4186 return ret;
4190 /* parse a ascii version of a security descriptor */
4191 static void
4192 dos_attr_parse(SMBCCTX *context,
4193 DOS_ATTR_DESC *dad,
4194 SMBCSRV *srv,
4195 char *str)
4197 int n;
4198 const char *p = str;
4199 fstring tok;
4200 struct {
4201 const char * create_time_attr;
4202 const char * access_time_attr;
4203 const char * write_time_attr;
4204 const char * change_time_attr;
4205 } attr_strings;
4207 /* Determine whether to use old-style or new-style attribute names */
4208 if (context->internal->_full_time_names) {
4209 /* new-style names */
4210 attr_strings.create_time_attr = "CREATE_TIME";
4211 attr_strings.access_time_attr = "ACCESS_TIME";
4212 attr_strings.write_time_attr = "WRITE_TIME";
4213 attr_strings.change_time_attr = "CHANGE_TIME";
4214 } else {
4215 /* old-style names */
4216 attr_strings.create_time_attr = NULL;
4217 attr_strings.access_time_attr = "A_TIME";
4218 attr_strings.write_time_attr = "M_TIME";
4219 attr_strings.change_time_attr = "C_TIME";
4222 /* if this is to set the entire ACL... */
4223 if (*str == '*') {
4224 /* ... then increment past the first colon if there is one */
4225 if ((p = strchr(str, ':')) != NULL) {
4226 ++p;
4227 } else {
4228 p = str;
4232 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4234 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4235 dad->mode = strtol(tok+5, NULL, 16);
4236 continue;
4239 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4240 dad->size = (SMB_OFF_T)atof(tok+5);
4241 continue;
4244 n = strlen(attr_strings.access_time_attr);
4245 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4246 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4247 continue;
4250 n = strlen(attr_strings.change_time_attr);
4251 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4252 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4253 continue;
4256 n = strlen(attr_strings.write_time_attr);
4257 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4258 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4259 continue;
4262 if (attr_strings.create_time_attr != NULL) {
4263 n = strlen(attr_strings.create_time_attr);
4264 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4265 n) == 0) {
4266 dad->create_time = (time_t)strtol(tok+n+1,
4267 NULL, 10);
4268 continue;
4272 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4273 dad->inode = (SMB_INO_T)atof(tok+6);
4274 continue;
4279 /*****************************************************
4280 Retrieve the acls for a file.
4281 *******************************************************/
4283 static int
4284 cacl_get(SMBCCTX *context,
4285 TALLOC_CTX *ctx,
4286 SMBCSRV *srv,
4287 struct cli_state *ipc_cli,
4288 POLICY_HND *pol,
4289 char *filename,
4290 char *attr_name,
4291 char *buf,
4292 int bufsize)
4294 uint32 i;
4295 int n = 0;
4296 int n_used;
4297 BOOL all;
4298 BOOL all_nt;
4299 BOOL all_nt_acls;
4300 BOOL all_dos;
4301 BOOL some_nt;
4302 BOOL some_dos;
4303 BOOL exclude_nt_revision = False;
4304 BOOL exclude_nt_owner = False;
4305 BOOL exclude_nt_group = False;
4306 BOOL exclude_nt_acl = False;
4307 BOOL exclude_dos_mode = False;
4308 BOOL exclude_dos_size = False;
4309 BOOL exclude_dos_create_time = False;
4310 BOOL exclude_dos_access_time = False;
4311 BOOL exclude_dos_write_time = False;
4312 BOOL exclude_dos_change_time = False;
4313 BOOL exclude_dos_inode = False;
4314 BOOL numeric = True;
4315 BOOL determine_size = (bufsize == 0);
4316 int fnum = -1;
4317 SEC_DESC *sd;
4318 fstring sidstr;
4319 fstring name_sandbox;
4320 char *name;
4321 char *pExclude;
4322 char *p;
4323 struct timespec create_time_ts;
4324 struct timespec write_time_ts;
4325 struct timespec access_time_ts;
4326 struct timespec change_time_ts;
4327 time_t create_time = (time_t)0;
4328 time_t write_time = (time_t)0;
4329 time_t access_time = (time_t)0;
4330 time_t change_time = (time_t)0;
4331 SMB_OFF_T size = 0;
4332 uint16 mode = 0;
4333 SMB_INO_T ino = 0;
4334 struct cli_state *cli = srv->cli;
4335 struct {
4336 const char * create_time_attr;
4337 const char * access_time_attr;
4338 const char * write_time_attr;
4339 const char * change_time_attr;
4340 } attr_strings;
4341 struct {
4342 const char * create_time_attr;
4343 const char * access_time_attr;
4344 const char * write_time_attr;
4345 const char * change_time_attr;
4346 } excl_attr_strings;
4348 /* Determine whether to use old-style or new-style attribute names */
4349 if (context->internal->_full_time_names) {
4350 /* new-style names */
4351 attr_strings.create_time_attr = "CREATE_TIME";
4352 attr_strings.access_time_attr = "ACCESS_TIME";
4353 attr_strings.write_time_attr = "WRITE_TIME";
4354 attr_strings.change_time_attr = "CHANGE_TIME";
4356 excl_attr_strings.create_time_attr = "CREATE_TIME";
4357 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4358 excl_attr_strings.write_time_attr = "WRITE_TIME";
4359 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4360 } else {
4361 /* old-style names */
4362 attr_strings.create_time_attr = NULL;
4363 attr_strings.access_time_attr = "A_TIME";
4364 attr_strings.write_time_attr = "M_TIME";
4365 attr_strings.change_time_attr = "C_TIME";
4367 excl_attr_strings.create_time_attr = NULL;
4368 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4369 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4370 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4373 /* Copy name so we can strip off exclusions (if any are specified) */
4374 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4376 /* Ensure name is null terminated */
4377 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4379 /* Play in the sandbox */
4380 name = name_sandbox;
4382 /* If there are any exclusions, point to them and mask them from name */
4383 if ((pExclude = strchr(name, '!')) != NULL)
4385 *pExclude++ = '\0';
4388 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4389 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4390 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4391 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4392 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4393 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4394 numeric = (* (name + strlen(name) - 1) != '+');
4396 /* Look for exclusions from "all" requests */
4397 if (all || all_nt || all_dos) {
4399 /* Exclusions are delimited by '!' */
4400 for (;
4401 pExclude != NULL;
4402 pExclude = (p == NULL ? NULL : p + 1)) {
4404 /* Find end of this exclusion name */
4405 if ((p = strchr(pExclude, '!')) != NULL)
4407 *p = '\0';
4410 /* Which exclusion name is this? */
4411 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4412 exclude_nt_revision = True;
4414 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4415 exclude_nt_owner = True;
4417 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4418 exclude_nt_group = True;
4420 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4421 exclude_nt_acl = True;
4423 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4424 exclude_dos_mode = True;
4426 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4427 exclude_dos_size = True;
4429 else if (excl_attr_strings.create_time_attr != NULL &&
4430 StrCaseCmp(pExclude,
4431 excl_attr_strings.change_time_attr) == 0) {
4432 exclude_dos_create_time = True;
4434 else if (StrCaseCmp(pExclude,
4435 excl_attr_strings.access_time_attr) == 0) {
4436 exclude_dos_access_time = True;
4438 else if (StrCaseCmp(pExclude,
4439 excl_attr_strings.write_time_attr) == 0) {
4440 exclude_dos_write_time = True;
4442 else if (StrCaseCmp(pExclude,
4443 excl_attr_strings.change_time_attr) == 0) {
4444 exclude_dos_change_time = True;
4446 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4447 exclude_dos_inode = True;
4449 else {
4450 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4451 pExclude));
4452 errno = ENOATTR;
4453 return -1;
4458 n_used = 0;
4461 * If we are (possibly) talking to an NT or new system and some NT
4462 * attributes have been requested...
4464 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4465 /* Point to the portion after "system.nt_sec_desc." */
4466 name += 19; /* if (all) this will be invalid but unused */
4468 /* ... then obtain any NT attributes which were requested */
4469 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4471 if (fnum == -1) {
4472 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4473 filename, cli_errstr(cli)));
4474 errno = 0;
4475 return -1;
4478 sd = cli_query_secdesc(cli, fnum, ctx);
4480 if (!sd) {
4481 DEBUG(5,
4482 ("cacl_get Failed to query old descriptor\n"));
4483 errno = 0;
4484 return -1;
4487 cli_close(cli, fnum);
4489 if (! exclude_nt_revision) {
4490 if (all || all_nt) {
4491 if (determine_size) {
4492 p = talloc_asprintf(ctx,
4493 "REVISION:%d",
4494 sd->revision);
4495 if (!p) {
4496 errno = ENOMEM;
4497 return -1;
4499 n = strlen(p);
4500 } else {
4501 n = snprintf(buf, bufsize,
4502 "REVISION:%d",
4503 sd->revision);
4505 } else if (StrCaseCmp(name, "revision") == 0) {
4506 if (determine_size) {
4507 p = talloc_asprintf(ctx, "%d",
4508 sd->revision);
4509 if (!p) {
4510 errno = ENOMEM;
4511 return -1;
4513 n = strlen(p);
4514 } else {
4515 n = snprintf(buf, bufsize, "%d",
4516 sd->revision);
4520 if (!determine_size && n > bufsize) {
4521 errno = ERANGE;
4522 return -1;
4524 buf += n;
4525 n_used += n;
4526 bufsize -= n;
4529 if (! exclude_nt_owner) {
4530 /* Get owner and group sid */
4531 if (sd->owner_sid) {
4532 convert_sid_to_string(ipc_cli, pol,
4533 sidstr,
4534 numeric,
4535 sd->owner_sid);
4536 } else {
4537 fstrcpy(sidstr, "");
4540 if (all || all_nt) {
4541 if (determine_size) {
4542 p = talloc_asprintf(ctx, ",OWNER:%s",
4543 sidstr);
4544 if (!p) {
4545 errno = ENOMEM;
4546 return -1;
4548 n = strlen(p);
4549 } else if (sidstr[0] != '\0') {
4550 n = snprintf(buf, bufsize,
4551 ",OWNER:%s", sidstr);
4553 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4554 if (determine_size) {
4555 p = talloc_asprintf(ctx, "%s", sidstr);
4556 if (!p) {
4557 errno = ENOMEM;
4558 return -1;
4560 n = strlen(p);
4561 } else {
4562 n = snprintf(buf, bufsize, "%s",
4563 sidstr);
4567 if (!determine_size && n > bufsize) {
4568 errno = ERANGE;
4569 return -1;
4571 buf += n;
4572 n_used += n;
4573 bufsize -= n;
4576 if (! exclude_nt_group) {
4577 if (sd->group_sid) {
4578 convert_sid_to_string(ipc_cli, pol,
4579 sidstr, numeric,
4580 sd->group_sid);
4581 } else {
4582 fstrcpy(sidstr, "");
4585 if (all || all_nt) {
4586 if (determine_size) {
4587 p = talloc_asprintf(ctx, ",GROUP:%s",
4588 sidstr);
4589 if (!p) {
4590 errno = ENOMEM;
4591 return -1;
4593 n = strlen(p);
4594 } else if (sidstr[0] != '\0') {
4595 n = snprintf(buf, bufsize,
4596 ",GROUP:%s", sidstr);
4598 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4599 if (determine_size) {
4600 p = talloc_asprintf(ctx, "%s", sidstr);
4601 if (!p) {
4602 errno = ENOMEM;
4603 return -1;
4605 n = strlen(p);
4606 } else {
4607 n = snprintf(buf, bufsize,
4608 "%s", sidstr);
4612 if (!determine_size && n > bufsize) {
4613 errno = ERANGE;
4614 return -1;
4616 buf += n;
4617 n_used += n;
4618 bufsize -= n;
4621 if (! exclude_nt_acl) {
4622 /* Add aces to value buffer */
4623 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4625 SEC_ACE *ace = &sd->dacl->aces[i];
4626 convert_sid_to_string(ipc_cli, pol,
4627 sidstr, numeric,
4628 &ace->trustee);
4630 if (all || all_nt) {
4631 if (determine_size) {
4632 p = talloc_asprintf(
4633 ctx,
4634 ",ACL:"
4635 "%s:%d/%d/0x%08x",
4636 sidstr,
4637 ace->type,
4638 ace->flags,
4639 ace->access_mask);
4640 if (!p) {
4641 errno = ENOMEM;
4642 return -1;
4644 n = strlen(p);
4645 } else {
4646 n = snprintf(
4647 buf, bufsize,
4648 ",ACL:%s:%d/%d/0x%08x",
4649 sidstr,
4650 ace->type,
4651 ace->flags,
4652 ace->access_mask);
4654 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4655 StrCaseCmp(name+3, sidstr) == 0) ||
4656 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4657 StrCaseCmp(name+4, sidstr) == 0)) {
4658 if (determine_size) {
4659 p = talloc_asprintf(
4660 ctx,
4661 "%d/%d/0x%08x",
4662 ace->type,
4663 ace->flags,
4664 ace->access_mask);
4665 if (!p) {
4666 errno = ENOMEM;
4667 return -1;
4669 n = strlen(p);
4670 } else {
4671 n = snprintf(buf, bufsize,
4672 "%d/%d/0x%08x",
4673 ace->type,
4674 ace->flags,
4675 ace->access_mask);
4677 } else if (all_nt_acls) {
4678 if (determine_size) {
4679 p = talloc_asprintf(
4680 ctx,
4681 "%s%s:%d/%d/0x%08x",
4682 i ? "," : "",
4683 sidstr,
4684 ace->type,
4685 ace->flags,
4686 ace->access_mask);
4687 if (!p) {
4688 errno = ENOMEM;
4689 return -1;
4691 n = strlen(p);
4692 } else {
4693 n = snprintf(buf, bufsize,
4694 "%s%s:%d/%d/0x%08x",
4695 i ? "," : "",
4696 sidstr,
4697 ace->type,
4698 ace->flags,
4699 ace->access_mask);
4702 if (!determine_size && n > bufsize) {
4703 errno = ERANGE;
4704 return -1;
4706 buf += n;
4707 n_used += n;
4708 bufsize -= n;
4712 /* Restore name pointer to its original value */
4713 name -= 19;
4716 if (all || some_dos) {
4717 /* Point to the portion after "system.dos_attr." */
4718 name += 16; /* if (all) this will be invalid but unused */
4720 /* Obtain the DOS attributes */
4721 if (!smbc_getatr(context, srv, filename, &mode, &size,
4722 &create_time_ts,
4723 &access_time_ts,
4724 &write_time_ts,
4725 &change_time_ts,
4726 &ino)) {
4728 errno = smbc_errno(context, srv->cli);
4729 return -1;
4733 create_time = convert_timespec_to_time_t(create_time_ts);
4734 access_time = convert_timespec_to_time_t(access_time_ts);
4735 write_time = convert_timespec_to_time_t(write_time_ts);
4736 change_time = convert_timespec_to_time_t(change_time_ts);
4738 if (! exclude_dos_mode) {
4739 if (all || all_dos) {
4740 if (determine_size) {
4741 p = talloc_asprintf(ctx,
4742 "%sMODE:0x%x",
4743 (ipc_cli &&
4744 (all || some_nt)
4745 ? ","
4746 : ""),
4747 mode);
4748 if (!p) {
4749 errno = ENOMEM;
4750 return -1;
4752 n = strlen(p);
4753 } else {
4754 n = snprintf(buf, bufsize,
4755 "%sMODE:0x%x",
4756 (ipc_cli &&
4757 (all || some_nt)
4758 ? ","
4759 : ""),
4760 mode);
4762 } else if (StrCaseCmp(name, "mode") == 0) {
4763 if (determine_size) {
4764 p = talloc_asprintf(ctx, "0x%x", mode);
4765 if (!p) {
4766 errno = ENOMEM;
4767 return -1;
4769 n = strlen(p);
4770 } else {
4771 n = snprintf(buf, bufsize,
4772 "0x%x", mode);
4776 if (!determine_size && n > bufsize) {
4777 errno = ERANGE;
4778 return -1;
4780 buf += n;
4781 n_used += n;
4782 bufsize -= n;
4785 if (! exclude_dos_size) {
4786 if (all || all_dos) {
4787 if (determine_size) {
4788 p = talloc_asprintf(
4789 ctx,
4790 ",SIZE:%.0f",
4791 (double)size);
4792 if (!p) {
4793 errno = ENOMEM;
4794 return -1;
4796 n = strlen(p);
4797 } else {
4798 n = snprintf(buf, bufsize,
4799 ",SIZE:%.0f",
4800 (double)size);
4802 } else if (StrCaseCmp(name, "size") == 0) {
4803 if (determine_size) {
4804 p = talloc_asprintf(
4805 ctx,
4806 "%.0f",
4807 (double)size);
4808 if (!p) {
4809 errno = ENOMEM;
4810 return -1;
4812 n = strlen(p);
4813 } else {
4814 n = snprintf(buf, bufsize,
4815 "%.0f",
4816 (double)size);
4820 if (!determine_size && n > bufsize) {
4821 errno = ERANGE;
4822 return -1;
4824 buf += n;
4825 n_used += n;
4826 bufsize -= n;
4829 if (! exclude_dos_create_time &&
4830 attr_strings.create_time_attr != NULL) {
4831 if (all || all_dos) {
4832 if (determine_size) {
4833 p = talloc_asprintf(ctx,
4834 ",%s:%lu",
4835 attr_strings.create_time_attr,
4836 create_time);
4837 if (!p) {
4838 errno = ENOMEM;
4839 return -1;
4841 n = strlen(p);
4842 } else {
4843 n = snprintf(buf, bufsize,
4844 ",%s:%lu",
4845 attr_strings.create_time_attr,
4846 create_time);
4848 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
4849 if (determine_size) {
4850 p = talloc_asprintf(ctx, "%lu", create_time);
4851 if (!p) {
4852 errno = ENOMEM;
4853 return -1;
4855 n = strlen(p);
4856 } else {
4857 n = snprintf(buf, bufsize,
4858 "%lu", create_time);
4862 if (!determine_size && n > bufsize) {
4863 errno = ERANGE;
4864 return -1;
4866 buf += n;
4867 n_used += n;
4868 bufsize -= n;
4871 if (! exclude_dos_access_time) {
4872 if (all || all_dos) {
4873 if (determine_size) {
4874 p = talloc_asprintf(ctx,
4875 ",%s:%lu",
4876 attr_strings.access_time_attr,
4877 access_time);
4878 if (!p) {
4879 errno = ENOMEM;
4880 return -1;
4882 n = strlen(p);
4883 } else {
4884 n = snprintf(buf, bufsize,
4885 ",%s:%lu",
4886 attr_strings.access_time_attr,
4887 access_time);
4889 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
4890 if (determine_size) {
4891 p = talloc_asprintf(ctx, "%lu", access_time);
4892 if (!p) {
4893 errno = ENOMEM;
4894 return -1;
4896 n = strlen(p);
4897 } else {
4898 n = snprintf(buf, bufsize,
4899 "%lu", access_time);
4903 if (!determine_size && n > bufsize) {
4904 errno = ERANGE;
4905 return -1;
4907 buf += n;
4908 n_used += n;
4909 bufsize -= n;
4912 if (! exclude_dos_write_time) {
4913 if (all || all_dos) {
4914 if (determine_size) {
4915 p = talloc_asprintf(ctx,
4916 ",%s:%lu",
4917 attr_strings.write_time_attr,
4918 write_time);
4919 if (!p) {
4920 errno = ENOMEM;
4921 return -1;
4923 n = strlen(p);
4924 } else {
4925 n = snprintf(buf, bufsize,
4926 ",%s:%lu",
4927 attr_strings.write_time_attr,
4928 write_time);
4930 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
4931 if (determine_size) {
4932 p = talloc_asprintf(ctx, "%lu", write_time);
4933 if (!p) {
4934 errno = ENOMEM;
4935 return -1;
4937 n = strlen(p);
4938 } else {
4939 n = snprintf(buf, bufsize,
4940 "%lu", write_time);
4944 if (!determine_size && n > bufsize) {
4945 errno = ERANGE;
4946 return -1;
4948 buf += n;
4949 n_used += n;
4950 bufsize -= n;
4953 if (! exclude_dos_change_time) {
4954 if (all || all_dos) {
4955 if (determine_size) {
4956 p = talloc_asprintf(ctx,
4957 ",%s:%lu",
4958 attr_strings.change_time_attr,
4959 change_time);
4960 if (!p) {
4961 errno = ENOMEM;
4962 return -1;
4964 n = strlen(p);
4965 } else {
4966 n = snprintf(buf, bufsize,
4967 ",%s:%lu",
4968 attr_strings.change_time_attr,
4969 change_time);
4971 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
4972 if (determine_size) {
4973 p = talloc_asprintf(ctx, "%lu", change_time);
4974 if (!p) {
4975 errno = ENOMEM;
4976 return -1;
4978 n = strlen(p);
4979 } else {
4980 n = snprintf(buf, bufsize,
4981 "%lu", change_time);
4985 if (!determine_size && n > bufsize) {
4986 errno = ERANGE;
4987 return -1;
4989 buf += n;
4990 n_used += n;
4991 bufsize -= n;
4994 if (! exclude_dos_inode) {
4995 if (all || all_dos) {
4996 if (determine_size) {
4997 p = talloc_asprintf(
4998 ctx,
4999 ",INODE:%.0f",
5000 (double)ino);
5001 if (!p) {
5002 errno = ENOMEM;
5003 return -1;
5005 n = strlen(p);
5006 } else {
5007 n = snprintf(buf, bufsize,
5008 ",INODE:%.0f",
5009 (double) ino);
5011 } else if (StrCaseCmp(name, "inode") == 0) {
5012 if (determine_size) {
5013 p = talloc_asprintf(
5014 ctx,
5015 "%.0f",
5016 (double) ino);
5017 if (!p) {
5018 errno = ENOMEM;
5019 return -1;
5021 n = strlen(p);
5022 } else {
5023 n = snprintf(buf, bufsize,
5024 "%.0f",
5025 (double) ino);
5029 if (!determine_size && n > bufsize) {
5030 errno = ERANGE;
5031 return -1;
5033 buf += n;
5034 n_used += n;
5035 bufsize -= n;
5038 /* Restore name pointer to its original value */
5039 name -= 16;
5042 if (n_used == 0) {
5043 errno = ENOATTR;
5044 return -1;
5047 return n_used;
5051 /*****************************************************
5052 set the ACLs on a file given an ascii description
5053 *******************************************************/
5054 static int
5055 cacl_set(TALLOC_CTX *ctx,
5056 struct cli_state *cli,
5057 struct cli_state *ipc_cli,
5058 POLICY_HND *pol,
5059 const char *filename,
5060 const char *the_acl,
5061 int mode,
5062 int flags)
5064 int fnum;
5065 int err = 0;
5066 SEC_DESC *sd = NULL, *old;
5067 SEC_ACL *dacl = NULL;
5068 DOM_SID *owner_sid = NULL;
5069 DOM_SID *grp_sid = NULL;
5070 uint32 i, j;
5071 size_t sd_size;
5072 int ret = 0;
5073 char *p;
5074 BOOL numeric = True;
5076 /* the_acl will be null for REMOVE_ALL operations */
5077 if (the_acl) {
5078 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5079 p > the_acl &&
5080 p[-1] != '+');
5082 /* if this is to set the entire ACL... */
5083 if (*the_acl == '*') {
5084 /* ... then increment past the first colon */
5085 the_acl = p + 1;
5088 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5089 CONST_DISCARD(char *, the_acl));
5091 if (!sd) {
5092 errno = EINVAL;
5093 return -1;
5097 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5098 that doesn't deref sd */
5100 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5101 errno = EINVAL;
5102 return -1;
5105 /* The desired access below is the only one I could find that works
5106 with NT4, W2KP and Samba */
5108 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5110 if (fnum == -1) {
5111 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5112 filename, cli_errstr(cli)));
5113 errno = 0;
5114 return -1;
5117 old = cli_query_secdesc(cli, fnum, ctx);
5119 if (!old) {
5120 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5121 errno = 0;
5122 return -1;
5125 cli_close(cli, fnum);
5127 switch (mode) {
5128 case SMBC_XATTR_MODE_REMOVE_ALL:
5129 old->dacl->num_aces = 0;
5130 SAFE_FREE(old->dacl->aces);
5131 SAFE_FREE(old->dacl);
5132 old->dacl = NULL;
5133 dacl = old->dacl;
5134 break;
5136 case SMBC_XATTR_MODE_REMOVE:
5137 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5138 BOOL found = False;
5140 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5141 if (sec_ace_equal(&sd->dacl->aces[i],
5142 &old->dacl->aces[j])) {
5143 uint32 k;
5144 for (k=j; k<old->dacl->num_aces-1;k++) {
5145 old->dacl->aces[k] =
5146 old->dacl->aces[k+1];
5148 old->dacl->num_aces--;
5149 if (old->dacl->num_aces == 0) {
5150 SAFE_FREE(old->dacl->aces);
5151 SAFE_FREE(old->dacl);
5152 old->dacl = NULL;
5154 found = True;
5155 dacl = old->dacl;
5156 break;
5160 if (!found) {
5161 err = ENOATTR;
5162 ret = -1;
5163 goto failed;
5166 break;
5168 case SMBC_XATTR_MODE_ADD:
5169 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5170 BOOL found = False;
5172 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5173 if (sid_equal(&sd->dacl->aces[i].trustee,
5174 &old->dacl->aces[j].trustee)) {
5175 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5176 err = EEXIST;
5177 ret = -1;
5178 goto failed;
5180 old->dacl->aces[j] = sd->dacl->aces[i];
5181 ret = -1;
5182 found = True;
5186 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5187 err = ENOATTR;
5188 ret = -1;
5189 goto failed;
5192 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5193 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5196 dacl = old->dacl;
5197 break;
5199 case SMBC_XATTR_MODE_SET:
5200 old = sd;
5201 owner_sid = old->owner_sid;
5202 grp_sid = old->group_sid;
5203 dacl = old->dacl;
5204 break;
5206 case SMBC_XATTR_MODE_CHOWN:
5207 owner_sid = sd->owner_sid;
5208 break;
5210 case SMBC_XATTR_MODE_CHGRP:
5211 grp_sid = sd->group_sid;
5212 break;
5215 /* Denied ACE entries must come before allowed ones */
5216 sort_acl(old->dacl);
5218 /* Create new security descriptor and set it */
5219 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5220 owner_sid, grp_sid, NULL, dacl, &sd_size);
5222 fnum = cli_nt_create(cli, filename,
5223 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5225 if (fnum == -1) {
5226 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5227 filename, cli_errstr(cli)));
5228 errno = 0;
5229 return -1;
5232 if (!cli_set_secdesc(cli, fnum, sd)) {
5233 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5234 ret = -1;
5237 /* Clean up */
5239 failed:
5240 cli_close(cli, fnum);
5242 if (err != 0) {
5243 errno = err;
5246 return ret;
5250 static int
5251 smbc_setxattr_ctx(SMBCCTX *context,
5252 const char *fname,
5253 const char *name,
5254 const void *value,
5255 size_t size,
5256 int flags)
5258 int ret;
5259 int ret2;
5260 SMBCSRV *srv;
5261 SMBCSRV *ipc_srv;
5262 fstring server;
5263 fstring share;
5264 fstring user;
5265 fstring password;
5266 fstring workgroup;
5267 pstring path;
5268 TALLOC_CTX *ctx;
5269 POLICY_HND pol;
5270 DOS_ATTR_DESC *dad;
5271 struct {
5272 const char * create_time_attr;
5273 const char * access_time_attr;
5274 const char * write_time_attr;
5275 const char * change_time_attr;
5276 } attr_strings;
5278 if (!context || !context->internal ||
5279 !context->internal->_initialized) {
5281 errno = EINVAL; /* Best I can think of ... */
5282 return -1;
5286 if (!fname) {
5288 errno = EINVAL;
5289 return -1;
5293 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5294 fname, name, (int) size, (const char*)value));
5296 if (smbc_parse_path(context, fname,
5297 workgroup, sizeof(workgroup),
5298 server, sizeof(server),
5299 share, sizeof(share),
5300 path, sizeof(path),
5301 user, sizeof(user),
5302 password, sizeof(password),
5303 NULL, 0)) {
5304 errno = EINVAL;
5305 return -1;
5308 if (user[0] == (char)0) fstrcpy(user, context->user);
5310 srv = smbc_server(context, True,
5311 server, share, workgroup, user, password);
5312 if (!srv) {
5313 return -1; /* errno set by smbc_server */
5316 if (! srv->no_nt_session) {
5317 ipc_srv = smbc_attr_server(context, server, share,
5318 workgroup, user, password,
5319 &pol);
5320 if (! ipc_srv) {
5321 srv->no_nt_session = True;
5323 } else {
5324 ipc_srv = NULL;
5327 ctx = talloc_init("smbc_setxattr");
5328 if (!ctx) {
5329 errno = ENOMEM;
5330 return -1;
5334 * Are they asking to set the entire set of known attributes?
5336 if (StrCaseCmp(name, "system.*") == 0 ||
5337 StrCaseCmp(name, "system.*+") == 0) {
5338 /* Yup. */
5339 char *namevalue =
5340 talloc_asprintf(ctx, "%s:%s",
5341 name+7, (const char *) value);
5342 if (! namevalue) {
5343 errno = ENOMEM;
5344 ret = -1;
5345 return -1;
5348 if (ipc_srv) {
5349 ret = cacl_set(ctx, srv->cli,
5350 ipc_srv->cli, &pol, path,
5351 namevalue,
5352 (*namevalue == '*'
5353 ? SMBC_XATTR_MODE_SET
5354 : SMBC_XATTR_MODE_ADD),
5355 flags);
5356 } else {
5357 ret = 0;
5360 /* get a DOS Attribute Descriptor with current attributes */
5361 dad = dos_attr_query(context, ctx, path, srv);
5362 if (dad) {
5363 /* Overwrite old with new, using what was provided */
5364 dos_attr_parse(context, dad, srv, namevalue);
5366 /* Set the new DOS attributes */
5367 if (! smbc_setatr(context, srv, path,
5368 dad->create_time,
5369 dad->access_time,
5370 dad->write_time,
5371 dad->change_time,
5372 dad->mode)) {
5374 /* cause failure if NT failed too */
5375 dad = NULL;
5379 /* we only fail if both NT and DOS sets failed */
5380 if (ret < 0 && ! dad) {
5381 ret = -1; /* in case dad was null */
5383 else {
5384 ret = 0;
5387 talloc_destroy(ctx);
5388 return ret;
5392 * Are they asking to set an access control element or to set
5393 * the entire access control list?
5395 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5396 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5397 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5398 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5399 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5401 /* Yup. */
5402 char *namevalue =
5403 talloc_asprintf(ctx, "%s:%s",
5404 name+19, (const char *) value);
5406 if (! ipc_srv) {
5407 ret = -1; /* errno set by smbc_server() */
5409 else if (! namevalue) {
5410 errno = ENOMEM;
5411 ret = -1;
5412 } else {
5413 ret = cacl_set(ctx, srv->cli,
5414 ipc_srv->cli, &pol, path,
5415 namevalue,
5416 (*namevalue == '*'
5417 ? SMBC_XATTR_MODE_SET
5418 : SMBC_XATTR_MODE_ADD),
5419 flags);
5421 talloc_destroy(ctx);
5422 return ret;
5426 * Are they asking to set the owner?
5428 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5429 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5431 /* Yup. */
5432 char *namevalue =
5433 talloc_asprintf(ctx, "%s:%s",
5434 name+19, (const char *) value);
5436 if (! ipc_srv) {
5438 ret = -1; /* errno set by smbc_server() */
5440 else if (! namevalue) {
5441 errno = ENOMEM;
5442 ret = -1;
5443 } else {
5444 ret = cacl_set(ctx, srv->cli,
5445 ipc_srv->cli, &pol, path,
5446 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5448 talloc_destroy(ctx);
5449 return ret;
5453 * Are they asking to set the group?
5455 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5456 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5458 /* Yup. */
5459 char *namevalue =
5460 talloc_asprintf(ctx, "%s:%s",
5461 name+19, (const char *) value);
5463 if (! ipc_srv) {
5464 /* errno set by smbc_server() */
5465 ret = -1;
5467 else if (! namevalue) {
5468 errno = ENOMEM;
5469 ret = -1;
5470 } else {
5471 ret = cacl_set(ctx, srv->cli,
5472 ipc_srv->cli, &pol, path,
5473 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5475 talloc_destroy(ctx);
5476 return ret;
5479 /* Determine whether to use old-style or new-style attribute names */
5480 if (context->internal->_full_time_names) {
5481 /* new-style names */
5482 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5483 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5484 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5485 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5486 } else {
5487 /* old-style names */
5488 attr_strings.create_time_attr = NULL;
5489 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5490 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5491 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5495 * Are they asking to set a DOS attribute?
5497 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5498 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5499 (attr_strings.create_time_attr != NULL &&
5500 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5501 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5502 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5503 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5505 /* get a DOS Attribute Descriptor with current attributes */
5506 dad = dos_attr_query(context, ctx, path, srv);
5507 if (dad) {
5508 char *namevalue =
5509 talloc_asprintf(ctx, "%s:%s",
5510 name+16, (const char *) value);
5511 if (! namevalue) {
5512 errno = ENOMEM;
5513 ret = -1;
5514 } else {
5515 /* Overwrite old with provided new params */
5516 dos_attr_parse(context, dad, srv, namevalue);
5518 /* Set the new DOS attributes */
5519 ret2 = smbc_setatr(context, srv, path,
5520 dad->create_time,
5521 dad->access_time,
5522 dad->write_time,
5523 dad->change_time,
5524 dad->mode);
5526 /* ret2 has True (success) / False (failure) */
5527 if (ret2) {
5528 ret = 0;
5529 } else {
5530 ret = -1;
5533 } else {
5534 ret = -1;
5537 talloc_destroy(ctx);
5538 return ret;
5541 /* Unsupported attribute name */
5542 talloc_destroy(ctx);
5543 errno = EINVAL;
5544 return -1;
5547 static int
5548 smbc_getxattr_ctx(SMBCCTX *context,
5549 const char *fname,
5550 const char *name,
5551 const void *value,
5552 size_t size)
5554 int ret;
5555 SMBCSRV *srv;
5556 SMBCSRV *ipc_srv;
5557 fstring server;
5558 fstring share;
5559 fstring user;
5560 fstring password;
5561 fstring workgroup;
5562 pstring path;
5563 TALLOC_CTX *ctx;
5564 POLICY_HND pol;
5565 struct {
5566 const char * create_time_attr;
5567 const char * access_time_attr;
5568 const char * write_time_attr;
5569 const char * change_time_attr;
5570 } attr_strings;
5573 if (!context || !context->internal ||
5574 !context->internal->_initialized) {
5576 errno = EINVAL; /* Best I can think of ... */
5577 return -1;
5581 if (!fname) {
5583 errno = EINVAL;
5584 return -1;
5588 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5590 if (smbc_parse_path(context, fname,
5591 workgroup, sizeof(workgroup),
5592 server, sizeof(server),
5593 share, sizeof(share),
5594 path, sizeof(path),
5595 user, sizeof(user),
5596 password, sizeof(password),
5597 NULL, 0)) {
5598 errno = EINVAL;
5599 return -1;
5602 if (user[0] == (char)0) fstrcpy(user, context->user);
5604 srv = smbc_server(context, True,
5605 server, share, workgroup, user, password);
5606 if (!srv) {
5607 return -1; /* errno set by smbc_server */
5610 if (! srv->no_nt_session) {
5611 ipc_srv = smbc_attr_server(context, server, share,
5612 workgroup, user, password,
5613 &pol);
5614 if (! ipc_srv) {
5615 srv->no_nt_session = True;
5617 } else {
5618 ipc_srv = NULL;
5621 ctx = talloc_init("smbc:getxattr");
5622 if (!ctx) {
5623 errno = ENOMEM;
5624 return -1;
5627 /* Determine whether to use old-style or new-style attribute names */
5628 if (context->internal->_full_time_names) {
5629 /* new-style names */
5630 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5631 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5632 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5633 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5634 } else {
5635 /* old-style names */
5636 attr_strings.create_time_attr = NULL;
5637 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5638 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5639 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5642 /* Are they requesting a supported attribute? */
5643 if (StrCaseCmp(name, "system.*") == 0 ||
5644 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5645 StrCaseCmp(name, "system.*+") == 0 ||
5646 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5647 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5648 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5649 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5650 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5651 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5652 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5653 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5654 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5655 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5656 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5657 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5658 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5659 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5660 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5661 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5662 (attr_strings.create_time_attr != NULL &&
5663 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5664 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5665 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5666 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5667 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
5669 /* Yup. */
5670 ret = cacl_get(context, ctx, srv,
5671 ipc_srv == NULL ? NULL : ipc_srv->cli,
5672 &pol, path,
5673 CONST_DISCARD(char *, name),
5674 CONST_DISCARD(char *, value), size);
5675 if (ret < 0 && errno == 0) {
5676 errno = smbc_errno(context, srv->cli);
5678 talloc_destroy(ctx);
5679 return ret;
5682 /* Unsupported attribute name */
5683 talloc_destroy(ctx);
5684 errno = EINVAL;
5685 return -1;
5689 static int
5690 smbc_removexattr_ctx(SMBCCTX *context,
5691 const char *fname,
5692 const char *name)
5694 int ret;
5695 SMBCSRV *srv;
5696 SMBCSRV *ipc_srv;
5697 fstring server;
5698 fstring share;
5699 fstring user;
5700 fstring password;
5701 fstring workgroup;
5702 pstring path;
5703 TALLOC_CTX *ctx;
5704 POLICY_HND pol;
5706 if (!context || !context->internal ||
5707 !context->internal->_initialized) {
5709 errno = EINVAL; /* Best I can think of ... */
5710 return -1;
5714 if (!fname) {
5716 errno = EINVAL;
5717 return -1;
5721 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
5723 if (smbc_parse_path(context, fname,
5724 workgroup, sizeof(workgroup),
5725 server, sizeof(server),
5726 share, sizeof(share),
5727 path, sizeof(path),
5728 user, sizeof(user),
5729 password, sizeof(password),
5730 NULL, 0)) {
5731 errno = EINVAL;
5732 return -1;
5735 if (user[0] == (char)0) fstrcpy(user, context->user);
5737 srv = smbc_server(context, True,
5738 server, share, workgroup, user, password);
5739 if (!srv) {
5740 return -1; /* errno set by smbc_server */
5743 if (! srv->no_nt_session) {
5744 ipc_srv = smbc_attr_server(context, server, share,
5745 workgroup, user, password,
5746 &pol);
5747 if (! ipc_srv) {
5748 srv->no_nt_session = True;
5750 } else {
5751 ipc_srv = NULL;
5754 if (! ipc_srv) {
5755 return -1; /* errno set by smbc_attr_server */
5758 ctx = talloc_init("smbc_removexattr");
5759 if (!ctx) {
5760 errno = ENOMEM;
5761 return -1;
5764 /* Are they asking to set the entire ACL? */
5765 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5766 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
5768 /* Yup. */
5769 ret = cacl_set(ctx, srv->cli,
5770 ipc_srv->cli, &pol, path,
5771 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
5772 talloc_destroy(ctx);
5773 return ret;
5777 * Are they asking to remove one or more spceific security descriptor
5778 * attributes?
5780 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5781 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5782 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5783 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5784 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5785 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5786 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5788 /* Yup. */
5789 ret = cacl_set(ctx, srv->cli,
5790 ipc_srv->cli, &pol, path,
5791 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
5792 talloc_destroy(ctx);
5793 return ret;
5796 /* Unsupported attribute name */
5797 talloc_destroy(ctx);
5798 errno = EINVAL;
5799 return -1;
5802 static int
5803 smbc_listxattr_ctx(SMBCCTX *context,
5804 const char *fname,
5805 char *list,
5806 size_t size)
5809 * This isn't quite what listxattr() is supposed to do. This returns
5810 * the complete set of attribute names, always, rather than only those
5811 * attribute names which actually exist for a file. Hmmm...
5813 const char supported_old[] =
5814 "system.*\0"
5815 "system.*+\0"
5816 "system.nt_sec_desc.revision\0"
5817 "system.nt_sec_desc.owner\0"
5818 "system.nt_sec_desc.owner+\0"
5819 "system.nt_sec_desc.group\0"
5820 "system.nt_sec_desc.group+\0"
5821 "system.nt_sec_desc.acl.*\0"
5822 "system.nt_sec_desc.acl\0"
5823 "system.nt_sec_desc.acl+\0"
5824 "system.nt_sec_desc.*\0"
5825 "system.nt_sec_desc.*+\0"
5826 "system.dos_attr.*\0"
5827 "system.dos_attr.mode\0"
5828 "system.dos_attr.c_time\0"
5829 "system.dos_attr.a_time\0"
5830 "system.dos_attr.m_time\0"
5832 const char supported_new[] =
5833 "system.*\0"
5834 "system.*+\0"
5835 "system.nt_sec_desc.revision\0"
5836 "system.nt_sec_desc.owner\0"
5837 "system.nt_sec_desc.owner+\0"
5838 "system.nt_sec_desc.group\0"
5839 "system.nt_sec_desc.group+\0"
5840 "system.nt_sec_desc.acl.*\0"
5841 "system.nt_sec_desc.acl\0"
5842 "system.nt_sec_desc.acl+\0"
5843 "system.nt_sec_desc.*\0"
5844 "system.nt_sec_desc.*+\0"
5845 "system.dos_attr.*\0"
5846 "system.dos_attr.mode\0"
5847 "system.dos_attr.create_time\0"
5848 "system.dos_attr.access_time\0"
5849 "system.dos_attr.write_time\0"
5850 "system.dos_attr.change_time\0"
5852 const char * supported;
5854 if (context->internal->_full_time_names) {
5855 supported = supported_new;
5856 } else {
5857 supported = supported_old;
5860 if (size == 0) {
5861 return sizeof(supported);
5864 if (sizeof(supported) > size) {
5865 errno = ERANGE;
5866 return -1;
5869 /* this can't be strcpy() because there are embedded null characters */
5870 memcpy(list, supported, sizeof(supported));
5871 return sizeof(supported);
5876 * Open a print file to be written to by other calls
5879 static SMBCFILE *
5880 smbc_open_print_job_ctx(SMBCCTX *context,
5881 const char *fname)
5883 fstring server;
5884 fstring share;
5885 fstring user;
5886 fstring password;
5887 pstring path;
5889 if (!context || !context->internal ||
5890 !context->internal->_initialized) {
5892 errno = EINVAL;
5893 return NULL;
5897 if (!fname) {
5899 errno = EINVAL;
5900 return NULL;
5904 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
5906 if (smbc_parse_path(context, fname,
5907 NULL, 0,
5908 server, sizeof(server),
5909 share, sizeof(share),
5910 path, sizeof(path),
5911 user, sizeof(user),
5912 password, sizeof(password),
5913 NULL, 0)) {
5914 errno = EINVAL;
5915 return NULL;
5918 /* What if the path is empty, or the file exists? */
5920 return context->open(context, fname, O_WRONLY, 666);
5925 * Routine to print a file on a remote server ...
5927 * We open the file, which we assume to be on a remote server, and then
5928 * copy it to a print file on the share specified by printq.
5931 static int
5932 smbc_print_file_ctx(SMBCCTX *c_file,
5933 const char *fname,
5934 SMBCCTX *c_print,
5935 const char *printq)
5937 SMBCFILE *fid1;
5938 SMBCFILE *fid2;
5939 int bytes;
5940 int saverr;
5941 int tot_bytes = 0;
5942 char buf[4096];
5944 if (!c_file || !c_file->internal->_initialized || !c_print ||
5945 !c_print->internal->_initialized) {
5947 errno = EINVAL;
5948 return -1;
5952 if (!fname && !printq) {
5954 errno = EINVAL;
5955 return -1;
5959 /* Try to open the file for reading ... */
5961 if ((long)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
5963 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
5964 return -1; /* smbc_open sets errno */
5968 /* Now, try to open the printer file for writing */
5970 if ((long)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
5972 saverr = errno; /* Save errno */
5973 c_file->close_fn(c_file, fid1);
5974 errno = saverr;
5975 return -1;
5979 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
5981 tot_bytes += bytes;
5983 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
5985 saverr = errno;
5986 c_file->close_fn(c_file, fid1);
5987 c_print->close_fn(c_print, fid2);
5988 errno = saverr;
5994 saverr = errno;
5996 c_file->close_fn(c_file, fid1); /* We have to close these anyway */
5997 c_print->close_fn(c_print, fid2);
5999 if (bytes < 0) {
6001 errno = saverr;
6002 return -1;
6006 return tot_bytes;
6011 * Routine to list print jobs on a printer share ...
6014 static int
6015 smbc_list_print_jobs_ctx(SMBCCTX *context,
6016 const char *fname,
6017 smbc_list_print_job_fn fn)
6019 SMBCSRV *srv;
6020 fstring server;
6021 fstring share;
6022 fstring user;
6023 fstring password;
6024 fstring workgroup;
6025 pstring path;
6027 if (!context || !context->internal ||
6028 !context->internal->_initialized) {
6030 errno = EINVAL;
6031 return -1;
6035 if (!fname) {
6037 errno = EINVAL;
6038 return -1;
6042 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6044 if (smbc_parse_path(context, fname,
6045 workgroup, sizeof(workgroup),
6046 server, sizeof(server),
6047 share, sizeof(share),
6048 path, sizeof(path),
6049 user, sizeof(user),
6050 password, sizeof(password),
6051 NULL, 0)) {
6052 errno = EINVAL;
6053 return -1;
6056 if (user[0] == (char)0) fstrcpy(user, context->user);
6058 srv = smbc_server(context, True,
6059 server, share, workgroup, user, password);
6061 if (!srv) {
6063 return -1; /* errno set by smbc_server */
6067 if (cli_print_queue(srv->cli,
6068 (void (*)(struct print_job_info *))fn) < 0) {
6070 errno = smbc_errno(context, srv->cli);
6071 return -1;
6075 return 0;
6080 * Delete a print job from a remote printer share
6083 static int
6084 smbc_unlink_print_job_ctx(SMBCCTX *context,
6085 const char *fname,
6086 int id)
6088 SMBCSRV *srv;
6089 fstring server;
6090 fstring share;
6091 fstring user;
6092 fstring password;
6093 fstring workgroup;
6094 pstring path;
6095 int err;
6097 if (!context || !context->internal ||
6098 !context->internal->_initialized) {
6100 errno = EINVAL;
6101 return -1;
6105 if (!fname) {
6107 errno = EINVAL;
6108 return -1;
6112 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6114 if (smbc_parse_path(context, fname,
6115 workgroup, sizeof(workgroup),
6116 server, sizeof(server),
6117 share, sizeof(share),
6118 path, sizeof(path),
6119 user, sizeof(user),
6120 password, sizeof(password),
6121 NULL, 0)) {
6122 errno = EINVAL;
6123 return -1;
6126 if (user[0] == (char)0) fstrcpy(user, context->user);
6128 srv = smbc_server(context, True,
6129 server, share, workgroup, user, password);
6131 if (!srv) {
6133 return -1; /* errno set by smbc_server */
6137 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6139 if (err < 0)
6140 errno = smbc_errno(context, srv->cli);
6141 else if (err == ERRnosuchprintjob)
6142 errno = EINVAL;
6143 return -1;
6147 return 0;
6152 * Get a new empty handle to fill in with your own info
6154 SMBCCTX *
6155 smbc_new_context(void)
6157 SMBCCTX *context;
6159 context = SMB_MALLOC_P(SMBCCTX);
6160 if (!context) {
6161 errno = ENOMEM;
6162 return NULL;
6165 ZERO_STRUCTP(context);
6167 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6168 if (!context->internal) {
6169 SAFE_FREE(context);
6170 errno = ENOMEM;
6171 return NULL;
6174 ZERO_STRUCTP(context->internal);
6177 /* ADD REASONABLE DEFAULTS */
6178 context->debug = 0;
6179 context->timeout = 20000; /* 20 seconds */
6181 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6182 context->options.urlencode_readdir_entries = False;/* backward compat */
6183 context->options.one_share_per_server = False;/* backward compat */
6184 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6185 /* backward compat */
6187 context->open = smbc_open_ctx;
6188 context->creat = smbc_creat_ctx;
6189 context->read = smbc_read_ctx;
6190 context->write = smbc_write_ctx;
6191 context->close_fn = smbc_close_ctx;
6192 context->unlink = smbc_unlink_ctx;
6193 context->rename = smbc_rename_ctx;
6194 context->lseek = smbc_lseek_ctx;
6195 context->stat = smbc_stat_ctx;
6196 context->fstat = smbc_fstat_ctx;
6197 context->opendir = smbc_opendir_ctx;
6198 context->closedir = smbc_closedir_ctx;
6199 context->readdir = smbc_readdir_ctx;
6200 context->getdents = smbc_getdents_ctx;
6201 context->mkdir = smbc_mkdir_ctx;
6202 context->rmdir = smbc_rmdir_ctx;
6203 context->telldir = smbc_telldir_ctx;
6204 context->lseekdir = smbc_lseekdir_ctx;
6205 context->fstatdir = smbc_fstatdir_ctx;
6206 context->chmod = smbc_chmod_ctx;
6207 context->utimes = smbc_utimes_ctx;
6208 context->setxattr = smbc_setxattr_ctx;
6209 context->getxattr = smbc_getxattr_ctx;
6210 context->removexattr = smbc_removexattr_ctx;
6211 context->listxattr = smbc_listxattr_ctx;
6212 context->open_print_job = smbc_open_print_job_ctx;
6213 context->print_file = smbc_print_file_ctx;
6214 context->list_print_jobs = smbc_list_print_jobs_ctx;
6215 context->unlink_print_job = smbc_unlink_print_job_ctx;
6217 context->callbacks.check_server_fn = smbc_check_server;
6218 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6220 smbc_default_cache_functions(context);
6222 return context;
6226 * Free a context
6228 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6229 * and thus you'll be leaking memory if not handled properly.
6233 smbc_free_context(SMBCCTX *context,
6234 int shutdown_ctx)
6236 if (!context) {
6237 errno = EBADF;
6238 return 1;
6241 if (shutdown_ctx) {
6242 SMBCFILE * f;
6243 DEBUG(1,("Performing aggressive shutdown.\n"));
6245 f = context->internal->_files;
6246 while (f) {
6247 context->close_fn(context, f);
6248 f = f->next;
6250 context->internal->_files = NULL;
6252 /* First try to remove the servers the nice way. */
6253 if (context->callbacks.purge_cached_fn(context)) {
6254 SMBCSRV * s;
6255 SMBCSRV * next;
6256 DEBUG(1, ("Could not purge all servers, "
6257 "Nice way shutdown failed.\n"));
6258 s = context->internal->_servers;
6259 while (s) {
6260 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6261 s, s->cli->fd));
6262 cli_shutdown(s->cli);
6263 context->callbacks.remove_cached_srv_fn(context,
6265 next = s->next;
6266 DLIST_REMOVE(context->internal->_servers, s);
6267 SAFE_FREE(s);
6268 s = next;
6270 context->internal->_servers = NULL;
6273 else {
6274 /* This is the polite way */
6275 if (context->callbacks.purge_cached_fn(context)) {
6276 DEBUG(1, ("Could not purge all servers, "
6277 "free_context failed.\n"));
6278 errno = EBUSY;
6279 return 1;
6281 if (context->internal->_servers) {
6282 DEBUG(1, ("Active servers in context, "
6283 "free_context failed.\n"));
6284 errno = EBUSY;
6285 return 1;
6287 if (context->internal->_files) {
6288 DEBUG(1, ("Active files in context, "
6289 "free_context failed.\n"));
6290 errno = EBUSY;
6291 return 1;
6295 /* Things we have to clean up */
6296 SAFE_FREE(context->workgroup);
6297 SAFE_FREE(context->netbios_name);
6298 SAFE_FREE(context->user);
6300 DEBUG(3, ("Context %p succesfully freed\n", context));
6301 SAFE_FREE(context->internal);
6302 SAFE_FREE(context);
6303 return 0;
6308 * Each time the context structure is changed, we have binary backward
6309 * compatibility issues. Instead of modifying the public portions of the
6310 * context structure to add new options, instead, we put them in the internal
6311 * portion of the context structure and provide a set function for these new
6312 * options.
6314 void
6315 smbc_option_set(SMBCCTX *context,
6316 char *option_name,
6317 ... /* option_value */)
6319 va_list ap;
6320 union {
6321 int i;
6322 BOOL b;
6323 smbc_get_auth_data_with_context_fn auth_fn;
6324 void *v;
6325 } option_value;
6327 va_start(ap, option_name);
6329 if (strcmp(option_name, "debug_to_stderr") == 0) {
6331 * Log to standard error instead of standard output.
6333 option_value.b = (BOOL) va_arg(ap, int);
6334 context->internal->_debug_stderr = option_value.b;
6336 } else if (strcmp(option_name, "full_time_names") == 0) {
6338 * Use new-style time attribute names, e.g. WRITE_TIME rather
6339 * than the old-style names such as M_TIME. This allows also
6340 * setting/getting CREATE_TIME which was previously
6341 * unimplemented. (Note that the old C_TIME was supposed to
6342 * be CHANGE_TIME but was confused and sometimes referred to
6343 * CREATE_TIME.)
6345 option_value.b = (BOOL) va_arg(ap, int);
6346 context->internal->_full_time_names = option_value.b;
6348 } else if (strcmp(option_name, "open_share_mode") == 0) {
6350 * The share mode to use for files opened with
6351 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6353 option_value.i = va_arg(ap, int);
6354 context->internal->_share_mode =
6355 (smbc_share_mode) option_value.i;
6357 } else if (strcmp(option_name, "auth_function") == 0) {
6359 * Use the new-style authentication function which includes
6360 * the context.
6362 option_value.auth_fn =
6363 va_arg(ap, smbc_get_auth_data_with_context_fn);
6364 context->internal->_auth_fn_with_context =
6365 option_value.auth_fn;
6366 } else if (strcmp(option_name, "user_data") == 0) {
6368 * Save a user data handle which may be retrieved by the user
6369 * with smbc_option_get()
6371 option_value.v = va_arg(ap, void *);
6372 context->internal->_user_data = option_value.v;
6375 va_end(ap);
6380 * Retrieve the current value of an option
6382 void *
6383 smbc_option_get(SMBCCTX *context,
6384 char *option_name)
6386 if (strcmp(option_name, "debug_stderr") == 0) {
6388 * Log to standard error instead of standard output.
6390 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6391 return (void *) (intptr_t) context->internal->_debug_stderr;
6392 #else
6393 return (void *) context->internal->_debug_stderr;
6394 #endif
6395 } else if (strcmp(option_name, "full_time_names") == 0) {
6397 * Use new-style time attribute names, e.g. WRITE_TIME rather
6398 * than the old-style names such as M_TIME. This allows also
6399 * setting/getting CREATE_TIME which was previously
6400 * unimplemented. (Note that the old C_TIME was supposed to
6401 * be CHANGE_TIME but was confused and sometimes referred to
6402 * CREATE_TIME.)
6404 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6405 return (void *) (intptr_t) context->internal->_full_time_names;
6406 #else
6407 return (void *) context->internal->_full_time_names;
6408 #endif
6410 } else if (strcmp(option_name, "auth_function") == 0) {
6412 * Use the new-style authentication function which includes
6413 * the context.
6415 return (void *) context->internal->_auth_fn_with_context;
6416 } else if (strcmp(option_name, "user_data") == 0) {
6418 * Save a user data handle which may be retrieved by the user
6419 * with smbc_option_get()
6421 return context->internal->_user_data;
6424 return NULL;
6429 * Initialise the library etc
6431 * We accept a struct containing handle information.
6432 * valid values for info->debug from 0 to 100,
6433 * and insist that info->fn must be non-null.
6435 SMBCCTX *
6436 smbc_init_context(SMBCCTX *context)
6438 pstring conf;
6439 int pid;
6440 char *user = NULL;
6441 char *home = NULL;
6443 if (!context || !context->internal) {
6444 errno = EBADF;
6445 return NULL;
6448 /* Do not initialise the same client twice */
6449 if (context->internal->_initialized) {
6450 return 0;
6453 if ((!context->callbacks.auth_fn &&
6454 !context->internal->_auth_fn_with_context) ||
6455 context->debug < 0 ||
6456 context->debug > 100) {
6458 errno = EINVAL;
6459 return NULL;
6463 if (!smbc_initialized) {
6465 * Do some library-wide intializations the first time we get
6466 * called
6468 BOOL conf_loaded = False;
6470 /* Set this to what the user wants */
6471 DEBUGLEVEL = context->debug;
6473 load_case_tables();
6475 setup_logging("libsmbclient", True);
6476 if (context->internal->_debug_stderr) {
6477 dbf = x_stderr;
6478 x_setbuf(x_stderr, NULL);
6481 /* Here we would open the smb.conf file if needed ... */
6483 in_client = True; /* FIXME, make a param */
6485 home = getenv("HOME");
6486 if (home) {
6487 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
6488 if (lp_load(conf, True, False, False, True)) {
6489 conf_loaded = True;
6490 } else {
6491 DEBUG(5, ("Could not load config file: %s\n",
6492 conf));
6496 if (!conf_loaded) {
6498 * Well, if that failed, try the dyn_CONFIGFILE
6499 * Which points to the standard locn, and if that
6500 * fails, silently ignore it and use the internal
6501 * defaults ...
6504 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6505 DEBUG(5, ("Could not load config file: %s\n",
6506 dyn_CONFIGFILE));
6507 } else if (home) {
6509 * We loaded the global config file. Now lets
6510 * load user-specific modifications to the
6511 * global config.
6513 slprintf(conf, sizeof(conf),
6514 "%s/.smb/smb.conf.append", home);
6515 if (!lp_load(conf, True, False, False, False)) {
6516 DEBUG(10,
6517 ("Could not append config file: "
6518 "%s\n",
6519 conf));
6524 load_interfaces(); /* Load the list of interfaces ... */
6526 reopen_logs(); /* Get logging working ... */
6529 * Block SIGPIPE (from lib/util_sock.c: write())
6530 * It is not needed and should not stop execution
6532 BlockSignals(True, SIGPIPE);
6534 /* Done with one-time initialisation */
6535 smbc_initialized = 1;
6539 if (!context->user) {
6541 * FIXME: Is this the best way to get the user info?
6543 user = getenv("USER");
6544 /* walk around as "guest" if no username can be found */
6545 if (!user) context->user = SMB_STRDUP("guest");
6546 else context->user = SMB_STRDUP(user);
6549 if (!context->netbios_name) {
6551 * We try to get our netbios name from the config. If that
6552 * fails we fall back on constructing our netbios name from
6553 * our hostname etc
6555 if (global_myname()) {
6556 context->netbios_name = SMB_STRDUP(global_myname());
6558 else {
6560 * Hmmm, I want to get hostname as well, but I am too
6561 * lazy for the moment
6563 pid = sys_getpid();
6564 context->netbios_name = (char *)SMB_MALLOC(17);
6565 if (!context->netbios_name) {
6566 errno = ENOMEM;
6567 return NULL;
6569 slprintf(context->netbios_name, 16,
6570 "smbc%s%d", context->user, pid);
6574 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6576 if (!context->workgroup) {
6577 if (lp_workgroup()) {
6578 context->workgroup = SMB_STRDUP(lp_workgroup());
6580 else {
6581 /* TODO: Think about a decent default workgroup */
6582 context->workgroup = SMB_STRDUP("samba");
6586 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6588 /* shortest timeout is 1 second */
6589 if (context->timeout > 0 && context->timeout < 1000)
6590 context->timeout = 1000;
6593 * FIXME: Should we check the function pointers here?
6596 context->internal->_initialized = True;
6598 return context;
6602 /* Return the verion of samba, and thus libsmbclient */
6603 const char *
6604 smbc_version(void)
6606 return samba_version_string();