2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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')
137 * Convert strings of %xx to their single character equivalent. Each 'x' must
138 * be a valid hexadecimal digit, or that % sequence is left undecoded.
140 * dest may, but need not be, the same pointer as src.
142 * Returns the number of % sequences which could not be converted due to lack
143 * of two following hexadecimal digits.
146 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
148 int old_length = strlen(src);
154 if ( old_length == 0 ) {
159 while ( i < old_length ) {
160 unsigned char character = src[ i++ ];
162 if (character == '%') {
163 int a = i+1 < old_length ? hex2int( src[i] ) : -1;
164 int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
166 /* Replace valid sequence */
167 if (a != -1 && b != -1) {
169 /* Replace valid %xx sequence with %dd */
170 character = (a * 16) + b;
172 if (character == '\0') {
173 break; /* Stop at %00 */
188 strncpy(dest, temp, max_dest_len - 1);
189 dest[max_dest_len - 1] = '\0';
197 * Convert any characters not specifically allowed in a URL into their %xx
200 * Returns the remaining buffer length.
203 smbc_urlencode(char * dest, char * src, int max_dest_len)
205 char hex[] = "0123456789ABCDEF";
207 for (; *src != '\0' && max_dest_len >= 3; src++) {
219 *dest++ = hex[(*src >> 4) & 0x0f];
220 *dest++ = hex[*src & 0x0f];
235 * Function to parse a path and turn it into components
237 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
238 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
239 * general format ("smb:" only; we do not look for "cifs:").
243 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
247 * smb:// Show all workgroups.
249 * The method of locating the list of workgroups varies
250 * depending upon the setting of the context variable
251 * context->options.browse_max_lmb_count. This value
252 * determine the maximum number of local master browsers to
253 * query for the list of workgroups. In order to ensure that
254 * a complete list of workgroups is obtained, all master
255 * browsers must be queried, but if there are many
256 * workgroups, the time spent querying can begin to add up.
257 * For small networks (not many workgroups), it is suggested
258 * that this variable be set to 0, indicating query all local
259 * master browsers. When the network has many workgroups, a
260 * reasonable setting for this variable might be around 3.
262 * smb://name/ if name<1D> or name<1B> exists, list servers in
263 * workgroup, else, if name<20> exists, list all shares
266 * If "options" are provided, this function returns the entire option list as a
267 * string, for later parsing by the caller. Note that currently, no options
271 static const char *smbc_prefix = "smb:";
274 smbc_parse_path(SMBCCTX *context,
276 char *workgroup, int workgroup_len,
277 char *server, int server_len,
278 char *share, int share_len,
279 char *path, int path_len,
280 char *user, int user_len,
281 char *password, int password_len,
282 char *options, int options_len)
290 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
293 * Assume we wont find an authentication domain to parse, so default
294 * to the workgroup in the provided context.
296 if (workgroup != NULL) {
297 strncpy(workgroup, context->workgroup, workgroup_len - 1);
298 workgroup[workgroup_len - 1] = '\0';
301 if (options != NULL && options_len > 0) {
302 options[0] = (char)0;
306 /* see if it has the right prefix */
307 len = strlen(smbc_prefix);
308 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
309 return -1; /* What about no smb: ? */
314 /* Watch the test below, we are testing to see if we should exit */
316 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
318 DEBUG(1, ("Invalid path (does not begin with smb://"));
323 p += 2; /* Skip the double slash */
325 /* See if any options were specified */
326 if ((q = strrchr(p, '?')) != NULL ) {
327 /* There are options. Null terminate here and point to them */
330 DEBUG(4, ("Found options '%s'", q));
332 /* Copy the options */
333 if (options != NULL && options_len > 0) {
334 safe_strcpy(options, q, options_len - 1);
342 int wl = strlen(context->workgroup);
348 strncpy(server, context->workgroup, wl);
354 * ok, its for us. Now parse out the server, share etc.
356 * However, we want to parse out [[domain;]user[:password]@] if it
360 /* check that '@' occurs before '/', if '/' exists at all */
361 q = strchr_m(p, '@');
362 r = strchr_m(p, '/');
363 if (q && (!r || q < r)) {
364 pstring username, passwd, domain;
365 const char *u = userinfo;
367 next_token_no_ltrim(&p, userinfo, "@", sizeof(fstring));
369 username[0] = passwd[0] = domain[0] = 0;
371 if (strchr_m(u, ';')) {
373 next_token_no_ltrim(&u, domain, ";", sizeof(fstring));
377 if (strchr_m(u, ':')) {
379 next_token_no_ltrim(&u, username, ":", sizeof(fstring));
386 pstrcpy(username, u);
390 if (domain[0] && workgroup) {
391 strncpy(workgroup, domain, workgroup_len - 1);
392 workgroup[workgroup_len - 1] = '\0';
396 strncpy(user, username, user_len - 1);
397 user[user_len - 1] = '\0';
401 strncpy(password, passwd, password_len - 1);
402 password[password_len - 1] = '\0';
407 if (!next_token(&p, server, "/", sizeof(fstring))) {
413 if (*p == (char)0) goto decoding; /* That's it ... */
415 if (!next_token(&p, share, "/", sizeof(fstring))) {
422 * Prepend a leading slash if there's a file path, as required by
428 safe_strcpy(path + 1, p, path_len - 2);
431 all_string_sub(path, "/", "\\", 0);
434 (void) smbc_urldecode(path, path, path_len);
435 (void) smbc_urldecode(server, server, server_len);
436 (void) smbc_urldecode(share, share, share_len);
437 (void) smbc_urldecode(user, user, user_len);
438 (void) smbc_urldecode(password, password, password_len);
444 * Verify that the options specified in a URL are valid
447 smbc_check_options(char *server,
452 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
453 "path='%s' options='%s'\n",
454 server, share, path, options));
456 /* No options at all is always ok */
457 if (! *options) return 0;
459 /* Currently, we don't support any options. */
464 * Convert an SMB error into a UNIX error ...
467 smbc_errno(SMBCCTX *context,
470 int ret = cli_errno(c);
472 if (cli_is_dos_error(c)) {
476 cli_dos_error(c, &eclass, &ecode);
478 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
479 (int)eclass, (int)ecode, (int)ecode, ret));
483 status = cli_nt_error(c);
485 DEBUG(3,("smbc errno %s -> %d\n",
486 nt_errstr(status), ret));
493 * Check a server for being alive and well.
494 * returns 0 if the server is in shape. Returns 1 on error
496 * Also useable outside libsmbclient to enable external cache
497 * to do some checks too.
500 smbc_check_server(SMBCCTX * context,
504 struct sockaddr addr;
507 * Although the use of port 139 is not a guarantee that we're using
508 * netbios, we assume so. We don't want to send a keepalive packet if
509 * not netbios because it's not valid, and Vista, at least,
510 * disconnects the client on such a request.
512 if (server->cli->port == 139) {
513 /* Assuming netbios. Send a keepalive packet */
514 if ( send_keepalive(server->cli->fd) == False ) {
519 * Assuming not netbios. Try a different method to detect if
520 * the connection is still alive.
523 if (getpeername(server->cli->fd, &addr, &size) == -1) {
528 /* connection is ok */
533 * Remove a server from the cached server list it's unused.
534 * On success, 0 is returned. 1 is returned if the server could not be removed.
536 * Also useable outside libsmbclient
539 smbc_remove_unused_server(SMBCCTX * context,
544 /* are we being fooled ? */
545 if (!context || !context->internal ||
546 !context->internal->_initialized || !srv) return 1;
549 /* Check all open files/directories for a relation with this server */
550 for (file = context->internal->_files; file; file=file->next) {
551 if (file->srv == srv) {
553 DEBUG(3, ("smbc_remove_usused_server: "
554 "%p still used by %p.\n",
560 DLIST_REMOVE(context->internal->_servers, srv);
562 cli_shutdown(srv->cli);
565 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
567 context->callbacks.remove_cached_srv_fn(context, srv);
575 find_server(SMBCCTX *context,
587 srv = context->callbacks.get_cached_srv_fn(context, server, share,
588 workgroup, username);
590 if (!auth_called && !srv && (!username[0] || !password[0])) {
591 if (context->internal->_auth_fn_with_context != NULL) {
592 context->internal->_auth_fn_with_context(
595 workgroup, sizeof(fstring),
596 username, sizeof(fstring),
597 password, sizeof(fstring));
599 context->callbacks.auth_fn(
601 workgroup, sizeof(fstring),
602 username, sizeof(fstring),
603 password, sizeof(fstring));
607 * However, smbc_auth_fn may have picked up info relating to
608 * an existing connection, so try for an existing connection
612 goto check_server_cache;
617 if (context->callbacks.check_server_fn(context, srv)) {
619 * This server is no good anymore
620 * Try to remove it and check for more possible
621 * servers in the cache
623 if (context->callbacks.remove_unused_server_fn(context,
626 * We could not remove the server completely,
627 * remove it from the cache so we will not get
628 * it again. It will be removed when the last
629 * file/dir is closed.
631 context->callbacks.remove_cached_srv_fn(context,
636 * Maybe there are more cached connections to this
639 goto check_server_cache;
649 * Connect to a server, possibly on an existing connection
651 * Here, what we want to do is: If the server and username
652 * match an existing connection, reuse that, otherwise, establish a
655 * If we have to create a new connection, call the auth_fn to get the
656 * info we need, unless the username and password were passed in.
660 smbc_server(SMBCCTX *context,
661 BOOL connect_if_not_found,
670 struct nmb_name called, calling;
671 const char *server_n = server;
674 int tried_reverse = 0;
677 const char *username_used;
682 if (server[0] == 0) {
687 /* Look for a cached connection */
688 srv = find_server(context, server, share,
689 workgroup, username, password);
692 * If we found a connection and we're only allowed one share per
695 if (srv && *share != '\0' && context->options.one_share_per_server) {
698 * ... then if there's no current connection to the share,
699 * connect to it. find_server(), or rather the function
700 * pointed to by context->callbacks.get_cached_srv_fn which
701 * was called by find_server(), will have issued a tree
702 * disconnect if the requested share is not the same as the
703 * one that was already connected.
705 if (srv->cli->cnum == (uint16) -1) {
706 /* Ensure we have accurate auth info */
707 if (context->internal->_auth_fn_with_context != NULL) {
708 context->internal->_auth_fn_with_context(
711 workgroup, sizeof(fstring),
712 username, sizeof(fstring),
713 password, sizeof(fstring));
715 context->callbacks.auth_fn(
717 workgroup, sizeof(fstring),
718 username, sizeof(fstring),
719 password, sizeof(fstring));
722 if (! cli_send_tconX(srv->cli, share, "?????",
723 password, strlen(password)+1)) {
725 errno = smbc_errno(context, srv->cli);
726 cli_shutdown(srv->cli);
728 context->callbacks.remove_cached_srv_fn(context,
734 * Regenerate the dev value since it's based on both
738 srv->dev = (dev_t)(str_checksum(server) ^
739 str_checksum(share));
744 /* If we have a connection... */
747 /* ... then we're done here. Give 'em what they came for. */
751 /* If we're not asked to connect when a connection doesn't exist... */
752 if (! connect_if_not_found) {
753 /* ... then we're done here. */
757 make_nmb_name(&calling, context->netbios_name, 0x0);
758 make_nmb_name(&called , server, 0x20);
760 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
762 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
765 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
769 /* have to open a new connection */
770 if ((c = cli_initialise()) == NULL) {
775 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
776 c->use_kerberos = True;
778 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
779 c->fallback_after_kerberos = True;
782 c->timeout = context->timeout;
785 * Force use of port 139 for first try if share is $IPC, empty, or
786 * null, so browse lists can work
788 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
789 port_try_first = 139;
792 port_try_first = 445;
796 c->port = port_try_first;
798 if (!cli_connect(c, server_n, &ip)) {
800 /* First connection attempt failed. Try alternate port. */
801 c->port = port_try_next;
803 if (!cli_connect(c, server_n, &ip)) {
810 if (!cli_session_request(c, &calling, &called)) {
812 if (strcmp(called.name, "*SMBSERVER")) {
813 make_nmb_name(&called , "*SMBSERVER", 0x20);
815 } else { /* Try one more time, but ensure we don't loop */
817 /* Only try this if server is an IP address ... */
819 if (is_ipaddress(server) && !tried_reverse) {
821 struct in_addr rem_ip;
823 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
824 DEBUG(4, ("Could not convert IP address "
825 "%s to struct in_addr\n", server));
830 tried_reverse++; /* Yuck */
832 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
833 make_nmb_name(&called, remote_name, 0x20);
842 DEBUG(4,(" session request ok\n"));
844 if (!cli_negprot(c)) {
850 username_used = username;
852 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
853 password, strlen(password),
854 password, strlen(password),
857 /* Failed. Try an anonymous login, if allowed by flags. */
860 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
861 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
872 DEBUG(4,(" session setup ok\n"));
874 if (!cli_send_tconX(c, share, "?????",
875 password, strlen(password)+1)) {
876 errno = smbc_errno(context, c);
881 DEBUG(4,(" tconx ok\n"));
884 * Ok, we have got a nice connection
885 * Let's allocate a server structure.
888 srv = SMB_MALLOC_P(SMBCSRV);
896 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
897 srv->no_pathinfo = False;
898 srv->no_pathinfo2 = False;
899 srv->no_nt_session = False;
901 /* now add it to the cache (internal or external) */
902 /* Let the cache function set errno if it wants to */
904 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
905 int saved_errno = errno;
906 DEBUG(3, (" Failed to add server to cache\n"));
914 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
915 server, share, srv));
917 DLIST_ADD(context->internal->_servers, srv);
931 * Connect to a server for getting/setting attributes, possibly on an existing
932 * connection. This works similarly to smbc_server().
935 smbc_attr_server(SMBCCTX *context,
945 struct cli_state *ipc_cli;
946 struct rpc_pipe_client *pipe_hnd;
948 SMBCSRV *ipc_srv=NULL;
951 * See if we've already created this special connection. Reference
952 * our "special" share name '*IPC$', which is an impossible real share
953 * name due to the leading asterisk.
955 ipc_srv = find_server(context, server, "*IPC$",
956 workgroup, username, password);
959 /* We didn't find a cached connection. Get the password */
960 if (*password == '\0') {
961 /* ... then retrieve it now. */
962 if (context->internal->_auth_fn_with_context != NULL) {
963 context->internal->_auth_fn_with_context(
966 workgroup, sizeof(fstring),
967 username, sizeof(fstring),
968 password, sizeof(fstring));
970 context->callbacks.auth_fn(
972 workgroup, sizeof(fstring),
973 username, sizeof(fstring),
974 password, sizeof(fstring));
979 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
980 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
984 nt_status = cli_full_connection(&ipc_cli,
985 global_myname(), server,
986 &ip, 0, "IPC$", "?????",
990 if (! NT_STATUS_IS_OK(nt_status)) {
991 DEBUG(1,("cli_full_connection failed! (%s)\n",
992 nt_errstr(nt_status)));
997 ipc_srv = SMB_MALLOC_P(SMBCSRV);
1000 cli_shutdown(ipc_cli);
1004 ZERO_STRUCTP(ipc_srv);
1005 ipc_srv->cli = ipc_cli;
1008 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1012 DEBUG(1, ("cli_nt_session_open fail!\n"));
1014 cli_shutdown(ipc_srv->cli);
1020 * Some systems don't support
1021 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1022 * so we might as well do it too.
1025 nt_status = rpccli_lsa_open_policy(
1027 ipc_srv->cli->mem_ctx,
1029 GENERIC_EXECUTE_ACCESS,
1032 if (!NT_STATUS_IS_OK(nt_status)) {
1033 errno = smbc_errno(context, ipc_srv->cli);
1034 cli_shutdown(ipc_srv->cli);
1039 /* now add it to the cache (internal or external) */
1041 errno = 0; /* let cache function set errno if it likes */
1042 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
1047 DEBUG(3, (" Failed to add server to cache\n"));
1051 cli_shutdown(ipc_srv->cli);
1056 DLIST_ADD(context->internal->_servers, ipc_srv);
1063 * Routine to open() a file ...
1067 smbc_open_ctx(SMBCCTX *context,
1072 fstring server, share, user, password, workgroup;
1075 struct cli_state *targetcli;
1076 SMBCSRV *srv = NULL;
1077 SMBCFILE *file = NULL;
1080 if (!context || !context->internal ||
1081 !context->internal->_initialized) {
1083 errno = EINVAL; /* Best I can think of ... */
1095 if (smbc_parse_path(context, fname,
1096 workgroup, sizeof(workgroup),
1097 server, sizeof(server),
1098 share, sizeof(share),
1101 password, sizeof(password),
1107 if (user[0] == (char)0) fstrcpy(user, context->user);
1109 srv = smbc_server(context, True,
1110 server, share, workgroup, user, password);
1114 if (errno == EPERM) errno = EACCES;
1115 return NULL; /* smbc_server sets errno */
1119 /* Hmmm, the test for a directory is suspect here ... FIXME */
1121 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1128 file = SMB_MALLOC_P(SMBCFILE);
1139 /*d_printf(">>>open: resolving %s\n", path);*/
1140 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1142 d_printf("Could not resolve %s\n", path);
1146 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1148 if ( targetcli->dfsroot )
1151 pstrcpy(temppath, targetpath);
1152 cli_dfs_make_full_path( targetpath, targetcli->desthost, targetcli->share, temppath);
1155 if ((fd = cli_open(targetcli, targetpath, flags,
1156 context->internal->_share_mode)) < 0) {
1158 /* Handle the error ... */
1161 errno = smbc_errno(context, targetcli);
1166 /* Fill in file struct */
1169 file->fname = SMB_STRDUP(fname);
1174 DLIST_ADD(context->internal->_files, file);
1177 * If the file was opened in O_APPEND mode, all write
1178 * operations should be appended to the file. To do that,
1179 * though, using this protocol, would require a getattrE()
1180 * call for each and every write, to determine where the end
1181 * of the file is. (There does not appear to be an append flag
1182 * in the protocol.) Rather than add all of that overhead of
1183 * retrieving the current end-of-file offset prior to each
1184 * write operation, we'll assume that most append operations
1185 * will continuously write, so we'll just set the offset to
1186 * the end of the file now and hope that's adequate.
1188 * Note to self: If this proves inadequate, and O_APPEND
1189 * should, in some cases, be forced for each write, add a
1190 * field in the context options structure, for
1191 * "strict_append_mode" which would select between the current
1192 * behavior (if FALSE) or issuing a getattrE() prior to each
1193 * write and forcing the write to the end of the file (if
1194 * TRUE). Adding that capability will likely require adding
1195 * an "append" flag into the _SMBCFILE structure to track
1196 * whether a file was opened in O_APPEND mode. -- djl
1198 if (flags & O_APPEND) {
1199 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1200 (void) smbc_close_ctx(context, file);
1210 /* Check if opendir needed ... */
1215 eno = smbc_errno(context, srv->cli);
1216 file = context->opendir(context, fname);
1217 if (!file) errno = eno;
1222 errno = EINVAL; /* FIXME, correct errno ? */
1228 * Routine to create a file
1231 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1234 smbc_creat_ctx(SMBCCTX *context,
1239 if (!context || !context->internal ||
1240 !context->internal->_initialized) {
1247 return smbc_open_ctx(context, path, creat_bits, mode);
1251 * Routine to read() a file ...
1255 smbc_read_ctx(SMBCCTX *context,
1261 fstring server, share, user, password;
1262 pstring path, targetpath;
1263 struct cli_state *targetcli;
1268 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1269 * appears to pass file->offset (which is type off_t) differently than
1270 * a local variable of type off_t. Using local variable "offset" in
1271 * the call to cli_read() instead of file->offset fixes a problem
1272 * retrieving data at an offset greater than 4GB.
1276 if (!context || !context->internal ||
1277 !context->internal->_initialized) {
1284 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1286 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1293 offset = file->offset;
1295 /* Check that the buffer exists ... */
1304 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1305 if (smbc_parse_path(context, file->fname,
1307 server, sizeof(server),
1308 share, sizeof(share),
1311 password, sizeof(password),
1317 /*d_printf(">>>read: resolving %s\n", path);*/
1318 if (!cli_resolve_path("", file->srv->cli, path,
1319 &targetcli, targetpath))
1321 d_printf("Could not resolve %s\n", path);
1324 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1326 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1330 errno = smbc_errno(context, targetcli);
1335 file->offset += ret;
1337 DEBUG(4, (" --> %d\n", ret));
1339 return ret; /* Success, ret bytes of data ... */
1344 * Routine to write() a file ...
1348 smbc_write_ctx(SMBCCTX *context,
1355 fstring server, share, user, password;
1356 pstring path, targetpath;
1357 struct cli_state *targetcli;
1359 /* First check all pointers before dereferencing them */
1361 if (!context || !context->internal ||
1362 !context->internal->_initialized) {
1369 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1376 /* Check that the buffer exists ... */
1385 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1387 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1388 if (smbc_parse_path(context, file->fname,
1390 server, sizeof(server),
1391 share, sizeof(share),
1394 password, sizeof(password),
1400 /*d_printf(">>>write: resolving %s\n", path);*/
1401 if (!cli_resolve_path("", file->srv->cli, path,
1402 &targetcli, targetpath))
1404 d_printf("Could not resolve %s\n", path);
1407 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1410 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1414 errno = smbc_errno(context, targetcli);
1419 file->offset += ret;
1421 return ret; /* Success, 0 bytes of data ... */
1425 * Routine to close() a file ...
1429 smbc_close_ctx(SMBCCTX *context,
1433 fstring server, share, user, password;
1434 pstring path, targetpath;
1435 struct cli_state *targetcli;
1437 if (!context || !context->internal ||
1438 !context->internal->_initialized) {
1445 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1455 return context->closedir(context, file);
1459 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1460 if (smbc_parse_path(context, file->fname,
1462 server, sizeof(server),
1463 share, sizeof(share),
1466 password, sizeof(password),
1472 /*d_printf(">>>close: resolving %s\n", path);*/
1473 if (!cli_resolve_path("", file->srv->cli, path,
1474 &targetcli, targetpath))
1476 d_printf("Could not resolve %s\n", path);
1479 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1481 if (!cli_close(targetcli, file->cli_fd)) {
1483 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1485 /* Deallocate slot and remove the server
1486 * from the server cache if unused */
1487 errno = smbc_errno(context, targetcli);
1489 DLIST_REMOVE(context->internal->_files, file);
1490 SAFE_FREE(file->fname);
1492 context->callbacks.remove_unused_server_fn(context, srv);
1498 DLIST_REMOVE(context->internal->_files, file);
1499 SAFE_FREE(file->fname);
1506 * Get info from an SMB server on a file. Use a qpathinfo call first
1507 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1510 smbc_getatr(SMBCCTX * context,
1515 struct timespec *create_time_ts,
1516 struct timespec *access_time_ts,
1517 struct timespec *write_time_ts,
1518 struct timespec *change_time_ts,
1523 struct cli_state *targetcli;
1526 if (!context || !context->internal ||
1527 !context->internal->_initialized) {
1534 /* path fixup for . and .. */
1535 if (strequal(path, ".") || strequal(path, ".."))
1536 pstrcpy(fixedpath, "\\");
1539 pstrcpy(fixedpath, path);
1540 trim_string(fixedpath, NULL, "\\..");
1541 trim_string(fixedpath, NULL, "\\.");
1543 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1545 if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
1547 d_printf("Couldn't resolve %s\n", path);
1551 if ( targetcli->dfsroot )
1554 pstrcpy(temppath, targetpath);
1555 cli_dfs_make_full_path(targetpath, targetcli->desthost,
1556 targetcli->share, temppath);
1559 if (!srv->no_pathinfo2 &&
1560 cli_qpathinfo2(targetcli, targetpath,
1569 /* if this is NT then don't bother with the getatr */
1570 if (targetcli->capabilities & CAP_NT_SMBS) {
1575 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1577 struct timespec w_time_ts;
1579 w_time_ts = convert_time_t_to_timespec(write_time);
1581 if (write_time_ts != NULL) {
1582 *write_time_ts = w_time_ts;
1585 if (create_time_ts != NULL) {
1586 *create_time_ts = w_time_ts;
1589 if (access_time_ts != NULL) {
1590 *access_time_ts = w_time_ts;
1593 if (change_time_ts != NULL) {
1594 *change_time_ts = w_time_ts;
1597 srv->no_pathinfo2 = True;
1607 * Set file info on an SMB server. Use setpathinfo call first. If that
1608 * fails, use setattrE..
1610 * Access and modification time parameters are always used and must be
1611 * provided. Create time, if zero, will be determined from the actual create
1612 * time of the file. If non-zero, the create time will be set as well.
1614 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1617 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1628 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1629 * modern function for "new code" to be using, and it works given a
1630 * filename rather than requiring that the file be opened to have its
1631 * attributes manipulated.
1633 if (srv->no_pathinfo ||
1634 ! cli_setpathinfo(srv->cli, path,
1642 * setpathinfo is not supported; go to plan B.
1644 * cli_setatr() does not work on win98, and it also doesn't
1645 * support setting the access time (only the modification
1646 * time), so in all cases, we open the specified file and use
1647 * cli_setattrE() which should work on all OS versions, and
1648 * supports both times.
1651 /* Don't try {q,set}pathinfo() again, with this server */
1652 srv->no_pathinfo = True;
1655 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1657 errno = smbc_errno(context, srv->cli);
1661 /* Set the new attributes */
1662 ret = cli_setattrE(srv->cli, fd,
1667 /* Close the file */
1668 cli_close(srv->cli, fd);
1671 * Unfortunately, setattrE() doesn't have a provision for
1672 * setting the access mode (attributes). We'll have to try
1673 * cli_setatr() for that, and with only this parameter, it
1674 * seems to work on win98.
1676 if (ret && mode != (uint16) -1) {
1677 ret = cli_setatr(srv->cli, path, mode, 0);
1681 errno = smbc_errno(context, srv->cli);
1690 * Routine to unlink() a file
1694 smbc_unlink_ctx(SMBCCTX *context,
1697 fstring server, share, user, password, workgroup;
1698 pstring path, targetpath;
1699 struct cli_state *targetcli;
1700 SMBCSRV *srv = NULL;
1702 if (!context || !context->internal ||
1703 !context->internal->_initialized) {
1705 errno = EINVAL; /* Best I can think of ... */
1717 if (smbc_parse_path(context, fname,
1718 workgroup, sizeof(workgroup),
1719 server, sizeof(server),
1720 share, sizeof(share),
1723 password, sizeof(password),
1729 if (user[0] == (char)0) fstrcpy(user, context->user);
1731 srv = smbc_server(context, True,
1732 server, share, workgroup, user, password);
1736 return -1; /* smbc_server sets errno */
1740 /*d_printf(">>>unlink: resolving %s\n", path);*/
1741 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1743 d_printf("Could not resolve %s\n", path);
1746 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1748 if (!cli_unlink(targetcli, targetpath)) {
1750 errno = smbc_errno(context, targetcli);
1752 if (errno == EACCES) { /* Check if the file is a directory */
1757 struct timespec write_time_ts;
1758 struct timespec access_time_ts;
1759 struct timespec change_time_ts;
1762 if (!smbc_getatr(context, srv, path, &mode, &size,
1769 /* Hmmm, bad error ... What? */
1771 errno = smbc_errno(context, targetcli);
1777 if (IS_DOS_DIR(mode))
1780 errno = saverr; /* Restore this */
1789 return 0; /* Success ... */
1794 * Routine to rename() a file
1798 smbc_rename_ctx(SMBCCTX *ocontext,
1814 pstring targetpath1;
1815 pstring targetpath2;
1816 struct cli_state *targetcli1;
1817 struct cli_state *targetcli2;
1818 SMBCSRV *srv = NULL;
1820 if (!ocontext || !ncontext ||
1821 !ocontext->internal || !ncontext->internal ||
1822 !ocontext->internal->_initialized ||
1823 !ncontext->internal->_initialized) {
1825 errno = EINVAL; /* Best I can think of ... */
1830 if (!oname || !nname) {
1837 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1839 smbc_parse_path(ocontext, oname,
1840 workgroup, sizeof(workgroup),
1841 server1, sizeof(server1),
1842 share1, sizeof(share1),
1843 path1, sizeof(path1),
1844 user1, sizeof(user1),
1845 password1, sizeof(password1),
1848 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1850 smbc_parse_path(ncontext, nname,
1852 server2, sizeof(server2),
1853 share2, sizeof(share2),
1854 path2, sizeof(path2),
1855 user2, sizeof(user2),
1856 password2, sizeof(password2),
1859 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1861 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1862 strcmp(user1, user2)) {
1864 /* Can't rename across file systems, or users?? */
1871 srv = smbc_server(ocontext, True,
1872 server1, share1, workgroup, user1, password1);
1879 /*d_printf(">>>rename: resolving %s\n", path1);*/
1880 if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
1882 d_printf("Could not resolve %s\n", path1);
1885 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1886 /*d_printf(">>>rename: resolving %s\n", path2);*/
1887 if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
1889 d_printf("Could not resolve %s\n", path2);
1892 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1894 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1895 strcmp(targetcli1->share, targetcli2->share))
1897 /* can't rename across file systems */
1903 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1904 int eno = smbc_errno(ocontext, targetcli1);
1906 if (eno != EEXIST ||
1907 !cli_unlink(targetcli1, targetpath2) ||
1908 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1916 return 0; /* Success */
1921 * A routine to lseek() a file
1925 smbc_lseek_ctx(SMBCCTX *context,
1931 fstring server, share, user, password;
1932 pstring path, targetpath;
1933 struct cli_state *targetcli;
1935 if (!context || !context->internal ||
1936 !context->internal->_initialized) {
1943 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1953 return -1; /* Can't lseek a dir ... */
1959 file->offset = offset;
1963 file->offset += offset;
1967 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1968 if (smbc_parse_path(context, file->fname,
1970 server, sizeof(server),
1971 share, sizeof(share),
1974 password, sizeof(password),
1981 /*d_printf(">>>lseek: resolving %s\n", path);*/
1982 if (!cli_resolve_path("", file->srv->cli, path,
1983 &targetcli, targetpath))
1985 d_printf("Could not resolve %s\n", path);
1988 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1990 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
1991 &size, NULL, NULL, NULL, NULL, NULL))
1993 SMB_OFF_T b_size = size;
1994 if (!cli_getattrE(targetcli, file->cli_fd,
1995 NULL, &b_size, NULL, NULL, NULL))
2002 file->offset = size + offset;
2011 return file->offset;
2016 * Generate an inode number from file name for those things that need it
2020 smbc_inode(SMBCCTX *context,
2024 if (!context || !context->internal ||
2025 !context->internal->_initialized) {
2032 if (!*name) return 2; /* FIXME, why 2 ??? */
2033 return (ino_t)str_checksum(name);
2038 * Routine to put basic stat info into a stat structure ... Used by stat and
2043 smbc_setup_stat(SMBCCTX *context,
2052 if (IS_DOS_DIR(mode)) {
2053 st->st_mode = SMBC_DIR_MODE;
2055 st->st_mode = SMBC_FILE_MODE;
2058 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2059 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2060 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2061 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2064 #ifdef HAVE_STAT_ST_BLKSIZE
2065 st->st_blksize = 512;
2067 #ifdef HAVE_STAT_ST_BLOCKS
2068 st->st_blocks = (size+511)/512;
2070 st->st_uid = getuid();
2071 st->st_gid = getgid();
2073 if (IS_DOS_DIR(mode)) {
2079 if (st->st_ino == 0) {
2080 st->st_ino = smbc_inode(context, fname);
2083 return True; /* FIXME: Is this needed ? */
2088 * Routine to stat a file given a name
2092 smbc_stat_ctx(SMBCCTX *context,
2103 struct timespec write_time_ts;
2104 struct timespec access_time_ts;
2105 struct timespec change_time_ts;
2110 if (!context || !context->internal ||
2111 !context->internal->_initialized) {
2113 errno = EINVAL; /* Best I can think of ... */
2125 DEBUG(4, ("smbc_stat(%s)\n", fname));
2127 if (smbc_parse_path(context, fname,
2128 workgroup, sizeof(workgroup),
2129 server, sizeof(server),
2130 share, sizeof(share),
2133 password, sizeof(password),
2139 if (user[0] == (char)0) fstrcpy(user, context->user);
2141 srv = smbc_server(context, True,
2142 server, share, workgroup, user, password);
2145 return -1; /* errno set by smbc_server */
2148 if (!smbc_getatr(context, srv, path, &mode, &size,
2155 errno = smbc_errno(context, srv->cli);
2162 smbc_setup_stat(context, st, path, size, mode);
2164 set_atimespec(st, access_time_ts);
2165 set_ctimespec(st, change_time_ts);
2166 set_mtimespec(st, write_time_ts);
2167 st->st_dev = srv->dev;
2174 * Routine to stat a file given an fd
2178 smbc_fstat_ctx(SMBCCTX *context,
2182 struct timespec change_time_ts;
2183 struct timespec access_time_ts;
2184 struct timespec write_time_ts;
2193 struct cli_state *targetcli;
2196 if (!context || !context->internal ||
2197 !context->internal->_initialized) {
2204 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2213 return context->fstatdir(context, file, st);
2217 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2218 if (smbc_parse_path(context, file->fname,
2220 server, sizeof(server),
2221 share, sizeof(share),
2224 password, sizeof(password),
2230 /*d_printf(">>>fstat: resolving %s\n", path);*/
2231 if (!cli_resolve_path("", file->srv->cli, path,
2232 &targetcli, targetpath))
2234 d_printf("Could not resolve %s\n", path);
2237 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2239 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2246 time_t change_time, access_time, write_time;
2248 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2249 &change_time, &access_time, &write_time)) {
2255 change_time_ts = convert_time_t_to_timespec(change_time);
2256 access_time_ts = convert_time_t_to_timespec(access_time);
2257 write_time_ts = convert_time_t_to_timespec(write_time);
2262 smbc_setup_stat(context, st, file->fname, size, mode);
2264 set_atimespec(st, access_time_ts);
2265 set_ctimespec(st, change_time_ts);
2266 set_mtimespec(st, write_time_ts);
2267 st->st_dev = file->srv->dev;
2274 * Routine to open a directory
2275 * We accept the URL syntax explained in smbc_parse_path(), above.
2279 smbc_remove_dir(SMBCFILE *dir)
2281 struct smbc_dir_list *d,*f;
2288 SAFE_FREE(f->dirent);
2293 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2298 add_dirent(SMBCFILE *dir,
2300 const char *comment,
2303 struct smbc_dirent *dirent;
2305 int name_length = (name == NULL ? 0 : strlen(name));
2306 int comment_len = (comment == NULL ? 0 : strlen(comment));
2309 * Allocate space for the dirent, which must be increased by the
2310 * size of the name and the comment and 1 each for the null terminator.
2313 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2315 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2319 dir->dir_error = ENOMEM;
2324 ZERO_STRUCTP(dirent);
2326 if (dir->dir_list == NULL) {
2328 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2329 if (!dir->dir_list) {
2332 dir->dir_error = ENOMEM;
2336 ZERO_STRUCTP(dir->dir_list);
2338 dir->dir_end = dir->dir_next = dir->dir_list;
2342 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2344 if (!dir->dir_end->next) {
2347 dir->dir_error = ENOMEM;
2351 ZERO_STRUCTP(dir->dir_end->next);
2353 dir->dir_end = dir->dir_end->next;
2356 dir->dir_end->next = NULL;
2357 dir->dir_end->dirent = dirent;
2359 dirent->smbc_type = type;
2360 dirent->namelen = name_length;
2361 dirent->commentlen = comment_len;
2362 dirent->dirlen = size;
2365 * dirent->namelen + 1 includes the null (no null termination needed)
2366 * Ditto for dirent->commentlen.
2367 * The space for the two null bytes was allocated.
2369 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2370 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2371 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2378 list_unique_wg_fn(const char *name,
2380 const char *comment,
2383 SMBCFILE *dir = (SMBCFILE *)state;
2384 struct smbc_dir_list *dir_list;
2385 struct smbc_dirent *dirent;
2389 dirent_type = dir->dir_type;
2391 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2393 /* An error occurred, what do we do? */
2394 /* FIXME: Add some code here */
2397 /* Point to the one just added */
2398 dirent = dir->dir_end->dirent;
2400 /* See if this was a duplicate */
2401 for (dir_list = dir->dir_list;
2402 dir_list != dir->dir_end;
2403 dir_list = dir_list->next) {
2405 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2406 /* Duplicate. End end of list need to be removed. */
2410 if (do_remove && dir_list->next == dir->dir_end) {
2411 /* Found the end of the list. Remove it. */
2412 dir->dir_end = dir_list;
2413 free(dir_list->next);
2415 dir_list->next = NULL;
2422 list_fn(const char *name,
2424 const char *comment,
2427 SMBCFILE *dir = (SMBCFILE *)state;
2431 * We need to process the type a little ...
2433 * Disk share = 0x00000000
2434 * Print share = 0x00000001
2435 * Comms share = 0x00000002 (obsolete?)
2436 * IPC$ share = 0x00000003
2438 * administrative shares:
2439 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2442 if (dir->dir_type == SMBC_FILE_SHARE) {
2445 case 0 | 0x80000000:
2447 dirent_type = SMBC_FILE_SHARE;
2451 dirent_type = SMBC_PRINTER_SHARE;
2455 dirent_type = SMBC_COMMS_SHARE;
2458 case 3 | 0x80000000:
2460 dirent_type = SMBC_IPC_SHARE;
2464 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2469 dirent_type = dir->dir_type;
2472 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2474 /* An error occurred, what do we do? */
2475 /* FIXME: Add some code here */
2481 dir_list_fn(const char *mnt,
2487 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2488 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2490 /* Handle an error ... */
2492 /* FIXME: Add some code ... */
2499 net_share_enum_rpc(struct cli_state *cli,
2500 void (*fn)(const char *name,
2502 const char *comment,
2509 uint32 info_level = 1;
2510 uint32 preferred_len = 0xffffffff;
2511 struct srvsvc_NetShareCtr1 ctr1;
2512 union srvsvc_NetShareCtr ctr;
2514 struct rpc_pipe_client *pipe_hnd;
2518 /* Open the server service pipe */
2519 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2521 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2525 /* Allocate a context for parsing and for the entries in "ctr" */
2526 mem_ctx = talloc_init("libsmbclient: net_share_enum_rpc");
2527 if (mem_ctx == NULL) {
2528 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2529 cli_rpc_pipe_close(pipe_hnd);
2536 /* Issue the NetShareEnum RPC call and retrieve the response */
2538 result = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
2539 &info_level, &ctr, preferred_len,
2540 &numentries, &enum_hnd);
2542 /* Was it successful? */
2543 if (!NT_STATUS_IS_OK(result) || numentries == 0) {
2544 /* Nope. Go clean up. */
2548 /* For each returned entry... */
2549 for (i = 0; i < numentries; i++) {
2551 /* Add this share to the list */
2552 (*fn)(ctr.ctr1->array[i].name,
2553 ctr.ctr1->array[i].type,
2554 ctr.ctr1->array[i].comment, state);
2558 /* Close the server service pipe */
2559 cli_rpc_pipe_close(pipe_hnd);
2561 /* Free all memory which was allocated for this request */
2562 TALLOC_FREE(mem_ctx);
2564 /* Tell 'em if it worked */
2565 return NT_STATUS_IS_OK(result) ? 0 : -1;
2571 smbc_opendir_ctx(SMBCCTX *context,
2575 fstring server, share, user, password, options;
2580 SMBCSRV *srv = NULL;
2581 SMBCFILE *dir = NULL;
2582 struct _smbc_callbacks *cb;
2583 struct in_addr rem_ip;
2585 if (!context || !context->internal ||
2586 !context->internal->_initialized) {
2587 DEBUG(4, ("no valid context\n"));
2588 errno = EINVAL + 8192;
2594 DEBUG(4, ("no valid fname\n"));
2595 errno = EINVAL + 8193;
2599 if (smbc_parse_path(context, fname,
2600 workgroup, sizeof(workgroup),
2601 server, sizeof(server),
2602 share, sizeof(share),
2605 password, sizeof(password),
2606 options, sizeof(options))) {
2607 DEBUG(4, ("no valid path\n"));
2608 errno = EINVAL + 8194;
2612 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2613 "path='%s' options='%s'\n",
2614 fname, server, share, path, options));
2616 /* Ensure the options are valid */
2617 if (smbc_check_options(server, share, path, options)) {
2618 DEBUG(4, ("unacceptable options (%s)\n", options));
2619 errno = EINVAL + 8195;
2623 if (user[0] == (char)0) fstrcpy(user, context->user);
2625 dir = SMB_MALLOC_P(SMBCFILE);
2637 dir->fname = SMB_STRDUP(fname);
2641 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2643 if (server[0] == (char)0) {
2648 struct ip_service *ip_list;
2649 struct ip_service server_addr;
2650 struct user_auth_info u_info;
2651 struct cli_state *cli;
2653 if (share[0] != (char)0 || path[0] != (char)0) {
2655 errno = EINVAL + 8196;
2657 SAFE_FREE(dir->fname);
2663 /* Determine how many local master browsers to query */
2664 max_lmb_count = (context->options.browse_max_lmb_count == 0
2666 : context->options.browse_max_lmb_count);
2668 pstrcpy(u_info.username, user);
2669 pstrcpy(u_info.password, password);
2672 * We have server and share and path empty but options
2673 * requesting that we scan all master browsers for their list
2674 * of workgroups/domains. This implies that we must first try
2675 * broadcast queries to find all master browsers, and if that
2676 * doesn't work, then try our other methods which return only
2677 * a single master browser.
2681 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
2685 if (!find_master_ip(workgroup, &server_addr.ip)) {
2688 SAFE_FREE(dir->fname);
2695 ip_list = &server_addr;
2699 for (i = 0; i < count && i < max_lmb_count; i++) {
2700 DEBUG(99, ("Found master browser %d of %d: %s\n",
2701 i+1, MAX(count, max_lmb_count),
2702 inet_ntoa(ip_list[i].ip)));
2704 cli = get_ipc_connect_master_ip(&ip_list[i],
2705 workgroup, &u_info);
2706 /* cli == NULL is the master browser refused to talk or
2707 could not be found */
2711 fstrcpy(server, cli->desthost);
2714 DEBUG(4, ("using workgroup %s %s\n",
2715 workgroup, server));
2718 * For each returned master browser IP address, get a
2719 * connection to IPC$ on the server if we do not
2720 * already have one, and determine the
2721 * workgroups/domains that it knows about.
2724 srv = smbc_server(context, True, server, "IPC$",
2725 workgroup, user, password);
2731 dir->dir_type = SMBC_WORKGROUP;
2733 /* Now, list the stuff ... */
2735 if (!cli_NetServerEnum(srv->cli,
2737 SV_TYPE_DOMAIN_ENUM,
2747 * Server not an empty string ... Check the rest and see what
2750 if (*share == '\0') {
2751 if (*path != '\0') {
2753 /* Should not have empty share with path */
2754 errno = EINVAL + 8197;
2756 SAFE_FREE(dir->fname);
2764 * We don't know if <server> is really a server name
2765 * or is a workgroup/domain name. If we already have
2766 * a server structure for it, we'll use it.
2767 * Otherwise, check to see if <server><1D>,
2768 * <server><1B>, or <server><20> translates. We check
2769 * to see if <server> is an IP address first.
2773 * See if we have an existing server. Do not
2774 * establish a connection if one does not already
2777 srv = smbc_server(context, False, server, "IPC$",
2778 workgroup, user, password);
2781 * If no existing server and not an IP addr, look for
2785 !is_ipaddress(server) &&
2786 (resolve_name(server, &rem_ip, 0x1d) || /* LMB */
2787 resolve_name(server, &rem_ip, 0x1b) )) { /* DMB */
2791 dir->dir_type = SMBC_SERVER;
2794 * Get the backup list ...
2796 if (!name_status_find(server, 0, 0,
2797 rem_ip, buserver)) {
2799 DEBUG(0, ("Could not get name of "
2800 "local/domain master browser "
2801 "for server %s\n", server));
2803 SAFE_FREE(dir->fname);
2812 * Get a connection to IPC$ on the server if
2813 * we do not already have one
2815 srv = smbc_server(context, True,
2817 workgroup, user, password);
2819 DEBUG(0, ("got no contact to IPC$\n"));
2821 SAFE_FREE(dir->fname);
2830 /* Now, list the servers ... */
2831 if (!cli_NetServerEnum(srv->cli, server,
2832 0x0000FFFE, list_fn,
2836 SAFE_FREE(dir->fname);
2842 (resolve_name(server, &rem_ip, 0x20))) {
2844 /* If we hadn't found the server, get one now */
2846 srv = smbc_server(context, True,
2854 SAFE_FREE(dir->fname);
2861 dir->dir_type = SMBC_FILE_SHARE;
2864 /* List the shares ... */
2866 if (net_share_enum_rpc(
2869 (void *) dir) < 0 &&
2875 errno = cli_errno(srv->cli);
2877 SAFE_FREE(dir->fname);
2884 /* Neither the workgroup nor server exists */
2885 errno = ECONNREFUSED;
2887 SAFE_FREE(dir->fname);
2896 * The server and share are specified ... work from
2900 struct cli_state *targetcli;
2902 /* We connect to the server and list the directory */
2903 dir->dir_type = SMBC_FILE_SHARE;
2905 srv = smbc_server(context, True, server, share,
2906 workgroup, user, password);
2911 SAFE_FREE(dir->fname);
2920 /* Now, list the files ... */
2922 p = path + strlen(path);
2923 pstrcat(path, "\\*");
2925 if (!cli_resolve_path("", srv->cli, path,
2926 &targetcli, targetpath))
2928 d_printf("Could not resolve %s\n", path);
2930 SAFE_FREE(dir->fname);
2936 if (cli_list(targetcli, targetpath,
2937 aDIR | aSYSTEM | aHIDDEN,
2938 dir_list_fn, (void *)dir) < 0) {
2941 SAFE_FREE(dir->fname);
2944 saved_errno = smbc_errno(context, targetcli);
2946 if (saved_errno == EINVAL) {
2948 * See if they asked to opendir something
2949 * other than a directory. If so, the
2950 * converted error value we got would have
2951 * been EINVAL rather than ENOTDIR.
2953 *p = '\0'; /* restore original path */
2955 if (smbc_getatr(context, srv, path,
2957 NULL, NULL, NULL, NULL,
2959 ! IS_DOS_DIR(mode)) {
2961 /* It is. Correct the error value */
2962 saved_errno = ENOTDIR;
2967 * If there was an error and the server is no
2970 cb = &context->callbacks;
2971 if (cli_is_error(targetcli) &&
2972 cb->check_server_fn(context, srv)) {
2974 /* ... then remove it. */
2975 if (cb->remove_unused_server_fn(context,
2978 * We could not remove the server
2979 * completely, remove it from the
2980 * cache so we will not get it
2981 * again. It will be removed when the
2982 * last file/dir is closed.
2984 cb->remove_cached_srv_fn(context, srv);
2988 errno = saved_errno;
2995 DLIST_ADD(context->internal->_files, dir);
3001 * Routine to close a directory
3005 smbc_closedir_ctx(SMBCCTX *context,
3009 if (!context || !context->internal ||
3010 !context->internal->_initialized) {
3017 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3024 smbc_remove_dir(dir); /* Clean it up */
3026 DLIST_REMOVE(context->internal->_files, dir);
3030 SAFE_FREE(dir->fname);
3031 SAFE_FREE(dir); /* Free the space too */
3039 smbc_readdir_internal(SMBCCTX * context,
3040 struct smbc_dirent *dest,
3041 struct smbc_dirent *src,
3042 int max_namebuf_len)
3044 if (context->options.urlencode_readdir_entries) {
3046 /* url-encode the name. get back remaining buffer space */
3048 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3050 /* We now know the name length */
3051 dest->namelen = strlen(dest->name);
3053 /* Save the pointer to the beginning of the comment */
3054 dest->comment = dest->name + dest->namelen + 1;
3056 /* Copy the comment */
3057 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3058 dest->comment[max_namebuf_len - 1] = '\0';
3060 /* Save other fields */
3061 dest->smbc_type = src->smbc_type;
3062 dest->commentlen = strlen(dest->comment);
3063 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3067 /* No encoding. Just copy the entry as is. */
3068 memcpy(dest, src, src->dirlen);
3069 dest->comment = (char *)(&dest->name + src->namelen + 1);
3075 * Routine to get a directory entry
3078 struct smbc_dirent *
3079 smbc_readdir_ctx(SMBCCTX *context,
3083 struct smbc_dirent *dirp, *dirent;
3085 /* Check that all is ok first ... */
3087 if (!context || !context->internal ||
3088 !context->internal->_initialized) {
3091 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3096 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3099 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3104 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3107 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3112 if (!dir->dir_next) {
3116 dirent = dir->dir_next->dirent;
3124 dirp = (struct smbc_dirent *)context->internal->_dirent;
3125 maxlen = (sizeof(context->internal->_dirent) -
3126 sizeof(struct smbc_dirent));
3128 smbc_readdir_internal(context, dirp, dirent, maxlen);
3130 dir->dir_next = dir->dir_next->next;
3136 * Routine to get directory entries
3140 smbc_getdents_ctx(SMBCCTX *context,
3142 struct smbc_dirent *dirp,
3148 char *ndir = (char *)dirp;
3149 struct smbc_dir_list *dirlist;
3151 /* Check that all is ok first ... */
3153 if (!context || !context->internal ||
3154 !context->internal->_initialized) {
3161 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3168 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3176 * Now, retrieve the number of entries that will fit in what was passed
3177 * We have to figure out if the info is in the list, or we need to
3178 * send a request to the server to get the info.
3181 while ((dirlist = dir->dir_next)) {
3182 struct smbc_dirent *dirent;
3184 if (!dirlist->dirent) {
3186 errno = ENOENT; /* Bad error */
3191 /* Do urlencoding of next entry, if so selected */
3192 dirent = (struct smbc_dirent *)context->internal->_dirent;
3193 maxlen = (sizeof(context->internal->_dirent) -
3194 sizeof(struct smbc_dirent));
3195 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3197 reqd = dirent->dirlen;
3201 if (rem < count) { /* We managed to copy something */
3207 else { /* Nothing copied ... */
3209 errno = EINVAL; /* Not enough space ... */
3216 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3218 ((struct smbc_dirent *)ndir)->comment =
3219 (char *)(&((struct smbc_dirent *)ndir)->name +
3227 dir->dir_next = dirlist = dirlist -> next;
3238 * Routine to create a directory ...
3242 smbc_mkdir_ctx(SMBCCTX *context,
3252 pstring path, targetpath;
3253 struct cli_state *targetcli;
3255 if (!context || !context->internal ||
3256 !context->internal->_initialized) {
3270 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3272 if (smbc_parse_path(context, fname,
3273 workgroup, sizeof(workgroup),
3274 server, sizeof(server),
3275 share, sizeof(share),
3278 password, sizeof(password),
3284 if (user[0] == (char)0) fstrcpy(user, context->user);
3286 srv = smbc_server(context, True,
3287 server, share, workgroup, user, password);
3291 return -1; /* errno set by smbc_server */
3295 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3296 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3298 d_printf("Could not resolve %s\n", path);
3301 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3303 if (!cli_mkdir(targetcli, targetpath)) {
3305 errno = smbc_errno(context, targetcli);
3315 * Our list function simply checks to see if a directory is not empty
3318 static int smbc_rmdir_dirempty = True;
3321 rmdir_list_fn(const char *mnt,
3326 if (strncmp(finfo->name, ".", 1) != 0 &&
3327 strncmp(finfo->name, "..", 2) != 0) {
3329 smbc_rmdir_dirempty = False;
3334 * Routine to remove a directory
3338 smbc_rmdir_ctx(SMBCCTX *context,
3349 struct cli_state *targetcli;
3351 if (!context || !context->internal ||
3352 !context->internal->_initialized) {
3366 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3368 if (smbc_parse_path(context, fname,
3369 workgroup, sizeof(workgroup),
3370 server, sizeof(server),
3371 share, sizeof(share),
3374 password, sizeof(password),
3381 if (user[0] == (char)0) fstrcpy(user, context->user);
3383 srv = smbc_server(context, True,
3384 server, share, workgroup, user, password);
3388 return -1; /* errno set by smbc_server */
3392 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3393 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3395 d_printf("Could not resolve %s\n", path);
3398 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3401 if (!cli_rmdir(targetcli, targetpath)) {
3403 errno = smbc_errno(context, targetcli);
3405 if (errno == EACCES) { /* Check if the dir empty or not */
3407 /* Local storage to avoid buffer overflows */
3410 smbc_rmdir_dirempty = True; /* Make this so ... */
3412 pstrcpy(lpath, targetpath);
3413 pstrcat(lpath, "\\*");
3415 if (cli_list(targetcli, lpath,
3416 aDIR | aSYSTEM | aHIDDEN,
3417 rmdir_list_fn, NULL) < 0) {
3419 /* Fix errno to ignore latest error ... */
3420 DEBUG(5, ("smbc_rmdir: "
3421 "cli_list returned an error: %d\n",
3422 smbc_errno(context, targetcli)));
3427 if (smbc_rmdir_dirempty)
3443 * Routine to return the current directory position
3447 smbc_telldir_ctx(SMBCCTX *context,
3450 off_t ret_val; /* Squash warnings about cast */
3452 if (!context || !context->internal ||
3453 !context->internal->_initialized) {
3460 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3467 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3475 * We return the pointer here as the offset
3477 ret_val = (off_t)(long)dir->dir_next;
3483 * A routine to run down the list and see if the entry is OK
3486 struct smbc_dir_list *
3487 smbc_check_dir_ent(struct smbc_dir_list *list,
3488 struct smbc_dirent *dirent)
3491 /* Run down the list looking for what we want */
3495 struct smbc_dir_list *tmp = list;
3499 if (tmp->dirent == dirent)
3508 return NULL; /* Not found, or an error */
3514 * Routine to seek on a directory
3518 smbc_lseekdir_ctx(SMBCCTX *context,
3522 long int l_offset = offset; /* Handle problems of size */
3523 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3524 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3526 if (!context || !context->internal ||
3527 !context->internal->_initialized) {
3534 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3541 /* Now, check what we were passed and see if it is OK ... */
3543 if (dirent == NULL) { /* Seek to the begining of the list */
3545 dir->dir_next = dir->dir_list;
3550 /* Now, run down the list and make sure that the entry is OK */
3551 /* This may need to be changed if we change the format of the list */
3553 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3555 errno = EINVAL; /* Bad entry */
3560 dir->dir_next = list_ent;
3567 * Routine to fstat a dir
3571 smbc_fstatdir_ctx(SMBCCTX *context,
3576 if (!context || !context->internal ||
3577 !context->internal->_initialized) {
3584 /* No code yet ... */
3591 smbc_chmod_ctx(SMBCCTX *context,
3604 if (!context || !context->internal ||
3605 !context->internal->_initialized) {
3607 errno = EINVAL; /* Best I can think of ... */
3619 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3621 if (smbc_parse_path(context, fname,
3622 workgroup, sizeof(workgroup),
3623 server, sizeof(server),
3624 share, sizeof(share),
3627 password, sizeof(password),
3633 if (user[0] == (char)0) fstrcpy(user, context->user);
3635 srv = smbc_server(context, True,
3636 server, share, workgroup, user, password);
3639 return -1; /* errno set by smbc_server */
3644 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3645 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3646 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3647 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3649 if (!cli_setatr(srv->cli, path, mode, 0)) {
3650 errno = smbc_errno(context, srv->cli);
3658 smbc_utimes_ctx(SMBCCTX *context,
3660 struct timeval *tbuf)
3672 if (!context || !context->internal ||
3673 !context->internal->_initialized) {
3675 errno = EINVAL; /* Best I can think of ... */
3688 access_time = write_time = time(NULL);
3690 access_time = tbuf[0].tv_sec;
3691 write_time = tbuf[1].tv_sec;
3700 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3701 atimebuf[sizeof(atimebuf) - 1] = '\0';
3702 if ((p = strchr(atimebuf, '\n')) != NULL) {
3706 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3707 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3708 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3712 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3713 fname, atimebuf, mtimebuf);
3716 if (smbc_parse_path(context, fname,
3717 workgroup, sizeof(workgroup),
3718 server, sizeof(server),
3719 share, sizeof(share),
3722 password, sizeof(password),
3728 if (user[0] == (char)0) fstrcpy(user, context->user);
3730 srv = smbc_server(context, True,
3731 server, share, workgroup, user, password);
3734 return -1; /* errno set by smbc_server */
3737 if (!smbc_setatr(context, srv, path,
3738 0, access_time, write_time, 0, 0)) {
3739 return -1; /* errno set by smbc_setatr */
3746 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
3747 However NT4 gives a "The information may have been modified by a
3748 computer running Windows NT 5.0" if denied ACEs do not appear before
3752 ace_compare(SEC_ACE *ace1,
3755 if (sec_ace_equal(ace1, ace2))
3758 if (ace1->type != ace2->type)
3759 return ace2->type - ace1->type;
3761 if (sid_compare(&ace1->trustee, &ace2->trustee))
3762 return sid_compare(&ace1->trustee, &ace2->trustee);
3764 if (ace1->flags != ace2->flags)
3765 return ace1->flags - ace2->flags;
3767 if (ace1->access_mask != ace2->access_mask)
3768 return ace1->access_mask - ace2->access_mask;
3770 if (ace1->size != ace2->size)
3771 return ace1->size - ace2->size;
3773 return memcmp(ace1, ace2, sizeof(SEC_ACE));
3778 sort_acl(SEC_ACL *the_acl)
3781 if (!the_acl) return;
3783 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
3784 QSORT_CAST ace_compare);
3786 for (i=1;i<the_acl->num_aces;) {
3787 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
3789 for (j=i; j<the_acl->num_aces-1; j++) {
3790 the_acl->aces[j] = the_acl->aces[j+1];
3792 the_acl->num_aces--;
3799 /* convert a SID to a string, either numeric or username/group */
3801 convert_sid_to_string(struct cli_state *ipc_cli,
3807 char **domains = NULL;
3808 char **names = NULL;
3809 enum lsa_SidType *types = NULL;
3810 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3811 sid_to_string(str, sid);
3814 return; /* no lookup desired */
3821 /* Ask LSA to convert the sid to a name */
3823 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ipc_cli->mem_ctx,
3824 pol, 1, sid, &domains,
3826 !domains || !domains[0] || !names || !names[0]) {
3832 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3833 domains[0], lp_winbind_separator(),
3837 /* convert a string to a SID, either numeric or username/group */
3839 convert_string_to_sid(struct cli_state *ipc_cli,
3845 enum lsa_SidType *types = NULL;
3846 DOM_SID *sids = NULL;
3848 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3855 if (strncmp(str, "S-", 2) == 0) {
3856 return string_to_sid(sid, str);
3863 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx,
3864 pol, 1, &str, NULL, &sids,
3870 sid_copy(sid, &sids[0]);
3877 /* parse an ACE in the same format as print_ace() */
3879 parse_ace(struct cli_state *ipc_cli,
3889 unsigned int aflags;
3893 const struct perm_value *v;
3899 /* These values discovered by inspection */
3900 static const struct perm_value special_values[] = {
3901 { "R", 0x00120089 },
3902 { "W", 0x00120116 },
3903 { "X", 0x001200a0 },
3904 { "D", 0x00010000 },
3905 { "P", 0x00040000 },
3906 { "O", 0x00080000 },
3910 static const struct perm_value standard_values[] = {
3911 { "READ", 0x001200a9 },
3912 { "CHANGE", 0x001301bf },
3913 { "FULL", 0x001f01ff },
3919 p = strchr_m(str,':');
3920 if (!p) return False;
3923 /* Try to parse numeric form */
3925 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3926 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3930 /* Try to parse text form */
3932 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3937 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3941 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3942 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3943 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3944 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3949 /* Only numeric form accepted for flags at present */
3951 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3952 sscanf(tok, "%i", &aflags))) {
3956 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3960 if (strncmp(tok, "0x", 2) == 0) {
3961 if (sscanf(tok, "%i", &amask) != 1) {
3967 for (v = standard_values; v->perm; v++) {
3968 if (strcmp(tok, v->perm) == 0) {
3979 for (v = special_values; v->perm; v++) {
3980 if (v->perm[0] == *p) {
3986 if (!found) return False;
3996 init_sec_ace(ace, &sid, atype, mask, aflags);
4000 /* add an ACE to a list of ACEs in a SEC_ACL */
4002 add_ace(SEC_ACL **the_acl,
4010 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
4014 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4017 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4018 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4019 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4020 1+(*the_acl)->num_aces, aces);
4022 (*the_acl) = newacl;
4027 /* parse a ascii version of a security descriptor */
4029 sec_desc_parse(TALLOC_CTX *ctx,
4030 struct cli_state *ipc_cli,
4035 const char *p = str;
4037 SEC_DESC *ret = NULL;
4039 DOM_SID *grp_sid=NULL;
4040 DOM_SID *owner_sid=NULL;
4044 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4046 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4047 revision = strtol(tok+9, NULL, 16);
4051 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4053 DEBUG(5, ("OWNER specified more than once!\n"));
4056 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4058 !convert_string_to_sid(ipc_cli, pol,
4060 owner_sid, tok+6)) {
4061 DEBUG(5, ("Failed to parse owner sid\n"));
4067 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4069 DEBUG(5, ("OWNER specified more than once!\n"));
4072 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4074 !convert_string_to_sid(ipc_cli, pol,
4076 owner_sid, tok+7)) {
4077 DEBUG(5, ("Failed to parse owner sid\n"));
4083 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4085 DEBUG(5, ("GROUP specified more than once!\n"));
4088 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4090 !convert_string_to_sid(ipc_cli, pol,
4093 DEBUG(5, ("Failed to parse group sid\n"));
4099 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4101 DEBUG(5, ("GROUP specified more than once!\n"));
4104 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4106 !convert_string_to_sid(ipc_cli, pol,
4109 DEBUG(5, ("Failed to parse group sid\n"));
4115 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4117 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4118 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4121 if(!add_ace(&dacl, &ace, ctx)) {
4122 DEBUG(5, ("Failed to add ACL %s\n", tok));
4128 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4130 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4131 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4134 if(!add_ace(&dacl, &ace, ctx)) {
4135 DEBUG(5, ("Failed to add ACL %s\n", tok));
4141 DEBUG(5, ("Failed to parse security descriptor\n"));
4145 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4146 owner_sid, grp_sid, NULL, dacl, &sd_size);
4150 SAFE_FREE(owner_sid);
4156 /* Obtain the current dos attributes */
4157 static DOS_ATTR_DESC *
4158 dos_attr_query(SMBCCTX *context,
4160 const char *filename,
4163 struct timespec create_time_ts;
4164 struct timespec write_time_ts;
4165 struct timespec access_time_ts;
4166 struct timespec change_time_ts;
4169 SMB_INO_T inode = 0;
4172 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4178 /* Obtain the DOS attributes */
4179 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4187 errno = smbc_errno(context, srv->cli);
4188 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4195 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4196 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4197 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4198 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4205 /* parse a ascii version of a security descriptor */
4207 dos_attr_parse(SMBCCTX *context,
4213 const char *p = str;
4216 const char * create_time_attr;
4217 const char * access_time_attr;
4218 const char * write_time_attr;
4219 const char * change_time_attr;
4222 /* Determine whether to use old-style or new-style attribute names */
4223 if (context->internal->_full_time_names) {
4224 /* new-style names */
4225 attr_strings.create_time_attr = "CREATE_TIME";
4226 attr_strings.access_time_attr = "ACCESS_TIME";
4227 attr_strings.write_time_attr = "WRITE_TIME";
4228 attr_strings.change_time_attr = "CHANGE_TIME";
4230 /* old-style names */
4231 attr_strings.create_time_attr = NULL;
4232 attr_strings.access_time_attr = "A_TIME";
4233 attr_strings.write_time_attr = "M_TIME";
4234 attr_strings.change_time_attr = "C_TIME";
4237 /* if this is to set the entire ACL... */
4239 /* ... then increment past the first colon if there is one */
4240 if ((p = strchr(str, ':')) != NULL) {
4247 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4249 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4250 dad->mode = strtol(tok+5, NULL, 16);
4254 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4255 dad->size = (SMB_OFF_T)atof(tok+5);
4259 n = strlen(attr_strings.access_time_attr);
4260 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4261 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4265 n = strlen(attr_strings.change_time_attr);
4266 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4267 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4271 n = strlen(attr_strings.write_time_attr);
4272 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4273 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4277 if (attr_strings.create_time_attr != NULL) {
4278 n = strlen(attr_strings.create_time_attr);
4279 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4281 dad->create_time = (time_t)strtol(tok+n+1,
4287 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4288 dad->inode = (SMB_INO_T)atof(tok+6);
4294 /*****************************************************
4295 Retrieve the acls for a file.
4296 *******************************************************/
4299 cacl_get(SMBCCTX *context,
4302 struct cli_state *ipc_cli,
4318 BOOL exclude_nt_revision = False;
4319 BOOL exclude_nt_owner = False;
4320 BOOL exclude_nt_group = False;
4321 BOOL exclude_nt_acl = False;
4322 BOOL exclude_dos_mode = False;
4323 BOOL exclude_dos_size = False;
4324 BOOL exclude_dos_create_time = False;
4325 BOOL exclude_dos_access_time = False;
4326 BOOL exclude_dos_write_time = False;
4327 BOOL exclude_dos_change_time = False;
4328 BOOL exclude_dos_inode = False;
4329 BOOL numeric = True;
4330 BOOL determine_size = (bufsize == 0);
4334 fstring name_sandbox;
4338 struct timespec create_time_ts;
4339 struct timespec write_time_ts;
4340 struct timespec access_time_ts;
4341 struct timespec change_time_ts;
4342 time_t create_time = (time_t)0;
4343 time_t write_time = (time_t)0;
4344 time_t access_time = (time_t)0;
4345 time_t change_time = (time_t)0;
4349 struct cli_state *cli = srv->cli;
4351 const char * create_time_attr;
4352 const char * access_time_attr;
4353 const char * write_time_attr;
4354 const char * change_time_attr;
4357 const char * create_time_attr;
4358 const char * access_time_attr;
4359 const char * write_time_attr;
4360 const char * change_time_attr;
4361 } excl_attr_strings;
4363 /* Determine whether to use old-style or new-style attribute names */
4364 if (context->internal->_full_time_names) {
4365 /* new-style names */
4366 attr_strings.create_time_attr = "CREATE_TIME";
4367 attr_strings.access_time_attr = "ACCESS_TIME";
4368 attr_strings.write_time_attr = "WRITE_TIME";
4369 attr_strings.change_time_attr = "CHANGE_TIME";
4371 excl_attr_strings.create_time_attr = "CREATE_TIME";
4372 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4373 excl_attr_strings.write_time_attr = "WRITE_TIME";
4374 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4376 /* old-style names */
4377 attr_strings.create_time_attr = NULL;
4378 attr_strings.access_time_attr = "A_TIME";
4379 attr_strings.write_time_attr = "M_TIME";
4380 attr_strings.change_time_attr = "C_TIME";
4382 excl_attr_strings.create_time_attr = NULL;
4383 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4384 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4385 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4388 /* Copy name so we can strip off exclusions (if any are specified) */
4389 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4391 /* Ensure name is null terminated */
4392 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4394 /* Play in the sandbox */
4395 name = name_sandbox;
4397 /* If there are any exclusions, point to them and mask them from name */
4398 if ((pExclude = strchr(name, '!')) != NULL)
4403 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4404 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4405 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4406 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4407 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4408 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4409 numeric = (* (name + strlen(name) - 1) != '+');
4411 /* Look for exclusions from "all" requests */
4412 if (all || all_nt || all_dos) {
4414 /* Exclusions are delimited by '!' */
4417 pExclude = (p == NULL ? NULL : p + 1)) {
4419 /* Find end of this exclusion name */
4420 if ((p = strchr(pExclude, '!')) != NULL)
4425 /* Which exclusion name is this? */
4426 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4427 exclude_nt_revision = True;
4429 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4430 exclude_nt_owner = True;
4432 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4433 exclude_nt_group = True;
4435 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4436 exclude_nt_acl = True;
4438 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4439 exclude_dos_mode = True;
4441 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4442 exclude_dos_size = True;
4444 else if (excl_attr_strings.create_time_attr != NULL &&
4445 StrCaseCmp(pExclude,
4446 excl_attr_strings.change_time_attr) == 0) {
4447 exclude_dos_create_time = True;
4449 else if (StrCaseCmp(pExclude,
4450 excl_attr_strings.access_time_attr) == 0) {
4451 exclude_dos_access_time = True;
4453 else if (StrCaseCmp(pExclude,
4454 excl_attr_strings.write_time_attr) == 0) {
4455 exclude_dos_write_time = True;
4457 else if (StrCaseCmp(pExclude,
4458 excl_attr_strings.change_time_attr) == 0) {
4459 exclude_dos_change_time = True;
4461 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4462 exclude_dos_inode = True;
4465 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4476 * If we are (possibly) talking to an NT or new system and some NT
4477 * attributes have been requested...
4479 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4480 /* Point to the portion after "system.nt_sec_desc." */
4481 name += 19; /* if (all) this will be invalid but unused */
4483 /* ... then obtain any NT attributes which were requested */
4484 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4487 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4488 filename, cli_errstr(cli)));
4493 sd = cli_query_secdesc(cli, fnum, ctx);
4497 ("cacl_get Failed to query old descriptor\n"));
4502 cli_close(cli, fnum);
4504 if (! exclude_nt_revision) {
4505 if (all || all_nt) {
4506 if (determine_size) {
4507 p = talloc_asprintf(ctx,
4516 n = snprintf(buf, bufsize,
4520 } else if (StrCaseCmp(name, "revision") == 0) {
4521 if (determine_size) {
4522 p = talloc_asprintf(ctx, "%d",
4530 n = snprintf(buf, bufsize, "%d",
4535 if (!determine_size && n > bufsize) {
4544 if (! exclude_nt_owner) {
4545 /* Get owner and group sid */
4546 if (sd->owner_sid) {
4547 convert_sid_to_string(ipc_cli, pol,
4552 fstrcpy(sidstr, "");
4555 if (all || all_nt) {
4556 if (determine_size) {
4557 p = talloc_asprintf(ctx, ",OWNER:%s",
4565 n = snprintf(buf, bufsize,
4566 ",OWNER:%s", sidstr);
4568 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4569 if (determine_size) {
4570 p = talloc_asprintf(ctx, "%s", sidstr);
4577 n = snprintf(buf, bufsize, "%s",
4582 if (!determine_size && n > bufsize) {
4591 if (! exclude_nt_group) {
4592 if (sd->group_sid) {
4593 convert_sid_to_string(ipc_cli, pol,
4597 fstrcpy(sidstr, "");
4600 if (all || all_nt) {
4601 if (determine_size) {
4602 p = talloc_asprintf(ctx, ",GROUP:%s",
4610 n = snprintf(buf, bufsize,
4611 ",GROUP:%s", sidstr);
4613 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4614 if (determine_size) {
4615 p = talloc_asprintf(ctx, "%s", sidstr);
4622 n = snprintf(buf, bufsize,
4627 if (!determine_size && n > bufsize) {
4636 if (! exclude_nt_acl) {
4637 /* Add aces to value buffer */
4638 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4640 SEC_ACE *ace = &sd->dacl->aces[i];
4641 convert_sid_to_string(ipc_cli, pol,
4645 if (all || all_nt) {
4646 if (determine_size) {
4647 p = talloc_asprintf(
4663 ",ACL:%s:%d/%d/0x%08x",
4669 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4670 StrCaseCmp(name+3, sidstr) == 0) ||
4671 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4672 StrCaseCmp(name+4, sidstr) == 0)) {
4673 if (determine_size) {
4674 p = talloc_asprintf(
4686 n = snprintf(buf, bufsize,
4692 } else if (all_nt_acls) {
4693 if (determine_size) {
4694 p = talloc_asprintf(
4696 "%s%s:%d/%d/0x%08x",
4708 n = snprintf(buf, bufsize,
4709 "%s%s:%d/%d/0x%08x",
4727 /* Restore name pointer to its original value */
4731 if (all || some_dos) {
4732 /* Point to the portion after "system.dos_attr." */
4733 name += 16; /* if (all) this will be invalid but unused */
4735 /* Obtain the DOS attributes */
4736 if (!smbc_getatr(context, srv, filename, &mode, &size,
4743 errno = smbc_errno(context, srv->cli);
4748 create_time = convert_timespec_to_time_t(create_time_ts);
4749 access_time = convert_timespec_to_time_t(access_time_ts);
4750 write_time = convert_timespec_to_time_t(write_time_ts);
4751 change_time = convert_timespec_to_time_t(change_time_ts);
4753 if (! exclude_dos_mode) {
4754 if (all || all_dos) {
4755 if (determine_size) {
4756 p = talloc_asprintf(ctx,
4769 n = snprintf(buf, bufsize,
4777 } else if (StrCaseCmp(name, "mode") == 0) {
4778 if (determine_size) {
4779 p = talloc_asprintf(ctx, "0x%x", mode);
4786 n = snprintf(buf, bufsize,
4791 if (!determine_size && n > bufsize) {
4800 if (! exclude_dos_size) {
4801 if (all || all_dos) {
4802 if (determine_size) {
4803 p = talloc_asprintf(
4813 n = snprintf(buf, bufsize,
4817 } else if (StrCaseCmp(name, "size") == 0) {
4818 if (determine_size) {
4819 p = talloc_asprintf(
4829 n = snprintf(buf, bufsize,
4835 if (!determine_size && n > bufsize) {
4844 if (! exclude_dos_create_time &&
4845 attr_strings.create_time_attr != NULL) {
4846 if (all || all_dos) {
4847 if (determine_size) {
4848 p = talloc_asprintf(ctx,
4850 attr_strings.create_time_attr,
4858 n = snprintf(buf, bufsize,
4860 attr_strings.create_time_attr,
4863 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
4864 if (determine_size) {
4865 p = talloc_asprintf(ctx, "%lu", create_time);
4872 n = snprintf(buf, bufsize,
4873 "%lu", create_time);
4877 if (!determine_size && n > bufsize) {
4886 if (! exclude_dos_access_time) {
4887 if (all || all_dos) {
4888 if (determine_size) {
4889 p = talloc_asprintf(ctx,
4891 attr_strings.access_time_attr,
4899 n = snprintf(buf, bufsize,
4901 attr_strings.access_time_attr,
4904 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
4905 if (determine_size) {
4906 p = talloc_asprintf(ctx, "%lu", access_time);
4913 n = snprintf(buf, bufsize,
4914 "%lu", access_time);
4918 if (!determine_size && n > bufsize) {
4927 if (! exclude_dos_write_time) {
4928 if (all || all_dos) {
4929 if (determine_size) {
4930 p = talloc_asprintf(ctx,
4932 attr_strings.write_time_attr,
4940 n = snprintf(buf, bufsize,
4942 attr_strings.write_time_attr,
4945 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
4946 if (determine_size) {
4947 p = talloc_asprintf(ctx, "%lu", write_time);
4954 n = snprintf(buf, bufsize,
4959 if (!determine_size && n > bufsize) {
4968 if (! exclude_dos_change_time) {
4969 if (all || all_dos) {
4970 if (determine_size) {
4971 p = talloc_asprintf(ctx,
4973 attr_strings.change_time_attr,
4981 n = snprintf(buf, bufsize,
4983 attr_strings.change_time_attr,
4986 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
4987 if (determine_size) {
4988 p = talloc_asprintf(ctx, "%lu", change_time);
4995 n = snprintf(buf, bufsize,
4996 "%lu", change_time);
5000 if (!determine_size && n > bufsize) {
5009 if (! exclude_dos_inode) {
5010 if (all || all_dos) {
5011 if (determine_size) {
5012 p = talloc_asprintf(
5022 n = snprintf(buf, bufsize,
5026 } else if (StrCaseCmp(name, "inode") == 0) {
5027 if (determine_size) {
5028 p = talloc_asprintf(
5038 n = snprintf(buf, bufsize,
5044 if (!determine_size && n > bufsize) {
5053 /* Restore name pointer to its original value */
5066 /*****************************************************
5067 set the ACLs on a file given an ascii description
5068 *******************************************************/
5070 cacl_set(TALLOC_CTX *ctx,
5071 struct cli_state *cli,
5072 struct cli_state *ipc_cli,
5074 const char *filename,
5075 const char *the_acl,
5081 SEC_DESC *sd = NULL, *old;
5082 SEC_ACL *dacl = NULL;
5083 DOM_SID *owner_sid = NULL;
5084 DOM_SID *grp_sid = NULL;
5089 BOOL numeric = True;
5091 /* the_acl will be null for REMOVE_ALL operations */
5093 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5097 /* if this is to set the entire ACL... */
5098 if (*the_acl == '*') {
5099 /* ... then increment past the first colon */
5103 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5104 CONST_DISCARD(char *, the_acl));
5112 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5113 that doesn't deref sd */
5115 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5120 /* The desired access below is the only one I could find that works
5121 with NT4, W2KP and Samba */
5123 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5126 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5127 filename, cli_errstr(cli)));
5132 old = cli_query_secdesc(cli, fnum, ctx);
5135 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5140 cli_close(cli, fnum);
5143 case SMBC_XATTR_MODE_REMOVE_ALL:
5144 old->dacl->num_aces = 0;
5145 SAFE_FREE(old->dacl->aces);
5146 SAFE_FREE(old->dacl);
5151 case SMBC_XATTR_MODE_REMOVE:
5152 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5155 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5156 if (sec_ace_equal(&sd->dacl->aces[i],
5157 &old->dacl->aces[j])) {
5159 for (k=j; k<old->dacl->num_aces-1;k++) {
5160 old->dacl->aces[k] =
5161 old->dacl->aces[k+1];
5163 old->dacl->num_aces--;
5164 if (old->dacl->num_aces == 0) {
5165 SAFE_FREE(old->dacl->aces);
5166 SAFE_FREE(old->dacl);
5183 case SMBC_XATTR_MODE_ADD:
5184 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5187 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5188 if (sid_equal(&sd->dacl->aces[i].trustee,
5189 &old->dacl->aces[j].trustee)) {
5190 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5195 old->dacl->aces[j] = sd->dacl->aces[i];
5201 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5207 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5208 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5214 case SMBC_XATTR_MODE_SET:
5216 owner_sid = old->owner_sid;
5217 grp_sid = old->group_sid;
5221 case SMBC_XATTR_MODE_CHOWN:
5222 owner_sid = sd->owner_sid;
5225 case SMBC_XATTR_MODE_CHGRP:
5226 grp_sid = sd->group_sid;
5230 /* Denied ACE entries must come before allowed ones */
5231 sort_acl(old->dacl);
5233 /* Create new security descriptor and set it */
5234 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5235 owner_sid, grp_sid, NULL, dacl, &sd_size);
5237 fnum = cli_nt_create(cli, filename,
5238 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5241 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5242 filename, cli_errstr(cli)));
5247 if (!cli_set_secdesc(cli, fnum, sd)) {
5248 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5255 cli_close(cli, fnum);
5266 smbc_setxattr_ctx(SMBCCTX *context,
5287 const char * create_time_attr;
5288 const char * access_time_attr;
5289 const char * write_time_attr;
5290 const char * change_time_attr;
5293 if (!context || !context->internal ||
5294 !context->internal->_initialized) {
5296 errno = EINVAL; /* Best I can think of ... */
5308 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5309 fname, name, (int) size, (const char*)value));
5311 if (smbc_parse_path(context, fname,
5312 workgroup, sizeof(workgroup),
5313 server, sizeof(server),
5314 share, sizeof(share),
5317 password, sizeof(password),
5323 if (user[0] == (char)0) fstrcpy(user, context->user);
5325 srv = smbc_server(context, True,
5326 server, share, workgroup, user, password);
5328 return -1; /* errno set by smbc_server */
5331 if (! srv->no_nt_session) {
5332 ipc_srv = smbc_attr_server(context, server, share,
5333 workgroup, user, password,
5335 srv->no_nt_session = True;
5340 ctx = talloc_init("smbc_setxattr");
5347 * Are they asking to set the entire set of known attributes?
5349 if (StrCaseCmp(name, "system.*") == 0 ||
5350 StrCaseCmp(name, "system.*+") == 0) {
5353 talloc_asprintf(ctx, "%s:%s",
5354 name+7, (const char *) value);
5362 ret = cacl_set(ctx, srv->cli,
5363 ipc_srv->cli, &pol, path,
5366 ? SMBC_XATTR_MODE_SET
5367 : SMBC_XATTR_MODE_ADD),
5373 /* get a DOS Attribute Descriptor with current attributes */
5374 dad = dos_attr_query(context, ctx, path, srv);
5376 /* Overwrite old with new, using what was provided */
5377 dos_attr_parse(context, dad, srv, namevalue);
5379 /* Set the new DOS attributes */
5380 if (! smbc_setatr(context, srv, path,
5387 /* cause failure if NT failed too */
5392 /* we only fail if both NT and DOS sets failed */
5393 if (ret < 0 && ! dad) {
5394 ret = -1; /* in case dad was null */
5400 talloc_destroy(ctx);
5405 * Are they asking to set an access control element or to set
5406 * the entire access control list?
5408 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5409 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5410 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5411 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5412 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5416 talloc_asprintf(ctx, "%s:%s",
5417 name+19, (const char *) value);
5420 ret = -1; /* errno set by smbc_server() */
5422 else if (! namevalue) {
5426 ret = cacl_set(ctx, srv->cli,
5427 ipc_srv->cli, &pol, path,
5430 ? SMBC_XATTR_MODE_SET
5431 : SMBC_XATTR_MODE_ADD),
5434 talloc_destroy(ctx);
5439 * Are they asking to set the owner?
5441 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5442 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5446 talloc_asprintf(ctx, "%s:%s",
5447 name+19, (const char *) value);
5451 ret = -1; /* errno set by smbc_server() */
5453 else if (! namevalue) {
5457 ret = cacl_set(ctx, srv->cli,
5458 ipc_srv->cli, &pol, path,
5459 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5461 talloc_destroy(ctx);
5466 * Are they asking to set the group?
5468 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5469 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5473 talloc_asprintf(ctx, "%s:%s",
5474 name+19, (const char *) value);
5477 /* errno set by smbc_server() */
5480 else if (! namevalue) {
5484 ret = cacl_set(ctx, srv->cli,
5485 ipc_srv->cli, &pol, path,
5486 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5488 talloc_destroy(ctx);
5492 /* Determine whether to use old-style or new-style attribute names */
5493 if (context->internal->_full_time_names) {
5494 /* new-style names */
5495 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5496 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5497 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5498 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5500 /* old-style names */
5501 attr_strings.create_time_attr = NULL;
5502 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5503 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5504 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5508 * Are they asking to set a DOS attribute?
5510 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5511 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5512 (attr_strings.create_time_attr != NULL &&
5513 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5514 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5515 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5516 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5518 /* get a DOS Attribute Descriptor with current attributes */
5519 dad = dos_attr_query(context, ctx, path, srv);
5522 talloc_asprintf(ctx, "%s:%s",
5523 name+16, (const char *) value);
5528 /* Overwrite old with provided new params */
5529 dos_attr_parse(context, dad, srv, namevalue);
5531 /* Set the new DOS attributes */
5532 ret2 = smbc_setatr(context, srv, path,
5539 /* ret2 has True (success) / False (failure) */
5550 talloc_destroy(ctx);
5554 /* Unsupported attribute name */
5555 talloc_destroy(ctx);
5561 smbc_getxattr_ctx(SMBCCTX *context,
5579 const char * create_time_attr;
5580 const char * access_time_attr;
5581 const char * write_time_attr;
5582 const char * change_time_attr;
5586 if (!context || !context->internal ||
5587 !context->internal->_initialized) {
5589 errno = EINVAL; /* Best I can think of ... */
5601 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5603 if (smbc_parse_path(context, fname,
5604 workgroup, sizeof(workgroup),
5605 server, sizeof(server),
5606 share, sizeof(share),
5609 password, sizeof(password),
5615 if (user[0] == (char)0) fstrcpy(user, context->user);
5617 srv = smbc_server(context, True,
5618 server, share, workgroup, user, password);
5620 return -1; /* errno set by smbc_server */
5623 if (! srv->no_nt_session) {
5624 ipc_srv = smbc_attr_server(context, server, share,
5625 workgroup, user, password,
5628 srv->no_nt_session = True;
5634 ctx = talloc_init("smbc:getxattr");
5640 /* Determine whether to use old-style or new-style attribute names */
5641 if (context->internal->_full_time_names) {
5642 /* new-style names */
5643 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5644 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5645 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5646 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5648 /* old-style names */
5649 attr_strings.create_time_attr = NULL;
5650 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5651 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5652 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5655 /* Are they requesting a supported attribute? */
5656 if (StrCaseCmp(name, "system.*") == 0 ||
5657 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5658 StrCaseCmp(name, "system.*+") == 0 ||
5659 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5660 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5661 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5662 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5663 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5664 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5665 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5666 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5667 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5668 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5669 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5670 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5671 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5672 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5673 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5674 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5675 (attr_strings.create_time_attr != NULL &&
5676 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5677 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5678 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5679 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5680 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
5683 ret = cacl_get(context, ctx, srv,
5684 ipc_srv == NULL ? NULL : ipc_srv->cli,
5686 CONST_DISCARD(char *, name),
5687 CONST_DISCARD(char *, value), size);
5688 if (ret < 0 && errno == 0) {
5689 errno = smbc_errno(context, srv->cli);
5691 talloc_destroy(ctx);
5695 /* Unsupported attribute name */
5696 talloc_destroy(ctx);
5703 smbc_removexattr_ctx(SMBCCTX *context,
5719 if (!context || !context->internal ||
5720 !context->internal->_initialized) {
5722 errno = EINVAL; /* Best I can think of ... */
5734 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
5736 if (smbc_parse_path(context, fname,
5737 workgroup, sizeof(workgroup),
5738 server, sizeof(server),
5739 share, sizeof(share),
5742 password, sizeof(password),
5748 if (user[0] == (char)0) fstrcpy(user, context->user);
5750 srv = smbc_server(context, True,
5751 server, share, workgroup, user, password);
5753 return -1; /* errno set by smbc_server */
5756 if (! srv->no_nt_session) {
5757 ipc_srv = smbc_attr_server(context, server, share,
5758 workgroup, user, password,
5760 srv->no_nt_session = True;
5766 return -1; /* errno set by smbc_attr_server */
5769 ctx = talloc_init("smbc_removexattr");
5775 /* Are they asking to set the entire ACL? */
5776 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5777 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
5780 ret = cacl_set(ctx, srv->cli,
5781 ipc_srv->cli, &pol, path,
5782 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
5783 talloc_destroy(ctx);
5788 * Are they asking to remove one or more spceific security descriptor
5791 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5792 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5793 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5794 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5795 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5796 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5797 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5800 ret = cacl_set(ctx, srv->cli,
5801 ipc_srv->cli, &pol, path,
5802 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
5803 talloc_destroy(ctx);
5807 /* Unsupported attribute name */
5808 talloc_destroy(ctx);
5814 smbc_listxattr_ctx(SMBCCTX *context,
5820 * This isn't quite what listxattr() is supposed to do. This returns
5821 * the complete set of attribute names, always, rather than only those
5822 * attribute names which actually exist for a file. Hmmm...
5824 const char supported_old[] =
5827 "system.nt_sec_desc.revision\0"
5828 "system.nt_sec_desc.owner\0"
5829 "system.nt_sec_desc.owner+\0"
5830 "system.nt_sec_desc.group\0"
5831 "system.nt_sec_desc.group+\0"
5832 "system.nt_sec_desc.acl.*\0"
5833 "system.nt_sec_desc.acl\0"
5834 "system.nt_sec_desc.acl+\0"
5835 "system.nt_sec_desc.*\0"
5836 "system.nt_sec_desc.*+\0"
5837 "system.dos_attr.*\0"
5838 "system.dos_attr.mode\0"
5839 "system.dos_attr.c_time\0"
5840 "system.dos_attr.a_time\0"
5841 "system.dos_attr.m_time\0"
5843 const char supported_new[] =
5846 "system.nt_sec_desc.revision\0"
5847 "system.nt_sec_desc.owner\0"
5848 "system.nt_sec_desc.owner+\0"
5849 "system.nt_sec_desc.group\0"
5850 "system.nt_sec_desc.group+\0"
5851 "system.nt_sec_desc.acl.*\0"
5852 "system.nt_sec_desc.acl\0"
5853 "system.nt_sec_desc.acl+\0"
5854 "system.nt_sec_desc.*\0"
5855 "system.nt_sec_desc.*+\0"
5856 "system.dos_attr.*\0"
5857 "system.dos_attr.mode\0"
5858 "system.dos_attr.create_time\0"
5859 "system.dos_attr.access_time\0"
5860 "system.dos_attr.write_time\0"
5861 "system.dos_attr.change_time\0"
5863 const char * supported;
5865 if (context->internal->_full_time_names) {
5866 supported = supported_new;
5868 supported = supported_old;
5872 return sizeof(supported);
5875 if (sizeof(supported) > size) {
5880 /* this can't be strcpy() because there are embedded null characters */
5881 memcpy(list, supported, sizeof(supported));
5882 return sizeof(supported);
5887 * Open a print file to be written to by other calls
5891 smbc_open_print_job_ctx(SMBCCTX *context,
5900 if (!context || !context->internal ||
5901 !context->internal->_initialized) {
5915 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
5917 if (smbc_parse_path(context, fname,
5919 server, sizeof(server),
5920 share, sizeof(share),
5923 password, sizeof(password),
5929 /* What if the path is empty, or the file exists? */
5931 return context->open(context, fname, O_WRONLY, 666);
5936 * Routine to print a file on a remote server ...
5938 * We open the file, which we assume to be on a remote server, and then
5939 * copy it to a print file on the share specified by printq.
5943 smbc_print_file_ctx(SMBCCTX *c_file,
5955 if (!c_file || !c_file->internal->_initialized || !c_print ||
5956 !c_print->internal->_initialized) {
5963 if (!fname && !printq) {
5970 /* Try to open the file for reading ... */
5972 if ((long)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
5974 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
5975 return -1; /* smbc_open sets errno */
5979 /* Now, try to open the printer file for writing */
5981 if ((long)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
5983 saverr = errno; /* Save errno */
5984 c_file->close_fn(c_file, fid1);
5990 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
5994 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
5997 c_file->close_fn(c_file, fid1);
5998 c_print->close_fn(c_print, fid2);
6007 c_file->close_fn(c_file, fid1); /* We have to close these anyway */
6008 c_print->close_fn(c_print, fid2);
6022 * Routine to list print jobs on a printer share ...
6026 smbc_list_print_jobs_ctx(SMBCCTX *context,
6028 smbc_list_print_job_fn fn)
6038 if (!context || !context->internal ||
6039 !context->internal->_initialized) {
6053 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6055 if (smbc_parse_path(context, fname,
6056 workgroup, sizeof(workgroup),
6057 server, sizeof(server),
6058 share, sizeof(share),
6061 password, sizeof(password),
6067 if (user[0] == (char)0) fstrcpy(user, context->user);
6069 srv = smbc_server(context, True,
6070 server, share, workgroup, user, password);
6074 return -1; /* errno set by smbc_server */
6078 if (cli_print_queue(srv->cli,
6079 (void (*)(struct print_job_info *))fn) < 0) {
6081 errno = smbc_errno(context, srv->cli);
6091 * Delete a print job from a remote printer share
6095 smbc_unlink_print_job_ctx(SMBCCTX *context,
6108 if (!context || !context->internal ||
6109 !context->internal->_initialized) {
6123 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6125 if (smbc_parse_path(context, fname,
6126 workgroup, sizeof(workgroup),
6127 server, sizeof(server),
6128 share, sizeof(share),
6131 password, sizeof(password),
6137 if (user[0] == (char)0) fstrcpy(user, context->user);
6139 srv = smbc_server(context, True,
6140 server, share, workgroup, user, password);
6144 return -1; /* errno set by smbc_server */
6148 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6151 errno = smbc_errno(context, srv->cli);
6152 else if (err == ERRnosuchprintjob)
6163 * Get a new empty handle to fill in with your own info
6166 smbc_new_context(void)
6170 context = SMB_MALLOC_P(SMBCCTX);
6176 ZERO_STRUCTP(context);
6178 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6179 if (!context->internal) {
6185 ZERO_STRUCTP(context->internal);
6188 /* ADD REASONABLE DEFAULTS */
6190 context->timeout = 20000; /* 20 seconds */
6192 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6193 context->options.urlencode_readdir_entries = False;/* backward compat */
6194 context->options.one_share_per_server = False;/* backward compat */
6195 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6196 /* backward compat */
6198 context->open = smbc_open_ctx;
6199 context->creat = smbc_creat_ctx;
6200 context->read = smbc_read_ctx;
6201 context->write = smbc_write_ctx;
6202 context->close_fn = smbc_close_ctx;
6203 context->unlink = smbc_unlink_ctx;
6204 context->rename = smbc_rename_ctx;
6205 context->lseek = smbc_lseek_ctx;
6206 context->stat = smbc_stat_ctx;
6207 context->fstat = smbc_fstat_ctx;
6208 context->opendir = smbc_opendir_ctx;
6209 context->closedir = smbc_closedir_ctx;
6210 context->readdir = smbc_readdir_ctx;
6211 context->getdents = smbc_getdents_ctx;
6212 context->mkdir = smbc_mkdir_ctx;
6213 context->rmdir = smbc_rmdir_ctx;
6214 context->telldir = smbc_telldir_ctx;
6215 context->lseekdir = smbc_lseekdir_ctx;
6216 context->fstatdir = smbc_fstatdir_ctx;
6217 context->chmod = smbc_chmod_ctx;
6218 context->utimes = smbc_utimes_ctx;
6219 context->setxattr = smbc_setxattr_ctx;
6220 context->getxattr = smbc_getxattr_ctx;
6221 context->removexattr = smbc_removexattr_ctx;
6222 context->listxattr = smbc_listxattr_ctx;
6223 context->open_print_job = smbc_open_print_job_ctx;
6224 context->print_file = smbc_print_file_ctx;
6225 context->list_print_jobs = smbc_list_print_jobs_ctx;
6226 context->unlink_print_job = smbc_unlink_print_job_ctx;
6228 context->callbacks.check_server_fn = smbc_check_server;
6229 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6231 smbc_default_cache_functions(context);
6239 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6240 * and thus you'll be leaking memory if not handled properly.
6244 smbc_free_context(SMBCCTX *context,
6254 DEBUG(1,("Performing aggressive shutdown.\n"));
6256 f = context->internal->_files;
6258 context->close_fn(context, f);
6261 context->internal->_files = NULL;
6263 /* First try to remove the servers the nice way. */
6264 if (context->callbacks.purge_cached_fn(context)) {
6267 DEBUG(1, ("Could not purge all servers, "
6268 "Nice way shutdown failed.\n"));
6269 s = context->internal->_servers;
6271 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6273 cli_shutdown(s->cli);
6274 context->callbacks.remove_cached_srv_fn(context,
6277 DLIST_REMOVE(context->internal->_servers, s);
6281 context->internal->_servers = NULL;
6285 /* This is the polite way */
6286 if (context->callbacks.purge_cached_fn(context)) {
6287 DEBUG(1, ("Could not purge all servers, "
6288 "free_context failed.\n"));
6292 if (context->internal->_servers) {
6293 DEBUG(1, ("Active servers in context, "
6294 "free_context failed.\n"));
6298 if (context->internal->_files) {
6299 DEBUG(1, ("Active files in context, "
6300 "free_context failed.\n"));
6306 /* Things we have to clean up */
6307 SAFE_FREE(context->workgroup);
6308 SAFE_FREE(context->netbios_name);
6309 SAFE_FREE(context->user);
6311 DEBUG(3, ("Context %p succesfully freed\n", context));
6312 SAFE_FREE(context->internal);
6319 * Each time the context structure is changed, we have binary backward
6320 * compatibility issues. Instead of modifying the public portions of the
6321 * context structure to add new options, instead, we put them in the internal
6322 * portion of the context structure and provide a set function for these new
6326 smbc_option_set(SMBCCTX *context,
6328 ... /* option_value */)
6334 smbc_get_auth_data_with_context_fn auth_fn;
6338 va_start(ap, option_name);
6340 if (strcmp(option_name, "debug_to_stderr") == 0) {
6342 * Log to standard error instead of standard output.
6344 option_value.b = (BOOL) va_arg(ap, int);
6345 context->internal->_debug_stderr = option_value.b;
6347 } else if (strcmp(option_name, "full_time_names") == 0) {
6349 * Use new-style time attribute names, e.g. WRITE_TIME rather
6350 * than the old-style names such as M_TIME. This allows also
6351 * setting/getting CREATE_TIME which was previously
6352 * unimplemented. (Note that the old C_TIME was supposed to
6353 * be CHANGE_TIME but was confused and sometimes referred to
6356 option_value.b = (BOOL) va_arg(ap, int);
6357 context->internal->_full_time_names = option_value.b;
6359 } else if (strcmp(option_name, "open_share_mode") == 0) {
6361 * The share mode to use for files opened with
6362 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6364 option_value.i = va_arg(ap, int);
6365 context->internal->_share_mode =
6366 (smbc_share_mode) option_value.i;
6368 } else if (strcmp(option_name, "auth_function") == 0) {
6370 * Use the new-style authentication function which includes
6373 option_value.auth_fn =
6374 va_arg(ap, smbc_get_auth_data_with_context_fn);
6375 context->internal->_auth_fn_with_context =
6376 option_value.auth_fn;
6377 } else if (strcmp(option_name, "user_data") == 0) {
6379 * Save a user data handle which may be retrieved by the user
6380 * with smbc_option_get()
6382 option_value.v = va_arg(ap, void *);
6383 context->internal->_user_data = option_value.v;
6391 * Retrieve the current value of an option
6394 smbc_option_get(SMBCCTX *context,
6397 if (strcmp(option_name, "debug_stderr") == 0) {
6399 * Log to standard error instead of standard output.
6401 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6402 return (void *) (intptr_t) context->internal->_debug_stderr;
6404 return (void *) context->internal->_debug_stderr;
6406 } else if (strcmp(option_name, "full_time_names") == 0) {
6408 * Use new-style time attribute names, e.g. WRITE_TIME rather
6409 * than the old-style names such as M_TIME. This allows also
6410 * setting/getting CREATE_TIME which was previously
6411 * unimplemented. (Note that the old C_TIME was supposed to
6412 * be CHANGE_TIME but was confused and sometimes referred to
6415 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6416 return (void *) (intptr_t) context->internal->_full_time_names;
6418 return (void *) context->internal->_full_time_names;
6421 } else if (strcmp(option_name, "auth_function") == 0) {
6423 * Use the new-style authentication function which includes
6426 return (void *) context->internal->_auth_fn_with_context;
6427 } else if (strcmp(option_name, "user_data") == 0) {
6429 * Save a user data handle which may be retrieved by the user
6430 * with smbc_option_get()
6432 return context->internal->_user_data;
6440 * Initialise the library etc
6442 * We accept a struct containing handle information.
6443 * valid values for info->debug from 0 to 100,
6444 * and insist that info->fn must be non-null.
6447 smbc_init_context(SMBCCTX *context)
6454 if (!context || !context->internal) {
6459 /* Do not initialise the same client twice */
6460 if (context->internal->_initialized) {
6464 if ((!context->callbacks.auth_fn &&
6465 !context->internal->_auth_fn_with_context) ||
6466 context->debug < 0 ||
6467 context->debug > 100) {
6474 if (!smbc_initialized) {
6476 * Do some library-wide intializations the first time we get
6479 BOOL conf_loaded = False;
6481 /* Set this to what the user wants */
6482 DEBUGLEVEL = context->debug;
6486 setup_logging("libsmbclient", True);
6487 if (context->internal->_debug_stderr) {
6489 x_setbuf(x_stderr, NULL);
6492 /* Here we would open the smb.conf file if needed ... */
6494 in_client = True; /* FIXME, make a param */
6496 home = getenv("HOME");
6498 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
6499 if (lp_load(conf, True, False, False, True)) {
6502 DEBUG(5, ("Could not load config file: %s\n",
6509 * Well, if that failed, try the dyn_CONFIGFILE
6510 * Which points to the standard locn, and if that
6511 * fails, silently ignore it and use the internal
6515 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6516 DEBUG(5, ("Could not load config file: %s\n",
6520 * We loaded the global config file. Now lets
6521 * load user-specific modifications to the
6524 slprintf(conf, sizeof(conf),
6525 "%s/.smb/smb.conf.append", home);
6526 if (!lp_load(conf, True, False, False, False)) {
6528 ("Could not append config file: "
6535 load_interfaces(); /* Load the list of interfaces ... */
6537 reopen_logs(); /* Get logging working ... */
6540 * Block SIGPIPE (from lib/util_sock.c: write())
6541 * It is not needed and should not stop execution
6543 BlockSignals(True, SIGPIPE);
6545 /* Done with one-time initialisation */
6546 smbc_initialized = 1;
6550 if (!context->user) {
6552 * FIXME: Is this the best way to get the user info?
6554 user = getenv("USER");
6555 /* walk around as "guest" if no username can be found */
6556 if (!user) context->user = SMB_STRDUP("guest");
6557 else context->user = SMB_STRDUP(user);
6560 if (!context->netbios_name) {
6562 * We try to get our netbios name from the config. If that
6563 * fails we fall back on constructing our netbios name from
6566 if (global_myname()) {
6567 context->netbios_name = SMB_STRDUP(global_myname());
6571 * Hmmm, I want to get hostname as well, but I am too
6572 * lazy for the moment
6575 context->netbios_name = (char *)SMB_MALLOC(17);
6576 if (!context->netbios_name) {
6580 slprintf(context->netbios_name, 16,
6581 "smbc%s%d", context->user, pid);
6585 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6587 if (!context->workgroup) {
6588 if (lp_workgroup()) {
6589 context->workgroup = SMB_STRDUP(lp_workgroup());
6592 /* TODO: Think about a decent default workgroup */
6593 context->workgroup = SMB_STRDUP("samba");
6597 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6599 /* shortest timeout is 1 second */
6600 if (context->timeout > 0 && context->timeout < 1000)
6601 context->timeout = 1000;
6604 * FIXME: Should we check the function pointers here?
6607 context->internal->_initialized = True;
6613 /* Return the verion of samba, and thus libsmbclient */
6617 return samba_version_string();