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
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "include/libsmb_internal.h"
29 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir);
30 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
31 struct smbc_dirent *dirent);
34 * DOS Attribute values (used internally)
36 typedef struct DOS_ATTR_DESC {
48 * Internal flags for extended attributes
51 /* internal mode values */
52 #define SMBC_XATTR_MODE_ADD 1
53 #define SMBC_XATTR_MODE_REMOVE 2
54 #define SMBC_XATTR_MODE_REMOVE_ALL 3
55 #define SMBC_XATTR_MODE_SET 4
56 #define SMBC_XATTR_MODE_CHOWN 5
57 #define SMBC_XATTR_MODE_CHGRP 6
59 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
61 /*We should test for this in configure ... */
63 #define ENOTSUP EOPNOTSUPP
67 * Functions exported by libsmb_cache.c that we need here
69 int smbc_default_cache_functions(SMBCCTX *context);
72 * check if an element is part of the list.
73 * FIXME: Does not belong here !
74 * Can anyone put this in a macro in dlinklist.h ?
77 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
78 if (!p || !list) return False;
80 if (p == list) return True;
87 * Find an lsa pipe handle associated with a cli struct.
89 static struct rpc_pipe_client *
90 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
92 struct rpc_pipe_client *pipe_hnd;
94 for (pipe_hnd = ipc_cli->pipe_list;
96 pipe_hnd = pipe_hnd->next) {
98 if (pipe_hnd->pipe_idx == PI_LSARPC) {
107 smbc_close_ctx(SMBCCTX *context,
110 smbc_lseek_ctx(SMBCCTX *context,
115 extern bool in_client;
118 * Is the logging working / configfile read ?
120 static int smbc_initialized = 0;
123 hex2int( unsigned int _char )
125 if ( _char >= 'A' && _char <='F')
126 return _char - 'A' + 10;
127 if ( _char >= 'a' && _char <='f')
128 return _char - 'a' + 10;
129 if ( _char >= '0' && _char <='9')
136 * and smbc_urldecode_talloc() (internal fn.)
138 * Convert strings of %xx to their single character equivalent. Each 'x' must
139 * be a valid hexadecimal digit, or that % sequence is left undecoded.
141 * dest may, but need not be, the same pointer as src.
143 * Returns the number of % sequences which could not be converted due to lack
144 * of two following hexadecimal digits.
147 smbc_urldecode_talloc(TALLOC_CTX *ctx, char **pp_dest, const char *src)
149 int old_length = strlen(src);
155 if (old_length == 0) {
160 for (i = 0; i < old_length; ) {
161 unsigned char character = src[i++];
163 if (character == '%') {
164 int a = i+1 < old_length ? hex2int(src[i]) : -1;
165 int b = i+1 < old_length ? hex2int(src[i+1]) : -1;
167 /* Replace valid sequence */
168 if (a != -1 && b != -1) {
169 /* Replace valid %xx sequence with %dd */
170 character = (a * 16) + b;
171 if (character == '\0') {
172 break; /* Stop at %00 */
182 dest = TALLOC_ARRAY(ctx, char, newlen);
188 for (p = dest, i = 0; i < old_length; ) {
189 unsigned char character = src[i++];
191 if (character == '%') {
192 int a = i+1 < old_length ? hex2int(src[i]) : -1;
193 int b = i+1 < old_length ? hex2int(src[i+1]) : -1;
195 /* Replace valid sequence */
196 if (a != -1 && b != -1) {
197 /* Replace valid %xx sequence with %dd */
198 character = (a * 16) + b;
199 if (character == '\0') {
200 break; /* Stop at %00 */
216 smbc_urldecode(char *dest, char *src, size_t max_dest_len)
218 TALLOC_CTX *frame = talloc_stackframe();
220 int ret = smbc_urldecode_talloc(frame, &pdest, src);
223 strlcpy(dest, pdest, max_dest_len);
232 * Convert any characters not specifically allowed in a URL into their %xx
235 * Returns the remaining buffer length.
238 smbc_urlencode(char *dest, char *src, int max_dest_len)
240 char hex[] = "0123456789ABCDEF";
242 for (; *src != '\0' && max_dest_len >= 3; src++) {
254 *dest++ = hex[(*src >> 4) & 0x0f];
255 *dest++ = hex[*src & 0x0f];
270 * Function to parse a path and turn it into components
272 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
273 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
274 * general format ("smb:" only; we do not look for "cifs:").
278 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
282 * smb:// Show all workgroups.
284 * The method of locating the list of workgroups varies
285 * depending upon the setting of the context variable
286 * context->options.browse_max_lmb_count. This value
287 * determine the maximum number of local master browsers to
288 * query for the list of workgroups. In order to ensure that
289 * a complete list of workgroups is obtained, all master
290 * browsers must be queried, but if there are many
291 * workgroups, the time spent querying can begin to add up.
292 * For small networks (not many workgroups), it is suggested
293 * that this variable be set to 0, indicating query all local
294 * master browsers. When the network has many workgroups, a
295 * reasonable setting for this variable might be around 3.
297 * smb://name/ if name<1D> or name<1B> exists, list servers in
298 * workgroup, else, if name<20> exists, list all shares
301 * If "options" are provided, this function returns the entire option list as a
302 * string, for later parsing by the caller. Note that currently, no options
306 static const char *smbc_prefix = "smb:";
309 smbc_parse_path(TALLOC_CTX *ctx,
325 /* Ensure these returns are at least valid pointers. */
326 *pp_server = talloc_strdup(ctx, "");
327 *pp_share = talloc_strdup(ctx, "");
328 *pp_path = talloc_strdup(ctx, "");
329 *pp_user = talloc_strdup(ctx, "");
330 *pp_password = talloc_strdup(ctx, "");
332 if (!*pp_server || !*pp_share || !*pp_path ||
333 !*pp_user || !*pp_password) {
338 * Assume we wont find an authentication domain to parse, so default
339 * to the workgroup in the provided context.
341 if (pp_workgroup != NULL) {
342 *pp_workgroup = talloc_strdup(ctx, context->workgroup);
346 *pp_options = talloc_strdup(ctx, "");
348 s = talloc_strdup(ctx, fname);
350 /* see if it has the right prefix */
351 len = strlen(smbc_prefix);
352 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
353 return -1; /* What about no smb: ? */
358 /* Watch the test below, we are testing to see if we should exit */
360 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
361 DEBUG(1, ("Invalid path (does not begin with smb://"));
365 p += 2; /* Skip the double slash */
367 /* See if any options were specified */
368 if ((q = strrchr(p, '?')) != NULL ) {
369 /* There are options. Null terminate here and point to them */
372 DEBUG(4, ("Found options '%s'", q));
374 /* Copy the options */
375 if (*pp_options != NULL) {
376 TALLOC_FREE(*pp_options);
377 *pp_options = talloc_strdup(ctx, q);
386 int wl = strlen(context->workgroup);
392 *pp_server = talloc_strdup(ctx, context->workgroup);
396 *pp_server[wl] = '\0';
401 * ok, its for us. Now parse out the server, share etc.
403 * However, we want to parse out [[domain;]user[:password]@] if it
407 /* check that '@' occurs before '/', if '/' exists at all */
408 q = strchr_m(p, '@');
409 r = strchr_m(p, '/');
410 if (q && (!r || q < r)) {
411 char *userinfo = NULL;
414 next_token_no_ltrim_talloc(ctx, &p, &userinfo, "@");
420 if (strchr_m(u, ';')) {
422 next_token_no_ltrim_talloc(ctx, &u, &workgroup, ";");
427 *pp_workgroup = workgroup;
431 if (strchr_m(u, ':')) {
432 next_token_no_ltrim_talloc(ctx, &u, pp_user, ":");
436 *pp_password = talloc_strdup(ctx, u);
441 *pp_user = talloc_strdup(ctx, u);
448 if (!next_token_talloc(ctx, &p, pp_server, "/")) {
453 goto decoding; /* That's it ... */
456 if (!next_token_talloc(ctx, &p, pp_share, "/")) {
461 * Prepend a leading slash if there's a file path, as required by
465 *pp_path = talloc_asprintf(ctx,
469 *pp_path = talloc_strdup(ctx, "");
474 string_replace(*pp_path, '/', '\\');
478 (void) smbc_urldecode_talloc(ctx, pp_path, *pp_path);
479 (void) smbc_urldecode_talloc(ctx, pp_server, *pp_server);
480 (void) smbc_urldecode_talloc(ctx, pp_share, *pp_share);
481 (void) smbc_urldecode_talloc(ctx, pp_user, *pp_user);
482 (void) smbc_urldecode_talloc(ctx, pp_password, *pp_password);
488 * Verify that the options specified in a URL are valid
491 smbc_check_options(char *server,
496 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
497 "path='%s' options='%s'\n",
498 server, share, path, options));
500 /* No options at all is always ok */
501 if (! *options) return 0;
503 /* Currently, we don't support any options. */
508 * Convert an SMB error into a UNIX error ...
511 smbc_errno(SMBCCTX *context,
514 int ret = cli_errno(c);
516 if (cli_is_dos_error(c)) {
520 cli_dos_error(c, &eclass, &ecode);
522 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
523 (int)eclass, (int)ecode, (int)ecode, ret));
527 status = cli_nt_error(c);
529 DEBUG(3,("smbc errno %s -> %d\n",
530 nt_errstr(status), ret));
537 * Check a server for being alive and well.
538 * returns 0 if the server is in shape. Returns 1 on error
540 * Also useable outside libsmbclient to enable external cache
541 * to do some checks too.
544 smbc_check_server(SMBCCTX * context,
548 struct sockaddr addr;
551 return (getpeername(server->cli->fd, &addr, &size) == -1);
555 * Remove a server from the cached server list it's unused.
556 * On success, 0 is returned. 1 is returned if the server could not be removed.
558 * Also useable outside libsmbclient
561 smbc_remove_unused_server(SMBCCTX * context,
566 /* are we being fooled ? */
567 if (!context || !context->internal ||
568 !context->internal->_initialized || !srv) return 1;
571 /* Check all open files/directories for a relation with this server */
572 for (file = context->internal->_files; file; file=file->next) {
573 if (file->srv == srv) {
575 DEBUG(3, ("smbc_remove_usused_server: "
576 "%p still used by %p.\n",
582 DLIST_REMOVE(context->internal->_servers, srv);
584 cli_shutdown(srv->cli);
587 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
589 (context->callbacks.remove_cached_srv_fn)(context, srv);
596 find_server(SMBCCTX *context,
608 srv = (context->callbacks.get_cached_srv_fn)(context, server, share,
609 workgroup, username);
611 if (!auth_called && !srv && (!username[0] || !password[0])) {
612 if (context->internal->_auth_fn_with_context != NULL) {
613 (context->internal->_auth_fn_with_context)(
616 workgroup, strlen(workgroup)+1,
617 username, strlen(username)+1,
618 password, strlen(password)+1);
620 (context->callbacks.auth_fn)(
622 workgroup, strlen(workgroup)+1,
623 username, strlen(username)+1,
624 password, strlen(password)+1);
628 * However, smbc_auth_fn may have picked up info relating to
629 * an existing connection, so try for an existing connection
633 goto check_server_cache;
638 if ((context->callbacks.check_server_fn)(context, srv)) {
640 * This server is no good anymore
641 * Try to remove it and check for more possible
642 * servers in the cache
644 if ((context->callbacks.remove_unused_server_fn)(context,
647 * We could not remove the server completely,
648 * remove it from the cache so we will not get
649 * it again. It will be removed when the last
650 * file/dir is closed.
652 (context->callbacks.remove_cached_srv_fn)(context,
657 * Maybe there are more cached connections to this
660 goto check_server_cache;
670 * Connect to a server, possibly on an existing connection
672 * Here, what we want to do is: If the server and username
673 * match an existing connection, reuse that, otherwise, establish a
676 * If we have to create a new connection, call the auth_fn to get the
677 * info we need, unless the username and password were passed in.
681 smbc_server(SMBCCTX *context,
682 bool connect_if_not_found,
691 struct nmb_name called, calling;
692 const char *server_n = server;
693 struct sockaddr_storage ss;
694 int tried_reverse = 0;
697 const char *username_used;
703 if (server[0] == 0) {
708 /* Look for a cached connection */
709 srv = find_server(context, server, share,
710 workgroup, username, password);
713 * If we found a connection and we're only allowed one share per
716 if (srv && *share != '\0' && context->options.one_share_per_server) {
719 * ... then if there's no current connection to the share,
720 * connect to it. find_server(), or rather the function
721 * pointed to by context->callbacks.get_cached_srv_fn which
722 * was called by find_server(), will have issued a tree
723 * disconnect if the requested share is not the same as the
724 * one that was already connected.
726 if (srv->cli->cnum == (uint16) -1) {
727 /* Ensure we have accurate auth info */
728 if (context->internal->_auth_fn_with_context != NULL) {
729 (context->internal->_auth_fn_with_context)(
732 workgroup, strlen(workgroup)+1,
733 username, strlen(username)+1,
734 password, strlen(password)+1);
736 (context->callbacks.auth_fn)(
738 workgroup, strlen(workgroup)+1,
739 username, strlen(username)+1,
740 password, strlen(password)+1);
744 * We don't need to renegotiate encryption
745 * here as the encryption context is not per
749 if (! cli_send_tconX(srv->cli, share, "?????",
750 password, strlen(password)+1)) {
752 errno = smbc_errno(context, srv->cli);
753 cli_shutdown(srv->cli);
755 (context->callbacks.remove_cached_srv_fn)(context,
761 * Regenerate the dev value since it's based on both
765 srv->dev = (dev_t)(str_checksum(server) ^
766 str_checksum(share));
771 /* If we have a connection... */
774 /* ... then we're done here. Give 'em what they came for. */
778 /* If we're not asked to connect when a connection doesn't exist... */
779 if (! connect_if_not_found) {
780 /* ... then we're done here. */
784 make_nmb_name(&calling, context->netbios_name, 0x0);
785 make_nmb_name(&called , server, 0x20);
787 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
789 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
795 /* have to open a new connection */
796 if ((c = cli_initialise()) == NULL) {
801 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
802 c->use_kerberos = True;
804 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
805 c->fallback_after_kerberos = True;
808 c->timeout = context->timeout;
811 * Force use of port 139 for first try if share is $IPC, empty, or
812 * null, so browse lists can work
814 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
815 port_try_first = 139;
818 port_try_first = 445;
822 c->port = port_try_first;
824 status = cli_connect(c, server_n, &ss);
825 if (!NT_STATUS_IS_OK(status)) {
827 /* First connection attempt failed. Try alternate port. */
828 c->port = port_try_next;
830 status = cli_connect(c, server_n, &ss);
831 if (!NT_STATUS_IS_OK(status)) {
838 if (!cli_session_request(c, &calling, &called)) {
840 if (strcmp(called.name, "*SMBSERVER")) {
841 make_nmb_name(&called , "*SMBSERVER", 0x20);
843 } else { /* Try one more time, but ensure we don't loop */
845 /* Only try this if server is an IP address ... */
847 if (is_ipaddress(server) && !tried_reverse) {
849 struct sockaddr_storage rem_ss;
851 if (!interpret_string_addr(&rem_ss, server,
853 DEBUG(4, ("Could not convert IP address "
854 "%s to struct sockaddr_storage\n",
860 tried_reverse++; /* Yuck */
862 if (name_status_find("*", 0, 0, &rem_ss, remote_name)) {
863 make_nmb_name(&called, remote_name, 0x20);
872 DEBUG(4,(" session request ok\n"));
874 if (!cli_negprot(c)) {
880 username_used = username;
882 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
883 password, strlen(password),
884 password, strlen(password),
887 /* Failed. Try an anonymous login, if allowed by flags. */
890 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
891 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
902 DEBUG(4,(" session setup ok\n"));
904 if (!cli_send_tconX(c, share, "?????",
905 password, strlen(password)+1)) {
906 errno = smbc_errno(context, c);
911 DEBUG(4,(" tconx ok\n"));
913 if (context->internal->_smb_encryption_level) {
914 /* Attempt UNIX smb encryption. */
915 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
921 * context->internal->_smb_encryption_level == 1
922 * means don't fail if encryption can't be negotiated,
923 * == 2 means fail if encryption can't be negotiated.
926 DEBUG(4,(" SMB encrypt failed\n"));
928 if (context->internal->_smb_encryption_level == 2) {
934 DEBUG(4,(" SMB encrypt ok\n"));
938 * Ok, we have got a nice connection
939 * Let's allocate a server structure.
942 srv = SMB_MALLOC_P(SMBCSRV);
950 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
951 srv->no_pathinfo = False;
952 srv->no_pathinfo2 = False;
953 srv->no_nt_session = False;
955 /* now add it to the cache (internal or external) */
956 /* Let the cache function set errno if it wants to */
958 if ((context->callbacks.add_cached_srv_fn)(context, srv,
960 workgroup, username)) {
961 int saved_errno = errno;
962 DEBUG(3, (" Failed to add server to cache\n"));
970 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
971 server, share, srv));
973 DLIST_ADD(context->internal->_servers, srv);
987 * Connect to a server for getting/setting attributes, possibly on an existing
988 * connection. This works similarly to smbc_server().
991 smbc_attr_server(SMBCCTX *context,
1000 struct sockaddr_storage ss;
1001 struct cli_state *ipc_cli;
1002 struct rpc_pipe_client *pipe_hnd;
1004 SMBCSRV *ipc_srv=NULL;
1007 * See if we've already created this special connection. Reference
1008 * our "special" share name '*IPC$', which is an impossible real share
1009 * name due to the leading asterisk.
1011 ipc_srv = find_server(context, server, "*IPC$",
1012 workgroup, username, password);
1015 /* We didn't find a cached connection. Get the password */
1016 if (*password == '\0') {
1017 /* ... then retrieve it now. */
1018 if (context->internal->_auth_fn_with_context != NULL) {
1019 (context->internal->_auth_fn_with_context)(
1022 workgroup, strlen(workgroup)+1,
1023 username, strlen(username)+1,
1024 password, strlen(password)+1);
1026 (context->callbacks.auth_fn)(
1028 workgroup, strlen(workgroup)+1,
1029 username, strlen(username)+1,
1030 password, strlen(password)+1);
1035 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
1036 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1040 nt_status = cli_full_connection(&ipc_cli,
1041 global_myname(), server,
1042 &ss, 0, "IPC$", "?????",
1043 username, workgroup,
1046 if (! NT_STATUS_IS_OK(nt_status)) {
1047 DEBUG(1,("cli_full_connection failed! (%s)\n",
1048 nt_errstr(nt_status)));
1053 if (context->internal->_smb_encryption_level) {
1054 /* Attempt UNIX smb encryption. */
1055 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
1061 * context->internal->_smb_encryption_level == 1
1062 * means don't fail if encryption can't be negotiated,
1063 * == 2 means fail if encryption can't be negotiated.
1066 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
1068 if (context->internal->_smb_encryption_level == 2) {
1069 cli_shutdown(ipc_cli);
1074 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
1077 ipc_srv = SMB_MALLOC_P(SMBCSRV);
1080 cli_shutdown(ipc_cli);
1084 ZERO_STRUCTP(ipc_srv);
1085 ipc_srv->cli = ipc_cli;
1088 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1092 DEBUG(1, ("cli_nt_session_open fail!\n"));
1094 cli_shutdown(ipc_srv->cli);
1100 * Some systems don't support
1101 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1102 * so we might as well do it too.
1105 nt_status = rpccli_lsa_open_policy(
1109 GENERIC_EXECUTE_ACCESS,
1112 if (!NT_STATUS_IS_OK(nt_status)) {
1113 errno = smbc_errno(context, ipc_srv->cli);
1114 cli_shutdown(ipc_srv->cli);
1119 /* now add it to the cache (internal or external) */
1121 errno = 0; /* let cache function set errno if it likes */
1122 if ((context->callbacks.add_cached_srv_fn)(context, ipc_srv,
1127 DEBUG(3, (" Failed to add server to cache\n"));
1131 cli_shutdown(ipc_srv->cli);
1136 DLIST_ADD(context->internal->_servers, ipc_srv);
1143 * Routine to open() a file ...
1147 smbc_open_ctx(SMBCCTX *context,
1152 char *server, *share, *user, *password, *workgroup;
1154 char *targetpath = NULL;
1155 struct cli_state *targetcli;
1156 SMBCSRV *srv = NULL;
1157 SMBCFILE *file = NULL;
1159 TALLOC_CTX *frame = talloc_stackframe();
1161 if (!context || !context->internal ||
1162 !context->internal->_initialized) {
1164 errno = EINVAL; /* Best I can think of ... */
1178 if (smbc_parse_path(frame,
1193 if (!user || user[0] == (char)0) {
1194 user = talloc_strdup(frame, context->user);
1202 srv = smbc_server(context, True,
1203 server, share, workgroup, user, password);
1206 if (errno == EPERM) errno = EACCES;
1208 return NULL; /* smbc_server sets errno */
1211 /* Hmmm, the test for a directory is suspect here ... FIXME */
1213 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1216 file = SMB_MALLOC_P(SMBCFILE);
1226 /*d_printf(">>>open: resolving %s\n", path);*/
1227 if (!cli_resolve_path(frame, "", srv->cli, path, &targetcli, &targetpath)) {
1228 d_printf("Could not resolve %s\n", path);
1233 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1235 if ((fd = cli_open(targetcli, targetpath, flags,
1236 context->internal->_share_mode)) < 0) {
1238 /* Handle the error ... */
1241 errno = smbc_errno(context, targetcli);
1247 /* Fill in file struct */
1250 file->fname = SMB_STRDUP(fname);
1255 DLIST_ADD(context->internal->_files, file);
1258 * If the file was opened in O_APPEND mode, all write
1259 * operations should be appended to the file. To do that,
1260 * though, using this protocol, would require a getattrE()
1261 * call for each and every write, to determine where the end
1262 * of the file is. (There does not appear to be an append flag
1263 * in the protocol.) Rather than add all of that overhead of
1264 * retrieving the current end-of-file offset prior to each
1265 * write operation, we'll assume that most append operations
1266 * will continuously write, so we'll just set the offset to
1267 * the end of the file now and hope that's adequate.
1269 * Note to self: If this proves inadequate, and O_APPEND
1270 * should, in some cases, be forced for each write, add a
1271 * field in the context options structure, for
1272 * "strict_append_mode" which would select between the current
1273 * behavior (if FALSE) or issuing a getattrE() prior to each
1274 * write and forcing the write to the end of the file (if
1275 * TRUE). Adding that capability will likely require adding
1276 * an "append" flag into the _SMBCFILE structure to track
1277 * whether a file was opened in O_APPEND mode. -- djl
1279 if (flags & O_APPEND) {
1280 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1281 (void) smbc_close_ctx(context, file);
1293 /* Check if opendir needed ... */
1298 eno = smbc_errno(context, srv->cli);
1299 file = (context->opendir)(context, fname);
1300 if (!file) errno = eno;
1306 errno = EINVAL; /* FIXME, correct errno ? */
1313 * Routine to create a file
1316 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1319 smbc_creat_ctx(SMBCCTX *context,
1324 if (!context || !context->internal ||
1325 !context->internal->_initialized) {
1332 return smbc_open_ctx(context, path, creat_bits, mode);
1336 * Routine to read() a file ...
1340 smbc_read_ctx(SMBCCTX *context,
1346 char *server, *share, *user, *password;
1348 char *targetpath = NULL;
1349 struct cli_state *targetcli;
1350 TALLOC_CTX *frame = talloc_stackframe();
1355 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1356 * appears to pass file->offset (which is type off_t) differently than
1357 * a local variable of type off_t. Using local variable "offset" in
1358 * the call to cli_read() instead of file->offset fixes a problem
1359 * retrieving data at an offset greater than 4GB.
1363 if (!context || !context->internal ||
1364 !context->internal->_initialized) {
1371 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1373 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1380 offset = file->offset;
1382 /* Check that the buffer exists ... */
1391 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1392 if (smbc_parse_path(frame,
1407 /*d_printf(">>>read: resolving %s\n", path);*/
1408 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1409 &targetcli, &targetpath)) {
1410 d_printf("Could not resolve %s\n", path);
1414 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1416 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1420 errno = smbc_errno(context, targetcli);
1426 file->offset += ret;
1428 DEBUG(4, (" --> %d\n", ret));
1431 return ret; /* Success, ret bytes of data ... */
1436 * Routine to write() a file ...
1440 smbc_write_ctx(SMBCCTX *context,
1447 char *server, *share, *user, *password;
1449 char *targetpath = NULL;
1450 struct cli_state *targetcli;
1451 TALLOC_CTX *frame = talloc_stackframe();
1453 /* First check all pointers before dereferencing them */
1455 if (!context || !context->internal ||
1456 !context->internal->_initialized) {
1463 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1469 /* Check that the buffer exists ... */
1478 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1480 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1481 if (smbc_parse_path(frame,
1496 /*d_printf(">>>write: resolving %s\n", path);*/
1497 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1498 &targetcli, &targetpath)) {
1499 d_printf("Could not resolve %s\n", path);
1503 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1505 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1508 errno = smbc_errno(context, targetcli);
1514 file->offset += ret;
1517 return ret; /* Success, 0 bytes of data ... */
1521 * Routine to close() a file ...
1525 smbc_close_ctx(SMBCCTX *context,
1529 char *server, *share, *user, *password;
1531 char *targetpath = NULL;
1532 struct cli_state *targetcli;
1533 TALLOC_CTX *frame = talloc_stackframe();
1535 if (!context || !context->internal ||
1536 !context->internal->_initialized) {
1543 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1552 return (context->closedir)(context, file);
1555 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1556 if (smbc_parse_path(frame,
1571 /*d_printf(">>>close: resolving %s\n", path);*/
1572 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1573 &targetcli, &targetpath)) {
1574 d_printf("Could not resolve %s\n", path);
1578 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1580 if (!cli_close(targetcli, file->cli_fd)) {
1582 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1584 /* Deallocate slot and remove the server
1585 * from the server cache if unused */
1586 errno = smbc_errno(context, targetcli);
1588 DLIST_REMOVE(context->internal->_files, file);
1589 SAFE_FREE(file->fname);
1591 (context->callbacks.remove_unused_server_fn)(context, srv);
1597 DLIST_REMOVE(context->internal->_files, file);
1598 SAFE_FREE(file->fname);
1606 * Get info from an SMB server on a file. Use a qpathinfo call first
1607 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1610 smbc_getatr(SMBCCTX * context,
1615 struct timespec *create_time_ts,
1616 struct timespec *access_time_ts,
1617 struct timespec *write_time_ts,
1618 struct timespec *change_time_ts,
1622 char *targetpath = NULL;
1623 struct cli_state *targetcli;
1625 TALLOC_CTX *frame = talloc_stackframe();
1627 if (!context || !context->internal ||
1628 !context->internal->_initialized) {
1634 /* path fixup for . and .. */
1635 if (strequal(path, ".") || strequal(path, "..")) {
1636 fixedpath = talloc_strdup(frame, "\\");
1643 fixedpath = talloc_strdup(frame, path);
1649 trim_string(fixedpath, NULL, "\\..");
1650 trim_string(fixedpath, NULL, "\\.");
1652 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1654 if (!cli_resolve_path(frame, "", srv->cli, fixedpath,
1655 &targetcli, &targetpath)) {
1656 d_printf("Couldn't resolve %s\n", path);
1661 if (!srv->no_pathinfo2 &&
1662 cli_qpathinfo2(targetcli, targetpath,
1672 /* if this is NT then don't bother with the getatr */
1673 if (targetcli->capabilities & CAP_NT_SMBS) {
1679 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1681 struct timespec w_time_ts;
1683 w_time_ts = convert_time_t_to_timespec(write_time);
1685 if (write_time_ts != NULL) {
1686 *write_time_ts = w_time_ts;
1689 if (create_time_ts != NULL) {
1690 *create_time_ts = w_time_ts;
1693 if (access_time_ts != NULL) {
1694 *access_time_ts = w_time_ts;
1697 if (change_time_ts != NULL) {
1698 *change_time_ts = w_time_ts;
1701 srv->no_pathinfo2 = True;
1713 * Set file info on an SMB server. Use setpathinfo call first. If that
1714 * fails, use setattrE..
1716 * Access and modification time parameters are always used and must be
1717 * provided. Create time, if zero, will be determined from the actual create
1718 * time of the file. If non-zero, the create time will be set as well.
1720 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1723 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1732 TALLOC_CTX *frame = talloc_stackframe();
1735 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1736 * modern function for "new code" to be using, and it works given a
1737 * filename rather than requiring that the file be opened to have its
1738 * attributes manipulated.
1740 if (srv->no_pathinfo ||
1741 ! cli_setpathinfo(srv->cli, path,
1749 * setpathinfo is not supported; go to plan B.
1751 * cli_setatr() does not work on win98, and it also doesn't
1752 * support setting the access time (only the modification
1753 * time), so in all cases, we open the specified file and use
1754 * cli_setattrE() which should work on all OS versions, and
1755 * supports both times.
1758 /* Don't try {q,set}pathinfo() again, with this server */
1759 srv->no_pathinfo = True;
1762 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1764 errno = smbc_errno(context, srv->cli);
1769 /* Set the new attributes */
1770 ret = cli_setattrE(srv->cli, fd,
1775 /* Close the file */
1776 cli_close(srv->cli, fd);
1779 * Unfortunately, setattrE() doesn't have a provision for
1780 * setting the access mode (attributes). We'll have to try
1781 * cli_setatr() for that, and with only this parameter, it
1782 * seems to work on win98.
1784 if (ret && mode != (uint16) -1) {
1785 ret = cli_setatr(srv->cli, path, mode, 0);
1789 errno = smbc_errno(context, srv->cli);
1800 * Routine to unlink() a file
1804 smbc_unlink_ctx(SMBCCTX *context,
1807 char *server, *share, *user, *password, *workgroup;
1810 struct cli_state *targetcli;
1811 SMBCSRV *srv = NULL;
1812 TALLOC_CTX *frame = talloc_stackframe();
1814 if (!context || !context->internal ||
1815 !context->internal->_initialized) {
1816 errno = EINVAL; /* Best I can think of ... */
1829 if (smbc_parse_path(frame,
1844 if (!user || user[0] == (char)0) {
1845 user = talloc_strdup(frame, context->user);
1853 srv = smbc_server(context, True,
1854 server, share, workgroup, user, password);
1858 return -1; /* smbc_server sets errno */
1862 /*d_printf(">>>unlink: resolving %s\n", path);*/
1863 if (!cli_resolve_path(frame, "", srv->cli, path,
1864 &targetcli, &targetpath)) {
1865 d_printf("Could not resolve %s\n", path);
1869 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1871 if (!cli_unlink(targetcli, targetpath)) {
1873 errno = smbc_errno(context, targetcli);
1875 if (errno == EACCES) { /* Check if the file is a directory */
1880 struct timespec write_time_ts;
1881 struct timespec access_time_ts;
1882 struct timespec change_time_ts;
1885 if (!smbc_getatr(context, srv, path, &mode, &size,
1892 /* Hmmm, bad error ... What? */
1894 errno = smbc_errno(context, targetcli);
1901 if (IS_DOS_DIR(mode))
1904 errno = saverr; /* Restore this */
1915 return 0; /* Success ... */
1920 * Routine to rename() a file
1924 smbc_rename_ctx(SMBCCTX *ocontext,
1942 struct cli_state *targetcli1;
1943 struct cli_state *targetcli2;
1944 SMBCSRV *srv = NULL;
1945 TALLOC_CTX *frame = talloc_stackframe();
1947 if (!ocontext || !ncontext ||
1948 !ocontext->internal || !ncontext->internal ||
1949 !ocontext->internal->_initialized ||
1950 !ncontext->internal->_initialized) {
1951 errno = EINVAL; /* Best I can think of ... */
1956 if (!oname || !nname) {
1962 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1964 if (smbc_parse_path(frame,
1979 if (!user1 || user1[0] == (char)0) {
1980 user1 = talloc_strdup(frame, ocontext->user);
1988 if (smbc_parse_path(frame,
2003 if (!user2 || user2[0] == (char)0) {
2004 user2 = talloc_strdup(frame, ncontext->user);
2012 if (strcmp(server1, server2) || strcmp(share1, share2) ||
2013 strcmp(user1, user2)) {
2014 /* Can't rename across file systems, or users?? */
2020 srv = smbc_server(ocontext, True,
2021 server1, share1, workgroup, user1, password1);
2028 /*d_printf(">>>rename: resolving %s\n", path1);*/
2029 if (!cli_resolve_path(frame, "", srv->cli, path1,
2030 &targetcli1, &targetpath1)) {
2031 d_printf("Could not resolve %s\n", path1);
2035 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2036 /*d_printf(">>>rename: resolving %s\n", path2);*/
2037 if (!cli_resolve_path(frame, "", srv->cli, path2,
2038 &targetcli2, &targetpath2)) {
2039 d_printf("Could not resolve %s\n", path2);
2043 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2045 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
2046 strcmp(targetcli1->share, targetcli2->share))
2048 /* can't rename across file systems */
2054 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
2055 int eno = smbc_errno(ocontext, targetcli1);
2057 if (eno != EEXIST ||
2058 !cli_unlink(targetcli1, targetpath2) ||
2059 !cli_rename(targetcli1, targetpath1, targetpath2)) {
2069 return 0; /* Success */
2073 * A routine to lseek() a file
2077 smbc_lseek_ctx(SMBCCTX *context,
2083 char *server, *share, *user, *password;
2086 struct cli_state *targetcli;
2087 TALLOC_CTX *frame = talloc_stackframe();
2089 if (!context || !context->internal ||
2090 !context->internal->_initialized) {
2096 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2108 return -1; /* Can't lseek a dir ... */
2114 file->offset = offset;
2118 file->offset += offset;
2122 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
2123 if (smbc_parse_path(frame,
2138 /*d_printf(">>>lseek: resolving %s\n", path);*/
2139 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2140 &targetcli, &targetpath)) {
2141 d_printf("Could not resolve %s\n", path);
2145 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
2147 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
2148 &size, NULL, NULL, NULL, NULL, NULL))
2150 SMB_OFF_T b_size = size;
2151 if (!cli_getattrE(targetcli, file->cli_fd,
2152 NULL, &b_size, NULL, NULL, NULL))
2160 file->offset = size + offset;
2170 return file->offset;
2175 * Generate an inode number from file name for those things that need it
2179 smbc_inode(SMBCCTX *context,
2182 if (!context || !context->internal ||
2183 !context->internal->_initialized) {
2190 if (!*name) return 2; /* FIXME, why 2 ??? */
2191 return (ino_t)str_checksum(name);
2196 * Routine to put basic stat info into a stat structure ... Used by stat and
2201 smbc_setup_stat(SMBCCTX *context,
2207 TALLOC_CTX *frame = talloc_stackframe();
2211 if (IS_DOS_DIR(mode)) {
2212 st->st_mode = SMBC_DIR_MODE;
2214 st->st_mode = SMBC_FILE_MODE;
2217 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2218 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2219 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2220 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2223 #ifdef HAVE_STAT_ST_BLKSIZE
2224 st->st_blksize = 512;
2226 #ifdef HAVE_STAT_ST_BLOCKS
2227 st->st_blocks = (size+511)/512;
2229 st->st_uid = getuid();
2230 st->st_gid = getgid();
2232 if (IS_DOS_DIR(mode)) {
2238 if (st->st_ino == 0) {
2239 st->st_ino = smbc_inode(context, fname);
2243 return True; /* FIXME: Is this needed ? */
2248 * Routine to stat a file given a name
2252 smbc_stat_ctx(SMBCCTX *context,
2263 struct timespec write_time_ts;
2264 struct timespec access_time_ts;
2265 struct timespec change_time_ts;
2269 TALLOC_CTX *frame = talloc_stackframe();
2271 if (!context || !context->internal ||
2272 !context->internal->_initialized) {
2274 errno = EINVAL; /* Best I can think of ... */
2285 DEBUG(4, ("smbc_stat(%s)\n", fname));
2287 if (smbc_parse_path(frame,
2302 if (!user || user[0] == (char)0) {
2303 user = talloc_strdup(frame,context->user);
2311 srv = smbc_server(context, True,
2312 server, share, workgroup, user, password);
2316 return -1; /* errno set by smbc_server */
2319 if (!smbc_getatr(context, srv, path, &mode, &size,
2325 errno = smbc_errno(context, srv->cli);
2332 smbc_setup_stat(context, st, path, size, mode);
2334 set_atimespec(st, access_time_ts);
2335 set_ctimespec(st, change_time_ts);
2336 set_mtimespec(st, write_time_ts);
2337 st->st_dev = srv->dev;
2345 * Routine to stat a file given an fd
2349 smbc_fstat_ctx(SMBCCTX *context,
2353 struct timespec change_time_ts;
2354 struct timespec access_time_ts;
2355 struct timespec write_time_ts;
2364 struct cli_state *targetcli;
2366 TALLOC_CTX *frame = talloc_stackframe();
2368 if (!context || !context->internal ||
2369 !context->internal->_initialized) {
2375 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2383 return (context->fstatdir)(context, file, st);
2386 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2387 if (smbc_parse_path(frame,
2402 /*d_printf(">>>fstat: resolving %s\n", path);*/
2403 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2404 &targetcli, &targetpath)) {
2405 d_printf("Could not resolve %s\n", path);
2409 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2411 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2418 time_t change_time, access_time, write_time;
2420 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2421 &change_time, &access_time, &write_time)) {
2428 change_time_ts = convert_time_t_to_timespec(change_time);
2429 access_time_ts = convert_time_t_to_timespec(access_time);
2430 write_time_ts = convert_time_t_to_timespec(write_time);
2435 smbc_setup_stat(context, st, file->fname, size, mode);
2437 set_atimespec(st, access_time_ts);
2438 set_ctimespec(st, change_time_ts);
2439 set_mtimespec(st, write_time_ts);
2440 st->st_dev = file->srv->dev;
2448 * Routine to open a directory
2449 * We accept the URL syntax explained in smbc_parse_path(), above.
2453 smbc_remove_dir(SMBCFILE *dir)
2455 struct smbc_dir_list *d,*f;
2462 SAFE_FREE(f->dirent);
2467 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2472 add_dirent(SMBCFILE *dir,
2474 const char *comment,
2477 struct smbc_dirent *dirent;
2479 int name_length = (name == NULL ? 0 : strlen(name));
2480 int comment_len = (comment == NULL ? 0 : strlen(comment));
2483 * Allocate space for the dirent, which must be increased by the
2484 * size of the name and the comment and 1 each for the null terminator.
2487 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2489 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2493 dir->dir_error = ENOMEM;
2498 ZERO_STRUCTP(dirent);
2500 if (dir->dir_list == NULL) {
2502 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2503 if (!dir->dir_list) {
2506 dir->dir_error = ENOMEM;
2510 ZERO_STRUCTP(dir->dir_list);
2512 dir->dir_end = dir->dir_next = dir->dir_list;
2516 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2518 if (!dir->dir_end->next) {
2521 dir->dir_error = ENOMEM;
2525 ZERO_STRUCTP(dir->dir_end->next);
2527 dir->dir_end = dir->dir_end->next;
2530 dir->dir_end->next = NULL;
2531 dir->dir_end->dirent = dirent;
2533 dirent->smbc_type = type;
2534 dirent->namelen = name_length;
2535 dirent->commentlen = comment_len;
2536 dirent->dirlen = size;
2539 * dirent->namelen + 1 includes the null (no null termination needed)
2540 * Ditto for dirent->commentlen.
2541 * The space for the two null bytes was allocated.
2543 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2544 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2545 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2552 list_unique_wg_fn(const char *name,
2554 const char *comment,
2557 SMBCFILE *dir = (SMBCFILE *)state;
2558 struct smbc_dir_list *dir_list;
2559 struct smbc_dirent *dirent;
2563 dirent_type = dir->dir_type;
2565 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2567 /* An error occurred, what do we do? */
2568 /* FIXME: Add some code here */
2571 /* Point to the one just added */
2572 dirent = dir->dir_end->dirent;
2574 /* See if this was a duplicate */
2575 for (dir_list = dir->dir_list;
2576 dir_list != dir->dir_end;
2577 dir_list = dir_list->next) {
2579 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2580 /* Duplicate. End end of list need to be removed. */
2584 if (do_remove && dir_list->next == dir->dir_end) {
2585 /* Found the end of the list. Remove it. */
2586 dir->dir_end = dir_list;
2587 free(dir_list->next);
2589 dir_list->next = NULL;
2596 list_fn(const char *name,
2598 const char *comment,
2601 SMBCFILE *dir = (SMBCFILE *)state;
2605 * We need to process the type a little ...
2607 * Disk share = 0x00000000
2608 * Print share = 0x00000001
2609 * Comms share = 0x00000002 (obsolete?)
2610 * IPC$ share = 0x00000003
2612 * administrative shares:
2613 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2616 if (dir->dir_type == SMBC_FILE_SHARE) {
2618 case 0 | 0x80000000:
2620 dirent_type = SMBC_FILE_SHARE;
2624 dirent_type = SMBC_PRINTER_SHARE;
2628 dirent_type = SMBC_COMMS_SHARE;
2631 case 3 | 0x80000000:
2633 dirent_type = SMBC_IPC_SHARE;
2637 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2642 dirent_type = dir->dir_type;
2645 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2647 /* An error occurred, what do we do? */
2648 /* FIXME: Add some code here */
2654 dir_list_fn(const char *mnt,
2660 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2661 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2663 /* Handle an error ... */
2665 /* FIXME: Add some code ... */
2672 net_share_enum_rpc(struct cli_state *cli,
2673 void (*fn)(const char *name,
2675 const char *comment,
2682 uint32 info_level = 1;
2683 uint32 preferred_len = 0xffffffff;
2685 SRV_SHARE_INFO_CTR ctr;
2687 fstring comment = "";
2688 struct rpc_pipe_client *pipe_hnd;
2691 /* Open the server service pipe */
2692 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2694 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2698 /* Issue the NetShareEnum RPC call and retrieve the response */
2699 init_enum_hnd(&enum_hnd, 0);
2700 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2707 /* Was it successful? */
2708 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2709 /* Nope. Go clean up. */
2713 /* For each returned entry... */
2714 for (i = 0; i < ctr.num_entries; i++) {
2716 /* pull out the share name */
2717 rpcstr_pull_unistr2_fstring(
2718 name, &ctr.share.info1[i].info_1_str.uni_netname);
2720 /* pull out the share's comment */
2721 rpcstr_pull_unistr2_fstring(
2722 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2724 /* Get the type value */
2725 type = ctr.share.info1[i].info_1.type;
2727 /* Add this share to the list */
2728 (*fn)(name, type, comment, state);
2732 /* Close the server service pipe */
2733 cli_rpc_pipe_close(pipe_hnd);
2735 /* Tell 'em if it worked */
2736 return W_ERROR_IS_OK(result) ? 0 : -1;
2742 smbc_opendir_ctx(SMBCCTX *context,
2746 char *server, *share, *user, *password, *options;
2751 SMBCSRV *srv = NULL;
2752 SMBCFILE *dir = NULL;
2753 struct _smbc_callbacks *cb;
2754 struct sockaddr_storage rem_ss;
2755 TALLOC_CTX *frame = talloc_stackframe();
2757 if (!context || !context->internal ||
2758 !context->internal->_initialized) {
2759 DEBUG(4, ("no valid context\n"));
2760 errno = EINVAL + 8192;
2767 DEBUG(4, ("no valid fname\n"));
2768 errno = EINVAL + 8193;
2773 if (smbc_parse_path(frame,
2783 DEBUG(4, ("no valid path\n"));
2784 errno = EINVAL + 8194;
2789 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2790 "path='%s' options='%s'\n",
2791 fname, server, share, path, options));
2793 /* Ensure the options are valid */
2794 if (smbc_check_options(server, share, path, options)) {
2795 DEBUG(4, ("unacceptable options (%s)\n", options));
2796 errno = EINVAL + 8195;
2801 if (!user || user[0] == (char)0) {
2802 user = talloc_strdup(frame, context->user);
2810 dir = SMB_MALLOC_P(SMBCFILE);
2821 dir->fname = SMB_STRDUP(fname);
2825 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2827 if (server[0] == (char)0) {
2832 struct ip_service *ip_list;
2833 struct ip_service server_addr;
2834 struct user_auth_info u_info;
2836 if (share[0] != (char)0 || path[0] != (char)0) {
2838 errno = EINVAL + 8196;
2840 SAFE_FREE(dir->fname);
2847 /* Determine how many local master browsers to query */
2848 max_lmb_count = (context->options.browse_max_lmb_count == 0
2850 : context->options.browse_max_lmb_count);
2852 memset(&u_info, '\0', sizeof(u_info));
2853 u_info.username = talloc_strdup(frame,user);
2854 u_info.password = talloc_strdup(frame,password);
2855 if (!u_info.username || !u_info.password) {
2857 SAFE_FREE(dir->fname);
2865 * We have server and share and path empty but options
2866 * requesting that we scan all master browsers for their list
2867 * of workgroups/domains. This implies that we must first try
2868 * broadcast queries to find all master browsers, and if that
2869 * doesn't work, then try our other methods which return only
2870 * a single master browser.
2874 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
2880 if (!find_master_ip(workgroup, &server_addr.ss)) {
2883 SAFE_FREE(dir->fname);
2891 ip_list = (struct ip_service *)memdup(
2892 &server_addr, sizeof(server_addr));
2893 if (ip_list == NULL) {
2901 for (i = 0; i < count && i < max_lmb_count; i++) {
2902 char addr[INET6_ADDRSTRLEN];
2903 char *wg_ptr = NULL;
2904 struct cli_state *cli = NULL;
2906 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
2907 DEBUG(99, ("Found master browser %d of %d: %s\n",
2908 i+1, MAX(count, max_lmb_count),
2911 cli = get_ipc_connect_master_ip(talloc_tos(),
2915 /* cli == NULL is the master browser refused to talk or
2916 could not be found */
2921 workgroup = talloc_strdup(frame, wg_ptr);
2922 server = talloc_strdup(frame, cli->desthost);
2926 if (!workgroup || !server) {
2932 DEBUG(4, ("using workgroup %s %s\n",
2933 workgroup, server));
2936 * For each returned master browser IP address, get a
2937 * connection to IPC$ on the server if we do not
2938 * already have one, and determine the
2939 * workgroups/domains that it knows about.
2942 srv = smbc_server(context, True, server, "IPC$",
2943 workgroup, user, password);
2949 dir->dir_type = SMBC_WORKGROUP;
2951 /* Now, list the stuff ... */
2953 if (!cli_NetServerEnum(srv->cli,
2955 SV_TYPE_DOMAIN_ENUM,
2965 * Server not an empty string ... Check the rest and see what
2968 if (*share == '\0') {
2969 if (*path != '\0') {
2971 /* Should not have empty share with path */
2972 errno = EINVAL + 8197;
2974 SAFE_FREE(dir->fname);
2983 * We don't know if <server> is really a server name
2984 * or is a workgroup/domain name. If we already have
2985 * a server structure for it, we'll use it.
2986 * Otherwise, check to see if <server><1D>,
2987 * <server><1B>, or <server><20> translates. We check
2988 * to see if <server> is an IP address first.
2992 * See if we have an existing server. Do not
2993 * establish a connection if one does not already
2996 srv = smbc_server(context, False, server, "IPC$",
2997 workgroup, user, password);
3000 * If no existing server and not an IP addr, look for
3004 !is_ipaddress(server) &&
3005 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
3006 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
3010 dir->dir_type = SMBC_SERVER;
3013 * Get the backup list ...
3015 if (!name_status_find(server, 0, 0,
3016 &rem_ss, buserver)) {
3018 DEBUG(0, ("Could not get name of "
3019 "local/domain master browser "
3020 "for server %s\n", server));
3022 SAFE_FREE(dir->fname);
3032 * Get a connection to IPC$ on the server if
3033 * we do not already have one
3035 srv = smbc_server(context, True,
3037 workgroup, user, password);
3039 DEBUG(0, ("got no contact to IPC$\n"));
3041 SAFE_FREE(dir->fname);
3051 /* Now, list the servers ... */
3052 if (!cli_NetServerEnum(srv->cli, server,
3053 0x0000FFFE, list_fn,
3057 SAFE_FREE(dir->fname);
3064 (resolve_name(server, &rem_ss, 0x20))) {
3066 /* If we hadn't found the server, get one now */
3068 srv = smbc_server(context, True,
3076 SAFE_FREE(dir->fname);
3084 dir->dir_type = SMBC_FILE_SHARE;
3087 /* List the shares ... */
3089 if (net_share_enum_rpc(
3092 (void *) dir) < 0 &&
3098 errno = cli_errno(srv->cli);
3100 SAFE_FREE(dir->fname);
3108 /* Neither the workgroup nor server exists */
3109 errno = ECONNREFUSED;
3111 SAFE_FREE(dir->fname);
3121 * The server and share are specified ... work from
3125 struct cli_state *targetcli;
3127 /* We connect to the server and list the directory */
3128 dir->dir_type = SMBC_FILE_SHARE;
3130 srv = smbc_server(context, True, server, share,
3131 workgroup, user, password);
3135 SAFE_FREE(dir->fname);
3144 /* Now, list the files ... */
3146 p = path + strlen(path);
3147 path = talloc_asprintf_append(path, "\\*");
3150 SAFE_FREE(dir->fname);
3157 if (!cli_resolve_path(frame, "", srv->cli, path,
3158 &targetcli, &targetpath)) {
3159 d_printf("Could not resolve %s\n", path);
3161 SAFE_FREE(dir->fname);
3168 if (cli_list(targetcli, targetpath,
3169 aDIR | aSYSTEM | aHIDDEN,
3170 dir_list_fn, (void *)dir) < 0) {
3173 SAFE_FREE(dir->fname);
3176 saved_errno = smbc_errno(context, targetcli);
3178 if (saved_errno == EINVAL) {
3180 * See if they asked to opendir something
3181 * other than a directory. If so, the
3182 * converted error value we got would have
3183 * been EINVAL rather than ENOTDIR.
3185 *p = '\0'; /* restore original path */
3187 if (smbc_getatr(context, srv, path,
3189 NULL, NULL, NULL, NULL,
3191 ! IS_DOS_DIR(mode)) {
3193 /* It is. Correct the error value */
3194 saved_errno = ENOTDIR;
3199 * If there was an error and the server is no
3202 cb = &context->callbacks;
3203 if (cli_is_error(targetcli) &&
3204 (cb->check_server_fn)(context, srv)) {
3206 /* ... then remove it. */
3207 if ((cb->remove_unused_server_fn)(context,
3210 * We could not remove the
3211 * server completely, remove
3212 * it from the cache so we
3213 * will not get it again. It
3214 * will be removed when the
3215 * last file/dir is closed.
3217 (cb->remove_cached_srv_fn)(context,
3222 errno = saved_errno;
3230 DLIST_ADD(context->internal->_files, dir);
3237 * Routine to close a directory
3241 smbc_closedir_ctx(SMBCCTX *context,
3244 TALLOC_CTX *frame = talloc_stackframe();
3246 if (!context || !context->internal ||
3247 !context->internal->_initialized) {
3253 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3259 smbc_remove_dir(dir); /* Clean it up */
3261 DLIST_REMOVE(context->internal->_files, dir);
3265 SAFE_FREE(dir->fname);
3266 SAFE_FREE(dir); /* Free the space too */
3275 smbc_readdir_internal(SMBCCTX * context,
3276 struct smbc_dirent *dest,
3277 struct smbc_dirent *src,
3278 int max_namebuf_len)
3280 if (context->options.urlencode_readdir_entries) {
3282 /* url-encode the name. get back remaining buffer space */
3284 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3286 /* We now know the name length */
3287 dest->namelen = strlen(dest->name);
3289 /* Save the pointer to the beginning of the comment */
3290 dest->comment = dest->name + dest->namelen + 1;
3292 /* Copy the comment */
3293 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3294 dest->comment[max_namebuf_len - 1] = '\0';
3296 /* Save other fields */
3297 dest->smbc_type = src->smbc_type;
3298 dest->commentlen = strlen(dest->comment);
3299 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3303 /* No encoding. Just copy the entry as is. */
3304 memcpy(dest, src, src->dirlen);
3305 dest->comment = (char *)(&dest->name + src->namelen + 1);
3311 * Routine to get a directory entry
3314 struct smbc_dirent *
3315 smbc_readdir_ctx(SMBCCTX *context,
3319 struct smbc_dirent *dirp, *dirent;
3320 TALLOC_CTX *frame = talloc_stackframe();
3322 /* Check that all is ok first ... */
3324 if (!context || !context->internal ||
3325 !context->internal->_initialized) {
3328 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3334 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3337 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3343 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3346 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3352 if (!dir->dir_next) {
3357 dirent = dir->dir_next->dirent;
3366 dirp = (struct smbc_dirent *)context->internal->_dirent;
3367 maxlen = (sizeof(context->internal->_dirent) -
3368 sizeof(struct smbc_dirent));
3370 smbc_readdir_internal(context, dirp, dirent, maxlen);
3372 dir->dir_next = dir->dir_next->next;
3379 * Routine to get directory entries
3383 smbc_getdents_ctx(SMBCCTX *context,
3385 struct smbc_dirent *dirp,
3391 char *ndir = (char *)dirp;
3392 struct smbc_dir_list *dirlist;
3393 TALLOC_CTX *frame = talloc_stackframe();
3395 /* Check that all is ok first ... */
3397 if (!context || !context->internal ||
3398 !context->internal->_initialized) {
3406 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3414 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3423 * Now, retrieve the number of entries that will fit in what was passed
3424 * We have to figure out if the info is in the list, or we need to
3425 * send a request to the server to get the info.
3428 while ((dirlist = dir->dir_next)) {
3429 struct smbc_dirent *dirent;
3431 if (!dirlist->dirent) {
3433 errno = ENOENT; /* Bad error */
3439 /* Do urlencoding of next entry, if so selected */
3440 dirent = (struct smbc_dirent *)context->internal->_dirent;
3441 maxlen = (sizeof(context->internal->_dirent) -
3442 sizeof(struct smbc_dirent));
3443 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3445 reqd = dirent->dirlen;
3449 if (rem < count) { /* We managed to copy something */
3456 else { /* Nothing copied ... */
3458 errno = EINVAL; /* Not enough space ... */
3466 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3468 ((struct smbc_dirent *)ndir)->comment =
3469 (char *)(&((struct smbc_dirent *)ndir)->name +
3477 dir->dir_next = dirlist = dirlist -> next;
3490 * Routine to create a directory ...
3494 smbc_mkdir_ctx(SMBCCTX *context,
3506 struct cli_state *targetcli;
3507 TALLOC_CTX *frame = talloc_stackframe();
3509 if (!context || !context->internal ||
3510 !context->internal->_initialized) {
3522 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3524 if (smbc_parse_path(frame,
3539 if (!user || user[0] == (char)0) {
3540 user = talloc_strdup(frame, context->user);
3548 srv = smbc_server(context, True,
3549 server, share, workgroup, user, password);
3554 return -1; /* errno set by smbc_server */
3558 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3559 if (!cli_resolve_path(frame, "", srv->cli, path,
3560 &targetcli, &targetpath)) {
3561 d_printf("Could not resolve %s\n", path);
3565 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3567 if (!cli_mkdir(targetcli, targetpath)) {
3569 errno = smbc_errno(context, targetcli);
3581 * Our list function simply checks to see if a directory is not empty
3584 static int smbc_rmdir_dirempty = True;
3587 rmdir_list_fn(const char *mnt,
3592 if (strncmp(finfo->name, ".", 1) != 0 &&
3593 strncmp(finfo->name, "..", 2) != 0) {
3594 smbc_rmdir_dirempty = False;
3599 * Routine to remove a directory
3603 smbc_rmdir_ctx(SMBCCTX *context,
3614 struct cli_state *targetcli;
3615 TALLOC_CTX *frame = talloc_stackframe();
3617 if (!context || !context->internal ||
3618 !context->internal->_initialized) {
3630 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3632 if (smbc_parse_path(frame,
3647 if (!user || user[0] == (char)0) {
3648 user = talloc_strdup(frame, context->user);
3656 srv = smbc_server(context, True,
3657 server, share, workgroup, user, password);
3662 return -1; /* errno set by smbc_server */
3666 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3667 if (!cli_resolve_path(frame, "", srv->cli, path,
3668 &targetcli, &targetpath)) {
3669 d_printf("Could not resolve %s\n", path);
3673 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3676 if (!cli_rmdir(targetcli, targetpath)) {
3678 errno = smbc_errno(context, targetcli);
3680 if (errno == EACCES) { /* Check if the dir empty or not */
3682 /* Local storage to avoid buffer overflows */
3685 smbc_rmdir_dirempty = True; /* Make this so ... */
3687 lpath = talloc_asprintf(frame, "%s\\*",
3695 if (cli_list(targetcli, lpath,
3696 aDIR | aSYSTEM | aHIDDEN,
3697 rmdir_list_fn, NULL) < 0) {
3699 /* Fix errno to ignore latest error ... */
3700 DEBUG(5, ("smbc_rmdir: "
3701 "cli_list returned an error: %d\n",
3702 smbc_errno(context, targetcli)));
3707 if (smbc_rmdir_dirempty)
3725 * Routine to return the current directory position
3729 smbc_telldir_ctx(SMBCCTX *context,
3732 TALLOC_CTX *frame = talloc_stackframe();
3734 if (!context || !context->internal ||
3735 !context->internal->_initialized) {
3743 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3751 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3759 /* See if we're already at the end. */
3760 if (dir->dir_next == NULL) {
3767 * We return the pointer here as the offset
3770 return (off_t)(long)dir->dir_next->dirent;
3774 * A routine to run down the list and see if the entry is OK
3777 struct smbc_dir_list *
3778 smbc_check_dir_ent(struct smbc_dir_list *list,
3779 struct smbc_dirent *dirent)
3782 /* Run down the list looking for what we want */
3786 struct smbc_dir_list *tmp = list;
3790 if (tmp->dirent == dirent)
3799 return NULL; /* Not found, or an error */
3805 * Routine to seek on a directory
3809 smbc_lseekdir_ctx(SMBCCTX *context,
3813 long int l_offset = offset; /* Handle problems of size */
3814 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3815 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3816 TALLOC_CTX *frame = talloc_stackframe();
3818 if (!context || !context->internal ||
3819 !context->internal->_initialized) {
3827 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3835 /* Now, check what we were passed and see if it is OK ... */
3837 if (dirent == NULL) { /* Seek to the begining of the list */
3839 dir->dir_next = dir->dir_list;
3845 if (offset == -1) { /* Seek to the end of the list */
3846 dir->dir_next = NULL;
3851 /* Now, run down the list and make sure that the entry is OK */
3852 /* This may need to be changed if we change the format of the list */
3854 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3855 errno = EINVAL; /* Bad entry */
3860 dir->dir_next = list_ent;
3867 * Routine to fstat a dir
3871 smbc_fstatdir_ctx(SMBCCTX *context,
3876 if (!context || !context->internal ||
3877 !context->internal->_initialized) {
3882 /* No code yet ... */
3887 smbc_chmod_ctx(SMBCCTX *context,
3899 TALLOC_CTX *frame = talloc_stackframe();
3901 if (!context || !context->internal ||
3902 !context->internal->_initialized) {
3903 errno = EINVAL; /* Best I can think of ... */
3914 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3916 if (smbc_parse_path(frame,
3931 if (!user || user[0] == (char)0) {
3932 user = talloc_strdup(frame, context->user);
3940 srv = smbc_server(context, True,
3941 server, share, workgroup, user, password);
3945 return -1; /* errno set by smbc_server */
3950 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3951 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3952 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3953 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3955 if (!cli_setatr(srv->cli, path, mode, 0)) {
3956 errno = smbc_errno(context, srv->cli);
3966 smbc_utimes_ctx(SMBCCTX *context,
3968 struct timeval *tbuf)
3979 TALLOC_CTX *frame = talloc_stackframe();
3981 if (!context || !context->internal ||
3982 !context->internal->_initialized) {
3983 errno = EINVAL; /* Best I can think of ... */
3995 access_time = write_time = time(NULL);
3997 access_time = tbuf[0].tv_sec;
3998 write_time = tbuf[1].tv_sec;
4006 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
4007 atimebuf[sizeof(atimebuf) - 1] = '\0';
4008 if ((p = strchr(atimebuf, '\n')) != NULL) {
4012 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
4013 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
4014 if ((p = strchr(mtimebuf, '\n')) != NULL) {
4018 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
4019 fname, atimebuf, mtimebuf);
4022 if (smbc_parse_path(frame,
4037 if (!user || user[0] == (char)0) {
4038 user = talloc_strdup(frame, context->user);
4046 srv = smbc_server(context, True,
4047 server, share, workgroup, user, password);
4051 return -1; /* errno set by smbc_server */
4054 if (!smbc_setatr(context, srv, path,
4055 0, access_time, write_time, 0, 0)) {
4057 return -1; /* errno set by smbc_setatr */
4066 * Sort ACEs according to the documentation at
4067 * http://support.microsoft.com/kb/269175, at least as far as it defines the
4072 ace_compare(SEC_ACE *ace1,
4078 /* If the ACEs are equal, we have nothing more to do. */
4079 if (sec_ace_equal(ace1, ace2)) {
4083 /* Inherited follow non-inherited */
4084 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
4085 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
4087 return (b1 ? 1 : -1);
4091 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
4092 * sort them after DENY and ALLOW.
4094 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
4095 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
4096 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
4097 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4098 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
4099 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
4100 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
4101 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4103 return (b1 ? 1 : -1);
4106 /* Allowed ACEs follow denied ACEs */
4107 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
4108 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
4109 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
4110 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
4112 return (b1 ? 1 : -1);
4116 * ACEs applying to an entity's object follow those applying to the
4119 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
4120 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4121 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
4122 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4124 return (b1 ? 1 : -1);
4128 * If we get this far, the ACEs are similar as far as the
4129 * characteristics we typically care about (those defined by the
4130 * referenced MS document). We'll now sort by characteristics that
4131 * just seems reasonable.
4134 if (ace1->type != ace2->type) {
4135 return ace2->type - ace1->type;
4138 if (sid_compare(&ace1->trustee, &ace2->trustee)) {
4139 return sid_compare(&ace1->trustee, &ace2->trustee);
4142 if (ace1->flags != ace2->flags) {
4143 return ace1->flags - ace2->flags;
4146 if (ace1->access_mask != ace2->access_mask) {
4147 return ace1->access_mask - ace2->access_mask;
4150 if (ace1->size != ace2->size) {
4151 return ace1->size - ace2->size;
4154 return memcmp(ace1, ace2, sizeof(SEC_ACE));
4159 sort_acl(SEC_ACL *the_acl)
4162 if (!the_acl) return;
4164 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
4165 QSORT_CAST ace_compare);
4167 for (i=1;i<the_acl->num_aces;) {
4168 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
4170 for (j=i; j<the_acl->num_aces-1; j++) {
4171 the_acl->aces[j] = the_acl->aces[j+1];
4173 the_acl->num_aces--;
4180 /* convert a SID to a string, either numeric or username/group */
4182 convert_sid_to_string(struct cli_state *ipc_cli,
4188 char **domains = NULL;
4189 char **names = NULL;
4190 enum lsa_SidType *types = NULL;
4191 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
4194 sid_to_fstring(str, sid);
4197 return; /* no lookup desired */
4204 /* Ask LSA to convert the sid to a name */
4206 ctx = talloc_stackframe();
4208 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ctx,
4209 pol, 1, sid, &domains,
4211 !domains || !domains[0] || !names || !names[0]) {
4219 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
4220 domains[0], lp_winbind_separator(),
4224 /* convert a string to a SID, either numeric or username/group */
4226 convert_string_to_sid(struct cli_state *ipc_cli,
4232 enum lsa_SidType *types = NULL;
4233 DOM_SID *sids = NULL;
4235 TALLOC_CTX *ctx = NULL;
4236 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
4243 if (strncmp(str, "S-", 2) == 0) {
4244 return string_to_sid(sid, str);
4251 ctx = talloc_stackframe();
4252 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ctx,
4253 pol, 1, &str, NULL, 1, &sids,
4259 sid_copy(sid, &sids[0]);
4267 /* parse an ACE in the same format as print_ace() */
4269 parse_ace(struct cli_state *ipc_cli,
4279 unsigned int aflags;
4283 const struct perm_value *v;
4288 TALLOC_CTX *frame = talloc_stackframe();
4290 /* These values discovered by inspection */
4291 static const struct perm_value special_values[] = {
4292 { "R", 0x00120089 },
4293 { "W", 0x00120116 },
4294 { "X", 0x001200a0 },
4295 { "D", 0x00010000 },
4296 { "P", 0x00040000 },
4297 { "O", 0x00080000 },
4301 static const struct perm_value standard_values[] = {
4302 { "READ", 0x001200a9 },
4303 { "CHANGE", 0x001301bf },
4304 { "FULL", 0x001f01ff },
4310 p = strchr_m(str,':');
4317 /* Try to parse numeric form */
4319 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
4320 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
4324 /* Try to parse text form */
4326 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
4332 if (!next_token_talloc(frame, &cp, &tok, "/")) {
4337 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
4338 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
4339 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
4340 atype = SEC_ACE_TYPE_ACCESS_DENIED;
4346 /* Only numeric form accepted for flags at present */
4348 if (!(next_token_talloc(frame, &cp, &tok, "/") &&
4349 sscanf(tok, "%i", &aflags))) {
4354 if (!next_token_talloc(frame, &cp, &tok, "/")) {
4359 if (strncmp(tok, "0x", 2) == 0) {
4360 if (sscanf(tok, "%i", &amask) != 1) {
4367 for (v = standard_values; v->perm; v++) {
4368 if (strcmp(tok, v->perm) == 0) {
4379 for (v = special_values; v->perm; v++) {
4380 if (v->perm[0] == *p) {
4400 init_sec_ace(ace, &sid, atype, mask, aflags);
4405 /* add an ACE to a list of ACEs in a SEC_ACL */
4407 add_ace(SEC_ACL **the_acl,
4415 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
4419 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4422 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4423 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4424 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4425 1+(*the_acl)->num_aces, aces);
4427 (*the_acl) = newacl;
4432 /* parse a ascii version of a security descriptor */
4434 sec_desc_parse(TALLOC_CTX *ctx,
4435 struct cli_state *ipc_cli,
4440 const char *p = str;
4442 SEC_DESC *ret = NULL;
4444 DOM_SID *group_sid=NULL;
4445 DOM_SID *owner_sid=NULL;
4449 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
4451 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4452 revision = strtol(tok+9, NULL, 16);
4456 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4458 DEBUG(5, ("OWNER specified more than once!\n"));
4461 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4463 !convert_string_to_sid(ipc_cli, pol,
4465 owner_sid, tok+6)) {
4466 DEBUG(5, ("Failed to parse owner sid\n"));
4472 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4474 DEBUG(5, ("OWNER specified more than once!\n"));
4477 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4479 !convert_string_to_sid(ipc_cli, pol,
4481 owner_sid, tok+7)) {
4482 DEBUG(5, ("Failed to parse owner sid\n"));
4488 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4490 DEBUG(5, ("GROUP specified more than once!\n"));
4493 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4495 !convert_string_to_sid(ipc_cli, pol,
4497 group_sid, tok+6)) {
4498 DEBUG(5, ("Failed to parse group sid\n"));
4504 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4506 DEBUG(5, ("GROUP specified more than once!\n"));
4509 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4511 !convert_string_to_sid(ipc_cli, pol,
4513 group_sid, tok+6)) {
4514 DEBUG(5, ("Failed to parse group sid\n"));
4520 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4522 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4523 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4526 if(!add_ace(&dacl, &ace, ctx)) {
4527 DEBUG(5, ("Failed to add ACL %s\n", tok));
4533 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4535 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4536 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4539 if(!add_ace(&dacl, &ace, ctx)) {
4540 DEBUG(5, ("Failed to add ACL %s\n", tok));
4546 DEBUG(5, ("Failed to parse security descriptor\n"));
4550 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4551 owner_sid, group_sid, NULL, dacl, &sd_size);
4554 SAFE_FREE(group_sid);
4555 SAFE_FREE(owner_sid);
4561 /* Obtain the current dos attributes */
4562 static DOS_ATTR_DESC *
4563 dos_attr_query(SMBCCTX *context,
4565 const char *filename,
4568 struct timespec create_time_ts;
4569 struct timespec write_time_ts;
4570 struct timespec access_time_ts;
4571 struct timespec change_time_ts;
4574 SMB_INO_T inode = 0;
4577 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4583 /* Obtain the DOS attributes */
4584 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4591 errno = smbc_errno(context, srv->cli);
4592 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4598 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4599 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4600 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4601 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4608 /* parse a ascii version of a security descriptor */
4610 dos_attr_parse(SMBCCTX *context,
4616 const char *p = str;
4618 TALLOC_CTX *frame = NULL;
4620 const char * create_time_attr;
4621 const char * access_time_attr;
4622 const char * write_time_attr;
4623 const char * change_time_attr;
4626 /* Determine whether to use old-style or new-style attribute names */
4627 if (context->internal->_full_time_names) {
4628 /* new-style names */
4629 attr_strings.create_time_attr = "CREATE_TIME";
4630 attr_strings.access_time_attr = "ACCESS_TIME";
4631 attr_strings.write_time_attr = "WRITE_TIME";
4632 attr_strings.change_time_attr = "CHANGE_TIME";
4634 /* old-style names */
4635 attr_strings.create_time_attr = NULL;
4636 attr_strings.access_time_attr = "A_TIME";
4637 attr_strings.write_time_attr = "M_TIME";
4638 attr_strings.change_time_attr = "C_TIME";
4641 /* if this is to set the entire ACL... */
4643 /* ... then increment past the first colon if there is one */
4644 if ((p = strchr(str, ':')) != NULL) {
4651 frame = talloc_stackframe();
4652 while (next_token_talloc(frame, &p, &tok, "\t,\r\n")) {
4653 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4654 dad->mode = strtol(tok+5, NULL, 16);
4658 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4659 dad->size = (SMB_OFF_T)atof(tok+5);
4663 n = strlen(attr_strings.access_time_attr);
4664 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4665 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4669 n = strlen(attr_strings.change_time_attr);
4670 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4671 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4675 n = strlen(attr_strings.write_time_attr);
4676 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4677 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4681 if (attr_strings.create_time_attr != NULL) {
4682 n = strlen(attr_strings.create_time_attr);
4683 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4685 dad->create_time = (time_t)strtol(tok+n+1,
4691 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4692 dad->inode = (SMB_INO_T)atof(tok+6);
4699 /*****************************************************
4700 Retrieve the acls for a file.
4701 *******************************************************/
4704 cacl_get(SMBCCTX *context,
4707 struct cli_state *ipc_cli,
4723 bool exclude_nt_revision = False;
4724 bool exclude_nt_owner = False;
4725 bool exclude_nt_group = False;
4726 bool exclude_nt_acl = False;
4727 bool exclude_dos_mode = False;
4728 bool exclude_dos_size = False;
4729 bool exclude_dos_create_time = False;
4730 bool exclude_dos_access_time = False;
4731 bool exclude_dos_write_time = False;
4732 bool exclude_dos_change_time = False;
4733 bool exclude_dos_inode = False;
4734 bool numeric = True;
4735 bool determine_size = (bufsize == 0);
4739 fstring name_sandbox;
4743 struct timespec create_time_ts;
4744 struct timespec write_time_ts;
4745 struct timespec access_time_ts;
4746 struct timespec change_time_ts;
4747 time_t create_time = (time_t)0;
4748 time_t write_time = (time_t)0;
4749 time_t access_time = (time_t)0;
4750 time_t change_time = (time_t)0;
4754 struct cli_state *cli = srv->cli;
4756 const char * create_time_attr;
4757 const char * access_time_attr;
4758 const char * write_time_attr;
4759 const char * change_time_attr;
4762 const char * create_time_attr;
4763 const char * access_time_attr;
4764 const char * write_time_attr;
4765 const char * change_time_attr;
4766 } excl_attr_strings;
4768 /* Determine whether to use old-style or new-style attribute names */
4769 if (context->internal->_full_time_names) {
4770 /* new-style names */
4771 attr_strings.create_time_attr = "CREATE_TIME";
4772 attr_strings.access_time_attr = "ACCESS_TIME";
4773 attr_strings.write_time_attr = "WRITE_TIME";
4774 attr_strings.change_time_attr = "CHANGE_TIME";
4776 excl_attr_strings.create_time_attr = "CREATE_TIME";
4777 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4778 excl_attr_strings.write_time_attr = "WRITE_TIME";
4779 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4781 /* old-style names */
4782 attr_strings.create_time_attr = NULL;
4783 attr_strings.access_time_attr = "A_TIME";
4784 attr_strings.write_time_attr = "M_TIME";
4785 attr_strings.change_time_attr = "C_TIME";
4787 excl_attr_strings.create_time_attr = NULL;
4788 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4789 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4790 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4793 /* Copy name so we can strip off exclusions (if any are specified) */
4794 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4796 /* Ensure name is null terminated */
4797 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4799 /* Play in the sandbox */
4800 name = name_sandbox;
4802 /* If there are any exclusions, point to them and mask them from name */
4803 if ((pExclude = strchr(name, '!')) != NULL)
4808 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4809 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4810 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4811 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4812 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4813 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4814 numeric = (* (name + strlen(name) - 1) != '+');
4816 /* Look for exclusions from "all" requests */
4817 if (all || all_nt || all_dos) {
4819 /* Exclusions are delimited by '!' */
4822 pExclude = (p == NULL ? NULL : p + 1)) {
4824 /* Find end of this exclusion name */
4825 if ((p = strchr(pExclude, '!')) != NULL)
4830 /* Which exclusion name is this? */
4831 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4832 exclude_nt_revision = True;
4834 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4835 exclude_nt_owner = True;
4837 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4838 exclude_nt_group = True;
4840 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4841 exclude_nt_acl = True;
4843 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4844 exclude_dos_mode = True;
4846 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4847 exclude_dos_size = True;
4849 else if (excl_attr_strings.create_time_attr != NULL &&
4850 StrCaseCmp(pExclude,
4851 excl_attr_strings.change_time_attr) == 0) {
4852 exclude_dos_create_time = True;
4854 else if (StrCaseCmp(pExclude,
4855 excl_attr_strings.access_time_attr) == 0) {
4856 exclude_dos_access_time = True;
4858 else if (StrCaseCmp(pExclude,
4859 excl_attr_strings.write_time_attr) == 0) {
4860 exclude_dos_write_time = True;
4862 else if (StrCaseCmp(pExclude,
4863 excl_attr_strings.change_time_attr) == 0) {
4864 exclude_dos_change_time = True;
4866 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4867 exclude_dos_inode = True;
4870 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4881 * If we are (possibly) talking to an NT or new system and some NT
4882 * attributes have been requested...
4884 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4885 /* Point to the portion after "system.nt_sec_desc." */
4886 name += 19; /* if (all) this will be invalid but unused */
4888 /* ... then obtain any NT attributes which were requested */
4889 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4892 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4893 filename, cli_errstr(cli)));
4898 sd = cli_query_secdesc(cli, fnum, ctx);
4902 ("cacl_get Failed to query old descriptor\n"));
4907 cli_close(cli, fnum);
4909 if (! exclude_nt_revision) {
4910 if (all || all_nt) {
4911 if (determine_size) {
4912 p = talloc_asprintf(ctx,
4921 n = snprintf(buf, bufsize,
4925 } else if (StrCaseCmp(name, "revision") == 0) {
4926 if (determine_size) {
4927 p = talloc_asprintf(ctx, "%d",
4935 n = snprintf(buf, bufsize, "%d",
4940 if (!determine_size && n > bufsize) {
4950 if (! exclude_nt_owner) {
4951 /* Get owner and group sid */
4952 if (sd->owner_sid) {
4953 convert_sid_to_string(ipc_cli, pol,
4958 fstrcpy(sidstr, "");
4961 if (all || all_nt) {
4962 if (determine_size) {
4963 p = talloc_asprintf(ctx, ",OWNER:%s",
4970 } else if (sidstr[0] != '\0') {
4971 n = snprintf(buf, bufsize,
4972 ",OWNER:%s", sidstr);
4974 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4975 if (determine_size) {
4976 p = talloc_asprintf(ctx, "%s", sidstr);
4983 n = snprintf(buf, bufsize, "%s",
4988 if (!determine_size && n > bufsize) {
4998 if (! exclude_nt_group) {
4999 if (sd->group_sid) {
5000 convert_sid_to_string(ipc_cli, pol,
5004 fstrcpy(sidstr, "");
5007 if (all || all_nt) {
5008 if (determine_size) {
5009 p = talloc_asprintf(ctx, ",GROUP:%s",
5016 } else if (sidstr[0] != '\0') {
5017 n = snprintf(buf, bufsize,
5018 ",GROUP:%s", sidstr);
5020 } else if (StrnCaseCmp(name, "group", 5) == 0) {
5021 if (determine_size) {
5022 p = talloc_asprintf(ctx, "%s", sidstr);
5029 n = snprintf(buf, bufsize,
5034 if (!determine_size && n > bufsize) {
5044 if (! exclude_nt_acl) {
5045 /* Add aces to value buffer */
5046 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5048 SEC_ACE *ace = &sd->dacl->aces[i];
5049 convert_sid_to_string(ipc_cli, pol,
5053 if (all || all_nt) {
5054 if (determine_size) {
5055 p = talloc_asprintf(
5071 ",ACL:%s:%d/%d/0x%08x",
5077 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
5078 StrCaseCmp(name+3, sidstr) == 0) ||
5079 (StrnCaseCmp(name, "acl+", 4) == 0 &&
5080 StrCaseCmp(name+4, sidstr) == 0)) {
5081 if (determine_size) {
5082 p = talloc_asprintf(
5094 n = snprintf(buf, bufsize,
5100 } else if (all_nt_acls) {
5101 if (determine_size) {
5102 p = talloc_asprintf(
5104 "%s%s:%d/%d/0x%08x",
5116 n = snprintf(buf, bufsize,
5117 "%s%s:%d/%d/0x%08x",
5125 if (!determine_size && n > bufsize) {
5136 /* Restore name pointer to its original value */
5140 if (all || some_dos) {
5141 /* Point to the portion after "system.dos_attr." */
5142 name += 16; /* if (all) this will be invalid but unused */
5144 /* Obtain the DOS attributes */
5145 if (!smbc_getatr(context, srv, filename, &mode, &size,
5152 errno = smbc_errno(context, srv->cli);
5157 create_time = convert_timespec_to_time_t(create_time_ts);
5158 access_time = convert_timespec_to_time_t(access_time_ts);
5159 write_time = convert_timespec_to_time_t(write_time_ts);
5160 change_time = convert_timespec_to_time_t(change_time_ts);
5162 if (! exclude_dos_mode) {
5163 if (all || all_dos) {
5164 if (determine_size) {
5165 p = talloc_asprintf(ctx,
5178 n = snprintf(buf, bufsize,
5186 } else if (StrCaseCmp(name, "mode") == 0) {
5187 if (determine_size) {
5188 p = talloc_asprintf(ctx, "0x%x", mode);
5195 n = snprintf(buf, bufsize,
5200 if (!determine_size && n > bufsize) {
5210 if (! exclude_dos_size) {
5211 if (all || all_dos) {
5212 if (determine_size) {
5213 p = talloc_asprintf(
5223 n = snprintf(buf, bufsize,
5227 } else if (StrCaseCmp(name, "size") == 0) {
5228 if (determine_size) {
5229 p = talloc_asprintf(
5239 n = snprintf(buf, bufsize,
5245 if (!determine_size && n > bufsize) {
5255 if (! exclude_dos_create_time &&
5256 attr_strings.create_time_attr != NULL) {
5257 if (all || all_dos) {
5258 if (determine_size) {
5259 p = talloc_asprintf(ctx,
5261 attr_strings.create_time_attr,
5269 n = snprintf(buf, bufsize,
5271 attr_strings.create_time_attr,
5274 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
5275 if (determine_size) {
5276 p = talloc_asprintf(ctx, "%lu", create_time);
5283 n = snprintf(buf, bufsize,
5284 "%lu", create_time);
5288 if (!determine_size && n > bufsize) {
5298 if (! exclude_dos_access_time) {
5299 if (all || all_dos) {
5300 if (determine_size) {
5301 p = talloc_asprintf(ctx,
5303 attr_strings.access_time_attr,
5311 n = snprintf(buf, bufsize,
5313 attr_strings.access_time_attr,
5316 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
5317 if (determine_size) {
5318 p = talloc_asprintf(ctx, "%lu", access_time);
5325 n = snprintf(buf, bufsize,
5326 "%lu", access_time);
5330 if (!determine_size && n > bufsize) {
5340 if (! exclude_dos_write_time) {
5341 if (all || all_dos) {
5342 if (determine_size) {
5343 p = talloc_asprintf(ctx,
5345 attr_strings.write_time_attr,
5353 n = snprintf(buf, bufsize,
5355 attr_strings.write_time_attr,
5358 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
5359 if (determine_size) {
5360 p = talloc_asprintf(ctx, "%lu", write_time);
5367 n = snprintf(buf, bufsize,
5372 if (!determine_size && n > bufsize) {
5382 if (! exclude_dos_change_time) {
5383 if (all || all_dos) {
5384 if (determine_size) {
5385 p = talloc_asprintf(ctx,
5387 attr_strings.change_time_attr,
5395 n = snprintf(buf, bufsize,
5397 attr_strings.change_time_attr,
5400 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5401 if (determine_size) {
5402 p = talloc_asprintf(ctx, "%lu", change_time);
5409 n = snprintf(buf, bufsize,
5410 "%lu", change_time);
5414 if (!determine_size && n > bufsize) {
5424 if (! exclude_dos_inode) {
5425 if (all || all_dos) {
5426 if (determine_size) {
5427 p = talloc_asprintf(
5437 n = snprintf(buf, bufsize,
5441 } else if (StrCaseCmp(name, "inode") == 0) {
5442 if (determine_size) {
5443 p = talloc_asprintf(
5453 n = snprintf(buf, bufsize,
5459 if (!determine_size && n > bufsize) {
5469 /* Restore name pointer to its original value */
5481 /*****************************************************
5482 set the ACLs on a file given an ascii description
5483 *******************************************************/
5485 cacl_set(TALLOC_CTX *ctx,
5486 struct cli_state *cli,
5487 struct cli_state *ipc_cli,
5489 const char *filename,
5490 const char *the_acl,
5496 SEC_DESC *sd = NULL, *old;
5497 SEC_ACL *dacl = NULL;
5498 DOM_SID *owner_sid = NULL;
5499 DOM_SID *group_sid = NULL;
5504 bool numeric = True;
5506 /* the_acl will be null for REMOVE_ALL operations */
5508 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5512 /* if this is to set the entire ACL... */
5513 if (*the_acl == '*') {
5514 /* ... then increment past the first colon */
5518 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5519 CONST_DISCARD(char *, the_acl));
5527 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5528 that doesn't deref sd */
5530 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5535 /* The desired access below is the only one I could find that works
5536 with NT4, W2KP and Samba */
5538 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5541 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5542 filename, cli_errstr(cli)));
5547 old = cli_query_secdesc(cli, fnum, ctx);
5550 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5555 cli_close(cli, fnum);
5558 case SMBC_XATTR_MODE_REMOVE_ALL:
5559 old->dacl->num_aces = 0;
5563 case SMBC_XATTR_MODE_REMOVE:
5564 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5567 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5568 if (sec_ace_equal(&sd->dacl->aces[i],
5569 &old->dacl->aces[j])) {
5571 for (k=j; k<old->dacl->num_aces-1;k++) {
5572 old->dacl->aces[k] =
5573 old->dacl->aces[k+1];
5575 old->dacl->num_aces--;
5590 case SMBC_XATTR_MODE_ADD:
5591 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5594 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5595 if (sid_equal(&sd->dacl->aces[i].trustee,
5596 &old->dacl->aces[j].trustee)) {
5597 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5602 old->dacl->aces[j] = sd->dacl->aces[i];
5608 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5614 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5615 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5621 case SMBC_XATTR_MODE_SET:
5623 owner_sid = old->owner_sid;
5624 group_sid = old->group_sid;
5628 case SMBC_XATTR_MODE_CHOWN:
5629 owner_sid = sd->owner_sid;
5632 case SMBC_XATTR_MODE_CHGRP:
5633 group_sid = sd->group_sid;
5637 /* Denied ACE entries must come before allowed ones */
5638 sort_acl(old->dacl);
5640 /* Create new security descriptor and set it */
5641 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5642 owner_sid, group_sid, NULL, dacl, &sd_size);
5644 fnum = cli_nt_create(cli, filename,
5645 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5648 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5649 filename, cli_errstr(cli)));
5654 if (!cli_set_secdesc(cli, fnum, sd)) {
5655 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5662 cli_close(cli, fnum);
5673 smbc_setxattr_ctx(SMBCCTX *context,
5693 const char * create_time_attr;
5694 const char * access_time_attr;
5695 const char * write_time_attr;
5696 const char * change_time_attr;
5698 TALLOC_CTX *frame = talloc_stackframe();
5700 if (!context || !context->internal ||
5701 !context->internal->_initialized) {
5702 errno = EINVAL; /* Best I can think of ... */
5713 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5714 fname, name, (int) size, (const char*)value));
5716 if (smbc_parse_path(frame,
5731 if (!user || user[0] == (char)0) {
5732 user = talloc_strdup(frame, context->user);
5740 srv = smbc_server(context, True,
5741 server, share, workgroup, user, password);
5744 return -1; /* errno set by smbc_server */
5747 if (! srv->no_nt_session) {
5748 ipc_srv = smbc_attr_server(context, server, share,
5749 workgroup, user, password,
5752 srv->no_nt_session = True;
5759 * Are they asking to set the entire set of known attributes?
5761 if (StrCaseCmp(name, "system.*") == 0 ||
5762 StrCaseCmp(name, "system.*+") == 0) {
5765 talloc_asprintf(talloc_tos(), "%s:%s",
5766 name+7, (const char *) value);
5775 ret = cacl_set(talloc_tos(), srv->cli,
5776 ipc_srv->cli, &pol, path,
5779 ? SMBC_XATTR_MODE_SET
5780 : SMBC_XATTR_MODE_ADD),
5786 /* get a DOS Attribute Descriptor with current attributes */
5787 dad = dos_attr_query(context, talloc_tos(), path, srv);
5789 /* Overwrite old with new, using what was provided */
5790 dos_attr_parse(context, dad, srv, namevalue);
5792 /* Set the new DOS attributes */
5793 if (! smbc_setatr(context, srv, path,
5800 /* cause failure if NT failed too */
5805 /* we only fail if both NT and DOS sets failed */
5806 if (ret < 0 && ! dad) {
5807 ret = -1; /* in case dad was null */
5818 * Are they asking to set an access control element or to set
5819 * the entire access control list?
5821 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5822 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5823 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5824 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5825 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5829 talloc_asprintf(talloc_tos(), "%s:%s",
5830 name+19, (const char *) value);
5833 ret = -1; /* errno set by smbc_server() */
5835 else if (! namevalue) {
5839 ret = cacl_set(talloc_tos(), srv->cli,
5840 ipc_srv->cli, &pol, path,
5843 ? SMBC_XATTR_MODE_SET
5844 : SMBC_XATTR_MODE_ADD),
5852 * Are they asking to set the owner?
5854 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5855 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5859 talloc_asprintf(talloc_tos(), "%s:%s",
5860 name+19, (const char *) value);
5863 ret = -1; /* errno set by smbc_server() */
5865 else if (! namevalue) {
5869 ret = cacl_set(talloc_tos(), srv->cli,
5870 ipc_srv->cli, &pol, path,
5871 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5878 * Are they asking to set the group?
5880 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5881 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5885 talloc_asprintf(talloc_tos(), "%s:%s",
5886 name+19, (const char *) value);
5889 /* errno set by smbc_server() */
5892 else if (! namevalue) {
5896 ret = cacl_set(talloc_tos(), srv->cli,
5897 ipc_srv->cli, &pol, path,
5898 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5904 /* Determine whether to use old-style or new-style attribute names */
5905 if (context->internal->_full_time_names) {
5906 /* new-style names */
5907 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5908 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5909 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5910 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5912 /* old-style names */
5913 attr_strings.create_time_attr = NULL;
5914 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5915 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5916 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5920 * Are they asking to set a DOS attribute?
5922 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5923 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5924 (attr_strings.create_time_attr != NULL &&
5925 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5926 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5927 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5928 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5930 /* get a DOS Attribute Descriptor with current attributes */
5931 dad = dos_attr_query(context, talloc_tos(), path, srv);
5934 talloc_asprintf(talloc_tos(), "%s:%s",
5935 name+16, (const char *) value);
5940 /* Overwrite old with provided new params */
5941 dos_attr_parse(context, dad, srv, namevalue);
5943 /* Set the new DOS attributes */
5944 ret2 = smbc_setatr(context, srv, path,
5951 /* ret2 has True (success) / False (failure) */
5966 /* Unsupported attribute name */
5973 smbc_getxattr_ctx(SMBCCTX *context,
5990 const char * create_time_attr;
5991 const char * access_time_attr;
5992 const char * write_time_attr;
5993 const char * change_time_attr;
5995 TALLOC_CTX *frame = talloc_stackframe();
5997 if (!context || !context->internal ||
5998 !context->internal->_initialized) {
5999 errno = EINVAL; /* Best I can think of ... */
6010 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
6012 if (smbc_parse_path(frame,
6027 if (!user || user[0] == (char)0) {
6028 user = talloc_strdup(frame, context->user);
6036 srv = smbc_server(context, True,
6037 server, share, workgroup, user, password);
6040 return -1; /* errno set by smbc_server */
6043 if (! srv->no_nt_session) {
6044 ipc_srv = smbc_attr_server(context, server, share,
6045 workgroup, user, password,
6048 srv->no_nt_session = True;
6054 /* Determine whether to use old-style or new-style attribute names */
6055 if (context->internal->_full_time_names) {
6056 /* new-style names */
6057 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
6058 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
6059 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
6060 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
6062 /* old-style names */
6063 attr_strings.create_time_attr = NULL;
6064 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
6065 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
6066 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
6069 /* Are they requesting a supported attribute? */
6070 if (StrCaseCmp(name, "system.*") == 0 ||
6071 StrnCaseCmp(name, "system.*!", 9) == 0 ||
6072 StrCaseCmp(name, "system.*+") == 0 ||
6073 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
6074 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
6075 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
6076 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
6077 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
6078 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
6079 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
6080 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
6081 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
6082 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
6083 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
6084 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
6085 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
6086 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
6087 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
6088 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
6089 (attr_strings.create_time_attr != NULL &&
6090 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
6091 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
6092 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
6093 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
6094 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
6097 ret = cacl_get(context, talloc_tos(), srv,
6098 ipc_srv == NULL ? NULL : ipc_srv->cli,
6100 CONST_DISCARD(char *, name),
6101 CONST_DISCARD(char *, value), size);
6102 if (ret < 0 && errno == 0) {
6103 errno = smbc_errno(context, srv->cli);
6109 /* Unsupported attribute name */
6117 smbc_removexattr_ctx(SMBCCTX *context,
6131 TALLOC_CTX *frame = talloc_stackframe();
6133 if (!context || !context->internal ||
6134 !context->internal->_initialized) {
6135 errno = EINVAL; /* Best I can think of ... */
6146 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
6148 if (smbc_parse_path(frame,
6163 if (!user || user[0] == (char)0) {
6164 user = talloc_strdup(frame, context->user);
6172 srv = smbc_server(context, True,
6173 server, share, workgroup, user, password);
6176 return -1; /* errno set by smbc_server */
6179 if (! srv->no_nt_session) {
6180 ipc_srv = smbc_attr_server(context, server, share,
6181 workgroup, user, password,
6184 srv->no_nt_session = True;
6192 return -1; /* errno set by smbc_attr_server */
6195 /* Are they asking to set the entire ACL? */
6196 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
6197 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
6200 ret = cacl_set(talloc_tos(), srv->cli,
6201 ipc_srv->cli, &pol, path,
6202 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
6208 * Are they asking to remove one or more spceific security descriptor
6211 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
6212 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
6213 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
6214 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
6215 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
6216 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
6217 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
6220 ret = cacl_set(talloc_tos(), srv->cli,
6221 ipc_srv->cli, &pol, path,
6222 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
6227 /* Unsupported attribute name */
6234 smbc_listxattr_ctx(SMBCCTX *context,
6240 * This isn't quite what listxattr() is supposed to do. This returns
6241 * the complete set of attribute names, always, rather than only those
6242 * attribute names which actually exist for a file. Hmmm...
6245 const char supported_old[] =
6248 "system.nt_sec_desc.revision\0"
6249 "system.nt_sec_desc.owner\0"
6250 "system.nt_sec_desc.owner+\0"
6251 "system.nt_sec_desc.group\0"
6252 "system.nt_sec_desc.group+\0"
6253 "system.nt_sec_desc.acl.*\0"
6254 "system.nt_sec_desc.acl\0"
6255 "system.nt_sec_desc.acl+\0"
6256 "system.nt_sec_desc.*\0"
6257 "system.nt_sec_desc.*+\0"
6258 "system.dos_attr.*\0"
6259 "system.dos_attr.mode\0"
6260 "system.dos_attr.c_time\0"
6261 "system.dos_attr.a_time\0"
6262 "system.dos_attr.m_time\0"
6264 const char supported_new[] =
6267 "system.nt_sec_desc.revision\0"
6268 "system.nt_sec_desc.owner\0"
6269 "system.nt_sec_desc.owner+\0"
6270 "system.nt_sec_desc.group\0"
6271 "system.nt_sec_desc.group+\0"
6272 "system.nt_sec_desc.acl.*\0"
6273 "system.nt_sec_desc.acl\0"
6274 "system.nt_sec_desc.acl+\0"
6275 "system.nt_sec_desc.*\0"
6276 "system.nt_sec_desc.*+\0"
6277 "system.dos_attr.*\0"
6278 "system.dos_attr.mode\0"
6279 "system.dos_attr.create_time\0"
6280 "system.dos_attr.access_time\0"
6281 "system.dos_attr.write_time\0"
6282 "system.dos_attr.change_time\0"
6284 const char * supported;
6286 if (context->internal->_full_time_names) {
6287 supported = supported_new;
6288 retsize = sizeof(supported_new);
6290 supported = supported_old;
6291 retsize = sizeof(supported_old);
6298 if (retsize > size) {
6303 /* this can't be strcpy() because there are embedded null characters */
6304 memcpy(list, supported, retsize);
6310 * Open a print file to be written to by other calls
6314 smbc_open_print_job_ctx(SMBCCTX *context,
6322 TALLOC_CTX *frame = talloc_stackframe();
6324 if (!context || !context->internal ||
6325 !context->internal->_initialized) {
6337 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
6339 if (smbc_parse_path(frame,
6354 /* What if the path is empty, or the file exists? */
6357 return (context->open)(context, fname, O_WRONLY, 666);
6361 * Routine to print a file on a remote server ...
6363 * We open the file, which we assume to be on a remote server, and then
6364 * copy it to a print file on the share specified by printq.
6368 smbc_print_file_ctx(SMBCCTX *c_file,
6379 TALLOC_CTX *frame = talloc_stackframe();
6381 if (!c_file || !c_file->internal->_initialized || !c_print ||
6382 !c_print->internal->_initialized) {
6390 if (!fname && !printq) {
6398 /* Try to open the file for reading ... */
6400 if ((long)(fid1 = (c_file->open)(c_file, fname, O_RDONLY, 0666)) < 0) {
6401 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
6403 return -1; /* smbc_open sets errno */
6406 /* Now, try to open the printer file for writing */
6408 if ((long)(fid2 = (c_print->open_print_job)(c_print, printq)) < 0) {
6410 saverr = errno; /* Save errno */
6411 (c_file->close_fn)(c_file, fid1);
6418 while ((bytes = (c_file->read)(c_file, fid1, buf, sizeof(buf))) > 0) {
6422 if (((c_print->write)(c_print, fid2, buf, bytes)) < 0) {
6425 (c_file->close_fn)(c_file, fid1);
6426 (c_print->close_fn)(c_print, fid2);
6435 (c_file->close_fn)(c_file, fid1); /* We have to close these anyway */
6436 (c_print->close_fn)(c_print, fid2);
6452 * Routine to list print jobs on a printer share ...
6456 smbc_list_print_jobs_ctx(SMBCCTX *context,
6458 smbc_list_print_job_fn fn)
6467 TALLOC_CTX *frame = talloc_stackframe();
6469 if (!context || !context->internal ||
6470 !context->internal->_initialized) {
6482 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6484 if (smbc_parse_path(frame,
6499 if (!user || user[0] == (char)0) {
6500 user = talloc_strdup(frame, context->user);
6508 srv = smbc_server(context, True,
6509 server, share, workgroup, user, password);
6513 return -1; /* errno set by smbc_server */
6516 if (cli_print_queue(srv->cli,
6517 (void (*)(struct print_job_info *))fn) < 0) {
6518 errno = smbc_errno(context, srv->cli);
6529 * Delete a print job from a remote printer share
6533 smbc_unlink_print_job_ctx(SMBCCTX *context,
6545 TALLOC_CTX *frame = talloc_stackframe();
6547 if (!context || !context->internal ||
6548 !context->internal->_initialized) {
6560 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6562 if (smbc_parse_path(frame,
6577 if (!user || user[0] == (char)0) {
6578 user = talloc_strdup(frame, context->user);
6586 srv = smbc_server(context, True,
6587 server, share, workgroup, user, password);
6592 return -1; /* errno set by smbc_server */
6596 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6599 errno = smbc_errno(context, srv->cli);
6600 else if (err == ERRnosuchprintjob)
6613 * Get a new empty handle to fill in with your own info
6616 smbc_new_context(void)
6620 context = SMB_MALLOC_P(SMBCCTX);
6626 ZERO_STRUCTP(context);
6628 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6629 if (!context->internal) {
6635 ZERO_STRUCTP(context->internal);
6637 /* ADD REASONABLE DEFAULTS */
6639 context->timeout = 20000; /* 20 seconds */
6641 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6642 context->options.urlencode_readdir_entries = False;/* backward compat */
6643 context->options.one_share_per_server = False;/* backward compat */
6644 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6645 /* backward compat */
6647 context->open = smbc_open_ctx;
6648 context->creat = smbc_creat_ctx;
6649 context->read = smbc_read_ctx;
6650 context->write = smbc_write_ctx;
6651 context->close_fn = smbc_close_ctx;
6652 context->unlink = smbc_unlink_ctx;
6653 context->rename = smbc_rename_ctx;
6654 context->lseek = smbc_lseek_ctx;
6655 context->stat = smbc_stat_ctx;
6656 context->fstat = smbc_fstat_ctx;
6657 context->opendir = smbc_opendir_ctx;
6658 context->closedir = smbc_closedir_ctx;
6659 context->readdir = smbc_readdir_ctx;
6660 context->getdents = smbc_getdents_ctx;
6661 context->mkdir = smbc_mkdir_ctx;
6662 context->rmdir = smbc_rmdir_ctx;
6663 context->telldir = smbc_telldir_ctx;
6664 context->lseekdir = smbc_lseekdir_ctx;
6665 context->fstatdir = smbc_fstatdir_ctx;
6666 context->chmod = smbc_chmod_ctx;
6667 context->utimes = smbc_utimes_ctx;
6668 context->setxattr = smbc_setxattr_ctx;
6669 context->getxattr = smbc_getxattr_ctx;
6670 context->removexattr = smbc_removexattr_ctx;
6671 context->listxattr = smbc_listxattr_ctx;
6672 context->open_print_job = smbc_open_print_job_ctx;
6673 context->print_file = smbc_print_file_ctx;
6674 context->list_print_jobs = smbc_list_print_jobs_ctx;
6675 context->unlink_print_job = smbc_unlink_print_job_ctx;
6677 context->callbacks.check_server_fn = smbc_check_server;
6678 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6680 smbc_default_cache_functions(context);
6688 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6689 * and thus you'll be leaking memory if not handled properly.
6693 smbc_free_context(SMBCCTX *context,
6703 DEBUG(1,("Performing aggressive shutdown.\n"));
6705 f = context->internal->_files;
6707 (context->close_fn)(context, f);
6710 context->internal->_files = NULL;
6712 /* First try to remove the servers the nice way. */
6713 if (context->callbacks.purge_cached_fn(context)) {
6716 DEBUG(1, ("Could not purge all servers, "
6717 "Nice way shutdown failed.\n"));
6718 s = context->internal->_servers;
6720 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6722 cli_shutdown(s->cli);
6723 (context->callbacks.remove_cached_srv_fn)(context,
6726 DLIST_REMOVE(context->internal->_servers, s);
6730 context->internal->_servers = NULL;
6734 /* This is the polite way */
6735 if ((context->callbacks.purge_cached_fn)(context)) {
6736 DEBUG(1, ("Could not purge all servers, "
6737 "free_context failed.\n"));
6741 if (context->internal->_servers) {
6742 DEBUG(1, ("Active servers in context, "
6743 "free_context failed.\n"));
6747 if (context->internal->_files) {
6748 DEBUG(1, ("Active files in context, "
6749 "free_context failed.\n"));
6755 /* Things we have to clean up */
6756 SAFE_FREE(context->workgroup);
6757 SAFE_FREE(context->netbios_name);
6758 SAFE_FREE(context->user);
6760 DEBUG(3, ("Context %p succesfully freed\n", context));
6761 SAFE_FREE(context->internal);
6768 * Each time the context structure is changed, we have binary backward
6769 * compatibility issues. Instead of modifying the public portions of the
6770 * context structure to add new options, instead, we put them in the internal
6771 * portion of the context structure and provide a set function for these new
6775 smbc_option_set(SMBCCTX *context,
6777 ... /* option_value */)
6783 smbc_get_auth_data_with_context_fn auth_fn;
6788 va_start(ap, option_name);
6790 if (strcmp(option_name, "debug_to_stderr") == 0) {
6792 * Log to standard error instead of standard output.
6794 option_value.b = (bool) va_arg(ap, int);
6795 context->internal->_debug_stderr = option_value.b;
6797 } else if (strcmp(option_name, "full_time_names") == 0) {
6799 * Use new-style time attribute names, e.g. WRITE_TIME rather
6800 * than the old-style names such as M_TIME. This allows also
6801 * setting/getting CREATE_TIME which was previously
6802 * unimplemented. (Note that the old C_TIME was supposed to
6803 * be CHANGE_TIME but was confused and sometimes referred to
6806 option_value.b = (bool) va_arg(ap, int);
6807 context->internal->_full_time_names = option_value.b;
6809 } else if (strcmp(option_name, "open_share_mode") == 0) {
6811 * The share mode to use for files opened with
6812 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6814 option_value.i = va_arg(ap, int);
6815 context->internal->_share_mode =
6816 (smbc_share_mode) option_value.i;
6818 } else if (strcmp(option_name, "auth_function") == 0) {
6820 * Use the new-style authentication function which includes
6823 option_value.auth_fn =
6824 va_arg(ap, smbc_get_auth_data_with_context_fn);
6825 context->internal->_auth_fn_with_context =
6826 option_value.auth_fn;
6827 } else if (strcmp(option_name, "user_data") == 0) {
6829 * Save a user data handle which may be retrieved by the user
6830 * with smbc_option_get()
6832 option_value.v = va_arg(ap, void *);
6833 context->internal->_user_data = option_value.v;
6834 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
6836 * Save an encoded value for encryption level.
6837 * 0 = off, 1 = attempt, 2 = required.
6839 option_value.s = va_arg(ap, const char *);
6840 if (strcmp(option_value.s, "none") == 0) {
6841 context->internal->_smb_encryption_level = 0;
6842 } else if (strcmp(option_value.s, "request") == 0) {
6843 context->internal->_smb_encryption_level = 1;
6844 } else if (strcmp(option_value.s, "require") == 0) {
6845 context->internal->_smb_encryption_level = 2;
6854 * Retrieve the current value of an option
6857 smbc_option_get(SMBCCTX *context,
6860 if (strcmp(option_name, "debug_stderr") == 0) {
6862 * Log to standard error instead of standard output.
6864 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6865 return (void *) (intptr_t) context->internal->_debug_stderr;
6867 return (void *) context->internal->_debug_stderr;
6869 } else if (strcmp(option_name, "full_time_names") == 0) {
6871 * Use new-style time attribute names, e.g. WRITE_TIME rather
6872 * than the old-style names such as M_TIME. This allows also
6873 * setting/getting CREATE_TIME which was previously
6874 * unimplemented. (Note that the old C_TIME was supposed to
6875 * be CHANGE_TIME but was confused and sometimes referred to
6878 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6879 return (void *) (intptr_t) context->internal->_full_time_names;
6881 return (void *) context->internal->_full_time_names;
6884 } else if (strcmp(option_name, "auth_function") == 0) {
6886 * Use the new-style authentication function which includes
6889 return (void *) context->internal->_auth_fn_with_context;
6890 } else if (strcmp(option_name, "user_data") == 0) {
6892 * Save a user data handle which may be retrieved by the user
6893 * with smbc_option_get()
6895 return context->internal->_user_data;
6896 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
6898 * Return the current smb encrypt negotiate option as a string.
6900 switch (context->internal->_smb_encryption_level) {
6902 return (void *) "none";
6904 return (void *) "request";
6906 return (void *) "require";
6908 } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
6910 * Return the current smb encrypt status option as a bool.
6911 * false = off, true = on. We don't know what server is
6912 * being requested, so we only return true if all servers
6913 * are using an encrypted connection.
6916 unsigned int num_servers = 0;
6918 for (s = context->internal->_servers; s; s = s->next) {
6920 if (s->cli->trans_enc_state == NULL) {
6921 return (void *)false;
6924 return (void *) (bool) (num_servers > 0);
6932 * Initialise the library etc
6934 * We accept a struct containing handle information.
6935 * valid values for info->debug from 0 to 100,
6936 * and insist that info->fn must be non-null.
6939 smbc_init_context(SMBCCTX *context)
6945 if (!context || !context->internal) {
6950 /* Do not initialise the same client twice */
6951 if (context->internal->_initialized) {
6955 if ((!context->callbacks.auth_fn &&
6956 !context->internal->_auth_fn_with_context) ||
6957 context->debug < 0 ||
6958 context->debug > 100) {
6965 if (!smbc_initialized) {
6967 * Do some library-wide intializations the first time we get
6970 bool conf_loaded = False;
6971 TALLOC_CTX *frame = talloc_stackframe();
6973 /* Set this to what the user wants */
6974 DEBUGLEVEL = context->debug;
6978 setup_logging("libsmbclient", True);
6979 if (context->internal->_debug_stderr) {
6981 x_setbuf(x_stderr, NULL);
6984 /* Here we would open the smb.conf file if needed ... */
6986 in_client = True; /* FIXME, make a param */
6988 home = getenv("HOME");
6991 if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
6992 if (lp_load(conf, True, False, False, True)) {
6995 DEBUG(5, ("Could not load config file: %s\n",
7004 * Well, if that failed, try the get_dyn_CONFIGFILE
7005 * Which points to the standard locn, and if that
7006 * fails, silently ignore it and use the internal
7010 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
7011 DEBUG(5, ("Could not load config file: %s\n",
7012 get_dyn_CONFIGFILE()));
7016 * We loaded the global config file. Now lets
7017 * load user-specific modifications to the
7021 "%s/.smb/smb.conf.append",
7023 if (!lp_load(conf, True, False, False, False)) {
7025 ("Could not append config file: "
7034 load_interfaces(); /* Load the list of interfaces ... */
7036 reopen_logs(); /* Get logging working ... */
7039 * Block SIGPIPE (from lib/util_sock.c: write())
7040 * It is not needed and should not stop execution
7042 BlockSignals(True, SIGPIPE);
7044 /* Done with one-time initialisation */
7045 smbc_initialized = 1;
7050 if (!context->user) {
7052 * FIXME: Is this the best way to get the user info?
7054 user = getenv("USER");
7055 /* walk around as "guest" if no username can be found */
7056 if (!user) context->user = SMB_STRDUP("guest");
7057 else context->user = SMB_STRDUP(user);
7060 if (!context->netbios_name) {
7062 * We try to get our netbios name from the config. If that
7063 * fails we fall back on constructing our netbios name from
7066 if (global_myname()) {
7067 context->netbios_name = SMB_STRDUP(global_myname());
7071 * Hmmm, I want to get hostname as well, but I am too
7072 * lazy for the moment
7075 context->netbios_name = (char *)SMB_MALLOC(17);
7076 if (!context->netbios_name) {
7080 slprintf(context->netbios_name, 16,
7081 "smbc%s%d", context->user, pid);
7085 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
7087 if (!context->workgroup) {
7088 if (lp_workgroup()) {
7089 context->workgroup = SMB_STRDUP(lp_workgroup());
7092 /* TODO: Think about a decent default workgroup */
7093 context->workgroup = SMB_STRDUP("samba");
7097 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
7099 /* shortest timeout is 1 second */
7100 if (context->timeout > 0 && context->timeout < 1000)
7101 context->timeout = 1000;
7104 * FIXME: Should we check the function pointers here?
7107 context->internal->_initialized = True;
7113 /* Return the verion of samba, and thus libsmbclient */
7117 return samba_version_string();