2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "include/libsmb_internal.h"
31 * DOS Attribute values (used internally)
33 typedef struct DOS_ATTR_DESC
36 unsigned long long size;
40 unsigned long long inode;
45 * Internal flags for extended attributes
48 /* internal mode values */
49 #define SMBC_XATTR_MODE_ADD 1
50 #define SMBC_XATTR_MODE_REMOVE 2
51 #define SMBC_XATTR_MODE_REMOVE_ALL 3
52 #define SMBC_XATTR_MODE_SET 4
53 #define SMBC_XATTR_MODE_CHOWN 5
54 #define SMBC_XATTR_MODE_CHGRP 6
56 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
58 /*We should test for this in configure ... */
60 #define ENOTSUP EOPNOTSUPP
64 * Functions exported by libsmb_cache.c that we need here
66 int smbc_default_cache_functions(SMBCCTX *context);
69 * check if an element is part of the list.
70 * FIXME: Does not belong here !
71 * Can anyone put this in a macro in dlinklist.h ?
74 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
75 if (!p || !list) return False;
77 if (p == list) return True;
83 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file);
84 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence);
86 extern BOOL in_client;
89 * Is the logging working / configfile read ?
91 static int smbc_initialized = 0;
94 hex2int( unsigned int _char )
96 if ( _char >= 'A' && _char <='F')
97 return _char - 'A' + 10;
98 if ( _char >= 'a' && _char <='f')
99 return _char - 'a' + 10;
100 if ( _char >= '0' && _char <='9')
108 * Convert strings of %xx to their single character equivalent. Each 'x' must
109 * be a valid hexadecimal digit, or that % sequence is left undecoded.
111 * dest may, but need not be, the same pointer as src.
113 * Returns the number of % sequences which could not be converted due to lack
114 * of two following hexadecimal digits.
117 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
119 int old_length = strlen(src);
125 if ( old_length == 0 ) {
130 while ( i < old_length ) {
131 unsigned char character = src[ i++ ];
133 if (character == '%') {
134 int a = i+1 < old_length ? hex2int( src[i] ) : -1;
135 int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
137 /* Replace valid sequence */
138 if (a != -1 && b != -1) {
140 /* Replace valid %xx sequence with %dd */
141 character = (a * 16) + b;
143 if (character == '\0') {
144 break; /* Stop at %00 */
159 strncpy(dest, temp, max_dest_len);
167 * Convert any characters not specifically allowed in a URL into their %xx
170 * Returns the remaining buffer length.
173 smbc_urlencode(char * dest, char * src, int max_dest_len)
175 char hex[] = "0123456789ABCDEF";
177 for (; *src != '\0' && max_dest_len >= 3; src++) {
189 *dest++ = hex[(*src >> 4) & 0x0f];
190 *dest++ = hex[*src & 0x0f];
205 * Function to parse a path and turn it into components
207 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
208 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
209 * general format ("smb:" only; we do not look for "cifs:").
213 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]][?options]
217 * smb:// Show all workgroups.
219 * The method of locating the list of workgroups varies
220 * depending upon the setting of the context variable
221 * context->options.browse_max_lmb_count. This value
222 * determine the maximum number of local master browsers to
223 * query for the list of workgroups. In order to ensure that
224 * a complete list of workgroups is obtained, all master
225 * browsers must be queried, but if there are many
226 * workgroups, the time spent querying can begin to add up.
227 * For small networks (not many workgroups), it is suggested
228 * that this variable be set to 0, indicating query all local
229 * master browsers. When the network has many workgroups, a
230 * reasonable setting for this variable might be around 3.
232 * smb://name/ if name<1D> or name<1B> exists, list servers in
233 * workgroup, else, if name<20> exists, list all shares
236 * If "options" are provided, this function returns the entire option list as a
237 * string, for later parsing by the caller. Note that currently, no options
241 static const char *smbc_prefix = "smb:";
244 smbc_parse_path(SMBCCTX *context,
246 char *server, int server_len,
247 char *share, int share_len,
248 char *path, int path_len,
249 char *user, int user_len,
250 char *password, int password_len,
251 char *options, int options_len)
259 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
260 if (options != NULL && options_len > 0) {
261 options[0] = (char)0;
265 /* see if it has the right prefix */
266 len = strlen(smbc_prefix);
267 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
268 return -1; /* What about no smb: ? */
273 /* Watch the test below, we are testing to see if we should exit */
275 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
277 DEBUG(1, ("Invalid path (does not begin with smb://"));
282 p += 2; /* Skip the double slash */
284 /* See if any options were specified */
285 if ((q = strrchr(p, '?')) != NULL ) {
286 /* There are options. Null terminate here and point to them */
289 DEBUG(4, ("Found options '%s'", q));
291 /* Copy the options */
292 if (options != NULL && options_len > 0) {
293 safe_strcpy(options, q, options_len - 1);
302 strncpy(server, context->workgroup,
303 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
309 * ok, its for us. Now parse out the server, share etc.
311 * However, we want to parse out [[domain;]user[:password]@] if it
315 /* check that '@' occurs before '/', if '/' exists at all */
316 q = strchr_m(p, '@');
317 r = strchr_m(p, '/');
318 if (q && (!r || q < r)) {
319 pstring username, passwd, domain;
320 const char *u = userinfo;
322 next_token(&p, userinfo, "@", sizeof(fstring));
324 username[0] = passwd[0] = domain[0] = 0;
326 if (strchr_m(u, ';')) {
328 next_token(&u, domain, ";", sizeof(fstring));
332 if (strchr_m(u, ':')) {
334 next_token(&u, username, ":", sizeof(fstring));
341 pstrcpy(username, u);
346 strncpy(user, username, user_len); /* FIXME, domain */
349 strncpy(password, passwd, password_len);
353 if (!next_token(&p, server, "/", sizeof(fstring))) {
359 if (*p == (char)0) goto decoding; /* That's it ... */
361 if (!next_token(&p, share, "/", sizeof(fstring))) {
367 safe_strcpy(path, p, path_len - 1);
369 all_string_sub(path, "/", "\\", 0);
372 (void) smbc_urldecode(path, path, path_len);
373 (void) smbc_urldecode(server, server, server_len);
374 (void) smbc_urldecode(share, share, share_len);
375 (void) smbc_urldecode(user, user, user_len);
376 (void) smbc_urldecode(password, password, password_len);
382 * Verify that the options specified in a URL are valid
384 static int smbc_check_options(char *server, char *share, char *path, char *options)
386 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' path='%s' options='%s'\n", server, share, path, options));
388 /* No options at all is always ok */
389 if (! *options) return 0;
391 /* Currently, we don't support any options. */
396 * Convert an SMB error into a UNIX error ...
398 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
400 int ret = cli_errno(c);
402 if (cli_is_dos_error(c)) {
406 cli_dos_error(c, &eclass, &ecode);
408 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
409 (int)eclass, (int)ecode, (int)ecode, ret));
413 status = cli_nt_error(c);
415 DEBUG(3,("smbc errno %s -> %d\n",
416 nt_errstr(status), ret));
424 * returns 0 if the server is in shape. Returns 1 on error
426 * Also useable outside libsmbclient to enable external cache
427 * to do some checks too.
429 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
431 if ( send_keepalive(server->cli.fd) == False )
434 /* connection is ok */
439 * Remove a server from the cached server list it's unused.
440 * On success, 0 is returned. 1 is returned if the server could not be removed.
442 * Also useable outside libsmbclient
444 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
448 /* are we being fooled ? */
449 if (!context || !context->internal ||
450 !context->internal->_initialized || !srv) return 1;
453 /* Check all open files/directories for a relation with this server */
454 for (file = context->internal->_files; file; file=file->next) {
455 if (file->srv == srv) {
457 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
463 DLIST_REMOVE(context->internal->_servers, srv);
465 cli_shutdown(&srv->cli);
467 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
469 context->callbacks.remove_cached_srv_fn(context, srv);
474 SMBCSRV *find_server(SMBCCTX *context,
486 srv = context->callbacks.get_cached_srv_fn(context, server, share,
487 workgroup, username);
489 if (!auth_called && !srv && (!username[0] || !password[0])) {
490 context->callbacks.auth_fn(server, share,
491 workgroup, sizeof(fstring),
492 username, sizeof(fstring),
493 password, sizeof(fstring));
495 * However, smbc_auth_fn may have picked up info relating to
496 * an existing connection, so try for an existing connection
500 goto check_server_cache;
505 if (context->callbacks.check_server_fn(context, srv)) {
507 * This server is no good anymore
508 * Try to remove it and check for more possible
509 * servers in the cache
511 if (context->callbacks.remove_unused_server_fn(context,
514 * We could not remove the server completely,
515 * remove it from the cache so we will not get
516 * it again. It will be removed when the last
517 * file/dir is closed.
519 context->callbacks.remove_cached_srv_fn(context,
524 * Maybe there are more cached connections to this
527 goto check_server_cache;
537 * Connect to a server, possibly on an existing connection
539 * Here, what we want to do is: If the server and username
540 * match an existing connection, reuse that, otherwise, establish a
543 * If we have to create a new connection, call the auth_fn to get the
544 * info we need, unless the username and password were passed in.
547 SMBCSRV *smbc_server(SMBCCTX *context,
548 const char *server, const char *share,
549 fstring workgroup, fstring username,
554 struct nmb_name called, calling;
555 const char *server_n = server;
558 int tried_reverse = 0;
561 const char *username_used;
566 if (server[0] == 0) {
571 srv = find_server(context, server, share,
572 workgroup, username, password);
575 * If we found a connection and we're only allowed one share per
578 if (srv && *share != '\0' && context->options.one_share_per_server) {
581 * ... then if there's no current connection to the share,
582 * connect to it. find_server(), or rather the function
583 * pointed to by context->callbacks.get_cached_srv_fn which
584 * was called by find_server(), will have issued a tree
585 * disconnect if the requested share is not the same as the
586 * one that was already connected.
588 if (srv->cli.cnum == (uint16) -1) {
589 /* Ensure we have accurate auth info */
590 context->callbacks.auth_fn(server, share,
591 workgroup, sizeof(fstring),
592 username, sizeof(fstring),
593 password, sizeof(fstring));
595 if (! cli_send_tconX(&srv->cli, share, "?????",
596 password, strlen(password)+1)) {
598 errno = smbc_errno(context, &srv->cli);
599 cli_shutdown(&srv->cli);
600 context->callbacks.remove_cached_srv_fn(context, srv);
604 /* Regenerate the dev value since it's based on both server and share */
606 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
611 /* If we have a connection... */
614 /* ... then we're done here. Give 'em what they came for. */
618 make_nmb_name(&calling, context->netbios_name, 0x0);
619 make_nmb_name(&called , server, 0x20);
621 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
623 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
626 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
630 /* have to open a new connection */
631 if (!cli_initialise(&c)) {
636 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
637 c.use_kerberos = True;
639 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
640 c.fallback_after_kerberos = True;
643 c.timeout = context->timeout;
646 * Force use of port 139 for first try if share is $IPC, empty, or
647 * null, so browse lists can work
649 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0) {
650 port_try_first = 139;
653 port_try_first = 445;
657 c.port = port_try_first;
659 if (!cli_connect(&c, server_n, &ip)) {
661 /* First connection attempt failed. Try alternate port. */
662 c.port = port_try_next;
664 if (!cli_connect(&c, server_n, &ip)) {
671 if (!cli_session_request(&c, &calling, &called)) {
673 if (strcmp(called.name, "*SMBSERVER")) {
674 make_nmb_name(&called , "*SMBSERVER", 0x20);
677 else { /* Try one more time, but ensure we don't loop */
679 /* Only try this if server is an IP address ... */
681 if (is_ipaddress(server) && !tried_reverse) {
683 struct in_addr rem_ip;
685 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
686 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
691 tried_reverse++; /* Yuck */
693 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
694 make_nmb_name(&called, remote_name, 0x20);
705 DEBUG(4,(" session request ok\n"));
707 if (!cli_negprot(&c)) {
713 username_used = username;
715 if (!cli_session_setup(&c, username_used,
716 password, strlen(password),
717 password, strlen(password),
720 /* Failed. Try an anonymous login, if allowed by flags. */
723 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
724 !cli_session_setup(&c, username_used,
735 DEBUG(4,(" session setup ok\n"));
737 if (!cli_send_tconX(&c, share, "?????",
738 password, strlen(password)+1)) {
739 errno = smbc_errno(context, &c);
744 DEBUG(4,(" tconx ok\n"));
747 * Ok, we have got a nice connection
748 * Let's find a free server_fd
751 srv = SMB_MALLOC_P(SMBCSRV);
759 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
761 /* now add it to the cache (internal or external) */
762 /* Let the cache function set errno if it wants to */
764 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username_used)) {
765 int saved_errno = errno;
766 DEBUG(3, (" Failed to add server to cache\n"));
774 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
775 server, share, srv));
777 DLIST_ADD(context->internal->_servers, srv);
782 if (!srv) return NULL;
789 * Connect to a server for getting/setting attributes, possibly on an existing
790 * connection. This works similarly to smbc_server().
792 SMBCSRV *smbc_attr_server(SMBCCTX *context,
793 const char *server, const char *share,
795 fstring username, fstring password,
799 struct cli_state *ipc_cli;
801 SMBCSRV *ipc_srv=NULL;
804 * See if we've already created this special connection. Reference our
805 * "special" share name '*IPC$', which is an impossible real share name
806 * due to the leading asterisk.
808 ipc_srv = find_server(context, server, "*IPC$",
809 workgroup, username, password);
812 /* We didn't find a cached connection. Get the password */
813 if (*password == '\0') {
814 /* ... then retrieve it now. */
815 context->callbacks.auth_fn(server, share,
816 workgroup, sizeof(fstring),
817 username, sizeof(fstring),
818 password, sizeof(fstring));
822 nt_status = cli_full_connection(&ipc_cli,
823 global_myname(), server,
824 &ip, 0, "IPC$", "?????",
828 if (! NT_STATUS_IS_OK(nt_status)) {
829 DEBUG(1,("cli_full_connection failed! (%s)\n",
830 nt_errstr(nt_status)));
835 if (!cli_nt_session_open(ipc_cli, PI_LSARPC)) {
836 DEBUG(1, ("cli_nt_session_open fail!\n"));
838 cli_shutdown(ipc_cli);
842 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
843 but NT sends 0x2000000 so we might as well do it too. */
845 nt_status = cli_lsa_open_policy(ipc_cli,
848 GENERIC_EXECUTE_ACCESS,
851 if (!NT_STATUS_IS_OK(nt_status)) {
852 errno = smbc_errno(context, ipc_cli);
853 cli_shutdown(ipc_cli);
857 ipc_srv = SMB_MALLOC_P(SMBCSRV);
860 cli_shutdown(ipc_cli);
864 ZERO_STRUCTP(ipc_srv);
865 ipc_srv->cli = *ipc_cli;
869 /* now add it to the cache (internal or external) */
871 errno = 0; /* let cache function set errno if it likes */
872 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
877 DEBUG(3, (" Failed to add server to cache\n"));
881 cli_shutdown(&ipc_srv->cli);
886 DLIST_ADD(context->internal->_servers, ipc_srv);
893 * Routine to open() a file ...
896 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
898 fstring server, share, user, password, workgroup;
901 SMBCFILE *file = NULL;
904 if (!context || !context->internal ||
905 !context->internal->_initialized) {
907 errno = EINVAL; /* Best I can think of ... */
919 if (smbc_parse_path(context, fname,
920 server, sizeof(server),
921 share, sizeof(share),
924 password, sizeof(password),
930 if (user[0] == (char)0) fstrcpy(user, context->user);
932 fstrcpy(workgroup, context->workgroup);
934 srv = smbc_server(context, server, share, workgroup, user, password);
938 if (errno == EPERM) errno = EACCES;
939 return NULL; /* smbc_server sets errno */
943 /* Hmmm, the test for a directory is suspect here ... FIXME */
945 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
952 file = SMB_MALLOC_P(SMBCFILE);
963 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
965 /* Handle the error ... */
968 errno = smbc_errno(context, &srv->cli);
973 /* Fill in file struct */
976 file->fname = SMB_STRDUP(fname);
981 DLIST_ADD(context->internal->_files, file);
984 * If the file was opened in O_APPEND mode, all write
985 * operations should be appended to the file. To do that,
986 * though, using this protocol, would require a getattrE()
987 * call for each and every write, to determine where the end
988 * of the file is. (There does not appear to be an append flag
989 * in the protocol.) Rather than add all of that overhead of
990 * retrieving the current end-of-file offset prior to each
991 * write operation, we'll assume that most append operations
992 * will continuously write, so we'll just set the offset to
993 * the end of the file now and hope that's adequate.
995 * Note to self: If this proves inadequate, and O_APPEND
996 * should, in some cases, be forced for each write, add a
997 * field in the context options structure, for
998 * "strict_append_mode" which would select between the current
999 * behavior (if FALSE) or issuing a getattrE() prior to each
1000 * write and forcing the write to the end of the file (if
1001 * TRUE). Adding that capability will likely require adding
1002 * an "append" flag into the _SMBCFILE structure to track
1003 * whether a file was opened in O_APPEND mode. -- djl
1005 if (flags & O_APPEND) {
1006 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1007 (void) smbc_close_ctx(context, file);
1017 /* Check if opendir needed ... */
1022 eno = smbc_errno(context, &srv->cli);
1023 file = context->opendir(context, fname);
1024 if (!file) errno = eno;
1029 errno = EINVAL; /* FIXME, correct errno ? */
1035 * Routine to create a file
1038 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1040 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
1043 if (!context || !context->internal ||
1044 !context->internal->_initialized) {
1051 return smbc_open_ctx(context, path, creat_bits, mode);
1055 * Routine to read() a file ...
1058 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
1065 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1066 * appears to pass file->offset (which is type off_t) differently than
1067 * a local variable of type off_t. Using local variable "offset" in
1068 * the call to cli_read() instead of file->offset fixes a problem
1069 * retrieving data at an offset greater than 4GB.
1071 off_t offset = file->offset;
1073 if (!context || !context->internal ||
1074 !context->internal->_initialized) {
1081 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1083 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1090 /* Check that the buffer exists ... */
1099 ret = cli_read(&file->srv->cli, file->cli_fd, buf, offset, count);
1103 errno = smbc_errno(context, &file->srv->cli);
1108 file->offset += ret;
1110 DEBUG(4, (" --> %d\n", ret));
1112 return ret; /* Success, ret bytes of data ... */
1117 * Routine to write() a file ...
1120 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
1123 off_t offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1125 if (!context || !context->internal ||
1126 !context->internal->_initialized) {
1133 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1140 /* Check that the buffer exists ... */
1149 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, offset, count);
1153 errno = smbc_errno(context, &file->srv->cli);
1158 file->offset += ret;
1160 return ret; /* Success, 0 bytes of data ... */
1164 * Routine to close() a file ...
1167 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
1171 if (!context || !context->internal ||
1172 !context->internal->_initialized) {
1179 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1189 return context->closedir(context, file);
1193 if (!cli_close(&file->srv->cli, file->cli_fd)) {
1195 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1197 /* Deallocate slot and remove the server
1198 * from the server cache if unused */
1199 errno = smbc_errno(context, &file->srv->cli);
1201 DLIST_REMOVE(context->internal->_files, file);
1202 SAFE_FREE(file->fname);
1204 context->callbacks.remove_unused_server_fn(context, srv);
1210 DLIST_REMOVE(context->internal->_files, file);
1211 SAFE_FREE(file->fname);
1218 * Get info from an SMB server on a file. Use a qpathinfo call first
1219 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1221 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1222 uint16 *mode, SMB_OFF_T *size,
1223 time_t *c_time, time_t *a_time, time_t *m_time,
1227 if (!context || !context->internal ||
1228 !context->internal->_initialized) {
1235 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1237 if (!srv->no_pathinfo2 &&
1238 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1239 size, mode, ino)) return True;
1241 /* if this is NT then don't bother with the getatr */
1242 if (srv->cli.capabilities & CAP_NT_SMBS) {
1247 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1248 a_time = c_time = m_time;
1249 srv->no_pathinfo2 = True;
1259 * Routine to unlink() a file
1262 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
1264 fstring server, share, user, password, workgroup;
1266 SMBCSRV *srv = NULL;
1268 if (!context || !context->internal ||
1269 !context->internal->_initialized) {
1271 errno = EINVAL; /* Best I can think of ... */
1283 if (smbc_parse_path(context, fname,
1284 server, sizeof(server),
1285 share, sizeof(share),
1288 password, sizeof(password),
1294 if (user[0] == (char)0) fstrcpy(user, context->user);
1296 fstrcpy(workgroup, context->workgroup);
1298 srv = smbc_server(context, server, share, workgroup, user, password);
1302 return -1; /* smbc_server sets errno */
1306 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1308 int job = smbc_stat_printjob(srv, path, NULL, NULL);
1314 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
1322 if (!cli_unlink(&srv->cli, path)) {
1324 errno = smbc_errno(context, &srv->cli);
1326 if (errno == EACCES) { /* Check if the file is a directory */
1331 time_t m_time = 0, a_time = 0, c_time = 0;
1334 if (!smbc_getatr(context, srv, path, &mode, &size,
1335 &c_time, &a_time, &m_time, &ino)) {
1337 /* Hmmm, bad error ... What? */
1339 errno = smbc_errno(context, &srv->cli);
1345 if (IS_DOS_DIR(mode))
1348 errno = saverr; /* Restore this */
1357 return 0; /* Success ... */
1362 * Routine to rename() a file
1365 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
1366 SMBCCTX *ncontext, const char *nname)
1368 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
1369 pstring path1, path2;
1370 SMBCSRV *srv = NULL;
1372 if (!ocontext || !ncontext ||
1373 !ocontext->internal || !ncontext->internal ||
1374 !ocontext->internal->_initialized ||
1375 !ncontext->internal->_initialized) {
1377 errno = EINVAL; /* Best I can think of ... */
1382 if (!oname || !nname) {
1389 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1391 smbc_parse_path(ocontext, oname,
1392 server1, sizeof(server1),
1393 share1, sizeof(share1),
1394 path1, sizeof(path1),
1395 user1, sizeof(user1),
1396 password1, sizeof(password1),
1399 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1401 smbc_parse_path(ncontext, nname,
1402 server2, sizeof(server2),
1403 share2, sizeof(share2),
1404 path2, sizeof(path2),
1405 user2, sizeof(user2),
1406 password2, sizeof(password2),
1409 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1411 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1412 strcmp(user1, user2)) {
1414 /* Can't rename across file systems, or users?? */
1421 fstrcpy(workgroup, ocontext->workgroup);
1422 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1423 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
1430 if (!cli_rename(&srv->cli, path1, path2)) {
1431 int eno = smbc_errno(ocontext, &srv->cli);
1433 if (eno != EEXIST ||
1434 !cli_unlink(&srv->cli, path2) ||
1435 !cli_rename(&srv->cli, path1, path2)) {
1443 return 0; /* Success */
1448 * A routine to lseek() a file
1451 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
1455 if (!context || !context->internal ||
1456 !context->internal->_initialized) {
1463 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1473 return -1; /* Can't lseek a dir ... */
1479 file->offset = offset;
1483 file->offset += offset;
1487 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1490 SMB_BIG_UINT b_size = size;
1491 if (!cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &b_size, NULL, NULL,
1499 file->offset = size + offset;
1508 return file->offset;
1513 * Generate an inode number from file name for those things that need it
1517 ino_t smbc_inode(SMBCCTX *context, const char *name)
1520 if (!context || !context->internal ||
1521 !context->internal->_initialized) {
1528 if (!*name) return 2; /* FIXME, why 2 ??? */
1529 return (ino_t)str_checksum(name);
1534 * Routine to put basic stat info into a stat structure ... Used by stat and
1539 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname,
1540 SMB_OFF_T size, int mode)
1545 if (IS_DOS_DIR(mode)) {
1546 st->st_mode = SMBC_DIR_MODE;
1548 st->st_mode = SMBC_FILE_MODE;
1551 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1552 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1553 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1554 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1557 #ifdef HAVE_STAT_ST_BLKSIZE
1558 st->st_blksize = 512;
1560 #ifdef HAVE_STAT_ST_BLOCKS
1561 st->st_blocks = (size+511)/512;
1563 st->st_uid = getuid();
1564 st->st_gid = getgid();
1566 if (IS_DOS_DIR(mode)) {
1572 if (st->st_ino == 0) {
1573 st->st_ino = smbc_inode(context, fname);
1576 return True; /* FIXME: Is this needed ? */
1581 * Routine to stat a file given a name
1584 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1587 fstring server, share, user, password, workgroup;
1589 time_t m_time = 0, a_time = 0, c_time = 0;
1594 if (!context || !context->internal ||
1595 !context->internal->_initialized) {
1597 errno = EINVAL; /* Best I can think of ... */
1609 DEBUG(4, ("smbc_stat(%s)\n", fname));
1611 if (smbc_parse_path(context, fname,
1612 server, sizeof(server),
1613 share, sizeof(share),
1616 password, sizeof(password),
1622 if (user[0] == (char)0) fstrcpy(user, context->user);
1624 fstrcpy(workgroup, context->workgroup);
1626 srv = smbc_server(context, server, share, workgroup, user, password);
1629 return -1; /* errno set by smbc_server */
1632 if (!smbc_getatr(context, srv, path, &mode, &size,
1633 &c_time, &a_time, &m_time, &ino)) {
1635 errno = smbc_errno(context, &srv->cli);
1642 smbc_setup_stat(context, st, path, size, mode);
1644 st->st_atime = a_time;
1645 st->st_ctime = c_time;
1646 st->st_mtime = m_time;
1647 st->st_dev = srv->dev;
1654 * Routine to stat a file given an fd
1657 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1659 time_t c_time, a_time, m_time;
1664 if (!context || !context->internal ||
1665 !context->internal->_initialized) {
1672 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1681 return context->fstatdir(context, file, st);
1685 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1686 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino)) {
1687 if (!cli_getattrE(&file->srv->cli, file->cli_fd,
1688 &mode, &size, &c_time, &a_time, &m_time)) {
1697 smbc_setup_stat(context, st, file->fname, size, mode);
1699 st->st_atime = a_time;
1700 st->st_ctime = c_time;
1701 st->st_mtime = m_time;
1702 st->st_dev = file->srv->dev;
1709 * Routine to open a directory
1710 * We accept the URL syntax explained in smbc_parse_path(), above.
1713 static void smbc_remove_dir(SMBCFILE *dir)
1715 struct smbc_dir_list *d,*f;
1722 SAFE_FREE(f->dirent);
1727 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1731 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1733 struct smbc_dirent *dirent;
1735 int name_length = (name == NULL ? 0 : strlen(name));
1736 int comment_len = (comment == NULL ? 0 : strlen(comment));
1739 * Allocate space for the dirent, which must be increased by the
1740 * size of the name and the comment and 1 each for the null terminator.
1743 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
1745 dirent = SMB_MALLOC(size);
1749 dir->dir_error = ENOMEM;
1754 ZERO_STRUCTP(dirent);
1756 if (dir->dir_list == NULL) {
1758 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
1759 if (!dir->dir_list) {
1762 dir->dir_error = ENOMEM;
1766 ZERO_STRUCTP(dir->dir_list);
1768 dir->dir_end = dir->dir_next = dir->dir_list;
1772 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
1774 if (!dir->dir_end->next) {
1777 dir->dir_error = ENOMEM;
1781 ZERO_STRUCTP(dir->dir_end->next);
1783 dir->dir_end = dir->dir_end->next;
1786 dir->dir_end->next = NULL;
1787 dir->dir_end->dirent = dirent;
1789 dirent->smbc_type = type;
1790 dirent->namelen = name_length;
1791 dirent->commentlen = comment_len;
1792 dirent->dirlen = size;
1794 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1796 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1797 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1804 list_unique_wg_fn(const char *name, uint32 type, const char *comment, void *state)
1806 SMBCFILE *dir = (SMBCFILE *)state;
1807 struct smbc_dir_list *dir_list;
1808 struct smbc_dirent *dirent;
1812 dirent_type = dir->dir_type;
1814 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1816 /* An error occurred, what do we do? */
1817 /* FIXME: Add some code here */
1820 /* Point to the one just added */
1821 dirent = dir->dir_end->dirent;
1823 /* See if this was a duplicate */
1824 for (dir_list = dir->dir_list;
1825 dir_list != dir->dir_end;
1826 dir_list = dir_list->next) {
1828 strcmp(dir_list->dirent->name, dirent->name) == 0) {
1829 /* Duplicate. End end of list need to be removed. */
1833 if (do_remove && dir_list->next == dir->dir_end) {
1834 /* Found the end of the list. Remove it. */
1835 dir->dir_end = dir_list;
1836 free(dir_list->next);
1837 dir_list->next = NULL;
1844 list_fn(const char *name, uint32 type, const char *comment, void *state)
1846 SMBCFILE *dir = (SMBCFILE *)state;
1849 /* We need to process the type a little ... */
1851 if (dir->dir_type == SMBC_FILE_SHARE) {
1854 case 0: /* Directory tree */
1855 dirent_type = SMBC_FILE_SHARE;
1859 dirent_type = SMBC_PRINTER_SHARE;
1863 dirent_type = SMBC_COMMS_SHARE;
1867 dirent_type = SMBC_IPC_SHARE;
1871 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1875 else dirent_type = dir->dir_type;
1877 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1879 /* An error occurred, what do we do? */
1880 /* FIXME: Add some code here */
1886 dir_list_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
1889 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1890 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1892 /* Handle an error ... */
1894 /* FIXME: Add some code ... */
1900 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1902 fstring server, share, user, password, options;
1907 SMBCSRV *srv = NULL;
1908 SMBCFILE *dir = NULL;
1909 struct in_addr rem_ip;
1911 if (!context || !context->internal ||
1912 !context->internal->_initialized) {
1913 DEBUG(4, ("no valid context\n"));
1914 errno = EINVAL + 8192;
1920 DEBUG(4, ("no valid fname\n"));
1921 errno = EINVAL + 8193;
1925 if (smbc_parse_path(context, fname,
1926 server, sizeof(server),
1927 share, sizeof(share),
1930 password, sizeof(password),
1931 options, sizeof(options))) {
1932 DEBUG(4, ("no valid path\n"));
1933 errno = EINVAL + 8194;
1937 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s' options='%s'\n", fname, server, share, path, options));
1939 /* Ensure the options are valid */
1940 if (smbc_check_options(server, share, path, options)) {
1941 DEBUG(4, ("unacceptable options (%s)\n", options));
1942 errno = EINVAL + 8195;
1946 if (user[0] == (char)0) fstrcpy(user, context->user);
1948 pstrcpy(workgroup, context->workgroup);
1950 dir = SMB_MALLOC_P(SMBCFILE);
1962 dir->fname = SMB_STRDUP(fname);
1966 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1968 if (server[0] == (char)0) {
1973 struct ip_service *ip_list;
1974 struct ip_service server_addr;
1975 struct user_auth_info u_info;
1976 struct cli_state *cli;
1978 if (share[0] != (char)0 || path[0] != (char)0) {
1980 errno = EINVAL + 8196;
1982 SAFE_FREE(dir->fname);
1988 /* Determine how many local master browsers to query */
1989 max_lmb_count = (context->options.browse_max_lmb_count == 0
1991 : context->options.browse_max_lmb_count);
1993 pstrcpy(u_info.username, user);
1994 pstrcpy(u_info.password, password);
1997 * We have server and share and path empty but options
1998 * requesting that we scan all master browsers for their list
1999 * of workgroups/domains. This implies that we must first try
2000 * broadcast queries to find all master browsers, and if that
2001 * doesn't work, then try our other methods which return only
2002 * a single master browser.
2005 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
2006 if (!find_master_ip(workgroup, &server_addr.ip)) {
2012 ip_list = &server_addr;
2016 for (i = 0; i < count && i < max_lmb_count; i++) {
2017 DEBUG(99, ("Found master browser %d of %d: %s\n", i+1, MAX(count, max_lmb_count), inet_ntoa(ip_list[i].ip)));
2019 cli = get_ipc_connect_master_ip(&ip_list[i], workgroup, &u_info);
2020 /* cli == NULL is the master browser refused to talk or
2021 could not be found */
2025 fstrcpy(server, cli->desthost);
2028 DEBUG(4, ("using workgroup %s %s\n", workgroup, server));
2031 * For each returned master browser IP address, get a
2032 * connection to IPC$ on the server if we do not
2033 * already have one, and determine the
2034 * workgroups/domains that it knows about.
2037 srv = smbc_server(context, server,
2038 "IPC$", workgroup, user, password);
2044 dir->dir_type = SMBC_WORKGROUP;
2046 /* Now, list the stuff ... */
2048 if (!cli_NetServerEnum(&srv->cli, workgroup, SV_TYPE_DOMAIN_ENUM, list_unique_wg_fn,
2056 * Server not an empty string ... Check the rest and see what
2059 if (share[0] == (char)0) {
2061 if (path[0] != (char)0) { /* Should not have empty share with path */
2063 errno = EINVAL + 8197;
2065 SAFE_FREE(dir->fname);
2072 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
2073 /* However, we check to see if <server> is an IP address first */
2075 if (!is_ipaddress(server) && /* Not an IP addr so check next */
2076 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
2077 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
2080 dir->dir_type = SMBC_SERVER;
2083 * Get the backup list ...
2087 if (!name_status_find(server, 0, 0, rem_ip, buserver)) {
2089 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
2090 errno = EPERM; /* FIXME, is this correct */
2096 * Get a connection to IPC$ on the server if we do not already have one
2099 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
2102 DEBUG(0, ("got no contact to IPC$\n"));
2104 SAFE_FREE(dir->fname);
2113 /* Now, list the servers ... */
2115 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
2119 SAFE_FREE(dir->fname);
2128 if (resolve_name(server, &rem_ip, 0x20)) {
2130 /* Now, list the shares ... */
2132 dir->dir_type = SMBC_FILE_SHARE;
2134 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
2139 SAFE_FREE(dir->fname);
2148 /* Now, list the servers ... */
2150 if (cli_RNetShareEnum(&srv->cli, list_fn,
2153 errno = cli_errno(&srv->cli);
2155 SAFE_FREE(dir->fname);
2165 errno = ECONNREFUSED; /* Neither the workgroup nor server exists */
2167 SAFE_FREE(dir->fname);
2177 else { /* The server and share are specified ... work from there ... */
2179 /* Well, we connect to the server and list the directory */
2181 dir->dir_type = SMBC_FILE_SHARE;
2183 srv = smbc_server(context, server, share, workgroup, user, password);
2188 SAFE_FREE(dir->fname);
2197 /* Now, list the files ... */
2199 p = path + strlen(path);
2200 pstrcat(path, "\\*");
2202 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
2206 SAFE_FREE(dir->fname);
2209 errno = smbc_errno(context, &srv->cli);
2211 if (errno == EINVAL) {
2213 * See if they asked to opendir something
2214 * other than a directory. If so, the
2215 * converted error value we got would have
2216 * been EINVAL rather than ENOTDIR.
2218 *p = '\0'; /* restore original path */
2220 if (smbc_getatr(context, srv, path,
2224 ! IS_DOS_DIR(mode)) {
2226 /* It is. Correct the error value */
2238 DLIST_ADD(context->internal->_files, dir);
2244 * Routine to close a directory
2247 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
2250 if (!context || !context->internal ||
2251 !context->internal->_initialized) {
2258 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2265 smbc_remove_dir(dir); /* Clean it up */
2267 DLIST_REMOVE(context->internal->_files, dir);
2271 SAFE_FREE(dir->fname);
2272 SAFE_FREE(dir); /* Free the space too */
2279 static void smbc_readdir_internal(SMBCCTX * context,
2280 struct smbc_dirent *dest,
2281 struct smbc_dirent *src,
2282 int max_namebuf_len)
2284 if (context->options.urlencode_readdir_entries) {
2286 /* url-encode the name. get back remaining buffer space */
2288 smbc_urlencode(dest->name, src->name, max_namebuf_len);
2290 /* We now know the name length */
2291 dest->namelen = strlen(dest->name);
2293 /* Save the pointer to the beginning of the comment */
2294 dest->comment = dest->name + dest->namelen + 1;
2296 /* Copy the comment */
2297 strncpy(dest->comment, src->comment, max_namebuf_len);
2299 /* Ensure the comment is null terminated */
2300 if (max_namebuf_len > src->commentlen) {
2301 dest->comment[src->commentlen] = '\0';
2303 dest->comment[max_namebuf_len - 1] = '\0';
2306 /* Save other fields */
2307 dest->smbc_type = src->smbc_type;
2308 dest->commentlen = strlen(dest->comment);
2309 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
2313 /* No encoding. Just copy the entry as is. */
2314 memcpy(dest, src, src->dirlen);
2315 dest->comment = (char *)(&dest->name + src->namelen + 1);
2321 * Routine to get a directory entry
2324 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
2327 struct smbc_dirent *dirp, *dirent;
2329 /* Check that all is ok first ... */
2331 if (!context || !context->internal ||
2332 !context->internal->_initialized) {
2335 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
2340 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2343 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
2348 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2351 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
2356 if (!dir->dir_next) {
2360 dirent = dir->dir_next->dirent;
2368 dirp = (struct smbc_dirent *)context->internal->_dirent;
2369 maxlen = (sizeof(context->internal->_dirent) -
2370 sizeof(struct smbc_dirent));
2372 smbc_readdir_internal(context, dirp, dirent, maxlen);
2374 dir->dir_next = dir->dir_next->next;
2380 * Routine to get directory entries
2383 static int smbc_getdents_ctx(SMBCCTX *context,
2385 struct smbc_dirent *dirp,
2391 char *ndir = (char *)dirp;
2392 struct smbc_dir_list *dirlist;
2394 /* Check that all is ok first ... */
2396 if (!context || !context->internal ||
2397 !context->internal->_initialized) {
2404 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2411 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2419 * Now, retrieve the number of entries that will fit in what was passed
2420 * We have to figure out if the info is in the list, or we need to
2421 * send a request to the server to get the info.
2424 while ((dirlist = dir->dir_next)) {
2425 struct smbc_dirent *dirent;
2427 if (!dirlist->dirent) {
2429 errno = ENOENT; /* Bad error */
2434 /* Do urlencoding of next entry, if so selected */
2435 dirent = (struct smbc_dirent *)context->internal->_dirent;
2436 maxlen = (sizeof(context->internal->_dirent) -
2437 sizeof(struct smbc_dirent));
2438 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
2440 reqd = dirent->dirlen;
2444 if (rem < count) { /* We managed to copy something */
2450 else { /* Nothing copied ... */
2452 errno = EINVAL; /* Not enough space ... */
2459 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
2461 ((struct smbc_dirent *)ndir)->comment =
2462 (char *)(&((struct smbc_dirent *)ndir)->name +
2470 dir->dir_next = dirlist = dirlist -> next;
2481 * Routine to create a directory ...
2484 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
2487 fstring server, share, user, password, workgroup;
2490 if (!context || !context->internal ||
2491 !context->internal->_initialized) {
2505 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
2507 if (smbc_parse_path(context, fname,
2508 server, sizeof(server),
2509 share, sizeof(share),
2512 password, sizeof(password),
2518 if (user[0] == (char)0) fstrcpy(user, context->user);
2520 fstrcpy(workgroup, context->workgroup);
2522 srv = smbc_server(context, server, share, workgroup, user, password);
2526 return -1; /* errno set by smbc_server */
2530 if (!cli_mkdir(&srv->cli, path)) {
2532 errno = smbc_errno(context, &srv->cli);
2542 * Our list function simply checks to see if a directory is not empty
2545 static int smbc_rmdir_dirempty = True;
2547 static void rmdir_list_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
2550 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2551 smbc_rmdir_dirempty = False;
2556 * Routine to remove a directory
2559 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
2562 fstring server, share, user, password, workgroup;
2565 if (!context || !context->internal ||
2566 !context->internal->_initialized) {
2580 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2582 if (smbc_parse_path(context, fname,
2583 server, sizeof(server),
2584 share, sizeof(share),
2587 password, sizeof(password),
2594 if (user[0] == (char)0) fstrcpy(user, context->user);
2596 fstrcpy(workgroup, context->workgroup);
2598 srv = smbc_server(context, server, share, workgroup, user, password);
2602 return -1; /* errno set by smbc_server */
2606 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2608 mode = aDIR | aRONLY;
2611 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2613 if (strcmp(path, "\\") == 0) {
2615 mode = aDIR | aRONLY;
2621 smbc_stat_printjob(srv, path, &size, &m_time);
2622 c_time = a_time = m_time;
2627 if (!cli_rmdir(&srv->cli, path)) {
2629 errno = smbc_errno(context, &srv->cli);
2631 if (errno == EACCES) { /* Check if the dir empty or not */
2633 pstring lpath; /* Local storage to avoid buffer overflows */
2635 smbc_rmdir_dirempty = True; /* Make this so ... */
2637 pstrcpy(lpath, path);
2638 pstrcat(lpath, "\\*");
2640 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2643 /* Fix errno to ignore latest error ... */
2645 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2646 smbc_errno(context, &srv->cli)));
2651 if (smbc_rmdir_dirempty)
2667 * Routine to return the current directory position
2670 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2672 off_t ret_val; /* Squash warnings about cast */
2674 if (!context || !context->internal ||
2675 !context->internal->_initialized) {
2682 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2689 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2697 * We return the pointer here as the offset
2699 ret_val = (int)dir->dir_next;
2705 * A routine to run down the list and see if the entry is OK
2708 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2709 struct smbc_dirent *dirent)
2712 /* Run down the list looking for what we want */
2716 struct smbc_dir_list *tmp = list;
2720 if (tmp->dirent == dirent)
2729 return NULL; /* Not found, or an error */
2735 * Routine to seek on a directory
2738 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2740 long int l_offset = offset; /* Handle problems of size */
2741 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
2742 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
2744 if (!context || !context->internal ||
2745 !context->internal->_initialized) {
2752 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2759 /* Now, check what we were passed and see if it is OK ... */
2761 if (dirent == NULL) { /* Seek to the begining of the list */
2763 dir->dir_next = dir->dir_list;
2768 /* Now, run down the list and make sure that the entry is OK */
2769 /* This may need to be changed if we change the format of the list */
2771 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2773 errno = EINVAL; /* Bad entry */
2778 dir->dir_next = list_ent;
2785 * Routine to fstat a dir
2788 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2791 if (!context || !context->internal ||
2792 !context->internal->_initialized) {
2799 /* No code yet ... */
2805 int smbc_chmod_ctx(SMBCCTX *context, const char *fname, mode_t newmode)
2808 fstring server, share, user, password, workgroup;
2812 if (!context || !context->internal ||
2813 !context->internal->_initialized) {
2815 errno = EINVAL; /* Best I can think of ... */
2827 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
2829 if (smbc_parse_path(context, fname,
2830 server, sizeof(server),
2831 share, sizeof(share),
2834 password, sizeof(password),
2840 if (user[0] == (char)0) fstrcpy(user, context->user);
2842 fstrcpy(workgroup, context->workgroup);
2844 srv = smbc_server(context, server, share, workgroup, user, password);
2847 return -1; /* errno set by smbc_server */
2852 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
2853 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
2854 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
2855 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
2857 if (!cli_setatr(&srv->cli, path, mode, 0)) {
2858 errno = smbc_errno(context, &srv->cli);
2865 int smbc_utimes_ctx(SMBCCTX *context, const char *fname, struct timeval *tbuf)
2868 fstring server, share, user, password, workgroup;
2871 time_t t = (tbuf == NULL ? time(NULL) : tbuf->tv_sec);
2873 if (!context || !context->internal ||
2874 !context->internal->_initialized) {
2876 errno = EINVAL; /* Best I can think of ... */
2888 DEBUG(4, ("smbc_utimes(%s, [%s])\n", fname, ctime(&t)));
2890 if (smbc_parse_path(context, fname,
2891 server, sizeof(server),
2892 share, sizeof(share),
2895 password, sizeof(password),
2901 if (user[0] == (char)0) fstrcpy(user, context->user);
2903 fstrcpy(workgroup, context->workgroup);
2905 srv = smbc_server(context, server, share, workgroup, user, password);
2908 return -1; /* errno set by smbc_server */
2911 if (!smbc_getatr(context, srv, path,
2918 if (!cli_setatr(&srv->cli, path, mode, t)) {
2919 /* some servers always refuse directory changes */
2920 if (!(mode & aDIR)) {
2921 errno = smbc_errno(context, &srv->cli);
2930 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
2931 However NT4 gives a "The information may have been modified by a
2932 computer running Windows NT 5.0" if denied ACEs do not appear before
2935 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
2937 if (sec_ace_equal(ace1, ace2))
2940 if (ace1->type != ace2->type)
2941 return ace2->type - ace1->type;
2943 if (sid_compare(&ace1->trustee, &ace2->trustee))
2944 return sid_compare(&ace1->trustee, &ace2->trustee);
2946 if (ace1->flags != ace2->flags)
2947 return ace1->flags - ace2->flags;
2949 if (ace1->info.mask != ace2->info.mask)
2950 return ace1->info.mask - ace2->info.mask;
2952 if (ace1->size != ace2->size)
2953 return ace1->size - ace2->size;
2955 return memcmp(ace1, ace2, sizeof(SEC_ACE));
2959 static void sort_acl(SEC_ACL *the_acl)
2962 if (!the_acl) return;
2964 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
2966 for (i=1;i<the_acl->num_aces;) {
2967 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
2969 for (j=i; j<the_acl->num_aces-1; j++) {
2970 the_acl->ace[j] = the_acl->ace[j+1];
2972 the_acl->num_aces--;
2979 /* convert a SID to a string, either numeric or username/group */
2980 static void convert_sid_to_string(struct cli_state *ipc_cli,
2986 char **domains = NULL;
2987 char **names = NULL;
2988 uint32 *types = NULL;
2990 sid_to_string(str, sid);
2992 if (numeric) return; /* no lookup desired */
2994 /* Ask LSA to convert the sid to a name */
2996 if (!NT_STATUS_IS_OK(cli_lsa_lookup_sids(ipc_cli, ipc_cli->mem_ctx,
2997 pol, 1, sid, &domains,
2999 !domains || !domains[0] || !names || !names[0]) {
3005 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
3006 domains[0], lp_winbind_separator(),
3010 /* convert a string to a SID, either numeric or username/group */
3011 static BOOL convert_string_to_sid(struct cli_state *ipc_cli,
3017 uint32 *types = NULL;
3018 DOM_SID *sids = NULL;
3022 if (strncmp(str, "S-", 2) == 0) {
3023 return string_to_sid(sid, str);
3030 if (!NT_STATUS_IS_OK(cli_lsa_lookup_names(ipc_cli, ipc_cli->mem_ctx,
3031 pol, 1, &str, &sids,
3037 sid_copy(sid, &sids[0]);
3044 /* parse an ACE in the same format as print_ace() */
3045 static BOOL parse_ace(struct cli_state *ipc_cli,
3054 unsigned atype, aflags, amask;
3057 const struct perm_value *v;
3063 /* These values discovered by inspection */
3064 static const struct perm_value special_values[] = {
3065 { "R", 0x00120089 },
3066 { "W", 0x00120116 },
3067 { "X", 0x001200a0 },
3068 { "D", 0x00010000 },
3069 { "P", 0x00040000 },
3070 { "O", 0x00080000 },
3074 static const struct perm_value standard_values[] = {
3075 { "READ", 0x001200a9 },
3076 { "CHANGE", 0x001301bf },
3077 { "FULL", 0x001f01ff },
3083 p = strchr_m(str,':');
3084 if (!p) return False;
3087 /* Try to parse numeric form */
3089 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3090 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3094 /* Try to parse text form */
3096 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3101 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3105 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3106 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3107 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3108 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3113 /* Only numeric form accepted for flags at present */
3115 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3116 sscanf(tok, "%i", &aflags))) {
3120 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3124 if (strncmp(tok, "0x", 2) == 0) {
3125 if (sscanf(tok, "%i", &amask) != 1) {
3131 for (v = standard_values; v->perm; v++) {
3132 if (strcmp(tok, v->perm) == 0) {
3143 for (v = special_values; v->perm; v++) {
3144 if (v->perm[0] == *p) {
3150 if (!found) return False;
3160 init_sec_ace(ace, &sid, atype, mask, aflags);
3164 /* add an ACE to a list of ACEs in a SEC_ACL */
3165 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace, TALLOC_CTX *ctx)
3170 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
3174 aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces);
3175 memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
3176 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
3177 new = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
3184 /* parse a ascii version of a security descriptor */
3185 static SEC_DESC *sec_desc_parse(TALLOC_CTX *ctx,
3186 struct cli_state *ipc_cli,
3191 const char *p = str;
3195 DOM_SID *grp_sid=NULL, *owner_sid=NULL;
3199 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
3201 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
3202 revision = strtol(tok+9, NULL, 16);
3206 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
3207 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3209 !convert_string_to_sid(ipc_cli, pol,
3211 owner_sid, tok+6)) {
3212 DEBUG(5, ("Failed to parse owner sid\n"));
3218 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
3219 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3221 !convert_string_to_sid(ipc_cli, pol,
3223 owner_sid, tok+7)) {
3224 DEBUG(5, ("Failed to parse owner sid\n"));
3230 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
3231 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3233 !convert_string_to_sid(ipc_cli, pol,
3236 DEBUG(5, ("Failed to parse group sid\n"));
3242 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
3243 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3245 !convert_string_to_sid(ipc_cli, pol,
3248 DEBUG(5, ("Failed to parse group sid\n"));
3254 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
3256 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
3257 DEBUG(5, ("Failed to parse ACL %s\n", tok));
3260 if(!add_ace(&dacl, &ace, ctx)) {
3261 DEBUG(5, ("Failed to add ACL %s\n", tok));
3267 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
3269 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
3270 DEBUG(5, ("Failed to parse ACL %s\n", tok));
3273 if(!add_ace(&dacl, &ace, ctx)) {
3274 DEBUG(5, ("Failed to add ACL %s\n", tok));
3280 DEBUG(5, ("Failed to parse security descriptor\n"));
3284 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
3285 owner_sid, grp_sid, NULL, dacl, &sd_size);
3288 SAFE_FREE(owner_sid);
3294 /* Obtain the current dos attributes */
3295 static DOS_ATTR_DESC *dos_attr_query(SMBCCTX *context,
3297 const char *filename,
3300 time_t m_time = 0, a_time = 0, c_time = 0;
3303 SMB_INO_T inode = 0;
3306 ret = talloc(ctx, sizeof(DOS_ATTR_DESC));
3312 /* Obtain the DOS attributes */
3313 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
3315 &c_time, &a_time, &m_time, &inode)) {
3317 errno = smbc_errno(context, &srv->cli);
3318 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
3325 ret->a_time = a_time;
3326 ret->c_time = c_time;
3327 ret->m_time = m_time;
3334 /* parse a ascii version of a security descriptor */
3335 static void dos_attr_parse(SMBCCTX *context,
3340 const char *p = str;
3343 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
3345 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
3346 dad->mode = strtol(tok+5, NULL, 16);
3350 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
3351 dad->size = strtoll(tok+5, NULL, 10);
3355 if (StrnCaseCmp(tok, "A_TIME:", 7) == 0) {
3356 dad->a_time = strtoll(tok+7, NULL, 10);
3360 if (StrnCaseCmp(tok, "C_TIME:", 7) == 0) {
3361 dad->c_time = strtoll(tok+7, NULL, 10);
3365 if (StrnCaseCmp(tok, "M_TIME:", 7) == 0) {
3366 dad->m_time = strtoll(tok+7, NULL, 10);
3370 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
3371 dad->inode = strtoll(tok+6, NULL, 10);
3378 /*****************************************************
3379 retrieve the acls for a file
3380 *******************************************************/
3381 static int cacl_get(SMBCCTX *context, TALLOC_CTX *ctx, SMBCSRV *srv,
3382 struct cli_state *ipc_cli, POLICY_HND *pol,
3383 char *filename, char *attr_name, char *buf, int bufsize)
3394 BOOL exclude_nt_revision = False;
3395 BOOL exclude_nt_owner = False;
3396 BOOL exclude_nt_group = False;
3397 BOOL exclude_nt_acl = False;
3398 BOOL exclude_dos_mode = False;
3399 BOOL exclude_dos_size = False;
3400 BOOL exclude_dos_ctime = False;
3401 BOOL exclude_dos_atime = False;
3402 BOOL exclude_dos_mtime = False;
3403 BOOL exclude_dos_inode = False;
3404 BOOL numeric = True;
3405 BOOL determine_size = (bufsize == 0);
3409 fstring name_sandbox;
3413 time_t m_time = 0, a_time = 0, c_time = 0;
3417 struct cli_state *cli = &srv->cli;
3419 /* Copy name so we can strip off exclusions (if any are specified) */
3420 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
3422 /* Ensure name is null terminated */
3423 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
3425 /* Play in the sandbox */
3426 name = name_sandbox;
3428 /* If there are any exclusions, point to them and mask them from name */
3429 if ((pExclude = strchr(name, '!')) != NULL)
3434 all = (StrnCaseCmp(name, "system.*", 8) == 0);
3435 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
3436 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
3437 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
3438 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
3439 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
3440 numeric = (* (name + strlen(name) - 1) != '+');
3442 /* Look for exclusions from "all" requests */
3443 if (all || all_nt || all_dos) {
3445 /* Exclusions are delimited by '!' */
3446 for (; pExclude != NULL; pExclude = (p == NULL ? NULL : p + 1)) {
3448 /* Find end of this exclusion name */
3449 if ((p = strchr(pExclude, '!')) != NULL)
3454 /* Which exclusion name is this? */
3455 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
3456 exclude_nt_revision = True;
3458 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
3459 exclude_nt_owner = True;
3461 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
3462 exclude_nt_group = True;
3464 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
3465 exclude_nt_acl = True;
3467 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
3468 exclude_dos_mode = True;
3470 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
3471 exclude_dos_size = True;
3473 else if (StrCaseCmp(pExclude, "dos_attr.c_time") == 0) {
3474 exclude_dos_ctime = True;
3476 else if (StrCaseCmp(pExclude, "dos_attr.a_time") == 0) {
3477 exclude_dos_atime = True;
3479 else if (StrCaseCmp(pExclude, "dos_attr.m_time") == 0) {
3480 exclude_dos_mtime = True;
3482 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
3483 exclude_dos_inode = True;
3486 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
3497 * If we are (possibly) talking to an NT or new system and some NT
3498 * attributes have been requested...
3500 if (ipc_cli && (all || some_nt || all_nt_acls)) {
3501 /* Point to the portion after "system.nt_sec_desc." */
3502 name += 19; /* if (all) this will be invalid but unused */
3504 /* ... then obtain any NT attributes which were requested */
3505 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
3508 DEBUG(5, ("cacl_get failed to open %s: %s\n",
3509 filename, cli_errstr(cli)));
3514 sd = cli_query_secdesc(cli, fnum, ctx);
3518 ("cacl_get Failed to query old descriptor\n"));
3523 cli_close(cli, fnum);
3525 if (! exclude_nt_revision) {
3526 if (all || all_nt) {
3527 if (determine_size) {
3528 p = talloc_asprintf(ctx,
3537 n = snprintf(buf, bufsize,
3538 "REVISION:%d", sd->revision);
3540 } else if (StrCaseCmp(name, "revision") == 0) {
3541 if (determine_size) {
3542 p = talloc_asprintf(ctx, "%d",
3550 n = snprintf(buf, bufsize, "%d",
3555 if (!determine_size && n > bufsize) {
3564 if (! exclude_nt_owner) {
3565 /* Get owner and group sid */
3566 if (sd->owner_sid) {
3567 convert_sid_to_string(ipc_cli, pol,
3572 fstrcpy(sidstr, "");
3575 if (all || all_nt) {
3576 if (determine_size) {
3577 p = talloc_asprintf(ctx, ",OWNER:%s",
3585 n = snprintf(buf, bufsize,
3586 ",OWNER:%s", sidstr);
3588 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
3589 if (determine_size) {
3590 p = talloc_asprintf(ctx, "%s", sidstr);
3597 n = snprintf(buf, bufsize, "%s",
3602 if (!determine_size && n > bufsize) {
3611 if (! exclude_nt_group) {
3613 convert_sid_to_string(ipc_cli, pol,
3617 fstrcpy(sidstr, "");
3620 if (all || all_nt) {
3621 if (determine_size) {
3622 p = talloc_asprintf(ctx, ",GROUP:%s",
3630 n = snprintf(buf, bufsize,
3631 ",GROUP:%s", sidstr);
3633 } else if (StrnCaseCmp(name, "group", 5) == 0) {
3634 if (determine_size) {
3635 p = talloc_asprintf(ctx, "%s", sidstr);
3642 n = snprintf(buf, bufsize, "%s", sidstr);
3646 if (!determine_size && n > bufsize) {
3655 if (! exclude_nt_acl) {
3656 /* Add aces to value buffer */
3657 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
3659 SEC_ACE *ace = &sd->dacl->ace[i];
3660 convert_sid_to_string(ipc_cli, pol,
3664 if (all || all_nt) {
3665 if (determine_size) {
3666 p = talloc_asprintf(
3682 ",ACL:%s:%d/%d/0x%08x",
3688 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
3689 StrCaseCmp(name + 3, sidstr) == 0) ||
3690 (StrnCaseCmp(name, "acl+", 4) == 0 &&
3691 StrCaseCmp(name + 4, sidstr) == 0)) {
3692 if (determine_size) {
3693 p = talloc_asprintf(
3705 n = snprintf(buf, bufsize,
3711 } else if (all_nt_acls) {
3712 if (determine_size) {
3713 p = talloc_asprintf(
3715 "%s%s:%d/%d/0x%08x",
3727 n = snprintf(buf, bufsize,
3728 "%s%s:%d/%d/0x%08x",
3746 /* Restore name pointer to its original value */
3750 if (all || some_dos) {
3751 /* Point to the portion after "system.dos_attr." */
3752 name += 16; /* if (all) this will be invalid but unused */
3754 /* Obtain the DOS attributes */
3755 if (!smbc_getatr(context, srv, filename, &mode, &size,
3756 &c_time, &a_time, &m_time, &ino)) {
3758 errno = smbc_errno(context, &srv->cli);
3763 if (! exclude_dos_mode) {
3764 if (all || all_dos) {
3765 if (determine_size) {
3766 p = talloc_asprintf(ctx,
3779 n = snprintf(buf, bufsize,
3787 } else if (StrCaseCmp(name, "mode") == 0) {
3788 if (determine_size) {
3789 p = talloc_asprintf(ctx, "0x%x", mode);
3796 n = snprintf(buf, bufsize, "0x%x", mode);
3800 if (!determine_size && n > bufsize) {
3809 if (! exclude_dos_size) {
3810 if (all || all_dos) {
3811 if (determine_size) {
3812 p = talloc_asprintf(
3815 (unsigned long long) size);
3822 n = snprintf(buf, bufsize,
3824 (unsigned long long) size);
3826 } else if (StrCaseCmp(name, "size") == 0) {
3827 if (determine_size) {
3828 p = talloc_asprintf(
3831 (unsigned long long) size);
3838 n = snprintf(buf, bufsize,
3840 (unsigned long long) size);
3844 if (!determine_size && n > bufsize) {
3853 if (! exclude_dos_ctime) {
3854 if (all || all_dos) {
3855 if (determine_size) {
3856 p = talloc_asprintf(ctx,
3865 n = snprintf(buf, bufsize,
3866 ",C_TIME:%lu", c_time);
3868 } else if (StrCaseCmp(name, "c_time") == 0) {
3869 if (determine_size) {
3870 p = talloc_asprintf(ctx, "%lu", c_time);
3877 n = snprintf(buf, bufsize, "%lu", c_time);
3881 if (!determine_size && n > bufsize) {
3890 if (! exclude_dos_atime) {
3891 if (all || all_dos) {
3892 if (determine_size) {
3893 p = talloc_asprintf(ctx,
3902 n = snprintf(buf, bufsize,
3903 ",A_TIME:%lu", a_time);
3905 } else if (StrCaseCmp(name, "a_time") == 0) {
3906 if (determine_size) {
3907 p = talloc_asprintf(ctx, "%lu", a_time);
3914 n = snprintf(buf, bufsize, "%lu", a_time);
3918 if (!determine_size && n > bufsize) {
3927 if (! exclude_dos_mtime) {
3928 if (all || all_dos) {
3929 if (determine_size) {
3930 p = talloc_asprintf(ctx,
3939 n = snprintf(buf, bufsize,
3940 ",M_TIME:%lu", m_time);
3942 } else if (StrCaseCmp(name, "m_time") == 0) {
3943 if (determine_size) {
3944 p = talloc_asprintf(ctx, "%lu", m_time);
3951 n = snprintf(buf, bufsize, "%lu", m_time);
3955 if (!determine_size && n > bufsize) {
3964 if (! exclude_dos_inode) {
3965 if (all || all_dos) {
3966 if (determine_size) {
3967 p = talloc_asprintf(
3970 (unsigned long long) ino);
3977 n = snprintf(buf, bufsize,
3979 (unsigned long long) ino);
3981 } else if (StrCaseCmp(name, "inode") == 0) {
3982 if (determine_size) {
3983 p = talloc_asprintf(
3986 (unsigned long long) ino);
3993 n = snprintf(buf, bufsize,
3995 (unsigned long long) ino);
3999 if (!determine_size && n > bufsize) {
4008 /* Restore name pointer to its original value */
4021 /*****************************************************
4022 set the ACLs on a file given an ascii description
4023 *******************************************************/
4024 static int cacl_set(TALLOC_CTX *ctx, struct cli_state *cli,
4025 struct cli_state *ipc_cli, POLICY_HND *pol,
4026 const char *filename, const char *the_acl,
4027 int mode, int flags)
4031 SEC_DESC *sd = NULL, *old;
4032 SEC_ACL *dacl = NULL;
4033 DOM_SID *owner_sid = NULL;
4034 DOM_SID *grp_sid = NULL;
4039 BOOL numeric = True;
4041 /* the_acl will be null for REMOVE_ALL operations */
4043 numeric = ((p = strchr(the_acl, ':')) != NULL &&
4047 /* if this is to set the entire ACL... */
4048 if (*the_acl == '*') {
4049 /* ... then increment past the first colon */
4053 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
4054 CONST_DISCARD(char *, the_acl));
4062 /* The desired access below is the only one I could find that works
4063 with NT4, W2KP and Samba */
4065 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4068 DEBUG(5, ("cacl_set failed to open %s: %s\n",
4069 filename, cli_errstr(cli)));
4074 old = cli_query_secdesc(cli, fnum, ctx);
4077 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
4082 cli_close(cli, fnum);
4085 case SMBC_XATTR_MODE_REMOVE_ALL:
4086 old->dacl->num_aces = 0;
4087 SAFE_FREE(old->dacl->ace);
4088 SAFE_FREE(old->dacl);
4093 case SMBC_XATTR_MODE_REMOVE:
4094 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
4097 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
4098 if (sec_ace_equal(&sd->dacl->ace[i],
4099 &old->dacl->ace[j])) {
4101 for (k=j; k<old->dacl->num_aces-1;k++) {
4102 old->dacl->ace[k] = old->dacl->ace[k+1];
4104 old->dacl->num_aces--;
4105 if (old->dacl->num_aces == 0) {
4106 SAFE_FREE(old->dacl->ace);
4107 SAFE_FREE(old->dacl);
4124 case SMBC_XATTR_MODE_ADD:
4125 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
4128 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
4129 if (sid_equal(&sd->dacl->ace[i].trustee,
4130 &old->dacl->ace[j].trustee)) {
4131 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
4136 old->dacl->ace[j] = sd->dacl->ace[i];
4142 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
4148 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
4149 add_ace(&old->dacl, &sd->dacl->ace[i], ctx);
4155 case SMBC_XATTR_MODE_SET:
4157 owner_sid = old->owner_sid;
4158 grp_sid = old->grp_sid;
4162 case SMBC_XATTR_MODE_CHOWN:
4163 owner_sid = sd->owner_sid;
4166 case SMBC_XATTR_MODE_CHGRP:
4167 grp_sid = sd->grp_sid;
4171 /* Denied ACE entries must come before allowed ones */
4172 sort_acl(old->dacl);
4174 /* Create new security descriptor and set it */
4175 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
4176 owner_sid, grp_sid, NULL, dacl, &sd_size);
4178 fnum = cli_nt_create(cli, filename,
4179 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
4182 DEBUG(5, ("cacl_set failed to open %s: %s\n",
4183 filename, cli_errstr(cli)));
4188 if (!cli_set_secdesc(cli, fnum, sd)) {
4189 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
4196 cli_close(cli, fnum);
4206 int smbc_setxattr_ctx(SMBCCTX *context,
4217 fstring server, share, user, password, workgroup;
4223 if (!context || !context->internal ||
4224 !context->internal->_initialized) {
4226 errno = EINVAL; /* Best I can think of ... */
4238 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n", fname, name, (int) size, (const char*)value));
4240 if (smbc_parse_path(context, fname,
4241 server, sizeof(server),
4242 share, sizeof(share),
4245 password, sizeof(password),
4251 if (user[0] == (char)0) fstrcpy(user, context->user);
4253 fstrcpy(workgroup, context->workgroup);
4255 srv = smbc_server(context, server, share, workgroup, user, password);
4257 return -1; /* errno set by smbc_server */
4260 if (! srv->no_nt_session) {
4261 ipc_srv = smbc_attr_server(context, server, share,
4262 workgroup, user, password,
4264 srv->no_nt_session = True;
4269 ctx = talloc_init("smbc_setxattr");
4276 * Are they asking to set the entire set of known attributes?
4278 if (StrCaseCmp(name, "system.*") == 0 ||
4279 StrCaseCmp(name, "system.*+") == 0) {
4282 talloc_asprintf(ctx, "%s:%s", name+7, (const char *) value);
4290 ret = cacl_set(ctx, &srv->cli,
4291 &ipc_srv->cli, &pol, path,
4294 ? SMBC_XATTR_MODE_SET
4295 : SMBC_XATTR_MODE_ADD),
4301 /* get a DOS Attribute Descriptor with current attributes */
4302 dad = dos_attr_query(context, ctx, path, srv);
4304 /* Overwrite old with new, using what was provided */
4305 dos_attr_parse(context, dad, srv, namevalue);
4307 /* Set the new DOS attributes */
4308 #if 0 /* not yet implemented */
4309 if (! cli_setpathinfo(&srv->cli, path,
4314 if (!cli_setatr(&srv->cli, path,
4315 dad->mode, dad->m_time)) {
4316 errno = smbc_errno(context, &srv->cli);
4320 if (!cli_setatr(&srv->cli, path,
4321 dad->mode, dad->m_time)) {
4322 errno = smbc_errno(context, &srv->cli);
4327 /* we only fail if both NT and DOS sets failed */
4328 if (ret < 0 && ! dad) {
4329 ret = -1; /* in case dad was null */
4335 talloc_destroy(ctx);
4340 * Are they asking to set an access control element or to set
4341 * the entire access control list?
4343 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4344 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
4345 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4346 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4347 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
4351 talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4354 ret = -1; /* errno set by smbc_server() */
4356 else if (! namevalue) {
4360 ret = cacl_set(ctx, &srv->cli,
4361 &ipc_srv->cli, &pol, path,
4364 ? SMBC_XATTR_MODE_SET
4365 : SMBC_XATTR_MODE_ADD),
4368 talloc_destroy(ctx);
4373 * Are they asking to set the owner?
4375 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4376 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
4380 talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4384 ret = -1; /* errno set by smbc_server() */
4386 else if (! namevalue) {
4390 ret = cacl_set(ctx, &srv->cli,
4391 &ipc_srv->cli, &pol, path,
4392 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
4394 talloc_destroy(ctx);
4399 * Are they asking to set the group?
4401 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4402 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
4406 talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4409 /* errno set by smbc_server() */
4412 else if (! namevalue) {
4416 ret = cacl_set(ctx, &srv->cli,
4417 &ipc_srv->cli, &pol, path,
4418 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
4420 talloc_destroy(ctx);
4425 * Are they asking to set a DOS attribute?
4427 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
4428 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
4429 StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
4430 StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
4431 StrCaseCmp(name, "system.dos_attr.m_time") == 0) {
4433 /* get a DOS Attribute Descriptor with current attributes */
4434 dad = dos_attr_query(context, ctx, path, srv);
4437 talloc_asprintf(ctx, "%s:%s", name+16, (const char *) value);
4442 /* Overwrite old with provided new params */
4443 dos_attr_parse(context, dad, srv, namevalue);
4445 /* Set the new DOS attributes */
4446 #if 0 /* not yet implemented */
4447 ret2 = cli_setpathinfo(&srv->cli, path,
4453 ret2 = cli_setatr(&srv->cli, path,
4457 errno = smbc_errno(context,
4462 ret2 = cli_setatr(&srv->cli, path,
4463 dad->mode, dad->m_time);
4465 errno = smbc_errno(context, &srv->cli);
4469 /* ret2 has True (success) / False (failure) */
4480 talloc_destroy(ctx);
4484 /* Unsupported attribute name */
4485 talloc_destroy(ctx);
4490 int smbc_getxattr_ctx(SMBCCTX *context,
4499 fstring server, share, user, password, workgroup;
4505 if (!context || !context->internal ||
4506 !context->internal->_initialized) {
4508 errno = EINVAL; /* Best I can think of ... */
4520 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
4522 if (smbc_parse_path(context, fname,
4523 server, sizeof(server),
4524 share, sizeof(share),
4527 password, sizeof(password),
4533 if (user[0] == (char)0) fstrcpy(user, context->user);
4535 fstrcpy(workgroup, context->workgroup);
4537 srv = smbc_server(context, server, share, workgroup, user, password);
4539 return -1; /* errno set by smbc_server */
4542 if (! srv->no_nt_session) {
4543 ipc_srv = smbc_attr_server(context, server, share,
4544 workgroup, user, password,
4547 srv->no_nt_session = True;
4553 ctx = talloc_init("smbc:getxattr");
4559 /* Are they requesting a supported attribute? */
4560 if (StrCaseCmp(name, "system.*") == 0 ||
4561 StrnCaseCmp(name, "system.*!", 9) == 0 ||
4562 StrCaseCmp(name, "system.*+") == 0 ||
4563 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
4564 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4565 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
4566 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
4567 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
4568 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4569 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4570 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
4571 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4572 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
4573 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4574 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
4575 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
4576 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
4577 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
4578 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
4579 StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
4580 StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
4581 StrCaseCmp(name, "system.dos_attr.m_time") == 0 ||
4582 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
4585 ret = cacl_get(context, ctx, srv,
4586 ipc_srv == NULL ? NULL : &ipc_srv->cli,
4588 CONST_DISCARD(char *, name),
4589 CONST_DISCARD(char *, value), size);
4590 if (ret < 0 && errno == 0) {
4591 errno = smbc_errno(context, &srv->cli);
4593 talloc_destroy(ctx);
4597 /* Unsupported attribute name */
4598 talloc_destroy(ctx);
4604 int smbc_removexattr_ctx(SMBCCTX *context,
4611 fstring server, share, user, password, workgroup;
4616 if (!context || !context->internal ||
4617 !context->internal->_initialized) {
4619 errno = EINVAL; /* Best I can think of ... */
4631 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
4633 if (smbc_parse_path(context, fname,
4634 server, sizeof(server),
4635 share, sizeof(share),
4638 password, sizeof(password),
4644 if (user[0] == (char)0) fstrcpy(user, context->user);
4646 fstrcpy(workgroup, context->workgroup);
4648 srv = smbc_server(context, server, share, workgroup, user, password);
4650 return -1; /* errno set by smbc_server */
4653 if (! srv->no_nt_session) {
4654 ipc_srv = smbc_attr_server(context, server, share,
4655 workgroup, user, password,
4657 srv->no_nt_session = True;
4663 return -1; /* errno set by smbc_attr_server */
4666 ctx = talloc_init("smbc_removexattr");
4672 /* Are they asking to set the entire ACL? */
4673 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4674 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
4677 ret = cacl_set(ctx, &srv->cli,
4678 &ipc_srv->cli, &pol, path,
4679 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
4680 talloc_destroy(ctx);
4685 * Are they asking to remove one or more spceific security descriptor
4688 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4689 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4690 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
4691 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4692 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
4693 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4694 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
4697 ret = cacl_set(ctx, &srv->cli,
4698 &ipc_srv->cli, &pol, path,
4699 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
4700 talloc_destroy(ctx);
4704 /* Unsupported attribute name */
4705 talloc_destroy(ctx);
4710 int smbc_listxattr_ctx(SMBCCTX *context,
4716 * This isn't quite what listxattr() is supposed to do. This returns
4717 * the complete set of attribute names, always, rather than only those
4718 * attribute names which actually exist for a file. Hmmm...
4720 const char supported[] =
4723 "system.nt_sec_desc.revision\0"
4724 "system.nt_sec_desc.owner\0"
4725 "system.nt_sec_desc.owner+\0"
4726 "system.nt_sec_desc.group\0"
4727 "system.nt_sec_desc.group+\0"
4728 "system.nt_sec_desc.acl.*\0"
4729 "system.nt_sec_desc.acl\0"
4730 "system.nt_sec_desc.acl+\0"
4731 "system.nt_sec_desc.*\0"
4732 "system.nt_sec_desc.*+\0"
4733 "system.dos_attr.*\0"
4734 "system.dos_attr.mode\0"
4735 "system.dos_attr.c_time\0"
4736 "system.dos_attr.a_time\0"
4737 "system.dos_attr.m_time\0"
4741 return sizeof(supported);
4744 if (sizeof(supported) > size) {
4749 /* this can't be strcpy() because there are embedded null characters */
4750 memcpy(list, supported, sizeof(supported));
4751 return sizeof(supported);
4756 * Open a print file to be written to by other calls
4759 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
4761 fstring server, share, user, password;
4764 if (!context || !context->internal ||
4765 !context->internal->_initialized) {
4779 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
4781 if (smbc_parse_path(context, fname,
4782 server, sizeof(server),
4783 share, sizeof(share),
4786 password, sizeof(password),
4792 /* What if the path is empty, or the file exists? */
4794 return context->open(context, fname, O_WRONLY, 666);
4799 * Routine to print a file on a remote server ...
4801 * We open the file, which we assume to be on a remote server, and then
4802 * copy it to a print file on the share specified by printq.
4805 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
4807 SMBCFILE *fid1, *fid2;
4808 int bytes, saverr, tot_bytes = 0;
4811 if (!c_file || !c_file->internal->_initialized || !c_print ||
4812 !c_print->internal->_initialized) {
4819 if (!fname && !printq) {
4826 /* Try to open the file for reading ... */
4828 if ((int)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
4830 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
4831 return -1; /* smbc_open sets errno */
4835 /* Now, try to open the printer file for writing */
4837 if ((int)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
4839 saverr = errno; /* Save errno */
4840 c_file->close(c_file, fid1);
4846 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
4850 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
4853 c_file->close(c_file, fid1);
4854 c_print->close(c_print, fid2);
4863 c_file->close(c_file, fid1); /* We have to close these anyway */
4864 c_print->close(c_print, fid2);
4878 * Routine to list print jobs on a printer share ...
4881 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
4884 fstring server, share, user, password, workgroup;
4887 if (!context || !context->internal ||
4888 !context->internal->_initialized) {
4902 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
4904 if (smbc_parse_path(context, fname,
4905 server, sizeof(server),
4906 share, sizeof(share),
4909 password, sizeof(password),
4915 if (user[0] == (char)0) fstrcpy(user, context->user);
4917 fstrcpy(workgroup, context->workgroup);
4919 srv = smbc_server(context, server, share, workgroup, user, password);
4923 return -1; /* errno set by smbc_server */
4927 if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
4929 errno = smbc_errno(context, &srv->cli);
4939 * Delete a print job from a remote printer share
4942 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
4945 fstring server, share, user, password, workgroup;
4949 if (!context || !context->internal ||
4950 !context->internal->_initialized) {
4964 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
4966 if (smbc_parse_path(context, fname,
4967 server, sizeof(server),
4968 share, sizeof(share),
4971 password, sizeof(password),
4977 if (user[0] == (char)0) fstrcpy(user, context->user);
4979 fstrcpy(workgroup, context->workgroup);
4981 srv = smbc_server(context, server, share, workgroup, user, password);
4985 return -1; /* errno set by smbc_server */
4989 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
4992 errno = smbc_errno(context, &srv->cli);
4993 else if (err == ERRnosuchprintjob)
5004 * Get a new empty handle to fill in with your own info
5006 SMBCCTX * smbc_new_context(void)
5010 context = SMB_MALLOC_P(SMBCCTX);
5016 ZERO_STRUCTP(context);
5018 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
5019 if (!context->internal) {
5024 ZERO_STRUCTP(context->internal);
5027 /* ADD REASONABLE DEFAULTS */
5029 context->timeout = 20000; /* 20 seconds */
5031 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
5032 context->options.urlencode_readdir_entries = False;/* backward compat */
5033 context->options.one_share_per_server = False;/* backward compat */
5035 context->open = smbc_open_ctx;
5036 context->creat = smbc_creat_ctx;
5037 context->read = smbc_read_ctx;
5038 context->write = smbc_write_ctx;
5039 context->close = smbc_close_ctx;
5040 context->unlink = smbc_unlink_ctx;
5041 context->rename = smbc_rename_ctx;
5042 context->lseek = smbc_lseek_ctx;
5043 context->stat = smbc_stat_ctx;
5044 context->fstat = smbc_fstat_ctx;
5045 context->opendir = smbc_opendir_ctx;
5046 context->closedir = smbc_closedir_ctx;
5047 context->readdir = smbc_readdir_ctx;
5048 context->getdents = smbc_getdents_ctx;
5049 context->mkdir = smbc_mkdir_ctx;
5050 context->rmdir = smbc_rmdir_ctx;
5051 context->telldir = smbc_telldir_ctx;
5052 context->lseekdir = smbc_lseekdir_ctx;
5053 context->fstatdir = smbc_fstatdir_ctx;
5054 context->chmod = smbc_chmod_ctx;
5055 context->utimes = smbc_utimes_ctx;
5056 context->setxattr = smbc_setxattr_ctx;
5057 context->getxattr = smbc_getxattr_ctx;
5058 context->removexattr = smbc_removexattr_ctx;
5059 context->listxattr = smbc_listxattr_ctx;
5060 context->open_print_job = smbc_open_print_job_ctx;
5061 context->print_file = smbc_print_file_ctx;
5062 context->list_print_jobs = smbc_list_print_jobs_ctx;
5063 context->unlink_print_job = smbc_unlink_print_job_ctx;
5065 context->callbacks.check_server_fn = smbc_check_server;
5066 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
5068 smbc_default_cache_functions(context);
5076 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
5077 * and thus you'll be leaking memory if not handled properly.
5080 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
5089 DEBUG(1,("Performing aggressive shutdown.\n"));
5091 f = context->internal->_files;
5093 context->close(context, f);
5096 context->internal->_files = NULL;
5098 /* First try to remove the servers the nice way. */
5099 if (context->callbacks.purge_cached_fn(context)) {
5102 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
5103 s = context->internal->_servers;
5105 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n", s, s->cli.fd));
5106 cli_shutdown(&s->cli);
5107 context->callbacks.remove_cached_srv_fn(context, s);
5109 DLIST_REMOVE(context->internal->_servers, s);
5113 context->internal->_servers = NULL;
5117 /* This is the polite way */
5118 if (context->callbacks.purge_cached_fn(context)) {
5119 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
5123 if (context->internal->_servers) {
5124 DEBUG(1, ("Active servers in context, free_context failed.\n"));
5128 if (context->internal->_files) {
5129 DEBUG(1, ("Active files in context, free_context failed.\n"));
5135 /* Things we have to clean up */
5136 SAFE_FREE(context->workgroup);
5137 SAFE_FREE(context->netbios_name);
5138 SAFE_FREE(context->user);
5140 DEBUG(3, ("Context %p succesfully freed\n", context));
5141 SAFE_FREE(context->internal);
5148 * Initialise the library etc
5150 * We accept a struct containing handle information.
5151 * valid values for info->debug from 0 to 100,
5152 * and insist that info->fn must be non-null.
5154 SMBCCTX * smbc_init_context(SMBCCTX * context)
5158 char *user = NULL, *home = NULL;
5160 if (!context || !context->internal) {
5165 /* Do not initialise the same client twice */
5166 if (context->internal->_initialized) {
5170 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
5177 if (!smbc_initialized) {
5178 /* Do some library wide intialisations the first time we get called */
5180 /* Set this to what the user wants */
5181 DEBUGLEVEL = context->debug;
5183 setup_logging( "libsmbclient", True);
5185 /* Here we would open the smb.conf file if needed ... */
5187 home = getenv("HOME");
5189 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
5191 load_interfaces(); /* Load the list of interfaces ... */
5193 in_client = True; /* FIXME, make a param */
5195 if (!lp_load(conf, True, False, False)) {
5198 * Well, if that failed, try the dyn_CONFIGFILE
5199 * Which points to the standard locn, and if that
5200 * fails, silently ignore it and use the internal
5204 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
5205 DEBUG(5, ("Could not load either config file: "
5207 conf, dyn_CONFIGFILE));
5210 * We loaded the global config file. Now lets
5211 * load user-specific modifications to the
5214 slprintf(conf, sizeof(conf),
5215 "%s/.smb/smb.conf.append", home);
5216 if (!lp_load(conf, True, False, False)) {
5218 ("Could not append config file: "
5225 reopen_logs(); /* Get logging working ... */
5228 * Block SIGPIPE (from lib/util_sock.c: write())
5229 * It is not needed and should not stop execution
5231 BlockSignals(True, SIGPIPE);
5233 /* Done with one-time initialisation */
5234 smbc_initialized = 1;
5238 if (!context->user) {
5240 * FIXME: Is this the best way to get the user info?
5242 user = getenv("USER");
5243 /* walk around as "guest" if no username can be found */
5244 if (!user) context->user = SMB_STRDUP("guest");
5245 else context->user = SMB_STRDUP(user);
5248 if (!context->netbios_name) {
5250 * We try to get our netbios name from the config. If that fails we fall
5251 * back on constructing our netbios name from our hostname etc
5253 if (global_myname()) {
5254 context->netbios_name = SMB_STRDUP(global_myname());
5258 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
5261 context->netbios_name = SMB_MALLOC(17);
5262 if (!context->netbios_name) {
5266 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
5270 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
5272 if (!context->workgroup) {
5273 if (lp_workgroup()) {
5274 context->workgroup = SMB_STRDUP(lp_workgroup());
5277 /* TODO: Think about a decent default workgroup */
5278 context->workgroup = SMB_STRDUP("samba");
5282 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
5284 /* shortest timeout is 1 second */
5285 if (context->timeout > 0 && context->timeout < 1000)
5286 context->timeout = 1000;
5289 * FIXME: Should we check the function pointers here?
5292 context->internal->_initialized = 1;
5298 /* Return the verion of samba, and thus libsmbclient */
5302 return samba_version_string();