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.
27 #include "include/libsmb_internal.h"
31 * DOS Attribute values (used internally)
33 typedef struct DOS_ATTR_DESC {
45 * Internal flags for extended attributes
48 /* internal mode values */
49 #define SMBC_XATTR_MODE_ADD 1
50 #define SMBC_XATTR_MODE_REMOVE 2
51 #define SMBC_XATTR_MODE_REMOVE_ALL 3
52 #define SMBC_XATTR_MODE_SET 4
53 #define SMBC_XATTR_MODE_CHOWN 5
54 #define SMBC_XATTR_MODE_CHGRP 6
56 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
58 /*We should test for this in configure ... */
60 #define ENOTSUP EOPNOTSUPP
64 * Functions exported by libsmb_cache.c that we need here
66 int smbc_default_cache_functions(SMBCCTX *context);
69 * check if an element is part of the list.
70 * FIXME: Does not belong here !
71 * Can anyone put this in a macro in dlinklist.h ?
74 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
75 if (!p || !list) return False;
77 if (p == list) return True;
84 * Find an lsa pipe handle associated with a cli struct.
86 static struct rpc_pipe_client *
87 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
89 struct rpc_pipe_client *pipe_hnd;
91 for (pipe_hnd = ipc_cli->pipe_list;
93 pipe_hnd = pipe_hnd->next) {
95 if (pipe_hnd->pipe_idx == PI_LSARPC) {
104 smbc_close_ctx(SMBCCTX *context,
107 smbc_lseek_ctx(SMBCCTX *context,
112 extern BOOL in_client;
115 * Is the logging working / configfile read ?
117 static int smbc_initialized = 0;
120 hex2int( unsigned int _char )
122 if ( _char >= 'A' && _char <='F')
123 return _char - 'A' + 10;
124 if ( _char >= 'a' && _char <='f')
125 return _char - 'a' + 10;
126 if ( _char >= '0' && _char <='9')
134 * Convert strings of %xx to their single character equivalent. Each 'x' must
135 * be a valid hexadecimal digit, or that % sequence is left undecoded.
137 * dest may, but need not be, the same pointer as src.
139 * Returns the number of % sequences which could not be converted due to lack
140 * of two following hexadecimal digits.
143 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
145 int old_length = strlen(src);
151 if ( old_length == 0 ) {
156 while ( i < old_length ) {
157 unsigned char character = src[ i++ ];
159 if (character == '%') {
160 int a = i+1 < old_length ? hex2int( src[i] ) : -1;
161 int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
163 /* Replace valid sequence */
164 if (a != -1 && b != -1) {
166 /* Replace valid %xx sequence with %dd */
167 character = (a * 16) + b;
169 if (character == '\0') {
170 break; /* Stop at %00 */
185 strncpy(dest, temp, max_dest_len - 1);
186 dest[max_dest_len - 1] = '\0';
194 * Convert any characters not specifically allowed in a URL into their %xx
197 * Returns the remaining buffer length.
200 smbc_urlencode(char * dest, char * src, int max_dest_len)
202 char hex[] = "0123456789ABCDEF";
204 for (; *src != '\0' && max_dest_len >= 3; src++) {
216 *dest++ = hex[(*src >> 4) & 0x0f];
217 *dest++ = hex[*src & 0x0f];
232 * Function to parse a path and turn it into components
234 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
235 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
236 * general format ("smb:" only; we do not look for "cifs:").
240 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
244 * smb:// Show all workgroups.
246 * The method of locating the list of workgroups varies
247 * depending upon the setting of the context variable
248 * context->options.browse_max_lmb_count. This value
249 * determine the maximum number of local master browsers to
250 * query for the list of workgroups. In order to ensure that
251 * a complete list of workgroups is obtained, all master
252 * browsers must be queried, but if there are many
253 * workgroups, the time spent querying can begin to add up.
254 * For small networks (not many workgroups), it is suggested
255 * that this variable be set to 0, indicating query all local
256 * master browsers. When the network has many workgroups, a
257 * reasonable setting for this variable might be around 3.
259 * smb://name/ if name<1D> or name<1B> exists, list servers in
260 * workgroup, else, if name<20> exists, list all shares
263 * If "options" are provided, this function returns the entire option list as a
264 * string, for later parsing by the caller. Note that currently, no options
268 static const char *smbc_prefix = "smb:";
271 smbc_parse_path(SMBCCTX *context,
273 char *workgroup, int workgroup_len,
274 char *server, int server_len,
275 char *share, int share_len,
276 char *path, int path_len,
277 char *user, int user_len,
278 char *password, int password_len,
279 char *options, int options_len)
287 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
290 * Assume we wont find an authentication domain to parse, so default
291 * to the workgroup in the provided context.
293 if (workgroup != NULL) {
294 strncpy(workgroup, context->workgroup, workgroup_len - 1);
295 workgroup[workgroup_len - 1] = '\0';
298 if (options != NULL && options_len > 0) {
299 options[0] = (char)0;
303 /* see if it has the right prefix */
304 len = strlen(smbc_prefix);
305 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
306 return -1; /* What about no smb: ? */
311 /* Watch the test below, we are testing to see if we should exit */
313 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
315 DEBUG(1, ("Invalid path (does not begin with smb://"));
320 p += 2; /* Skip the double slash */
322 /* See if any options were specified */
323 if ((q = strrchr(p, '?')) != NULL ) {
324 /* There are options. Null terminate here and point to them */
327 DEBUG(4, ("Found options '%s'", q));
329 /* Copy the options */
330 if (options != NULL && options_len > 0) {
331 safe_strcpy(options, q, options_len - 1);
340 strncpy(server, context->workgroup,
341 ((strlen(context->workgroup) < 16)
342 ? strlen(context->workgroup)
344 server[server_len - 1] = '\0';
350 * ok, its for us. Now parse out the server, share etc.
352 * However, we want to parse out [[domain;]user[:password]@] if it
356 /* check that '@' occurs before '/', if '/' exists at all */
357 q = strchr_m(p, '@');
358 r = strchr_m(p, '/');
359 if (q && (!r || q < r)) {
360 pstring username, passwd, domain;
361 const char *u = userinfo;
363 next_token_no_ltrim(&p, userinfo, "@", sizeof(fstring));
365 username[0] = passwd[0] = domain[0] = 0;
367 if (strchr_m(u, ';')) {
369 next_token_no_ltrim(&u, domain, ";", sizeof(fstring));
373 if (strchr_m(u, ':')) {
375 next_token_no_ltrim(&u, username, ":", sizeof(fstring));
382 pstrcpy(username, u);
386 if (domain[0] && workgroup) {
387 strncpy(workgroup, domain, workgroup_len - 1);
388 workgroup[workgroup_len - 1] = '\0';
392 strncpy(user, username, user_len - 1);
393 user[user_len - 1] = '\0';
397 strncpy(password, passwd, password_len - 1);
398 password[password_len - 1] = '\0';
403 if (!next_token(&p, server, "/", sizeof(fstring))) {
409 if (*p == (char)0) goto decoding; /* That's it ... */
411 if (!next_token(&p, share, "/", sizeof(fstring))) {
417 safe_strcpy(path, p, path_len - 1);
419 all_string_sub(path, "/", "\\", 0);
422 (void) smbc_urldecode(path, path, path_len);
423 (void) smbc_urldecode(server, server, server_len);
424 (void) smbc_urldecode(share, share, share_len);
425 (void) smbc_urldecode(user, user, user_len);
426 (void) smbc_urldecode(password, password, password_len);
432 * Verify that the options specified in a URL are valid
435 smbc_check_options(char *server,
440 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
441 "path='%s' options='%s'\n",
442 server, share, path, options));
444 /* No options at all is always ok */
445 if (! *options) return 0;
447 /* Currently, we don't support any options. */
452 * Convert an SMB error into a UNIX error ...
455 smbc_errno(SMBCCTX *context,
458 int ret = cli_errno(c);
460 if (cli_is_dos_error(c)) {
464 cli_dos_error(c, &eclass, &ecode);
466 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
467 (int)eclass, (int)ecode, (int)ecode, ret));
471 status = cli_nt_error(c);
473 DEBUG(3,("smbc errno %s -> %d\n",
474 nt_errstr(status), ret));
481 * Check a server for being alive and well.
482 * returns 0 if the server is in shape. Returns 1 on error
484 * Also useable outside libsmbclient to enable external cache
485 * to do some checks too.
488 smbc_check_server(SMBCCTX * context,
491 if ( send_keepalive(server->cli->fd) == False )
494 /* connection is ok */
499 * Remove a server from the cached server list it's unused.
500 * On success, 0 is returned. 1 is returned if the server could not be removed.
502 * Also useable outside libsmbclient
505 smbc_remove_unused_server(SMBCCTX * context,
510 /* are we being fooled ? */
511 if (!context || !context->internal ||
512 !context->internal->_initialized || !srv) return 1;
515 /* Check all open files/directories for a relation with this server */
516 for (file = context->internal->_files; file; file=file->next) {
517 if (file->srv == srv) {
519 DEBUG(3, ("smbc_remove_usused_server: "
520 "%p still used by %p.\n",
526 DLIST_REMOVE(context->internal->_servers, srv);
528 cli_shutdown(srv->cli);
531 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
533 context->callbacks.remove_cached_srv_fn(context, srv);
541 find_server(SMBCCTX *context,
553 srv = context->callbacks.get_cached_srv_fn(context, server, share,
554 workgroup, username);
556 if (!auth_called && !srv && (!username[0] || !password[0])) {
557 if (context->internal->_auth_fn_with_context != NULL) {
558 context->internal->_auth_fn_with_context(
561 workgroup, sizeof(fstring),
562 username, sizeof(fstring),
563 password, sizeof(fstring));
565 context->callbacks.auth_fn(
567 workgroup, sizeof(fstring),
568 username, sizeof(fstring),
569 password, sizeof(fstring));
573 * However, smbc_auth_fn may have picked up info relating to
574 * an existing connection, so try for an existing connection
578 goto check_server_cache;
583 if (context->callbacks.check_server_fn(context, srv)) {
585 * This server is no good anymore
586 * Try to remove it and check for more possible
587 * servers in the cache
589 if (context->callbacks.remove_unused_server_fn(context,
592 * We could not remove the server completely,
593 * remove it from the cache so we will not get
594 * it again. It will be removed when the last
595 * file/dir is closed.
597 context->callbacks.remove_cached_srv_fn(context,
602 * Maybe there are more cached connections to this
605 goto check_server_cache;
615 * Connect to a server, possibly on an existing connection
617 * Here, what we want to do is: If the server and username
618 * match an existing connection, reuse that, otherwise, establish a
621 * If we have to create a new connection, call the auth_fn to get the
622 * info we need, unless the username and password were passed in.
626 smbc_server(SMBCCTX *context,
627 BOOL connect_if_not_found,
636 struct nmb_name called, calling;
637 const char *server_n = server;
640 int tried_reverse = 0;
643 const char *username_used;
648 if (server[0] == 0) {
653 /* Look for a cached connection */
654 srv = find_server(context, server, share,
655 workgroup, username, password);
658 * If we found a connection and we're only allowed one share per
661 if (srv && *share != '\0' && context->options.one_share_per_server) {
664 * ... then if there's no current connection to the share,
665 * connect to it. find_server(), or rather the function
666 * pointed to by context->callbacks.get_cached_srv_fn which
667 * was called by find_server(), will have issued a tree
668 * disconnect if the requested share is not the same as the
669 * one that was already connected.
671 if (srv->cli->cnum == (uint16) -1) {
672 /* Ensure we have accurate auth info */
673 if (context->internal->_auth_fn_with_context != NULL) {
674 context->internal->_auth_fn_with_context(
677 workgroup, sizeof(fstring),
678 username, sizeof(fstring),
679 password, sizeof(fstring));
681 context->callbacks.auth_fn(
683 workgroup, sizeof(fstring),
684 username, sizeof(fstring),
685 password, sizeof(fstring));
688 if (! cli_send_tconX(srv->cli, share, "?????",
689 password, strlen(password)+1)) {
691 errno = smbc_errno(context, srv->cli);
692 cli_shutdown(srv->cli);
694 context->callbacks.remove_cached_srv_fn(context,
700 * Regenerate the dev value since it's based on both
704 srv->dev = (dev_t)(str_checksum(server) ^
705 str_checksum(share));
710 /* If we have a connection... */
713 /* ... then we're done here. Give 'em what they came for. */
717 /* If we're not asked to connect when a connection doesn't exist... */
718 if (! connect_if_not_found) {
719 /* ... then we're done here. */
723 make_nmb_name(&calling, context->netbios_name, 0x0);
724 make_nmb_name(&called , server, 0x20);
726 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
728 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
731 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
735 /* have to open a new connection */
736 if ((c = cli_initialise()) == NULL) {
741 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
742 c->use_kerberos = True;
744 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
745 c->fallback_after_kerberos = True;
748 c->timeout = context->timeout;
751 * Force use of port 139 for first try if share is $IPC, empty, or
752 * null, so browse lists can work
754 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
755 port_try_first = 139;
758 port_try_first = 445;
762 c->port = port_try_first;
764 if (!cli_connect(c, server_n, &ip)) {
766 /* First connection attempt failed. Try alternate port. */
767 c->port = port_try_next;
769 if (!cli_connect(c, server_n, &ip)) {
776 if (!cli_session_request(c, &calling, &called)) {
778 if (strcmp(called.name, "*SMBSERVER")) {
779 make_nmb_name(&called , "*SMBSERVER", 0x20);
781 } else { /* Try one more time, but ensure we don't loop */
783 /* Only try this if server is an IP address ... */
785 if (is_ipaddress(server) && !tried_reverse) {
787 struct in_addr rem_ip;
789 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
790 DEBUG(4, ("Could not convert IP address "
791 "%s to struct in_addr\n", server));
796 tried_reverse++; /* Yuck */
798 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
799 make_nmb_name(&called, remote_name, 0x20);
808 DEBUG(4,(" session request ok\n"));
810 if (!cli_negprot(c)) {
816 username_used = username;
818 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
819 password, strlen(password),
820 password, strlen(password),
823 /* Failed. Try an anonymous login, if allowed by flags. */
826 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
827 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
838 DEBUG(4,(" session setup ok\n"));
840 if (!cli_send_tconX(c, share, "?????",
841 password, strlen(password)+1)) {
842 errno = smbc_errno(context, c);
847 DEBUG(4,(" tconx ok\n"));
850 * Ok, we have got a nice connection
851 * Let's allocate a server structure.
854 srv = SMB_MALLOC_P(SMBCSRV);
862 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
863 srv->no_pathinfo = False;
864 srv->no_pathinfo2 = False;
865 srv->no_nt_session = False;
867 /* now add it to the cache (internal or external) */
868 /* Let the cache function set errno if it wants to */
870 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
871 int saved_errno = errno;
872 DEBUG(3, (" Failed to add server to cache\n"));
880 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
881 server, share, srv));
883 DLIST_ADD(context->internal->_servers, srv);
897 * Connect to a server for getting/setting attributes, possibly on an existing
898 * connection. This works similarly to smbc_server().
901 smbc_attr_server(SMBCCTX *context,
910 struct cli_state *ipc_cli;
911 struct rpc_pipe_client *pipe_hnd;
913 SMBCSRV *ipc_srv=NULL;
916 * See if we've already created this special connection. Reference
917 * our "special" share name '*IPC$', which is an impossible real share
918 * name due to the leading asterisk.
920 ipc_srv = find_server(context, server, "*IPC$",
921 workgroup, username, password);
924 /* We didn't find a cached connection. Get the password */
925 if (*password == '\0') {
926 /* ... then retrieve it now. */
927 if (context->internal->_auth_fn_with_context != NULL) {
928 context->internal->_auth_fn_with_context(
931 workgroup, sizeof(fstring),
932 username, sizeof(fstring),
933 password, sizeof(fstring));
935 context->callbacks.auth_fn(
937 workgroup, sizeof(fstring),
938 username, sizeof(fstring),
939 password, sizeof(fstring));
944 nt_status = cli_full_connection(&ipc_cli,
945 global_myname(), server,
946 &ip, 0, "IPC$", "?????",
950 if (! NT_STATUS_IS_OK(nt_status)) {
951 DEBUG(1,("cli_full_connection failed! (%s)\n",
952 nt_errstr(nt_status)));
957 ipc_srv = SMB_MALLOC_P(SMBCSRV);
960 cli_shutdown(ipc_cli);
964 ZERO_STRUCTP(ipc_srv);
965 ipc_srv->cli = ipc_cli;
968 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
972 DEBUG(1, ("cli_nt_session_open fail!\n"));
974 cli_shutdown(ipc_srv->cli);
980 * Some systems don't support
981 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
982 * so we might as well do it too.
985 nt_status = rpccli_lsa_open_policy(
987 ipc_srv->cli->mem_ctx,
989 GENERIC_EXECUTE_ACCESS,
992 if (!NT_STATUS_IS_OK(nt_status)) {
993 errno = smbc_errno(context, ipc_srv->cli);
994 cli_shutdown(ipc_srv->cli);
999 /* now add it to the cache (internal or external) */
1001 errno = 0; /* let cache function set errno if it likes */
1002 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
1007 DEBUG(3, (" Failed to add server to cache\n"));
1011 cli_shutdown(ipc_srv->cli);
1016 DLIST_ADD(context->internal->_servers, ipc_srv);
1023 * Routine to open() a file ...
1027 smbc_open_ctx(SMBCCTX *context,
1032 fstring server, share, user, password, workgroup;
1035 struct cli_state *targetcli;
1036 SMBCSRV *srv = NULL;
1037 SMBCFILE *file = NULL;
1040 if (!context || !context->internal ||
1041 !context->internal->_initialized) {
1043 errno = EINVAL; /* Best I can think of ... */
1055 if (smbc_parse_path(context, fname,
1056 workgroup, sizeof(workgroup),
1057 server, sizeof(server),
1058 share, sizeof(share),
1061 password, sizeof(password),
1067 if (user[0] == (char)0) fstrcpy(user, context->user);
1069 srv = smbc_server(context, True,
1070 server, share, workgroup, user, password);
1074 if (errno == EPERM) errno = EACCES;
1075 return NULL; /* smbc_server sets errno */
1079 /* Hmmm, the test for a directory is suspect here ... FIXME */
1081 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1088 file = SMB_MALLOC_P(SMBCFILE);
1099 /*d_printf(">>>open: resolving %s\n", path);*/
1100 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1102 d_printf("Could not resolve %s\n", path);
1106 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1108 if ( targetcli->dfsroot )
1111 pstrcpy(temppath, targetpath);
1112 cli_dfs_make_full_path( targetpath, targetcli->desthost, targetcli->share, temppath);
1115 if ((fd = cli_open(targetcli, targetpath, flags, DENY_NONE)) < 0) {
1117 /* Handle the error ... */
1120 errno = smbc_errno(context, targetcli);
1125 /* Fill in file struct */
1128 file->fname = SMB_STRDUP(fname);
1133 DLIST_ADD(context->internal->_files, file);
1136 * If the file was opened in O_APPEND mode, all write
1137 * operations should be appended to the file. To do that,
1138 * though, using this protocol, would require a getattrE()
1139 * call for each and every write, to determine where the end
1140 * of the file is. (There does not appear to be an append flag
1141 * in the protocol.) Rather than add all of that overhead of
1142 * retrieving the current end-of-file offset prior to each
1143 * write operation, we'll assume that most append operations
1144 * will continuously write, so we'll just set the offset to
1145 * the end of the file now and hope that's adequate.
1147 * Note to self: If this proves inadequate, and O_APPEND
1148 * should, in some cases, be forced for each write, add a
1149 * field in the context options structure, for
1150 * "strict_append_mode" which would select between the current
1151 * behavior (if FALSE) or issuing a getattrE() prior to each
1152 * write and forcing the write to the end of the file (if
1153 * TRUE). Adding that capability will likely require adding
1154 * an "append" flag into the _SMBCFILE structure to track
1155 * whether a file was opened in O_APPEND mode. -- djl
1157 if (flags & O_APPEND) {
1158 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1159 (void) smbc_close_ctx(context, file);
1169 /* Check if opendir needed ... */
1174 eno = smbc_errno(context, srv->cli);
1175 file = context->opendir(context, fname);
1176 if (!file) errno = eno;
1181 errno = EINVAL; /* FIXME, correct errno ? */
1187 * Routine to create a file
1190 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1193 smbc_creat_ctx(SMBCCTX *context,
1198 if (!context || !context->internal ||
1199 !context->internal->_initialized) {
1206 return smbc_open_ctx(context, path, creat_bits, mode);
1210 * Routine to read() a file ...
1214 smbc_read_ctx(SMBCCTX *context,
1220 fstring server, share, user, password;
1221 pstring path, targetpath;
1222 struct cli_state *targetcli;
1227 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1228 * appears to pass file->offset (which is type off_t) differently than
1229 * a local variable of type off_t. Using local variable "offset" in
1230 * the call to cli_read() instead of file->offset fixes a problem
1231 * retrieving data at an offset greater than 4GB.
1235 if (!context || !context->internal ||
1236 !context->internal->_initialized) {
1243 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1245 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1252 offset = file->offset;
1254 /* Check that the buffer exists ... */
1263 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1264 if (smbc_parse_path(context, file->fname,
1266 server, sizeof(server),
1267 share, sizeof(share),
1270 password, sizeof(password),
1276 /*d_printf(">>>read: resolving %s\n", path);*/
1277 if (!cli_resolve_path("", file->srv->cli, path,
1278 &targetcli, targetpath))
1280 d_printf("Could not resolve %s\n", path);
1283 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1285 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1289 errno = smbc_errno(context, targetcli);
1294 file->offset += ret;
1296 DEBUG(4, (" --> %d\n", ret));
1298 return ret; /* Success, ret bytes of data ... */
1303 * Routine to write() a file ...
1307 smbc_write_ctx(SMBCCTX *context,
1314 fstring server, share, user, password;
1315 pstring path, targetpath;
1316 struct cli_state *targetcli;
1318 /* First check all pointers before dereferencing them */
1320 if (!context || !context->internal ||
1321 !context->internal->_initialized) {
1328 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1335 /* Check that the buffer exists ... */
1344 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1346 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1347 if (smbc_parse_path(context, file->fname,
1349 server, sizeof(server),
1350 share, sizeof(share),
1353 password, sizeof(password),
1359 /*d_printf(">>>write: resolving %s\n", path);*/
1360 if (!cli_resolve_path("", file->srv->cli, path,
1361 &targetcli, targetpath))
1363 d_printf("Could not resolve %s\n", path);
1366 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1369 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1373 errno = smbc_errno(context, targetcli);
1378 file->offset += ret;
1380 return ret; /* Success, 0 bytes of data ... */
1384 * Routine to close() a file ...
1388 smbc_close_ctx(SMBCCTX *context,
1392 fstring server, share, user, password;
1393 pstring path, targetpath;
1394 struct cli_state *targetcli;
1396 if (!context || !context->internal ||
1397 !context->internal->_initialized) {
1404 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1414 return context->closedir(context, file);
1418 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1419 if (smbc_parse_path(context, file->fname,
1421 server, sizeof(server),
1422 share, sizeof(share),
1425 password, sizeof(password),
1431 /*d_printf(">>>close: resolving %s\n", path);*/
1432 if (!cli_resolve_path("", file->srv->cli, path,
1433 &targetcli, targetpath))
1435 d_printf("Could not resolve %s\n", path);
1438 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1440 if (!cli_close(targetcli, file->cli_fd)) {
1442 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1444 /* Deallocate slot and remove the server
1445 * from the server cache if unused */
1446 errno = smbc_errno(context, targetcli);
1448 DLIST_REMOVE(context->internal->_files, file);
1449 SAFE_FREE(file->fname);
1451 context->callbacks.remove_unused_server_fn(context, srv);
1457 DLIST_REMOVE(context->internal->_files, file);
1458 SAFE_FREE(file->fname);
1465 * Get info from an SMB server on a file. Use a qpathinfo call first
1466 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1469 smbc_getatr(SMBCCTX * context,
1474 struct timespec *create_time_ts,
1475 struct timespec *access_time_ts,
1476 struct timespec *write_time_ts,
1477 struct timespec *change_time_ts,
1482 struct cli_state *targetcli;
1485 if (!context || !context->internal ||
1486 !context->internal->_initialized) {
1493 /* path fixup for . and .. */
1494 if (strequal(path, ".") || strequal(path, ".."))
1495 pstrcpy(fixedpath, "\\");
1498 pstrcpy(fixedpath, path);
1499 trim_string(fixedpath, NULL, "\\..");
1500 trim_string(fixedpath, NULL, "\\.");
1502 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1504 if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
1506 d_printf("Couldn't resolve %s\n", path);
1510 if ( targetcli->dfsroot )
1513 pstrcpy(temppath, targetpath);
1514 cli_dfs_make_full_path(targetpath, targetcli->desthost,
1515 targetcli->share, temppath);
1518 if (!srv->no_pathinfo2 &&
1519 cli_qpathinfo2(targetcli, targetpath,
1528 /* if this is NT then don't bother with the getatr */
1529 if (targetcli->capabilities & CAP_NT_SMBS) {
1534 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1536 struct timespec w_time_ts;
1538 w_time_ts = convert_time_t_to_timespec(write_time);
1540 if (write_time_ts != NULL) {
1541 *write_time_ts = w_time_ts;
1544 if (create_time_ts != NULL) {
1545 *create_time_ts = w_time_ts;
1548 if (access_time_ts != NULL) {
1549 *access_time_ts = w_time_ts;
1552 if (change_time_ts != NULL) {
1553 *change_time_ts = w_time_ts;
1556 srv->no_pathinfo2 = True;
1566 * Set file info on an SMB server. Use setpathinfo call first. If that
1567 * fails, use setattrE..
1569 * Access and modification time parameters are always used and must be
1570 * provided. Create time, if zero, will be determined from the actual create
1571 * time of the file. If non-zero, the create time will be set as well.
1573 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1576 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1587 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1588 * modern function for "new code" to be using, and it works given a
1589 * filename rather than requiring that the file be opened to have its
1590 * attributes manipulated.
1592 if (srv->no_pathinfo ||
1593 ! cli_setpathinfo(srv->cli, path,
1601 * setpathinfo is not supported; go to plan B.
1603 * cli_setatr() does not work on win98, and it also doesn't
1604 * support setting the access time (only the modification
1605 * time), so in all cases, we open the specified file and use
1606 * cli_setattrE() which should work on all OS versions, and
1607 * supports both times.
1610 /* Don't try {q,set}pathinfo() again, with this server */
1611 srv->no_pathinfo = True;
1614 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1616 errno = smbc_errno(context, srv->cli);
1620 /* Set the new attributes */
1621 ret = cli_setattrE(srv->cli, fd,
1626 /* Close the file */
1627 cli_close(srv->cli, fd);
1630 * Unfortunately, setattrE() doesn't have a provision for
1631 * setting the access mode (attributes). We'll have to try
1632 * cli_setatr() for that, and with only this parameter, it
1633 * seems to work on win98.
1635 if (ret && mode != (uint16) -1) {
1636 ret = cli_setatr(srv->cli, path, mode, 0);
1640 errno = smbc_errno(context, srv->cli);
1649 * Routine to unlink() a file
1653 smbc_unlink_ctx(SMBCCTX *context,
1656 fstring server, share, user, password, workgroup;
1657 pstring path, targetpath;
1658 struct cli_state *targetcli;
1659 SMBCSRV *srv = NULL;
1661 if (!context || !context->internal ||
1662 !context->internal->_initialized) {
1664 errno = EINVAL; /* Best I can think of ... */
1676 if (smbc_parse_path(context, fname,
1677 workgroup, sizeof(workgroup),
1678 server, sizeof(server),
1679 share, sizeof(share),
1682 password, sizeof(password),
1688 if (user[0] == (char)0) fstrcpy(user, context->user);
1690 srv = smbc_server(context, True,
1691 server, share, workgroup, user, password);
1695 return -1; /* smbc_server sets errno */
1699 /*d_printf(">>>unlink: resolving %s\n", path);*/
1700 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1702 d_printf("Could not resolve %s\n", path);
1705 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1707 if (!cli_unlink(targetcli, targetpath)) {
1709 errno = smbc_errno(context, targetcli);
1711 if (errno == EACCES) { /* Check if the file is a directory */
1716 struct timespec write_time_ts;
1717 struct timespec access_time_ts;
1718 struct timespec change_time_ts;
1721 if (!smbc_getatr(context, srv, path, &mode, &size,
1728 /* Hmmm, bad error ... What? */
1730 errno = smbc_errno(context, targetcli);
1736 if (IS_DOS_DIR(mode))
1739 errno = saverr; /* Restore this */
1748 return 0; /* Success ... */
1753 * Routine to rename() a file
1757 smbc_rename_ctx(SMBCCTX *ocontext,
1773 pstring targetpath1;
1774 pstring targetpath2;
1775 struct cli_state *targetcli1;
1776 struct cli_state *targetcli2;
1777 SMBCSRV *srv = NULL;
1779 if (!ocontext || !ncontext ||
1780 !ocontext->internal || !ncontext->internal ||
1781 !ocontext->internal->_initialized ||
1782 !ncontext->internal->_initialized) {
1784 errno = EINVAL; /* Best I can think of ... */
1789 if (!oname || !nname) {
1796 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1798 smbc_parse_path(ocontext, oname,
1799 workgroup, sizeof(workgroup),
1800 server1, sizeof(server1),
1801 share1, sizeof(share1),
1802 path1, sizeof(path1),
1803 user1, sizeof(user1),
1804 password1, sizeof(password1),
1807 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1809 smbc_parse_path(ncontext, nname,
1811 server2, sizeof(server2),
1812 share2, sizeof(share2),
1813 path2, sizeof(path2),
1814 user2, sizeof(user2),
1815 password2, sizeof(password2),
1818 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1820 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1821 strcmp(user1, user2)) {
1823 /* Can't rename across file systems, or users?? */
1830 srv = smbc_server(ocontext, True,
1831 server1, share1, workgroup, user1, password1);
1838 /*d_printf(">>>rename: resolving %s\n", path1);*/
1839 if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
1841 d_printf("Could not resolve %s\n", path1);
1844 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1845 /*d_printf(">>>rename: resolving %s\n", path2);*/
1846 if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
1848 d_printf("Could not resolve %s\n", path2);
1851 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1853 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1854 strcmp(targetcli1->share, targetcli2->share))
1856 /* can't rename across file systems */
1862 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1863 int eno = smbc_errno(ocontext, targetcli1);
1865 if (eno != EEXIST ||
1866 !cli_unlink(targetcli1, targetpath2) ||
1867 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1875 return 0; /* Success */
1880 * A routine to lseek() a file
1884 smbc_lseek_ctx(SMBCCTX *context,
1890 fstring server, share, user, password;
1891 pstring path, targetpath;
1892 struct cli_state *targetcli;
1894 if (!context || !context->internal ||
1895 !context->internal->_initialized) {
1902 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1912 return -1; /* Can't lseek a dir ... */
1918 file->offset = offset;
1922 file->offset += offset;
1926 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1927 if (smbc_parse_path(context, file->fname,
1929 server, sizeof(server),
1930 share, sizeof(share),
1933 password, sizeof(password),
1940 /*d_printf(">>>lseek: resolving %s\n", path);*/
1941 if (!cli_resolve_path("", file->srv->cli, path,
1942 &targetcli, targetpath))
1944 d_printf("Could not resolve %s\n", path);
1947 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1949 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
1950 &size, NULL, NULL, NULL, NULL, NULL))
1952 SMB_OFF_T b_size = size;
1953 if (!cli_getattrE(targetcli, file->cli_fd,
1954 NULL, &b_size, NULL, NULL, NULL))
1961 file->offset = size + offset;
1970 return file->offset;
1975 * Generate an inode number from file name for those things that need it
1979 smbc_inode(SMBCCTX *context,
1983 if (!context || !context->internal ||
1984 !context->internal->_initialized) {
1991 if (!*name) return 2; /* FIXME, why 2 ??? */
1992 return (ino_t)str_checksum(name);
1997 * Routine to put basic stat info into a stat structure ... Used by stat and
2002 smbc_setup_stat(SMBCCTX *context,
2011 if (IS_DOS_DIR(mode)) {
2012 st->st_mode = SMBC_DIR_MODE;
2014 st->st_mode = SMBC_FILE_MODE;
2017 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2018 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2019 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2020 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2023 #ifdef HAVE_STAT_ST_BLKSIZE
2024 st->st_blksize = 512;
2026 #ifdef HAVE_STAT_ST_BLOCKS
2027 st->st_blocks = (size+511)/512;
2029 st->st_uid = getuid();
2030 st->st_gid = getgid();
2032 if (IS_DOS_DIR(mode)) {
2038 if (st->st_ino == 0) {
2039 st->st_ino = smbc_inode(context, fname);
2042 return True; /* FIXME: Is this needed ? */
2047 * Routine to stat a file given a name
2051 smbc_stat_ctx(SMBCCTX *context,
2062 struct timespec write_time_ts;
2063 struct timespec access_time_ts;
2064 struct timespec change_time_ts;
2069 if (!context || !context->internal ||
2070 !context->internal->_initialized) {
2072 errno = EINVAL; /* Best I can think of ... */
2084 DEBUG(4, ("smbc_stat(%s)\n", fname));
2086 if (smbc_parse_path(context, fname,
2087 workgroup, sizeof(workgroup),
2088 server, sizeof(server),
2089 share, sizeof(share),
2092 password, sizeof(password),
2098 if (user[0] == (char)0) fstrcpy(user, context->user);
2100 srv = smbc_server(context, True,
2101 server, share, workgroup, user, password);
2104 return -1; /* errno set by smbc_server */
2107 if (!smbc_getatr(context, srv, path, &mode, &size,
2114 errno = smbc_errno(context, srv->cli);
2121 smbc_setup_stat(context, st, path, size, mode);
2123 set_atimespec(st, access_time_ts);
2124 set_ctimespec(st, change_time_ts);
2125 set_mtimespec(st, write_time_ts);
2126 st->st_dev = srv->dev;
2133 * Routine to stat a file given an fd
2137 smbc_fstat_ctx(SMBCCTX *context,
2141 struct timespec change_time_ts;
2142 struct timespec access_time_ts;
2143 struct timespec write_time_ts;
2152 struct cli_state *targetcli;
2155 if (!context || !context->internal ||
2156 !context->internal->_initialized) {
2163 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2172 return context->fstatdir(context, file, st);
2176 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2177 if (smbc_parse_path(context, file->fname,
2179 server, sizeof(server),
2180 share, sizeof(share),
2183 password, sizeof(password),
2189 /*d_printf(">>>fstat: resolving %s\n", path);*/
2190 if (!cli_resolve_path("", file->srv->cli, path,
2191 &targetcli, targetpath))
2193 d_printf("Could not resolve %s\n", path);
2196 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2198 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2205 time_t change_time, access_time, write_time;
2207 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2208 &change_time, &access_time, &write_time)) {
2214 change_time_ts = convert_time_t_to_timespec(change_time);
2215 access_time_ts = convert_time_t_to_timespec(access_time);
2216 write_time_ts = convert_time_t_to_timespec(write_time);
2221 smbc_setup_stat(context, st, file->fname, size, mode);
2223 set_atimespec(st, access_time_ts);
2224 set_ctimespec(st, change_time_ts);
2225 set_mtimespec(st, write_time_ts);
2226 st->st_dev = file->srv->dev;
2233 * Routine to open a directory
2234 * We accept the URL syntax explained in smbc_parse_path(), above.
2238 smbc_remove_dir(SMBCFILE *dir)
2240 struct smbc_dir_list *d,*f;
2247 SAFE_FREE(f->dirent);
2252 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2257 add_dirent(SMBCFILE *dir,
2259 const char *comment,
2262 struct smbc_dirent *dirent;
2264 int name_length = (name == NULL ? 0 : strlen(name));
2265 int comment_len = (comment == NULL ? 0 : strlen(comment));
2268 * Allocate space for the dirent, which must be increased by the
2269 * size of the name and the comment and 1 each for the null terminator.
2272 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2274 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2278 dir->dir_error = ENOMEM;
2283 ZERO_STRUCTP(dirent);
2285 if (dir->dir_list == NULL) {
2287 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2288 if (!dir->dir_list) {
2291 dir->dir_error = ENOMEM;
2295 ZERO_STRUCTP(dir->dir_list);
2297 dir->dir_end = dir->dir_next = dir->dir_list;
2301 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2303 if (!dir->dir_end->next) {
2306 dir->dir_error = ENOMEM;
2310 ZERO_STRUCTP(dir->dir_end->next);
2312 dir->dir_end = dir->dir_end->next;
2315 dir->dir_end->next = NULL;
2316 dir->dir_end->dirent = dirent;
2318 dirent->smbc_type = type;
2319 dirent->namelen = name_length;
2320 dirent->commentlen = comment_len;
2321 dirent->dirlen = size;
2324 * dirent->namelen + 1 includes the null (no null termination needed)
2325 * Ditto for dirent->commentlen.
2326 * The space for the two null bytes was allocated.
2328 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2329 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2330 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2337 list_unique_wg_fn(const char *name,
2339 const char *comment,
2342 SMBCFILE *dir = (SMBCFILE *)state;
2343 struct smbc_dir_list *dir_list;
2344 struct smbc_dirent *dirent;
2348 dirent_type = dir->dir_type;
2350 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2352 /* An error occurred, what do we do? */
2353 /* FIXME: Add some code here */
2356 /* Point to the one just added */
2357 dirent = dir->dir_end->dirent;
2359 /* See if this was a duplicate */
2360 for (dir_list = dir->dir_list;
2361 dir_list != dir->dir_end;
2362 dir_list = dir_list->next) {
2364 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2365 /* Duplicate. End end of list need to be removed. */
2369 if (do_remove && dir_list->next == dir->dir_end) {
2370 /* Found the end of the list. Remove it. */
2371 dir->dir_end = dir_list;
2372 free(dir_list->next);
2374 dir_list->next = NULL;
2381 list_fn(const char *name,
2383 const char *comment,
2386 SMBCFILE *dir = (SMBCFILE *)state;
2390 * We need to process the type a little ...
2392 * Disk share = 0x00000000
2393 * Print share = 0x00000001
2394 * Comms share = 0x00000002 (obsolete?)
2395 * IPC$ share = 0x00000003
2397 * administrative shares:
2398 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2401 if (dir->dir_type == SMBC_FILE_SHARE) {
2404 case 0 | 0x80000000:
2406 dirent_type = SMBC_FILE_SHARE;
2410 dirent_type = SMBC_PRINTER_SHARE;
2414 dirent_type = SMBC_COMMS_SHARE;
2417 case 3 | 0x80000000:
2419 dirent_type = SMBC_IPC_SHARE;
2423 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2428 dirent_type = dir->dir_type;
2431 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2433 /* An error occurred, what do we do? */
2434 /* FIXME: Add some code here */
2440 dir_list_fn(const char *mnt,
2446 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2447 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2449 /* Handle an error ... */
2451 /* FIXME: Add some code ... */
2458 net_share_enum_rpc(struct cli_state *cli,
2459 void (*fn)(const char *name,
2461 const char *comment,
2468 uint32 info_level = 1;
2469 uint32 preferred_len = 0xffffffff;
2471 SRV_SHARE_INFO_CTR ctr;
2473 fstring comment = "";
2475 struct rpc_pipe_client *pipe_hnd;
2478 /* Open the server service pipe */
2479 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2481 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2485 /* Allocate a context for parsing and for the entries in "ctr" */
2486 mem_ctx = talloc_init("libsmbclient: net_share_enum_rpc");
2487 if (mem_ctx == NULL) {
2488 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2489 cli_rpc_pipe_close(pipe_hnd);
2493 /* Issue the NetShareEnum RPC call and retrieve the response */
2494 init_enum_hnd(&enum_hnd, 0);
2495 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2502 /* Was it successful? */
2503 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2504 /* Nope. Go clean up. */
2508 /* For each returned entry... */
2509 for (i = 0; i < ctr.num_entries; i++) {
2511 /* pull out the share name */
2512 rpcstr_pull_unistr2_fstring(
2513 name, &ctr.share.info1[i].info_1_str.uni_netname);
2515 /* pull out the share's comment */
2516 rpcstr_pull_unistr2_fstring(
2517 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2519 /* Get the type value */
2520 type = ctr.share.info1[i].info_1.type;
2522 /* Add this share to the list */
2523 (*fn)(name, type, comment, state);
2527 /* Close the server service pipe */
2528 cli_rpc_pipe_close(pipe_hnd);
2530 /* Free all memory which was allocated for this request */
2531 TALLOC_FREE(mem_ctx);
2533 /* Tell 'em if it worked */
2534 return W_ERROR_IS_OK(result) ? 0 : -1;
2540 smbc_opendir_ctx(SMBCCTX *context,
2543 fstring server, share, user, password, options;
2548 SMBCSRV *srv = NULL;
2549 SMBCFILE *dir = NULL;
2550 struct in_addr rem_ip;
2552 if (!context || !context->internal ||
2553 !context->internal->_initialized) {
2554 DEBUG(4, ("no valid context\n"));
2555 errno = EINVAL + 8192;
2561 DEBUG(4, ("no valid fname\n"));
2562 errno = EINVAL + 8193;
2566 if (smbc_parse_path(context, fname,
2567 workgroup, sizeof(workgroup),
2568 server, sizeof(server),
2569 share, sizeof(share),
2572 password, sizeof(password),
2573 options, sizeof(options))) {
2574 DEBUG(4, ("no valid path\n"));
2575 errno = EINVAL + 8194;
2579 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2580 "path='%s' options='%s'\n",
2581 fname, server, share, path, options));
2583 /* Ensure the options are valid */
2584 if (smbc_check_options(server, share, path, options)) {
2585 DEBUG(4, ("unacceptable options (%s)\n", options));
2586 errno = EINVAL + 8195;
2590 if (user[0] == (char)0) fstrcpy(user, context->user);
2592 dir = SMB_MALLOC_P(SMBCFILE);
2604 dir->fname = SMB_STRDUP(fname);
2608 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2610 if (server[0] == (char)0) {
2615 struct ip_service *ip_list;
2616 struct ip_service server_addr;
2617 struct user_auth_info u_info;
2618 struct cli_state *cli;
2620 if (share[0] != (char)0 || path[0] != (char)0) {
2622 errno = EINVAL + 8196;
2624 SAFE_FREE(dir->fname);
2630 /* Determine how many local master browsers to query */
2631 max_lmb_count = (context->options.browse_max_lmb_count == 0
2633 : context->options.browse_max_lmb_count);
2635 pstrcpy(u_info.username, user);
2636 pstrcpy(u_info.password, password);
2639 * We have server and share and path empty but options
2640 * requesting that we scan all master browsers for their list
2641 * of workgroups/domains. This implies that we must first try
2642 * broadcast queries to find all master browsers, and if that
2643 * doesn't work, then try our other methods which return only
2644 * a single master browser.
2648 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
2652 if (!find_master_ip(workgroup, &server_addr.ip)) {
2655 SAFE_FREE(dir->fname);
2662 ip_list = &server_addr;
2666 for (i = 0; i < count && i < max_lmb_count; i++) {
2667 DEBUG(99, ("Found master browser %d of %d: %s\n",
2668 i+1, MAX(count, max_lmb_count),
2669 inet_ntoa(ip_list[i].ip)));
2671 cli = get_ipc_connect_master_ip(&ip_list[i],
2672 workgroup, &u_info);
2673 /* cli == NULL is the master browser refused to talk or
2674 could not be found */
2678 fstrcpy(server, cli->desthost);
2681 DEBUG(4, ("using workgroup %s %s\n",
2682 workgroup, server));
2685 * For each returned master browser IP address, get a
2686 * connection to IPC$ on the server if we do not
2687 * already have one, and determine the
2688 * workgroups/domains that it knows about.
2691 srv = smbc_server(context, True, server, "IPC$",
2692 workgroup, user, password);
2698 dir->dir_type = SMBC_WORKGROUP;
2700 /* Now, list the stuff ... */
2702 if (!cli_NetServerEnum(srv->cli,
2704 SV_TYPE_DOMAIN_ENUM,
2714 * Server not an empty string ... Check the rest and see what
2717 if (*share == '\0') {
2718 if (*path != '\0') {
2720 /* Should not have empty share with path */
2721 errno = EINVAL + 8197;
2723 SAFE_FREE(dir->fname);
2731 * We don't know if <server> is really a server name
2732 * or is a workgroup/domain name. If we already have
2733 * a server structure for it, we'll use it.
2734 * Otherwise, check to see if <server><1D>,
2735 * <server><1B>, or <server><20> translates. We check
2736 * to see if <server> is an IP address first.
2740 * See if we have an existing server. Do not
2741 * establish a connection if one does not already
2744 srv = smbc_server(context, False, server, "IPC$",
2745 workgroup, user, password);
2748 * If no existing server and not an IP addr, look for
2752 !is_ipaddress(server) &&
2753 (resolve_name(server, &rem_ip, 0x1d) || /* LMB */
2754 resolve_name(server, &rem_ip, 0x1b) )) { /* DMB */
2758 dir->dir_type = SMBC_SERVER;
2761 * Get the backup list ...
2763 if (!name_status_find(server, 0, 0,
2764 rem_ip, buserver)) {
2766 DEBUG(0, ("Could not get name of "
2767 "local/domain master browser "
2768 "for server %s\n", server));
2770 SAFE_FREE(dir->fname);
2779 * Get a connection to IPC$ on the server if
2780 * we do not already have one
2782 srv = smbc_server(context, True,
2784 workgroup, user, password);
2786 DEBUG(0, ("got no contact to IPC$\n"));
2788 SAFE_FREE(dir->fname);
2797 /* Now, list the servers ... */
2798 if (!cli_NetServerEnum(srv->cli, server,
2799 0x0000FFFE, list_fn,
2803 SAFE_FREE(dir->fname);
2809 (resolve_name(server, &rem_ip, 0x20))) {
2811 /* If we hadn't found the server, get one now */
2813 srv = smbc_server(context, True,
2821 SAFE_FREE(dir->fname);
2828 dir->dir_type = SMBC_FILE_SHARE;
2831 /* List the shares ... */
2833 if (net_share_enum_rpc(
2836 (void *) dir) < 0 &&
2842 errno = cli_errno(srv->cli);
2844 SAFE_FREE(dir->fname);
2851 /* Neither the workgroup nor server exists */
2852 errno = ECONNREFUSED;
2854 SAFE_FREE(dir->fname);
2863 * The server and share are specified ... work from
2867 struct cli_state *targetcli;
2869 /* We connect to the server and list the directory */
2870 dir->dir_type = SMBC_FILE_SHARE;
2872 srv = smbc_server(context, True, server, share,
2873 workgroup, user, password);
2878 SAFE_FREE(dir->fname);
2887 /* Now, list the files ... */
2889 p = path + strlen(path);
2890 pstrcat(path, "\\*");
2892 if (!cli_resolve_path("", srv->cli, path,
2893 &targetcli, targetpath))
2895 d_printf("Could not resolve %s\n", path);
2897 SAFE_FREE(dir->fname);
2903 if (cli_list(targetcli, targetpath,
2904 aDIR | aSYSTEM | aHIDDEN,
2905 dir_list_fn, (void *)dir) < 0) {
2908 SAFE_FREE(dir->fname);
2911 errno = smbc_errno(context, targetcli);
2913 if (errno == EINVAL) {
2915 * See if they asked to opendir something
2916 * other than a directory. If so, the
2917 * converted error value we got would have
2918 * been EINVAL rather than ENOTDIR.
2920 *p = '\0'; /* restore original path */
2922 if (smbc_getatr(context, srv, path,
2924 NULL, NULL, NULL, NULL,
2926 ! IS_DOS_DIR(mode)) {
2928 /* It is. Correct the error value */
2940 DLIST_ADD(context->internal->_files, dir);
2946 * Routine to close a directory
2950 smbc_closedir_ctx(SMBCCTX *context,
2954 if (!context || !context->internal ||
2955 !context->internal->_initialized) {
2962 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2969 smbc_remove_dir(dir); /* Clean it up */
2971 DLIST_REMOVE(context->internal->_files, dir);
2975 SAFE_FREE(dir->fname);
2976 SAFE_FREE(dir); /* Free the space too */
2984 smbc_readdir_internal(SMBCCTX * context,
2985 struct smbc_dirent *dest,
2986 struct smbc_dirent *src,
2987 int max_namebuf_len)
2989 if (context->options.urlencode_readdir_entries) {
2991 /* url-encode the name. get back remaining buffer space */
2993 smbc_urlencode(dest->name, src->name, max_namebuf_len);
2995 /* We now know the name length */
2996 dest->namelen = strlen(dest->name);
2998 /* Save the pointer to the beginning of the comment */
2999 dest->comment = dest->name + dest->namelen + 1;
3001 /* Copy the comment */
3002 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3003 dest->comment[max_namebuf_len - 1] = '\0';
3005 /* Save other fields */
3006 dest->smbc_type = src->smbc_type;
3007 dest->commentlen = strlen(dest->comment);
3008 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3012 /* No encoding. Just copy the entry as is. */
3013 memcpy(dest, src, src->dirlen);
3014 dest->comment = (char *)(&dest->name + src->namelen + 1);
3020 * Routine to get a directory entry
3023 struct smbc_dirent *
3024 smbc_readdir_ctx(SMBCCTX *context,
3028 struct smbc_dirent *dirp, *dirent;
3030 /* Check that all is ok first ... */
3032 if (!context || !context->internal ||
3033 !context->internal->_initialized) {
3036 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3041 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3044 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3049 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3052 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3057 if (!dir->dir_next) {
3061 dirent = dir->dir_next->dirent;
3069 dirp = (struct smbc_dirent *)context->internal->_dirent;
3070 maxlen = (sizeof(context->internal->_dirent) -
3071 sizeof(struct smbc_dirent));
3073 smbc_readdir_internal(context, dirp, dirent, maxlen);
3075 dir->dir_next = dir->dir_next->next;
3081 * Routine to get directory entries
3085 smbc_getdents_ctx(SMBCCTX *context,
3087 struct smbc_dirent *dirp,
3093 char *ndir = (char *)dirp;
3094 struct smbc_dir_list *dirlist;
3096 /* Check that all is ok first ... */
3098 if (!context || !context->internal ||
3099 !context->internal->_initialized) {
3106 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3113 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3121 * Now, retrieve the number of entries that will fit in what was passed
3122 * We have to figure out if the info is in the list, or we need to
3123 * send a request to the server to get the info.
3126 while ((dirlist = dir->dir_next)) {
3127 struct smbc_dirent *dirent;
3129 if (!dirlist->dirent) {
3131 errno = ENOENT; /* Bad error */
3136 /* Do urlencoding of next entry, if so selected */
3137 dirent = (struct smbc_dirent *)context->internal->_dirent;
3138 maxlen = (sizeof(context->internal->_dirent) -
3139 sizeof(struct smbc_dirent));
3140 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3142 reqd = dirent->dirlen;
3146 if (rem < count) { /* We managed to copy something */
3152 else { /* Nothing copied ... */
3154 errno = EINVAL; /* Not enough space ... */
3161 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3163 ((struct smbc_dirent *)ndir)->comment =
3164 (char *)(&((struct smbc_dirent *)ndir)->name +
3172 dir->dir_next = dirlist = dirlist -> next;
3183 * Routine to create a directory ...
3187 smbc_mkdir_ctx(SMBCCTX *context,
3197 pstring path, targetpath;
3198 struct cli_state *targetcli;
3200 if (!context || !context->internal ||
3201 !context->internal->_initialized) {
3215 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3217 if (smbc_parse_path(context, fname,
3218 workgroup, sizeof(workgroup),
3219 server, sizeof(server),
3220 share, sizeof(share),
3223 password, sizeof(password),
3229 if (user[0] == (char)0) fstrcpy(user, context->user);
3231 srv = smbc_server(context, True,
3232 server, share, workgroup, user, password);
3236 return -1; /* errno set by smbc_server */
3240 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3241 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3243 d_printf("Could not resolve %s\n", path);
3246 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3248 if (!cli_mkdir(targetcli, targetpath)) {
3250 errno = smbc_errno(context, targetcli);
3260 * Our list function simply checks to see if a directory is not empty
3263 static int smbc_rmdir_dirempty = True;
3266 rmdir_list_fn(const char *mnt,
3271 if (strncmp(finfo->name, ".", 1) != 0 &&
3272 strncmp(finfo->name, "..", 2) != 0) {
3274 smbc_rmdir_dirempty = False;
3279 * Routine to remove a directory
3283 smbc_rmdir_ctx(SMBCCTX *context,
3294 struct cli_state *targetcli;
3296 if (!context || !context->internal ||
3297 !context->internal->_initialized) {
3311 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3313 if (smbc_parse_path(context, fname,
3314 workgroup, sizeof(workgroup),
3315 server, sizeof(server),
3316 share, sizeof(share),
3319 password, sizeof(password),
3326 if (user[0] == (char)0) fstrcpy(user, context->user);
3328 srv = smbc_server(context, True,
3329 server, share, workgroup, user, password);
3333 return -1; /* errno set by smbc_server */
3337 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3338 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3340 d_printf("Could not resolve %s\n", path);
3343 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3346 if (!cli_rmdir(targetcli, targetpath)) {
3348 errno = smbc_errno(context, targetcli);
3350 if (errno == EACCES) { /* Check if the dir empty or not */
3352 /* Local storage to avoid buffer overflows */
3355 smbc_rmdir_dirempty = True; /* Make this so ... */
3357 pstrcpy(lpath, targetpath);
3358 pstrcat(lpath, "\\*");
3360 if (cli_list(targetcli, lpath,
3361 aDIR | aSYSTEM | aHIDDEN,
3362 rmdir_list_fn, NULL) < 0) {
3364 /* Fix errno to ignore latest error ... */
3365 DEBUG(5, ("smbc_rmdir: "
3366 "cli_list returned an error: %d\n",
3367 smbc_errno(context, targetcli)));
3372 if (smbc_rmdir_dirempty)
3388 * Routine to return the current directory position
3392 smbc_telldir_ctx(SMBCCTX *context,
3395 off_t ret_val; /* Squash warnings about cast */
3397 if (!context || !context->internal ||
3398 !context->internal->_initialized) {
3405 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3412 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3420 * We return the pointer here as the offset
3422 ret_val = (off_t)(long)dir->dir_next;
3428 * A routine to run down the list and see if the entry is OK
3431 struct smbc_dir_list *
3432 smbc_check_dir_ent(struct smbc_dir_list *list,
3433 struct smbc_dirent *dirent)
3436 /* Run down the list looking for what we want */
3440 struct smbc_dir_list *tmp = list;
3444 if (tmp->dirent == dirent)
3453 return NULL; /* Not found, or an error */
3459 * Routine to seek on a directory
3463 smbc_lseekdir_ctx(SMBCCTX *context,
3467 long int l_offset = offset; /* Handle problems of size */
3468 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3469 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3471 if (!context || !context->internal ||
3472 !context->internal->_initialized) {
3479 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3486 /* Now, check what we were passed and see if it is OK ... */
3488 if (dirent == NULL) { /* Seek to the begining of the list */
3490 dir->dir_next = dir->dir_list;
3495 /* Now, run down the list and make sure that the entry is OK */
3496 /* This may need to be changed if we change the format of the list */
3498 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3500 errno = EINVAL; /* Bad entry */
3505 dir->dir_next = list_ent;
3512 * Routine to fstat a dir
3516 smbc_fstatdir_ctx(SMBCCTX *context,
3521 if (!context || !context->internal ||
3522 !context->internal->_initialized) {
3529 /* No code yet ... */
3536 smbc_chmod_ctx(SMBCCTX *context,
3549 if (!context || !context->internal ||
3550 !context->internal->_initialized) {
3552 errno = EINVAL; /* Best I can think of ... */
3564 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3566 if (smbc_parse_path(context, fname,
3567 workgroup, sizeof(workgroup),
3568 server, sizeof(server),
3569 share, sizeof(share),
3572 password, sizeof(password),
3578 if (user[0] == (char)0) fstrcpy(user, context->user);
3580 srv = smbc_server(context, True,
3581 server, share, workgroup, user, password);
3584 return -1; /* errno set by smbc_server */
3589 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3590 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3591 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3592 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3594 if (!cli_setatr(srv->cli, path, mode, 0)) {
3595 errno = smbc_errno(context, srv->cli);
3603 smbc_utimes_ctx(SMBCCTX *context,
3605 struct timeval *tbuf)
3617 if (!context || !context->internal ||
3618 !context->internal->_initialized) {
3620 errno = EINVAL; /* Best I can think of ... */
3633 access_time = write_time = time(NULL);
3635 access_time = tbuf[0].tv_sec;
3636 write_time = tbuf[1].tv_sec;
3645 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3646 atimebuf[sizeof(atimebuf) - 1] = '\0';
3647 if ((p = strchr(atimebuf, '\n')) != NULL) {
3651 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3652 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3653 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3657 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3658 fname, atimebuf, mtimebuf);
3661 if (smbc_parse_path(context, fname,
3662 workgroup, sizeof(workgroup),
3663 server, sizeof(server),
3664 share, sizeof(share),
3667 password, sizeof(password),
3673 if (user[0] == (char)0) fstrcpy(user, context->user);
3675 srv = smbc_server(context, True,
3676 server, share, workgroup, user, password);
3679 return -1; /* errno set by smbc_server */
3682 if (!smbc_setatr(context, srv, path,
3683 0, access_time, write_time, 0, 0)) {
3684 return -1; /* errno set by smbc_setatr */
3691 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
3692 However NT4 gives a "The information may have been modified by a
3693 computer running Windows NT 5.0" if denied ACEs do not appear before
3697 ace_compare(SEC_ACE *ace1,
3700 if (sec_ace_equal(ace1, ace2))
3703 if (ace1->type != ace2->type)
3704 return ace2->type - ace1->type;
3706 if (sid_compare(&ace1->trustee, &ace2->trustee))
3707 return sid_compare(&ace1->trustee, &ace2->trustee);
3709 if (ace1->flags != ace2->flags)
3710 return ace1->flags - ace2->flags;
3712 if (ace1->info.mask != ace2->info.mask)
3713 return ace1->info.mask - ace2->info.mask;
3715 if (ace1->size != ace2->size)
3716 return ace1->size - ace2->size;
3718 return memcmp(ace1, ace2, sizeof(SEC_ACE));
3723 sort_acl(SEC_ACL *the_acl)
3726 if (!the_acl) return;
3728 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]),
3729 QSORT_CAST ace_compare);
3731 for (i=1;i<the_acl->num_aces;) {
3732 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
3734 for (j=i; j<the_acl->num_aces-1; j++) {
3735 the_acl->ace[j] = the_acl->ace[j+1];
3737 the_acl->num_aces--;
3744 /* convert a SID to a string, either numeric or username/group */
3746 convert_sid_to_string(struct cli_state *ipc_cli,
3752 char **domains = NULL;
3753 char **names = NULL;
3754 enum SID_NAME_USE *types = NULL;
3755 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3756 sid_to_string(str, sid);
3759 return; /* no lookup desired */
3766 /* Ask LSA to convert the sid to a name */
3768 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ipc_cli->mem_ctx,
3769 pol, 1, sid, &domains,
3771 !domains || !domains[0] || !names || !names[0]) {
3777 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3778 domains[0], lp_winbind_separator(),
3782 /* convert a string to a SID, either numeric or username/group */
3784 convert_string_to_sid(struct cli_state *ipc_cli,
3790 enum SID_NAME_USE *types = NULL;
3791 DOM_SID *sids = NULL;
3793 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3800 if (strncmp(str, "S-", 2) == 0) {
3801 return string_to_sid(sid, str);
3808 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx,
3809 pol, 1, &str, NULL, &sids,
3815 sid_copy(sid, &sids[0]);
3822 /* parse an ACE in the same format as print_ace() */
3824 parse_ace(struct cli_state *ipc_cli,
3834 unsigned int aflags;
3838 const struct perm_value *v;
3844 /* These values discovered by inspection */
3845 static const struct perm_value special_values[] = {
3846 { "R", 0x00120089 },
3847 { "W", 0x00120116 },
3848 { "X", 0x001200a0 },
3849 { "D", 0x00010000 },
3850 { "P", 0x00040000 },
3851 { "O", 0x00080000 },
3855 static const struct perm_value standard_values[] = {
3856 { "READ", 0x001200a9 },
3857 { "CHANGE", 0x001301bf },
3858 { "FULL", 0x001f01ff },
3864 p = strchr_m(str,':');
3865 if (!p) return False;
3868 /* Try to parse numeric form */
3870 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3871 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3875 /* Try to parse text form */
3877 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3882 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3886 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3887 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3888 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3889 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3894 /* Only numeric form accepted for flags at present */
3896 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3897 sscanf(tok, "%i", &aflags))) {
3901 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3905 if (strncmp(tok, "0x", 2) == 0) {
3906 if (sscanf(tok, "%i", &amask) != 1) {
3912 for (v = standard_values; v->perm; v++) {
3913 if (strcmp(tok, v->perm) == 0) {
3924 for (v = special_values; v->perm; v++) {
3925 if (v->perm[0] == *p) {
3931 if (!found) return False;
3941 init_sec_ace(ace, &sid, atype, mask, aflags);
3945 /* add an ACE to a list of ACEs in a SEC_ACL */
3947 add_ace(SEC_ACL **the_acl,
3955 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
3959 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
3962 memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
3963 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
3964 newacl = make_sec_acl(ctx, (*the_acl)->revision,
3965 1+(*the_acl)->num_aces, aces);
3967 (*the_acl) = newacl;
3972 /* parse a ascii version of a security descriptor */
3974 sec_desc_parse(TALLOC_CTX *ctx,
3975 struct cli_state *ipc_cli,
3980 const char *p = str;
3982 SEC_DESC *ret = NULL;
3984 DOM_SID *grp_sid=NULL;
3985 DOM_SID *owner_sid=NULL;
3989 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
3991 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
3992 revision = strtol(tok+9, NULL, 16);
3996 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
3998 DEBUG(5, ("OWNER specified more than once!\n"));
4001 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4003 !convert_string_to_sid(ipc_cli, pol,
4005 owner_sid, tok+6)) {
4006 DEBUG(5, ("Failed to parse owner sid\n"));
4012 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4014 DEBUG(5, ("OWNER specified more than once!\n"));
4017 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4019 !convert_string_to_sid(ipc_cli, pol,
4021 owner_sid, tok+7)) {
4022 DEBUG(5, ("Failed to parse owner sid\n"));
4028 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4030 DEBUG(5, ("GROUP specified more than once!\n"));
4033 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4035 !convert_string_to_sid(ipc_cli, pol,
4038 DEBUG(5, ("Failed to parse group sid\n"));
4044 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4046 DEBUG(5, ("GROUP specified more than once!\n"));
4049 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4051 !convert_string_to_sid(ipc_cli, pol,
4054 DEBUG(5, ("Failed to parse group sid\n"));
4060 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4062 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4063 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4066 if(!add_ace(&dacl, &ace, ctx)) {
4067 DEBUG(5, ("Failed to add ACL %s\n", tok));
4073 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4075 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4076 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4079 if(!add_ace(&dacl, &ace, ctx)) {
4080 DEBUG(5, ("Failed to add ACL %s\n", tok));
4086 DEBUG(5, ("Failed to parse security descriptor\n"));
4090 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4091 owner_sid, grp_sid, NULL, dacl, &sd_size);
4095 SAFE_FREE(owner_sid);
4101 /* Obtain the current dos attributes */
4102 static DOS_ATTR_DESC *
4103 dos_attr_query(SMBCCTX *context,
4105 const char *filename,
4108 struct timespec create_time_ts;
4109 struct timespec write_time_ts;
4110 struct timespec access_time_ts;
4111 struct timespec change_time_ts;
4114 SMB_INO_T inode = 0;
4117 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4123 /* Obtain the DOS attributes */
4124 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4132 errno = smbc_errno(context, srv->cli);
4133 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4140 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4141 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4142 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4143 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4150 /* parse a ascii version of a security descriptor */
4152 dos_attr_parse(SMBCCTX *context,
4158 const char *p = str;
4161 const char * create_time_attr;
4162 const char * access_time_attr;
4163 const char * write_time_attr;
4164 const char * change_time_attr;
4167 /* Determine whether to use old-style or new-style attribute names */
4168 if (context->internal->_full_time_names) {
4169 /* new-style names */
4170 attr_strings.create_time_attr = "CREATE_TIME";
4171 attr_strings.access_time_attr = "ACCESS_TIME";
4172 attr_strings.write_time_attr = "WRITE_TIME";
4173 attr_strings.change_time_attr = "CHANGE_TIME";
4175 /* old-style names */
4176 attr_strings.create_time_attr = NULL;
4177 attr_strings.access_time_attr = "A_TIME";
4178 attr_strings.write_time_attr = "M_TIME";
4179 attr_strings.change_time_attr = "C_TIME";
4182 /* if this is to set the entire ACL... */
4184 /* ... then increment past the first colon if there is one */
4185 if ((p = strchr(str, ':')) != NULL) {
4192 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4194 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4195 dad->mode = strtol(tok+5, NULL, 16);
4199 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4200 dad->size = (SMB_OFF_T)atof(tok+5);
4204 n = strlen(attr_strings.access_time_attr);
4205 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4206 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4210 n = strlen(attr_strings.change_time_attr);
4211 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4212 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4216 n = strlen(attr_strings.write_time_attr);
4217 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4218 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4222 n = strlen(attr_strings.create_time_attr);
4223 if (attr_strings.create_time_attr != NULL &&
4224 StrnCaseCmp(tok, attr_strings.create_time_attr, n) == 0) {
4225 dad->create_time = (time_t)strtol(tok+n+1, NULL, 10);
4229 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4230 dad->inode = (SMB_INO_T)atof(tok+6);
4236 /*****************************************************
4237 Retrieve the acls for a file.
4238 *******************************************************/
4241 cacl_get(SMBCCTX *context,
4244 struct cli_state *ipc_cli,
4260 BOOL exclude_nt_revision = False;
4261 BOOL exclude_nt_owner = False;
4262 BOOL exclude_nt_group = False;
4263 BOOL exclude_nt_acl = False;
4264 BOOL exclude_dos_mode = False;
4265 BOOL exclude_dos_size = False;
4266 BOOL exclude_dos_create_time = False;
4267 BOOL exclude_dos_access_time = False;
4268 BOOL exclude_dos_write_time = False;
4269 BOOL exclude_dos_change_time = False;
4270 BOOL exclude_dos_inode = False;
4271 BOOL numeric = True;
4272 BOOL determine_size = (bufsize == 0);
4276 fstring name_sandbox;
4280 struct timespec create_time_ts;
4281 struct timespec write_time_ts;
4282 struct timespec access_time_ts;
4283 struct timespec change_time_ts;
4284 time_t create_time = (time_t)0;
4285 time_t write_time = (time_t)0;
4286 time_t access_time = (time_t)0;
4287 time_t change_time = (time_t)0;
4291 struct cli_state *cli = srv->cli;
4293 const char * create_time_attr;
4294 const char * access_time_attr;
4295 const char * write_time_attr;
4296 const char * change_time_attr;
4299 const char * create_time_attr;
4300 const char * access_time_attr;
4301 const char * write_time_attr;
4302 const char * change_time_attr;
4303 } excl_attr_strings;
4305 /* Determine whether to use old-style or new-style attribute names */
4306 if (context->internal->_full_time_names) {
4307 /* new-style names */
4308 attr_strings.create_time_attr = "CREATE_TIME";
4309 attr_strings.access_time_attr = "ACCESS_TIME";
4310 attr_strings.write_time_attr = "WRITE_TIME";
4311 attr_strings.change_time_attr = "CHANGE_TIME";
4313 excl_attr_strings.create_time_attr = "CREATE_TIME";
4314 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4315 excl_attr_strings.write_time_attr = "WRITE_TIME";
4316 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4318 /* old-style names */
4319 attr_strings.create_time_attr = NULL;
4320 attr_strings.access_time_attr = "A_TIME";
4321 attr_strings.write_time_attr = "M_TIME";
4322 attr_strings.change_time_attr = "C_TIME";
4324 excl_attr_strings.create_time_attr = NULL;
4325 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4326 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4327 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4330 /* Copy name so we can strip off exclusions (if any are specified) */
4331 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4333 /* Ensure name is null terminated */
4334 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4336 /* Play in the sandbox */
4337 name = name_sandbox;
4339 /* If there are any exclusions, point to them and mask them from name */
4340 if ((pExclude = strchr(name, '!')) != NULL)
4345 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4346 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4347 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4348 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4349 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4350 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4351 numeric = (* (name + strlen(name) - 1) != '+');
4353 /* Look for exclusions from "all" requests */
4354 if (all || all_nt || all_dos) {
4356 /* Exclusions are delimited by '!' */
4359 pExclude = (p == NULL ? NULL : p + 1)) {
4361 /* Find end of this exclusion name */
4362 if ((p = strchr(pExclude, '!')) != NULL)
4367 /* Which exclusion name is this? */
4368 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4369 exclude_nt_revision = True;
4371 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4372 exclude_nt_owner = True;
4374 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4375 exclude_nt_group = True;
4377 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4378 exclude_nt_acl = True;
4380 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4381 exclude_dos_mode = True;
4383 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4384 exclude_dos_size = True;
4386 else if (excl_attr_strings.create_time_attr != NULL &&
4387 StrCaseCmp(pExclude,
4388 excl_attr_strings.change_time_attr) == 0) {
4389 exclude_dos_create_time = True;
4391 else if (StrCaseCmp(pExclude,
4392 excl_attr_strings.access_time_attr) == 0) {
4393 exclude_dos_access_time = True;
4395 else if (StrCaseCmp(pExclude,
4396 excl_attr_strings.write_time_attr) == 0) {
4397 exclude_dos_write_time = True;
4399 else if (StrCaseCmp(pExclude,
4400 excl_attr_strings.change_time_attr) == 0) {
4401 exclude_dos_change_time = True;
4403 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4404 exclude_dos_inode = True;
4407 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4418 * If we are (possibly) talking to an NT or new system and some NT
4419 * attributes have been requested...
4421 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4422 /* Point to the portion after "system.nt_sec_desc." */
4423 name += 19; /* if (all) this will be invalid but unused */
4425 /* ... then obtain any NT attributes which were requested */
4426 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4429 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4430 filename, cli_errstr(cli)));
4435 sd = cli_query_secdesc(cli, fnum, ctx);
4439 ("cacl_get Failed to query old descriptor\n"));
4444 cli_close(cli, fnum);
4446 if (! exclude_nt_revision) {
4447 if (all || all_nt) {
4448 if (determine_size) {
4449 p = talloc_asprintf(ctx,
4458 n = snprintf(buf, bufsize,
4462 } else if (StrCaseCmp(name, "revision") == 0) {
4463 if (determine_size) {
4464 p = talloc_asprintf(ctx, "%d",
4472 n = snprintf(buf, bufsize, "%d",
4477 if (!determine_size && n > bufsize) {
4486 if (! exclude_nt_owner) {
4487 /* Get owner and group sid */
4488 if (sd->owner_sid) {
4489 convert_sid_to_string(ipc_cli, pol,
4494 fstrcpy(sidstr, "");
4497 if (all || all_nt) {
4498 if (determine_size) {
4499 p = talloc_asprintf(ctx, ",OWNER:%s",
4507 n = snprintf(buf, bufsize,
4508 ",OWNER:%s", sidstr);
4510 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4511 if (determine_size) {
4512 p = talloc_asprintf(ctx, "%s", sidstr);
4519 n = snprintf(buf, bufsize, "%s",
4524 if (!determine_size && n > bufsize) {
4533 if (! exclude_nt_group) {
4535 convert_sid_to_string(ipc_cli, pol,
4539 fstrcpy(sidstr, "");
4542 if (all || all_nt) {
4543 if (determine_size) {
4544 p = talloc_asprintf(ctx, ",GROUP:%s",
4552 n = snprintf(buf, bufsize,
4553 ",GROUP:%s", sidstr);
4555 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4556 if (determine_size) {
4557 p = talloc_asprintf(ctx, "%s", sidstr);
4564 n = snprintf(buf, bufsize,
4569 if (!determine_size && n > bufsize) {
4578 if (! exclude_nt_acl) {
4579 /* Add aces to value buffer */
4580 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4582 SEC_ACE *ace = &sd->dacl->ace[i];
4583 convert_sid_to_string(ipc_cli, pol,
4587 if (all || all_nt) {
4588 if (determine_size) {
4589 p = talloc_asprintf(
4605 ",ACL:%s:%d/%d/0x%08x",
4611 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4612 StrCaseCmp(name+3, sidstr) == 0) ||
4613 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4614 StrCaseCmp(name+4, sidstr) == 0)) {
4615 if (determine_size) {
4616 p = talloc_asprintf(
4628 n = snprintf(buf, bufsize,
4634 } else if (all_nt_acls) {
4635 if (determine_size) {
4636 p = talloc_asprintf(
4638 "%s%s:%d/%d/0x%08x",
4650 n = snprintf(buf, bufsize,
4651 "%s%s:%d/%d/0x%08x",
4669 /* Restore name pointer to its original value */
4673 if (all || some_dos) {
4674 /* Point to the portion after "system.dos_attr." */
4675 name += 16; /* if (all) this will be invalid but unused */
4677 /* Obtain the DOS attributes */
4678 if (!smbc_getatr(context, srv, filename, &mode, &size,
4685 errno = smbc_errno(context, srv->cli);
4690 create_time = convert_timespec_to_time_t(create_time_ts);
4691 access_time = convert_timespec_to_time_t(access_time_ts);
4692 write_time = convert_timespec_to_time_t(write_time_ts);
4693 change_time = convert_timespec_to_time_t(change_time_ts);
4695 if (! exclude_dos_mode) {
4696 if (all || all_dos) {
4697 if (determine_size) {
4698 p = talloc_asprintf(ctx,
4711 n = snprintf(buf, bufsize,
4719 } else if (StrCaseCmp(name, "mode") == 0) {
4720 if (determine_size) {
4721 p = talloc_asprintf(ctx, "0x%x", mode);
4728 n = snprintf(buf, bufsize,
4733 if (!determine_size && n > bufsize) {
4742 if (! exclude_dos_size) {
4743 if (all || all_dos) {
4744 if (determine_size) {
4745 p = talloc_asprintf(
4755 n = snprintf(buf, bufsize,
4759 } else if (StrCaseCmp(name, "size") == 0) {
4760 if (determine_size) {
4761 p = talloc_asprintf(
4771 n = snprintf(buf, bufsize,
4777 if (!determine_size && n > bufsize) {
4786 if (! exclude_dos_create_time &&
4787 attr_strings.create_time_attr != NULL) {
4788 if (all || all_dos) {
4789 if (determine_size) {
4790 p = talloc_asprintf(ctx,
4792 attr_strings.create_time_attr,
4800 n = snprintf(buf, bufsize,
4802 attr_strings.create_time_attr,
4805 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
4806 if (determine_size) {
4807 p = talloc_asprintf(ctx, "%lu", create_time);
4814 n = snprintf(buf, bufsize,
4815 "%lu", create_time);
4819 if (!determine_size && n > bufsize) {
4828 if (! exclude_dos_access_time) {
4829 if (all || all_dos) {
4830 if (determine_size) {
4831 p = talloc_asprintf(ctx,
4833 attr_strings.access_time_attr,
4841 n = snprintf(buf, bufsize,
4843 attr_strings.access_time_attr,
4846 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
4847 if (determine_size) {
4848 p = talloc_asprintf(ctx, "%lu", access_time);
4855 n = snprintf(buf, bufsize,
4856 "%lu", access_time);
4860 if (!determine_size && n > bufsize) {
4869 if (! exclude_dos_write_time) {
4870 if (all || all_dos) {
4871 if (determine_size) {
4872 p = talloc_asprintf(ctx,
4874 attr_strings.write_time_attr,
4882 n = snprintf(buf, bufsize,
4884 attr_strings.write_time_attr,
4887 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
4888 if (determine_size) {
4889 p = talloc_asprintf(ctx, "%lu", write_time);
4896 n = snprintf(buf, bufsize,
4901 if (!determine_size && n > bufsize) {
4910 if (! exclude_dos_change_time) {
4911 if (all || all_dos) {
4912 if (determine_size) {
4913 p = talloc_asprintf(ctx,
4915 attr_strings.change_time_attr,
4923 n = snprintf(buf, bufsize,
4925 attr_strings.change_time_attr,
4928 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
4929 if (determine_size) {
4930 p = talloc_asprintf(ctx, "%lu", change_time);
4937 n = snprintf(buf, bufsize,
4938 "%lu", change_time);
4942 if (!determine_size && n > bufsize) {
4951 if (! exclude_dos_inode) {
4952 if (all || all_dos) {
4953 if (determine_size) {
4954 p = talloc_asprintf(
4964 n = snprintf(buf, bufsize,
4968 } else if (StrCaseCmp(name, "inode") == 0) {
4969 if (determine_size) {
4970 p = talloc_asprintf(
4980 n = snprintf(buf, bufsize,
4986 if (!determine_size && n > bufsize) {
4995 /* Restore name pointer to its original value */
5008 /*****************************************************
5009 set the ACLs on a file given an ascii description
5010 *******************************************************/
5012 cacl_set(TALLOC_CTX *ctx,
5013 struct cli_state *cli,
5014 struct cli_state *ipc_cli,
5016 const char *filename,
5017 const char *the_acl,
5023 SEC_DESC *sd = NULL, *old;
5024 SEC_ACL *dacl = NULL;
5025 DOM_SID *owner_sid = NULL;
5026 DOM_SID *grp_sid = NULL;
5031 BOOL numeric = True;
5033 /* the_acl will be null for REMOVE_ALL operations */
5035 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5039 /* if this is to set the entire ACL... */
5040 if (*the_acl == '*') {
5041 /* ... then increment past the first colon */
5045 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5046 CONST_DISCARD(char *, the_acl));
5054 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5055 that doesn't deref sd */
5057 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5062 /* The desired access below is the only one I could find that works
5063 with NT4, W2KP and Samba */
5065 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5068 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5069 filename, cli_errstr(cli)));
5074 old = cli_query_secdesc(cli, fnum, ctx);
5077 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5082 cli_close(cli, fnum);
5085 case SMBC_XATTR_MODE_REMOVE_ALL:
5086 old->dacl->num_aces = 0;
5087 SAFE_FREE(old->dacl->ace);
5088 SAFE_FREE(old->dacl);
5093 case SMBC_XATTR_MODE_REMOVE:
5094 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5097 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5098 if (sec_ace_equal(&sd->dacl->ace[i],
5099 &old->dacl->ace[j])) {
5101 for (k=j; k<old->dacl->num_aces-1;k++) {
5103 old->dacl->ace[k+1];
5105 old->dacl->num_aces--;
5106 if (old->dacl->num_aces == 0) {
5107 SAFE_FREE(old->dacl->ace);
5108 SAFE_FREE(old->dacl);
5125 case SMBC_XATTR_MODE_ADD:
5126 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5129 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5130 if (sid_equal(&sd->dacl->ace[i].trustee,
5131 &old->dacl->ace[j].trustee)) {
5132 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5137 old->dacl->ace[j] = sd->dacl->ace[i];
5143 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5149 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5150 add_ace(&old->dacl, &sd->dacl->ace[i], ctx);
5156 case SMBC_XATTR_MODE_SET:
5158 owner_sid = old->owner_sid;
5159 grp_sid = old->grp_sid;
5163 case SMBC_XATTR_MODE_CHOWN:
5164 owner_sid = sd->owner_sid;
5167 case SMBC_XATTR_MODE_CHGRP:
5168 grp_sid = sd->grp_sid;
5172 /* Denied ACE entries must come before allowed ones */
5173 sort_acl(old->dacl);
5175 /* Create new security descriptor and set it */
5176 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5177 owner_sid, grp_sid, NULL, dacl, &sd_size);
5179 fnum = cli_nt_create(cli, filename,
5180 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5183 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5184 filename, cli_errstr(cli)));
5189 if (!cli_set_secdesc(cli, fnum, sd)) {
5190 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5197 cli_close(cli, fnum);
5208 smbc_setxattr_ctx(SMBCCTX *context,
5229 const char * create_time_attr;
5230 const char * access_time_attr;
5231 const char * write_time_attr;
5232 const char * change_time_attr;
5235 if (!context || !context->internal ||
5236 !context->internal->_initialized) {
5238 errno = EINVAL; /* Best I can think of ... */
5250 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5251 fname, name, (int) size, (const char*)value));
5253 if (smbc_parse_path(context, fname,
5254 workgroup, sizeof(workgroup),
5255 server, sizeof(server),
5256 share, sizeof(share),
5259 password, sizeof(password),
5265 if (user[0] == (char)0) fstrcpy(user, context->user);
5267 srv = smbc_server(context, True,
5268 server, share, workgroup, user, password);
5270 return -1; /* errno set by smbc_server */
5273 if (! srv->no_nt_session) {
5274 ipc_srv = smbc_attr_server(context, server, share,
5275 workgroup, user, password,
5277 srv->no_nt_session = True;
5282 ctx = talloc_init("smbc_setxattr");
5289 * Are they asking to set the entire set of known attributes?
5291 if (StrCaseCmp(name, "system.*") == 0 ||
5292 StrCaseCmp(name, "system.*+") == 0) {
5295 talloc_asprintf(ctx, "%s:%s",
5296 name+7, (const char *) value);
5304 ret = cacl_set(ctx, srv->cli,
5305 ipc_srv->cli, &pol, path,
5308 ? SMBC_XATTR_MODE_SET
5309 : SMBC_XATTR_MODE_ADD),
5315 /* get a DOS Attribute Descriptor with current attributes */
5316 dad = dos_attr_query(context, ctx, path, srv);
5318 /* Overwrite old with new, using what was provided */
5319 dos_attr_parse(context, dad, srv, namevalue);
5321 /* Set the new DOS attributes */
5322 if (! smbc_setatr(context, srv, path,
5329 /* cause failure if NT failed too */
5334 /* we only fail if both NT and DOS sets failed */
5335 if (ret < 0 && ! dad) {
5336 ret = -1; /* in case dad was null */
5342 talloc_destroy(ctx);
5347 * Are they asking to set an access control element or to set
5348 * the entire access control list?
5350 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5351 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5352 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5353 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5354 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5358 talloc_asprintf(ctx, "%s:%s",
5359 name+19, (const char *) value);
5362 ret = -1; /* errno set by smbc_server() */
5364 else if (! namevalue) {
5368 ret = cacl_set(ctx, srv->cli,
5369 ipc_srv->cli, &pol, path,
5372 ? SMBC_XATTR_MODE_SET
5373 : SMBC_XATTR_MODE_ADD),
5376 talloc_destroy(ctx);
5381 * Are they asking to set the owner?
5383 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5384 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5388 talloc_asprintf(ctx, "%s:%s",
5389 name+19, (const char *) value);
5393 ret = -1; /* errno set by smbc_server() */
5395 else if (! namevalue) {
5399 ret = cacl_set(ctx, srv->cli,
5400 ipc_srv->cli, &pol, path,
5401 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5403 talloc_destroy(ctx);
5408 * Are they asking to set the group?
5410 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5411 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5415 talloc_asprintf(ctx, "%s:%s",
5416 name+19, (const char *) value);
5419 /* errno set by smbc_server() */
5422 else if (! namevalue) {
5426 ret = cacl_set(ctx, srv->cli,
5427 ipc_srv->cli, &pol, path,
5428 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5430 talloc_destroy(ctx);
5434 /* Determine whether to use old-style or new-style attribute names */
5435 if (context->internal->_full_time_names) {
5436 /* new-style names */
5437 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5438 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5439 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5440 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5442 /* old-style names */
5443 attr_strings.create_time_attr = NULL;
5444 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5445 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5446 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5450 * Are they asking to set a DOS attribute?
5452 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5453 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5454 (attr_strings.create_time_attr != NULL &&
5455 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5456 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5457 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5458 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5460 /* get a DOS Attribute Descriptor with current attributes */
5461 dad = dos_attr_query(context, ctx, path, srv);
5464 talloc_asprintf(ctx, "%s:%s",
5465 name+16, (const char *) value);
5470 /* Overwrite old with provided new params */
5471 dos_attr_parse(context, dad, srv, namevalue);
5473 /* Set the new DOS attributes */
5474 ret2 = smbc_setatr(context, srv, path,
5481 /* ret2 has True (success) / False (failure) */
5492 talloc_destroy(ctx);
5496 /* Unsupported attribute name */
5497 talloc_destroy(ctx);
5503 smbc_getxattr_ctx(SMBCCTX *context,
5521 const char * create_time_attr;
5522 const char * access_time_attr;
5523 const char * write_time_attr;
5524 const char * change_time_attr;
5528 if (!context || !context->internal ||
5529 !context->internal->_initialized) {
5531 errno = EINVAL; /* Best I can think of ... */
5543 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5545 if (smbc_parse_path(context, fname,
5546 workgroup, sizeof(workgroup),
5547 server, sizeof(server),
5548 share, sizeof(share),
5551 password, sizeof(password),
5557 if (user[0] == (char)0) fstrcpy(user, context->user);
5559 srv = smbc_server(context, True,
5560 server, share, workgroup, user, password);
5562 return -1; /* errno set by smbc_server */
5565 if (! srv->no_nt_session) {
5566 ipc_srv = smbc_attr_server(context, server, share,
5567 workgroup, user, password,
5570 srv->no_nt_session = True;
5576 ctx = talloc_init("smbc:getxattr");
5582 /* Determine whether to use old-style or new-style attribute names */
5583 if (context->internal->_full_time_names) {
5584 /* new-style names */
5585 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5586 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5587 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5588 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5590 /* old-style names */
5591 attr_strings.create_time_attr = NULL;
5592 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5593 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5594 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5597 /* Are they requesting a supported attribute? */
5598 if (StrCaseCmp(name, "system.*") == 0 ||
5599 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5600 StrCaseCmp(name, "system.*+") == 0 ||
5601 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5602 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5603 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5604 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5605 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5606 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5607 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5608 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5609 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5610 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5611 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5612 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5613 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5614 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5615 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5616 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5617 (attr_strings.create_time_attr != NULL &&
5618 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5619 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5620 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5621 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5622 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
5625 ret = cacl_get(context, ctx, srv,
5626 ipc_srv == NULL ? NULL : ipc_srv->cli,
5628 CONST_DISCARD(char *, name),
5629 CONST_DISCARD(char *, value), size);
5630 if (ret < 0 && errno == 0) {
5631 errno = smbc_errno(context, srv->cli);
5633 talloc_destroy(ctx);
5637 /* Unsupported attribute name */
5638 talloc_destroy(ctx);
5645 smbc_removexattr_ctx(SMBCCTX *context,
5661 if (!context || !context->internal ||
5662 !context->internal->_initialized) {
5664 errno = EINVAL; /* Best I can think of ... */
5676 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
5678 if (smbc_parse_path(context, fname,
5679 workgroup, sizeof(workgroup),
5680 server, sizeof(server),
5681 share, sizeof(share),
5684 password, sizeof(password),
5690 if (user[0] == (char)0) fstrcpy(user, context->user);
5692 srv = smbc_server(context, True,
5693 server, share, workgroup, user, password);
5695 return -1; /* errno set by smbc_server */
5698 if (! srv->no_nt_session) {
5699 ipc_srv = smbc_attr_server(context, server, share,
5700 workgroup, user, password,
5702 srv->no_nt_session = True;
5708 return -1; /* errno set by smbc_attr_server */
5711 ctx = talloc_init("smbc_removexattr");
5717 /* Are they asking to set the entire ACL? */
5718 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5719 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
5722 ret = cacl_set(ctx, srv->cli,
5723 ipc_srv->cli, &pol, path,
5724 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
5725 talloc_destroy(ctx);
5730 * Are they asking to remove one or more spceific security descriptor
5733 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5734 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5735 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5736 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5737 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5738 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5739 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5742 ret = cacl_set(ctx, srv->cli,
5743 ipc_srv->cli, &pol, path,
5744 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
5745 talloc_destroy(ctx);
5749 /* Unsupported attribute name */
5750 talloc_destroy(ctx);
5756 smbc_listxattr_ctx(SMBCCTX *context,
5762 * This isn't quite what listxattr() is supposed to do. This returns
5763 * the complete set of attribute names, always, rather than only those
5764 * attribute names which actually exist for a file. Hmmm...
5766 const char supported_old[] =
5769 "system.nt_sec_desc.revision\0"
5770 "system.nt_sec_desc.owner\0"
5771 "system.nt_sec_desc.owner+\0"
5772 "system.nt_sec_desc.group\0"
5773 "system.nt_sec_desc.group+\0"
5774 "system.nt_sec_desc.acl.*\0"
5775 "system.nt_sec_desc.acl\0"
5776 "system.nt_sec_desc.acl+\0"
5777 "system.nt_sec_desc.*\0"
5778 "system.nt_sec_desc.*+\0"
5779 "system.dos_attr.*\0"
5780 "system.dos_attr.mode\0"
5781 "system.dos_attr.c_time\0"
5782 "system.dos_attr.a_time\0"
5783 "system.dos_attr.m_time\0"
5785 const char supported_new[] =
5788 "system.nt_sec_desc.revision\0"
5789 "system.nt_sec_desc.owner\0"
5790 "system.nt_sec_desc.owner+\0"
5791 "system.nt_sec_desc.group\0"
5792 "system.nt_sec_desc.group+\0"
5793 "system.nt_sec_desc.acl.*\0"
5794 "system.nt_sec_desc.acl\0"
5795 "system.nt_sec_desc.acl+\0"
5796 "system.nt_sec_desc.*\0"
5797 "system.nt_sec_desc.*+\0"
5798 "system.dos_attr.*\0"
5799 "system.dos_attr.mode\0"
5800 "system.dos_attr.create_time\0"
5801 "system.dos_attr.access_time\0"
5802 "system.dos_attr.write_time\0"
5803 "system.dos_attr.change_time\0"
5805 const char * supported;
5807 if (context->internal->_full_time_names) {
5808 supported = supported_new;
5810 supported = supported_old;
5814 return sizeof(supported);
5817 if (sizeof(supported) > size) {
5822 /* this can't be strcpy() because there are embedded null characters */
5823 memcpy(list, supported, sizeof(supported));
5824 return sizeof(supported);
5829 * Open a print file to be written to by other calls
5833 smbc_open_print_job_ctx(SMBCCTX *context,
5842 if (!context || !context->internal ||
5843 !context->internal->_initialized) {
5857 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
5859 if (smbc_parse_path(context, fname,
5861 server, sizeof(server),
5862 share, sizeof(share),
5865 password, sizeof(password),
5871 /* What if the path is empty, or the file exists? */
5873 return context->open(context, fname, O_WRONLY, 666);
5878 * Routine to print a file on a remote server ...
5880 * We open the file, which we assume to be on a remote server, and then
5881 * copy it to a print file on the share specified by printq.
5885 smbc_print_file_ctx(SMBCCTX *c_file,
5897 if (!c_file || !c_file->internal->_initialized || !c_print ||
5898 !c_print->internal->_initialized) {
5905 if (!fname && !printq) {
5912 /* Try to open the file for reading ... */
5914 if ((long)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
5916 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
5917 return -1; /* smbc_open sets errno */
5921 /* Now, try to open the printer file for writing */
5923 if ((long)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
5925 saverr = errno; /* Save errno */
5926 c_file->close_fn(c_file, fid1);
5932 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
5936 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
5939 c_file->close_fn(c_file, fid1);
5940 c_print->close_fn(c_print, fid2);
5949 c_file->close_fn(c_file, fid1); /* We have to close these anyway */
5950 c_print->close_fn(c_print, fid2);
5964 * Routine to list print jobs on a printer share ...
5968 smbc_list_print_jobs_ctx(SMBCCTX *context,
5970 smbc_list_print_job_fn fn)
5980 if (!context || !context->internal ||
5981 !context->internal->_initialized) {
5995 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
5997 if (smbc_parse_path(context, fname,
5998 workgroup, sizeof(workgroup),
5999 server, sizeof(server),
6000 share, sizeof(share),
6003 password, sizeof(password),
6009 if (user[0] == (char)0) fstrcpy(user, context->user);
6011 srv = smbc_server(context, True,
6012 server, share, workgroup, user, password);
6016 return -1; /* errno set by smbc_server */
6020 if (cli_print_queue(srv->cli,
6021 (void (*)(struct print_job_info *))fn) < 0) {
6023 errno = smbc_errno(context, srv->cli);
6033 * Delete a print job from a remote printer share
6037 smbc_unlink_print_job_ctx(SMBCCTX *context,
6050 if (!context || !context->internal ||
6051 !context->internal->_initialized) {
6065 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6067 if (smbc_parse_path(context, fname,
6068 workgroup, sizeof(workgroup),
6069 server, sizeof(server),
6070 share, sizeof(share),
6073 password, sizeof(password),
6079 if (user[0] == (char)0) fstrcpy(user, context->user);
6081 srv = smbc_server(context, True,
6082 server, share, workgroup, user, password);
6086 return -1; /* errno set by smbc_server */
6090 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6093 errno = smbc_errno(context, srv->cli);
6094 else if (err == ERRnosuchprintjob)
6105 * Get a new empty handle to fill in with your own info
6108 smbc_new_context(void)
6112 context = SMB_MALLOC_P(SMBCCTX);
6118 ZERO_STRUCTP(context);
6120 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6121 if (!context->internal) {
6127 ZERO_STRUCTP(context->internal);
6130 /* ADD REASONABLE DEFAULTS */
6132 context->timeout = 20000; /* 20 seconds */
6134 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6135 context->options.urlencode_readdir_entries = False;/* backward compat */
6136 context->options.one_share_per_server = False;/* backward compat */
6138 context->open = smbc_open_ctx;
6139 context->creat = smbc_creat_ctx;
6140 context->read = smbc_read_ctx;
6141 context->write = smbc_write_ctx;
6142 context->close_fn = smbc_close_ctx;
6143 context->unlink = smbc_unlink_ctx;
6144 context->rename = smbc_rename_ctx;
6145 context->lseek = smbc_lseek_ctx;
6146 context->stat = smbc_stat_ctx;
6147 context->fstat = smbc_fstat_ctx;
6148 context->opendir = smbc_opendir_ctx;
6149 context->closedir = smbc_closedir_ctx;
6150 context->readdir = smbc_readdir_ctx;
6151 context->getdents = smbc_getdents_ctx;
6152 context->mkdir = smbc_mkdir_ctx;
6153 context->rmdir = smbc_rmdir_ctx;
6154 context->telldir = smbc_telldir_ctx;
6155 context->lseekdir = smbc_lseekdir_ctx;
6156 context->fstatdir = smbc_fstatdir_ctx;
6157 context->chmod = smbc_chmod_ctx;
6158 context->utimes = smbc_utimes_ctx;
6159 context->setxattr = smbc_setxattr_ctx;
6160 context->getxattr = smbc_getxattr_ctx;
6161 context->removexattr = smbc_removexattr_ctx;
6162 context->listxattr = smbc_listxattr_ctx;
6163 context->open_print_job = smbc_open_print_job_ctx;
6164 context->print_file = smbc_print_file_ctx;
6165 context->list_print_jobs = smbc_list_print_jobs_ctx;
6166 context->unlink_print_job = smbc_unlink_print_job_ctx;
6168 context->callbacks.check_server_fn = smbc_check_server;
6169 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6171 smbc_default_cache_functions(context);
6179 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6180 * and thus you'll be leaking memory if not handled properly.
6184 smbc_free_context(SMBCCTX *context,
6194 DEBUG(1,("Performing aggressive shutdown.\n"));
6196 f = context->internal->_files;
6198 context->close_fn(context, f);
6201 context->internal->_files = NULL;
6203 /* First try to remove the servers the nice way. */
6204 if (context->callbacks.purge_cached_fn(context)) {
6207 DEBUG(1, ("Could not purge all servers, "
6208 "Nice way shutdown failed.\n"));
6209 s = context->internal->_servers;
6211 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6213 cli_shutdown(s->cli);
6214 context->callbacks.remove_cached_srv_fn(context,
6217 DLIST_REMOVE(context->internal->_servers, s);
6221 context->internal->_servers = NULL;
6225 /* This is the polite way */
6226 if (context->callbacks.purge_cached_fn(context)) {
6227 DEBUG(1, ("Could not purge all servers, "
6228 "free_context failed.\n"));
6232 if (context->internal->_servers) {
6233 DEBUG(1, ("Active servers in context, "
6234 "free_context failed.\n"));
6238 if (context->internal->_files) {
6239 DEBUG(1, ("Active files in context, "
6240 "free_context failed.\n"));
6246 /* Things we have to clean up */
6247 SAFE_FREE(context->workgroup);
6248 SAFE_FREE(context->netbios_name);
6249 SAFE_FREE(context->user);
6251 DEBUG(3, ("Context %p succesfully freed\n", context));
6252 SAFE_FREE(context->internal);
6259 * Each time the context structure is changed, we have binary backward
6260 * compatibility issues. Instead of modifying the public portions of the
6261 * context structure to add new options, instead, we put them in the internal
6262 * portion of the context structure and provide a set function for these new
6266 smbc_option_set(SMBCCTX *context,
6268 ... /* option_value */)
6273 smbc_get_auth_data_with_context_fn auth_fn;
6277 va_start(ap, option_name);
6279 if (strcmp(option_name, "debug_to_stderr") == 0) {
6281 * Log to standard error instead of standard output.
6283 option_value.b = (BOOL) va_arg(ap, int);
6284 context->internal->_debug_stderr = option_value.b;
6286 } else if (strcmp(option_name, "full_time_names") == 0) {
6288 * Use new-style time attribute names, e.g. WRITE_TIME rather
6289 * than the old-style names such as M_TIME. This allows also
6290 * setting/getting CREATE_TIME which was previously
6291 * unimplemented. (Note that the old C_TIME was supposed to
6292 * be CHANGE_TIME but was confused and sometimes referred to
6295 option_value.b = (BOOL) va_arg(ap, int);
6296 context->internal->_full_time_names = option_value.b;
6298 } else if (strcmp(option_name, "auth_function") == 0) {
6300 * Use the new-style authentication function which includes
6303 option_value.auth_fn =
6304 va_arg(ap, smbc_get_auth_data_with_context_fn);
6305 context->internal->_auth_fn_with_context =
6306 option_value.auth_fn;
6307 } else if (strcmp(option_name, "user_data") == 0) {
6309 * Save a user data handle which may be retrieved by the user
6310 * with smbc_option_get()
6312 option_value.v = va_arg(ap, void *);
6313 context->internal->_user_data = option_value.v;
6321 * Retrieve the current value of an option
6324 smbc_option_get(SMBCCTX *context,
6327 if (strcmp(option_name, "debug_stderr") == 0) {
6329 * Log to standard error instead of standard output.
6331 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6332 return (void *) (intptr_t) context->internal->_debug_stderr;
6334 return (void *) context->internal->_debug_stderr;
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
6345 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6346 return (void *) (intptr_t) context->internal->_full_time_names;
6348 return (void *) context->internal->_full_time_names;
6351 } else if (strcmp(option_name, "auth_function") == 0) {
6353 * Use the new-style authentication function which includes
6356 return (void *) context->internal->_auth_fn_with_context;
6357 } else if (strcmp(option_name, "user_data") == 0) {
6359 * Save a user data handle which may be retrieved by the user
6360 * with smbc_option_get()
6362 return context->internal->_user_data;
6370 * Initialise the library etc
6372 * We accept a struct containing handle information.
6373 * valid values for info->debug from 0 to 100,
6374 * and insist that info->fn must be non-null.
6377 smbc_init_context(SMBCCTX *context)
6384 if (!context || !context->internal) {
6389 /* Do not initialise the same client twice */
6390 if (context->internal->_initialized) {
6394 if ((!context->callbacks.auth_fn &&
6395 !context->internal->_auth_fn_with_context) ||
6396 context->debug < 0 ||
6397 context->debug > 100) {
6404 if (!smbc_initialized) {
6406 * Do some library-wide intializations the first time we get
6409 BOOL conf_loaded = False;
6411 /* Set this to what the user wants */
6412 DEBUGLEVEL = context->debug;
6416 setup_logging("libsmbclient", True);
6417 if (context->internal->_debug_stderr) {
6419 x_setbuf(x_stderr, NULL);
6422 /* Here we would open the smb.conf file if needed ... */
6424 in_client = True; /* FIXME, make a param */
6426 home = getenv("HOME");
6428 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
6429 if (lp_load(conf, True, False, False, True)) {
6432 DEBUG(5, ("Could not load config file: %s\n",
6439 * Well, if that failed, try the dyn_CONFIGFILE
6440 * Which points to the standard locn, and if that
6441 * fails, silently ignore it and use the internal
6445 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6446 DEBUG(5, ("Could not load config file: %s\n",
6450 * We loaded the global config file. Now lets
6451 * load user-specific modifications to the
6454 slprintf(conf, sizeof(conf),
6455 "%s/.smb/smb.conf.append", home);
6456 if (!lp_load(conf, True, False, False, False)) {
6458 ("Could not append config file: "
6465 load_interfaces(); /* Load the list of interfaces ... */
6467 reopen_logs(); /* Get logging working ... */
6470 * Block SIGPIPE (from lib/util_sock.c: write())
6471 * It is not needed and should not stop execution
6473 BlockSignals(True, SIGPIPE);
6475 /* Done with one-time initialisation */
6476 smbc_initialized = 1;
6480 if (!context->user) {
6482 * FIXME: Is this the best way to get the user info?
6484 user = getenv("USER");
6485 /* walk around as "guest" if no username can be found */
6486 if (!user) context->user = SMB_STRDUP("guest");
6487 else context->user = SMB_STRDUP(user);
6490 if (!context->netbios_name) {
6492 * We try to get our netbios name from the config. If that
6493 * fails we fall back on constructing our netbios name from
6496 if (global_myname()) {
6497 context->netbios_name = SMB_STRDUP(global_myname());
6501 * Hmmm, I want to get hostname as well, but I am too
6502 * lazy for the moment
6505 context->netbios_name = (char *)SMB_MALLOC(17);
6506 if (!context->netbios_name) {
6510 slprintf(context->netbios_name, 16,
6511 "smbc%s%d", context->user, pid);
6515 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6517 if (!context->workgroup) {
6518 if (lp_workgroup()) {
6519 context->workgroup = SMB_STRDUP(lp_workgroup());
6522 /* TODO: Think about a decent default workgroup */
6523 context->workgroup = SMB_STRDUP("samba");
6527 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6529 /* shortest timeout is 1 second */
6530 if (context->timeout > 0 && context->timeout < 1000)
6531 context->timeout = 1000;
6534 * FIXME: Should we check the function pointers here?
6537 context->internal->_initialized = True;
6543 /* Return the verion of samba, and thus libsmbclient */
6547 return samba_version_string();