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 extern BOOL in_client;
86 * Is the logging working / configfile read ?
88 static int smbc_initialized = 0;
91 hex2int( unsigned int _char )
93 if ( _char >= 'A' && _char <='F')
94 return _char - 'A' + 10;
95 if ( _char >= 'a' && _char <='f')
96 return _char - 'a' + 10;
97 if ( _char >= '0' && _char <='9')
105 * Convert strings of %xx to their single character equivalent. Each 'x' must
106 * be a valid hexadecimal digit, or that % sequence is left undecoded.
108 * dest may, but need not be, the same pointer as src.
110 * Returns the number of % sequences which could not be converted due to lack
111 * of two following hexadecimal digits.
114 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
116 int old_length = strlen(src);
122 if ( old_length == 0 ) {
127 while ( i < old_length ) {
128 unsigned char character = src[ i++ ];
130 if (character == '%') {
131 int a = i+1 < old_length ? hex2int( src[i] ) : -1;
132 int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
134 /* Replace valid sequence */
135 if (a != -1 && b != -1) {
137 /* Replace valid %xx sequence with %dd */
138 character = (a * 16) + b;
140 if (character == '\0') {
141 break; /* Stop at %00 */
156 strncpy(dest, temp, max_dest_len);
164 * Convert any characters not specifically allowed in a URL into their %xx
167 * Returns the remaining buffer length.
170 smbc_urlencode(char * dest, char * src, int max_dest_len)
172 char hex[] = "0123456789ABCDEF";
174 for (; *src != '\0' && max_dest_len >= 3; src++) {
186 *dest++ = hex[(*src >> 4) & 0x0f];
187 *dest++ = hex[*src & 0x0f];
202 * Function to parse a path and turn it into components
204 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
205 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
206 * general format ("smb:" only; we do not look for "cifs:").
210 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]][?options]
214 * smb:// Show all workgroups.
216 * The method of locating the list of workgroups varies
217 * depending upon the setting of the context variable
218 * context->options.browse_max_lmb_count. This value
219 * determine the maximum number of local master browsers to
220 * query for the list of workgroups. In order to ensure that
221 * a complete list of workgroups is obtained, all master
222 * browsers must be queried, but if there are many
223 * workgroups, the time spent querying can begin to add up.
224 * For small networks (not many workgroups), it is suggested
225 * that this variable be set to 0, indicating query all local
226 * master browsers. When the network has many workgroups, a
227 * reasonable setting for this variable might be around 3.
229 * smb://name/ if name<1D> or name<1B> exists, list servers in
230 * workgroup, else, if name<20> exists, list all shares
233 * If "options" are provided, this function returns the entire option list as a
234 * string, for later parsing by the caller. Note that currently, no options
238 static const char *smbc_prefix = "smb:";
241 smbc_parse_path(SMBCCTX *context,
243 char *server, int server_len,
244 char *share, int share_len,
245 char *path, int path_len,
246 char *user, int user_len,
247 char *password, int password_len,
248 char *options, int options_len)
256 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
257 if (options != NULL && options_len > 0) {
258 options[0] = (char)0;
262 /* see if it has the right prefix */
263 len = strlen(smbc_prefix);
264 if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
265 return -1; /* What about no smb: ? */
270 /* Watch the test below, we are testing to see if we should exit */
272 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
274 DEBUG(1, ("Invalid path (does not begin with smb://"));
279 p += 2; /* Skip the double slash */
281 /* See if any options were specified */
282 if ((q = strrchr(p, '?')) != NULL ) {
283 /* There are options. Null terminate here and point to them */
286 DEBUG(4, ("Found options '%s'", q));
288 /* Copy the options */
289 if (options != NULL && options_len > 0) {
290 safe_strcpy(options, q, options_len - 1);
299 strncpy(server, context->workgroup,
300 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
306 * ok, its for us. Now parse out the server, share etc.
308 * However, we want to parse out [[domain;]user[:password]@] if it
312 /* check that '@' occurs before '/', if '/' exists at all */
313 q = strchr_m(p, '@');
314 r = strchr_m(p, '/');
315 if (q && (!r || q < r)) {
316 pstring username, passwd, domain;
317 const char *u = userinfo;
319 next_token(&p, userinfo, "@", sizeof(fstring));
321 username[0] = passwd[0] = domain[0] = 0;
323 if (strchr_m(u, ';')) {
325 next_token(&u, domain, ";", sizeof(fstring));
329 if (strchr_m(u, ':')) {
331 next_token(&u, username, ":", sizeof(fstring));
338 pstrcpy(username, u);
343 strncpy(user, username, user_len); /* FIXME, domain */
346 strncpy(password, passwd, password_len);
350 if (!next_token(&p, server, "/", sizeof(fstring))) {
356 if (*p == (char)0) goto decoding; /* That's it ... */
358 if (!next_token(&p, share, "/", sizeof(fstring))) {
364 safe_strcpy(path, p, path_len - 1);
366 all_string_sub(path, "/", "\\", 0);
369 (void) smbc_urldecode(path, path, path_len);
370 (void) smbc_urldecode(server, server, server_len);
371 (void) smbc_urldecode(share, share, share_len);
372 (void) smbc_urldecode(user, user, user_len);
373 (void) smbc_urldecode(password, password, password_len);
379 * Verify that the options specified in a URL are valid
381 static int smbc_check_options(char *server, char *share, char *path, char *options)
383 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' path='%s' options='%s'\n", server, share, path, options));
385 /* No options at all is always ok */
386 if (! *options) return 0;
388 /* Currently, we don't support any options. */
393 * Convert an SMB error into a UNIX error ...
395 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
397 int ret = cli_errno(c);
399 if (cli_is_dos_error(c)) {
403 cli_dos_error(c, &eclass, &ecode);
405 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
406 (int)eclass, (int)ecode, (int)ecode, ret));
410 status = cli_nt_error(c);
412 DEBUG(3,("smbc errno %s -> %d\n",
413 nt_errstr(status), ret));
421 * returns 0 if the server is in shape. Returns 1 on error
423 * Also useable outside libsmbclient to enable external cache
424 * to do some checks too.
426 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
428 if ( send_keepalive(server->cli.fd) == False )
431 /* connection is ok */
436 * Remove a server from the cached server list it's unused.
437 * On success, 0 is returned. 1 is returned if the server could not be removed.
439 * Also useable outside libsmbclient
441 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
445 /* are we being fooled ? */
446 if (!context || !context->internal ||
447 !context->internal->_initialized || !srv) return 1;
450 /* Check all open files/directories for a relation with this server */
451 for (file = context->internal->_files; file; file=file->next) {
452 if (file->srv == srv) {
454 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
460 DLIST_REMOVE(context->internal->_servers, srv);
462 cli_shutdown(&srv->cli);
464 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
466 context->callbacks.remove_cached_srv_fn(context, srv);
471 SMBCSRV *find_server(SMBCCTX *context,
483 srv = context->callbacks.get_cached_srv_fn(context, server, share,
484 workgroup, username);
486 if (!auth_called && !srv && (!username[0] || !password[0])) {
487 context->callbacks.auth_fn(server, share,
488 workgroup, sizeof(fstring),
489 username, sizeof(fstring),
490 password, sizeof(fstring));
492 * However, smbc_auth_fn may have picked up info relating to
493 * an existing connection, so try for an existing connection
497 goto check_server_cache;
502 if (context->callbacks.check_server_fn(context, srv)) {
504 * This server is no good anymore
505 * Try to remove it and check for more possible
506 * servers in the cache
508 if (context->callbacks.remove_unused_server_fn(context,
511 * We could not remove the server completely,
512 * remove it from the cache so we will not get
513 * it again. It will be removed when the last
514 * file/dir is closed.
516 context->callbacks.remove_cached_srv_fn(context,
521 * Maybe there are more cached connections to this
524 goto check_server_cache;
534 * Connect to a server, possibly on an existing connection
536 * Here, what we want to do is: If the server and username
537 * match an existing connection, reuse that, otherwise, establish a
540 * If we have to create a new connection, call the auth_fn to get the
541 * info we need, unless the username and password were passed in.
544 SMBCSRV *smbc_server(SMBCCTX *context,
545 const char *server, const char *share,
546 fstring workgroup, fstring username,
551 struct nmb_name called, calling;
552 const char *server_n = server;
555 int tried_reverse = 0;
562 if (server[0] == 0) {
567 srv = find_server(context, server, share,
568 workgroup, username, password);
571 * If we found a connection and we're only allowed one share per
574 if (srv && *share != '\0' && context->options.one_share_per_server) {
577 * ... then if there's no current connection to the share,
578 * connect to it. find_server(), or rather the function
579 * pointed to by context->callbacks.get_cached_srv_fn which
580 * was called by find_server(), will have issued a tree
581 * disconnect if the requested share is not the same as the
582 * one that was already connected.
584 if (srv->cli.cnum == (uint16) -1) {
585 /* Ensure we have accurate auth info */
586 context->callbacks.auth_fn(server, share,
587 workgroup, sizeof(fstring),
588 username, sizeof(fstring),
589 password, sizeof(fstring));
591 if (! cli_send_tconX(&srv->cli, share, "?????",
592 password, strlen(password)+1)) {
594 errno = smbc_errno(context, &srv->cli);
595 cli_shutdown(&srv->cli);
596 context->callbacks.remove_cached_srv_fn(context, srv);
600 /* Regenerate the dev value since it's based on both server and share */
602 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
607 /* If we have a connection... */
610 /* ... then we're done here. Give 'em what they came for. */
614 make_nmb_name(&calling, context->netbios_name, 0x0);
615 make_nmb_name(&called , server, 0x20);
617 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
619 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
622 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
626 /* have to open a new connection */
627 if (!cli_initialise(&c)) {
632 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
633 c.use_kerberos = True;
635 if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
636 c.fallback_after_kerberos = True;
639 c.timeout = context->timeout;
642 * Force use of port 139 for first try if share is $IPC, empty, or
643 * null, so browse lists can work
645 if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0)
647 port_try_first = 139;
652 port_try_first = 445;
656 c.port = port_try_first;
658 if (!cli_connect(&c, server_n, &ip)) {
660 /* First connection attempt failed. Try alternate port. */
661 c.port = port_try_next;
663 if (!cli_connect(&c, server_n, &ip)) {
670 if (!cli_session_request(&c, &calling, &called)) {
672 if (strcmp(called.name, "*SMBSERVER")) {
673 make_nmb_name(&called , "*SMBSERVER", 0x20);
676 else { /* Try one more time, but ensure we don't loop */
678 /* Only try this if server is an IP address ... */
680 if (is_ipaddress(server) && !tried_reverse) {
682 struct in_addr rem_ip;
684 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
685 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
690 tried_reverse++; /* Yuck */
692 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
693 make_nmb_name(&called, remote_name, 0x20);
704 DEBUG(4,(" session request ok\n"));
706 if (!cli_negprot(&c)) {
712 if (!cli_session_setup(&c, username,
713 password, strlen(password),
714 password, strlen(password),
716 /* Try an anonymous login if it failed and this was allowed by flags. */
717 ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
718 !cli_session_setup(&c, "", "", 1,"", 0, workgroup))) {
724 DEBUG(4,(" session setup ok\n"));
726 if (!cli_send_tconX(&c, share, "?????",
727 password, strlen(password)+1)) {
728 errno = smbc_errno(context, &c);
733 DEBUG(4,(" tconx ok\n"));
736 * Ok, we have got a nice connection
737 * Let's find a free server_fd
740 srv = SMB_MALLOC_P(SMBCSRV);
748 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
750 /* now add it to the cache (internal or external) */
751 /* Let the cache function set errno if it wants to */
753 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
754 int saved_errno = errno;
755 DEBUG(3, (" Failed to add server to cache\n"));
763 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
764 server, share, srv));
766 DLIST_ADD(context->internal->_servers, srv);
771 if (!srv) return NULL;
778 * Connect to a server for getting/setting attributes, possibly on an existing
779 * connection. This works similarly to smbc_server().
781 SMBCSRV *smbc_attr_server(SMBCCTX *context,
782 const char *server, const char *share,
784 fstring username, fstring password,
788 struct cli_state *ipc_cli;
790 SMBCSRV *ipc_srv=NULL;
793 * See if we've already created this special connection. Reference our
794 * "special" share name '*IPC$', which is an impossible real share name
795 * due to the leading asterisk.
797 ipc_srv = find_server(context, server, "*IPC$",
798 workgroup, username, password);
801 /* We didn't find a cached connection. Get the password */
802 if (*password == '\0') {
803 /* ... then retrieve it now. */
804 context->callbacks.auth_fn(server, share,
805 workgroup, sizeof(fstring),
806 username, sizeof(fstring),
807 password, sizeof(fstring));
811 nt_status = cli_full_connection(&ipc_cli,
812 global_myname(), server,
813 &ip, 0, "IPC$", "?????",
817 if (! NT_STATUS_IS_OK(nt_status)) {
818 DEBUG(1,("cli_full_connection failed! (%s)\n",
819 nt_errstr(nt_status)));
824 if (!cli_nt_session_open(ipc_cli, PI_LSARPC)) {
825 DEBUG(1, ("cli_nt_session_open fail!\n"));
827 cli_shutdown(ipc_cli);
831 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
832 but NT sends 0x2000000 so we might as well do it too. */
834 nt_status = cli_lsa_open_policy(ipc_cli,
837 GENERIC_EXECUTE_ACCESS,
840 if (!NT_STATUS_IS_OK(nt_status)) {
841 errno = smbc_errno(context, ipc_cli);
842 cli_shutdown(ipc_cli);
846 ipc_srv = SMB_MALLOC_P(SMBCSRV);
849 cli_shutdown(ipc_cli);
853 ZERO_STRUCTP(ipc_srv);
854 ipc_srv->cli = *ipc_cli;
858 /* now add it to the cache (internal or external) */
860 errno = 0; /* let cache function set errno if it likes */
861 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
866 DEBUG(3, (" Failed to add server to cache\n"));
870 cli_shutdown(&ipc_srv->cli);
875 DLIST_ADD(context->internal->_servers, ipc_srv);
882 * Routine to open() a file ...
885 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
887 fstring server, share, user, password, workgroup;
890 SMBCFILE *file = NULL;
893 if (!context || !context->internal ||
894 !context->internal->_initialized) {
896 errno = EINVAL; /* Best I can think of ... */
908 if (smbc_parse_path(context, fname,
909 server, sizeof(server),
910 share, sizeof(share),
913 password, sizeof(password),
919 if (user[0] == (char)0) fstrcpy(user, context->user);
921 fstrcpy(workgroup, context->workgroup);
923 srv = smbc_server(context, server, share, workgroup, user, password);
927 if (errno == EPERM) errno = EACCES;
928 return NULL; /* smbc_server sets errno */
932 /* Hmmm, the test for a directory is suspect here ... FIXME */
934 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
941 file = SMB_MALLOC_P(SMBCFILE);
952 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
954 /* Handle the error ... */
957 errno = smbc_errno(context, &srv->cli);
962 /* Fill in file struct */
965 file->fname = SMB_STRDUP(fname);
970 DLIST_ADD(context->internal->_files, file);
975 /* Check if opendir needed ... */
980 eno = smbc_errno(context, &srv->cli);
981 file = context->opendir(context, fname);
982 if (!file) errno = eno;
987 errno = EINVAL; /* FIXME, correct errno ? */
993 * Routine to create a file
996 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
998 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
1001 if (!context || !context->internal ||
1002 !context->internal->_initialized) {
1009 return smbc_open_ctx(context, path, creat_bits, mode);
1013 * Routine to read() a file ...
1016 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
1023 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1024 * appears to pass file->offset (which is type off_t) differently than
1025 * a local variable of type off_t. Using local variable "offset" in
1026 * the call to cli_read() instead of file->offset fixes a problem
1027 * retrieving data at an offset greater than 4GB.
1029 off_t offset = file->offset;
1031 if (!context || !context->internal ||
1032 !context->internal->_initialized) {
1039 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1041 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1048 /* Check that the buffer exists ... */
1057 ret = cli_read(&file->srv->cli, file->cli_fd, buf, offset, count);
1061 errno = smbc_errno(context, &file->srv->cli);
1066 file->offset += ret;
1068 DEBUG(4, (" --> %d\n", ret));
1070 return ret; /* Success, ret bytes of data ... */
1075 * Routine to write() a file ...
1078 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
1081 off_t offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1083 if (!context || !context->internal ||
1084 !context->internal->_initialized) {
1091 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1098 /* Check that the buffer exists ... */
1107 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, offset, count);
1111 errno = smbc_errno(context, &file->srv->cli);
1116 file->offset += ret;
1118 return ret; /* Success, 0 bytes of data ... */
1122 * Routine to close() a file ...
1125 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
1129 if (!context || !context->internal ||
1130 !context->internal->_initialized) {
1137 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1147 return context->closedir(context, file);
1151 if (!cli_close(&file->srv->cli, file->cli_fd)) {
1153 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1155 /* Deallocate slot and remove the server
1156 * from the server cache if unused */
1157 errno = smbc_errno(context, &file->srv->cli);
1159 DLIST_REMOVE(context->internal->_files, file);
1160 SAFE_FREE(file->fname);
1162 context->callbacks.remove_unused_server_fn(context, srv);
1168 DLIST_REMOVE(context->internal->_files, file);
1169 SAFE_FREE(file->fname);
1176 * Get info from an SMB server on a file. Use a qpathinfo call first
1177 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1179 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1180 uint16 *mode, SMB_OFF_T *size,
1181 time_t *c_time, time_t *a_time, time_t *m_time,
1185 if (!context || !context->internal ||
1186 !context->internal->_initialized) {
1193 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1195 if (!srv->no_pathinfo2 &&
1196 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1197 size, mode, ino)) return True;
1199 /* if this is NT then don't bother with the getatr */
1200 if (srv->cli.capabilities & CAP_NT_SMBS) {
1205 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1206 a_time = c_time = m_time;
1207 srv->no_pathinfo2 = True;
1217 * Routine to unlink() a file
1220 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
1222 fstring server, share, user, password, workgroup;
1224 SMBCSRV *srv = NULL;
1226 if (!context || !context->internal ||
1227 !context->internal->_initialized) {
1229 errno = EINVAL; /* Best I can think of ... */
1241 if (smbc_parse_path(context, fname,
1242 server, sizeof(server),
1243 share, sizeof(share),
1246 password, sizeof(password),
1252 if (user[0] == (char)0) fstrcpy(user, context->user);
1254 fstrcpy(workgroup, context->workgroup);
1256 srv = smbc_server(context, server, share, workgroup, user, password);
1260 return -1; /* smbc_server sets errno */
1264 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1266 int job = smbc_stat_printjob(srv, path, NULL, NULL);
1272 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
1280 if (!cli_unlink(&srv->cli, path)) {
1282 errno = smbc_errno(context, &srv->cli);
1284 if (errno == EACCES) { /* Check if the file is a directory */
1289 time_t m_time = 0, a_time = 0, c_time = 0;
1292 if (!smbc_getatr(context, srv, path, &mode, &size,
1293 &c_time, &a_time, &m_time, &ino)) {
1295 /* Hmmm, bad error ... What? */
1297 errno = smbc_errno(context, &srv->cli);
1303 if (IS_DOS_DIR(mode))
1306 errno = saverr; /* Restore this */
1315 return 0; /* Success ... */
1320 * Routine to rename() a file
1323 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
1324 SMBCCTX *ncontext, const char *nname)
1326 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
1327 pstring path1, path2;
1328 SMBCSRV *srv = NULL;
1330 if (!ocontext || !ncontext ||
1331 !ocontext->internal || !ncontext->internal ||
1332 !ocontext->internal->_initialized ||
1333 !ncontext->internal->_initialized) {
1335 errno = EINVAL; /* Best I can think of ... */
1340 if (!oname || !nname) {
1347 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1349 smbc_parse_path(ocontext, oname,
1350 server1, sizeof(server1),
1351 share1, sizeof(share1),
1352 path1, sizeof(path1),
1353 user1, sizeof(user1),
1354 password1, sizeof(password1),
1357 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1359 smbc_parse_path(ncontext, nname,
1360 server2, sizeof(server2),
1361 share2, sizeof(share2),
1362 path2, sizeof(path2),
1363 user2, sizeof(user2),
1364 password2, sizeof(password2),
1367 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1369 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1370 strcmp(user1, user2)) {
1372 /* Can't rename across file systems, or users?? */
1379 fstrcpy(workgroup, ocontext->workgroup);
1380 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1381 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
1388 if (!cli_rename(&srv->cli, path1, path2)) {
1389 int eno = smbc_errno(ocontext, &srv->cli);
1391 if (eno != EEXIST ||
1392 !cli_unlink(&srv->cli, path2) ||
1393 !cli_rename(&srv->cli, path1, path2)) {
1401 return 0; /* Success */
1406 * A routine to lseek() a file
1409 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
1413 if (!context || !context->internal ||
1414 !context->internal->_initialized) {
1421 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1431 return -1; /* Can't lseek a dir ... */
1437 file->offset = offset;
1441 file->offset += offset;
1445 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1448 SMB_BIG_UINT b_size = size;
1449 if (!cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &b_size, NULL, NULL,
1457 file->offset = size + offset;
1466 return file->offset;
1471 * Generate an inode number from file name for those things that need it
1475 ino_t smbc_inode(SMBCCTX *context, const char *name)
1478 if (!context || !context->internal ||
1479 !context->internal->_initialized) {
1486 if (!*name) return 2; /* FIXME, why 2 ??? */
1487 return (ino_t)str_checksum(name);
1492 * Routine to put basic stat info into a stat structure ... Used by stat and
1497 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname,
1498 SMB_OFF_T size, int mode)
1503 if (IS_DOS_DIR(mode)) {
1504 st->st_mode = SMBC_DIR_MODE;
1506 st->st_mode = SMBC_FILE_MODE;
1509 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1510 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1511 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1512 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1515 #ifdef HAVE_STAT_ST_BLKSIZE
1516 st->st_blksize = 512;
1518 #ifdef HAVE_STAT_ST_BLOCKS
1519 st->st_blocks = (size+511)/512;
1521 st->st_uid = getuid();
1522 st->st_gid = getgid();
1524 if (IS_DOS_DIR(mode)) {
1530 if (st->st_ino == 0) {
1531 st->st_ino = smbc_inode(context, fname);
1534 return True; /* FIXME: Is this needed ? */
1539 * Routine to stat a file given a name
1542 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1545 fstring server, share, user, password, workgroup;
1547 time_t m_time = 0, a_time = 0, c_time = 0;
1552 if (!context || !context->internal ||
1553 !context->internal->_initialized) {
1555 errno = EINVAL; /* Best I can think of ... */
1567 DEBUG(4, ("smbc_stat(%s)\n", fname));
1569 if (smbc_parse_path(context, fname,
1570 server, sizeof(server),
1571 share, sizeof(share),
1574 password, sizeof(password),
1580 if (user[0] == (char)0) fstrcpy(user, context->user);
1582 fstrcpy(workgroup, context->workgroup);
1584 srv = smbc_server(context, server, share, workgroup, user, password);
1587 return -1; /* errno set by smbc_server */
1590 if (!smbc_getatr(context, srv, path, &mode, &size,
1591 &c_time, &a_time, &m_time, &ino)) {
1593 errno = smbc_errno(context, &srv->cli);
1600 smbc_setup_stat(context, st, path, size, mode);
1602 st->st_atime = a_time;
1603 st->st_ctime = c_time;
1604 st->st_mtime = m_time;
1605 st->st_dev = srv->dev;
1612 * Routine to stat a file given an fd
1615 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1617 time_t c_time, a_time, m_time;
1622 if (!context || !context->internal ||
1623 !context->internal->_initialized) {
1630 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1639 return context->fstatdir(context, file, st);
1643 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1644 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino)) {
1645 if (!cli_getattrE(&file->srv->cli, file->cli_fd,
1646 &mode, &size, &c_time, &a_time, &m_time)) {
1655 smbc_setup_stat(context, st, file->fname, size, mode);
1657 st->st_atime = a_time;
1658 st->st_ctime = c_time;
1659 st->st_mtime = m_time;
1660 st->st_dev = file->srv->dev;
1667 * Routine to open a directory
1668 * We accept the URL syntax explained in smbc_parse_path(), above.
1671 static void smbc_remove_dir(SMBCFILE *dir)
1673 struct smbc_dir_list *d,*f;
1680 SAFE_FREE(f->dirent);
1685 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1689 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1691 struct smbc_dirent *dirent;
1693 int name_length = (name == NULL ? 0 : strlen(name));
1694 int comment_len = (comment == NULL ? 0 : strlen(comment));
1697 * Allocate space for the dirent, which must be increased by the
1698 * size of the name and the comment and 1 each for the null terminator.
1701 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
1703 dirent = SMB_MALLOC(size);
1707 dir->dir_error = ENOMEM;
1712 ZERO_STRUCTP(dirent);
1714 if (dir->dir_list == NULL) {
1716 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
1717 if (!dir->dir_list) {
1720 dir->dir_error = ENOMEM;
1724 ZERO_STRUCTP(dir->dir_list);
1726 dir->dir_end = dir->dir_next = dir->dir_list;
1730 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
1732 if (!dir->dir_end->next) {
1735 dir->dir_error = ENOMEM;
1739 ZERO_STRUCTP(dir->dir_end->next);
1741 dir->dir_end = dir->dir_end->next;
1744 dir->dir_end->next = NULL;
1745 dir->dir_end->dirent = dirent;
1747 dirent->smbc_type = type;
1748 dirent->namelen = name_length;
1749 dirent->commentlen = comment_len;
1750 dirent->dirlen = size;
1752 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1754 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1755 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1762 list_unique_wg_fn(const char *name, uint32 type, const char *comment, void *state)
1764 SMBCFILE *dir = (SMBCFILE *)state;
1765 struct smbc_dir_list *dir_list;
1766 struct smbc_dirent *dirent;
1770 dirent_type = dir->dir_type;
1772 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1774 /* An error occurred, what do we do? */
1775 /* FIXME: Add some code here */
1778 /* Point to the one just added */
1779 dirent = dir->dir_end->dirent;
1781 /* See if this was a duplicate */
1782 for (dir_list = dir->dir_list;
1783 dir_list != dir->dir_end;
1784 dir_list = dir_list->next) {
1786 strcmp(dir_list->dirent->name, dirent->name) == 0) {
1787 /* Duplicate. End end of list need to be removed. */
1791 if (do_remove && dir_list->next == dir->dir_end) {
1792 /* Found the end of the list. Remove it. */
1793 dir->dir_end = dir_list;
1794 free(dir_list->next);
1795 dir_list->next = NULL;
1802 list_fn(const char *name, uint32 type, const char *comment, void *state)
1804 SMBCFILE *dir = (SMBCFILE *)state;
1807 /* We need to process the type a little ... */
1809 if (dir->dir_type == SMBC_FILE_SHARE) {
1812 case 0: /* Directory tree */
1813 dirent_type = SMBC_FILE_SHARE;
1817 dirent_type = SMBC_PRINTER_SHARE;
1821 dirent_type = SMBC_COMMS_SHARE;
1825 dirent_type = SMBC_IPC_SHARE;
1829 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1833 else dirent_type = dir->dir_type;
1835 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1837 /* An error occurred, what do we do? */
1838 /* FIXME: Add some code here */
1844 dir_list_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
1847 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1848 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1850 /* Handle an error ... */
1852 /* FIXME: Add some code ... */
1858 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1860 fstring server, share, user, password, options;
1863 SMBCSRV *srv = NULL;
1864 SMBCFILE *dir = NULL;
1865 struct in_addr rem_ip;
1867 if (!context || !context->internal ||
1868 !context->internal->_initialized) {
1869 DEBUG(4, ("no valid context\n"));
1870 errno = EINVAL + 8192;
1876 DEBUG(4, ("no valid fname\n"));
1877 errno = EINVAL + 8193;
1881 if (smbc_parse_path(context, fname,
1882 server, sizeof(server),
1883 share, sizeof(share),
1886 password, sizeof(password),
1887 options, sizeof(options))) {
1888 DEBUG(4, ("no valid path\n"));
1889 errno = EINVAL + 8194;
1893 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s' options='%s'\n", fname, server, share, path, options));
1895 /* Ensure the options are valid */
1896 if (smbc_check_options(server, share, path, options)) {
1897 DEBUG(4, ("unacceptable options (%s)\n", options));
1898 errno = EINVAL + 8195;
1902 if (user[0] == (char)0) fstrcpy(user, context->user);
1904 pstrcpy(workgroup, context->workgroup);
1906 dir = SMB_MALLOC_P(SMBCFILE);
1918 dir->fname = SMB_STRDUP(fname);
1922 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1924 if (server[0] == (char)0) {
1929 struct ip_service *ip_list;
1930 struct ip_service server_addr;
1931 struct user_auth_info u_info;
1932 struct cli_state *cli;
1934 if (share[0] != (char)0 || path[0] != (char)0) {
1936 errno = EINVAL + 8196;
1938 SAFE_FREE(dir->fname);
1944 /* Determine how many local master browsers to query */
1945 max_lmb_count = (context->options.browse_max_lmb_count == 0
1947 : context->options.browse_max_lmb_count);
1949 pstrcpy(u_info.username, user);
1950 pstrcpy(u_info.password, password);
1953 * We have server and share and path empty but options
1954 * requesting that we scan all master browsers for their list
1955 * of workgroups/domains. This implies that we must first try
1956 * broadcast queries to find all master browsers, and if that
1957 * doesn't work, then try our other methods which return only
1958 * a single master browser.
1961 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
1962 if (!find_master_ip(workgroup, &server_addr.ip)) {
1968 ip_list = &server_addr;
1972 for (i = 0; i < count && i < max_lmb_count; i++) {
1973 DEBUG(99, ("Found master browser %d of %d: %s\n", i+1, MAX(count, max_lmb_count), inet_ntoa(ip_list[i].ip)));
1975 cli = get_ipc_connect_master_ip(&ip_list[i], workgroup, &u_info);
1976 /* cli == NULL is the master browser refused to talk or
1977 could not be found */
1981 fstrcpy(server, cli->desthost);
1984 DEBUG(4, ("using workgroup %s %s\n", workgroup, server));
1987 * For each returned master browser IP address, get a
1988 * connection to IPC$ on the server if we do not
1989 * already have one, and determine the
1990 * workgroups/domains that it knows about.
1993 srv = smbc_server(context, server,
1994 "IPC$", workgroup, user, password);
2000 dir->dir_type = SMBC_WORKGROUP;
2002 /* Now, list the stuff ... */
2004 if (!cli_NetServerEnum(&srv->cli, workgroup, SV_TYPE_DOMAIN_ENUM, list_unique_wg_fn,
2012 * Server not an empty string ... Check the rest and see what
2015 if (share[0] == (char)0) {
2017 if (path[0] != (char)0) { /* Should not have empty share with path */
2019 errno = EINVAL + 8197;
2021 SAFE_FREE(dir->fname);
2028 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
2029 /* However, we check to see if <server> is an IP address first */
2031 if (!is_ipaddress(server) && /* Not an IP addr so check next */
2032 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
2033 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
2036 dir->dir_type = SMBC_SERVER;
2039 * Get the backup list ...
2043 if (!name_status_find(server, 0, 0, rem_ip, buserver)) {
2045 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
2046 errno = EPERM; /* FIXME, is this correct */
2052 * Get a connection to IPC$ on the server if we do not already have one
2055 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
2058 DEBUG(0, ("got no contact to IPC$\n"));
2060 SAFE_FREE(dir->fname);
2069 /* Now, list the servers ... */
2071 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
2075 SAFE_FREE(dir->fname);
2084 if (resolve_name(server, &rem_ip, 0x20)) {
2086 /* Now, list the shares ... */
2088 dir->dir_type = SMBC_FILE_SHARE;
2090 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
2095 SAFE_FREE(dir->fname);
2104 /* Now, list the servers ... */
2106 if (cli_RNetShareEnum(&srv->cli, list_fn,
2109 errno = cli_errno(&srv->cli);
2111 SAFE_FREE(dir->fname);
2121 errno = ECONNREFUSED; /* Neither the workgroup nor server exists */
2123 SAFE_FREE(dir->fname);
2133 else { /* The server and share are specified ... work from there ... */
2135 /* Well, we connect to the server and list the directory */
2137 dir->dir_type = SMBC_FILE_SHARE;
2139 srv = smbc_server(context, server, share, workgroup, user, password);
2144 SAFE_FREE(dir->fname);
2153 /* Now, list the files ... */
2155 pstrcat(path, "\\*");
2157 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
2161 SAFE_FREE(dir->fname);
2164 errno = smbc_errno(context, &srv->cli);
2172 DLIST_ADD(context->internal->_files, dir);
2178 * Routine to close a directory
2181 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
2184 if (!context || !context->internal ||
2185 !context->internal->_initialized) {
2192 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2199 smbc_remove_dir(dir); /* Clean it up */
2201 DLIST_REMOVE(context->internal->_files, dir);
2205 SAFE_FREE(dir->fname);
2206 SAFE_FREE(dir); /* Free the space too */
2213 static void smbc_readdir_internal(SMBCCTX * context,
2214 struct smbc_dirent *dest,
2215 struct smbc_dirent *src,
2216 int max_namebuf_len)
2218 if (context->options.urlencode_readdir_entries) {
2220 /* url-encode the name. get back remaining buffer space */
2222 smbc_urlencode(dest->name, src->name, max_namebuf_len);
2224 /* We now know the name length */
2225 dest->namelen = strlen(dest->name);
2227 /* Save the pointer to the beginning of the comment */
2228 dest->comment = dest->name + dest->namelen + 1;
2230 /* Copy the comment */
2231 strncpy(dest->comment, src->comment, max_namebuf_len);
2233 /* Ensure the comment is null terminated */
2234 if (max_namebuf_len > src->commentlen) {
2235 dest->comment[src->commentlen] = '\0';
2237 dest->comment[max_namebuf_len - 1] = '\0';
2240 /* Save other fields */
2241 dest->smbc_type = src->smbc_type;
2242 dest->commentlen = strlen(dest->comment);
2243 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
2247 /* No encoding. Just copy the entry as is. */
2248 memcpy(dest, src, src->dirlen);
2249 dest->comment = (char *)(&dest->name + src->namelen + 1);
2255 * Routine to get a directory entry
2258 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
2261 struct smbc_dirent *dirp, *dirent;
2263 /* Check that all is ok first ... */
2265 if (!context || !context->internal ||
2266 !context->internal->_initialized) {
2269 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
2274 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2277 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
2282 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2285 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
2290 if (!dir->dir_next) {
2294 dirent = dir->dir_next->dirent;
2302 dirp = (struct smbc_dirent *)context->internal->_dirent;
2303 maxlen = (sizeof(context->internal->_dirent) -
2304 sizeof(struct smbc_dirent));
2306 smbc_readdir_internal(context, dirp, dirent, maxlen);
2308 dir->dir_next = dir->dir_next->next;
2314 * Routine to get directory entries
2317 static int smbc_getdents_ctx(SMBCCTX *context,
2319 struct smbc_dirent *dirp,
2325 char *ndir = (char *)dirp;
2326 struct smbc_dir_list *dirlist;
2328 /* Check that all is ok first ... */
2330 if (!context || !context->internal ||
2331 !context->internal->_initialized) {
2338 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2345 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2353 * Now, retrieve the number of entries that will fit in what was passed
2354 * We have to figure out if the info is in the list, or we need to
2355 * send a request to the server to get the info.
2358 while ((dirlist = dir->dir_next)) {
2359 struct smbc_dirent *dirent;
2361 if (!dirlist->dirent) {
2363 errno = ENOENT; /* Bad error */
2368 /* Do urlencoding of next entry, if so selected */
2369 dirent = (struct smbc_dirent *)context->internal->_dirent;
2370 maxlen = (sizeof(context->internal->_dirent) -
2371 sizeof(struct smbc_dirent));
2372 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
2374 reqd = dirent->dirlen;
2378 if (rem < count) { /* We managed to copy something */
2384 else { /* Nothing copied ... */
2386 errno = EINVAL; /* Not enough space ... */
2393 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
2395 ((struct smbc_dirent *)ndir)->comment =
2396 (char *)(&((struct smbc_dirent *)ndir)->name +
2404 dir->dir_next = dirlist = dirlist -> next;
2415 * Routine to create a directory ...
2418 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
2421 fstring server, share, user, password, workgroup;
2424 if (!context || !context->internal ||
2425 !context->internal->_initialized) {
2439 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
2441 if (smbc_parse_path(context, fname,
2442 server, sizeof(server),
2443 share, sizeof(share),
2446 password, sizeof(password),
2452 if (user[0] == (char)0) fstrcpy(user, context->user);
2454 fstrcpy(workgroup, context->workgroup);
2456 srv = smbc_server(context, server, share, workgroup, user, password);
2460 return -1; /* errno set by smbc_server */
2464 if (!cli_mkdir(&srv->cli, path)) {
2466 errno = smbc_errno(context, &srv->cli);
2476 * Our list function simply checks to see if a directory is not empty
2479 static int smbc_rmdir_dirempty = True;
2481 static void rmdir_list_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
2484 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2485 smbc_rmdir_dirempty = False;
2490 * Routine to remove a directory
2493 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
2496 fstring server, share, user, password, workgroup;
2499 if (!context || !context->internal ||
2500 !context->internal->_initialized) {
2514 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2516 if (smbc_parse_path(context, fname,
2517 server, sizeof(server),
2518 share, sizeof(share),
2521 password, sizeof(password),
2528 if (user[0] == (char)0) fstrcpy(user, context->user);
2530 fstrcpy(workgroup, context->workgroup);
2532 srv = smbc_server(context, server, share, workgroup, user, password);
2536 return -1; /* errno set by smbc_server */
2540 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2542 mode = aDIR | aRONLY;
2545 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2547 if (strcmp(path, "\\") == 0) {
2549 mode = aDIR | aRONLY;
2555 smbc_stat_printjob(srv, path, &size, &m_time);
2556 c_time = a_time = m_time;
2561 if (!cli_rmdir(&srv->cli, path)) {
2563 errno = smbc_errno(context, &srv->cli);
2565 if (errno == EACCES) { /* Check if the dir empty or not */
2567 pstring lpath; /* Local storage to avoid buffer overflows */
2569 smbc_rmdir_dirempty = True; /* Make this so ... */
2571 pstrcpy(lpath, path);
2572 pstrcat(lpath, "\\*");
2574 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2577 /* Fix errno to ignore latest error ... */
2579 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2580 smbc_errno(context, &srv->cli)));
2585 if (smbc_rmdir_dirempty)
2601 * Routine to return the current directory position
2604 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2606 off_t ret_val; /* Squash warnings about cast */
2608 if (!context || !context->internal ||
2609 !context->internal->_initialized) {
2616 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2623 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2631 * We return the pointer here as the offset
2633 ret_val = (int)dir->dir_next;
2639 * A routine to run down the list and see if the entry is OK
2642 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2643 struct smbc_dirent *dirent)
2646 /* Run down the list looking for what we want */
2650 struct smbc_dir_list *tmp = list;
2654 if (tmp->dirent == dirent)
2663 return NULL; /* Not found, or an error */
2669 * Routine to seek on a directory
2672 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2674 long int l_offset = offset; /* Handle problems of size */
2675 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
2676 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
2678 if (!context || !context->internal ||
2679 !context->internal->_initialized) {
2686 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2693 /* Now, check what we were passed and see if it is OK ... */
2695 if (dirent == NULL) { /* Seek to the begining of the list */
2697 dir->dir_next = dir->dir_list;
2702 /* Now, run down the list and make sure that the entry is OK */
2703 /* This may need to be changed if we change the format of the list */
2705 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2707 errno = EINVAL; /* Bad entry */
2712 dir->dir_next = list_ent;
2719 * Routine to fstat a dir
2722 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2725 if (!context || !context->internal ||
2726 !context->internal->_initialized) {
2733 /* No code yet ... */
2739 int smbc_chmod_ctx(SMBCCTX *context, const char *fname, mode_t newmode)
2742 fstring server, share, user, password, workgroup;
2746 if (!context || !context->internal ||
2747 !context->internal->_initialized) {
2749 errno = EINVAL; /* Best I can think of ... */
2761 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
2763 if (smbc_parse_path(context, fname,
2764 server, sizeof(server),
2765 share, sizeof(share),
2768 password, sizeof(password),
2774 if (user[0] == (char)0) fstrcpy(user, context->user);
2776 fstrcpy(workgroup, context->workgroup);
2778 srv = smbc_server(context, server, share, workgroup, user, password);
2781 return -1; /* errno set by smbc_server */
2786 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
2787 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
2788 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
2789 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
2791 if (!cli_setatr(&srv->cli, path, mode, 0)) {
2792 errno = smbc_errno(context, &srv->cli);
2799 int smbc_utimes_ctx(SMBCCTX *context, const char *fname, struct timeval *tbuf)
2802 fstring server, share, user, password, workgroup;
2805 time_t t = (tbuf == NULL ? time(NULL) : tbuf->tv_sec);
2807 if (!context || !context->internal ||
2808 !context->internal->_initialized) {
2810 errno = EINVAL; /* Best I can think of ... */
2822 DEBUG(4, ("smbc_utimes(%s, [%s])\n", fname, ctime(&t)));
2824 if (smbc_parse_path(context, fname,
2825 server, sizeof(server),
2826 share, sizeof(share),
2829 password, sizeof(password),
2835 if (user[0] == (char)0) fstrcpy(user, context->user);
2837 fstrcpy(workgroup, context->workgroup);
2839 srv = smbc_server(context, server, share, workgroup, user, password);
2842 return -1; /* errno set by smbc_server */
2845 if (!smbc_getatr(context, srv, path,
2852 if (!cli_setatr(&srv->cli, path, mode, t)) {
2853 /* some servers always refuse directory changes */
2854 if (!(mode & aDIR)) {
2855 errno = smbc_errno(context, &srv->cli);
2864 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
2865 However NT4 gives a "The information may have been modified by a
2866 computer running Windows NT 5.0" if denied ACEs do not appear before
2869 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
2871 if (sec_ace_equal(ace1, ace2))
2874 if (ace1->type != ace2->type)
2875 return ace2->type - ace1->type;
2877 if (sid_compare(&ace1->trustee, &ace2->trustee))
2878 return sid_compare(&ace1->trustee, &ace2->trustee);
2880 if (ace1->flags != ace2->flags)
2881 return ace1->flags - ace2->flags;
2883 if (ace1->info.mask != ace2->info.mask)
2884 return ace1->info.mask - ace2->info.mask;
2886 if (ace1->size != ace2->size)
2887 return ace1->size - ace2->size;
2889 return memcmp(ace1, ace2, sizeof(SEC_ACE));
2893 static void sort_acl(SEC_ACL *the_acl)
2896 if (!the_acl) return;
2898 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
2900 for (i=1;i<the_acl->num_aces;) {
2901 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
2903 for (j=i; j<the_acl->num_aces-1; j++) {
2904 the_acl->ace[j] = the_acl->ace[j+1];
2906 the_acl->num_aces--;
2913 /* convert a SID to a string, either numeric or username/group */
2914 static void convert_sid_to_string(struct cli_state *ipc_cli,
2920 char **domains = NULL;
2921 char **names = NULL;
2922 uint32 *types = NULL;
2924 sid_to_string(str, sid);
2926 if (numeric) return; /* no lookup desired */
2928 /* Ask LSA to convert the sid to a name */
2930 if (!NT_STATUS_IS_OK(cli_lsa_lookup_sids(ipc_cli, ipc_cli->mem_ctx,
2931 pol, 1, sid, &domains,
2933 !domains || !domains[0] || !names || !names[0]) {
2939 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
2940 domains[0], lp_winbind_separator(),
2944 /* convert a string to a SID, either numeric or username/group */
2945 static BOOL convert_string_to_sid(struct cli_state *ipc_cli,
2951 uint32 *types = NULL;
2952 DOM_SID *sids = NULL;
2956 if (strncmp(str, "S-", 2) == 0) {
2957 return string_to_sid(sid, str);
2964 if (!NT_STATUS_IS_OK(cli_lsa_lookup_names(ipc_cli, ipc_cli->mem_ctx,
2965 pol, 1, &str, &sids,
2971 sid_copy(sid, &sids[0]);
2978 /* parse an ACE in the same format as print_ace() */
2979 static BOOL parse_ace(struct cli_state *ipc_cli,
2988 unsigned atype, aflags, amask;
2991 const struct perm_value *v;
2997 /* These values discovered by inspection */
2998 static const struct perm_value special_values[] = {
2999 { "R", 0x00120089 },
3000 { "W", 0x00120116 },
3001 { "X", 0x001200a0 },
3002 { "D", 0x00010000 },
3003 { "P", 0x00040000 },
3004 { "O", 0x00080000 },
3008 static const struct perm_value standard_values[] = {
3009 { "READ", 0x001200a9 },
3010 { "CHANGE", 0x001301bf },
3011 { "FULL", 0x001f01ff },
3017 p = strchr_m(str,':');
3018 if (!p) return False;
3021 /* Try to parse numeric form */
3023 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3024 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3028 /* Try to parse text form */
3030 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3035 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3039 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3040 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3041 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3042 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3047 /* Only numeric form accepted for flags at present */
3049 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3050 sscanf(tok, "%i", &aflags))) {
3054 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3058 if (strncmp(tok, "0x", 2) == 0) {
3059 if (sscanf(tok, "%i", &amask) != 1) {
3065 for (v = standard_values; v->perm; v++) {
3066 if (strcmp(tok, v->perm) == 0) {
3077 for (v = special_values; v->perm; v++) {
3078 if (v->perm[0] == *p) {
3084 if (!found) return False;
3094 init_sec_ace(ace, &sid, atype, mask, aflags);
3098 /* add an ACE to a list of ACEs in a SEC_ACL */
3099 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace, TALLOC_CTX *ctx)
3104 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
3108 aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces);
3109 memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
3110 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
3111 new = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
3118 /* parse a ascii version of a security descriptor */
3119 static SEC_DESC *sec_desc_parse(TALLOC_CTX *ctx,
3120 struct cli_state *ipc_cli,
3125 const char *p = str;
3129 DOM_SID *grp_sid=NULL, *owner_sid=NULL;
3133 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
3135 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
3136 revision = strtol(tok+9, NULL, 16);
3140 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
3141 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3143 !convert_string_to_sid(ipc_cli, pol,
3145 owner_sid, tok+6)) {
3146 DEBUG(5, ("Failed to parse owner sid\n"));
3152 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
3153 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3155 !convert_string_to_sid(ipc_cli, pol,
3157 owner_sid, tok+7)) {
3158 DEBUG(5, ("Failed to parse owner sid\n"));
3164 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
3165 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3167 !convert_string_to_sid(ipc_cli, pol,
3170 DEBUG(5, ("Failed to parse group sid\n"));
3176 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
3177 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3179 !convert_string_to_sid(ipc_cli, pol,
3182 DEBUG(5, ("Failed to parse group sid\n"));
3188 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
3190 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
3191 DEBUG(5, ("Failed to parse ACL %s\n", tok));
3194 if(!add_ace(&dacl, &ace, ctx)) {
3195 DEBUG(5, ("Failed to add ACL %s\n", tok));
3201 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
3203 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
3204 DEBUG(5, ("Failed to parse ACL %s\n", tok));
3207 if(!add_ace(&dacl, &ace, ctx)) {
3208 DEBUG(5, ("Failed to add ACL %s\n", tok));
3214 DEBUG(5, ("Failed to parse security descriptor\n"));
3218 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
3219 owner_sid, grp_sid, NULL, dacl, &sd_size);
3222 SAFE_FREE(owner_sid);
3228 /* Obtain the current dos attributes */
3229 static DOS_ATTR_DESC *dos_attr_query(SMBCCTX *context,
3231 const char *filename,
3234 time_t m_time = 0, a_time = 0, c_time = 0;
3237 SMB_INO_T inode = 0;
3240 ret = talloc(ctx, sizeof(DOS_ATTR_DESC));
3246 /* Obtain the DOS attributes */
3247 if (!smbc_getatr(context, srv, (char *) filename, &mode, &size,
3248 &c_time, &a_time, &m_time, &inode)) {
3250 errno = smbc_errno(context, &srv->cli);
3251 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
3258 ret->a_time = a_time;
3259 ret->c_time = c_time;
3260 ret->m_time = m_time;
3267 /* parse a ascii version of a security descriptor */
3268 static void dos_attr_parse(SMBCCTX *context,
3273 const char *p = str;
3276 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
3278 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
3279 dad->mode = strtol(tok+5, NULL, 16);
3283 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
3284 dad->size = strtoll(tok+5, NULL, 10);
3288 if (StrnCaseCmp(tok, "A_TIME:", 7) == 0) {
3289 dad->a_time = strtoll(tok+7, NULL, 10);
3293 if (StrnCaseCmp(tok, "C_TIME:", 7) == 0) {
3294 dad->c_time = strtoll(tok+7, NULL, 10);
3298 if (StrnCaseCmp(tok, "M_TIME:", 7) == 0) {
3299 dad->m_time = strtoll(tok+7, NULL, 10);
3303 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
3304 dad->inode = strtoll(tok+6, NULL, 10);
3311 /*****************************************************
3312 retrieve the acls for a file
3313 *******************************************************/
3314 static int cacl_get(SMBCCTX *context, TALLOC_CTX *ctx, SMBCSRV *srv,
3315 struct cli_state *ipc_cli, POLICY_HND *pol,
3316 char *filename, char *name, char *buf, int bufsize)
3326 BOOL numeric = True;
3327 BOOL determine_size = (bufsize == 0);
3332 time_t m_time = 0, a_time = 0, c_time = 0;
3336 struct cli_state *cli = &srv->cli;
3338 all = (StrnCaseCmp(name, "system.*", 8) == 0);
3339 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
3340 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
3341 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
3342 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
3343 numeric = (* (name + strlen(name) - 1) != '+');
3348 * If we are (possibly) talking to an NT or new system and some NT
3349 * attributes have been requested...
3351 if (ipc_cli && (all || some_nt)) {
3352 /* Point to the portion after "system.nt_sec_desc." */
3353 name += 19; /* if (all) this will be invalid but unused */
3355 /* ... then obtain any NT attributes which were requested */
3356 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
3359 DEBUG(5, ("cacl_get failed to open %s: %s\n",
3360 filename, cli_errstr(cli)));
3365 sd = cli_query_secdesc(cli, fnum, ctx);
3369 ("cacl_get Failed to query old descriptor\n"));
3374 cli_close(cli, fnum);
3376 if (all || all_nt) {
3377 if (determine_size) {
3378 p = talloc_asprintf(ctx,
3387 n = snprintf(buf, bufsize,
3388 "REVISION:%d", sd->revision);
3390 } else if (StrCaseCmp(name, "revision") == 0) {
3391 if (determine_size) {
3392 p = talloc_asprintf(ctx, "%d", sd->revision);
3399 n = snprintf(buf, bufsize, "%d", sd->revision);
3403 if (!determine_size && n > bufsize) {
3411 /* Get owner and group sid */
3413 if (sd->owner_sid) {
3414 convert_sid_to_string(ipc_cli, pol,
3415 sidstr, numeric, sd->owner_sid);
3417 fstrcpy(sidstr, "");
3420 if (all || all_nt) {
3421 if (determine_size) {
3422 p = talloc_asprintf(ctx, ",OWNER:%s", sidstr);
3429 n = snprintf(buf, bufsize,
3430 ",OWNER:%s", sidstr);
3432 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
3433 if (determine_size) {
3434 p = talloc_asprintf(ctx, "%s", sidstr);
3441 n = snprintf(buf, bufsize, "%s", sidstr);
3445 if (!determine_size && n > bufsize) {
3454 convert_sid_to_string(ipc_cli, pol,
3455 sidstr, numeric, sd->grp_sid);
3457 fstrcpy(sidstr, "");
3460 if (all || all_nt) {
3461 if (determine_size) {
3462 p = talloc_asprintf(ctx, ",GROUP:%s", sidstr);
3469 n = snprintf(buf, bufsize,
3470 ",GROUP:%s", sidstr);
3472 } else if (StrnCaseCmp(name, "group", 5) == 0) {
3473 if (determine_size) {
3474 p = talloc_asprintf(ctx, "%s", sidstr);
3481 n = snprintf(buf, bufsize, "%s", sidstr);
3485 if (!determine_size && n > bufsize) {
3493 /* Add aces to value buffer */
3494 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
3496 SEC_ACE *ace = &sd->dacl->ace[i];
3497 convert_sid_to_string(ipc_cli, pol,
3498 sidstr, numeric, &ace->trustee);
3500 if (all || all_nt) {
3501 if (determine_size) {
3502 p = talloc_asprintf(ctx,
3515 n = snprintf(buf, bufsize,
3516 ",ACL:%s:%d/%d/0x%08x",
3522 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
3523 StrCaseCmp(name + 3, sidstr) == 0) ||
3524 (StrnCaseCmp(name, "acl+", 4) == 0 &&
3525 StrCaseCmp(name + 4, sidstr) == 0)) {
3526 if (determine_size) {
3527 p = talloc_asprintf(ctx,
3538 n = snprintf(buf, bufsize,
3554 /* Restore name pointer to its original value */
3558 if (all || some_dos) {
3559 /* Point to the portion after "system.dos_attr." */
3560 name += 16; /* if (all) this will be invalid but unused */
3562 /* Obtain the DOS attributes */
3563 if (!smbc_getatr(context, srv, filename, &mode, &size,
3564 &c_time, &a_time, &m_time, &ino)) {
3566 errno = smbc_errno(context, &srv->cli);
3571 if (all || all_dos) {
3572 if (determine_size) {
3573 p = talloc_asprintf(ctx,
3586 n = snprintf(buf, bufsize,
3594 } else if (StrCaseCmp(name, "mode") == 0) {
3595 if (determine_size) {
3596 p = talloc_asprintf(ctx, "0x%x", mode);
3603 n = snprintf(buf, bufsize, "0x%x", mode);
3607 if (!determine_size && n > bufsize) {
3615 if (all || all_dos) {
3616 if (determine_size) {
3617 p = talloc_asprintf(ctx,
3619 (unsigned long long) size);
3626 n = snprintf(buf, bufsize,
3628 (unsigned long long) size);
3630 } else if (StrCaseCmp(name, "size") == 0) {
3631 if (determine_size) {
3632 p = talloc_asprintf(ctx,
3634 (unsigned long long) size);
3641 n = snprintf(buf, bufsize,
3643 (unsigned long long) size);
3647 if (!determine_size && n > bufsize) {
3655 if (all || all_dos) {
3656 if (determine_size) {
3657 p = talloc_asprintf(ctx,
3658 ",C_TIME:%lu", c_time);
3665 n = snprintf(buf, bufsize,
3666 ",C_TIME:%lu", c_time);
3668 } else if (StrCaseCmp(name, "c_time") == 0) {
3669 if (determine_size) {
3670 p = talloc_asprintf(ctx, "%lu", c_time);
3677 n = snprintf(buf, bufsize, "%lu", c_time);
3681 if (!determine_size && n > bufsize) {
3689 if (all || all_dos) {
3690 if (determine_size) {
3691 p = talloc_asprintf(ctx,
3692 ",A_TIME:%lu", a_time);
3699 n = snprintf(buf, bufsize,
3700 ",A_TIME:%lu", a_time);
3702 } else if (StrCaseCmp(name, "a_time") == 0) {
3703 if (determine_size) {
3704 p = talloc_asprintf(ctx, "%lu", a_time);
3711 n = snprintf(buf, bufsize, "%lu", a_time);
3715 if (!determine_size && n > bufsize) {
3723 if (all || all_dos) {
3724 if (determine_size) {
3725 p = talloc_asprintf(ctx,
3726 ",M_TIME:%lu", m_time);
3733 n = snprintf(buf, bufsize,
3734 ",M_TIME:%lu", m_time);
3736 } else if (StrCaseCmp(name, "m_time") == 0) {
3737 if (determine_size) {
3738 p = talloc_asprintf(ctx, "%lu", m_time);
3745 n = snprintf(buf, bufsize, "%lu", m_time);
3749 if (!determine_size && n > bufsize) {
3757 if (all || all_dos) {
3758 if (determine_size) {
3759 p = talloc_asprintf(ctx,
3761 (unsigned long long) ino);
3768 n = snprintf(buf, bufsize,
3770 (unsigned long long) ino);
3772 } else if (StrCaseCmp(name, "inode") == 0) {
3773 if (determine_size) {
3774 p = talloc_asprintf(ctx,
3776 (unsigned long long) ino);
3783 n = snprintf(buf, bufsize,
3785 (unsigned long long) ino);
3789 if (!determine_size && n > bufsize) {
3797 /* Restore name pointer to its original value */
3810 /*****************************************************
3811 set the ACLs on a file given an ascii description
3812 *******************************************************/
3813 static int cacl_set(TALLOC_CTX *ctx, struct cli_state *cli,
3814 struct cli_state *ipc_cli, POLICY_HND *pol,
3815 const char *filename, const char *the_acl,
3816 int mode, int flags)
3820 SEC_DESC *sd = NULL, *old;
3821 SEC_ACL *dacl = NULL;
3822 DOM_SID *owner_sid = NULL;
3823 DOM_SID *grp_sid = NULL;
3828 BOOL numeric = True;
3830 /* the_acl will be null for REMOVE_ALL operations */
3832 numeric = ((p = strchr(the_acl, ':')) != NULL &&
3836 /* if this is to set the entire ACL... */
3837 if (*the_acl == '*') {
3838 /* ... then increment past the first colon */
3842 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric, (char *) the_acl);
3850 /* The desired access below is the only one I could find that works
3851 with NT4, W2KP and Samba */
3853 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
3856 DEBUG(5, ("cacl_set failed to open %s: %s\n",
3857 filename, cli_errstr(cli)));
3862 old = cli_query_secdesc(cli, fnum, ctx);
3865 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
3870 cli_close(cli, fnum);
3873 case SMBC_XATTR_MODE_REMOVE_ALL:
3874 old->dacl->num_aces = 0;
3875 SAFE_FREE(old->dacl->ace);
3876 SAFE_FREE(old->dacl);
3881 case SMBC_XATTR_MODE_REMOVE:
3882 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
3885 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
3886 if (sec_ace_equal(&sd->dacl->ace[i],
3887 &old->dacl->ace[j])) {
3889 for (k=j; k<old->dacl->num_aces-1;k++) {
3890 old->dacl->ace[k] = old->dacl->ace[k+1];
3892 old->dacl->num_aces--;
3893 if (old->dacl->num_aces == 0) {
3894 SAFE_FREE(old->dacl->ace);
3895 SAFE_FREE(old->dacl);
3912 case SMBC_XATTR_MODE_ADD:
3913 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
3916 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
3917 if (sid_equal(&sd->dacl->ace[i].trustee,
3918 &old->dacl->ace[j].trustee)) {
3919 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
3924 old->dacl->ace[j] = sd->dacl->ace[i];
3930 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
3936 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
3937 add_ace(&old->dacl, &sd->dacl->ace[i], ctx);
3943 case SMBC_XATTR_MODE_SET:
3945 owner_sid = old->owner_sid;
3946 grp_sid = old->grp_sid;
3950 case SMBC_XATTR_MODE_CHOWN:
3951 owner_sid = sd->owner_sid;
3954 case SMBC_XATTR_MODE_CHGRP:
3955 grp_sid = sd->grp_sid;
3959 /* Denied ACE entries must come before allowed ones */
3960 sort_acl(old->dacl);
3962 /* Create new security descriptor and set it */
3963 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
3964 owner_sid, grp_sid, NULL, dacl, &sd_size);
3966 fnum = cli_nt_create(cli, filename,
3967 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
3970 DEBUG(5, ("cacl_set failed to open %s: %s\n",
3971 filename, cli_errstr(cli)));
3976 if (!cli_set_secdesc(cli, fnum, sd)) {
3977 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
3984 cli_close(cli, fnum);
3994 int smbc_setxattr_ctx(SMBCCTX *context,
4005 fstring server, share, user, password, workgroup;
4011 if (!context || !context->internal ||
4012 !context->internal->_initialized) {
4014 errno = EINVAL; /* Best I can think of ... */
4026 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n", fname, name, (int) size, (const char*)value));
4028 if (smbc_parse_path(context, fname,
4029 server, sizeof(server),
4030 share, sizeof(share),
4033 password, sizeof(password),
4039 if (user[0] == (char)0) fstrcpy(user, context->user);
4041 fstrcpy(workgroup, context->workgroup);
4043 srv = smbc_server(context, server, share, workgroup, user, password);
4045 return -1; /* errno set by smbc_server */
4048 if (! srv->no_nt_session) {
4049 ipc_srv = smbc_attr_server(context, server, share,
4050 workgroup, user, password,
4052 srv->no_nt_session = True;
4057 ctx = talloc_init("smbc_setxattr");
4064 * Are they asking to set the entire set of known attributes?
4066 if (StrCaseCmp(name, "system.*") == 0 ||
4067 StrCaseCmp(name, "system.*+") == 0) {
4070 talloc_asprintf(ctx, "%s:%s", name+7, (const char *) value);
4078 ret = cacl_set(ctx, &srv->cli,
4079 &ipc_srv->cli, &pol, path,
4082 ? SMBC_XATTR_MODE_SET
4083 : SMBC_XATTR_MODE_ADD),
4089 /* get a DOS Attribute Descriptor with current attributes */
4090 dad = dos_attr_query(context, ctx, path, srv);
4092 /* Overwrite old with new, using what was provided */
4093 dos_attr_parse(context, dad, srv, namevalue);
4095 /* Set the new DOS attributes */
4096 #if 0 /* not yet implemented */
4097 if (! cli_setpathinfo(&srv->cli, path,
4102 if (!cli_setatr(&srv->cli, path,
4103 dad->mode, dad->m_time)) {
4104 errno = smbc_errno(context, &srv->cli);
4108 if (!cli_setatr(&srv->cli, path,
4109 dad->mode, dad->m_time)) {
4110 errno = smbc_errno(context, &srv->cli);
4115 /* we only fail if both NT and DOS sets failed */
4116 if (ret < 0 && ! dad) {
4117 ret = -1; /* in case dad was null */
4123 talloc_destroy(ctx);
4128 * Are they asking to set an access control element or to set
4129 * the entire access control list?
4131 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4132 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
4133 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4134 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4135 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
4139 talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4142 ret = -1; /* errno set by smbc_server() */
4144 else if (! namevalue) {
4148 ret = cacl_set(ctx, &srv->cli,
4149 &ipc_srv->cli, &pol, path,
4152 ? SMBC_XATTR_MODE_SET
4153 : SMBC_XATTR_MODE_ADD),
4156 talloc_destroy(ctx);
4161 * Are they asking to set the owner?
4163 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4164 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
4168 talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4172 ret = -1; /* errno set by smbc_server() */
4174 else if (! namevalue) {
4178 ret = cacl_set(ctx, &srv->cli,
4179 &ipc_srv->cli, &pol, path,
4180 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
4182 talloc_destroy(ctx);
4187 * Are they asking to set the group?
4189 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4190 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
4194 talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4197 /* errno set by smbc_server() */
4200 else if (! namevalue) {
4204 ret = cacl_set(ctx, &srv->cli,
4205 &ipc_srv->cli, &pol, path,
4206 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
4208 talloc_destroy(ctx);
4213 * Are they asking to set a DOS attribute?
4215 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
4216 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
4217 StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
4218 StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
4219 StrCaseCmp(name, "system.dos_attr.m_time") == 0) {
4221 /* get a DOS Attribute Descriptor with current attributes */
4222 dad = dos_attr_query(context, ctx, path, srv);
4225 talloc_asprintf(ctx, "%s:%s", name+16, (const char *) value);
4230 /* Overwrite old with provided new params */
4231 dos_attr_parse(context, dad, srv, namevalue);
4233 /* Set the new DOS attributes */
4234 #if 0 /* not yet implemented */
4235 ret2 = cli_setpathinfo(&srv->cli, path,
4241 ret2 = cli_setatr(&srv->cli, path,
4245 errno = smbc_errno(context,
4250 ret2 = cli_setatr(&srv->cli, path,
4251 dad->mode, dad->m_time);
4253 errno = smbc_errno(context, &srv->cli);
4257 /* ret2 has True (success) / False (failure) */
4268 talloc_destroy(ctx);
4272 /* Unsupported attribute name */
4273 talloc_destroy(ctx);
4278 int smbc_getxattr_ctx(SMBCCTX *context,
4287 fstring server, share, user, password, workgroup;
4293 if (!context || !context->internal ||
4294 !context->internal->_initialized) {
4296 errno = EINVAL; /* Best I can think of ... */
4308 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
4310 if (smbc_parse_path(context, fname,
4311 server, sizeof(server),
4312 share, sizeof(share),
4315 password, sizeof(password),
4321 if (user[0] == (char)0) fstrcpy(user, context->user);
4323 fstrcpy(workgroup, context->workgroup);
4325 srv = smbc_server(context, server, share, workgroup, user, password);
4327 return -1; /* errno set by smbc_server */
4330 if (! srv->no_nt_session) {
4331 ipc_srv = smbc_attr_server(context, server, share,
4332 workgroup, user, password,
4335 srv->no_nt_session = True;
4341 ctx = talloc_init("smbc:getxattr");
4347 /* Are they requesting a supported attribute? */
4348 if (StrCaseCmp(name, "system.*") == 0 ||
4349 StrCaseCmp(name, "system.*+") == 0 ||
4350 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4351 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
4352 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4353 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4354 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
4355 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4356 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
4357 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4358 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
4359 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
4360 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
4361 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
4362 StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
4363 StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
4364 StrCaseCmp(name, "system.dos_attr.m_time") == 0 ||
4365 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
4368 ret = cacl_get(context, ctx, srv,
4369 ipc_srv == NULL ? NULL : &ipc_srv->cli,
4370 &pol, path, (char*)name, (char *) value, size);
4371 if (ret < 0 && errno == 0) {
4372 errno = smbc_errno(context, &srv->cli);
4374 talloc_destroy(ctx);
4378 /* Unsupported attribute name */
4379 talloc_destroy(ctx);
4385 int smbc_removexattr_ctx(SMBCCTX *context,
4392 fstring server, share, user, password, workgroup;
4397 if (!context || !context->internal ||
4398 !context->internal->_initialized) {
4400 errno = EINVAL; /* Best I can think of ... */
4412 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
4414 if (smbc_parse_path(context, fname,
4415 server, sizeof(server),
4416 share, sizeof(share),
4419 password, sizeof(password),
4425 if (user[0] == (char)0) fstrcpy(user, context->user);
4427 fstrcpy(workgroup, context->workgroup);
4429 srv = smbc_server(context, server, share, workgroup, user, password);
4431 return -1; /* errno set by smbc_server */
4434 if (! srv->no_nt_session) {
4435 ipc_srv = smbc_attr_server(context, server, share,
4436 workgroup, user, password,
4438 srv->no_nt_session = True;
4444 return -1; /* errno set by smbc_attr_server */
4447 ctx = talloc_init("smbc_removexattr");
4453 /* Are they asking to set the entire ACL? */
4454 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4455 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
4458 ret = cacl_set(ctx, &srv->cli,
4459 &ipc_srv->cli, &pol, path,
4460 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
4461 talloc_destroy(ctx);
4466 * Are they asking to remove one or more spceific security descriptor
4469 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4470 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4471 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
4472 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4473 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
4474 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4475 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
4478 ret = cacl_set(ctx, &srv->cli,
4479 &ipc_srv->cli, &pol, path,
4480 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
4481 talloc_destroy(ctx);
4485 /* Unsupported attribute name */
4486 talloc_destroy(ctx);
4491 int smbc_listxattr_ctx(SMBCCTX *context,
4497 * This isn't quite what listxattr() is supposed to do. This returns
4498 * the complete set of attribute names, always, rather than only those
4499 * attribute names which actually exist for a file. Hmmm...
4501 const char supported[] =
4504 "system.nt_sec_desc.revision\0"
4505 "system.nt_sec_desc.owner\0"
4506 "system.nt_sec_desc.owner+\0"
4507 "system.nt_sec_desc.group\0"
4508 "system.nt_sec_desc.group+\0"
4509 "system.nt_sec_desc.acl\0"
4510 "system.nt_sec_desc.acl+\0"
4511 "system.nt_sec_desc.*\0"
4512 "system.nt_sec_desc.*+\0"
4513 "system.dos_attr.*\0"
4514 "system.dos_attr.mode\0"
4515 "system.dos_attr.c_time\0"
4516 "system.dos_attr.a_time\0"
4517 "system.dos_attr.m_time\0"
4521 return sizeof(supported);
4524 if (sizeof(supported) > size) {
4529 /* this can't be strcpy() because there are embedded null characters */
4530 memcpy(list, supported, sizeof(supported));
4531 return sizeof(supported);
4536 * Open a print file to be written to by other calls
4539 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
4541 fstring server, share, user, password;
4544 if (!context || !context->internal ||
4545 !context->internal->_initialized) {
4559 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
4561 if (smbc_parse_path(context, fname,
4562 server, sizeof(server),
4563 share, sizeof(share),
4566 password, sizeof(password),
4572 /* What if the path is empty, or the file exists? */
4574 return context->open(context, fname, O_WRONLY, 666);
4579 * Routine to print a file on a remote server ...
4581 * We open the file, which we assume to be on a remote server, and then
4582 * copy it to a print file on the share specified by printq.
4585 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
4587 SMBCFILE *fid1, *fid2;
4588 int bytes, saverr, tot_bytes = 0;
4591 if (!c_file || !c_file->internal->_initialized || !c_print ||
4592 !c_print->internal->_initialized) {
4599 if (!fname && !printq) {
4606 /* Try to open the file for reading ... */
4608 if ((int)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
4610 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
4611 return -1; /* smbc_open sets errno */
4615 /* Now, try to open the printer file for writing */
4617 if ((int)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
4619 saverr = errno; /* Save errno */
4620 c_file->close(c_file, fid1);
4626 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
4630 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
4633 c_file->close(c_file, fid1);
4634 c_print->close(c_print, fid2);
4643 c_file->close(c_file, fid1); /* We have to close these anyway */
4644 c_print->close(c_print, fid2);
4658 * Routine to list print jobs on a printer share ...
4661 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
4664 fstring server, share, user, password, workgroup;
4667 if (!context || !context->internal ||
4668 !context->internal->_initialized) {
4682 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
4684 if (smbc_parse_path(context, fname,
4685 server, sizeof(server),
4686 share, sizeof(share),
4689 password, sizeof(password),
4695 if (user[0] == (char)0) fstrcpy(user, context->user);
4697 fstrcpy(workgroup, context->workgroup);
4699 srv = smbc_server(context, server, share, workgroup, user, password);
4703 return -1; /* errno set by smbc_server */
4707 if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
4709 errno = smbc_errno(context, &srv->cli);
4719 * Delete a print job from a remote printer share
4722 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
4725 fstring server, share, user, password, workgroup;
4729 if (!context || !context->internal ||
4730 !context->internal->_initialized) {
4744 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
4746 if (smbc_parse_path(context, fname,
4747 server, sizeof(server),
4748 share, sizeof(share),
4751 password, sizeof(password),
4757 if (user[0] == (char)0) fstrcpy(user, context->user);
4759 fstrcpy(workgroup, context->workgroup);
4761 srv = smbc_server(context, server, share, workgroup, user, password);
4765 return -1; /* errno set by smbc_server */
4769 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
4772 errno = smbc_errno(context, &srv->cli);
4773 else if (err == ERRnosuchprintjob)
4784 * Get a new empty handle to fill in with your own info
4786 SMBCCTX * smbc_new_context(void)
4790 context = SMB_MALLOC_P(SMBCCTX);
4796 ZERO_STRUCTP(context);
4798 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
4799 if (!context->internal) {
4804 ZERO_STRUCTP(context->internal);
4807 /* ADD REASONABLE DEFAULTS */
4809 context->timeout = 20000; /* 20 seconds */
4811 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
4812 context->options.urlencode_readdir_entries = False;/* backward compat */
4813 context->options.one_share_per_server = False;/* backward compat */
4815 context->open = smbc_open_ctx;
4816 context->creat = smbc_creat_ctx;
4817 context->read = smbc_read_ctx;
4818 context->write = smbc_write_ctx;
4819 context->close = smbc_close_ctx;
4820 context->unlink = smbc_unlink_ctx;
4821 context->rename = smbc_rename_ctx;
4822 context->lseek = smbc_lseek_ctx;
4823 context->stat = smbc_stat_ctx;
4824 context->fstat = smbc_fstat_ctx;
4825 context->opendir = smbc_opendir_ctx;
4826 context->closedir = smbc_closedir_ctx;
4827 context->readdir = smbc_readdir_ctx;
4828 context->getdents = smbc_getdents_ctx;
4829 context->mkdir = smbc_mkdir_ctx;
4830 context->rmdir = smbc_rmdir_ctx;
4831 context->telldir = smbc_telldir_ctx;
4832 context->lseekdir = smbc_lseekdir_ctx;
4833 context->fstatdir = smbc_fstatdir_ctx;
4834 context->chmod = smbc_chmod_ctx;
4835 context->utimes = smbc_utimes_ctx;
4836 context->setxattr = smbc_setxattr_ctx;
4837 context->getxattr = smbc_getxattr_ctx;
4838 context->removexattr = smbc_removexattr_ctx;
4839 context->listxattr = smbc_listxattr_ctx;
4840 context->open_print_job = smbc_open_print_job_ctx;
4841 context->print_file = smbc_print_file_ctx;
4842 context->list_print_jobs = smbc_list_print_jobs_ctx;
4843 context->unlink_print_job = smbc_unlink_print_job_ctx;
4845 context->callbacks.check_server_fn = smbc_check_server;
4846 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
4848 smbc_default_cache_functions(context);
4856 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
4857 * and thus you'll be leaking memory if not handled properly.
4860 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
4869 DEBUG(1,("Performing aggressive shutdown.\n"));
4871 f = context->internal->_files;
4873 context->close(context, f);
4876 context->internal->_files = NULL;
4878 /* First try to remove the servers the nice way. */
4879 if (context->callbacks.purge_cached_fn(context)) {
4882 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
4883 s = context->internal->_servers;
4885 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n", s, s->cli.fd));
4886 cli_shutdown(&s->cli);
4887 context->callbacks.remove_cached_srv_fn(context, s);
4889 DLIST_REMOVE(context->internal->_servers, s);
4893 context->internal->_servers = NULL;
4897 /* This is the polite way */
4898 if (context->callbacks.purge_cached_fn(context)) {
4899 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
4903 if (context->internal->_servers) {
4904 DEBUG(1, ("Active servers in context, free_context failed.\n"));
4908 if (context->internal->_files) {
4909 DEBUG(1, ("Active files in context, free_context failed.\n"));
4915 /* Things we have to clean up */
4916 SAFE_FREE(context->workgroup);
4917 SAFE_FREE(context->netbios_name);
4918 SAFE_FREE(context->user);
4920 DEBUG(3, ("Context %p succesfully freed\n", context));
4921 SAFE_FREE(context->internal);
4928 * Initialise the library etc
4930 * We accept a struct containing handle information.
4931 * valid values for info->debug from 0 to 100,
4932 * and insist that info->fn must be non-null.
4934 SMBCCTX * smbc_init_context(SMBCCTX * context)
4938 char *user = NULL, *home = NULL;
4940 if (!context || !context->internal) {
4945 /* Do not initialise the same client twice */
4946 if (context->internal->_initialized) {
4950 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
4957 if (!smbc_initialized) {
4958 /* Do some library wide intialisations the first time we get called */
4960 /* Set this to what the user wants */
4961 DEBUGLEVEL = context->debug;
4963 setup_logging( "libsmbclient", True);
4965 /* Here we would open the smb.conf file if needed ... */
4967 home = getenv("HOME");
4969 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
4971 load_interfaces(); /* Load the list of interfaces ... */
4973 in_client = True; /* FIXME, make a param */
4975 if (!lp_load(conf, True, False, False)) {
4978 * Well, if that failed, try the dyn_CONFIGFILE
4979 * Which points to the standard locn, and if that
4980 * fails, silently ignore it and use the internal
4984 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
4985 DEBUG(5, ("Could not load either config file: %s or %s\n",
4986 conf, dyn_CONFIGFILE));
4990 reopen_logs(); /* Get logging working ... */
4993 * Block SIGPIPE (from lib/util_sock.c: write())
4994 * It is not needed and should not stop execution
4996 BlockSignals(True, SIGPIPE);
4998 /* Done with one-time initialisation */
4999 smbc_initialized = 1;
5003 if (!context->user) {
5005 * FIXME: Is this the best way to get the user info?
5007 user = getenv("USER");
5008 /* walk around as "guest" if no username can be found */
5009 if (!user) context->user = SMB_STRDUP("guest");
5010 else context->user = SMB_STRDUP(user);
5013 if (!context->netbios_name) {
5015 * We try to get our netbios name from the config. If that fails we fall
5016 * back on constructing our netbios name from our hostname etc
5018 if (global_myname()) {
5019 context->netbios_name = SMB_STRDUP(global_myname());
5023 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
5026 context->netbios_name = SMB_MALLOC(17);
5027 if (!context->netbios_name) {
5031 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
5035 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
5037 if (!context->workgroup) {
5038 if (lp_workgroup()) {
5039 context->workgroup = SMB_STRDUP(lp_workgroup());
5042 /* TODO: Think about a decent default workgroup */
5043 context->workgroup = SMB_STRDUP("samba");
5047 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
5049 /* shortest timeout is 1 second */
5050 if (context->timeout > 0 && context->timeout < 1000)
5051 context->timeout = 1000;
5054 * FIXME: Should we check the function pointers here?
5057 context->internal->_initialized = 1;
5063 /* Return the verion of samba, and thus libsmbclient */
5067 return samba_version_string();