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);
154 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);
595 find_server(SMBCCTX *context,
607 srv = (context->callbacks.get_cached_srv_fn)(context, server, share,
608 workgroup, username);
610 if (!auth_called && !srv && (!username[0] || !password[0])) {
611 if (context->internal->_auth_fn_with_context != NULL) {
612 (context->internal->_auth_fn_with_context)(
615 workgroup, strlen(workgroup)+1,
616 username, strlen(username)+1,
617 password, strlen(password)+1);
619 (context->callbacks.auth_fn)(
621 workgroup, strlen(workgroup)+1,
622 username, strlen(username)+1,
623 password, strlen(password)+1);
627 * However, smbc_auth_fn may have picked up info relating to
628 * an existing connection, so try for an existing connection
632 goto check_server_cache;
637 if ((context->callbacks.check_server_fn)(context, srv)) {
639 * This server is no good anymore
640 * Try to remove it and check for more possible
641 * servers in the cache
643 if ((context->callbacks.remove_unused_server_fn)(context,
646 * We could not remove the server completely,
647 * remove it from the cache so we will not get
648 * it again. It will be removed when the last
649 * file/dir is closed.
651 (context->callbacks.remove_cached_srv_fn)(context,
656 * Maybe there are more cached connections to this
659 goto check_server_cache;
669 * Connect to a server, possibly on an existing connection
671 * Here, what we want to do is: If the server and username
672 * match an existing connection, reuse that, otherwise, establish a
675 * If we have to create a new connection, call the auth_fn to get the
676 * info we need, unless the username and password were passed in.
680 smbc_server(SMBCCTX *context,
681 bool connect_if_not_found,
690 struct nmb_name called, calling;
691 const char *server_n = server;
692 struct sockaddr_storage ss;
693 int tried_reverse = 0;
696 const char *username_used;
702 if (server[0] == 0) {
707 /* Look for a cached connection */
708 srv = find_server(context, server, share,
709 workgroup, username, password);
712 * If we found a connection and we're only allowed one share per
715 if (srv && *share != '\0' && context->options.one_share_per_server) {
718 * ... then if there's no current connection to the share,
719 * connect to it. find_server(), or rather the function
720 * pointed to by context->callbacks.get_cached_srv_fn which
721 * was called by find_server(), will have issued a tree
722 * disconnect if the requested share is not the same as the
723 * one that was already connected.
725 if (srv->cli->cnum == (uint16) -1) {
726 /* Ensure we have accurate auth info */
727 if (context->internal->_auth_fn_with_context != NULL) {
728 (context->internal->_auth_fn_with_context)(
731 workgroup, strlen(workgroup)+1,
732 username, strlen(username)+1,
733 password, strlen(password)+1);
735 (context->callbacks.auth_fn)(
737 workgroup, strlen(workgroup)+1,
738 username, strlen(username)+1,
739 password, strlen(password)+1);
742 if (! cli_send_tconX(srv->cli, share, "?????",
743 password, strlen(password)+1)) {
745 errno = smbc_errno(context, srv->cli);
746 cli_shutdown(srv->cli);
748 (context->callbacks.remove_cached_srv_fn)(context,
754 * Regenerate the dev value since it's based on both
758 srv->dev = (dev_t)(str_checksum(server) ^
759 str_checksum(share));
764 /* If we have a connection... */
767 /* ... then we're done here. Give 'em what they came for. */
771 /* If we're not asked to connect when a connection doesn't exist... */
772 if (! connect_if_not_found) {
773 /* ... then we're done here. */
777 make_nmb_name(&calling, context->netbios_name, 0x0);
778 make_nmb_name(&called , server, 0x20);
780 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
782 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
788 /* have to open a new connection */
789 if ((c = cli_initialise()) == NULL) {
794 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
795 c->use_kerberos = True;
797 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
798 c->fallback_after_kerberos = True;
801 c->timeout = context->timeout;
804 * Force use of port 139 for first try if share is $IPC, empty, or
805 * null, so browse lists can work
807 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
808 port_try_first = 139;
811 port_try_first = 445;
815 c->port = port_try_first;
817 status = cli_connect(c, server_n, &ss);
818 if (!NT_STATUS_IS_OK(status)) {
820 /* First connection attempt failed. Try alternate port. */
821 c->port = port_try_next;
823 status = cli_connect(c, server_n, &ss);
824 if (!NT_STATUS_IS_OK(status)) {
831 if (!cli_session_request(c, &calling, &called)) {
833 if (strcmp(called.name, "*SMBSERVER")) {
834 make_nmb_name(&called , "*SMBSERVER", 0x20);
836 } else { /* Try one more time, but ensure we don't loop */
838 /* Only try this if server is an IP address ... */
840 if (is_ipaddress(server) && !tried_reverse) {
842 struct sockaddr_storage rem_ss;
844 if (!interpret_string_addr(&rem_ss, server,
846 DEBUG(4, ("Could not convert IP address "
847 "%s to struct sockaddr_storage\n",
853 tried_reverse++; /* Yuck */
855 if (name_status_find("*", 0, 0, &rem_ss, remote_name)) {
856 make_nmb_name(&called, remote_name, 0x20);
865 DEBUG(4,(" session request ok\n"));
867 if (!cli_negprot(c)) {
873 username_used = username;
875 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
876 password, strlen(password),
877 password, strlen(password),
880 /* Failed. Try an anonymous login, if allowed by flags. */
883 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
884 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
895 DEBUG(4,(" session setup ok\n"));
897 if (!cli_send_tconX(c, share, "?????",
898 password, strlen(password)+1)) {
899 errno = smbc_errno(context, c);
904 DEBUG(4,(" tconx ok\n"));
907 * Ok, we have got a nice connection
908 * Let's allocate a server structure.
911 srv = SMB_MALLOC_P(SMBCSRV);
919 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
920 srv->no_pathinfo = False;
921 srv->no_pathinfo2 = False;
922 srv->no_nt_session = False;
924 /* now add it to the cache (internal or external) */
925 /* Let the cache function set errno if it wants to */
927 if ((context->callbacks.add_cached_srv_fn)(context, srv,
929 workgroup, username)) {
930 int saved_errno = errno;
931 DEBUG(3, (" Failed to add server to cache\n"));
939 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
940 server, share, srv));
942 DLIST_ADD(context->internal->_servers, srv);
956 * Connect to a server for getting/setting attributes, possibly on an existing
957 * connection. This works similarly to smbc_server().
960 smbc_attr_server(SMBCCTX *context,
969 struct sockaddr_storage ss;
970 struct cli_state *ipc_cli;
971 struct rpc_pipe_client *pipe_hnd;
973 SMBCSRV *ipc_srv=NULL;
976 * See if we've already created this special connection. Reference
977 * our "special" share name '*IPC$', which is an impossible real share
978 * name due to the leading asterisk.
980 ipc_srv = find_server(context, server, "*IPC$",
981 workgroup, username, password);
984 /* We didn't find a cached connection. Get the password */
985 if (*password == '\0') {
986 /* ... then retrieve it now. */
987 if (context->internal->_auth_fn_with_context != NULL) {
988 (context->internal->_auth_fn_with_context)(
991 workgroup, strlen(workgroup)+1,
992 username, strlen(username)+1,
993 password, strlen(password)+1);
995 (context->callbacks.auth_fn)(
997 workgroup, strlen(workgroup)+1,
998 username, strlen(username)+1,
999 password, strlen(password)+1);
1004 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
1005 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1009 nt_status = cli_full_connection(&ipc_cli,
1010 global_myname(), server,
1011 &ss, 0, "IPC$", "?????",
1012 username, workgroup,
1015 if (! NT_STATUS_IS_OK(nt_status)) {
1016 DEBUG(1,("cli_full_connection failed! (%s)\n",
1017 nt_errstr(nt_status)));
1022 ipc_srv = SMB_MALLOC_P(SMBCSRV);
1025 cli_shutdown(ipc_cli);
1029 ZERO_STRUCTP(ipc_srv);
1030 ipc_srv->cli = ipc_cli;
1033 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1037 DEBUG(1, ("cli_nt_session_open fail!\n"));
1039 cli_shutdown(ipc_srv->cli);
1045 * Some systems don't support
1046 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1047 * so we might as well do it too.
1050 nt_status = rpccli_lsa_open_policy(
1054 GENERIC_EXECUTE_ACCESS,
1057 if (!NT_STATUS_IS_OK(nt_status)) {
1058 errno = smbc_errno(context, ipc_srv->cli);
1059 cli_shutdown(ipc_srv->cli);
1064 /* now add it to the cache (internal or external) */
1066 errno = 0; /* let cache function set errno if it likes */
1067 if ((context->callbacks.add_cached_srv_fn)(context, ipc_srv,
1072 DEBUG(3, (" Failed to add server to cache\n"));
1076 cli_shutdown(ipc_srv->cli);
1081 DLIST_ADD(context->internal->_servers, ipc_srv);
1088 * Routine to open() a file ...
1092 smbc_open_ctx(SMBCCTX *context,
1097 char *server, *share, *user, *password, *workgroup;
1099 char *targetpath = NULL;
1100 struct cli_state *targetcli;
1101 SMBCSRV *srv = NULL;
1102 SMBCFILE *file = NULL;
1104 TALLOC_CTX *frame = talloc_stackframe();
1106 if (!context || !context->internal ||
1107 !context->internal->_initialized) {
1109 errno = EINVAL; /* Best I can think of ... */
1123 if (smbc_parse_path(frame,
1138 if (!user || user[0] == (char)0) {
1139 user = talloc_strdup(frame, context->user);
1147 srv = smbc_server(context, True,
1148 server, share, workgroup, user, password);
1151 if (errno == EPERM) errno = EACCES;
1153 return NULL; /* smbc_server sets errno */
1156 /* Hmmm, the test for a directory is suspect here ... FIXME */
1158 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1161 file = SMB_MALLOC_P(SMBCFILE);
1171 /*d_printf(">>>open: resolving %s\n", path);*/
1172 if (!cli_resolve_path(frame, "", srv->cli, path, &targetcli, &targetpath)) {
1173 d_printf("Could not resolve %s\n", path);
1178 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1180 if ((fd = cli_open(targetcli, targetpath, flags,
1181 context->internal->_share_mode)) < 0) {
1183 /* Handle the error ... */
1186 errno = smbc_errno(context, targetcli);
1192 /* Fill in file struct */
1195 file->fname = SMB_STRDUP(fname);
1200 DLIST_ADD(context->internal->_files, file);
1203 * If the file was opened in O_APPEND mode, all write
1204 * operations should be appended to the file. To do that,
1205 * though, using this protocol, would require a getattrE()
1206 * call for each and every write, to determine where the end
1207 * of the file is. (There does not appear to be an append flag
1208 * in the protocol.) Rather than add all of that overhead of
1209 * retrieving the current end-of-file offset prior to each
1210 * write operation, we'll assume that most append operations
1211 * will continuously write, so we'll just set the offset to
1212 * the end of the file now and hope that's adequate.
1214 * Note to self: If this proves inadequate, and O_APPEND
1215 * should, in some cases, be forced for each write, add a
1216 * field in the context options structure, for
1217 * "strict_append_mode" which would select between the current
1218 * behavior (if FALSE) or issuing a getattrE() prior to each
1219 * write and forcing the write to the end of the file (if
1220 * TRUE). Adding that capability will likely require adding
1221 * an "append" flag into the _SMBCFILE structure to track
1222 * whether a file was opened in O_APPEND mode. -- djl
1224 if (flags & O_APPEND) {
1225 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1226 (void) smbc_close_ctx(context, file);
1238 /* Check if opendir needed ... */
1243 eno = smbc_errno(context, srv->cli);
1244 file = (context->opendir)(context, fname);
1245 if (!file) errno = eno;
1251 errno = EINVAL; /* FIXME, correct errno ? */
1258 * Routine to create a file
1261 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1264 smbc_creat_ctx(SMBCCTX *context,
1269 if (!context || !context->internal ||
1270 !context->internal->_initialized) {
1277 return smbc_open_ctx(context, path, creat_bits, mode);
1281 * Routine to read() a file ...
1285 smbc_read_ctx(SMBCCTX *context,
1291 char *server, *share, *user, *password;
1293 char *targetpath = NULL;
1294 struct cli_state *targetcli;
1295 TALLOC_CTX *frame = talloc_stackframe();
1300 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1301 * appears to pass file->offset (which is type off_t) differently than
1302 * a local variable of type off_t. Using local variable "offset" in
1303 * the call to cli_read() instead of file->offset fixes a problem
1304 * retrieving data at an offset greater than 4GB.
1308 if (!context || !context->internal ||
1309 !context->internal->_initialized) {
1316 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1318 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1325 offset = file->offset;
1327 /* Check that the buffer exists ... */
1336 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1337 if (smbc_parse_path(frame,
1352 /*d_printf(">>>read: resolving %s\n", path);*/
1353 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1354 &targetcli, &targetpath)) {
1355 d_printf("Could not resolve %s\n", path);
1359 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1361 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1365 errno = smbc_errno(context, targetcli);
1371 file->offset += ret;
1373 DEBUG(4, (" --> %d\n", ret));
1376 return ret; /* Success, ret bytes of data ... */
1381 * Routine to write() a file ...
1385 smbc_write_ctx(SMBCCTX *context,
1392 char *server, *share, *user, *password;
1394 char *targetpath = NULL;
1395 struct cli_state *targetcli;
1396 TALLOC_CTX *frame = talloc_stackframe();
1398 /* First check all pointers before dereferencing them */
1400 if (!context || !context->internal ||
1401 !context->internal->_initialized) {
1408 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1414 /* Check that the buffer exists ... */
1423 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1425 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1426 if (smbc_parse_path(frame,
1441 /*d_printf(">>>write: resolving %s\n", path);*/
1442 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1443 &targetcli, &targetpath)) {
1444 d_printf("Could not resolve %s\n", path);
1448 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1450 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1453 errno = smbc_errno(context, targetcli);
1459 file->offset += ret;
1462 return ret; /* Success, 0 bytes of data ... */
1466 * Routine to close() a file ...
1470 smbc_close_ctx(SMBCCTX *context,
1474 char *server, *share, *user, *password;
1476 char *targetpath = NULL;
1477 struct cli_state *targetcli;
1478 TALLOC_CTX *frame = talloc_stackframe();
1480 if (!context || !context->internal ||
1481 !context->internal->_initialized) {
1488 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1497 return (context->closedir)(context, file);
1500 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1501 if (smbc_parse_path(frame,
1516 /*d_printf(">>>close: resolving %s\n", path);*/
1517 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1518 &targetcli, &targetpath)) {
1519 d_printf("Could not resolve %s\n", path);
1523 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1525 if (!cli_close(targetcli, file->cli_fd)) {
1527 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1529 /* Deallocate slot and remove the server
1530 * from the server cache if unused */
1531 errno = smbc_errno(context, targetcli);
1533 DLIST_REMOVE(context->internal->_files, file);
1534 SAFE_FREE(file->fname);
1536 (context->callbacks.remove_unused_server_fn)(context, srv);
1542 DLIST_REMOVE(context->internal->_files, file);
1543 SAFE_FREE(file->fname);
1551 * Get info from an SMB server on a file. Use a qpathinfo call first
1552 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1555 smbc_getatr(SMBCCTX * context,
1560 struct timespec *create_time_ts,
1561 struct timespec *access_time_ts,
1562 struct timespec *write_time_ts,
1563 struct timespec *change_time_ts,
1567 char *targetpath = NULL;
1568 struct cli_state *targetcli;
1570 TALLOC_CTX *frame = talloc_stackframe();
1572 if (!context || !context->internal ||
1573 !context->internal->_initialized) {
1579 /* path fixup for . and .. */
1580 if (strequal(path, ".") || strequal(path, "..")) {
1581 fixedpath = talloc_strdup(frame, "\\");
1588 fixedpath = talloc_strdup(frame, path);
1594 trim_string(fixedpath, NULL, "\\..");
1595 trim_string(fixedpath, NULL, "\\.");
1597 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1599 if (!cli_resolve_path(frame, "", srv->cli, fixedpath,
1600 &targetcli, &targetpath)) {
1601 d_printf("Couldn't resolve %s\n", path);
1606 if (!srv->no_pathinfo2 &&
1607 cli_qpathinfo2(targetcli, targetpath,
1617 /* if this is NT then don't bother with the getatr */
1618 if (targetcli->capabilities & CAP_NT_SMBS) {
1624 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1626 struct timespec w_time_ts;
1628 w_time_ts = convert_time_t_to_timespec(write_time);
1630 if (write_time_ts != NULL) {
1631 *write_time_ts = w_time_ts;
1634 if (create_time_ts != NULL) {
1635 *create_time_ts = w_time_ts;
1638 if (access_time_ts != NULL) {
1639 *access_time_ts = w_time_ts;
1642 if (change_time_ts != NULL) {
1643 *change_time_ts = w_time_ts;
1646 srv->no_pathinfo2 = True;
1658 * Set file info on an SMB server. Use setpathinfo call first. If that
1659 * fails, use setattrE..
1661 * Access and modification time parameters are always used and must be
1662 * provided. Create time, if zero, will be determined from the actual create
1663 * time of the file. If non-zero, the create time will be set as well.
1665 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1668 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1677 TALLOC_CTX *frame = talloc_stackframe();
1680 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1681 * modern function for "new code" to be using, and it works given a
1682 * filename rather than requiring that the file be opened to have its
1683 * attributes manipulated.
1685 if (srv->no_pathinfo ||
1686 ! cli_setpathinfo(srv->cli, path,
1694 * setpathinfo is not supported; go to plan B.
1696 * cli_setatr() does not work on win98, and it also doesn't
1697 * support setting the access time (only the modification
1698 * time), so in all cases, we open the specified file and use
1699 * cli_setattrE() which should work on all OS versions, and
1700 * supports both times.
1703 /* Don't try {q,set}pathinfo() again, with this server */
1704 srv->no_pathinfo = True;
1707 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1709 errno = smbc_errno(context, srv->cli);
1714 /* Set the new attributes */
1715 ret = cli_setattrE(srv->cli, fd,
1720 /* Close the file */
1721 cli_close(srv->cli, fd);
1724 * Unfortunately, setattrE() doesn't have a provision for
1725 * setting the access mode (attributes). We'll have to try
1726 * cli_setatr() for that, and with only this parameter, it
1727 * seems to work on win98.
1729 if (ret && mode != (uint16) -1) {
1730 ret = cli_setatr(srv->cli, path, mode, 0);
1734 errno = smbc_errno(context, srv->cli);
1745 * Routine to unlink() a file
1749 smbc_unlink_ctx(SMBCCTX *context,
1752 char *server, *share, *user, *password, *workgroup;
1755 struct cli_state *targetcli;
1756 SMBCSRV *srv = NULL;
1757 TALLOC_CTX *frame = talloc_stackframe();
1759 if (!context || !context->internal ||
1760 !context->internal->_initialized) {
1761 errno = EINVAL; /* Best I can think of ... */
1774 if (smbc_parse_path(frame,
1789 if (!user || user[0] == (char)0) {
1790 user = talloc_strdup(frame, context->user);
1798 srv = smbc_server(context, True,
1799 server, share, workgroup, user, password);
1803 return -1; /* smbc_server sets errno */
1807 /*d_printf(">>>unlink: resolving %s\n", path);*/
1808 if (!cli_resolve_path(frame, "", srv->cli, path,
1809 &targetcli, &targetpath)) {
1810 d_printf("Could not resolve %s\n", path);
1814 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1816 if (!cli_unlink(targetcli, targetpath)) {
1818 errno = smbc_errno(context, targetcli);
1820 if (errno == EACCES) { /* Check if the file is a directory */
1825 struct timespec write_time_ts;
1826 struct timespec access_time_ts;
1827 struct timespec change_time_ts;
1830 if (!smbc_getatr(context, srv, path, &mode, &size,
1837 /* Hmmm, bad error ... What? */
1839 errno = smbc_errno(context, targetcli);
1846 if (IS_DOS_DIR(mode))
1849 errno = saverr; /* Restore this */
1860 return 0; /* Success ... */
1865 * Routine to rename() a file
1869 smbc_rename_ctx(SMBCCTX *ocontext,
1887 struct cli_state *targetcli1;
1888 struct cli_state *targetcli2;
1889 SMBCSRV *srv = NULL;
1890 TALLOC_CTX *frame = talloc_stackframe();
1892 if (!ocontext || !ncontext ||
1893 !ocontext->internal || !ncontext->internal ||
1894 !ocontext->internal->_initialized ||
1895 !ncontext->internal->_initialized) {
1896 errno = EINVAL; /* Best I can think of ... */
1901 if (!oname || !nname) {
1907 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1909 if (smbc_parse_path(frame,
1924 if (!user1 || user1[0] == (char)0) {
1925 user1 = talloc_strdup(frame, ocontext->user);
1933 if (smbc_parse_path(frame,
1948 if (!user2 || user2[0] == (char)0) {
1949 user2 = talloc_strdup(frame, ncontext->user);
1957 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1958 strcmp(user1, user2)) {
1959 /* Can't rename across file systems, or users?? */
1965 srv = smbc_server(ocontext, True,
1966 server1, share1, workgroup, user1, password1);
1973 /*d_printf(">>>rename: resolving %s\n", path1);*/
1974 if (!cli_resolve_path(frame, "", srv->cli, path1,
1975 &targetcli1, &targetpath1)) {
1976 d_printf("Could not resolve %s\n", path1);
1980 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1981 /*d_printf(">>>rename: resolving %s\n", path2);*/
1982 if (!cli_resolve_path(frame, "", srv->cli, path2,
1983 &targetcli2, &targetpath2)) {
1984 d_printf("Could not resolve %s\n", path2);
1988 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1990 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1991 strcmp(targetcli1->share, targetcli2->share))
1993 /* can't rename across file systems */
1999 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
2000 int eno = smbc_errno(ocontext, targetcli1);
2002 if (eno != EEXIST ||
2003 !cli_unlink(targetcli1, targetpath2) ||
2004 !cli_rename(targetcli1, targetpath1, targetpath2)) {
2014 return 0; /* Success */
2018 * A routine to lseek() a file
2022 smbc_lseek_ctx(SMBCCTX *context,
2028 char *server, *share, *user, *password;
2031 struct cli_state *targetcli;
2032 TALLOC_CTX *frame = talloc_stackframe();
2034 if (!context || !context->internal ||
2035 !context->internal->_initialized) {
2041 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2053 return -1; /* Can't lseek a dir ... */
2059 file->offset = offset;
2063 file->offset += offset;
2067 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
2068 if (smbc_parse_path(frame,
2083 /*d_printf(">>>lseek: resolving %s\n", path);*/
2084 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2085 &targetcli, &targetpath)) {
2086 d_printf("Could not resolve %s\n", path);
2090 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
2092 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
2093 &size, NULL, NULL, NULL, NULL, NULL))
2095 SMB_OFF_T b_size = size;
2096 if (!cli_getattrE(targetcli, file->cli_fd,
2097 NULL, &b_size, NULL, NULL, NULL))
2105 file->offset = size + offset;
2115 return file->offset;
2120 * Generate an inode number from file name for those things that need it
2124 smbc_inode(SMBCCTX *context,
2127 if (!context || !context->internal ||
2128 !context->internal->_initialized) {
2135 if (!*name) return 2; /* FIXME, why 2 ??? */
2136 return (ino_t)str_checksum(name);
2141 * Routine to put basic stat info into a stat structure ... Used by stat and
2146 smbc_setup_stat(SMBCCTX *context,
2152 TALLOC_CTX *frame = talloc_stackframe();
2156 if (IS_DOS_DIR(mode)) {
2157 st->st_mode = SMBC_DIR_MODE;
2159 st->st_mode = SMBC_FILE_MODE;
2162 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2163 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2164 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2165 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2168 #ifdef HAVE_STAT_ST_BLKSIZE
2169 st->st_blksize = 512;
2171 #ifdef HAVE_STAT_ST_BLOCKS
2172 st->st_blocks = (size+511)/512;
2174 st->st_uid = getuid();
2175 st->st_gid = getgid();
2177 if (IS_DOS_DIR(mode)) {
2183 if (st->st_ino == 0) {
2184 st->st_ino = smbc_inode(context, fname);
2188 return True; /* FIXME: Is this needed ? */
2193 * Routine to stat a file given a name
2197 smbc_stat_ctx(SMBCCTX *context,
2208 struct timespec write_time_ts;
2209 struct timespec access_time_ts;
2210 struct timespec change_time_ts;
2214 TALLOC_CTX *frame = talloc_stackframe();
2216 if (!context || !context->internal ||
2217 !context->internal->_initialized) {
2219 errno = EINVAL; /* Best I can think of ... */
2230 DEBUG(4, ("smbc_stat(%s)\n", fname));
2232 if (smbc_parse_path(frame,
2247 if (!user || user[0] == (char)0) {
2248 user = talloc_strdup(frame,context->user);
2256 srv = smbc_server(context, True,
2257 server, share, workgroup, user, password);
2261 return -1; /* errno set by smbc_server */
2264 if (!smbc_getatr(context, srv, path, &mode, &size,
2270 errno = smbc_errno(context, srv->cli);
2277 smbc_setup_stat(context, st, path, size, mode);
2279 set_atimespec(st, access_time_ts);
2280 set_ctimespec(st, change_time_ts);
2281 set_mtimespec(st, write_time_ts);
2282 st->st_dev = srv->dev;
2290 * Routine to stat a file given an fd
2294 smbc_fstat_ctx(SMBCCTX *context,
2298 struct timespec change_time_ts;
2299 struct timespec access_time_ts;
2300 struct timespec write_time_ts;
2309 struct cli_state *targetcli;
2311 TALLOC_CTX *frame = talloc_stackframe();
2313 if (!context || !context->internal ||
2314 !context->internal->_initialized) {
2320 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2328 return (context->fstatdir)(context, file, st);
2331 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2332 if (smbc_parse_path(frame,
2347 /*d_printf(">>>fstat: resolving %s\n", path);*/
2348 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2349 &targetcli, &targetpath)) {
2350 d_printf("Could not resolve %s\n", path);
2354 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2356 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2363 time_t change_time, access_time, write_time;
2365 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2366 &change_time, &access_time, &write_time)) {
2373 change_time_ts = convert_time_t_to_timespec(change_time);
2374 access_time_ts = convert_time_t_to_timespec(access_time);
2375 write_time_ts = convert_time_t_to_timespec(write_time);
2380 smbc_setup_stat(context, st, file->fname, size, mode);
2382 set_atimespec(st, access_time_ts);
2383 set_ctimespec(st, change_time_ts);
2384 set_mtimespec(st, write_time_ts);
2385 st->st_dev = file->srv->dev;
2393 * Routine to open a directory
2394 * We accept the URL syntax explained in smbc_parse_path(), above.
2398 smbc_remove_dir(SMBCFILE *dir)
2400 struct smbc_dir_list *d,*f;
2407 SAFE_FREE(f->dirent);
2412 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2417 add_dirent(SMBCFILE *dir,
2419 const char *comment,
2422 struct smbc_dirent *dirent;
2424 int name_length = (name == NULL ? 0 : strlen(name));
2425 int comment_len = (comment == NULL ? 0 : strlen(comment));
2428 * Allocate space for the dirent, which must be increased by the
2429 * size of the name and the comment and 1 each for the null terminator.
2432 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2434 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2438 dir->dir_error = ENOMEM;
2443 ZERO_STRUCTP(dirent);
2445 if (dir->dir_list == NULL) {
2447 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2448 if (!dir->dir_list) {
2451 dir->dir_error = ENOMEM;
2455 ZERO_STRUCTP(dir->dir_list);
2457 dir->dir_end = dir->dir_next = dir->dir_list;
2461 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2463 if (!dir->dir_end->next) {
2466 dir->dir_error = ENOMEM;
2470 ZERO_STRUCTP(dir->dir_end->next);
2472 dir->dir_end = dir->dir_end->next;
2475 dir->dir_end->next = NULL;
2476 dir->dir_end->dirent = dirent;
2478 dirent->smbc_type = type;
2479 dirent->namelen = name_length;
2480 dirent->commentlen = comment_len;
2481 dirent->dirlen = size;
2484 * dirent->namelen + 1 includes the null (no null termination needed)
2485 * Ditto for dirent->commentlen.
2486 * The space for the two null bytes was allocated.
2488 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2489 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2490 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2497 list_unique_wg_fn(const char *name,
2499 const char *comment,
2502 SMBCFILE *dir = (SMBCFILE *)state;
2503 struct smbc_dir_list *dir_list;
2504 struct smbc_dirent *dirent;
2508 dirent_type = dir->dir_type;
2510 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2512 /* An error occurred, what do we do? */
2513 /* FIXME: Add some code here */
2516 /* Point to the one just added */
2517 dirent = dir->dir_end->dirent;
2519 /* See if this was a duplicate */
2520 for (dir_list = dir->dir_list;
2521 dir_list != dir->dir_end;
2522 dir_list = dir_list->next) {
2524 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2525 /* Duplicate. End end of list need to be removed. */
2529 if (do_remove && dir_list->next == dir->dir_end) {
2530 /* Found the end of the list. Remove it. */
2531 dir->dir_end = dir_list;
2532 free(dir_list->next);
2534 dir_list->next = NULL;
2541 list_fn(const char *name,
2543 const char *comment,
2546 SMBCFILE *dir = (SMBCFILE *)state;
2550 * We need to process the type a little ...
2552 * Disk share = 0x00000000
2553 * Print share = 0x00000001
2554 * Comms share = 0x00000002 (obsolete?)
2555 * IPC$ share = 0x00000003
2557 * administrative shares:
2558 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2561 if (dir->dir_type == SMBC_FILE_SHARE) {
2563 case 0 | 0x80000000:
2565 dirent_type = SMBC_FILE_SHARE;
2569 dirent_type = SMBC_PRINTER_SHARE;
2573 dirent_type = SMBC_COMMS_SHARE;
2576 case 3 | 0x80000000:
2578 dirent_type = SMBC_IPC_SHARE;
2582 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2587 dirent_type = dir->dir_type;
2590 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2592 /* An error occurred, what do we do? */
2593 /* FIXME: Add some code here */
2599 dir_list_fn(const char *mnt,
2605 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2606 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2608 /* Handle an error ... */
2610 /* FIXME: Add some code ... */
2617 net_share_enum_rpc(struct cli_state *cli,
2618 void (*fn)(const char *name,
2620 const char *comment,
2627 uint32 info_level = 1;
2628 uint32 preferred_len = 0xffffffff;
2630 SRV_SHARE_INFO_CTR ctr;
2632 fstring comment = "";
2633 struct rpc_pipe_client *pipe_hnd;
2636 /* Open the server service pipe */
2637 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2639 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2643 /* Issue the NetShareEnum RPC call and retrieve the response */
2644 init_enum_hnd(&enum_hnd, 0);
2645 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2652 /* Was it successful? */
2653 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2654 /* Nope. Go clean up. */
2658 /* For each returned entry... */
2659 for (i = 0; i < ctr.num_entries; i++) {
2661 /* pull out the share name */
2662 rpcstr_pull_unistr2_fstring(
2663 name, &ctr.share.info1[i].info_1_str.uni_netname);
2665 /* pull out the share's comment */
2666 rpcstr_pull_unistr2_fstring(
2667 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2669 /* Get the type value */
2670 type = ctr.share.info1[i].info_1.type;
2672 /* Add this share to the list */
2673 (*fn)(name, type, comment, state);
2677 /* Close the server service pipe */
2678 cli_rpc_pipe_close(pipe_hnd);
2680 /* Tell 'em if it worked */
2681 return W_ERROR_IS_OK(result) ? 0 : -1;
2687 smbc_opendir_ctx(SMBCCTX *context,
2691 char *server, *share, *user, *password, *options;
2696 SMBCSRV *srv = NULL;
2697 SMBCFILE *dir = NULL;
2698 struct _smbc_callbacks *cb;
2699 struct sockaddr_storage rem_ss;
2700 TALLOC_CTX *frame = talloc_stackframe();
2702 if (!context || !context->internal ||
2703 !context->internal->_initialized) {
2704 DEBUG(4, ("no valid context\n"));
2705 errno = EINVAL + 8192;
2712 DEBUG(4, ("no valid fname\n"));
2713 errno = EINVAL + 8193;
2718 if (smbc_parse_path(frame,
2728 DEBUG(4, ("no valid path\n"));
2729 errno = EINVAL + 8194;
2734 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2735 "path='%s' options='%s'\n",
2736 fname, server, share, path, options));
2738 /* Ensure the options are valid */
2739 if (smbc_check_options(server, share, path, options)) {
2740 DEBUG(4, ("unacceptable options (%s)\n", options));
2741 errno = EINVAL + 8195;
2746 if (!user || user[0] == (char)0) {
2747 user = talloc_strdup(frame, context->user);
2755 dir = SMB_MALLOC_P(SMBCFILE);
2766 dir->fname = SMB_STRDUP(fname);
2770 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2772 if (server[0] == (char)0) {
2777 struct ip_service *ip_list;
2778 struct ip_service server_addr;
2779 struct user_auth_info u_info;
2781 if (share[0] != (char)0 || path[0] != (char)0) {
2783 errno = EINVAL + 8196;
2785 SAFE_FREE(dir->fname);
2792 /* Determine how many local master browsers to query */
2793 max_lmb_count = (context->options.browse_max_lmb_count == 0
2795 : context->options.browse_max_lmb_count);
2797 pstrcpy(u_info.username, user);
2798 pstrcpy(u_info.password, password);
2801 * We have server and share and path empty but options
2802 * requesting that we scan all master browsers for their list
2803 * of workgroups/domains. This implies that we must first try
2804 * broadcast queries to find all master browsers, and if that
2805 * doesn't work, then try our other methods which return only
2806 * a single master browser.
2810 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
2816 if (!find_master_ip(workgroup, &server_addr.ss)) {
2819 SAFE_FREE(dir->fname);
2827 ip_list = (struct ip_service *)memdup(
2828 &server_addr, sizeof(server_addr));
2829 if (ip_list == NULL) {
2837 for (i = 0; i < count && i < max_lmb_count; i++) {
2838 char addr[INET6_ADDRSTRLEN];
2839 char *wg_ptr = NULL;
2840 struct cli_state *cli = NULL;
2842 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
2843 DEBUG(99, ("Found master browser %d of %d: %s\n",
2844 i+1, MAX(count, max_lmb_count),
2847 cli = get_ipc_connect_master_ip(talloc_tos(),
2851 /* cli == NULL is the master browser refused to talk or
2852 could not be found */
2857 workgroup = talloc_strdup(frame, wg_ptr);
2858 server = talloc_strdup(frame, cli->desthost);
2862 if (!workgroup || !server) {
2868 DEBUG(4, ("using workgroup %s %s\n",
2869 workgroup, server));
2872 * For each returned master browser IP address, get a
2873 * connection to IPC$ on the server if we do not
2874 * already have one, and determine the
2875 * workgroups/domains that it knows about.
2878 srv = smbc_server(context, True, server, "IPC$",
2879 workgroup, user, password);
2885 dir->dir_type = SMBC_WORKGROUP;
2887 /* Now, list the stuff ... */
2889 if (!cli_NetServerEnum(srv->cli,
2891 SV_TYPE_DOMAIN_ENUM,
2901 * Server not an empty string ... Check the rest and see what
2904 if (*share == '\0') {
2905 if (*path != '\0') {
2907 /* Should not have empty share with path */
2908 errno = EINVAL + 8197;
2910 SAFE_FREE(dir->fname);
2919 * We don't know if <server> is really a server name
2920 * or is a workgroup/domain name. If we already have
2921 * a server structure for it, we'll use it.
2922 * Otherwise, check to see if <server><1D>,
2923 * <server><1B>, or <server><20> translates. We check
2924 * to see if <server> is an IP address first.
2928 * See if we have an existing server. Do not
2929 * establish a connection if one does not already
2932 srv = smbc_server(context, False, server, "IPC$",
2933 workgroup, user, password);
2936 * If no existing server and not an IP addr, look for
2940 !is_ipaddress(server) &&
2941 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
2942 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
2946 dir->dir_type = SMBC_SERVER;
2949 * Get the backup list ...
2951 if (!name_status_find(server, 0, 0,
2952 &rem_ss, buserver)) {
2954 DEBUG(0, ("Could not get name of "
2955 "local/domain master browser "
2956 "for server %s\n", server));
2958 SAFE_FREE(dir->fname);
2968 * Get a connection to IPC$ on the server if
2969 * we do not already have one
2971 srv = smbc_server(context, True,
2973 workgroup, user, password);
2975 DEBUG(0, ("got no contact to IPC$\n"));
2977 SAFE_FREE(dir->fname);
2987 /* Now, list the servers ... */
2988 if (!cli_NetServerEnum(srv->cli, server,
2989 0x0000FFFE, list_fn,
2993 SAFE_FREE(dir->fname);
3000 (resolve_name(server, &rem_ss, 0x20))) {
3002 /* If we hadn't found the server, get one now */
3004 srv = smbc_server(context, True,
3012 SAFE_FREE(dir->fname);
3020 dir->dir_type = SMBC_FILE_SHARE;
3023 /* List the shares ... */
3025 if (net_share_enum_rpc(
3028 (void *) dir) < 0 &&
3034 errno = cli_errno(srv->cli);
3036 SAFE_FREE(dir->fname);
3044 /* Neither the workgroup nor server exists */
3045 errno = ECONNREFUSED;
3047 SAFE_FREE(dir->fname);
3057 * The server and share are specified ... work from
3061 struct cli_state *targetcli;
3063 /* We connect to the server and list the directory */
3064 dir->dir_type = SMBC_FILE_SHARE;
3066 srv = smbc_server(context, True, server, share,
3067 workgroup, user, password);
3071 SAFE_FREE(dir->fname);
3080 /* Now, list the files ... */
3082 p = path + strlen(path);
3083 path = talloc_asprintf_append(path, "\\*");
3086 SAFE_FREE(dir->fname);
3093 if (!cli_resolve_path(frame, "", srv->cli, path,
3094 &targetcli, &targetpath)) {
3095 d_printf("Could not resolve %s\n", path);
3097 SAFE_FREE(dir->fname);
3104 if (cli_list(targetcli, targetpath,
3105 aDIR | aSYSTEM | aHIDDEN,
3106 dir_list_fn, (void *)dir) < 0) {
3109 SAFE_FREE(dir->fname);
3112 saved_errno = smbc_errno(context, targetcli);
3114 if (saved_errno == EINVAL) {
3116 * See if they asked to opendir something
3117 * other than a directory. If so, the
3118 * converted error value we got would have
3119 * been EINVAL rather than ENOTDIR.
3121 *p = '\0'; /* restore original path */
3123 if (smbc_getatr(context, srv, path,
3125 NULL, NULL, NULL, NULL,
3127 ! IS_DOS_DIR(mode)) {
3129 /* It is. Correct the error value */
3130 saved_errno = ENOTDIR;
3135 * If there was an error and the server is no
3138 cb = &context->callbacks;
3139 if (cli_is_error(targetcli) &&
3140 (cb->check_server_fn)(context, srv)) {
3142 /* ... then remove it. */
3143 if ((cb->remove_unused_server_fn)(context,
3146 * We could not remove the
3147 * server completely, remove
3148 * it from the cache so we
3149 * will not get it again. It
3150 * will be removed when the
3151 * last file/dir is closed.
3153 (cb->remove_cached_srv_fn)(context,
3158 errno = saved_errno;
3166 DLIST_ADD(context->internal->_files, dir);
3173 * Routine to close a directory
3177 smbc_closedir_ctx(SMBCCTX *context,
3180 TALLOC_CTX *frame = talloc_stackframe();
3182 if (!context || !context->internal ||
3183 !context->internal->_initialized) {
3191 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3199 smbc_remove_dir(dir); /* Clean it up */
3201 DLIST_REMOVE(context->internal->_files, dir);
3205 SAFE_FREE(dir->fname);
3206 SAFE_FREE(dir); /* Free the space too */
3215 smbc_readdir_internal(SMBCCTX * context,
3216 struct smbc_dirent *dest,
3217 struct smbc_dirent *src,
3218 int max_namebuf_len)
3220 if (context->options.urlencode_readdir_entries) {
3222 /* url-encode the name. get back remaining buffer space */
3224 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3226 /* We now know the name length */
3227 dest->namelen = strlen(dest->name);
3229 /* Save the pointer to the beginning of the comment */
3230 dest->comment = dest->name + dest->namelen + 1;
3232 /* Copy the comment */
3233 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3234 dest->comment[max_namebuf_len - 1] = '\0';
3236 /* Save other fields */
3237 dest->smbc_type = src->smbc_type;
3238 dest->commentlen = strlen(dest->comment);
3239 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3243 /* No encoding. Just copy the entry as is. */
3244 memcpy(dest, src, src->dirlen);
3245 dest->comment = (char *)(&dest->name + src->namelen + 1);
3251 * Routine to get a directory entry
3254 struct smbc_dirent *
3255 smbc_readdir_ctx(SMBCCTX *context,
3259 struct smbc_dirent *dirp, *dirent;
3260 TALLOC_CTX *frame = talloc_stackframe();
3262 /* Check that all is ok first ... */
3264 if (!context || !context->internal ||
3265 !context->internal->_initialized) {
3268 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3274 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3277 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3283 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3286 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3292 if (!dir->dir_next) {
3297 dirent = dir->dir_next->dirent;
3306 dirp = (struct smbc_dirent *)context->internal->_dirent;
3307 maxlen = (sizeof(context->internal->_dirent) -
3308 sizeof(struct smbc_dirent));
3310 smbc_readdir_internal(context, dirp, dirent, maxlen);
3312 dir->dir_next = dir->dir_next->next;
3319 * Routine to get directory entries
3323 smbc_getdents_ctx(SMBCCTX *context,
3325 struct smbc_dirent *dirp,
3331 char *ndir = (char *)dirp;
3332 struct smbc_dir_list *dirlist;
3333 TALLOC_CTX *frame = talloc_stackframe();
3335 /* Check that all is ok first ... */
3337 if (!context || !context->internal ||
3338 !context->internal->_initialized) {
3346 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3354 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3363 * Now, retrieve the number of entries that will fit in what was passed
3364 * We have to figure out if the info is in the list, or we need to
3365 * send a request to the server to get the info.
3368 while ((dirlist = dir->dir_next)) {
3369 struct smbc_dirent *dirent;
3371 if (!dirlist->dirent) {
3373 errno = ENOENT; /* Bad error */
3379 /* Do urlencoding of next entry, if so selected */
3380 dirent = (struct smbc_dirent *)context->internal->_dirent;
3381 maxlen = (sizeof(context->internal->_dirent) -
3382 sizeof(struct smbc_dirent));
3383 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3385 reqd = dirent->dirlen;
3389 if (rem < count) { /* We managed to copy something */
3396 else { /* Nothing copied ... */
3398 errno = EINVAL; /* Not enough space ... */
3406 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3408 ((struct smbc_dirent *)ndir)->comment =
3409 (char *)(&((struct smbc_dirent *)ndir)->name +
3417 dir->dir_next = dirlist = dirlist -> next;
3430 * Routine to create a directory ...
3434 smbc_mkdir_ctx(SMBCCTX *context,
3446 struct cli_state *targetcli;
3447 TALLOC_CTX *frame = talloc_stackframe();
3449 if (!context || !context->internal ||
3450 !context->internal->_initialized) {
3462 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3464 if (smbc_parse_path(frame,
3479 if (!user || user[0] == (char)0) {
3480 user = talloc_strdup(frame, context->user);
3488 srv = smbc_server(context, True,
3489 server, share, workgroup, user, password);
3494 return -1; /* errno set by smbc_server */
3498 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3499 if (!cli_resolve_path(frame, "", srv->cli, path,
3500 &targetcli, &targetpath)) {
3501 d_printf("Could not resolve %s\n", path);
3505 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3507 if (!cli_mkdir(targetcli, targetpath)) {
3509 errno = smbc_errno(context, targetcli);
3521 * Our list function simply checks to see if a directory is not empty
3524 static int smbc_rmdir_dirempty = True;
3527 rmdir_list_fn(const char *mnt,
3532 if (strncmp(finfo->name, ".", 1) != 0 &&
3533 strncmp(finfo->name, "..", 2) != 0) {
3534 smbc_rmdir_dirempty = False;
3539 * Routine to remove a directory
3543 smbc_rmdir_ctx(SMBCCTX *context,
3554 struct cli_state *targetcli;
3555 TALLOC_CTX *frame = talloc_stackframe();
3557 if (!context || !context->internal ||
3558 !context->internal->_initialized) {
3570 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3572 if (smbc_parse_path(frame,
3587 if (!user || user[0] == (char)0) {
3588 user = talloc_strdup(frame, context->user);
3596 srv = smbc_server(context, True,
3597 server, share, workgroup, user, password);
3602 return -1; /* errno set by smbc_server */
3606 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3607 if (!cli_resolve_path(frame, "", srv->cli, path,
3608 &targetcli, &targetpath)) {
3609 d_printf("Could not resolve %s\n", path);
3613 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3616 if (!cli_rmdir(targetcli, targetpath)) {
3618 errno = smbc_errno(context, targetcli);
3620 if (errno == EACCES) { /* Check if the dir empty or not */
3622 /* Local storage to avoid buffer overflows */
3625 smbc_rmdir_dirempty = True; /* Make this so ... */
3627 lpath = talloc_asprintf(frame, "%s\\*",
3635 if (cli_list(targetcli, lpath,
3636 aDIR | aSYSTEM | aHIDDEN,
3637 rmdir_list_fn, NULL) < 0) {
3639 /* Fix errno to ignore latest error ... */
3640 DEBUG(5, ("smbc_rmdir: "
3641 "cli_list returned an error: %d\n",
3642 smbc_errno(context, targetcli)));
3647 if (smbc_rmdir_dirempty)
3665 * Routine to return the current directory position
3669 smbc_telldir_ctx(SMBCCTX *context,
3672 TALLOC_CTX *frame = talloc_stackframe();
3674 if (!context || !context->internal ||
3675 !context->internal->_initialized) {
3683 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3691 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3699 /* See if we're already at the end. */
3700 if (dir->dir_next == NULL) {
3707 * We return the pointer here as the offset
3710 return (off_t)(long)dir->dir_next->dirent;
3714 * A routine to run down the list and see if the entry is OK
3717 struct smbc_dir_list *
3718 smbc_check_dir_ent(struct smbc_dir_list *list,
3719 struct smbc_dirent *dirent)
3722 /* Run down the list looking for what we want */
3726 struct smbc_dir_list *tmp = list;
3730 if (tmp->dirent == dirent)
3739 return NULL; /* Not found, or an error */
3745 * Routine to seek on a directory
3749 smbc_lseekdir_ctx(SMBCCTX *context,
3753 long int l_offset = offset; /* Handle problems of size */
3754 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3755 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3756 TALLOC_CTX *frame = talloc_stackframe();
3758 if (!context || !context->internal ||
3759 !context->internal->_initialized) {
3767 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3775 /* Now, check what we were passed and see if it is OK ... */
3777 if (dirent == NULL) { /* Seek to the begining of the list */
3779 dir->dir_next = dir->dir_list;
3785 if (offset == -1) { /* Seek to the end of the list */
3786 dir->dir_next = NULL;
3791 /* Now, run down the list and make sure that the entry is OK */
3792 /* This may need to be changed if we change the format of the list */
3794 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3795 errno = EINVAL; /* Bad entry */
3800 dir->dir_next = list_ent;
3807 * Routine to fstat a dir
3811 smbc_fstatdir_ctx(SMBCCTX *context,
3816 if (!context || !context->internal ||
3817 !context->internal->_initialized) {
3822 /* No code yet ... */
3827 smbc_chmod_ctx(SMBCCTX *context,
3839 TALLOC_CTX *frame = talloc_stackframe();
3841 if (!context || !context->internal ||
3842 !context->internal->_initialized) {
3843 errno = EINVAL; /* Best I can think of ... */
3854 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3856 if (smbc_parse_path(frame,
3871 if (!user || user[0] == (char)0) {
3872 user = talloc_strdup(frame, context->user);
3880 srv = smbc_server(context, True,
3881 server, share, workgroup, user, password);
3885 return -1; /* errno set by smbc_server */
3890 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3891 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3892 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3893 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3895 if (!cli_setatr(srv->cli, path, mode, 0)) {
3896 errno = smbc_errno(context, srv->cli);
3906 smbc_utimes_ctx(SMBCCTX *context,
3908 struct timeval *tbuf)
3919 TALLOC_CTX *frame = talloc_stackframe();
3921 if (!context || !context->internal ||
3922 !context->internal->_initialized) {
3923 errno = EINVAL; /* Best I can think of ... */
3935 access_time = write_time = time(NULL);
3937 access_time = tbuf[0].tv_sec;
3938 write_time = tbuf[1].tv_sec;
3946 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3947 atimebuf[sizeof(atimebuf) - 1] = '\0';
3948 if ((p = strchr(atimebuf, '\n')) != NULL) {
3952 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3953 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3954 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3958 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3959 fname, atimebuf, mtimebuf);
3962 if (smbc_parse_path(frame,
3977 if (!user || user[0] == (char)0) {
3978 user = talloc_strdup(frame, context->user);
3986 srv = smbc_server(context, True,
3987 server, share, workgroup, user, password);
3991 return -1; /* errno set by smbc_server */
3994 if (!smbc_setatr(context, srv, path,
3995 0, access_time, write_time, 0, 0)) {
3997 return -1; /* errno set by smbc_setatr */
4006 * Sort ACEs according to the documentation at
4007 * http://support.microsoft.com/kb/269175, at least as far as it defines the
4012 ace_compare(SEC_ACE *ace1,
4018 /* If the ACEs are equal, we have nothing more to do. */
4019 if (sec_ace_equal(ace1, ace2)) {
4023 /* Inherited follow non-inherited */
4024 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
4025 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
4027 return (b1 ? 1 : -1);
4031 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
4032 * sort them after DENY and ALLOW.
4034 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
4035 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
4036 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
4037 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4038 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
4039 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
4040 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
4041 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4043 return (b1 ? 1 : -1);
4046 /* Allowed ACEs follow denied ACEs */
4047 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
4048 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
4049 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
4050 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
4052 return (b1 ? 1 : -1);
4056 * ACEs applying to an entity's object follow those applying to the
4059 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
4060 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4061 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
4062 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4064 return (b1 ? 1 : -1);
4068 * If we get this far, the ACEs are similar as far as the
4069 * characteristics we typically care about (those defined by the
4070 * referenced MS document). We'll now sort by characteristics that
4071 * just seems reasonable.
4074 if (ace1->type != ace2->type) {
4075 return ace2->type - ace1->type;
4078 if (sid_compare(&ace1->trustee, &ace2->trustee)) {
4079 return sid_compare(&ace1->trustee, &ace2->trustee);
4082 if (ace1->flags != ace2->flags) {
4083 return ace1->flags - ace2->flags;
4086 if (ace1->access_mask != ace2->access_mask) {
4087 return ace1->access_mask - ace2->access_mask;
4090 if (ace1->size != ace2->size) {
4091 return ace1->size - ace2->size;
4094 return memcmp(ace1, ace2, sizeof(SEC_ACE));
4099 sort_acl(SEC_ACL *the_acl)
4102 if (!the_acl) return;
4104 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
4105 QSORT_CAST ace_compare);
4107 for (i=1;i<the_acl->num_aces;) {
4108 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
4110 for (j=i; j<the_acl->num_aces-1; j++) {
4111 the_acl->aces[j] = the_acl->aces[j+1];
4113 the_acl->num_aces--;
4120 /* convert a SID to a string, either numeric or username/group */
4122 convert_sid_to_string(struct cli_state *ipc_cli,
4128 char **domains = NULL;
4129 char **names = NULL;
4130 enum lsa_SidType *types = NULL;
4131 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
4134 sid_to_string(str, sid);
4137 return; /* no lookup desired */
4144 /* Ask LSA to convert the sid to a name */
4146 ctx = talloc_stackframe();
4148 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ctx,
4149 pol, 1, sid, &domains,
4151 !domains || !domains[0] || !names || !names[0]) {
4159 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
4160 domains[0], lp_winbind_separator(),
4164 /* convert a string to a SID, either numeric or username/group */
4166 convert_string_to_sid(struct cli_state *ipc_cli,
4172 enum lsa_SidType *types = NULL;
4173 DOM_SID *sids = NULL;
4175 TALLOC_CTX *ctx = NULL;
4176 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
4183 if (strncmp(str, "S-", 2) == 0) {
4184 return string_to_sid(sid, str);
4191 ctx = talloc_stackframe();
4192 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ctx,
4193 pol, 1, &str, NULL, 1, &sids,
4199 sid_copy(sid, &sids[0]);
4207 /* parse an ACE in the same format as print_ace() */
4209 parse_ace(struct cli_state *ipc_cli,
4219 unsigned int aflags;
4223 const struct perm_value *v;
4229 /* These values discovered by inspection */
4230 static const struct perm_value special_values[] = {
4231 { "R", 0x00120089 },
4232 { "W", 0x00120116 },
4233 { "X", 0x001200a0 },
4234 { "D", 0x00010000 },
4235 { "P", 0x00040000 },
4236 { "O", 0x00080000 },
4240 static const struct perm_value standard_values[] = {
4241 { "READ", 0x001200a9 },
4242 { "CHANGE", 0x001301bf },
4243 { "FULL", 0x001f01ff },
4249 p = strchr_m(str,':');
4250 if (!p) return False;
4253 /* Try to parse numeric form */
4255 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
4256 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
4260 /* Try to parse text form */
4262 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
4267 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
4271 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
4272 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
4273 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
4274 atype = SEC_ACE_TYPE_ACCESS_DENIED;
4279 /* Only numeric form accepted for flags at present */
4281 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
4282 sscanf(tok, "%i", &aflags))) {
4286 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
4290 if (strncmp(tok, "0x", 2) == 0) {
4291 if (sscanf(tok, "%i", &amask) != 1) {
4297 for (v = standard_values; v->perm; v++) {
4298 if (strcmp(tok, v->perm) == 0) {
4309 for (v = special_values; v->perm; v++) {
4310 if (v->perm[0] == *p) {
4316 if (!found) return False;
4326 init_sec_ace(ace, &sid, atype, mask, aflags);
4330 /* add an ACE to a list of ACEs in a SEC_ACL */
4332 add_ace(SEC_ACL **the_acl,
4340 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
4344 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4347 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4348 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4349 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4350 1+(*the_acl)->num_aces, aces);
4352 (*the_acl) = newacl;
4357 /* parse a ascii version of a security descriptor */
4359 sec_desc_parse(TALLOC_CTX *ctx,
4360 struct cli_state *ipc_cli,
4365 const char *p = str;
4367 SEC_DESC *ret = NULL;
4369 DOM_SID *group_sid=NULL;
4370 DOM_SID *owner_sid=NULL;
4374 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4376 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4377 revision = strtol(tok+9, NULL, 16);
4381 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4383 DEBUG(5, ("OWNER specified more than once!\n"));
4386 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4388 !convert_string_to_sid(ipc_cli, pol,
4390 owner_sid, tok+6)) {
4391 DEBUG(5, ("Failed to parse owner sid\n"));
4397 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4399 DEBUG(5, ("OWNER specified more than once!\n"));
4402 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4404 !convert_string_to_sid(ipc_cli, pol,
4406 owner_sid, tok+7)) {
4407 DEBUG(5, ("Failed to parse owner sid\n"));
4413 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4415 DEBUG(5, ("GROUP specified more than once!\n"));
4418 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4420 !convert_string_to_sid(ipc_cli, pol,
4422 group_sid, tok+6)) {
4423 DEBUG(5, ("Failed to parse group sid\n"));
4429 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4431 DEBUG(5, ("GROUP specified more than once!\n"));
4434 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4436 !convert_string_to_sid(ipc_cli, pol,
4438 group_sid, tok+6)) {
4439 DEBUG(5, ("Failed to parse group sid\n"));
4445 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4447 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4448 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4451 if(!add_ace(&dacl, &ace, ctx)) {
4452 DEBUG(5, ("Failed to add ACL %s\n", tok));
4458 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4460 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4461 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4464 if(!add_ace(&dacl, &ace, ctx)) {
4465 DEBUG(5, ("Failed to add ACL %s\n", tok));
4471 DEBUG(5, ("Failed to parse security descriptor\n"));
4475 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4476 owner_sid, group_sid, NULL, dacl, &sd_size);
4479 SAFE_FREE(group_sid);
4480 SAFE_FREE(owner_sid);
4486 /* Obtain the current dos attributes */
4487 static DOS_ATTR_DESC *
4488 dos_attr_query(SMBCCTX *context,
4490 const char *filename,
4493 struct timespec create_time_ts;
4494 struct timespec write_time_ts;
4495 struct timespec access_time_ts;
4496 struct timespec change_time_ts;
4499 SMB_INO_T inode = 0;
4502 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4508 /* Obtain the DOS attributes */
4509 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4516 errno = smbc_errno(context, srv->cli);
4517 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4523 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4524 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4525 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4526 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4533 /* parse a ascii version of a security descriptor */
4535 dos_attr_parse(SMBCCTX *context,
4541 const char *p = str;
4544 const char * create_time_attr;
4545 const char * access_time_attr;
4546 const char * write_time_attr;
4547 const char * change_time_attr;
4550 /* Determine whether to use old-style or new-style attribute names */
4551 if (context->internal->_full_time_names) {
4552 /* new-style names */
4553 attr_strings.create_time_attr = "CREATE_TIME";
4554 attr_strings.access_time_attr = "ACCESS_TIME";
4555 attr_strings.write_time_attr = "WRITE_TIME";
4556 attr_strings.change_time_attr = "CHANGE_TIME";
4558 /* old-style names */
4559 attr_strings.create_time_attr = NULL;
4560 attr_strings.access_time_attr = "A_TIME";
4561 attr_strings.write_time_attr = "M_TIME";
4562 attr_strings.change_time_attr = "C_TIME";
4565 /* if this is to set the entire ACL... */
4567 /* ... then increment past the first colon if there is one */
4568 if ((p = strchr(str, ':')) != NULL) {
4575 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4577 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4578 dad->mode = strtol(tok+5, NULL, 16);
4582 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4583 dad->size = (SMB_OFF_T)atof(tok+5);
4587 n = strlen(attr_strings.access_time_attr);
4588 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4589 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4593 n = strlen(attr_strings.change_time_attr);
4594 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4595 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4599 n = strlen(attr_strings.write_time_attr);
4600 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4601 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4605 if (attr_strings.create_time_attr != NULL) {
4606 n = strlen(attr_strings.create_time_attr);
4607 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4609 dad->create_time = (time_t)strtol(tok+n+1,
4615 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4616 dad->inode = (SMB_INO_T)atof(tok+6);
4622 /*****************************************************
4623 Retrieve the acls for a file.
4624 *******************************************************/
4627 cacl_get(SMBCCTX *context,
4630 struct cli_state *ipc_cli,
4646 bool exclude_nt_revision = False;
4647 bool exclude_nt_owner = False;
4648 bool exclude_nt_group = False;
4649 bool exclude_nt_acl = False;
4650 bool exclude_dos_mode = False;
4651 bool exclude_dos_size = False;
4652 bool exclude_dos_create_time = False;
4653 bool exclude_dos_access_time = False;
4654 bool exclude_dos_write_time = False;
4655 bool exclude_dos_change_time = False;
4656 bool exclude_dos_inode = False;
4657 bool numeric = True;
4658 bool determine_size = (bufsize == 0);
4662 fstring name_sandbox;
4666 struct timespec create_time_ts;
4667 struct timespec write_time_ts;
4668 struct timespec access_time_ts;
4669 struct timespec change_time_ts;
4670 time_t create_time = (time_t)0;
4671 time_t write_time = (time_t)0;
4672 time_t access_time = (time_t)0;
4673 time_t change_time = (time_t)0;
4677 struct cli_state *cli = srv->cli;
4679 const char * create_time_attr;
4680 const char * access_time_attr;
4681 const char * write_time_attr;
4682 const char * change_time_attr;
4685 const char * create_time_attr;
4686 const char * access_time_attr;
4687 const char * write_time_attr;
4688 const char * change_time_attr;
4689 } excl_attr_strings;
4691 /* Determine whether to use old-style or new-style attribute names */
4692 if (context->internal->_full_time_names) {
4693 /* new-style names */
4694 attr_strings.create_time_attr = "CREATE_TIME";
4695 attr_strings.access_time_attr = "ACCESS_TIME";
4696 attr_strings.write_time_attr = "WRITE_TIME";
4697 attr_strings.change_time_attr = "CHANGE_TIME";
4699 excl_attr_strings.create_time_attr = "CREATE_TIME";
4700 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4701 excl_attr_strings.write_time_attr = "WRITE_TIME";
4702 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4704 /* old-style names */
4705 attr_strings.create_time_attr = NULL;
4706 attr_strings.access_time_attr = "A_TIME";
4707 attr_strings.write_time_attr = "M_TIME";
4708 attr_strings.change_time_attr = "C_TIME";
4710 excl_attr_strings.create_time_attr = NULL;
4711 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4712 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4713 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4716 /* Copy name so we can strip off exclusions (if any are specified) */
4717 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4719 /* Ensure name is null terminated */
4720 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4722 /* Play in the sandbox */
4723 name = name_sandbox;
4725 /* If there are any exclusions, point to them and mask them from name */
4726 if ((pExclude = strchr(name, '!')) != NULL)
4731 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4732 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4733 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4734 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4735 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4736 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4737 numeric = (* (name + strlen(name) - 1) != '+');
4739 /* Look for exclusions from "all" requests */
4740 if (all || all_nt || all_dos) {
4742 /* Exclusions are delimited by '!' */
4745 pExclude = (p == NULL ? NULL : p + 1)) {
4747 /* Find end of this exclusion name */
4748 if ((p = strchr(pExclude, '!')) != NULL)
4753 /* Which exclusion name is this? */
4754 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4755 exclude_nt_revision = True;
4757 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4758 exclude_nt_owner = True;
4760 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4761 exclude_nt_group = True;
4763 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4764 exclude_nt_acl = True;
4766 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4767 exclude_dos_mode = True;
4769 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4770 exclude_dos_size = True;
4772 else if (excl_attr_strings.create_time_attr != NULL &&
4773 StrCaseCmp(pExclude,
4774 excl_attr_strings.change_time_attr) == 0) {
4775 exclude_dos_create_time = True;
4777 else if (StrCaseCmp(pExclude,
4778 excl_attr_strings.access_time_attr) == 0) {
4779 exclude_dos_access_time = True;
4781 else if (StrCaseCmp(pExclude,
4782 excl_attr_strings.write_time_attr) == 0) {
4783 exclude_dos_write_time = True;
4785 else if (StrCaseCmp(pExclude,
4786 excl_attr_strings.change_time_attr) == 0) {
4787 exclude_dos_change_time = True;
4789 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4790 exclude_dos_inode = True;
4793 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4804 * If we are (possibly) talking to an NT or new system and some NT
4805 * attributes have been requested...
4807 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4808 /* Point to the portion after "system.nt_sec_desc." */
4809 name += 19; /* if (all) this will be invalid but unused */
4811 /* ... then obtain any NT attributes which were requested */
4812 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4815 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4816 filename, cli_errstr(cli)));
4821 sd = cli_query_secdesc(cli, fnum, ctx);
4825 ("cacl_get Failed to query old descriptor\n"));
4830 cli_close(cli, fnum);
4832 if (! exclude_nt_revision) {
4833 if (all || all_nt) {
4834 if (determine_size) {
4835 p = talloc_asprintf(ctx,
4844 n = snprintf(buf, bufsize,
4848 } else if (StrCaseCmp(name, "revision") == 0) {
4849 if (determine_size) {
4850 p = talloc_asprintf(ctx, "%d",
4858 n = snprintf(buf, bufsize, "%d",
4863 if (!determine_size && n > bufsize) {
4873 if (! exclude_nt_owner) {
4874 /* Get owner and group sid */
4875 if (sd->owner_sid) {
4876 convert_sid_to_string(ipc_cli, pol,
4881 fstrcpy(sidstr, "");
4884 if (all || all_nt) {
4885 if (determine_size) {
4886 p = talloc_asprintf(ctx, ",OWNER:%s",
4893 } else if (sidstr[0] != '\0') {
4894 n = snprintf(buf, bufsize,
4895 ",OWNER:%s", sidstr);
4897 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4898 if (determine_size) {
4899 p = talloc_asprintf(ctx, "%s", sidstr);
4906 n = snprintf(buf, bufsize, "%s",
4911 if (!determine_size && n > bufsize) {
4921 if (! exclude_nt_group) {
4922 if (sd->group_sid) {
4923 convert_sid_to_string(ipc_cli, pol,
4927 fstrcpy(sidstr, "");
4930 if (all || all_nt) {
4931 if (determine_size) {
4932 p = talloc_asprintf(ctx, ",GROUP:%s",
4939 } else if (sidstr[0] != '\0') {
4940 n = snprintf(buf, bufsize,
4941 ",GROUP:%s", sidstr);
4943 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4944 if (determine_size) {
4945 p = talloc_asprintf(ctx, "%s", sidstr);
4952 n = snprintf(buf, bufsize,
4957 if (!determine_size && n > bufsize) {
4967 if (! exclude_nt_acl) {
4968 /* Add aces to value buffer */
4969 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4971 SEC_ACE *ace = &sd->dacl->aces[i];
4972 convert_sid_to_string(ipc_cli, pol,
4976 if (all || all_nt) {
4977 if (determine_size) {
4978 p = talloc_asprintf(
4994 ",ACL:%s:%d/%d/0x%08x",
5000 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
5001 StrCaseCmp(name+3, sidstr) == 0) ||
5002 (StrnCaseCmp(name, "acl+", 4) == 0 &&
5003 StrCaseCmp(name+4, sidstr) == 0)) {
5004 if (determine_size) {
5005 p = talloc_asprintf(
5017 n = snprintf(buf, bufsize,
5023 } else if (all_nt_acls) {
5024 if (determine_size) {
5025 p = talloc_asprintf(
5027 "%s%s:%d/%d/0x%08x",
5039 n = snprintf(buf, bufsize,
5040 "%s%s:%d/%d/0x%08x",
5048 if (!determine_size && n > bufsize) {
5059 /* Restore name pointer to its original value */
5063 if (all || some_dos) {
5064 /* Point to the portion after "system.dos_attr." */
5065 name += 16; /* if (all) this will be invalid but unused */
5067 /* Obtain the DOS attributes */
5068 if (!smbc_getatr(context, srv, filename, &mode, &size,
5075 errno = smbc_errno(context, srv->cli);
5080 create_time = convert_timespec_to_time_t(create_time_ts);
5081 access_time = convert_timespec_to_time_t(access_time_ts);
5082 write_time = convert_timespec_to_time_t(write_time_ts);
5083 change_time = convert_timespec_to_time_t(change_time_ts);
5085 if (! exclude_dos_mode) {
5086 if (all || all_dos) {
5087 if (determine_size) {
5088 p = talloc_asprintf(ctx,
5101 n = snprintf(buf, bufsize,
5109 } else if (StrCaseCmp(name, "mode") == 0) {
5110 if (determine_size) {
5111 p = talloc_asprintf(ctx, "0x%x", mode);
5118 n = snprintf(buf, bufsize,
5123 if (!determine_size && n > bufsize) {
5133 if (! exclude_dos_size) {
5134 if (all || all_dos) {
5135 if (determine_size) {
5136 p = talloc_asprintf(
5146 n = snprintf(buf, bufsize,
5150 } else if (StrCaseCmp(name, "size") == 0) {
5151 if (determine_size) {
5152 p = talloc_asprintf(
5162 n = snprintf(buf, bufsize,
5168 if (!determine_size && n > bufsize) {
5178 if (! exclude_dos_create_time &&
5179 attr_strings.create_time_attr != NULL) {
5180 if (all || all_dos) {
5181 if (determine_size) {
5182 p = talloc_asprintf(ctx,
5184 attr_strings.create_time_attr,
5192 n = snprintf(buf, bufsize,
5194 attr_strings.create_time_attr,
5197 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
5198 if (determine_size) {
5199 p = talloc_asprintf(ctx, "%lu", create_time);
5206 n = snprintf(buf, bufsize,
5207 "%lu", create_time);
5211 if (!determine_size && n > bufsize) {
5221 if (! exclude_dos_access_time) {
5222 if (all || all_dos) {
5223 if (determine_size) {
5224 p = talloc_asprintf(ctx,
5226 attr_strings.access_time_attr,
5234 n = snprintf(buf, bufsize,
5236 attr_strings.access_time_attr,
5239 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
5240 if (determine_size) {
5241 p = talloc_asprintf(ctx, "%lu", access_time);
5248 n = snprintf(buf, bufsize,
5249 "%lu", access_time);
5253 if (!determine_size && n > bufsize) {
5263 if (! exclude_dos_write_time) {
5264 if (all || all_dos) {
5265 if (determine_size) {
5266 p = talloc_asprintf(ctx,
5268 attr_strings.write_time_attr,
5276 n = snprintf(buf, bufsize,
5278 attr_strings.write_time_attr,
5281 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
5282 if (determine_size) {
5283 p = talloc_asprintf(ctx, "%lu", write_time);
5290 n = snprintf(buf, bufsize,
5295 if (!determine_size && n > bufsize) {
5305 if (! exclude_dos_change_time) {
5306 if (all || all_dos) {
5307 if (determine_size) {
5308 p = talloc_asprintf(ctx,
5310 attr_strings.change_time_attr,
5318 n = snprintf(buf, bufsize,
5320 attr_strings.change_time_attr,
5323 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5324 if (determine_size) {
5325 p = talloc_asprintf(ctx, "%lu", change_time);
5332 n = snprintf(buf, bufsize,
5333 "%lu", change_time);
5337 if (!determine_size && n > bufsize) {
5347 if (! exclude_dos_inode) {
5348 if (all || all_dos) {
5349 if (determine_size) {
5350 p = talloc_asprintf(
5360 n = snprintf(buf, bufsize,
5364 } else if (StrCaseCmp(name, "inode") == 0) {
5365 if (determine_size) {
5366 p = talloc_asprintf(
5376 n = snprintf(buf, bufsize,
5382 if (!determine_size && n > bufsize) {
5392 /* Restore name pointer to its original value */
5404 /*****************************************************
5405 set the ACLs on a file given an ascii description
5406 *******************************************************/
5408 cacl_set(TALLOC_CTX *ctx,
5409 struct cli_state *cli,
5410 struct cli_state *ipc_cli,
5412 const char *filename,
5413 const char *the_acl,
5419 SEC_DESC *sd = NULL, *old;
5420 SEC_ACL *dacl = NULL;
5421 DOM_SID *owner_sid = NULL;
5422 DOM_SID *group_sid = NULL;
5427 bool numeric = True;
5429 /* the_acl will be null for REMOVE_ALL operations */
5431 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5435 /* if this is to set the entire ACL... */
5436 if (*the_acl == '*') {
5437 /* ... then increment past the first colon */
5441 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5442 CONST_DISCARD(char *, the_acl));
5450 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5451 that doesn't deref sd */
5453 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5458 /* The desired access below is the only one I could find that works
5459 with NT4, W2KP and Samba */
5461 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5464 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5465 filename, cli_errstr(cli)));
5470 old = cli_query_secdesc(cli, fnum, ctx);
5473 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5478 cli_close(cli, fnum);
5481 case SMBC_XATTR_MODE_REMOVE_ALL:
5482 old->dacl->num_aces = 0;
5486 case SMBC_XATTR_MODE_REMOVE:
5487 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5490 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5491 if (sec_ace_equal(&sd->dacl->aces[i],
5492 &old->dacl->aces[j])) {
5494 for (k=j; k<old->dacl->num_aces-1;k++) {
5495 old->dacl->aces[k] =
5496 old->dacl->aces[k+1];
5498 old->dacl->num_aces--;
5513 case SMBC_XATTR_MODE_ADD:
5514 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5517 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5518 if (sid_equal(&sd->dacl->aces[i].trustee,
5519 &old->dacl->aces[j].trustee)) {
5520 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5525 old->dacl->aces[j] = sd->dacl->aces[i];
5531 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5537 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5538 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5544 case SMBC_XATTR_MODE_SET:
5546 owner_sid = old->owner_sid;
5547 group_sid = old->group_sid;
5551 case SMBC_XATTR_MODE_CHOWN:
5552 owner_sid = sd->owner_sid;
5555 case SMBC_XATTR_MODE_CHGRP:
5556 group_sid = sd->group_sid;
5560 /* Denied ACE entries must come before allowed ones */
5561 sort_acl(old->dacl);
5563 /* Create new security descriptor and set it */
5564 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5565 owner_sid, group_sid, NULL, dacl, &sd_size);
5567 fnum = cli_nt_create(cli, filename,
5568 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5571 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5572 filename, cli_errstr(cli)));
5577 if (!cli_set_secdesc(cli, fnum, sd)) {
5578 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5585 cli_close(cli, fnum);
5596 smbc_setxattr_ctx(SMBCCTX *context,
5616 const char * create_time_attr;
5617 const char * access_time_attr;
5618 const char * write_time_attr;
5619 const char * change_time_attr;
5621 TALLOC_CTX *frame = talloc_stackframe();
5623 if (!context || !context->internal ||
5624 !context->internal->_initialized) {
5625 errno = EINVAL; /* Best I can think of ... */
5636 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5637 fname, name, (int) size, (const char*)value));
5639 if (smbc_parse_path(frame,
5654 if (!user || user[0] == (char)0) {
5655 user = talloc_strdup(frame, context->user);
5663 srv = smbc_server(context, True,
5664 server, share, workgroup, user, password);
5667 return -1; /* errno set by smbc_server */
5670 if (! srv->no_nt_session) {
5671 ipc_srv = smbc_attr_server(context, server, share,
5672 workgroup, user, password,
5675 srv->no_nt_session = True;
5682 * Are they asking to set the entire set of known attributes?
5684 if (StrCaseCmp(name, "system.*") == 0 ||
5685 StrCaseCmp(name, "system.*+") == 0) {
5688 talloc_asprintf(talloc_tos(), "%s:%s",
5689 name+7, (const char *) value);
5698 ret = cacl_set(talloc_tos(), srv->cli,
5699 ipc_srv->cli, &pol, path,
5702 ? SMBC_XATTR_MODE_SET
5703 : SMBC_XATTR_MODE_ADD),
5709 /* get a DOS Attribute Descriptor with current attributes */
5710 dad = dos_attr_query(context, talloc_tos(), path, srv);
5712 /* Overwrite old with new, using what was provided */
5713 dos_attr_parse(context, dad, srv, namevalue);
5715 /* Set the new DOS attributes */
5716 if (! smbc_setatr(context, srv, path,
5723 /* cause failure if NT failed too */
5728 /* we only fail if both NT and DOS sets failed */
5729 if (ret < 0 && ! dad) {
5730 ret = -1; /* in case dad was null */
5741 * Are they asking to set an access control element or to set
5742 * the entire access control list?
5744 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5745 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5746 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5747 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5748 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5752 talloc_asprintf(talloc_tos(), "%s:%s",
5753 name+19, (const char *) value);
5756 ret = -1; /* errno set by smbc_server() */
5758 else if (! namevalue) {
5762 ret = cacl_set(talloc_tos(), srv->cli,
5763 ipc_srv->cli, &pol, path,
5766 ? SMBC_XATTR_MODE_SET
5767 : SMBC_XATTR_MODE_ADD),
5775 * Are they asking to set the owner?
5777 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5778 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5782 talloc_asprintf(talloc_tos(), "%s:%s",
5783 name+19, (const char *) value);
5786 ret = -1; /* errno set by smbc_server() */
5788 else if (! namevalue) {
5792 ret = cacl_set(talloc_tos(), srv->cli,
5793 ipc_srv->cli, &pol, path,
5794 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5801 * Are they asking to set the group?
5803 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5804 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5808 talloc_asprintf(talloc_tos(), "%s:%s",
5809 name+19, (const char *) value);
5812 /* errno set by smbc_server() */
5815 else if (! namevalue) {
5819 ret = cacl_set(talloc_tos(), srv->cli,
5820 ipc_srv->cli, &pol, path,
5821 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5827 /* Determine whether to use old-style or new-style attribute names */
5828 if (context->internal->_full_time_names) {
5829 /* new-style names */
5830 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5831 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5832 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5833 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5835 /* old-style names */
5836 attr_strings.create_time_attr = NULL;
5837 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5838 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5839 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5843 * Are they asking to set a DOS attribute?
5845 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5846 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5847 (attr_strings.create_time_attr != NULL &&
5848 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5849 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5850 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5851 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5853 /* get a DOS Attribute Descriptor with current attributes */
5854 dad = dos_attr_query(context, talloc_tos(), path, srv);
5857 talloc_asprintf(talloc_tos(), "%s:%s",
5858 name+16, (const char *) value);
5863 /* Overwrite old with provided new params */
5864 dos_attr_parse(context, dad, srv, namevalue);
5866 /* Set the new DOS attributes */
5867 ret2 = smbc_setatr(context, srv, path,
5874 /* ret2 has True (success) / False (failure) */
5889 /* Unsupported attribute name */
5896 smbc_getxattr_ctx(SMBCCTX *context,
5913 const char * create_time_attr;
5914 const char * access_time_attr;
5915 const char * write_time_attr;
5916 const char * change_time_attr;
5918 TALLOC_CTX *frame = talloc_stackframe();
5920 if (!context || !context->internal ||
5921 !context->internal->_initialized) {
5922 errno = EINVAL; /* Best I can think of ... */
5933 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5935 if (smbc_parse_path(frame,
5950 if (!user || user[0] == (char)0) {
5951 user = talloc_strdup(frame, context->user);
5959 srv = smbc_server(context, True,
5960 server, share, workgroup, user, password);
5963 return -1; /* errno set by smbc_server */
5966 if (! srv->no_nt_session) {
5967 ipc_srv = smbc_attr_server(context, server, share,
5968 workgroup, user, password,
5971 srv->no_nt_session = True;
5977 /* Determine whether to use old-style or new-style attribute names */
5978 if (context->internal->_full_time_names) {
5979 /* new-style names */
5980 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5981 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5982 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5983 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5985 /* old-style names */
5986 attr_strings.create_time_attr = NULL;
5987 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5988 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5989 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5992 /* Are they requesting a supported attribute? */
5993 if (StrCaseCmp(name, "system.*") == 0 ||
5994 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5995 StrCaseCmp(name, "system.*+") == 0 ||
5996 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5997 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5998 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5999 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
6000 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
6001 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
6002 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
6003 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
6004 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
6005 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
6006 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
6007 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
6008 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
6009 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
6010 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
6011 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
6012 (attr_strings.create_time_attr != NULL &&
6013 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
6014 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
6015 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
6016 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
6017 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
6020 ret = cacl_get(context, talloc_tos(), srv,
6021 ipc_srv == NULL ? NULL : ipc_srv->cli,
6023 CONST_DISCARD(char *, name),
6024 CONST_DISCARD(char *, value), size);
6025 if (ret < 0 && errno == 0) {
6026 errno = smbc_errno(context, srv->cli);
6032 /* Unsupported attribute name */
6040 smbc_removexattr_ctx(SMBCCTX *context,
6054 TALLOC_CTX *frame = talloc_stackframe();
6056 if (!context || !context->internal ||
6057 !context->internal->_initialized) {
6058 errno = EINVAL; /* Best I can think of ... */
6069 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
6071 if (smbc_parse_path(frame,
6086 if (!user || user[0] == (char)0) {
6087 user = talloc_strdup(frame, context->user);
6095 srv = smbc_server(context, True,
6096 server, share, workgroup, user, password);
6099 return -1; /* errno set by smbc_server */
6102 if (! srv->no_nt_session) {
6103 ipc_srv = smbc_attr_server(context, server, share,
6104 workgroup, user, password,
6107 srv->no_nt_session = True;
6115 return -1; /* errno set by smbc_attr_server */
6118 /* Are they asking to set the entire ACL? */
6119 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
6120 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
6123 ret = cacl_set(talloc_tos(), srv->cli,
6124 ipc_srv->cli, &pol, path,
6125 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
6131 * Are they asking to remove one or more spceific security descriptor
6134 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
6135 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
6136 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
6137 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
6138 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
6139 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
6140 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
6143 ret = cacl_set(talloc_tos(), srv->cli,
6144 ipc_srv->cli, &pol, path,
6145 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
6150 /* Unsupported attribute name */
6157 smbc_listxattr_ctx(SMBCCTX *context,
6163 * This isn't quite what listxattr() is supposed to do. This returns
6164 * the complete set of attribute names, always, rather than only those
6165 * attribute names which actually exist for a file. Hmmm...
6167 const char supported_old[] =
6170 "system.nt_sec_desc.revision\0"
6171 "system.nt_sec_desc.owner\0"
6172 "system.nt_sec_desc.owner+\0"
6173 "system.nt_sec_desc.group\0"
6174 "system.nt_sec_desc.group+\0"
6175 "system.nt_sec_desc.acl.*\0"
6176 "system.nt_sec_desc.acl\0"
6177 "system.nt_sec_desc.acl+\0"
6178 "system.nt_sec_desc.*\0"
6179 "system.nt_sec_desc.*+\0"
6180 "system.dos_attr.*\0"
6181 "system.dos_attr.mode\0"
6182 "system.dos_attr.c_time\0"
6183 "system.dos_attr.a_time\0"
6184 "system.dos_attr.m_time\0"
6186 const char supported_new[] =
6189 "system.nt_sec_desc.revision\0"
6190 "system.nt_sec_desc.owner\0"
6191 "system.nt_sec_desc.owner+\0"
6192 "system.nt_sec_desc.group\0"
6193 "system.nt_sec_desc.group+\0"
6194 "system.nt_sec_desc.acl.*\0"
6195 "system.nt_sec_desc.acl\0"
6196 "system.nt_sec_desc.acl+\0"
6197 "system.nt_sec_desc.*\0"
6198 "system.nt_sec_desc.*+\0"
6199 "system.dos_attr.*\0"
6200 "system.dos_attr.mode\0"
6201 "system.dos_attr.create_time\0"
6202 "system.dos_attr.access_time\0"
6203 "system.dos_attr.write_time\0"
6204 "system.dos_attr.change_time\0"
6206 const char * supported;
6208 if (context->internal->_full_time_names) {
6209 supported = supported_new;
6211 supported = supported_old;
6215 return sizeof(supported);
6218 if (sizeof(supported) > size) {
6223 /* this can't be strcpy() because there are embedded null characters */
6224 memcpy(list, supported, sizeof(supported));
6225 return sizeof(supported);
6230 * Open a print file to be written to by other calls
6234 smbc_open_print_job_ctx(SMBCCTX *context,
6242 TALLOC_CTX *frame = talloc_stackframe();
6244 if (!context || !context->internal ||
6245 !context->internal->_initialized) {
6257 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
6259 if (smbc_parse_path(frame,
6274 /* What if the path is empty, or the file exists? */
6277 return (context->open)(context, fname, O_WRONLY, 666);
6281 * Routine to print a file on a remote server ...
6283 * We open the file, which we assume to be on a remote server, and then
6284 * copy it to a print file on the share specified by printq.
6288 smbc_print_file_ctx(SMBCCTX *c_file,
6299 TALLOC_CTX *frame = talloc_stackframe();
6301 if (!c_file || !c_file->internal->_initialized || !c_print ||
6302 !c_print->internal->_initialized) {
6310 if (!fname && !printq) {
6318 /* Try to open the file for reading ... */
6320 if ((long)(fid1 = (c_file->open)(c_file, fname, O_RDONLY, 0666)) < 0) {
6321 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
6323 return -1; /* smbc_open sets errno */
6326 /* Now, try to open the printer file for writing */
6328 if ((long)(fid2 = (c_print->open_print_job)(c_print, printq)) < 0) {
6330 saverr = errno; /* Save errno */
6331 (c_file->close_fn)(c_file, fid1);
6338 while ((bytes = (c_file->read)(c_file, fid1, buf, sizeof(buf))) > 0) {
6342 if (((c_print->write)(c_print, fid2, buf, bytes)) < 0) {
6345 (c_file->close_fn)(c_file, fid1);
6346 (c_print->close_fn)(c_print, fid2);
6355 (c_file->close_fn)(c_file, fid1); /* We have to close these anyway */
6356 (c_print->close_fn)(c_print, fid2);
6372 * Routine to list print jobs on a printer share ...
6376 smbc_list_print_jobs_ctx(SMBCCTX *context,
6378 smbc_list_print_job_fn fn)
6387 TALLOC_CTX *frame = talloc_stackframe();
6389 if (!context || !context->internal ||
6390 !context->internal->_initialized) {
6402 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6404 if (smbc_parse_path(frame,
6419 if (!user || user[0] == (char)0) {
6420 user = talloc_strdup(frame, context->user);
6428 srv = smbc_server(context, True,
6429 server, share, workgroup, user, password);
6433 return -1; /* errno set by smbc_server */
6436 if (cli_print_queue(srv->cli,
6437 (void (*)(struct print_job_info *))fn) < 0) {
6438 errno = smbc_errno(context, srv->cli);
6449 * Delete a print job from a remote printer share
6453 smbc_unlink_print_job_ctx(SMBCCTX *context,
6465 TALLOC_CTX *frame = talloc_stackframe();
6467 if (!context || !context->internal ||
6468 !context->internal->_initialized) {
6480 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6482 if (smbc_parse_path(frame,
6497 if (!user || user[0] == (char)0) {
6498 user = talloc_strdup(frame, context->user);
6506 srv = smbc_server(context, True,
6507 server, share, workgroup, user, password);
6512 return -1; /* errno set by smbc_server */
6516 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6519 errno = smbc_errno(context, srv->cli);
6520 else if (err == ERRnosuchprintjob)
6533 * Get a new empty handle to fill in with your own info
6536 smbc_new_context(void)
6540 context = SMB_MALLOC_P(SMBCCTX);
6546 ZERO_STRUCTP(context);
6548 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6549 if (!context->internal) {
6555 ZERO_STRUCTP(context->internal);
6557 /* ADD REASONABLE DEFAULTS */
6559 context->timeout = 20000; /* 20 seconds */
6561 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6562 context->options.urlencode_readdir_entries = False;/* backward compat */
6563 context->options.one_share_per_server = False;/* backward compat */
6564 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6565 /* backward compat */
6567 context->open = smbc_open_ctx;
6568 context->creat = smbc_creat_ctx;
6569 context->read = smbc_read_ctx;
6570 context->write = smbc_write_ctx;
6571 context->close_fn = smbc_close_ctx;
6572 context->unlink = smbc_unlink_ctx;
6573 context->rename = smbc_rename_ctx;
6574 context->lseek = smbc_lseek_ctx;
6575 context->stat = smbc_stat_ctx;
6576 context->fstat = smbc_fstat_ctx;
6577 context->opendir = smbc_opendir_ctx;
6578 context->closedir = smbc_closedir_ctx;
6579 context->readdir = smbc_readdir_ctx;
6580 context->getdents = smbc_getdents_ctx;
6581 context->mkdir = smbc_mkdir_ctx;
6582 context->rmdir = smbc_rmdir_ctx;
6583 context->telldir = smbc_telldir_ctx;
6584 context->lseekdir = smbc_lseekdir_ctx;
6585 context->fstatdir = smbc_fstatdir_ctx;
6586 context->chmod = smbc_chmod_ctx;
6587 context->utimes = smbc_utimes_ctx;
6588 context->setxattr = smbc_setxattr_ctx;
6589 context->getxattr = smbc_getxattr_ctx;
6590 context->removexattr = smbc_removexattr_ctx;
6591 context->listxattr = smbc_listxattr_ctx;
6592 context->open_print_job = smbc_open_print_job_ctx;
6593 context->print_file = smbc_print_file_ctx;
6594 context->list_print_jobs = smbc_list_print_jobs_ctx;
6595 context->unlink_print_job = smbc_unlink_print_job_ctx;
6597 context->callbacks.check_server_fn = smbc_check_server;
6598 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6600 smbc_default_cache_functions(context);
6608 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6609 * and thus you'll be leaking memory if not handled properly.
6613 smbc_free_context(SMBCCTX *context,
6623 DEBUG(1,("Performing aggressive shutdown.\n"));
6625 f = context->internal->_files;
6627 (context->close_fn)(context, f);
6630 context->internal->_files = NULL;
6632 /* First try to remove the servers the nice way. */
6633 if (context->callbacks.purge_cached_fn(context)) {
6636 DEBUG(1, ("Could not purge all servers, "
6637 "Nice way shutdown failed.\n"));
6638 s = context->internal->_servers;
6640 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6642 cli_shutdown(s->cli);
6643 (context->callbacks.remove_cached_srv_fn)(context,
6646 DLIST_REMOVE(context->internal->_servers, s);
6650 context->internal->_servers = NULL;
6654 /* This is the polite way */
6655 if ((context->callbacks.purge_cached_fn)(context)) {
6656 DEBUG(1, ("Could not purge all servers, "
6657 "free_context failed.\n"));
6661 if (context->internal->_servers) {
6662 DEBUG(1, ("Active servers in context, "
6663 "free_context failed.\n"));
6667 if (context->internal->_files) {
6668 DEBUG(1, ("Active files in context, "
6669 "free_context failed.\n"));
6675 /* Things we have to clean up */
6676 SAFE_FREE(context->workgroup);
6677 SAFE_FREE(context->netbios_name);
6678 SAFE_FREE(context->user);
6680 DEBUG(3, ("Context %p succesfully freed\n", context));
6681 SAFE_FREE(context->internal);
6688 * Each time the context structure is changed, we have binary backward
6689 * compatibility issues. Instead of modifying the public portions of the
6690 * context structure to add new options, instead, we put them in the internal
6691 * portion of the context structure and provide a set function for these new
6695 smbc_option_set(SMBCCTX *context,
6697 ... /* option_value */)
6703 smbc_get_auth_data_with_context_fn auth_fn;
6707 va_start(ap, option_name);
6709 if (strcmp(option_name, "debug_to_stderr") == 0) {
6711 * Log to standard error instead of standard output.
6713 option_value.b = (bool) va_arg(ap, int);
6714 context->internal->_debug_stderr = option_value.b;
6716 } else if (strcmp(option_name, "full_time_names") == 0) {
6718 * Use new-style time attribute names, e.g. WRITE_TIME rather
6719 * than the old-style names such as M_TIME. This allows also
6720 * setting/getting CREATE_TIME which was previously
6721 * unimplemented. (Note that the old C_TIME was supposed to
6722 * be CHANGE_TIME but was confused and sometimes referred to
6725 option_value.b = (bool) va_arg(ap, int);
6726 context->internal->_full_time_names = option_value.b;
6728 } else if (strcmp(option_name, "open_share_mode") == 0) {
6730 * The share mode to use for files opened with
6731 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6733 option_value.i = va_arg(ap, int);
6734 context->internal->_share_mode =
6735 (smbc_share_mode) option_value.i;
6737 } else if (strcmp(option_name, "auth_function") == 0) {
6739 * Use the new-style authentication function which includes
6742 option_value.auth_fn =
6743 va_arg(ap, smbc_get_auth_data_with_context_fn);
6744 context->internal->_auth_fn_with_context =
6745 option_value.auth_fn;
6746 } else if (strcmp(option_name, "user_data") == 0) {
6748 * Save a user data handle which may be retrieved by the user
6749 * with smbc_option_get()
6751 option_value.v = va_arg(ap, void *);
6752 context->internal->_user_data = option_value.v;
6760 * Retrieve the current value of an option
6763 smbc_option_get(SMBCCTX *context,
6766 if (strcmp(option_name, "debug_stderr") == 0) {
6768 * Log to standard error instead of standard output.
6770 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6771 return (void *) (intptr_t) context->internal->_debug_stderr;
6773 return (void *) context->internal->_debug_stderr;
6775 } else if (strcmp(option_name, "full_time_names") == 0) {
6777 * Use new-style time attribute names, e.g. WRITE_TIME rather
6778 * than the old-style names such as M_TIME. This allows also
6779 * setting/getting CREATE_TIME which was previously
6780 * unimplemented. (Note that the old C_TIME was supposed to
6781 * be CHANGE_TIME but was confused and sometimes referred to
6784 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6785 return (void *) (intptr_t) context->internal->_full_time_names;
6787 return (void *) context->internal->_full_time_names;
6790 } else if (strcmp(option_name, "auth_function") == 0) {
6792 * Use the new-style authentication function which includes
6795 return (void *) context->internal->_auth_fn_with_context;
6796 } else if (strcmp(option_name, "user_data") == 0) {
6798 * Save a user data handle which may be retrieved by the user
6799 * with smbc_option_get()
6801 return context->internal->_user_data;
6809 * Initialise the library etc
6811 * We accept a struct containing handle information.
6812 * valid values for info->debug from 0 to 100,
6813 * and insist that info->fn must be non-null.
6816 smbc_init_context(SMBCCTX *context)
6822 if (!context || !context->internal) {
6827 /* Do not initialise the same client twice */
6828 if (context->internal->_initialized) {
6832 if ((!context->callbacks.auth_fn &&
6833 !context->internal->_auth_fn_with_context) ||
6834 context->debug < 0 ||
6835 context->debug > 100) {
6842 if (!smbc_initialized) {
6844 * Do some library-wide intializations the first time we get
6847 bool conf_loaded = False;
6848 TALLOC_CTX *frame = talloc_stackframe();
6850 /* Set this to what the user wants */
6851 DEBUGLEVEL = context->debug;
6855 setup_logging("libsmbclient", True);
6856 if (context->internal->_debug_stderr) {
6858 x_setbuf(x_stderr, NULL);
6861 /* Here we would open the smb.conf file if needed ... */
6863 in_client = True; /* FIXME, make a param */
6865 home = getenv("HOME");
6868 if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
6869 if (lp_load(conf, True, False, False, True)) {
6872 DEBUG(5, ("Could not load config file: %s\n",
6881 * Well, if that failed, try the dyn_CONFIGFILE
6882 * Which points to the standard locn, and if that
6883 * fails, silently ignore it and use the internal
6887 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6888 DEBUG(5, ("Could not load config file: %s\n",
6893 * We loaded the global config file. Now lets
6894 * load user-specific modifications to the
6898 "%s/.smb/smb.conf.append",
6900 if (!lp_load(conf, True, False, False, False)) {
6902 ("Could not append config file: "
6911 load_interfaces(); /* Load the list of interfaces ... */
6913 reopen_logs(); /* Get logging working ... */
6916 * Block SIGPIPE (from lib/util_sock.c: write())
6917 * It is not needed and should not stop execution
6919 BlockSignals(True, SIGPIPE);
6921 /* Done with one-time initialisation */
6922 smbc_initialized = 1;
6927 if (!context->user) {
6929 * FIXME: Is this the best way to get the user info?
6931 user = getenv("USER");
6932 /* walk around as "guest" if no username can be found */
6933 if (!user) context->user = SMB_STRDUP("guest");
6934 else context->user = SMB_STRDUP(user);
6937 if (!context->netbios_name) {
6939 * We try to get our netbios name from the config. If that
6940 * fails we fall back on constructing our netbios name from
6943 if (global_myname()) {
6944 context->netbios_name = SMB_STRDUP(global_myname());
6948 * Hmmm, I want to get hostname as well, but I am too
6949 * lazy for the moment
6952 context->netbios_name = (char *)SMB_MALLOC(17);
6953 if (!context->netbios_name) {
6957 slprintf(context->netbios_name, 16,
6958 "smbc%s%d", context->user, pid);
6962 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6964 if (!context->workgroup) {
6965 if (lp_workgroup()) {
6966 context->workgroup = SMB_STRDUP(lp_workgroup());
6969 /* TODO: Think about a decent default workgroup */
6970 context->workgroup = SMB_STRDUP("samba");
6974 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6976 /* shortest timeout is 1 second */
6977 if (context->timeout > 0 && context->timeout < 1000)
6978 context->timeout = 1000;
6981 * FIXME: Should we check the function pointers here?
6984 context->internal->_initialized = True;
6990 /* Return the verion of samba, and thus libsmbclient */
6994 return samba_version_string();