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;
651 int tried_reverse = 0;
654 const char *username_used;
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);
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, &ip);
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, &ip);
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 in_addr rem_ip;
803 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
804 DEBUG(4, ("Could not convert IP address "
805 "%s to struct in_addr\n", server));
810 tried_reverse++; /* Yuck */
812 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
813 make_nmb_name(&called, remote_name, 0x20);
822 DEBUG(4,(" session request ok\n"));
824 if (!cli_negprot(c)) {
830 username_used = username;
832 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
833 password, strlen(password),
834 password, strlen(password),
837 /* Failed. Try an anonymous login, if allowed by flags. */
840 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
841 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
852 DEBUG(4,(" session setup ok\n"));
854 if (!cli_send_tconX(c, share, "?????",
855 password, strlen(password)+1)) {
856 errno = smbc_errno(context, c);
861 DEBUG(4,(" tconx ok\n"));
864 * Ok, we have got a nice connection
865 * Let's allocate a server structure.
868 srv = SMB_MALLOC_P(SMBCSRV);
876 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
877 srv->no_pathinfo = False;
878 srv->no_pathinfo2 = False;
879 srv->no_nt_session = False;
881 /* now add it to the cache (internal or external) */
882 /* Let the cache function set errno if it wants to */
884 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
885 int saved_errno = errno;
886 DEBUG(3, (" Failed to add server to cache\n"));
894 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
895 server, share, srv));
897 DLIST_ADD(context->internal->_servers, srv);
911 * Connect to a server for getting/setting attributes, possibly on an existing
912 * connection. This works similarly to smbc_server().
915 smbc_attr_server(SMBCCTX *context,
925 struct cli_state *ipc_cli;
926 struct rpc_pipe_client *pipe_hnd;
928 SMBCSRV *ipc_srv=NULL;
931 * See if we've already created this special connection. Reference
932 * our "special" share name '*IPC$', which is an impossible real share
933 * name due to the leading asterisk.
935 ipc_srv = find_server(context, server, "*IPC$",
936 workgroup, username, password);
939 /* We didn't find a cached connection. Get the password */
940 if (*password == '\0') {
941 /* ... then retrieve it now. */
942 if (context->internal->_auth_fn_with_context != NULL) {
943 context->internal->_auth_fn_with_context(
946 workgroup, sizeof(fstring),
947 username, sizeof(fstring),
948 password, sizeof(fstring));
950 context->callbacks.auth_fn(
952 workgroup, sizeof(fstring),
953 username, sizeof(fstring),
954 password, sizeof(fstring));
959 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
960 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
964 nt_status = cli_full_connection(&ipc_cli,
965 global_myname(), server,
966 &ip, 0, "IPC$", "?????",
970 if (! NT_STATUS_IS_OK(nt_status)) {
971 DEBUG(1,("cli_full_connection failed! (%s)\n",
972 nt_errstr(nt_status)));
977 ipc_srv = SMB_MALLOC_P(SMBCSRV);
980 cli_shutdown(ipc_cli);
984 ZERO_STRUCTP(ipc_srv);
985 ipc_srv->cli = ipc_cli;
988 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
992 DEBUG(1, ("cli_nt_session_open fail!\n"));
994 cli_shutdown(ipc_srv->cli);
1000 * Some systems don't support
1001 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1002 * so we might as well do it too.
1005 nt_status = rpccli_lsa_open_policy(
1007 ipc_srv->cli->mem_ctx,
1009 GENERIC_EXECUTE_ACCESS,
1012 if (!NT_STATUS_IS_OK(nt_status)) {
1013 errno = smbc_errno(context, ipc_srv->cli);
1014 cli_shutdown(ipc_srv->cli);
1019 /* now add it to the cache (internal or external) */
1021 errno = 0; /* let cache function set errno if it likes */
1022 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
1027 DEBUG(3, (" Failed to add server to cache\n"));
1031 cli_shutdown(ipc_srv->cli);
1036 DLIST_ADD(context->internal->_servers, ipc_srv);
1043 * Routine to open() a file ...
1047 smbc_open_ctx(SMBCCTX *context,
1052 fstring server, share, user, password, workgroup;
1055 struct cli_state *targetcli;
1056 SMBCSRV *srv = NULL;
1057 SMBCFILE *file = NULL;
1060 if (!context || !context->internal ||
1061 !context->internal->_initialized) {
1063 errno = EINVAL; /* Best I can think of ... */
1075 if (smbc_parse_path(context, fname,
1076 workgroup, sizeof(workgroup),
1077 server, sizeof(server),
1078 share, sizeof(share),
1081 password, sizeof(password),
1087 if (user[0] == (char)0) fstrcpy(user, context->user);
1089 srv = smbc_server(context, True,
1090 server, share, workgroup, user, password);
1094 if (errno == EPERM) errno = EACCES;
1095 return NULL; /* smbc_server sets errno */
1099 /* Hmmm, the test for a directory is suspect here ... FIXME */
1101 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1108 file = SMB_MALLOC_P(SMBCFILE);
1119 /*d_printf(">>>open: resolving %s\n", path);*/
1120 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1122 d_printf("Could not resolve %s\n", path);
1126 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1128 if ((fd = cli_open(targetcli, targetpath, flags,
1129 context->internal->_share_mode)) < 0) {
1131 /* Handle the error ... */
1134 errno = smbc_errno(context, targetcli);
1139 /* Fill in file struct */
1142 file->fname = SMB_STRDUP(fname);
1147 DLIST_ADD(context->internal->_files, file);
1150 * If the file was opened in O_APPEND mode, all write
1151 * operations should be appended to the file. To do that,
1152 * though, using this protocol, would require a getattrE()
1153 * call for each and every write, to determine where the end
1154 * of the file is. (There does not appear to be an append flag
1155 * in the protocol.) Rather than add all of that overhead of
1156 * retrieving the current end-of-file offset prior to each
1157 * write operation, we'll assume that most append operations
1158 * will continuously write, so we'll just set the offset to
1159 * the end of the file now and hope that's adequate.
1161 * Note to self: If this proves inadequate, and O_APPEND
1162 * should, in some cases, be forced for each write, add a
1163 * field in the context options structure, for
1164 * "strict_append_mode" which would select between the current
1165 * behavior (if FALSE) or issuing a getattrE() prior to each
1166 * write and forcing the write to the end of the file (if
1167 * TRUE). Adding that capability will likely require adding
1168 * an "append" flag into the _SMBCFILE structure to track
1169 * whether a file was opened in O_APPEND mode. -- djl
1171 if (flags & O_APPEND) {
1172 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1173 (void) smbc_close_ctx(context, file);
1183 /* Check if opendir needed ... */
1188 eno = smbc_errno(context, srv->cli);
1189 file = context->opendir(context, fname);
1190 if (!file) errno = eno;
1195 errno = EINVAL; /* FIXME, correct errno ? */
1201 * Routine to create a file
1204 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1207 smbc_creat_ctx(SMBCCTX *context,
1212 if (!context || !context->internal ||
1213 !context->internal->_initialized) {
1220 return smbc_open_ctx(context, path, creat_bits, mode);
1224 * Routine to read() a file ...
1228 smbc_read_ctx(SMBCCTX *context,
1234 fstring server, share, user, password;
1235 pstring path, targetpath;
1236 struct cli_state *targetcli;
1241 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1242 * appears to pass file->offset (which is type off_t) differently than
1243 * a local variable of type off_t. Using local variable "offset" in
1244 * the call to cli_read() instead of file->offset fixes a problem
1245 * retrieving data at an offset greater than 4GB.
1249 if (!context || !context->internal ||
1250 !context->internal->_initialized) {
1257 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1259 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1266 offset = file->offset;
1268 /* Check that the buffer exists ... */
1277 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1278 if (smbc_parse_path(context, file->fname,
1280 server, sizeof(server),
1281 share, sizeof(share),
1284 password, sizeof(password),
1290 /*d_printf(">>>read: resolving %s\n", path);*/
1291 if (!cli_resolve_path("", file->srv->cli, path,
1292 &targetcli, targetpath))
1294 d_printf("Could not resolve %s\n", path);
1297 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1299 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1303 errno = smbc_errno(context, targetcli);
1308 file->offset += ret;
1310 DEBUG(4, (" --> %d\n", ret));
1312 return ret; /* Success, ret bytes of data ... */
1317 * Routine to write() a file ...
1321 smbc_write_ctx(SMBCCTX *context,
1328 fstring server, share, user, password;
1329 pstring path, targetpath;
1330 struct cli_state *targetcli;
1332 /* First check all pointers before dereferencing them */
1334 if (!context || !context->internal ||
1335 !context->internal->_initialized) {
1342 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1349 /* Check that the buffer exists ... */
1358 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1360 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1361 if (smbc_parse_path(context, file->fname,
1363 server, sizeof(server),
1364 share, sizeof(share),
1367 password, sizeof(password),
1373 /*d_printf(">>>write: resolving %s\n", path);*/
1374 if (!cli_resolve_path("", file->srv->cli, path,
1375 &targetcli, targetpath))
1377 d_printf("Could not resolve %s\n", path);
1380 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1383 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1387 errno = smbc_errno(context, targetcli);
1392 file->offset += ret;
1394 return ret; /* Success, 0 bytes of data ... */
1398 * Routine to close() a file ...
1402 smbc_close_ctx(SMBCCTX *context,
1406 fstring server, share, user, password;
1407 pstring path, targetpath;
1408 struct cli_state *targetcli;
1410 if (!context || !context->internal ||
1411 !context->internal->_initialized) {
1418 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1428 return context->closedir(context, file);
1432 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1433 if (smbc_parse_path(context, file->fname,
1435 server, sizeof(server),
1436 share, sizeof(share),
1439 password, sizeof(password),
1445 /*d_printf(">>>close: resolving %s\n", path);*/
1446 if (!cli_resolve_path("", file->srv->cli, path,
1447 &targetcli, targetpath))
1449 d_printf("Could not resolve %s\n", path);
1452 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1454 if (!cli_close(targetcli, file->cli_fd)) {
1456 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1458 /* Deallocate slot and remove the server
1459 * from the server cache if unused */
1460 errno = smbc_errno(context, targetcli);
1462 DLIST_REMOVE(context->internal->_files, file);
1463 SAFE_FREE(file->fname);
1465 context->callbacks.remove_unused_server_fn(context, srv);
1471 DLIST_REMOVE(context->internal->_files, file);
1472 SAFE_FREE(file->fname);
1479 * Get info from an SMB server on a file. Use a qpathinfo call first
1480 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1483 smbc_getatr(SMBCCTX * context,
1488 struct timespec *create_time_ts,
1489 struct timespec *access_time_ts,
1490 struct timespec *write_time_ts,
1491 struct timespec *change_time_ts,
1496 struct cli_state *targetcli;
1499 if (!context || !context->internal ||
1500 !context->internal->_initialized) {
1507 /* path fixup for . and .. */
1508 if (strequal(path, ".") || strequal(path, ".."))
1509 pstrcpy(fixedpath, "\\");
1512 pstrcpy(fixedpath, path);
1513 trim_string(fixedpath, NULL, "\\..");
1514 trim_string(fixedpath, NULL, "\\.");
1516 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1518 if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
1520 d_printf("Couldn't resolve %s\n", path);
1524 if (!srv->no_pathinfo2 &&
1525 cli_qpathinfo2(targetcli, targetpath,
1534 /* if this is NT then don't bother with the getatr */
1535 if (targetcli->capabilities & CAP_NT_SMBS) {
1540 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1542 struct timespec w_time_ts;
1544 w_time_ts = convert_time_t_to_timespec(write_time);
1546 if (write_time_ts != NULL) {
1547 *write_time_ts = w_time_ts;
1550 if (create_time_ts != NULL) {
1551 *create_time_ts = w_time_ts;
1554 if (access_time_ts != NULL) {
1555 *access_time_ts = w_time_ts;
1558 if (change_time_ts != NULL) {
1559 *change_time_ts = w_time_ts;
1562 srv->no_pathinfo2 = True;
1572 * Set file info on an SMB server. Use setpathinfo call first. If that
1573 * fails, use setattrE..
1575 * Access and modification time parameters are always used and must be
1576 * provided. Create time, if zero, will be determined from the actual create
1577 * time of the file. If non-zero, the create time will be set as well.
1579 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1582 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1593 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1594 * modern function for "new code" to be using, and it works given a
1595 * filename rather than requiring that the file be opened to have its
1596 * attributes manipulated.
1598 if (srv->no_pathinfo ||
1599 ! cli_setpathinfo(srv->cli, path,
1607 * setpathinfo is not supported; go to plan B.
1609 * cli_setatr() does not work on win98, and it also doesn't
1610 * support setting the access time (only the modification
1611 * time), so in all cases, we open the specified file and use
1612 * cli_setattrE() which should work on all OS versions, and
1613 * supports both times.
1616 /* Don't try {q,set}pathinfo() again, with this server */
1617 srv->no_pathinfo = True;
1620 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1622 errno = smbc_errno(context, srv->cli);
1626 /* Set the new attributes */
1627 ret = cli_setattrE(srv->cli, fd,
1632 /* Close the file */
1633 cli_close(srv->cli, fd);
1636 * Unfortunately, setattrE() doesn't have a provision for
1637 * setting the access mode (attributes). We'll have to try
1638 * cli_setatr() for that, and with only this parameter, it
1639 * seems to work on win98.
1641 if (ret && mode != (uint16) -1) {
1642 ret = cli_setatr(srv->cli, path, mode, 0);
1646 errno = smbc_errno(context, srv->cli);
1655 * Routine to unlink() a file
1659 smbc_unlink_ctx(SMBCCTX *context,
1662 fstring server, share, user, password, workgroup;
1663 pstring path, targetpath;
1664 struct cli_state *targetcli;
1665 SMBCSRV *srv = NULL;
1667 if (!context || !context->internal ||
1668 !context->internal->_initialized) {
1670 errno = EINVAL; /* Best I can think of ... */
1682 if (smbc_parse_path(context, fname,
1683 workgroup, sizeof(workgroup),
1684 server, sizeof(server),
1685 share, sizeof(share),
1688 password, sizeof(password),
1694 if (user[0] == (char)0) fstrcpy(user, context->user);
1696 srv = smbc_server(context, True,
1697 server, share, workgroup, user, password);
1701 return -1; /* smbc_server sets errno */
1705 /*d_printf(">>>unlink: resolving %s\n", path);*/
1706 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1708 d_printf("Could not resolve %s\n", path);
1711 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1713 if (!cli_unlink(targetcli, targetpath)) {
1715 errno = smbc_errno(context, targetcli);
1717 if (errno == EACCES) { /* Check if the file is a directory */
1722 struct timespec write_time_ts;
1723 struct timespec access_time_ts;
1724 struct timespec change_time_ts;
1727 if (!smbc_getatr(context, srv, path, &mode, &size,
1734 /* Hmmm, bad error ... What? */
1736 errno = smbc_errno(context, targetcli);
1742 if (IS_DOS_DIR(mode))
1745 errno = saverr; /* Restore this */
1754 return 0; /* Success ... */
1759 * Routine to rename() a file
1763 smbc_rename_ctx(SMBCCTX *ocontext,
1779 pstring targetpath1;
1780 pstring targetpath2;
1781 struct cli_state *targetcli1;
1782 struct cli_state *targetcli2;
1783 SMBCSRV *srv = NULL;
1785 if (!ocontext || !ncontext ||
1786 !ocontext->internal || !ncontext->internal ||
1787 !ocontext->internal->_initialized ||
1788 !ncontext->internal->_initialized) {
1790 errno = EINVAL; /* Best I can think of ... */
1795 if (!oname || !nname) {
1802 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1804 smbc_parse_path(ocontext, oname,
1805 workgroup, sizeof(workgroup),
1806 server1, sizeof(server1),
1807 share1, sizeof(share1),
1808 path1, sizeof(path1),
1809 user1, sizeof(user1),
1810 password1, sizeof(password1),
1813 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1815 smbc_parse_path(ncontext, nname,
1817 server2, sizeof(server2),
1818 share2, sizeof(share2),
1819 path2, sizeof(path2),
1820 user2, sizeof(user2),
1821 password2, sizeof(password2),
1824 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1826 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1827 strcmp(user1, user2)) {
1829 /* Can't rename across file systems, or users?? */
1836 srv = smbc_server(ocontext, True,
1837 server1, share1, workgroup, user1, password1);
1844 /*d_printf(">>>rename: resolving %s\n", path1);*/
1845 if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
1847 d_printf("Could not resolve %s\n", path1);
1850 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1851 /*d_printf(">>>rename: resolving %s\n", path2);*/
1852 if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
1854 d_printf("Could not resolve %s\n", path2);
1857 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1859 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1860 strcmp(targetcli1->share, targetcli2->share))
1862 /* can't rename across file systems */
1868 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1869 int eno = smbc_errno(ocontext, targetcli1);
1871 if (eno != EEXIST ||
1872 !cli_unlink(targetcli1, targetpath2) ||
1873 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1881 return 0; /* Success */
1886 * A routine to lseek() a file
1890 smbc_lseek_ctx(SMBCCTX *context,
1896 fstring server, share, user, password;
1897 pstring path, targetpath;
1898 struct cli_state *targetcli;
1900 if (!context || !context->internal ||
1901 !context->internal->_initialized) {
1908 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1918 return -1; /* Can't lseek a dir ... */
1924 file->offset = offset;
1928 file->offset += offset;
1932 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1933 if (smbc_parse_path(context, file->fname,
1935 server, sizeof(server),
1936 share, sizeof(share),
1939 password, sizeof(password),
1946 /*d_printf(">>>lseek: resolving %s\n", path);*/
1947 if (!cli_resolve_path("", file->srv->cli, path,
1948 &targetcli, targetpath))
1950 d_printf("Could not resolve %s\n", path);
1953 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1955 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
1956 &size, NULL, NULL, NULL, NULL, NULL))
1958 SMB_OFF_T b_size = size;
1959 if (!cli_getattrE(targetcli, file->cli_fd,
1960 NULL, &b_size, NULL, NULL, NULL))
1967 file->offset = size + offset;
1976 return file->offset;
1981 * Generate an inode number from file name for those things that need it
1985 smbc_inode(SMBCCTX *context,
1989 if (!context || !context->internal ||
1990 !context->internal->_initialized) {
1997 if (!*name) return 2; /* FIXME, why 2 ??? */
1998 return (ino_t)str_checksum(name);
2003 * Routine to put basic stat info into a stat structure ... Used by stat and
2008 smbc_setup_stat(SMBCCTX *context,
2017 if (IS_DOS_DIR(mode)) {
2018 st->st_mode = SMBC_DIR_MODE;
2020 st->st_mode = SMBC_FILE_MODE;
2023 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2024 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2025 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2026 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2029 #ifdef HAVE_STAT_ST_BLKSIZE
2030 st->st_blksize = 512;
2032 #ifdef HAVE_STAT_ST_BLOCKS
2033 st->st_blocks = (size+511)/512;
2035 st->st_uid = getuid();
2036 st->st_gid = getgid();
2038 if (IS_DOS_DIR(mode)) {
2044 if (st->st_ino == 0) {
2045 st->st_ino = smbc_inode(context, fname);
2048 return True; /* FIXME: Is this needed ? */
2053 * Routine to stat a file given a name
2057 smbc_stat_ctx(SMBCCTX *context,
2068 struct timespec write_time_ts;
2069 struct timespec access_time_ts;
2070 struct timespec change_time_ts;
2075 if (!context || !context->internal ||
2076 !context->internal->_initialized) {
2078 errno = EINVAL; /* Best I can think of ... */
2090 DEBUG(4, ("smbc_stat(%s)\n", fname));
2092 if (smbc_parse_path(context, fname,
2093 workgroup, sizeof(workgroup),
2094 server, sizeof(server),
2095 share, sizeof(share),
2098 password, sizeof(password),
2104 if (user[0] == (char)0) fstrcpy(user, context->user);
2106 srv = smbc_server(context, True,
2107 server, share, workgroup, user, password);
2110 return -1; /* errno set by smbc_server */
2113 if (!smbc_getatr(context, srv, path, &mode, &size,
2120 errno = smbc_errno(context, srv->cli);
2127 smbc_setup_stat(context, st, path, size, mode);
2129 set_atimespec(st, access_time_ts);
2130 set_ctimespec(st, change_time_ts);
2131 set_mtimespec(st, write_time_ts);
2132 st->st_dev = srv->dev;
2139 * Routine to stat a file given an fd
2143 smbc_fstat_ctx(SMBCCTX *context,
2147 struct timespec change_time_ts;
2148 struct timespec access_time_ts;
2149 struct timespec write_time_ts;
2158 struct cli_state *targetcli;
2161 if (!context || !context->internal ||
2162 !context->internal->_initialized) {
2169 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2178 return context->fstatdir(context, file, st);
2182 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2183 if (smbc_parse_path(context, file->fname,
2185 server, sizeof(server),
2186 share, sizeof(share),
2189 password, sizeof(password),
2195 /*d_printf(">>>fstat: resolving %s\n", path);*/
2196 if (!cli_resolve_path("", file->srv->cli, path,
2197 &targetcli, targetpath))
2199 d_printf("Could not resolve %s\n", path);
2202 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2204 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2211 time_t change_time, access_time, write_time;
2213 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2214 &change_time, &access_time, &write_time)) {
2220 change_time_ts = convert_time_t_to_timespec(change_time);
2221 access_time_ts = convert_time_t_to_timespec(access_time);
2222 write_time_ts = convert_time_t_to_timespec(write_time);
2227 smbc_setup_stat(context, st, file->fname, size, mode);
2229 set_atimespec(st, access_time_ts);
2230 set_ctimespec(st, change_time_ts);
2231 set_mtimespec(st, write_time_ts);
2232 st->st_dev = file->srv->dev;
2239 * Routine to open a directory
2240 * We accept the URL syntax explained in smbc_parse_path(), above.
2244 smbc_remove_dir(SMBCFILE *dir)
2246 struct smbc_dir_list *d,*f;
2253 SAFE_FREE(f->dirent);
2258 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2263 add_dirent(SMBCFILE *dir,
2265 const char *comment,
2268 struct smbc_dirent *dirent;
2270 int name_length = (name == NULL ? 0 : strlen(name));
2271 int comment_len = (comment == NULL ? 0 : strlen(comment));
2274 * Allocate space for the dirent, which must be increased by the
2275 * size of the name and the comment and 1 each for the null terminator.
2278 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2280 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2284 dir->dir_error = ENOMEM;
2289 ZERO_STRUCTP(dirent);
2291 if (dir->dir_list == NULL) {
2293 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2294 if (!dir->dir_list) {
2297 dir->dir_error = ENOMEM;
2301 ZERO_STRUCTP(dir->dir_list);
2303 dir->dir_end = dir->dir_next = dir->dir_list;
2307 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2309 if (!dir->dir_end->next) {
2312 dir->dir_error = ENOMEM;
2316 ZERO_STRUCTP(dir->dir_end->next);
2318 dir->dir_end = dir->dir_end->next;
2321 dir->dir_end->next = NULL;
2322 dir->dir_end->dirent = dirent;
2324 dirent->smbc_type = type;
2325 dirent->namelen = name_length;
2326 dirent->commentlen = comment_len;
2327 dirent->dirlen = size;
2330 * dirent->namelen + 1 includes the null (no null termination needed)
2331 * Ditto for dirent->commentlen.
2332 * The space for the two null bytes was allocated.
2334 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2335 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2336 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2343 list_unique_wg_fn(const char *name,
2345 const char *comment,
2348 SMBCFILE *dir = (SMBCFILE *)state;
2349 struct smbc_dir_list *dir_list;
2350 struct smbc_dirent *dirent;
2354 dirent_type = dir->dir_type;
2356 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2358 /* An error occurred, what do we do? */
2359 /* FIXME: Add some code here */
2362 /* Point to the one just added */
2363 dirent = dir->dir_end->dirent;
2365 /* See if this was a duplicate */
2366 for (dir_list = dir->dir_list;
2367 dir_list != dir->dir_end;
2368 dir_list = dir_list->next) {
2370 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2371 /* Duplicate. End end of list need to be removed. */
2375 if (do_remove && dir_list->next == dir->dir_end) {
2376 /* Found the end of the list. Remove it. */
2377 dir->dir_end = dir_list;
2378 free(dir_list->next);
2380 dir_list->next = NULL;
2387 list_fn(const char *name,
2389 const char *comment,
2392 SMBCFILE *dir = (SMBCFILE *)state;
2396 * We need to process the type a little ...
2398 * Disk share = 0x00000000
2399 * Print share = 0x00000001
2400 * Comms share = 0x00000002 (obsolete?)
2401 * IPC$ share = 0x00000003
2403 * administrative shares:
2404 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2407 if (dir->dir_type == SMBC_FILE_SHARE) {
2410 case 0 | 0x80000000:
2412 dirent_type = SMBC_FILE_SHARE;
2416 dirent_type = SMBC_PRINTER_SHARE;
2420 dirent_type = SMBC_COMMS_SHARE;
2423 case 3 | 0x80000000:
2425 dirent_type = SMBC_IPC_SHARE;
2429 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2434 dirent_type = dir->dir_type;
2437 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2439 /* An error occurred, what do we do? */
2440 /* FIXME: Add some code here */
2446 dir_list_fn(const char *mnt,
2452 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2453 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2455 /* Handle an error ... */
2457 /* FIXME: Add some code ... */
2464 net_share_enum_rpc(struct cli_state *cli,
2465 void (*fn)(const char *name,
2467 const char *comment,
2474 uint32 info_level = 1;
2475 uint32 preferred_len = 0xffffffff;
2476 struct srvsvc_NetShareCtr1 ctr1;
2477 union srvsvc_NetShareCtr ctr;
2479 struct rpc_pipe_client *pipe_hnd;
2483 /* Open the server service pipe */
2484 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2486 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2490 /* Allocate a context for parsing and for the entries in "ctr" */
2491 mem_ctx = talloc_init("libsmbclient: net_share_enum_rpc");
2492 if (mem_ctx == NULL) {
2493 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2494 cli_rpc_pipe_close(pipe_hnd);
2501 /* Issue the NetShareEnum RPC call and retrieve the response */
2503 result = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
2504 &info_level, &ctr, preferred_len,
2505 &numentries, &enum_hnd);
2507 /* Was it successful? */
2508 if (!NT_STATUS_IS_OK(result) || numentries == 0) {
2509 /* Nope. Go clean up. */
2513 /* For each returned entry... */
2514 for (i = 0; i < numentries; i++) {
2516 /* Add this share to the list */
2517 (*fn)(ctr.ctr1->array[i].name,
2518 ctr.ctr1->array[i].type,
2519 ctr.ctr1->array[i].comment, state);
2523 /* Close the server service pipe */
2524 cli_rpc_pipe_close(pipe_hnd);
2526 /* Free all memory which was allocated for this request */
2527 TALLOC_FREE(mem_ctx);
2529 /* Tell 'em if it worked */
2530 return NT_STATUS_IS_OK(result) ? 0 : -1;
2536 smbc_opendir_ctx(SMBCCTX *context,
2540 fstring server, share, user, password, options;
2545 SMBCSRV *srv = NULL;
2546 SMBCFILE *dir = NULL;
2547 struct _smbc_callbacks *cb;
2548 struct in_addr rem_ip;
2550 if (!context || !context->internal ||
2551 !context->internal->_initialized) {
2552 DEBUG(4, ("no valid context\n"));
2553 errno = EINVAL + 8192;
2559 DEBUG(4, ("no valid fname\n"));
2560 errno = EINVAL + 8193;
2564 if (smbc_parse_path(context, fname,
2565 workgroup, sizeof(workgroup),
2566 server, sizeof(server),
2567 share, sizeof(share),
2570 password, sizeof(password),
2571 options, sizeof(options))) {
2572 DEBUG(4, ("no valid path\n"));
2573 errno = EINVAL + 8194;
2577 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2578 "path='%s' options='%s'\n",
2579 fname, server, share, path, options));
2581 /* Ensure the options are valid */
2582 if (smbc_check_options(server, share, path, options)) {
2583 DEBUG(4, ("unacceptable options (%s)\n", options));
2584 errno = EINVAL + 8195;
2588 if (user[0] == (char)0) fstrcpy(user, context->user);
2590 dir = SMB_MALLOC_P(SMBCFILE);
2602 dir->fname = SMB_STRDUP(fname);
2606 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2608 if (server[0] == (char)0) {
2613 struct ip_service *ip_list;
2614 struct ip_service server_addr;
2615 struct user_auth_info u_info;
2616 struct cli_state *cli;
2618 if (share[0] != (char)0 || path[0] != (char)0) {
2620 errno = EINVAL + 8196;
2622 SAFE_FREE(dir->fname);
2628 /* Determine how many local master browsers to query */
2629 max_lmb_count = (context->options.browse_max_lmb_count == 0
2631 : context->options.browse_max_lmb_count);
2633 pstrcpy(u_info.username, user);
2634 pstrcpy(u_info.password, password);
2637 * We have server and share and path empty but options
2638 * requesting that we scan all master browsers for their list
2639 * of workgroups/domains. This implies that we must first try
2640 * broadcast queries to find all master browsers, and if that
2641 * doesn't work, then try our other methods which return only
2642 * a single master browser.
2646 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
2652 if (!find_master_ip(workgroup, &server_addr.ip)) {
2655 SAFE_FREE(dir->fname);
2662 ip_list = &server_addr;
2666 for (i = 0; i < count && i < max_lmb_count; i++) {
2667 DEBUG(99, ("Found master browser %d of %d: %s\n",
2668 i+1, MAX(count, max_lmb_count),
2669 inet_ntoa(ip_list[i].ip)));
2671 cli = get_ipc_connect_master_ip(&ip_list[i],
2672 workgroup, &u_info);
2673 /* cli == NULL is the master browser refused to talk or
2674 could not be found */
2678 fstrcpy(server, cli->desthost);
2681 DEBUG(4, ("using workgroup %s %s\n",
2682 workgroup, server));
2685 * For each returned master browser IP address, get a
2686 * connection to IPC$ on the server if we do not
2687 * already have one, and determine the
2688 * workgroups/domains that it knows about.
2691 srv = smbc_server(context, True, server, "IPC$",
2692 workgroup, user, password);
2698 dir->dir_type = SMBC_WORKGROUP;
2700 /* Now, list the stuff ... */
2702 if (!cli_NetServerEnum(srv->cli,
2704 SV_TYPE_DOMAIN_ENUM,
2714 * Server not an empty string ... Check the rest and see what
2717 if (*share == '\0') {
2718 if (*path != '\0') {
2720 /* Should not have empty share with path */
2721 errno = EINVAL + 8197;
2723 SAFE_FREE(dir->fname);
2731 * We don't know if <server> is really a server name
2732 * or is a workgroup/domain name. If we already have
2733 * a server structure for it, we'll use it.
2734 * Otherwise, check to see if <server><1D>,
2735 * <server><1B>, or <server><20> translates. We check
2736 * to see if <server> is an IP address first.
2740 * See if we have an existing server. Do not
2741 * establish a connection if one does not already
2744 srv = smbc_server(context, False, server, "IPC$",
2745 workgroup, user, password);
2748 * If no existing server and not an IP addr, look for
2752 !is_ipaddress(server) &&
2753 (resolve_name(server, &rem_ip, 0x1d) || /* LMB */
2754 resolve_name(server, &rem_ip, 0x1b) )) { /* DMB */
2758 dir->dir_type = SMBC_SERVER;
2761 * Get the backup list ...
2763 if (!name_status_find(server, 0, 0,
2764 rem_ip, buserver)) {
2766 DEBUG(0, ("Could not get name of "
2767 "local/domain master browser "
2768 "for server %s\n", server));
2770 SAFE_FREE(dir->fname);
2779 * Get a connection to IPC$ on the server if
2780 * we do not already have one
2782 srv = smbc_server(context, True,
2784 workgroup, user, password);
2786 DEBUG(0, ("got no contact to IPC$\n"));
2788 SAFE_FREE(dir->fname);
2797 /* Now, list the servers ... */
2798 if (!cli_NetServerEnum(srv->cli, server,
2799 0x0000FFFE, list_fn,
2803 SAFE_FREE(dir->fname);
2809 (resolve_name(server, &rem_ip, 0x20))) {
2811 /* If we hadn't found the server, get one now */
2813 srv = smbc_server(context, True,
2821 SAFE_FREE(dir->fname);
2828 dir->dir_type = SMBC_FILE_SHARE;
2831 /* List the shares ... */
2833 if (net_share_enum_rpc(
2836 (void *) dir) < 0 &&
2842 errno = cli_errno(srv->cli);
2844 SAFE_FREE(dir->fname);
2851 /* Neither the workgroup nor server exists */
2852 errno = ECONNREFUSED;
2854 SAFE_FREE(dir->fname);
2863 * The server and share are specified ... work from
2867 struct cli_state *targetcli;
2869 /* We connect to the server and list the directory */
2870 dir->dir_type = SMBC_FILE_SHARE;
2872 srv = smbc_server(context, True, server, share,
2873 workgroup, user, password);
2878 SAFE_FREE(dir->fname);
2887 /* Now, list the files ... */
2889 p = path + strlen(path);
2890 pstrcat(path, "\\*");
2892 if (!cli_resolve_path("", srv->cli, path,
2893 &targetcli, targetpath))
2895 d_printf("Could not resolve %s\n", path);
2897 SAFE_FREE(dir->fname);
2903 if (cli_list(targetcli, targetpath,
2904 aDIR | aSYSTEM | aHIDDEN,
2905 dir_list_fn, (void *)dir) < 0) {
2908 SAFE_FREE(dir->fname);
2911 saved_errno = smbc_errno(context, targetcli);
2913 if (saved_errno == EINVAL) {
2915 * See if they asked to opendir something
2916 * other than a directory. If so, the
2917 * converted error value we got would have
2918 * been EINVAL rather than ENOTDIR.
2920 *p = '\0'; /* restore original path */
2922 if (smbc_getatr(context, srv, path,
2924 NULL, NULL, NULL, NULL,
2926 ! IS_DOS_DIR(mode)) {
2928 /* It is. Correct the error value */
2929 saved_errno = ENOTDIR;
2934 * If there was an error and the server is no
2937 cb = &context->callbacks;
2938 if (cli_is_error(targetcli) &&
2939 cb->check_server_fn(context, srv)) {
2941 /* ... then remove it. */
2942 if (cb->remove_unused_server_fn(context,
2945 * We could not remove the server
2946 * completely, remove it from the
2947 * cache so we will not get it
2948 * again. It will be removed when the
2949 * last file/dir is closed.
2951 cb->remove_cached_srv_fn(context, srv);
2955 errno = saved_errno;
2962 DLIST_ADD(context->internal->_files, dir);
2968 * Routine to close a directory
2972 smbc_closedir_ctx(SMBCCTX *context,
2976 if (!context || !context->internal ||
2977 !context->internal->_initialized) {
2984 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2991 smbc_remove_dir(dir); /* Clean it up */
2993 DLIST_REMOVE(context->internal->_files, dir);
2997 SAFE_FREE(dir->fname);
2998 SAFE_FREE(dir); /* Free the space too */
3006 smbc_readdir_internal(SMBCCTX * context,
3007 struct smbc_dirent *dest,
3008 struct smbc_dirent *src,
3009 int max_namebuf_len)
3011 if (context->options.urlencode_readdir_entries) {
3013 /* url-encode the name. get back remaining buffer space */
3015 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3017 /* We now know the name length */
3018 dest->namelen = strlen(dest->name);
3020 /* Save the pointer to the beginning of the comment */
3021 dest->comment = dest->name + dest->namelen + 1;
3023 /* Copy the comment */
3024 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3025 dest->comment[max_namebuf_len - 1] = '\0';
3027 /* Save other fields */
3028 dest->smbc_type = src->smbc_type;
3029 dest->commentlen = strlen(dest->comment);
3030 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3034 /* No encoding. Just copy the entry as is. */
3035 memcpy(dest, src, src->dirlen);
3036 dest->comment = (char *)(&dest->name + src->namelen + 1);
3042 * Routine to get a directory entry
3045 struct smbc_dirent *
3046 smbc_readdir_ctx(SMBCCTX *context,
3050 struct smbc_dirent *dirp, *dirent;
3052 /* Check that all is ok first ... */
3054 if (!context || !context->internal ||
3055 !context->internal->_initialized) {
3058 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3063 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3066 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3071 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3074 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3079 if (!dir->dir_next) {
3083 dirent = dir->dir_next->dirent;
3091 dirp = (struct smbc_dirent *)context->internal->_dirent;
3092 maxlen = (sizeof(context->internal->_dirent) -
3093 sizeof(struct smbc_dirent));
3095 smbc_readdir_internal(context, dirp, dirent, maxlen);
3097 dir->dir_next = dir->dir_next->next;
3103 * Routine to get directory entries
3107 smbc_getdents_ctx(SMBCCTX *context,
3109 struct smbc_dirent *dirp,
3115 char *ndir = (char *)dirp;
3116 struct smbc_dir_list *dirlist;
3118 /* Check that all is ok first ... */
3120 if (!context || !context->internal ||
3121 !context->internal->_initialized) {
3128 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3135 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3143 * Now, retrieve the number of entries that will fit in what was passed
3144 * We have to figure out if the info is in the list, or we need to
3145 * send a request to the server to get the info.
3148 while ((dirlist = dir->dir_next)) {
3149 struct smbc_dirent *dirent;
3151 if (!dirlist->dirent) {
3153 errno = ENOENT; /* Bad error */
3158 /* Do urlencoding of next entry, if so selected */
3159 dirent = (struct smbc_dirent *)context->internal->_dirent;
3160 maxlen = (sizeof(context->internal->_dirent) -
3161 sizeof(struct smbc_dirent));
3162 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3164 reqd = dirent->dirlen;
3168 if (rem < count) { /* We managed to copy something */
3174 else { /* Nothing copied ... */
3176 errno = EINVAL; /* Not enough space ... */
3183 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3185 ((struct smbc_dirent *)ndir)->comment =
3186 (char *)(&((struct smbc_dirent *)ndir)->name +
3194 dir->dir_next = dirlist = dirlist -> next;
3205 * Routine to create a directory ...
3209 smbc_mkdir_ctx(SMBCCTX *context,
3219 pstring path, targetpath;
3220 struct cli_state *targetcli;
3222 if (!context || !context->internal ||
3223 !context->internal->_initialized) {
3237 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3239 if (smbc_parse_path(context, fname,
3240 workgroup, sizeof(workgroup),
3241 server, sizeof(server),
3242 share, sizeof(share),
3245 password, sizeof(password),
3251 if (user[0] == (char)0) fstrcpy(user, context->user);
3253 srv = smbc_server(context, True,
3254 server, share, workgroup, user, password);
3258 return -1; /* errno set by smbc_server */
3262 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3263 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3265 d_printf("Could not resolve %s\n", path);
3268 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3270 if (!cli_mkdir(targetcli, targetpath)) {
3272 errno = smbc_errno(context, targetcli);
3282 * Our list function simply checks to see if a directory is not empty
3285 static int smbc_rmdir_dirempty = True;
3288 rmdir_list_fn(const char *mnt,
3293 if (strncmp(finfo->name, ".", 1) != 0 &&
3294 strncmp(finfo->name, "..", 2) != 0) {
3296 smbc_rmdir_dirempty = False;
3301 * Routine to remove a directory
3305 smbc_rmdir_ctx(SMBCCTX *context,
3316 struct cli_state *targetcli;
3318 if (!context || !context->internal ||
3319 !context->internal->_initialized) {
3333 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3335 if (smbc_parse_path(context, fname,
3336 workgroup, sizeof(workgroup),
3337 server, sizeof(server),
3338 share, sizeof(share),
3341 password, sizeof(password),
3348 if (user[0] == (char)0) fstrcpy(user, context->user);
3350 srv = smbc_server(context, True,
3351 server, share, workgroup, user, password);
3355 return -1; /* errno set by smbc_server */
3359 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3360 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3362 d_printf("Could not resolve %s\n", path);
3365 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3368 if (!cli_rmdir(targetcli, targetpath)) {
3370 errno = smbc_errno(context, targetcli);
3372 if (errno == EACCES) { /* Check if the dir empty or not */
3374 /* Local storage to avoid buffer overflows */
3377 smbc_rmdir_dirempty = True; /* Make this so ... */
3379 pstrcpy(lpath, targetpath);
3380 pstrcat(lpath, "\\*");
3382 if (cli_list(targetcli, lpath,
3383 aDIR | aSYSTEM | aHIDDEN,
3384 rmdir_list_fn, NULL) < 0) {
3386 /* Fix errno to ignore latest error ... */
3387 DEBUG(5, ("smbc_rmdir: "
3388 "cli_list returned an error: %d\n",
3389 smbc_errno(context, targetcli)));
3394 if (smbc_rmdir_dirempty)
3410 * Routine to return the current directory position
3414 smbc_telldir_ctx(SMBCCTX *context,
3417 if (!context || !context->internal ||
3418 !context->internal->_initialized) {
3425 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3432 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3439 /* See if we're already at the end. */
3440 if (dir->dir_next == NULL) {
3446 * We return the pointer here as the offset
3448 return (off_t)(long)dir->dir_next->dirent;
3452 * A routine to run down the list and see if the entry is OK
3455 struct smbc_dir_list *
3456 smbc_check_dir_ent(struct smbc_dir_list *list,
3457 struct smbc_dirent *dirent)
3460 /* Run down the list looking for what we want */
3464 struct smbc_dir_list *tmp = list;
3468 if (tmp->dirent == dirent)
3477 return NULL; /* Not found, or an error */
3483 * Routine to seek on a directory
3487 smbc_lseekdir_ctx(SMBCCTX *context,
3491 long int l_offset = offset; /* Handle problems of size */
3492 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3493 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3495 if (!context || !context->internal ||
3496 !context->internal->_initialized) {
3503 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3510 /* Now, check what we were passed and see if it is OK ... */
3512 if (dirent == NULL) { /* Seek to the begining of the list */
3514 dir->dir_next = dir->dir_list;
3519 if (offset == -1) { /* Seek to the end of the list */
3520 dir->dir_next = NULL;
3524 /* Now, run down the list and make sure that the entry is OK */
3525 /* This may need to be changed if we change the format of the list */
3527 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3529 errno = EINVAL; /* Bad entry */
3534 dir->dir_next = list_ent;
3541 * Routine to fstat a dir
3545 smbc_fstatdir_ctx(SMBCCTX *context,
3550 if (!context || !context->internal ||
3551 !context->internal->_initialized) {
3558 /* No code yet ... */
3565 smbc_chmod_ctx(SMBCCTX *context,
3578 if (!context || !context->internal ||
3579 !context->internal->_initialized) {
3581 errno = EINVAL; /* Best I can think of ... */
3593 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3595 if (smbc_parse_path(context, fname,
3596 workgroup, sizeof(workgroup),
3597 server, sizeof(server),
3598 share, sizeof(share),
3601 password, sizeof(password),
3607 if (user[0] == (char)0) fstrcpy(user, context->user);
3609 srv = smbc_server(context, True,
3610 server, share, workgroup, user, password);
3613 return -1; /* errno set by smbc_server */
3618 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3619 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3620 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3621 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3623 if (!cli_setatr(srv->cli, path, mode, 0)) {
3624 errno = smbc_errno(context, srv->cli);
3632 smbc_utimes_ctx(SMBCCTX *context,
3634 struct timeval *tbuf)
3646 if (!context || !context->internal ||
3647 !context->internal->_initialized) {
3649 errno = EINVAL; /* Best I can think of ... */
3662 access_time = write_time = time(NULL);
3664 access_time = tbuf[0].tv_sec;
3665 write_time = tbuf[1].tv_sec;
3674 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3675 atimebuf[sizeof(atimebuf) - 1] = '\0';
3676 if ((p = strchr(atimebuf, '\n')) != NULL) {
3680 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3681 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3682 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3686 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3687 fname, atimebuf, mtimebuf);
3690 if (smbc_parse_path(context, fname,
3691 workgroup, sizeof(workgroup),
3692 server, sizeof(server),
3693 share, sizeof(share),
3696 password, sizeof(password),
3702 if (user[0] == (char)0) fstrcpy(user, context->user);
3704 srv = smbc_server(context, True,
3705 server, share, workgroup, user, password);
3708 return -1; /* errno set by smbc_server */
3711 if (!smbc_setatr(context, srv, path,
3712 0, access_time, write_time, 0, 0)) {
3713 return -1; /* errno set by smbc_setatr */
3721 * Sort ACEs according to the documentation at
3722 * http://support.microsoft.com/kb/269175, at least as far as it defines the
3727 ace_compare(SEC_ACE *ace1,
3733 /* If the ACEs are equal, we have nothing more to do. */
3734 if (sec_ace_equal(ace1, ace2)) {
3738 /* Inherited follow non-inherited */
3739 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
3740 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
3742 return (b1 ? 1 : -1);
3746 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
3747 * sort them after DENY and ALLOW.
3749 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
3750 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
3751 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
3752 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3753 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
3754 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
3755 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
3756 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3758 return (b1 ? 1 : -1);
3761 /* Allowed ACEs follow denied ACEs */
3762 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
3763 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
3764 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
3765 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
3767 return (b1 ? 1 : -1);
3771 * ACEs applying to an entity's object follow those applying to the
3774 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
3775 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3776 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
3777 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
3779 return (b1 ? 1 : -1);
3783 * If we get this far, the ACEs are similar as far as the
3784 * characteristics we typically care about (those defined by the
3785 * referenced MS document). We'll now sort by characteristics that
3786 * just seems reasonable.
3789 if (ace1->type != ace2->type) {
3790 return ace2->type - ace1->type;
3793 if (sid_compare(&ace1->trustee, &ace2->trustee)) {
3794 return sid_compare(&ace1->trustee, &ace2->trustee);
3797 if (ace1->flags != ace2->flags) {
3798 return ace1->flags - ace2->flags;
3801 if (ace1->access_mask != ace2->access_mask) {
3802 return ace1->access_mask - ace2->access_mask;
3805 if (ace1->size != ace2->size) {
3806 return ace1->size - ace2->size;
3809 return memcmp(ace1, ace2, sizeof(SEC_ACE));
3814 sort_acl(SEC_ACL *the_acl)
3817 if (!the_acl) return;
3819 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
3820 QSORT_CAST ace_compare);
3822 for (i=1;i<the_acl->num_aces;) {
3823 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
3825 for (j=i; j<the_acl->num_aces-1; j++) {
3826 the_acl->aces[j] = the_acl->aces[j+1];
3828 the_acl->num_aces--;
3835 /* convert a SID to a string, either numeric or username/group */
3837 convert_sid_to_string(struct cli_state *ipc_cli,
3843 char **domains = NULL;
3844 char **names = NULL;
3845 enum lsa_SidType *types = NULL;
3846 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3847 sid_to_string(str, sid);
3850 return; /* no lookup desired */
3857 /* Ask LSA to convert the sid to a name */
3859 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ipc_cli->mem_ctx,
3860 pol, 1, sid, &domains,
3862 !domains || !domains[0] || !names || !names[0]) {
3868 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3869 domains[0], lp_winbind_separator(),
3873 /* convert a string to a SID, either numeric or username/group */
3875 convert_string_to_sid(struct cli_state *ipc_cli,
3881 enum lsa_SidType *types = NULL;
3882 DOM_SID *sids = NULL;
3884 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3891 if (strncmp(str, "S-", 2) == 0) {
3892 return string_to_sid(sid, str);
3899 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx,
3900 pol, 1, &str, NULL, 1, &sids,
3906 sid_copy(sid, &sids[0]);
3913 /* parse an ACE in the same format as print_ace() */
3915 parse_ace(struct cli_state *ipc_cli,
3925 unsigned int aflags;
3929 const struct perm_value *v;
3935 /* These values discovered by inspection */
3936 static const struct perm_value special_values[] = {
3937 { "R", 0x00120089 },
3938 { "W", 0x00120116 },
3939 { "X", 0x001200a0 },
3940 { "D", 0x00010000 },
3941 { "P", 0x00040000 },
3942 { "O", 0x00080000 },
3946 static const struct perm_value standard_values[] = {
3947 { "READ", 0x001200a9 },
3948 { "CHANGE", 0x001301bf },
3949 { "FULL", 0x001f01ff },
3955 p = strchr_m(str,':');
3956 if (!p) return False;
3959 /* Try to parse numeric form */
3961 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3962 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3966 /* Try to parse text form */
3968 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3973 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3977 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3978 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3979 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3980 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3985 /* Only numeric form accepted for flags at present */
3987 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3988 sscanf(tok, "%i", &aflags))) {
3992 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3996 if (strncmp(tok, "0x", 2) == 0) {
3997 if (sscanf(tok, "%i", &amask) != 1) {
4003 for (v = standard_values; v->perm; v++) {
4004 if (strcmp(tok, v->perm) == 0) {
4015 for (v = special_values; v->perm; v++) {
4016 if (v->perm[0] == *p) {
4022 if (!found) return False;
4032 init_sec_ace(ace, &sid, atype, mask, aflags);
4036 /* add an ACE to a list of ACEs in a SEC_ACL */
4038 add_ace(SEC_ACL **the_acl,
4046 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
4050 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4053 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4054 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4055 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4056 1+(*the_acl)->num_aces, aces);
4058 (*the_acl) = newacl;
4063 /* parse a ascii version of a security descriptor */
4065 sec_desc_parse(TALLOC_CTX *ctx,
4066 struct cli_state *ipc_cli,
4071 const char *p = str;
4073 SEC_DESC *ret = NULL;
4075 DOM_SID *grp_sid=NULL;
4076 DOM_SID *owner_sid=NULL;
4080 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4082 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4083 revision = strtol(tok+9, NULL, 16);
4087 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4089 DEBUG(5, ("OWNER specified more than once!\n"));
4092 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4094 !convert_string_to_sid(ipc_cli, pol,
4096 owner_sid, tok+6)) {
4097 DEBUG(5, ("Failed to parse owner sid\n"));
4103 if (StrnCaseCmp(tok,"OWNER+:", 7) == 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+7)) {
4113 DEBUG(5, ("Failed to parse owner sid\n"));
4119 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4121 DEBUG(5, ("GROUP specified more than once!\n"));
4124 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4126 !convert_string_to_sid(ipc_cli, pol,
4129 DEBUG(5, ("Failed to parse group sid\n"));
4135 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4137 DEBUG(5, ("GROUP specified more than once!\n"));
4140 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4142 !convert_string_to_sid(ipc_cli, pol,
4145 DEBUG(5, ("Failed to parse group sid\n"));
4151 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4153 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4154 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4157 if(!add_ace(&dacl, &ace, ctx)) {
4158 DEBUG(5, ("Failed to add ACL %s\n", tok));
4164 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4166 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4167 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4170 if(!add_ace(&dacl, &ace, ctx)) {
4171 DEBUG(5, ("Failed to add ACL %s\n", tok));
4177 DEBUG(5, ("Failed to parse security descriptor\n"));
4181 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4182 owner_sid, grp_sid, NULL, dacl, &sd_size);
4186 SAFE_FREE(owner_sid);
4192 /* Obtain the current dos attributes */
4193 static DOS_ATTR_DESC *
4194 dos_attr_query(SMBCCTX *context,
4196 const char *filename,
4199 struct timespec create_time_ts;
4200 struct timespec write_time_ts;
4201 struct timespec access_time_ts;
4202 struct timespec change_time_ts;
4205 SMB_INO_T inode = 0;
4208 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4214 /* Obtain the DOS attributes */
4215 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4223 errno = smbc_errno(context, srv->cli);
4224 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4231 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4232 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4233 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4234 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4241 /* parse a ascii version of a security descriptor */
4243 dos_attr_parse(SMBCCTX *context,
4249 const char *p = str;
4252 const char * create_time_attr;
4253 const char * access_time_attr;
4254 const char * write_time_attr;
4255 const char * change_time_attr;
4258 /* Determine whether to use old-style or new-style attribute names */
4259 if (context->internal->_full_time_names) {
4260 /* new-style names */
4261 attr_strings.create_time_attr = "CREATE_TIME";
4262 attr_strings.access_time_attr = "ACCESS_TIME";
4263 attr_strings.write_time_attr = "WRITE_TIME";
4264 attr_strings.change_time_attr = "CHANGE_TIME";
4266 /* old-style names */
4267 attr_strings.create_time_attr = NULL;
4268 attr_strings.access_time_attr = "A_TIME";
4269 attr_strings.write_time_attr = "M_TIME";
4270 attr_strings.change_time_attr = "C_TIME";
4273 /* if this is to set the entire ACL... */
4275 /* ... then increment past the first colon if there is one */
4276 if ((p = strchr(str, ':')) != NULL) {
4283 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4285 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4286 dad->mode = strtol(tok+5, NULL, 16);
4290 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4291 dad->size = (SMB_OFF_T)atof(tok+5);
4295 n = strlen(attr_strings.access_time_attr);
4296 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4297 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4301 n = strlen(attr_strings.change_time_attr);
4302 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4303 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4307 n = strlen(attr_strings.write_time_attr);
4308 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4309 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4313 if (attr_strings.create_time_attr != NULL) {
4314 n = strlen(attr_strings.create_time_attr);
4315 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4317 dad->create_time = (time_t)strtol(tok+n+1,
4323 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4324 dad->inode = (SMB_INO_T)atof(tok+6);
4330 /*****************************************************
4331 Retrieve the acls for a file.
4332 *******************************************************/
4335 cacl_get(SMBCCTX *context,
4338 struct cli_state *ipc_cli,
4354 BOOL exclude_nt_revision = False;
4355 BOOL exclude_nt_owner = False;
4356 BOOL exclude_nt_group = False;
4357 BOOL exclude_nt_acl = False;
4358 BOOL exclude_dos_mode = False;
4359 BOOL exclude_dos_size = False;
4360 BOOL exclude_dos_create_time = False;
4361 BOOL exclude_dos_access_time = False;
4362 BOOL exclude_dos_write_time = False;
4363 BOOL exclude_dos_change_time = False;
4364 BOOL exclude_dos_inode = False;
4365 BOOL numeric = True;
4366 BOOL determine_size = (bufsize == 0);
4370 fstring name_sandbox;
4374 struct timespec create_time_ts;
4375 struct timespec write_time_ts;
4376 struct timespec access_time_ts;
4377 struct timespec change_time_ts;
4378 time_t create_time = (time_t)0;
4379 time_t write_time = (time_t)0;
4380 time_t access_time = (time_t)0;
4381 time_t change_time = (time_t)0;
4385 struct cli_state *cli = srv->cli;
4387 const char * create_time_attr;
4388 const char * access_time_attr;
4389 const char * write_time_attr;
4390 const char * change_time_attr;
4393 const char * create_time_attr;
4394 const char * access_time_attr;
4395 const char * write_time_attr;
4396 const char * change_time_attr;
4397 } excl_attr_strings;
4399 /* Determine whether to use old-style or new-style attribute names */
4400 if (context->internal->_full_time_names) {
4401 /* new-style names */
4402 attr_strings.create_time_attr = "CREATE_TIME";
4403 attr_strings.access_time_attr = "ACCESS_TIME";
4404 attr_strings.write_time_attr = "WRITE_TIME";
4405 attr_strings.change_time_attr = "CHANGE_TIME";
4407 excl_attr_strings.create_time_attr = "CREATE_TIME";
4408 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4409 excl_attr_strings.write_time_attr = "WRITE_TIME";
4410 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4412 /* old-style names */
4413 attr_strings.create_time_attr = NULL;
4414 attr_strings.access_time_attr = "A_TIME";
4415 attr_strings.write_time_attr = "M_TIME";
4416 attr_strings.change_time_attr = "C_TIME";
4418 excl_attr_strings.create_time_attr = NULL;
4419 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4420 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4421 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4424 /* Copy name so we can strip off exclusions (if any are specified) */
4425 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4427 /* Ensure name is null terminated */
4428 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4430 /* Play in the sandbox */
4431 name = name_sandbox;
4433 /* If there are any exclusions, point to them and mask them from name */
4434 if ((pExclude = strchr(name, '!')) != NULL)
4439 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4440 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4441 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4442 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4443 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4444 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4445 numeric = (* (name + strlen(name) - 1) != '+');
4447 /* Look for exclusions from "all" requests */
4448 if (all || all_nt || all_dos) {
4450 /* Exclusions are delimited by '!' */
4453 pExclude = (p == NULL ? NULL : p + 1)) {
4455 /* Find end of this exclusion name */
4456 if ((p = strchr(pExclude, '!')) != NULL)
4461 /* Which exclusion name is this? */
4462 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4463 exclude_nt_revision = True;
4465 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4466 exclude_nt_owner = True;
4468 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4469 exclude_nt_group = True;
4471 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4472 exclude_nt_acl = True;
4474 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4475 exclude_dos_mode = True;
4477 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4478 exclude_dos_size = True;
4480 else if (excl_attr_strings.create_time_attr != NULL &&
4481 StrCaseCmp(pExclude,
4482 excl_attr_strings.change_time_attr) == 0) {
4483 exclude_dos_create_time = True;
4485 else if (StrCaseCmp(pExclude,
4486 excl_attr_strings.access_time_attr) == 0) {
4487 exclude_dos_access_time = True;
4489 else if (StrCaseCmp(pExclude,
4490 excl_attr_strings.write_time_attr) == 0) {
4491 exclude_dos_write_time = True;
4493 else if (StrCaseCmp(pExclude,
4494 excl_attr_strings.change_time_attr) == 0) {
4495 exclude_dos_change_time = True;
4497 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4498 exclude_dos_inode = True;
4501 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4512 * If we are (possibly) talking to an NT or new system and some NT
4513 * attributes have been requested...
4515 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4516 /* Point to the portion after "system.nt_sec_desc." */
4517 name += 19; /* if (all) this will be invalid but unused */
4519 /* ... then obtain any NT attributes which were requested */
4520 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4523 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4524 filename, cli_errstr(cli)));
4529 sd = cli_query_secdesc(cli, fnum, ctx);
4533 ("cacl_get Failed to query old descriptor\n"));
4538 cli_close(cli, fnum);
4540 if (! exclude_nt_revision) {
4541 if (all || all_nt) {
4542 if (determine_size) {
4543 p = talloc_asprintf(ctx,
4552 n = snprintf(buf, bufsize,
4556 } else if (StrCaseCmp(name, "revision") == 0) {
4557 if (determine_size) {
4558 p = talloc_asprintf(ctx, "%d",
4566 n = snprintf(buf, bufsize, "%d",
4571 if (!determine_size && n > bufsize) {
4581 if (! exclude_nt_owner) {
4582 /* Get owner and group sid */
4583 if (sd->owner_sid) {
4584 convert_sid_to_string(ipc_cli, pol,
4589 fstrcpy(sidstr, "");
4592 if (all || all_nt) {
4593 if (determine_size) {
4594 p = talloc_asprintf(ctx, ",OWNER:%s",
4601 } else if (sidstr[0] != '\0') {
4602 n = snprintf(buf, bufsize,
4603 ",OWNER:%s", sidstr);
4605 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4606 if (determine_size) {
4607 p = talloc_asprintf(ctx, "%s", sidstr);
4614 n = snprintf(buf, bufsize, "%s",
4619 if (!determine_size && n > bufsize) {
4629 if (! exclude_nt_group) {
4630 if (sd->group_sid) {
4631 convert_sid_to_string(ipc_cli, pol,
4635 fstrcpy(sidstr, "");
4638 if (all || all_nt) {
4639 if (determine_size) {
4640 p = talloc_asprintf(ctx, ",GROUP:%s",
4647 } else if (sidstr[0] != '\0') {
4648 n = snprintf(buf, bufsize,
4649 ",GROUP:%s", sidstr);
4651 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4652 if (determine_size) {
4653 p = talloc_asprintf(ctx, "%s", sidstr);
4660 n = snprintf(buf, bufsize,
4665 if (!determine_size && n > bufsize) {
4675 if (! exclude_nt_acl) {
4676 /* Add aces to value buffer */
4677 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4679 SEC_ACE *ace = &sd->dacl->aces[i];
4680 convert_sid_to_string(ipc_cli, pol,
4684 if (all || all_nt) {
4685 if (determine_size) {
4686 p = talloc_asprintf(
4702 ",ACL:%s:%d/%d/0x%08x",
4708 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4709 StrCaseCmp(name+3, sidstr) == 0) ||
4710 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4711 StrCaseCmp(name+4, sidstr) == 0)) {
4712 if (determine_size) {
4713 p = talloc_asprintf(
4725 n = snprintf(buf, bufsize,
4731 } else if (all_nt_acls) {
4732 if (determine_size) {
4733 p = talloc_asprintf(
4735 "%s%s:%d/%d/0x%08x",
4747 n = snprintf(buf, bufsize,
4748 "%s%s:%d/%d/0x%08x",
4756 if (!determine_size && n > bufsize) {
4767 /* Restore name pointer to its original value */
4771 if (all || some_dos) {
4772 /* Point to the portion after "system.dos_attr." */
4773 name += 16; /* if (all) this will be invalid but unused */
4775 /* Obtain the DOS attributes */
4776 if (!smbc_getatr(context, srv, filename, &mode, &size,
4783 errno = smbc_errno(context, srv->cli);
4788 create_time = convert_timespec_to_time_t(create_time_ts);
4789 access_time = convert_timespec_to_time_t(access_time_ts);
4790 write_time = convert_timespec_to_time_t(write_time_ts);
4791 change_time = convert_timespec_to_time_t(change_time_ts);
4793 if (! exclude_dos_mode) {
4794 if (all || all_dos) {
4795 if (determine_size) {
4796 p = talloc_asprintf(ctx,
4809 n = snprintf(buf, bufsize,
4817 } else if (StrCaseCmp(name, "mode") == 0) {
4818 if (determine_size) {
4819 p = talloc_asprintf(ctx, "0x%x", mode);
4826 n = snprintf(buf, bufsize,
4831 if (!determine_size && n > bufsize) {
4841 if (! exclude_dos_size) {
4842 if (all || all_dos) {
4843 if (determine_size) {
4844 p = talloc_asprintf(
4854 n = snprintf(buf, bufsize,
4858 } else if (StrCaseCmp(name, "size") == 0) {
4859 if (determine_size) {
4860 p = talloc_asprintf(
4870 n = snprintf(buf, bufsize,
4876 if (!determine_size && n > bufsize) {
4886 if (! exclude_dos_create_time &&
4887 attr_strings.create_time_attr != NULL) {
4888 if (all || all_dos) {
4889 if (determine_size) {
4890 p = talloc_asprintf(ctx,
4892 attr_strings.create_time_attr,
4900 n = snprintf(buf, bufsize,
4902 attr_strings.create_time_attr,
4905 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
4906 if (determine_size) {
4907 p = talloc_asprintf(ctx, "%lu", create_time);
4914 n = snprintf(buf, bufsize,
4915 "%lu", create_time);
4919 if (!determine_size && n > bufsize) {
4929 if (! exclude_dos_access_time) {
4930 if (all || all_dos) {
4931 if (determine_size) {
4932 p = talloc_asprintf(ctx,
4934 attr_strings.access_time_attr,
4942 n = snprintf(buf, bufsize,
4944 attr_strings.access_time_attr,
4947 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
4948 if (determine_size) {
4949 p = talloc_asprintf(ctx, "%lu", access_time);
4956 n = snprintf(buf, bufsize,
4957 "%lu", access_time);
4961 if (!determine_size && n > bufsize) {
4971 if (! exclude_dos_write_time) {
4972 if (all || all_dos) {
4973 if (determine_size) {
4974 p = talloc_asprintf(ctx,
4976 attr_strings.write_time_attr,
4984 n = snprintf(buf, bufsize,
4986 attr_strings.write_time_attr,
4989 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
4990 if (determine_size) {
4991 p = talloc_asprintf(ctx, "%lu", write_time);
4998 n = snprintf(buf, bufsize,
5003 if (!determine_size && n > bufsize) {
5013 if (! exclude_dos_change_time) {
5014 if (all || all_dos) {
5015 if (determine_size) {
5016 p = talloc_asprintf(ctx,
5018 attr_strings.change_time_attr,
5026 n = snprintf(buf, bufsize,
5028 attr_strings.change_time_attr,
5031 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5032 if (determine_size) {
5033 p = talloc_asprintf(ctx, "%lu", change_time);
5040 n = snprintf(buf, bufsize,
5041 "%lu", change_time);
5045 if (!determine_size && n > bufsize) {
5055 if (! exclude_dos_inode) {
5056 if (all || all_dos) {
5057 if (determine_size) {
5058 p = talloc_asprintf(
5068 n = snprintf(buf, bufsize,
5072 } else if (StrCaseCmp(name, "inode") == 0) {
5073 if (determine_size) {
5074 p = talloc_asprintf(
5084 n = snprintf(buf, bufsize,
5090 if (!determine_size && n > bufsize) {
5100 /* Restore name pointer to its original value */
5113 /*****************************************************
5114 set the ACLs on a file given an ascii description
5115 *******************************************************/
5117 cacl_set(TALLOC_CTX *ctx,
5118 struct cli_state *cli,
5119 struct cli_state *ipc_cli,
5121 const char *filename,
5122 const char *the_acl,
5128 SEC_DESC *sd = NULL, *old;
5129 SEC_ACL *dacl = NULL;
5130 DOM_SID *owner_sid = NULL;
5131 DOM_SID *grp_sid = NULL;
5136 BOOL numeric = True;
5138 /* the_acl will be null for REMOVE_ALL operations */
5140 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5144 /* if this is to set the entire ACL... */
5145 if (*the_acl == '*') {
5146 /* ... then increment past the first colon */
5150 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5151 CONST_DISCARD(char *, the_acl));
5159 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5160 that doesn't deref sd */
5162 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5167 /* The desired access below is the only one I could find that works
5168 with NT4, W2KP and Samba */
5170 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5173 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5174 filename, cli_errstr(cli)));
5179 old = cli_query_secdesc(cli, fnum, ctx);
5182 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5187 cli_close(cli, fnum);
5190 case SMBC_XATTR_MODE_REMOVE_ALL:
5191 old->dacl->num_aces = 0;
5195 case SMBC_XATTR_MODE_REMOVE:
5196 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5199 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5200 if (sec_ace_equal(&sd->dacl->aces[i],
5201 &old->dacl->aces[j])) {
5203 for (k=j; k<old->dacl->num_aces-1;k++) {
5204 old->dacl->aces[k] =
5205 old->dacl->aces[k+1];
5207 old->dacl->num_aces--;
5222 case SMBC_XATTR_MODE_ADD:
5223 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5226 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5227 if (sid_equal(&sd->dacl->aces[i].trustee,
5228 &old->dacl->aces[j].trustee)) {
5229 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5234 old->dacl->aces[j] = sd->dacl->aces[i];
5240 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5246 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5247 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5253 case SMBC_XATTR_MODE_SET:
5255 owner_sid = old->owner_sid;
5256 grp_sid = old->group_sid;
5260 case SMBC_XATTR_MODE_CHOWN:
5261 owner_sid = sd->owner_sid;
5264 case SMBC_XATTR_MODE_CHGRP:
5265 grp_sid = sd->group_sid;
5269 /* Denied ACE entries must come before allowed ones */
5270 sort_acl(old->dacl);
5272 /* Create new security descriptor and set it */
5273 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5274 owner_sid, grp_sid, NULL, dacl, &sd_size);
5276 fnum = cli_nt_create(cli, filename,
5277 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5280 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5281 filename, cli_errstr(cli)));
5286 if (!cli_set_secdesc(cli, fnum, sd)) {
5287 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5294 cli_close(cli, fnum);
5305 smbc_setxattr_ctx(SMBCCTX *context,
5326 const char * create_time_attr;
5327 const char * access_time_attr;
5328 const char * write_time_attr;
5329 const char * change_time_attr;
5332 if (!context || !context->internal ||
5333 !context->internal->_initialized) {
5335 errno = EINVAL; /* Best I can think of ... */
5347 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5348 fname, name, (int) size, (const char*)value));
5350 if (smbc_parse_path(context, fname,
5351 workgroup, sizeof(workgroup),
5352 server, sizeof(server),
5353 share, sizeof(share),
5356 password, sizeof(password),
5362 if (user[0] == (char)0) fstrcpy(user, context->user);
5364 srv = smbc_server(context, True,
5365 server, share, workgroup, user, password);
5367 return -1; /* errno set by smbc_server */
5370 if (! srv->no_nt_session) {
5371 ipc_srv = smbc_attr_server(context, server, share,
5372 workgroup, user, password,
5375 srv->no_nt_session = True;
5381 ctx = talloc_init("smbc_setxattr");
5388 * Are they asking to set the entire set of known attributes?
5390 if (StrCaseCmp(name, "system.*") == 0 ||
5391 StrCaseCmp(name, "system.*+") == 0) {
5394 talloc_asprintf(ctx, "%s:%s",
5395 name+7, (const char *) value);
5403 ret = cacl_set(ctx, srv->cli,
5404 ipc_srv->cli, &pol, path,
5407 ? SMBC_XATTR_MODE_SET
5408 : SMBC_XATTR_MODE_ADD),
5414 /* get a DOS Attribute Descriptor with current attributes */
5415 dad = dos_attr_query(context, ctx, path, srv);
5417 /* Overwrite old with new, using what was provided */
5418 dos_attr_parse(context, dad, srv, namevalue);
5420 /* Set the new DOS attributes */
5421 if (! smbc_setatr(context, srv, path,
5428 /* cause failure if NT failed too */
5433 /* we only fail if both NT and DOS sets failed */
5434 if (ret < 0 && ! dad) {
5435 ret = -1; /* in case dad was null */
5441 talloc_destroy(ctx);
5446 * Are they asking to set an access control element or to set
5447 * the entire access control list?
5449 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5450 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5451 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5452 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5453 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5457 talloc_asprintf(ctx, "%s:%s",
5458 name+19, (const char *) value);
5461 ret = -1; /* errno set by smbc_server() */
5463 else if (! namevalue) {
5467 ret = cacl_set(ctx, srv->cli,
5468 ipc_srv->cli, &pol, path,
5471 ? SMBC_XATTR_MODE_SET
5472 : SMBC_XATTR_MODE_ADD),
5475 talloc_destroy(ctx);
5480 * Are they asking to set the owner?
5482 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5483 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5487 talloc_asprintf(ctx, "%s:%s",
5488 name+19, (const char *) value);
5492 ret = -1; /* errno set by smbc_server() */
5494 else if (! namevalue) {
5498 ret = cacl_set(ctx, srv->cli,
5499 ipc_srv->cli, &pol, path,
5500 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5502 talloc_destroy(ctx);
5507 * Are they asking to set the group?
5509 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5510 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5514 talloc_asprintf(ctx, "%s:%s",
5515 name+19, (const char *) value);
5518 /* errno set by smbc_server() */
5521 else if (! namevalue) {
5525 ret = cacl_set(ctx, srv->cli,
5526 ipc_srv->cli, &pol, path,
5527 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5529 talloc_destroy(ctx);
5533 /* Determine whether to use old-style or new-style attribute names */
5534 if (context->internal->_full_time_names) {
5535 /* new-style names */
5536 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5537 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5538 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5539 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5541 /* old-style names */
5542 attr_strings.create_time_attr = NULL;
5543 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5544 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5545 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5549 * Are they asking to set a DOS attribute?
5551 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5552 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5553 (attr_strings.create_time_attr != NULL &&
5554 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5555 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5556 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5557 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5559 /* get a DOS Attribute Descriptor with current attributes */
5560 dad = dos_attr_query(context, ctx, path, srv);
5563 talloc_asprintf(ctx, "%s:%s",
5564 name+16, (const char *) value);
5569 /* Overwrite old with provided new params */
5570 dos_attr_parse(context, dad, srv, namevalue);
5572 /* Set the new DOS attributes */
5573 ret2 = smbc_setatr(context, srv, path,
5580 /* ret2 has True (success) / False (failure) */
5591 talloc_destroy(ctx);
5595 /* Unsupported attribute name */
5596 talloc_destroy(ctx);
5602 smbc_getxattr_ctx(SMBCCTX *context,
5620 const char * create_time_attr;
5621 const char * access_time_attr;
5622 const char * write_time_attr;
5623 const char * change_time_attr;
5627 if (!context || !context->internal ||
5628 !context->internal->_initialized) {
5630 errno = EINVAL; /* Best I can think of ... */
5642 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5644 if (smbc_parse_path(context, fname,
5645 workgroup, sizeof(workgroup),
5646 server, sizeof(server),
5647 share, sizeof(share),
5650 password, sizeof(password),
5656 if (user[0] == (char)0) fstrcpy(user, context->user);
5658 srv = smbc_server(context, True,
5659 server, share, workgroup, user, password);
5661 return -1; /* errno set by smbc_server */
5664 if (! srv->no_nt_session) {
5665 ipc_srv = smbc_attr_server(context, server, share,
5666 workgroup, user, password,
5669 srv->no_nt_session = True;
5675 ctx = talloc_init("smbc:getxattr");
5681 /* Determine whether to use old-style or new-style attribute names */
5682 if (context->internal->_full_time_names) {
5683 /* new-style names */
5684 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5685 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5686 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5687 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5689 /* old-style names */
5690 attr_strings.create_time_attr = NULL;
5691 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5692 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5693 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5696 /* Are they requesting a supported attribute? */
5697 if (StrCaseCmp(name, "system.*") == 0 ||
5698 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5699 StrCaseCmp(name, "system.*+") == 0 ||
5700 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5701 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5702 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5703 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5704 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5705 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5706 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5707 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5708 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5709 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5710 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5711 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5712 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5713 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5714 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5715 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5716 (attr_strings.create_time_attr != NULL &&
5717 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5718 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5719 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5720 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5721 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
5724 ret = cacl_get(context, ctx, srv,
5725 ipc_srv == NULL ? NULL : ipc_srv->cli,
5727 CONST_DISCARD(char *, name),
5728 CONST_DISCARD(char *, value), size);
5729 if (ret < 0 && errno == 0) {
5730 errno = smbc_errno(context, srv->cli);
5732 talloc_destroy(ctx);
5736 /* Unsupported attribute name */
5737 talloc_destroy(ctx);
5744 smbc_removexattr_ctx(SMBCCTX *context,
5760 if (!context || !context->internal ||
5761 !context->internal->_initialized) {
5763 errno = EINVAL; /* Best I can think of ... */
5775 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
5777 if (smbc_parse_path(context, fname,
5778 workgroup, sizeof(workgroup),
5779 server, sizeof(server),
5780 share, sizeof(share),
5783 password, sizeof(password),
5789 if (user[0] == (char)0) fstrcpy(user, context->user);
5791 srv = smbc_server(context, True,
5792 server, share, workgroup, user, password);
5794 return -1; /* errno set by smbc_server */
5797 if (! srv->no_nt_session) {
5798 ipc_srv = smbc_attr_server(context, server, share,
5799 workgroup, user, password,
5802 srv->no_nt_session = True;
5809 return -1; /* errno set by smbc_attr_server */
5812 ctx = talloc_init("smbc_removexattr");
5818 /* Are they asking to set the entire ACL? */
5819 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5820 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
5823 ret = cacl_set(ctx, srv->cli,
5824 ipc_srv->cli, &pol, path,
5825 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
5826 talloc_destroy(ctx);
5831 * Are they asking to remove one or more spceific security descriptor
5834 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5835 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5836 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5837 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5838 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5839 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5840 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5843 ret = cacl_set(ctx, srv->cli,
5844 ipc_srv->cli, &pol, path,
5845 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
5846 talloc_destroy(ctx);
5850 /* Unsupported attribute name */
5851 talloc_destroy(ctx);
5857 smbc_listxattr_ctx(SMBCCTX *context,
5863 * This isn't quite what listxattr() is supposed to do. This returns
5864 * the complete set of attribute names, always, rather than only those
5865 * attribute names which actually exist for a file. Hmmm...
5867 const char supported_old[] =
5870 "system.nt_sec_desc.revision\0"
5871 "system.nt_sec_desc.owner\0"
5872 "system.nt_sec_desc.owner+\0"
5873 "system.nt_sec_desc.group\0"
5874 "system.nt_sec_desc.group+\0"
5875 "system.nt_sec_desc.acl.*\0"
5876 "system.nt_sec_desc.acl\0"
5877 "system.nt_sec_desc.acl+\0"
5878 "system.nt_sec_desc.*\0"
5879 "system.nt_sec_desc.*+\0"
5880 "system.dos_attr.*\0"
5881 "system.dos_attr.mode\0"
5882 "system.dos_attr.c_time\0"
5883 "system.dos_attr.a_time\0"
5884 "system.dos_attr.m_time\0"
5886 const char supported_new[] =
5889 "system.nt_sec_desc.revision\0"
5890 "system.nt_sec_desc.owner\0"
5891 "system.nt_sec_desc.owner+\0"
5892 "system.nt_sec_desc.group\0"
5893 "system.nt_sec_desc.group+\0"
5894 "system.nt_sec_desc.acl.*\0"
5895 "system.nt_sec_desc.acl\0"
5896 "system.nt_sec_desc.acl+\0"
5897 "system.nt_sec_desc.*\0"
5898 "system.nt_sec_desc.*+\0"
5899 "system.dos_attr.*\0"
5900 "system.dos_attr.mode\0"
5901 "system.dos_attr.create_time\0"
5902 "system.dos_attr.access_time\0"
5903 "system.dos_attr.write_time\0"
5904 "system.dos_attr.change_time\0"
5906 const char * supported;
5908 if (context->internal->_full_time_names) {
5909 supported = supported_new;
5911 supported = supported_old;
5915 return sizeof(supported);
5918 if (sizeof(supported) > size) {
5923 /* this can't be strcpy() because there are embedded null characters */
5924 memcpy(list, supported, sizeof(supported));
5925 return sizeof(supported);
5930 * Open a print file to be written to by other calls
5934 smbc_open_print_job_ctx(SMBCCTX *context,
5943 if (!context || !context->internal ||
5944 !context->internal->_initialized) {
5958 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
5960 if (smbc_parse_path(context, fname,
5962 server, sizeof(server),
5963 share, sizeof(share),
5966 password, sizeof(password),
5972 /* What if the path is empty, or the file exists? */
5974 return (context->open)(context, fname, O_WRONLY, 666);
5979 * Routine to print a file on a remote server ...
5981 * We open the file, which we assume to be on a remote server, and then
5982 * copy it to a print file on the share specified by printq.
5986 smbc_print_file_ctx(SMBCCTX *c_file,
5998 if (!c_file || !c_file->internal->_initialized || !c_print ||
5999 !c_print->internal->_initialized) {
6006 if (!fname && !printq) {
6013 /* Try to open the file for reading ... */
6015 if ((long)(fid1 = (c_file->open)(c_file, fname, O_RDONLY, 0666)) < 0) {
6017 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
6018 return -1; /* smbc_open sets errno */
6022 /* Now, try to open the printer file for writing */
6024 if ((long)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
6026 saverr = errno; /* Save errno */
6027 c_file->close_fn(c_file, fid1);
6033 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
6037 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
6040 c_file->close_fn(c_file, fid1);
6041 c_print->close_fn(c_print, fid2);
6050 c_file->close_fn(c_file, fid1); /* We have to close these anyway */
6051 c_print->close_fn(c_print, fid2);
6065 * Routine to list print jobs on a printer share ...
6069 smbc_list_print_jobs_ctx(SMBCCTX *context,
6071 smbc_list_print_job_fn fn)
6081 if (!context || !context->internal ||
6082 !context->internal->_initialized) {
6096 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6098 if (smbc_parse_path(context, fname,
6099 workgroup, sizeof(workgroup),
6100 server, sizeof(server),
6101 share, sizeof(share),
6104 password, sizeof(password),
6110 if (user[0] == (char)0) fstrcpy(user, context->user);
6112 srv = smbc_server(context, True,
6113 server, share, workgroup, user, password);
6117 return -1; /* errno set by smbc_server */
6121 if (cli_print_queue(srv->cli,
6122 (void (*)(struct print_job_info *))fn) < 0) {
6124 errno = smbc_errno(context, srv->cli);
6134 * Delete a print job from a remote printer share
6138 smbc_unlink_print_job_ctx(SMBCCTX *context,
6151 if (!context || !context->internal ||
6152 !context->internal->_initialized) {
6166 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6168 if (smbc_parse_path(context, fname,
6169 workgroup, sizeof(workgroup),
6170 server, sizeof(server),
6171 share, sizeof(share),
6174 password, sizeof(password),
6180 if (user[0] == (char)0) fstrcpy(user, context->user);
6182 srv = smbc_server(context, True,
6183 server, share, workgroup, user, password);
6187 return -1; /* errno set by smbc_server */
6191 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6194 errno = smbc_errno(context, srv->cli);
6195 else if (err == ERRnosuchprintjob)
6206 * Get a new empty handle to fill in with your own info
6209 smbc_new_context(void)
6213 context = SMB_MALLOC_P(SMBCCTX);
6219 ZERO_STRUCTP(context);
6221 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6222 if (!context->internal) {
6228 ZERO_STRUCTP(context->internal);
6231 /* ADD REASONABLE DEFAULTS */
6233 context->timeout = 20000; /* 20 seconds */
6235 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6236 context->options.urlencode_readdir_entries = False;/* backward compat */
6237 context->options.one_share_per_server = False;/* backward compat */
6238 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6239 /* backward compat */
6241 context->open = smbc_open_ctx;
6242 context->creat = smbc_creat_ctx;
6243 context->read = smbc_read_ctx;
6244 context->write = smbc_write_ctx;
6245 context->close_fn = smbc_close_ctx;
6246 context->unlink = smbc_unlink_ctx;
6247 context->rename = smbc_rename_ctx;
6248 context->lseek = smbc_lseek_ctx;
6249 context->stat = smbc_stat_ctx;
6250 context->fstat = smbc_fstat_ctx;
6251 context->opendir = smbc_opendir_ctx;
6252 context->closedir = smbc_closedir_ctx;
6253 context->readdir = smbc_readdir_ctx;
6254 context->getdents = smbc_getdents_ctx;
6255 context->mkdir = smbc_mkdir_ctx;
6256 context->rmdir = smbc_rmdir_ctx;
6257 context->telldir = smbc_telldir_ctx;
6258 context->lseekdir = smbc_lseekdir_ctx;
6259 context->fstatdir = smbc_fstatdir_ctx;
6260 context->chmod = smbc_chmod_ctx;
6261 context->utimes = smbc_utimes_ctx;
6262 context->setxattr = smbc_setxattr_ctx;
6263 context->getxattr = smbc_getxattr_ctx;
6264 context->removexattr = smbc_removexattr_ctx;
6265 context->listxattr = smbc_listxattr_ctx;
6266 context->open_print_job = smbc_open_print_job_ctx;
6267 context->print_file = smbc_print_file_ctx;
6268 context->list_print_jobs = smbc_list_print_jobs_ctx;
6269 context->unlink_print_job = smbc_unlink_print_job_ctx;
6271 context->callbacks.check_server_fn = smbc_check_server;
6272 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6274 smbc_default_cache_functions(context);
6282 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6283 * and thus you'll be leaking memory if not handled properly.
6287 smbc_free_context(SMBCCTX *context,
6297 DEBUG(1,("Performing aggressive shutdown.\n"));
6299 f = context->internal->_files;
6301 context->close_fn(context, f);
6304 context->internal->_files = NULL;
6306 /* First try to remove the servers the nice way. */
6307 if (context->callbacks.purge_cached_fn(context)) {
6310 DEBUG(1, ("Could not purge all servers, "
6311 "Nice way shutdown failed.\n"));
6312 s = context->internal->_servers;
6314 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6316 cli_shutdown(s->cli);
6317 context->callbacks.remove_cached_srv_fn(context,
6320 DLIST_REMOVE(context->internal->_servers, s);
6324 context->internal->_servers = NULL;
6328 /* This is the polite way */
6329 if (context->callbacks.purge_cached_fn(context)) {
6330 DEBUG(1, ("Could not purge all servers, "
6331 "free_context failed.\n"));
6335 if (context->internal->_servers) {
6336 DEBUG(1, ("Active servers in context, "
6337 "free_context failed.\n"));
6341 if (context->internal->_files) {
6342 DEBUG(1, ("Active files in context, "
6343 "free_context failed.\n"));
6349 /* Things we have to clean up */
6350 SAFE_FREE(context->workgroup);
6351 SAFE_FREE(context->netbios_name);
6352 SAFE_FREE(context->user);
6354 DEBUG(3, ("Context %p succesfully freed\n", context));
6355 SAFE_FREE(context->internal);
6362 * Each time the context structure is changed, we have binary backward
6363 * compatibility issues. Instead of modifying the public portions of the
6364 * context structure to add new options, instead, we put them in the internal
6365 * portion of the context structure and provide a set function for these new
6369 smbc_option_set(SMBCCTX *context,
6371 ... /* option_value */)
6377 smbc_get_auth_data_with_context_fn auth_fn;
6381 va_start(ap, option_name);
6383 if (strcmp(option_name, "debug_to_stderr") == 0) {
6385 * Log to standard error instead of standard output.
6387 option_value.b = (BOOL) va_arg(ap, int);
6388 context->internal->_debug_stderr = option_value.b;
6390 } else if (strcmp(option_name, "full_time_names") == 0) {
6392 * Use new-style time attribute names, e.g. WRITE_TIME rather
6393 * than the old-style names such as M_TIME. This allows also
6394 * setting/getting CREATE_TIME which was previously
6395 * unimplemented. (Note that the old C_TIME was supposed to
6396 * be CHANGE_TIME but was confused and sometimes referred to
6399 option_value.b = (BOOL) va_arg(ap, int);
6400 context->internal->_full_time_names = option_value.b;
6402 } else if (strcmp(option_name, "open_share_mode") == 0) {
6404 * The share mode to use for files opened with
6405 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6407 option_value.i = va_arg(ap, int);
6408 context->internal->_share_mode =
6409 (smbc_share_mode) option_value.i;
6411 } else if (strcmp(option_name, "auth_function") == 0) {
6413 * Use the new-style authentication function which includes
6416 option_value.auth_fn =
6417 va_arg(ap, smbc_get_auth_data_with_context_fn);
6418 context->internal->_auth_fn_with_context =
6419 option_value.auth_fn;
6420 } else if (strcmp(option_name, "user_data") == 0) {
6422 * Save a user data handle which may be retrieved by the user
6423 * with smbc_option_get()
6425 option_value.v = va_arg(ap, void *);
6426 context->internal->_user_data = option_value.v;
6434 * Retrieve the current value of an option
6437 smbc_option_get(SMBCCTX *context,
6440 if (strcmp(option_name, "debug_stderr") == 0) {
6442 * Log to standard error instead of standard output.
6444 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6445 return (void *) (intptr_t) context->internal->_debug_stderr;
6447 return (void *) context->internal->_debug_stderr;
6449 } else if (strcmp(option_name, "full_time_names") == 0) {
6451 * Use new-style time attribute names, e.g. WRITE_TIME rather
6452 * than the old-style names such as M_TIME. This allows also
6453 * setting/getting CREATE_TIME which was previously
6454 * unimplemented. (Note that the old C_TIME was supposed to
6455 * be CHANGE_TIME but was confused and sometimes referred to
6458 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6459 return (void *) (intptr_t) context->internal->_full_time_names;
6461 return (void *) context->internal->_full_time_names;
6464 } else if (strcmp(option_name, "auth_function") == 0) {
6466 * Use the new-style authentication function which includes
6469 return (void *) context->internal->_auth_fn_with_context;
6470 } else if (strcmp(option_name, "user_data") == 0) {
6472 * Save a user data handle which may be retrieved by the user
6473 * with smbc_option_get()
6475 return context->internal->_user_data;
6483 * Initialise the library etc
6485 * We accept a struct containing handle information.
6486 * valid values for info->debug from 0 to 100,
6487 * and insist that info->fn must be non-null.
6490 smbc_init_context(SMBCCTX *context)
6497 if (!context || !context->internal) {
6502 /* Do not initialise the same client twice */
6503 if (context->internal->_initialized) {
6507 if ((!context->callbacks.auth_fn &&
6508 !context->internal->_auth_fn_with_context) ||
6509 context->debug < 0 ||
6510 context->debug > 100) {
6517 if (!smbc_initialized) {
6519 * Do some library-wide intializations the first time we get
6522 BOOL conf_loaded = False;
6524 /* Set this to what the user wants */
6525 DEBUGLEVEL = context->debug;
6529 setup_logging("libsmbclient", True);
6530 if (context->internal->_debug_stderr) {
6532 x_setbuf(x_stderr, NULL);
6535 /* Here we would open the smb.conf file if needed ... */
6537 in_client = True; /* FIXME, make a param */
6539 home = getenv("HOME");
6541 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
6542 if (lp_load(conf, True, False, False, True)) {
6545 DEBUG(5, ("Could not load config file: %s\n",
6552 * Well, if that failed, try the dyn_CONFIGFILE
6553 * Which points to the standard locn, and if that
6554 * fails, silently ignore it and use the internal
6558 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6559 DEBUG(5, ("Could not load config file: %s\n",
6563 * We loaded the global config file. Now lets
6564 * load user-specific modifications to the
6567 slprintf(conf, sizeof(conf),
6568 "%s/.smb/smb.conf.append", home);
6569 if (!lp_load(conf, True, False, False, False)) {
6571 ("Could not append config file: "
6578 load_interfaces(); /* Load the list of interfaces ... */
6580 reopen_logs(); /* Get logging working ... */
6583 * Block SIGPIPE (from lib/util_sock.c: write())
6584 * It is not needed and should not stop execution
6586 BlockSignals(True, SIGPIPE);
6588 /* Done with one-time initialisation */
6589 smbc_initialized = 1;
6593 if (!context->user) {
6595 * FIXME: Is this the best way to get the user info?
6597 user = getenv("USER");
6598 /* walk around as "guest" if no username can be found */
6599 if (!user) context->user = SMB_STRDUP("guest");
6600 else context->user = SMB_STRDUP(user);
6603 if (!context->netbios_name) {
6605 * We try to get our netbios name from the config. If that
6606 * fails we fall back on constructing our netbios name from
6609 if (global_myname()) {
6610 context->netbios_name = SMB_STRDUP(global_myname());
6614 * Hmmm, I want to get hostname as well, but I am too
6615 * lazy for the moment
6618 context->netbios_name = (char *)SMB_MALLOC(17);
6619 if (!context->netbios_name) {
6623 slprintf(context->netbios_name, 16,
6624 "smbc%s%d", context->user, pid);
6628 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6630 if (!context->workgroup) {
6631 if (lp_workgroup()) {
6632 context->workgroup = SMB_STRDUP(lp_workgroup());
6635 /* TODO: Think about a decent default workgroup */
6636 context->workgroup = SMB_STRDUP("samba");
6640 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6642 /* shortest timeout is 1 second */
6643 if (context->timeout > 0 && context->timeout < 1000)
6644 context->timeout = 1000;
6647 * FIXME: Should we check the function pointers here?
6650 context->internal->_initialized = True;
6656 /* Return the verion of samba, and thus libsmbclient */
6660 return samba_version_string();