2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "include/libsmb_internal.h"
28 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir);
29 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
30 struct smbc_dirent *dirent);
33 * DOS Attribute values (used internally)
35 typedef struct DOS_ATTR_DESC {
47 * Internal flags for extended attributes
50 /* internal mode values */
51 #define SMBC_XATTR_MODE_ADD 1
52 #define SMBC_XATTR_MODE_REMOVE 2
53 #define SMBC_XATTR_MODE_REMOVE_ALL 3
54 #define SMBC_XATTR_MODE_SET 4
55 #define SMBC_XATTR_MODE_CHOWN 5
56 #define SMBC_XATTR_MODE_CHGRP 6
58 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
60 /*We should test for this in configure ... */
62 #define ENOTSUP EOPNOTSUPP
66 * Functions exported by libsmb_cache.c that we need here
68 int smbc_default_cache_functions(SMBCCTX *context);
71 * check if an element is part of the list.
72 * FIXME: Does not belong here !
73 * Can anyone put this in a macro in dlinklist.h ?
76 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
77 if (!p || !list) return False;
79 if (p == list) return True;
86 * Find an lsa pipe handle associated with a cli struct.
88 static struct rpc_pipe_client *
89 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
91 struct rpc_pipe_client *pipe_hnd;
93 for (pipe_hnd = ipc_cli->pipe_list;
95 pipe_hnd = pipe_hnd->next) {
97 if (pipe_hnd->pipe_idx == PI_LSARPC) {
106 smbc_close_ctx(SMBCCTX *context,
109 smbc_lseek_ctx(SMBCCTX *context,
114 extern BOOL in_client;
117 * Is the logging working / configfile read ?
119 static int smbc_initialized = 0;
122 hex2int( unsigned int _char )
124 if ( _char >= 'A' && _char <='F')
125 return _char - 'A' + 10;
126 if ( _char >= 'a' && _char <='f')
127 return _char - 'a' + 10;
128 if ( _char >= '0' && _char <='9')
136 * Convert strings of %xx to their single character equivalent. Each 'x' must
137 * be a valid hexadecimal digit, or that % sequence is left undecoded.
139 * dest may, but need not be, the same pointer as src.
141 * Returns the number of % sequences which could not be converted due to lack
142 * of two following hexadecimal digits.
145 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
147 int old_length = strlen(src);
153 if ( old_length == 0 ) {
158 while ( i < old_length ) {
159 unsigned char character = src[ i++ ];
161 if (character == '%') {
162 int a = i+1 < old_length ? hex2int( src[i] ) : -1;
163 int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
165 /* Replace valid sequence */
166 if (a != -1 && b != -1) {
168 /* Replace valid %xx sequence with %dd */
169 character = (a * 16) + b;
171 if (character == '\0') {
172 break; /* Stop at %00 */
187 strncpy(dest, temp, max_dest_len - 1);
188 dest[max_dest_len - 1] = '\0';
196 * Convert any characters not specifically allowed in a URL into their %xx
199 * Returns the remaining buffer length.
202 smbc_urlencode(char * dest, char * src, int max_dest_len)
204 char hex[] = "0123456789ABCDEF";
206 for (; *src != '\0' && max_dest_len >= 3; src++) {
218 *dest++ = hex[(*src >> 4) & 0x0f];
219 *dest++ = hex[*src & 0x0f];
234 * Function to parse a path and turn it into components
236 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
237 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
238 * general format ("smb:" only; we do not look for "cifs:").
242 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
246 * smb:// Show all workgroups.
248 * The method of locating the list of workgroups varies
249 * depending upon the setting of the context variable
250 * context->options.browse_max_lmb_count. This value
251 * determine the maximum number of local master browsers to
252 * query for the list of workgroups. In order to ensure that
253 * a complete list of workgroups is obtained, all master
254 * browsers must be queried, but if there are many
255 * workgroups, the time spent querying can begin to add up.
256 * For small networks (not many workgroups), it is suggested
257 * that this variable be set to 0, indicating query all local
258 * master browsers. When the network has many workgroups, a
259 * reasonable setting for this variable might be around 3.
261 * smb://name/ if name<1D> or name<1B> exists, list servers in
262 * workgroup, else, if name<20> exists, list all shares
265 * If "options" are provided, this function returns the entire option list as a
266 * string, for later parsing by the caller. Note that currently, no options
270 static const char *smbc_prefix = "smb:";
273 smbc_parse_path(SMBCCTX *context,
275 char *workgroup, int workgroup_len,
276 char *server, int server_len,
277 char *share, int share_len,
278 char *path, int path_len,
279 char *user, int user_len,
280 char *password, int password_len,
281 char *options, int options_len)
289 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
292 * Assume we wont find an authentication domain to parse, so default
293 * to the workgroup in the provided context.
295 if (workgroup != NULL) {
296 strncpy(workgroup, context->workgroup, workgroup_len - 1);
297 workgroup[workgroup_len - 1] = '\0';
300 if (options != NULL && options_len > 0) {
301 options[0] = (char)0;
305 /* see if it has the right prefix */
306 len = strlen(smbc_prefix);
307 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
308 return -1; /* What about no smb: ? */
313 /* Watch the test below, we are testing to see if we should exit */
315 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
317 DEBUG(1, ("Invalid path (does not begin with smb://"));
322 p += 2; /* Skip the double slash */
324 /* See if any options were specified */
325 if ((q = strrchr(p, '?')) != NULL ) {
326 /* There are options. Null terminate here and point to them */
329 DEBUG(4, ("Found options '%s'", q));
331 /* Copy the options */
332 if (options != NULL && options_len > 0) {
333 safe_strcpy(options, q, options_len - 1);
341 int wl = strlen(context->workgroup);
347 strncpy(server, context->workgroup, wl);
353 * ok, its for us. Now parse out the server, share etc.
355 * However, we want to parse out [[domain;]user[:password]@] if it
359 /* check that '@' occurs before '/', if '/' exists at all */
360 q = strchr_m(p, '@');
361 r = strchr_m(p, '/');
362 if (q && (!r || q < r)) {
363 pstring username, passwd, domain;
364 const char *u = userinfo;
366 next_token_no_ltrim(&p, userinfo, "@", sizeof(fstring));
368 username[0] = passwd[0] = domain[0] = 0;
370 if (strchr_m(u, ';')) {
372 next_token_no_ltrim(&u, domain, ";", sizeof(fstring));
376 if (strchr_m(u, ':')) {
378 next_token_no_ltrim(&u, username, ":", sizeof(fstring));
385 pstrcpy(username, u);
389 if (domain[0] && workgroup) {
390 strncpy(workgroup, domain, workgroup_len - 1);
391 workgroup[workgroup_len - 1] = '\0';
395 strncpy(user, username, user_len - 1);
396 user[user_len - 1] = '\0';
400 strncpy(password, passwd, password_len - 1);
401 password[password_len - 1] = '\0';
406 if (!next_token(&p, server, "/", sizeof(fstring))) {
412 if (*p == (char)0) goto decoding; /* That's it ... */
414 if (!next_token(&p, share, "/", sizeof(fstring))) {
421 * Prepend a leading slash if there's a file path, as required by
427 safe_strcpy(path + 1, p, path_len - 2);
430 all_string_sub(path, "/", "\\", 0);
433 (void) smbc_urldecode(path, path, path_len);
434 (void) smbc_urldecode(server, server, server_len);
435 (void) smbc_urldecode(share, share, share_len);
436 (void) smbc_urldecode(user, user, user_len);
437 (void) smbc_urldecode(password, password, password_len);
443 * Verify that the options specified in a URL are valid
446 smbc_check_options(char *server,
451 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
452 "path='%s' options='%s'\n",
453 server, share, path, options));
455 /* No options at all is always ok */
456 if (! *options) return 0;
458 /* Currently, we don't support any options. */
463 * Convert an SMB error into a UNIX error ...
466 smbc_errno(SMBCCTX *context,
469 int ret = cli_errno(c);
471 if (cli_is_dos_error(c)) {
475 cli_dos_error(c, &eclass, &ecode);
477 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
478 (int)eclass, (int)ecode, (int)ecode, ret));
482 status = cli_nt_error(c);
484 DEBUG(3,("smbc errno %s -> %d\n",
485 nt_errstr(status), ret));
492 * Check a server for being alive and well.
493 * returns 0 if the server is in shape. Returns 1 on error
495 * Also useable outside libsmbclient to enable external cache
496 * to do some checks too.
499 smbc_check_server(SMBCCTX * context,
503 struct sockaddr addr;
506 * Although the use of port 139 is not a guarantee that we're using
507 * netbios, we assume so. We don't want to send a keepalive packet if
508 * not netbios because it's not valid, and Vista, at least,
509 * disconnects the client on such a request.
511 if (server->cli->port == 139) {
512 /* Assuming netbios. Send a keepalive packet */
513 if ( send_keepalive(server->cli->fd) == False ) {
518 * Assuming not netbios. Try a different method to detect if
519 * the connection is still alive.
522 if (getpeername(server->cli->fd, &addr, &size) == -1) {
527 /* connection is ok */
532 * Remove a server from the cached server list it's unused.
533 * On success, 0 is returned. 1 is returned if the server could not be removed.
535 * Also useable outside libsmbclient
538 smbc_remove_unused_server(SMBCCTX * context,
543 /* are we being fooled ? */
544 if (!context || !context->internal ||
545 !context->internal->_initialized || !srv) return 1;
548 /* Check all open files/directories for a relation with this server */
549 for (file = context->internal->_files; file; file=file->next) {
550 if (file->srv == srv) {
552 DEBUG(3, ("smbc_remove_usused_server: "
553 "%p still used by %p.\n",
559 DLIST_REMOVE(context->internal->_servers, srv);
561 cli_shutdown(srv->cli);
564 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
566 context->callbacks.remove_cached_srv_fn(context, srv);
574 find_server(SMBCCTX *context,
586 srv = context->callbacks.get_cached_srv_fn(context, server, share,
587 workgroup, username);
589 if (!auth_called && !srv && (!username[0] || !password[0])) {
590 if (context->internal->_auth_fn_with_context != NULL) {
591 context->internal->_auth_fn_with_context(
594 workgroup, sizeof(fstring),
595 username, sizeof(fstring),
596 password, sizeof(fstring));
598 context->callbacks.auth_fn(
600 workgroup, sizeof(fstring),
601 username, sizeof(fstring),
602 password, sizeof(fstring));
606 * However, smbc_auth_fn may have picked up info relating to
607 * an existing connection, so try for an existing connection
611 goto check_server_cache;
616 if (context->callbacks.check_server_fn(context, srv)) {
618 * This server is no good anymore
619 * Try to remove it and check for more possible
620 * servers in the cache
622 if (context->callbacks.remove_unused_server_fn(context,
625 * We could not remove the server completely,
626 * remove it from the cache so we will not get
627 * it again. It will be removed when the last
628 * file/dir is closed.
630 context->callbacks.remove_cached_srv_fn(context,
635 * Maybe there are more cached connections to this
638 goto check_server_cache;
648 * Connect to a server, possibly on an existing connection
650 * Here, what we want to do is: If the server and username
651 * match an existing connection, reuse that, otherwise, establish a
654 * If we have to create a new connection, call the auth_fn to get the
655 * info we need, unless the username and password were passed in.
659 smbc_server(SMBCCTX *context,
660 BOOL connect_if_not_found,
669 struct nmb_name called, calling;
670 const char *server_n = server;
673 int tried_reverse = 0;
676 const char *username_used;
682 if (server[0] == 0) {
687 /* Look for a cached connection */
688 srv = find_server(context, server, share,
689 workgroup, username, password);
692 * If we found a connection and we're only allowed one share per
695 if (srv && *share != '\0' && context->options.one_share_per_server) {
698 * ... then if there's no current connection to the share,
699 * connect to it. find_server(), or rather the function
700 * pointed to by context->callbacks.get_cached_srv_fn which
701 * was called by find_server(), will have issued a tree
702 * disconnect if the requested share is not the same as the
703 * one that was already connected.
705 if (srv->cli->cnum == (uint16) -1) {
706 /* Ensure we have accurate auth info */
707 if (context->internal->_auth_fn_with_context != NULL) {
708 context->internal->_auth_fn_with_context(
711 workgroup, sizeof(fstring),
712 username, sizeof(fstring),
713 password, sizeof(fstring));
715 context->callbacks.auth_fn(
717 workgroup, sizeof(fstring),
718 username, sizeof(fstring),
719 password, sizeof(fstring));
722 if (! cli_send_tconX(srv->cli, share, "?????",
723 password, strlen(password)+1)) {
725 errno = smbc_errno(context, srv->cli);
726 cli_shutdown(srv->cli);
728 context->callbacks.remove_cached_srv_fn(context,
734 * Regenerate the dev value since it's based on both
738 srv->dev = (dev_t)(str_checksum(server) ^
739 str_checksum(share));
744 /* If we have a connection... */
747 /* ... then we're done here. Give 'em what they came for. */
751 /* If we're not asked to connect when a connection doesn't exist... */
752 if (! connect_if_not_found) {
753 /* ... then we're done here. */
757 make_nmb_name(&calling, context->netbios_name, 0x0);
758 make_nmb_name(&called , server, 0x20);
760 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
762 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
765 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
769 /* have to open a new connection */
770 if ((c = cli_initialise()) == NULL) {
775 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
776 c->use_kerberos = True;
778 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
779 c->fallback_after_kerberos = True;
782 c->timeout = context->timeout;
785 * Force use of port 139 for first try if share is $IPC, empty, or
786 * null, so browse lists can work
788 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
789 port_try_first = 139;
792 port_try_first = 445;
796 c->port = port_try_first;
798 status = cli_connect(c, server_n, &ip);
799 if (!NT_STATUS_IS_OK(status)) {
801 /* First connection attempt failed. Try alternate port. */
802 c->port = port_try_next;
804 status = cli_connect(c, server_n, &ip);
805 if (!NT_STATUS_IS_OK(status)) {
812 if (!cli_session_request(c, &calling, &called)) {
814 if (strcmp(called.name, "*SMBSERVER")) {
815 make_nmb_name(&called , "*SMBSERVER", 0x20);
817 } else { /* Try one more time, but ensure we don't loop */
819 /* Only try this if server is an IP address ... */
821 if (is_ipaddress(server) && !tried_reverse) {
823 struct in_addr rem_ip;
825 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
826 DEBUG(4, ("Could not convert IP address "
827 "%s to struct in_addr\n", server));
832 tried_reverse++; /* Yuck */
834 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
835 make_nmb_name(&called, remote_name, 0x20);
844 DEBUG(4,(" session request ok\n"));
846 if (!cli_negprot(c)) {
852 username_used = username;
854 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
855 password, strlen(password),
856 password, strlen(password),
859 /* Failed. Try an anonymous login, if allowed by flags. */
862 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
863 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
874 DEBUG(4,(" session setup ok\n"));
876 if (!cli_send_tconX(c, share, "?????",
877 password, strlen(password)+1)) {
878 errno = smbc_errno(context, c);
883 DEBUG(4,(" tconx ok\n"));
886 * Ok, we have got a nice connection
887 * Let's allocate a server structure.
890 srv = SMB_MALLOC_P(SMBCSRV);
898 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
899 srv->no_pathinfo = False;
900 srv->no_pathinfo2 = False;
901 srv->no_nt_session = False;
903 /* now add it to the cache (internal or external) */
904 /* Let the cache function set errno if it wants to */
906 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
907 int saved_errno = errno;
908 DEBUG(3, (" Failed to add server to cache\n"));
916 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
917 server, share, srv));
919 DLIST_ADD(context->internal->_servers, srv);
933 * Connect to a server for getting/setting attributes, possibly on an existing
934 * connection. This works similarly to smbc_server().
937 smbc_attr_server(SMBCCTX *context,
947 struct cli_state *ipc_cli;
948 struct rpc_pipe_client *pipe_hnd;
950 SMBCSRV *ipc_srv=NULL;
953 * See if we've already created this special connection. Reference
954 * our "special" share name '*IPC$', which is an impossible real share
955 * name due to the leading asterisk.
957 ipc_srv = find_server(context, server, "*IPC$",
958 workgroup, username, password);
961 /* We didn't find a cached connection. Get the password */
962 if (*password == '\0') {
963 /* ... then retrieve it now. */
964 if (context->internal->_auth_fn_with_context != NULL) {
965 context->internal->_auth_fn_with_context(
968 workgroup, sizeof(fstring),
969 username, sizeof(fstring),
970 password, sizeof(fstring));
972 context->callbacks.auth_fn(
974 workgroup, sizeof(fstring),
975 username, sizeof(fstring),
976 password, sizeof(fstring));
981 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
982 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
986 nt_status = cli_full_connection(&ipc_cli,
987 global_myname(), server,
988 &ip, 0, "IPC$", "?????",
992 if (! NT_STATUS_IS_OK(nt_status)) {
993 DEBUG(1,("cli_full_connection failed! (%s)\n",
994 nt_errstr(nt_status)));
999 ipc_srv = SMB_MALLOC_P(SMBCSRV);
1002 cli_shutdown(ipc_cli);
1006 ZERO_STRUCTP(ipc_srv);
1007 ipc_srv->cli = ipc_cli;
1010 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1014 DEBUG(1, ("cli_nt_session_open fail!\n"));
1016 cli_shutdown(ipc_srv->cli);
1022 * Some systems don't support
1023 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1024 * so we might as well do it too.
1027 nt_status = rpccli_lsa_open_policy(
1029 ipc_srv->cli->mem_ctx,
1031 GENERIC_EXECUTE_ACCESS,
1034 if (!NT_STATUS_IS_OK(nt_status)) {
1035 errno = smbc_errno(context, ipc_srv->cli);
1036 cli_shutdown(ipc_srv->cli);
1041 /* now add it to the cache (internal or external) */
1043 errno = 0; /* let cache function set errno if it likes */
1044 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
1049 DEBUG(3, (" Failed to add server to cache\n"));
1053 cli_shutdown(ipc_srv->cli);
1058 DLIST_ADD(context->internal->_servers, ipc_srv);
1065 * Routine to open() a file ...
1069 smbc_open_ctx(SMBCCTX *context,
1074 fstring server, share, user, password, workgroup;
1077 struct cli_state *targetcli;
1078 SMBCSRV *srv = NULL;
1079 SMBCFILE *file = NULL;
1082 if (!context || !context->internal ||
1083 !context->internal->_initialized) {
1085 errno = EINVAL; /* Best I can think of ... */
1097 if (smbc_parse_path(context, fname,
1098 workgroup, sizeof(workgroup),
1099 server, sizeof(server),
1100 share, sizeof(share),
1103 password, sizeof(password),
1109 if (user[0] == (char)0) fstrcpy(user, context->user);
1111 srv = smbc_server(context, True,
1112 server, share, workgroup, user, password);
1116 if (errno == EPERM) errno = EACCES;
1117 return NULL; /* smbc_server sets errno */
1121 /* Hmmm, the test for a directory is suspect here ... FIXME */
1123 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1130 file = SMB_MALLOC_P(SMBCFILE);
1141 /*d_printf(">>>open: resolving %s\n", path);*/
1142 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1144 d_printf("Could not resolve %s\n", path);
1148 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1150 if ((fd = cli_open(targetcli, targetpath, flags,
1151 context->internal->_share_mode)) < 0) {
1153 /* Handle the error ... */
1156 errno = smbc_errno(context, targetcli);
1161 /* Fill in file struct */
1164 file->fname = SMB_STRDUP(fname);
1169 DLIST_ADD(context->internal->_files, file);
1172 * If the file was opened in O_APPEND mode, all write
1173 * operations should be appended to the file. To do that,
1174 * though, using this protocol, would require a getattrE()
1175 * call for each and every write, to determine where the end
1176 * of the file is. (There does not appear to be an append flag
1177 * in the protocol.) Rather than add all of that overhead of
1178 * retrieving the current end-of-file offset prior to each
1179 * write operation, we'll assume that most append operations
1180 * will continuously write, so we'll just set the offset to
1181 * the end of the file now and hope that's adequate.
1183 * Note to self: If this proves inadequate, and O_APPEND
1184 * should, in some cases, be forced for each write, add a
1185 * field in the context options structure, for
1186 * "strict_append_mode" which would select between the current
1187 * behavior (if FALSE) or issuing a getattrE() prior to each
1188 * write and forcing the write to the end of the file (if
1189 * TRUE). Adding that capability will likely require adding
1190 * an "append" flag into the _SMBCFILE structure to track
1191 * whether a file was opened in O_APPEND mode. -- djl
1193 if (flags & O_APPEND) {
1194 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1195 (void) smbc_close_ctx(context, file);
1205 /* Check if opendir needed ... */
1210 eno = smbc_errno(context, srv->cli);
1211 file = context->opendir(context, fname);
1212 if (!file) errno = eno;
1217 errno = EINVAL; /* FIXME, correct errno ? */
1223 * Routine to create a file
1226 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1229 smbc_creat_ctx(SMBCCTX *context,
1234 if (!context || !context->internal ||
1235 !context->internal->_initialized) {
1242 return smbc_open_ctx(context, path, creat_bits, mode);
1246 * Routine to read() a file ...
1250 smbc_read_ctx(SMBCCTX *context,
1256 fstring server, share, user, password;
1257 pstring path, targetpath;
1258 struct cli_state *targetcli;
1263 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1264 * appears to pass file->offset (which is type off_t) differently than
1265 * a local variable of type off_t. Using local variable "offset" in
1266 * the call to cli_read() instead of file->offset fixes a problem
1267 * retrieving data at an offset greater than 4GB.
1271 if (!context || !context->internal ||
1272 !context->internal->_initialized) {
1279 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1281 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1288 offset = file->offset;
1290 /* Check that the buffer exists ... */
1299 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1300 if (smbc_parse_path(context, file->fname,
1302 server, sizeof(server),
1303 share, sizeof(share),
1306 password, sizeof(password),
1312 /*d_printf(">>>read: resolving %s\n", path);*/
1313 if (!cli_resolve_path("", file->srv->cli, path,
1314 &targetcli, targetpath))
1316 d_printf("Could not resolve %s\n", path);
1319 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1321 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1325 errno = smbc_errno(context, targetcli);
1330 file->offset += ret;
1332 DEBUG(4, (" --> %d\n", ret));
1334 return ret; /* Success, ret bytes of data ... */
1339 * Routine to write() a file ...
1343 smbc_write_ctx(SMBCCTX *context,
1350 fstring server, share, user, password;
1351 pstring path, targetpath;
1352 struct cli_state *targetcli;
1354 /* First check all pointers before dereferencing them */
1356 if (!context || !context->internal ||
1357 !context->internal->_initialized) {
1364 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1371 /* Check that the buffer exists ... */
1380 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1382 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1383 if (smbc_parse_path(context, file->fname,
1385 server, sizeof(server),
1386 share, sizeof(share),
1389 password, sizeof(password),
1395 /*d_printf(">>>write: resolving %s\n", path);*/
1396 if (!cli_resolve_path("", file->srv->cli, path,
1397 &targetcli, targetpath))
1399 d_printf("Could not resolve %s\n", path);
1402 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1405 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1409 errno = smbc_errno(context, targetcli);
1414 file->offset += ret;
1416 return ret; /* Success, 0 bytes of data ... */
1420 * Routine to close() a file ...
1424 smbc_close_ctx(SMBCCTX *context,
1428 fstring server, share, user, password;
1429 pstring path, targetpath;
1430 struct cli_state *targetcli;
1432 if (!context || !context->internal ||
1433 !context->internal->_initialized) {
1440 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1450 return context->closedir(context, file);
1454 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1455 if (smbc_parse_path(context, file->fname,
1457 server, sizeof(server),
1458 share, sizeof(share),
1461 password, sizeof(password),
1467 /*d_printf(">>>close: resolving %s\n", path);*/
1468 if (!cli_resolve_path("", file->srv->cli, path,
1469 &targetcli, targetpath))
1471 d_printf("Could not resolve %s\n", path);
1474 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1476 if (!cli_close(targetcli, file->cli_fd)) {
1478 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1480 /* Deallocate slot and remove the server
1481 * from the server cache if unused */
1482 errno = smbc_errno(context, targetcli);
1484 DLIST_REMOVE(context->internal->_files, file);
1485 SAFE_FREE(file->fname);
1487 context->callbacks.remove_unused_server_fn(context, srv);
1493 DLIST_REMOVE(context->internal->_files, file);
1494 SAFE_FREE(file->fname);
1501 * Get info from an SMB server on a file. Use a qpathinfo call first
1502 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1505 smbc_getatr(SMBCCTX * context,
1510 struct timespec *create_time_ts,
1511 struct timespec *access_time_ts,
1512 struct timespec *write_time_ts,
1513 struct timespec *change_time_ts,
1518 struct cli_state *targetcli;
1521 if (!context || !context->internal ||
1522 !context->internal->_initialized) {
1529 /* path fixup for . and .. */
1530 if (strequal(path, ".") || strequal(path, ".."))
1531 pstrcpy(fixedpath, "\\");
1534 pstrcpy(fixedpath, path);
1535 trim_string(fixedpath, NULL, "\\..");
1536 trim_string(fixedpath, NULL, "\\.");
1538 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1540 if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
1542 d_printf("Couldn't resolve %s\n", path);
1546 if (!srv->no_pathinfo2 &&
1547 cli_qpathinfo2(targetcli, targetpath,
1556 /* if this is NT then don't bother with the getatr */
1557 if (targetcli->capabilities & CAP_NT_SMBS) {
1562 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1564 struct timespec w_time_ts;
1566 w_time_ts = convert_time_t_to_timespec(write_time);
1568 if (write_time_ts != NULL) {
1569 *write_time_ts = w_time_ts;
1572 if (create_time_ts != NULL) {
1573 *create_time_ts = w_time_ts;
1576 if (access_time_ts != NULL) {
1577 *access_time_ts = w_time_ts;
1580 if (change_time_ts != NULL) {
1581 *change_time_ts = w_time_ts;
1584 srv->no_pathinfo2 = True;
1594 * Set file info on an SMB server. Use setpathinfo call first. If that
1595 * fails, use setattrE..
1597 * Access and modification time parameters are always used and must be
1598 * provided. Create time, if zero, will be determined from the actual create
1599 * time of the file. If non-zero, the create time will be set as well.
1601 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1604 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1615 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1616 * modern function for "new code" to be using, and it works given a
1617 * filename rather than requiring that the file be opened to have its
1618 * attributes manipulated.
1620 if (srv->no_pathinfo ||
1621 ! cli_setpathinfo(srv->cli, path,
1629 * setpathinfo is not supported; go to plan B.
1631 * cli_setatr() does not work on win98, and it also doesn't
1632 * support setting the access time (only the modification
1633 * time), so in all cases, we open the specified file and use
1634 * cli_setattrE() which should work on all OS versions, and
1635 * supports both times.
1638 /* Don't try {q,set}pathinfo() again, with this server */
1639 srv->no_pathinfo = True;
1642 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1644 errno = smbc_errno(context, srv->cli);
1648 /* Set the new attributes */
1649 ret = cli_setattrE(srv->cli, fd,
1654 /* Close the file */
1655 cli_close(srv->cli, fd);
1658 * Unfortunately, setattrE() doesn't have a provision for
1659 * setting the access mode (attributes). We'll have to try
1660 * cli_setatr() for that, and with only this parameter, it
1661 * seems to work on win98.
1663 if (ret && mode != (uint16) -1) {
1664 ret = cli_setatr(srv->cli, path, mode, 0);
1668 errno = smbc_errno(context, srv->cli);
1677 * Routine to unlink() a file
1681 smbc_unlink_ctx(SMBCCTX *context,
1684 fstring server, share, user, password, workgroup;
1685 pstring path, targetpath;
1686 struct cli_state *targetcli;
1687 SMBCSRV *srv = NULL;
1689 if (!context || !context->internal ||
1690 !context->internal->_initialized) {
1692 errno = EINVAL; /* Best I can think of ... */
1704 if (smbc_parse_path(context, fname,
1705 workgroup, sizeof(workgroup),
1706 server, sizeof(server),
1707 share, sizeof(share),
1710 password, sizeof(password),
1716 if (user[0] == (char)0) fstrcpy(user, context->user);
1718 srv = smbc_server(context, True,
1719 server, share, workgroup, user, password);
1723 return -1; /* smbc_server sets errno */
1727 /*d_printf(">>>unlink: resolving %s\n", path);*/
1728 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1730 d_printf("Could not resolve %s\n", path);
1733 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1735 if (!cli_unlink(targetcli, targetpath)) {
1737 errno = smbc_errno(context, targetcli);
1739 if (errno == EACCES) { /* Check if the file is a directory */
1744 struct timespec write_time_ts;
1745 struct timespec access_time_ts;
1746 struct timespec change_time_ts;
1749 if (!smbc_getatr(context, srv, path, &mode, &size,
1756 /* Hmmm, bad error ... What? */
1758 errno = smbc_errno(context, targetcli);
1764 if (IS_DOS_DIR(mode))
1767 errno = saverr; /* Restore this */
1776 return 0; /* Success ... */
1781 * Routine to rename() a file
1785 smbc_rename_ctx(SMBCCTX *ocontext,
1801 pstring targetpath1;
1802 pstring targetpath2;
1803 struct cli_state *targetcli1;
1804 struct cli_state *targetcli2;
1805 SMBCSRV *srv = NULL;
1807 if (!ocontext || !ncontext ||
1808 !ocontext->internal || !ncontext->internal ||
1809 !ocontext->internal->_initialized ||
1810 !ncontext->internal->_initialized) {
1812 errno = EINVAL; /* Best I can think of ... */
1817 if (!oname || !nname) {
1824 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1826 smbc_parse_path(ocontext, oname,
1827 workgroup, sizeof(workgroup),
1828 server1, sizeof(server1),
1829 share1, sizeof(share1),
1830 path1, sizeof(path1),
1831 user1, sizeof(user1),
1832 password1, sizeof(password1),
1835 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1837 smbc_parse_path(ncontext, nname,
1839 server2, sizeof(server2),
1840 share2, sizeof(share2),
1841 path2, sizeof(path2),
1842 user2, sizeof(user2),
1843 password2, sizeof(password2),
1846 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1848 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1849 strcmp(user1, user2)) {
1851 /* Can't rename across file systems, or users?? */
1858 srv = smbc_server(ocontext, True,
1859 server1, share1, workgroup, user1, password1);
1866 /*d_printf(">>>rename: resolving %s\n", path1);*/
1867 if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
1869 d_printf("Could not resolve %s\n", path1);
1872 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1873 /*d_printf(">>>rename: resolving %s\n", path2);*/
1874 if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
1876 d_printf("Could not resolve %s\n", path2);
1879 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1881 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1882 strcmp(targetcli1->share, targetcli2->share))
1884 /* can't rename across file systems */
1890 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1891 int eno = smbc_errno(ocontext, targetcli1);
1893 if (eno != EEXIST ||
1894 !cli_unlink(targetcli1, targetpath2) ||
1895 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1903 return 0; /* Success */
1908 * A routine to lseek() a file
1912 smbc_lseek_ctx(SMBCCTX *context,
1918 fstring server, share, user, password;
1919 pstring path, targetpath;
1920 struct cli_state *targetcli;
1922 if (!context || !context->internal ||
1923 !context->internal->_initialized) {
1930 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1940 return -1; /* Can't lseek a dir ... */
1946 file->offset = offset;
1950 file->offset += offset;
1954 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1955 if (smbc_parse_path(context, file->fname,
1957 server, sizeof(server),
1958 share, sizeof(share),
1961 password, sizeof(password),
1968 /*d_printf(">>>lseek: resolving %s\n", path);*/
1969 if (!cli_resolve_path("", file->srv->cli, path,
1970 &targetcli, targetpath))
1972 d_printf("Could not resolve %s\n", path);
1975 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1977 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
1978 &size, NULL, NULL, NULL, NULL, NULL))
1980 SMB_OFF_T b_size = size;
1981 if (!cli_getattrE(targetcli, file->cli_fd,
1982 NULL, &b_size, NULL, NULL, NULL))
1989 file->offset = size + offset;
1998 return file->offset;
2003 * Generate an inode number from file name for those things that need it
2007 smbc_inode(SMBCCTX *context,
2011 if (!context || !context->internal ||
2012 !context->internal->_initialized) {
2019 if (!*name) return 2; /* FIXME, why 2 ??? */
2020 return (ino_t)str_checksum(name);
2025 * Routine to put basic stat info into a stat structure ... Used by stat and
2030 smbc_setup_stat(SMBCCTX *context,
2039 if (IS_DOS_DIR(mode)) {
2040 st->st_mode = SMBC_DIR_MODE;
2042 st->st_mode = SMBC_FILE_MODE;
2045 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2046 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2047 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2048 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2051 #ifdef HAVE_STAT_ST_BLKSIZE
2052 st->st_blksize = 512;
2054 #ifdef HAVE_STAT_ST_BLOCKS
2055 st->st_blocks = (size+511)/512;
2057 st->st_uid = getuid();
2058 st->st_gid = getgid();
2060 if (IS_DOS_DIR(mode)) {
2066 if (st->st_ino == 0) {
2067 st->st_ino = smbc_inode(context, fname);
2070 return True; /* FIXME: Is this needed ? */
2075 * Routine to stat a file given a name
2079 smbc_stat_ctx(SMBCCTX *context,
2090 struct timespec write_time_ts;
2091 struct timespec access_time_ts;
2092 struct timespec change_time_ts;
2097 if (!context || !context->internal ||
2098 !context->internal->_initialized) {
2100 errno = EINVAL; /* Best I can think of ... */
2112 DEBUG(4, ("smbc_stat(%s)\n", fname));
2114 if (smbc_parse_path(context, fname,
2115 workgroup, sizeof(workgroup),
2116 server, sizeof(server),
2117 share, sizeof(share),
2120 password, sizeof(password),
2126 if (user[0] == (char)0) fstrcpy(user, context->user);
2128 srv = smbc_server(context, True,
2129 server, share, workgroup, user, password);
2132 return -1; /* errno set by smbc_server */
2135 if (!smbc_getatr(context, srv, path, &mode, &size,
2142 errno = smbc_errno(context, srv->cli);
2149 smbc_setup_stat(context, st, path, size, mode);
2151 set_atimespec(st, access_time_ts);
2152 set_ctimespec(st, change_time_ts);
2153 set_mtimespec(st, write_time_ts);
2154 st->st_dev = srv->dev;
2161 * Routine to stat a file given an fd
2165 smbc_fstat_ctx(SMBCCTX *context,
2169 struct timespec change_time_ts;
2170 struct timespec access_time_ts;
2171 struct timespec write_time_ts;
2180 struct cli_state *targetcli;
2183 if (!context || !context->internal ||
2184 !context->internal->_initialized) {
2191 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2200 return context->fstatdir(context, file, st);
2204 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2205 if (smbc_parse_path(context, file->fname,
2207 server, sizeof(server),
2208 share, sizeof(share),
2211 password, sizeof(password),
2217 /*d_printf(">>>fstat: resolving %s\n", path);*/
2218 if (!cli_resolve_path("", file->srv->cli, path,
2219 &targetcli, targetpath))
2221 d_printf("Could not resolve %s\n", path);
2224 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2226 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2233 time_t change_time, access_time, write_time;
2235 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2236 &change_time, &access_time, &write_time)) {
2242 change_time_ts = convert_time_t_to_timespec(change_time);
2243 access_time_ts = convert_time_t_to_timespec(access_time);
2244 write_time_ts = convert_time_t_to_timespec(write_time);
2249 smbc_setup_stat(context, st, file->fname, size, mode);
2251 set_atimespec(st, access_time_ts);
2252 set_ctimespec(st, change_time_ts);
2253 set_mtimespec(st, write_time_ts);
2254 st->st_dev = file->srv->dev;
2261 * Routine to open a directory
2262 * We accept the URL syntax explained in smbc_parse_path(), above.
2266 smbc_remove_dir(SMBCFILE *dir)
2268 struct smbc_dir_list *d,*f;
2275 SAFE_FREE(f->dirent);
2280 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2285 add_dirent(SMBCFILE *dir,
2287 const char *comment,
2290 struct smbc_dirent *dirent;
2292 int name_length = (name == NULL ? 0 : strlen(name));
2293 int comment_len = (comment == NULL ? 0 : strlen(comment));
2296 * Allocate space for the dirent, which must be increased by the
2297 * size of the name and the comment and 1 each for the null terminator.
2300 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2302 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2306 dir->dir_error = ENOMEM;
2311 ZERO_STRUCTP(dirent);
2313 if (dir->dir_list == NULL) {
2315 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2316 if (!dir->dir_list) {
2319 dir->dir_error = ENOMEM;
2323 ZERO_STRUCTP(dir->dir_list);
2325 dir->dir_end = dir->dir_next = dir->dir_list;
2329 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2331 if (!dir->dir_end->next) {
2334 dir->dir_error = ENOMEM;
2338 ZERO_STRUCTP(dir->dir_end->next);
2340 dir->dir_end = dir->dir_end->next;
2343 dir->dir_end->next = NULL;
2344 dir->dir_end->dirent = dirent;
2346 dirent->smbc_type = type;
2347 dirent->namelen = name_length;
2348 dirent->commentlen = comment_len;
2349 dirent->dirlen = size;
2352 * dirent->namelen + 1 includes the null (no null termination needed)
2353 * Ditto for dirent->commentlen.
2354 * The space for the two null bytes was allocated.
2356 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2357 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2358 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2365 list_unique_wg_fn(const char *name,
2367 const char *comment,
2370 SMBCFILE *dir = (SMBCFILE *)state;
2371 struct smbc_dir_list *dir_list;
2372 struct smbc_dirent *dirent;
2376 dirent_type = dir->dir_type;
2378 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2380 /* An error occurred, what do we do? */
2381 /* FIXME: Add some code here */
2384 /* Point to the one just added */
2385 dirent = dir->dir_end->dirent;
2387 /* See if this was a duplicate */
2388 for (dir_list = dir->dir_list;
2389 dir_list != dir->dir_end;
2390 dir_list = dir_list->next) {
2392 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2393 /* Duplicate. End end of list need to be removed. */
2397 if (do_remove && dir_list->next == dir->dir_end) {
2398 /* Found the end of the list. Remove it. */
2399 dir->dir_end = dir_list;
2400 free(dir_list->next);
2402 dir_list->next = NULL;
2409 list_fn(const char *name,
2411 const char *comment,
2414 SMBCFILE *dir = (SMBCFILE *)state;
2418 * We need to process the type a little ...
2420 * Disk share = 0x00000000
2421 * Print share = 0x00000001
2422 * Comms share = 0x00000002 (obsolete?)
2423 * IPC$ share = 0x00000003
2425 * administrative shares:
2426 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2429 if (dir->dir_type == SMBC_FILE_SHARE) {
2432 case 0 | 0x80000000:
2434 dirent_type = SMBC_FILE_SHARE;
2438 dirent_type = SMBC_PRINTER_SHARE;
2442 dirent_type = SMBC_COMMS_SHARE;
2445 case 3 | 0x80000000:
2447 dirent_type = SMBC_IPC_SHARE;
2451 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2456 dirent_type = dir->dir_type;
2459 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2461 /* An error occurred, what do we do? */
2462 /* FIXME: Add some code here */
2468 dir_list_fn(const char *mnt,
2474 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2475 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2477 /* Handle an error ... */
2479 /* FIXME: Add some code ... */
2486 net_share_enum_rpc(struct cli_state *cli,
2487 void (*fn)(const char *name,
2489 const char *comment,
2496 uint32 info_level = 1;
2497 uint32 preferred_len = 0xffffffff;
2498 struct srvsvc_NetShareCtr1 ctr1;
2499 union srvsvc_NetShareCtr ctr;
2501 struct rpc_pipe_client *pipe_hnd;
2505 /* Open the server service pipe */
2506 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2508 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2512 /* Allocate a context for parsing and for the entries in "ctr" */
2513 mem_ctx = talloc_init("libsmbclient: net_share_enum_rpc");
2514 if (mem_ctx == NULL) {
2515 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2516 cli_rpc_pipe_close(pipe_hnd);
2523 /* Issue the NetShareEnum RPC call and retrieve the response */
2525 result = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
2526 &info_level, &ctr, preferred_len,
2527 &numentries, &enum_hnd);
2529 /* Was it successful? */
2530 if (!NT_STATUS_IS_OK(result) || numentries == 0) {
2531 /* Nope. Go clean up. */
2535 /* For each returned entry... */
2536 for (i = 0; i < numentries; i++) {
2538 /* Add this share to the list */
2539 (*fn)(ctr.ctr1->array[i].name,
2540 ctr.ctr1->array[i].type,
2541 ctr.ctr1->array[i].comment, state);
2545 /* Close the server service pipe */
2546 cli_rpc_pipe_close(pipe_hnd);
2548 /* Free all memory which was allocated for this request */
2549 TALLOC_FREE(mem_ctx);
2551 /* Tell 'em if it worked */
2552 return NT_STATUS_IS_OK(result) ? 0 : -1;
2558 smbc_opendir_ctx(SMBCCTX *context,
2562 fstring server, share, user, password, options;
2567 SMBCSRV *srv = NULL;
2568 SMBCFILE *dir = NULL;
2569 struct _smbc_callbacks *cb;
2570 struct in_addr rem_ip;
2572 if (!context || !context->internal ||
2573 !context->internal->_initialized) {
2574 DEBUG(4, ("no valid context\n"));
2575 errno = EINVAL + 8192;
2581 DEBUG(4, ("no valid fname\n"));
2582 errno = EINVAL + 8193;
2586 if (smbc_parse_path(context, fname,
2587 workgroup, sizeof(workgroup),
2588 server, sizeof(server),
2589 share, sizeof(share),
2592 password, sizeof(password),
2593 options, sizeof(options))) {
2594 DEBUG(4, ("no valid path\n"));
2595 errno = EINVAL + 8194;
2599 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2600 "path='%s' options='%s'\n",
2601 fname, server, share, path, options));
2603 /* Ensure the options are valid */
2604 if (smbc_check_options(server, share, path, options)) {
2605 DEBUG(4, ("unacceptable options (%s)\n", options));
2606 errno = EINVAL + 8195;
2610 if (user[0] == (char)0) fstrcpy(user, context->user);
2612 dir = SMB_MALLOC_P(SMBCFILE);
2624 dir->fname = SMB_STRDUP(fname);
2628 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2630 if (server[0] == (char)0) {
2635 struct ip_service *ip_list;
2636 struct ip_service server_addr;
2637 struct user_auth_info u_info;
2638 struct cli_state *cli;
2640 if (share[0] != (char)0 || path[0] != (char)0) {
2642 errno = EINVAL + 8196;
2644 SAFE_FREE(dir->fname);
2650 /* Determine how many local master browsers to query */
2651 max_lmb_count = (context->options.browse_max_lmb_count == 0
2653 : context->options.browse_max_lmb_count);
2655 pstrcpy(u_info.username, user);
2656 pstrcpy(u_info.password, password);
2659 * We have server and share and path empty but options
2660 * requesting that we scan all master browsers for their list
2661 * of workgroups/domains. This implies that we must first try
2662 * broadcast queries to find all master browsers, and if that
2663 * doesn't work, then try our other methods which return only
2664 * a single master browser.
2668 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
2672 if (!find_master_ip(workgroup, &server_addr.ip)) {
2675 SAFE_FREE(dir->fname);
2682 ip_list = &server_addr;
2686 for (i = 0; i < count && i < max_lmb_count; i++) {
2687 DEBUG(99, ("Found master browser %d of %d: %s\n",
2688 i+1, MAX(count, max_lmb_count),
2689 inet_ntoa(ip_list[i].ip)));
2691 cli = get_ipc_connect_master_ip(&ip_list[i],
2692 workgroup, &u_info);
2693 /* cli == NULL is the master browser refused to talk or
2694 could not be found */
2698 fstrcpy(server, cli->desthost);
2701 DEBUG(4, ("using workgroup %s %s\n",
2702 workgroup, server));
2705 * For each returned master browser IP address, get a
2706 * connection to IPC$ on the server if we do not
2707 * already have one, and determine the
2708 * workgroups/domains that it knows about.
2711 srv = smbc_server(context, True, server, "IPC$",
2712 workgroup, user, password);
2718 dir->dir_type = SMBC_WORKGROUP;
2720 /* Now, list the stuff ... */
2722 if (!cli_NetServerEnum(srv->cli,
2724 SV_TYPE_DOMAIN_ENUM,
2734 * Server not an empty string ... Check the rest and see what
2737 if (*share == '\0') {
2738 if (*path != '\0') {
2740 /* Should not have empty share with path */
2741 errno = EINVAL + 8197;
2743 SAFE_FREE(dir->fname);
2751 * We don't know if <server> is really a server name
2752 * or is a workgroup/domain name. If we already have
2753 * a server structure for it, we'll use it.
2754 * Otherwise, check to see if <server><1D>,
2755 * <server><1B>, or <server><20> translates. We check
2756 * to see if <server> is an IP address first.
2760 * See if we have an existing server. Do not
2761 * establish a connection if one does not already
2764 srv = smbc_server(context, False, server, "IPC$",
2765 workgroup, user, password);
2768 * If no existing server and not an IP addr, look for
2772 !is_ipaddress(server) &&
2773 (resolve_name(server, &rem_ip, 0x1d) || /* LMB */
2774 resolve_name(server, &rem_ip, 0x1b) )) { /* DMB */
2778 dir->dir_type = SMBC_SERVER;
2781 * Get the backup list ...
2783 if (!name_status_find(server, 0, 0,
2784 rem_ip, buserver)) {
2786 DEBUG(0, ("Could not get name of "
2787 "local/domain master browser "
2788 "for server %s\n", server));
2790 SAFE_FREE(dir->fname);
2799 * Get a connection to IPC$ on the server if
2800 * we do not already have one
2802 srv = smbc_server(context, True,
2804 workgroup, user, password);
2806 DEBUG(0, ("got no contact to IPC$\n"));
2808 SAFE_FREE(dir->fname);
2817 /* Now, list the servers ... */
2818 if (!cli_NetServerEnum(srv->cli, server,
2819 0x0000FFFE, list_fn,
2823 SAFE_FREE(dir->fname);
2829 (resolve_name(server, &rem_ip, 0x20))) {
2831 /* If we hadn't found the server, get one now */
2833 srv = smbc_server(context, True,
2841 SAFE_FREE(dir->fname);
2848 dir->dir_type = SMBC_FILE_SHARE;
2851 /* List the shares ... */
2853 if (net_share_enum_rpc(
2856 (void *) dir) < 0 &&
2862 errno = cli_errno(srv->cli);
2864 SAFE_FREE(dir->fname);
2871 /* Neither the workgroup nor server exists */
2872 errno = ECONNREFUSED;
2874 SAFE_FREE(dir->fname);
2883 * The server and share are specified ... work from
2887 struct cli_state *targetcli;
2889 /* We connect to the server and list the directory */
2890 dir->dir_type = SMBC_FILE_SHARE;
2892 srv = smbc_server(context, True, server, share,
2893 workgroup, user, password);
2898 SAFE_FREE(dir->fname);
2907 /* Now, list the files ... */
2909 p = path + strlen(path);
2910 pstrcat(path, "\\*");
2912 if (!cli_resolve_path("", srv->cli, path,
2913 &targetcli, targetpath))
2915 d_printf("Could not resolve %s\n", path);
2917 SAFE_FREE(dir->fname);
2923 if (cli_list(targetcli, targetpath,
2924 aDIR | aSYSTEM | aHIDDEN,
2925 dir_list_fn, (void *)dir) < 0) {
2928 SAFE_FREE(dir->fname);
2931 saved_errno = smbc_errno(context, targetcli);
2933 if (saved_errno == EINVAL) {
2935 * See if they asked to opendir something
2936 * other than a directory. If so, the
2937 * converted error value we got would have
2938 * been EINVAL rather than ENOTDIR.
2940 *p = '\0'; /* restore original path */
2942 if (smbc_getatr(context, srv, path,
2944 NULL, NULL, NULL, NULL,
2946 ! IS_DOS_DIR(mode)) {
2948 /* It is. Correct the error value */
2949 saved_errno = ENOTDIR;
2954 * If there was an error and the server is no
2957 cb = &context->callbacks;
2958 if (cli_is_error(targetcli) &&
2959 cb->check_server_fn(context, srv)) {
2961 /* ... then remove it. */
2962 if (cb->remove_unused_server_fn(context,
2965 * We could not remove the server
2966 * completely, remove it from the
2967 * cache so we will not get it
2968 * again. It will be removed when the
2969 * last file/dir is closed.
2971 cb->remove_cached_srv_fn(context, srv);
2975 errno = saved_errno;
2982 DLIST_ADD(context->internal->_files, dir);
2988 * Routine to close a directory
2992 smbc_closedir_ctx(SMBCCTX *context,
2996 if (!context || !context->internal ||
2997 !context->internal->_initialized) {
3004 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3011 smbc_remove_dir(dir); /* Clean it up */
3013 DLIST_REMOVE(context->internal->_files, dir);
3017 SAFE_FREE(dir->fname);
3018 SAFE_FREE(dir); /* Free the space too */
3026 smbc_readdir_internal(SMBCCTX * context,
3027 struct smbc_dirent *dest,
3028 struct smbc_dirent *src,
3029 int max_namebuf_len)
3031 if (context->options.urlencode_readdir_entries) {
3033 /* url-encode the name. get back remaining buffer space */
3035 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3037 /* We now know the name length */
3038 dest->namelen = strlen(dest->name);
3040 /* Save the pointer to the beginning of the comment */
3041 dest->comment = dest->name + dest->namelen + 1;
3043 /* Copy the comment */
3044 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3045 dest->comment[max_namebuf_len - 1] = '\0';
3047 /* Save other fields */
3048 dest->smbc_type = src->smbc_type;
3049 dest->commentlen = strlen(dest->comment);
3050 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3054 /* No encoding. Just copy the entry as is. */
3055 memcpy(dest, src, src->dirlen);
3056 dest->comment = (char *)(&dest->name + src->namelen + 1);
3062 * Routine to get a directory entry
3065 struct smbc_dirent *
3066 smbc_readdir_ctx(SMBCCTX *context,
3070 struct smbc_dirent *dirp, *dirent;
3072 /* Check that all is ok first ... */
3074 if (!context || !context->internal ||
3075 !context->internal->_initialized) {
3078 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3083 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3086 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3091 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3094 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3099 if (!dir->dir_next) {
3103 dirent = dir->dir_next->dirent;
3111 dirp = (struct smbc_dirent *)context->internal->_dirent;
3112 maxlen = (sizeof(context->internal->_dirent) -
3113 sizeof(struct smbc_dirent));
3115 smbc_readdir_internal(context, dirp, dirent, maxlen);
3117 dir->dir_next = dir->dir_next->next;
3123 * Routine to get directory entries
3127 smbc_getdents_ctx(SMBCCTX *context,
3129 struct smbc_dirent *dirp,
3135 char *ndir = (char *)dirp;
3136 struct smbc_dir_list *dirlist;
3138 /* Check that all is ok first ... */
3140 if (!context || !context->internal ||
3141 !context->internal->_initialized) {
3148 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3155 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3163 * Now, retrieve the number of entries that will fit in what was passed
3164 * We have to figure out if the info is in the list, or we need to
3165 * send a request to the server to get the info.
3168 while ((dirlist = dir->dir_next)) {
3169 struct smbc_dirent *dirent;
3171 if (!dirlist->dirent) {
3173 errno = ENOENT; /* Bad error */
3178 /* Do urlencoding of next entry, if so selected */
3179 dirent = (struct smbc_dirent *)context->internal->_dirent;
3180 maxlen = (sizeof(context->internal->_dirent) -
3181 sizeof(struct smbc_dirent));
3182 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3184 reqd = dirent->dirlen;
3188 if (rem < count) { /* We managed to copy something */
3194 else { /* Nothing copied ... */
3196 errno = EINVAL; /* Not enough space ... */
3203 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3205 ((struct smbc_dirent *)ndir)->comment =
3206 (char *)(&((struct smbc_dirent *)ndir)->name +
3214 dir->dir_next = dirlist = dirlist -> next;
3225 * Routine to create a directory ...
3229 smbc_mkdir_ctx(SMBCCTX *context,
3239 pstring path, targetpath;
3240 struct cli_state *targetcli;
3242 if (!context || !context->internal ||
3243 !context->internal->_initialized) {
3257 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3259 if (smbc_parse_path(context, fname,
3260 workgroup, sizeof(workgroup),
3261 server, sizeof(server),
3262 share, sizeof(share),
3265 password, sizeof(password),
3271 if (user[0] == (char)0) fstrcpy(user, context->user);
3273 srv = smbc_server(context, True,
3274 server, share, workgroup, user, password);
3278 return -1; /* errno set by smbc_server */
3282 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3283 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3285 d_printf("Could not resolve %s\n", path);
3288 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3290 if (!cli_mkdir(targetcli, targetpath)) {
3292 errno = smbc_errno(context, targetcli);
3302 * Our list function simply checks to see if a directory is not empty
3305 static int smbc_rmdir_dirempty = True;
3308 rmdir_list_fn(const char *mnt,
3313 if (strncmp(finfo->name, ".", 1) != 0 &&
3314 strncmp(finfo->name, "..", 2) != 0) {
3316 smbc_rmdir_dirempty = False;
3321 * Routine to remove a directory
3325 smbc_rmdir_ctx(SMBCCTX *context,
3336 struct cli_state *targetcli;
3338 if (!context || !context->internal ||
3339 !context->internal->_initialized) {
3353 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3355 if (smbc_parse_path(context, fname,
3356 workgroup, sizeof(workgroup),
3357 server, sizeof(server),
3358 share, sizeof(share),
3361 password, sizeof(password),
3368 if (user[0] == (char)0) fstrcpy(user, context->user);
3370 srv = smbc_server(context, True,
3371 server, share, workgroup, user, password);
3375 return -1; /* errno set by smbc_server */
3379 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3380 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3382 d_printf("Could not resolve %s\n", path);
3385 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3388 if (!cli_rmdir(targetcli, targetpath)) {
3390 errno = smbc_errno(context, targetcli);
3392 if (errno == EACCES) { /* Check if the dir empty or not */
3394 /* Local storage to avoid buffer overflows */
3397 smbc_rmdir_dirempty = True; /* Make this so ... */
3399 pstrcpy(lpath, targetpath);
3400 pstrcat(lpath, "\\*");
3402 if (cli_list(targetcli, lpath,
3403 aDIR | aSYSTEM | aHIDDEN,
3404 rmdir_list_fn, NULL) < 0) {
3406 /* Fix errno to ignore latest error ... */
3407 DEBUG(5, ("smbc_rmdir: "
3408 "cli_list returned an error: %d\n",
3409 smbc_errno(context, targetcli)));
3414 if (smbc_rmdir_dirempty)
3430 * Routine to return the current directory position
3434 smbc_telldir_ctx(SMBCCTX *context,
3437 if (!context || !context->internal ||
3438 !context->internal->_initialized) {
3445 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3452 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3459 /* See if we're already at the end. */
3460 if (dir->dir_next == NULL) {
3466 * We return the pointer here as the offset
3468 return (off_t)(long)dir->dir_next->dirent;
3472 * A routine to run down the list and see if the entry is OK
3475 struct smbc_dir_list *
3476 smbc_check_dir_ent(struct smbc_dir_list *list,
3477 struct smbc_dirent *dirent)
3480 /* Run down the list looking for what we want */
3484 struct smbc_dir_list *tmp = list;
3488 if (tmp->dirent == dirent)
3497 return NULL; /* Not found, or an error */
3503 * Routine to seek on a directory
3507 smbc_lseekdir_ctx(SMBCCTX *context,
3511 long int l_offset = offset; /* Handle problems of size */
3512 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3513 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3515 if (!context || !context->internal ||
3516 !context->internal->_initialized) {
3523 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3530 /* Now, check what we were passed and see if it is OK ... */
3532 if (dirent == NULL) { /* Seek to the begining of the list */
3534 dir->dir_next = dir->dir_list;
3539 /* Now, run down the list and make sure that the entry is OK */
3540 /* This may need to be changed if we change the format of the list */
3542 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3544 errno = EINVAL; /* Bad entry */
3549 dir->dir_next = list_ent;
3556 * Routine to fstat a dir
3560 smbc_fstatdir_ctx(SMBCCTX *context,
3565 if (!context || !context->internal ||
3566 !context->internal->_initialized) {
3573 /* No code yet ... */
3580 smbc_chmod_ctx(SMBCCTX *context,
3593 if (!context || !context->internal ||
3594 !context->internal->_initialized) {
3596 errno = EINVAL; /* Best I can think of ... */
3608 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3610 if (smbc_parse_path(context, fname,
3611 workgroup, sizeof(workgroup),
3612 server, sizeof(server),
3613 share, sizeof(share),
3616 password, sizeof(password),
3622 if (user[0] == (char)0) fstrcpy(user, context->user);
3624 srv = smbc_server(context, True,
3625 server, share, workgroup, user, password);
3628 return -1; /* errno set by smbc_server */
3633 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3634 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3635 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3636 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3638 if (!cli_setatr(srv->cli, path, mode, 0)) {
3639 errno = smbc_errno(context, srv->cli);
3647 smbc_utimes_ctx(SMBCCTX *context,
3649 struct timeval *tbuf)
3661 if (!context || !context->internal ||
3662 !context->internal->_initialized) {
3664 errno = EINVAL; /* Best I can think of ... */
3677 access_time = write_time = time(NULL);
3679 access_time = tbuf[0].tv_sec;
3680 write_time = tbuf[1].tv_sec;
3689 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3690 atimebuf[sizeof(atimebuf) - 1] = '\0';
3691 if ((p = strchr(atimebuf, '\n')) != NULL) {
3695 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3696 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3697 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3701 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3702 fname, atimebuf, mtimebuf);
3705 if (smbc_parse_path(context, fname,
3706 workgroup, sizeof(workgroup),
3707 server, sizeof(server),
3708 share, sizeof(share),
3711 password, sizeof(password),
3717 if (user[0] == (char)0) fstrcpy(user, context->user);
3719 srv = smbc_server(context, True,
3720 server, share, workgroup, user, password);
3723 return -1; /* errno set by smbc_server */
3726 if (!smbc_setatr(context, srv, path,
3727 0, access_time, write_time, 0, 0)) {
3728 return -1; /* errno set by smbc_setatr */
3735 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
3736 However NT4 gives a "The information may have been modified by a
3737 computer running Windows NT 5.0" if denied ACEs do not appear before
3741 ace_compare(SEC_ACE *ace1,
3744 if (sec_ace_equal(ace1, ace2))
3747 if (ace1->type != ace2->type)
3748 return ace2->type - ace1->type;
3750 if (sid_compare(&ace1->trustee, &ace2->trustee))
3751 return sid_compare(&ace1->trustee, &ace2->trustee);
3753 if (ace1->flags != ace2->flags)
3754 return ace1->flags - ace2->flags;
3756 if (ace1->access_mask != ace2->access_mask)
3757 return ace1->access_mask - ace2->access_mask;
3759 if (ace1->size != ace2->size)
3760 return ace1->size - ace2->size;
3762 return memcmp(ace1, ace2, sizeof(SEC_ACE));
3767 sort_acl(SEC_ACL *the_acl)
3770 if (!the_acl) return;
3772 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
3773 QSORT_CAST ace_compare);
3775 for (i=1;i<the_acl->num_aces;) {
3776 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
3778 for (j=i; j<the_acl->num_aces-1; j++) {
3779 the_acl->aces[j] = the_acl->aces[j+1];
3781 the_acl->num_aces--;
3788 /* convert a SID to a string, either numeric or username/group */
3790 convert_sid_to_string(struct cli_state *ipc_cli,
3796 char **domains = NULL;
3797 char **names = NULL;
3798 enum lsa_SidType *types = NULL;
3799 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3800 sid_to_string(str, sid);
3803 return; /* no lookup desired */
3810 /* Ask LSA to convert the sid to a name */
3812 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ipc_cli->mem_ctx,
3813 pol, 1, sid, &domains,
3815 !domains || !domains[0] || !names || !names[0]) {
3821 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3822 domains[0], lp_winbind_separator(),
3826 /* convert a string to a SID, either numeric or username/group */
3828 convert_string_to_sid(struct cli_state *ipc_cli,
3834 enum lsa_SidType *types = NULL;
3835 DOM_SID *sids = NULL;
3837 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3844 if (strncmp(str, "S-", 2) == 0) {
3845 return string_to_sid(sid, str);
3852 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx,
3853 pol, 1, &str, NULL, 1, &sids,
3859 sid_copy(sid, &sids[0]);
3866 /* parse an ACE in the same format as print_ace() */
3868 parse_ace(struct cli_state *ipc_cli,
3878 unsigned int aflags;
3882 const struct perm_value *v;
3888 /* These values discovered by inspection */
3889 static const struct perm_value special_values[] = {
3890 { "R", 0x00120089 },
3891 { "W", 0x00120116 },
3892 { "X", 0x001200a0 },
3893 { "D", 0x00010000 },
3894 { "P", 0x00040000 },
3895 { "O", 0x00080000 },
3899 static const struct perm_value standard_values[] = {
3900 { "READ", 0x001200a9 },
3901 { "CHANGE", 0x001301bf },
3902 { "FULL", 0x001f01ff },
3908 p = strchr_m(str,':');
3909 if (!p) return False;
3912 /* Try to parse numeric form */
3914 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3915 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3919 /* Try to parse text form */
3921 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3926 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3930 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3931 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3932 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3933 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3938 /* Only numeric form accepted for flags at present */
3940 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3941 sscanf(tok, "%i", &aflags))) {
3945 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3949 if (strncmp(tok, "0x", 2) == 0) {
3950 if (sscanf(tok, "%i", &amask) != 1) {
3956 for (v = standard_values; v->perm; v++) {
3957 if (strcmp(tok, v->perm) == 0) {
3968 for (v = special_values; v->perm; v++) {
3969 if (v->perm[0] == *p) {
3975 if (!found) return False;
3985 init_sec_ace(ace, &sid, atype, mask, aflags);
3989 /* add an ACE to a list of ACEs in a SEC_ACL */
3991 add_ace(SEC_ACL **the_acl,
3999 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
4003 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4006 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4007 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4008 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4009 1+(*the_acl)->num_aces, aces);
4011 (*the_acl) = newacl;
4016 /* parse a ascii version of a security descriptor */
4018 sec_desc_parse(TALLOC_CTX *ctx,
4019 struct cli_state *ipc_cli,
4024 const char *p = str;
4026 SEC_DESC *ret = NULL;
4028 DOM_SID *grp_sid=NULL;
4029 DOM_SID *owner_sid=NULL;
4033 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4035 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4036 revision = strtol(tok+9, NULL, 16);
4040 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4042 DEBUG(5, ("OWNER specified more than once!\n"));
4045 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4047 !convert_string_to_sid(ipc_cli, pol,
4049 owner_sid, tok+6)) {
4050 DEBUG(5, ("Failed to parse owner sid\n"));
4056 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4058 DEBUG(5, ("OWNER specified more than once!\n"));
4061 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4063 !convert_string_to_sid(ipc_cli, pol,
4065 owner_sid, tok+7)) {
4066 DEBUG(5, ("Failed to parse owner sid\n"));
4072 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4074 DEBUG(5, ("GROUP specified more than once!\n"));
4077 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4079 !convert_string_to_sid(ipc_cli, pol,
4082 DEBUG(5, ("Failed to parse group sid\n"));
4088 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4090 DEBUG(5, ("GROUP specified more than once!\n"));
4093 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4095 !convert_string_to_sid(ipc_cli, pol,
4098 DEBUG(5, ("Failed to parse group sid\n"));
4104 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4106 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4107 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4110 if(!add_ace(&dacl, &ace, ctx)) {
4111 DEBUG(5, ("Failed to add ACL %s\n", tok));
4117 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4119 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4120 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4123 if(!add_ace(&dacl, &ace, ctx)) {
4124 DEBUG(5, ("Failed to add ACL %s\n", tok));
4130 DEBUG(5, ("Failed to parse security descriptor\n"));
4134 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4135 owner_sid, grp_sid, NULL, dacl, &sd_size);
4139 SAFE_FREE(owner_sid);
4145 /* Obtain the current dos attributes */
4146 static DOS_ATTR_DESC *
4147 dos_attr_query(SMBCCTX *context,
4149 const char *filename,
4152 struct timespec create_time_ts;
4153 struct timespec write_time_ts;
4154 struct timespec access_time_ts;
4155 struct timespec change_time_ts;
4158 SMB_INO_T inode = 0;
4161 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4167 /* Obtain the DOS attributes */
4168 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4176 errno = smbc_errno(context, srv->cli);
4177 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4184 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4185 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4186 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4187 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4194 /* parse a ascii version of a security descriptor */
4196 dos_attr_parse(SMBCCTX *context,
4202 const char *p = str;
4205 const char * create_time_attr;
4206 const char * access_time_attr;
4207 const char * write_time_attr;
4208 const char * change_time_attr;
4211 /* Determine whether to use old-style or new-style attribute names */
4212 if (context->internal->_full_time_names) {
4213 /* new-style names */
4214 attr_strings.create_time_attr = "CREATE_TIME";
4215 attr_strings.access_time_attr = "ACCESS_TIME";
4216 attr_strings.write_time_attr = "WRITE_TIME";
4217 attr_strings.change_time_attr = "CHANGE_TIME";
4219 /* old-style names */
4220 attr_strings.create_time_attr = NULL;
4221 attr_strings.access_time_attr = "A_TIME";
4222 attr_strings.write_time_attr = "M_TIME";
4223 attr_strings.change_time_attr = "C_TIME";
4226 /* if this is to set the entire ACL... */
4228 /* ... then increment past the first colon if there is one */
4229 if ((p = strchr(str, ':')) != NULL) {
4236 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4238 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4239 dad->mode = strtol(tok+5, NULL, 16);
4243 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4244 dad->size = (SMB_OFF_T)atof(tok+5);
4248 n = strlen(attr_strings.access_time_attr);
4249 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4250 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4254 n = strlen(attr_strings.change_time_attr);
4255 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4256 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4260 n = strlen(attr_strings.write_time_attr);
4261 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4262 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4266 if (attr_strings.create_time_attr != NULL) {
4267 n = strlen(attr_strings.create_time_attr);
4268 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4270 dad->create_time = (time_t)strtol(tok+n+1,
4276 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4277 dad->inode = (SMB_INO_T)atof(tok+6);
4283 /*****************************************************
4284 Retrieve the acls for a file.
4285 *******************************************************/
4288 cacl_get(SMBCCTX *context,
4291 struct cli_state *ipc_cli,
4307 BOOL exclude_nt_revision = False;
4308 BOOL exclude_nt_owner = False;
4309 BOOL exclude_nt_group = False;
4310 BOOL exclude_nt_acl = False;
4311 BOOL exclude_dos_mode = False;
4312 BOOL exclude_dos_size = False;
4313 BOOL exclude_dos_create_time = False;
4314 BOOL exclude_dos_access_time = False;
4315 BOOL exclude_dos_write_time = False;
4316 BOOL exclude_dos_change_time = False;
4317 BOOL exclude_dos_inode = False;
4318 BOOL numeric = True;
4319 BOOL determine_size = (bufsize == 0);
4323 fstring name_sandbox;
4327 struct timespec create_time_ts;
4328 struct timespec write_time_ts;
4329 struct timespec access_time_ts;
4330 struct timespec change_time_ts;
4331 time_t create_time = (time_t)0;
4332 time_t write_time = (time_t)0;
4333 time_t access_time = (time_t)0;
4334 time_t change_time = (time_t)0;
4338 struct cli_state *cli = srv->cli;
4340 const char * create_time_attr;
4341 const char * access_time_attr;
4342 const char * write_time_attr;
4343 const char * change_time_attr;
4346 const char * create_time_attr;
4347 const char * access_time_attr;
4348 const char * write_time_attr;
4349 const char * change_time_attr;
4350 } excl_attr_strings;
4352 /* Determine whether to use old-style or new-style attribute names */
4353 if (context->internal->_full_time_names) {
4354 /* new-style names */
4355 attr_strings.create_time_attr = "CREATE_TIME";
4356 attr_strings.access_time_attr = "ACCESS_TIME";
4357 attr_strings.write_time_attr = "WRITE_TIME";
4358 attr_strings.change_time_attr = "CHANGE_TIME";
4360 excl_attr_strings.create_time_attr = "CREATE_TIME";
4361 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4362 excl_attr_strings.write_time_attr = "WRITE_TIME";
4363 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4365 /* old-style names */
4366 attr_strings.create_time_attr = NULL;
4367 attr_strings.access_time_attr = "A_TIME";
4368 attr_strings.write_time_attr = "M_TIME";
4369 attr_strings.change_time_attr = "C_TIME";
4371 excl_attr_strings.create_time_attr = NULL;
4372 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4373 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4374 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4377 /* Copy name so we can strip off exclusions (if any are specified) */
4378 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4380 /* Ensure name is null terminated */
4381 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4383 /* Play in the sandbox */
4384 name = name_sandbox;
4386 /* If there are any exclusions, point to them and mask them from name */
4387 if ((pExclude = strchr(name, '!')) != NULL)
4392 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4393 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4394 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4395 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4396 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4397 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4398 numeric = (* (name + strlen(name) - 1) != '+');
4400 /* Look for exclusions from "all" requests */
4401 if (all || all_nt || all_dos) {
4403 /* Exclusions are delimited by '!' */
4406 pExclude = (p == NULL ? NULL : p + 1)) {
4408 /* Find end of this exclusion name */
4409 if ((p = strchr(pExclude, '!')) != NULL)
4414 /* Which exclusion name is this? */
4415 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4416 exclude_nt_revision = True;
4418 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4419 exclude_nt_owner = True;
4421 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4422 exclude_nt_group = True;
4424 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4425 exclude_nt_acl = True;
4427 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4428 exclude_dos_mode = True;
4430 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4431 exclude_dos_size = True;
4433 else if (excl_attr_strings.create_time_attr != NULL &&
4434 StrCaseCmp(pExclude,
4435 excl_attr_strings.change_time_attr) == 0) {
4436 exclude_dos_create_time = True;
4438 else if (StrCaseCmp(pExclude,
4439 excl_attr_strings.access_time_attr) == 0) {
4440 exclude_dos_access_time = True;
4442 else if (StrCaseCmp(pExclude,
4443 excl_attr_strings.write_time_attr) == 0) {
4444 exclude_dos_write_time = True;
4446 else if (StrCaseCmp(pExclude,
4447 excl_attr_strings.change_time_attr) == 0) {
4448 exclude_dos_change_time = True;
4450 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4451 exclude_dos_inode = True;
4454 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4465 * If we are (possibly) talking to an NT or new system and some NT
4466 * attributes have been requested...
4468 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4469 /* Point to the portion after "system.nt_sec_desc." */
4470 name += 19; /* if (all) this will be invalid but unused */
4472 /* ... then obtain any NT attributes which were requested */
4473 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4476 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4477 filename, cli_errstr(cli)));
4482 sd = cli_query_secdesc(cli, fnum, ctx);
4486 ("cacl_get Failed to query old descriptor\n"));
4491 cli_close(cli, fnum);
4493 if (! exclude_nt_revision) {
4494 if (all || all_nt) {
4495 if (determine_size) {
4496 p = talloc_asprintf(ctx,
4505 n = snprintf(buf, bufsize,
4509 } else if (StrCaseCmp(name, "revision") == 0) {
4510 if (determine_size) {
4511 p = talloc_asprintf(ctx, "%d",
4519 n = snprintf(buf, bufsize, "%d",
4524 if (!determine_size && n > bufsize) {
4534 if (! exclude_nt_owner) {
4535 /* Get owner and group sid */
4536 if (sd->owner_sid) {
4537 convert_sid_to_string(ipc_cli, pol,
4542 fstrcpy(sidstr, "");
4545 if (all || all_nt) {
4546 if (determine_size) {
4547 p = talloc_asprintf(ctx, ",OWNER:%s",
4554 } else if (sidstr[0] != '\0') {
4555 n = snprintf(buf, bufsize,
4556 ",OWNER:%s", sidstr);
4558 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4559 if (determine_size) {
4560 p = talloc_asprintf(ctx, "%s", sidstr);
4567 n = snprintf(buf, bufsize, "%s",
4572 if (!determine_size && n > bufsize) {
4582 if (! exclude_nt_group) {
4583 if (sd->group_sid) {
4584 convert_sid_to_string(ipc_cli, pol,
4588 fstrcpy(sidstr, "");
4591 if (all || all_nt) {
4592 if (determine_size) {
4593 p = talloc_asprintf(ctx, ",GROUP:%s",
4600 } else if (sidstr[0] != '\0') {
4601 n = snprintf(buf, bufsize,
4602 ",GROUP:%s", sidstr);
4604 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4605 if (determine_size) {
4606 p = talloc_asprintf(ctx, "%s", sidstr);
4613 n = snprintf(buf, bufsize,
4618 if (!determine_size && n > bufsize) {
4628 if (! exclude_nt_acl) {
4629 /* Add aces to value buffer */
4630 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4632 SEC_ACE *ace = &sd->dacl->aces[i];
4633 convert_sid_to_string(ipc_cli, pol,
4637 if (all || all_nt) {
4638 if (determine_size) {
4639 p = talloc_asprintf(
4655 ",ACL:%s:%d/%d/0x%08x",
4661 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4662 StrCaseCmp(name+3, sidstr) == 0) ||
4663 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4664 StrCaseCmp(name+4, sidstr) == 0)) {
4665 if (determine_size) {
4666 p = talloc_asprintf(
4678 n = snprintf(buf, bufsize,
4684 } else if (all_nt_acls) {
4685 if (determine_size) {
4686 p = talloc_asprintf(
4688 "%s%s:%d/%d/0x%08x",
4700 n = snprintf(buf, bufsize,
4701 "%s%s:%d/%d/0x%08x",
4709 if (!determine_size && n > bufsize) {
4720 /* Restore name pointer to its original value */
4724 if (all || some_dos) {
4725 /* Point to the portion after "system.dos_attr." */
4726 name += 16; /* if (all) this will be invalid but unused */
4728 /* Obtain the DOS attributes */
4729 if (!smbc_getatr(context, srv, filename, &mode, &size,
4736 errno = smbc_errno(context, srv->cli);
4741 create_time = convert_timespec_to_time_t(create_time_ts);
4742 access_time = convert_timespec_to_time_t(access_time_ts);
4743 write_time = convert_timespec_to_time_t(write_time_ts);
4744 change_time = convert_timespec_to_time_t(change_time_ts);
4746 if (! exclude_dos_mode) {
4747 if (all || all_dos) {
4748 if (determine_size) {
4749 p = talloc_asprintf(ctx,
4762 n = snprintf(buf, bufsize,
4770 } else if (StrCaseCmp(name, "mode") == 0) {
4771 if (determine_size) {
4772 p = talloc_asprintf(ctx, "0x%x", mode);
4779 n = snprintf(buf, bufsize,
4784 if (!determine_size && n > bufsize) {
4794 if (! exclude_dos_size) {
4795 if (all || all_dos) {
4796 if (determine_size) {
4797 p = talloc_asprintf(
4807 n = snprintf(buf, bufsize,
4811 } else if (StrCaseCmp(name, "size") == 0) {
4812 if (determine_size) {
4813 p = talloc_asprintf(
4823 n = snprintf(buf, bufsize,
4829 if (!determine_size && n > bufsize) {
4839 if (! exclude_dos_create_time &&
4840 attr_strings.create_time_attr != NULL) {
4841 if (all || all_dos) {
4842 if (determine_size) {
4843 p = talloc_asprintf(ctx,
4845 attr_strings.create_time_attr,
4853 n = snprintf(buf, bufsize,
4855 attr_strings.create_time_attr,
4858 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
4859 if (determine_size) {
4860 p = talloc_asprintf(ctx, "%lu", create_time);
4867 n = snprintf(buf, bufsize,
4868 "%lu", create_time);
4872 if (!determine_size && n > bufsize) {
4882 if (! exclude_dos_access_time) {
4883 if (all || all_dos) {
4884 if (determine_size) {
4885 p = talloc_asprintf(ctx,
4887 attr_strings.access_time_attr,
4895 n = snprintf(buf, bufsize,
4897 attr_strings.access_time_attr,
4900 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
4901 if (determine_size) {
4902 p = talloc_asprintf(ctx, "%lu", access_time);
4909 n = snprintf(buf, bufsize,
4910 "%lu", access_time);
4914 if (!determine_size && n > bufsize) {
4924 if (! exclude_dos_write_time) {
4925 if (all || all_dos) {
4926 if (determine_size) {
4927 p = talloc_asprintf(ctx,
4929 attr_strings.write_time_attr,
4937 n = snprintf(buf, bufsize,
4939 attr_strings.write_time_attr,
4942 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
4943 if (determine_size) {
4944 p = talloc_asprintf(ctx, "%lu", write_time);
4951 n = snprintf(buf, bufsize,
4956 if (!determine_size && n > bufsize) {
4966 if (! exclude_dos_change_time) {
4967 if (all || all_dos) {
4968 if (determine_size) {
4969 p = talloc_asprintf(ctx,
4971 attr_strings.change_time_attr,
4979 n = snprintf(buf, bufsize,
4981 attr_strings.change_time_attr,
4984 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
4985 if (determine_size) {
4986 p = talloc_asprintf(ctx, "%lu", change_time);
4993 n = snprintf(buf, bufsize,
4994 "%lu", change_time);
4998 if (!determine_size && n > bufsize) {
5008 if (! exclude_dos_inode) {
5009 if (all || all_dos) {
5010 if (determine_size) {
5011 p = talloc_asprintf(
5021 n = snprintf(buf, bufsize,
5025 } else if (StrCaseCmp(name, "inode") == 0) {
5026 if (determine_size) {
5027 p = talloc_asprintf(
5037 n = snprintf(buf, bufsize,
5043 if (!determine_size && n > bufsize) {
5053 /* Restore name pointer to its original value */
5066 /*****************************************************
5067 set the ACLs on a file given an ascii description
5068 *******************************************************/
5070 cacl_set(TALLOC_CTX *ctx,
5071 struct cli_state *cli,
5072 struct cli_state *ipc_cli,
5074 const char *filename,
5075 const char *the_acl,
5081 SEC_DESC *sd = NULL, *old;
5082 SEC_ACL *dacl = NULL;
5083 DOM_SID *owner_sid = NULL;
5084 DOM_SID *grp_sid = NULL;
5089 BOOL numeric = True;
5091 /* the_acl will be null for REMOVE_ALL operations */
5093 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5097 /* if this is to set the entire ACL... */
5098 if (*the_acl == '*') {
5099 /* ... then increment past the first colon */
5103 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5104 CONST_DISCARD(char *, the_acl));
5112 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5113 that doesn't deref sd */
5115 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5120 /* The desired access below is the only one I could find that works
5121 with NT4, W2KP and Samba */
5123 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5126 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5127 filename, cli_errstr(cli)));
5132 old = cli_query_secdesc(cli, fnum, ctx);
5135 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5140 cli_close(cli, fnum);
5143 case SMBC_XATTR_MODE_REMOVE_ALL:
5144 old->dacl->num_aces = 0;
5149 case SMBC_XATTR_MODE_REMOVE:
5150 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5153 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5154 if (sec_ace_equal(&sd->dacl->aces[i],
5155 &old->dacl->aces[j])) {
5157 for (k=j; k<old->dacl->num_aces-1;k++) {
5158 old->dacl->aces[k] =
5159 old->dacl->aces[k+1];
5161 old->dacl->num_aces--;
5162 if (old->dacl->num_aces == 0) {
5179 case SMBC_XATTR_MODE_ADD:
5180 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5183 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5184 if (sid_equal(&sd->dacl->aces[i].trustee,
5185 &old->dacl->aces[j].trustee)) {
5186 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5191 old->dacl->aces[j] = sd->dacl->aces[i];
5197 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5203 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5204 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5210 case SMBC_XATTR_MODE_SET:
5212 owner_sid = old->owner_sid;
5213 grp_sid = old->group_sid;
5217 case SMBC_XATTR_MODE_CHOWN:
5218 owner_sid = sd->owner_sid;
5221 case SMBC_XATTR_MODE_CHGRP:
5222 grp_sid = sd->group_sid;
5226 /* Denied ACE entries must come before allowed ones */
5227 sort_acl(old->dacl);
5229 /* Create new security descriptor and set it */
5230 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5231 owner_sid, grp_sid, NULL, dacl, &sd_size);
5233 fnum = cli_nt_create(cli, filename,
5234 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5237 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5238 filename, cli_errstr(cli)));
5243 if (!cli_set_secdesc(cli, fnum, sd)) {
5244 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5251 cli_close(cli, fnum);
5262 smbc_setxattr_ctx(SMBCCTX *context,
5283 const char * create_time_attr;
5284 const char * access_time_attr;
5285 const char * write_time_attr;
5286 const char * change_time_attr;
5289 if (!context || !context->internal ||
5290 !context->internal->_initialized) {
5292 errno = EINVAL; /* Best I can think of ... */
5304 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5305 fname, name, (int) size, (const char*)value));
5307 if (smbc_parse_path(context, fname,
5308 workgroup, sizeof(workgroup),
5309 server, sizeof(server),
5310 share, sizeof(share),
5313 password, sizeof(password),
5319 if (user[0] == (char)0) fstrcpy(user, context->user);
5321 srv = smbc_server(context, True,
5322 server, share, workgroup, user, password);
5324 return -1; /* errno set by smbc_server */
5327 if (! srv->no_nt_session) {
5328 ipc_srv = smbc_attr_server(context, server, share,
5329 workgroup, user, password,
5332 srv->no_nt_session = True;
5338 ctx = talloc_init("smbc_setxattr");
5345 * Are they asking to set the entire set of known attributes?
5347 if (StrCaseCmp(name, "system.*") == 0 ||
5348 StrCaseCmp(name, "system.*+") == 0) {
5351 talloc_asprintf(ctx, "%s:%s",
5352 name+7, (const char *) value);
5360 ret = cacl_set(ctx, srv->cli,
5361 ipc_srv->cli, &pol, path,
5364 ? SMBC_XATTR_MODE_SET
5365 : SMBC_XATTR_MODE_ADD),
5371 /* get a DOS Attribute Descriptor with current attributes */
5372 dad = dos_attr_query(context, ctx, path, srv);
5374 /* Overwrite old with new, using what was provided */
5375 dos_attr_parse(context, dad, srv, namevalue);
5377 /* Set the new DOS attributes */
5378 if (! smbc_setatr(context, srv, path,
5385 /* cause failure if NT failed too */
5390 /* we only fail if both NT and DOS sets failed */
5391 if (ret < 0 && ! dad) {
5392 ret = -1; /* in case dad was null */
5398 talloc_destroy(ctx);
5403 * Are they asking to set an access control element or to set
5404 * the entire access control list?
5406 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5407 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5408 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5409 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5410 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5414 talloc_asprintf(ctx, "%s:%s",
5415 name+19, (const char *) value);
5418 ret = -1; /* errno set by smbc_server() */
5420 else if (! namevalue) {
5424 ret = cacl_set(ctx, srv->cli,
5425 ipc_srv->cli, &pol, path,
5428 ? SMBC_XATTR_MODE_SET
5429 : SMBC_XATTR_MODE_ADD),
5432 talloc_destroy(ctx);
5437 * Are they asking to set the owner?
5439 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5440 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5444 talloc_asprintf(ctx, "%s:%s",
5445 name+19, (const char *) value);
5449 ret = -1; /* errno set by smbc_server() */
5451 else if (! namevalue) {
5455 ret = cacl_set(ctx, srv->cli,
5456 ipc_srv->cli, &pol, path,
5457 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5459 talloc_destroy(ctx);
5464 * Are they asking to set the group?
5466 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5467 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5471 talloc_asprintf(ctx, "%s:%s",
5472 name+19, (const char *) value);
5475 /* errno set by smbc_server() */
5478 else if (! namevalue) {
5482 ret = cacl_set(ctx, srv->cli,
5483 ipc_srv->cli, &pol, path,
5484 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5486 talloc_destroy(ctx);
5490 /* Determine whether to use old-style or new-style attribute names */
5491 if (context->internal->_full_time_names) {
5492 /* new-style names */
5493 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5494 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5495 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5496 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5498 /* old-style names */
5499 attr_strings.create_time_attr = NULL;
5500 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5501 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5502 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5506 * Are they asking to set a DOS attribute?
5508 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5509 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5510 (attr_strings.create_time_attr != NULL &&
5511 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5512 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5513 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5514 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5516 /* get a DOS Attribute Descriptor with current attributes */
5517 dad = dos_attr_query(context, ctx, path, srv);
5520 talloc_asprintf(ctx, "%s:%s",
5521 name+16, (const char *) value);
5526 /* Overwrite old with provided new params */
5527 dos_attr_parse(context, dad, srv, namevalue);
5529 /* Set the new DOS attributes */
5530 ret2 = smbc_setatr(context, srv, path,
5537 /* ret2 has True (success) / False (failure) */
5548 talloc_destroy(ctx);
5552 /* Unsupported attribute name */
5553 talloc_destroy(ctx);
5559 smbc_getxattr_ctx(SMBCCTX *context,
5577 const char * create_time_attr;
5578 const char * access_time_attr;
5579 const char * write_time_attr;
5580 const char * change_time_attr;
5584 if (!context || !context->internal ||
5585 !context->internal->_initialized) {
5587 errno = EINVAL; /* Best I can think of ... */
5599 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5601 if (smbc_parse_path(context, fname,
5602 workgroup, sizeof(workgroup),
5603 server, sizeof(server),
5604 share, sizeof(share),
5607 password, sizeof(password),
5613 if (user[0] == (char)0) fstrcpy(user, context->user);
5615 srv = smbc_server(context, True,
5616 server, share, workgroup, user, password);
5618 return -1; /* errno set by smbc_server */
5621 if (! srv->no_nt_session) {
5622 ipc_srv = smbc_attr_server(context, server, share,
5623 workgroup, user, password,
5626 srv->no_nt_session = True;
5632 ctx = talloc_init("smbc:getxattr");
5638 /* Determine whether to use old-style or new-style attribute names */
5639 if (context->internal->_full_time_names) {
5640 /* new-style names */
5641 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5642 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5643 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5644 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5646 /* old-style names */
5647 attr_strings.create_time_attr = NULL;
5648 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5649 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5650 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5653 /* Are they requesting a supported attribute? */
5654 if (StrCaseCmp(name, "system.*") == 0 ||
5655 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5656 StrCaseCmp(name, "system.*+") == 0 ||
5657 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5658 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5659 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5660 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5661 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5662 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5663 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5664 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5665 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5666 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5667 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5668 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5669 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5670 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5671 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5672 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5673 (attr_strings.create_time_attr != NULL &&
5674 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5675 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5676 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5677 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5678 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
5681 ret = cacl_get(context, ctx, srv,
5682 ipc_srv == NULL ? NULL : ipc_srv->cli,
5684 CONST_DISCARD(char *, name),
5685 CONST_DISCARD(char *, value), size);
5686 if (ret < 0 && errno == 0) {
5687 errno = smbc_errno(context, srv->cli);
5689 talloc_destroy(ctx);
5693 /* Unsupported attribute name */
5694 talloc_destroy(ctx);
5701 smbc_removexattr_ctx(SMBCCTX *context,
5717 if (!context || !context->internal ||
5718 !context->internal->_initialized) {
5720 errno = EINVAL; /* Best I can think of ... */
5732 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
5734 if (smbc_parse_path(context, fname,
5735 workgroup, sizeof(workgroup),
5736 server, sizeof(server),
5737 share, sizeof(share),
5740 password, sizeof(password),
5746 if (user[0] == (char)0) fstrcpy(user, context->user);
5748 srv = smbc_server(context, True,
5749 server, share, workgroup, user, password);
5751 return -1; /* errno set by smbc_server */
5754 if (! srv->no_nt_session) {
5755 ipc_srv = smbc_attr_server(context, server, share,
5756 workgroup, user, password,
5759 srv->no_nt_session = True;
5766 return -1; /* errno set by smbc_attr_server */
5769 ctx = talloc_init("smbc_removexattr");
5775 /* Are they asking to set the entire ACL? */
5776 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5777 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
5780 ret = cacl_set(ctx, srv->cli,
5781 ipc_srv->cli, &pol, path,
5782 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
5783 talloc_destroy(ctx);
5788 * Are they asking to remove one or more spceific security descriptor
5791 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5792 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5793 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5794 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5795 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5796 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5797 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5800 ret = cacl_set(ctx, srv->cli,
5801 ipc_srv->cli, &pol, path,
5802 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
5803 talloc_destroy(ctx);
5807 /* Unsupported attribute name */
5808 talloc_destroy(ctx);
5814 smbc_listxattr_ctx(SMBCCTX *context,
5820 * This isn't quite what listxattr() is supposed to do. This returns
5821 * the complete set of attribute names, always, rather than only those
5822 * attribute names which actually exist for a file. Hmmm...
5824 const char supported_old[] =
5827 "system.nt_sec_desc.revision\0"
5828 "system.nt_sec_desc.owner\0"
5829 "system.nt_sec_desc.owner+\0"
5830 "system.nt_sec_desc.group\0"
5831 "system.nt_sec_desc.group+\0"
5832 "system.nt_sec_desc.acl.*\0"
5833 "system.nt_sec_desc.acl\0"
5834 "system.nt_sec_desc.acl+\0"
5835 "system.nt_sec_desc.*\0"
5836 "system.nt_sec_desc.*+\0"
5837 "system.dos_attr.*\0"
5838 "system.dos_attr.mode\0"
5839 "system.dos_attr.c_time\0"
5840 "system.dos_attr.a_time\0"
5841 "system.dos_attr.m_time\0"
5843 const char supported_new[] =
5846 "system.nt_sec_desc.revision\0"
5847 "system.nt_sec_desc.owner\0"
5848 "system.nt_sec_desc.owner+\0"
5849 "system.nt_sec_desc.group\0"
5850 "system.nt_sec_desc.group+\0"
5851 "system.nt_sec_desc.acl.*\0"
5852 "system.nt_sec_desc.acl\0"
5853 "system.nt_sec_desc.acl+\0"
5854 "system.nt_sec_desc.*\0"
5855 "system.nt_sec_desc.*+\0"
5856 "system.dos_attr.*\0"
5857 "system.dos_attr.mode\0"
5858 "system.dos_attr.create_time\0"
5859 "system.dos_attr.access_time\0"
5860 "system.dos_attr.write_time\0"
5861 "system.dos_attr.change_time\0"
5863 const char * supported;
5865 if (context->internal->_full_time_names) {
5866 supported = supported_new;
5868 supported = supported_old;
5872 return sizeof(supported);
5875 if (sizeof(supported) > size) {
5880 /* this can't be strcpy() because there are embedded null characters */
5881 memcpy(list, supported, sizeof(supported));
5882 return sizeof(supported);
5887 * Open a print file to be written to by other calls
5891 smbc_open_print_job_ctx(SMBCCTX *context,
5900 if (!context || !context->internal ||
5901 !context->internal->_initialized) {
5915 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
5917 if (smbc_parse_path(context, fname,
5919 server, sizeof(server),
5920 share, sizeof(share),
5923 password, sizeof(password),
5929 /* What if the path is empty, or the file exists? */
5931 return context->open(context, fname, O_WRONLY, 666);
5936 * Routine to print a file on a remote server ...
5938 * We open the file, which we assume to be on a remote server, and then
5939 * copy it to a print file on the share specified by printq.
5943 smbc_print_file_ctx(SMBCCTX *c_file,
5955 if (!c_file || !c_file->internal->_initialized || !c_print ||
5956 !c_print->internal->_initialized) {
5963 if (!fname && !printq) {
5970 /* Try to open the file for reading ... */
5972 if ((long)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
5974 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
5975 return -1; /* smbc_open sets errno */
5979 /* Now, try to open the printer file for writing */
5981 if ((long)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
5983 saverr = errno; /* Save errno */
5984 c_file->close_fn(c_file, fid1);
5990 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
5994 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
5997 c_file->close_fn(c_file, fid1);
5998 c_print->close_fn(c_print, fid2);
6007 c_file->close_fn(c_file, fid1); /* We have to close these anyway */
6008 c_print->close_fn(c_print, fid2);
6022 * Routine to list print jobs on a printer share ...
6026 smbc_list_print_jobs_ctx(SMBCCTX *context,
6028 smbc_list_print_job_fn fn)
6038 if (!context || !context->internal ||
6039 !context->internal->_initialized) {
6053 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6055 if (smbc_parse_path(context, fname,
6056 workgroup, sizeof(workgroup),
6057 server, sizeof(server),
6058 share, sizeof(share),
6061 password, sizeof(password),
6067 if (user[0] == (char)0) fstrcpy(user, context->user);
6069 srv = smbc_server(context, True,
6070 server, share, workgroup, user, password);
6074 return -1; /* errno set by smbc_server */
6078 if (cli_print_queue(srv->cli,
6079 (void (*)(struct print_job_info *))fn) < 0) {
6081 errno = smbc_errno(context, srv->cli);
6091 * Delete a print job from a remote printer share
6095 smbc_unlink_print_job_ctx(SMBCCTX *context,
6108 if (!context || !context->internal ||
6109 !context->internal->_initialized) {
6123 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6125 if (smbc_parse_path(context, fname,
6126 workgroup, sizeof(workgroup),
6127 server, sizeof(server),
6128 share, sizeof(share),
6131 password, sizeof(password),
6137 if (user[0] == (char)0) fstrcpy(user, context->user);
6139 srv = smbc_server(context, True,
6140 server, share, workgroup, user, password);
6144 return -1; /* errno set by smbc_server */
6148 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6151 errno = smbc_errno(context, srv->cli);
6152 else if (err == ERRnosuchprintjob)
6163 * Get a new empty handle to fill in with your own info
6166 smbc_new_context(void)
6170 context = SMB_MALLOC_P(SMBCCTX);
6176 ZERO_STRUCTP(context);
6178 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6179 if (!context->internal) {
6185 ZERO_STRUCTP(context->internal);
6188 /* ADD REASONABLE DEFAULTS */
6190 context->timeout = 20000; /* 20 seconds */
6192 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6193 context->options.urlencode_readdir_entries = False;/* backward compat */
6194 context->options.one_share_per_server = False;/* backward compat */
6195 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6196 /* backward compat */
6198 context->open = smbc_open_ctx;
6199 context->creat = smbc_creat_ctx;
6200 context->read = smbc_read_ctx;
6201 context->write = smbc_write_ctx;
6202 context->close_fn = smbc_close_ctx;
6203 context->unlink = smbc_unlink_ctx;
6204 context->rename = smbc_rename_ctx;
6205 context->lseek = smbc_lseek_ctx;
6206 context->stat = smbc_stat_ctx;
6207 context->fstat = smbc_fstat_ctx;
6208 context->opendir = smbc_opendir_ctx;
6209 context->closedir = smbc_closedir_ctx;
6210 context->readdir = smbc_readdir_ctx;
6211 context->getdents = smbc_getdents_ctx;
6212 context->mkdir = smbc_mkdir_ctx;
6213 context->rmdir = smbc_rmdir_ctx;
6214 context->telldir = smbc_telldir_ctx;
6215 context->lseekdir = smbc_lseekdir_ctx;
6216 context->fstatdir = smbc_fstatdir_ctx;
6217 context->chmod = smbc_chmod_ctx;
6218 context->utimes = smbc_utimes_ctx;
6219 context->setxattr = smbc_setxattr_ctx;
6220 context->getxattr = smbc_getxattr_ctx;
6221 context->removexattr = smbc_removexattr_ctx;
6222 context->listxattr = smbc_listxattr_ctx;
6223 context->open_print_job = smbc_open_print_job_ctx;
6224 context->print_file = smbc_print_file_ctx;
6225 context->list_print_jobs = smbc_list_print_jobs_ctx;
6226 context->unlink_print_job = smbc_unlink_print_job_ctx;
6228 context->callbacks.check_server_fn = smbc_check_server;
6229 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6231 smbc_default_cache_functions(context);
6239 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6240 * and thus you'll be leaking memory if not handled properly.
6244 smbc_free_context(SMBCCTX *context,
6254 DEBUG(1,("Performing aggressive shutdown.\n"));
6256 f = context->internal->_files;
6258 context->close_fn(context, f);
6261 context->internal->_files = NULL;
6263 /* First try to remove the servers the nice way. */
6264 if (context->callbacks.purge_cached_fn(context)) {
6267 DEBUG(1, ("Could not purge all servers, "
6268 "Nice way shutdown failed.\n"));
6269 s = context->internal->_servers;
6271 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6273 cli_shutdown(s->cli);
6274 context->callbacks.remove_cached_srv_fn(context,
6277 DLIST_REMOVE(context->internal->_servers, s);
6281 context->internal->_servers = NULL;
6285 /* This is the polite way */
6286 if (context->callbacks.purge_cached_fn(context)) {
6287 DEBUG(1, ("Could not purge all servers, "
6288 "free_context failed.\n"));
6292 if (context->internal->_servers) {
6293 DEBUG(1, ("Active servers in context, "
6294 "free_context failed.\n"));
6298 if (context->internal->_files) {
6299 DEBUG(1, ("Active files in context, "
6300 "free_context failed.\n"));
6306 /* Things we have to clean up */
6307 SAFE_FREE(context->workgroup);
6308 SAFE_FREE(context->netbios_name);
6309 SAFE_FREE(context->user);
6311 DEBUG(3, ("Context %p succesfully freed\n", context));
6312 SAFE_FREE(context->internal);
6319 * Each time the context structure is changed, we have binary backward
6320 * compatibility issues. Instead of modifying the public portions of the
6321 * context structure to add new options, instead, we put them in the internal
6322 * portion of the context structure and provide a set function for these new
6326 smbc_option_set(SMBCCTX *context,
6328 ... /* option_value */)
6334 smbc_get_auth_data_with_context_fn auth_fn;
6338 va_start(ap, option_name);
6340 if (strcmp(option_name, "debug_to_stderr") == 0) {
6342 * Log to standard error instead of standard output.
6344 option_value.b = (BOOL) va_arg(ap, int);
6345 context->internal->_debug_stderr = option_value.b;
6347 } else if (strcmp(option_name, "full_time_names") == 0) {
6349 * Use new-style time attribute names, e.g. WRITE_TIME rather
6350 * than the old-style names such as M_TIME. This allows also
6351 * setting/getting CREATE_TIME which was previously
6352 * unimplemented. (Note that the old C_TIME was supposed to
6353 * be CHANGE_TIME but was confused and sometimes referred to
6356 option_value.b = (BOOL) va_arg(ap, int);
6357 context->internal->_full_time_names = option_value.b;
6359 } else if (strcmp(option_name, "open_share_mode") == 0) {
6361 * The share mode to use for files opened with
6362 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6364 option_value.i = va_arg(ap, int);
6365 context->internal->_share_mode =
6366 (smbc_share_mode) option_value.i;
6368 } else if (strcmp(option_name, "auth_function") == 0) {
6370 * Use the new-style authentication function which includes
6373 option_value.auth_fn =
6374 va_arg(ap, smbc_get_auth_data_with_context_fn);
6375 context->internal->_auth_fn_with_context =
6376 option_value.auth_fn;
6377 } else if (strcmp(option_name, "user_data") == 0) {
6379 * Save a user data handle which may be retrieved by the user
6380 * with smbc_option_get()
6382 option_value.v = va_arg(ap, void *);
6383 context->internal->_user_data = option_value.v;
6391 * Retrieve the current value of an option
6394 smbc_option_get(SMBCCTX *context,
6397 if (strcmp(option_name, "debug_stderr") == 0) {
6399 * Log to standard error instead of standard output.
6401 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6402 return (void *) (intptr_t) context->internal->_debug_stderr;
6404 return (void *) context->internal->_debug_stderr;
6406 } else if (strcmp(option_name, "full_time_names") == 0) {
6408 * Use new-style time attribute names, e.g. WRITE_TIME rather
6409 * than the old-style names such as M_TIME. This allows also
6410 * setting/getting CREATE_TIME which was previously
6411 * unimplemented. (Note that the old C_TIME was supposed to
6412 * be CHANGE_TIME but was confused and sometimes referred to
6415 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6416 return (void *) (intptr_t) context->internal->_full_time_names;
6418 return (void *) context->internal->_full_time_names;
6421 } else if (strcmp(option_name, "auth_function") == 0) {
6423 * Use the new-style authentication function which includes
6426 return (void *) context->internal->_auth_fn_with_context;
6427 } else if (strcmp(option_name, "user_data") == 0) {
6429 * Save a user data handle which may be retrieved by the user
6430 * with smbc_option_get()
6432 return context->internal->_user_data;
6440 * Initialise the library etc
6442 * We accept a struct containing handle information.
6443 * valid values for info->debug from 0 to 100,
6444 * and insist that info->fn must be non-null.
6447 smbc_init_context(SMBCCTX *context)
6454 if (!context || !context->internal) {
6459 /* Do not initialise the same client twice */
6460 if (context->internal->_initialized) {
6464 if ((!context->callbacks.auth_fn &&
6465 !context->internal->_auth_fn_with_context) ||
6466 context->debug < 0 ||
6467 context->debug > 100) {
6474 if (!smbc_initialized) {
6476 * Do some library-wide intializations the first time we get
6479 BOOL conf_loaded = False;
6481 /* Set this to what the user wants */
6482 DEBUGLEVEL = context->debug;
6486 setup_logging("libsmbclient", True);
6487 if (context->internal->_debug_stderr) {
6489 x_setbuf(x_stderr, NULL);
6492 /* Here we would open the smb.conf file if needed ... */
6494 in_client = True; /* FIXME, make a param */
6496 home = getenv("HOME");
6498 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
6499 if (lp_load(conf, True, False, False, True)) {
6502 DEBUG(5, ("Could not load config file: %s\n",
6509 * Well, if that failed, try the dyn_CONFIGFILE
6510 * Which points to the standard locn, and if that
6511 * fails, silently ignore it and use the internal
6515 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6516 DEBUG(5, ("Could not load config file: %s\n",
6520 * We loaded the global config file. Now lets
6521 * load user-specific modifications to the
6524 slprintf(conf, sizeof(conf),
6525 "%s/.smb/smb.conf.append", home);
6526 if (!lp_load(conf, True, False, False, False)) {
6528 ("Could not append config file: "
6535 load_interfaces(); /* Load the list of interfaces ... */
6537 reopen_logs(); /* Get logging working ... */
6540 * Block SIGPIPE (from lib/util_sock.c: write())
6541 * It is not needed and should not stop execution
6543 BlockSignals(True, SIGPIPE);
6545 /* Done with one-time initialisation */
6546 smbc_initialized = 1;
6550 if (!context->user) {
6552 * FIXME: Is this the best way to get the user info?
6554 user = getenv("USER");
6555 /* walk around as "guest" if no username can be found */
6556 if (!user) context->user = SMB_STRDUP("guest");
6557 else context->user = SMB_STRDUP(user);
6560 if (!context->netbios_name) {
6562 * We try to get our netbios name from the config. If that
6563 * fails we fall back on constructing our netbios name from
6566 if (global_myname()) {
6567 context->netbios_name = SMB_STRDUP(global_myname());
6571 * Hmmm, I want to get hostname as well, but I am too
6572 * lazy for the moment
6575 context->netbios_name = (char *)SMB_MALLOC(17);
6576 if (!context->netbios_name) {
6580 slprintf(context->netbios_name, 16,
6581 "smbc%s%d", context->user, pid);
6585 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6587 if (!context->workgroup) {
6588 if (lp_workgroup()) {
6589 context->workgroup = SMB_STRDUP(lp_workgroup());
6592 /* TODO: Think about a decent default workgroup */
6593 context->workgroup = SMB_STRDUP("samba");
6597 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6599 /* shortest timeout is 1 second */
6600 if (context->timeout > 0 && context->timeout < 1000)
6601 context->timeout = 1000;
6604 * FIXME: Should we check the function pointers here?
6607 context->internal->_initialized = True;
6613 /* Return the verion of samba, and thus libsmbclient */
6617 return samba_version_string();