2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2004
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "include/libsmb_internal.h"
29 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir);
30 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
31 struct smbc_dirent *dirent);
34 * DOS Attribute values (used internally)
36 typedef struct DOS_ATTR_DESC {
48 * Internal flags for extended attributes
51 /* internal mode values */
52 #define SMBC_XATTR_MODE_ADD 1
53 #define SMBC_XATTR_MODE_REMOVE 2
54 #define SMBC_XATTR_MODE_REMOVE_ALL 3
55 #define SMBC_XATTR_MODE_SET 4
56 #define SMBC_XATTR_MODE_CHOWN 5
57 #define SMBC_XATTR_MODE_CHGRP 6
59 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
61 /*We should test for this in configure ... */
63 #define ENOTSUP EOPNOTSUPP
67 * Functions exported by libsmb_cache.c that we need here
69 int smbc_default_cache_functions(SMBCCTX *context);
72 * check if an element is part of the list.
73 * FIXME: Does not belong here !
74 * Can anyone put this in a macro in dlinklist.h ?
77 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
78 if (!p || !list) return False;
80 if (p == list) return True;
87 * Find an lsa pipe handle associated with a cli struct.
89 static struct rpc_pipe_client *
90 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
92 struct rpc_pipe_client *pipe_hnd;
94 for (pipe_hnd = ipc_cli->pipe_list;
96 pipe_hnd = pipe_hnd->next) {
98 if (pipe_hnd->pipe_idx == PI_LSARPC) {
107 smbc_close_ctx(SMBCCTX *context,
110 smbc_lseek_ctx(SMBCCTX *context,
115 extern bool in_client;
118 * Is the logging working / configfile read ?
120 static int smbc_initialized = 0;
123 hex2int( unsigned int _char )
125 if ( _char >= 'A' && _char <='F')
126 return _char - 'A' + 10;
127 if ( _char >= 'a' && _char <='f')
128 return _char - 'a' + 10;
129 if ( _char >= '0' && _char <='9')
136 * and smbc_urldecode_talloc() (internal fn.)
138 * Convert strings of %xx to their single character equivalent. Each 'x' must
139 * be a valid hexadecimal digit, or that % sequence is left undecoded.
141 * dest may, but need not be, the same pointer as src.
143 * Returns the number of % sequences which could not be converted due to lack
144 * of two following hexadecimal digits.
147 smbc_urldecode_talloc(TALLOC_CTX *ctx, char **pp_dest, const char *src)
149 int old_length = strlen(src);
155 if (old_length == 0) {
160 for (i = 0; i < old_length; ) {
161 unsigned char character = src[i++];
163 if (character == '%') {
164 int a = i+1 < old_length ? hex2int(src[i]) : -1;
165 int b = i+1 < old_length ? hex2int(src[i+1]) : -1;
167 /* Replace valid sequence */
168 if (a != -1 && b != -1) {
169 /* Replace valid %xx sequence with %dd */
170 character = (a * 16) + b;
171 if (character == '\0') {
172 break; /* Stop at %00 */
182 dest = TALLOC_ARRAY(ctx, char, newlen);
188 for (p = dest, i = 0; i < old_length; ) {
189 unsigned char character = src[i++];
191 if (character == '%') {
192 int a = i+1 < old_length ? hex2int(src[i]) : -1;
193 int b = i+1 < old_length ? hex2int(src[i+1]) : -1;
195 /* Replace valid sequence */
196 if (a != -1 && b != -1) {
197 /* Replace valid %xx sequence with %dd */
198 character = (a * 16) + b;
199 if (character == '\0') {
200 break; /* Stop at %00 */
216 smbc_urldecode(char *dest, char *src, size_t max_dest_len)
218 TALLOC_CTX *frame = talloc_stackframe();
220 int ret = smbc_urldecode_talloc(frame, &pdest, src);
223 strlcpy(dest, pdest, max_dest_len);
232 * Convert any characters not specifically allowed in a URL into their %xx
235 * Returns the remaining buffer length.
238 smbc_urlencode(char *dest, char *src, int max_dest_len)
240 char hex[] = "0123456789ABCDEF";
242 for (; *src != '\0' && max_dest_len >= 3; src++) {
254 *dest++ = hex[(*src >> 4) & 0x0f];
255 *dest++ = hex[*src & 0x0f];
270 * Function to parse a path and turn it into components
272 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
273 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
274 * general format ("smb:" only; we do not look for "cifs:").
278 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
282 * smb:// Show all workgroups.
284 * The method of locating the list of workgroups varies
285 * depending upon the setting of the context variable
286 * context->options.browse_max_lmb_count. This value
287 * determine the maximum number of local master browsers to
288 * query for the list of workgroups. In order to ensure that
289 * a complete list of workgroups is obtained, all master
290 * browsers must be queried, but if there are many
291 * workgroups, the time spent querying can begin to add up.
292 * For small networks (not many workgroups), it is suggested
293 * that this variable be set to 0, indicating query all local
294 * master browsers. When the network has many workgroups, a
295 * reasonable setting for this variable might be around 3.
297 * smb://name/ if name<1D> or name<1B> exists, list servers in
298 * workgroup, else, if name<20> exists, list all shares
301 * If "options" are provided, this function returns the entire option list as a
302 * string, for later parsing by the caller. Note that currently, no options
306 static const char *smbc_prefix = "smb:";
309 smbc_parse_path(TALLOC_CTX *ctx,
325 /* Ensure these returns are at least valid pointers. */
326 *pp_server = talloc_strdup(ctx, "");
327 *pp_share = talloc_strdup(ctx, "");
328 *pp_path = talloc_strdup(ctx, "");
329 *pp_user = talloc_strdup(ctx, "");
330 *pp_password = talloc_strdup(ctx, "");
332 if (!*pp_server || !*pp_share || !*pp_path ||
333 !*pp_user || !*pp_password) {
338 * Assume we wont find an authentication domain to parse, so default
339 * to the workgroup in the provided context.
341 if (pp_workgroup != NULL) {
342 *pp_workgroup = talloc_strdup(ctx, context->workgroup);
346 *pp_options = talloc_strdup(ctx, "");
348 s = talloc_strdup(ctx, fname);
350 /* see if it has the right prefix */
351 len = strlen(smbc_prefix);
352 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
353 return -1; /* What about no smb: ? */
358 /* Watch the test below, we are testing to see if we should exit */
360 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
361 DEBUG(1, ("Invalid path (does not begin with smb://"));
365 p += 2; /* Skip the double slash */
367 /* See if any options were specified */
368 if ((q = strrchr(p, '?')) != NULL ) {
369 /* There are options. Null terminate here and point to them */
372 DEBUG(4, ("Found options '%s'", q));
374 /* Copy the options */
375 if (*pp_options != NULL) {
376 TALLOC_FREE(*pp_options);
377 *pp_options = talloc_strdup(ctx, q);
386 int wl = strlen(context->workgroup);
392 *pp_server = talloc_strdup(ctx, context->workgroup);
396 *pp_server[wl] = '\0';
401 * ok, its for us. Now parse out the server, share etc.
403 * However, we want to parse out [[domain;]user[:password]@] if it
407 /* check that '@' occurs before '/', if '/' exists at all */
408 q = strchr_m(p, '@');
409 r = strchr_m(p, '/');
410 if (q && (!r || q < r)) {
411 char *userinfo = NULL;
414 next_token_no_ltrim_talloc(ctx, &p, &userinfo, "@");
420 if (strchr_m(u, ';')) {
422 next_token_no_ltrim_talloc(ctx, &u, &workgroup, ";");
427 *pp_workgroup = workgroup;
431 if (strchr_m(u, ':')) {
432 next_token_no_ltrim_talloc(ctx, &u, pp_user, ":");
436 *pp_password = talloc_strdup(ctx, u);
441 *pp_user = talloc_strdup(ctx, u);
448 if (!next_token_talloc(ctx, &p, pp_server, "/")) {
453 goto decoding; /* That's it ... */
456 if (!next_token_talloc(ctx, &p, pp_share, "/")) {
461 * Prepend a leading slash if there's a file path, as required by
465 *pp_path = talloc_asprintf(ctx,
469 *pp_path = talloc_strdup(ctx, "");
474 string_replace(*pp_path, '/', '\\');
478 (void) smbc_urldecode_talloc(ctx, pp_path, *pp_path);
479 (void) smbc_urldecode_talloc(ctx, pp_server, *pp_server);
480 (void) smbc_urldecode_talloc(ctx, pp_share, *pp_share);
481 (void) smbc_urldecode_talloc(ctx, pp_user, *pp_user);
482 (void) smbc_urldecode_talloc(ctx, pp_password, *pp_password);
488 * Verify that the options specified in a URL are valid
491 smbc_check_options(char *server,
496 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
497 "path='%s' options='%s'\n",
498 server, share, path, options));
500 /* No options at all is always ok */
501 if (! *options) return 0;
503 /* Currently, we don't support any options. */
508 * Convert an SMB error into a UNIX error ...
511 smbc_errno(SMBCCTX *context,
514 int ret = cli_errno(c);
516 if (cli_is_dos_error(c)) {
520 cli_dos_error(c, &eclass, &ecode);
522 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
523 (int)eclass, (int)ecode, (int)ecode, ret));
527 status = cli_nt_error(c);
529 DEBUG(3,("smbc errno %s -> %d\n",
530 nt_errstr(status), ret));
537 * Check a server for being alive and well.
538 * returns 0 if the server is in shape. Returns 1 on error
540 * Also useable outside libsmbclient to enable external cache
541 * to do some checks too.
544 smbc_check_server(SMBCCTX * context,
548 struct sockaddr addr;
551 return (getpeername(server->cli->fd, &addr, &size) == -1);
555 * Remove a server from the cached server list it's unused.
556 * On success, 0 is returned. 1 is returned if the server could not be removed.
558 * Also useable outside libsmbclient
561 smbc_remove_unused_server(SMBCCTX * context,
566 /* are we being fooled ? */
567 if (!context || !context->internal ||
568 !context->internal->_initialized || !srv) return 1;
571 /* Check all open files/directories for a relation with this server */
572 for (file = context->internal->_files; file; file=file->next) {
573 if (file->srv == srv) {
575 DEBUG(3, ("smbc_remove_usused_server: "
576 "%p still used by %p.\n",
582 DLIST_REMOVE(context->internal->_servers, srv);
584 cli_shutdown(srv->cli);
587 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
589 (context->callbacks.remove_cached_srv_fn)(context, srv);
596 find_server(SMBCCTX *context,
608 srv = (context->callbacks.get_cached_srv_fn)(context, server, share,
609 workgroup, username);
611 if (!auth_called && !srv && (!username[0] || !password[0])) {
612 if (context->internal->_auth_fn_with_context != NULL) {
613 (context->internal->_auth_fn_with_context)(
616 workgroup, strlen(workgroup)+1,
617 username, strlen(username)+1,
618 password, strlen(password)+1);
620 (context->callbacks.auth_fn)(
622 workgroup, strlen(workgroup)+1,
623 username, strlen(username)+1,
624 password, strlen(password)+1);
628 * However, smbc_auth_fn may have picked up info relating to
629 * an existing connection, so try for an existing connection
633 goto check_server_cache;
638 if ((context->callbacks.check_server_fn)(context, srv)) {
640 * This server is no good anymore
641 * Try to remove it and check for more possible
642 * servers in the cache
644 if ((context->callbacks.remove_unused_server_fn)(context,
647 * We could not remove the server completely,
648 * remove it from the cache so we will not get
649 * it again. It will be removed when the last
650 * file/dir is closed.
652 (context->callbacks.remove_cached_srv_fn)(context,
657 * Maybe there are more cached connections to this
660 goto check_server_cache;
670 * Connect to a server, possibly on an existing connection
672 * Here, what we want to do is: If the server and username
673 * match an existing connection, reuse that, otherwise, establish a
676 * If we have to create a new connection, call the auth_fn to get the
677 * info we need, unless the username and password were passed in.
681 smbc_server(SMBCCTX *context,
682 bool connect_if_not_found,
691 struct nmb_name called, calling;
692 const char *server_n = server;
693 struct sockaddr_storage ss;
694 int tried_reverse = 0;
697 const char *username_used;
703 if (server[0] == 0) {
708 /* Look for a cached connection */
709 srv = find_server(context, server, share,
710 workgroup, username, password);
713 * If we found a connection and we're only allowed one share per
716 if (srv && *share != '\0' && context->options.one_share_per_server) {
719 * ... then if there's no current connection to the share,
720 * connect to it. find_server(), or rather the function
721 * pointed to by context->callbacks.get_cached_srv_fn which
722 * was called by find_server(), will have issued a tree
723 * disconnect if the requested share is not the same as the
724 * one that was already connected.
726 if (srv->cli->cnum == (uint16) -1) {
727 /* Ensure we have accurate auth info */
728 if (context->internal->_auth_fn_with_context != NULL) {
729 (context->internal->_auth_fn_with_context)(
732 workgroup, strlen(workgroup)+1,
733 username, strlen(username)+1,
734 password, strlen(password)+1);
736 (context->callbacks.auth_fn)(
738 workgroup, strlen(workgroup)+1,
739 username, strlen(username)+1,
740 password, strlen(password)+1);
744 * We don't need to renegotiate encryption
745 * here as the encryption context is not per
749 if (! cli_send_tconX(srv->cli, share, "?????",
750 password, strlen(password)+1)) {
752 errno = smbc_errno(context, srv->cli);
753 cli_shutdown(srv->cli);
755 (context->callbacks.remove_cached_srv_fn)(context,
761 * Regenerate the dev value since it's based on both
765 srv->dev = (dev_t)(str_checksum(server) ^
766 str_checksum(share));
771 /* If we have a connection... */
774 /* ... then we're done here. Give 'em what they came for. */
778 /* If we're not asked to connect when a connection doesn't exist... */
779 if (! connect_if_not_found) {
780 /* ... then we're done here. */
784 make_nmb_name(&calling, context->netbios_name, 0x0);
785 make_nmb_name(&called , server, 0x20);
787 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
789 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
795 /* have to open a new connection */
796 if ((c = cli_initialise()) == NULL) {
801 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
802 c->use_kerberos = True;
804 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
805 c->fallback_after_kerberos = True;
808 c->timeout = context->timeout;
811 * Force use of port 139 for first try if share is $IPC, empty, or
812 * null, so browse lists can work
814 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
815 port_try_first = 139;
818 port_try_first = 445;
822 c->port = port_try_first;
824 status = cli_connect(c, server_n, &ss);
825 if (!NT_STATUS_IS_OK(status)) {
827 /* First connection attempt failed. Try alternate port. */
828 c->port = port_try_next;
830 status = cli_connect(c, server_n, &ss);
831 if (!NT_STATUS_IS_OK(status)) {
838 if (!cli_session_request(c, &calling, &called)) {
840 if (strcmp(called.name, "*SMBSERVER")) {
841 make_nmb_name(&called , "*SMBSERVER", 0x20);
843 } else { /* Try one more time, but ensure we don't loop */
845 /* Only try this if server is an IP address ... */
847 if (is_ipaddress(server) && !tried_reverse) {
849 struct sockaddr_storage rem_ss;
851 if (!interpret_string_addr(&rem_ss, server,
853 DEBUG(4, ("Could not convert IP address "
854 "%s to struct sockaddr_storage\n",
860 tried_reverse++; /* Yuck */
862 if (name_status_find("*", 0, 0, &rem_ss, remote_name)) {
863 make_nmb_name(&called, remote_name, 0x20);
872 DEBUG(4,(" session request ok\n"));
874 if (!cli_negprot(c)) {
880 username_used = username;
882 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
883 password, strlen(password),
884 password, strlen(password),
887 /* Failed. Try an anonymous login, if allowed by flags. */
890 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
891 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
902 DEBUG(4,(" session setup ok\n"));
904 if (!cli_send_tconX(c, share, "?????",
905 password, strlen(password)+1)) {
906 errno = smbc_errno(context, c);
911 DEBUG(4,(" tconx ok\n"));
913 if (context->internal->_smb_encryption_level) {
914 /* Attempt UNIX smb encryption. */
915 if (!NT_STATUS_IS_OK(cli_force_encryption(c,
921 * context->internal->_smb_encryption_level == 1
922 * means don't fail if encryption can't be negotiated,
923 * == 2 means fail if encryption can't be negotiated.
926 DEBUG(4,(" SMB encrypt failed\n"));
928 if (context->internal->_smb_encryption_level == 2) {
934 DEBUG(4,(" SMB encrypt ok\n"));
938 * Ok, we have got a nice connection
939 * Let's allocate a server structure.
942 srv = SMB_MALLOC_P(SMBCSRV);
950 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
951 srv->no_pathinfo = False;
952 srv->no_pathinfo2 = False;
953 srv->no_nt_session = False;
955 /* now add it to the cache (internal or external) */
956 /* Let the cache function set errno if it wants to */
958 if ((context->callbacks.add_cached_srv_fn)(context, srv,
960 workgroup, username)) {
961 int saved_errno = errno;
962 DEBUG(3, (" Failed to add server to cache\n"));
970 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
971 server, share, srv));
973 DLIST_ADD(context->internal->_servers, srv);
987 * Connect to a server for getting/setting attributes, possibly on an existing
988 * connection. This works similarly to smbc_server().
991 smbc_attr_server(SMBCCTX *context,
1000 struct sockaddr_storage ss;
1001 struct cli_state *ipc_cli;
1002 struct rpc_pipe_client *pipe_hnd;
1004 SMBCSRV *ipc_srv=NULL;
1007 * See if we've already created this special connection. Reference
1008 * our "special" share name '*IPC$', which is an impossible real share
1009 * name due to the leading asterisk.
1011 ipc_srv = find_server(context, server, "*IPC$",
1012 workgroup, username, password);
1015 /* We didn't find a cached connection. Get the password */
1016 if (*password == '\0') {
1017 /* ... then retrieve it now. */
1018 if (context->internal->_auth_fn_with_context != NULL) {
1019 (context->internal->_auth_fn_with_context)(
1022 workgroup, strlen(workgroup)+1,
1023 username, strlen(username)+1,
1024 password, strlen(password)+1);
1026 (context->callbacks.auth_fn)(
1028 workgroup, strlen(workgroup)+1,
1029 username, strlen(username)+1,
1030 password, strlen(password)+1);
1035 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
1036 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1040 nt_status = cli_full_connection(&ipc_cli,
1041 global_myname(), server,
1042 &ss, 0, "IPC$", "?????",
1043 username, workgroup,
1046 if (! NT_STATUS_IS_OK(nt_status)) {
1047 DEBUG(1,("cli_full_connection failed! (%s)\n",
1048 nt_errstr(nt_status)));
1053 if (context->internal->_smb_encryption_level) {
1054 /* Attempt UNIX smb encryption. */
1055 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
1061 * context->internal->_smb_encryption_level == 1
1062 * means don't fail if encryption can't be negotiated,
1063 * == 2 means fail if encryption can't be negotiated.
1066 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
1068 if (context->internal->_smb_encryption_level == 2) {
1069 cli_shutdown(ipc_cli);
1074 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
1077 ipc_srv = SMB_MALLOC_P(SMBCSRV);
1080 cli_shutdown(ipc_cli);
1084 ZERO_STRUCTP(ipc_srv);
1085 ipc_srv->cli = ipc_cli;
1088 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1092 DEBUG(1, ("cli_nt_session_open fail!\n"));
1094 cli_shutdown(ipc_srv->cli);
1100 * Some systems don't support
1101 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1102 * so we might as well do it too.
1105 nt_status = rpccli_lsa_open_policy(
1109 GENERIC_EXECUTE_ACCESS,
1112 if (!NT_STATUS_IS_OK(nt_status)) {
1113 errno = smbc_errno(context, ipc_srv->cli);
1114 cli_shutdown(ipc_srv->cli);
1119 /* now add it to the cache (internal or external) */
1121 errno = 0; /* let cache function set errno if it likes */
1122 if ((context->callbacks.add_cached_srv_fn)(context, ipc_srv,
1127 DEBUG(3, (" Failed to add server to cache\n"));
1131 cli_shutdown(ipc_srv->cli);
1136 DLIST_ADD(context->internal->_servers, ipc_srv);
1143 * Routine to open() a file ...
1147 smbc_open_ctx(SMBCCTX *context,
1152 char *server, *share, *user, *password, *workgroup;
1154 char *targetpath = NULL;
1155 struct cli_state *targetcli;
1156 SMBCSRV *srv = NULL;
1157 SMBCFILE *file = NULL;
1159 TALLOC_CTX *frame = talloc_stackframe();
1161 if (!context || !context->internal ||
1162 !context->internal->_initialized) {
1164 errno = EINVAL; /* Best I can think of ... */
1178 if (smbc_parse_path(frame,
1193 if (!user || user[0] == (char)0) {
1194 user = talloc_strdup(frame, context->user);
1202 srv = smbc_server(context, True,
1203 server, share, workgroup, user, password);
1206 if (errno == EPERM) errno = EACCES;
1208 return NULL; /* smbc_server sets errno */
1211 /* Hmmm, the test for a directory is suspect here ... FIXME */
1213 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1216 file = SMB_MALLOC_P(SMBCFILE);
1226 /*d_printf(">>>open: resolving %s\n", path);*/
1227 if (!cli_resolve_path(frame, "", srv->cli, path, &targetcli, &targetpath)) {
1228 d_printf("Could not resolve %s\n", path);
1233 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1235 if ((fd = cli_open(targetcli, targetpath, flags,
1236 context->internal->_share_mode)) < 0) {
1238 /* Handle the error ... */
1241 errno = smbc_errno(context, targetcli);
1247 /* Fill in file struct */
1250 file->fname = SMB_STRDUP(fname);
1255 DLIST_ADD(context->internal->_files, file);
1258 * If the file was opened in O_APPEND mode, all write
1259 * operations should be appended to the file. To do that,
1260 * though, using this protocol, would require a getattrE()
1261 * call for each and every write, to determine where the end
1262 * of the file is. (There does not appear to be an append flag
1263 * in the protocol.) Rather than add all of that overhead of
1264 * retrieving the current end-of-file offset prior to each
1265 * write operation, we'll assume that most append operations
1266 * will continuously write, so we'll just set the offset to
1267 * the end of the file now and hope that's adequate.
1269 * Note to self: If this proves inadequate, and O_APPEND
1270 * should, in some cases, be forced for each write, add a
1271 * field in the context options structure, for
1272 * "strict_append_mode" which would select between the current
1273 * behavior (if FALSE) or issuing a getattrE() prior to each
1274 * write and forcing the write to the end of the file (if
1275 * TRUE). Adding that capability will likely require adding
1276 * an "append" flag into the _SMBCFILE structure to track
1277 * whether a file was opened in O_APPEND mode. -- djl
1279 if (flags & O_APPEND) {
1280 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1281 (void) smbc_close_ctx(context, file);
1293 /* Check if opendir needed ... */
1298 eno = smbc_errno(context, srv->cli);
1299 file = (context->opendir)(context, fname);
1300 if (!file) errno = eno;
1306 errno = EINVAL; /* FIXME, correct errno ? */
1313 * Routine to create a file
1316 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1319 smbc_creat_ctx(SMBCCTX *context,
1324 if (!context || !context->internal ||
1325 !context->internal->_initialized) {
1332 return smbc_open_ctx(context, path, creat_bits, mode);
1336 * Routine to read() a file ...
1340 smbc_read_ctx(SMBCCTX *context,
1346 char *server, *share, *user, *password;
1348 char *targetpath = NULL;
1349 struct cli_state *targetcli;
1350 TALLOC_CTX *frame = talloc_stackframe();
1355 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1356 * appears to pass file->offset (which is type off_t) differently than
1357 * a local variable of type off_t. Using local variable "offset" in
1358 * the call to cli_read() instead of file->offset fixes a problem
1359 * retrieving data at an offset greater than 4GB.
1363 if (!context || !context->internal ||
1364 !context->internal->_initialized) {
1371 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1373 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1380 offset = file->offset;
1382 /* Check that the buffer exists ... */
1391 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1392 if (smbc_parse_path(frame,
1407 /*d_printf(">>>read: resolving %s\n", path);*/
1408 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1409 &targetcli, &targetpath)) {
1410 d_printf("Could not resolve %s\n", path);
1414 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1416 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1420 errno = smbc_errno(context, targetcli);
1426 file->offset += ret;
1428 DEBUG(4, (" --> %d\n", ret));
1431 return ret; /* Success, ret bytes of data ... */
1436 * Routine to write() a file ...
1440 smbc_write_ctx(SMBCCTX *context,
1447 char *server, *share, *user, *password;
1449 char *targetpath = NULL;
1450 struct cli_state *targetcli;
1451 TALLOC_CTX *frame = talloc_stackframe();
1453 /* First check all pointers before dereferencing them */
1455 if (!context || !context->internal ||
1456 !context->internal->_initialized) {
1463 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1469 /* Check that the buffer exists ... */
1478 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1480 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1481 if (smbc_parse_path(frame,
1496 /*d_printf(">>>write: resolving %s\n", path);*/
1497 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1498 &targetcli, &targetpath)) {
1499 d_printf("Could not resolve %s\n", path);
1503 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1505 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1508 errno = smbc_errno(context, targetcli);
1514 file->offset += ret;
1517 return ret; /* Success, 0 bytes of data ... */
1521 * Routine to close() a file ...
1525 smbc_close_ctx(SMBCCTX *context,
1529 char *server, *share, *user, *password;
1531 char *targetpath = NULL;
1532 struct cli_state *targetcli;
1533 TALLOC_CTX *frame = talloc_stackframe();
1535 if (!context || !context->internal ||
1536 !context->internal->_initialized) {
1543 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1552 return (context->closedir)(context, file);
1555 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1556 if (smbc_parse_path(frame,
1571 /*d_printf(">>>close: resolving %s\n", path);*/
1572 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1573 &targetcli, &targetpath)) {
1574 d_printf("Could not resolve %s\n", path);
1578 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1580 if (!cli_close(targetcli, file->cli_fd)) {
1582 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1584 /* Deallocate slot and remove the server
1585 * from the server cache if unused */
1586 errno = smbc_errno(context, targetcli);
1588 DLIST_REMOVE(context->internal->_files, file);
1589 SAFE_FREE(file->fname);
1591 (context->callbacks.remove_unused_server_fn)(context, srv);
1597 DLIST_REMOVE(context->internal->_files, file);
1598 SAFE_FREE(file->fname);
1606 * Get info from an SMB server on a file. Use a qpathinfo call first
1607 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1610 smbc_getatr(SMBCCTX * context,
1615 struct timespec *create_time_ts,
1616 struct timespec *access_time_ts,
1617 struct timespec *write_time_ts,
1618 struct timespec *change_time_ts,
1622 char *targetpath = NULL;
1623 struct cli_state *targetcli;
1625 TALLOC_CTX *frame = talloc_stackframe();
1627 if (!context || !context->internal ||
1628 !context->internal->_initialized) {
1634 /* path fixup for . and .. */
1635 if (strequal(path, ".") || strequal(path, "..")) {
1636 fixedpath = talloc_strdup(frame, "\\");
1643 fixedpath = talloc_strdup(frame, path);
1649 trim_string(fixedpath, NULL, "\\..");
1650 trim_string(fixedpath, NULL, "\\.");
1652 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1654 if (!cli_resolve_path(frame, "", srv->cli, fixedpath,
1655 &targetcli, &targetpath)) {
1656 d_printf("Couldn't resolve %s\n", path);
1661 if (!srv->no_pathinfo2 &&
1662 cli_qpathinfo2(targetcli, targetpath,
1672 /* if this is NT then don't bother with the getatr */
1673 if (targetcli->capabilities & CAP_NT_SMBS) {
1679 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1681 struct timespec w_time_ts;
1683 w_time_ts = convert_time_t_to_timespec(write_time);
1685 if (write_time_ts != NULL) {
1686 *write_time_ts = w_time_ts;
1689 if (create_time_ts != NULL) {
1690 *create_time_ts = w_time_ts;
1693 if (access_time_ts != NULL) {
1694 *access_time_ts = w_time_ts;
1697 if (change_time_ts != NULL) {
1698 *change_time_ts = w_time_ts;
1701 srv->no_pathinfo2 = True;
1713 * Set file info on an SMB server. Use setpathinfo call first. If that
1714 * fails, use setattrE..
1716 * Access and modification time parameters are always used and must be
1717 * provided. Create time, if zero, will be determined from the actual create
1718 * time of the file. If non-zero, the create time will be set as well.
1720 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1723 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1732 TALLOC_CTX *frame = talloc_stackframe();
1735 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1736 * modern function for "new code" to be using, and it works given a
1737 * filename rather than requiring that the file be opened to have its
1738 * attributes manipulated.
1740 if (srv->no_pathinfo ||
1741 ! cli_setpathinfo(srv->cli, path,
1749 * setpathinfo is not supported; go to plan B.
1751 * cli_setatr() does not work on win98, and it also doesn't
1752 * support setting the access time (only the modification
1753 * time), so in all cases, we open the specified file and use
1754 * cli_setattrE() which should work on all OS versions, and
1755 * supports both times.
1758 /* Don't try {q,set}pathinfo() again, with this server */
1759 srv->no_pathinfo = True;
1762 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1764 errno = smbc_errno(context, srv->cli);
1769 /* Set the new attributes */
1770 ret = cli_setattrE(srv->cli, fd,
1775 /* Close the file */
1776 cli_close(srv->cli, fd);
1779 * Unfortunately, setattrE() doesn't have a provision for
1780 * setting the access mode (attributes). We'll have to try
1781 * cli_setatr() for that, and with only this parameter, it
1782 * seems to work on win98.
1784 if (ret && mode != (uint16) -1) {
1785 ret = cli_setatr(srv->cli, path, mode, 0);
1789 errno = smbc_errno(context, srv->cli);
1800 * Routine to unlink() a file
1804 smbc_unlink_ctx(SMBCCTX *context,
1807 char *server, *share, *user, *password, *workgroup;
1810 struct cli_state *targetcli;
1811 SMBCSRV *srv = NULL;
1812 TALLOC_CTX *frame = talloc_stackframe();
1814 if (!context || !context->internal ||
1815 !context->internal->_initialized) {
1816 errno = EINVAL; /* Best I can think of ... */
1829 if (smbc_parse_path(frame,
1844 if (!user || user[0] == (char)0) {
1845 user = talloc_strdup(frame, context->user);
1853 srv = smbc_server(context, True,
1854 server, share, workgroup, user, password);
1858 return -1; /* smbc_server sets errno */
1862 /*d_printf(">>>unlink: resolving %s\n", path);*/
1863 if (!cli_resolve_path(frame, "", srv->cli, path,
1864 &targetcli, &targetpath)) {
1865 d_printf("Could not resolve %s\n", path);
1869 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1871 if (!cli_unlink(targetcli, targetpath)) {
1873 errno = smbc_errno(context, targetcli);
1875 if (errno == EACCES) { /* Check if the file is a directory */
1880 struct timespec write_time_ts;
1881 struct timespec access_time_ts;
1882 struct timespec change_time_ts;
1885 if (!smbc_getatr(context, srv, path, &mode, &size,
1892 /* Hmmm, bad error ... What? */
1894 errno = smbc_errno(context, targetcli);
1901 if (IS_DOS_DIR(mode))
1904 errno = saverr; /* Restore this */
1915 return 0; /* Success ... */
1920 * Routine to rename() a file
1924 smbc_rename_ctx(SMBCCTX *ocontext,
1942 struct cli_state *targetcli1;
1943 struct cli_state *targetcli2;
1944 SMBCSRV *srv = NULL;
1945 TALLOC_CTX *frame = talloc_stackframe();
1947 if (!ocontext || !ncontext ||
1948 !ocontext->internal || !ncontext->internal ||
1949 !ocontext->internal->_initialized ||
1950 !ncontext->internal->_initialized) {
1951 errno = EINVAL; /* Best I can think of ... */
1956 if (!oname || !nname) {
1962 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1964 if (smbc_parse_path(frame,
1979 if (!user1 || user1[0] == (char)0) {
1980 user1 = talloc_strdup(frame, ocontext->user);
1988 if (smbc_parse_path(frame,
2003 if (!user2 || user2[0] == (char)0) {
2004 user2 = talloc_strdup(frame, ncontext->user);
2012 if (strcmp(server1, server2) || strcmp(share1, share2) ||
2013 strcmp(user1, user2)) {
2014 /* Can't rename across file systems, or users?? */
2020 srv = smbc_server(ocontext, True,
2021 server1, share1, workgroup, user1, password1);
2028 /*d_printf(">>>rename: resolving %s\n", path1);*/
2029 if (!cli_resolve_path(frame, "", srv->cli, path1,
2030 &targetcli1, &targetpath1)) {
2031 d_printf("Could not resolve %s\n", path1);
2035 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2036 /*d_printf(">>>rename: resolving %s\n", path2);*/
2037 if (!cli_resolve_path(frame, "", srv->cli, path2,
2038 &targetcli2, &targetpath2)) {
2039 d_printf("Could not resolve %s\n", path2);
2043 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2045 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
2046 strcmp(targetcli1->share, targetcli2->share))
2048 /* can't rename across file systems */
2054 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
2055 int eno = smbc_errno(ocontext, targetcli1);
2057 if (eno != EEXIST ||
2058 !cli_unlink(targetcli1, targetpath2) ||
2059 !cli_rename(targetcli1, targetpath1, targetpath2)) {
2069 return 0; /* Success */
2073 * A routine to lseek() a file
2077 smbc_lseek_ctx(SMBCCTX *context,
2083 char *server, *share, *user, *password;
2086 struct cli_state *targetcli;
2087 TALLOC_CTX *frame = talloc_stackframe();
2089 if (!context || !context->internal ||
2090 !context->internal->_initialized) {
2096 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2108 return -1; /* Can't lseek a dir ... */
2114 file->offset = offset;
2118 file->offset += offset;
2122 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
2123 if (smbc_parse_path(frame,
2138 /*d_printf(">>>lseek: resolving %s\n", path);*/
2139 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2140 &targetcli, &targetpath)) {
2141 d_printf("Could not resolve %s\n", path);
2145 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
2147 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
2148 &size, NULL, NULL, NULL, NULL, NULL))
2150 SMB_OFF_T b_size = size;
2151 if (!cli_getattrE(targetcli, file->cli_fd,
2152 NULL, &b_size, NULL, NULL, NULL))
2160 file->offset = size + offset;
2170 return file->offset;
2175 * Generate an inode number from file name for those things that need it
2179 smbc_inode(SMBCCTX *context,
2182 if (!context || !context->internal ||
2183 !context->internal->_initialized) {
2190 if (!*name) return 2; /* FIXME, why 2 ??? */
2191 return (ino_t)str_checksum(name);
2196 * Routine to put basic stat info into a stat structure ... Used by stat and
2201 smbc_setup_stat(SMBCCTX *context,
2207 TALLOC_CTX *frame = talloc_stackframe();
2211 if (IS_DOS_DIR(mode)) {
2212 st->st_mode = SMBC_DIR_MODE;
2214 st->st_mode = SMBC_FILE_MODE;
2217 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2218 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2219 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2220 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2223 #ifdef HAVE_STAT_ST_BLKSIZE
2224 st->st_blksize = 512;
2226 #ifdef HAVE_STAT_ST_BLOCKS
2227 st->st_blocks = (size+511)/512;
2229 st->st_uid = getuid();
2230 st->st_gid = getgid();
2232 if (IS_DOS_DIR(mode)) {
2238 if (st->st_ino == 0) {
2239 st->st_ino = smbc_inode(context, fname);
2243 return True; /* FIXME: Is this needed ? */
2248 * Routine to stat a file given a name
2252 smbc_stat_ctx(SMBCCTX *context,
2263 struct timespec write_time_ts;
2264 struct timespec access_time_ts;
2265 struct timespec change_time_ts;
2269 TALLOC_CTX *frame = talloc_stackframe();
2271 if (!context || !context->internal ||
2272 !context->internal->_initialized) {
2274 errno = EINVAL; /* Best I can think of ... */
2285 DEBUG(4, ("smbc_stat(%s)\n", fname));
2287 if (smbc_parse_path(frame,
2302 if (!user || user[0] == (char)0) {
2303 user = talloc_strdup(frame,context->user);
2311 srv = smbc_server(context, True,
2312 server, share, workgroup, user, password);
2316 return -1; /* errno set by smbc_server */
2319 if (!smbc_getatr(context, srv, path, &mode, &size,
2325 errno = smbc_errno(context, srv->cli);
2332 smbc_setup_stat(context, st, path, size, mode);
2334 set_atimespec(st, access_time_ts);
2335 set_ctimespec(st, change_time_ts);
2336 set_mtimespec(st, write_time_ts);
2337 st->st_dev = srv->dev;
2345 * Routine to stat a file given an fd
2349 smbc_fstat_ctx(SMBCCTX *context,
2353 struct timespec change_time_ts;
2354 struct timespec access_time_ts;
2355 struct timespec write_time_ts;
2364 struct cli_state *targetcli;
2366 TALLOC_CTX *frame = talloc_stackframe();
2368 if (!context || !context->internal ||
2369 !context->internal->_initialized) {
2375 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2383 return (context->fstatdir)(context, file, st);
2386 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2387 if (smbc_parse_path(frame,
2402 /*d_printf(">>>fstat: resolving %s\n", path);*/
2403 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2404 &targetcli, &targetpath)) {
2405 d_printf("Could not resolve %s\n", path);
2409 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2411 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2418 time_t change_time, access_time, write_time;
2420 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2421 &change_time, &access_time, &write_time)) {
2428 change_time_ts = convert_time_t_to_timespec(change_time);
2429 access_time_ts = convert_time_t_to_timespec(access_time);
2430 write_time_ts = convert_time_t_to_timespec(write_time);
2435 smbc_setup_stat(context, st, file->fname, size, mode);
2437 set_atimespec(st, access_time_ts);
2438 set_ctimespec(st, change_time_ts);
2439 set_mtimespec(st, write_time_ts);
2440 st->st_dev = file->srv->dev;
2448 * Routine to open a directory
2449 * We accept the URL syntax explained in smbc_parse_path(), above.
2453 smbc_remove_dir(SMBCFILE *dir)
2455 struct smbc_dir_list *d,*f;
2462 SAFE_FREE(f->dirent);
2467 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2472 add_dirent(SMBCFILE *dir,
2474 const char *comment,
2477 struct smbc_dirent *dirent;
2479 int name_length = (name == NULL ? 0 : strlen(name));
2480 int comment_len = (comment == NULL ? 0 : strlen(comment));
2483 * Allocate space for the dirent, which must be increased by the
2484 * size of the name and the comment and 1 each for the null terminator.
2487 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2489 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2493 dir->dir_error = ENOMEM;
2498 ZERO_STRUCTP(dirent);
2500 if (dir->dir_list == NULL) {
2502 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2503 if (!dir->dir_list) {
2506 dir->dir_error = ENOMEM;
2510 ZERO_STRUCTP(dir->dir_list);
2512 dir->dir_end = dir->dir_next = dir->dir_list;
2516 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2518 if (!dir->dir_end->next) {
2521 dir->dir_error = ENOMEM;
2525 ZERO_STRUCTP(dir->dir_end->next);
2527 dir->dir_end = dir->dir_end->next;
2530 dir->dir_end->next = NULL;
2531 dir->dir_end->dirent = dirent;
2533 dirent->smbc_type = type;
2534 dirent->namelen = name_length;
2535 dirent->commentlen = comment_len;
2536 dirent->dirlen = size;
2539 * dirent->namelen + 1 includes the null (no null termination needed)
2540 * Ditto for dirent->commentlen.
2541 * The space for the two null bytes was allocated.
2543 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2544 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2545 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2552 list_unique_wg_fn(const char *name,
2554 const char *comment,
2557 SMBCFILE *dir = (SMBCFILE *)state;
2558 struct smbc_dir_list *dir_list;
2559 struct smbc_dirent *dirent;
2563 dirent_type = dir->dir_type;
2565 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2567 /* An error occurred, what do we do? */
2568 /* FIXME: Add some code here */
2571 /* Point to the one just added */
2572 dirent = dir->dir_end->dirent;
2574 /* See if this was a duplicate */
2575 for (dir_list = dir->dir_list;
2576 dir_list != dir->dir_end;
2577 dir_list = dir_list->next) {
2579 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2580 /* Duplicate. End end of list need to be removed. */
2584 if (do_remove && dir_list->next == dir->dir_end) {
2585 /* Found the end of the list. Remove it. */
2586 dir->dir_end = dir_list;
2587 free(dir_list->next);
2589 dir_list->next = NULL;
2596 list_fn(const char *name,
2598 const char *comment,
2601 SMBCFILE *dir = (SMBCFILE *)state;
2605 * We need to process the type a little ...
2607 * Disk share = 0x00000000
2608 * Print share = 0x00000001
2609 * Comms share = 0x00000002 (obsolete?)
2610 * IPC$ share = 0x00000003
2612 * administrative shares:
2613 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2616 if (dir->dir_type == SMBC_FILE_SHARE) {
2618 case 0 | 0x80000000:
2620 dirent_type = SMBC_FILE_SHARE;
2624 dirent_type = SMBC_PRINTER_SHARE;
2628 dirent_type = SMBC_COMMS_SHARE;
2631 case 3 | 0x80000000:
2633 dirent_type = SMBC_IPC_SHARE;
2637 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2642 dirent_type = dir->dir_type;
2645 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2647 /* An error occurred, what do we do? */
2648 /* FIXME: Add some code here */
2654 dir_list_fn(const char *mnt,
2660 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2661 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2663 /* Handle an error ... */
2665 /* FIXME: Add some code ... */
2672 net_share_enum_rpc(struct cli_state *cli,
2673 void (*fn)(const char *name,
2675 const char *comment,
2682 uint32 info_level = 1;
2683 uint32 preferred_len = 0xffffffff;
2685 SRV_SHARE_INFO_CTR ctr;
2687 fstring comment = "";
2688 struct rpc_pipe_client *pipe_hnd;
2691 /* Open the server service pipe */
2692 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2694 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2698 /* Issue the NetShareEnum RPC call and retrieve the response */
2699 init_enum_hnd(&enum_hnd, 0);
2700 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2707 /* Was it successful? */
2708 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2709 /* Nope. Go clean up. */
2713 /* For each returned entry... */
2714 for (i = 0; i < ctr.num_entries; i++) {
2716 /* pull out the share name */
2717 rpcstr_pull_unistr2_fstring(
2718 name, &ctr.share.info1[i].info_1_str.uni_netname);
2720 /* pull out the share's comment */
2721 rpcstr_pull_unistr2_fstring(
2722 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2724 /* Get the type value */
2725 type = ctr.share.info1[i].info_1.type;
2727 /* Add this share to the list */
2728 (*fn)(name, type, comment, state);
2732 /* Close the server service pipe */
2733 cli_rpc_pipe_close(pipe_hnd);
2735 /* Tell 'em if it worked */
2736 return W_ERROR_IS_OK(result) ? 0 : -1;
2742 smbc_opendir_ctx(SMBCCTX *context,
2746 char *server, *share, *user, *password, *options;
2751 SMBCSRV *srv = NULL;
2752 SMBCFILE *dir = NULL;
2753 struct _smbc_callbacks *cb;
2754 struct sockaddr_storage rem_ss;
2755 TALLOC_CTX *frame = talloc_stackframe();
2757 if (!context || !context->internal ||
2758 !context->internal->_initialized) {
2759 DEBUG(4, ("no valid context\n"));
2760 errno = EINVAL + 8192;
2767 DEBUG(4, ("no valid fname\n"));
2768 errno = EINVAL + 8193;
2773 if (smbc_parse_path(frame,
2783 DEBUG(4, ("no valid path\n"));
2784 errno = EINVAL + 8194;
2789 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2790 "path='%s' options='%s'\n",
2791 fname, server, share, path, options));
2793 /* Ensure the options are valid */
2794 if (smbc_check_options(server, share, path, options)) {
2795 DEBUG(4, ("unacceptable options (%s)\n", options));
2796 errno = EINVAL + 8195;
2801 if (!user || user[0] == (char)0) {
2802 user = talloc_strdup(frame, context->user);
2810 dir = SMB_MALLOC_P(SMBCFILE);
2821 dir->fname = SMB_STRDUP(fname);
2825 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2827 if (server[0] == (char)0) {
2832 struct ip_service *ip_list;
2833 struct ip_service server_addr;
2834 struct user_auth_info u_info;
2836 if (share[0] != (char)0 || path[0] != (char)0) {
2838 errno = EINVAL + 8196;
2840 SAFE_FREE(dir->fname);
2847 /* Determine how many local master browsers to query */
2848 max_lmb_count = (context->options.browse_max_lmb_count == 0
2850 : context->options.browse_max_lmb_count);
2852 memset(&u_info, '\0', sizeof(u_info));
2853 u_info.username = talloc_strdup(frame,user);
2854 u_info.password = talloc_strdup(frame,password);
2855 if (!u_info.username || !u_info.password) {
2857 SAFE_FREE(dir->fname);
2865 * We have server and share and path empty but options
2866 * requesting that we scan all master browsers for their list
2867 * of workgroups/domains. This implies that we must first try
2868 * broadcast queries to find all master browsers, and if that
2869 * doesn't work, then try our other methods which return only
2870 * a single master browser.
2874 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
2880 if (!find_master_ip(workgroup, &server_addr.ss)) {
2883 SAFE_FREE(dir->fname);
2891 ip_list = (struct ip_service *)memdup(
2892 &server_addr, sizeof(server_addr));
2893 if (ip_list == NULL) {
2901 for (i = 0; i < count && i < max_lmb_count; i++) {
2902 char addr[INET6_ADDRSTRLEN];
2903 char *wg_ptr = NULL;
2904 struct cli_state *cli = NULL;
2906 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
2907 DEBUG(99, ("Found master browser %d of %d: %s\n",
2908 i+1, MAX(count, max_lmb_count),
2911 cli = get_ipc_connect_master_ip(talloc_tos(),
2915 /* cli == NULL is the master browser refused to talk or
2916 could not be found */
2921 workgroup = talloc_strdup(frame, wg_ptr);
2922 server = talloc_strdup(frame, cli->desthost);
2926 if (!workgroup || !server) {
2932 DEBUG(4, ("using workgroup %s %s\n",
2933 workgroup, server));
2936 * For each returned master browser IP address, get a
2937 * connection to IPC$ on the server if we do not
2938 * already have one, and determine the
2939 * workgroups/domains that it knows about.
2942 srv = smbc_server(context, True, server, "IPC$",
2943 workgroup, user, password);
2949 dir->dir_type = SMBC_WORKGROUP;
2951 /* Now, list the stuff ... */
2953 if (!cli_NetServerEnum(srv->cli,
2955 SV_TYPE_DOMAIN_ENUM,
2965 * Server not an empty string ... Check the rest and see what
2968 if (*share == '\0') {
2969 if (*path != '\0') {
2971 /* Should not have empty share with path */
2972 errno = EINVAL + 8197;
2974 SAFE_FREE(dir->fname);
2983 * We don't know if <server> is really a server name
2984 * or is a workgroup/domain name. If we already have
2985 * a server structure for it, we'll use it.
2986 * Otherwise, check to see if <server><1D>,
2987 * <server><1B>, or <server><20> translates. We check
2988 * to see if <server> is an IP address first.
2992 * See if we have an existing server. Do not
2993 * establish a connection if one does not already
2996 srv = smbc_server(context, False, server, "IPC$",
2997 workgroup, user, password);
3000 * If no existing server and not an IP addr, look for
3004 !is_ipaddress(server) &&
3005 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
3006 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
3010 dir->dir_type = SMBC_SERVER;
3013 * Get the backup list ...
3015 if (!name_status_find(server, 0, 0,
3016 &rem_ss, buserver)) {
3018 DEBUG(0, ("Could not get name of "
3019 "local/domain master browser "
3020 "for server %s\n", server));
3022 SAFE_FREE(dir->fname);
3032 * Get a connection to IPC$ on the server if
3033 * we do not already have one
3035 srv = smbc_server(context, True,
3037 workgroup, user, password);
3039 DEBUG(0, ("got no contact to IPC$\n"));
3041 SAFE_FREE(dir->fname);
3051 /* Now, list the servers ... */
3052 if (!cli_NetServerEnum(srv->cli, server,
3053 0x0000FFFE, list_fn,
3057 SAFE_FREE(dir->fname);
3064 (resolve_name(server, &rem_ss, 0x20))) {
3066 /* If we hadn't found the server, get one now */
3068 srv = smbc_server(context, True,
3076 SAFE_FREE(dir->fname);
3084 dir->dir_type = SMBC_FILE_SHARE;
3087 /* List the shares ... */
3089 if (net_share_enum_rpc(
3092 (void *) dir) < 0 &&
3098 errno = cli_errno(srv->cli);
3100 SAFE_FREE(dir->fname);