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)
1020 if (!context || !context->internal ||
1021 !context->internal->_initialized) {
1028 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1030 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1037 /* Check that the buffer exists ... */
1046 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
1050 errno = smbc_errno(context, &file->srv->cli);
1055 file->offset += ret;
1057 DEBUG(4, (" --> %d\n", ret));
1059 return ret; /* Success, ret bytes of data ... */
1064 * Routine to write() a file ...
1067 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
1071 if (!context || !context->internal ||
1072 !context->internal->_initialized) {
1079 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1086 /* Check that the buffer exists ... */
1095 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
1099 errno = smbc_errno(context, &file->srv->cli);
1104 file->offset += ret;
1106 return ret; /* Success, 0 bytes of data ... */
1110 * Routine to close() a file ...
1113 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
1117 if (!context || !context->internal ||
1118 !context->internal->_initialized) {
1125 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1135 return context->closedir(context, file);
1139 if (!cli_close(&file->srv->cli, file->cli_fd)) {
1141 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1143 /* Deallocate slot and remove the server
1144 * from the server cache if unused */
1145 errno = smbc_errno(context, &file->srv->cli);
1147 DLIST_REMOVE(context->internal->_files, file);
1148 SAFE_FREE(file->fname);
1150 context->callbacks.remove_unused_server_fn(context, srv);
1156 DLIST_REMOVE(context->internal->_files, file);
1157 SAFE_FREE(file->fname);
1164 * Get info from an SMB server on a file. Use a qpathinfo call first
1165 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1167 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1168 uint16 *mode, size_t *size,
1169 time_t *c_time, time_t *a_time, time_t *m_time,
1173 if (!context || !context->internal ||
1174 !context->internal->_initialized) {
1181 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1183 if (!srv->no_pathinfo2 &&
1184 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1185 size, mode, ino)) return True;
1187 /* if this is NT then don't bother with the getatr */
1188 if (srv->cli.capabilities & CAP_NT_SMBS) {
1193 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1194 a_time = c_time = m_time;
1195 srv->no_pathinfo2 = True;
1205 * Routine to unlink() a file
1208 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
1210 fstring server, share, user, password, workgroup;
1212 SMBCSRV *srv = NULL;
1214 if (!context || !context->internal ||
1215 !context->internal->_initialized) {
1217 errno = EINVAL; /* Best I can think of ... */
1229 if (smbc_parse_path(context, fname,
1230 server, sizeof(server),
1231 share, sizeof(share),
1234 password, sizeof(password),
1240 if (user[0] == (char)0) fstrcpy(user, context->user);
1242 fstrcpy(workgroup, context->workgroup);
1244 srv = smbc_server(context, server, share, workgroup, user, password);
1248 return -1; /* smbc_server sets errno */
1252 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1254 int job = smbc_stat_printjob(srv, path, NULL, NULL);
1260 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
1268 if (!cli_unlink(&srv->cli, path)) {
1270 errno = smbc_errno(context, &srv->cli);
1272 if (errno == EACCES) { /* Check if the file is a directory */
1277 time_t m_time = 0, a_time = 0, c_time = 0;
1280 if (!smbc_getatr(context, srv, path, &mode, &size,
1281 &c_time, &a_time, &m_time, &ino)) {
1283 /* Hmmm, bad error ... What? */
1285 errno = smbc_errno(context, &srv->cli);
1291 if (IS_DOS_DIR(mode))
1294 errno = saverr; /* Restore this */
1303 return 0; /* Success ... */
1308 * Routine to rename() a file
1311 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
1312 SMBCCTX *ncontext, const char *nname)
1314 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
1315 pstring path1, path2;
1316 SMBCSRV *srv = NULL;
1318 if (!ocontext || !ncontext ||
1319 !ocontext->internal || !ncontext->internal ||
1320 !ocontext->internal->_initialized ||
1321 !ncontext->internal->_initialized) {
1323 errno = EINVAL; /* Best I can think of ... */
1328 if (!oname || !nname) {
1335 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1337 smbc_parse_path(ocontext, oname,
1338 server1, sizeof(server1),
1339 share1, sizeof(share1),
1340 path1, sizeof(path1),
1341 user1, sizeof(user1),
1342 password1, sizeof(password1),
1345 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1347 smbc_parse_path(ncontext, nname,
1348 server2, sizeof(server2),
1349 share2, sizeof(share2),
1350 path2, sizeof(path2),
1351 user2, sizeof(user2),
1352 password2, sizeof(password2),
1355 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1357 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1358 strcmp(user1, user2)) {
1360 /* Can't rename across file systems, or users?? */
1367 fstrcpy(workgroup, ocontext->workgroup);
1368 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1369 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
1376 if (!cli_rename(&srv->cli, path1, path2)) {
1377 int eno = smbc_errno(ocontext, &srv->cli);
1379 if (eno != EEXIST ||
1380 !cli_unlink(&srv->cli, path2) ||
1381 !cli_rename(&srv->cli, path1, path2)) {
1389 return 0; /* Success */
1394 * A routine to lseek() a file
1397 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
1401 if (!context || !context->internal ||
1402 !context->internal->_initialized) {
1409 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1419 return -1; /* Can't lseek a dir ... */
1425 file->offset = offset;
1429 file->offset += offset;
1433 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1436 SMB_BIG_UINT b_size = size;
1437 if (!cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &b_size, NULL, NULL,
1445 file->offset = size + offset;
1454 return file->offset;
1459 * Generate an inode number from file name for those things that need it
1463 ino_t smbc_inode(SMBCCTX *context, const char *name)
1466 if (!context || !context->internal ||
1467 !context->internal->_initialized) {
1474 if (!*name) return 2; /* FIXME, why 2 ??? */
1475 return (ino_t)str_checksum(name);
1480 * Routine to put basic stat info into a stat structure ... Used by stat and
1485 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1490 if (IS_DOS_DIR(mode)) {
1491 st->st_mode = SMBC_DIR_MODE;
1493 st->st_mode = SMBC_FILE_MODE;
1496 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1497 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1498 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1499 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1502 #ifdef HAVE_STAT_ST_BLKSIZE
1503 st->st_blksize = 512;
1505 #ifdef HAVE_STAT_ST_BLOCKS
1506 st->st_blocks = (size+511)/512;
1508 st->st_uid = getuid();
1509 st->st_gid = getgid();
1511 if (IS_DOS_DIR(mode)) {
1517 if (st->st_ino == 0) {
1518 st->st_ino = smbc_inode(context, fname);
1521 return True; /* FIXME: Is this needed ? */
1526 * Routine to stat a file given a name
1529 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1532 fstring server, share, user, password, workgroup;
1534 time_t m_time = 0, a_time = 0, c_time = 0;
1539 if (!context || !context->internal ||
1540 !context->internal->_initialized) {
1542 errno = EINVAL; /* Best I can think of ... */
1554 DEBUG(4, ("smbc_stat(%s)\n", fname));
1556 if (smbc_parse_path(context, fname,
1557 server, sizeof(server),
1558 share, sizeof(share),
1561 password, sizeof(password),
1567 if (user[0] == (char)0) fstrcpy(user, context->user);
1569 fstrcpy(workgroup, context->workgroup);
1571 srv = smbc_server(context, server, share, workgroup, user, password);
1574 return -1; /* errno set by smbc_server */
1577 if (!smbc_getatr(context, srv, path, &mode, &size,
1578 &c_time, &a_time, &m_time, &ino)) {
1580 errno = smbc_errno(context, &srv->cli);
1587 smbc_setup_stat(context, st, path, size, mode);
1589 st->st_atime = a_time;
1590 st->st_ctime = c_time;
1591 st->st_mtime = m_time;
1592 st->st_dev = srv->dev;
1599 * Routine to stat a file given an fd
1602 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1604 time_t c_time, a_time, m_time;
1609 if (!context || !context->internal ||
1610 !context->internal->_initialized) {
1617 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1626 return context->fstatdir(context, file, st);
1630 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1631 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino)) {
1632 SMB_BIG_UINT b_size = size;
1633 if (!cli_getattrE(&file->srv->cli, file->cli_fd,
1634 &mode, &b_size, &c_time, &a_time, &m_time)) {
1645 smbc_setup_stat(context, st, file->fname, size, mode);
1647 st->st_atime = a_time;
1648 st->st_ctime = c_time;
1649 st->st_mtime = m_time;
1650 st->st_dev = file->srv->dev;
1657 * Routine to open a directory
1658 * We accept the URL syntax explained in smbc_parse_path(), above.
1661 static void smbc_remove_dir(SMBCFILE *dir)
1663 struct smbc_dir_list *d,*f;
1670 SAFE_FREE(f->dirent);
1675 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1679 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1681 struct smbc_dirent *dirent;
1683 int name_length = (name == NULL ? 0 : strlen(name));
1684 int comment_len = (comment == NULL ? 0 : strlen(comment));
1687 * Allocate space for the dirent, which must be increased by the
1688 * size of the name and the comment and 1 each for the null terminator.
1691 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
1693 dirent = SMB_MALLOC(size);
1697 dir->dir_error = ENOMEM;
1702 ZERO_STRUCTP(dirent);
1704 if (dir->dir_list == NULL) {
1706 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
1707 if (!dir->dir_list) {
1710 dir->dir_error = ENOMEM;
1714 ZERO_STRUCTP(dir->dir_list);
1716 dir->dir_end = dir->dir_next = dir->dir_list;
1720 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
1722 if (!dir->dir_end->next) {
1725 dir->dir_error = ENOMEM;
1729 ZERO_STRUCTP(dir->dir_end->next);
1731 dir->dir_end = dir->dir_end->next;
1734 dir->dir_end->next = NULL;
1735 dir->dir_end->dirent = dirent;
1737 dirent->smbc_type = type;
1738 dirent->namelen = name_length;
1739 dirent->commentlen = comment_len;
1740 dirent->dirlen = size;
1742 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1744 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1745 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1752 list_unique_wg_fn(const char *name, uint32 type, const char *comment, void *state)
1754 SMBCFILE *dir = (SMBCFILE *)state;
1755 struct smbc_dir_list *dir_list;
1756 struct smbc_dirent *dirent;
1760 dirent_type = dir->dir_type;
1762 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1764 /* An error occurred, what do we do? */
1765 /* FIXME: Add some code here */
1768 /* Point to the one just added */
1769 dirent = dir->dir_end->dirent;
1771 /* See if this was a duplicate */
1772 for (dir_list = dir->dir_list;
1773 dir_list != dir->dir_end;
1774 dir_list = dir_list->next) {
1776 strcmp(dir_list->dirent->name, dirent->name) == 0) {
1777 /* Duplicate. End end of list need to be removed. */
1781 if (do_remove && dir_list->next == dir->dir_end) {
1782 /* Found the end of the list. Remove it. */
1783 dir->dir_end = dir_list;
1784 free(dir_list->next);
1785 dir_list->next = NULL;
1792 list_fn(const char *name, uint32 type, const char *comment, void *state)
1794 SMBCFILE *dir = (SMBCFILE *)state;
1797 /* We need to process the type a little ... */
1799 if (dir->dir_type == SMBC_FILE_SHARE) {
1802 case 0: /* Directory tree */
1803 dirent_type = SMBC_FILE_SHARE;
1807 dirent_type = SMBC_PRINTER_SHARE;
1811 dirent_type = SMBC_COMMS_SHARE;
1815 dirent_type = SMBC_IPC_SHARE;
1819 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1823 else dirent_type = dir->dir_type;
1825 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1827 /* An error occurred, what do we do? */
1828 /* FIXME: Add some code here */
1834 dir_list_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
1837 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1838 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1840 /* Handle an error ... */
1842 /* FIXME: Add some code ... */
1848 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1850 fstring server, share, user, password, options;
1853 SMBCSRV *srv = NULL;
1854 SMBCFILE *dir = NULL;
1855 struct in_addr rem_ip;
1857 if (!context || !context->internal ||
1858 !context->internal->_initialized) {
1859 DEBUG(4, ("no valid context\n"));
1860 errno = EINVAL + 8192;
1866 DEBUG(4, ("no valid fname\n"));
1867 errno = EINVAL + 8193;
1871 if (smbc_parse_path(context, fname,
1872 server, sizeof(server),
1873 share, sizeof(share),
1876 password, sizeof(password),
1877 options, sizeof(options))) {
1878 DEBUG(4, ("no valid path\n"));
1879 errno = EINVAL + 8194;
1883 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s' options='%s'\n", fname, server, share, path, options));
1885 /* Ensure the options are valid */
1886 if (smbc_check_options(server, share, path, options)) {
1887 DEBUG(4, ("unacceptable options (%s)\n", options));
1888 errno = EINVAL + 8195;
1892 if (user[0] == (char)0) fstrcpy(user, context->user);
1894 pstrcpy(workgroup, context->workgroup);
1896 dir = SMB_MALLOC_P(SMBCFILE);
1908 dir->fname = SMB_STRDUP(fname);
1912 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1914 if (server[0] == (char)0) {
1919 struct ip_service *ip_list;
1920 struct ip_service server_addr;
1921 struct user_auth_info u_info;
1922 struct cli_state *cli;
1924 if (share[0] != (char)0 || path[0] != (char)0) {
1926 errno = EINVAL + 8196;
1928 SAFE_FREE(dir->fname);
1934 /* Determine how many local master browsers to query */
1935 max_lmb_count = (context->options.browse_max_lmb_count == 0
1937 : context->options.browse_max_lmb_count);
1939 pstrcpy(u_info.username, user);
1940 pstrcpy(u_info.password, password);
1943 * We have server and share and path empty but options
1944 * requesting that we scan all master browsers for their list
1945 * of workgroups/domains. This implies that we must first try
1946 * broadcast queries to find all master browsers, and if that
1947 * doesn't work, then try our other methods which return only
1948 * a single master browser.
1951 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
1952 if (!find_master_ip(workgroup, &server_addr.ip)) {
1958 ip_list = &server_addr;
1962 for (i = 0; i < count && i < max_lmb_count; i++) {
1963 DEBUG(99, ("Found master browser %s\n", inet_ntoa(ip_list[i].ip)));
1965 cli = get_ipc_connect_master_ip(&ip_list[i], workgroup, &u_info);
1966 /* cli == NULL is the master browser refused to talk or
1967 could not be found */
1971 fstrcpy(server, cli->desthost);
1974 DEBUG(4, ("using workgroup %s %s\n", workgroup, server));
1977 * For each returned master browser IP address, get a
1978 * connection to IPC$ on the server if we do not
1979 * already have one, and determine the
1980 * workgroups/domains that it knows about.
1983 srv = smbc_server(context, server,
1984 "IPC$", workgroup, user, password);
1988 SAFE_FREE(dir->fname);
1995 dir->dir_type = SMBC_WORKGROUP;
1997 /* Now, list the stuff ... */
1999 if (!cli_NetServerEnum(&srv->cli, workgroup, SV_TYPE_DOMAIN_ENUM, list_unique_wg_fn,
2003 SAFE_FREE(dir->fname);
2013 * Server not an empty string ... Check the rest and see what
2016 if (share[0] == (char)0) {
2018 if (path[0] != (char)0) { /* Should not have empty share with path */
2020 errno = EINVAL + 8197;
2022 SAFE_FREE(dir->fname);
2029 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
2030 /* However, we check to see if <server> is an IP address first */
2032 if (!is_ipaddress(server) && /* Not an IP addr so check next */
2033 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
2034 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
2037 dir->dir_type = SMBC_SERVER;
2040 * Get the backup list ...
2044 if (!name_status_find(server, 0, 0, rem_ip, buserver)) {
2046 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
2047 errno = EPERM; /* FIXME, is this correct */
2053 * Get a connection to IPC$ on the server if we do not already have one
2056 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
2059 DEBUG(0, ("got no contact to IPC$\n"));
2061 SAFE_FREE(dir->fname);
2070 /* Now, list the servers ... */
2072 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
2076 SAFE_FREE(dir->fname);
2085 if (resolve_name(server, &rem_ip, 0x20)) {
2087 /* Now, list the shares ... */
2089 dir->dir_type = SMBC_FILE_SHARE;
2091 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
2096 SAFE_FREE(dir->fname);
2105 /* Now, list the servers ... */
2107 if (cli_RNetShareEnum(&srv->cli, list_fn,
2110 errno = cli_errno(&srv->cli);
2112 SAFE_FREE(dir->fname);
2122 errno = ECONNREFUSED; /* Neither the workgroup nor server exists */
2124 SAFE_FREE(dir->fname);
2134 else { /* The server and share are specified ... work from there ... */
2136 /* Well, we connect to the server and list the directory */
2138 dir->dir_type = SMBC_FILE_SHARE;
2140 srv = smbc_server(context, server, share, workgroup, user, password);
2145 SAFE_FREE(dir->fname);
2154 /* Now, list the files ... */
2156 pstrcat(path, "\\*");
2158 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
2162 SAFE_FREE(dir->fname);
2165 errno = smbc_errno(context, &srv->cli);
2173 DLIST_ADD(context->internal->_files, dir);
2179 * Routine to close a directory
2182 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
2185 if (!context || !context->internal ||
2186 !context->internal->_initialized) {
2193 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2200 smbc_remove_dir(dir); /* Clean it up */
2202 DLIST_REMOVE(context->internal->_files, dir);
2206 SAFE_FREE(dir->fname);
2207 SAFE_FREE(dir); /* Free the space too */
2214 static void smbc_readdir_internal(SMBCCTX * context,
2215 struct smbc_dirent *dest,
2216 struct smbc_dirent *src,
2217 int max_namebuf_len)
2219 if (context->options.urlencode_readdir_entries) {
2221 /* url-encode the name. get back remaining buffer space */
2223 smbc_urlencode(dest->name, src->name, max_namebuf_len);
2225 /* We now know the name length */
2226 dest->namelen = strlen(dest->name);
2228 /* Save the pointer to the beginning of the comment */
2229 dest->comment = dest->name + dest->namelen + 1;
2231 /* Copy the comment */
2232 strncpy(dest->comment, src->comment, max_namebuf_len);
2234 /* Ensure the comment is null terminated */
2235 if (max_namebuf_len > src->commentlen) {
2236 dest->comment[src->commentlen] = '\0';
2238 dest->comment[max_namebuf_len - 1] = '\0';
2241 /* Save other fields */
2242 dest->smbc_type = src->smbc_type;
2243 dest->commentlen = strlen(dest->comment);
2244 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
2248 /* No encoding. Just copy the entry as is. */
2249 memcpy(dest, src, src->dirlen);
2250 dest->comment = (char *)(&dest->name + src->namelen + 1);
2256 * Routine to get a directory entry
2259 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
2262 struct smbc_dirent *dirp, *dirent;
2264 /* Check that all is ok first ... */
2266 if (!context || !context->internal ||
2267 !context->internal->_initialized) {
2270 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
2275 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2278 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
2283 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2286 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
2291 if (!dir->dir_next) {
2295 dirent = dir->dir_next->dirent;
2303 dirp = (struct smbc_dirent *)context->internal->_dirent;
2304 maxlen = (sizeof(context->internal->_dirent) -
2305 sizeof(struct smbc_dirent));
2307 smbc_readdir_internal(context, dirp, dirent, maxlen);
2309 dir->dir_next = dir->dir_next->next;
2315 * Routine to get directory entries
2318 static int smbc_getdents_ctx(SMBCCTX *context,
2320 struct smbc_dirent *dirp,
2326 char *ndir = (char *)dirp;
2327 struct smbc_dir_list *dirlist;
2329 /* Check that all is ok first ... */
2331 if (!context || !context->internal ||
2332 !context->internal->_initialized) {
2339 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2346 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2354 * Now, retrieve the number of entries that will fit in what was passed
2355 * We have to figure out if the info is in the list, or we need to
2356 * send a request to the server to get the info.
2359 while ((dirlist = dir->dir_next)) {
2360 struct smbc_dirent *dirent;
2362 if (!dirlist->dirent) {
2364 errno = ENOENT; /* Bad error */
2369 /* Do urlencoding of next entry, if so selected */
2370 dirent = (struct smbc_dirent *)context->internal->_dirent;
2371 maxlen = (sizeof(context->internal->_dirent) -
2372 sizeof(struct smbc_dirent));
2373 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
2375 reqd = dirent->dirlen;
2379 if (rem < count) { /* We managed to copy something */
2385 else { /* Nothing copied ... */
2387 errno = EINVAL; /* Not enough space ... */
2394 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
2396 ((struct smbc_dirent *)ndir)->comment =
2397 (char *)(&((struct smbc_dirent *)ndir)->name +
2405 dir->dir_next = dirlist = dirlist -> next;
2416 * Routine to create a directory ...
2419 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
2422 fstring server, share, user, password, workgroup;
2425 if (!context || !context->internal ||
2426 !context->internal->_initialized) {
2440 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
2442 if (smbc_parse_path(context, fname,
2443 server, sizeof(server),
2444 share, sizeof(share),
2447 password, sizeof(password),
2453 if (user[0] == (char)0) fstrcpy(user, context->user);
2455 fstrcpy(workgroup, context->workgroup);
2457 srv = smbc_server(context, server, share, workgroup, user, password);
2461 return -1; /* errno set by smbc_server */
2465 if (!cli_mkdir(&srv->cli, path)) {
2467 errno = smbc_errno(context, &srv->cli);
2477 * Our list function simply checks to see if a directory is not empty
2480 static int smbc_rmdir_dirempty = True;
2482 static void rmdir_list_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
2485 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2486 smbc_rmdir_dirempty = False;
2491 * Routine to remove a directory
2494 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
2497 fstring server, share, user, password, workgroup;
2500 if (!context || !context->internal ||
2501 !context->internal->_initialized) {
2515 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2517 if (smbc_parse_path(context, fname,
2518 server, sizeof(server),
2519 share, sizeof(share),
2522 password, sizeof(password),
2529 if (user[0] == (char)0) fstrcpy(user, context->user);
2531 fstrcpy(workgroup, context->workgroup);
2533 srv = smbc_server(context, server, share, workgroup, user, password);
2537 return -1; /* errno set by smbc_server */
2541 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2543 mode = aDIR | aRONLY;
2546 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2548 if (strcmp(path, "\\") == 0) {
2550 mode = aDIR | aRONLY;
2556 smbc_stat_printjob(srv, path, &size, &m_time);
2557 c_time = a_time = m_time;
2562 if (!cli_rmdir(&srv->cli, path)) {
2564 errno = smbc_errno(context, &srv->cli);
2566 if (errno == EACCES) { /* Check if the dir empty or not */
2568 pstring lpath; /* Local storage to avoid buffer overflows */
2570 smbc_rmdir_dirempty = True; /* Make this so ... */
2572 pstrcpy(lpath, path);
2573 pstrcat(lpath, "\\*");
2575 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2578 /* Fix errno to ignore latest error ... */
2580 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2581 smbc_errno(context, &srv->cli)));
2586 if (smbc_rmdir_dirempty)
2602 * Routine to return the current directory position
2605 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2607 off_t ret_val; /* Squash warnings about cast */
2609 if (!context || !context->internal ||
2610 !context->internal->_initialized) {
2617 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2624 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2632 * We return the pointer here as the offset
2634 ret_val = (int)dir->dir_next;
2640 * A routine to run down the list and see if the entry is OK
2643 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2644 struct smbc_dirent *dirent)
2647 /* Run down the list looking for what we want */
2651 struct smbc_dir_list *tmp = list;
2655 if (tmp->dirent == dirent)
2664 return NULL; /* Not found, or an error */
2670 * Routine to seek on a directory
2673 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2675 long int l_offset = offset; /* Handle problems of size */
2676 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
2677 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
2679 if (!context || !context->internal ||
2680 !context->internal->_initialized) {
2687 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2694 /* Now, check what we were passed and see if it is OK ... */
2696 if (dirent == NULL) { /* Seek to the begining of the list */
2698 dir->dir_next = dir->dir_list;
2703 /* Now, run down the list and make sure that the entry is OK */
2704 /* This may need to be changed if we change the format of the list */
2706 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2708 errno = EINVAL; /* Bad entry */
2713 dir->dir_next = list_ent;
2720 * Routine to fstat a dir
2723 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2726 if (!context || !context->internal ||
2727 !context->internal->_initialized) {
2734 /* No code yet ... */
2740 int smbc_chmod_ctx(SMBCCTX *context, const char *fname, mode_t newmode)
2743 fstring server, share, user, password, workgroup;
2747 if (!context || !context->internal ||
2748 !context->internal->_initialized) {
2750 errno = EINVAL; /* Best I can think of ... */
2762 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
2764 if (smbc_parse_path(context, fname,
2765 server, sizeof(server),
2766 share, sizeof(share),
2769 password, sizeof(password),
2775 if (user[0] == (char)0) fstrcpy(user, context->user);
2777 fstrcpy(workgroup, context->workgroup);
2779 srv = smbc_server(context, server, share, workgroup, user, password);
2782 return -1; /* errno set by smbc_server */
2787 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
2788 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
2789 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
2790 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
2792 if (!cli_setatr(&srv->cli, path, mode, 0)) {
2793 errno = smbc_errno(context, &srv->cli);
2800 int smbc_utimes_ctx(SMBCCTX *context, const char *fname, struct timeval *tbuf)
2803 fstring server, share, user, password, workgroup;
2806 time_t t = (tbuf == NULL ? time(NULL) : tbuf->tv_sec);
2808 if (!context || !context->internal ||
2809 !context->internal->_initialized) {
2811 errno = EINVAL; /* Best I can think of ... */
2823 DEBUG(4, ("smbc_utimes(%s, [%s])\n", fname, ctime(&t)));
2825 if (smbc_parse_path(context, fname,
2826 server, sizeof(server),
2827 share, sizeof(share),
2830 password, sizeof(password),
2836 if (user[0] == (char)0) fstrcpy(user, context->user);
2838 fstrcpy(workgroup, context->workgroup);
2840 srv = smbc_server(context, server, share, workgroup, user, password);
2843 return -1; /* errno set by smbc_server */
2846 if (!smbc_getatr(context, srv, path,
2853 if (!cli_setatr(&srv->cli, path, mode, t)) {
2854 /* some servers always refuse directory changes */
2855 if (!(mode & aDIR)) {
2856 errno = smbc_errno(context, &srv->cli);
2865 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
2866 However NT4 gives a "The information may have been modified by a
2867 computer running Windows NT 5.0" if denied ACEs do not appear before
2870 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
2872 if (sec_ace_equal(ace1, ace2))
2875 if (ace1->type != ace2->type)
2876 return ace2->type - ace1->type;
2878 if (sid_compare(&ace1->trustee, &ace2->trustee))
2879 return sid_compare(&ace1->trustee, &ace2->trustee);
2881 if (ace1->flags != ace2->flags)
2882 return ace1->flags - ace2->flags;
2884 if (ace1->info.mask != ace2->info.mask)
2885 return ace1->info.mask - ace2->info.mask;
2887 if (ace1->size != ace2->size)
2888 return ace1->size - ace2->size;
2890 return memcmp(ace1, ace2, sizeof(SEC_ACE));
2894 static void sort_acl(SEC_ACL *the_acl)
2897 if (!the_acl) return;
2899 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
2901 for (i=1;i<the_acl->num_aces;) {
2902 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
2904 for (j=i; j<the_acl->num_aces-1; j++) {
2905 the_acl->ace[j] = the_acl->ace[j+1];
2907 the_acl->num_aces--;
2914 /* convert a SID to a string, either numeric or username/group */
2915 static void convert_sid_to_string(struct cli_state *ipc_cli,
2921 char **domains = NULL;
2922 char **names = NULL;
2923 uint32 *types = NULL;
2925 sid_to_string(str, sid);
2927 if (numeric) return; /* no lookup desired */
2929 /* Ask LSA to convert the sid to a name */
2931 if (!NT_STATUS_IS_OK(cli_lsa_lookup_sids(ipc_cli, ipc_cli->mem_ctx,
2932 pol, 1, sid, &domains,
2934 !domains || !domains[0] || !names || !names[0]) {
2940 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
2941 domains[0], lp_winbind_separator(),
2945 /* convert a string to a SID, either numeric or username/group */
2946 static BOOL convert_string_to_sid(struct cli_state *ipc_cli,
2952 uint32 *types = NULL;
2953 DOM_SID *sids = NULL;
2957 if (strncmp(str, "S-", 2) == 0) {
2958 return string_to_sid(sid, str);
2965 if (!NT_STATUS_IS_OK(cli_lsa_lookup_names(ipc_cli, ipc_cli->mem_ctx,
2966 pol, 1, &str, &sids,
2972 sid_copy(sid, &sids[0]);
2979 /* parse an ACE in the same format as print_ace() */
2980 static BOOL parse_ace(struct cli_state *ipc_cli,
2989 unsigned atype, aflags, amask;
2992 const struct perm_value *v;
2998 /* These values discovered by inspection */
2999 static const struct perm_value special_values[] = {
3000 { "R", 0x00120089 },
3001 { "W", 0x00120116 },
3002 { "X", 0x001200a0 },
3003 { "D", 0x00010000 },
3004 { "P", 0x00040000 },
3005 { "O", 0x00080000 },
3009 static const struct perm_value standard_values[] = {
3010 { "READ", 0x001200a9 },
3011 { "CHANGE", 0x001301bf },
3012 { "FULL", 0x001f01ff },
3018 p = strchr_m(str,':');
3019 if (!p) return False;
3022 /* Try to parse numeric form */
3024 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3025 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3029 /* Try to parse text form */
3031 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3036 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3040 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3041 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3042 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3043 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3048 /* Only numeric form accepted for flags at present */
3050 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3051 sscanf(tok, "%i", &aflags))) {
3055 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3059 if (strncmp(tok, "0x", 2) == 0) {
3060 if (sscanf(tok, "%i", &amask) != 1) {
3066 for (v = standard_values; v->perm; v++) {
3067 if (strcmp(tok, v->perm) == 0) {
3078 for (v = special_values; v->perm; v++) {
3079 if (v->perm[0] == *p) {
3085 if (!found) return False;
3095 init_sec_ace(ace, &sid, atype, mask, aflags);
3099 /* add an ACE to a list of ACEs in a SEC_ACL */
3100 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace, TALLOC_CTX *ctx)
3105 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
3109 aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces);
3110 memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
3111 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
3112 new = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
3119 /* parse a ascii version of a security descriptor */
3120 static SEC_DESC *sec_desc_parse(TALLOC_CTX *ctx,
3121 struct cli_state *ipc_cli,
3126 const char *p = str;
3130 DOM_SID *grp_sid=NULL, *owner_sid=NULL;
3134 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
3136 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
3137 revision = strtol(tok+9, NULL, 16);
3141 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
3142 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3144 !convert_string_to_sid(ipc_cli, pol,
3146 owner_sid, tok+6)) {
3147 DEBUG(5, ("Failed to parse owner sid\n"));
3153 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
3154 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3156 !convert_string_to_sid(ipc_cli, pol,
3158 owner_sid, tok+7)) {
3159 DEBUG(5, ("Failed to parse owner sid\n"));
3165 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
3166 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3168 !convert_string_to_sid(ipc_cli, pol,
3171 DEBUG(5, ("Failed to parse group sid\n"));
3177 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
3178 grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3180 !convert_string_to_sid(ipc_cli, pol,
3183 DEBUG(5, ("Failed to parse group sid\n"));
3189 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
3191 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
3192 DEBUG(5, ("Failed to parse ACL %s\n", tok));
3195 if(!add_ace(&dacl, &ace, ctx)) {
3196 DEBUG(5, ("Failed to add ACL %s\n", tok));
3202 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
3204 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
3205 DEBUG(5, ("Failed to parse ACL %s\n", tok));
3208 if(!add_ace(&dacl, &ace, ctx)) {
3209 DEBUG(5, ("Failed to add ACL %s\n", tok));
3215 DEBUG(5, ("Failed to parse security descriptor\n"));
3219 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
3220 owner_sid, grp_sid, NULL, dacl, &sd_size);
3223 SAFE_FREE(owner_sid);
3229 /* Obtain the current dos attributes */
3230 static DOS_ATTR_DESC *dos_attr_query(SMBCCTX *context,
3232 const char *filename,
3235 time_t m_time = 0, a_time = 0, c_time = 0;
3238 SMB_INO_T inode = 0;
3241 ret = talloc(ctx, sizeof(DOS_ATTR_DESC));
3247 /* Obtain the DOS attributes */
3248 if (!smbc_getatr(context, srv, filename, &mode, &size,
3249 &c_time, &a_time, &m_time, &inode)) {
3251 errno = smbc_errno(context, &srv->cli);
3252 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
3259 ret->a_time = a_time;
3260 ret->c_time = c_time;
3261 ret->m_time = m_time;
3268 /* parse a ascii version of a security descriptor */
3269 static void dos_attr_parse(SMBCCTX *context,
3274 const char *p = str;
3277 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
3279 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
3280 dad->mode = strtol(tok+5, NULL, 16);
3284 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
3285 dad->size = strtoll(tok+5, NULL, 10);
3289 if (StrnCaseCmp(tok, "A_TIME:", 7) == 0) {
3290 dad->a_time = strtoll(tok+7, NULL, 10);
3294 if (StrnCaseCmp(tok, "C_TIME:", 7) == 0) {
3295 dad->c_time = strtoll(tok+7, NULL, 10);
3299 if (StrnCaseCmp(tok, "M_TIME:", 7) == 0) {
3300 dad->m_time = strtoll(tok+7, NULL, 10);
3304 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
3305 dad->inode = strtoll(tok+6, NULL, 10);
3312 /*****************************************************
3313 retrieve the acls for a file
3314 *******************************************************/
3315 static int cacl_get(SMBCCTX *context, TALLOC_CTX *ctx, SMBCSRV *srv,
3316 struct cli_state *ipc_cli, POLICY_HND *pol,
3317 char *filename, char *name, char *buf, int bufsize)
3327 BOOL numeric = True;
3328 BOOL determine_size = (bufsize == 0);
3333 time_t m_time = 0, a_time = 0, c_time = 0;
3337 struct cli_state *cli = &srv->cli;
3339 all = (StrnCaseCmp(name, "system.*", 8) == 0);
3340 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
3341 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
3342 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
3343 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
3344 numeric = (* (name + strlen(name) - 1) != '+');
3349 * If we are (possibly) talking to an NT or new system and some NT
3350 * attributes have been requested...
3352 if (ipc_cli && (all || some_nt)) {
3353 /* Point to the portion after "system.nt_sec_desc." */
3354 name += 19; /* if (all) this will be invalid but unused */
3356 /* ... then obtain any NT attributes which were requested */
3357 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
3360 DEBUG(5, ("cacl_get failed to open %s: %s\n",
3361 filename, cli_errstr(cli)));
3366 sd = cli_query_secdesc(cli, fnum, ctx);
3370 ("cacl_get Failed to query old descriptor\n"));
3375 cli_close(cli, fnum);
3377 if (all || all_nt) {
3378 if (determine_size) {
3379 p = talloc_asprintf(ctx,
3388 n = snprintf(buf, bufsize,
3389 "REVISION:%d", sd->revision);
3391 } else if (StrCaseCmp(name, "revision") == 0) {
3392 if (determine_size) {
3393 p = talloc_asprintf(ctx, "%d", sd->revision);
3400 n = snprintf(buf, bufsize, "%d", sd->revision);
3404 if (!determine_size && n > bufsize) {
3412 /* Get owner and group sid */
3414 if (sd->owner_sid) {
3415 convert_sid_to_string(ipc_cli, pol,
3416 sidstr, numeric, sd->owner_sid);
3418 fstrcpy(sidstr, "");
3421 if (all || all_nt) {
3422 if (determine_size) {
3423 p = talloc_asprintf(ctx, ",OWNER:%s", sidstr);
3430 n = snprintf(buf, bufsize,
3431 ",OWNER:%s", sidstr);
3433 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
3434 if (determine_size) {
3435 p = talloc_asprintf(ctx, "%s", sidstr);
3442 n = snprintf(buf, bufsize, "%s", sidstr);
3446 if (!determine_size && n > bufsize) {
3455 convert_sid_to_string(ipc_cli, pol,
3456 sidstr, numeric, sd->grp_sid);
3458 fstrcpy(sidstr, "");
3461 if (all || all_nt) {
3462 if (determine_size) {
3463 p = talloc_asprintf(ctx, ",GROUP:%s", sidstr);
3470 n = snprintf(buf, bufsize,
3471 ",GROUP:%s", sidstr);
3473 } else if (StrnCaseCmp(name, "group", 5) == 0) {
3474 if (determine_size) {
3475 p = talloc_asprintf(ctx, "%s", sidstr);
3482 n = snprintf(buf, bufsize, "%s", sidstr);
3486 if (!determine_size && n > bufsize) {
3494 /* Add aces to value buffer */
3495 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
3497 SEC_ACE *ace = &sd->dacl->ace[i];
3498 convert_sid_to_string(ipc_cli, pol,
3499 sidstr, numeric, &ace->trustee);
3501 if (all || all_nt) {
3502 if (determine_size) {
3503 p = talloc_asprintf(ctx,
3516 n = snprintf(buf, bufsize,
3517 ",ACL:%s:%d/%d/0x%08x",
3523 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
3524 StrCaseCmp(name + 3, sidstr) == 0) ||
3525 (StrnCaseCmp(name, "acl+", 4) == 0 &&
3526 StrCaseCmp(name + 4, sidstr) == 0)) {
3527 if (determine_size) {
3528 p = talloc_asprintf(ctx,
3539 n = snprintf(buf, bufsize,
3555 /* Restore name pointer to its original value */
3559 if (all || some_dos) {
3560 /* Point to the portion after "system.dos_attr." */
3561 name += 16; /* if (all) this will be invalid but unused */
3563 /* Obtain the DOS attributes */
3564 if (!smbc_getatr(context, srv, filename, &mode, &size,
3565 &c_time, &a_time, &m_time, &ino)) {
3567 errno = smbc_errno(context, &srv->cli);
3572 if (all || all_dos) {
3573 if (determine_size) {
3574 p = talloc_asprintf(ctx,
3587 n = snprintf(buf, bufsize,
3595 } else if (StrCaseCmp(name, "mode") == 0) {
3596 if (determine_size) {
3597 p = talloc_asprintf(ctx, "0x%x", mode);
3604 n = snprintf(buf, bufsize, "0x%x", mode);
3608 if (!determine_size && n > bufsize) {
3616 if (all || all_dos) {
3617 if (determine_size) {
3618 p = talloc_asprintf(ctx,
3620 (unsigned long long) size);
3627 n = snprintf(buf, bufsize,
3629 (unsigned long long) size);
3631 } else if (StrCaseCmp(name, "size") == 0) {
3632 if (determine_size) {
3633 p = talloc_asprintf(ctx,
3635 (unsigned long long) size);
3642 n = snprintf(buf, bufsize,
3644 (unsigned long long) size);
3648 if (!determine_size && n > bufsize) {
3656 if (all || all_dos) {
3657 if (determine_size) {
3658 p = talloc_asprintf(ctx,
3659 ",C_TIME:%lu", c_time);
3666 n = snprintf(buf, bufsize,
3667 ",C_TIME:%lu", c_time);
3669 } else if (StrCaseCmp(name, "c_time") == 0) {
3670 if (determine_size) {
3671 p = talloc_asprintf(ctx, "%lu", c_time);
3678 n = snprintf(buf, bufsize, "%lu", c_time);
3682 if (!determine_size && n > bufsize) {
3690 if (all || all_dos) {
3691 if (determine_size) {
3692 p = talloc_asprintf(ctx,
3693 ",A_TIME:%lu", a_time);
3700 n = snprintf(buf, bufsize,
3701 ",A_TIME:%lu", a_time);
3703 } else if (StrCaseCmp(name, "a_time") == 0) {
3704 if (determine_size) {
3705 p = talloc_asprintf(ctx, "%lu", a_time);
3712 n = snprintf(buf, bufsize, "%lu", a_time);
3716 if (!determine_size && n > bufsize) {
3724 if (all || all_dos) {
3725 if (determine_size) {
3726 p = talloc_asprintf(ctx,
3727 ",M_TIME:%lu", m_time);
3734 n = snprintf(buf, bufsize,
3735 ",M_TIME:%lu", m_time);
3737 } else if (StrCaseCmp(name, "m_time") == 0) {
3738 if (determine_size) {
3739 p = talloc_asprintf(ctx, "%lu", m_time);
3746 n = snprintf(buf, bufsize, "%lu", m_time);
3750 if (!determine_size && n > bufsize) {
3758 if (all || all_dos) {
3759 if (determine_size) {
3760 p = talloc_asprintf(ctx,
3762 (unsigned long long) ino);
3769 n = snprintf(buf, bufsize,
3771 (unsigned long long) ino);
3773 } else if (StrCaseCmp(name, "inode") == 0) {
3774 if (determine_size) {
3775 p = talloc_asprintf(ctx,
3777 (unsigned long long) ino);
3784 n = snprintf(buf, bufsize,
3786 (unsigned long long) ino);
3790 if (!determine_size && n > bufsize) {
3798 /* Restore name pointer to its original value */
3811 /*****************************************************
3812 set the ACLs on a file given an ascii description
3813 *******************************************************/
3814 static int cacl_set(TALLOC_CTX *ctx, struct cli_state *cli,
3815 struct cli_state *ipc_cli, POLICY_HND *pol,
3816 const char *filename, const char *the_acl,
3817 int mode, int flags)
3821 SEC_DESC *sd = NULL, *old;
3822 SEC_ACL *dacl = NULL;
3823 DOM_SID *owner_sid = NULL;
3824 DOM_SID *grp_sid = NULL;
3829 BOOL numeric = True;
3831 /* the_acl will be null for REMOVE_ALL operations */
3833 numeric = ((p = strchr(the_acl, ':')) != NULL &&
3837 /* if this is to set the entire ACL... */
3838 if (*the_acl == '*') {
3839 /* ... then increment past the first colon */
3843 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric, the_acl);
3851 /* The desired access below is the only one I could find that works
3852 with NT4, W2KP and Samba */
3854 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
3857 DEBUG(5, ("cacl_set failed to open %s: %s\n",
3858 filename, cli_errstr(cli)));
3863 old = cli_query_secdesc(cli, fnum, ctx);
3866 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
3871 cli_close(cli, fnum);
3874 case SMBC_XATTR_MODE_REMOVE_ALL:
3875 old->dacl->num_aces = 0;
3876 SAFE_FREE(old->dacl->ace);
3877 SAFE_FREE(old->dacl);
3882 case SMBC_XATTR_MODE_REMOVE:
3883 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
3886 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
3887 if (sec_ace_equal(&sd->dacl->ace[i],
3888 &old->dacl->ace[j])) {
3890 for (k=j; k<old->dacl->num_aces-1;k++) {
3891 old->dacl->ace[k] = old->dacl->ace[k+1];
3893 old->dacl->num_aces--;
3894 if (old->dacl->num_aces == 0) {
3895 SAFE_FREE(old->dacl->ace);
3896 SAFE_FREE(old->dacl);
3913 case SMBC_XATTR_MODE_ADD:
3914 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
3917 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
3918 if (sid_equal(&sd->dacl->ace[i].trustee,
3919 &old->dacl->ace[j].trustee)) {
3920 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
3925 old->dacl->ace[j] = sd->dacl->ace[i];
3931 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
3937 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
3938 add_ace(&old->dacl, &sd->dacl->ace[i], ctx);
3944 case SMBC_XATTR_MODE_SET:
3946 owner_sid = old->owner_sid;
3947 grp_sid = old->grp_sid;
3951 case SMBC_XATTR_MODE_CHOWN:
3952 owner_sid = sd->owner_sid;
3955 case SMBC_XATTR_MODE_CHGRP:
3956 grp_sid = sd->grp_sid;
3960 /* Denied ACE entries must come before allowed ones */
3961 sort_acl(old->dacl);
3963 /* Create new security descriptor and set it */
3964 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
3965 owner_sid, grp_sid, NULL, dacl, &sd_size);
3967 fnum = cli_nt_create(cli, filename,
3968 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
3971 DEBUG(5, ("cacl_set failed to open %s: %s\n",
3972 filename, cli_errstr(cli)));
3977 if (!cli_set_secdesc(cli, fnum, sd)) {
3978 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
3985 cli_close(cli, fnum);
3995 int smbc_setxattr_ctx(SMBCCTX *context,
4006 fstring server, share, user, password, workgroup;
4012 if (!context || !context->internal ||
4013 !context->internal->_initialized) {
4015 errno = EINVAL; /* Best I can think of ... */
4027 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n", fname, name, (int) size, (const char*)value));
4029 if (smbc_parse_path(context, fname,
4030 server, sizeof(server),
4031 share, sizeof(share),
4034 password, sizeof(password),
4040 if (user[0] == (char)0) fstrcpy(user, context->user);
4042 fstrcpy(workgroup, context->workgroup);
4044 srv = smbc_server(context, server, share, workgroup, user, password);
4046 return -1; /* errno set by smbc_server */
4049 if (! srv->no_nt_session) {
4050 ipc_srv = smbc_attr_server(context, server, share,
4051 workgroup, user, password,
4053 srv->no_nt_session = True;
4058 ctx = talloc_init("smbc_setxattr");
4065 * Are they asking to set the entire set of known attributes?
4067 if (StrCaseCmp(name, "system.*") == 0 ||
4068 StrCaseCmp(name, "system.*+") == 0) {
4071 talloc_asprintf(ctx, "%s:%s", name+7, (const char *) value);
4079 ret = cacl_set(ctx, &srv->cli,
4080 &ipc_srv->cli, &pol, path,
4083 ? SMBC_XATTR_MODE_SET
4084 : SMBC_XATTR_MODE_ADD),
4090 /* get a DOS Attribute Descriptor with current attributes */
4091 dad = dos_attr_query(context, ctx, path, srv);
4093 /* Overwrite old with new, using what was provided */
4094 dos_attr_parse(context, dad, srv, namevalue);
4096 /* Set the new DOS attributes */
4097 #if 0 /* not yet implemented */
4098 if (! cli_setpathinfo(&srv->cli, path,
4103 if (!cli_setatr(&srv->cli, path,
4104 dad->mode, dad->m_time)) {
4105 errno = smbc_errno(context, &srv->cli);
4109 if (!cli_setatr(&srv->cli, path,
4110 dad->mode, dad->m_time)) {
4111 errno = smbc_errno(context, &srv->cli);
4116 /* we only fail if both NT and DOS sets failed */
4117 if (ret < 0 && ! dad) {
4118 ret = -1; /* in case dad was null */
4124 talloc_destroy(ctx);
4129 * Are they asking to set an access control element or to set
4130 * the entire access control list?
4132 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4133 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
4134 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4135 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4136 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
4140 talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4143 ret = -1; /* errno set by smbc_server() */
4145 else if (! namevalue) {
4149 ret = cacl_set(ctx, &srv->cli,
4150 &ipc_srv->cli, &pol, path,
4153 ? SMBC_XATTR_MODE_SET
4154 : SMBC_XATTR_MODE_ADD),
4157 talloc_destroy(ctx);
4162 * Are they asking to set the owner?
4164 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4165 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
4169 talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4173 ret = -1; /* errno set by smbc_server() */
4175 else if (! namevalue) {
4179 ret = cacl_set(ctx, &srv->cli,
4180 &ipc_srv->cli, &pol, path,
4181 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
4183 talloc_destroy(ctx);
4188 * Are they asking to set the group?
4190 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4191 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
4195 talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4198 /* errno set by smbc_server() */
4201 else if (! namevalue) {
4205 ret = cacl_set(ctx, &srv->cli,
4206 &ipc_srv->cli, &pol, path,
4207 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
4209 talloc_destroy(ctx);
4214 * Are they asking to set a DOS attribute?
4216 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
4217 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
4218 StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
4219 StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
4220 StrCaseCmp(name, "system.dos_attr.m_time") == 0) {
4222 /* get a DOS Attribute Descriptor with current attributes */
4223 dad = dos_attr_query(context, ctx, path, srv);
4226 talloc_asprintf(ctx, "%s:%s", name+16, (const char *) value);
4231 /* Overwrite old with provided new params */
4232 dos_attr_parse(context, dad, srv, namevalue);
4234 /* Set the new DOS attributes */
4235 #if 0 /* not yet implemented */
4236 ret2 = cli_setpathinfo(&srv->cli, path,
4242 ret2 = cli_setatr(&srv->cli, path,
4246 errno = smbc_errno(context,
4251 ret2 = cli_setatr(&srv->cli, path,
4252 dad->mode, dad->m_time);
4254 errno = smbc_errno(context, &srv->cli);
4258 /* ret2 has True (success) / False (failure) */
4269 talloc_destroy(ctx);
4273 /* Unsupported attribute name */
4274 talloc_destroy(ctx);
4279 int smbc_getxattr_ctx(SMBCCTX *context,
4288 fstring server, share, user, password, workgroup;
4294 if (!context || !context->internal ||
4295 !context->internal->_initialized) {
4297 errno = EINVAL; /* Best I can think of ... */
4309 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
4311 if (smbc_parse_path(context, fname,
4312 server, sizeof(server),
4313 share, sizeof(share),
4316 password, sizeof(password),
4322 if (user[0] == (char)0) fstrcpy(user, context->user);
4324 fstrcpy(workgroup, context->workgroup);
4326 srv = smbc_server(context, server, share, workgroup, user, password);
4328 return -1; /* errno set by smbc_server */
4331 if (! srv->no_nt_session) {
4332 ipc_srv = smbc_attr_server(context, server, share,
4333 workgroup, user, password,
4336 srv->no_nt_session = True;
4342 ctx = talloc_init("smbc:getxattr");
4348 /* Are they requesting a supported attribute? */
4349 if (StrCaseCmp(name, "system.*") == 0 ||
4350 StrCaseCmp(name, "system.*+") == 0 ||
4351 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4352 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
4353 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4354 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4355 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
4356 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4357 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
4358 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4359 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
4360 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
4361 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
4362 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
4363 StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
4364 StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
4365 StrCaseCmp(name, "system.dos_attr.m_time") == 0 ||
4366 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
4369 ret = cacl_get(context, ctx, srv,
4370 ipc_srv == NULL ? NULL : &ipc_srv->cli,
4371 &pol, path, name, (const char *) value, size);
4372 if (ret < 0 && errno == 0) {
4373 errno = smbc_errno(context, &srv->cli);
4375 talloc_destroy(ctx);
4379 /* Unsupported attribute name */
4380 talloc_destroy(ctx);
4386 int smbc_removexattr_ctx(SMBCCTX *context,
4393 fstring server, share, user, password, workgroup;
4398 if (!context || !context->internal ||
4399 !context->internal->_initialized) {
4401 errno = EINVAL; /* Best I can think of ... */
4413 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
4415 if (smbc_parse_path(context, fname,
4416 server, sizeof(server),
4417 share, sizeof(share),
4420 password, sizeof(password),
4426 if (user[0] == (char)0) fstrcpy(user, context->user);
4428 fstrcpy(workgroup, context->workgroup);
4430 srv = smbc_server(context, server, share, workgroup, user, password);
4432 return -1; /* errno set by smbc_server */
4435 if (! srv->no_nt_session) {
4436 ipc_srv = smbc_attr_server(context, server, share,
4437 workgroup, user, password,
4439 srv->no_nt_session = True;
4445 return -1; /* errno set by smbc_attr_server */
4448 ctx = talloc_init("smbc_removexattr");
4454 /* Are they asking to set the entire ACL? */
4455 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4456 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
4459 ret = cacl_set(ctx, &srv->cli,
4460 &ipc_srv->cli, &pol, path,
4461 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
4462 talloc_destroy(ctx);
4467 * Are they asking to remove one or more spceific security descriptor
4470 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4471 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4472 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
4473 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4474 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
4475 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4476 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
4479 ret = cacl_set(ctx, &srv->cli,
4480 &ipc_srv->cli, &pol, path,
4481 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
4482 talloc_destroy(ctx);
4486 /* Unsupported attribute name */
4487 talloc_destroy(ctx);
4492 int smbc_listxattr_ctx(SMBCCTX *context,
4498 * This isn't quite what listxattr() is supposed to do. This returns
4499 * the complete set of attribute names, always, rather than only those
4500 * attribute names which actually exist for a file. Hmmm...
4502 const char supported[] =
4505 "system.nt_sec_desc.revision\0"
4506 "system.nt_sec_desc.owner\0"
4507 "system.nt_sec_desc.owner+\0"
4508 "system.nt_sec_desc.group\0"
4509 "system.nt_sec_desc.group+\0"
4510 "system.nt_sec_desc.acl\0"
4511 "system.nt_sec_desc.acl+\0"
4512 "system.nt_sec_desc.*\0"
4513 "system.nt_sec_desc.*+\0"
4514 "system.dos_attr.*\0"
4515 "system.dos_attr.mode\0"
4516 "system.dos_attr.c_time\0"
4517 "system.dos_attr.a_time\0"
4518 "system.dos_attr.m_time\0"
4522 return sizeof(supported);
4525 if (sizeof(supported) > size) {
4530 /* this can't be strcpy() because there are embedded null characters */
4531 memcpy(list, supported, sizeof(supported));
4532 return sizeof(supported);
4537 * Open a print file to be written to by other calls
4540 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
4542 fstring server, share, user, password;
4545 if (!context || !context->internal ||
4546 !context->internal->_initialized) {
4560 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
4562 if (smbc_parse_path(context, fname,
4563 server, sizeof(server),
4564 share, sizeof(share),
4567 password, sizeof(password),
4573 /* What if the path is empty, or the file exists? */
4575 return context->open(context, fname, O_WRONLY, 666);
4580 * Routine to print a file on a remote server ...
4582 * We open the file, which we assume to be on a remote server, and then
4583 * copy it to a print file on the share specified by printq.
4586 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
4588 SMBCFILE *fid1, *fid2;
4589 int bytes, saverr, tot_bytes = 0;
4592 if (!c_file || !c_file->internal->_initialized || !c_print ||
4593 !c_print->internal->_initialized) {
4600 if (!fname && !printq) {
4607 /* Try to open the file for reading ... */
4609 if ((int)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
4611 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
4612 return -1; /* smbc_open sets errno */
4616 /* Now, try to open the printer file for writing */
4618 if ((int)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
4620 saverr = errno; /* Save errno */
4621 c_file->close(c_file, fid1);
4627 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
4631 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
4634 c_file->close(c_file, fid1);
4635 c_print->close(c_print, fid2);
4644 c_file->close(c_file, fid1); /* We have to close these anyway */
4645 c_print->close(c_print, fid2);
4659 * Routine to list print jobs on a printer share ...
4662 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
4665 fstring server, share, user, password, workgroup;
4668 if (!context || !context->internal ||
4669 !context->internal->_initialized) {
4683 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
4685 if (smbc_parse_path(context, fname,
4686 server, sizeof(server),
4687 share, sizeof(share),
4690 password, sizeof(password),
4696 if (user[0] == (char)0) fstrcpy(user, context->user);
4698 fstrcpy(workgroup, context->workgroup);
4700 srv = smbc_server(context, server, share, workgroup, user, password);
4704 return -1; /* errno set by smbc_server */
4708 if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
4710 errno = smbc_errno(context, &srv->cli);
4720 * Delete a print job from a remote printer share
4723 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
4726 fstring server, share, user, password, workgroup;
4730 if (!context || !context->internal ||
4731 !context->internal->_initialized) {
4745 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
4747 if (smbc_parse_path(context, fname,
4748 server, sizeof(server),
4749 share, sizeof(share),
4752 password, sizeof(password),
4758 if (user[0] == (char)0) fstrcpy(user, context->user);
4760 fstrcpy(workgroup, context->workgroup);
4762 srv = smbc_server(context, server, share, workgroup, user, password);
4766 return -1; /* errno set by smbc_server */
4770 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
4773 errno = smbc_errno(context, &srv->cli);
4774 else if (err == ERRnosuchprintjob)
4785 * Get a new empty handle to fill in with your own info
4787 SMBCCTX * smbc_new_context(void)
4791 context = SMB_MALLOC_P(SMBCCTX);
4797 ZERO_STRUCTP(context);
4799 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
4800 if (!context->internal) {
4805 ZERO_STRUCTP(context->internal);
4808 /* ADD REASONABLE DEFAULTS */
4810 context->timeout = 20000; /* 20 seconds */
4812 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
4813 context->options.urlencode_readdir_entries = False;/* backward compat */
4814 context->options.one_share_per_server = False;/* backward compat */
4816 context->open = smbc_open_ctx;
4817 context->creat = smbc_creat_ctx;
4818 context->read = smbc_read_ctx;
4819 context->write = smbc_write_ctx;
4820 context->close = smbc_close_ctx;
4821 context->unlink = smbc_unlink_ctx;
4822 context->rename = smbc_rename_ctx;
4823 context->lseek = smbc_lseek_ctx;
4824 context->stat = smbc_stat_ctx;
4825 context->fstat = smbc_fstat_ctx;
4826 context->opendir = smbc_opendir_ctx;
4827 context->closedir = smbc_closedir_ctx;
4828 context->readdir = smbc_readdir_ctx;
4829 context->getdents = smbc_getdents_ctx;
4830 context->mkdir = smbc_mkdir_ctx;
4831 context->rmdir = smbc_rmdir_ctx;
4832 context->telldir = smbc_telldir_ctx;
4833 context->lseekdir = smbc_lseekdir_ctx;
4834 context->fstatdir = smbc_fstatdir_ctx;
4835 context->chmod = smbc_chmod_ctx;
4836 context->utimes = smbc_utimes_ctx;
4837 context->setxattr = smbc_setxattr_ctx;
4838 context->getxattr = smbc_getxattr_ctx;
4839 context->removexattr = smbc_removexattr_ctx;
4840 context->listxattr = smbc_listxattr_ctx;
4841 context->open_print_job = smbc_open_print_job_ctx;
4842 context->print_file = smbc_print_file_ctx;
4843 context->list_print_jobs = smbc_list_print_jobs_ctx;
4844 context->unlink_print_job = smbc_unlink_print_job_ctx;
4846 context->callbacks.check_server_fn = smbc_check_server;
4847 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
4849 smbc_default_cache_functions(context);
4857 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
4858 * and thus you'll be leaking memory if not handled properly.
4861 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
4870 DEBUG(1,("Performing aggressive shutdown.\n"));
4872 f = context->internal->_files;
4874 context->close(context, f);
4877 context->internal->_files = NULL;
4879 /* First try to remove the servers the nice way. */
4880 if (context->callbacks.purge_cached_fn(context)) {
4883 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
4884 s = context->internal->_servers;
4886 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n", s, s->cli.fd));
4887 cli_shutdown(&s->cli);
4888 context->callbacks.remove_cached_srv_fn(context, s);
4890 DLIST_REMOVE(context->internal->_servers, s);
4894 context->internal->_servers = NULL;
4898 /* This is the polite way */
4899 if (context->callbacks.purge_cached_fn(context)) {
4900 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
4904 if (context->internal->_servers) {
4905 DEBUG(1, ("Active servers in context, free_context failed.\n"));
4909 if (context->internal->_files) {
4910 DEBUG(1, ("Active files in context, free_context failed.\n"));
4916 /* Things we have to clean up */
4917 SAFE_FREE(context->workgroup);
4918 SAFE_FREE(context->netbios_name);
4919 SAFE_FREE(context->user);
4921 DEBUG(3, ("Context %p succesfully freed\n", context));
4922 SAFE_FREE(context->internal);
4929 * Initialise the library etc
4931 * We accept a struct containing handle information.
4932 * valid values for info->debug from 0 to 100,
4933 * and insist that info->fn must be non-null.
4935 SMBCCTX * smbc_init_context(SMBCCTX * context)
4939 char *user = NULL, *home = NULL;
4941 if (!context || !context->internal) {
4946 /* Do not initialise the same client twice */
4947 if (context->internal->_initialized) {
4951 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
4958 if (!smbc_initialized) {
4959 /* Do some library wide intialisations the first time we get called */
4961 /* Set this to what the user wants */
4962 DEBUGLEVEL = context->debug;
4964 setup_logging( "libsmbclient", True);
4966 /* Here we would open the smb.conf file if needed ... */
4968 home = getenv("HOME");
4970 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
4972 load_interfaces(); /* Load the list of interfaces ... */
4974 in_client = True; /* FIXME, make a param */
4976 if (!lp_load(conf, True, False, False)) {
4979 * Well, if that failed, try the dyn_CONFIGFILE
4980 * Which points to the standard locn, and if that
4981 * fails, silently ignore it and use the internal
4985 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
4986 DEBUG(5, ("Could not load either config file: %s or %s\n",
4987 conf, dyn_CONFIGFILE));
4991 reopen_logs(); /* Get logging working ... */
4994 * Block SIGPIPE (from lib/util_sock.c: write())
4995 * It is not needed and should not stop execution
4997 BlockSignals(True, SIGPIPE);
4999 /* Done with one-time initialisation */
5000 smbc_initialized = 1;
5004 if (!context->user) {
5006 * FIXME: Is this the best way to get the user info?
5008 user = getenv("USER");
5009 /* walk around as "guest" if no username can be found */
5010 if (!user) context->user = SMB_STRDUP("guest");
5011 else context->user = SMB_STRDUP(user);
5014 if (!context->netbios_name) {
5016 * We try to get our netbios name from the config. If that fails we fall
5017 * back on constructing our netbios name from our hostname etc
5019 if (global_myname()) {
5020 context->netbios_name = SMB_STRDUP(global_myname());
5024 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
5027 context->netbios_name = SMB_MALLOC(17);
5028 if (!context->netbios_name) {
5032 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
5036 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
5038 if (!context->workgroup) {
5039 if (lp_workgroup()) {
5040 context->workgroup = SMB_STRDUP(lp_workgroup());
5043 /* TODO: Think about a decent default workgroup */
5044 context->workgroup = SMB_STRDUP("samba");
5048 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
5050 /* shortest timeout is 1 second */
5051 if (context->timeout > 0 && context->timeout < 1000)
5052 context->timeout = 1000;
5055 * FIXME: Should we check the function pointers here?
5058 context->internal->_initialized = 1;
5064 /* Return the verion of samba, and thus libsmbclient */
5068 return samba_version_string();