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')
135 * and smbc_urldecode_talloc() (internal fn.)
137 * Convert strings of %xx to their single character equivalent. Each 'x' must
138 * be a valid hexadecimal digit, or that % sequence is left undecoded.
140 * dest may, but need not be, the same pointer as src.
142 * Returns the number of % sequences which could not be converted due to lack
143 * of two following hexadecimal digits.
146 smbc_urldecode_talloc(TALLOC_CTX *ctx, char **pp_dest, const char *src)
148 int old_length = strlen(src);
155 if (old_length == 0) {
159 for (i = 0; i < old_length; ) {
160 unsigned char character = src[i++];
162 if (character == '%') {
163 int a = i+1 < old_length ? hex2int(src[i]) : -1;
164 int b = i+1 < old_length ? hex2int(src[i+1]) : -1;
166 /* Replace valid sequence */
167 if (a != -1 && b != -1) {
168 /* Replace valid %xx sequence with %dd */
169 character = (a * 16) + b;
170 if (character == '\0') {
171 break; /* Stop at %00 */
181 dest = TALLOC_ARRAY(ctx, char, newlen);
187 for (p = dest, i = 0; i < old_length; ) {
188 unsigned char character = src[i++];
190 if (character == '%') {
191 int a = i+1 < old_length ? hex2int(src[i]) : -1;
192 int b = i+1 < old_length ? hex2int(src[i+1]) : -1;
194 /* Replace valid sequence */
195 if (a != -1 && b != -1) {
196 /* Replace valid %xx sequence with %dd */
197 character = (a * 16) + b;
198 if (character == '\0') {
199 break; /* Stop at %00 */
215 smbc_urldecode(char *dest, char *src, size_t max_dest_len)
217 TALLOC_CTX *frame = talloc_stackframe();
219 int ret = smbc_urldecode_talloc(frame, &pdest, src);
222 strlcpy(dest, pdest, max_dest_len);
231 * Convert any characters not specifically allowed in a URL into their %xx
234 * Returns the remaining buffer length.
237 smbc_urlencode(char *dest, char *src, int max_dest_len)
239 char hex[] = "0123456789ABCDEF";
241 for (; *src != '\0' && max_dest_len >= 3; src++) {
253 *dest++ = hex[(*src >> 4) & 0x0f];
254 *dest++ = hex[*src & 0x0f];
269 * Function to parse a path and turn it into components
271 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
272 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
273 * general format ("smb:" only; we do not look for "cifs:").
277 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
281 * smb:// Show all workgroups.
283 * The method of locating the list of workgroups varies
284 * depending upon the setting of the context variable
285 * context->options.browse_max_lmb_count. This value
286 * determine the maximum number of local master browsers to
287 * query for the list of workgroups. In order to ensure that
288 * a complete list of workgroups is obtained, all master
289 * browsers must be queried, but if there are many
290 * workgroups, the time spent querying can begin to add up.
291 * For small networks (not many workgroups), it is suggested
292 * that this variable be set to 0, indicating query all local
293 * master browsers. When the network has many workgroups, a
294 * reasonable setting for this variable might be around 3.
296 * smb://name/ if name<1D> or name<1B> exists, list servers in
297 * workgroup, else, if name<20> exists, list all shares
300 * If "options" are provided, this function returns the entire option list as a
301 * string, for later parsing by the caller. Note that currently, no options
305 static const char *smbc_prefix = "smb:";
308 smbc_parse_path(TALLOC_CTX *ctx,
324 /* Ensure these returns are at least valid pointers. */
325 *pp_server = talloc_strdup(ctx, "");
326 *pp_share = talloc_strdup(ctx, "");
327 *pp_path = talloc_strdup(ctx, "");
328 *pp_user = talloc_strdup(ctx, "");
329 *pp_password = talloc_strdup(ctx, "");
331 if (!*pp_server || !*pp_share || !*pp_path ||
332 !*pp_user || !*pp_password) {
337 * Assume we wont find an authentication domain to parse, so default
338 * to the workgroup in the provided context.
340 if (pp_workgroup != NULL) {
341 *pp_workgroup = talloc_strdup(ctx, context->workgroup);
345 *pp_options = talloc_strdup(ctx, "");
347 s = talloc_strdup(ctx, fname);
349 /* see if it has the right prefix */
350 len = strlen(smbc_prefix);
351 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
352 return -1; /* What about no smb: ? */
357 /* Watch the test below, we are testing to see if we should exit */
359 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
360 DEBUG(1, ("Invalid path (does not begin with smb://"));
364 p += 2; /* Skip the double slash */
366 /* See if any options were specified */
367 if ((q = strrchr(p, '?')) != NULL ) {
368 /* There are options. Null terminate here and point to them */
371 DEBUG(4, ("Found options '%s'", q));
373 /* Copy the options */
374 if (*pp_options != NULL) {
375 TALLOC_FREE(*pp_options);
376 *pp_options = talloc_strdup(ctx, q);
385 int wl = strlen(context->workgroup);
391 *pp_server = talloc_strdup(ctx, context->workgroup);
395 *pp_server[wl] = '\0';
400 * ok, its for us. Now parse out the server, share etc.
402 * However, we want to parse out [[domain;]user[:password]@] if it
406 /* check that '@' occurs before '/', if '/' exists at all */
407 q = strchr_m(p, '@');
408 r = strchr_m(p, '/');
409 if (q && (!r || q < r)) {
410 char *userinfo = NULL;
413 next_token_no_ltrim_talloc(ctx, &p, &userinfo, "@");
419 if (strchr_m(u, ';')) {
421 next_token_no_ltrim_talloc(ctx, &u, &workgroup, ";");
426 *pp_workgroup = workgroup;
430 if (strchr_m(u, ':')) {
431 next_token_no_ltrim_talloc(ctx, &u, pp_user, ":");
435 *pp_password = talloc_strdup(ctx, u);
440 *pp_user = talloc_strdup(ctx, u);
447 if (!next_token_talloc(ctx, &p, pp_server, "/")) {
452 goto decoding; /* That's it ... */
455 if (!next_token_talloc(ctx, &p, pp_share, "/")) {
460 * Prepend a leading slash if there's a file path, as required by
464 *pp_path = talloc_asprintf(ctx,
468 *pp_path = talloc_strdup(ctx, "");
473 string_replace(*pp_path, '/', '\\');
477 (void) smbc_urldecode_talloc(ctx, pp_path, *pp_path);
478 (void) smbc_urldecode_talloc(ctx, pp_server, *pp_server);
479 (void) smbc_urldecode_talloc(ctx, pp_share, *pp_share);
480 (void) smbc_urldecode_talloc(ctx, pp_user, *pp_user);
481 (void) smbc_urldecode_talloc(ctx, pp_password, *pp_password);
487 * Verify that the options specified in a URL are valid
490 smbc_check_options(char *server,
495 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
496 "path='%s' options='%s'\n",
497 server, share, path, options));
499 /* No options at all is always ok */
500 if (! *options) return 0;
502 /* Currently, we don't support any options. */
507 * Convert an SMB error into a UNIX error ...
510 smbc_errno(SMBCCTX *context,
513 int ret = cli_errno(c);
515 if (cli_is_dos_error(c)) {
519 cli_dos_error(c, &eclass, &ecode);
521 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
522 (int)eclass, (int)ecode, (int)ecode, ret));
526 status = cli_nt_error(c);
528 DEBUG(3,("smbc errno %s -> %d\n",
529 nt_errstr(status), ret));
536 * Check a server for being alive and well.
537 * returns 0 if the server is in shape. Returns 1 on error
539 * Also useable outside libsmbclient to enable external cache
540 * to do some checks too.
543 smbc_check_server(SMBCCTX * context,
547 struct sockaddr addr;
550 return (getpeername(server->cli->fd, &addr, &size) == -1);
554 * Remove a server from the cached server list it's unused.
555 * On success, 0 is returned. 1 is returned if the server could not be removed.
557 * Also useable outside libsmbclient
560 smbc_remove_unused_server(SMBCCTX * context,
565 /* are we being fooled ? */
566 if (!context || !context->internal ||
567 !context->internal->_initialized || !srv) return 1;
570 /* Check all open files/directories for a relation with this server */
571 for (file = context->internal->_files; file; file=file->next) {
572 if (file->srv == srv) {
574 DEBUG(3, ("smbc_remove_usused_server: "
575 "%p still used by %p.\n",
581 DLIST_REMOVE(context->internal->_servers, srv);
583 cli_shutdown(srv->cli);
586 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
588 (context->callbacks.remove_cached_srv_fn)(context, srv);
596 find_server(SMBCCTX *context,
608 srv = (context->callbacks.get_cached_srv_fn)(context, server, share,
609 workgroup, username);
611 if (!auth_called && !srv && (!username[0] || !password[0])) {
612 if (context->internal->_auth_fn_with_context != NULL) {
613 (context->internal->_auth_fn_with_context)(
616 workgroup, sizeof(fstring),
617 username, sizeof(fstring),
618 password, sizeof(fstring));
620 (context->callbacks.auth_fn)(
622 workgroup, sizeof(fstring),
623 username, sizeof(fstring),
624 password, sizeof(fstring));
628 * However, smbc_auth_fn may have picked up info relating to
629 * an existing connection, so try for an existing connection
633 goto check_server_cache;
638 if ((context->callbacks.check_server_fn)(context, srv)) {
640 * This server is no good anymore
641 * Try to remove it and check for more possible
642 * servers in the cache
644 if ((context->callbacks.remove_unused_server_fn)(context,
647 * We could not remove the server completely,
648 * remove it from the cache so we will not get
649 * it again. It will be removed when the last
650 * file/dir is closed.
652 (context->callbacks.remove_cached_srv_fn)(context,
657 * Maybe there are more cached connections to this
660 goto check_server_cache;
670 * Connect to a server, possibly on an existing connection
672 * Here, what we want to do is: If the server and username
673 * match an existing connection, reuse that, otherwise, establish a
676 * If we have to create a new connection, call the auth_fn to get the
677 * info we need, unless the username and password were passed in.
681 smbc_server(SMBCCTX *context,
682 bool connect_if_not_found,
691 struct nmb_name called, calling;
692 const char *server_n = server;
693 struct sockaddr_storage ss;
694 int tried_reverse = 0;
697 const char *username_used;
703 if (server[0] == 0) {
708 /* Look for a cached connection */
709 srv = find_server(context, server, share,
710 workgroup, username, password);
713 * If we found a connection and we're only allowed one share per
716 if (srv && *share != '\0' && context->options.one_share_per_server) {
719 * ... then if there's no current connection to the share,
720 * connect to it. find_server(), or rather the function
721 * pointed to by context->callbacks.get_cached_srv_fn which
722 * was called by find_server(), will have issued a tree
723 * disconnect if the requested share is not the same as the
724 * one that was already connected.
726 if (srv->cli->cnum == (uint16) -1) {
727 /* Ensure we have accurate auth info */
728 if (context->internal->_auth_fn_with_context != NULL) {
729 (context->internal->_auth_fn_with_context)(
732 workgroup, sizeof(fstring),
733 username, sizeof(fstring),
734 password, sizeof(fstring));
736 (context->callbacks.auth_fn)(
738 workgroup, sizeof(fstring),
739 username, sizeof(fstring),
740 password, sizeof(fstring));
743 if (! cli_send_tconX(srv->cli, share, "?????",
744 password, strlen(password)+1)) {
746 errno = smbc_errno(context, srv->cli);
747 cli_shutdown(srv->cli);
749 (context->callbacks.remove_cached_srv_fn)(context,
755 * Regenerate the dev value since it's based on both
759 srv->dev = (dev_t)(str_checksum(server) ^
760 str_checksum(share));
765 /* If we have a connection... */
768 /* ... then we're done here. Give 'em what they came for. */
772 /* If we're not asked to connect when a connection doesn't exist... */
773 if (! connect_if_not_found) {
774 /* ... then we're done here. */
778 make_nmb_name(&calling, context->netbios_name, 0x0);
779 make_nmb_name(&called , server, 0x20);
781 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
783 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
789 /* have to open a new connection */
790 if ((c = cli_initialise()) == NULL) {
795 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
796 c->use_kerberos = True;
798 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
799 c->fallback_after_kerberos = True;
802 c->timeout = context->timeout;
805 * Force use of port 139 for first try if share is $IPC, empty, or
806 * null, so browse lists can work
808 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
809 port_try_first = 139;
812 port_try_first = 445;
816 c->port = port_try_first;
818 status = cli_connect(c, server_n, &ss);
819 if (!NT_STATUS_IS_OK(status)) {
821 /* First connection attempt failed. Try alternate port. */
822 c->port = port_try_next;
824 status = cli_connect(c, server_n, &ss);
825 if (!NT_STATUS_IS_OK(status)) {
832 if (!cli_session_request(c, &calling, &called)) {
834 if (strcmp(called.name, "*SMBSERVER")) {
835 make_nmb_name(&called , "*SMBSERVER", 0x20);
837 } else { /* Try one more time, but ensure we don't loop */
839 /* Only try this if server is an IP address ... */
841 if (is_ipaddress(server) && !tried_reverse) {
843 struct sockaddr_storage rem_ss;
845 if (!interpret_string_addr(&rem_ss, server,
847 DEBUG(4, ("Could not convert IP address "
848 "%s to struct sockaddr_storage\n",
854 tried_reverse++; /* Yuck */
856 if (name_status_find("*", 0, 0, &rem_ss, remote_name)) {
857 make_nmb_name(&called, remote_name, 0x20);
866 DEBUG(4,(" session request ok\n"));
868 if (!cli_negprot(c)) {
874 username_used = username;
876 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
877 password, strlen(password),
878 password, strlen(password),
881 /* Failed. Try an anonymous login, if allowed by flags. */
884 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
885 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
896 DEBUG(4,(" session setup ok\n"));
898 if (!cli_send_tconX(c, share, "?????",
899 password, strlen(password)+1)) {
900 errno = smbc_errno(context, c);
905 DEBUG(4,(" tconx ok\n"));
908 * Ok, we have got a nice connection
909 * Let's allocate a server structure.
912 srv = SMB_MALLOC_P(SMBCSRV);
920 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
921 srv->no_pathinfo = False;
922 srv->no_pathinfo2 = False;
923 srv->no_nt_session = False;
925 /* now add it to the cache (internal or external) */
926 /* Let the cache function set errno if it wants to */
928 if ((context->callbacks.add_cached_srv_fn)(context, srv,
930 workgroup, username)) {
931 int saved_errno = errno;
932 DEBUG(3, (" Failed to add server to cache\n"));
940 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
941 server, share, srv));
943 DLIST_ADD(context->internal->_servers, srv);
957 * Connect to a server for getting/setting attributes, possibly on an existing
958 * connection. This works similarly to smbc_server().
961 smbc_attr_server(SMBCCTX *context,
970 struct sockaddr_storage ss;
971 struct cli_state *ipc_cli;
972 struct rpc_pipe_client *pipe_hnd;
974 SMBCSRV *ipc_srv=NULL;
977 * See if we've already created this special connection. Reference
978 * our "special" share name '*IPC$', which is an impossible real share
979 * name due to the leading asterisk.
981 ipc_srv = find_server(context, server, "*IPC$",
982 workgroup, username, password);
985 /* We didn't find a cached connection. Get the password */
986 if (*password == '\0') {
987 /* ... then retrieve it now. */
988 if (context->internal->_auth_fn_with_context != NULL) {
989 (context->internal->_auth_fn_with_context)(
992 workgroup, sizeof(fstring),
993 username, sizeof(fstring),
994 password, sizeof(fstring));
996 (context->callbacks.auth_fn)(
998 workgroup, sizeof(fstring),
999 username, sizeof(fstring),
1000 password, sizeof(fstring));
1005 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
1006 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1010 nt_status = cli_full_connection(&ipc_cli,
1011 global_myname(), server,
1012 &ss, 0, "IPC$", "?????",
1013 username, workgroup,
1016 if (! NT_STATUS_IS_OK(nt_status)) {
1017 DEBUG(1,("cli_full_connection failed! (%s)\n",
1018 nt_errstr(nt_status)));
1023 ipc_srv = SMB_MALLOC_P(SMBCSRV);
1026 cli_shutdown(ipc_cli);
1030 ZERO_STRUCTP(ipc_srv);
1031 ipc_srv->cli = ipc_cli;
1034 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1038 DEBUG(1, ("cli_nt_session_open fail!\n"));
1040 cli_shutdown(ipc_srv->cli);
1046 * Some systems don't support
1047 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1048 * so we might as well do it too.
1051 nt_status = rpccli_lsa_open_policy(
1055 GENERIC_EXECUTE_ACCESS,
1058 if (!NT_STATUS_IS_OK(nt_status)) {
1059 errno = smbc_errno(context, ipc_srv->cli);
1060 cli_shutdown(ipc_srv->cli);
1065 /* now add it to the cache (internal or external) */
1067 errno = 0; /* let cache function set errno if it likes */
1068 if ((context->callbacks.add_cached_srv_fn)(context, ipc_srv,
1073 DEBUG(3, (" Failed to add server to cache\n"));
1077 cli_shutdown(ipc_srv->cli);
1082 DLIST_ADD(context->internal->_servers, ipc_srv);
1089 * Routine to open() a file ...
1093 smbc_open_ctx(SMBCCTX *context,
1098 char *server, *share, *user, *password, *workgroup;
1100 char *targetpath = NULL;
1101 struct cli_state *targetcli;
1102 SMBCSRV *srv = NULL;
1103 SMBCFILE *file = NULL;
1105 TALLOC_CTX *frame = talloc_stackframe();
1107 if (!context || !context->internal ||
1108 !context->internal->_initialized) {
1110 errno = EINVAL; /* Best I can think of ... */
1124 if (smbc_parse_path(frame,
1139 if (!user || user[0] == (char)0) {
1140 user = talloc_strdup(frame, context->user);
1148 srv = smbc_server(context, True,
1149 server, share, workgroup, user, password);
1152 if (errno == EPERM) errno = EACCES;
1154 return NULL; /* smbc_server sets errno */
1157 /* Hmmm, the test for a directory is suspect here ... FIXME */
1159 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1162 file = SMB_MALLOC_P(SMBCFILE);
1172 /*d_printf(">>>open: resolving %s\n", path);*/
1173 if (!cli_resolve_path(frame, "", srv->cli, path, &targetcli, &targetpath)) {
1174 d_printf("Could not resolve %s\n", path);
1179 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1181 if ((fd = cli_open(targetcli, targetpath, flags,
1182 context->internal->_share_mode)) < 0) {
1184 /* Handle the error ... */
1187 errno = smbc_errno(context, targetcli);
1193 /* Fill in file struct */
1196 file->fname = SMB_STRDUP(fname);
1201 DLIST_ADD(context->internal->_files, file);
1204 * If the file was opened in O_APPEND mode, all write
1205 * operations should be appended to the file. To do that,
1206 * though, using this protocol, would require a getattrE()
1207 * call for each and every write, to determine where the end
1208 * of the file is. (There does not appear to be an append flag
1209 * in the protocol.) Rather than add all of that overhead of
1210 * retrieving the current end-of-file offset prior to each
1211 * write operation, we'll assume that most append operations
1212 * will continuously write, so we'll just set the offset to
1213 * the end of the file now and hope that's adequate.
1215 * Note to self: If this proves inadequate, and O_APPEND
1216 * should, in some cases, be forced for each write, add a
1217 * field in the context options structure, for
1218 * "strict_append_mode" which would select between the current
1219 * behavior (if FALSE) or issuing a getattrE() prior to each
1220 * write and forcing the write to the end of the file (if
1221 * TRUE). Adding that capability will likely require adding
1222 * an "append" flag into the _SMBCFILE structure to track
1223 * whether a file was opened in O_APPEND mode. -- djl
1225 if (flags & O_APPEND) {
1226 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1227 (void) smbc_close_ctx(context, file);
1239 /* Check if opendir needed ... */
1244 eno = smbc_errno(context, srv->cli);
1245 file = (context->opendir)(context, fname);
1246 if (!file) errno = eno;
1252 errno = EINVAL; /* FIXME, correct errno ? */
1259 * Routine to create a file
1262 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1265 smbc_creat_ctx(SMBCCTX *context,
1270 if (!context || !context->internal ||
1271 !context->internal->_initialized) {
1278 return smbc_open_ctx(context, path, creat_bits, mode);
1282 * Routine to read() a file ...
1286 smbc_read_ctx(SMBCCTX *context,
1292 char *server, *share, *user, *password;
1294 char *targetpath = NULL;
1295 struct cli_state *targetcli;
1296 TALLOC_CTX *frame = talloc_stackframe();
1301 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1302 * appears to pass file->offset (which is type off_t) differently than
1303 * a local variable of type off_t. Using local variable "offset" in
1304 * the call to cli_read() instead of file->offset fixes a problem
1305 * retrieving data at an offset greater than 4GB.
1309 if (!context || !context->internal ||
1310 !context->internal->_initialized) {
1317 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1319 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1326 offset = file->offset;
1328 /* Check that the buffer exists ... */
1337 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1338 if (smbc_parse_path(frame,
1353 /*d_printf(">>>read: resolving %s\n", path);*/
1354 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1355 &targetcli, &targetpath)) {
1356 d_printf("Could not resolve %s\n", path);
1360 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1362 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1366 errno = smbc_errno(context, targetcli);
1372 file->offset += ret;
1374 DEBUG(4, (" --> %d\n", ret));
1377 return ret; /* Success, ret bytes of data ... */
1382 * Routine to write() a file ...
1386 smbc_write_ctx(SMBCCTX *context,
1393 char *server, *share, *user, *password;
1395 char *targetpath = NULL;
1396 struct cli_state *targetcli;
1397 TALLOC_CTX *frame = talloc_stackframe();
1399 /* First check all pointers before dereferencing them */
1401 if (!context || !context->internal ||
1402 !context->internal->_initialized) {
1409 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1415 /* Check that the buffer exists ... */
1424 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1426 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1427 if (smbc_parse_path(frame,
1442 /*d_printf(">>>write: resolving %s\n", path);*/
1443 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1444 &targetcli, &targetpath)) {
1445 d_printf("Could not resolve %s\n", path);
1449 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1451 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1454 errno = smbc_errno(context, targetcli);
1460 file->offset += ret;
1463 return ret; /* Success, 0 bytes of data ... */
1467 * Routine to close() a file ...
1471 smbc_close_ctx(SMBCCTX *context,
1475 char *server, *share, *user, *password;
1477 char *targetpath = NULL;
1478 struct cli_state *targetcli;
1479 TALLOC_CTX *frame = talloc_stackframe();
1481 if (!context || !context->internal ||
1482 !context->internal->_initialized) {
1489 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1498 return (context->closedir)(context, file);
1501 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1502 if (smbc_parse_path(frame,
1517 /*d_printf(">>>close: resolving %s\n", path);*/
1518 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1519 &targetcli, &targetpath)) {
1520 d_printf("Could not resolve %s\n", path);
1524 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1526 if (!cli_close(targetcli, file->cli_fd)) {
1528 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1530 /* Deallocate slot and remove the server
1531 * from the server cache if unused */
1532 errno = smbc_errno(context, targetcli);
1534 DLIST_REMOVE(context->internal->_files, file);
1535 SAFE_FREE(file->fname);
1537 (context->callbacks.remove_unused_server_fn)(context, srv);
1543 DLIST_REMOVE(context->internal->_files, file);
1544 SAFE_FREE(file->fname);
1552 * Get info from an SMB server on a file. Use a qpathinfo call first
1553 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1556 smbc_getatr(SMBCCTX * context,
1561 struct timespec *create_time_ts,
1562 struct timespec *access_time_ts,
1563 struct timespec *write_time_ts,
1564 struct timespec *change_time_ts,
1568 char *targetpath = NULL;
1569 struct cli_state *targetcli;
1571 TALLOC_CTX *frame = talloc_stackframe();
1573 if (!context || !context->internal ||
1574 !context->internal->_initialized) {
1580 /* path fixup for . and .. */
1581 if (strequal(path, ".") || strequal(path, "..")) {
1582 fixedpath = talloc_strdup(frame, "\\");
1589 fixedpath = talloc_strdup(frame, path);
1595 trim_string(fixedpath, NULL, "\\..");
1596 trim_string(fixedpath, NULL, "\\.");
1598 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1600 if (!cli_resolve_path(frame, "", srv->cli, fixedpath,
1601 &targetcli, &targetpath)) {
1602 d_printf("Couldn't resolve %s\n", path);
1607 if (!srv->no_pathinfo2 &&
1608 cli_qpathinfo2(targetcli, targetpath,
1618 /* if this is NT then don't bother with the getatr */
1619 if (targetcli->capabilities & CAP_NT_SMBS) {
1625 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1627 struct timespec w_time_ts;
1629 w_time_ts = convert_time_t_to_timespec(write_time);
1631 if (write_time_ts != NULL) {
1632 *write_time_ts = w_time_ts;
1635 if (create_time_ts != NULL) {
1636 *create_time_ts = w_time_ts;
1639 if (access_time_ts != NULL) {
1640 *access_time_ts = w_time_ts;
1643 if (change_time_ts != NULL) {
1644 *change_time_ts = w_time_ts;
1647 srv->no_pathinfo2 = True;
1659 * Set file info on an SMB server. Use setpathinfo call first. If that
1660 * fails, use setattrE..
1662 * Access and modification time parameters are always used and must be
1663 * provided. Create time, if zero, will be determined from the actual create
1664 * time of the file. If non-zero, the create time will be set as well.
1666 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1669 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1678 TALLOC_CTX *frame = talloc_stackframe();
1681 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1682 * modern function for "new code" to be using, and it works given a
1683 * filename rather than requiring that the file be opened to have its
1684 * attributes manipulated.
1686 if (srv->no_pathinfo ||
1687 ! cli_setpathinfo(srv->cli, path,
1695 * setpathinfo is not supported; go to plan B.
1697 * cli_setatr() does not work on win98, and it also doesn't
1698 * support setting the access time (only the modification
1699 * time), so in all cases, we open the specified file and use
1700 * cli_setattrE() which should work on all OS versions, and
1701 * supports both times.
1704 /* Don't try {q,set}pathinfo() again, with this server */
1705 srv->no_pathinfo = True;
1708 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1710 errno = smbc_errno(context, srv->cli);
1715 /* Set the new attributes */
1716 ret = cli_setattrE(srv->cli, fd,
1721 /* Close the file */
1722 cli_close(srv->cli, fd);
1725 * Unfortunately, setattrE() doesn't have a provision for
1726 * setting the access mode (attributes). We'll have to try
1727 * cli_setatr() for that, and with only this parameter, it
1728 * seems to work on win98.
1730 if (ret && mode != (uint16) -1) {
1731 ret = cli_setatr(srv->cli, path, mode, 0);
1735 errno = smbc_errno(context, srv->cli);
1746 * Routine to unlink() a file
1750 smbc_unlink_ctx(SMBCCTX *context,
1753 char *server, *share, *user, *password, *workgroup;
1756 struct cli_state *targetcli;
1757 SMBCSRV *srv = NULL;
1758 TALLOC_CTX *frame = talloc_stackframe();
1760 if (!context || !context->internal ||
1761 !context->internal->_initialized) {
1762 errno = EINVAL; /* Best I can think of ... */
1775 if (smbc_parse_path(frame,
1790 if (!user || user[0] == (char)0) {
1791 user = talloc_strdup(frame, context->user);
1799 srv = smbc_server(context, True,
1800 server, share, workgroup, user, password);
1804 return -1; /* smbc_server sets errno */
1808 /*d_printf(">>>unlink: resolving %s\n", path);*/
1809 if (!cli_resolve_path(frame, "", srv->cli, path,
1810 &targetcli, &targetpath)) {
1811 d_printf("Could not resolve %s\n", path);
1815 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1817 if (!cli_unlink(targetcli, targetpath)) {
1819 errno = smbc_errno(context, targetcli);
1821 if (errno == EACCES) { /* Check if the file is a directory */
1826 struct timespec write_time_ts;
1827 struct timespec access_time_ts;
1828 struct timespec change_time_ts;
1831 if (!smbc_getatr(context, srv, path, &mode, &size,
1838 /* Hmmm, bad error ... What? */
1840 errno = smbc_errno(context, targetcli);
1847 if (IS_DOS_DIR(mode))
1850 errno = saverr; /* Restore this */
1861 return 0; /* Success ... */
1866 * Routine to rename() a file
1870 smbc_rename_ctx(SMBCCTX *ocontext,
1888 struct cli_state *targetcli1;
1889 struct cli_state *targetcli2;
1890 SMBCSRV *srv = NULL;
1891 TALLOC_CTX *frame = talloc_stackframe();
1893 if (!ocontext || !ncontext ||
1894 !ocontext->internal || !ncontext->internal ||
1895 !ocontext->internal->_initialized ||
1896 !ncontext->internal->_initialized) {
1897 errno = EINVAL; /* Best I can think of ... */
1902 if (!oname || !nname) {
1908 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1910 if (smbc_parse_path(frame,
1925 if (!user1 || user1[0] == (char)0) {
1926 user1 = talloc_strdup(frame, ocontext->user);
1934 if (smbc_parse_path(frame,
1949 if (!user2 || user2[0] == (char)0) {
1950 user2 = talloc_strdup(frame, ncontext->user);
1958 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1959 strcmp(user1, user2)) {
1960 /* Can't rename across file systems, or users?? */
1966 srv = smbc_server(ocontext, True,
1967 server1, share1, workgroup, user1, password1);
1974 /*d_printf(">>>rename: resolving %s\n", path1);*/
1975 if (!cli_resolve_path(frame, "", srv->cli, path1,
1976 &targetcli1, &targetpath1)) {
1977 d_printf("Could not resolve %s\n", path1);
1981 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1982 /*d_printf(">>>rename: resolving %s\n", path2);*/
1983 if (!cli_resolve_path(frame, "", srv->cli, path2,
1984 &targetcli2, &targetpath2)) {
1985 d_printf("Could not resolve %s\n", path2);
1989 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1991 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1992 strcmp(targetcli1->share, targetcli2->share))
1994 /* can't rename across file systems */
2000 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
2001 int eno = smbc_errno(ocontext, targetcli1);
2003 if (eno != EEXIST ||
2004 !cli_unlink(targetcli1, targetpath2) ||
2005 !cli_rename(targetcli1, targetpath1, targetpath2)) {
2015 return 0; /* Success */
2019 * A routine to lseek() a file
2023 smbc_lseek_ctx(SMBCCTX *context,
2029 char *server, *share, *user, *password;
2032 struct cli_state *targetcli;
2033 TALLOC_CTX *frame = talloc_stackframe();
2035 if (!context || !context->internal ||
2036 !context->internal->_initialized) {
2042 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2054 return -1; /* Can't lseek a dir ... */
2060 file->offset = offset;
2064 file->offset += offset;
2068 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
2069 if (smbc_parse_path(frame,
2084 /*d_printf(">>>lseek: resolving %s\n", path);*/
2085 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2086 &targetcli, &targetpath)) {
2087 d_printf("Could not resolve %s\n", path);
2091 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
2093 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
2094 &size, NULL, NULL, NULL, NULL, NULL))
2096 SMB_OFF_T b_size = size;
2097 if (!cli_getattrE(targetcli, file->cli_fd,
2098 NULL, &b_size, NULL, NULL, NULL))
2106 file->offset = size + offset;
2116 return file->offset;
2121 * Generate an inode number from file name for those things that need it
2125 smbc_inode(SMBCCTX *context,
2128 if (!context || !context->internal ||
2129 !context->internal->_initialized) {
2136 if (!*name) return 2; /* FIXME, why 2 ??? */
2137 return (ino_t)str_checksum(name);
2142 * Routine to put basic stat info into a stat structure ... Used by stat and
2147 smbc_setup_stat(SMBCCTX *context,
2153 TALLOC_CTX *frame = talloc_stackframe();
2157 if (IS_DOS_DIR(mode)) {
2158 st->st_mode = SMBC_DIR_MODE;
2160 st->st_mode = SMBC_FILE_MODE;
2163 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2164 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2165 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2166 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2169 #ifdef HAVE_STAT_ST_BLKSIZE
2170 st->st_blksize = 512;
2172 #ifdef HAVE_STAT_ST_BLOCKS
2173 st->st_blocks = (size+511)/512;
2175 st->st_uid = getuid();
2176 st->st_gid = getgid();
2178 if (IS_DOS_DIR(mode)) {
2184 if (st->st_ino == 0) {
2185 st->st_ino = smbc_inode(context, fname);
2189 return True; /* FIXME: Is this needed ? */
2194 * Routine to stat a file given a name
2198 smbc_stat_ctx(SMBCCTX *context,
2209 struct timespec write_time_ts;
2210 struct timespec access_time_ts;
2211 struct timespec change_time_ts;
2215 TALLOC_CTX *frame = talloc_stackframe();
2217 if (!context || !context->internal ||
2218 !context->internal->_initialized) {
2220 errno = EINVAL; /* Best I can think of ... */
2231 DEBUG(4, ("smbc_stat(%s)\n", fname));
2233 if (smbc_parse_path(frame,
2248 if (!user || user[0] == (char)0) {
2249 user = talloc_strdup(frame,context->user);
2257 srv = smbc_server(context, True,
2258 server, share, workgroup, user, password);
2262 return -1; /* errno set by smbc_server */
2265 if (!smbc_getatr(context, srv, path, &mode, &size,
2271 errno = smbc_errno(context, srv->cli);
2278 smbc_setup_stat(context, st, path, size, mode);
2280 set_atimespec(st, access_time_ts);
2281 set_ctimespec(st, change_time_ts);
2282 set_mtimespec(st, write_time_ts);
2283 st->st_dev = srv->dev;
2291 * Routine to stat a file given an fd
2295 smbc_fstat_ctx(SMBCCTX *context,
2299 struct timespec change_time_ts;
2300 struct timespec access_time_ts;
2301 struct timespec write_time_ts;
2310 struct cli_state *targetcli;
2312 TALLOC_CTX *frame = talloc_stackframe();
2314 if (!context || !context->internal ||
2315 !context->internal->_initialized) {
2321 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2329 return (context->fstatdir)(context, file, st);
2332 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2333 if (smbc_parse_path(frame,
2348 /*d_printf(">>>fstat: resolving %s\n", path);*/
2349 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2350 &targetcli, &targetpath)) {
2351 d_printf("Could not resolve %s\n", path);
2355 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2357 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2364 time_t change_time, access_time, write_time;
2366 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2367 &change_time, &access_time, &write_time)) {
2374 change_time_ts = convert_time_t_to_timespec(change_time);
2375 access_time_ts = convert_time_t_to_timespec(access_time);
2376 write_time_ts = convert_time_t_to_timespec(write_time);
2381 smbc_setup_stat(context, st, file->fname, size, mode);
2383 set_atimespec(st, access_time_ts);
2384 set_ctimespec(st, change_time_ts);
2385 set_mtimespec(st, write_time_ts);
2386 st->st_dev = file->srv->dev;
2394 * Routine to open a directory
2395 * We accept the URL syntax explained in smbc_parse_path(), above.
2399 smbc_remove_dir(SMBCFILE *dir)
2401 struct smbc_dir_list *d,*f;
2408 SAFE_FREE(f->dirent);
2413 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2418 add_dirent(SMBCFILE *dir,
2420 const char *comment,
2423 struct smbc_dirent *dirent;
2425 int name_length = (name == NULL ? 0 : strlen(name));
2426 int comment_len = (comment == NULL ? 0 : strlen(comment));
2429 * Allocate space for the dirent, which must be increased by the
2430 * size of the name and the comment and 1 each for the null terminator.
2433 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2435 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2439 dir->dir_error = ENOMEM;
2444 ZERO_STRUCTP(dirent);
2446 if (dir->dir_list == NULL) {
2448 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2449 if (!dir->dir_list) {
2452 dir->dir_error = ENOMEM;
2456 ZERO_STRUCTP(dir->dir_list);
2458 dir->dir_end = dir->dir_next = dir->dir_list;
2462 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2464 if (!dir->dir_end->next) {
2467 dir->dir_error = ENOMEM;
2471 ZERO_STRUCTP(dir->dir_end->next);
2473 dir->dir_end = dir->dir_end->next;
2476 dir->dir_end->next = NULL;
2477 dir->dir_end->dirent = dirent;
2479 dirent->smbc_type = type;
2480 dirent->namelen = name_length;
2481 dirent->commentlen = comment_len;
2482 dirent->dirlen = size;
2485 * dirent->namelen + 1 includes the null (no null termination needed)
2486 * Ditto for dirent->commentlen.
2487 * The space for the two null bytes was allocated.
2489 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2490 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2491 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2498 list_unique_wg_fn(const char *name,
2500 const char *comment,
2503 SMBCFILE *dir = (SMBCFILE *)state;
2504 struct smbc_dir_list *dir_list;
2505 struct smbc_dirent *dirent;
2509 dirent_type = dir->dir_type;
2511 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2513 /* An error occurred, what do we do? */
2514 /* FIXME: Add some code here */
2517 /* Point to the one just added */
2518 dirent = dir->dir_end->dirent;
2520 /* See if this was a duplicate */
2521 for (dir_list = dir->dir_list;
2522 dir_list != dir->dir_end;
2523 dir_list = dir_list->next) {
2525 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2526 /* Duplicate. End end of list need to be removed. */
2530 if (do_remove && dir_list->next == dir->dir_end) {
2531 /* Found the end of the list. Remove it. */
2532 dir->dir_end = dir_list;
2533 free(dir_list->next);
2535 dir_list->next = NULL;
2542 list_fn(const char *name,
2544 const char *comment,
2547 SMBCFILE *dir = (SMBCFILE *)state;
2551 * We need to process the type a little ...
2553 * Disk share = 0x00000000
2554 * Print share = 0x00000001
2555 * Comms share = 0x00000002 (obsolete?)
2556 * IPC$ share = 0x00000003
2558 * administrative shares:
2559 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2562 if (dir->dir_type == SMBC_FILE_SHARE) {
2564 case 0 | 0x80000000:
2566 dirent_type = SMBC_FILE_SHARE;
2570 dirent_type = SMBC_PRINTER_SHARE;
2574 dirent_type = SMBC_COMMS_SHARE;
2577 case 3 | 0x80000000:
2579 dirent_type = SMBC_IPC_SHARE;
2583 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2588 dirent_type = dir->dir_type;
2591 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2593 /* An error occurred, what do we do? */
2594 /* FIXME: Add some code here */
2600 dir_list_fn(const char *mnt,
2606 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2607 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2609 /* Handle an error ... */
2611 /* FIXME: Add some code ... */
2618 net_share_enum_rpc(struct cli_state *cli,
2619 void (*fn)(const char *name,
2621 const char *comment,
2628 uint32 info_level = 1;
2629 uint32 preferred_len = 0xffffffff;
2631 SRV_SHARE_INFO_CTR ctr;
2633 fstring comment = "";
2634 struct rpc_pipe_client *pipe_hnd;
2637 /* Open the server service pipe */
2638 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2640 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2644 /* Issue the NetShareEnum RPC call and retrieve the response */
2645 init_enum_hnd(&enum_hnd, 0);
2646 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2653 /* Was it successful? */
2654 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2655 /* Nope. Go clean up. */
2659 /* For each returned entry... */
2660 for (i = 0; i < ctr.num_entries; i++) {
2662 /* pull out the share name */
2663 rpcstr_pull_unistr2_fstring(
2664 name, &ctr.share.info1[i].info_1_str.uni_netname);
2666 /* pull out the share's comment */
2667 rpcstr_pull_unistr2_fstring(
2668 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2670 /* Get the type value */
2671 type = ctr.share.info1[i].info_1.type;
2673 /* Add this share to the list */
2674 (*fn)(name, type, comment, state);
2678 /* Close the server service pipe */
2679 cli_rpc_pipe_close(pipe_hnd);
2681 /* Tell 'em if it worked */
2682 return W_ERROR_IS_OK(result) ? 0 : -1;
2688 smbc_opendir_ctx(SMBCCTX *context,
2692 char *server, *share, *user, *password, *options;
2697 SMBCSRV *srv = NULL;
2698 SMBCFILE *dir = NULL;
2699 struct _smbc_callbacks *cb;
2700 struct sockaddr_storage rem_ss;
2701 TALLOC_CTX *frame = talloc_stackframe();
2703 if (!context || !context->internal ||
2704 !context->internal->_initialized) {
2705 DEBUG(4, ("no valid context\n"));
2706 errno = EINVAL + 8192;
2713 DEBUG(4, ("no valid fname\n"));
2714 errno = EINVAL + 8193;
2719 if (smbc_parse_path(frame,
2729 DEBUG(4, ("no valid path\n"));
2730 errno = EINVAL + 8194;
2735 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2736 "path='%s' options='%s'\n",
2737 fname, server, share, path, options));
2739 /* Ensure the options are valid */
2740 if (smbc_check_options(server, share, path, options)) {
2741 DEBUG(4, ("unacceptable options (%s)\n", options));
2742 errno = EINVAL + 8195;
2747 if (user[0] == (char)0) fstrcpy(user, context->user);
2749 dir = SMB_MALLOC_P(SMBCFILE);
2760 dir->fname = SMB_STRDUP(fname);
2764 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2766 if (server[0] == (char)0) {
2771 struct ip_service *ip_list;
2772 struct ip_service server_addr;
2773 struct user_auth_info u_info;
2774 struct cli_state *cli;
2776 if (share[0] != (char)0 || path[0] != (char)0) {
2778 errno = EINVAL + 8196;
2780 SAFE_FREE(dir->fname);
2787 /* Determine how many local master browsers to query */
2788 max_lmb_count = (context->options.browse_max_lmb_count == 0
2790 : context->options.browse_max_lmb_count);
2792 pstrcpy(u_info.username, user);
2793 pstrcpy(u_info.password, password);
2796 * We have server and share and path empty but options
2797 * requesting that we scan all master browsers for their list
2798 * of workgroups/domains. This implies that we must first try
2799 * broadcast queries to find all master browsers, and if that
2800 * doesn't work, then try our other methods which return only
2801 * a single master browser.
2805 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
2811 if (!find_master_ip(workgroup, &server_addr.ss)) {
2814 SAFE_FREE(dir->fname);
2822 ip_list = (struct ip_service *)memdup(
2823 &server_addr, sizeof(server_addr));
2824 if (ip_list == NULL) {
2832 for (i = 0; i < count && i < max_lmb_count; i++) {
2833 char addr[INET6_ADDRSTRLEN];
2834 char *wg_ptr = NULL;
2836 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
2837 DEBUG(99, ("Found master browser %d of %d: %s\n",
2838 i+1, MAX(count, max_lmb_count),
2841 cli = get_ipc_connect_master_ip(talloc_tos(),
2845 /* cli == NULL is the master browser refused to talk or
2846 could not be found */
2850 pstrcpy(workgroup, wg_ptr);
2851 fstrcpy(server, cli->desthost);
2854 DEBUG(4, ("using workgroup %s %s\n",
2855 workgroup, server));
2858 * For each returned master browser IP address, get a
2859 * connection to IPC$ on the server if we do not
2860 * already have one, and determine the
2861 * workgroups/domains that it knows about.
2864 srv = smbc_server(context, True, server, "IPC$",
2865 workgroup, user, password);
2871 dir->dir_type = SMBC_WORKGROUP;
2873 /* Now, list the stuff ... */
2875 if (!cli_NetServerEnum(srv->cli,
2877 SV_TYPE_DOMAIN_ENUM,
2887 * Server not an empty string ... Check the rest and see what
2890 if (*share == '\0') {
2891 if (*path != '\0') {
2893 /* Should not have empty share with path */
2894 errno = EINVAL + 8197;
2896 SAFE_FREE(dir->fname);
2905 * We don't know if <server> is really a server name
2906 * or is a workgroup/domain name. If we already have
2907 * a server structure for it, we'll use it.
2908 * Otherwise, check to see if <server><1D>,
2909 * <server><1B>, or <server><20> translates. We check
2910 * to see if <server> is an IP address first.
2914 * See if we have an existing server. Do not
2915 * establish a connection if one does not already
2918 srv = smbc_server(context, False, server, "IPC$",
2919 workgroup, user, password);
2922 * If no existing server and not an IP addr, look for
2926 !is_ipaddress(server) &&
2927 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
2928 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
2932 dir->dir_type = SMBC_SERVER;
2935 * Get the backup list ...
2937 if (!name_status_find(server, 0, 0,
2938 &rem_ss, buserver)) {
2940 DEBUG(0, ("Could not get name of "
2941 "local/domain master browser "
2942 "for server %s\n", server));
2944 SAFE_FREE(dir->fname);
2954 * Get a connection to IPC$ on the server if
2955 * we do not already have one
2957 srv = smbc_server(context, True,
2959 workgroup, user, password);
2961 DEBUG(0, ("got no contact to IPC$\n"));
2963 SAFE_FREE(dir->fname);
2973 /* Now, list the servers ... */
2974 if (!cli_NetServerEnum(srv->cli, server,
2975 0x0000FFFE, list_fn,
2979 SAFE_FREE(dir->fname);
2986 (resolve_name(server, &rem_ss, 0x20))) {
2988 /* If we hadn't found the server, get one now */
2990 srv = smbc_server(context, True,
2998 SAFE_FREE(dir->fname);
3006 dir->dir_type = SMBC_FILE_SHARE;
3009 /* List the shares ... */
3011 if (net_share_enum_rpc(
3014 (void *) dir) < 0 &&
3020 errno = cli_errno(srv->cli);
3022 SAFE_FREE(dir->fname);
3030 /* Neither the workgroup nor server exists */
3031 errno = ECONNREFUSED;
3033 SAFE_FREE(dir->fname);
3043 * The server and share are specified ... work from
3047 struct cli_state *targetcli;
3049 /* We connect to the server and list the directory */
3050 dir->dir_type = SMBC_FILE_SHARE;
3052 srv = smbc_server(context, True, server, share,
3053 workgroup, user, password);
3058 SAFE_FREE(dir->fname);
3068 /* Now, list the files ... */
3070 p = path + strlen(path);
3071 pstrcat(path, "\\*");
3073 if (!cli_resolve_path(frame, "", srv->cli, path,
3074 &targetcli, &targetpath)) {
3075 d_printf("Could not resolve %s\n", path);
3077 SAFE_FREE(dir->fname);
3084 if (cli_list(targetcli, targetpath,
3085 aDIR | aSYSTEM | aHIDDEN,
3086 dir_list_fn, (void *)dir) < 0) {
3089 SAFE_FREE(dir->fname);
3092 saved_errno = smbc_errno(context, targetcli);
3094 if (saved_errno == EINVAL) {
3096 * See if they asked to opendir something
3097 * other than a directory. If so, the
3098 * converted error value we got would have
3099 * been EINVAL rather than ENOTDIR.
3101 *p = '\0'; /* restore original path */
3103 if (smbc_getatr(context, srv, path,
3105 NULL, NULL, NULL, NULL,
3107 ! IS_DOS_DIR(mode)) {
3109 /* It is. Correct the error value */
3110 saved_errno = ENOTDIR;
3115 * If there was an error and the server is no
3118 cb = &context->callbacks;
3119 if (cli_is_error(targetcli) &&
3120 (cb->check_server_fn)(context, srv)) {
3122 /* ... then remove it. */
3123 if ((cb->remove_unused_server_fn)(context,
3126 * We could not remove the
3127 * server completely, remove
3128 * it from the cache so we
3129 * will not get it again. It
3130 * will be removed when the
3131 * last file/dir is closed.
3133 (cb->remove_cached_srv_fn)(context,
3138 errno = saved_errno;
3146 DLIST_ADD(context->internal->_files, dir);
3153 * Routine to close a directory
3157 smbc_closedir_ctx(SMBCCTX *context,
3160 TALLOC_CTX *frame = talloc_stackframe();
3162 if (!context || !context->internal ||
3163 !context->internal->_initialized) {
3171 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3179 smbc_remove_dir(dir); /* Clean it up */
3181 DLIST_REMOVE(context->internal->_files, dir);
3185 SAFE_FREE(dir->fname);
3186 SAFE_FREE(dir); /* Free the space too */
3195 smbc_readdir_internal(SMBCCTX * context,
3196 struct smbc_dirent *dest,
3197 struct smbc_dirent *src,
3198 int max_namebuf_len)
3200 if (context->options.urlencode_readdir_entries) {
3202 /* url-encode the name. get back remaining buffer space */
3204 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3206 /* We now know the name length */
3207 dest->namelen = strlen(dest->name);
3209 /* Save the pointer to the beginning of the comment */
3210 dest->comment = dest->name + dest->namelen + 1;
3212 /* Copy the comment */
3213 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3214 dest->comment[max_namebuf_len - 1] = '\0';
3216 /* Save other fields */
3217 dest->smbc_type = src->smbc_type;
3218 dest->commentlen = strlen(dest->comment);
3219 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3223 /* No encoding. Just copy the entry as is. */
3224 memcpy(dest, src, src->dirlen);
3225 dest->comment = (char *)(&dest->name + src->namelen + 1);
3231 * Routine to get a directory entry
3234 struct smbc_dirent *
3235 smbc_readdir_ctx(SMBCCTX *context,
3239 struct smbc_dirent *dirp, *dirent;
3240 TALLOC_CTX *frame = talloc_stackframe();
3242 /* Check that all is ok first ... */
3244 if (!context || !context->internal ||
3245 !context->internal->_initialized) {
3248 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3254 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3257 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3263 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3266 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3272 if (!dir->dir_next) {
3277 dirent = dir->dir_next->dirent;
3286 dirp = (struct smbc_dirent *)context->internal->_dirent;
3287 maxlen = (sizeof(context->internal->_dirent) -
3288 sizeof(struct smbc_dirent));
3290 smbc_readdir_internal(context, dirp, dirent, maxlen);
3292 dir->dir_next = dir->dir_next->next;
3299 * Routine to get directory entries
3303 smbc_getdents_ctx(SMBCCTX *context,
3305 struct smbc_dirent *dirp,
3311 char *ndir = (char *)dirp;
3312 struct smbc_dir_list *dirlist;
3313 TALLOC_CTX *frame = talloc_stackframe();
3315 /* Check that all is ok first ... */
3317 if (!context || !context->internal ||
3318 !context->internal->_initialized) {
3326 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3334 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3343 * Now, retrieve the number of entries that will fit in what was passed
3344 * We have to figure out if the info is in the list, or we need to
3345 * send a request to the server to get the info.
3348 while ((dirlist = dir->dir_next)) {
3349 struct smbc_dirent *dirent;
3351 if (!dirlist->dirent) {
3353 errno = ENOENT; /* Bad error */
3359 /* Do urlencoding of next entry, if so selected */
3360 dirent = (struct smbc_dirent *)context->internal->_dirent;
3361 maxlen = (sizeof(context->internal->_dirent) -
3362 sizeof(struct smbc_dirent));
3363 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3365 reqd = dirent->dirlen;
3369 if (rem < count) { /* We managed to copy something */
3376 else { /* Nothing copied ... */
3378 errno = EINVAL; /* Not enough space ... */
3386 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3388 ((struct smbc_dirent *)ndir)->comment =
3389 (char *)(&((struct smbc_dirent *)ndir)->name +
3397 dir->dir_next = dirlist = dirlist -> next;
3410 * Routine to create a directory ...
3414 smbc_mkdir_ctx(SMBCCTX *context,
3426 struct cli_state *targetcli;
3427 TALLOC_CTX *frame = talloc_stackframe();
3429 if (!context || !context->internal ||
3430 !context->internal->_initialized) {
3442 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3444 if (smbc_parse_path(frame,
3459 if (!user || user[0] == (char)0) {
3460 user = talloc_strdup(frame, context->user);
3468 srv = smbc_server(context, True,
3469 server, share, workgroup, user, password);
3474 return -1; /* errno set by smbc_server */
3478 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3479 if (!cli_resolve_path(frame, "", srv->cli, path,
3480 &targetcli, &targetpath)) {
3481 d_printf("Could not resolve %s\n", path);
3485 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3487 if (!cli_mkdir(targetcli, targetpath)) {
3489 errno = smbc_errno(context, targetcli);
3501 * Our list function simply checks to see if a directory is not empty
3504 static int smbc_rmdir_dirempty = True;
3507 rmdir_list_fn(const char *mnt,
3512 if (strncmp(finfo->name, ".", 1) != 0 &&
3513 strncmp(finfo->name, "..", 2) != 0) {
3514 smbc_rmdir_dirempty = False;
3519 * Routine to remove a directory
3523 smbc_rmdir_ctx(SMBCCTX *context,
3534 struct cli_state *targetcli;
3535 TALLOC_CTX *frame = talloc_stackframe();
3537 if (!context || !context->internal ||
3538 !context->internal->_initialized) {
3550 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3552 if (smbc_parse_path(frame,
3567 if (!user || user[0] == (char)0) {
3568 user = talloc_strdup(frame, context->user);
3576 srv = smbc_server(context, True,
3577 server, share, workgroup, user, password);
3582 return -1; /* errno set by smbc_server */
3586 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3587 if (!cli_resolve_path(frame, "", srv->cli, path,
3588 &targetcli, &targetpath)) {
3589 d_printf("Could not resolve %s\n", path);
3593 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3596 if (!cli_rmdir(targetcli, targetpath)) {
3598 errno = smbc_errno(context, targetcli);
3600 if (errno == EACCES) { /* Check if the dir empty or not */
3602 /* Local storage to avoid buffer overflows */
3605 smbc_rmdir_dirempty = True; /* Make this so ... */
3607 lpath = talloc_asprintf(frame, "%s\\*",
3615 if (cli_list(targetcli, lpath,
3616 aDIR | aSYSTEM | aHIDDEN,
3617 rmdir_list_fn, NULL) < 0) {
3619 /* Fix errno to ignore latest error ... */
3620 DEBUG(5, ("smbc_rmdir: "
3621 "cli_list returned an error: %d\n",
3622 smbc_errno(context, targetcli)));
3627 if (smbc_rmdir_dirempty)
3645 * Routine to return the current directory position
3649 smbc_telldir_ctx(SMBCCTX *context,
3652 TALLOC_CTX *frame = talloc_stackframe();
3654 if (!context || !context->internal ||
3655 !context->internal->_initialized) {
3663 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3671 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3679 /* See if we're already at the end. */
3680 if (dir->dir_next == NULL) {
3687 * We return the pointer here as the offset
3690 return (off_t)(long)dir->dir_next->dirent;
3694 * A routine to run down the list and see if the entry is OK
3697 struct smbc_dir_list *
3698 smbc_check_dir_ent(struct smbc_dir_list *list,
3699 struct smbc_dirent *dirent)
3702 /* Run down the list looking for what we want */
3706 struct smbc_dir_list *tmp = list;
3710 if (tmp->dirent == dirent)
3719 return NULL; /* Not found, or an error */
3725 * Routine to seek on a directory
3729 smbc_lseekdir_ctx(SMBCCTX *context,
3733 long int l_offset = offset; /* Handle problems of size */
3734 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3735 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3736 TALLOC_CTX *frame = talloc_stackframe();
3738 if (!context || !context->internal ||
3739 !context->internal->_initialized) {
3747 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3755 /* Now, check what we were passed and see if it is OK ... */
3757 if (dirent == NULL) { /* Seek to the begining of the list */
3759 dir->dir_next = dir->dir_list;
3765 if (offset == -1) { /* Seek to the end of the list */
3766 dir->dir_next = NULL;
3771 /* Now, run down the list and make sure that the entry is OK */
3772 /* This may need to be changed if we change the format of the list */
3774 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3775 errno = EINVAL; /* Bad entry */
3780 dir->dir_next = list_ent;
3787 * Routine to fstat a dir
3791 smbc_fstatdir_ctx(SMBCCTX *context,
3796 if (!context || !context->internal ||
3797 !context->internal->_initialized) {
3802 /* No code yet ... */
3807 smbc_chmod_ctx(SMBCCTX *context,
3819 TALLOC_CTX *frame = talloc_stackframe();
3821 if (!context || !context->internal ||
3822 !context->internal->_initialized) {
3823 errno = EINVAL; /* Best I can think of ... */
3834 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3836 if (smbc_parse_path(frame,
3851 if (!user || user[0] == (char)0) {
3852 user = talloc_strdup(frame, context->user);
3860 srv = smbc_server(context, True,
3861 server, share, workgroup, user, password);
3865 return -1; /* errno set by smbc_server */
3870 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3871 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3872 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3873 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3875 if (!cli_setatr(srv->cli, path, mode, 0)) {
3876 errno = smbc_errno(context, srv->cli);
3886 smbc_utimes_ctx(SMBCCTX *context,
3888 struct timeval *tbuf)
3899 TALLOC_CTX *frame = talloc_stackframe();
3901 if (!context || !context->internal ||
3902 !context->internal->_initialized) {
3903 errno = EINVAL; /* Best I can think of ... */
3915 access_time = write_time = time(NULL);
3917 access_time = tbuf[0].tv_sec;
3918 write_time = tbuf[1].tv_sec;
3926 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3927 atimebuf[sizeof(atimebuf) - 1] = '\0';
3928 if ((p = strchr(atimebuf, '\n')) != NULL) {
3932 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3933 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3934 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3938 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3939 fname, atimebuf, mtimebuf);
3942 if (smbc_parse_path(frame,
3957 if (!user || user[0] == (char)0) {
3958 user = talloc_strdup(frame, context->user);
3966 srv = smbc_server(context, True,
3967 server, share, workgroup, user, password);
3971 return -1; /* errno set by smbc_server */
3974 if (!smbc_setatr(context, srv, path,
3975 0, access_time, write_time, 0, 0)) {
3977 return -1; /* errno set by smbc_setatr */
3986 * Sort ACEs according to the documentation at
3987 * http://support.microsoft.com/kb/269175, at least as far as it defines the
3992 ace_compare(SEC_ACE *ace1,
3998 /* If the ACEs are equal, we have nothing more to do. */
3999 if (sec_ace_equal(ace1, ace2)) {
4003 /* Inherited follow non-inherited */
4004 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
4005 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
4007 return (b1 ? 1 : -1);
4011 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
4012 * sort them after DENY and ALLOW.
4014 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
4015 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
4016 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
4017 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4018 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
4019 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
4020 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
4021 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4023 return (b1 ? 1 : -1);
4026 /* Allowed ACEs follow denied ACEs */
4027 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
4028 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
4029 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
4030 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
4032 return (b1 ? 1 : -1);
4036 * ACEs applying to an entity's object follow those applying to the
4039 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
4040 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4041 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
4042 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4044 return (b1 ? 1 : -1);
4048 * If we get this far, the ACEs are similar as far as the
4049 * characteristics we typically care about (those defined by the
4050 * referenced MS document). We'll now sort by characteristics that
4051 * just seems reasonable.
4054 if (ace1->type != ace2->type) {
4055 return ace2->type - ace1->type;
4058 if (sid_compare(&ace1->trustee, &ace2->trustee)) {
4059 return sid_compare(&ace1->trustee, &ace2->trustee);
4062 if (ace1->flags != ace2->flags) {
4063 return ace1->flags - ace2->flags;
4066 if (ace1->access_mask != ace2->access_mask) {
4067 return ace1->access_mask - ace2->access_mask;
4070 if (ace1->size != ace2->size) {
4071 return ace1->size - ace2->size;
4074 return memcmp(ace1, ace2, sizeof(SEC_ACE));
4079 sort_acl(SEC_ACL *the_acl)
4082 if (!the_acl) return;
4084 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
4085 QSORT_CAST ace_compare);
4087 for (i=1;i<the_acl->num_aces;) {
4088 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
4090 for (j=i; j<the_acl->num_aces-1; j++) {
4091 the_acl->aces[j] = the_acl->aces[j+1];
4093 the_acl->num_aces--;
4100 /* convert a SID to a string, either numeric or username/group */
4102 convert_sid_to_string(struct cli_state *ipc_cli,
4108 char **domains = NULL;
4109 char **names = NULL;
4110 enum lsa_SidType *types = NULL;
4111 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
4114 sid_to_string(str, sid);
4117 return; /* no lookup desired */
4124 /* Ask LSA to convert the sid to a name */
4126 ctx = talloc_stackframe();
4128 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ctx,
4129 pol, 1, sid, &domains,
4131 !domains || !domains[0] || !names || !names[0]) {
4139 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
4140 domains[0], lp_winbind_separator(),
4144 /* convert a string to a SID, either numeric or username/group */
4146 convert_string_to_sid(struct cli_state *ipc_cli,
4152 enum lsa_SidType *types = NULL;
4153 DOM_SID *sids = NULL;
4156 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
4163 if (strncmp(str, "S-", 2) == 0) {
4164 return string_to_sid(sid, str);
4171 ctx = talloc_stackframe();
4172 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ctx,
4173 pol, 1, &str, NULL, 1, &sids,
4179 sid_copy(sid, &sids[0]);
4187 /* parse an ACE in the same format as print_ace() */
4189 parse_ace(struct cli_state *ipc_cli,
4199 unsigned int aflags;
4203 const struct perm_value *v;
4209 /* These values discovered by inspection */
4210 static const struct perm_value special_values[] = {
4211 { "R", 0x00120089 },
4212 { "W", 0x00120116 },
4213 { "X", 0x001200a0 },
4214 { "D", 0x00010000 },
4215 { "P", 0x00040000 },
4216 { "O", 0x00080000 },
4220 static const struct perm_value standard_values[] = {
4221 { "READ", 0x001200a9 },
4222 { "CHANGE", 0x001301bf },
4223 { "FULL", 0x001f01ff },
4229 p = strchr_m(str,':');
4230 if (!p) return False;
4233 /* Try to parse numeric form */
4235 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
4236 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
4240 /* Try to parse text form */
4242 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
4247 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
4251 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
4252 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
4253 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
4254 atype = SEC_ACE_TYPE_ACCESS_DENIED;
4259 /* Only numeric form accepted for flags at present */
4261 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
4262 sscanf(tok, "%i", &aflags))) {
4266 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
4270 if (strncmp(tok, "0x", 2) == 0) {
4271 if (sscanf(tok, "%i", &amask) != 1) {
4277 for (v = standard_values; v->perm; v++) {
4278 if (strcmp(tok, v->perm) == 0) {
4289 for (v = special_values; v->perm; v++) {
4290 if (v->perm[0] == *p) {
4296 if (!found) return False;
4306 init_sec_ace(ace, &sid, atype, mask, aflags);
4310 /* add an ACE to a list of ACEs in a SEC_ACL */
4312 add_ace(SEC_ACL **the_acl,
4320 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
4324 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4327 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4328 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4329 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4330 1+(*the_acl)->num_aces, aces);
4332 (*the_acl) = newacl;
4337 /* parse a ascii version of a security descriptor */
4339 sec_desc_parse(TALLOC_CTX *ctx,
4340 struct cli_state *ipc_cli,
4345 const char *p = str;
4347 SEC_DESC *ret = NULL;
4349 DOM_SID *group_sid=NULL;
4350 DOM_SID *owner_sid=NULL;
4354 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4356 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4357 revision = strtol(tok+9, NULL, 16);
4361 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4363 DEBUG(5, ("OWNER specified more than once!\n"));
4366 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4368 !convert_string_to_sid(ipc_cli, pol,
4370 owner_sid, tok+6)) {
4371 DEBUG(5, ("Failed to parse owner sid\n"));
4377 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4379 DEBUG(5, ("OWNER specified more than once!\n"));
4382 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4384 !convert_string_to_sid(ipc_cli, pol,
4386 owner_sid, tok+7)) {
4387 DEBUG(5, ("Failed to parse owner sid\n"));
4393 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4395 DEBUG(5, ("GROUP specified more than once!\n"));
4398 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4400 !convert_string_to_sid(ipc_cli, pol,
4402 group_sid, tok+6)) {
4403 DEBUG(5, ("Failed to parse group sid\n"));
4409 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4411 DEBUG(5, ("GROUP specified more than once!\n"));
4414 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4416 !convert_string_to_sid(ipc_cli, pol,
4418 group_sid, tok+6)) {
4419 DEBUG(5, ("Failed to parse group sid\n"));
4425 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4427 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4428 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4431 if(!add_ace(&dacl, &ace, ctx)) {
4432 DEBUG(5, ("Failed to add ACL %s\n", tok));
4438 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4440 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4441 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4444 if(!add_ace(&dacl, &ace, ctx)) {
4445 DEBUG(5, ("Failed to add ACL %s\n", tok));
4451 DEBUG(5, ("Failed to parse security descriptor\n"));
4455 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4456 owner_sid, group_sid, NULL, dacl, &sd_size);
4459 SAFE_FREE(group_sid);
4460 SAFE_FREE(owner_sid);
4466 /* Obtain the current dos attributes */
4467 static DOS_ATTR_DESC *
4468 dos_attr_query(SMBCCTX *context,
4470 const char *filename,
4473 struct timespec create_time_ts;
4474 struct timespec write_time_ts;
4475 struct timespec access_time_ts;
4476 struct timespec change_time_ts;
4479 SMB_INO_T inode = 0;
4482 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4488 /* Obtain the DOS attributes */
4489 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4496 errno = smbc_errno(context, srv->cli);
4497 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4503 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4504 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4505 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4506 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4513 /* parse a ascii version of a security descriptor */
4515 dos_attr_parse(SMBCCTX *context,
4521 const char *p = str;
4524 const char * create_time_attr;
4525 const char * access_time_attr;
4526 const char * write_time_attr;
4527 const char * change_time_attr;
4530 /* Determine whether to use old-style or new-style attribute names */
4531 if (context->internal->_full_time_names) {
4532 /* new-style names */
4533 attr_strings.create_time_attr = "CREATE_TIME";
4534 attr_strings.access_time_attr = "ACCESS_TIME";
4535 attr_strings.write_time_attr = "WRITE_TIME";
4536 attr_strings.change_time_attr = "CHANGE_TIME";
4538 /* old-style names */
4539 attr_strings.create_time_attr = NULL;
4540 attr_strings.access_time_attr = "A_TIME";
4541 attr_strings.write_time_attr = "M_TIME";
4542 attr_strings.change_time_attr = "C_TIME";
4545 /* if this is to set the entire ACL... */
4547 /* ... then increment past the first colon if there is one */
4548 if ((p = strchr(str, ':')) != NULL) {
4555 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4557 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4558 dad->mode = strtol(tok+5, NULL, 16);
4562 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4563 dad->size = (SMB_OFF_T)atof(tok+5);
4567 n = strlen(attr_strings.access_time_attr);
4568 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4569 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4573 n = strlen(attr_strings.change_time_attr);
4574 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4575 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4579 n = strlen(attr_strings.write_time_attr);
4580 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4581 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4585 if (attr_strings.create_time_attr != NULL) {
4586 n = strlen(attr_strings.create_time_attr);
4587 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4589 dad->create_time = (time_t)strtol(tok+n+1,
4595 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4596 dad->inode = (SMB_INO_T)atof(tok+6);
4602 /*****************************************************
4603 Retrieve the acls for a file.
4604 *******************************************************/
4607 cacl_get(SMBCCTX *context,
4610 struct cli_state *ipc_cli,
4626 bool exclude_nt_revision = False;
4627 bool exclude_nt_owner = False;
4628 bool exclude_nt_group = False;
4629 bool exclude_nt_acl = False;
4630 bool exclude_dos_mode = False;
4631 bool exclude_dos_size = False;
4632 bool exclude_dos_create_time = False;
4633 bool exclude_dos_access_time = False;
4634 bool exclude_dos_write_time = False;
4635 bool exclude_dos_change_time = False;
4636 bool exclude_dos_inode = False;
4637 bool numeric = True;
4638 bool determine_size = (bufsize == 0);
4642 fstring name_sandbox;
4646 struct timespec create_time_ts;
4647 struct timespec write_time_ts;
4648 struct timespec access_time_ts;
4649 struct timespec change_time_ts;
4650 time_t create_time = (time_t)0;
4651 time_t write_time = (time_t)0;
4652 time_t access_time = (time_t)0;
4653 time_t change_time = (time_t)0;
4657 struct cli_state *cli = srv->cli;
4659 const char * create_time_attr;
4660 const char * access_time_attr;
4661 const char * write_time_attr;
4662 const char * change_time_attr;
4665 const char * create_time_attr;
4666 const char * access_time_attr;
4667 const char * write_time_attr;
4668 const char * change_time_attr;
4669 } excl_attr_strings;
4671 /* Determine whether to use old-style or new-style attribute names */
4672 if (context->internal->_full_time_names) {
4673 /* new-style names */
4674 attr_strings.create_time_attr = "CREATE_TIME";
4675 attr_strings.access_time_attr = "ACCESS_TIME";
4676 attr_strings.write_time_attr = "WRITE_TIME";
4677 attr_strings.change_time_attr = "CHANGE_TIME";
4679 excl_attr_strings.create_time_attr = "CREATE_TIME";
4680 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4681 excl_attr_strings.write_time_attr = "WRITE_TIME";
4682 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4684 /* old-style names */
4685 attr_strings.create_time_attr = NULL;
4686 attr_strings.access_time_attr = "A_TIME";
4687 attr_strings.write_time_attr = "M_TIME";
4688 attr_strings.change_time_attr = "C_TIME";
4690 excl_attr_strings.create_time_attr = NULL;
4691 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4692 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4693 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4696 /* Copy name so we can strip off exclusions (if any are specified) */
4697 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4699 /* Ensure name is null terminated */
4700 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4702 /* Play in the sandbox */
4703 name = name_sandbox;
4705 /* If there are any exclusions, point to them and mask them from name */
4706 if ((pExclude = strchr(name, '!')) != NULL)
4711 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4712 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4713 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4714 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4715 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4716 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4717 numeric = (* (name + strlen(name) - 1) != '+');
4719 /* Look for exclusions from "all" requests */
4720 if (all || all_nt || all_dos) {
4722 /* Exclusions are delimited by '!' */
4725 pExclude = (p == NULL ? NULL : p + 1)) {
4727 /* Find end of this exclusion name */
4728 if ((p = strchr(pExclude, '!')) != NULL)
4733 /* Which exclusion name is this? */
4734 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4735 exclude_nt_revision = True;
4737 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4738 exclude_nt_owner = True;
4740 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4741 exclude_nt_group = True;
4743 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4744 exclude_nt_acl = True;
4746 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4747 exclude_dos_mode = True;
4749 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4750 exclude_dos_size = True;
4752 else if (excl_attr_strings.create_time_attr != NULL &&
4753 StrCaseCmp(pExclude,
4754 excl_attr_strings.change_time_attr) == 0) {
4755 exclude_dos_create_time = True;
4757 else if (StrCaseCmp(pExclude,
4758 excl_attr_strings.access_time_attr) == 0) {
4759 exclude_dos_access_time = True;
4761 else if (StrCaseCmp(pExclude,
4762 excl_attr_strings.write_time_attr) == 0) {
4763 exclude_dos_write_time = True;
4765 else if (StrCaseCmp(pExclude,
4766 excl_attr_strings.change_time_attr) == 0) {
4767 exclude_dos_change_time = True;
4769 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4770 exclude_dos_inode = True;
4773 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4784 * If we are (possibly) talking to an NT or new system and some NT
4785 * attributes have been requested...
4787 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4788 /* Point to the portion after "system.nt_sec_desc." */
4789 name += 19; /* if (all) this will be invalid but unused */
4791 /* ... then obtain any NT attributes which were requested */
4792 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4795 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4796 filename, cli_errstr(cli)));
4801 sd = cli_query_secdesc(cli, fnum, ctx);
4805 ("cacl_get Failed to query old descriptor\n"));
4810 cli_close(cli, fnum);
4812 if (! exclude_nt_revision) {
4813 if (all || all_nt) {
4814 if (determine_size) {
4815 p = talloc_asprintf(ctx,
4824 n = snprintf(buf, bufsize,
4828 } else if (StrCaseCmp(name, "revision") == 0) {
4829 if (determine_size) {
4830 p = talloc_asprintf(ctx, "%d",
4838 n = snprintf(buf, bufsize, "%d",
4843 if (!determine_size && n > bufsize) {
4853 if (! exclude_nt_owner) {
4854 /* Get owner and group sid */
4855 if (sd->owner_sid) {
4856 convert_sid_to_string(ipc_cli, pol,
4861 fstrcpy(sidstr, "");
4864 if (all || all_nt) {
4865 if (determine_size) {
4866 p = talloc_asprintf(ctx, ",OWNER:%s",
4873 } else if (sidstr[0] != '\0') {
4874 n = snprintf(buf, bufsize,
4875 ",OWNER:%s", sidstr);
4877 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4878 if (determine_size) {
4879 p = talloc_asprintf(ctx, "%s", sidstr);
4886 n = snprintf(buf, bufsize, "%s",
4891 if (!determine_size && n > bufsize) {
4901 if (! exclude_nt_group) {
4902 if (sd->group_sid) {
4903 convert_sid_to_string(ipc_cli, pol,
4907 fstrcpy(sidstr, "");
4910 if (all || all_nt) {
4911 if (determine_size) {
4912 p = talloc_asprintf(ctx, ",GROUP:%s",
4919 } else if (sidstr[0] != '\0') {
4920 n = snprintf(buf, bufsize,
4921 ",GROUP:%s", sidstr);
4923 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4924 if (determine_size) {
4925 p = talloc_asprintf(ctx, "%s", sidstr);
4932 n = snprintf(buf, bufsize,
4937 if (!determine_size && n > bufsize) {
4947 if (! exclude_nt_acl) {
4948 /* Add aces to value buffer */
4949 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4951 SEC_ACE *ace = &sd->dacl->aces[i];
4952 convert_sid_to_string(ipc_cli, pol,
4956 if (all || all_nt) {
4957 if (determine_size) {
4958 p = talloc_asprintf(
4974 ",ACL:%s:%d/%d/0x%08x",
4980 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4981 StrCaseCmp(name+3, sidstr) == 0) ||
4982 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4983 StrCaseCmp(name+4, sidstr) == 0)) {
4984 if (determine_size) {
4985 p = talloc_asprintf(
4997 n = snprintf(buf, bufsize,
5003 } else if (all_nt_acls) {
5004 if (determine_size) {
5005 p = talloc_asprintf(
5007 "%s%s:%d/%d/0x%08x",
5019 n = snprintf(buf, bufsize,
5020 "%s%s:%d/%d/0x%08x",
5028 if (!determine_size && n > bufsize) {
5039 /* Restore name pointer to its original value */
5043 if (all || some_dos) {
5044 /* Point to the portion after "system.dos_attr." */
5045 name += 16; /* if (all) this will be invalid but unused */
5047 /* Obtain the DOS attributes */
5048 if (!smbc_getatr(context, srv, filename, &mode, &size,
5055 errno = smbc_errno(context, srv->cli);
5060 create_time = convert_timespec_to_time_t(create_time_ts);
5061 access_time = convert_timespec_to_time_t(access_time_ts);
5062 write_time = convert_timespec_to_time_t(write_time_ts);
5063 change_time = convert_timespec_to_time_t(change_time_ts);
5065 if (! exclude_dos_mode) {
5066 if (all || all_dos) {
5067 if (determine_size) {
5068 p = talloc_asprintf(ctx,
5081 n = snprintf(buf, bufsize,
5089 } else if (StrCaseCmp(name, "mode") == 0) {
5090 if (determine_size) {
5091 p = talloc_asprintf(ctx, "0x%x", mode);
5098 n = snprintf(buf, bufsize,
5103 if (!determine_size && n > bufsize) {
5113 if (! exclude_dos_size) {
5114 if (all || all_dos) {
5115 if (determine_size) {
5116 p = talloc_asprintf(
5126 n = snprintf(buf, bufsize,
5130 } else if (StrCaseCmp(name, "size") == 0) {
5131 if (determine_size) {
5132 p = talloc_asprintf(
5142 n = snprintf(buf, bufsize,
5148 if (!determine_size && n > bufsize) {
5158 if (! exclude_dos_create_time &&
5159 attr_strings.create_time_attr != NULL) {
5160 if (all || all_dos) {
5161 if (determine_size) {
5162 p = talloc_asprintf(ctx,
5164 attr_strings.create_time_attr,
5172 n = snprintf(buf, bufsize,
5174 attr_strings.create_time_attr,
5177 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
5178 if (determine_size) {
5179 p = talloc_asprintf(ctx, "%lu", create_time);
5186 n = snprintf(buf, bufsize,
5187 "%lu", create_time);
5191 if (!determine_size && n > bufsize) {
5201 if (! exclude_dos_access_time) {
5202 if (all || all_dos) {
5203 if (determine_size) {
5204 p = talloc_asprintf(ctx,
5206 attr_strings.access_time_attr,
5214 n = snprintf(buf, bufsize,
5216 attr_strings.access_time_attr,
5219 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
5220 if (determine_size) {
5221 p = talloc_asprintf(ctx, "%lu", access_time);
5228 n = snprintf(buf, bufsize,
5229 "%lu", access_time);
5233 if (!determine_size && n > bufsize) {
5243 if (! exclude_dos_write_time) {
5244 if (all || all_dos) {
5245 if (determine_size) {
5246 p = talloc_asprintf(ctx,
5248 attr_strings.write_time_attr,
5256 n = snprintf(buf, bufsize,
5258 attr_strings.write_time_attr,
5261 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
5262 if (determine_size) {
5263 p = talloc_asprintf(ctx, "%lu", write_time);
5270 n = snprintf(buf, bufsize,
5275 if (!determine_size && n > bufsize) {
5285 if (! exclude_dos_change_time) {
5286 if (all || all_dos) {
5287 if (determine_size) {
5288 p = talloc_asprintf(ctx,
5290 attr_strings.change_time_attr,
5298 n = snprintf(buf, bufsize,
5300 attr_strings.change_time_attr,
5303 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5304 if (determine_size) {
5305 p = talloc_asprintf(ctx, "%lu", change_time);
5312 n = snprintf(buf, bufsize,
5313 "%lu", change_time);
5317 if (!determine_size && n > bufsize) {
5327 if (! exclude_dos_inode) {
5328 if (all || all_dos) {
5329 if (determine_size) {
5330 p = talloc_asprintf(
5340 n = snprintf(buf, bufsize,
5344 } else if (StrCaseCmp(name, "inode") == 0) {
5345 if (determine_size) {
5346 p = talloc_asprintf(
5356 n = snprintf(buf, bufsize,
5362 if (!determine_size && n > bufsize) {
5372 /* Restore name pointer to its original value */
5384 /*****************************************************
5385 set the ACLs on a file given an ascii description
5386 *******************************************************/
5388 cacl_set(TALLOC_CTX *ctx,
5389 struct cli_state *cli,
5390 struct cli_state *ipc_cli,
5392 const char *filename,
5393 const char *the_acl,
5399 SEC_DESC *sd = NULL, *old;
5400 SEC_ACL *dacl = NULL;
5401 DOM_SID *owner_sid = NULL;
5402 DOM_SID *group_sid = NULL;
5407 bool numeric = True;
5409 /* the_acl will be null for REMOVE_ALL operations */
5411 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5415 /* if this is to set the entire ACL... */
5416 if (*the_acl == '*') {
5417 /* ... then increment past the first colon */
5421 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5422 CONST_DISCARD(char *, the_acl));
5430 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5431 that doesn't deref sd */
5433 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5438 /* The desired access below is the only one I could find that works
5439 with NT4, W2KP and Samba */
5441 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5444 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5445 filename, cli_errstr(cli)));
5450 old = cli_query_secdesc(cli, fnum, ctx);
5453 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5458 cli_close(cli, fnum);
5461 case SMBC_XATTR_MODE_REMOVE_ALL:
5462 old->dacl->num_aces = 0;
5466 case SMBC_XATTR_MODE_REMOVE:
5467 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5470 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5471 if (sec_ace_equal(&sd->dacl->aces[i],
5472 &old->dacl->aces[j])) {
5474 for (k=j; k<old->dacl->num_aces-1;k++) {
5475 old->dacl->aces[k] =
5476 old->dacl->aces[k+1];
5478 old->dacl->num_aces--;
5493 case SMBC_XATTR_MODE_ADD:
5494 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5497 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5498 if (sid_equal(&sd->dacl->aces[i].trustee,
5499 &old->dacl->aces[j].trustee)) {
5500 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5505 old->dacl->aces[j] = sd->dacl->aces[i];
5511 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5517 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5518 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5524 case SMBC_XATTR_MODE_SET:
5526 owner_sid = old->owner_sid;
5527 group_sid = old->group_sid;
5531 case SMBC_XATTR_MODE_CHOWN:
5532 owner_sid = sd->owner_sid;
5535 case SMBC_XATTR_MODE_CHGRP:
5536 group_sid = sd->group_sid;
5540 /* Denied ACE entries must come before allowed ones */
5541 sort_acl(old->dacl);
5543 /* Create new security descriptor and set it */
5544 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5545 owner_sid, group_sid, NULL, dacl, &sd_size);
5547 fnum = cli_nt_create(cli, filename,
5548 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5551 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5552 filename, cli_errstr(cli)));
5557 if (!cli_set_secdesc(cli, fnum, sd)) {
5558 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5565 cli_close(cli, fnum);
5576 smbc_setxattr_ctx(SMBCCTX *context,
5596 const char * create_time_attr;
5597 const char * access_time_attr;
5598 const char * write_time_attr;
5599 const char * change_time_attr;
5601 TALLOC_CTX *frame = talloc_stackframe();
5603 if (!context || !context->internal ||
5604 !context->internal->_initialized) {
5605 errno = EINVAL; /* Best I can think of ... */
5616 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5617 fname, name, (int) size, (const char*)value));
5619 if (smbc_parse_path(frame,
5634 if (!user || user[0] == (char)0) {
5635 user = talloc_strdup(frame, context->user);
5643 srv = smbc_server(context, True,
5644 server, share, workgroup, user, password);
5647 return -1; /* errno set by smbc_server */
5650 if (! srv->no_nt_session) {
5651 ipc_srv = smbc_attr_server(context, server, share,
5652 workgroup, user, password,
5655 srv->no_nt_session = True;
5662 * Are they asking to set the entire set of known attributes?
5664 if (StrCaseCmp(name, "system.*") == 0 ||
5665 StrCaseCmp(name, "system.*+") == 0) {
5668 talloc_asprintf(talloc_tos(), "%s:%s",
5669 name+7, (const char *) value);
5678 ret = cacl_set(talloc_tos(), srv->cli,
5679 ipc_srv->cli, &pol, path,
5682 ? SMBC_XATTR_MODE_SET
5683 : SMBC_XATTR_MODE_ADD),
5689 /* get a DOS Attribute Descriptor with current attributes */
5690 dad = dos_attr_query(context, talloc_tos(), path, srv);
5692 /* Overwrite old with new, using what was provided */
5693 dos_attr_parse(context, dad, srv, namevalue);
5695 /* Set the new DOS attributes */
5696 if (! smbc_setatr(context, srv, path,
5703 /* cause failure if NT failed too */
5708 /* we only fail if both NT and DOS sets failed */
5709 if (ret < 0 && ! dad) {
5710 ret = -1; /* in case dad was null */
5721 * Are they asking to set an access control element or to set
5722 * the entire access control list?
5724 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5725 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5726 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5727 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5728 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5732 talloc_asprintf(talloc_tos(), "%s:%s",
5733 name+19, (const char *) value);
5736 ret = -1; /* errno set by smbc_server() */
5738 else if (! namevalue) {
5742 ret = cacl_set(talloc_tos(), srv->cli,
5743 ipc_srv->cli, &pol, path,
5746 ? SMBC_XATTR_MODE_SET
5747 : SMBC_XATTR_MODE_ADD),
5755 * Are they asking to set the owner?
5757 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5758 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5762 talloc_asprintf(talloc_tos(), "%s:%s",
5763 name+19, (const char *) value);
5766 ret = -1; /* errno set by smbc_server() */
5768 else if (! namevalue) {
5772 ret = cacl_set(talloc_tos(), srv->cli,
5773 ipc_srv->cli, &pol, path,
5774 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5781 * Are they asking to set the group?
5783 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5784 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5788 talloc_asprintf(talloc_tos(), "%s:%s",
5789 name+19, (const char *) value);
5792 /* errno set by smbc_server() */
5795 else if (! namevalue) {
5799 ret = cacl_set(talloc_tos(), srv->cli,
5800 ipc_srv->cli, &pol, path,
5801 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5807 /* Determine whether to use old-style or new-style attribute names */
5808 if (context->internal->_full_time_names) {
5809 /* new-style names */
5810 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5811 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5812 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5813 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5815 /* old-style names */
5816 attr_strings.create_time_attr = NULL;
5817 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5818 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5819 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5823 * Are they asking to set a DOS attribute?
5825 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5826 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5827 (attr_strings.create_time_attr != NULL &&
5828 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5829 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5830 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5831 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5833 /* get a DOS Attribute Descriptor with current attributes */
5834 dad = dos_attr_query(context, talloc_tos(), path, srv);
5837 talloc_asprintf(talloc_tos(), "%s:%s",
5838 name+16, (const char *) value);
5843 /* Overwrite old with provided new params */
5844 dos_attr_parse(context, dad, srv, namevalue);
5846 /* Set the new DOS attributes */
5847 ret2 = smbc_setatr(context, srv, path,
5854 /* ret2 has True (success) / False (failure) */
5869 /* Unsupported attribute name */
5876 smbc_getxattr_ctx(SMBCCTX *context,
5893 const char * create_time_attr;
5894 const char * access_time_attr;
5895 const char * write_time_attr;
5896 const char * change_time_attr;
5898 TALLOC_CTX *frame = talloc_stackframe();
5900 if (!context || !context->internal ||
5901 !context->internal->_initialized) {
5902 errno = EINVAL; /* Best I can think of ... */
5913 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5915 if (smbc_parse_path(frame,
5930 if (!user || user[0] == (char)0) {
5931 user = talloc_strdup(frame, context->user);
5939 srv = smbc_server(context, True,
5940 server, share, workgroup, user, password);
5943 return -1; /* errno set by smbc_server */
5946 if (! srv->no_nt_session) {
5947 ipc_srv = smbc_attr_server(context, server, share,
5948 workgroup, user, password,
5951 srv->no_nt_session = True;
5957 /* Determine whether to use old-style or new-style attribute names */
5958 if (context->internal->_full_time_names) {
5959 /* new-style names */
5960 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5961 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5962 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5963 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5965 /* old-style names */
5966 attr_strings.create_time_attr = NULL;
5967 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5968 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5969 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5972 /* Are they requesting a supported attribute? */
5973 if (StrCaseCmp(name, "system.*") == 0 ||
5974 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5975 StrCaseCmp(name, "system.*+") == 0 ||
5976 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5977 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5978 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5979 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5980 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5981 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5982 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5983 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5984 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5985 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5986 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5987 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5988 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5989 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5990 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5991 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5992 (attr_strings.create_time_attr != NULL &&
5993 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5994 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5995 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5996 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5997 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
6000 ret = cacl_get(context, talloc_tos(), srv,
6001 ipc_srv == NULL ? NULL : ipc_srv->cli,
6003 CONST_DISCARD(char *, name),
6004 CONST_DISCARD(char *, value), size);
6005 if (ret < 0 && errno == 0) {
6006 errno = smbc_errno(context, srv->cli);
6012 /* Unsupported attribute name */
6020 smbc_removexattr_ctx(SMBCCTX *context,
6034 TALLOC_CTX *frame = talloc_stackframe();
6036 if (!context || !context->internal ||
6037 !context->internal->_initialized) {
6038 errno = EINVAL; /* Best I can think of ... */
6049 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
6051 if (smbc_parse_path(frame,
6066 if (!user || user[0] == (char)0) {
6067 user = talloc_strdup(frame, context->user);
6075 srv = smbc_server(context, True,
6076 server, share, workgroup, user, password);
6079 return -1; /* errno set by smbc_server */
6082 if (! srv->no_nt_session) {
6083 ipc_srv = smbc_attr_server(context, server, share,
6084 workgroup, user, password,
6087 srv->no_nt_session = True;
6095 return -1; /* errno set by smbc_attr_server */
6098 /* Are they asking to set the entire ACL? */
6099 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
6100 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
6103 ret = cacl_set(talloc_tos(), srv->cli,
6104 ipc_srv->cli, &pol, path,
6105 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
6111 * Are they asking to remove one or more spceific security descriptor
6114 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
6115 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
6116 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
6117 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
6118 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
6119 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
6120 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
6123 ret = cacl_set(talloc_tos(), srv->cli,
6124 ipc_srv->cli, &pol, path,
6125 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
6130 /* Unsupported attribute name */
6137 smbc_listxattr_ctx(SMBCCTX *context,
6143 * This isn't quite what listxattr() is supposed to do. This returns
6144 * the complete set of attribute names, always, rather than only those
6145 * attribute names which actually exist for a file. Hmmm...
6147 const char supported_old[] =
6150 "system.nt_sec_desc.revision\0"
6151 "system.nt_sec_desc.owner\0"
6152 "system.nt_sec_desc.owner+\0"
6153 "system.nt_sec_desc.group\0"
6154 "system.nt_sec_desc.group+\0"
6155 "system.nt_sec_desc.acl.*\0"
6156 "system.nt_sec_desc.acl\0"
6157 "system.nt_sec_desc.acl+\0"
6158 "system.nt_sec_desc.*\0"
6159 "system.nt_sec_desc.*+\0"
6160 "system.dos_attr.*\0"
6161 "system.dos_attr.mode\0"
6162 "system.dos_attr.c_time\0"
6163 "system.dos_attr.a_time\0"
6164 "system.dos_attr.m_time\0"
6166 const char supported_new[] =
6169 "system.nt_sec_desc.revision\0"
6170 "system.nt_sec_desc.owner\0"
6171 "system.nt_sec_desc.owner+\0"
6172 "system.nt_sec_desc.group\0"
6173 "system.nt_sec_desc.group+\0"
6174 "system.nt_sec_desc.acl.*\0"
6175 "system.nt_sec_desc.acl\0"
6176 "system.nt_sec_desc.acl+\0"
6177 "system.nt_sec_desc.*\0"
6178 "system.nt_sec_desc.*+\0"
6179 "system.dos_attr.*\0"
6180 "system.dos_attr.mode\0"
6181 "system.dos_attr.create_time\0"
6182 "system.dos_attr.access_time\0"
6183 "system.dos_attr.write_time\0"
6184 "system.dos_attr.change_time\0"
6186 const char * supported;
6188 if (context->internal->_full_time_names) {
6189 supported = supported_new;
6191 supported = supported_old;
6195 return sizeof(supported);
6198 if (sizeof(supported) > size) {
6203 /* this can't be strcpy() because there are embedded null characters */
6204 memcpy(list, supported, sizeof(supported));
6205 return sizeof(supported);
6210 * Open a print file to be written to by other calls
6214 smbc_open_print_job_ctx(SMBCCTX *context,
6222 TALLOC_CTX *frame = talloc_stackframe();
6224 if (!context || !context->internal ||
6225 !context->internal->_initialized) {
6237 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
6239 if (smbc_parse_path(frame,
6254 /* What if the path is empty, or the file exists? */
6257 return (context->open)(context, fname, O_WRONLY, 666);
6261 * Routine to print a file on a remote server ...
6263 * We open the file, which we assume to be on a remote server, and then
6264 * copy it to a print file on the share specified by printq.
6268 smbc_print_file_ctx(SMBCCTX *c_file,
6279 TALLOC_CTX *frame = talloc_stackframe();
6281 if (!c_file || !c_file->internal->_initialized || !c_print ||
6282 !c_print->internal->_initialized) {
6290 if (!fname && !printq) {
6298 /* Try to open the file for reading ... */
6300 if ((long)(fid1 = (c_file->open)(c_file, fname, O_RDONLY, 0666)) < 0) {
6301 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
6303 return -1; /* smbc_open sets errno */
6306 /* Now, try to open the printer file for writing */
6308 if ((long)(fid2 = (c_print->open_print_job)(c_print, printq)) < 0) {
6310 saverr = errno; /* Save errno */
6311 (c_file->close_fn)(c_file, fid1);
6318 while ((bytes = (c_file->read)(c_file, fid1, buf, sizeof(buf))) > 0) {
6322 if (((c_print->write)(c_print, fid2, buf, bytes)) < 0) {
6325 (c_file->close_fn)(c_file, fid1);
6326 (c_print->close_fn)(c_print, fid2);
6335 (c_file->close_fn)(c_file, fid1); /* We have to close these anyway */
6336 (c_print->close_fn)(c_print, fid2);
6352 * Routine to list print jobs on a printer share ...
6356 smbc_list_print_jobs_ctx(SMBCCTX *context,
6358 smbc_list_print_job_fn fn)
6367 TALLOC_CTX *frame = talloc_stackframe();
6369 if (!context || !context->internal ||
6370 !context->internal->_initialized) {
6382 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6384 if (smbc_parse_path(frame,
6399 if (!user || user[0] == (char)0) {
6400 user = talloc_strdup(frame, context->user);
6408 srv = smbc_server(context, True,
6409 server, share, workgroup, user, password);
6413 return -1; /* errno set by smbc_server */
6416 if (cli_print_queue(srv->cli,
6417 (void (*)(struct print_job_info *))fn) < 0) {
6418 errno = smbc_errno(context, srv->cli);
6429 * Delete a print job from a remote printer share
6433 smbc_unlink_print_job_ctx(SMBCCTX *context,
6445 TALLOC_CTX *frame = talloc_stackframe();
6447 if (!context || !context->internal ||
6448 !context->internal->_initialized) {
6460 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6462 if (smbc_parse_path(frame,
6477 if (!user || user[0] == (char)0) {
6478 user = talloc_strdup(frame, context->user);
6486 srv = smbc_server(context, True,
6487 server, share, workgroup, user, password);
6492 return -1; /* errno set by smbc_server */
6496 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6499 errno = smbc_errno(context, srv->cli);
6500 else if (err == ERRnosuchprintjob)
6513 * Get a new empty handle to fill in with your own info
6516 smbc_new_context(void)
6520 context = SMB_MALLOC_P(SMBCCTX);
6526 ZERO_STRUCTP(context);
6528 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6529 if (!context->internal) {
6535 ZERO_STRUCTP(context->internal);
6537 /* ADD REASONABLE DEFAULTS */
6539 context->timeout = 20000; /* 20 seconds */
6541 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6542 context->options.urlencode_readdir_entries = False;/* backward compat */
6543 context->options.one_share_per_server = False;/* backward compat */
6544 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6545 /* backward compat */
6547 context->open = smbc_open_ctx;
6548 context->creat = smbc_creat_ctx;
6549 context->read = smbc_read_ctx;
6550 context->write = smbc_write_ctx;
6551 context->close_fn = smbc_close_ctx;
6552 context->unlink = smbc_unlink_ctx;
6553 context->rename = smbc_rename_ctx;
6554 context->lseek = smbc_lseek_ctx;
6555 context->stat = smbc_stat_ctx;
6556 context->fstat = smbc_fstat_ctx;
6557 context->opendir = smbc_opendir_ctx;
6558 context->closedir = smbc_closedir_ctx;
6559 context->readdir = smbc_readdir_ctx;
6560 context->getdents = smbc_getdents_ctx;
6561 context->mkdir = smbc_mkdir_ctx;
6562 context->rmdir = smbc_rmdir_ctx;
6563 context->telldir = smbc_telldir_ctx;
6564 context->lseekdir = smbc_lseekdir_ctx;
6565 context->fstatdir = smbc_fstatdir_ctx;
6566 context->chmod = smbc_chmod_ctx;
6567 context->utimes = smbc_utimes_ctx;
6568 context->setxattr = smbc_setxattr_ctx;
6569 context->getxattr = smbc_getxattr_ctx;
6570 context->removexattr = smbc_removexattr_ctx;
6571 context->listxattr = smbc_listxattr_ctx;
6572 context->open_print_job = smbc_open_print_job_ctx;
6573 context->print_file = smbc_print_file_ctx;
6574 context->list_print_jobs = smbc_list_print_jobs_ctx;
6575 context->unlink_print_job = smbc_unlink_print_job_ctx;
6577 context->callbacks.check_server_fn = smbc_check_server;
6578 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6580 smbc_default_cache_functions(context);
6588 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6589 * and thus you'll be leaking memory if not handled properly.
6593 smbc_free_context(SMBCCTX *context,
6603 DEBUG(1,("Performing aggressive shutdown.\n"));
6605 f = context->internal->_files;
6607 (context->close_fn)(context, f);
6610 context->internal->_files = NULL;
6612 /* First try to remove the servers the nice way. */
6613 if (context->callbacks.purge_cached_fn(context)) {
6616 DEBUG(1, ("Could not purge all servers, "
6617 "Nice way shutdown failed.\n"));
6618 s = context->internal->_servers;
6620 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6622 cli_shutdown(s->cli);
6623 (context->callbacks.remove_cached_srv_fn)(context,
6626 DLIST_REMOVE(context->internal->_servers, s);
6630 context->internal->_servers = NULL;
6634 /* This is the polite way */
6635 if ((context->callbacks.purge_cached_fn)(context)) {
6636 DEBUG(1, ("Could not purge all servers, "
6637 "free_context failed.\n"));
6641 if (context->internal->_servers) {
6642 DEBUG(1, ("Active servers in context, "
6643 "free_context failed.\n"));
6647 if (context->internal->_files) {
6648 DEBUG(1, ("Active files in context, "
6649 "free_context failed.\n"));
6655 /* Things we have to clean up */
6656 SAFE_FREE(context->workgroup);
6657 SAFE_FREE(context->netbios_name);
6658 SAFE_FREE(context->user);
6660 DEBUG(3, ("Context %p succesfully freed\n", context));
6661 SAFE_FREE(context->internal);
6668 * Each time the context structure is changed, we have binary backward
6669 * compatibility issues. Instead of modifying the public portions of the
6670 * context structure to add new options, instead, we put them in the internal
6671 * portion of the context structure and provide a set function for these new
6675 smbc_option_set(SMBCCTX *context,
6677 ... /* option_value */)
6683 smbc_get_auth_data_with_context_fn auth_fn;
6687 va_start(ap, option_name);
6689 if (strcmp(option_name, "debug_to_stderr") == 0) {
6691 * Log to standard error instead of standard output.
6693 option_value.b = (bool) va_arg(ap, int);
6694 context->internal->_debug_stderr = option_value.b;
6696 } else if (strcmp(option_name, "full_time_names") == 0) {
6698 * Use new-style time attribute names, e.g. WRITE_TIME rather
6699 * than the old-style names such as M_TIME. This allows also
6700 * setting/getting CREATE_TIME which was previously
6701 * unimplemented. (Note that the old C_TIME was supposed to
6702 * be CHANGE_TIME but was confused and sometimes referred to
6705 option_value.b = (bool) va_arg(ap, int);
6706 context->internal->_full_time_names = option_value.b;
6708 } else if (strcmp(option_name, "open_share_mode") == 0) {
6710 * The share mode to use for files opened with
6711 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6713 option_value.i = va_arg(ap, int);
6714 context->internal->_share_mode =
6715 (smbc_share_mode) option_value.i;
6717 } else if (strcmp(option_name, "auth_function") == 0) {
6719 * Use the new-style authentication function which includes
6722 option_value.auth_fn =
6723 va_arg(ap, smbc_get_auth_data_with_context_fn);
6724 context->internal->_auth_fn_with_context =
6725 option_value.auth_fn;
6726 } else if (strcmp(option_name, "user_data") == 0) {
6728 * Save a user data handle which may be retrieved by the user
6729 * with smbc_option_get()
6731 option_value.v = va_arg(ap, void *);
6732 context->internal->_user_data = option_value.v;
6740 * Retrieve the current value of an option
6743 smbc_option_get(SMBCCTX *context,
6746 if (strcmp(option_name, "debug_stderr") == 0) {
6748 * Log to standard error instead of standard output.
6750 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6751 return (void *) (intptr_t) context->internal->_debug_stderr;
6753 return (void *) context->internal->_debug_stderr;
6755 } else if (strcmp(option_name, "full_time_names") == 0) {
6757 * Use new-style time attribute names, e.g. WRITE_TIME rather
6758 * than the old-style names such as M_TIME. This allows also
6759 * setting/getting CREATE_TIME which was previously
6760 * unimplemented. (Note that the old C_TIME was supposed to
6761 * be CHANGE_TIME but was confused and sometimes referred to
6764 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6765 return (void *) (intptr_t) context->internal->_full_time_names;
6767 return (void *) context->internal->_full_time_names;
6770 } else if (strcmp(option_name, "auth_function") == 0) {
6772 * Use the new-style authentication function which includes
6775 return (void *) context->internal->_auth_fn_with_context;
6776 } else if (strcmp(option_name, "user_data") == 0) {
6778 * Save a user data handle which may be retrieved by the user
6779 * with smbc_option_get()
6781 return context->internal->_user_data;
6789 * Initialise the library etc
6791 * We accept a struct containing handle information.
6792 * valid values for info->debug from 0 to 100,
6793 * and insist that info->fn must be non-null.
6796 smbc_init_context(SMBCCTX *context)
6802 if (!context || !context->internal) {
6807 /* Do not initialise the same client twice */
6808 if (context->internal->_initialized) {
6812 if ((!context->callbacks.auth_fn &&
6813 !context->internal->_auth_fn_with_context) ||
6814 context->debug < 0 ||
6815 context->debug > 100) {
6822 if (!smbc_initialized) {
6824 * Do some library-wide intializations the first time we get
6827 bool conf_loaded = False;
6829 /* Set this to what the user wants */
6830 DEBUGLEVEL = context->debug;
6834 setup_logging("libsmbclient", True);
6835 if (context->internal->_debug_stderr) {
6837 x_setbuf(x_stderr, NULL);
6840 /* Here we would open the smb.conf file if needed ... */
6842 in_client = True; /* FIXME, make a param */
6844 home = getenv("HOME");
6847 if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
6848 if (lp_load(conf, True, False, False, True)) {
6851 DEBUG(5, ("Could not load config file: %s\n",
6860 * Well, if that failed, try the dyn_CONFIGFILE
6861 * Which points to the standard locn, and if that
6862 * fails, silently ignore it and use the internal
6866 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6867 DEBUG(5, ("Could not load config file: %s\n",
6872 * We loaded the global config file. Now lets
6873 * load user-specific modifications to the
6877 "%s/.smb/smb.conf.append",
6879 if (!lp_load(conf, True, False, False, False)) {
6881 ("Could not append config file: "
6890 load_interfaces(); /* Load the list of interfaces ... */
6892 reopen_logs(); /* Get logging working ... */
6895 * Block SIGPIPE (from lib/util_sock.c: write())
6896 * It is not needed and should not stop execution
6898 BlockSignals(True, SIGPIPE);
6900 /* Done with one-time initialisation */
6901 smbc_initialized = 1;
6905 if (!context->user) {
6907 * FIXME: Is this the best way to get the user info?
6909 user = getenv("USER");
6910 /* walk around as "guest" if no username can be found */
6911 if (!user) context->user = SMB_STRDUP("guest");
6912 else context->user = SMB_STRDUP(user);
6915 if (!context->netbios_name) {
6917 * We try to get our netbios name from the config. If that
6918 * fails we fall back on constructing our netbios name from
6921 if (global_myname()) {
6922 context->netbios_name = SMB_STRDUP(global_myname());
6926 * Hmmm, I want to get hostname as well, but I am too
6927 * lazy for the moment
6930 context->netbios_name = (char *)SMB_MALLOC(17);
6931 if (!context->netbios_name) {
6935 slprintf(context->netbios_name, 16,
6936 "smbc%s%d", context->user, pid);
6940 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6942 if (!context->workgroup) {
6943 if (lp_workgroup()) {
6944 context->workgroup = SMB_STRDUP(lp_workgroup());
6947 /* TODO: Think about a decent default workgroup */
6948 context->workgroup = SMB_STRDUP("samba");
6952 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6954 /* shortest timeout is 1 second */
6955 if (context->timeout > 0 && context->timeout < 1000)
6956 context->timeout = 1000;
6959 * FIXME: Should we check the function pointers here?
6962 context->internal->_initialized = True;
6968 /* Return the verion of samba, and thus libsmbclient */
6972 return samba_version_string();