2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "include/libsmb_internal.h"
29 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir);
30 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
31 struct smbc_dirent *dirent);
34 * DOS Attribute values (used internally)
36 typedef struct DOS_ATTR_DESC {
48 * Internal flags for extended attributes
51 /* internal mode values */
52 #define SMBC_XATTR_MODE_ADD 1
53 #define SMBC_XATTR_MODE_REMOVE 2
54 #define SMBC_XATTR_MODE_REMOVE_ALL 3
55 #define SMBC_XATTR_MODE_SET 4
56 #define SMBC_XATTR_MODE_CHOWN 5
57 #define SMBC_XATTR_MODE_CHGRP 6
59 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
61 /*We should test for this in configure ... */
63 #define ENOTSUP EOPNOTSUPP
67 * Functions exported by libsmb_cache.c that we need here
69 int smbc_default_cache_functions(SMBCCTX *context);
72 * check if an element is part of the list.
73 * FIXME: Does not belong here !
74 * Can anyone put this in a macro in dlinklist.h ?
77 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
78 if (!p || !list) return False;
80 if (p == list) return True;
87 * Find an lsa pipe handle associated with a cli struct.
89 static struct rpc_pipe_client *
90 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
92 struct rpc_pipe_client *pipe_hnd;
94 for (pipe_hnd = ipc_cli->pipe_list;
96 pipe_hnd = pipe_hnd->next) {
98 if (pipe_hnd->pipe_idx == PI_LSARPC) {
107 smbc_close_ctx(SMBCCTX *context,
110 smbc_lseek_ctx(SMBCCTX *context,
115 extern BOOL in_client;
118 * Is the logging working / configfile read ?
120 static int smbc_initialized = 0;
123 hex2int( unsigned int _char )
125 if ( _char >= 'A' && _char <='F')
126 return _char - 'A' + 10;
127 if ( _char >= 'a' && _char <='f')
128 return _char - 'a' + 10;
129 if ( _char >= '0' && _char <='9')
137 * Convert strings of %xx to their single character equivalent. Each 'x' must
138 * be a valid hexadecimal digit, or that % sequence is left undecoded.
140 * dest may, but need not be, the same pointer as src.
142 * Returns the number of % sequences which could not be converted due to lack
143 * of two following hexadecimal digits.
146 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
148 int old_length = strlen(src);
154 if ( old_length == 0 ) {
159 while ( i < old_length ) {
160 unsigned char character = src[ i++ ];
162 if (character == '%') {
163 int a = i+1 < old_length ? hex2int( src[i] ) : -1;
164 int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
166 /* Replace valid sequence */
167 if (a != -1 && b != -1) {
169 /* Replace valid %xx sequence with %dd */
170 character = (a * 16) + b;
172 if (character == '\0') {
173 break; /* Stop at %00 */
188 strncpy(dest, temp, max_dest_len - 1);
189 dest[max_dest_len - 1] = '\0';
197 * Convert any characters not specifically allowed in a URL into their %xx
200 * Returns the remaining buffer length.
203 smbc_urlencode(char * dest, char * src, int max_dest_len)
205 char hex[] = "0123456789ABCDEF";
207 for (; *src != '\0' && max_dest_len >= 3; src++) {
219 *dest++ = hex[(*src >> 4) & 0x0f];
220 *dest++ = hex[*src & 0x0f];
235 * Function to parse a path and turn it into components
237 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
238 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
239 * general format ("smb:" only; we do not look for "cifs:").
243 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
247 * smb:// Show all workgroups.
249 * The method of locating the list of workgroups varies
250 * depending upon the setting of the context variable
251 * context->options.browse_max_lmb_count. This value
252 * determine the maximum number of local master browsers to
253 * query for the list of workgroups. In order to ensure that
254 * a complete list of workgroups is obtained, all master
255 * browsers must be queried, but if there are many
256 * workgroups, the time spent querying can begin to add up.
257 * For small networks (not many workgroups), it is suggested
258 * that this variable be set to 0, indicating query all local
259 * master browsers. When the network has many workgroups, a
260 * reasonable setting for this variable might be around 3.
262 * smb://name/ if name<1D> or name<1B> exists, list servers in
263 * workgroup, else, if name<20> exists, list all shares
266 * If "options" are provided, this function returns the entire option list as a
267 * string, for later parsing by the caller. Note that currently, no options
271 static const char *smbc_prefix = "smb:";
274 smbc_parse_path(SMBCCTX *context,
276 char *workgroup, int workgroup_len,
277 char *server, int server_len,
278 char *share, int share_len,
279 char *path, int path_len,
280 char *user, int user_len,
281 char *password, int password_len,
282 char *options, int options_len)
290 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
293 * Assume we wont find an authentication domain to parse, so default
294 * to the workgroup in the provided context.
296 if (workgroup != NULL) {
297 strncpy(workgroup, context->workgroup, workgroup_len - 1);
298 workgroup[workgroup_len - 1] = '\0';
301 if (options != NULL && options_len > 0) {
302 options[0] = (char)0;
306 /* see if it has the right prefix */
307 len = strlen(smbc_prefix);
308 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
309 return -1; /* What about no smb: ? */
314 /* Watch the test below, we are testing to see if we should exit */
316 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
318 DEBUG(1, ("Invalid path (does not begin with smb://"));
323 p += 2; /* Skip the double slash */
325 /* See if any options were specified */
326 if ((q = strrchr(p, '?')) != NULL ) {
327 /* There are options. Null terminate here and point to them */
330 DEBUG(4, ("Found options '%s'", q));
332 /* Copy the options */
333 if (options != NULL && options_len > 0) {
334 safe_strcpy(options, q, options_len - 1);
343 strncpy(server, context->workgroup,
344 ((strlen(context->workgroup) < 16)
345 ? strlen(context->workgroup)
347 server[server_len - 1] = '\0';
353 * ok, its for us. Now parse out the server, share etc.
355 * However, we want to parse out [[domain;]user[:password]@] if it
359 /* check that '@' occurs before '/', if '/' exists at all */
360 q = strchr_m(p, '@');
361 r = strchr_m(p, '/');
362 if (q && (!r || q < r)) {
363 pstring username, passwd, domain;
364 const char *u = userinfo;
366 next_token_no_ltrim(&p, userinfo, "@", sizeof(fstring));
368 username[0] = passwd[0] = domain[0] = 0;
370 if (strchr_m(u, ';')) {
372 next_token_no_ltrim(&u, domain, ";", sizeof(fstring));
376 if (strchr_m(u, ':')) {
378 next_token_no_ltrim(&u, username, ":", sizeof(fstring));
385 pstrcpy(username, u);
389 if (domain[0] && workgroup) {
390 strncpy(workgroup, domain, workgroup_len - 1);
391 workgroup[workgroup_len - 1] = '\0';
395 strncpy(user, username, user_len - 1);
396 user[user_len - 1] = '\0';
400 strncpy(password, passwd, password_len - 1);
401 password[password_len - 1] = '\0';
406 if (!next_token(&p, server, "/", sizeof(fstring))) {
412 if (*p == (char)0) goto decoding; /* That's it ... */
414 if (!next_token(&p, share, "/", sizeof(fstring))) {
421 * Prepend a leading slash if there's a file path, as required by
427 safe_strcpy(path + 1, p, path_len - 2);
430 all_string_sub(path, "/", "\\", 0);
433 (void) smbc_urldecode(path, path, path_len);
434 (void) smbc_urldecode(server, server, server_len);
435 (void) smbc_urldecode(share, share, share_len);
436 (void) smbc_urldecode(user, user, user_len);
437 (void) smbc_urldecode(password, password, password_len);
443 * Verify that the options specified in a URL are valid
446 smbc_check_options(char *server,
451 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
452 "path='%s' options='%s'\n",
453 server, share, path, options));
455 /* No options at all is always ok */
456 if (! *options) return 0;
458 /* Currently, we don't support any options. */
463 * Convert an SMB error into a UNIX error ...
466 smbc_errno(SMBCCTX *context,
469 int ret = cli_errno(c);
471 if (cli_is_dos_error(c)) {
475 cli_dos_error(c, &eclass, &ecode);
477 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
478 (int)eclass, (int)ecode, (int)ecode, ret));
482 status = cli_nt_error(c);
484 DEBUG(3,("smbc errno %s -> %d\n",
485 nt_errstr(status), ret));
492 * Check a server for being alive and well.
493 * returns 0 if the server is in shape. Returns 1 on error
495 * Also useable outside libsmbclient to enable external cache
496 * to do some checks too.
499 smbc_check_server(SMBCCTX * context,
502 if ( send_keepalive(server->cli->fd) == False )
505 /* connection is ok */
510 * Remove a server from the cached server list it's unused.
511 * On success, 0 is returned. 1 is returned if the server could not be removed.
513 * Also useable outside libsmbclient
516 smbc_remove_unused_server(SMBCCTX * context,
521 /* are we being fooled ? */
522 if (!context || !context->internal ||
523 !context->internal->_initialized || !srv) return 1;
526 /* Check all open files/directories for a relation with this server */
527 for (file = context->internal->_files; file; file=file->next) {
528 if (file->srv == srv) {
530 DEBUG(3, ("smbc_remove_usused_server: "
531 "%p still used by %p.\n",
537 DLIST_REMOVE(context->internal->_servers, srv);
539 cli_shutdown(srv->cli);
542 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
544 context->callbacks.remove_cached_srv_fn(context, srv);
552 find_server(SMBCCTX *context,
564 srv = context->callbacks.get_cached_srv_fn(context, server, share,
565 workgroup, username);
567 if (!auth_called && !srv && (!username[0] || !password[0])) {
568 if (context->internal->_auth_fn_with_context != NULL) {
569 context->internal->_auth_fn_with_context(
572 workgroup, sizeof(fstring),
573 username, sizeof(fstring),
574 password, sizeof(fstring));
576 context->callbacks.auth_fn(
578 workgroup, sizeof(fstring),
579 username, sizeof(fstring),
580 password, sizeof(fstring));
584 * However, smbc_auth_fn may have picked up info relating to
585 * an existing connection, so try for an existing connection
589 goto check_server_cache;
594 if (context->callbacks.check_server_fn(context, srv)) {
596 * This server is no good anymore
597 * Try to remove it and check for more possible
598 * servers in the cache
600 if (context->callbacks.remove_unused_server_fn(context,
603 * We could not remove the server completely,
604 * remove it from the cache so we will not get
605 * it again. It will be removed when the last
606 * file/dir is closed.
608 context->callbacks.remove_cached_srv_fn(context,
613 * Maybe there are more cached connections to this
616 goto check_server_cache;
626 * Connect to a server, possibly on an existing connection
628 * Here, what we want to do is: If the server and username
629 * match an existing connection, reuse that, otherwise, establish a
632 * If we have to create a new connection, call the auth_fn to get the
633 * info we need, unless the username and password were passed in.
637 smbc_server(SMBCCTX *context,
638 BOOL connect_if_not_found,
647 struct nmb_name called, calling;
648 const char *server_n = server;
651 int tried_reverse = 0;
654 const char *username_used;
659 if (server[0] == 0) {
664 /* Look for a cached connection */
665 srv = find_server(context, server, share,
666 workgroup, username, password);
669 * If we found a connection and we're only allowed one share per
672 if (srv && *share != '\0' && context->options.one_share_per_server) {
675 * ... then if there's no current connection to the share,
676 * connect to it. find_server(), or rather the function
677 * pointed to by context->callbacks.get_cached_srv_fn which
678 * was called by find_server(), will have issued a tree
679 * disconnect if the requested share is not the same as the
680 * one that was already connected.
682 if (srv->cli->cnum == (uint16) -1) {
683 /* Ensure we have accurate auth info */
684 if (context->internal->_auth_fn_with_context != NULL) {
685 context->internal->_auth_fn_with_context(
688 workgroup, sizeof(fstring),
689 username, sizeof(fstring),
690 password, sizeof(fstring));
692 context->callbacks.auth_fn(
694 workgroup, sizeof(fstring),
695 username, sizeof(fstring),
696 password, sizeof(fstring));
699 if (! cli_send_tconX(srv->cli, share, "?????",
700 password, strlen(password)+1)) {
702 errno = smbc_errno(context, srv->cli);
703 cli_shutdown(srv->cli);
705 context->callbacks.remove_cached_srv_fn(context,
711 * Regenerate the dev value since it's based on both
715 srv->dev = (dev_t)(str_checksum(server) ^
716 str_checksum(share));
721 /* If we have a connection... */
724 /* ... then we're done here. Give 'em what they came for. */
728 /* If we're not asked to connect when a connection doesn't exist... */
729 if (! connect_if_not_found) {
730 /* ... then we're done here. */
734 make_nmb_name(&calling, context->netbios_name, 0x0);
735 make_nmb_name(&called , server, 0x20);
737 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
739 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
742 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
746 /* have to open a new connection */
747 if ((c = cli_initialise()) == NULL) {
752 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
753 c->use_kerberos = True;
755 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
756 c->fallback_after_kerberos = True;
759 c->timeout = context->timeout;
762 * Force use of port 139 for first try if share is $IPC, empty, or
763 * null, so browse lists can work
765 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
766 port_try_first = 139;
769 port_try_first = 445;
773 c->port = port_try_first;
775 if (!cli_connect(c, server_n, &ip)) {
777 /* First connection attempt failed. Try alternate port. */
778 c->port = port_try_next;
780 if (!cli_connect(c, server_n, &ip)) {
787 if (!cli_session_request(c, &calling, &called)) {
789 if (strcmp(called.name, "*SMBSERVER")) {
790 make_nmb_name(&called , "*SMBSERVER", 0x20);
792 } else { /* Try one more time, but ensure we don't loop */
794 /* Only try this if server is an IP address ... */
796 if (is_ipaddress(server) && !tried_reverse) {
798 struct in_addr rem_ip;
800 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
801 DEBUG(4, ("Could not convert IP address "
802 "%s to struct in_addr\n", server));
807 tried_reverse++; /* Yuck */
809 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
810 make_nmb_name(&called, remote_name, 0x20);
819 DEBUG(4,(" session request ok\n"));
821 if (!cli_negprot(c)) {
827 username_used = username;
829 if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used,
830 password, strlen(password),
831 password, strlen(password),
834 /* Failed. Try an anonymous login, if allowed by flags. */
837 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
838 !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
849 DEBUG(4,(" session setup ok\n"));
851 if (!cli_send_tconX(c, share, "?????",
852 password, strlen(password)+1)) {
853 errno = smbc_errno(context, c);
858 DEBUG(4,(" tconx ok\n"));
861 * Ok, we have got a nice connection
862 * Let's allocate a server structure.
865 srv = SMB_MALLOC_P(SMBCSRV);
873 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
874 srv->no_pathinfo = False;
875 srv->no_pathinfo2 = False;
876 srv->no_nt_session = False;
878 /* now add it to the cache (internal or external) */
879 /* Let the cache function set errno if it wants to */
881 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
882 int saved_errno = errno;
883 DEBUG(3, (" Failed to add server to cache\n"));
891 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
892 server, share, srv));
894 DLIST_ADD(context->internal->_servers, srv);
908 * Connect to a server for getting/setting attributes, possibly on an existing
909 * connection. This works similarly to smbc_server().
912 smbc_attr_server(SMBCCTX *context,
921 struct cli_state *ipc_cli;
922 struct rpc_pipe_client *pipe_hnd;
924 SMBCSRV *ipc_srv=NULL;
927 * See if we've already created this special connection. Reference
928 * our "special" share name '*IPC$', which is an impossible real share
929 * name due to the leading asterisk.
931 ipc_srv = find_server(context, server, "*IPC$",
932 workgroup, username, password);
935 /* We didn't find a cached connection. Get the password */
936 if (*password == '\0') {
937 /* ... then retrieve it now. */
938 if (context->internal->_auth_fn_with_context != NULL) {
939 context->internal->_auth_fn_with_context(
942 workgroup, sizeof(fstring),
943 username, sizeof(fstring),
944 password, sizeof(fstring));
946 context->callbacks.auth_fn(
948 workgroup, sizeof(fstring),
949 username, sizeof(fstring),
950 password, sizeof(fstring));
955 nt_status = cli_full_connection(&ipc_cli,
956 global_myname(), server,
957 &ip, 0, "IPC$", "?????",
961 if (! NT_STATUS_IS_OK(nt_status)) {
962 DEBUG(1,("cli_full_connection failed! (%s)\n",
963 nt_errstr(nt_status)));
968 ipc_srv = SMB_MALLOC_P(SMBCSRV);
971 cli_shutdown(ipc_cli);
975 ZERO_STRUCTP(ipc_srv);
976 ipc_srv->cli = ipc_cli;
979 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
983 DEBUG(1, ("cli_nt_session_open fail!\n"));
985 cli_shutdown(ipc_srv->cli);
991 * Some systems don't support
992 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
993 * so we might as well do it too.
996 nt_status = rpccli_lsa_open_policy(
998 ipc_srv->cli->mem_ctx,
1000 GENERIC_EXECUTE_ACCESS,
1003 if (!NT_STATUS_IS_OK(nt_status)) {
1004 errno = smbc_errno(context, ipc_srv->cli);
1005 cli_shutdown(ipc_srv->cli);
1010 /* now add it to the cache (internal or external) */
1012 errno = 0; /* let cache function set errno if it likes */
1013 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
1018 DEBUG(3, (" Failed to add server to cache\n"));
1022 cli_shutdown(ipc_srv->cli);
1027 DLIST_ADD(context->internal->_servers, ipc_srv);
1034 * Routine to open() a file ...
1038 smbc_open_ctx(SMBCCTX *context,
1043 fstring server, share, user, password, workgroup;
1046 struct cli_state *targetcli;
1047 SMBCSRV *srv = NULL;
1048 SMBCFILE *file = NULL;
1051 if (!context || !context->internal ||
1052 !context->internal->_initialized) {
1054 errno = EINVAL; /* Best I can think of ... */
1066 if (smbc_parse_path(context, fname,
1067 workgroup, sizeof(workgroup),
1068 server, sizeof(server),
1069 share, sizeof(share),
1072 password, sizeof(password),
1078 if (user[0] == (char)0) fstrcpy(user, context->user);
1080 srv = smbc_server(context, True,
1081 server, share, workgroup, user, password);
1085 if (errno == EPERM) errno = EACCES;
1086 return NULL; /* smbc_server sets errno */
1090 /* Hmmm, the test for a directory is suspect here ... FIXME */
1092 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1099 file = SMB_MALLOC_P(SMBCFILE);
1110 /*d_printf(">>>open: resolving %s\n", path);*/
1111 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1113 d_printf("Could not resolve %s\n", path);
1117 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1119 if ( targetcli->dfsroot )
1122 pstrcpy(temppath, targetpath);
1123 cli_dfs_make_full_path( targetpath, targetcli->desthost, targetcli->share, temppath);
1126 if ((fd = cli_open(targetcli, targetpath, flags,
1127 context->internal->_share_mode)) < 0) {
1129 /* Handle the error ... */
1132 errno = smbc_errno(context, targetcli);
1137 /* Fill in file struct */
1140 file->fname = SMB_STRDUP(fname);
1145 DLIST_ADD(context->internal->_files, file);
1148 * If the file was opened in O_APPEND mode, all write
1149 * operations should be appended to the file. To do that,
1150 * though, using this protocol, would require a getattrE()
1151 * call for each and every write, to determine where the end
1152 * of the file is. (There does not appear to be an append flag
1153 * in the protocol.) Rather than add all of that overhead of
1154 * retrieving the current end-of-file offset prior to each
1155 * write operation, we'll assume that most append operations
1156 * will continuously write, so we'll just set the offset to
1157 * the end of the file now and hope that's adequate.
1159 * Note to self: If this proves inadequate, and O_APPEND
1160 * should, in some cases, be forced for each write, add a
1161 * field in the context options structure, for
1162 * "strict_append_mode" which would select between the current
1163 * behavior (if FALSE) or issuing a getattrE() prior to each
1164 * write and forcing the write to the end of the file (if
1165 * TRUE). Adding that capability will likely require adding
1166 * an "append" flag into the _SMBCFILE structure to track
1167 * whether a file was opened in O_APPEND mode. -- djl
1169 if (flags & O_APPEND) {
1170 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1171 (void) smbc_close_ctx(context, file);
1181 /* Check if opendir needed ... */
1186 eno = smbc_errno(context, srv->cli);
1187 file = context->opendir(context, fname);
1188 if (!file) errno = eno;
1193 errno = EINVAL; /* FIXME, correct errno ? */
1199 * Routine to create a file
1202 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1205 smbc_creat_ctx(SMBCCTX *context,
1210 if (!context || !context->internal ||
1211 !context->internal->_initialized) {
1218 return smbc_open_ctx(context, path, creat_bits, mode);
1222 * Routine to read() a file ...
1226 smbc_read_ctx(SMBCCTX *context,
1232 fstring server, share, user, password;
1233 pstring path, targetpath;
1234 struct cli_state *targetcli;
1239 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1240 * appears to pass file->offset (which is type off_t) differently than
1241 * a local variable of type off_t. Using local variable "offset" in
1242 * the call to cli_read() instead of file->offset fixes a problem
1243 * retrieving data at an offset greater than 4GB.
1247 if (!context || !context->internal ||
1248 !context->internal->_initialized) {
1255 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1257 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1264 offset = file->offset;
1266 /* Check that the buffer exists ... */
1275 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1276 if (smbc_parse_path(context, file->fname,
1278 server, sizeof(server),
1279 share, sizeof(share),
1282 password, sizeof(password),
1288 /*d_printf(">>>read: resolving %s\n", path);*/
1289 if (!cli_resolve_path("", file->srv->cli, path,
1290 &targetcli, targetpath))
1292 d_printf("Could not resolve %s\n", path);
1295 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1297 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1301 errno = smbc_errno(context, targetcli);
1306 file->offset += ret;
1308 DEBUG(4, (" --> %d\n", ret));
1310 return ret; /* Success, ret bytes of data ... */
1315 * Routine to write() a file ...
1319 smbc_write_ctx(SMBCCTX *context,
1326 fstring server, share, user, password;
1327 pstring path, targetpath;
1328 struct cli_state *targetcli;
1330 /* First check all pointers before dereferencing them */
1332 if (!context || !context->internal ||
1333 !context->internal->_initialized) {
1340 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1347 /* Check that the buffer exists ... */
1356 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1358 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1359 if (smbc_parse_path(context, file->fname,
1361 server, sizeof(server),
1362 share, sizeof(share),
1365 password, sizeof(password),
1371 /*d_printf(">>>write: resolving %s\n", path);*/
1372 if (!cli_resolve_path("", file->srv->cli, path,
1373 &targetcli, targetpath))
1375 d_printf("Could not resolve %s\n", path);
1378 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1381 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1385 errno = smbc_errno(context, targetcli);
1390 file->offset += ret;
1392 return ret; /* Success, 0 bytes of data ... */
1396 * Routine to close() a file ...
1400 smbc_close_ctx(SMBCCTX *context,
1404 fstring server, share, user, password;
1405 pstring path, targetpath;
1406 struct cli_state *targetcli;
1408 if (!context || !context->internal ||
1409 !context->internal->_initialized) {
1416 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1426 return context->closedir(context, file);
1430 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1431 if (smbc_parse_path(context, file->fname,
1433 server, sizeof(server),
1434 share, sizeof(share),
1437 password, sizeof(password),
1443 /*d_printf(">>>close: resolving %s\n", path);*/
1444 if (!cli_resolve_path("", file->srv->cli, path,
1445 &targetcli, targetpath))
1447 d_printf("Could not resolve %s\n", path);
1450 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1452 if (!cli_close(targetcli, file->cli_fd)) {
1454 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1456 /* Deallocate slot and remove the server
1457 * from the server cache if unused */
1458 errno = smbc_errno(context, targetcli);
1460 DLIST_REMOVE(context->internal->_files, file);
1461 SAFE_FREE(file->fname);
1463 context->callbacks.remove_unused_server_fn(context, srv);
1469 DLIST_REMOVE(context->internal->_files, file);
1470 SAFE_FREE(file->fname);
1477 * Get info from an SMB server on a file. Use a qpathinfo call first
1478 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1481 smbc_getatr(SMBCCTX * context,
1486 struct timespec *create_time_ts,
1487 struct timespec *access_time_ts,
1488 struct timespec *write_time_ts,
1489 struct timespec *change_time_ts,
1494 struct cli_state *targetcli;
1497 if (!context || !context->internal ||
1498 !context->internal->_initialized) {
1505 /* path fixup for . and .. */
1506 if (strequal(path, ".") || strequal(path, ".."))
1507 pstrcpy(fixedpath, "\\");
1510 pstrcpy(fixedpath, path);
1511 trim_string(fixedpath, NULL, "\\..");
1512 trim_string(fixedpath, NULL, "\\.");
1514 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1516 if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
1518 d_printf("Couldn't resolve %s\n", path);
1522 if ( targetcli->dfsroot )
1525 pstrcpy(temppath, targetpath);
1526 cli_dfs_make_full_path(targetpath, targetcli->desthost,
1527 targetcli->share, temppath);
1530 if (!srv->no_pathinfo2 &&
1531 cli_qpathinfo2(targetcli, targetpath,
1540 /* if this is NT then don't bother with the getatr */
1541 if (targetcli->capabilities & CAP_NT_SMBS) {
1546 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1548 struct timespec w_time_ts;
1550 w_time_ts = convert_time_t_to_timespec(write_time);
1552 if (write_time_ts != NULL) {
1553 *write_time_ts = w_time_ts;
1556 if (create_time_ts != NULL) {
1557 *create_time_ts = w_time_ts;
1560 if (access_time_ts != NULL) {
1561 *access_time_ts = w_time_ts;
1564 if (change_time_ts != NULL) {
1565 *change_time_ts = w_time_ts;
1568 srv->no_pathinfo2 = True;
1578 * Set file info on an SMB server. Use setpathinfo call first. If that
1579 * fails, use setattrE..
1581 * Access and modification time parameters are always used and must be
1582 * provided. Create time, if zero, will be determined from the actual create
1583 * time of the file. If non-zero, the create time will be set as well.
1585 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1588 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1599 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1600 * modern function for "new code" to be using, and it works given a
1601 * filename rather than requiring that the file be opened to have its
1602 * attributes manipulated.
1604 if (srv->no_pathinfo ||
1605 ! cli_setpathinfo(srv->cli, path,
1613 * setpathinfo is not supported; go to plan B.
1615 * cli_setatr() does not work on win98, and it also doesn't
1616 * support setting the access time (only the modification
1617 * time), so in all cases, we open the specified file and use
1618 * cli_setattrE() which should work on all OS versions, and
1619 * supports both times.
1622 /* Don't try {q,set}pathinfo() again, with this server */
1623 srv->no_pathinfo = True;
1626 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1628 errno = smbc_errno(context, srv->cli);
1632 /* Set the new attributes */
1633 ret = cli_setattrE(srv->cli, fd,
1638 /* Close the file */
1639 cli_close(srv->cli, fd);
1642 * Unfortunately, setattrE() doesn't have a provision for
1643 * setting the access mode (attributes). We'll have to try
1644 * cli_setatr() for that, and with only this parameter, it
1645 * seems to work on win98.
1647 if (ret && mode != (uint16) -1) {
1648 ret = cli_setatr(srv->cli, path, mode, 0);
1652 errno = smbc_errno(context, srv->cli);
1661 * Routine to unlink() a file
1665 smbc_unlink_ctx(SMBCCTX *context,
1668 fstring server, share, user, password, workgroup;
1669 pstring path, targetpath;
1670 struct cli_state *targetcli;
1671 SMBCSRV *srv = NULL;
1673 if (!context || !context->internal ||
1674 !context->internal->_initialized) {
1676 errno = EINVAL; /* Best I can think of ... */
1688 if (smbc_parse_path(context, fname,
1689 workgroup, sizeof(workgroup),
1690 server, sizeof(server),
1691 share, sizeof(share),
1694 password, sizeof(password),
1700 if (user[0] == (char)0) fstrcpy(user, context->user);
1702 srv = smbc_server(context, True,
1703 server, share, workgroup, user, password);
1707 return -1; /* smbc_server sets errno */
1711 /*d_printf(">>>unlink: resolving %s\n", path);*/
1712 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
1714 d_printf("Could not resolve %s\n", path);
1717 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1719 if (!cli_unlink(targetcli, targetpath)) {
1721 errno = smbc_errno(context, targetcli);
1723 if (errno == EACCES) { /* Check if the file is a directory */
1728 struct timespec write_time_ts;
1729 struct timespec access_time_ts;
1730 struct timespec change_time_ts;
1733 if (!smbc_getatr(context, srv, path, &mode, &size,
1740 /* Hmmm, bad error ... What? */
1742 errno = smbc_errno(context, targetcli);
1748 if (IS_DOS_DIR(mode))
1751 errno = saverr; /* Restore this */
1760 return 0; /* Success ... */
1765 * Routine to rename() a file
1769 smbc_rename_ctx(SMBCCTX *ocontext,
1785 pstring targetpath1;
1786 pstring targetpath2;
1787 struct cli_state *targetcli1;
1788 struct cli_state *targetcli2;
1789 SMBCSRV *srv = NULL;
1791 if (!ocontext || !ncontext ||
1792 !ocontext->internal || !ncontext->internal ||
1793 !ocontext->internal->_initialized ||
1794 !ncontext->internal->_initialized) {
1796 errno = EINVAL; /* Best I can think of ... */
1801 if (!oname || !nname) {
1808 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1810 smbc_parse_path(ocontext, oname,
1811 workgroup, sizeof(workgroup),
1812 server1, sizeof(server1),
1813 share1, sizeof(share1),
1814 path1, sizeof(path1),
1815 user1, sizeof(user1),
1816 password1, sizeof(password1),
1819 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1821 smbc_parse_path(ncontext, nname,
1823 server2, sizeof(server2),
1824 share2, sizeof(share2),
1825 path2, sizeof(path2),
1826 user2, sizeof(user2),
1827 password2, sizeof(password2),
1830 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1832 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1833 strcmp(user1, user2)) {
1835 /* Can't rename across file systems, or users?? */
1842 srv = smbc_server(ocontext, True,
1843 server1, share1, workgroup, user1, password1);
1850 /*d_printf(">>>rename: resolving %s\n", path1);*/
1851 if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
1853 d_printf("Could not resolve %s\n", path1);
1856 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1857 /*d_printf(">>>rename: resolving %s\n", path2);*/
1858 if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
1860 d_printf("Could not resolve %s\n", path2);
1863 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1865 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
1866 strcmp(targetcli1->share, targetcli2->share))
1868 /* can't rename across file systems */
1874 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
1875 int eno = smbc_errno(ocontext, targetcli1);
1877 if (eno != EEXIST ||
1878 !cli_unlink(targetcli1, targetpath2) ||
1879 !cli_rename(targetcli1, targetpath1, targetpath2)) {
1887 return 0; /* Success */
1892 * A routine to lseek() a file
1896 smbc_lseek_ctx(SMBCCTX *context,
1902 fstring server, share, user, password;
1903 pstring path, targetpath;
1904 struct cli_state *targetcli;
1906 if (!context || !context->internal ||
1907 !context->internal->_initialized) {
1914 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1924 return -1; /* Can't lseek a dir ... */
1930 file->offset = offset;
1934 file->offset += offset;
1938 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1939 if (smbc_parse_path(context, file->fname,
1941 server, sizeof(server),
1942 share, sizeof(share),
1945 password, sizeof(password),
1952 /*d_printf(">>>lseek: resolving %s\n", path);*/
1953 if (!cli_resolve_path("", file->srv->cli, path,
1954 &targetcli, targetpath))
1956 d_printf("Could not resolve %s\n", path);
1959 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1961 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
1962 &size, NULL, NULL, NULL, NULL, NULL))
1964 SMB_OFF_T b_size = size;
1965 if (!cli_getattrE(targetcli, file->cli_fd,
1966 NULL, &b_size, NULL, NULL, NULL))
1973 file->offset = size + offset;
1982 return file->offset;
1987 * Generate an inode number from file name for those things that need it
1991 smbc_inode(SMBCCTX *context,
1995 if (!context || !context->internal ||
1996 !context->internal->_initialized) {
2003 if (!*name) return 2; /* FIXME, why 2 ??? */
2004 return (ino_t)str_checksum(name);
2009 * Routine to put basic stat info into a stat structure ... Used by stat and
2014 smbc_setup_stat(SMBCCTX *context,
2023 if (IS_DOS_DIR(mode)) {
2024 st->st_mode = SMBC_DIR_MODE;
2026 st->st_mode = SMBC_FILE_MODE;
2029 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2030 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2031 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2032 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2035 #ifdef HAVE_STAT_ST_BLKSIZE
2036 st->st_blksize = 512;
2038 #ifdef HAVE_STAT_ST_BLOCKS
2039 st->st_blocks = (size+511)/512;
2041 st->st_uid = getuid();
2042 st->st_gid = getgid();
2044 if (IS_DOS_DIR(mode)) {
2050 if (st->st_ino == 0) {
2051 st->st_ino = smbc_inode(context, fname);
2054 return True; /* FIXME: Is this needed ? */
2059 * Routine to stat a file given a name
2063 smbc_stat_ctx(SMBCCTX *context,
2074 struct timespec write_time_ts;
2075 struct timespec access_time_ts;
2076 struct timespec change_time_ts;
2081 if (!context || !context->internal ||
2082 !context->internal->_initialized) {
2084 errno = EINVAL; /* Best I can think of ... */
2096 DEBUG(4, ("smbc_stat(%s)\n", fname));
2098 if (smbc_parse_path(context, fname,
2099 workgroup, sizeof(workgroup),
2100 server, sizeof(server),
2101 share, sizeof(share),
2104 password, sizeof(password),
2110 if (user[0] == (char)0) fstrcpy(user, context->user);
2112 srv = smbc_server(context, True,
2113 server, share, workgroup, user, password);
2116 return -1; /* errno set by smbc_server */
2119 if (!smbc_getatr(context, srv, path, &mode, &size,
2126 errno = smbc_errno(context, srv->cli);
2133 smbc_setup_stat(context, st, path, size, mode);
2135 set_atimespec(st, access_time_ts);
2136 set_ctimespec(st, change_time_ts);
2137 set_mtimespec(st, write_time_ts);
2138 st->st_dev = srv->dev;
2145 * Routine to stat a file given an fd
2149 smbc_fstat_ctx(SMBCCTX *context,
2153 struct timespec change_time_ts;
2154 struct timespec access_time_ts;
2155 struct timespec write_time_ts;
2164 struct cli_state *targetcli;
2167 if (!context || !context->internal ||
2168 !context->internal->_initialized) {
2175 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2184 return context->fstatdir(context, file, st);
2188 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2189 if (smbc_parse_path(context, file->fname,
2191 server, sizeof(server),
2192 share, sizeof(share),
2195 password, sizeof(password),
2201 /*d_printf(">>>fstat: resolving %s\n", path);*/
2202 if (!cli_resolve_path("", file->srv->cli, path,
2203 &targetcli, targetpath))
2205 d_printf("Could not resolve %s\n", path);
2208 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2210 if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
2217 time_t change_time, access_time, write_time;
2219 if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
2220 &change_time, &access_time, &write_time)) {
2226 change_time_ts = convert_time_t_to_timespec(change_time);
2227 access_time_ts = convert_time_t_to_timespec(access_time);
2228 write_time_ts = convert_time_t_to_timespec(write_time);
2233 smbc_setup_stat(context, st, file->fname, size, mode);
2235 set_atimespec(st, access_time_ts);
2236 set_ctimespec(st, change_time_ts);
2237 set_mtimespec(st, write_time_ts);
2238 st->st_dev = file->srv->dev;
2245 * Routine to open a directory
2246 * We accept the URL syntax explained in smbc_parse_path(), above.
2250 smbc_remove_dir(SMBCFILE *dir)
2252 struct smbc_dir_list *d,*f;
2259 SAFE_FREE(f->dirent);
2264 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2269 add_dirent(SMBCFILE *dir,
2271 const char *comment,
2274 struct smbc_dirent *dirent;
2276 int name_length = (name == NULL ? 0 : strlen(name));
2277 int comment_len = (comment == NULL ? 0 : strlen(comment));
2280 * Allocate space for the dirent, which must be increased by the
2281 * size of the name and the comment and 1 each for the null terminator.
2284 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2286 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2290 dir->dir_error = ENOMEM;
2295 ZERO_STRUCTP(dirent);
2297 if (dir->dir_list == NULL) {
2299 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2300 if (!dir->dir_list) {
2303 dir->dir_error = ENOMEM;
2307 ZERO_STRUCTP(dir->dir_list);
2309 dir->dir_end = dir->dir_next = dir->dir_list;
2313 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2315 if (!dir->dir_end->next) {
2318 dir->dir_error = ENOMEM;
2322 ZERO_STRUCTP(dir->dir_end->next);
2324 dir->dir_end = dir->dir_end->next;
2327 dir->dir_end->next = NULL;
2328 dir->dir_end->dirent = dirent;
2330 dirent->smbc_type = type;
2331 dirent->namelen = name_length;
2332 dirent->commentlen = comment_len;
2333 dirent->dirlen = size;
2336 * dirent->namelen + 1 includes the null (no null termination needed)
2337 * Ditto for dirent->commentlen.
2338 * The space for the two null bytes was allocated.
2340 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2341 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2342 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2349 list_unique_wg_fn(const char *name,
2351 const char *comment,
2354 SMBCFILE *dir = (SMBCFILE *)state;
2355 struct smbc_dir_list *dir_list;
2356 struct smbc_dirent *dirent;
2360 dirent_type = dir->dir_type;
2362 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2364 /* An error occurred, what do we do? */
2365 /* FIXME: Add some code here */
2368 /* Point to the one just added */
2369 dirent = dir->dir_end->dirent;
2371 /* See if this was a duplicate */
2372 for (dir_list = dir->dir_list;
2373 dir_list != dir->dir_end;
2374 dir_list = dir_list->next) {
2376 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2377 /* Duplicate. End end of list need to be removed. */
2381 if (do_remove && dir_list->next == dir->dir_end) {
2382 /* Found the end of the list. Remove it. */
2383 dir->dir_end = dir_list;
2384 free(dir_list->next);
2386 dir_list->next = NULL;
2393 list_fn(const char *name,
2395 const char *comment,
2398 SMBCFILE *dir = (SMBCFILE *)state;
2402 * We need to process the type a little ...
2404 * Disk share = 0x00000000
2405 * Print share = 0x00000001
2406 * Comms share = 0x00000002 (obsolete?)
2407 * IPC$ share = 0x00000003
2409 * administrative shares:
2410 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2413 if (dir->dir_type == SMBC_FILE_SHARE) {
2416 case 0 | 0x80000000:
2418 dirent_type = SMBC_FILE_SHARE;
2422 dirent_type = SMBC_PRINTER_SHARE;
2426 dirent_type = SMBC_COMMS_SHARE;
2429 case 3 | 0x80000000:
2431 dirent_type = SMBC_IPC_SHARE;
2435 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2440 dirent_type = dir->dir_type;
2443 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2445 /* An error occurred, what do we do? */
2446 /* FIXME: Add some code here */
2452 dir_list_fn(const char *mnt,
2458 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2459 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2461 /* Handle an error ... */
2463 /* FIXME: Add some code ... */
2470 net_share_enum_rpc(struct cli_state *cli,
2471 void (*fn)(const char *name,
2473 const char *comment,
2480 uint32 *penum_hnd = &enum_hnd;
2481 uint32 info_level = 1;
2482 uint32 preferred_len = 0xffffffff;
2483 struct srvsvc_NetShareCtr1 ctr1;
2484 union srvsvc_NetShareCtr ctr;
2486 struct rpc_pipe_client *pipe_hnd;
2490 /* Open the server service pipe */
2491 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2493 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2497 /* Allocate a context for parsing and for the entries in "ctr" */
2498 mem_ctx = talloc_init("libsmbclient: net_share_enum_rpc");
2499 if (mem_ctx == NULL) {
2500 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2501 cli_rpc_pipe_close(pipe_hnd);
2508 /* Issue the NetShareEnum RPC call and retrieve the response */
2510 result = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
2511 &info_level, &ctr, preferred_len,
2512 &numentries, &penum_hnd);
2514 /* Was it successful? */
2515 if (!NT_STATUS_IS_OK(result) || numentries == 0) {
2516 /* Nope. Go clean up. */
2520 /* For each returned entry... */
2521 for (i = 0; i < numentries; i++) {
2523 /* Add this share to the list */
2524 (*fn)(ctr.ctr1->array[i].name,
2525 ctr.ctr1->array[i].type,
2526 ctr.ctr1->array[i].comment, state);
2530 /* Close the server service pipe */
2531 cli_rpc_pipe_close(pipe_hnd);
2533 /* Free all memory which was allocated for this request */
2534 TALLOC_FREE(mem_ctx);
2536 /* Tell 'em if it worked */
2537 return NT_STATUS_IS_OK(result) ? 0 : -1;
2543 smbc_opendir_ctx(SMBCCTX *context,
2547 fstring server, share, user, password, options;
2552 SMBCSRV *srv = NULL;
2553 SMBCFILE *dir = NULL;
2554 struct _smbc_callbacks *cb;
2555 struct in_addr rem_ip;
2557 if (!context || !context->internal ||
2558 !context->internal->_initialized) {
2559 DEBUG(4, ("no valid context\n"));
2560 errno = EINVAL + 8192;
2566 DEBUG(4, ("no valid fname\n"));
2567 errno = EINVAL + 8193;
2571 if (smbc_parse_path(context, fname,
2572 workgroup, sizeof(workgroup),
2573 server, sizeof(server),
2574 share, sizeof(share),
2577 password, sizeof(password),
2578 options, sizeof(options))) {
2579 DEBUG(4, ("no valid path\n"));
2580 errno = EINVAL + 8194;
2584 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2585 "path='%s' options='%s'\n",
2586 fname, server, share, path, options));
2588 /* Ensure the options are valid */
2589 if (smbc_check_options(server, share, path, options)) {
2590 DEBUG(4, ("unacceptable options (%s)\n", options));
2591 errno = EINVAL + 8195;
2595 if (user[0] == (char)0) fstrcpy(user, context->user);
2597 dir = SMB_MALLOC_P(SMBCFILE);
2609 dir->fname = SMB_STRDUP(fname);
2613 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2615 if (server[0] == (char)0) {
2620 struct ip_service *ip_list;
2621 struct ip_service server_addr;
2622 struct user_auth_info u_info;
2623 struct cli_state *cli;
2625 if (share[0] != (char)0 || path[0] != (char)0) {
2627 errno = EINVAL + 8196;
2629 SAFE_FREE(dir->fname);
2635 /* Determine how many local master browsers to query */
2636 max_lmb_count = (context->options.browse_max_lmb_count == 0
2638 : context->options.browse_max_lmb_count);
2640 pstrcpy(u_info.username, user);
2641 pstrcpy(u_info.password, password);
2644 * We have server and share and path empty but options
2645 * requesting that we scan all master browsers for their list
2646 * of workgroups/domains. This implies that we must first try
2647 * broadcast queries to find all master browsers, and if that
2648 * doesn't work, then try our other methods which return only
2649 * a single master browser.
2653 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
2657 if (!find_master_ip(workgroup, &server_addr.ip)) {
2660 SAFE_FREE(dir->fname);
2667 ip_list = &server_addr;
2671 for (i = 0; i < count && i < max_lmb_count; i++) {
2672 DEBUG(99, ("Found master browser %d of %d: %s\n",
2673 i+1, MAX(count, max_lmb_count),
2674 inet_ntoa(ip_list[i].ip)));
2676 cli = get_ipc_connect_master_ip(&ip_list[i],
2677 workgroup, &u_info);
2678 /* cli == NULL is the master browser refused to talk or
2679 could not be found */
2683 fstrcpy(server, cli->desthost);
2686 DEBUG(4, ("using workgroup %s %s\n",
2687 workgroup, server));
2690 * For each returned master browser IP address, get a
2691 * connection to IPC$ on the server if we do not
2692 * already have one, and determine the
2693 * workgroups/domains that it knows about.
2696 srv = smbc_server(context, True, server, "IPC$",
2697 workgroup, user, password);
2703 dir->dir_type = SMBC_WORKGROUP;
2705 /* Now, list the stuff ... */
2707 if (!cli_NetServerEnum(srv->cli,
2709 SV_TYPE_DOMAIN_ENUM,
2719 * Server not an empty string ... Check the rest and see what
2722 if (*share == '\0') {
2723 if (*path != '\0') {
2725 /* Should not have empty share with path */
2726 errno = EINVAL + 8197;
2728 SAFE_FREE(dir->fname);
2736 * We don't know if <server> is really a server name
2737 * or is a workgroup/domain name. If we already have
2738 * a server structure for it, we'll use it.
2739 * Otherwise, check to see if <server><1D>,
2740 * <server><1B>, or <server><20> translates. We check
2741 * to see if <server> is an IP address first.
2745 * See if we have an existing server. Do not
2746 * establish a connection if one does not already
2749 srv = smbc_server(context, False, server, "IPC$",
2750 workgroup, user, password);
2753 * If no existing server and not an IP addr, look for
2757 !is_ipaddress(server) &&
2758 (resolve_name(server, &rem_ip, 0x1d) || /* LMB */
2759 resolve_name(server, &rem_ip, 0x1b) )) { /* DMB */
2763 dir->dir_type = SMBC_SERVER;
2766 * Get the backup list ...
2768 if (!name_status_find(server, 0, 0,
2769 rem_ip, buserver)) {
2771 DEBUG(0, ("Could not get name of "
2772 "local/domain master browser "
2773 "for server %s\n", server));
2775 SAFE_FREE(dir->fname);
2784 * Get a connection to IPC$ on the server if
2785 * we do not already have one
2787 srv = smbc_server(context, True,
2789 workgroup, user, password);
2791 DEBUG(0, ("got no contact to IPC$\n"));
2793 SAFE_FREE(dir->fname);
2802 /* Now, list the servers ... */
2803 if (!cli_NetServerEnum(srv->cli, server,
2804 0x0000FFFE, list_fn,
2808 SAFE_FREE(dir->fname);
2814 (resolve_name(server, &rem_ip, 0x20))) {
2816 /* If we hadn't found the server, get one now */
2818 srv = smbc_server(context, True,
2826 SAFE_FREE(dir->fname);
2833 dir->dir_type = SMBC_FILE_SHARE;
2836 /* List the shares ... */
2838 if (net_share_enum_rpc(
2841 (void *) dir) < 0 &&
2847 errno = cli_errno(srv->cli);
2849 SAFE_FREE(dir->fname);
2856 /* Neither the workgroup nor server exists */
2857 errno = ECONNREFUSED;
2859 SAFE_FREE(dir->fname);
2868 * The server and share are specified ... work from
2872 struct cli_state *targetcli;
2874 /* We connect to the server and list the directory */
2875 dir->dir_type = SMBC_FILE_SHARE;
2877 srv = smbc_server(context, True, server, share,
2878 workgroup, user, password);
2883 SAFE_FREE(dir->fname);
2892 /* Now, list the files ... */
2894 p = path + strlen(path);
2895 pstrcat(path, "\\*");
2897 if (!cli_resolve_path("", srv->cli, path,
2898 &targetcli, targetpath))
2900 d_printf("Could not resolve %s\n", path);
2902 SAFE_FREE(dir->fname);
2908 if (cli_list(targetcli, targetpath,
2909 aDIR | aSYSTEM | aHIDDEN,
2910 dir_list_fn, (void *)dir) < 0) {
2913 SAFE_FREE(dir->fname);
2916 saved_errno = smbc_errno(context, targetcli);
2918 if (saved_errno == EINVAL) {
2920 * See if they asked to opendir something
2921 * other than a directory. If so, the
2922 * converted error value we got would have
2923 * been EINVAL rather than ENOTDIR.
2925 *p = '\0'; /* restore original path */
2927 if (smbc_getatr(context, srv, path,
2929 NULL, NULL, NULL, NULL,
2931 ! IS_DOS_DIR(mode)) {
2933 /* It is. Correct the error value */
2934 saved_errno = ENOTDIR;
2939 * If there was an error and the server is no
2942 cb = &context->callbacks;
2943 if (cli_is_error(targetcli) &&
2944 cb->check_server_fn(context, srv)) {
2946 /* ... then remove it. */
2947 if (cb->remove_unused_server_fn(context,
2950 * We could not remove the server
2951 * completely, remove it from the
2952 * cache so we will not get it
2953 * again. It will be removed when the
2954 * last file/dir is closed.
2956 cb->remove_cached_srv_fn(context, srv);
2960 errno = saved_errno;
2967 DLIST_ADD(context->internal->_files, dir);
2973 * Routine to close a directory
2977 smbc_closedir_ctx(SMBCCTX *context,
2981 if (!context || !context->internal ||
2982 !context->internal->_initialized) {
2989 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2996 smbc_remove_dir(dir); /* Clean it up */
2998 DLIST_REMOVE(context->internal->_files, dir);
3002 SAFE_FREE(dir->fname);
3003 SAFE_FREE(dir); /* Free the space too */
3011 smbc_readdir_internal(SMBCCTX * context,
3012 struct smbc_dirent *dest,
3013 struct smbc_dirent *src,
3014 int max_namebuf_len)
3016 if (context->options.urlencode_readdir_entries) {
3018 /* url-encode the name. get back remaining buffer space */
3020 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3022 /* We now know the name length */
3023 dest->namelen = strlen(dest->name);
3025 /* Save the pointer to the beginning of the comment */
3026 dest->comment = dest->name + dest->namelen + 1;
3028 /* Copy the comment */
3029 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3030 dest->comment[max_namebuf_len - 1] = '\0';
3032 /* Save other fields */
3033 dest->smbc_type = src->smbc_type;
3034 dest->commentlen = strlen(dest->comment);
3035 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3039 /* No encoding. Just copy the entry as is. */
3040 memcpy(dest, src, src->dirlen);
3041 dest->comment = (char *)(&dest->name + src->namelen + 1);
3047 * Routine to get a directory entry
3050 struct smbc_dirent *
3051 smbc_readdir_ctx(SMBCCTX *context,
3055 struct smbc_dirent *dirp, *dirent;
3057 /* Check that all is ok first ... */
3059 if (!context || !context->internal ||
3060 !context->internal->_initialized) {
3063 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3068 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3071 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3076 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3079 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3084 if (!dir->dir_next) {
3088 dirent = dir->dir_next->dirent;
3096 dirp = (struct smbc_dirent *)context->internal->_dirent;
3097 maxlen = (sizeof(context->internal->_dirent) -
3098 sizeof(struct smbc_dirent));
3100 smbc_readdir_internal(context, dirp, dirent, maxlen);
3102 dir->dir_next = dir->dir_next->next;
3108 * Routine to get directory entries
3112 smbc_getdents_ctx(SMBCCTX *context,
3114 struct smbc_dirent *dirp,
3120 char *ndir = (char *)dirp;
3121 struct smbc_dir_list *dirlist;
3123 /* Check that all is ok first ... */
3125 if (!context || !context->internal ||
3126 !context->internal->_initialized) {
3133 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3140 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3148 * Now, retrieve the number of entries that will fit in what was passed
3149 * We have to figure out if the info is in the list, or we need to
3150 * send a request to the server to get the info.
3153 while ((dirlist = dir->dir_next)) {
3154 struct smbc_dirent *dirent;
3156 if (!dirlist->dirent) {
3158 errno = ENOENT; /* Bad error */
3163 /* Do urlencoding of next entry, if so selected */
3164 dirent = (struct smbc_dirent *)context->internal->_dirent;
3165 maxlen = (sizeof(context->internal->_dirent) -
3166 sizeof(struct smbc_dirent));
3167 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3169 reqd = dirent->dirlen;
3173 if (rem < count) { /* We managed to copy something */
3179 else { /* Nothing copied ... */
3181 errno = EINVAL; /* Not enough space ... */
3188 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3190 ((struct smbc_dirent *)ndir)->comment =
3191 (char *)(&((struct smbc_dirent *)ndir)->name +
3199 dir->dir_next = dirlist = dirlist -> next;
3210 * Routine to create a directory ...
3214 smbc_mkdir_ctx(SMBCCTX *context,
3224 pstring path, targetpath;
3225 struct cli_state *targetcli;
3227 if (!context || !context->internal ||
3228 !context->internal->_initialized) {
3242 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3244 if (smbc_parse_path(context, fname,
3245 workgroup, sizeof(workgroup),
3246 server, sizeof(server),
3247 share, sizeof(share),
3250 password, sizeof(password),
3256 if (user[0] == (char)0) fstrcpy(user, context->user);
3258 srv = smbc_server(context, True,
3259 server, share, workgroup, user, password);
3263 return -1; /* errno set by smbc_server */
3267 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3268 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3270 d_printf("Could not resolve %s\n", path);
3273 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3275 if (!cli_mkdir(targetcli, targetpath)) {
3277 errno = smbc_errno(context, targetcli);
3287 * Our list function simply checks to see if a directory is not empty
3290 static int smbc_rmdir_dirempty = True;
3293 rmdir_list_fn(const char *mnt,
3298 if (strncmp(finfo->name, ".", 1) != 0 &&
3299 strncmp(finfo->name, "..", 2) != 0) {
3301 smbc_rmdir_dirempty = False;
3306 * Routine to remove a directory
3310 smbc_rmdir_ctx(SMBCCTX *context,
3321 struct cli_state *targetcli;
3323 if (!context || !context->internal ||
3324 !context->internal->_initialized) {
3338 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3340 if (smbc_parse_path(context, fname,
3341 workgroup, sizeof(workgroup),
3342 server, sizeof(server),
3343 share, sizeof(share),
3346 password, sizeof(password),
3353 if (user[0] == (char)0) fstrcpy(user, context->user);
3355 srv = smbc_server(context, True,
3356 server, share, workgroup, user, password);
3360 return -1; /* errno set by smbc_server */
3364 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3365 if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
3367 d_printf("Could not resolve %s\n", path);
3370 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3373 if (!cli_rmdir(targetcli, targetpath)) {
3375 errno = smbc_errno(context, targetcli);
3377 if (errno == EACCES) { /* Check if the dir empty or not */
3379 /* Local storage to avoid buffer overflows */
3382 smbc_rmdir_dirempty = True; /* Make this so ... */
3384 pstrcpy(lpath, targetpath);
3385 pstrcat(lpath, "\\*");
3387 if (cli_list(targetcli, lpath,
3388 aDIR | aSYSTEM | aHIDDEN,
3389 rmdir_list_fn, NULL) < 0) {
3391 /* Fix errno to ignore latest error ... */
3392 DEBUG(5, ("smbc_rmdir: "
3393 "cli_list returned an error: %d\n",
3394 smbc_errno(context, targetcli)));
3399 if (smbc_rmdir_dirempty)
3415 * Routine to return the current directory position
3419 smbc_telldir_ctx(SMBCCTX *context,
3422 off_t ret_val; /* Squash warnings about cast */
3424 if (!context || !context->internal ||
3425 !context->internal->_initialized) {
3432 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3439 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3447 * We return the pointer here as the offset
3449 ret_val = (off_t)(long)dir->dir_next;
3455 * A routine to run down the list and see if the entry is OK
3458 struct smbc_dir_list *
3459 smbc_check_dir_ent(struct smbc_dir_list *list,
3460 struct smbc_dirent *dirent)
3463 /* Run down the list looking for what we want */
3467 struct smbc_dir_list *tmp = list;
3471 if (tmp->dirent == dirent)
3480 return NULL; /* Not found, or an error */
3486 * Routine to seek on a directory
3490 smbc_lseekdir_ctx(SMBCCTX *context,
3494 long int l_offset = offset; /* Handle problems of size */
3495 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3496 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3498 if (!context || !context->internal ||
3499 !context->internal->_initialized) {
3506 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3513 /* Now, check what we were passed and see if it is OK ... */
3515 if (dirent == NULL) { /* Seek to the begining of the list */
3517 dir->dir_next = dir->dir_list;
3522 /* Now, run down the list and make sure that the entry is OK */
3523 /* This may need to be changed if we change the format of the list */
3525 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3527 errno = EINVAL; /* Bad entry */
3532 dir->dir_next = list_ent;
3539 * Routine to fstat a dir
3543 smbc_fstatdir_ctx(SMBCCTX *context,
3548 if (!context || !context->internal ||
3549 !context->internal->_initialized) {
3556 /* No code yet ... */
3563 smbc_chmod_ctx(SMBCCTX *context,
3576 if (!context || !context->internal ||
3577 !context->internal->_initialized) {
3579 errno = EINVAL; /* Best I can think of ... */
3591 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3593 if (smbc_parse_path(context, fname,
3594 workgroup, sizeof(workgroup),
3595 server, sizeof(server),
3596 share, sizeof(share),
3599 password, sizeof(password),
3605 if (user[0] == (char)0) fstrcpy(user, context->user);
3607 srv = smbc_server(context, True,
3608 server, share, workgroup, user, password);
3611 return -1; /* errno set by smbc_server */
3616 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3617 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3618 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3619 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3621 if (!cli_setatr(srv->cli, path, mode, 0)) {
3622 errno = smbc_errno(context, srv->cli);
3630 smbc_utimes_ctx(SMBCCTX *context,
3632 struct timeval *tbuf)
3644 if (!context || !context->internal ||
3645 !context->internal->_initialized) {
3647 errno = EINVAL; /* Best I can think of ... */
3660 access_time = write_time = time(NULL);
3662 access_time = tbuf[0].tv_sec;
3663 write_time = tbuf[1].tv_sec;
3672 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
3673 atimebuf[sizeof(atimebuf) - 1] = '\0';
3674 if ((p = strchr(atimebuf, '\n')) != NULL) {
3678 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
3679 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
3680 if ((p = strchr(mtimebuf, '\n')) != NULL) {
3684 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3685 fname, atimebuf, mtimebuf);
3688 if (smbc_parse_path(context, fname,
3689 workgroup, sizeof(workgroup),
3690 server, sizeof(server),
3691 share, sizeof(share),
3694 password, sizeof(password),
3700 if (user[0] == (char)0) fstrcpy(user, context->user);
3702 srv = smbc_server(context, True,
3703 server, share, workgroup, user, password);
3706 return -1; /* errno set by smbc_server */
3709 if (!smbc_setatr(context, srv, path,
3710 0, access_time, write_time, 0, 0)) {
3711 return -1; /* errno set by smbc_setatr */
3718 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
3719 However NT4 gives a "The information may have been modified by a
3720 computer running Windows NT 5.0" if denied ACEs do not appear before
3724 ace_compare(SEC_ACE *ace1,
3727 if (sec_ace_equal(ace1, ace2))
3730 if (ace1->type != ace2->type)
3731 return ace2->type - ace1->type;
3733 if (sid_compare(&ace1->trustee, &ace2->trustee))
3734 return sid_compare(&ace1->trustee, &ace2->trustee);
3736 if (ace1->flags != ace2->flags)
3737 return ace1->flags - ace2->flags;
3739 if (ace1->access_mask != ace2->access_mask)
3740 return ace1->access_mask - ace2->access_mask;
3742 if (ace1->size != ace2->size)
3743 return ace1->size - ace2->size;
3745 return memcmp(ace1, ace2, sizeof(SEC_ACE));
3750 sort_acl(SEC_ACL *the_acl)
3753 if (!the_acl) return;
3755 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
3756 QSORT_CAST ace_compare);
3758 for (i=1;i<the_acl->num_aces;) {
3759 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
3761 for (j=i; j<the_acl->num_aces-1; j++) {
3762 the_acl->aces[j] = the_acl->aces[j+1];
3764 the_acl->num_aces--;
3771 /* convert a SID to a string, either numeric or username/group */
3773 convert_sid_to_string(struct cli_state *ipc_cli,
3779 char **domains = NULL;
3780 char **names = NULL;
3781 enum lsa_SidType *types = NULL;
3782 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3783 sid_to_string(str, sid);
3786 return; /* no lookup desired */
3793 /* Ask LSA to convert the sid to a name */
3795 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ipc_cli->mem_ctx,
3796 pol, 1, sid, &domains,
3798 !domains || !domains[0] || !names || !names[0]) {
3804 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3805 domains[0], lp_winbind_separator(),
3809 /* convert a string to a SID, either numeric or username/group */
3811 convert_string_to_sid(struct cli_state *ipc_cli,
3817 enum lsa_SidType *types = NULL;
3818 DOM_SID *sids = NULL;
3820 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
3827 if (strncmp(str, "S-", 2) == 0) {
3828 return string_to_sid(sid, str);
3835 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx,
3836 pol, 1, &str, NULL, &sids,
3842 sid_copy(sid, &sids[0]);
3849 /* parse an ACE in the same format as print_ace() */
3851 parse_ace(struct cli_state *ipc_cli,
3861 unsigned int aflags;
3865 const struct perm_value *v;
3871 /* These values discovered by inspection */
3872 static const struct perm_value special_values[] = {
3873 { "R", 0x00120089 },
3874 { "W", 0x00120116 },
3875 { "X", 0x001200a0 },
3876 { "D", 0x00010000 },
3877 { "P", 0x00040000 },
3878 { "O", 0x00080000 },
3882 static const struct perm_value standard_values[] = {
3883 { "READ", 0x001200a9 },
3884 { "CHANGE", 0x001301bf },
3885 { "FULL", 0x001f01ff },
3891 p = strchr_m(str,':');
3892 if (!p) return False;
3895 /* Try to parse numeric form */
3897 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3898 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3902 /* Try to parse text form */
3904 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3909 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3913 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3914 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3915 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3916 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3921 /* Only numeric form accepted for flags at present */
3923 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3924 sscanf(tok, "%i", &aflags))) {
3928 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3932 if (strncmp(tok, "0x", 2) == 0) {
3933 if (sscanf(tok, "%i", &amask) != 1) {
3939 for (v = standard_values; v->perm; v++) {
3940 if (strcmp(tok, v->perm) == 0) {
3951 for (v = special_values; v->perm; v++) {
3952 if (v->perm[0] == *p) {
3958 if (!found) return False;
3968 init_sec_ace(ace, &sid, atype, mask, aflags);
3972 /* add an ACE to a list of ACEs in a SEC_ACL */
3974 add_ace(SEC_ACL **the_acl,
3982 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
3986 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
3989 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
3990 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
3991 newacl = make_sec_acl(ctx, (*the_acl)->revision,
3992 1+(*the_acl)->num_aces, aces);
3994 (*the_acl) = newacl;
3999 /* parse a ascii version of a security descriptor */
4001 sec_desc_parse(TALLOC_CTX *ctx,
4002 struct cli_state *ipc_cli,
4007 const char *p = str;
4009 SEC_DESC *ret = NULL;
4011 DOM_SID *grp_sid=NULL;
4012 DOM_SID *owner_sid=NULL;
4016 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4018 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4019 revision = strtol(tok+9, NULL, 16);
4023 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4025 DEBUG(5, ("OWNER specified more than once!\n"));
4028 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4030 !convert_string_to_sid(ipc_cli, pol,
4032 owner_sid, tok+6)) {
4033 DEBUG(5, ("Failed to parse owner sid\n"));
4039 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4041 DEBUG(5, ("OWNER specified more than once!\n"));
4044 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4046 !convert_string_to_sid(ipc_cli, pol,
4048 owner_sid, tok+7)) {
4049 DEBUG(5, ("Failed to parse owner sid\n"));
4055 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4057 DEBUG(5, ("GROUP specified more than once!\n"));
4060 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4062 !convert_string_to_sid(ipc_cli, pol,
4065 DEBUG(5, ("Failed to parse group sid\n"));
4071 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4073 DEBUG(5, ("GROUP specified more than once!\n"));
4076 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4078 !convert_string_to_sid(ipc_cli, pol,
4081 DEBUG(5, ("Failed to parse group sid\n"));
4087 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4089 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4090 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4093 if(!add_ace(&dacl, &ace, ctx)) {
4094 DEBUG(5, ("Failed to add ACL %s\n", tok));
4100 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4102 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4103 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4106 if(!add_ace(&dacl, &ace, ctx)) {
4107 DEBUG(5, ("Failed to add ACL %s\n", tok));
4113 DEBUG(5, ("Failed to parse security descriptor\n"));
4117 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4118 owner_sid, grp_sid, NULL, dacl, &sd_size);
4122 SAFE_FREE(owner_sid);
4128 /* Obtain the current dos attributes */
4129 static DOS_ATTR_DESC *
4130 dos_attr_query(SMBCCTX *context,
4132 const char *filename,
4135 struct timespec create_time_ts;
4136 struct timespec write_time_ts;
4137 struct timespec access_time_ts;
4138 struct timespec change_time_ts;
4141 SMB_INO_T inode = 0;
4144 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4150 /* Obtain the DOS attributes */
4151 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4159 errno = smbc_errno(context, srv->cli);
4160 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4167 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4168 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4169 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4170 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4177 /* parse a ascii version of a security descriptor */
4179 dos_attr_parse(SMBCCTX *context,
4185 const char *p = str;
4188 const char * create_time_attr;
4189 const char * access_time_attr;
4190 const char * write_time_attr;
4191 const char * change_time_attr;
4194 /* Determine whether to use old-style or new-style attribute names */
4195 if (context->internal->_full_time_names) {
4196 /* new-style names */
4197 attr_strings.create_time_attr = "CREATE_TIME";
4198 attr_strings.access_time_attr = "ACCESS_TIME";
4199 attr_strings.write_time_attr = "WRITE_TIME";
4200 attr_strings.change_time_attr = "CHANGE_TIME";
4202 /* old-style names */
4203 attr_strings.create_time_attr = NULL;
4204 attr_strings.access_time_attr = "A_TIME";
4205 attr_strings.write_time_attr = "M_TIME";
4206 attr_strings.change_time_attr = "C_TIME";
4209 /* if this is to set the entire ACL... */
4211 /* ... then increment past the first colon if there is one */
4212 if ((p = strchr(str, ':')) != NULL) {
4219 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
4221 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4222 dad->mode = strtol(tok+5, NULL, 16);
4226 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4227 dad->size = (SMB_OFF_T)atof(tok+5);
4231 n = strlen(attr_strings.access_time_attr);
4232 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4233 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4237 n = strlen(attr_strings.change_time_attr);
4238 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4239 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4243 n = strlen(attr_strings.write_time_attr);
4244 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4245 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4249 if (attr_strings.create_time_attr != NULL) {
4250 n = strlen(attr_strings.create_time_attr);
4251 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4253 dad->create_time = (time_t)strtol(tok+n+1,
4259 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4260 dad->inode = (SMB_INO_T)atof(tok+6);
4266 /*****************************************************
4267 Retrieve the acls for a file.
4268 *******************************************************/
4271 cacl_get(SMBCCTX *context,
4274 struct cli_state *ipc_cli,
4290 BOOL exclude_nt_revision = False;
4291 BOOL exclude_nt_owner = False;
4292 BOOL exclude_nt_group = False;
4293 BOOL exclude_nt_acl = False;
4294 BOOL exclude_dos_mode = False;
4295 BOOL exclude_dos_size = False;
4296 BOOL exclude_dos_create_time = False;
4297 BOOL exclude_dos_access_time = False;
4298 BOOL exclude_dos_write_time = False;
4299 BOOL exclude_dos_change_time = False;
4300 BOOL exclude_dos_inode = False;
4301 BOOL numeric = True;
4302 BOOL determine_size = (bufsize == 0);
4306 fstring name_sandbox;
4310 struct timespec create_time_ts;
4311 struct timespec write_time_ts;
4312 struct timespec access_time_ts;
4313 struct timespec change_time_ts;
4314 time_t create_time = (time_t)0;
4315 time_t write_time = (time_t)0;
4316 time_t access_time = (time_t)0;
4317 time_t change_time = (time_t)0;
4321 struct cli_state *cli = srv->cli;
4323 const char * create_time_attr;
4324 const char * access_time_attr;
4325 const char * write_time_attr;
4326 const char * change_time_attr;
4329 const char * create_time_attr;
4330 const char * access_time_attr;
4331 const char * write_time_attr;
4332 const char * change_time_attr;
4333 } excl_attr_strings;
4335 /* Determine whether to use old-style or new-style attribute names */
4336 if (context->internal->_full_time_names) {
4337 /* new-style names */
4338 attr_strings.create_time_attr = "CREATE_TIME";
4339 attr_strings.access_time_attr = "ACCESS_TIME";
4340 attr_strings.write_time_attr = "WRITE_TIME";
4341 attr_strings.change_time_attr = "CHANGE_TIME";
4343 excl_attr_strings.create_time_attr = "CREATE_TIME";
4344 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4345 excl_attr_strings.write_time_attr = "WRITE_TIME";
4346 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4348 /* old-style names */
4349 attr_strings.create_time_attr = NULL;
4350 attr_strings.access_time_attr = "A_TIME";
4351 attr_strings.write_time_attr = "M_TIME";
4352 attr_strings.change_time_attr = "C_TIME";
4354 excl_attr_strings.create_time_attr = NULL;
4355 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4356 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4357 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4360 /* Copy name so we can strip off exclusions (if any are specified) */
4361 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4363 /* Ensure name is null terminated */
4364 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4366 /* Play in the sandbox */
4367 name = name_sandbox;
4369 /* If there are any exclusions, point to them and mask them from name */
4370 if ((pExclude = strchr(name, '!')) != NULL)
4375 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4376 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4377 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4378 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4379 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4380 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4381 numeric = (* (name + strlen(name) - 1) != '+');
4383 /* Look for exclusions from "all" requests */
4384 if (all || all_nt || all_dos) {
4386 /* Exclusions are delimited by '!' */
4389 pExclude = (p == NULL ? NULL : p + 1)) {
4391 /* Find end of this exclusion name */
4392 if ((p = strchr(pExclude, '!')) != NULL)
4397 /* Which exclusion name is this? */
4398 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4399 exclude_nt_revision = True;
4401 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4402 exclude_nt_owner = True;
4404 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4405 exclude_nt_group = True;
4407 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4408 exclude_nt_acl = True;
4410 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4411 exclude_dos_mode = True;
4413 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4414 exclude_dos_size = True;
4416 else if (excl_attr_strings.create_time_attr != NULL &&
4417 StrCaseCmp(pExclude,
4418 excl_attr_strings.change_time_attr) == 0) {
4419 exclude_dos_create_time = True;
4421 else if (StrCaseCmp(pExclude,
4422 excl_attr_strings.access_time_attr) == 0) {
4423 exclude_dos_access_time = True;
4425 else if (StrCaseCmp(pExclude,
4426 excl_attr_strings.write_time_attr) == 0) {
4427 exclude_dos_write_time = True;
4429 else if (StrCaseCmp(pExclude,
4430 excl_attr_strings.change_time_attr) == 0) {
4431 exclude_dos_change_time = True;
4433 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4434 exclude_dos_inode = True;
4437 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4448 * If we are (possibly) talking to an NT or new system and some NT
4449 * attributes have been requested...
4451 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4452 /* Point to the portion after "system.nt_sec_desc." */
4453 name += 19; /* if (all) this will be invalid but unused */
4455 /* ... then obtain any NT attributes which were requested */
4456 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4459 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4460 filename, cli_errstr(cli)));
4465 sd = cli_query_secdesc(cli, fnum, ctx);
4469 ("cacl_get Failed to query old descriptor\n"));
4474 cli_close(cli, fnum);
4476 if (! exclude_nt_revision) {
4477 if (all || all_nt) {
4478 if (determine_size) {
4479 p = talloc_asprintf(ctx,
4488 n = snprintf(buf, bufsize,
4492 } else if (StrCaseCmp(name, "revision") == 0) {
4493 if (determine_size) {
4494 p = talloc_asprintf(ctx, "%d",
4502 n = snprintf(buf, bufsize, "%d",
4507 if (!determine_size && n > bufsize) {
4516 if (! exclude_nt_owner) {
4517 /* Get owner and group sid */
4518 if (sd->owner_sid) {
4519 convert_sid_to_string(ipc_cli, pol,
4524 fstrcpy(sidstr, "");
4527 if (all || all_nt) {
4528 if (determine_size) {
4529 p = talloc_asprintf(ctx, ",OWNER:%s",
4537 n = snprintf(buf, bufsize,
4538 ",OWNER:%s", sidstr);
4540 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
4541 if (determine_size) {
4542 p = talloc_asprintf(ctx, "%s", sidstr);
4549 n = snprintf(buf, bufsize, "%s",
4554 if (!determine_size && n > bufsize) {
4563 if (! exclude_nt_group) {
4564 if (sd->group_sid) {
4565 convert_sid_to_string(ipc_cli, pol,
4569 fstrcpy(sidstr, "");
4572 if (all || all_nt) {
4573 if (determine_size) {
4574 p = talloc_asprintf(ctx, ",GROUP:%s",
4582 n = snprintf(buf, bufsize,
4583 ",GROUP:%s", sidstr);
4585 } else if (StrnCaseCmp(name, "group", 5) == 0) {
4586 if (determine_size) {
4587 p = talloc_asprintf(ctx, "%s", sidstr);
4594 n = snprintf(buf, bufsize,
4599 if (!determine_size && n > bufsize) {
4608 if (! exclude_nt_acl) {
4609 /* Add aces to value buffer */
4610 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
4612 SEC_ACE *ace = &sd->dacl->aces[i];
4613 convert_sid_to_string(ipc_cli, pol,
4617 if (all || all_nt) {
4618 if (determine_size) {
4619 p = talloc_asprintf(
4635 ",ACL:%s:%d/%d/0x%08x",
4641 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
4642 StrCaseCmp(name+3, sidstr) == 0) ||
4643 (StrnCaseCmp(name, "acl+", 4) == 0 &&
4644 StrCaseCmp(name+4, sidstr) == 0)) {
4645 if (determine_size) {
4646 p = talloc_asprintf(
4658 n = snprintf(buf, bufsize,
4664 } else if (all_nt_acls) {
4665 if (determine_size) {
4666 p = talloc_asprintf(
4668 "%s%s:%d/%d/0x%08x",
4680 n = snprintf(buf, bufsize,
4681 "%s%s:%d/%d/0x%08x",
4699 /* Restore name pointer to its original value */
4703 if (all || some_dos) {
4704 /* Point to the portion after "system.dos_attr." */
4705 name += 16; /* if (all) this will be invalid but unused */
4707 /* Obtain the DOS attributes */
4708 if (!smbc_getatr(context, srv, filename, &mode, &size,
4715 errno = smbc_errno(context, srv->cli);
4720 create_time = convert_timespec_to_time_t(create_time_ts);
4721 access_time = convert_timespec_to_time_t(access_time_ts);
4722 write_time = convert_timespec_to_time_t(write_time_ts);
4723 change_time = convert_timespec_to_time_t(change_time_ts);
4725 if (! exclude_dos_mode) {
4726 if (all || all_dos) {
4727 if (determine_size) {
4728 p = talloc_asprintf(ctx,
4741 n = snprintf(buf, bufsize,
4749 } else if (StrCaseCmp(name, "mode") == 0) {
4750 if (determine_size) {
4751 p = talloc_asprintf(ctx, "0x%x", mode);
4758 n = snprintf(buf, bufsize,
4763 if (!determine_size && n > bufsize) {
4772 if (! exclude_dos_size) {
4773 if (all || all_dos) {
4774 if (determine_size) {
4775 p = talloc_asprintf(
4785 n = snprintf(buf, bufsize,
4789 } else if (StrCaseCmp(name, "size") == 0) {
4790 if (determine_size) {
4791 p = talloc_asprintf(
4801 n = snprintf(buf, bufsize,
4807 if (!determine_size && n > bufsize) {
4816 if (! exclude_dos_create_time &&
4817 attr_strings.create_time_attr != NULL) {
4818 if (all || all_dos) {
4819 if (determine_size) {
4820 p = talloc_asprintf(ctx,
4822 attr_strings.create_time_attr,
4830 n = snprintf(buf, bufsize,
4832 attr_strings.create_time_attr,
4835 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
4836 if (determine_size) {
4837 p = talloc_asprintf(ctx, "%lu", create_time);
4844 n = snprintf(buf, bufsize,
4845 "%lu", create_time);
4849 if (!determine_size && n > bufsize) {
4858 if (! exclude_dos_access_time) {
4859 if (all || all_dos) {
4860 if (determine_size) {
4861 p = talloc_asprintf(ctx,
4863 attr_strings.access_time_attr,
4871 n = snprintf(buf, bufsize,
4873 attr_strings.access_time_attr,
4876 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
4877 if (determine_size) {
4878 p = talloc_asprintf(ctx, "%lu", access_time);
4885 n = snprintf(buf, bufsize,
4886 "%lu", access_time);
4890 if (!determine_size && n > bufsize) {
4899 if (! exclude_dos_write_time) {
4900 if (all || all_dos) {
4901 if (determine_size) {
4902 p = talloc_asprintf(ctx,
4904 attr_strings.write_time_attr,
4912 n = snprintf(buf, bufsize,
4914 attr_strings.write_time_attr,
4917 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
4918 if (determine_size) {
4919 p = talloc_asprintf(ctx, "%lu", write_time);
4926 n = snprintf(buf, bufsize,
4931 if (!determine_size && n > bufsize) {
4940 if (! exclude_dos_change_time) {
4941 if (all || all_dos) {
4942 if (determine_size) {
4943 p = talloc_asprintf(ctx,
4945 attr_strings.change_time_attr,
4953 n = snprintf(buf, bufsize,
4955 attr_strings.change_time_attr,
4958 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
4959 if (determine_size) {
4960 p = talloc_asprintf(ctx, "%lu", change_time);
4967 n = snprintf(buf, bufsize,
4968 "%lu", change_time);
4972 if (!determine_size && n > bufsize) {
4981 if (! exclude_dos_inode) {
4982 if (all || all_dos) {
4983 if (determine_size) {
4984 p = talloc_asprintf(
4994 n = snprintf(buf, bufsize,
4998 } else if (StrCaseCmp(name, "inode") == 0) {
4999 if (determine_size) {
5000 p = talloc_asprintf(
5010 n = snprintf(buf, bufsize,
5016 if (!determine_size && n > bufsize) {
5025 /* Restore name pointer to its original value */
5038 /*****************************************************
5039 set the ACLs on a file given an ascii description
5040 *******************************************************/
5042 cacl_set(TALLOC_CTX *ctx,
5043 struct cli_state *cli,
5044 struct cli_state *ipc_cli,
5046 const char *filename,
5047 const char *the_acl,
5053 SEC_DESC *sd = NULL, *old;
5054 SEC_ACL *dacl = NULL;
5055 DOM_SID *owner_sid = NULL;
5056 DOM_SID *grp_sid = NULL;
5061 BOOL numeric = True;
5063 /* the_acl will be null for REMOVE_ALL operations */
5065 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5069 /* if this is to set the entire ACL... */
5070 if (*the_acl == '*') {
5071 /* ... then increment past the first colon */
5075 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5076 CONST_DISCARD(char *, the_acl));
5084 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5085 that doesn't deref sd */
5087 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5092 /* The desired access below is the only one I could find that works
5093 with NT4, W2KP and Samba */
5095 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5098 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5099 filename, cli_errstr(cli)));
5104 old = cli_query_secdesc(cli, fnum, ctx);
5107 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5112 cli_close(cli, fnum);
5115 case SMBC_XATTR_MODE_REMOVE_ALL:
5116 old->dacl->num_aces = 0;
5117 SAFE_FREE(old->dacl->aces);
5118 SAFE_FREE(old->dacl);
5123 case SMBC_XATTR_MODE_REMOVE:
5124 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5127 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5128 if (sec_ace_equal(&sd->dacl->aces[i],
5129 &old->dacl->aces[j])) {
5131 for (k=j; k<old->dacl->num_aces-1;k++) {
5132 old->dacl->aces[k] =
5133 old->dacl->aces[k+1];
5135 old->dacl->num_aces--;
5136 if (old->dacl->num_aces == 0) {
5137 SAFE_FREE(old->dacl->aces);
5138 SAFE_FREE(old->dacl);
5155 case SMBC_XATTR_MODE_ADD:
5156 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5159 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5160 if (sid_equal(&sd->dacl->aces[i].trustee,
5161 &old->dacl->aces[j].trustee)) {
5162 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5167 old->dacl->aces[j] = sd->dacl->aces[i];
5173 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5179 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5180 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5186 case SMBC_XATTR_MODE_SET:
5188 owner_sid = old->owner_sid;
5189 grp_sid = old->group_sid;
5193 case SMBC_XATTR_MODE_CHOWN:
5194 owner_sid = sd->owner_sid;
5197 case SMBC_XATTR_MODE_CHGRP:
5198 grp_sid = sd->group_sid;
5202 /* Denied ACE entries must come before allowed ones */
5203 sort_acl(old->dacl);
5205 /* Create new security descriptor and set it */
5206 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5207 owner_sid, grp_sid, NULL, dacl, &sd_size);
5209 fnum = cli_nt_create(cli, filename,
5210 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5213 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5214 filename, cli_errstr(cli)));
5219 if (!cli_set_secdesc(cli, fnum, sd)) {
5220 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5227 cli_close(cli, fnum);
5238 smbc_setxattr_ctx(SMBCCTX *context,
5259 const char * create_time_attr;
5260 const char * access_time_attr;
5261 const char * write_time_attr;
5262 const char * change_time_attr;
5265 if (!context || !context->internal ||
5266 !context->internal->_initialized) {
5268 errno = EINVAL; /* Best I can think of ... */
5280 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5281 fname, name, (int) size, (const char*)value));
5283 if (smbc_parse_path(context, fname,
5284 workgroup, sizeof(workgroup),
5285 server, sizeof(server),
5286 share, sizeof(share),
5289 password, sizeof(password),
5295 if (user[0] == (char)0) fstrcpy(user, context->user);
5297 srv = smbc_server(context, True,
5298 server, share, workgroup, user, password);
5300 return -1; /* errno set by smbc_server */
5303 if (! srv->no_nt_session) {
5304 ipc_srv = smbc_attr_server(context, server, share,
5305 workgroup, user, password,
5307 srv->no_nt_session = True;
5312 ctx = talloc_init("smbc_setxattr");
5319 * Are they asking to set the entire set of known attributes?
5321 if (StrCaseCmp(name, "system.*") == 0 ||
5322 StrCaseCmp(name, "system.*+") == 0) {
5325 talloc_asprintf(ctx, "%s:%s",
5326 name+7, (const char *) value);
5334 ret = cacl_set(ctx, srv->cli,
5335 ipc_srv->cli, &pol, path,
5338 ? SMBC_XATTR_MODE_SET
5339 : SMBC_XATTR_MODE_ADD),
5345 /* get a DOS Attribute Descriptor with current attributes */
5346 dad = dos_attr_query(context, ctx, path, srv);
5348 /* Overwrite old with new, using what was provided */
5349 dos_attr_parse(context, dad, srv, namevalue);
5351 /* Set the new DOS attributes */
5352 if (! smbc_setatr(context, srv, path,
5359 /* cause failure if NT failed too */
5364 /* we only fail if both NT and DOS sets failed */
5365 if (ret < 0 && ! dad) {
5366 ret = -1; /* in case dad was null */
5372 talloc_destroy(ctx);
5377 * Are they asking to set an access control element or to set
5378 * the entire access control list?
5380 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5381 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5382 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5383 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5384 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5388 talloc_asprintf(ctx, "%s:%s",
5389 name+19, (const char *) value);
5392 ret = -1; /* errno set by smbc_server() */
5394 else if (! namevalue) {
5398 ret = cacl_set(ctx, srv->cli,
5399 ipc_srv->cli, &pol, path,
5402 ? SMBC_XATTR_MODE_SET
5403 : SMBC_XATTR_MODE_ADD),
5406 talloc_destroy(ctx);
5411 * Are they asking to set the owner?
5413 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5414 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5418 talloc_asprintf(ctx, "%s:%s",
5419 name+19, (const char *) value);
5423 ret = -1; /* errno set by smbc_server() */
5425 else if (! namevalue) {
5429 ret = cacl_set(ctx, srv->cli,
5430 ipc_srv->cli, &pol, path,
5431 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5433 talloc_destroy(ctx);
5438 * Are they asking to set the group?
5440 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5441 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5445 talloc_asprintf(ctx, "%s:%s",
5446 name+19, (const char *) value);
5449 /* errno set by smbc_server() */
5452 else if (! namevalue) {
5456 ret = cacl_set(ctx, srv->cli,
5457 ipc_srv->cli, &pol, path,
5458 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5460 talloc_destroy(ctx);
5464 /* Determine whether to use old-style or new-style attribute names */
5465 if (context->internal->_full_time_names) {
5466 /* new-style names */
5467 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5468 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5469 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5470 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5472 /* old-style names */
5473 attr_strings.create_time_attr = NULL;
5474 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5475 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5476 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5480 * Are they asking to set a DOS attribute?
5482 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5483 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5484 (attr_strings.create_time_attr != NULL &&
5485 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5486 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5487 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5488 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5490 /* get a DOS Attribute Descriptor with current attributes */
5491 dad = dos_attr_query(context, ctx, path, srv);
5494 talloc_asprintf(ctx, "%s:%s",
5495 name+16, (const char *) value);
5500 /* Overwrite old with provided new params */
5501 dos_attr_parse(context, dad, srv, namevalue);
5503 /* Set the new DOS attributes */
5504 ret2 = smbc_setatr(context, srv, path,
5511 /* ret2 has True (success) / False (failure) */
5522 talloc_destroy(ctx);
5526 /* Unsupported attribute name */
5527 talloc_destroy(ctx);
5533 smbc_getxattr_ctx(SMBCCTX *context,
5551 const char * create_time_attr;
5552 const char * access_time_attr;
5553 const char * write_time_attr;
5554 const char * change_time_attr;
5558 if (!context || !context->internal ||
5559 !context->internal->_initialized) {
5561 errno = EINVAL; /* Best I can think of ... */
5573 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
5575 if (smbc_parse_path(context, fname,
5576 workgroup, sizeof(workgroup),
5577 server, sizeof(server),
5578 share, sizeof(share),
5581 password, sizeof(password),
5587 if (user[0] == (char)0) fstrcpy(user, context->user);
5589 srv = smbc_server(context, True,
5590 server, share, workgroup, user, password);
5592 return -1; /* errno set by smbc_server */
5595 if (! srv->no_nt_session) {
5596 ipc_srv = smbc_attr_server(context, server, share,
5597 workgroup, user, password,
5600 srv->no_nt_session = True;
5606 ctx = talloc_init("smbc:getxattr");
5612 /* Determine whether to use old-style or new-style attribute names */
5613 if (context->internal->_full_time_names) {
5614 /* new-style names */
5615 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5616 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5617 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5618 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5620 /* old-style names */
5621 attr_strings.create_time_attr = NULL;
5622 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5623 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5624 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5627 /* Are they requesting a supported attribute? */
5628 if (StrCaseCmp(name, "system.*") == 0 ||
5629 StrnCaseCmp(name, "system.*!", 9) == 0 ||
5630 StrCaseCmp(name, "system.*+") == 0 ||
5631 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
5632 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5633 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
5634 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5635 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
5636 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5637 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5638 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5639 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5640 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5641 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5642 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
5643 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5644 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
5645 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5646 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
5647 (attr_strings.create_time_attr != NULL &&
5648 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5649 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5650 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5651 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
5652 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
5655 ret = cacl_get(context, ctx, srv,
5656 ipc_srv == NULL ? NULL : ipc_srv->cli,
5658 CONST_DISCARD(char *, name),
5659 CONST_DISCARD(char *, value), size);
5660 if (ret < 0 && errno == 0) {
5661 errno = smbc_errno(context, srv->cli);
5663 talloc_destroy(ctx);
5667 /* Unsupported attribute name */
5668 talloc_destroy(ctx);
5675 smbc_removexattr_ctx(SMBCCTX *context,
5691 if (!context || !context->internal ||
5692 !context->internal->_initialized) {
5694 errno = EINVAL; /* Best I can think of ... */
5706 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
5708 if (smbc_parse_path(context, fname,
5709 workgroup, sizeof(workgroup),
5710 server, sizeof(server),
5711 share, sizeof(share),
5714 password, sizeof(password),
5720 if (user[0] == (char)0) fstrcpy(user, context->user);
5722 srv = smbc_server(context, True,
5723 server, share, workgroup, user, password);
5725 return -1; /* errno set by smbc_server */
5728 if (! srv->no_nt_session) {
5729 ipc_srv = smbc_attr_server(context, server, share,
5730 workgroup, user, password,
5732 srv->no_nt_session = True;
5738 return -1; /* errno set by smbc_attr_server */
5741 ctx = talloc_init("smbc_removexattr");
5747 /* Are they asking to set the entire ACL? */
5748 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5749 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
5752 ret = cacl_set(ctx, srv->cli,
5753 ipc_srv->cli, &pol, path,
5754 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
5755 talloc_destroy(ctx);
5760 * Are they asking to remove one or more spceific security descriptor
5763 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5764 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5765 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
5766 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5767 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
5768 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5769 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5772 ret = cacl_set(ctx, srv->cli,
5773 ipc_srv->cli, &pol, path,
5774 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
5775 talloc_destroy(ctx);
5779 /* Unsupported attribute name */
5780 talloc_destroy(ctx);
5786 smbc_listxattr_ctx(SMBCCTX *context,
5792 * This isn't quite what listxattr() is supposed to do. This returns
5793 * the complete set of attribute names, always, rather than only those
5794 * attribute names which actually exist for a file. Hmmm...
5796 const char supported_old[] =
5799 "system.nt_sec_desc.revision\0"
5800 "system.nt_sec_desc.owner\0"
5801 "system.nt_sec_desc.owner+\0"
5802 "system.nt_sec_desc.group\0"
5803 "system.nt_sec_desc.group+\0"
5804 "system.nt_sec_desc.acl.*\0"
5805 "system.nt_sec_desc.acl\0"
5806 "system.nt_sec_desc.acl+\0"
5807 "system.nt_sec_desc.*\0"
5808 "system.nt_sec_desc.*+\0"
5809 "system.dos_attr.*\0"
5810 "system.dos_attr.mode\0"
5811 "system.dos_attr.c_time\0"
5812 "system.dos_attr.a_time\0"
5813 "system.dos_attr.m_time\0"
5815 const char supported_new[] =
5818 "system.nt_sec_desc.revision\0"
5819 "system.nt_sec_desc.owner\0"
5820 "system.nt_sec_desc.owner+\0"
5821 "system.nt_sec_desc.group\0"
5822 "system.nt_sec_desc.group+\0"
5823 "system.nt_sec_desc.acl.*\0"
5824 "system.nt_sec_desc.acl\0"
5825 "system.nt_sec_desc.acl+\0"
5826 "system.nt_sec_desc.*\0"
5827 "system.nt_sec_desc.*+\0"
5828 "system.dos_attr.*\0"
5829 "system.dos_attr.mode\0"
5830 "system.dos_attr.create_time\0"
5831 "system.dos_attr.access_time\0"
5832 "system.dos_attr.write_time\0"
5833 "system.dos_attr.change_time\0"
5835 const char * supported;
5837 if (context->internal->_full_time_names) {
5838 supported = supported_new;
5840 supported = supported_old;
5844 return sizeof(supported);
5847 if (sizeof(supported) > size) {
5852 /* this can't be strcpy() because there are embedded null characters */
5853 memcpy(list, supported, sizeof(supported));
5854 return sizeof(supported);
5859 * Open a print file to be written to by other calls
5863 smbc_open_print_job_ctx(SMBCCTX *context,
5872 if (!context || !context->internal ||
5873 !context->internal->_initialized) {
5887 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
5889 if (smbc_parse_path(context, fname,
5891 server, sizeof(server),
5892 share, sizeof(share),
5895 password, sizeof(password),
5901 /* What if the path is empty, or the file exists? */
5903 return context->open(context, fname, O_WRONLY, 666);
5908 * Routine to print a file on a remote server ...
5910 * We open the file, which we assume to be on a remote server, and then
5911 * copy it to a print file on the share specified by printq.
5915 smbc_print_file_ctx(SMBCCTX *c_file,
5927 if (!c_file || !c_file->internal->_initialized || !c_print ||
5928 !c_print->internal->_initialized) {
5935 if (!fname && !printq) {
5942 /* Try to open the file for reading ... */
5944 if ((long)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
5946 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
5947 return -1; /* smbc_open sets errno */
5951 /* Now, try to open the printer file for writing */
5953 if ((long)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
5955 saverr = errno; /* Save errno */
5956 c_file->close_fn(c_file, fid1);
5962 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
5966 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
5969 c_file->close_fn(c_file, fid1);
5970 c_print->close_fn(c_print, fid2);
5979 c_file->close_fn(c_file, fid1); /* We have to close these anyway */
5980 c_print->close_fn(c_print, fid2);
5994 * Routine to list print jobs on a printer share ...
5998 smbc_list_print_jobs_ctx(SMBCCTX *context,
6000 smbc_list_print_job_fn fn)
6010 if (!context || !context->internal ||
6011 !context->internal->_initialized) {
6025 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6027 if (smbc_parse_path(context, fname,
6028 workgroup, sizeof(workgroup),
6029 server, sizeof(server),
6030 share, sizeof(share),
6033 password, sizeof(password),
6039 if (user[0] == (char)0) fstrcpy(user, context->user);
6041 srv = smbc_server(context, True,
6042 server, share, workgroup, user, password);
6046 return -1; /* errno set by smbc_server */
6050 if (cli_print_queue(srv->cli,
6051 (void (*)(struct print_job_info *))fn) < 0) {
6053 errno = smbc_errno(context, srv->cli);
6063 * Delete a print job from a remote printer share
6067 smbc_unlink_print_job_ctx(SMBCCTX *context,
6080 if (!context || !context->internal ||
6081 !context->internal->_initialized) {
6095 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6097 if (smbc_parse_path(context, fname,
6098 workgroup, sizeof(workgroup),
6099 server, sizeof(server),
6100 share, sizeof(share),
6103 password, sizeof(password),
6109 if (user[0] == (char)0) fstrcpy(user, context->user);
6111 srv = smbc_server(context, True,
6112 server, share, workgroup, user, password);
6116 return -1; /* errno set by smbc_server */
6120 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6123 errno = smbc_errno(context, srv->cli);
6124 else if (err == ERRnosuchprintjob)
6135 * Get a new empty handle to fill in with your own info
6138 smbc_new_context(void)
6142 context = SMB_MALLOC_P(SMBCCTX);
6148 ZERO_STRUCTP(context);
6150 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6151 if (!context->internal) {
6157 ZERO_STRUCTP(context->internal);
6160 /* ADD REASONABLE DEFAULTS */
6162 context->timeout = 20000; /* 20 seconds */
6164 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6165 context->options.urlencode_readdir_entries = False;/* backward compat */
6166 context->options.one_share_per_server = False;/* backward compat */
6167 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6168 /* 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 */)
6306 smbc_get_auth_data_with_context_fn auth_fn;
6310 va_start(ap, option_name);
6312 if (strcmp(option_name, "debug_to_stderr") == 0) {
6314 * Log to standard error instead of standard output.
6316 option_value.b = (BOOL) va_arg(ap, int);
6317 context->internal->_debug_stderr = option_value.b;
6319 } else if (strcmp(option_name, "full_time_names") == 0) {
6321 * Use new-style time attribute names, e.g. WRITE_TIME rather
6322 * than the old-style names such as M_TIME. This allows also
6323 * setting/getting CREATE_TIME which was previously
6324 * unimplemented. (Note that the old C_TIME was supposed to
6325 * be CHANGE_TIME but was confused and sometimes referred to
6328 option_value.b = (BOOL) va_arg(ap, int);
6329 context->internal->_full_time_names = option_value.b;
6331 } else if (strcmp(option_name, "open_share_mode") == 0) {
6333 * The share mode to use for files opened with
6334 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6336 option_value.i = va_arg(ap, int);
6337 context->internal->_share_mode =
6338 (smbc_share_mode) option_value.i;
6340 } else if (strcmp(option_name, "auth_function") == 0) {
6342 * Use the new-style authentication function which includes
6345 option_value.auth_fn =
6346 va_arg(ap, smbc_get_auth_data_with_context_fn);
6347 context->internal->_auth_fn_with_context =
6348 option_value.auth_fn;
6349 } else if (strcmp(option_name, "user_data") == 0) {
6351 * Save a user data handle which may be retrieved by the user
6352 * with smbc_option_get()
6354 option_value.v = va_arg(ap, void *);
6355 context->internal->_user_data = option_value.v;
6363 * Retrieve the current value of an option
6366 smbc_option_get(SMBCCTX *context,
6369 if (strcmp(option_name, "debug_stderr") == 0) {
6371 * Log to standard error instead of standard output.
6373 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6374 return (void *) (intptr_t) context->internal->_debug_stderr;
6376 return (void *) context->internal->_debug_stderr;
6378 } else if (strcmp(option_name, "full_time_names") == 0) {
6380 * Use new-style time attribute names, e.g. WRITE_TIME rather
6381 * than the old-style names such as M_TIME. This allows also
6382 * setting/getting CREATE_TIME which was previously
6383 * unimplemented. (Note that the old C_TIME was supposed to
6384 * be CHANGE_TIME but was confused and sometimes referred to
6387 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6388 return (void *) (intptr_t) context->internal->_full_time_names;
6390 return (void *) context->internal->_full_time_names;
6393 } else if (strcmp(option_name, "auth_function") == 0) {
6395 * Use the new-style authentication function which includes
6398 return (void *) context->internal->_auth_fn_with_context;
6399 } else if (strcmp(option_name, "user_data") == 0) {
6401 * Save a user data handle which may be retrieved by the user
6402 * with smbc_option_get()
6404 return context->internal->_user_data;
6412 * Initialise the library etc
6414 * We accept a struct containing handle information.
6415 * valid values for info->debug from 0 to 100,
6416 * and insist that info->fn must be non-null.
6419 smbc_init_context(SMBCCTX *context)
6426 if (!context || !context->internal) {
6431 /* Do not initialise the same client twice */
6432 if (context->internal->_initialized) {
6436 if ((!context->callbacks.auth_fn &&
6437 !context->internal->_auth_fn_with_context) ||
6438 context->debug < 0 ||
6439 context->debug > 100) {
6446 if (!smbc_initialized) {
6448 * Do some library-wide intializations the first time we get
6451 BOOL conf_loaded = False;
6453 /* Set this to what the user wants */
6454 DEBUGLEVEL = context->debug;
6458 setup_logging("libsmbclient", True);
6459 if (context->internal->_debug_stderr) {
6461 x_setbuf(x_stderr, NULL);
6464 /* Here we would open the smb.conf file if needed ... */
6466 in_client = True; /* FIXME, make a param */
6468 home = getenv("HOME");
6470 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
6471 if (lp_load(conf, True, False, False, True)) {
6474 DEBUG(5, ("Could not load config file: %s\n",
6481 * Well, if that failed, try the dyn_CONFIGFILE
6482 * Which points to the standard locn, and if that
6483 * fails, silently ignore it and use the internal
6487 if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
6488 DEBUG(5, ("Could not load config file: %s\n",
6492 * We loaded the global config file. Now lets
6493 * load user-specific modifications to the
6496 slprintf(conf, sizeof(conf),
6497 "%s/.smb/smb.conf.append", home);
6498 if (!lp_load(conf, True, False, False, False)) {
6500 ("Could not append config file: "
6507 load_interfaces(); /* Load the list of interfaces ... */
6509 reopen_logs(); /* Get logging working ... */
6512 * Block SIGPIPE (from lib/util_sock.c: write())
6513 * It is not needed and should not stop execution
6515 BlockSignals(True, SIGPIPE);
6517 /* Done with one-time initialisation */
6518 smbc_initialized = 1;
6522 if (!context->user) {
6524 * FIXME: Is this the best way to get the user info?
6526 user = getenv("USER");
6527 /* walk around as "guest" if no username can be found */
6528 if (!user) context->user = SMB_STRDUP("guest");
6529 else context->user = SMB_STRDUP(user);
6532 if (!context->netbios_name) {
6534 * We try to get our netbios name from the config. If that
6535 * fails we fall back on constructing our netbios name from
6538 if (global_myname()) {
6539 context->netbios_name = SMB_STRDUP(global_myname());
6543 * Hmmm, I want to get hostname as well, but I am too
6544 * lazy for the moment
6547 context->netbios_name = (char *)SMB_MALLOC(17);
6548 if (!context->netbios_name) {
6552 slprintf(context->netbios_name, 16,
6553 "smbc%s%d", context->user, pid);
6557 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
6559 if (!context->workgroup) {
6560 if (lp_workgroup()) {
6561 context->workgroup = SMB_STRDUP(lp_workgroup());
6564 /* TODO: Think about a decent default workgroup */
6565 context->workgroup = SMB_STRDUP("samba");
6569 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
6571 /* shortest timeout is 1 second */
6572 if (context->timeout > 0 && context->timeout < 1000)
6573 context->timeout = 1000;
6576 * FIXME: Should we check the function pointers here?
6579 context->internal->_initialized = True;
6585 /* Return the verion of samba, and thus libsmbclient */
6589 return samba_version_string();