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 3 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, see <http://www.gnu.org/licenses/>.
26 #include "include/libsmb_internal.h"
28 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir);
29 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
30 struct smbc_dirent *dirent);
33 * DOS Attribute values (used internally)
35 typedef struct DOS_ATTR_DESC {
47 * Internal flags for extended attributes
50 /* internal mode values */
51 #define SMBC_XATTR_MODE_ADD 1
52 #define SMBC_XATTR_MODE_REMOVE 2
53 #define SMBC_XATTR_MODE_REMOVE_ALL 3
54 #define SMBC_XATTR_MODE_SET 4
55 #define SMBC_XATTR_MODE_CHOWN 5
56 #define SMBC_XATTR_MODE_CHGRP 6
58 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
60 /*We should test for this in configure ... */
62 #define ENOTSUP EOPNOTSUPP
66 * Functions exported by libsmb_cache.c that we need here
68 int smbc_default_cache_functions(SMBCCTX *context);
71 * check if an element is part of the list.
72 * FIXME: Does not belong here !
73 * Can anyone put this in a macro in dlinklist.h ?
76 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
77 if (!p || !list) return False;
79 if (p == list) return True;
86 * Find an lsa pipe handle associated with a cli struct.
88 static struct rpc_pipe_client *
89 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
91 struct rpc_pipe_client *pipe_hnd;
93 for (pipe_hnd = ipc_cli->pipe_list;
95 pipe_hnd = pipe_hnd->next) {
97 if (pipe_hnd->pipe_idx == PI_LSARPC) {
106 smbc_close_ctx(SMBCCTX *context,
109 smbc_lseek_ctx(SMBCCTX *context,
114 extern bool in_client;
117 * Is the logging working / configfile read ?
119 static int smbc_initialized = 0;
122 hex2int( unsigned int _char )
124 if ( _char >= 'A' && _char <='F')
125 return _char - 'A' + 10;
126 if ( _char >= 'a' && _char <='f')
127 return _char - 'a' + 10;
128 if ( _char >= '0' && _char <='9')
136 * Convert strings of %xx to their single character equivalent. Each 'x' must
137 * be a valid hexadecimal digit, or that % sequence is left undecoded.
139 * dest may, but need not be, the same pointer as src.
141 * Returns the number of % sequences which could not be converted due to lack
142 * of two following hexadecimal digits.
145 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
147 int old_length = strlen(src);
153 if ( old_length == 0 ) {
158 while ( i < old_length ) {
159 unsigned char character = src[ i++ ];
161 if (character == '%') {
162 int a = i+1 < old_length ? hex2int( src[i] ) : -1;
163 int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
165 /* Replace valid sequence */
166 if (a != -1 && b != -1) {
168 /* Replace valid %xx sequence with %dd */
169 character = (a * 16) + b;
171 if (character == '\0') {
172 break; /* Stop at %00 */
187 strncpy(dest, temp, max_dest_len - 1);
188 dest[max_dest_len - 1] = '\0';
196 * Convert any characters not specifically allowed in a URL into their %xx
199 * Returns the remaining buffer length.
202 smbc_urlencode(char * dest, char * src, int max_dest_len)
204 char hex[] = "0123456789ABCDEF";
206 for (; *src != '\0' && max_dest_len >= 3; src++) {
218 *dest++ = hex[(*src >> 4) & 0x0f];
219 *dest++ = hex[*src & 0x0f];
234 * Function to parse a path and turn it into components
236 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
237 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
238 * general format ("smb:" only; we do not look for "cifs:").
242 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
246 * smb:// Show all workgroups.
248 * The method of locating the list of workgroups varies
249 * depending upon the setting of the context variable
250 * context->options.browse_max_lmb_count. This value
251 * determine the maximum number of local master browsers to
252 * query for the list of workgroups. In order to ensure that
253 * a complete list of workgroups is obtained, all master
254 * browsers must be queried, but if there are many
255 * workgroups, the time spent querying can begin to add up.
256 * For small networks (not many workgroups), it is suggested
257 * that this variable be set to 0, indicating query all local
258 * master browsers. When the network has many workgroups, a
259 * reasonable setting for this variable might be around 3.
261 * smb://name/ if name<1D> or name<1B> exists, list servers in
262 * workgroup, else, if name<20> exists, list all shares
265 * If "options" are provided, this function returns the entire option list as a
266 * string, for later parsing by the caller. Note that currently, no options
270 static const char *smbc_prefix = "smb:";
273 smbc_parse_path(SMBCCTX *context,
275 char *workgroup, int workgroup_len,
276 char *server, int server_len,
277 char *share, int share_len,
278 char *path, int path_len,
279 char *user, int user_len,
280 char *password, int password_len,
281 char *options, int options_len)
289 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
292 * Assume we wont find an authentication domain to parse, so default
293 * to the workgroup in the provided context.
295 if (workgroup != NULL) {
296 strncpy(workgroup, context->workgroup, workgroup_len - 1);
297 workgroup[workgroup_len - 1] = '\0';
300 if (options != NULL && options_len > 0) {
301 options[0] = (char)0;
305 /* see if it has the right prefix */
306 len = strlen(smbc_prefix);
307 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
308 return -1; /* What about no smb: ? */
313 /* Watch the test below, we are testing to see if we should exit */
315 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
317 DEBUG(1, ("Invalid path (does not begin with smb://"));
322 p += 2; /* Skip the double slash */
324 /* See if any options were specified */
325 if ((q = strrchr(p, '?')) != NULL ) {
326 /* There are options. Null terminate here and point to them */
329 DEBUG(4, ("Found options '%s'", q));
331 /* Copy the options */
332 if (options != NULL && options_len > 0) {
333 safe_strcpy(options, q, options_len - 1);
341 int wl = strlen(context->workgroup);
347 strncpy(server, context->workgroup, wl);
353 * ok, its for us. Now parse out the server, share etc.
355 * However, we want to parse out [[domain;]user[:password]@] if it
359 /* check that '@' occurs before '/', if '/' exists at all */
360 q = strchr_m(p, '@');
361 r = strchr_m(p, '/');
362 if (q && (!r || q < r)) {
363 pstring username, passwd, domain;
364 const char *u = userinfo;
366 next_token_no_ltrim(&p, userinfo, "@", sizeof(fstring));
368 username[0] = passwd[0] = domain[0] = 0;
370 if (strchr_m(u, ';')) {
372 next_token_no_ltrim(&u, domain, ";", sizeof(fstring));
376 if (strchr_m(u, ':')) {
378 next_token_no_ltrim(&u, username, ":", sizeof(fstring));
385 pstrcpy(username, u);
389 if (domain[0] && workgroup) {
390 strncpy(workgroup, domain, workgroup_len - 1);
391 workgroup[workgroup_len - 1] = '\0';
395 strncpy(user, username, user_len - 1);
396 user[user_len - 1] = '\0';
400 strncpy(password, passwd, password_len - 1);
401 password[password_len - 1] = '\0';
406 if (!next_token(&p, server, "/", sizeof(fstring))) {
412 if (*p == (char)0) goto decoding; /* That's it ... */
414 if (!next_token(&p, share, "/", sizeof(fstring))) {
421 * Prepend a leading slash if there's a file path, as required by
427 safe_strcpy(path + 1, p, path_len - 2);
430 all_string_sub(path, "/", "\\", 0);
433 (void) smbc_urldecode(path, path, path_len);
434 (void) smbc_urldecode(server, server, server_len);
435 (void) smbc_urldecode(share, share, share_len);
436 (void) smbc_urldecode(user, user, user_len);
437 (void) smbc_urldecode(password, password, password_len);
443 * Verify that the options specified in a URL are valid
446 smbc_check_options(char *server,
451 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
452 "path='%s' options='%s'\n",
453 server, share, path, options));
455 /* No options at all is always ok */
456 if (! *options) return 0;
458 /* Currently, we don't support any options. */
463 * Convert an SMB error into a UNIX error ...
466 smbc_errno(SMBCCTX *context,
469 int ret = cli_errno(c);
471 if (cli_is_dos_error(c)) {
475 cli_dos_error(c, &eclass, &ecode);
477 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
478 (int)eclass, (int)ecode, (int)ecode, ret));
482 status = cli_nt_error(c);
484 DEBUG(3,("smbc errno %s -> %d\n",
485 nt_errstr(status), ret));
492 * Check a server for being alive and well.
493 * returns 0 if the server is in shape. Returns 1 on error
495 * Also useable outside libsmbclient to enable external cache
496 * to do some checks too.
499 smbc_check_server(SMBCCTX * context,
503 struct sockaddr addr;
506 return (getpeername(server->cli->fd, &addr, &size) == -1);
510 * Remove a server from the cached server list it's unused.
511 * On success, 0 is returned. 1 is returned if the server could not be removed.
513 * Also useable outside libsmbclient
516 smbc_remove_unused_server(SMBCCTX * context,
521 /* are we being fooled ? */
522 if (!context || !context->internal ||
523 !context->internal->_initialized || !srv) return 1;
526 /* Check all open files/directories for a relation with this server */
527 for (file = context->internal->_files; file; file=file->next) {
528 if (file->srv == srv) {
530 DEBUG(3, ("smbc_remove_usused_server: "
531 "%p still used by %p.\n",
537 DLIST_REMOVE(context->internal->_servers, srv);
539 cli_shutdown(srv->cli);
542 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
544 (context->callbacks.remove_cached_srv_fn)(context, srv);
552 find_server(SMBCCTX *context,
564 srv = (context->callbacks.get_cached_srv_fn)(context, server, share,
565 workgroup, username);
567 if (!auth_called && !srv && (!username[0] || !password[0])) {
568 if (context->internal->_auth_fn_with_context != NULL) {
569 (context->internal->_auth_fn_with_context)(
572 workgroup, sizeof(fstring),
573 username, sizeof(fstring),
574 password, sizeof(fstring));
576 (context->callbacks.auth_fn)(
578 workgroup, sizeof(fstring),
579 username, sizeof(fstring),
580 password, sizeof(fstring));
584 * However, smbc_auth_fn may have picked up info relating to
585 * an existing connection, so try for an existing connection
589 goto check_server_cache;
594 if ((context->callbacks.check_server_fn)(context, srv)) {
596 * This server is no good anymore
597 * Try to remove it and check for more possible
598 * servers in the cache
600 if ((context->callbacks.remove_unused_server_fn)(context,
603 * We could not remove the server completely,
604 * remove it from the cache so we will not get
605 * it again. It will be removed when the last
606 * file/dir is closed.
608 (context->callbacks.remove_cached_srv_fn)(context,
613 * Maybe there are more cached connections to this
616 goto check_server_cache;
626 * Connect to a server, possibly on an existing connection
628 * Here, what we want to do is: If the server and username
629 * match an existing connection, reuse that, otherwise, establish a
632 * If we have to create a new connection, call the auth_fn to get the
633 * info we need, unless the username and password were passed in.
637 smbc_server(SMBCCTX *context,
638 bool connect_if_not_found,
647 struct nmb_name called, calling;
648 const char *server_n = server;
650 struct sockaddr_storage ss;
651 int tried_reverse = 0;
654 const char *username_used;
657 zero_addr(&ss, AF_INET);
660 if (server[0] == 0) {
665 /* Look for a cached connection */
666 srv = find_server(context, server, share,
667 workgroup, username, password);
670 * If we found a connection and we're only allowed one share per
673 if (srv && *share != '\0' && context->options.one_share_per_server) {
676 * ... then if there's no current connection to the share,
677 * connect to it. find_server(), or rather the function
678 * pointed to by context->callbacks.get_cached_srv_fn which
679 * was called by find_server(), will have issued a tree
680 * disconnect if the requested share is not the same as the
681 * one that was already connected.
683 if (srv->cli->cnum == (uint16) -1) {
684 /* Ensure we have accurate auth info */
685 if (context->internal->_auth_fn_with_context != NULL) {
686 (context->internal->_auth_fn_with_context)(
689 workgroup, sizeof(fstring),
690 username, sizeof(fstring),
691 password, sizeof(fstring));
693 (context->callbacks.auth_fn)(
695 workgroup, sizeof(fstring),
696 username, sizeof(fstring),
697 password, sizeof(fstring));
700 if (! cli_send_tconX(srv->cli, share, "?????",
701 password, strlen(password)+1)) {
703 errno = smbc_errno(context, srv->cli);
704 cli_shutdown(srv->cli);
706 (context->callbacks.remove_cached_srv_fn)(context,
712 * Regenerate the dev value since it's based on both
716 srv->dev = (dev_t)(str_checksum(server) ^
717 str_checksum(share));
722 /* If we have a connection... */
725 /* ... then we're done here. Give 'em what they came for. */
729 /* If we're not asked to connect when a connection doesn't exist... */
730 if (! connect_if_not_found) {
731 /* ... then we're done here. */
735 make_nmb_name(&calling, context->netbios_name, 0x0);
736 make_nmb_name(&called , server, 0x20);
738 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
740 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
743 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
745 zero_addr(&ss, AF_INET);
747 /* have to open a new connection */
748 if ((c = cli_initialise()) == NULL) {
753 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
754 c->use_kerberos = True;
756 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
757 c->fallback_after_kerberos = True;
760 c->timeout = context->timeout;
763 * Force use of port 139 for first try if share is $IPC, empty, or
764 * null, so browse lists can work
766 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
767 port_try_first = 139;
770 port_try_first = 445;
774 c->port = port_try_first;
776 status = cli_connect(c, server_n, &ss);
777 if (!NT_STATUS_IS_OK(status)) {
779 /* First connection attempt failed. Try alternate port. */
780 c->port = port_try_next;
782 status = cli_connect(c, server_n, &ss);
783 if (!NT_STATUS_IS_OK(status)) {
790 if (!cli_session_request(c, &calling, &called)) {
792 if (strcmp(called.name, "*SMBSERVER")) {
793 make_nmb_name(&called , "*SMBSERVER", 0x20);
795 } else { /* Try one more time, but ensure we don't loop */
797 /* Only try this if server is an IP address ... */
799 if (is_ipaddress(server) && !tried_reverse) {
801 struct sockaddr_storage rem_ss;
803 if (!interpret_string_addr(&rem_ss, server,
805 DEBUG(4, ("Could not convert IP address "
806 "%s to struct sockaddr_storage\n",
812 tried_reverse++; /* Yuck */
814 if (name_status_find("*", 0, 0, &rem_ss, remote_name)) {
815 make_nmb_name(&called, remote_name, 0x20);
824 DEBUG(4,(" session request ok\n"));
826 if (!cli_negprot(c)) {
832 username_used = username;
834 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
835 password, strlen(password),
836 password, strlen(password),
839 /* Failed. Try an anonymous login, if allowed by flags. */
842 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
843 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
854 DEBUG(4,(" session setup ok\n"));
856 if (!cli_send_tconX(c, share, "?????",
857 password, strlen(password)+1)) {
858 errno = smbc_errno(context, c);
863 DEBUG(4,(" tconx ok\n"));
866 * Ok, we have got a nice connection
867 * Let's allocate a server structure.
870 srv = SMB_MALLOC_P(SMBCSRV);
878 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
879 srv->no_pathinfo = False;
880 srv->no_pathinfo2 = False;
881 srv->no_nt_session = False;
883 /* now add it to the cache (internal or external) */
884 /* Let the cache function set errno if it wants to */
886 if ((context->callbacks.add_cached_srv_fn)(context, srv,
888 workgroup, username)) {
889 int saved_errno = errno;
890 DEBUG(3, (" Failed to add server to cache\n"));
898 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
899 server, share, srv));
901 DLIST_ADD(context->internal->_servers, srv);
915 * Connect to a server for getting/setting attributes, possibly on an existing
916 * connection. This works similarly to smbc_server().
919 smbc_attr_server(SMBCCTX *context,
928 struct sockaddr_storage ss;
929 struct cli_state *ipc_cli;
930 struct rpc_pipe_client *pipe_hnd;
932 SMBCSRV *ipc_srv=NULL;
935 * See if we've already created this special connection. Reference
936 * our "special" share name '*IPC$', which is an impossible real share
937 * name due to the leading asterisk.
939 ipc_srv = find_server(context, server, "*IPC$",
940 workgroup, username, password);
943 /* We didn't find a cached connection. Get the password */
944 if (*password == '\0') {
945 /* ... then retrieve it now. */
946 if (context->internal->_auth_fn_with_context != NULL) {
947 (context->internal->_auth_fn_with_context)(
950 workgroup, sizeof(fstring),
951 username, sizeof(fstring),
952 password, sizeof(fstring));
954 (context->callbacks.auth_fn)(
956 workgroup, sizeof(fstring),
957 username, sizeof(fstring),
958 password, sizeof(fstring));
963 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
964 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
967 zero_addr(&ss, AF_INET);
968 nt_status = cli_full_connection(&ipc_cli,
969 global_myname(), server,
970 &ss, 0, "IPC$", "?????",
974 if (! NT_STATUS_IS_OK(nt_status)) {
975 DEBUG(1,("cli_full_connection failed! (%s)\n",
976 nt_errstr(nt_status)));
981 ipc_srv = SMB_MALLOC_P(SMBCSRV);
984 cli_shutdown(ipc_cli);
988 ZERO_STRUCTP(ipc_srv);
989 ipc_srv->cli = ipc_cli;
992 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
996 DEBUG(1, ("cli_nt_session_open fail!\n"));
998 cli_shutdown(ipc_srv->cli);
1004 * Some systems don't support
1005 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1006 * so we might as well do it too.
1009 nt_status = rpccli_lsa_open_policy(
1011 ipc_srv->cli->mem_ctx,
1013 GENERIC_EXECUTE_ACCESS,
1016 if (!NT_STATUS_IS_OK(nt_status)) {
1017 errno = smbc_errno(context, ipc_srv->cli);
1018 cli_shutdown(ipc_srv->cli);
1023 /* now add it to the cache (internal or external) */
1025 errno = 0; /* let cache function set errno if it likes */
1026 if ((context->callbacks.add_cached_srv_fn)(context, ipc_srv,
1031 DEBUG(3, (" Failed to add server to cache\n"));
1035 cli_shutdown(ipc_srv->cli);
1040 DLIST_ADD(context->internal->_servers, ipc_srv);
1047 * Routine to open() a file ...
1051 smbc_open_ctx(SMBCCTX *context,
1056 fstring server, share, user, password, workgroup;
1059 struct cli_state *targetcli;
1060 SMBCSRV *srv = NULL;
1061 SMBCFILE *file = NULL;
1064 if (!context || !context->internal ||
1065 !context->internal->_initialized) {
1067 errno = EINVAL; /* Best I can think of ... */
1079 if (smbc_parse_path(context, fname,
1080 workgroup, sizeof(workgroup),
1081 server, sizeof(server),
1082 share, sizeof(share),
1085 password, sizeof(password),
1091 if (user[0] == (char)0) fstrcpy(user, context->user);
1093 srv = smbc_server(context, True,
1094 server, share, workgroup, user, password);
1098 if (errno == EPERM) errno = EACCES;
1099 return NULL; /* smbc_server sets errno */
1103 /* Hmmm, the test for a directory is suspect here ... FIXME */
1105 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1112 file = SMB_MALLOC_P(SMBCFILE);
1123 /*d_printf(">>>open: resolving %s\n", path);*/
1124 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1126 d_printf("Could not resolve %s\n", path);
1130 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1132 if ((fd = cli_open(targetcli, targetpath, flags,
1133 context->internal->_share_mode)) < 0) {
1135 /* Handle the error ... */
1138 errno = smbc_errno(context, targetcli);
1143 /* Fill in file struct */
1146 file->fname = SMB_STRDUP(fname);
1151 DLIST_ADD(context->internal->_files, file);
1154 * If the file was opened in O_APPEND mode, all write
1155 * operations should be appended to the file. To do that,
1156 * though, using this protocol, would require a getattrE()
1157 * call for each and every write, to determine where the end
1158 * of the file is. (There does not appear to be an append flag
1159 * in the protocol.) Rather than add all of that overhead of
1160 * retrieving the current end-of-file offset prior to each
1161 * write operation, we'll assume that most append operations
1162 * will continuously write, so we'll just set the offset to
1163 * the end of the file now and hope that's adequate.
1165 * Note to self: If this proves inadequate, and O_APPEND
1166 * should, in some cases, be forced for each write, add a
1167 * field in the context options structure, for
1168 * "strict_append_mode" which would select between the current
1169 * behavior (if FALSE) or issuing a getattrE() prior to each
1170 * write and forcing the write to the end of the file (if
1171 * TRUE). Adding that capability will likely require adding
1172 * an "append" flag into the _SMBCFILE structure to track
1173 * whether a file was opened in O_APPEND mode. -- djl
1175 if (flags & O_APPEND) {
1176 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1177 (void) smbc_close_ctx(context, file);
1187 /* Check if opendir needed ... */
1192 eno = smbc_errno(context, srv->cli);
1193 file = (context->opendir)(context, fname);
1194 if (!file) errno = eno;
1199 errno = EINVAL; /* FIXME, correct errno ? */
1205 * Routine to create a file
1208 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1211 smbc_creat_ctx(SMBCCTX *context,
1216 if (!context || !context->internal ||
1217 !context->internal->_initialized) {
1224 return smbc_open_ctx(context, path, creat_bits, mode);
1228 * Routine to read() a file ...
1232 smbc_read_ctx(SMBCCTX *context,
1238 fstring server, share, user, password;
1239 pstring path, targetpath;
1240 struct cli_state *targetcli;
1245 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1246 * appears to pass file->offset (which is type off_t) differently than
1247 * a local variable of type off_t. Using local variable "offset" in
1248 * the call to cli_read() instead of file->offset fixes a problem
1249 * retrieving data at an offset greater than 4GB.
1253 if (!context || !context->internal ||
1254 !context->internal->_initialized) {
1261 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1263 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1270 offset = file->offset;
1272 /* Check that the buffer exists ... */
1281 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1282 if (smbc_parse_path(context, file->fname,
1284 server, sizeof(server),
1285 share, sizeof(share),
1288 password, sizeof(password),
1294 /*d_printf(">>>read: resolving %s\n", path);*/
1295 if (!cli_resolve_path("", file->srv->cli, path,
1296 &targetcli, targetpath))
1298 d_printf("Could not resolve %s\n", path);
1301 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1303 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1307 errno = smbc_errno(context, targetcli);
1312 file->offset += ret;
1314 DEBUG(4, (" --> %d\n", ret));
1316 return ret; /* Success, ret bytes of data ... */
1321 * Routine to write() a file ...
1325 smbc_write_ctx(SMBCCTX *context,
1332 fstring server, share, user, password;
1333 pstring path, targetpath;
1334 struct cli_state *targetcli;
1336 /* First check all pointers before dereferencing them */
1338 if (!context || !context->internal ||
1339 !context->internal->_initialized) {
1346 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1353 /* Check that the buffer exists ... */
1362 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1364 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1365 if (smbc_parse_path(context, file->fname,
1367 server, sizeof(server),
1368 share, sizeof(share),
1371 password, sizeof(password),
1377 /*d_printf(">>>write: resolving %s\n", path);*/
1378 if (!cli_resolve_path("", file->srv->cli, path,
1379 &targetcli, targetpath))
1381 d_printf("Could not resolve %s\n", path);
1384 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1387 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1391 errno = smbc_errno(context, targetcli);
1396 file->offset += ret;
1398 return ret; /* Success, 0 bytes of data ... */
1402 * Routine to close() a file ...
1406 smbc_close_ctx(SMBCCTX *context,
1410 fstring server, share, user, password;
1411 pstring path, targetpath;
1412 struct cli_state *targetcli;
1414 if (!context || !context->internal ||
1415 !context->internal->_initialized) {
1422 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1432 return (context->closedir)(context, file);
1436 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1437 if (smbc_parse_path(context, file->fname,
1439 server, sizeof(server),
1440 share, sizeof(share),
1443 password, sizeof(password),
1449 /*d_printf(">>>close: resolving %s\n", path);*/
1450 if (!cli_resolve_path("", file->srv->cli, path,
1451 &targetcli, targetpath))
1453 d_printf("Could not resolve %s\n", path);
1456 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1458 if (!cli_close(targetcli, file->cli_fd)) {
1460 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1462 /* Deallocate slot and remove the server
1463 * from the server cache if unused */
1464 errno = smbc_errno(context, targetcli);
1466 DLIST_REMOVE(context->internal->_files, file);
1467 SAFE_FREE(file->fname);
1469 (context->callbacks.remove_unused_server_fn)(context, srv);
1475 DLIST_REMOVE(context->internal->_files, file);
1476 SAFE_FREE(file->fname);
1483 * Get info from an SMB server on a file. Use a qpathinfo call first
1484 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1487 smbc_getatr(SMBCCTX * context,
1492 struct timespec *create_time_ts,
1493 struct timespec *access_time_ts,
1494 struct timespec *write_time_ts,
1495 struct timespec *change_time_ts,
1500 struct cli_state *targetcli;
1503 if (!context || !context->internal ||
1504 !context->internal->_initialized) {
1511 /* path fixup for . and .. */
1512 if (strequal(path, ".") || strequal(path, ".."))
1513 pstrcpy(fixedpath, "\\");
1516 pstrcpy(fixedpath, path);
1517 trim_string(fixedpath, NULL, "\\..");
1518 trim_string(fixedpath, NULL, "\\.");
1520 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1522 if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
1524 d_printf("Couldn't resolve %s\n", path);
1528 if (!srv->no_pathinfo2 &&
1529 cli_qpathinfo2(targetcli, targetpath,
1538 /* if this is NT then don't bother with the getatr */
1539 if (targetcli->capabilities & CAP_NT_SMBS) {
1544 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1546 struct timespec w_time_ts;
1548 w_time_ts = convert_time_t_to_timespec(write_time);
1550 if (write_time_ts != NULL) {
1551 *write_time_ts = w_time_ts;
1554 if (create_time_ts != NULL) {
1555 *create_time_ts = w_time_ts;
1558 if (access_time_ts != NULL) {
1559 *access_time_ts = w_time_ts;
1562 if (change_time_ts != NULL) {
1563 *change_time_ts = w_time_ts;
1566 srv->no_pathinfo2 = True;
1576 * Set file info on an SMB server. Use setpathinfo call first. If that
1577 * fails, use setattrE..
1579 * Access and modification time parameters are always used and must be
1580 * provided. Create time, if zero, will be determined from the actual create
1581 * time of the file. If non-zero, the create time will be set as well.
1583 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1586 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1597 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1598 * modern function for "new code" to be using, and it works given a
1599 * filename rather than requiring that the file be opened to have its
1600 * attributes manipulated.
1602 if (srv->no_pathinfo ||
1603 ! cli_setpathinfo(srv->cli, path,
1611 * setpathinfo is not supported; go to plan B.
1613 * cli_setatr() does not work on win98, and it also doesn't
1614 * support setting the access time (only the modification
1615 * time), so in all cases, we open the specified file and use
1616 * cli_setattrE() which should work on all OS versions, and
1617 * supports both times.
1620 /* Don't try {q,set}pathinfo() again, with this server */
1621 srv->no_pathinfo = True;
1624 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1626 errno = smbc_errno(context, srv->cli);
1630 /* Set the new attributes */
1631 ret = cli_setattrE(srv->cli, fd,
1636 /* Close the file */
1637 cli_close(srv->cli, fd);
1640 * Unfortunately, setattrE() doesn't have a provision for
1641 * setting the access mode (attributes). We'll have to try
1642 * cli_setatr() for that, and with only this parameter, it
1643 * seems to work on win98.
1645 if (ret && mode != (uint16) -1) {
1646 ret = cli_setatr(srv->cli, path, mode, 0);
1650 errno = smbc_errno(context, srv->cli);
1659 * Routine to unlink() a file
1663 smbc_unlink_ctx(SMBCCTX *context,
1666 fstring server, share, user, password, workgroup;
1667 pstring path, targetpath;
1668 struct cli_state *targetcli;
1669 SMBCSRV *srv = NULL;
1671 if (!context || !context->internal ||
1672 !context->internal->_initialized) {
1674 errno = EINVAL; /* Best I can think of ... */
1686 if (smbc_parse_path(context, fname,
1687 workgroup, sizeof(workgroup),
1688 server, sizeof(server),
1689 share, sizeof(share),
1692 password, sizeof(password),
1698 if (user[0] == (char)0) fstrcpy(user, context->user);
1700 srv = smbc_server(context, True,
1701 server, share, workgroup, user, password);
1705 return -1; /* smbc_server sets errno */
1709 /*d_printf(">>>unlink: resolving %s\n", path);*/
1710 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1712 d_printf("Could not resolve %s\n", path);
1715 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1717 if (!cli_unlink(targetcli, targetpath)) {
1719 errno = smbc_errno(context, targetcli);
1721 if (errno == EACCES) { /* Check if the file is a directory */
1726 struct timespec write_time_ts;
1727 struct timespec access_time_ts;
1728 struct timespec change_time_ts;
1731 if (!smbc_getatr(context, srv, path, &mode, &size,
1738 /* Hmmm, bad error ... What? */
1740 errno = smbc_errno(context, targetcli);
1746 if (IS_DOS_DIR(mode))
1749 errno = saverr; /* Restore this */
1758 return 0; /* Success ... */
1763 * Routine to rename() a file
1767 smbc_rename_ctx(SMBCCTX *ocontext,
1783 pstring targetpath1;
1784 pstring targetpath2;
1785 struct cli_state *targetcli1;
1786 struct cli_state *targetcli2;
1787 SMBCSRV *srv = NULL;
1789 if (!ocontext || !ncontext ||
1790 !ocontext->internal || !ncontext->internal ||
1791 !ocontext->internal->_initialized ||
1792 !ncontext->internal->_initialized) {
1794 errno = EINVAL; /* Best I can think of ... */
1799 if (!oname || !nname) {
1806 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1808 smbc_parse_path(ocontext, oname,
1809 workgroup, sizeof(workgroup),
1810 server1, sizeof(server1),
1811 share1, sizeof(share1),
1812 path1, sizeof(path1),
1813 user1, sizeof(user1),
1814 password1, sizeof(password1),
1817 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1819 smbc_parse_path(ncontext, nname,
1821 server2, sizeof(server2),
1822 share2, sizeof(share2),
1823 path2, sizeof(path2),
1824 user2, sizeof(user2),
1825 password2, sizeof(password2),
1828 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1830 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1831 strcmp(user1, user2)) {
1833 /* Can't rename across file systems, or users?? */
1840 srv = smbc_server(ocontext, True,
1841 server1, share1, workgroup, user1, password1);
1848 /*d_printf(">>>rename: resolving %s\n", path1);*/
1849 if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
1851 d_printf("Could not resolve %s\n", path1);
1854 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1855 /*d_printf(">>>rename: resolving %s\n", path2);*/
1856 if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
1858 d_printf("Could not resolve %s\n", path2);
1861 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1863 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1864 strcmp(targetcli1->share, targetcli2->share))
1866 /* can't rename across file systems */
1872 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1873 int eno = smbc_errno(ocontext, targetcli1);
1875 if (eno != EEXIST ||
1876 !cli_unlink(targetcli1, targetpath2) ||
1877 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1885 return 0; /* Success */
1890 * A routine to lseek() a file
1894 smbc_lseek_ctx(SMBCCTX *context,
1900 fstring server, share, user, password;
1901 pstring path, targetpath;
1902 struct cli_state *targetcli;
1904 if (!context || !context->internal ||
1905 !context->internal->_initialized) {
1912 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1922 return -1; /* Can't lseek a dir ... */
1928 file->offset = offset;
1932 file->offset += offset;
1936 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1937 if (smbc_parse_path(context, file->fname,
1939 server, sizeof(server),
1940 share, sizeof(share),
1943 password, sizeof(password),
1950 /*d_printf(">>>lseek: resolving %s\n", path);*/
1951 if (!cli_resolve_path("", file->srv->cli, path,
1952 &targetcli, targetpath))
1954 d_printf("Could not resolve %s\n", path);
1957 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1959 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
1960 &size, NULL, NULL, NULL, NULL, NULL))
1962 SMB_OFF_T b_size = size;
1963 if (!cli_getattrE(targetcli, file->cli_fd,
1964 NULL, &b_size, NULL, NULL, NULL))
1971 file->offset = size + offset;
1980 return file->offset;
1985 * Generate an inode number from file name for those things that need it
1989 smbc_inode(SMBCCTX *context,
1993 if (!context || !context->internal ||
1994 !context->internal->_initialized) {
2001 if (!*name) return 2; /* FIXME, why 2 ??? */
2002 return (ino_t)str_checksum(name);
2007 * Routine to put basic stat info into a stat structure ... Used by stat and
2012 smbc_setup_stat(SMBCCTX *context,
2021 if (IS_DOS_DIR(mode)) {
2022 st->st_mode = SMBC_DIR_MODE;
2024 st->st_mode = SMBC_FILE_MODE;
2027 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2028 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2029 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2030 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2033 #ifdef HAVE_STAT_ST_BLKSIZE
2034 st->st_blksize = 512;
2036 #ifdef HAVE_STAT_ST_BLOCKS
2037 st->st_blocks = (size+511)/512;
2039 st->st_uid = getuid();
2040 st->st_gid = getgid();
2042 if (IS_DOS_DIR(mode)) {
2048 if (st->st_ino == 0) {
2049 st->st_ino = smbc_inode(context, fname);
2052 return True; /* FIXME: Is this needed ? */
2057 * Routine to stat a file given a name
2061 smbc_stat_ctx(SMBCCTX *context,
2072 struct timespec write_time_ts;
2073 struct timespec access_time_ts;
2074 struct timespec change_time_ts;
2079 if (!context || !context->internal ||
2080 !context->internal->_initialized) {
2082 errno = EINVAL; /* Best I can think of ... */
2094 DEBUG(4, ("smbc_stat(%s)\n", fname));
2096 if (smbc_parse_path(context, fname,
2097 workgroup, sizeof(workgroup),
2098 server, sizeof(server),
2099 share, sizeof(share),
2102 password, sizeof(password),
2108 if (user[0] == (char)0) fstrcpy(user, context->user);
2110 srv = smbc_server(context, True,
2111 server, share, workgroup, user, password);
2114 return -1; /* errno set by smbc_server */
2117 if (!smbc_getatr(context, srv, path, &mode, &size,
2124 errno = smbc_errno(context, srv->cli);
2131 smbc_setup_stat(context, st, path, size, mode);
2133 set_atimespec(st, access_time_ts);
2134 set_ctimespec(st, change_time_ts);
2135 set_mtimespec(st, write_time_ts);
2136 st->st_dev = srv->dev;
2143 * Routine to stat a file given an fd
2147 smbc_fstat_ctx(SMBCCTX *context,
2151 struct timespec change_time_ts;
2152 struct timespec access_time_ts;
2153 struct timespec write_time_ts;
2162 struct cli_state *targetcli;
2165 if (!context || !context->internal ||
2166 !context->internal->_initialized) {
2173 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2182 return (context->fstatdir)(context, file, st);
2186 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2187 if (smbc_parse_path(context, file->fname,
2189 server, sizeof(server),
2190 share, sizeof(share),
2193 password, sizeof(password),
2199 /*d_printf(">>>fstat: resolving %s\n", path);*/
2200 if (!cli_resolve_path("", file->srv->cli, path,
2201 &targetcli, targetpath))
2203 d_printf("Could not resolve %s\n", path);
2206 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2208 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2215 time_t change_time, access_time, write_time;
2217 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2218 &change_time, &access_time, &write_time)) {
2224 change_time_ts = convert_time_t_to_timespec(change_time);
2225 access_time_ts = convert_time_t_to_timespec(access_time);
2226 write_time_ts = convert_time_t_to_timespec(write_time);
2231 smbc_setup_stat(context, st, file->fname, size, mode);
2233 set_atimespec(st, access_time_ts);
2234 set_ctimespec(st, change_time_ts);
2235 set_mtimespec(st, write_time_ts);
2236 st->st_dev = file->srv->dev;
2243 * Routine to open a directory
2244 * We accept the URL syntax explained in smbc_parse_path(), above.
2248 smbc_remove_dir(SMBCFILE *dir)
2250 struct smbc_dir_list *d,*f;
2257 SAFE_FREE(f->dirent);
2262 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2267 add_dirent(SMBCFILE *dir,
2269 const char *comment,
2272 struct smbc_dirent *dirent;
2274 int name_length = (name == NULL ? 0 : strlen(name));
2275 int comment_len = (comment == NULL ? 0 : strlen(comment));
2278 * Allocate space for the dirent, which must be increased by the
2279 * size of the name and the comment and 1 each for the null terminator.
2282 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2284 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2288 dir->dir_error = ENOMEM;
2293 ZERO_STRUCTP(dirent);
2295 if (dir->dir_list == NULL) {
2297 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2298 if (!dir->dir_list) {
2301 dir->dir_error = ENOMEM;
2305 ZERO_STRUCTP(dir->dir_list);
2307 dir->dir_end = dir->dir_next = dir->dir_list;
2311 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2313 if (!dir->dir_end->next) {
2316 dir->dir_error = ENOMEM;
2320 ZERO_STRUCTP(dir->dir_end->next);
2322 dir->dir_end = dir->dir_end->next;
2325 dir->dir_end->next = NULL;
2326 dir->dir_end->dirent = dirent;
2328 dirent->smbc_type = type;
2329 dirent->namelen = name_length;
2330 dirent->commentlen = comment_len;
2331 dirent->dirlen = size;
2334 * dirent->namelen + 1 includes the null (no null termination needed)
2335 * Ditto for dirent->commentlen.
2336 * The space for the two null bytes was allocated.
2338 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2339 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2340 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2347 list_unique_wg_fn(const char *name,
2349 const char *comment,
2352 SMBCFILE *dir = (SMBCFILE *)state;
2353 struct smbc_dir_list *dir_list;
2354 struct smbc_dirent *dirent;
2358 dirent_type = dir->dir_type;
2360 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2362 /* An error occurred, what do we do? */
2363 /* FIXME: Add some code here */
2366 /* Point to the one just added */
2367 dirent = dir->dir_end->dirent;
2369 /* See if this was a duplicate */
2370 for (dir_list = dir->dir_list;
2371 dir_list != dir->dir_end;
2372 dir_list = dir_list->next) {
2374 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2375 /* Duplicate. End end of list need to be removed. */
2379 if (do_remove && dir_list->next == dir->dir_end) {
2380 /* Found the end of the list. Remove it. */
2381 dir->dir_end = dir_list;
2382 free(dir_list->next);
2384 dir_list->next = NULL;
2391 list_fn(const char *name,
2393 const char *comment,
2396 SMBCFILE *dir = (SMBCFILE *)state;
2400 * We need to process the type a little ...
2402 * Disk share = 0x00000000
2403 * Print share = 0x00000001
2404 * Comms share = 0x00000002 (obsolete?)
2405 * IPC$ share = 0x00000003
2407 * administrative shares:
2408 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2411 if (dir->dir_type == SMBC_FILE_SHARE) {
2414 case 0 | 0x80000000:
2416 dirent_type = SMBC_FILE_SHARE;
2420 dirent_type = SMBC_PRINTER_SHARE;
2424 dirent_type = SMBC_COMMS_SHARE;
2427 case 3 | 0x80000000:
2429 dirent_type = SMBC_IPC_SHARE;
2433 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2438 dirent_type = dir->dir_type;
2441 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2443 /* An error occurred, what do we do? */
2444 /* FIXME: Add some code here */
2450 dir_list_fn(const char *mnt,
2456 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2457 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2459 /* Handle an error ... */
2461 /* FIXME: Add some code ... */
2468 net_share_enum_rpc(struct cli_state *cli,
2469 void (*fn)(const char *name,
2471 const char *comment,
2478 uint32 info_level = 1;
2479 uint32 preferred_len = 0xffffffff;
2481 SRV_SHARE_INFO_CTR ctr;
2483 fstring comment = "";
2485 struct rpc_pipe_client *pipe_hnd;
2488 /* Open the server service pipe */
2489 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2491 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2495 /* Allocate a context for parsing and for the entries in "ctr" */
2496 mem_ctx = talloc_init("libsmbclient: net_share_enum_rpc");
2497 if (mem_ctx == NULL) {
2498 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2499 cli_rpc_pipe_close(pipe_hnd);
2503 /* Issue the NetShareEnum RPC call and retrieve the response */
2504 init_enum_hnd(&enum_hnd, 0);
2505 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2512 /* Was it successful? */
2513 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2514 /* Nope. Go clean up. */
2518 /* For each returned entry... */
2519 for (i = 0; i < ctr.num_entries; i++) {
2521 /* pull out the share name */
2522 rpcstr_pull_unistr2_fstring(
2523 name, &ctr.share.info1[i].info_1_str.uni_netname);
2525 /* pull out the share's comment */
2526 rpcstr_pull_unistr2_fstring(
2527 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2529 /* Get the type value */
2530 type = ctr.share.info1[i].info_1.type;
2532 /* Add this share to the list */
2533 (*fn)(name, type, comment, state);
2537 /* Close the server service pipe */
2538 cli_rpc_pipe_close(pipe_hnd);
2540 /* Free all memory which was allocated for this request */
2541 TALLOC_FREE(mem_ctx);
2543 /* Tell 'em if it worked */
2544 return W_ERROR_IS_OK(result) ? 0 : -1;
2550 smbc_opendir_ctx(SMBCCTX *context,
2554 fstring server, share, user, password, options;
2559 SMBCSRV *srv = NULL;
2560 SMBCFILE *dir = NULL;
2561 struct _smbc_callbacks *cb;
2562 struct sockaddr_storage rem_ss;
2564 if (!context || !context->internal ||
2565 !context->internal->_initialized) {
2566 DEBUG(4, ("no valid context\n"));
2567 errno = EINVAL + 8192;
2573 DEBUG(4, ("no valid fname\n"));
2574 errno = EINVAL + 8193;
2578 if (smbc_parse_path(context, fname,
2579 workgroup, sizeof(workgroup),
2580 server, sizeof(server),
2581 share, sizeof(share),
2584 password, sizeof(password),
2585 options, sizeof(options))) {
2586 DEBUG(4, ("no valid path\n"));
2587 errno = EINVAL + 8194;
2591 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2592 "path='%s' options='%s'\n",
2593 fname, server, share, path, options));
2595 /* Ensure the options are valid */
2596 if (smbc_check_options(server, share, path, options)) {
2597 DEBUG(4, ("unacceptable options (%s)\n", options));
2598 errno = EINVAL + 8195;
2602 if (user[0] == (char)0) fstrcpy(user, context->user);
2604 dir = SMB_MALLOC_P(SMBCFILE);
2614 dir->fname = SMB_STRDUP(fname);
2618 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2620 if (server[0] == (char)0) {
2625 struct ip_service *ip_list;
2626 struct ip_service server_addr;
2627 struct user_auth_info u_info;
2628 struct cli_state *cli;
2630 if (share[0] != (char)0 || path[0] != (char)0) {
2632 errno = EINVAL + 8196;
2634 SAFE_FREE(dir->fname);
2640 /* Determine how many local master browsers to query */
2641 max_lmb_count = (context->options.browse_max_lmb_count == 0
2643 : context->options.browse_max_lmb_count);
2645 pstrcpy(u_info.username, user);
2646 pstrcpy(u_info.password, password);
2649 * We have server and share and path empty but options
2650 * requesting that we scan all master browsers for their list
2651 * of workgroups/domains. This implies that we must first try
2652 * broadcast queries to find all master browsers, and if that
2653 * doesn't work, then try our other methods which return only
2654 * a single master browser.
2658 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
2664 if (!find_master_ip(workgroup, &server_addr.ss)) {
2667 SAFE_FREE(dir->fname);
2674 ip_list = &server_addr;
2678 for (i = 0; i < count && i < max_lmb_count; i++) {
2679 char addr[INET6_ADDRSTRLEN];
2680 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
2681 DEBUG(99, ("Found master browser %d of %d: %s\n",
2682 i+1, MAX(count, max_lmb_count),
2685 cli = get_ipc_connect_master_ip(&ip_list[i],
2686 workgroup, &u_info);
2687 /* cli == NULL is the master browser refused to talk or
2688 could not be found */
2692 fstrcpy(server, cli->desthost);
2695 DEBUG(4, ("using workgroup %s %s\n",
2696 workgroup, server));
2699 * For each returned master browser IP address, get a
2700 * connection to IPC$ on the server if we do not
2701 * already have one, and determine the
2702 * workgroups/domains that it knows about.
2705 srv = smbc_server(context, True, server, "IPC$",
2706 workgroup, user, password);
2712 dir->dir_type = SMBC_WORKGROUP;
2714 /* Now, list the stuff ... */
2716 if (!cli_NetServerEnum(srv->cli,
2718 SV_TYPE_DOMAIN_ENUM,
2728 * Server not an empty string ... Check the rest and see what
2731 if (*share == '\0') {
2732 if (*path != '\0') {
2734 /* Should not have empty share with path */
2735 errno = EINVAL + 8197;
2737 SAFE_FREE(dir->fname);
2745 * We don't know if <server> is really a server name
2746 * or is a workgroup/domain name. If we already have
2747 * a server structure for it, we'll use it.
2748 * Otherwise, check to see if <server><1D>,
2749 * <server><1B>, or <server><20> translates. We check
2750 * to see if <server> is an IP address first.
2754 * See if we have an existing server. Do not
2755 * establish a connection if one does not already
2758 srv = smbc_server(context, False, server, "IPC$",
2759 workgroup, user, password);
2762 * If no existing server and not an IP addr, look for
2766 !is_ipaddress(server) &&
2767 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
2768 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
2772 dir->dir_type = SMBC_SERVER;
2775 * Get the backup list ...
2777 if (!name_status_find(server, 0, 0,
2778 &rem_ss, buserver)) {
2780 DEBUG(0, ("Could not get name of "
2781 "local/domain master browser "
2782 "for server %s\n", server));
2784 SAFE_FREE(dir->fname);
2793 * Get a connection to IPC$ on the server if
2794 * we do not already have one
2796 srv = smbc_server(context, True,
2798 workgroup, user, password);
2800 DEBUG(0, ("got no contact to IPC$\n"));
2802 SAFE_FREE(dir->fname);
2811 /* Now, list the servers ... */
2812 if (!cli_NetServerEnum(srv->cli, server,
2813 0x0000FFFE, list_fn,
2817 SAFE_FREE(dir->fname);
2823 (resolve_name(server, &rem_ss, 0x20))) {
2825 /* If we hadn't found the server, get one now */
2827 srv = smbc_server(context, True,
2835 SAFE_FREE(dir->fname);
2842 dir->dir_type = SMBC_FILE_SHARE;
2845 /* List the shares ... */
2847 if (net_share_enum_rpc(
2850 (void *) dir) < 0 &&
2856 errno = cli_errno(srv->cli);
2858 SAFE_FREE(dir->fname);
2865 /* Neither the workgroup nor server exists */
2866 errno = ECONNREFUSED;
2868 SAFE_FREE(dir->fname);
2877 * The server and share are specified ... work from
2881 struct cli_state *targetcli;
2883 /* We connect to the server and list the directory */
2884 dir->dir_type = SMBC_FILE_SHARE;
2886 srv = smbc_server(context, True, server, share,
2887 workgroup, user, password);
2892 SAFE_FREE(dir->fname);
2901 /* Now, list the files ... */
2903 p = path + strlen(path);
2904 pstrcat(path, "\\*");
2906 if (!cli_resolve_path("", srv->cli, path,
2907 &targetcli, targetpath))
2909 d_printf("Could not resolve %s\n", path);
2911 SAFE_FREE(dir->fname);
2917 if (cli_list(targetcli, targetpath,
2918 aDIR | aSYSTEM | aHIDDEN,
2919 dir_list_fn, (void *)dir) < 0) {
2922 SAFE_FREE(dir->fname);
2925 saved_errno = smbc_errno(context, targetcli);
2927 if (saved_errno == EINVAL) {
2929 * See if they asked to opendir something
2930 * other than a directory. If so, the
2931 * converted error value we got would have
2932 * been EINVAL rather than ENOTDIR.
2934 *p = '\0'; /* restore original path */
2936 if (smbc_getatr(context, srv, path,
2938 NULL, NULL, NULL, NULL,
2940 ! IS_DOS_DIR(mode)) {
2942 /* It is. Correct the error value */
2943 saved_errno = ENOTDIR;
2948 * If there was an error and the server is no
2951 cb = &context->callbacks;
2952 if (cli_is_error(targetcli) &&
2953 (cb->check_server_fn)(context, srv)) {
2955 /* ... then remove it. */
2956 if ((cb->remove_unused_server_fn)(context,
2959 * We could not remove the
2960 * server completely, remove
2961 * it from the cache so we
2962 * will not get it again. It
2963 * will be removed when the
2964 * last file/dir is closed.
2966 (cb->remove_cached_srv_fn)(context,
2971 errno = saved_errno;
2978 DLIST_ADD(context->internal->_files, dir);
2984 * Routine to close a directory
2988 smbc_closedir_ctx(SMBCCTX *context,
2992 if (!context || !context->internal ||
2993 !context->internal->_initialized) {
3000 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3007 smbc_remove_dir(dir); /* Clean it up */
3009 DLIST_REMOVE(context->internal->_files, dir);
3013 SAFE_FREE(dir->fname);
3014 SAFE_FREE(dir); /* Free the space too */
3022 smbc_readdir_internal(SMBCCTX * context,
3023 struct smbc_dirent *dest,
3024 struct smbc_dirent *src,
3025 int max_namebuf_len)
3027 if (context->options.urlencode_readdir_entries) {
3029 /* url-encode the name. get back remaining buffer space */
3031 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3033 /* We now know the name length */
3034 dest->namelen = strlen(dest->name);
3036 /* Save the pointer to the beginning of the comment */
3037 dest->comment = dest->name + dest->namelen + 1;
3039 /* Copy the comment */
3040 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3041 dest->comment[max_namebuf_len - 1] = '\0';
3043 /* Save other fields */
3044 dest->smbc_type = src->smbc_type;
3045 dest->commentlen = strlen(dest->comment);
3046 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3050 /* No encoding. Just copy the entry as is. */
3051 memcpy(dest, src, src->dirlen);
3052 dest->comment = (char *)(&dest->name + src->namelen + 1);
3058 * Routine to get a directory entry
3061 struct smbc_dirent *
3062 smbc_readdir_ctx(SMBCCTX *context,
3066 struct smbc_dirent *dirp, *dirent;
3068 /* Check that all is ok first ... */
3070 if (!context || !context->internal ||
3071 !context->internal->_initialized) {
3074 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3079 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3082 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3087 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3090 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3095 if (!dir->dir_next) {
3099 dirent = dir->dir_next->dirent;
3107 dirp = (struct smbc_dirent *)context->internal->_dirent;
3108 maxlen = (sizeof(context->internal->_dirent) -
3109 sizeof(struct smbc_dirent));
3111 smbc_readdir_internal(context, dirp, dirent, maxlen);
3113 dir->dir_next = dir->dir_next->next;
3119 * Routine to get directory entries
3123 smbc_getdents_ctx(SMBCCTX *context,
3125 struct smbc_dirent *dirp,
3131 char *ndir = (char *)dirp;
3132 struct smbc_dir_list *dirlist;
3134 /* Check that all is ok first ... */
3136 if (!context || !context->internal ||
3137 !context->internal->_initialized) {
3144 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3151 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3159 * Now, retrieve the number of entries that will fit in what was passed
3160 * We have to figure out if the info is in the list, or we need to
3161 * send a request to the server to get the info.
3164 while ((dirlist = dir->dir_next)) {
3165 struct smbc_dirent *dirent;
3167 if (!dirlist->dirent) {
3169 errno = ENOENT; /* Bad error */
3174 /* Do urlencoding of next entry, if so selected */
3175 dirent = (struct smbc_dirent *)context->internal->_dirent;
3176 maxlen = (sizeof(context->internal->_dirent) -
3177 sizeof(struct smbc_dirent));
3178 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3180 reqd = dirent->dirlen;
3184 if (rem < count) { /* We managed to copy something */
3190 else { /* Nothing copied ... */
3192 errno = EINVAL; /* Not enough space ... */
3199 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3201 ((struct smbc_dirent *)ndir)->comment =
3202 (char *)(&((struct smbc_dirent *)ndir)->name +
3210 dir->dir_next = dirlist = dirlist -> next;
3221 * Routine to create a directory ...
3225 smbc_mkdir_ctx(SMBCCTX *context,
3235 pstring path, targetpath;
3236 struct cli_state *targetcli;
3238 if (!context || !context->internal ||
3239 !context->internal->_initialized) {
3253 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3255 if (smbc_parse_path(context, fname,
3256 workgroup, sizeof(workgroup),
3257 server, sizeof(server),
3258 share, sizeof(share),
3261 password, sizeof(password),
3267 if (user[0] == (char)0) fstrcpy(user, context->user);
3269 srv = smbc_server(context, True,
3270 server, share, workgroup, user, password);
3274 return -1; /* errno set by smbc_server */
3278 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3279 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3281 d_printf("Could not resolve %s\n", path);
3284 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3286 if (!cli_mkdir(targetcli, targetpath)) {
3288 errno = smbc_errno(context, targetcli);
3298 * Our list function simply checks to see if a directory is not empty
3301 static int smbc_rmdir_dirempty = True;
3304 rmdir_list_fn(const char *mnt,
3309 if (strncmp(finfo->name, ".", 1) != 0 &&
3310 strncmp(finfo->name, "..", 2) != 0) {
3312 smbc_rmdir_dirempty = False;
3317 * Routine to remove a directory
3321 smbc_rmdir_ctx(SMBCCTX *context,
3332 struct cli_state *targetcli;
3334 if (!context || !context->internal ||
3335 !context->internal->_initialized) {
3349 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3351 if (smbc_parse_path(context, fname,
3352 workgroup, sizeof(workgroup),
3353 server, sizeof(server),
3354 share, sizeof(share),
3357 password, sizeof(password),
3364 if (user[0] == (char)0) fstrcpy(user, context->user);
3366 srv = smbc_server(context, True,
3367 server, share, workgroup, user, password);
3371 return -1; /* errno set by smbc_server */
3375 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3376 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3378 d_printf("Could not resolve %s\n", path);
3381 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3384 if (!cli_rmdir(targetcli, targetpath)) {
3386 errno = smbc_errno(context, targetcli);
3388 if (errno == EACCES) { /* Check if the dir empty or not */
3390 /* Local storage to avoid buffer overflows */
3393 smbc_rmdir_dirempty = True; /* Make this so ... */
3395 pstrcpy(lpath, targetpath);
3396 pstrcat(lpath, "\\*");
3398 if (cli_list(targetcli, lpath,
3399 aDIR | aSYSTEM | aHIDDEN,
3400 rmdir_list_fn, NULL) < 0) {
3402 /* Fix errno to ignore latest error ... */
3403 DEBUG(5, ("smbc_rmdir: "
3404 "cli_list returned an error: %d\n",
3405 smbc_errno(context, targetcli)));
3410 if (smbc_rmdir_dirempty)
3426 * Routine to return the current directory position
3430 smbc_telldir_ctx(SMBCCTX *context,
3433 if (!context || !context->internal ||
3434 !context->internal->_initialized) {
3441 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3448 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3455 /* See if we're already at the end. */
3456 if (dir->dir_next == NULL) {
3462 * We return the pointer here as the offset
3464 return (off_t)(long)dir->dir_next->dirent;
3468 * A routine to run down the list and see if the entry is OK
3471 struct smbc_dir_list *
3472 smbc_check_dir_ent(struct smbc_dir_list *list,
3473 struct smbc_dirent *dirent)
3476 /* Run down the list looking for what we want */
3480 struct smbc_dir_list *tmp = list;
3484 if (tmp->dirent == dirent)
3493 return NULL; /* Not found, or an error */
3499 * Routine to seek on a directory
3503 smbc_lseekdir_ctx(SMBCCTX *context,
3507 long int l_offset = offset; /* Handle problems of size */
3508 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3509 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3511 if (!context || !context->internal ||
3512 !context->internal->_initialized) {
3519 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3526 /* Now, check what we were passed and see if it is OK ... */
3528 if (dirent == NULL) { /* Seek to the begining of the list */
3530 dir->dir_next = dir->dir_list;
3535 if (offset == -1) { /* Seek to the end of the list */
3536 dir->dir_next = NULL;
3540 /* Now, run down the list and make sure that the entry is OK */
3541 /* This may need to be changed if we change the format of the list */
3543 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3545 errno = EINVAL; /* Bad entry */
3550 dir->dir_next = list_ent;
3557 * Routine to fstat a dir
3561 smbc_fstatdir_ctx(SMBCCTX *context,
3566 if (!context || !context->internal ||
3567 !context->internal->_initialized) {
3574 /* No code yet ... */
3581 smbc_chmod_ctx(SMBCCTX *context,
3594 if (!context || !context->internal ||
3595 !context->internal->_initialized) {
3597 errno = EINVAL; /* Best I can think of ... */
3609 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3611 if (smbc_parse_path(context, fname,
3612 workgroup, sizeof(workgroup),
3613 server, sizeof(server),
3614 share, sizeof(share),
3617 password, sizeof(password),
3623 if (user[0] == (char)0) fstrcpy(user, context->user);
3625 srv = smbc_server(context, True,
3626 server, share, workgroup, user, password);
3629 return -1; /* errno set by smbc_server */
3634 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3635 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3636 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3637 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3639 if (!cli_setatr(srv->cli, path, mode, 0)) {
3640 errno = smbc_errno(context, srv->cli);
3648 smbc_utimes_ctx(SMBCCTX *context,
3650 struct timeval *tbuf)
3662 if (!context || !context->internal ||
3663 !context->internal->_initialized) {
3665 errno = EINVAL; /* Best I can think of ... */
3678 access_time = write_time = time(NULL);
3680 access_time = tbuf[0].tv_sec;
3681 write_time = tbuf[1].tv_sec;
3690 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3691 atimebuf[sizeof(atimebuf) - 1] = '\0';
3692 if ((p = strchr(atimebuf, '\n')) != NULL) {
3696 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3697 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3698 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3702 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3703 fname, atimebuf, mtimebuf);
3706 if (smbc_parse_path(context, fname,
3707 workgroup, sizeof(workgroup),
3708 server, sizeof(server),
3709 share, sizeof(share),
3712 password, sizeof(password),
3718 if (user[0] == (char)0) fstrcpy(user, context->user);
3720 srv = smbc_server(context, True,
3721 server, share, workgroup, user, password);
3724 return -1; /* errno set by smbc_server */
3727 if (!smbc_setatr(context, srv, path,
3728 0, access_time, write_time, 0, 0)) {
3729 return -1; /* errno set by smbc_setatr */
3737 * Sort ACEs according to the documentation at
3738 * http://support.microsoft.com/kb/269175, at least as far as it defines the
3743 ace_compare(SEC_ACE *ace1,
3749 /* If the ACEs are equal, we have nothing more to do. */
3750 if (sec_ace_equal(ace1, ace2)) {
3754 /* Inherited follow non-inherited */
3755 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
3756 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
3758 return (b1 ? 1 : -1);
3762 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
3763 * sort them after DENY and ALLOW.
3765 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
3766 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
3767 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
3768 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3769 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
3770 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
3771 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
3772 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3774 return (b1 ? 1 : -1);
3777 /* Allowed ACEs follow denied ACEs */
3778 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
3779 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
3780 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
3781 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
3783 return (b1 ? 1 : -1);
3787 * ACEs applying to an entity's object follow those applying to the
3790 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
3791 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3792 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
3793 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3795 return (b1 ? 1 : -1);
3799 * If we get this far, the ACEs are similar as far as the
3800 * characteristics we typically care about (those defined by the
3801 * referenced MS document). We'll now sort by characteristics that
3802 * just seems reasonable.
3805 if (ace1->type != ace2->type) {
3806 return ace2->type - ace1->type;
3809 if (sid_compare(&ace1->trustee, &ace2->trustee)) {
3810 return sid_compare(&ace1->trustee, &ace2->trustee);
3813 if (ace1->flags != ace2->flags) {
3814 return ace1->flags - ace2->flags;
3817 if (ace1->access_mask != ace2->access_mask) {
3818 return ace1->access_mask - ace2->access_mask;
3821 if (ace1->size != ace2->size) {
3822 return ace1->size - ace2->size;
3825 return memcmp(ace1, ace2, sizeof(SEC_ACE));
3830 sort_acl(SEC_ACL *the_acl)
3833 if (!the_acl) return;
3835 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
3836 QSORT_CAST ace_compare);
3838 for (i=1;i<the_acl->num_aces;) {
3839 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
3841 for (j=i; j<the_acl->num_aces-1; j++) {
3842 the_acl->aces[j] = the_acl->aces[j+1];
3844 the_acl->num_aces--;
3851 /* convert a SID to a string, either numeric or username/group */
3853 convert_sid_to_string(struct cli_state *ipc_cli,
3859 char **domains = NULL;
3860 char **names = NULL;
3861 enum lsa_SidType *types = NULL;
3862 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3863 sid_to_string(str, sid);
3866 return; /* no lookup desired */
3873 /* Ask LSA to convert the sid to a name */
3875 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ipc_cli->mem_ctx,
3876 pol, 1, sid, &domains,
3878 !domains || !domains[0] || !names || !names[0]) {
3884 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3885 domains[0], lp_winbind_separator(),
3889 /* convert a string to a SID, either numeric or username/group */
3891 convert_string_to_sid(struct cli_state *ipc_cli,
3897 enum lsa_SidType *types = NULL;
3898 DOM_SID *sids = NULL;
3900 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3907 if (strncmp(str, "S-", 2) == 0) {
3908 return string_to_sid(sid, str);
3915 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx,
3916 pol, 1, &str, NULL, 1, &sids,
3922 sid_copy(sid, &sids[0]);
3929 /* parse an ACE in the same format as print_ace() */
3931 parse_ace(struct cli_state *ipc_cli,
3941 unsigned int aflags;
3945 const struct perm_value *v;
3951 /* These values discovered by inspection */
3952 static const struct perm_value special_values[] = {
3953 { "R", 0x00120089 },
3954 { "W", 0x00120116 },
3955 { "X", 0x001200a0 },
3956 { "D", 0x00010000 },
3957 { "P", 0x00040000 },
3958 { "O", 0x00080000 },
3962 static const struct perm_value standard_values[] = {
3963 { "READ", 0x001200a9 },
3964 { "CHANGE", 0x001301bf },
3965 { "FULL", 0x001f01ff },
3971 p = strchr_m(str,':');
3972 if (!p) return False;
3975 /* Try to parse numeric form */
3977 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3978 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3982 /* Try to parse text form */
3984 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3989 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3993 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3994 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3995 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3996 atype = SEC_ACE_TYPE_ACCESS_DENIED;
4001 /* Only numeric form accepted for flags at present */
4003 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
4004 sscanf(tok, "%i", &aflags))) {
4008 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
4012 if (strncmp(tok, "0x", 2) == 0) {
4013 if (sscanf(tok, "%i", &amask) != 1) {
4019 for (v = standard_values; v->perm; v++) {
4020 if (strcmp(tok, v->perm) == 0) {
4031 for (v = special_values; v->perm; v++) {
4032 if (v->perm[0] == *p) {
4038 if (!found) return False;
4048 init_sec_ace(ace, &sid, atype, mask, aflags);
4052 /* add an ACE to a list of ACEs in a SEC_ACL */
4054 add_ace(SEC_ACL **the_acl,
4062 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
4066 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4069 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4070 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4071 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4072 1+(*the_acl)->num_aces, aces);
4074 (*the_acl) = newacl;
4079 /* parse a ascii version of a security descriptor */
4081 sec_desc_parse(TALLOC_CTX *ctx,
4082 struct cli_state *ipc_cli,
4087 const char *p = str;
4089 SEC_DESC *ret = NULL;
4091 DOM_SID *group_sid=NULL;
4092 DOM_SID *owner_sid=NULL;
4096 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4098 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4099 revision = strtol(tok+9, NULL, 16);
4103 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4105 DEBUG(5, ("OWNER specified more than once!\n"));
4108 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4110 !convert_string_to_sid(ipc_cli, pol,
4112 owner_sid, tok+6)) {
4113 DEBUG(5, ("Failed to parse owner sid\n"));
4119 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4121 DEBUG(5, ("OWNER specified more than once!\n"));
4124 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4126 !convert_string_to_sid(ipc_cli, pol,
4128 owner_sid, tok+7)) {
4129 DEBUG(5, ("Failed to parse owner sid\n"));
4135 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4137 DEBUG(5, ("GROUP specified more than once!\n"));
4140 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4142 !convert_string_to_sid(ipc_cli, pol,
4144 group_sid, tok+6)) {
4145 DEBUG(5, ("Failed to parse group sid\n"));
4151 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4153 DEBUG(5, ("GROUP specified more than once!\n"));
4156 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4158 !convert_string_to_sid(ipc_cli, pol,
4160 group_sid, tok+6)) {
4161 DEBUG(5, ("Failed to parse group sid\n"));
4167 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4169 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4170 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4173 if(!add_ace(&dacl, &ace, ctx)) {
4174 DEBUG(5, ("Failed to add ACL %s\n", tok));
4180 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4182 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4183 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4186 if(!add_ace(&dacl, &ace, ctx)) {
4187 DEBUG(5, ("Failed to add ACL %s\n", tok));
4193 DEBUG(5, ("Failed to parse security descriptor\n"));
4197 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4198 owner_sid, group_sid, NULL, dacl, &sd_size);
4201 SAFE_FREE(group_sid);
4202 SAFE_FREE(owner_sid);
4208 /* Obtain the current dos attributes */
4209 static DOS_ATTR_DESC *
4210 dos_attr_query(SMBCCTX *context,
4212 const char *filename,
4215 struct timespec create_time_ts;
4216 struct timespec write_time_ts;
4217 struct timespec access_time_ts;
4218 struct timespec change_time_ts;
4221 SMB_INO_T inode = 0;
4224 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4230 /* Obtain the DOS attributes */
4231 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4239 errno = smbc_errno(context, srv->cli);
4240 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4247 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4248 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4249 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4250 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4257 /* parse a ascii version of a security descriptor */
4259 dos_attr_parse(SMBCCTX *context,
4265 const char *p = str;
4268 const char * create_time_attr;
4269 const char * access_time_attr;
4270 const char * write_time_attr;
4271 const char * change_time_attr;
4274 /* Determine whether to use old-style or new-style attribute names */
4275 if (context->internal->_full_time_names) {
4276 /* new-style names */
4277 attr_strings.create_time_attr = "CREATE_TIME";
4278 attr_strings.access_time_attr = "ACCESS_TIME";
4279 attr_strings.write_time_attr = "WRITE_TIME";
4280 attr_strings.change_time_attr = "CHANGE_TIME";
4282 /* old-style names */
4283 attr_strings.create_time_attr = NULL;
4284 attr_strings.access_time_attr = "A_TIME";
4285 attr_strings.write_time_attr = "M_TIME";
4286 attr_strings.change_time_attr = "C_TIME";
4289 /* if this is to set the entire ACL... */
4291 /* ... then increment past the first colon if there is one */
4292 if ((p = strchr(str, ':')) != NULL) {
4299 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4301 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4302 dad->mode = strtol(tok+5, NULL, 16);
4306 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4307 dad->size = (SMB_OFF_T)atof(tok+5);
4311 n = strlen(attr_strings.access_time_attr);
4312 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4313 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4317 n = strlen(attr_strings.change_time_attr);
4318 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4319 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4323 n = strlen(attr_strings.write_time_attr);
4324 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4325 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4329 if (attr_strings.create_time_attr != NULL) {
4330 n = strlen(attr_strings.create_time_attr);
4331 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4333 dad->create_time = (time_t)strtol(tok+n+1,
4339 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4340 dad->inode = (SMB_INO_T)atof(tok+6);
4346 /*****************************************************
4347 Retrieve the acls for a file.
4348 *******************************************************/
4351 cacl_get(SMBCCTX *context,
4354 struct cli_state *ipc_cli,
4370 bool exclude_nt_revision = False;
4371 bool exclude_nt_owner = False;
4372 bool exclude_nt_group = False;
4373 bool exclude_nt_acl = False;
4374 bool exclude_dos_mode = False;
4375 bool exclude_dos_size = False;
4376 bool exclude_dos_create_time = False;
4377 bool exclude_dos_access_time = False;
4378 bool exclude_dos_write_time = False;
4379 bool exclude_dos_change_time = False;
4380 bool exclude_dos_inode = False;
4381 bool numeric = True;
4382 bool determine_size = (bufsize == 0);
4386 fstring name_sandbox;
4390 struct timespec create_time_ts;
4391 struct timespec write_time_ts;
4392 struct timespec access_time_ts;
4393 struct timespec change_time_ts;
4394 time_t create_time = (time_t)0;
4395 time_t write_time = (time_t)0;
4396 time_t access_time = (time_t)0;
4397 time_t change_time = (time_t)0;
4401 struct cli_state *cli = srv->cli;
4403 const char * create_time_attr;
4404 const char * access_time_attr;
4405 const char * write_time_attr;
4406 const char * change_time_attr;
4409 const char * create_time_attr;
4410 const char * access_time_attr;
4411 const char * write_time_attr;
4412 const char * change_time_attr;
4413 } excl_attr_strings;
4415 /* Determine whether to use old-style or new-style attribute names */
4416 if (context->internal->_full_time_names) {
4417 /* new-style names */
4418 attr_strings.create_time_attr = "CREATE_TIME";
4419 attr_strings.access_time_attr = "ACCESS_TIME";
4420 attr_strings.write_time_attr = "WRITE_TIME";
4421 attr_strings.change_time_attr = "CHANGE_TIME";
4423 excl_attr_strings.create_time_attr = "CREATE_TIME";
4424 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4425 excl_attr_strings.write_time_attr = "WRITE_TIME";
4426 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4428 /* old-style names */
4429 attr_strings.create_time_attr = NULL;
4430 attr_strings.access_time_attr = "A_TIME";
4431 attr_strings.write_time_attr = "M_TIME";
4432 attr_strings.change_time_attr = "C_TIME";
4434 excl_attr_strings.create_time_attr = NULL;
4435 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4436 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4437 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4440 /* Copy name so we can strip off exclusions (if any are specified) */
4441 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4443 /* Ensure name is null terminated */
4444 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4446 /* Play in the sandbox */
4447 name = name_sandbox;
4449 /* If there are any exclusions, point to them and mask them from name */
4450 if ((pExclude = strchr(name, '!')) != NULL)
4455 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4456 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4457 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4458 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4459 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4460 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4461 numeric = (* (name + strlen(name) - 1) != '+');
4463 /* Look for exclusions from "all" requests */
4464 if (all || all_nt || all_dos) {
4466 /* Exclusions are delimited by '!' */
4469 pExclude = (p == NULL ? NULL : p + 1)) {
4471 /* Find end of this exclusion name */
4472 if ((p = strchr(pExclude, '!')) != NULL)
4477 /* Which exclusion name is this? */
4478 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4479 exclude_nt_revision = True;
4481 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4482 exclude_nt_owner = True;
4484 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4485 exclude_nt_group = True;
4487 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4488 exclude_nt_acl = True;
4490 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4491 exclude_dos_mode = True;
4493 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4494 exclude_dos_size = True;
4496 else if (excl_attr_strings.create_time_attr != NULL &&
4497 StrCaseCmp(pExclude,
4498 excl_attr_strings.change_time_attr) == 0) {
4499 exclude_dos_create_time = True;
4501 else if (StrCaseCmp(pExclude,
4502 excl_attr_strings.access_time_attr) == 0) {
4503 exclude_dos_access_time = True;
4505 else if (StrCaseCmp(pExclude,
4506 excl_attr_strings.write_time_attr) == 0) {
4507 exclude_dos_write_time = True;
4509 else if (StrCaseCmp(pExclude,
4510 excl_attr_strings.change_time_attr) == 0) {
4511 exclude_dos_change_time = True;
4513 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4514 exclude_dos_inode = True;
4517 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4528 * If we are (possibly) talking to an NT or new system and some NT
4529 * attributes have been requested...
4531 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4532 /* Point to the portion after "system.nt_sec_desc." */
4533 name += 19; /* if (all) this will be invalid but unused */
4535 /* ... then obtain any NT attributes which were requested */
4536 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4539 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4540 filename, cli_errstr(cli)));
4545 sd = cli_query_secdesc(cli, fnum, ctx);
4549 ("cacl_get Failed to query old descriptor\n"));
4554 cli_close(cli, fnum);
4556 if (! exclude_nt_revision) {
4557 if (all || all_nt) {
4558 if (determine_size) {
4559 p = talloc_asprintf(ctx,
4568 n = snprintf(buf, bufsize,
4572 } else if (StrCaseCmp(name, "revision") == 0) {
4573 if (determine_size) {
4574 p = talloc_asprintf(ctx, "%d",
4582 n = snprintf(buf, bufsize, "%d",
4587 if (!determine_size && n > bufsize) {
4597 if (! exclude_nt_owner) {
4598 /* Get owner and group sid */
4599 if (sd->owner_sid) {
4600 convert_sid_to_string(ipc_cli, pol,
4605 fstrcpy(sidstr, "");
4608 if (all || all_nt) {
4609 if (determine_size) {
4610 p = talloc_asprintf(ctx, ",OWNER:%s",
4617 } else if (sidstr[0] != '\0') {
4618 n = snprintf(buf, bufsize,
4619 ",OWNER:%s", sidstr);
4621 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4622 if (determine_size) {
4623 p = talloc_asprintf(ctx, "%s", sidstr);
4630 n = snprintf(buf, bufsize, "%s",
4635 if (!determine_size && n > bufsize) {
4645 if (! exclude_nt_group) {
4646 if (sd->group_sid) {
4647 convert_sid_to_string(ipc_cli, pol,
4651 fstrcpy(sidstr, "");
4654 if (all || all_nt) {
4655 if (determine_size) {
4656 p = talloc_asprintf(ctx, ",GROUP:%s",
4663 } else if (sidstr[0] != '\0') {
4664 n = snprintf(buf, bufsize,
4665 ",GROUP:%s", sidstr);
4667 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4668 if (determine_size) {
4669 p = talloc_asprintf(ctx, "%s", sidstr);
4676 n = snprintf(buf, bufsize,
4681 if (!determine_size && n > bufsize) {
4691 if (! exclude_nt_acl) {
4692 /* Add aces to value buffer */
4693 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4695 SEC_ACE *ace = &sd->dacl->aces[i];
4696 convert_sid_to_string(ipc_cli, pol,
4700 if (all || all_nt) {
4701 if (determine_size) {
4702 p = talloc_asprintf(
4718 ",ACL:%s:%d/%d/0x%08x",
4724 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4725 StrCaseCmp(name+3, sidstr) == 0) ||
4726 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4727 StrCaseCmp(name+4, sidstr) == 0)) {
4728 if (determine_size) {
4729 p = talloc_asprintf(
4741 n = snprintf(buf, bufsize,
4747 } else if (all_nt_acls) {
4748 if (determine_size) {
4749 p = talloc_asprintf(
4751 "%s%s:%d/%d/0x%08x",
4763 n = snprintf(buf, bufsize,
4764 "%s%s:%d/%d/0x%08x",
4772 if (!determine_size && n > bufsize) {
4783 /* Restore name pointer to its original value */
4787 if (all || some_dos) {
4788 /* Point to the portion after "system.dos_attr." */
4789 name += 16; /* if (all) this will be invalid but unused */
4791 /* Obtain the DOS attributes */
4792 if (!smbc_getatr(context, srv, filename, &mode, &size,
4799 errno = smbc_errno(context, srv->cli);
4804 create_time = convert_timespec_to_time_t(create_time_ts);
4805 access_time = convert_timespec_to_time_t(access_time_ts);
4806 write_time = convert_timespec_to_time_t(write_time_ts);
4807 change_time = convert_timespec_to_time_t(change_time_ts);
4809 if (! exclude_dos_mode) {
4810 if (all || all_dos) {
4811 if (determine_size) {
4812 p = talloc_asprintf(ctx,
4825 n = snprintf(buf, bufsize,
4833 } else if (StrCaseCmp(name, "mode") == 0) {
4834 if (determine_size) {
4835 p = talloc_asprintf(ctx, "0x%x", mode);
4842 n = snprintf(buf, bufsize,
4847 if (!determine_size && n > bufsize) {
4857 if (! exclude_dos_size) {
4858 if (all || all_dos) {
4859 if (determine_size) {
4860 p = talloc_asprintf(
4870 n = snprintf(buf, bufsize,
4874 } else if (StrCaseCmp(name, "size") == 0) {
4875 if (determine_size) {
4876 p = talloc_asprintf(
4886 n = snprintf(buf, bufsize,
4892 if (!determine_size && n > bufsize) {
4902 if (! exclude_dos_create_time &&
4903 attr_strings.create_time_attr != NULL) {
4904 if (all || all_dos) {
4905 if (determine_size) {
4906 p = talloc_asprintf(ctx,
4908 attr_strings.create_time_attr,
4916 n = snprintf(buf, bufsize,
4918 attr_strings.create_time_attr,
4921 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
4922 if (determine_size) {
4923 p = talloc_asprintf(ctx, "%lu", create_time);
4930 n = snprintf(buf, bufsize,
4931 "%lu", create_time);
4935 if (!determine_size && n > bufsize) {
4945 if (! exclude_dos_access_time) {
4946 if (all || all_dos) {
4947 if (determine_size) {
4948 p = talloc_asprintf(ctx,
4950 attr_strings.access_time_attr,
4958 n = snprintf(buf, bufsize,
4960 attr_strings.access_time_attr,
4963 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
4964 if (determine_size) {
4965 p = talloc_asprintf(ctx, "%lu", access_time);
4972 n = snprintf(buf, bufsize,
4973 "%lu", access_time);
4977 if (!determine_size && n > bufsize) {
4987 if (! exclude_dos_write_time) {
4988 if (all || all_dos) {
4989 if (determine_size) {
4990 p = talloc_asprintf(ctx,
4992 attr_strings.write_time_attr,
5000 n = snprintf(buf, bufsize,
5002 attr_strings.write_time_attr,
5005 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
5006 if (determine_size) {
5007 p = talloc_asprintf(ctx, "%lu", write_time);
5014 n = snprintf(buf, bufsize,
5019 if (!determine_size && n > bufsize) {
5029 if (! exclude_dos_change_time) {
5030 if (all || all_dos) {
5031 if (determine_size) {
5032 p = talloc_asprintf(ctx,
5034 attr_strings.change_time_attr,
5042 n = snprintf(buf, bufsize,
5044 attr_strings.change_time_attr,
5047 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5048 if (determine_size) {
5049 p = talloc_asprintf(ctx, "%lu", change_time);
5056 n = snprintf(buf, bufsize,
5057 "%lu", change_time);
5061 if (!determine_size && n > bufsize) {
5071 if (! exclude_dos_inode) {
5072 if (all || all_dos) {
5073 if (determine_size) {
5074 p = talloc_asprintf(
5084 n = snprintf(buf, bufsize,
5088 } else if (StrCaseCmp(name, "inode") == 0) {
5089 if (determine_size) {
5090 p = talloc_asprintf(
5100 n = snprintf(buf, bufsize,
5106 if (!determine_size && n > bufsize) {
5116 /* Restore name pointer to its original value */
5129 /*****************************************************
5130 set the ACLs on a file given an ascii description
5131 *******************************************************/
5133 cacl_set(TALLOC_CTX *ctx,
5134 struct cli_state *cli,
5135 struct cli_state *ipc_cli,
5137 const char *filename,
5138 const char *the_acl,
5144 SEC_DESC *sd = NULL, *old;
5145 SEC_ACL *dacl = NULL;
5146 DOM_SID *owner_sid = NULL;
5147 DOM_SID *group_sid = NULL;
5152 bool numeric = True;
5154 /* the_acl will be null for REMOVE_ALL operations */
5156 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5160 /* if this is to set the entire ACL... */
5161 if (*the_acl == '*') {
5162 /* ... then increment past the first colon */
5166 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5167 CONST_DISCARD(char *, the_acl));
5175 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5176 that doesn't deref sd */
5178 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5183 /* The desired access below is the only one I could find that works
5184 with NT4, W2KP and Samba */
5186 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5189 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5190 filename, cli_errstr(cli)));
5195 old = cli_query_secdesc(cli, fnum, ctx);
5198 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5203 cli_close(cli, fnum);
5206 case SMBC_XATTR_MODE_REMOVE_ALL:
5207 old->dacl->num_aces = 0;
5211 case SMBC_XATTR_MODE_REMOVE:
5212 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5215 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5216 if (sec_ace_equal(&sd->dacl->aces[i],
5217 &old->dacl->aces[j])) {
5219 for (k=j; k<old->dacl->num_aces-1;k++) {
5220 old->dacl->aces[k] =
5221 old->dacl->aces[k+1];
5223 old->dacl->num_aces--;
5238 case SMBC_XATTR_MODE_ADD:
5239 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5242 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5243 if (sid_equal(&sd->dacl->aces[i].trustee,
5244 &old->dacl->aces[j].trustee)) {
5245 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5250 old->dacl->aces[j] = sd->dacl->aces[i];
5256 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5262 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5263 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5269 case SMBC_XATTR_MODE_SET:
5271 owner_sid = old->owner_sid;
5272 group_sid = old->group_sid;
5276 case SMBC_XATTR_MODE_CHOWN:
5277 owner_sid = sd->owner_sid;
5280 case SMBC_XATTR_MODE_CHGRP:
5281 group_sid = sd->group_sid;
5285 /* Denied ACE entries must come before allowed ones */
5286 sort_acl(old->dacl);
5288 /* Create new security descriptor and set it */
5289 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5290 owner_sid, group_sid, NULL, dacl, &sd_size);
5292 fnum = cli_nt_create(cli, filename,
5293 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5296 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5297 filename, cli_errstr(cli)));
5302 if (!cli_set_secdesc(cli, fnum, sd)) {
5303 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5310 cli_close(cli, fnum);
5321 smbc_setxattr_ctx(SMBCCTX *context,
5342 const char * create_time_attr;
5343 const char * access_time_attr;
5344 const char * write_time_attr;
5345 const char * change_time_attr;
5348 if (!context || !context->internal ||
5349 !context->internal->_initialized) {
5351 errno = EINVAL; /* Best I can think of ... */
5363 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5364 fname, name, (int) size, (const char*)value));
5366 if (smbc_parse_path(context, fname,
5367 workgroup, sizeof(workgroup),
5368 server, sizeof(server),
5369 share, sizeof(share),
5372 password, sizeof(password),
5378 if (user[0] == (char)0) fstrcpy(user, context->user);
5380 srv = smbc_server(context, True,
5381 server, share, workgroup, user, password);
5383 return -1; /* errno set by smbc_server */
5386 if (! srv->no_nt_session) {
5387 ipc_srv = smbc_attr_server(context, server, share,
5388 workgroup, user, password,
5391 srv->no_nt_session = True;
5397 ctx = talloc_init("smbc_setxattr");
5404 * Are they asking to set the entire set of known attributes?
5406 if (StrCaseCmp(name, "system.*") == 0 ||
5407 StrCaseCmp(name, "system.*+") == 0) {
5410 talloc_asprintf(ctx, "%s:%s",
5411 name+7, (const char *) value);
5419 ret = cacl_set(ctx, srv->cli,
5420 ipc_srv->cli, &pol, path,
5423 ? SMBC_XATTR_MODE_SET
5424 : SMBC_XATTR_MODE_ADD),
5430 /* get a DOS Attribute Descriptor with current attributes */
5431 dad = dos_attr_query(context, ctx, path, srv);
5433 /* Overwrite old with new, using what was provided */
5434 dos_attr_parse(context, dad, srv, namevalue);
5436 /* Set the new DOS attributes */
5437 if (! smbc_setatr(context, srv, path,
5444 /* cause failure if NT failed too */
5449 /* we only fail if both NT and DOS sets failed */
5450 if (ret < 0 && ! dad) {
5451 ret = -1; /* in case dad was null */
5457 talloc_destroy(ctx);
5462 * Are they asking to set an access control element or to set
5463 * the entire access control list?
5465 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5466 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5467 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5468 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5469 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5473 talloc_asprintf(ctx, "%s:%s",
5474 name+19, (const char *) value);
5477 ret = -1; /* errno set by smbc_server() */
5479 else if (! namevalue) {
5483 ret = cacl_set(ctx, srv->cli,
5484 ipc_srv->cli, &pol, path,
5487 ? SMBC_XATTR_MODE_SET
5488 : SMBC_XATTR_MODE_ADD),
5491 talloc_destroy(ctx);
5496 * Are they asking to set the owner?
5498 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5499 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5503 talloc_asprintf(ctx, "%s:%s",
5504 name+19, (const char *) value);
5508 ret = -1; /* errno set by smbc_server() */
5510 else if (! namevalue) {
5514 ret = cacl_set(ctx, srv->cli,
5515 ipc_srv->cli, &pol, path,
5516 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5518 talloc_destroy(ctx);
5523 * Are they asking to set the group?
5525 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5526 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5530 talloc_asprintf(ctx, "%s:%s",
5531 name+19, (const char *) value);
5534 /* errno set by smbc_server() */
5537 else if (! namevalue) {
5541 ret = cacl_set(ctx, srv->cli,
5542 ipc_srv->cli, &pol, path,
5543 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5545 talloc_destroy(ctx);
5549 /* Determine whether to use old-style or new-style attribute names */
5550 if (context->internal->_full_time_names) {
5551 /* new-style names */
5552 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5553 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5554 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5555 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5557 /* old-style names */
5558 attr_strings.create_time_attr = NULL;
5559 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5560 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5561 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5565 * Are they asking to set a DOS attribute?
5567 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5568 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5569 (attr_strings.create_time_attr != NULL &&
5570 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5571 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5572 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5573 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5575 /* get a DOS Attribute Descriptor with current attributes */
5576 dad = dos_attr_query(context, ctx, path, srv);
5579 talloc_asprintf(ctx, "%s:%s",
5580 name+16, (const char *) value);
5585 /* Overwrite old with provided new params */
5586 dos_attr_parse(context, dad, srv, namevalue);
5588 /* Set the new DOS attributes */
5589 ret2 = smbc_setatr(context, srv, path,
5596 /* ret2 has True (success) / False (failure) */
5607 talloc_destroy(ctx);
5611 /* Unsupported attribute name */
5612 talloc_destroy(ctx);
5618 smbc_getxattr_ctx(SMBCCTX *context,
5636 const char * create_time_attr;
5637 const char * access_time_attr;
5638 const char * write_time_attr;
5639 const char * change_time_attr;
5643 if (!context || !context->internal ||
5644 !context->internal->_initialized) {
5646 errno = EINVAL; /* Best I can think of ... */
5658 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5660 if (smbc_parse_path(context, fname,
5661 workgroup, sizeof(workgroup),
5662 server, sizeof(server),
5663 share, sizeof(share),
5666 password, sizeof(password),
5672 if (user[0] == (char)0) fstrcpy(user, context->user);
5674 srv = smbc_server(context, True,
5675 server, share, workgroup, user, password);
5677 return -1; /* errno set by smbc_server */
5680 if (! srv->no_nt_session) {
5681 ipc_srv = smbc_attr_server(context, server, share,
5682 workgroup, user, password,
5685 srv->no_nt_session = True;
5691 ctx = talloc_init("smbc:getxattr");
5697 /* Determine whether to use old-style or new-style attribute names */
5698 if (context->internal->_full_time_names) {
5699 /* new-style names */
5700 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5701 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5702 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5703 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5705 /* old-style names */
5706 attr_strings.create_time_attr = NULL;
5707 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5708 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5709 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5712 /* Are they requesting a supported attribute? */
5713 if (StrCaseCmp(name, "system.*") == 0 ||
5714 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5715 StrCaseCmp(name, "system.*+") == 0 ||
5716 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5717 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5718 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5719 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5720 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5721 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5722 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5723 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5724 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5725 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5726 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5727 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5728 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5729 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5730 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5731 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5732 (attr_strings.create_time_attr != NULL &&
5733 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5734 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5735 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5736 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5737 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
5740 ret = cacl_get(context, ctx, srv,
5741 ipc_srv == NULL ? NULL : ipc_srv->cli,
5743 CONST_DISCARD(char *, name),
5744 CONST_DISCARD(char *, value), size);
5745 if (ret < 0 && errno == 0) {
5746 errno = smbc_errno(context, srv->cli);
5748 talloc_destroy(ctx);
5752 /* Unsupported attribute name */
5753 talloc_destroy(ctx);
5760 smbc_removexattr_ctx(SMBCCTX *context,
5776 if (!context || !context->internal ||
5777 !context->internal->_initialized) {
5779 errno = EINVAL; /* Best I can think of ... */
5791 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
5793 if (smbc_parse_path(context, fname,
5794 workgroup, sizeof(workgroup),
5795 server, sizeof(server),
5796 share, sizeof(share),
5799 password, sizeof(password),
5805 if (user[0] == (char)0) fstrcpy(user, context->user);
5807 srv = smbc_server(context, True,
5808 server, share, workgroup, user, password);
5810 return -1; /* errno set by smbc_server */
5813 if (! srv->no_nt_session) {
5814 ipc_srv = smbc_attr_server(context, server, share,
5815 workgroup, user, password,
5818 srv->no_nt_session = True;
5825 return -1; /* errno set by smbc_attr_server */
5828 ctx = talloc_init("smbc_removexattr");
5834 /* Are they asking to set the entire ACL? */
5835 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5836 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
5839 ret = cacl_set(ctx, srv->cli,
5840 ipc_srv->cli, &pol, path,
5841 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
5842 talloc_destroy(ctx);
5847 * Are they asking to remove one or more spceific security descriptor
5850 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5851 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5852 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5853 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5854 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5855 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5856 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5859 ret = cacl_set(ctx, srv->cli,
5860 ipc_srv->cli, &pol, path,
5861 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
5862 talloc_destroy(ctx);
5866 /* Unsupported attribute name */
5867 talloc_destroy(ctx);
5873 smbc_listxattr_ctx(SMBCCTX *context,
5879 * This isn't quite what listxattr() is supposed to do. This returns
5880 * the complete set of attribute names, always, rather than only those
5881 * attribute names which actually exist for a file. Hmmm...
5883 const char supported_old[] =
5886 "system.nt_sec_desc.revision\0"
5887 "system.nt_sec_desc.owner\0"
5888 "system.nt_sec_desc.owner+\0"
5889 "system.nt_sec_desc.group\0"
5890 "system.nt_sec_desc.group+\0"
5891 "system.nt_sec_desc.acl.*\0"
5892 "system.nt_sec_desc.acl\0"
5893 "system.nt_sec_desc.acl+\0"
5894 "system.nt_sec_desc.*\0"
5895 "system.nt_sec_desc.*+\0"
5896 "system.dos_attr.*\0"
5897 "system.dos_attr.mode\0"
5898 "system.dos_attr.c_time\0"
5899 "system.dos_attr.a_time\0"
5900 "system.dos_attr.m_time\0"
5902 const char supported_new[] =
5905 "system.nt_sec_desc.revision\0"
5906 "system.nt_sec_desc.owner\0"
5907 "system.nt_sec_desc.owner+\0"
5908 "system.nt_sec_desc.group\0"
5909 "system.nt_sec_desc.group+\0"
5910 "system.nt_sec_desc.acl.*\0"
5911 "system.nt_sec_desc.acl\0"
5912 "system.nt_sec_desc.acl+\0"
5913 "system.nt_sec_desc.*\0"
5914 "system.nt_sec_desc.*+\0"
5915 "system.dos_attr.*\0"
5916 "system.dos_attr.mode\0"
5917 "system.dos_attr.create_time\0"
5918 "system.dos_attr.access_time\0"
5919 "system.dos_attr.write_time\0"
5920 "system.dos_attr.change_time\0"
5922 const char * supported;
5924 if (context->internal->_full_time_names) {
5925 supported = supported_new;
5927 supported = supported_old;
5931 return sizeof(supported);
5934 if (sizeof(supported) > size) {
5939 /* this can't be strcpy() because there are embedded null characters */
5940 memcpy(list, supported, sizeof(supported));
5941 return sizeof(supported);
5946 * Open a print file to be written to by other calls
5950 smbc_open_print_job_ctx(SMBCCTX *context,
5959 if (!context || !context->internal ||
5960 !context->internal->_initialized) {
5974 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
5976 if (smbc_parse_path(context, fname,
5978 server, sizeof(server),
5979 share, sizeof(share),
5982 password, sizeof(password),
5988 /* What if the path is empty, or the file exists? */
5990 return (context->open)(context, fname, O_WRONLY, 666);
5995 * Routine to print a file on a remote server ...
5997 * We open the file, which we assume to be on a remote server, and then
5998 * copy it to a print file on the share specified by printq.
6002 smbc_print_file_ctx(SMBCCTX *c_file,
6014 if (!c_file || !c_file->internal->_initialized || !c_print ||
6015 !c_print->internal->_initialized) {
6022 if (!fname && !printq) {
6029 /* Try to open the file for reading ... */
6031 if ((long)(fid1 = (c_file->open)(c_file, fname, O_RDONLY, 0666)) < 0) {
6033 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
6034 return -1; /* smbc_open sets errno */
6038 /* Now, try to open the printer file for writing */
6040 if ((long)(fid2 = (c_print->open_print_job)(c_print, printq)) < 0) {
6042 saverr = errno; /* Save errno */
6043 (c_file->close_fn)(c_file, fid1);
6049 while ((bytes = (c_file->read)(c_file, fid1, buf, sizeof(buf))) > 0) {
6053 if (((c_print->write)(c_print, fid2, buf, bytes)) < 0) {
6056 (c_file->close_fn)(c_file, fid1);
6057 (c_print->close_fn)(c_print, fid2);
6066 (c_file->close_fn)(c_file, fid1); /* We have to close these anyway */
6067 (c_print->close_fn)(c_print, fid2);
6081 * Routine to list print jobs on a printer share ...
6085 smbc_list_print_jobs_ctx(SMBCCTX *context,
6087 smbc_list_print_job_fn fn)
6097 if (!context || !context->internal ||
6098 !context->internal->_initialized) {
6112 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6114 if (smbc_parse_path(context, fname,
6115 workgroup, sizeof(workgroup),
6116 server, sizeof(server),
6117 share, sizeof(share),
6120 password, sizeof(password),
6126 if (user[0] == (char)0) fstrcpy(user, context->user);
6128 srv = smbc_server(context, True,
6129 server, share, workgroup, user, password);
6133 return -1; /* errno set by smbc_server */
6137 if (cli_print_queue(srv->cli,
6138 (void (*)(struct print_job_info *))fn) < 0) {
6140 errno = smbc_errno(context, srv->cli);
6150 * Delete a print job from a remote printer share
6154 smbc_unlink_print_job_ctx(SMBCCTX *context,
6167 if (!context || !context->internal ||
6168 !context->internal->_initialized) {
6182 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6184 if (smbc_parse_path(context, fname,
6185 workgroup, sizeof(workgroup),
6186 server, sizeof(server),
6187 share, sizeof(share),
6190 password, sizeof(password),
6196 if (user[0] == (char)0) fstrcpy(user, context->user);
6198 srv = smbc_server(context, True,
6199 server, share, workgroup, user, password);
6203 return -1; /* errno set by smbc_server */
6207 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6210 errno = smbc_errno(context, srv->cli);
6211 else if (err == ERRnosuchprintjob)
6222 * Get a new empty handle to fill in with your own info
6225 smbc_new_context(void)
6229 context = SMB_MALLOC_P(SMBCCTX);
6235 ZERO_STRUCTP(context);
6237 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6238 if (!context->internal) {
6244 ZERO_STRUCTP(context->internal);
6247 /* ADD REASONABLE DEFAULTS */
6249 context->timeout = 20000; /* 20 seconds */
6251 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6252 context->options.urlencode_readdir_entries = False;/* backward compat */
6253 context->options.one_share_per_server = False;/* backward compat */
6254 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6255 /* backward compat */
6257 context->open = smbc_open_ctx;
6258 context->creat = smbc_creat_ctx;
6259 context->read = smbc_read_ctx;
6260 context->write = smbc_write_ctx;
6261 context->close_fn = smbc_close_ctx;
6262 context->unlink = smbc_unlink_ctx;
6263 context->rename = smbc_rename_ctx;
6264 context->lseek = smbc_lseek_ctx;
6265 context->stat = smbc_stat_ctx;
6266 context->fstat = smbc_fstat_ctx;
6267 context->opendir = smbc_opendir_ctx;
6268 context->closedir = smbc_closedir_ctx;
6269 context->readdir = smbc_readdir_ctx;
6270 context->getdents = smbc_getdents_ctx;
6271 context->mkdir = smbc_mkdir_ctx;
6272 context->rmdir = smbc_rmdir_ctx;
6273 context->telldir = smbc_telldir_ctx;
6274 context->lseekdir = smbc_lseekdir_ctx;
6275 context->fstatdir = smbc_fstatdir_ctx;
6276 context->chmod = smbc_chmod_ctx;
6277 context->utimes = smbc_utimes_ctx;
6278 context->setxattr = smbc_setxattr_ctx;
6279 context->getxattr = smbc_getxattr_ctx;
6280 context->removexattr = smbc_removexattr_ctx;
6281 context->listxattr = smbc_listxattr_ctx;
6282 context->open_print_job = smbc_open_print_job_ctx;
6283 context->print_file = smbc_print_file_ctx;
6284 context->list_print_jobs = smbc_list_print_jobs_ctx;
6285 context->unlink_print_job = smbc_unlink_print_job_ctx;
6287 context->callbacks.check_server_fn = smbc_check_server;
6288 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6290 smbc_default_cache_functions(context);
6298 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6299 * and thus you'll be leaking memory if not handled properly.
6303 smbc_free_context(SMBCCTX *context,
6313 DEBUG(1,("Performing aggressive shutdown.\n"));
6315 f = context->internal->_files;
6317 (context->close_fn)(context, f);
6320 context->internal->_files = NULL;
6322 /* First try to remove the servers the nice way. */
6323 if (context->callbacks.purge_cached_fn(context)) {
6326 DEBUG(1, ("Could not purge all servers, "
6327 "Nice way shutdown failed.\n"));
6328 s = context->internal->_servers;
6330 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6332 cli_shutdown(s->cli);
6333 (context->callbacks.remove_cached_srv_fn)(context,
6336 DLIST_REMOVE(context->internal->_servers, s);
6340 context->internal->_servers = NULL;
6344 /* This is the polite way */
6345 if ((context->callbacks.purge_cached_fn)(context)) {
6346 DEBUG(1, ("Could not purge all servers, "
6347 "free_context failed.\n"));
6351 if (context->internal->_servers) {
6352 DEBUG(1, ("Active servers in context, "
6353 "free_context failed.\n"));
6357 if (context->internal->_files) {
6358 DEBUG(1, ("Active files in context, "
6359 "free_context failed.\n"));
6365 /* Things we have to clean up */
6366 SAFE_FREE(context->workgroup);
6367 SAFE_FREE(context->netbios_name);
6368 SAFE_FREE(context->user);
6370 DEBUG(3, ("Context %p succesfully freed\n", context));
6371 SAFE_FREE(context->internal);
6378 * Each time the context structure is changed, we have binary backward
6379 * compatibility issues. Instead of modifying the public portions of the
6380 * context structure to add new options, instead, we put them in the internal
6381 * portion of the context structure and provide a set function for these new
6385 smbc_option_set(SMBCCTX *context,
6387 ... /* option_value */)
6393 smbc_get_auth_data_with_context_fn auth_fn;
6397 va_start(ap, option_name);
6399 if (strcmp(option_name, "debug_to_stderr") == 0) {
6401 * Log to standard error instead of standard output.
6403 option_value.b = (bool) va_arg(ap, int);
6404 context->internal->_debug_stderr = option_value.b;
6406 } else if (strcmp(option_name, "full_time_names") == 0) {
6408 * Use new-style time attribute names, e.g. WRITE_TIME rather
6409 * than the old-style names such as M_TIME. This allows also
6410 * setting/getting CREATE_TIME which was previously
6411 * unimplemented. (Note that the old C_TIME was supposed to
6412 * be CHANGE_TIME but was confused and sometimes referred to
6415 option_value.b = (bool) va_arg(ap, int);
6416 context->internal->_full_time_names = option_value.b;
6418 } else if (strcmp(option_name, "open_share_mode") == 0) {
6420 * The share mode to use for files opened with
6421 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6423 option_value.i = va_arg(ap, int);
6424 context->internal->_share_mode =
6425 (smbc_share_mode) option_value.i;
6427 } else if (strcmp(option_name, "auth_function") == 0) {
6429 * Use the new-style authentication function which includes
6432 option_value.auth_fn =
6433 va_arg(ap, smbc_get_auth_data_with_context_fn);
6434 context->internal->_auth_fn_with_context =
6435 option_value.auth_fn;
6436 } else if (strcmp(option_name, "user_data") == 0) {
6438 * Save a user data handle which may be retrieved by the user
6439 * with smbc_option_get()
6441 option_value.v = va_arg(ap, void *);
6442 context->internal->_user_data = option_value.v;
6450 * Retrieve the current value of an option
6453 smbc_option_get(SMBCCTX *context,
6456 if (strcmp(option_name, "debug_stderr") == 0) {
6458 * Log to standard error instead of standard output.
6460 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6461 return (void *) (intptr_t) context->internal->_debug_stderr;
6463 return (void *) context->internal->_debug_stderr;
6465 } else if (strcmp(option_name, "full_time_names") == 0) {
6467 * Use new-style time attribute names, e.g. WRITE_TIME rather
6468 * than the old-style names such as M_TIME. This allows also
6469 * setting/getting CREATE_TIME which was previously
6470 * unimplemented. (Note that the old C_TIME was supposed to
6471 * be CHANGE_TIME but was confused and sometimes referred to
6474 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6475 return (void *) (intptr_t) context->internal->_full_time_names;
6477 return (void *) context->internal->_full_time_names;
6480 } else if (strcmp(option_name, "auth_function") == 0) {
6482 * Use the new-style authentication function which includes
6485 return (void *) context->internal->_auth_fn_with_context;
6486 } else if (strcmp(option_name, "user_data") == 0) {
6488 * Save a user data handle which may be retrieved by the user
6489 * with smbc_option_get()
6491 return context->internal->_user_data;
6499 * Initialise the library etc
6501 * We accept a struct containing handle information.
6502 * valid values for info->debug from 0 to 100,
6503 * and insist that info->fn must be non-null.
6506 smbc_init_context(SMBCCTX *context)
6513 if (!context || !context->internal) {
6518 /* Do not initialise the same client twice */
6519 if (context->internal->_initialized) {
6523 if ((!context->callbacks.auth_fn &&
6524 !context->internal->_auth_fn_with_context) ||
6525 context->debug < 0 ||
6526 context->debug > 100) {
6533 if (!smbc_initialized) {
6535 * Do some library-wide intializations the first time we get
6538 bool conf_loaded = False;
6540 /* Set this to what the user wants */
6541 DEBUGLEVEL = context->debug;
6545 setup_logging("libsmbclient", True);
6546 if (context->internal->_debug_stderr) {
6548 x_setbuf(x_stderr, NULL);
6551 /* Here we would open the smb.conf file if needed ... */
6553 in_client = True; /* FIXME, make a param */
6555 home = getenv("HOME");
6557 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
6558 if (lp_load(conf, True, False, False, True)) {
6561 DEBUG(5, ("Could not load config file: %s\n",
6568 * Well, if that failed, try the dyn_CONFIGFILE
6569 * Which points to the standard locn, and if that
6570 * fails, silently ignore it and use the internal
6574 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6575 DEBUG(5, ("Could not load config file: %s\n",
6579 * We loaded the global config file. Now lets
6580 * load user-specific modifications to the
6583 slprintf(conf, sizeof(conf),
6584 "%s/.smb/smb.conf.append", home);
6585 if (!lp_load(conf, True, False, False, False)) {
6587 ("Could not append config file: "
6594 load_interfaces(); /* Load the list of interfaces ... */
6596 reopen_logs(); /* Get logging working ... */
6599 * Block SIGPIPE (from lib/util_sock.c: write())
6600 * It is not needed and should not stop execution
6602 BlockSignals(True, SIGPIPE);
6604 /* Done with one-time initialisation */
6605 smbc_initialized = 1;
6609 if (!context->user) {
6611 * FIXME: Is this the best way to get the user info?
6613 user = getenv("USER");
6614 /* walk around as "guest" if no username can be found */
6615 if (!user) context->user = SMB_STRDUP("guest");
6616 else context->user = SMB_STRDUP(user);
6619 if (!context->netbios_name) {
6621 * We try to get our netbios name from the config. If that
6622 * fails we fall back on constructing our netbios name from
6625 if (global_myname()) {
6626 context->netbios_name = SMB_STRDUP(global_myname());
6630 * Hmmm, I want to get hostname as well, but I am too
6631 * lazy for the moment
6634 context->netbios_name = (char *)SMB_MALLOC(17);
6635 if (!context->netbios_name) {
6639 slprintf(context->netbios_name, 16,
6640 "smbc%s%d", context->user, pid);
6644 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6646 if (!context->workgroup) {
6647 if (lp_workgroup()) {
6648 context->workgroup = SMB_STRDUP(lp_workgroup());
6651 /* TODO: Think about a decent default workgroup */
6652 context->workgroup = SMB_STRDUP("samba");
6656 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6658 /* shortest timeout is 1 second */
6659 if (context->timeout > 0 && context->timeout < 1000)
6660 context->timeout = 1000;
6663 * FIXME: Should we check the function pointers here?
6666 context->internal->_initialized = True;
6672 /* Return the verion of samba, and thus libsmbclient */
6676 return samba_version_string();