2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2004
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "include/libsmb_internal.h"
29 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir);
30 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
31 struct smbc_dirent *dirent);
34 * DOS Attribute values (used internally)
36 typedef struct DOS_ATTR_DESC {
48 * Internal flags for extended attributes
51 /* internal mode values */
52 #define SMBC_XATTR_MODE_ADD 1
53 #define SMBC_XATTR_MODE_REMOVE 2
54 #define SMBC_XATTR_MODE_REMOVE_ALL 3
55 #define SMBC_XATTR_MODE_SET 4
56 #define SMBC_XATTR_MODE_CHOWN 5
57 #define SMBC_XATTR_MODE_CHGRP 6
59 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
61 /*We should test for this in configure ... */
63 #define ENOTSUP EOPNOTSUPP
67 * Functions exported by libsmb_cache.c that we need here
69 int smbc_default_cache_functions(SMBCCTX *context);
72 * check if an element is part of the list.
73 * FIXME: Does not belong here !
74 * Can anyone put this in a macro in dlinklist.h ?
77 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
78 if (!p || !list) return False;
80 if (p == list) return True;
87 * Find an lsa pipe handle associated with a cli struct.
89 static struct rpc_pipe_client *
90 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
92 struct rpc_pipe_client *pipe_hnd;
94 for (pipe_hnd = ipc_cli->pipe_list;
96 pipe_hnd = pipe_hnd->next) {
98 if (pipe_hnd->pipe_idx == PI_LSARPC) {
107 smbc_close_ctx(SMBCCTX *context,
110 smbc_lseek_ctx(SMBCCTX *context,
115 extern bool in_client;
118 * Is the logging working / configfile read ?
120 static int smbc_initialized = 0;
123 hex2int( unsigned int _char )
125 if ( _char >= 'A' && _char <='F')
126 return _char - 'A' + 10;
127 if ( _char >= 'a' && _char <='f')
128 return _char - 'a' + 10;
129 if ( _char >= '0' && _char <='9')
136 * and smbc_urldecode_talloc() (internal fn.)
138 * Convert strings of %xx to their single character equivalent. Each 'x' must
139 * be a valid hexadecimal digit, or that % sequence is left undecoded.
141 * dest may, but need not be, the same pointer as src.
143 * Returns the number of % sequences which could not be converted due to lack
144 * of two following hexadecimal digits.
147 smbc_urldecode_talloc(TALLOC_CTX *ctx, char **pp_dest, const char *src)
149 int old_length = strlen(src);
155 if (old_length == 0) {
160 for (i = 0; i < old_length; ) {
161 unsigned char character = src[i++];
163 if (character == '%') {
164 int a = i+1 < old_length ? hex2int(src[i]) : -1;
165 int b = i+1 < old_length ? hex2int(src[i+1]) : -1;
167 /* Replace valid sequence */
168 if (a != -1 && b != -1) {
169 /* Replace valid %xx sequence with %dd */
170 character = (a * 16) + b;
171 if (character == '\0') {
172 break; /* Stop at %00 */
182 dest = TALLOC_ARRAY(ctx, char, newlen);
188 for (p = dest, i = 0; i < old_length; ) {
189 unsigned char character = src[i++];
191 if (character == '%') {
192 int a = i+1 < old_length ? hex2int(src[i]) : -1;
193 int b = i+1 < old_length ? hex2int(src[i+1]) : -1;
195 /* Replace valid sequence */
196 if (a != -1 && b != -1) {
197 /* Replace valid %xx sequence with %dd */
198 character = (a * 16) + b;
199 if (character == '\0') {
200 break; /* Stop at %00 */
216 smbc_urldecode(char *dest, char *src, size_t max_dest_len)
218 TALLOC_CTX *frame = talloc_stackframe();
220 int ret = smbc_urldecode_talloc(frame, &pdest, src);
223 strlcpy(dest, pdest, max_dest_len);
232 * Convert any characters not specifically allowed in a URL into their %xx
235 * Returns the remaining buffer length.
238 smbc_urlencode(char *dest, char *src, int max_dest_len)
240 char hex[] = "0123456789ABCDEF";
242 for (; *src != '\0' && max_dest_len >= 3; src++) {
254 *dest++ = hex[(*src >> 4) & 0x0f];
255 *dest++ = hex[*src & 0x0f];
270 * Function to parse a path and turn it into components
272 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
273 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
274 * general format ("smb:" only; we do not look for "cifs:").
278 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
282 * smb:// Show all workgroups.
284 * The method of locating the list of workgroups varies
285 * depending upon the setting of the context variable
286 * context->options.browse_max_lmb_count. This value
287 * determine the maximum number of local master browsers to
288 * query for the list of workgroups. In order to ensure that
289 * a complete list of workgroups is obtained, all master
290 * browsers must be queried, but if there are many
291 * workgroups, the time spent querying can begin to add up.
292 * For small networks (not many workgroups), it is suggested
293 * that this variable be set to 0, indicating query all local
294 * master browsers. When the network has many workgroups, a
295 * reasonable setting for this variable might be around 3.
297 * smb://name/ if name<1D> or name<1B> exists, list servers in
298 * workgroup, else, if name<20> exists, list all shares
301 * If "options" are provided, this function returns the entire option list as a
302 * string, for later parsing by the caller. Note that currently, no options
306 static const char *smbc_prefix = "smb:";
309 smbc_parse_path(TALLOC_CTX *ctx,
325 /* Ensure these returns are at least valid pointers. */
326 *pp_server = talloc_strdup(ctx, "");
327 *pp_share = talloc_strdup(ctx, "");
328 *pp_path = talloc_strdup(ctx, "");
329 *pp_user = talloc_strdup(ctx, "");
330 *pp_password = talloc_strdup(ctx, "");
332 if (!*pp_server || !*pp_share || !*pp_path ||
333 !*pp_user || !*pp_password) {
338 * Assume we wont find an authentication domain to parse, so default
339 * to the workgroup in the provided context.
341 if (pp_workgroup != NULL) {
342 *pp_workgroup = talloc_strdup(ctx, context->workgroup);
346 *pp_options = talloc_strdup(ctx, "");
348 s = talloc_strdup(ctx, fname);
350 /* see if it has the right prefix */
351 len = strlen(smbc_prefix);
352 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
353 return -1; /* What about no smb: ? */
358 /* Watch the test below, we are testing to see if we should exit */
360 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
361 DEBUG(1, ("Invalid path (does not begin with smb://"));
365 p += 2; /* Skip the double slash */
367 /* See if any options were specified */
368 if ((q = strrchr(p, '?')) != NULL ) {
369 /* There are options. Null terminate here and point to them */
372 DEBUG(4, ("Found options '%s'", q));
374 /* Copy the options */
375 if (*pp_options != NULL) {
376 TALLOC_FREE(*pp_options);
377 *pp_options = talloc_strdup(ctx, q);
386 int wl = strlen(context->workgroup);
392 *pp_server = talloc_strdup(ctx, context->workgroup);
396 *pp_server[wl] = '\0';
401 * ok, its for us. Now parse out the server, share etc.
403 * However, we want to parse out [[domain;]user[:password]@] if it
407 /* check that '@' occurs before '/', if '/' exists at all */
408 q = strchr_m(p, '@');
409 r = strchr_m(p, '/');
410 if (q && (!r || q < r)) {
411 char *userinfo = NULL;
414 next_token_no_ltrim_talloc(ctx, &p, &userinfo, "@");
420 if (strchr_m(u, ';')) {
422 next_token_no_ltrim_talloc(ctx, &u, &workgroup, ";");
427 *pp_workgroup = workgroup;
431 if (strchr_m(u, ':')) {
432 next_token_no_ltrim_talloc(ctx, &u, pp_user, ":");
436 *pp_password = talloc_strdup(ctx, u);
441 *pp_user = talloc_strdup(ctx, u);
448 if (!next_token_talloc(ctx, &p, pp_server, "/")) {
453 goto decoding; /* That's it ... */
456 if (!next_token_talloc(ctx, &p, pp_share, "/")) {
461 * Prepend a leading slash if there's a file path, as required by
465 *pp_path = talloc_asprintf(ctx,
469 *pp_path = talloc_strdup(ctx, "");
474 string_replace(*pp_path, '/', '\\');
478 (void) smbc_urldecode_talloc(ctx, pp_path, *pp_path);
479 (void) smbc_urldecode_talloc(ctx, pp_server, *pp_server);
480 (void) smbc_urldecode_talloc(ctx, pp_share, *pp_share);
481 (void) smbc_urldecode_talloc(ctx, pp_user, *pp_user);
482 (void) smbc_urldecode_talloc(ctx, pp_password, *pp_password);
488 * Verify that the options specified in a URL are valid
491 smbc_check_options(char *server,
496 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
497 "path='%s' options='%s'\n",
498 server, share, path, options));
500 /* No options at all is always ok */
501 if (! *options) return 0;
503 /* Currently, we don't support any options. */
508 * Convert an SMB error into a UNIX error ...
511 smbc_errno(SMBCCTX *context,
514 int ret = cli_errno(c);
516 if (cli_is_dos_error(c)) {
520 cli_dos_error(c, &eclass, &ecode);
522 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
523 (int)eclass, (int)ecode, (int)ecode, ret));
527 status = cli_nt_error(c);
529 DEBUG(3,("smbc errno %s -> %d\n",
530 nt_errstr(status), ret));
537 * Check a server for being alive and well.
538 * returns 0 if the server is in shape. Returns 1 on error
540 * Also useable outside libsmbclient to enable external cache
541 * to do some checks too.
544 smbc_check_server(SMBCCTX * context,
548 struct sockaddr addr;
551 return (getpeername(server->cli->fd, &addr, &size) == -1);
555 * Remove a server from the cached server list it's unused.
556 * On success, 0 is returned. 1 is returned if the server could not be removed.
558 * Also useable outside libsmbclient
561 smbc_remove_unused_server(SMBCCTX * context,
566 /* are we being fooled ? */
567 if (!context || !context->internal ||
568 !context->internal->_initialized || !srv) return 1;
571 /* Check all open files/directories for a relation with this server */
572 for (file = context->internal->_files; file; file=file->next) {
573 if (file->srv == srv) {
575 DEBUG(3, ("smbc_remove_usused_server: "
576 "%p still used by %p.\n",
582 DLIST_REMOVE(context->internal->_servers, srv);
584 cli_shutdown(srv->cli);
587 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
589 (context->callbacks.remove_cached_srv_fn)(context, srv);
595 /****************************************************************
596 * Call the auth_fn with fixed size (fstring) buffers.
597 ***************************************************************/
599 static void call_auth_fn(TALLOC_CTX *ctx,
611 strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
612 strlcpy(username, *pp_username, sizeof(username));
613 strlcpy(password, *pp_password, sizeof(password));
615 if (context->internal->_auth_fn_with_context != NULL) {
616 (context->internal->_auth_fn_with_context)(
619 workgroup, sizeof(workgroup),
620 username, sizeof(username),
621 password, sizeof(password));
623 (context->callbacks.auth_fn)(
625 workgroup, sizeof(workgroup),
626 username, sizeof(username),
627 password, sizeof(password));
630 TALLOC_FREE(*pp_workgroup);
631 TALLOC_FREE(*pp_username);
632 TALLOC_FREE(*pp_password);
634 *pp_workgroup = talloc_strdup(ctx, workgroup);
635 *pp_username = talloc_strdup(ctx, username);
636 *pp_password = talloc_strdup(ctx, password);
640 find_server(TALLOC_CTX *ctx,
653 srv = (context->callbacks.get_cached_srv_fn)(context, server, share,
654 *pp_workgroup, *pp_username);
656 if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
657 !*pp_password || !(*pp_password)[0])) {
658 call_auth_fn(ctx, context, server, share,
659 pp_workgroup, pp_username, pp_password);
661 if (!pp_workgroup || !pp_username || !pp_password) {
666 * However, smbc_auth_fn may have picked up info relating to
667 * an existing connection, so try for an existing connection
671 goto check_server_cache;
676 if ((context->callbacks.check_server_fn)(context, srv)) {
678 * This server is no good anymore
679 * Try to remove it and check for more possible
680 * servers in the cache
682 if ((context->callbacks.remove_unused_server_fn)(context,
685 * We could not remove the server completely,
686 * remove it from the cache so we will not get
687 * it again. It will be removed when the last
688 * file/dir is closed.
690 (context->callbacks.remove_cached_srv_fn)(context,
695 * Maybe there are more cached connections to this
698 goto check_server_cache;
708 * Connect to a server, possibly on an existing connection
710 * Here, what we want to do is: If the server and username
711 * match an existing connection, reuse that, otherwise, establish a
714 * If we have to create a new connection, call the auth_fn to get the
715 * info we need, unless the username and password were passed in.
719 smbc_server(TALLOC_CTX *ctx,
721 bool connect_if_not_found,
730 struct nmb_name called, calling;
731 const char *server_n = server;
732 struct sockaddr_storage ss;
733 int tried_reverse = 0;
736 const char *username_used;
742 if (server[0] == 0) {
747 /* Look for a cached connection */
748 srv = find_server(ctx, context, server, share,
749 pp_workgroup, pp_username, pp_password);
752 * If we found a connection and we're only allowed one share per
755 if (srv && *share != '\0' && context->options.one_share_per_server) {
758 * ... then if there's no current connection to the share,
759 * connect to it. find_server(), or rather the function
760 * pointed to by context->callbacks.get_cached_srv_fn which
761 * was called by find_server(), will have issued a tree
762 * disconnect if the requested share is not the same as the
763 * one that was already connected.
765 if (srv->cli->cnum == (uint16) -1) {
766 /* Ensure we have accurate auth info */
767 call_auth_fn(ctx, context, server, share,
768 pp_workgroup, pp_username, pp_password);
770 if (!*pp_workgroup || !*pp_username || !*pp_password) {
772 cli_shutdown(srv->cli);
774 (context->callbacks.remove_cached_srv_fn)(context,
780 * We don't need to renegotiate encryption
781 * here as the encryption context is not per
785 if (!cli_send_tconX(srv->cli, share, "?????",
787 strlen(*pp_password)+1)) {
789 errno = smbc_errno(context, srv->cli);
790 cli_shutdown(srv->cli);
792 (context->callbacks.remove_cached_srv_fn)(context,
798 * Regenerate the dev value since it's based on both
802 srv->dev = (dev_t)(str_checksum(server) ^
803 str_checksum(share));
808 /* If we have a connection... */
811 /* ... then we're done here. Give 'em what they came for. */
815 /* If we're not asked to connect when a connection doesn't exist... */
816 if (! connect_if_not_found) {
817 /* ... then we're done here. */
821 if (!*pp_workgroup || !*pp_username || !*pp_password) {
826 make_nmb_name(&calling, context->netbios_name, 0x0);
827 make_nmb_name(&called , server, 0x20);
829 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
831 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
837 /* have to open a new connection */
838 if ((c = cli_initialise()) == NULL) {
843 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
844 c->use_kerberos = True;
846 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
847 c->fallback_after_kerberos = True;
850 c->timeout = context->timeout;
853 * Force use of port 139 for first try if share is $IPC, empty, or
854 * null, so browse lists can work
856 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
857 port_try_first = 139;
860 port_try_first = 445;
864 c->port = port_try_first;
866 status = cli_connect(c, server_n, &ss);
867 if (!NT_STATUS_IS_OK(status)) {
869 /* First connection attempt failed. Try alternate port. */
870 c->port = port_try_next;
872 status = cli_connect(c, server_n, &ss);
873 if (!NT_STATUS_IS_OK(status)) {
880 if (!cli_session_request(c, &calling, &called)) {
882 if (strcmp(called.name, "*SMBSERVER")) {
883 make_nmb_name(&called , "*SMBSERVER", 0x20);
885 } else { /* Try one more time, but ensure we don't loop */
887 /* Only try this if server is an IP address ... */
889 if (is_ipaddress(server) && !tried_reverse) {
891 struct sockaddr_storage rem_ss;
893 if (!interpret_string_addr(&rem_ss, server,
895 DEBUG(4, ("Could not convert IP address "
896 "%s to struct sockaddr_storage\n",
902 tried_reverse++; /* Yuck */
904 if (name_status_find("*", 0, 0, &rem_ss, remote_name)) {
905 make_nmb_name(&called, remote_name, 0x20);
914 DEBUG(4,(" session request ok\n"));
916 if (!cli_negprot(c)) {
922 username_used = *pp_username;
924 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
925 *pp_password, strlen(*pp_password),
926 *pp_password, strlen(*pp_password),
929 /* Failed. Try an anonymous login, if allowed by flags. */
932 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
933 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
944 DEBUG(4,(" session setup ok\n"));
946 if (!cli_send_tconX(c, share, "?????",
947 *pp_password, strlen(*pp_password)+1)) {
948 errno = smbc_errno(context, c);
953 DEBUG(4,(" tconx ok\n"));
955 if (context->internal->_smb_encryption_level) {
956 /* Attempt UNIX smb encryption. */
957 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
963 * context->internal->_smb_encryption_level == 1
964 * means don't fail if encryption can't be negotiated,
965 * == 2 means fail if encryption can't be negotiated.
968 DEBUG(4,(" SMB encrypt failed\n"));
970 if (context->internal->_smb_encryption_level == 2) {
976 DEBUG(4,(" SMB encrypt ok\n"));
980 * Ok, we have got a nice connection
981 * Let's allocate a server structure.
984 srv = SMB_MALLOC_P(SMBCSRV);
992 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
993 srv->no_pathinfo = False;
994 srv->no_pathinfo2 = False;
995 srv->no_nt_session = False;
997 /* now add it to the cache (internal or external) */
998 /* Let the cache function set errno if it wants to */
1000 if ((context->callbacks.add_cached_srv_fn)(context, srv,
1004 int saved_errno = errno;
1005 DEBUG(3, (" Failed to add server to cache\n"));
1006 errno = saved_errno;
1013 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
1014 server, share, srv));
1016 DLIST_ADD(context->internal->_servers, srv);
1030 * Connect to a server for getting/setting attributes, possibly on an existing
1031 * connection. This works similarly to smbc_server().
1034 smbc_attr_server(TALLOC_CTX *ctx,
1038 char **pp_workgroup,
1043 struct sockaddr_storage ss;
1044 struct cli_state *ipc_cli;
1045 struct rpc_pipe_client *pipe_hnd;
1047 SMBCSRV *ipc_srv=NULL;
1050 * See if we've already created this special connection. Reference
1051 * our "special" share name '*IPC$', which is an impossible real share
1052 * name due to the leading asterisk.
1054 ipc_srv = find_server(ctx, context, server, "*IPC$",
1055 pp_workgroup, pp_username, pp_password);
1058 /* We didn't find a cached connection. Get the password */
1059 if (!*pp_password || (*pp_password)[0] == '\0') {
1060 /* ... then retrieve it now. */
1061 call_auth_fn(ctx, context, server, share,
1062 pp_workgroup, pp_username, pp_password);
1063 if (!*pp_workgroup || !*pp_username || !*pp_password) {
1070 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
1071 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1075 nt_status = cli_full_connection(&ipc_cli,
1076 global_myname(), server,
1077 &ss, 0, "IPC$", "?????",
1083 if (! NT_STATUS_IS_OK(nt_status)) {
1084 DEBUG(1,("cli_full_connection failed! (%s)\n",
1085 nt_errstr(nt_status)));
1090 if (context->internal->_smb_encryption_level) {
1091 /* Attempt UNIX smb encryption. */
1092 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
1098 * context->internal->_smb_encryption_level == 1
1099 * means don't fail if encryption can't be negotiated,
1100 * == 2 means fail if encryption can't be negotiated.
1103 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
1105 if (context->internal->_smb_encryption_level == 2) {
1106 cli_shutdown(ipc_cli);
1111 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
1114 ipc_srv = SMB_MALLOC_P(SMBCSRV);
1117 cli_shutdown(ipc_cli);
1121 ZERO_STRUCTP(ipc_srv);
1122 ipc_srv->cli = ipc_cli;
1124 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1128 DEBUG(1, ("cli_nt_session_open fail!\n"));
1130 cli_shutdown(ipc_srv->cli);
1136 * Some systems don't support
1137 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1138 * so we might as well do it too.
1141 nt_status = rpccli_lsa_open_policy(
1145 GENERIC_EXECUTE_ACCESS,
1148 if (!NT_STATUS_IS_OK(nt_status)) {
1149 errno = smbc_errno(context, ipc_srv->cli);
1150 cli_shutdown(ipc_srv->cli);
1154 /* now add it to the cache (internal or external) */
1156 errno = 0; /* let cache function set errno if it likes */
1157 if ((context->callbacks.add_cached_srv_fn)(context, ipc_srv,
1162 DEBUG(3, (" Failed to add server to cache\n"));
1166 cli_shutdown(ipc_srv->cli);
1171 DLIST_ADD(context->internal->_servers, ipc_srv);
1178 * Routine to open() a file ...
1182 smbc_open_ctx(SMBCCTX *context,
1187 char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *workgroup = NULL;
1189 char *targetpath = NULL;
1190 struct cli_state *targetcli = NULL;
1191 SMBCSRV *srv = NULL;
1192 SMBCFILE *file = NULL;
1194 TALLOC_CTX *frame = talloc_stackframe();
1196 if (!context || !context->internal ||
1197 !context->internal->_initialized) {
1199 errno = EINVAL; /* Best I can think of ... */
1213 if (smbc_parse_path(frame,
1228 if (!user || user[0] == (char)0) {
1229 user = talloc_strdup(frame, context->user);
1237 srv = smbc_server(frame, context, True,
1238 server, share, &workgroup, &user, &password);
1241 if (errno == EPERM) errno = EACCES;
1243 return NULL; /* smbc_server sets errno */
1246 /* Hmmm, the test for a directory is suspect here ... FIXME */
1248 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1251 file = SMB_MALLOC_P(SMBCFILE);
1261 /*d_printf(">>>open: resolving %s\n", path);*/
1262 if (!cli_resolve_path(frame, "", srv->cli, path, &targetcli, &targetpath)) {
1263 d_printf("Could not resolve %s\n", path);
1268 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1270 if ((fd = cli_open(targetcli, targetpath, flags,
1271 context->internal->_share_mode)) < 0) {
1273 /* Handle the error ... */
1276 errno = smbc_errno(context, targetcli);
1282 /* Fill in file struct */
1285 file->fname = SMB_STRDUP(fname);
1290 DLIST_ADD(context->internal->_files, file);
1293 * If the file was opened in O_APPEND mode, all write
1294 * operations should be appended to the file. To do that,
1295 * though, using this protocol, would require a getattrE()
1296 * call for each and every write, to determine where the end
1297 * of the file is. (There does not appear to be an append flag
1298 * in the protocol.) Rather than add all of that overhead of
1299 * retrieving the current end-of-file offset prior to each
1300 * write operation, we'll assume that most append operations
1301 * will continuously write, so we'll just set the offset to
1302 * the end of the file now and hope that's adequate.
1304 * Note to self: If this proves inadequate, and O_APPEND
1305 * should, in some cases, be forced for each write, add a
1306 * field in the context options structure, for
1307 * "strict_append_mode" which would select between the current
1308 * behavior (if FALSE) or issuing a getattrE() prior to each
1309 * write and forcing the write to the end of the file (if
1310 * TRUE). Adding that capability will likely require adding
1311 * an "append" flag into the _SMBCFILE structure to track
1312 * whether a file was opened in O_APPEND mode. -- djl
1314 if (flags & O_APPEND) {
1315 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1316 (void) smbc_close_ctx(context, file);
1328 /* Check if opendir needed ... */
1333 eno = smbc_errno(context, srv->cli);
1334 file = (context->opendir)(context, fname);
1335 if (!file) errno = eno;
1341 errno = EINVAL; /* FIXME, correct errno ? */
1348 * Routine to create a file
1351 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1354 smbc_creat_ctx(SMBCCTX *context,
1359 if (!context || !context->internal ||
1360 !context->internal->_initialized) {
1367 return smbc_open_ctx(context, path, creat_bits, mode);
1371 * Routine to read() a file ...
1375 smbc_read_ctx(SMBCCTX *context,
1381 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
1383 char *targetpath = NULL;
1384 struct cli_state *targetcli = NULL;
1385 TALLOC_CTX *frame = talloc_stackframe();
1390 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1391 * appears to pass file->offset (which is type off_t) differently than
1392 * a local variable of type off_t. Using local variable "offset" in
1393 * the call to cli_read() instead of file->offset fixes a problem
1394 * retrieving data at an offset greater than 4GB.
1398 if (!context || !context->internal ||
1399 !context->internal->_initialized) {
1406 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1408 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1415 offset = file->offset;
1417 /* Check that the buffer exists ... */
1426 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1427 if (smbc_parse_path(frame,
1442 /*d_printf(">>>read: resolving %s\n", path);*/
1443 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1444 &targetcli, &targetpath)) {
1445 d_printf("Could not resolve %s\n", path);
1449 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1451 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1455 errno = smbc_errno(context, targetcli);
1461 file->offset += ret;
1463 DEBUG(4, (" --> %d\n", ret));
1466 return ret; /* Success, ret bytes of data ... */
1471 * Routine to write() a file ...
1475 smbc_write_ctx(SMBCCTX *context,
1482 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
1484 char *targetpath = NULL;
1485 struct cli_state *targetcli = NULL;
1486 TALLOC_CTX *frame = talloc_stackframe();
1488 /* First check all pointers before dereferencing them */
1490 if (!context || !context->internal ||
1491 !context->internal->_initialized) {
1498 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1504 /* Check that the buffer exists ... */
1513 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1515 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1516 if (smbc_parse_path(frame,
1531 /*d_printf(">>>write: resolving %s\n", path);*/
1532 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1533 &targetcli, &targetpath)) {
1534 d_printf("Could not resolve %s\n", path);
1538 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1540 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1543 errno = smbc_errno(context, targetcli);
1549 file->offset += ret;
1552 return ret; /* Success, 0 bytes of data ... */
1556 * Routine to close() a file ...
1560 smbc_close_ctx(SMBCCTX *context,
1564 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
1566 char *targetpath = NULL;
1567 struct cli_state *targetcli = NULL;
1568 TALLOC_CTX *frame = talloc_stackframe();
1570 if (!context || !context->internal ||
1571 !context->internal->_initialized) {
1578 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1587 return (context->closedir)(context, file);
1590 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1591 if (smbc_parse_path(frame,
1606 /*d_printf(">>>close: resolving %s\n", path);*/
1607 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1608 &targetcli, &targetpath)) {
1609 d_printf("Could not resolve %s\n", path);
1613 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1615 if (!cli_close(targetcli, file->cli_fd)) {
1617 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1619 /* Deallocate slot and remove the server
1620 * from the server cache if unused */
1621 errno = smbc_errno(context, targetcli);
1623 DLIST_REMOVE(context->internal->_files, file);
1624 SAFE_FREE(file->fname);
1626 (context->callbacks.remove_unused_server_fn)(context, srv);
1632 DLIST_REMOVE(context->internal->_files, file);
1633 SAFE_FREE(file->fname);
1641 * Get info from an SMB server on a file. Use a qpathinfo call first
1642 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1645 smbc_getatr(SMBCCTX * context,
1650 struct timespec *create_time_ts,
1651 struct timespec *access_time_ts,
1652 struct timespec *write_time_ts,
1653 struct timespec *change_time_ts,
1656 char *fixedpath = NULL;
1657 char *targetpath = NULL;
1658 struct cli_state *targetcli = NULL;
1660 TALLOC_CTX *frame = talloc_stackframe();
1662 if (!context || !context->internal ||
1663 !context->internal->_initialized) {
1669 /* path fixup for . and .. */
1670 if (strequal(path, ".") || strequal(path, "..")) {
1671 fixedpath = talloc_strdup(frame, "\\");
1678 fixedpath = talloc_strdup(frame, path);
1684 trim_string(fixedpath, NULL, "\\..");
1685 trim_string(fixedpath, NULL, "\\.");
1687 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1689 if (!cli_resolve_path(frame, "", srv->cli, fixedpath,
1690 &targetcli, &targetpath)) {
1691 d_printf("Couldn't resolve %s\n", path);
1696 if (!srv->no_pathinfo2 &&
1697 cli_qpathinfo2(targetcli, targetpath,
1707 /* if this is NT then don't bother with the getatr */
1708 if (targetcli->capabilities & CAP_NT_SMBS) {
1714 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1716 struct timespec w_time_ts;
1718 w_time_ts = convert_time_t_to_timespec(write_time);
1720 if (write_time_ts != NULL) {
1721 *write_time_ts = w_time_ts;
1724 if (create_time_ts != NULL) {
1725 *create_time_ts = w_time_ts;
1728 if (access_time_ts != NULL) {
1729 *access_time_ts = w_time_ts;
1732 if (change_time_ts != NULL) {
1733 *change_time_ts = w_time_ts;
1736 srv->no_pathinfo2 = True;
1748 * Set file info on an SMB server. Use setpathinfo call first. If that
1749 * fails, use setattrE..
1751 * Access and modification time parameters are always used and must be
1752 * provided. Create time, if zero, will be determined from the actual create
1753 * time of the file. If non-zero, the create time will be set as well.
1755 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1758 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1767 TALLOC_CTX *frame = talloc_stackframe();
1770 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1771 * modern function for "new code" to be using, and it works given a
1772 * filename rather than requiring that the file be opened to have its
1773 * attributes manipulated.
1775 if (srv->no_pathinfo ||
1776 ! cli_setpathinfo(srv->cli, path,
1784 * setpathinfo is not supported; go to plan B.
1786 * cli_setatr() does not work on win98, and it also doesn't
1787 * support setting the access time (only the modification
1788 * time), so in all cases, we open the specified file and use
1789 * cli_setattrE() which should work on all OS versions, and
1790 * supports both times.
1793 /* Don't try {q,set}pathinfo() again, with this server */
1794 srv->no_pathinfo = True;
1797 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1799 errno = smbc_errno(context, srv->cli);
1804 /* Set the new attributes */
1805 ret = cli_setattrE(srv->cli, fd,
1810 /* Close the file */
1811 cli_close(srv->cli, fd);
1814 * Unfortunately, setattrE() doesn't have a provision for
1815 * setting the access mode (attributes). We'll have to try
1816 * cli_setatr() for that, and with only this parameter, it
1817 * seems to work on win98.
1819 if (ret && mode != (uint16) -1) {
1820 ret = cli_setatr(srv->cli, path, mode, 0);
1824 errno = smbc_errno(context, srv->cli);
1835 * Routine to unlink() a file
1839 smbc_unlink_ctx(SMBCCTX *context,
1842 char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *workgroup = NULL;
1844 char *targetpath = NULL;
1845 struct cli_state *targetcli = NULL;
1846 SMBCSRV *srv = NULL;
1847 TALLOC_CTX *frame = talloc_stackframe();
1849 if (!context || !context->internal ||
1850 !context->internal->_initialized) {
1851 errno = EINVAL; /* Best I can think of ... */
1864 if (smbc_parse_path(frame,
1879 if (!user || user[0] == (char)0) {
1880 user = talloc_strdup(frame, context->user);
1888 srv = smbc_server(frame, context, True,
1889 server, share, &workgroup, &user, &password);
1893 return -1; /* smbc_server sets errno */
1897 /*d_printf(">>>unlink: resolving %s\n", path);*/
1898 if (!cli_resolve_path(frame, "", srv->cli, path,
1899 &targetcli, &targetpath)) {
1900 d_printf("Could not resolve %s\n", path);
1904 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1906 if (!cli_unlink(targetcli, targetpath)) {
1908 errno = smbc_errno(context, targetcli);
1910 if (errno == EACCES) { /* Check if the file is a directory */
1915 struct timespec write_time_ts;
1916 struct timespec access_time_ts;
1917 struct timespec change_time_ts;
1920 if (!smbc_getatr(context, srv, path, &mode, &size,
1927 /* Hmmm, bad error ... What? */
1929 errno = smbc_errno(context, targetcli);
1936 if (IS_DOS_DIR(mode))
1939 errno = saverr; /* Restore this */
1950 return 0; /* Success ... */
1955 * Routine to rename() a file
1959 smbc_rename_ctx(SMBCCTX *ocontext,
1964 char *server1 = NULL;
1965 char *share1 = NULL;
1966 char *server2 = NULL;
1967 char *share2 = NULL;
1970 char *password1 = NULL;
1971 char *password2 = NULL;
1972 char *workgroup = NULL;
1975 char *targetpath1 = NULL;
1976 char *targetpath2 = NULL;
1977 struct cli_state *targetcli1 = NULL;
1978 struct cli_state *targetcli2 = NULL;
1979 SMBCSRV *srv = NULL;
1980 TALLOC_CTX *frame = talloc_stackframe();
1982 if (!ocontext || !ncontext ||
1983 !ocontext->internal || !ncontext->internal ||
1984 !ocontext->internal->_initialized ||
1985 !ncontext->internal->_initialized) {
1986 errno = EINVAL; /* Best I can think of ... */
1991 if (!oname || !nname) {
1997 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1999 if (smbc_parse_path(frame,
2014 if (!user1 || user1[0] == (char)0) {
2015 user1 = talloc_strdup(frame, ocontext->user);
2023 if (smbc_parse_path(frame,
2038 if (!user2 || user2[0] == (char)0) {
2039 user2 = talloc_strdup(frame, ncontext->user);
2047 if (strcmp(server1, server2) || strcmp(share1, share2) ||
2048 strcmp(user1, user2)) {
2049 /* Can't rename across file systems, or users?? */
2055 srv = smbc_server(frame, ocontext, True,
2056 server1, share1, &workgroup, &user1, &password1);
2063 /*d_printf(">>>rename: resolving %s\n", path1);*/
2064 if (!cli_resolve_path(frame, "", srv->cli, path1,
2065 &targetcli1, &targetpath1)) {
2066 d_printf("Could not resolve %s\n", path1);
2070 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2071 /*d_printf(">>>rename: resolving %s\n", path2);*/
2072 if (!cli_resolve_path(frame, "", srv->cli, path2,
2073 &targetcli2, &targetpath2)) {
2074 d_printf("Could not resolve %s\n", path2);
2078 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2080 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
2081 strcmp(targetcli1->share, targetcli2->share))
2083 /* can't rename across file systems */
2089 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
2090 int eno = smbc_errno(ocontext, targetcli1);
2092 if (eno != EEXIST ||
2093 !cli_unlink(targetcli1, targetpath2) ||
2094 !cli_rename(targetcli1, targetpath1, targetpath2)) {
2104 return 0; /* Success */
2108 * A routine to lseek() a file
2112 smbc_lseek_ctx(SMBCCTX *context,
2118 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
2120 char *targetpath = NULL;
2121 struct cli_state *targetcli = NULL;
2122 TALLOC_CTX *frame = talloc_stackframe();
2124 if (!context || !context->internal ||
2125 !context->internal->_initialized) {
2131 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2143 return -1; /* Can't lseek a dir ... */
2149 file->offset = offset;
2153 file->offset += offset;
2157 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
2158 if (smbc_parse_path(frame,
2173 /*d_printf(">>>lseek: resolving %s\n", path);*/
2174 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2175 &targetcli, &targetpath)) {
2176 d_printf("Could not resolve %s\n", path);
2180 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
2182 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
2183 &size, NULL, NULL, NULL, NULL, NULL))
2185 SMB_OFF_T b_size = size;
2186 if (!cli_getattrE(targetcli, file->cli_fd,
2187 NULL, &b_size, NULL, NULL, NULL))
2195 file->offset = size + offset;
2205 return file->offset;
2210 * Generate an inode number from file name for those things that need it
2214 smbc_inode(SMBCCTX *context,
2217 if (!context || !context->internal ||
2218 !context->internal->_initialized) {
2225 if (!*name) return 2; /* FIXME, why 2 ??? */
2226 return (ino_t)str_checksum(name);
2231 * Routine to put basic stat info into a stat structure ... Used by stat and
2236 smbc_setup_stat(SMBCCTX *context,
2242 TALLOC_CTX *frame = talloc_stackframe();
2246 if (IS_DOS_DIR(mode)) {
2247 st->st_mode = SMBC_DIR_MODE;
2249 st->st_mode = SMBC_FILE_MODE;
2252 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2253 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2254 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2255 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2258 #ifdef HAVE_STAT_ST_BLKSIZE
2259 st->st_blksize = 512;
2261 #ifdef HAVE_STAT_ST_BLOCKS
2262 st->st_blocks = (size+511)/512;
2264 #ifdef HAVE_STRUCT_STAT_ST_RDEV
2267 st->st_uid = getuid();
2268 st->st_gid = getgid();
2270 if (IS_DOS_DIR(mode)) {
2276 if (st->st_ino == 0) {
2277 st->st_ino = smbc_inode(context, fname);
2281 return True; /* FIXME: Is this needed ? */
2286 * Routine to stat a file given a name
2290 smbc_stat_ctx(SMBCCTX *context,
2294 SMBCSRV *srv = NULL;
2295 char *server = NULL;
2298 char *password = NULL;
2299 char *workgroup = NULL;
2301 struct timespec write_time_ts;
2302 struct timespec access_time_ts;
2303 struct timespec change_time_ts;
2307 TALLOC_CTX *frame = talloc_stackframe();
2309 if (!context || !context->internal ||
2310 !context->internal->_initialized) {
2312 errno = EINVAL; /* Best I can think of ... */
2323 DEBUG(4, ("smbc_stat(%s)\n", fname));
2325 if (smbc_parse_path(frame,
2340 if (!user || user[0] == (char)0) {
2341 user = talloc_strdup(frame,context->user);
2349 srv = smbc_server(frame, context, True,
2350 server, share, &workgroup, &user, &password);
2354 return -1; /* errno set by smbc_server */
2357 if (!smbc_getatr(context, srv, path, &mode, &size,
2363 errno = smbc_errno(context, srv->cli);
2370 smbc_setup_stat(context, st, (char *) fname, size, mode);
2372 set_atimespec(st, access_time_ts);
2373 set_ctimespec(st, change_time_ts);
2374 set_mtimespec(st, write_time_ts);
2375 st->st_dev = srv->dev;
2383 * Routine to stat a file given an fd
2387 smbc_fstat_ctx(SMBCCTX *context,
2391 struct timespec change_time_ts;
2392 struct timespec access_time_ts;
2393 struct timespec write_time_ts;
2396 char *server = NULL;
2399 char *password = NULL;
2401 char *targetpath = NULL;
2402 struct cli_state *targetcli = NULL;
2404 TALLOC_CTX *frame = talloc_stackframe();
2406 if (!context || !context->internal ||
2407 !context->internal->_initialized) {
2413 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2421 return (context->fstatdir)(context, file, st);
2424 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2425 if (smbc_parse_path(frame,
2440 /*d_printf(">>>fstat: resolving %s\n", path);*/
2441 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2442 &targetcli, &targetpath)) {
2443 d_printf("Could not resolve %s\n", path);
2447 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2449 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2456 time_t change_time, access_time, write_time;
2458 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2459 &change_time, &access_time, &write_time)) {
2466 change_time_ts = convert_time_t_to_timespec(change_time);
2467 access_time_ts = convert_time_t_to_timespec(access_time);
2468 write_time_ts = convert_time_t_to_timespec(write_time);
2473 smbc_setup_stat(context, st, file->fname, size, mode);
2475 set_atimespec(st, access_time_ts);
2476 set_ctimespec(st, change_time_ts);
2477 set_mtimespec(st, write_time_ts);
2478 st->st_dev = file->srv->dev;
2486 * Routine to open a directory
2487 * We accept the URL syntax explained in smbc_parse_path(), above.
2491 smbc_remove_dir(SMBCFILE *dir)
2493 struct smbc_dir_list *d,*f;
2500 SAFE_FREE(f->dirent);
2505 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2510 add_dirent(SMBCFILE *dir,
2512 const char *comment,
2515 struct smbc_dirent *dirent;
2517 int name_length = (name == NULL ? 0 : strlen(name));
2518 int comment_len = (comment == NULL ? 0 : strlen(comment));
2521 * Allocate space for the dirent, which must be increased by the
2522 * size of the name and the comment and 1 each for the null terminator.
2525 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2527 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2531 dir->dir_error = ENOMEM;
2536 ZERO_STRUCTP(dirent);
2538 if (dir->dir_list == NULL) {
2540 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2541 if (!dir->dir_list) {
2544 dir->dir_error = ENOMEM;
2548 ZERO_STRUCTP(dir->dir_list);
2550 dir->dir_end = dir->dir_next = dir->dir_list;
2554 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2556 if (!dir->dir_end->next) {
2559 dir->dir_error = ENOMEM;
2563 ZERO_STRUCTP(dir->dir_end->next);
2565 dir->dir_end = dir->dir_end->next;
2568 dir->dir_end->next = NULL;
2569 dir->dir_end->dirent = dirent;
2571 dirent->smbc_type = type;
2572 dirent->namelen = name_length;
2573 dirent->commentlen = comment_len;
2574 dirent->dirlen = size;
2577 * dirent->namelen + 1 includes the null (no null termination needed)
2578 * Ditto for dirent->commentlen.
2579 * The space for the two null bytes was allocated.
2581 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2582 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2583 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2590 list_unique_wg_fn(const char *name,
2592 const char *comment,
2595 SMBCFILE *dir = (SMBCFILE *)state;
2596 struct smbc_dir_list *dir_list;
2597 struct smbc_dirent *dirent;
2601 dirent_type = dir->dir_type;
2603 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2605 /* An error occurred, what do we do? */
2606 /* FIXME: Add some code here */
2609 /* Point to the one just added */
2610 dirent = dir->dir_end->dirent;
2612 /* See if this was a duplicate */
2613 for (dir_list = dir->dir_list;
2614 dir_list != dir->dir_end;
2615 dir_list = dir_list->next) {
2617 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2618 /* Duplicate. End end of list need to be removed. */
2622 if (do_remove && dir_list->next == dir->dir_end) {
2623 /* Found the end of the list. Remove it. */
2624 dir->dir_end = dir_list;
2625 free(dir_list->next);
2627 dir_list->next = NULL;
2634 list_fn(const char *name,
2636 const char *comment,
2639 SMBCFILE *dir = (SMBCFILE *)state;
2643 * We need to process the type a little ...
2645 * Disk share = 0x00000000
2646 * Print share = 0x00000001
2647 * Comms share = 0x00000002 (obsolete?)
2648 * IPC$ share = 0x00000003
2650 * administrative shares:
2651 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2654 if (dir->dir_type == SMBC_FILE_SHARE) {
2656 case 0 | 0x80000000:
2658 dirent_type = SMBC_FILE_SHARE;
2662 dirent_type = SMBC_PRINTER_SHARE;
2666 dirent_type = SMBC_COMMS_SHARE;
2669 case 3 | 0x80000000:
2671 dirent_type = SMBC_IPC_SHARE;
2675 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2680 dirent_type = dir->dir_type;
2683 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2685 /* An error occurred, what do we do? */
2686 /* FIXME: Add some code here */
2692 dir_list_fn(const char *mnt,
2698 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2699 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2701 /* Handle an error ... */
2703 /* FIXME: Add some code ... */
2710 net_share_enum_rpc(struct cli_state *cli,
2711 void (*fn)(const char *name,
2713 const char *comment,
2720 uint32 info_level = 1;
2721 uint32 preferred_len = 0xffffffff;
2723 SRV_SHARE_INFO_CTR ctr;
2725 fstring comment = "";
2726 struct rpc_pipe_client *pipe_hnd;
2729 /* Open the server service pipe */
2730 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2732 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2736 /* Issue the NetShareEnum RPC call and retrieve the response */
2737 init_enum_hnd(&enum_hnd, 0);
2738 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2745 /* Was it successful? */
2746 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2747 /* Nope. Go clean up. */
2751 /* For each returned entry... */
2752 for (i = 0; i < ctr.num_entries; i++) {
2754 /* pull out the share name */
2755 rpcstr_pull_unistr2_fstring(
2756 name, &ctr.share.info1[i].info_1_str.uni_netname);
2758 /* pull out the share's comment */
2759 rpcstr_pull_unistr2_fstring(
2760 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2762 /* Get the type value */
2763 type = ctr.share.info1[i].info_1.type;
2765 /* Add this share to the list */
2766 (*fn)(name, type, comment, state);
2770 /* Close the server service pipe */
2771 cli_rpc_pipe_close(pipe_hnd);
2773 /* Tell 'em if it worked */
2774 return W_ERROR_IS_OK(result) ? 0 : -1;
2780 smbc_opendir_ctx(SMBCCTX *context,
2784 char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *options = NULL;
2785 char *workgroup = NULL;
2789 SMBCSRV *srv = NULL;
2790 SMBCFILE *dir = NULL;
2791 struct _smbc_callbacks *cb = NULL;
2792 struct sockaddr_storage rem_ss;
2793 TALLOC_CTX *frame = talloc_stackframe();
2795 if (!context || !context->internal ||
2796 !context->internal->_initialized) {
2797 DEBUG(4, ("no valid context\n"));
2798 errno = EINVAL + 8192;
2805 DEBUG(4, ("no valid fname\n"));
2806 errno = EINVAL + 8193;
2811 if (smbc_parse_path(frame,
2821 DEBUG(4, ("no valid path\n"));
2822 errno = EINVAL + 8194;
2827 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2828 "path='%s' options='%s'\n",
2829 fname, server, share, path, options));
2831 /* Ensure the options are valid */
2832 if (smbc_check_options(server, share, path, options)) {
2833 DEBUG(4, ("unacceptable options (%s)\n", options));
2834 errno = EINVAL + 8195;
2839 if (!user || user[0] == (char)0) {
2840 user = talloc_strdup(frame, context->user);
2848 dir = SMB_MALLOC_P(SMBCFILE);
2859 dir->fname = SMB_STRDUP(fname);
2863 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2865 if (server[0] == (char)0) {
2870 struct ip_service *ip_list;
2871 struct ip_service server_addr;
2872 struct user_auth_info u_info;
2874 if (share[0] != (char)0 || path[0] != (char)0) {
2876 errno = EINVAL + 8196;
2878 SAFE_FREE(dir->fname);
2885 /* Determine how many local master browsers to query */
2886 max_lmb_count = (context->options.browse_max_lmb_count == 0
2888 : context->options.browse_max_lmb_count);
2890 memset(&u_info, '\0', sizeof(u_info));
2891 u_info.username = talloc_strdup(frame,user);
2892 u_info.password = talloc_strdup(frame,password);
2893 if (!u_info.username || !u_info.password) {
2895 SAFE_FREE(dir->fname);
2903 * We have server and share and path empty but options
2904 * requesting that we scan all master browsers for their list
2905 * of workgroups/domains. This implies that we must first try
2906 * broadcast queries to find all master browsers, and if that
2907 * doesn't work, then try our other methods which return only
2908 * a single master browser.
2912 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
2918 if (!find_master_ip(workgroup, &server_addr.ss)) {
2921 SAFE_FREE(dir->fname);
2929 ip_list = (struct ip_service *)memdup(
2930 &server_addr, sizeof(server_addr));
2931 if (ip_list == NULL) {
2939 for (i = 0; i < count && i < max_lmb_count; i++) {
2940 char addr[INET6_ADDRSTRLEN];
2941 char *wg_ptr = NULL;
2942 struct cli_state *cli = NULL;
2944 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
2945 DEBUG(99, ("Found master browser %d of %d: %s\n",
2946 i+1, MAX(count, max_lmb_count),
2949 cli = get_ipc_connect_master_ip(talloc_tos(),
2953 /* cli == NULL is the master browser refused to talk or
2954 could not be found */
2959 workgroup = talloc_strdup(frame, wg_ptr);
2960 server = talloc_strdup(frame, cli->desthost);
2964 if (!workgroup || !server) {
2970 DEBUG(4, ("using workgroup %s %s\n",
2971 workgroup, server));
2974 * For each returned master browser IP address, get a
2975 * connection to IPC$ on the server if we do not
2976 * already have one, and determine the
2977 * workgroups/domains that it knows about.
2980 srv = smbc_server(frame, context, True, server, "IPC$",
2981 &workgroup, &user, &password);
2987 dir->dir_type = SMBC_WORKGROUP;
2989 /* Now, list the stuff ... */
2991 if (!cli_NetServerEnum(srv->cli,
2993 SV_TYPE_DOMAIN_ENUM,
3003 * Server not an empty string ... Check the rest and see what
3006 if (*share == '\0') {
3007 if (*path != '\0') {
3009 /* Should not have empty share with path */
3010 errno = EINVAL + 8197;
3012 SAFE_FREE(dir->fname);
3021 * We don't know if <server> is really a server name
3022 * or is a workgroup/domain name. If we already have
3023 * a server structure for it, we'll use it.
3024 * Otherwise, check to see if <server><1D>,
3025 * <server><1B>, or <server><20> translates. We check
3026 * to see if <server> is an IP address first.
3030 * See if we have an existing server. Do not
3031 * establish a connection if one does not already
3034 srv = smbc_server(frame, context, False, server, "IPC$",
3035 &workgroup, &user, &password);
3038 * If no existing server and not an IP addr, look for
3042 !is_ipaddress(server) &&
3043 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
3044 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
3048 dir->dir_type = SMBC_SERVER;
3051 * Get the backup list ...
3053 if (!name_status_find(server, 0, 0,
3054 &rem_ss, buserver)) {
3056 DEBUG(0, ("Could not get name of "
3057 "local/domain master browser "
3058 "for server %s\n", server));
3060 SAFE_FREE(dir->fname);
3070 * Get a connection to IPC$ on the server if
3071 * we do not already have one
3073 srv = smbc_server(frame, context, True,
3075 &workgroup, &user, &password);
3077 DEBUG(0, ("got no contact to IPC$\n"));
3079 SAFE_FREE(dir->fname);
3089 /* Now, list the servers ... */
3090 if (!cli_NetServerEnum(srv->cli, server,
3091 0x0000FFFE, list_fn,
3095 SAFE_FREE(dir->fname);
3102 (resolve_name(server, &rem_ss, 0x20))) {
3104 /* If we hadn't found the server, get one now */
3106 srv = smbc_server(frame, context, True,
3114 SAFE_FREE(dir->fname);
3122 dir->dir_type = SMBC_FILE_SHARE;