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"
31 * DOS Attribute values (used internally)
33 typedef struct DOS_ATTR_DESC {
45 * Internal flags for extended attributes
48 /* internal mode values */
49 #define SMBC_XATTR_MODE_ADD 1
50 #define SMBC_XATTR_MODE_REMOVE 2
51 #define SMBC_XATTR_MODE_REMOVE_ALL 3
52 #define SMBC_XATTR_MODE_SET 4
53 #define SMBC_XATTR_MODE_CHOWN 5
54 #define SMBC_XATTR_MODE_CHGRP 6
56 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
58 /*We should test for this in configure ... */
60 #define ENOTSUP EOPNOTSUPP
64 * Functions exported by libsmb_cache.c that we need here
66 int smbc_default_cache_functions(SMBCCTX *context);
69 * check if an element is part of the list.
70 * FIXME: Does not belong here !
71 * Can anyone put this in a macro in dlinklist.h ?
74 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
75 if (!p || !list) return False;
77 if (p == list) return True;
84 * Find an lsa pipe handle associated with a cli struct.
86 static struct rpc_pipe_client *
87 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
89 struct rpc_pipe_client *pipe_hnd;
91 for (pipe_hnd = ipc_cli->pipe_list;
93 pipe_hnd = pipe_hnd->next) {
95 if (pipe_hnd->pipe_idx == PI_LSARPC) {
104 smbc_close_ctx(SMBCCTX *context,
107 smbc_lseek_ctx(SMBCCTX *context,
112 extern BOOL in_client;
115 * Is the logging working / configfile read ?
117 static int smbc_initialized = 0;
120 hex2int( unsigned int _char )
122 if ( _char >= 'A' && _char <='F')
123 return _char - 'A' + 10;
124 if ( _char >= 'a' && _char <='f')
125 return _char - 'a' + 10;
126 if ( _char >= '0' && _char <='9')
134 * Convert strings of %xx to their single character equivalent. Each 'x' must
135 * be a valid hexadecimal digit, or that % sequence is left undecoded.
137 * dest may, but need not be, the same pointer as src.
139 * Returns the number of % sequences which could not be converted due to lack
140 * of two following hexadecimal digits.
143 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
145 int old_length = strlen(src);
151 if ( old_length == 0 ) {
156 while ( i < old_length ) {
157 unsigned char character = src[ i++ ];
159 if (character == '%') {
160 int a = i+1 < old_length ? hex2int( src[i] ) : -1;
161 int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
163 /* Replace valid sequence */
164 if (a != -1 && b != -1) {
166 /* Replace valid %xx sequence with %dd */
167 character = (a * 16) + b;
169 if (character == '\0') {
170 break; /* Stop at %00 */
185 strncpy(dest, temp, max_dest_len - 1);
186 dest[max_dest_len - 1] = '\0';
194 * Convert any characters not specifically allowed in a URL into their %xx
197 * Returns the remaining buffer length.
200 smbc_urlencode(char * dest, char * src, int max_dest_len)
202 char hex[] = "0123456789ABCDEF";
204 for (; *src != '\0' && max_dest_len >= 3; src++) {
216 *dest++ = hex[(*src >> 4) & 0x0f];
217 *dest++ = hex[*src & 0x0f];
232 * Function to parse a path and turn it into components
234 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
235 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
236 * general format ("smb:" only; we do not look for "cifs:").
240 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
244 * smb:// Show all workgroups.
246 * The method of locating the list of workgroups varies
247 * depending upon the setting of the context variable
248 * context->options.browse_max_lmb_count. This value
249 * determine the maximum number of local master browsers to
250 * query for the list of workgroups. In order to ensure that
251 * a complete list of workgroups is obtained, all master
252 * browsers must be queried, but if there are many
253 * workgroups, the time spent querying can begin to add up.
254 * For small networks (not many workgroups), it is suggested
255 * that this variable be set to 0, indicating query all local
256 * master browsers. When the network has many workgroups, a
257 * reasonable setting for this variable might be around 3.
259 * smb://name/ if name<1D> or name<1B> exists, list servers in
260 * workgroup, else, if name<20> exists, list all shares
263 * If "options" are provided, this function returns the entire option list as a
264 * string, for later parsing by the caller. Note that currently, no options
268 static const char *smbc_prefix = "smb:";
271 smbc_parse_path(SMBCCTX *context,
273 char *workgroup, int workgroup_len,
274 char *server, int server_len,
275 char *share, int share_len,
276 char *path, int path_len,
277 char *user, int user_len,
278 char *password, int password_len,
279 char *options, int options_len)
287 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
290 * Assume we wont find an authentication domain to parse, so default
291 * to the workgroup in the provided context.
293 if (workgroup != NULL) {
294 strncpy(workgroup, context->workgroup, workgroup_len - 1);
295 workgroup[workgroup_len - 1] = '\0';
298 if (options != NULL && options_len > 0) {
299 options[0] = (char)0;
303 /* see if it has the right prefix */
304 len = strlen(smbc_prefix);
305 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
306 return -1; /* What about no smb: ? */
311 /* Watch the test below, we are testing to see if we should exit */
313 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
315 DEBUG(1, ("Invalid path (does not begin with smb://"));
320 p += 2; /* Skip the double slash */
322 /* See if any options were specified */
323 if ((q = strrchr(p, '?')) != NULL ) {
324 /* There are options. Null terminate here and point to them */
327 DEBUG(4, ("Found options '%s'", q));
329 /* Copy the options */
330 if (options != NULL && options_len > 0) {
331 safe_strcpy(options, q, options_len - 1);
340 strncpy(server, context->workgroup,
341 ((strlen(context->workgroup) < 16)
342 ? strlen(context->workgroup)
344 server[server_len - 1] = '\0';
350 * ok, its for us. Now parse out the server, share etc.
352 * However, we want to parse out [[domain;]user[:password]@] if it
356 /* check that '@' occurs before '/', if '/' exists at all */
357 q = strchr_m(p, '@');
358 r = strchr_m(p, '/');
359 if (q && (!r || q < r)) {
360 pstring username, passwd, domain;
361 const char *u = userinfo;
363 next_token_no_ltrim(&p, userinfo, "@", sizeof(fstring));
365 username[0] = passwd[0] = domain[0] = 0;
367 if (strchr_m(u, ';')) {
369 next_token_no_ltrim(&u, domain, ";", sizeof(fstring));
373 if (strchr_m(u, ':')) {
375 next_token_no_ltrim(&u, username, ":", sizeof(fstring));
382 pstrcpy(username, u);
386 if (domain[0] && workgroup) {
387 strncpy(workgroup, domain, workgroup_len - 1);
388 workgroup[workgroup_len - 1] = '\0';
392 strncpy(user, username, user_len - 1);
393 user[user_len - 1] = '\0';
397 strncpy(password, passwd, password_len - 1);
398 password[password_len - 1] = '\0';
403 if (!next_token(&p, server, "/", sizeof(fstring))) {
409 if (*p == (char)0) goto decoding; /* That's it ... */
411 if (!next_token(&p, share, "/", sizeof(fstring))) {
418 * Prepend a leading slash if there's a file path, as required by
424 safe_strcpy(path + 1, p, path_len - 2);
427 all_string_sub(path, "/", "\\", 0);
430 (void) smbc_urldecode(path, path, path_len);
431 (void) smbc_urldecode(server, server, server_len);
432 (void) smbc_urldecode(share, share, share_len);
433 (void) smbc_urldecode(user, user, user_len);
434 (void) smbc_urldecode(password, password, password_len);
440 * Verify that the options specified in a URL are valid
443 smbc_check_options(char *server,
448 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
449 "path='%s' options='%s'\n",
450 server, share, path, options));
452 /* No options at all is always ok */
453 if (! *options) return 0;
455 /* Currently, we don't support any options. */
460 * Convert an SMB error into a UNIX error ...
463 smbc_errno(SMBCCTX *context,
466 int ret = cli_errno(c);
468 if (cli_is_dos_error(c)) {
472 cli_dos_error(c, &eclass, &ecode);
474 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
475 (int)eclass, (int)ecode, (int)ecode, ret));
479 status = cli_nt_error(c);
481 DEBUG(3,("smbc errno %s -> %d\n",
482 nt_errstr(status), ret));
489 * Check a server for being alive and well.
490 * returns 0 if the server is in shape. Returns 1 on error
492 * Also useable outside libsmbclient to enable external cache
493 * to do some checks too.
496 smbc_check_server(SMBCCTX * context,
499 if ( send_keepalive(server->cli->fd) == False )
502 /* connection is ok */
507 * Remove a server from the cached server list it's unused.
508 * On success, 0 is returned. 1 is returned if the server could not be removed.
510 * Also useable outside libsmbclient
513 smbc_remove_unused_server(SMBCCTX * context,
518 /* are we being fooled ? */
519 if (!context || !context->internal ||
520 !context->internal->_initialized || !srv) return 1;
523 /* Check all open files/directories for a relation with this server */
524 for (file = context->internal->_files; file; file=file->next) {
525 if (file->srv == srv) {
527 DEBUG(3, ("smbc_remove_usused_server: "
528 "%p still used by %p.\n",
534 DLIST_REMOVE(context->internal->_servers, srv);
536 cli_shutdown(srv->cli);
539 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
541 context->callbacks.remove_cached_srv_fn(context, srv);
549 find_server(SMBCCTX *context,
561 srv = context->callbacks.get_cached_srv_fn(context, server, share,
562 workgroup, username);
564 if (!auth_called && !srv && (!username[0] || !password[0])) {
565 if (context->internal->_auth_fn_with_context != NULL) {
566 context->internal->_auth_fn_with_context(
569 workgroup, sizeof(fstring),
570 username, sizeof(fstring),
571 password, sizeof(fstring));
573 context->callbacks.auth_fn(
575 workgroup, sizeof(fstring),
576 username, sizeof(fstring),
577 password, sizeof(fstring));
581 * However, smbc_auth_fn may have picked up info relating to
582 * an existing connection, so try for an existing connection
586 goto check_server_cache;
591 if (context->callbacks.check_server_fn(context, srv)) {
593 * This server is no good anymore
594 * Try to remove it and check for more possible
595 * servers in the cache
597 if (context->callbacks.remove_unused_server_fn(context,
600 * We could not remove the server completely,
601 * remove it from the cache so we will not get
602 * it again. It will be removed when the last
603 * file/dir is closed.
605 context->callbacks.remove_cached_srv_fn(context,
610 * Maybe there are more cached connections to this
613 goto check_server_cache;
623 * Connect to a server, possibly on an existing connection
625 * Here, what we want to do is: If the server and username
626 * match an existing connection, reuse that, otherwise, establish a
629 * If we have to create a new connection, call the auth_fn to get the
630 * info we need, unless the username and password were passed in.
634 smbc_server(SMBCCTX *context,
635 BOOL connect_if_not_found,
644 struct nmb_name called, calling;
645 const char *server_n = server;
648 int tried_reverse = 0;
651 const char *username_used;
656 if (server[0] == 0) {
661 /* Look for a cached connection */
662 srv = find_server(context, server, share,
663 workgroup, username, password);
666 * If we found a connection and we're only allowed one share per
669 if (srv && *share != '\0' && context->options.one_share_per_server) {
672 * ... then if there's no current connection to the share,
673 * connect to it. find_server(), or rather the function
674 * pointed to by context->callbacks.get_cached_srv_fn which
675 * was called by find_server(), will have issued a tree
676 * disconnect if the requested share is not the same as the
677 * one that was already connected.
679 if (srv->cli->cnum == (uint16) -1) {
680 /* Ensure we have accurate auth info */
681 if (context->internal->_auth_fn_with_context != NULL) {
682 context->internal->_auth_fn_with_context(
685 workgroup, sizeof(fstring),
686 username, sizeof(fstring),
687 password, sizeof(fstring));
689 context->callbacks.auth_fn(
691 workgroup, sizeof(fstring),
692 username, sizeof(fstring),
693 password, sizeof(fstring));
696 if (! cli_send_tconX(srv->cli, share, "?????",
697 password, strlen(password)+1)) {
699 errno = smbc_errno(context, srv->cli);
700 cli_shutdown(srv->cli);
702 context->callbacks.remove_cached_srv_fn(context,
708 * Regenerate the dev value since it's based on both
712 srv->dev = (dev_t)(str_checksum(server) ^
713 str_checksum(share));
718 /* If we have a connection... */
721 /* ... then we're done here. Give 'em what they came for. */
725 /* If we're not asked to connect when a connection doesn't exist... */
726 if (! connect_if_not_found) {
727 /* ... then we're done here. */
731 make_nmb_name(&calling, context->netbios_name, 0x0);
732 make_nmb_name(&called , server, 0x20);
734 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
736 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
739 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
743 /* have to open a new connection */
744 if ((c = cli_initialise()) == NULL) {
749 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
750 c->use_kerberos = True;
752 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
753 c->fallback_after_kerberos = True;
756 c->timeout = context->timeout;
759 * Force use of port 139 for first try if share is $IPC, empty, or
760 * null, so browse lists can work
762 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
763 port_try_first = 139;
766 port_try_first = 445;
770 c->port = port_try_first;
772 if (!cli_connect(c, server_n, &ip)) {
774 /* First connection attempt failed. Try alternate port. */
775 c->port = port_try_next;
777 if (!cli_connect(c, server_n, &ip)) {
784 if (!cli_session_request(c, &calling, &called)) {
786 if (strcmp(called.name, "*SMBSERVER")) {
787 make_nmb_name(&called , "*SMBSERVER", 0x20);
789 } else { /* Try one more time, but ensure we don't loop */
791 /* Only try this if server is an IP address ... */
793 if (is_ipaddress(server) && !tried_reverse) {
795 struct in_addr rem_ip;
797 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
798 DEBUG(4, ("Could not convert IP address "
799 "%s to struct in_addr\n", server));
804 tried_reverse++; /* Yuck */
806 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
807 make_nmb_name(&called, remote_name, 0x20);
816 DEBUG(4,(" session request ok\n"));
818 if (!cli_negprot(c)) {
824 username_used = username;
826 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
827 password, strlen(password),
828 password, strlen(password),
831 /* Failed. Try an anonymous login, if allowed by flags. */
834 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
835 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
846 DEBUG(4,(" session setup ok\n"));
848 if (!cli_send_tconX(c, share, "?????",
849 password, strlen(password)+1)) {
850 errno = smbc_errno(context, c);
855 DEBUG(4,(" tconx ok\n"));
858 * Ok, we have got a nice connection
859 * Let's allocate a server structure.
862 srv = SMB_MALLOC_P(SMBCSRV);
870 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
871 srv->no_pathinfo = False;
872 srv->no_pathinfo2 = False;
873 srv->no_nt_session = False;
875 /* now add it to the cache (internal or external) */
876 /* Let the cache function set errno if it wants to */
878 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
879 int saved_errno = errno;
880 DEBUG(3, (" Failed to add server to cache\n"));
888 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
889 server, share, srv));
891 DLIST_ADD(context->internal->_servers, srv);
905 * Connect to a server for getting/setting attributes, possibly on an existing
906 * connection. This works similarly to smbc_server().
909 smbc_attr_server(SMBCCTX *context,
918 struct cli_state *ipc_cli;
919 struct rpc_pipe_client *pipe_hnd;
921 SMBCSRV *ipc_srv=NULL;
924 * See if we've already created this special connection. Reference
925 * our "special" share name '*IPC$', which is an impossible real share
926 * name due to the leading asterisk.
928 ipc_srv = find_server(context, server, "*IPC$",
929 workgroup, username, password);
932 /* We didn't find a cached connection. Get the password */
933 if (*password == '\0') {
934 /* ... then retrieve it now. */
935 if (context->internal->_auth_fn_with_context != NULL) {
936 context->internal->_auth_fn_with_context(
939 workgroup, sizeof(fstring),
940 username, sizeof(fstring),
941 password, sizeof(fstring));
943 context->callbacks.auth_fn(
945 workgroup, sizeof(fstring),
946 username, sizeof(fstring),
947 password, sizeof(fstring));
952 nt_status = cli_full_connection(&ipc_cli,
953 global_myname(), server,
954 &ip, 0, "IPC$", "?????",
958 if (! NT_STATUS_IS_OK(nt_status)) {
959 DEBUG(1,("cli_full_connection failed! (%s)\n",
960 nt_errstr(nt_status)));
965 ipc_srv = SMB_MALLOC_P(SMBCSRV);
968 cli_shutdown(ipc_cli);
972 ZERO_STRUCTP(ipc_srv);
973 ipc_srv->cli = ipc_cli;
976 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
980 DEBUG(1, ("cli_nt_session_open fail!\n"));
982 cli_shutdown(ipc_srv->cli);
988 * Some systems don't support
989 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
990 * so we might as well do it too.
993 nt_status = rpccli_lsa_open_policy(
995 ipc_srv->cli->mem_ctx,
997 GENERIC_EXECUTE_ACCESS,
1000 if (!NT_STATUS_IS_OK(nt_status)) {
1001 errno = smbc_errno(context, ipc_srv->cli);
1002 cli_shutdown(ipc_srv->cli);
1007 /* now add it to the cache (internal or external) */
1009 errno = 0; /* let cache function set errno if it likes */
1010 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
1015 DEBUG(3, (" Failed to add server to cache\n"));
1019 cli_shutdown(ipc_srv->cli);
1024 DLIST_ADD(context->internal->_servers, ipc_srv);
1031 * Routine to open() a file ...
1035 smbc_open_ctx(SMBCCTX *context,
1040 fstring server, share, user, password, workgroup;
1043 struct cli_state *targetcli;
1044 SMBCSRV *srv = NULL;
1045 SMBCFILE *file = NULL;
1048 if (!context || !context->internal ||
1049 !context->internal->_initialized) {
1051 errno = EINVAL; /* Best I can think of ... */
1063 if (smbc_parse_path(context, fname,
1064 workgroup, sizeof(workgroup),
1065 server, sizeof(server),
1066 share, sizeof(share),
1069 password, sizeof(password),
1075 if (user[0] == (char)0) fstrcpy(user, context->user);
1077 srv = smbc_server(context, True,
1078 server, share, workgroup, user, password);
1082 if (errno == EPERM) errno = EACCES;
1083 return NULL; /* smbc_server sets errno */
1087 /* Hmmm, the test for a directory is suspect here ... FIXME */
1089 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1096 file = SMB_MALLOC_P(SMBCFILE);
1107 /*d_printf(">>>open: resolving %s\n", path);*/
1108 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1110 d_printf("Could not resolve %s\n", path);
1114 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1116 if ( targetcli->dfsroot )
1119 pstrcpy(temppath, targetpath);
1120 cli_dfs_make_full_path( targetpath, targetcli->desthost, targetcli->share, temppath);
1123 if ((fd = cli_open(targetcli, targetpath, flags, DENY_NONE)) < 0) {
1125 /* Handle the error ... */
1128 errno = smbc_errno(context, targetcli);
1133 /* Fill in file struct */
1136 file->fname = SMB_STRDUP(fname);
1141 DLIST_ADD(context->internal->_files, file);
1144 * If the file was opened in O_APPEND mode, all write
1145 * operations should be appended to the file. To do that,
1146 * though, using this protocol, would require a getattrE()
1147 * call for each and every write, to determine where the end
1148 * of the file is. (There does not appear to be an append flag
1149 * in the protocol.) Rather than add all of that overhead of
1150 * retrieving the current end-of-file offset prior to each
1151 * write operation, we'll assume that most append operations
1152 * will continuously write, so we'll just set the offset to
1153 * the end of the file now and hope that's adequate.
1155 * Note to self: If this proves inadequate, and O_APPEND
1156 * should, in some cases, be forced for each write, add a
1157 * field in the context options structure, for
1158 * "strict_append_mode" which would select between the current
1159 * behavior (if FALSE) or issuing a getattrE() prior to each
1160 * write and forcing the write to the end of the file (if
1161 * TRUE). Adding that capability will likely require adding
1162 * an "append" flag into the _SMBCFILE structure to track
1163 * whether a file was opened in O_APPEND mode. -- djl
1165 if (flags & O_APPEND) {
1166 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1167 (void) smbc_close_ctx(context, file);
1177 /* Check if opendir needed ... */
1182 eno = smbc_errno(context, srv->cli);
1183 file = context->opendir(context, fname);
1184 if (!file) errno = eno;
1189 errno = EINVAL; /* FIXME, correct errno ? */
1195 * Routine to create a file
1198 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1201 smbc_creat_ctx(SMBCCTX *context,
1206 if (!context || !context->internal ||
1207 !context->internal->_initialized) {
1214 return smbc_open_ctx(context, path, creat_bits, mode);
1218 * Routine to read() a file ...
1222 smbc_read_ctx(SMBCCTX *context,
1228 fstring server, share, user, password;
1229 pstring path, targetpath;
1230 struct cli_state *targetcli;
1235 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1236 * appears to pass file->offset (which is type off_t) differently than
1237 * a local variable of type off_t. Using local variable "offset" in
1238 * the call to cli_read() instead of file->offset fixes a problem
1239 * retrieving data at an offset greater than 4GB.
1243 if (!context || !context->internal ||
1244 !context->internal->_initialized) {
1251 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1253 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1260 offset = file->offset;
1262 /* Check that the buffer exists ... */
1271 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1272 if (smbc_parse_path(context, file->fname,
1274 server, sizeof(server),
1275 share, sizeof(share),
1278 password, sizeof(password),
1284 /*d_printf(">>>read: resolving %s\n", path);*/
1285 if (!cli_resolve_path("", file->srv->cli, path,
1286 &targetcli, targetpath))
1288 d_printf("Could not resolve %s\n", path);
1291 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1293 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1297 errno = smbc_errno(context, targetcli);
1302 file->offset += ret;
1304 DEBUG(4, (" --> %d\n", ret));
1306 return ret; /* Success, ret bytes of data ... */
1311 * Routine to write() a file ...
1315 smbc_write_ctx(SMBCCTX *context,
1322 fstring server, share, user, password;
1323 pstring path, targetpath;
1324 struct cli_state *targetcli;
1326 /* First check all pointers before dereferencing them */
1328 if (!context || !context->internal ||
1329 !context->internal->_initialized) {
1336 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1343 /* Check that the buffer exists ... */
1352 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1354 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1355 if (smbc_parse_path(context, file->fname,
1357 server, sizeof(server),
1358 share, sizeof(share),
1361 password, sizeof(password),
1367 /*d_printf(">>>write: resolving %s\n", path);*/
1368 if (!cli_resolve_path("", file->srv->cli, path,
1369 &targetcli, targetpath))
1371 d_printf("Could not resolve %s\n", path);
1374 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1377 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1381 errno = smbc_errno(context, targetcli);
1386 file->offset += ret;
1388 return ret; /* Success, 0 bytes of data ... */
1392 * Routine to close() a file ...
1396 smbc_close_ctx(SMBCCTX *context,
1400 fstring server, share, user, password;
1401 pstring path, targetpath;
1402 struct cli_state *targetcli;
1404 if (!context || !context->internal ||
1405 !context->internal->_initialized) {
1412 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1422 return context->closedir(context, file);
1426 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1427 if (smbc_parse_path(context, file->fname,
1429 server, sizeof(server),
1430 share, sizeof(share),
1433 password, sizeof(password),
1439 /*d_printf(">>>close: resolving %s\n", path);*/
1440 if (!cli_resolve_path("", file->srv->cli, path,
1441 &targetcli, targetpath))
1443 d_printf("Could not resolve %s\n", path);
1446 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1448 if (!cli_close(targetcli, file->cli_fd)) {
1450 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1452 /* Deallocate slot and remove the server
1453 * from the server cache if unused */
1454 errno = smbc_errno(context, targetcli);
1456 DLIST_REMOVE(context->internal->_files, file);
1457 SAFE_FREE(file->fname);
1459 context->callbacks.remove_unused_server_fn(context, srv);
1465 DLIST_REMOVE(context->internal->_files, file);
1466 SAFE_FREE(file->fname);
1473 * Get info from an SMB server on a file. Use a qpathinfo call first
1474 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1477 smbc_getatr(SMBCCTX * context,
1482 struct timespec *create_time_ts,
1483 struct timespec *access_time_ts,
1484 struct timespec *write_time_ts,
1485 struct timespec *change_time_ts,
1490 struct cli_state *targetcli;
1493 if (!context || !context->internal ||
1494 !context->internal->_initialized) {
1501 /* path fixup for . and .. */
1502 if (strequal(path, ".") || strequal(path, ".."))
1503 pstrcpy(fixedpath, "\\");
1506 pstrcpy(fixedpath, path);
1507 trim_string(fixedpath, NULL, "\\..");
1508 trim_string(fixedpath, NULL, "\\.");
1510 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1512 if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
1514 d_printf("Couldn't resolve %s\n", path);
1518 if ( targetcli->dfsroot )
1521 pstrcpy(temppath, targetpath);
1522 cli_dfs_make_full_path(targetpath, targetcli->desthost,
1523 targetcli->share, temppath);
1526 if (!srv->no_pathinfo2 &&
1527 cli_qpathinfo2(targetcli, targetpath,
1536 /* if this is NT then don't bother with the getatr */
1537 if (targetcli->capabilities & CAP_NT_SMBS) {
1542 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1544 struct timespec w_time_ts;
1546 w_time_ts = convert_time_t_to_timespec(write_time);
1548 if (write_time_ts != NULL) {
1549 *write_time_ts = w_time_ts;
1552 if (create_time_ts != NULL) {
1553 *create_time_ts = w_time_ts;
1556 if (access_time_ts != NULL) {
1557 *access_time_ts = w_time_ts;
1560 if (change_time_ts != NULL) {
1561 *change_time_ts = w_time_ts;
1564 srv->no_pathinfo2 = True;
1574 * Set file info on an SMB server. Use setpathinfo call first. If that
1575 * fails, use setattrE..
1577 * Access and modification time parameters are always used and must be
1578 * provided. Create time, if zero, will be determined from the actual create
1579 * time of the file. If non-zero, the create time will be set as well.
1581 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1584 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1595 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1596 * modern function for "new code" to be using, and it works given a
1597 * filename rather than requiring that the file be opened to have its
1598 * attributes manipulated.
1600 if (srv->no_pathinfo ||
1601 ! cli_setpathinfo(srv->cli, path,
1609 * setpathinfo is not supported; go to plan B.
1611 * cli_setatr() does not work on win98, and it also doesn't
1612 * support setting the access time (only the modification
1613 * time), so in all cases, we open the specified file and use
1614 * cli_setattrE() which should work on all OS versions, and
1615 * supports both times.
1618 /* Don't try {q,set}pathinfo() again, with this server */
1619 srv->no_pathinfo = True;
1622 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1624 errno = smbc_errno(context, srv->cli);
1628 /* Set the new attributes */
1629 ret = cli_setattrE(srv->cli, fd,
1634 /* Close the file */
1635 cli_close(srv->cli, fd);
1638 * Unfortunately, setattrE() doesn't have a provision for
1639 * setting the access mode (attributes). We'll have to try
1640 * cli_setatr() for that, and with only this parameter, it
1641 * seems to work on win98.
1643 if (ret && mode != (uint16) -1) {
1644 ret = cli_setatr(srv->cli, path, mode, 0);
1648 errno = smbc_errno(context, srv->cli);
1657 * Routine to unlink() a file
1661 smbc_unlink_ctx(SMBCCTX *context,
1664 fstring server, share, user, password, workgroup;
1665 pstring path, targetpath;
1666 struct cli_state *targetcli;
1667 SMBCSRV *srv = NULL;
1669 if (!context || !context->internal ||
1670 !context->internal->_initialized) {
1672 errno = EINVAL; /* Best I can think of ... */
1684 if (smbc_parse_path(context, fname,
1685 workgroup, sizeof(workgroup),
1686 server, sizeof(server),
1687 share, sizeof(share),
1690 password, sizeof(password),
1696 if (user[0] == (char)0) fstrcpy(user, context->user);
1698 srv = smbc_server(context, True,
1699 server, share, workgroup, user, password);
1703 return -1; /* smbc_server sets errno */
1707 /*d_printf(">>>unlink: resolving %s\n", path);*/
1708 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1710 d_printf("Could not resolve %s\n", path);
1713 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1715 if (!cli_unlink(targetcli, targetpath)) {
1717 errno = smbc_errno(context, targetcli);
1719 if (errno == EACCES) { /* Check if the file is a directory */
1724 struct timespec write_time_ts;
1725 struct timespec access_time_ts;
1726 struct timespec change_time_ts;
1729 if (!smbc_getatr(context, srv, path, &mode, &size,
1736 /* Hmmm, bad error ... What? */
1738 errno = smbc_errno(context, targetcli);
1744 if (IS_DOS_DIR(mode))
1747 errno = saverr; /* Restore this */
1756 return 0; /* Success ... */
1761 * Routine to rename() a file
1765 smbc_rename_ctx(SMBCCTX *ocontext,
1781 pstring targetpath1;
1782 pstring targetpath2;
1783 struct cli_state *targetcli1;
1784 struct cli_state *targetcli2;
1785 SMBCSRV *srv = NULL;
1787 if (!ocontext || !ncontext ||
1788 !ocontext->internal || !ncontext->internal ||
1789 !ocontext->internal->_initialized ||
1790 !ncontext->internal->_initialized) {
1792 errno = EINVAL; /* Best I can think of ... */
1797 if (!oname || !nname) {
1804 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1806 smbc_parse_path(ocontext, oname,
1807 workgroup, sizeof(workgroup),
1808 server1, sizeof(server1),
1809 share1, sizeof(share1),
1810 path1, sizeof(path1),
1811 user1, sizeof(user1),
1812 password1, sizeof(password1),
1815 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1817 smbc_parse_path(ncontext, nname,
1819 server2, sizeof(server2),
1820 share2, sizeof(share2),
1821 path2, sizeof(path2),
1822 user2, sizeof(user2),
1823 password2, sizeof(password2),
1826 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1828 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1829 strcmp(user1, user2)) {
1831 /* Can't rename across file systems, or users?? */
1838 srv = smbc_server(ocontext, True,
1839 server1, share1, workgroup, user1, password1);
1846 /*d_printf(">>>rename: resolving %s\n", path1);*/
1847 if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
1849 d_printf("Could not resolve %s\n", path1);
1852 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1853 /*d_printf(">>>rename: resolving %s\n", path2);*/
1854 if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
1856 d_printf("Could not resolve %s\n", path2);
1859 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1861 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1862 strcmp(targetcli1->share, targetcli2->share))
1864 /* can't rename across file systems */
1870 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1871 int eno = smbc_errno(ocontext, targetcli1);
1873 if (eno != EEXIST ||
1874 !cli_unlink(targetcli1, targetpath2) ||
1875 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1883 return 0; /* Success */
1888 * A routine to lseek() a file
1892 smbc_lseek_ctx(SMBCCTX *context,
1898 fstring server, share, user, password;
1899 pstring path, targetpath;
1900 struct cli_state *targetcli;
1902 if (!context || !context->internal ||
1903 !context->internal->_initialized) {
1910 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1920 return -1; /* Can't lseek a dir ... */
1926 file->offset = offset;
1930 file->offset += offset;
1934 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1935 if (smbc_parse_path(context, file->fname,
1937 server, sizeof(server),
1938 share, sizeof(share),
1941 password, sizeof(password),
1948 /*d_printf(">>>lseek: resolving %s\n", path);*/
1949 if (!cli_resolve_path("", file->srv->cli, path,
1950 &targetcli, targetpath))
1952 d_printf("Could not resolve %s\n", path);
1955 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1957 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
1958 &size, NULL, NULL, NULL, NULL, NULL))
1960 SMB_OFF_T b_size = size;
1961 if (!cli_getattrE(targetcli, file->cli_fd,
1962 NULL, &b_size, NULL, NULL, NULL))
1969 file->offset = size + offset;
1978 return file->offset;
1983 * Generate an inode number from file name for those things that need it
1987 smbc_inode(SMBCCTX *context,
1991 if (!context || !context->internal ||
1992 !context->internal->_initialized) {
1999 if (!*name) return 2; /* FIXME, why 2 ??? */
2000 return (ino_t)str_checksum(name);
2005 * Routine to put basic stat info into a stat structure ... Used by stat and
2010 smbc_setup_stat(SMBCCTX *context,
2019 if (IS_DOS_DIR(mode)) {
2020 st->st_mode = SMBC_DIR_MODE;
2022 st->st_mode = SMBC_FILE_MODE;
2025 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2026 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2027 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2028 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2031 #ifdef HAVE_STAT_ST_BLKSIZE
2032 st->st_blksize = 512;
2034 #ifdef HAVE_STAT_ST_BLOCKS
2035 st->st_blocks = (size+511)/512;
2037 st->st_uid = getuid();
2038 st->st_gid = getgid();
2040 if (IS_DOS_DIR(mode)) {
2046 if (st->st_ino == 0) {
2047 st->st_ino = smbc_inode(context, fname);
2050 return True; /* FIXME: Is this needed ? */
2055 * Routine to stat a file given a name
2059 smbc_stat_ctx(SMBCCTX *context,
2070 struct timespec write_time_ts;
2071 struct timespec access_time_ts;
2072 struct timespec change_time_ts;
2077 if (!context || !context->internal ||
2078 !context->internal->_initialized) {
2080 errno = EINVAL; /* Best I can think of ... */
2092 DEBUG(4, ("smbc_stat(%s)\n", fname));
2094 if (smbc_parse_path(context, fname,
2095 workgroup, sizeof(workgroup),
2096 server, sizeof(server),
2097 share, sizeof(share),
2100 password, sizeof(password),
2106 if (user[0] == (char)0) fstrcpy(user, context->user);
2108 srv = smbc_server(context, True,
2109 server, share, workgroup, user, password);
2112 return -1; /* errno set by smbc_server */
2115 if (!smbc_getatr(context, srv, path, &mode, &size,
2122 errno = smbc_errno(context, srv->cli);
2129 smbc_setup_stat(context, st, path, size, mode);
2131 set_atimespec(st, access_time_ts);
2132 set_ctimespec(st, change_time_ts);
2133 set_mtimespec(st, write_time_ts);
2134 st->st_dev = srv->dev;
2141 * Routine to stat a file given an fd
2145 smbc_fstat_ctx(SMBCCTX *context,
2149 struct timespec change_time_ts;
2150 struct timespec access_time_ts;
2151 struct timespec write_time_ts;
2160 struct cli_state *targetcli;
2163 if (!context || !context->internal ||
2164 !context->internal->_initialized) {
2171 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2180 return context->fstatdir(context, file, st);
2184 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2185 if (smbc_parse_path(context, file->fname,
2187 server, sizeof(server),
2188 share, sizeof(share),
2191 password, sizeof(password),
2197 /*d_printf(">>>fstat: resolving %s\n", path);*/
2198 if (!cli_resolve_path("", file->srv->cli, path,
2199 &targetcli, targetpath))
2201 d_printf("Could not resolve %s\n", path);
2204 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2206 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2213 time_t change_time, access_time, write_time;
2215 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2216 &change_time, &access_time, &write_time)) {
2222 change_time_ts = convert_time_t_to_timespec(change_time);
2223 access_time_ts = convert_time_t_to_timespec(access_time);
2224 write_time_ts = convert_time_t_to_timespec(write_time);
2229 smbc_setup_stat(context, st, file->fname, size, mode);
2231 set_atimespec(st, access_time_ts);
2232 set_ctimespec(st, change_time_ts);
2233 set_mtimespec(st, write_time_ts);
2234 st->st_dev = file->srv->dev;
2241 * Routine to open a directory
2242 * We accept the URL syntax explained in smbc_parse_path(), above.
2246 smbc_remove_dir(SMBCFILE *dir)
2248 struct smbc_dir_list *d,*f;
2255 SAFE_FREE(f->dirent);
2260 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2265 add_dirent(SMBCFILE *dir,
2267 const char *comment,
2270 struct smbc_dirent *dirent;
2272 int name_length = (name == NULL ? 0 : strlen(name));
2273 int comment_len = (comment == NULL ? 0 : strlen(comment));
2276 * Allocate space for the dirent, which must be increased by the
2277 * size of the name and the comment and 1 each for the null terminator.
2280 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2282 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2286 dir->dir_error = ENOMEM;
2291 ZERO_STRUCTP(dirent);
2293 if (dir->dir_list == NULL) {
2295 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2296 if (!dir->dir_list) {
2299 dir->dir_error = ENOMEM;
2303 ZERO_STRUCTP(dir->dir_list);
2305 dir->dir_end = dir->dir_next = dir->dir_list;
2309 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2311 if (!dir->dir_end->next) {
2314 dir->dir_error = ENOMEM;
2318 ZERO_STRUCTP(dir->dir_end->next);
2320 dir->dir_end = dir->dir_end->next;
2323 dir->dir_end->next = NULL;
2324 dir->dir_end->dirent = dirent;
2326 dirent->smbc_type = type;
2327 dirent->namelen = name_length;
2328 dirent->commentlen = comment_len;
2329 dirent->dirlen = size;
2332 * dirent->namelen + 1 includes the null (no null termination needed)
2333 * Ditto for dirent->commentlen.
2334 * The space for the two null bytes was allocated.
2336 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2337 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2338 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2345 list_unique_wg_fn(const char *name,
2347 const char *comment,
2350 SMBCFILE *dir = (SMBCFILE *)state;
2351 struct smbc_dir_list *dir_list;
2352 struct smbc_dirent *dirent;
2356 dirent_type = dir->dir_type;
2358 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2360 /* An error occurred, what do we do? */
2361 /* FIXME: Add some code here */
2364 /* Point to the one just added */
2365 dirent = dir->dir_end->dirent;
2367 /* See if this was a duplicate */
2368 for (dir_list = dir->dir_list;
2369 dir_list != dir->dir_end;
2370 dir_list = dir_list->next) {
2372 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2373 /* Duplicate. End end of list need to be removed. */
2377 if (do_remove && dir_list->next == dir->dir_end) {
2378 /* Found the end of the list. Remove it. */
2379 dir->dir_end = dir_list;
2380 free(dir_list->next);
2382 dir_list->next = NULL;
2389 list_fn(const char *name,
2391 const char *comment,
2394 SMBCFILE *dir = (SMBCFILE *)state;
2398 * We need to process the type a little ...
2400 * Disk share = 0x00000000
2401 * Print share = 0x00000001
2402 * Comms share = 0x00000002 (obsolete?)
2403 * IPC$ share = 0x00000003
2405 * administrative shares:
2406 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2409 if (dir->dir_type == SMBC_FILE_SHARE) {
2412 case 0 | 0x80000000:
2414 dirent_type = SMBC_FILE_SHARE;
2418 dirent_type = SMBC_PRINTER_SHARE;
2422 dirent_type = SMBC_COMMS_SHARE;
2425 case 3 | 0x80000000:
2427 dirent_type = SMBC_IPC_SHARE;
2431 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2436 dirent_type = dir->dir_type;
2439 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2441 /* An error occurred, what do we do? */
2442 /* FIXME: Add some code here */
2448 dir_list_fn(const char *mnt,
2454 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2455 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2457 /* Handle an error ... */
2459 /* FIXME: Add some code ... */
2466 net_share_enum_rpc(struct cli_state *cli,
2467 void (*fn)(const char *name,
2469 const char *comment,
2476 uint32 info_level = 1;
2477 uint32 preferred_len = 0xffffffff;
2479 SRV_SHARE_INFO_CTR ctr;
2481 fstring comment = "";
2483 struct rpc_pipe_client *pipe_hnd;
2486 /* Open the server service pipe */
2487 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2489 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2493 /* Allocate a context for parsing and for the entries in "ctr" */
2494 mem_ctx = talloc_init("libsmbclient: net_share_enum_rpc");
2495 if (mem_ctx == NULL) {
2496 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2497 cli_rpc_pipe_close(pipe_hnd);
2501 /* Issue the NetShareEnum RPC call and retrieve the response */
2502 init_enum_hnd(&enum_hnd, 0);
2503 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2510 /* Was it successful? */
2511 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2512 /* Nope. Go clean up. */
2516 /* For each returned entry... */
2517 for (i = 0; i < ctr.num_entries; i++) {
2519 /* pull out the share name */
2520 rpcstr_pull_unistr2_fstring(
2521 name, &ctr.share.info1[i].info_1_str.uni_netname);
2523 /* pull out the share's comment */
2524 rpcstr_pull_unistr2_fstring(
2525 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2527 /* Get the type value */
2528 type = ctr.share.info1[i].info_1.type;
2530 /* Add this share to the list */
2531 (*fn)(name, type, comment, state);
2535 /* Close the server service pipe */
2536 cli_rpc_pipe_close(pipe_hnd);
2538 /* Free all memory which was allocated for this request */
2539 TALLOC_FREE(mem_ctx);
2541 /* Tell 'em if it worked */
2542 return W_ERROR_IS_OK(result) ? 0 : -1;
2548 smbc_opendir_ctx(SMBCCTX *context,
2552 fstring server, share, user, password, options;
2557 SMBCSRV *srv = NULL;
2558 SMBCFILE *dir = NULL;
2559 struct _smbc_callbacks *cb;
2560 struct in_addr rem_ip;
2562 if (!context || !context->internal ||
2563 !context->internal->_initialized) {
2564 DEBUG(4, ("no valid context\n"));
2565 errno = EINVAL + 8192;
2571 DEBUG(4, ("no valid fname\n"));
2572 errno = EINVAL + 8193;
2576 if (smbc_parse_path(context, fname,
2577 workgroup, sizeof(workgroup),
2578 server, sizeof(server),
2579 share, sizeof(share),
2582 password, sizeof(password),
2583 options, sizeof(options))) {
2584 DEBUG(4, ("no valid path\n"));
2585 errno = EINVAL + 8194;
2589 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2590 "path='%s' options='%s'\n",
2591 fname, server, share, path, options));
2593 /* Ensure the options are valid */
2594 if (smbc_check_options(server, share, path, options)) {
2595 DEBUG(4, ("unacceptable options (%s)\n", options));
2596 errno = EINVAL + 8195;
2600 if (user[0] == (char)0) fstrcpy(user, context->user);
2602 dir = SMB_MALLOC_P(SMBCFILE);
2614 dir->fname = SMB_STRDUP(fname);
2618 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2620 if (server[0] == (char)0) {
2625 struct ip_service *ip_list;
2626 struct ip_service server_addr;
2627 struct user_auth_info u_info;
2628 struct cli_state *cli;
2630 if (share[0] != (char)0 || path[0] != (char)0) {
2632 errno = EINVAL + 8196;
2634 SAFE_FREE(dir->fname);
2640 /* Determine how many local master browsers to query */
2641 max_lmb_count = (context->options.browse_max_lmb_count == 0
2643 : context->options.browse_max_lmb_count);
2645 pstrcpy(u_info.username, user);
2646 pstrcpy(u_info.password, password);
2649 * We have server and share and path empty but options
2650 * requesting that we scan all master browsers for their list
2651 * of workgroups/domains. This implies that we must first try
2652 * broadcast queries to find all master browsers, and if that
2653 * doesn't work, then try our other methods which return only
2654 * a single master browser.
2658 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
2662 if (!find_master_ip(workgroup, &server_addr.ip)) {
2665 SAFE_FREE(dir->fname);
2672 ip_list = &server_addr;
2676 for (i = 0; i < count && i < max_lmb_count; i++) {
2677 DEBUG(99, ("Found master browser %d of %d: %s\n",
2678 i+1, MAX(count, max_lmb_count),
2679 inet_ntoa(ip_list[i].ip)));
2681 cli = get_ipc_connect_master_ip(&ip_list[i],
2682 workgroup, &u_info);
2683 /* cli == NULL is the master browser refused to talk or
2684 could not be found */
2688 fstrcpy(server, cli->desthost);
2691 DEBUG(4, ("using workgroup %s %s\n",
2692 workgroup, server));
2695 * For each returned master browser IP address, get a
2696 * connection to IPC$ on the server if we do not
2697 * already have one, and determine the
2698 * workgroups/domains that it knows about.
2701 srv = smbc_server(context, True, server, "IPC$",
2702 workgroup, user, password);
2708 dir->dir_type = SMBC_WORKGROUP;
2710 /* Now, list the stuff ... */
2712 if (!cli_NetServerEnum(srv->cli,
2714 SV_TYPE_DOMAIN_ENUM,
2724 * Server not an empty string ... Check the rest and see what
2727 if (*share == '\0') {
2728 if (*path != '\0') {
2730 /* Should not have empty share with path */
2731 errno = EINVAL + 8197;
2733 SAFE_FREE(dir->fname);
2741 * We don't know if <server> is really a server name
2742 * or is a workgroup/domain name. If we already have
2743 * a server structure for it, we'll use it.
2744 * Otherwise, check to see if <server><1D>,
2745 * <server><1B>, or <server><20> translates. We check
2746 * to see if <server> is an IP address first.
2750 * See if we have an existing server. Do not
2751 * establish a connection if one does not already
2754 srv = smbc_server(context, False, server, "IPC$",
2755 workgroup, user, password);
2758 * If no existing server and not an IP addr, look for
2762 !is_ipaddress(server) &&
2763 (resolve_name(server, &rem_ip, 0x1d) || /* LMB */
2764 resolve_name(server, &rem_ip, 0x1b) )) { /* DMB */
2768 dir->dir_type = SMBC_SERVER;
2771 * Get the backup list ...
2773 if (!name_status_find(server, 0, 0,
2774 rem_ip, buserver)) {
2776 DEBUG(0, ("Could not get name of "
2777 "local/domain master browser "
2778 "for server %s\n", server));
2780 SAFE_FREE(dir->fname);
2789 * Get a connection to IPC$ on the server if
2790 * we do not already have one
2792 srv = smbc_server(context, True,
2794 workgroup, user, password);
2796 DEBUG(0, ("got no contact to IPC$\n"));
2798 SAFE_FREE(dir->fname);
2807 /* Now, list the servers ... */
2808 if (!cli_NetServerEnum(srv->cli, server,
2809 0x0000FFFE, list_fn,
2813 SAFE_FREE(dir->fname);
2819 (resolve_name(server, &rem_ip, 0x20))) {
2821 /* If we hadn't found the server, get one now */
2823 srv = smbc_server(context, True,
2831 SAFE_FREE(dir->fname);
2838 dir->dir_type = SMBC_FILE_SHARE;
2841 /* List the shares ... */
2843 if (net_share_enum_rpc(
2846 (void *) dir) < 0 &&
2852 errno = cli_errno(srv->cli);
2854 SAFE_FREE(dir->fname);
2861 /* Neither the workgroup nor server exists */
2862 errno = ECONNREFUSED;
2864 SAFE_FREE(dir->fname);
2873 * The server and share are specified ... work from
2877 struct cli_state *targetcli;
2879 /* We connect to the server and list the directory */
2880 dir->dir_type = SMBC_FILE_SHARE;
2882 srv = smbc_server(context, True, server, share,
2883 workgroup, user, password);
2888 SAFE_FREE(dir->fname);
2897 /* Now, list the files ... */
2899 p = path + strlen(path);
2900 pstrcat(path, "\\*");
2902 if (!cli_resolve_path("", srv->cli, path,
2903 &targetcli, targetpath))
2905 d_printf("Could not resolve %s\n", path);
2907 SAFE_FREE(dir->fname);
2913 if (cli_list(targetcli, targetpath,
2914 aDIR | aSYSTEM | aHIDDEN,
2915 dir_list_fn, (void *)dir) < 0) {
2918 SAFE_FREE(dir->fname);
2921 saved_errno = smbc_errno(context, targetcli);
2923 if (saved_errno == EINVAL) {
2925 * See if they asked to opendir something
2926 * other than a directory. If so, the
2927 * converted error value we got would have
2928 * been EINVAL rather than ENOTDIR.
2930 *p = '\0'; /* restore original path */
2932 if (smbc_getatr(context, srv, path,
2934 NULL, NULL, NULL, NULL,
2936 ! IS_DOS_DIR(mode)) {
2938 /* It is. Correct the error value */
2939 saved_errno = ENOTDIR;
2944 * If there was an error and the server is no
2947 cb = &context->callbacks;
2948 if (cli_is_error(targetcli) &&
2949 cb->check_server_fn(context, srv)) {
2951 /* ... then remove it. */
2952 if (cb->remove_unused_server_fn(context,
2955 * We could not remove the server
2956 * completely, remove it from the
2957 * cache so we will not get it
2958 * again. It will be removed when the
2959 * last file/dir is closed.
2961 cb->remove_cached_srv_fn(context, srv);
2965 errno = saved_errno;
2972 DLIST_ADD(context->internal->_files, dir);
2978 * Routine to close a directory
2982 smbc_closedir_ctx(SMBCCTX *context,
2986 if (!context || !context->internal ||
2987 !context->internal->_initialized) {
2994 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3001 smbc_remove_dir(dir); /* Clean it up */
3003 DLIST_REMOVE(context->internal->_files, dir);
3007 SAFE_FREE(dir->fname);
3008 SAFE_FREE(dir); /* Free the space too */
3016 smbc_readdir_internal(SMBCCTX * context,
3017 struct smbc_dirent *dest,
3018 struct smbc_dirent *src,
3019 int max_namebuf_len)
3021 if (context->options.urlencode_readdir_entries) {
3023 /* url-encode the name. get back remaining buffer space */
3025 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3027 /* We now know the name length */
3028 dest->namelen = strlen(dest->name);
3030 /* Save the pointer to the beginning of the comment */
3031 dest->comment = dest->name + dest->namelen + 1;
3033 /* Copy the comment */
3034 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3035 dest->comment[max_namebuf_len - 1] = '\0';
3037 /* Save other fields */
3038 dest->smbc_type = src->smbc_type;
3039 dest->commentlen = strlen(dest->comment);
3040 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3044 /* No encoding. Just copy the entry as is. */
3045 memcpy(dest, src, src->dirlen);
3046 dest->comment = (char *)(&dest->name + src->namelen + 1);
3052 * Routine to get a directory entry
3055 struct smbc_dirent *
3056 smbc_readdir_ctx(SMBCCTX *context,
3060 struct smbc_dirent *dirp, *dirent;
3062 /* Check that all is ok first ... */
3064 if (!context || !context->internal ||
3065 !context->internal->_initialized) {
3068 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3073 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3076 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3081 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3084 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3089 if (!dir->dir_next) {
3093 dirent = dir->dir_next->dirent;
3101 dirp = (struct smbc_dirent *)context->internal->_dirent;
3102 maxlen = (sizeof(context->internal->_dirent) -
3103 sizeof(struct smbc_dirent));
3105 smbc_readdir_internal(context, dirp, dirent, maxlen);
3107 dir->dir_next = dir->dir_next->next;
3113 * Routine to get directory entries
3117 smbc_getdents_ctx(SMBCCTX *context,
3119 struct smbc_dirent *dirp,
3125 char *ndir = (char *)dirp;
3126 struct smbc_dir_list *dirlist;
3128 /* Check that all is ok first ... */
3130 if (!context || !context->internal ||
3131 !context->internal->_initialized) {
3138 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3145 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3153 * Now, retrieve the number of entries that will fit in what was passed
3154 * We have to figure out if the info is in the list, or we need to
3155 * send a request to the server to get the info.
3158 while ((dirlist = dir->dir_next)) {
3159 struct smbc_dirent *dirent;
3161 if (!dirlist->dirent) {
3163 errno = ENOENT; /* Bad error */
3168 /* Do urlencoding of next entry, if so selected */
3169 dirent = (struct smbc_dirent *)context->internal->_dirent;
3170 maxlen = (sizeof(context->internal->_dirent) -
3171 sizeof(struct smbc_dirent));
3172 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3174 reqd = dirent->dirlen;
3178 if (rem < count) { /* We managed to copy something */
3184 else { /* Nothing copied ... */
3186 errno = EINVAL; /* Not enough space ... */
3193 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3195 ((struct smbc_dirent *)ndir)->comment =
3196 (char *)(&((struct smbc_dirent *)ndir)->name +
3204 dir->dir_next = dirlist = dirlist -> next;
3215 * Routine to create a directory ...
3219 smbc_mkdir_ctx(SMBCCTX *context,
3229 pstring path, targetpath;
3230 struct cli_state *targetcli;
3232 if (!context || !context->internal ||
3233 !context->internal->_initialized) {
3247 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3249 if (smbc_parse_path(context, fname,
3250 workgroup, sizeof(workgroup),
3251 server, sizeof(server),
3252 share, sizeof(share),
3255 password, sizeof(password),
3261 if (user[0] == (char)0) fstrcpy(user, context->user);
3263 srv = smbc_server(context, True,
3264 server, share, workgroup, user, password);
3268 return -1; /* errno set by smbc_server */
3272 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3273 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3275 d_printf("Could not resolve %s\n", path);
3278 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3280 if (!cli_mkdir(targetcli, targetpath)) {
3282 errno = smbc_errno(context, targetcli);
3292 * Our list function simply checks to see if a directory is not empty
3295 static int smbc_rmdir_dirempty = True;
3298 rmdir_list_fn(const char *mnt,
3303 if (strncmp(finfo->name, ".", 1) != 0 &&
3304 strncmp(finfo->name, "..", 2) != 0) {
3306 smbc_rmdir_dirempty = False;
3311 * Routine to remove a directory
3315 smbc_rmdir_ctx(SMBCCTX *context,
3326 struct cli_state *targetcli;
3328 if (!context || !context->internal ||
3329 !context->internal->_initialized) {
3343 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3345 if (smbc_parse_path(context, fname,
3346 workgroup, sizeof(workgroup),
3347 server, sizeof(server),
3348 share, sizeof(share),
3351 password, sizeof(password),
3358 if (user[0] == (char)0) fstrcpy(user, context->user);
3360 srv = smbc_server(context, True,
3361 server, share, workgroup, user, password);
3365 return -1; /* errno set by smbc_server */
3369 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3370 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3372 d_printf("Could not resolve %s\n", path);
3375 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3378 if (!cli_rmdir(targetcli, targetpath)) {
3380 errno = smbc_errno(context, targetcli);
3382 if (errno == EACCES) { /* Check if the dir empty or not */
3384 /* Local storage to avoid buffer overflows */
3387 smbc_rmdir_dirempty = True; /* Make this so ... */
3389 pstrcpy(lpath, targetpath);
3390 pstrcat(lpath, "\\*");
3392 if (cli_list(targetcli, lpath,
3393 aDIR | aSYSTEM | aHIDDEN,
3394 rmdir_list_fn, NULL) < 0) {
3396 /* Fix errno to ignore latest error ... */
3397 DEBUG(5, ("smbc_rmdir: "
3398 "cli_list returned an error: %d\n",
3399 smbc_errno(context, targetcli)));
3404 if (smbc_rmdir_dirempty)
3420 * Routine to return the current directory position
3424 smbc_telldir_ctx(SMBCCTX *context,
3427 off_t ret_val; /* Squash warnings about cast */
3429 if (!context || !context->internal ||
3430 !context->internal->_initialized) {
3437 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3444 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3452 * We return the pointer here as the offset
3454 ret_val = (off_t)(long)dir->dir_next;
3460 * A routine to run down the list and see if the entry is OK
3463 struct smbc_dir_list *
3464 smbc_check_dir_ent(struct smbc_dir_list *list,
3465 struct smbc_dirent *dirent)
3468 /* Run down the list looking for what we want */
3472 struct smbc_dir_list *tmp = list;
3476 if (tmp->dirent == dirent)
3485 return NULL; /* Not found, or an error */
3491 * Routine to seek on a directory
3495 smbc_lseekdir_ctx(SMBCCTX *context,
3499 long int l_offset = offset; /* Handle problems of size */
3500 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3501 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3503 if (!context || !context->internal ||
3504 !context->internal->_initialized) {
3511 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3518 /* Now, check what we were passed and see if it is OK ... */
3520 if (dirent == NULL) { /* Seek to the begining of the list */
3522 dir->dir_next = dir->dir_list;
3527 /* Now, run down the list and make sure that the entry is OK */
3528 /* This may need to be changed if we change the format of the list */
3530 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3532 errno = EINVAL; /* Bad entry */
3537 dir->dir_next = list_ent;
3544 * Routine to fstat a dir
3548 smbc_fstatdir_ctx(SMBCCTX *context,
3553 if (!context || !context->internal ||
3554 !context->internal->_initialized) {
3561 /* No code yet ... */
3568 smbc_chmod_ctx(SMBCCTX *context,
3581 if (!context || !context->internal ||
3582 !context->internal->_initialized) {
3584 errno = EINVAL; /* Best I can think of ... */
3596 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3598 if (smbc_parse_path(context, fname,
3599 workgroup, sizeof(workgroup),
3600 server, sizeof(server),
3601 share, sizeof(share),
3604 password, sizeof(password),
3610 if (user[0] == (char)0) fstrcpy(user, context->user);
3612 srv = smbc_server(context, True,
3613 server, share, workgroup, user, password);
3616 return -1; /* errno set by smbc_server */
3621 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3622 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3623 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3624 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3626 if (!cli_setatr(srv->cli, path, mode, 0)) {
3627 errno = smbc_errno(context, srv->cli);
3635 smbc_utimes_ctx(SMBCCTX *context,
3637 struct timeval *tbuf)
3649 if (!context || !context->internal ||
3650 !context->internal->_initialized) {
3652 errno = EINVAL; /* Best I can think of ... */
3665 access_time = write_time = time(NULL);
3667 access_time = tbuf[0].tv_sec;
3668 write_time = tbuf[1].tv_sec;
3677 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3678 atimebuf[sizeof(atimebuf) - 1] = '\0';
3679 if ((p = strchr(atimebuf, '\n')) != NULL) {
3683 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3684 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3685 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3689 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3690 fname, atimebuf, mtimebuf);
3693 if (smbc_parse_path(context, fname,
3694 workgroup, sizeof(workgroup),
3695 server, sizeof(server),
3696 share, sizeof(share),
3699 password, sizeof(password),
3705 if (user[0] == (char)0) fstrcpy(user, context->user);
3707 srv = smbc_server(context, True,
3708 server, share, workgroup, user, password);
3711 return -1; /* errno set by smbc_server */
3714 if (!smbc_setatr(context, srv, path,
3715 0, access_time, write_time, 0, 0)) {
3716 return -1; /* errno set by smbc_setatr */
3723 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
3724 However NT4 gives a "The information may have been modified by a
3725 computer running Windows NT 5.0" if denied ACEs do not appear before
3729 ace_compare(SEC_ACE *ace1,
3732 if (sec_ace_equal(ace1, ace2))
3735 if (ace1->type != ace2->type)
3736 return ace2->type - ace1->type;
3738 if (sid_compare(&ace1->trustee, &ace2->trustee))
3739 return sid_compare(&ace1->trustee, &ace2->trustee);
3741 if (ace1->flags != ace2->flags)
3742 return ace1->flags - ace2->flags;
3744 if (ace1->info.mask != ace2->info.mask)
3745 return ace1->info.mask - ace2->info.mask;
3747 if (ace1->size != ace2->size)
3748 return ace1->size - ace2->size;
3750 return memcmp(ace1, ace2, sizeof(SEC_ACE));
3755 sort_acl(SEC_ACL *the_acl)
3758 if (!the_acl) return;
3760 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]),
3761 QSORT_CAST ace_compare);
3763 for (i=1;i<the_acl->num_aces;) {
3764 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
3766 for (j=i; j<the_acl->num_aces-1; j++) {
3767 the_acl->ace[j] = the_acl->ace[j+1];
3769 the_acl->num_aces--;
3776 /* convert a SID to a string, either numeric or username/group */
3778 convert_sid_to_string(struct cli_state *ipc_cli,
3784 char **domains = NULL;
3785 char **names = NULL;
3786 enum SID_NAME_USE *types = NULL;
3787 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3788 sid_to_string(str, sid);
3791 return; /* no lookup desired */
3798 /* Ask LSA to convert the sid to a name */
3800 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ipc_cli->mem_ctx,
3801 pol, 1, sid, &domains,
3803 !domains || !domains[0] || !names || !names[0]) {
3809 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3810 domains[0], lp_winbind_separator(),
3814 /* convert a string to a SID, either numeric or username/group */
3816 convert_string_to_sid(struct cli_state *ipc_cli,
3822 enum SID_NAME_USE *types = NULL;
3823 DOM_SID *sids = NULL;
3825 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3832 if (strncmp(str, "S-", 2) == 0) {
3833 return string_to_sid(sid, str);
3840 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx,
3841 pol, 1, &str, NULL, &sids,
3847 sid_copy(sid, &sids[0]);
3854 /* parse an ACE in the same format as print_ace() */
3856 parse_ace(struct cli_state *ipc_cli,
3866 unsigned int aflags;
3870 const struct perm_value *v;
3876 /* These values discovered by inspection */
3877 static const struct perm_value special_values[] = {
3878 { "R", 0x00120089 },
3879 { "W", 0x00120116 },
3880 { "X", 0x001200a0 },
3881 { "D", 0x00010000 },
3882 { "P", 0x00040000 },
3883 { "O", 0x00080000 },
3887 static const struct perm_value standard_values[] = {
3888 { "READ", 0x001200a9 },
3889 { "CHANGE", 0x001301bf },
3890 { "FULL", 0x001f01ff },
3896 p = strchr_m(str,':');
3897 if (!p) return False;
3900 /* Try to parse numeric form */
3902 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3903 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3907 /* Try to parse text form */
3909 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3914 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3918 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3919 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3920 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3921 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3926 /* Only numeric form accepted for flags at present */
3928 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3929 sscanf(tok, "%i", &aflags))) {
3933 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3937 if (strncmp(tok, "0x", 2) == 0) {
3938 if (sscanf(tok, "%i", &amask) != 1) {
3944 for (v = standard_values; v->perm; v++) {
3945 if (strcmp(tok, v->perm) == 0) {
3956 for (v = special_values; v->perm; v++) {
3957 if (v->perm[0] == *p) {
3963 if (!found) return False;
3973 init_sec_ace(ace, &sid, atype, mask, aflags);
3977 /* add an ACE to a list of ACEs in a SEC_ACL */
3979 add_ace(SEC_ACL **the_acl,
3987 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
3991 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
3994 memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
3995 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
3996 newacl = make_sec_acl(ctx, (*the_acl)->revision,
3997 1+(*the_acl)->num_aces, aces);
3999 (*the_acl) = newacl;
4004 /* parse a ascii version of a security descriptor */
4006 sec_desc_parse(TALLOC_CTX *ctx,
4007 struct cli_state *ipc_cli,
4012 const char *p = str;
4014 SEC_DESC *ret = NULL;
4016 DOM_SID *grp_sid=NULL;
4017 DOM_SID *owner_sid=NULL;
4021 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4023 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4024 revision = strtol(tok+9, NULL, 16);
4028 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4030 DEBUG(5, ("OWNER specified more than once!\n"));
4033 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4035 !convert_string_to_sid(ipc_cli, pol,
4037 owner_sid, tok+6)) {
4038 DEBUG(5, ("Failed to parse owner sid\n"));
4044 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4046 DEBUG(5, ("OWNER specified more than once!\n"));
4049 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4051 !convert_string_to_sid(ipc_cli, pol,
4053 owner_sid, tok+7)) {
4054 DEBUG(5, ("Failed to parse owner sid\n"));
4060 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4062 DEBUG(5, ("GROUP specified more than once!\n"));
4065 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4067 !convert_string_to_sid(ipc_cli, pol,
4070 DEBUG(5, ("Failed to parse group sid\n"));
4076 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4078 DEBUG(5, ("GROUP specified more than once!\n"));
4081 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4083 !convert_string_to_sid(ipc_cli, pol,
4086 DEBUG(5, ("Failed to parse group sid\n"));
4092 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4094 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4095 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4098 if(!add_ace(&dacl, &ace, ctx)) {
4099 DEBUG(5, ("Failed to add ACL %s\n", tok));
4105 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4107 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4108 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4111 if(!add_ace(&dacl, &ace, ctx)) {
4112 DEBUG(5, ("Failed to add ACL %s\n", tok));
4118 DEBUG(5, ("Failed to parse security descriptor\n"));
4122 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4123 owner_sid, grp_sid, NULL, dacl, &sd_size);
4127 SAFE_FREE(owner_sid);
4133 /* Obtain the current dos attributes */
4134 static DOS_ATTR_DESC *
4135 dos_attr_query(SMBCCTX *context,
4137 const char *filename,
4140 struct timespec create_time_ts;
4141 struct timespec write_time_ts;
4142 struct timespec access_time_ts;
4143 struct timespec change_time_ts;
4146 SMB_INO_T inode = 0;
4149 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4155 /* Obtain the DOS attributes */
4156 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4164 errno = smbc_errno(context, srv->cli);
4165 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4172 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4173 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4174 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4175 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4182 /* parse a ascii version of a security descriptor */
4184 dos_attr_parse(SMBCCTX *context,
4190 const char *p = str;
4193 const char * create_time_attr;
4194 const char * access_time_attr;
4195 const char * write_time_attr;
4196 const char * change_time_attr;
4199 /* Determine whether to use old-style or new-style attribute names */
4200 if (context->internal->_full_time_names) {
4201 /* new-style names */
4202 attr_strings.create_time_attr = "CREATE_TIME";
4203 attr_strings.access_time_attr = "ACCESS_TIME";
4204 attr_strings.write_time_attr = "WRITE_TIME";
4205 attr_strings.change_time_attr = "CHANGE_TIME";
4207 /* old-style names */
4208 attr_strings.create_time_attr = NULL;
4209 attr_strings.access_time_attr = "A_TIME";
4210 attr_strings.write_time_attr = "M_TIME";
4211 attr_strings.change_time_attr = "C_TIME";
4214 /* if this is to set the entire ACL... */
4216 /* ... then increment past the first colon if there is one */
4217 if ((p = strchr(str, ':')) != NULL) {
4224 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4226 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4227 dad->mode = strtol(tok+5, NULL, 16);
4231 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4232 dad->size = (SMB_OFF_T)atof(tok+5);
4236 n = strlen(attr_strings.access_time_attr);
4237 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4238 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4242 n = strlen(attr_strings.change_time_attr);
4243 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4244 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4248 n = strlen(attr_strings.write_time_attr);
4249 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4250 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4254 n = strlen(attr_strings.create_time_attr);
4255 if (attr_strings.create_time_attr != NULL &&
4256 StrnCaseCmp(tok, attr_strings.create_time_attr, n) == 0) {
4257 dad->create_time = (time_t)strtol(tok+n+1, NULL, 10);
4261 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4262 dad->inode = (SMB_INO_T)atof(tok+6);
4268 /*****************************************************
4269 Retrieve the acls for a file.
4270 *******************************************************/
4273 cacl_get(SMBCCTX *context,
4276 struct cli_state *ipc_cli,
4292 BOOL exclude_nt_revision = False;
4293 BOOL exclude_nt_owner = False;
4294 BOOL exclude_nt_group = False;
4295 BOOL exclude_nt_acl = False;
4296 BOOL exclude_dos_mode = False;
4297 BOOL exclude_dos_size = False;
4298 BOOL exclude_dos_create_time = False;
4299 BOOL exclude_dos_access_time = False;
4300 BOOL exclude_dos_write_time = False;
4301 BOOL exclude_dos_change_time = False;
4302 BOOL exclude_dos_inode = False;
4303 BOOL numeric = True;
4304 BOOL determine_size = (bufsize == 0);
4308 fstring name_sandbox;
4312 struct timespec create_time_ts;
4313 struct timespec write_time_ts;
4314 struct timespec access_time_ts;
4315 struct timespec change_time_ts;
4316 time_t create_time = (time_t)0;
4317 time_t write_time = (time_t)0;
4318 time_t access_time = (time_t)0;
4319 time_t change_time = (time_t)0;
4323 struct cli_state *cli = srv->cli;
4325 const char * create_time_attr;
4326 const char * access_time_attr;
4327 const char * write_time_attr;
4328 const char * change_time_attr;
4331 const char * create_time_attr;
4332 const char * access_time_attr;
4333 const char * write_time_attr;
4334 const char * change_time_attr;
4335 } excl_attr_strings;
4337 /* Determine whether to use old-style or new-style attribute names */
4338 if (context->internal->_full_time_names) {
4339 /* new-style names */
4340 attr_strings.create_time_attr = "CREATE_TIME";
4341 attr_strings.access_time_attr = "ACCESS_TIME";
4342 attr_strings.write_time_attr = "WRITE_TIME";
4343 attr_strings.change_time_attr = "CHANGE_TIME";
4345 excl_attr_strings.create_time_attr = "CREATE_TIME";
4346 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4347 excl_attr_strings.write_time_attr = "WRITE_TIME";
4348 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4350 /* old-style names */
4351 attr_strings.create_time_attr = NULL;
4352 attr_strings.access_time_attr = "A_TIME";
4353 attr_strings.write_time_attr = "M_TIME";
4354 attr_strings.change_time_attr = "C_TIME";
4356 excl_attr_strings.create_time_attr = NULL;
4357 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4358 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4359 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4362 /* Copy name so we can strip off exclusions (if any are specified) */
4363 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4365 /* Ensure name is null terminated */
4366 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4368 /* Play in the sandbox */
4369 name = name_sandbox;
4371 /* If there are any exclusions, point to them and mask them from name */
4372 if ((pExclude = strchr(name, '!')) != NULL)
4377 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4378 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4379 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4380 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4381 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4382 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4383 numeric = (* (name + strlen(name) - 1) != '+');
4385 /* Look for exclusions from "all" requests */
4386 if (all || all_nt || all_dos) {
4388 /* Exclusions are delimited by '!' */
4391 pExclude = (p == NULL ? NULL : p + 1)) {
4393 /* Find end of this exclusion name */
4394 if ((p = strchr(pExclude, '!')) != NULL)
4399 /* Which exclusion name is this? */
4400 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4401 exclude_nt_revision = True;
4403 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4404 exclude_nt_owner = True;
4406 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4407 exclude_nt_group = True;
4409 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4410 exclude_nt_acl = True;
4412 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4413 exclude_dos_mode = True;
4415 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4416 exclude_dos_size = True;
4418 else if (excl_attr_strings.create_time_attr != NULL &&
4419 StrCaseCmp(pExclude,
4420 excl_attr_strings.change_time_attr) == 0) {
4421 exclude_dos_create_time = True;
4423 else if (StrCaseCmp(pExclude,
4424 excl_attr_strings.access_time_attr) == 0) {
4425 exclude_dos_access_time = True;
4427 else if (StrCaseCmp(pExclude,
4428 excl_attr_strings.write_time_attr) == 0) {
4429 exclude_dos_write_time = True;
4431 else if (StrCaseCmp(pExclude,
4432 excl_attr_strings.change_time_attr) == 0) {
4433 exclude_dos_change_time = True;
4435 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4436 exclude_dos_inode = True;
4439 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4450 * If we are (possibly) talking to an NT or new system and some NT
4451 * attributes have been requested...
4453 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4454 /* Point to the portion after "system.nt_sec_desc." */
4455 name += 19; /* if (all) this will be invalid but unused */
4457 /* ... then obtain any NT attributes which were requested */
4458 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4461 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4462 filename, cli_errstr(cli)));
4467 sd = cli_query_secdesc(cli, fnum, ctx);
4471 ("cacl_get Failed to query old descriptor\n"));
4476 cli_close(cli, fnum);
4478 if (! exclude_nt_revision) {
4479 if (all || all_nt) {
4480 if (determine_size) {
4481 p = talloc_asprintf(ctx,
4490 n = snprintf(buf, bufsize,
4494 } else if (StrCaseCmp(name, "revision") == 0) {
4495 if (determine_size) {
4496 p = talloc_asprintf(ctx, "%d",
4504 n = snprintf(buf, bufsize, "%d",
4509 if (!determine_size && n > bufsize) {
4518 if (! exclude_nt_owner) {
4519 /* Get owner and group sid */
4520 if (sd->owner_sid) {
4521 convert_sid_to_string(ipc_cli, pol,
4526 fstrcpy(sidstr, "");
4529 if (all || all_nt) {
4530 if (determine_size) {
4531 p = talloc_asprintf(ctx, ",OWNER:%s",
4539 n = snprintf(buf, bufsize,
4540 ",OWNER:%s", sidstr);
4542 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4543 if (determine_size) {
4544 p = talloc_asprintf(ctx, "%s", sidstr);
4551 n = snprintf(buf, bufsize, "%s",
4556 if (!determine_size && n > bufsize) {
4565 if (! exclude_nt_group) {
4567 convert_sid_to_string(ipc_cli, pol,
4571 fstrcpy(sidstr, "");
4574 if (all || all_nt) {
4575 if (determine_size) {
4576 p = talloc_asprintf(ctx, ",GROUP:%s",
4584 n = snprintf(buf, bufsize,
4585 ",GROUP:%s", sidstr);
4587 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4588 if (determine_size) {
4589 p = talloc_asprintf(ctx, "%s", sidstr);
4596 n = snprintf(buf, bufsize,
4601 if (!determine_size && n > bufsize) {
4610 if (! exclude_nt_acl) {
4611 /* Add aces to value buffer */
4612 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4614 SEC_ACE *ace = &sd->dacl->ace[i];
4615 convert_sid_to_string(ipc_cli, pol,
4619 if (all || all_nt) {
4620 if (determine_size) {
4621 p = talloc_asprintf(
4637 ",ACL:%s:%d/%d/0x%08x",
4643 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4644 StrCaseCmp(name+3, sidstr) == 0) ||
4645 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4646 StrCaseCmp(name+4, sidstr) == 0)) {
4647 if (determine_size) {
4648 p = talloc_asprintf(
4660 n = snprintf(buf, bufsize,
4666 } else if (all_nt_acls) {
4667 if (determine_size) {
4668 p = talloc_asprintf(
4670 "%s%s:%d/%d/0x%08x",
4682 n = snprintf(buf, bufsize,
4683 "%s%s:%d/%d/0x%08x",
4701 /* Restore name pointer to its original value */
4705 if (all || some_dos) {
4706 /* Point to the portion after "system.dos_attr." */
4707 name += 16; /* if (all) this will be invalid but unused */
4709 /* Obtain the DOS attributes */
4710 if (!smbc_getatr(context, srv, filename, &mode, &size,
4717 errno = smbc_errno(context, srv->cli);
4722 create_time = convert_timespec_to_time_t(create_time_ts);
4723 access_time = convert_timespec_to_time_t(access_time_ts);
4724 write_time = convert_timespec_to_time_t(write_time_ts);
4725 change_time = convert_timespec_to_time_t(change_time_ts);
4727 if (! exclude_dos_mode) {
4728 if (all || all_dos) {
4729 if (determine_size) {
4730 p = talloc_asprintf(ctx,
4743 n = snprintf(buf, bufsize,
4751 } else if (StrCaseCmp(name, "mode") == 0) {
4752 if (determine_size) {
4753 p = talloc_asprintf(ctx, "0x%x", mode);
4760 n = snprintf(buf, bufsize,
4765 if (!determine_size && n > bufsize) {
4774 if (! exclude_dos_size) {
4775 if (all || all_dos) {
4776 if (determine_size) {
4777 p = talloc_asprintf(
4787 n = snprintf(buf, bufsize,
4791 } else if (StrCaseCmp(name, "size") == 0) {
4792 if (determine_size) {
4793 p = talloc_asprintf(
4803 n = snprintf(buf, bufsize,
4809 if (!determine_size && n > bufsize) {
4818 if (! exclude_dos_create_time &&
4819 attr_strings.create_time_attr != NULL) {
4820 if (all || all_dos) {
4821 if (determine_size) {
4822 p = talloc_asprintf(ctx,
4824 attr_strings.create_time_attr,
4832 n = snprintf(buf, bufsize,
4834 attr_strings.create_time_attr,
4837 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
4838 if (determine_size) {
4839 p = talloc_asprintf(ctx, "%lu", create_time);
4846 n = snprintf(buf, bufsize,
4847 "%lu", create_time);
4851 if (!determine_size && n > bufsize) {
4860 if (! exclude_dos_access_time) {
4861 if (all || all_dos) {
4862 if (determine_size) {
4863 p = talloc_asprintf(ctx,
4865 attr_strings.access_time_attr,
4873 n = snprintf(buf, bufsize,
4875 attr_strings.access_time_attr,
4878 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
4879 if (determine_size) {
4880 p = talloc_asprintf(ctx, "%lu", access_time);
4887 n = snprintf(buf, bufsize,
4888 "%lu", access_time);
4892 if (!determine_size && n > bufsize) {
4901 if (! exclude_dos_write_time) {
4902 if (all || all_dos) {
4903 if (determine_size) {
4904 p = talloc_asprintf(ctx,
4906 attr_strings.write_time_attr,
4914 n = snprintf(buf, bufsize,
4916 attr_strings.write_time_attr,
4919 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
4920 if (determine_size) {
4921 p = talloc_asprintf(ctx, "%lu", write_time);
4928 n = snprintf(buf, bufsize,
4933 if (!determine_size && n > bufsize) {
4942 if (! exclude_dos_change_time) {
4943 if (all || all_dos) {
4944 if (determine_size) {
4945 p = talloc_asprintf(ctx,
4947 attr_strings.change_time_attr,
4955 n = snprintf(buf, bufsize,
4957 attr_strings.change_time_attr,
4960 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
4961 if (determine_size) {
4962 p = talloc_asprintf(ctx, "%lu", change_time);
4969 n = snprintf(buf, bufsize,
4970 "%lu", change_time);
4974 if (!determine_size && n > bufsize) {
4983 if (! exclude_dos_inode) {
4984 if (all || all_dos) {
4985 if (determine_size) {
4986 p = talloc_asprintf(
4996 n = snprintf(buf, bufsize,
5000 } else if (StrCaseCmp(name, "inode") == 0) {
5001 if (determine_size) {
5002 p = talloc_asprintf(
5012 n = snprintf(buf, bufsize,
5018 if (!determine_size && n > bufsize) {
5027 /* Restore name pointer to its original value */
5040 /*****************************************************
5041 set the ACLs on a file given an ascii description
5042 *******************************************************/
5044 cacl_set(TALLOC_CTX *ctx,
5045 struct cli_state *cli,
5046 struct cli_state *ipc_cli,
5048 const char *filename,
5049 const char *the_acl,
5055 SEC_DESC *sd = NULL, *old;
5056 SEC_ACL *dacl = NULL;
5057 DOM_SID *owner_sid = NULL;
5058 DOM_SID *grp_sid = NULL;
5063 BOOL numeric = True;
5065 /* the_acl will be null for REMOVE_ALL operations */
5067 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5071 /* if this is to set the entire ACL... */
5072 if (*the_acl == '*') {
5073 /* ... then increment past the first colon */
5077 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5078 CONST_DISCARD(char *, the_acl));
5086 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5087 that doesn't deref sd */
5089 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5094 /* The desired access below is the only one I could find that works
5095 with NT4, W2KP and Samba */
5097 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5100 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5101 filename, cli_errstr(cli)));
5106 old = cli_query_secdesc(cli, fnum, ctx);
5109 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5114 cli_close(cli, fnum);
5117 case SMBC_XATTR_MODE_REMOVE_ALL:
5118 old->dacl->num_aces = 0;
5119 SAFE_FREE(old->dacl->ace);
5120 SAFE_FREE(old->dacl);
5125 case SMBC_XATTR_MODE_REMOVE:
5126 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5129 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5130 if (sec_ace_equal(&sd->dacl->ace[i],
5131 &old->dacl->ace[j])) {
5133 for (k=j; k<old->dacl->num_aces-1;k++) {
5135 old->dacl->ace[k+1];
5137 old->dacl->num_aces--;
5138 if (old->dacl->num_aces == 0) {
5139 SAFE_FREE(old->dacl->ace);
5140 SAFE_FREE(old->dacl);
5157 case SMBC_XATTR_MODE_ADD:
5158 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5161 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5162 if (sid_equal(&sd->dacl->ace[i].trustee,
5163 &old->dacl->ace[j].trustee)) {
5164 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5169 old->dacl->ace[j] = sd->dacl->ace[i];
5175 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5181 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5182 add_ace(&old->dacl, &sd->dacl->ace[i], ctx);
5188 case SMBC_XATTR_MODE_SET:
5190 owner_sid = old->owner_sid;
5191 grp_sid = old->grp_sid;
5195 case SMBC_XATTR_MODE_CHOWN:
5196 owner_sid = sd->owner_sid;
5199 case SMBC_XATTR_MODE_CHGRP:
5200 grp_sid = sd->grp_sid;
5204 /* Denied ACE entries must come before allowed ones */
5205 sort_acl(old->dacl);
5207 /* Create new security descriptor and set it */
5208 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5209 owner_sid, grp_sid, NULL, dacl, &sd_size);
5211 fnum = cli_nt_create(cli, filename,
5212 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5215 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5216 filename, cli_errstr(cli)));
5221 if (!cli_set_secdesc(cli, fnum, sd)) {
5222 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5229 cli_close(cli, fnum);
5240 smbc_setxattr_ctx(SMBCCTX *context,
5261 const char * create_time_attr;
5262 const char * access_time_attr;
5263 const char * write_time_attr;
5264 const char * change_time_attr;
5267 if (!context || !context->internal ||
5268 !context->internal->_initialized) {
5270 errno = EINVAL; /* Best I can think of ... */
5282 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5283 fname, name, (int) size, (const char*)value));
5285 if (smbc_parse_path(context, fname,
5286 workgroup, sizeof(workgroup),
5287 server, sizeof(server),
5288 share, sizeof(share),
5291 password, sizeof(password),
5297 if (user[0] == (char)0) fstrcpy(user, context->user);
5299 srv = smbc_server(context, True,
5300 server, share, workgroup, user, password);
5302 return -1; /* errno set by smbc_server */
5305 if (! srv->no_nt_session) {
5306 ipc_srv = smbc_attr_server(context, server, share,
5307 workgroup, user, password,
5309 srv->no_nt_session = True;
5314 ctx = talloc_init("smbc_setxattr");
5321 * Are they asking to set the entire set of known attributes?
5323 if (StrCaseCmp(name, "system.*") == 0 ||
5324 StrCaseCmp(name, "system.*+") == 0) {
5327 talloc_asprintf(ctx, "%s:%s",
5328 name+7, (const char *) value);
5336 ret = cacl_set(ctx, srv->cli,
5337 ipc_srv->cli, &pol, path,
5340 ? SMBC_XATTR_MODE_SET
5341 : SMBC_XATTR_MODE_ADD),
5347 /* get a DOS Attribute Descriptor with current attributes */
5348 dad = dos_attr_query(context, ctx, path, srv);
5350 /* Overwrite old with new, using what was provided */
5351 dos_attr_parse(context, dad, srv, namevalue);
5353 /* Set the new DOS attributes */
5354 if (! smbc_setatr(context, srv, path,
5361 /* cause failure if NT failed too */
5366 /* we only fail if both NT and DOS sets failed */
5367 if (ret < 0 && ! dad) {
5368 ret = -1; /* in case dad was null */
5374 talloc_destroy(ctx);
5379 * Are they asking to set an access control element or to set
5380 * the entire access control list?
5382 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5383 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5384 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5385 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5386 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5390 talloc_asprintf(ctx, "%s:%s",
5391 name+19, (const char *) value);
5394 ret = -1; /* errno set by smbc_server() */
5396 else if (! namevalue) {
5400 ret = cacl_set(ctx, srv->cli,
5401 ipc_srv->cli, &pol, path,
5404 ? SMBC_XATTR_MODE_SET
5405 : SMBC_XATTR_MODE_ADD),
5408 talloc_destroy(ctx);
5413 * Are they asking to set the owner?
5415 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5416 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5420 talloc_asprintf(ctx, "%s:%s",
5421 name+19, (const char *) value);
5425 ret = -1; /* errno set by smbc_server() */
5427 else if (! namevalue) {
5431 ret = cacl_set(ctx, srv->cli,
5432 ipc_srv->cli, &pol, path,
5433 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5435 talloc_destroy(ctx);
5440 * Are they asking to set the group?
5442 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5443 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5447 talloc_asprintf(ctx, "%s:%s",
5448 name+19, (const char *) value);
5451 /* errno set by smbc_server() */
5454 else if (! namevalue) {
5458 ret = cacl_set(ctx, srv->cli,
5459 ipc_srv->cli, &pol, path,
5460 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5462 talloc_destroy(ctx);
5466 /* Determine whether to use old-style or new-style attribute names */
5467 if (context->internal->_full_time_names) {
5468 /* new-style names */
5469 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5470 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5471 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5472 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5474 /* old-style names */
5475 attr_strings.create_time_attr = NULL;
5476 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5477 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5478 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5482 * Are they asking to set a DOS attribute?
5484 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5485 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5486 (attr_strings.create_time_attr != NULL &&
5487 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5488 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5489 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5490 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5492 /* get a DOS Attribute Descriptor with current attributes */
5493 dad = dos_attr_query(context, ctx, path, srv);
5496 talloc_asprintf(ctx, "%s:%s",
5497 name+16, (const char *) value);
5502 /* Overwrite old with provided new params */
5503 dos_attr_parse(context, dad, srv, namevalue);
5505 /* Set the new DOS attributes */
5506 ret2 = smbc_setatr(context, srv, path,
5513 /* ret2 has True (success) / False (failure) */
5524 talloc_destroy(ctx);
5528 /* Unsupported attribute name */
5529 talloc_destroy(ctx);
5535 smbc_getxattr_ctx(SMBCCTX *context,
5553 const char * create_time_attr;
5554 const char * access_time_attr;
5555 const char * write_time_attr;
5556 const char * change_time_attr;
5560 if (!context || !context->internal ||
5561 !context->internal->_initialized) {
5563 errno = EINVAL; /* Best I can think of ... */
5575 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5577 if (smbc_parse_path(context, fname,
5578 workgroup, sizeof(workgroup),
5579 server, sizeof(server),
5580 share, sizeof(share),
5583 password, sizeof(password),
5589 if (user[0] == (char)0) fstrcpy(user, context->user);
5591 srv = smbc_server(context, True,
5592 server, share, workgroup, user, password);
5594 return -1; /* errno set by smbc_server */
5597 if (! srv->no_nt_session) {
5598 ipc_srv = smbc_attr_server(context, server, share,
5599 workgroup, user, password,
5602 srv->no_nt_session = True;
5608 ctx = talloc_init("smbc:getxattr");
5614 /* Determine whether to use old-style or new-style attribute names */
5615 if (context->internal->_full_time_names) {
5616 /* new-style names */
5617 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5618 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5619 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5620 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5622 /* old-style names */
5623 attr_strings.create_time_attr = NULL;
5624 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5625 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5626 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5629 /* Are they requesting a supported attribute? */
5630 if (StrCaseCmp(name, "system.*") == 0 ||
5631 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5632 StrCaseCmp(name, "system.*+") == 0 ||
5633 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5634 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5635 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5636 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5637 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5638 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5639 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5640 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5641 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5642 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5643 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5644 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5645 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5646 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5647 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5648 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5649 (attr_strings.create_time_attr != NULL &&
5650 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5651 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5652 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5653 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5654 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
5657 ret = cacl_get(context, ctx, srv,
5658 ipc_srv == NULL ? NULL : ipc_srv->cli,
5660 CONST_DISCARD(char *, name),
5661 CONST_DISCARD(char *, value), size);
5662 if (ret < 0 && errno == 0) {
5663 errno = smbc_errno(context, srv->cli);
5665 talloc_destroy(ctx);
5669 /* Unsupported attribute name */
5670 talloc_destroy(ctx);
5677 smbc_removexattr_ctx(SMBCCTX *context,
5693 if (!context || !context->internal ||
5694 !context->internal->_initialized) {
5696 errno = EINVAL; /* Best I can think of ... */
5708 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
5710 if (smbc_parse_path(context, fname,
5711 workgroup, sizeof(workgroup),
5712 server, sizeof(server),
5713 share, sizeof(share),
5716 password, sizeof(password),
5722 if (user[0] == (char)0) fstrcpy(user, context->user);
5724 srv = smbc_server(context, True,
5725 server, share, workgroup, user, password);
5727 return -1; /* errno set by smbc_server */
5730 if (! srv->no_nt_session) {
5731 ipc_srv = smbc_attr_server(context, server, share,
5732 workgroup, user, password,
5734 srv->no_nt_session = True;
5740 return -1; /* errno set by smbc_attr_server */
5743 ctx = talloc_init("smbc_removexattr");
5749 /* Are they asking to set the entire ACL? */
5750 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5751 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
5754 ret = cacl_set(ctx, srv->cli,
5755 ipc_srv->cli, &pol, path,
5756 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
5757 talloc_destroy(ctx);
5762 * Are they asking to remove one or more spceific security descriptor
5765 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5766 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5767 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5768 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5769 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5770 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5771 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5774 ret = cacl_set(ctx, srv->cli,
5775 ipc_srv->cli, &pol, path,
5776 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
5777 talloc_destroy(ctx);
5781 /* Unsupported attribute name */
5782 talloc_destroy(ctx);
5788 smbc_listxattr_ctx(SMBCCTX *context,
5794 * This isn't quite what listxattr() is supposed to do. This returns
5795 * the complete set of attribute names, always, rather than only those
5796 * attribute names which actually exist for a file. Hmmm...
5798 const char supported_old[] =
5801 "system.nt_sec_desc.revision\0"
5802 "system.nt_sec_desc.owner\0"
5803 "system.nt_sec_desc.owner+\0"
5804 "system.nt_sec_desc.group\0"
5805 "system.nt_sec_desc.group+\0"
5806 "system.nt_sec_desc.acl.*\0"
5807 "system.nt_sec_desc.acl\0"
5808 "system.nt_sec_desc.acl+\0"
5809 "system.nt_sec_desc.*\0"
5810 "system.nt_sec_desc.*+\0"
5811 "system.dos_attr.*\0"
5812 "system.dos_attr.mode\0"
5813 "system.dos_attr.c_time\0"
5814 "system.dos_attr.a_time\0"
5815 "system.dos_attr.m_time\0"
5817 const char supported_new[] =
5820 "system.nt_sec_desc.revision\0"
5821 "system.nt_sec_desc.owner\0"
5822 "system.nt_sec_desc.owner+\0"
5823 "system.nt_sec_desc.group\0"
5824 "system.nt_sec_desc.group+\0"
5825 "system.nt_sec_desc.acl.*\0"
5826 "system.nt_sec_desc.acl\0"
5827 "system.nt_sec_desc.acl+\0"
5828 "system.nt_sec_desc.*\0"
5829 "system.nt_sec_desc.*+\0"
5830 "system.dos_attr.*\0"
5831 "system.dos_attr.mode\0"
5832 "system.dos_attr.create_time\0"
5833 "system.dos_attr.access_time\0"
5834 "system.dos_attr.write_time\0"
5835 "system.dos_attr.change_time\0"
5837 const char * supported;
5839 if (context->internal->_full_time_names) {
5840 supported = supported_new;
5842 supported = supported_old;
5846 return sizeof(supported);
5849 if (sizeof(supported) > size) {
5854 /* this can't be strcpy() because there are embedded null characters */
5855 memcpy(list, supported, sizeof(supported));
5856 return sizeof(supported);
5861 * Open a print file to be written to by other calls
5865 smbc_open_print_job_ctx(SMBCCTX *context,
5874 if (!context || !context->internal ||
5875 !context->internal->_initialized) {
5889 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
5891 if (smbc_parse_path(context, fname,
5893 server, sizeof(server),
5894 share, sizeof(share),
5897 password, sizeof(password),
5903 /* What if the path is empty, or the file exists? */
5905 return context->open(context, fname, O_WRONLY, 666);
5910 * Routine to print a file on a remote server ...
5912 * We open the file, which we assume to be on a remote server, and then
5913 * copy it to a print file on the share specified by printq.
5917 smbc_print_file_ctx(SMBCCTX *c_file,
5929 if (!c_file || !c_file->internal->_initialized || !c_print ||
5930 !c_print->internal->_initialized) {
5937 if (!fname && !printq) {
5944 /* Try to open the file for reading ... */
5946 if ((long)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
5948 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
5949 return -1; /* smbc_open sets errno */
5953 /* Now, try to open the printer file for writing */
5955 if ((long)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
5957 saverr = errno; /* Save errno */
5958 c_file->close_fn(c_file, fid1);
5964 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
5968 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
5971 c_file->close_fn(c_file, fid1);
5972 c_print->close_fn(c_print, fid2);
5981 c_file->close_fn(c_file, fid1); /* We have to close these anyway */
5982 c_print->close_fn(c_print, fid2);
5996 * Routine to list print jobs on a printer share ...
6000 smbc_list_print_jobs_ctx(SMBCCTX *context,
6002 smbc_list_print_job_fn fn)
6012 if (!context || !context->internal ||
6013 !context->internal->_initialized) {
6027 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6029 if (smbc_parse_path(context, fname,
6030 workgroup, sizeof(workgroup),
6031 server, sizeof(server),
6032 share, sizeof(share),
6035 password, sizeof(password),
6041 if (user[0] == (char)0) fstrcpy(user, context->user);
6043 srv = smbc_server(context, True,
6044 server, share, workgroup, user, password);
6048 return -1; /* errno set by smbc_server */
6052 if (cli_print_queue(srv->cli,
6053 (void (*)(struct print_job_info *))fn) < 0) {
6055 errno = smbc_errno(context, srv->cli);
6065 * Delete a print job from a remote printer share
6069 smbc_unlink_print_job_ctx(SMBCCTX *context,
6082 if (!context || !context->internal ||
6083 !context->internal->_initialized) {
6097 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6099 if (smbc_parse_path(context, fname,
6100 workgroup, sizeof(workgroup),
6101 server, sizeof(server),
6102 share, sizeof(share),
6105 password, sizeof(password),
6111 if (user[0] == (char)0) fstrcpy(user, context->user);
6113 srv = smbc_server(context, True,
6114 server, share, workgroup, user, password);
6118 return -1; /* errno set by smbc_server */
6122 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6125 errno = smbc_errno(context, srv->cli);
6126 else if (err == ERRnosuchprintjob)
6137 * Get a new empty handle to fill in with your own info
6140 smbc_new_context(void)
6144 context = SMB_MALLOC_P(SMBCCTX);
6150 ZERO_STRUCTP(context);
6152 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6153 if (!context->internal) {
6159 ZERO_STRUCTP(context->internal);
6162 /* ADD REASONABLE DEFAULTS */
6164 context->timeout = 20000; /* 20 seconds */
6166 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6167 context->options.urlencode_readdir_entries = False;/* backward compat */
6168 context->options.one_share_per_server = False;/* backward compat */
6170 context->open = smbc_open_ctx;
6171 context->creat = smbc_creat_ctx;
6172 context->read = smbc_read_ctx;
6173 context->write = smbc_write_ctx;
6174 context->close_fn = smbc_close_ctx;
6175 context->unlink = smbc_unlink_ctx;
6176 context->rename = smbc_rename_ctx;
6177 context->lseek = smbc_lseek_ctx;
6178 context->stat = smbc_stat_ctx;
6179 context->fstat = smbc_fstat_ctx;
6180 context->opendir = smbc_opendir_ctx;
6181 context->closedir = smbc_closedir_ctx;
6182 context->readdir = smbc_readdir_ctx;
6183 context->getdents = smbc_getdents_ctx;
6184 context->mkdir = smbc_mkdir_ctx;
6185 context->rmdir = smbc_rmdir_ctx;
6186 context->telldir = smbc_telldir_ctx;
6187 context->lseekdir = smbc_lseekdir_ctx;
6188 context->fstatdir = smbc_fstatdir_ctx;
6189 context->chmod = smbc_chmod_ctx;
6190 context->utimes = smbc_utimes_ctx;
6191 context->setxattr = smbc_setxattr_ctx;
6192 context->getxattr = smbc_getxattr_ctx;
6193 context->removexattr = smbc_removexattr_ctx;
6194 context->listxattr = smbc_listxattr_ctx;
6195 context->open_print_job = smbc_open_print_job_ctx;
6196 context->print_file = smbc_print_file_ctx;
6197 context->list_print_jobs = smbc_list_print_jobs_ctx;
6198 context->unlink_print_job = smbc_unlink_print_job_ctx;
6200 context->callbacks.check_server_fn = smbc_check_server;
6201 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6203 smbc_default_cache_functions(context);
6211 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6212 * and thus you'll be leaking memory if not handled properly.
6216 smbc_free_context(SMBCCTX *context,
6226 DEBUG(1,("Performing aggressive shutdown.\n"));
6228 f = context->internal->_files;
6230 context->close_fn(context, f);
6233 context->internal->_files = NULL;
6235 /* First try to remove the servers the nice way. */
6236 if (context->callbacks.purge_cached_fn(context)) {
6239 DEBUG(1, ("Could not purge all servers, "
6240 "Nice way shutdown failed.\n"));
6241 s = context->internal->_servers;
6243 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6245 cli_shutdown(s->cli);
6246 context->callbacks.remove_cached_srv_fn(context,
6249 DLIST_REMOVE(context->internal->_servers, s);
6253 context->internal->_servers = NULL;
6257 /* This is the polite way */
6258 if (context->callbacks.purge_cached_fn(context)) {
6259 DEBUG(1, ("Could not purge all servers, "
6260 "free_context failed.\n"));
6264 if (context->internal->_servers) {
6265 DEBUG(1, ("Active servers in context, "
6266 "free_context failed.\n"));
6270 if (context->internal->_files) {
6271 DEBUG(1, ("Active files in context, "
6272 "free_context failed.\n"));
6278 /* Things we have to clean up */
6279 SAFE_FREE(context->workgroup);
6280 SAFE_FREE(context->netbios_name);
6281 SAFE_FREE(context->user);
6283 DEBUG(3, ("Context %p succesfully freed\n", context));
6284 SAFE_FREE(context->internal);
6291 * Each time the context structure is changed, we have binary backward
6292 * compatibility issues. Instead of modifying the public portions of the
6293 * context structure to add new options, instead, we put them in the internal
6294 * portion of the context structure and provide a set function for these new
6298 smbc_option_set(SMBCCTX *context,
6300 ... /* option_value */)
6305 smbc_get_auth_data_with_context_fn auth_fn;
6309 va_start(ap, option_name);
6311 if (strcmp(option_name, "debug_to_stderr") == 0) {
6313 * Log to standard error instead of standard output.
6315 option_value.b = (BOOL) va_arg(ap, int);
6316 context->internal->_debug_stderr = option_value.b;
6318 } else if (strcmp(option_name, "full_time_names") == 0) {
6320 * Use new-style time attribute names, e.g. WRITE_TIME rather
6321 * than the old-style names such as M_TIME. This allows also
6322 * setting/getting CREATE_TIME which was previously
6323 * unimplemented. (Note that the old C_TIME was supposed to
6324 * be CHANGE_TIME but was confused and sometimes referred to
6327 option_value.b = (BOOL) va_arg(ap, int);
6328 context->internal->_full_time_names = option_value.b;
6330 } else if (strcmp(option_name, "auth_function") == 0) {
6332 * Use the new-style authentication function which includes
6335 option_value.auth_fn =
6336 va_arg(ap, smbc_get_auth_data_with_context_fn);
6337 context->internal->_auth_fn_with_context =
6338 option_value.auth_fn;
6339 } else if (strcmp(option_name, "user_data") == 0) {
6341 * Save a user data handle which may be retrieved by the user
6342 * with smbc_option_get()
6344 option_value.v = va_arg(ap, void *);
6345 context->internal->_user_data = option_value.v;
6353 * Retrieve the current value of an option
6356 smbc_option_get(SMBCCTX *context,
6359 if (strcmp(option_name, "debug_stderr") == 0) {
6361 * Log to standard error instead of standard output.
6363 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6364 return (void *) (intptr_t) context->internal->_debug_stderr;
6366 return (void *) context->internal->_debug_stderr;
6368 } else if (strcmp(option_name, "full_time_names") == 0) {
6370 * Use new-style time attribute names, e.g. WRITE_TIME rather
6371 * than the old-style names such as M_TIME. This allows also
6372 * setting/getting CREATE_TIME which was previously
6373 * unimplemented. (Note that the old C_TIME was supposed to
6374 * be CHANGE_TIME but was confused and sometimes referred to
6377 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6378 return (void *) (intptr_t) context->internal->_full_time_names;
6380 return (void *) context->internal->_full_time_names;
6383 } else if (strcmp(option_name, "auth_function") == 0) {
6385 * Use the new-style authentication function which includes
6388 return (void *) context->internal->_auth_fn_with_context;
6389 } else if (strcmp(option_name, "user_data") == 0) {
6391 * Save a user data handle which may be retrieved by the user
6392 * with smbc_option_get()
6394 return context->internal->_user_data;
6402 * Initialise the library etc
6404 * We accept a struct containing handle information.
6405 * valid values for info->debug from 0 to 100,
6406 * and insist that info->fn must be non-null.
6409 smbc_init_context(SMBCCTX *context)
6416 if (!context || !context->internal) {
6421 /* Do not initialise the same client twice */
6422 if (context->internal->_initialized) {
6426 if ((!context->callbacks.auth_fn &&
6427 !context->internal->_auth_fn_with_context) ||
6428 context->debug < 0 ||
6429 context->debug > 100) {
6436 if (!smbc_initialized) {
6438 * Do some library-wide intializations the first time we get
6441 BOOL conf_loaded = False;
6443 /* Set this to what the user wants */
6444 DEBUGLEVEL = context->debug;
6448 setup_logging("libsmbclient", True);
6449 if (context->internal->_debug_stderr) {
6451 x_setbuf(x_stderr, NULL);
6454 /* Here we would open the smb.conf file if needed ... */
6456 in_client = True; /* FIXME, make a param */
6458 home = getenv("HOME");
6460 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
6461 if (lp_load(conf, True, False, False, True)) {
6464 DEBUG(5, ("Could not load config file: %s\n",
6471 * Well, if that failed, try the dyn_CONFIGFILE
6472 * Which points to the standard locn, and if that
6473 * fails, silently ignore it and use the internal
6477 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6478 DEBUG(5, ("Could not load config file: %s\n",
6482 * We loaded the global config file. Now lets
6483 * load user-specific modifications to the
6486 slprintf(conf, sizeof(conf),
6487 "%s/.smb/smb.conf.append", home);
6488 if (!lp_load(conf, True, False, False, False)) {
6490 ("Could not append config file: "
6497 load_interfaces(); /* Load the list of interfaces ... */
6499 reopen_logs(); /* Get logging working ... */
6502 * Block SIGPIPE (from lib/util_sock.c: write())
6503 * It is not needed and should not stop execution
6505 BlockSignals(True, SIGPIPE);
6507 /* Done with one-time initialisation */
6508 smbc_initialized = 1;
6512 if (!context->user) {
6514 * FIXME: Is this the best way to get the user info?
6516 user = getenv("USER");
6517 /* walk around as "guest" if no username can be found */
6518 if (!user) context->user = SMB_STRDUP("guest");
6519 else context->user = SMB_STRDUP(user);
6522 if (!context->netbios_name) {
6524 * We try to get our netbios name from the config. If that
6525 * fails we fall back on constructing our netbios name from
6528 if (global_myname()) {
6529 context->netbios_name = SMB_STRDUP(global_myname());
6533 * Hmmm, I want to get hostname as well, but I am too
6534 * lazy for the moment
6537 context->netbios_name = (char *)SMB_MALLOC(17);
6538 if (!context->netbios_name) {
6542 slprintf(context->netbios_name, 16,
6543 "smbc%s%d", context->user, pid);
6547 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6549 if (!context->workgroup) {
6550 if (lp_workgroup()) {
6551 context->workgroup = SMB_STRDUP(lp_workgroup());
6554 /* TODO: Think about a decent default workgroup */
6555 context->workgroup = SMB_STRDUP("samba");
6559 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6561 /* shortest timeout is 1 second */
6562 if (context->timeout > 0 && context->timeout < 1000)
6563 context->timeout = 1000;
6566 * FIXME: Should we check the function pointers here?
6569 context->internal->_initialized = True;
6575 /* Return the verion of samba, and thus libsmbclient */
6579 return samba_version_string();