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
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"
30 * Internal flags for extended attributes
33 /* internal mode values */
34 #define SMBC_XATTR_MODE_ADD 1
35 #define SMBC_XATTR_MODE_REMOVE 2
36 #define SMBC_XATTR_MODE_REMOVE_ALL 3
37 #define SMBC_XATTR_MODE_SET 4
38 #define SMBC_XATTR_MODE_CHOWN 5
39 #define SMBC_XATTR_MODE_CHGRP 6
41 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
43 /*We should test for this in configure ... */
45 #define ENOTSUP EOPNOTSUPP
49 * Functions exported by libsmb_cache.c that we need here
51 int smbc_default_cache_functions(SMBCCTX
*context
);
54 * check if an element is part of the list.
55 * FIXME: Does not belong here !
56 * Can anyone put this in a macro in dlinklist.h ?
59 static int DLIST_CONTAINS(SMBCFILE
* list
, SMBCFILE
*p
) {
60 if (!p
|| !list
) return False
;
62 if (p
== list
) return True
;
68 extern BOOL in_client
;
71 * Is the logging working / configfile read ?
73 static int smbc_initialized
= 0;
76 hex2int( unsigned int _char
)
78 if ( _char
>= 'A' && _char
<='F')
79 return _char
- 'A' + 10;
80 if ( _char
>= 'a' && _char
<='f')
81 return _char
- 'a' + 10;
82 if ( _char
>= '0' && _char
<='9')
88 decode_urlpart(char *segment
, size_t sizeof_segment
)
90 int old_length
= strlen(segment
);
95 char *new_usegment
= 0;
101 /* make a copy of the old one */
102 new_usegment
= (char*)malloc( old_length
* 3 + 1 );
104 while( i
< old_length
) {
105 int bReencode
= False
;
106 unsigned char character
= segment
[ i
++ ];
107 if ((character
<= ' ') || (character
> 127))
110 new_usegment
[ new_length2
++ ] = character
;
111 if (character
== '%' ) {
112 int a
= i
+1 < old_length
? hex2int( segment
[i
] ) : -1;
113 int b
= i
+1 < old_length
? hex2int( segment
[i
+1] ) : -1;
114 if ((a
== -1) || (b
== -1)) { /* Only replace if sequence is valid */
115 /* Contains stray %, make sure to re-encode! */
118 /* Valid %xx sequence */
119 character
= a
* 16 + b
; /* Replace with value of %dd */
121 break; /* Stop at %00 */
123 new_usegment
[ new_length2
++ ] = (unsigned char) segment
[i
++];
124 new_usegment
[ new_length2
++ ] = (unsigned char) segment
[i
++];
128 unsigned int c
= character
/ 16;
130 new_usegment
[ new_length2
++ ] = '%';
132 c
+= (c
> 9) ? ('A' - 10) : '0';
133 new_usegment
[ new_length2
++ ] = c
;
136 c
+= (c
> 9) ? ('A' - 10) : '0';
137 new_usegment
[ new_length2
++ ] = c
;
140 new_segment
[ new_length
++ ] = character
;
142 new_segment
[ new_length
] = 0;
146 /* realloc it with unix charset */
147 pull_utf8_allocate(&new_usegment
, new_segment
);
149 /* this assumes (very safely) that removing %aa sequences
150 only shortens the string */
151 strncpy(segment
, new_usegment
, sizeof_segment
);
157 * Function to parse a path and turn it into components
159 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
160 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
161 * general format ("smb:" only; we do not look for "cifs:"), and expand on
162 * what he calls "context", herein called "options" to avoid conflict with the
163 * SMBCCTX context used throughout this library. We add the "mb" keyword
164 * which applies as follows:
168 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]][?options]
172 * smb:// show all workgroups known by the first master browser found
173 * smb://?mb=.any same as smb:// (i.e. without any options)
175 * smb://?mb=.all show all workgroups known by every master browser found.
176 * Why might you want this? In an "appliance" application
177 * where the workgroup/domain being used on the local network
178 * is not known ahead of time, but where one wanted to
179 * provide network services via samba, a unique workgroup
180 * could be used. However, when the appliance is first
181 * started, the local samba instance's master browser has not
182 * synchronized with the other master browser(s) on the
183 * network (and might not synchronize for 12 minutes) and
184 * therefore is not aware of the workgroup/ domain names
185 * available on the network. This option may be used to
186 * overcome the problem of a libsmbclient application
187 * arbitrarily selecting the local (still ignorant) master
188 * browser to obtain its list of workgroups/domains and
189 * getting back a practically emmpty list. By requesting
190 * the list of workgroups/domains from each found master
191 * browser on the local network, a complete list of
192 * workgroups/domains can be built.
194 * smb://?mb=name NOT YET IMPLEMENTED -- show all workgroups known by the
195 * master browser whose name is "name"
197 * smb://name/ if name<1D> or name<1B> exists, list servers in
198 * workgroup, else, if name<20> exists, list all shares
201 * If "options" are provided, this function returns the entire option list as
202 * a string, for later parsing by the caller.
205 static const char *smbc_prefix
= "smb:";
208 smbc_parse_path(SMBCCTX
*context
,
210 char *server
, int server_len
,
211 char *share
, int share_len
,
212 char *path
, int path_len
,
213 char *user
, int user_len
,
214 char *password
, int password_len
,
215 char *options
, int options_len
)
223 server
[0] = share
[0] = path
[0] = user
[0] = password
[0] = (char)0;
224 if (options
!= NULL
&& options_len
> 0) {
225 options
[0] = (char)0;
229 /* see if it has the right prefix */
230 len
= strlen(smbc_prefix
);
231 if (strncmp(s
,smbc_prefix
,len
) || (s
[len
] != '/' && s
[len
] != 0)) {
232 return -1; /* What about no smb: ? */
237 /* Watch the test below, we are testing to see if we should exit */
239 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
241 DEBUG(1, ("Invalid path (does not begin with smb://"));
246 p
+= 2; /* Skip the double slash */
248 /* See if any options were specified */
249 if ( (q
= strrchr(p
, '?')) != NULL
) {
250 /* There are options. Null terminate here and point to them */
253 DEBUG(4, ("Found options '%s'", q
));
255 /* Copy the options */
256 if (options
!= NULL
&& options_len
> 0) {
257 safe_strcpy(options
, q
, options_len
- 1);
266 strncpy(server
, context
->workgroup
,
267 (strlen(context
->workgroup
) < 16)?strlen(context
->workgroup
):16);
273 * ok, its for us. Now parse out the server, share etc.
275 * However, we want to parse out [[domain;]user[:password]@] if it
279 /* check that '@' occurs before '/', if '/' exists at all */
280 q
= strchr_m(p
, '@');
281 r
= strchr_m(p
, '/');
282 if (q
&& (!r
|| q
< r
)) {
283 pstring username
, passwd
, domain
;
284 const char *u
= userinfo
;
286 next_token(&p
, userinfo
, "@", sizeof(fstring
));
288 username
[0] = passwd
[0] = domain
[0] = 0;
290 if (strchr_m(u
, ';')) {
292 next_token(&u
, domain
, ";", sizeof(fstring
));
296 if (strchr_m(u
, ':')) {
298 next_token(&u
, username
, ":", sizeof(fstring
));
305 pstrcpy(username
, u
);
310 strncpy(user
, username
, user_len
); /* FIXME, domain */
313 strncpy(password
, passwd
, password_len
);
317 if (!next_token(&p
, server
, "/", sizeof(fstring
))) {
323 if (*p
== (char)0) goto decoding
; /* That's it ... */
325 if (!next_token(&p
, share
, "/", sizeof(fstring
))) {
331 safe_strcpy(path
, p
, path_len
- 1);
333 all_string_sub(path
, "/", "\\", 0);
336 decode_urlpart(path
, path_len
);
337 decode_urlpart(server
, server_len
);
338 decode_urlpart(share
, share_len
);
339 decode_urlpart(user
, user_len
);
340 decode_urlpart(password
, password_len
);
346 * Verify that the options specified in a URL are valid
348 static int smbc_check_options(char *server
, char *share
, char *path
, char *options
)
350 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' path='%s' options='%s'\n", server
, share
, path
, options
));
352 /* No options at all is always ok */
353 if (! *options
) return 0;
356 * For right now, we only support a very few options possibilities.
357 * No options are supported if server, share, or path are not empty.
358 * If all are empty, then we support the following two choices right
364 if ((*server
|| *share
|| *path
) && *options
) {
365 /* Invalid: options provided with server, share, or path */
366 DEBUG(1, ("Found unsupported options (%s) with non-empty server, share, or path\n", options
));
370 if (strcmp(options
, "mb=.any") != 0 &&
371 strcmp(options
, "mb=.all") != 0) {
372 DEBUG(1, ("Found unsupported options (%s)\n", options
));
380 * Convert an SMB error into a UNIX error ...
382 static int smbc_errno(SMBCCTX
*context
, struct cli_state
*c
)
384 int ret
= cli_errno(c
);
386 if (cli_is_dos_error(c
)) {
390 cli_dos_error(c
, &eclass
, &ecode
);
392 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
393 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
397 status
= cli_nt_error(c
);
399 DEBUG(3,("smbc errno %s -> %d\n",
400 nt_errstr(status
), ret
));
408 * returns 0 if the server is in shape. Returns 1 on error
410 * Also useable outside libsmbclient to enable external cache
411 * to do some checks too.
413 int smbc_check_server(SMBCCTX
* context
, SMBCSRV
* server
)
415 if ( send_keepalive(server
->cli
.fd
) == False
)
418 /* connection is ok */
423 * Remove a server from the cached server list it's unused.
424 * On success, 0 is returned. 1 is returned if the server could not be removed.
426 * Also useable outside libsmbclient
428 int smbc_remove_unused_server(SMBCCTX
* context
, SMBCSRV
* srv
)
432 /* are we being fooled ? */
433 if (!context
|| !context
->internal
||
434 !context
->internal
->_initialized
|| !srv
) return 1;
437 /* Check all open files/directories for a relation with this server */
438 for (file
= context
->internal
->_files
; file
; file
=file
->next
) {
439 if (file
->srv
== srv
) {
441 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
447 DLIST_REMOVE(context
->internal
->_servers
, srv
);
449 cli_shutdown(&srv
->cli
);
451 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
453 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
460 SMBCSRV
*find_server(SMBCCTX
*context
,
472 srv
= context
->callbacks
.get_cached_srv_fn(context
, server
, share
,
473 workgroup
, username
);
475 if (!auth_called
&& !srv
&& (!username
[0] || !password
[0])) {
476 context
->callbacks
.auth_fn(server
, share
,
477 workgroup
, sizeof(fstring
),
478 username
, sizeof(fstring
),
479 password
, sizeof(fstring
));
481 * However, smbc_auth_fn may have picked up info relating to
482 * an existing connection, so try for an existing connection
486 goto check_server_cache
;
491 if (context
->callbacks
.check_server_fn(context
, srv
)) {
493 * This server is no good anymore
494 * Try to remove it and check for more possible
495 * servers in the cache
497 if (context
->callbacks
.remove_unused_server_fn(context
,
500 * We could not remove the server completely,
501 * remove it from the cache so we will not get
502 * it again. It will be removed when the last
503 * file/dir is closed.
505 context
->callbacks
.remove_cached_srv_fn(context
,
510 * Maybe there are more cached connections to this
513 goto check_server_cache
;
522 * Connect to a server, possibly on an existing connection
524 * Here, what we want to do is: If the server and username
525 * match an existing connection, reuse that, otherwise, establish a
528 * If we have to create a new connection, call the auth_fn to get the
529 * info we need, unless the username and password were passed in.
532 SMBCSRV
*smbc_server(SMBCCTX
*context
,
533 const char *server
, const char *share
,
534 fstring workgroup
, fstring username
,
539 struct nmb_name called
, calling
;
540 const char *server_n
= server
;
543 int tried_reverse
= 0;
548 if (server
[0] == 0) {
553 srv
= find_server(context
, server
, share
,
554 workgroup
, username
, password
);
558 make_nmb_name(&calling
, context
->netbios_name
, 0x0);
559 make_nmb_name(&called
, server
, 0x20);
561 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n
, server
));
563 #if 0 /* djl: obsolete code? neither group nor p is used beyond here */
564 if ((p
=strchr_m(server_n
,'#')) &&
565 (strcmp(p
+1,"1D")==0 || strcmp(p
+1,"01")==0)) {
567 fstrcpy(group
, server_n
);
568 p
= strchr_m(group
,'#');
574 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
577 slprintf(ipenv
,sizeof(ipenv
)-1,"HOST_%s", server_n
);
581 /* have to open a new connection */
582 if (!cli_initialise(&c
)) {
587 c
.timeout
= context
->timeout
;
589 /* Force use of port 139 for first try, so browse lists can work */
592 if (!cli_connect(&c
, server_n
, &ip
)) {
594 * Port 139 connection failed. Try port 445 to handle
595 * connections to newer (e.g. XP) hosts with NetBIOS disabled.
598 if (!cli_connect(&c
, server_n
, &ip
)) {
604 if (!cli_session_request(&c
, &calling
, &called
)) {
606 if (strcmp(called
.name
, "*SMBSERVER")) {
607 make_nmb_name(&called
, "*SMBSERVER", 0x20);
610 else { /* Try one more time, but ensure we don't loop */
612 /* Only try this if server is an IP address ... */
614 if (is_ipaddress(server
) && !tried_reverse
) {
616 struct in_addr rem_ip
;
618 if ((rem_ip
.s_addr
=inet_addr(server
)) == INADDR_NONE
) {
619 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server
));
624 tried_reverse
++; /* Yuck */
626 if (name_status_find("*", 0, 0, rem_ip
, remote_name
)) {
627 make_nmb_name(&called
, remote_name
, 0x20);
638 DEBUG(4,(" session request ok\n"));
640 if (!cli_negprot(&c
)) {
646 if (!cli_session_setup(&c
, username
,
647 password
, strlen(password
),
648 password
, strlen(password
),
650 /* try an anonymous login if it failed */
651 !cli_session_setup(&c
, "", "", 1,"", 0, workgroup
)) {
657 DEBUG(4,(" session setup ok\n"));
659 if (!cli_send_tconX(&c
, share
, "?????",
660 password
, strlen(password
)+1)) {
661 errno
= smbc_errno(context
, &c
);
666 DEBUG(4,(" tconx ok\n"));
669 * Ok, we have got a nice connection
670 * Let's find a free server_fd
673 srv
= (SMBCSRV
*)malloc(sizeof(*srv
));
681 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
683 /* now add it to the cache (internal or external) */
684 /* Let the cache function set errno if it wants to */
686 if (context
->callbacks
.add_cached_srv_fn(context
, srv
, server
, share
, workgroup
, username
)) {
687 int saved_errno
= errno
;
688 DEBUG(3, (" Failed to add server to cache\n"));
696 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
697 server
, share
, srv
));
699 DLIST_ADD(context
->internal
->_servers
, srv
);
704 if (!srv
) return NULL
;
711 * Connect to a server for getting/setting attributes, possibly on an existing
712 * connection. This works similarly to smbc_server().
714 SMBCSRV
*smbc_attr_server(SMBCCTX
*context
,
715 const char *server
, const char *share
,
717 fstring username
, fstring password
,
721 struct cli_state
*ipc_cli
;
723 SMBCSRV
*ipc_srv
=NULL
;
726 * See if we've already created this special connection. Reference
727 * our "special" share name 'IPC$$'.
729 ipc_srv
= find_server(context
, server
, "IPC$$",
730 workgroup
, username
, password
);
733 /* We didn't find a cached connection. Get the password */
734 if (*password
== '\0') {
735 /* ... then retrieve it now. */
736 context
->callbacks
.auth_fn(server
, share
,
737 workgroup
, sizeof(fstring
),
738 username
, sizeof(fstring
),
739 password
, sizeof(fstring
));
743 nt_status
= cli_full_connection(&ipc_cli
,
744 global_myname(), server
,
745 &ip
, 0, "IPC$", "?????",
749 if (! NT_STATUS_IS_OK(nt_status
)) {
750 DEBUG(1,("cli_full_connection failed! (%s)\n",
751 nt_errstr(nt_status
)));
756 if (!cli_nt_session_open(ipc_cli
, PI_LSARPC
)) {
757 DEBUG(1, ("cli_nt_session_open fail!\n"));
759 cli_shutdown(ipc_cli
);
763 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
764 but NT sends 0x2000000 so we might as well do it too. */
766 nt_status
= cli_lsa_open_policy(ipc_cli
,
769 GENERIC_EXECUTE_ACCESS
,
772 if (!NT_STATUS_IS_OK(nt_status
)) {
773 errno
= smbc_errno(context
, ipc_cli
);
774 cli_shutdown(ipc_cli
);
778 ipc_srv
= (SMBCSRV
*)malloc(sizeof(*ipc_srv
));
781 cli_shutdown(ipc_cli
);
785 ZERO_STRUCTP(ipc_srv
);
786 ipc_srv
->cli
= *ipc_cli
;
790 /* now add it to the cache (internal or external) */
792 errno
= 0; /* let cache function set errno if it likes */
793 if (context
->callbacks
.add_cached_srv_fn(context
, ipc_srv
,
798 DEBUG(3, (" Failed to add server to cache\n"));
802 cli_shutdown(&ipc_srv
->cli
);
807 DLIST_ADD(context
->internal
->_servers
, ipc_srv
);
814 * Routine to open() a file ...
817 static SMBCFILE
*smbc_open_ctx(SMBCCTX
*context
, const char *fname
, int flags
, mode_t mode
)
819 fstring server
, share
, user
, password
, workgroup
;
822 SMBCFILE
*file
= NULL
;
825 if (!context
|| !context
->internal
||
826 !context
->internal
->_initialized
) {
828 errno
= EINVAL
; /* Best I can think of ... */
840 if (smbc_parse_path(context
, fname
,
841 server
, sizeof(server
),
842 share
, sizeof(share
),
845 password
, sizeof(password
),
851 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
853 fstrcpy(workgroup
, context
->workgroup
);
855 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
859 if (errno
== EPERM
) errno
= EACCES
;
860 return NULL
; /* smbc_server sets errno */
864 /* Hmmm, the test for a directory is suspect here ... FIXME */
866 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
873 file
= malloc(sizeof(SMBCFILE
));
884 if ((fd
= cli_open(&srv
->cli
, path
, flags
, DENY_NONE
)) < 0) {
886 /* Handle the error ... */
889 errno
= smbc_errno(context
, &srv
->cli
);
894 /* Fill in file struct */
897 file
->fname
= strdup(fname
);
902 DLIST_ADD(context
->internal
->_files
, file
);
907 /* Check if opendir needed ... */
912 eno
= smbc_errno(context
, &srv
->cli
);
913 file
= context
->opendir(context
, fname
);
914 if (!file
) errno
= eno
;
919 errno
= EINVAL
; /* FIXME, correct errno ? */
925 * Routine to create a file
928 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
930 static SMBCFILE
*smbc_creat_ctx(SMBCCTX
*context
, const char *path
, mode_t mode
)
933 if (!context
|| !context
->internal
||
934 !context
->internal
->_initialized
) {
941 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
945 * Routine to read() a file ...
948 static ssize_t
smbc_read_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
952 if (!context
|| !context
->internal
||
953 !context
->internal
->_initialized
) {
960 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
962 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
969 /* Check that the buffer exists ... */
978 ret
= cli_read(&file
->srv
->cli
, file
->cli_fd
, buf
, file
->offset
, count
);
982 errno
= smbc_errno(context
, &file
->srv
->cli
);
989 DEBUG(4, (" --> %d\n", ret
));
991 return ret
; /* Success, ret bytes of data ... */
996 * Routine to write() a file ...
999 static ssize_t
smbc_write_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
1003 if (!context
|| !context
->internal
||
1004 !context
->internal
->_initialized
) {
1011 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1018 /* Check that the buffer exists ... */
1027 ret
= cli_write(&file
->srv
->cli
, file
->cli_fd
, 0, buf
, file
->offset
, count
);
1031 errno
= smbc_errno(context
, &file
->srv
->cli
);
1036 file
->offset
+= ret
;
1038 return ret
; /* Success, 0 bytes of data ... */
1042 * Routine to close() a file ...
1045 static int smbc_close_ctx(SMBCCTX
*context
, SMBCFILE
*file
)
1049 if (!context
|| !context
->internal
||
1050 !context
->internal
->_initialized
) {
1057 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1067 return context
->closedir(context
, file
);
1071 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
1073 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1075 /* Deallocate slot and remove the server
1076 * from the server cache if unused */
1077 errno
= smbc_errno(context
, &file
->srv
->cli
);
1079 DLIST_REMOVE(context
->internal
->_files
, file
);
1080 SAFE_FREE(file
->fname
);
1082 context
->callbacks
.remove_unused_server_fn(context
, srv
);
1088 DLIST_REMOVE(context
->internal
->_files
, file
);
1089 SAFE_FREE(file
->fname
);
1096 * Get info from an SMB server on a file. Use a qpathinfo call first
1097 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1099 static BOOL
smbc_getatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
1100 uint16
*mode
, size_t *size
,
1101 time_t *c_time
, time_t *a_time
, time_t *m_time
,
1105 if (!context
|| !context
->internal
||
1106 !context
->internal
->_initialized
) {
1113 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1115 if (!srv
->no_pathinfo2
&&
1116 cli_qpathinfo2(&srv
->cli
, path
, c_time
, a_time
, m_time
, NULL
,
1117 size
, mode
, ino
)) return True
;
1119 /* if this is NT then don't bother with the getatr */
1120 if (srv
->cli
.capabilities
& CAP_NT_SMBS
) {
1125 if (cli_getatr(&srv
->cli
, path
, mode
, size
, m_time
)) {
1126 a_time
= c_time
= m_time
;
1127 srv
->no_pathinfo2
= True
;
1137 * Routine to unlink() a file
1140 static int smbc_unlink_ctx(SMBCCTX
*context
, const char *fname
)
1142 fstring server
, share
, user
, password
, workgroup
;
1144 SMBCSRV
*srv
= NULL
;
1146 if (!context
|| !context
->internal
||
1147 !context
->internal
->_initialized
) {
1149 errno
= EINVAL
; /* Best I can think of ... */
1161 if (smbc_parse_path(context
, fname
,
1162 server
, sizeof(server
),
1163 share
, sizeof(share
),
1166 password
, sizeof(password
),
1172 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1174 fstrcpy(workgroup
, context
->workgroup
);
1176 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1180 return -1; /* smbc_server sets errno */
1184 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1186 int job = smbc_stat_printjob(srv, path, NULL, NULL);
1192 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
1200 if (!cli_unlink(&srv
->cli
, path
)) {
1202 errno
= smbc_errno(context
, &srv
->cli
);
1204 if (errno
== EACCES
) { /* Check if the file is a directory */
1209 time_t m_time
= 0, a_time
= 0, c_time
= 0;
1212 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1213 &c_time
, &a_time
, &m_time
, &ino
)) {
1215 /* Hmmm, bad error ... What? */
1217 errno
= smbc_errno(context
, &srv
->cli
);
1223 if (IS_DOS_DIR(mode
))
1226 errno
= saverr
; /* Restore this */
1235 return 0; /* Success ... */
1240 * Routine to rename() a file
1243 static int smbc_rename_ctx(SMBCCTX
*ocontext
, const char *oname
,
1244 SMBCCTX
*ncontext
, const char *nname
)
1246 fstring server1
, share1
, server2
, share2
, user1
, user2
, password1
, password2
, workgroup
;
1247 pstring path1
, path2
;
1248 SMBCSRV
*srv
= NULL
;
1250 if (!ocontext
|| !ncontext
||
1251 !ocontext
->internal
|| !ncontext
->internal
||
1252 !ocontext
->internal
->_initialized
||
1253 !ncontext
->internal
->_initialized
) {
1255 errno
= EINVAL
; /* Best I can think of ... */
1260 if (!oname
|| !nname
) {
1267 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1269 smbc_parse_path(ocontext
, oname
,
1270 server1
, sizeof(server1
),
1271 share1
, sizeof(share1
),
1272 path1
, sizeof(path1
),
1273 user1
, sizeof(user1
),
1274 password1
, sizeof(password1
),
1277 if (user1
[0] == (char)0) fstrcpy(user1
, ocontext
->user
);
1279 smbc_parse_path(ncontext
, nname
,
1280 server2
, sizeof(server2
),
1281 share2
, sizeof(share2
),
1282 path2
, sizeof(path2
),
1283 user2
, sizeof(user2
),
1284 password2
, sizeof(password2
),
1287 if (user2
[0] == (char)0) fstrcpy(user2
, ncontext
->user
);
1289 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1290 strcmp(user1
, user2
)) {
1292 /* Can't rename across file systems, or users?? */
1299 fstrcpy(workgroup
, ocontext
->workgroup
);
1300 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1301 srv
= smbc_server(ocontext
, server1
, share1
, workgroup
, user1
, password1
);
1308 if (!cli_rename(&srv
->cli
, path1
, path2
)) {
1309 int eno
= smbc_errno(ocontext
, &srv
->cli
);
1311 if (eno
!= EEXIST
||
1312 !cli_unlink(&srv
->cli
, path2
) ||
1313 !cli_rename(&srv
->cli
, path1
, path2
)) {
1321 return 0; /* Success */
1326 * A routine to lseek() a file
1329 static off_t
smbc_lseek_ctx(SMBCCTX
*context
, SMBCFILE
*file
, off_t offset
, int whence
)
1333 if (!context
|| !context
->internal
||
1334 !context
->internal
->_initialized
) {
1341 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1351 return -1; /* Can't lseek a dir ... */
1357 file
->offset
= offset
;
1361 file
->offset
+= offset
;
1365 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1368 SMB_BIG_UINT b_size
= size
;
1369 if (!cli_getattrE(&file
->srv
->cli
, file
->cli_fd
, NULL
, &b_size
, NULL
, NULL
,
1377 file
->offset
= size
+ offset
;
1386 return file
->offset
;
1391 * Generate an inode number from file name for those things that need it
1395 ino_t
smbc_inode(SMBCCTX
*context
, const char *name
)
1398 if (!context
|| !context
->internal
||
1399 !context
->internal
->_initialized
) {
1406 if (!*name
) return 2; /* FIXME, why 2 ??? */
1407 return (ino_t
)str_checksum(name
);
1412 * Routine to put basic stat info into a stat structure ... Used by stat and
1417 int smbc_setup_stat(SMBCCTX
*context
, struct stat
*st
, char *fname
, size_t size
, int mode
)
1422 if (IS_DOS_DIR(mode
)) {
1423 st
->st_mode
= SMBC_DIR_MODE
;
1425 st
->st_mode
= SMBC_FILE_MODE
;
1428 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
1429 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
1430 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
1431 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
1434 #ifdef HAVE_STAT_ST_BLKSIZE
1435 st
->st_blksize
= 512;
1437 #ifdef HAVE_STAT_ST_BLOCKS
1438 st
->st_blocks
= (size
+511)/512;
1440 st
->st_uid
= getuid();
1441 st
->st_gid
= getgid();
1443 if (IS_DOS_DIR(mode
)) {
1449 if (st
->st_ino
== 0) {
1450 st
->st_ino
= smbc_inode(context
, fname
);
1453 return True
; /* FIXME: Is this needed ? */
1458 * Routine to stat a file given a name
1461 static int smbc_stat_ctx(SMBCCTX
*context
, const char *fname
, struct stat
*st
)
1464 fstring server
, share
, user
, password
, workgroup
;
1466 time_t m_time
= 0, a_time
= 0, c_time
= 0;
1471 if (!context
|| !context
->internal
||
1472 !context
->internal
->_initialized
) {
1474 errno
= EINVAL
; /* Best I can think of ... */
1486 DEBUG(4, ("smbc_stat(%s)\n", fname
));
1488 if (smbc_parse_path(context
, fname
,
1489 server
, sizeof(server
),
1490 share
, sizeof(share
),
1493 password
, sizeof(password
),
1499 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1501 fstrcpy(workgroup
, context
->workgroup
);
1503 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1506 return -1; /* errno set by smbc_server */
1509 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1510 &c_time
, &a_time
, &m_time
, &ino
)) {
1512 errno
= smbc_errno(context
, &srv
->cli
);
1519 smbc_setup_stat(context
, st
, path
, size
, mode
);
1521 st
->st_atime
= a_time
;
1522 st
->st_ctime
= c_time
;
1523 st
->st_mtime
= m_time
;
1524 st
->st_dev
= srv
->dev
;
1531 * Routine to stat a file given an fd
1534 static int smbc_fstat_ctx(SMBCCTX
*context
, SMBCFILE
*file
, struct stat
*st
)
1536 time_t c_time
, a_time
, m_time
;
1541 if (!context
|| !context
->internal
||
1542 !context
->internal
->_initialized
) {
1549 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1558 return context
->fstatdir(context
, file
, st
);
1562 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
,
1563 &mode
, &size
, &c_time
, &a_time
, &m_time
, NULL
, &ino
)) {
1564 SMB_BIG_UINT b_size
= size
;
1565 if (!cli_getattrE(&file
->srv
->cli
, file
->cli_fd
,
1566 &mode
, &b_size
, &c_time
, &a_time
, &m_time
)) {
1577 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
1579 st
->st_atime
= a_time
;
1580 st
->st_ctime
= c_time
;
1581 st
->st_mtime
= m_time
;
1582 st
->st_dev
= file
->srv
->dev
;
1589 * Routine to open a directory
1590 * We accept the URL syntax explained in smbc_parse_path(), above.
1593 static void smbc_remove_dir(SMBCFILE
*dir
)
1595 struct smbc_dir_list
*d
,*f
;
1602 SAFE_FREE(f
->dirent
);
1607 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
1611 static int add_dirent(SMBCFILE
*dir
, const char *name
, const char *comment
, uint32 type
)
1613 struct smbc_dirent
*dirent
;
1615 char *u_name
= NULL
, *u_comment
= NULL
;
1616 size_t u_name_len
= 0, u_comment_len
= 0;
1619 u_name_len
= push_utf8_allocate(&u_name
, name
);
1621 u_comment_len
= push_utf8_allocate(&u_comment
, comment
);
1624 * Allocate space for the dirent, which must be increased by the
1625 * size of the name and the comment and 1 for the null on the comment.
1626 * The null on the name is already accounted for.
1629 size
= sizeof(struct smbc_dirent
) + u_name_len
+ u_comment_len
+ 1;
1631 dirent
= malloc(size
);
1635 dir
->dir_error
= ENOMEM
;
1640 ZERO_STRUCTP(dirent
);
1642 if (dir
->dir_list
== NULL
) {
1644 dir
->dir_list
= malloc(sizeof(struct smbc_dir_list
));
1645 if (!dir
->dir_list
) {
1648 dir
->dir_error
= ENOMEM
;
1652 ZERO_STRUCTP(dir
->dir_list
);
1654 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
1658 dir
->dir_end
->next
= malloc(sizeof(struct smbc_dir_list
));
1660 if (!dir
->dir_end
->next
) {
1663 dir
->dir_error
= ENOMEM
;
1667 ZERO_STRUCTP(dir
->dir_end
->next
);
1669 dir
->dir_end
= dir
->dir_end
->next
;
1672 dir
->dir_end
->next
= NULL
;
1673 dir
->dir_end
->dirent
= dirent
;
1675 dirent
->smbc_type
= type
;
1676 dirent
->namelen
= u_name_len
;
1677 dirent
->commentlen
= u_comment_len
;
1678 dirent
->dirlen
= size
;
1680 strncpy(dirent
->name
, (u_name
?u_name
:""), dirent
->namelen
+ 1);
1682 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
1683 strncpy(dirent
->comment
, (u_comment
?u_comment
:""), dirent
->commentlen
+ 1);
1685 SAFE_FREE(u_comment
);
1693 list_unique_wg_fn(const char *name
, uint32 type
, const char *comment
, void *state
)
1695 SMBCFILE
*dir
= (SMBCFILE
*)state
;
1696 struct smbc_dir_list
*dir_list
;
1697 struct smbc_dirent
*dirent
;
1701 dirent_type
= dir
->dir_type
;
1703 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
1705 /* An error occurred, what do we do? */
1706 /* FIXME: Add some code here */
1709 /* Point to the one just added */
1710 dirent
= dir
->dir_end
->dirent
;
1712 /* See if this was a duplicate */
1713 for (dir_list
= dir
->dir_list
;
1714 dir_list
!= dir
->dir_end
;
1715 dir_list
= dir_list
->next
) {
1717 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
1718 /* Duplicate. End end of list need to be removed. */
1722 if (remove
&& dir_list
->next
== dir
->dir_end
) {
1723 /* Found the end of the list. Remove it. */
1724 dir
->dir_end
= dir_list
;
1725 free(dir_list
->next
);
1726 dir_list
->next
= NULL
;
1733 list_fn(const char *name
, uint32 type
, const char *comment
, void *state
)
1735 SMBCFILE
*dir
= (SMBCFILE
*)state
;
1738 /* We need to process the type a little ... */
1740 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
1743 case 0: /* Directory tree */
1744 dirent_type
= SMBC_FILE_SHARE
;
1748 dirent_type
= SMBC_PRINTER_SHARE
;
1752 dirent_type
= SMBC_COMMS_SHARE
;
1756 dirent_type
= SMBC_IPC_SHARE
;
1760 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
1764 else dirent_type
= dir
->dir_type
;
1766 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
1768 /* An error occurred, what do we do? */
1769 /* FIXME: Add some code here */
1775 dir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
1778 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
1779 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
1781 /* Handle an error ... */
1783 /* FIXME: Add some code ... */
1789 static SMBCFILE
*smbc_opendir_ctx(SMBCCTX
*context
, const char *fname
)
1791 fstring server
, share
, user
, password
, options
;
1794 SMBCSRV
*srv
= NULL
;
1795 SMBCFILE
*dir
= NULL
;
1796 struct in_addr rem_ip
;
1798 if (!context
|| !context
->internal
||
1799 !context
->internal
->_initialized
) {
1800 DEBUG(4, ("no valid context\n"));
1807 DEBUG(4, ("no valid fname\n"));
1812 if (smbc_parse_path(context
, fname
,
1813 server
, sizeof(server
),
1814 share
, sizeof(share
),
1817 password
, sizeof(password
),
1818 options
, sizeof(options
))) {
1819 DEBUG(4, ("no valid path\n"));
1824 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s' options='%s'\n", fname
, server
, share
, path
, options
));
1826 /* Ensure the options are valid */
1827 if (smbc_check_options(server
, share
, path
, options
)) {
1828 DEBUG(4, ("unacceptable options (%s)\n", options
));
1833 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1835 pstrcpy(workgroup
, context
->workgroup
);
1837 dir
= malloc(sizeof(*dir
));
1849 dir
->fname
= strdup(fname
);
1853 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
1855 if (server
[0] == (char)0 &&
1856 (! *options
|| strcmp(options
, "mb=.any") == 0)) {
1857 struct in_addr server_ip
;
1858 if (share
[0] != (char)0 || path
[0] != (char)0) {
1862 SAFE_FREE(dir
->fname
);
1869 * We have server and share and path empty ... so list the
1870 * workgroups first try to get the LMB for our workgroup, and
1871 * if that fails, try the DMB
1874 pstrcpy(workgroup
, lp_workgroup());
1876 if (!find_master_ip(workgroup
, &server_ip
)) {
1877 struct user_auth_info u_info
;
1878 struct cli_state
*cli
;
1880 DEBUG(4, ("Unable to find master browser for workgroup %s\n",
1883 /* find the name of the server ... */
1884 pstrcpy(u_info
.username
, user
);
1885 pstrcpy(u_info
.password
, password
);
1887 if (!(cli
= get_ipc_connect_master_ip_bcast(workgroup
, &u_info
))) {
1888 DEBUG(4, ("Unable to find master browser by "
1894 fstrcpy(server
, cli
->desthost
);
1899 * Do a name status query to find out the name of the
1900 * master browser. We use <01><02>__MSBROWSE__<02>#01 if
1901 * *#00 fails because a domain master browser will not
1902 * respond to a wildcard query (or, at least, an NT4
1903 * server acting as the domain master browser will not).
1905 * We might be able to use ONLY the query on MSBROWSE, but
1906 * that's not yet been tested with all Windows versions,
1907 * so until it is, leave the original wildcard query as
1908 * the first choice and fall back to MSBROWSE if the
1909 * wildcard query fails.
1911 if (!name_status_find("*", 0, 0x20, server_ip
, server
) &&
1912 !name_status_find(MSBROWSE
, 1, 0x1b, server_ip
, server
)) {
1918 DEBUG(4, ("using workgroup %s %s\n", workgroup
, server
));
1921 * Get a connection to IPC$ on the server if we do not already
1925 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1929 SAFE_FREE(dir
->fname
);
1936 dir
->dir_type
= SMBC_WORKGROUP
;
1938 /* Now, list the stuff ... */
1940 if (!cli_NetServerEnum(&srv
->cli
, workgroup
, SV_TYPE_DOMAIN_ENUM
, list_fn
,
1943 DEBUG(1, ("Could not enumerate domains using '%s'\n", workgroup
));
1945 SAFE_FREE(dir
->fname
);
1952 } else if (server
[0] == (char)0 &&
1953 (! *options
|| strcmp(options
, "mb=.all") == 0)) {
1957 struct ip_service
*ip_list
;
1958 struct ip_service server_addr
;
1959 struct user_auth_info u_info
;
1960 struct cli_state
*cli
;
1962 if (share
[0] != (char)0 || path
[0] != (char)0) {
1966 SAFE_FREE(dir
->fname
);
1972 pstrcpy(u_info
.username
, user
);
1973 pstrcpy(u_info
.password
, password
);
1976 * We have server and share and path empty but options
1977 * requesting that we scan all master browsers for their list
1978 * of workgroups/domains. This implies that we must first try
1979 * broadcast queries to find all master browsers, and if that
1980 * doesn't work, then try our other methods which return only
1981 * a single master browser.
1984 if (!name_resolve_bcast(MSBROWSE
, 1, &ip_list
, &count
)) {
1985 if (!find_master_ip(workgroup
, &server_addr
.ip
)) {
1991 ip_list
= &server_addr
;
1995 for (i
= 0; i
< count
; i
++) {
1996 DEBUG(99, ("Found master browser %s\n", inet_ntoa(ip_list
[i
].ip
)));
1998 cli
= get_ipc_connect_master_ip(&ip_list
[i
], workgroup
, &u_info
);
2000 /* cli == NULL is the master browser refused to talk or
2001 could not be found */
2005 fstrcpy(server
, cli
->desthost
);
2008 DEBUG(4, ("using workgroup %s %s\n", workgroup
, server
));
2011 * For each returned master browser IP address, get a
2012 * connection to IPC$ on the server if we do not
2013 * already have one, and determine the
2014 * workgroups/domains that it knows about.
2017 srv
= smbc_server(context
, server
,
2018 "IPC$", workgroup
, user
, password
);
2022 SAFE_FREE(dir
->fname
);
2029 dir
->dir_type
= SMBC_WORKGROUP
;
2031 /* Now, list the stuff ... */
2033 if (!cli_NetServerEnum(&srv
->cli
, workgroup
, SV_TYPE_DOMAIN_ENUM
, list_unique_wg_fn
,
2037 SAFE_FREE(dir
->fname
);
2047 * Server not an empty string ... Check the rest and see what
2050 if (share
[0] == (char)0) {
2052 if (path
[0] != (char)0) { /* Should not have empty share with path */
2056 SAFE_FREE(dir
->fname
);
2063 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
2064 /* However, we check to see if <server> is an IP address first */
2066 if (!is_ipaddress(server
) && /* Not an IP addr so check next */
2067 (resolve_name(server
, &rem_ip
, 0x1d) || /* Found LMB */
2068 resolve_name(server
, &rem_ip
, 0x1b) )) { /* Found DMB */
2071 dir
->dir_type
= SMBC_SERVER
;
2074 * Get the backup list ...
2078 if (!name_status_find(server
, 0, 0, rem_ip
, buserver
)) {
2080 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server
));
2081 errno
= EPERM
; /* FIXME, is this correct */
2087 * Get a connection to IPC$ on the server if we do not already have one
2090 srv
= smbc_server(context
, buserver
, "IPC$", workgroup
, user
, password
);
2093 DEBUG(0, ("got no contact to IPC$\n"));
2095 SAFE_FREE(dir
->fname
);
2104 /* Now, list the servers ... */
2106 if (!cli_NetServerEnum(&srv
->cli
, server
, 0x0000FFFE, list_fn
,
2110 SAFE_FREE(dir
->fname
);
2119 if (resolve_name(server
, &rem_ip
, 0x20)) {
2121 /* Now, list the shares ... */
2123 dir
->dir_type
= SMBC_FILE_SHARE
;
2125 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
2130 SAFE_FREE(dir
->fname
);
2139 /* Now, list the servers ... */
2141 if (cli_RNetShareEnum(&srv
->cli
, list_fn
,
2144 errno
= cli_errno(&srv
->cli
);
2146 SAFE_FREE(dir
->fname
);
2156 errno
= ECONNREFUSED
; /* Neither the workgroup nor server exists */
2158 SAFE_FREE(dir
->fname
);
2168 else { /* The server and share are specified ... work from there ... */
2170 /* Well, we connect to the server and list the directory */
2172 dir
->dir_type
= SMBC_FILE_SHARE
;
2174 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2179 SAFE_FREE(dir
->fname
);
2188 /* Now, list the files ... */
2190 pstrcat(path
, "\\*");
2192 if (cli_list(&srv
->cli
, path
, aDIR
| aSYSTEM
| aHIDDEN
, dir_list_fn
,
2196 SAFE_FREE(dir
->fname
);
2199 errno
= smbc_errno(context
, &srv
->cli
);
2207 DLIST_ADD(context
->internal
->_files
, dir
);
2213 * Routine to close a directory
2216 static int smbc_closedir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2219 if (!context
|| !context
->internal
||
2220 !context
->internal
->_initialized
) {
2227 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2234 smbc_remove_dir(dir
); /* Clean it up */
2236 DLIST_REMOVE(context
->internal
->_files
, dir
);
2240 SAFE_FREE(dir
->fname
);
2241 SAFE_FREE(dir
); /* Free the space too */
2249 * Routine to get a directory entry
2252 struct smbc_dirent
*smbc_readdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2254 struct smbc_dirent
*dirp
, *dirent
;
2256 /* Check that all is ok first ... */
2258 if (!context
|| !context
->internal
||
2259 !context
->internal
->_initialized
) {
2262 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
2267 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2270 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
2275 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2278 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
2283 if (!dir
->dir_next
) {
2288 dirent
= dir
->dir_next
->dirent
;
2296 /* Hmmm, do I even need to copy it? */
2298 memcpy(context
->internal
->_dirent
, dirent
, dirent
->dirlen
); /* Copy the dirent */
2299 dirp
= (struct smbc_dirent
*)context
->internal
->_dirent
;
2300 dirp
->comment
= (char *)(&dirp
->name
+ dirent
->namelen
+ 1);
2301 dir
->dir_next
= dir
->dir_next
->next
;
2303 return (struct smbc_dirent
*)context
->internal
->_dirent
;
2309 * Routine to get directory entries
2312 static int smbc_getdents_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct smbc_dirent
*dirp
, int count
)
2314 struct smbc_dir_list
*dirlist
;
2315 int rem
= count
, reqd
;
2316 char *ndir
= (char *)dirp
;
2318 /* Check that all is ok first ... */
2320 if (!context
|| !context
->internal
||
2321 !context
->internal
->_initialized
) {
2328 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2335 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2343 * Now, retrieve the number of entries that will fit in what was passed
2344 * We have to figure out if the info is in the list, or we need to
2345 * send a request to the server to get the info.
2348 while ((dirlist
= dir
->dir_next
)) {
2349 struct smbc_dirent
*dirent
;
2351 if (!dirlist
->dirent
) {
2353 errno
= ENOENT
; /* Bad error */
2358 if (rem
< (reqd
= (sizeof(struct smbc_dirent
) + dirlist
->dirent
->namelen
+
2359 dirlist
->dirent
->commentlen
+ 1))) {
2361 if (rem
< count
) { /* We managed to copy something */
2367 else { /* Nothing copied ... */
2369 errno
= EINVAL
; /* Not enough space ... */
2376 dirent
= dirlist
->dirent
;
2378 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
2380 ((struct smbc_dirent
*)ndir
)->comment
=
2381 (char *)(&((struct smbc_dirent
*)ndir
)->name
+ dirent
->namelen
+ 1);
2387 dir
->dir_next
= dirlist
= dirlist
-> next
;
2398 * Routine to create a directory ...
2401 static int smbc_mkdir_ctx(SMBCCTX
*context
, const char *fname
, mode_t mode
)
2404 fstring server
, share
, user
, password
, workgroup
;
2407 if (!context
|| !context
->internal
||
2408 !context
->internal
->_initialized
) {
2422 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
2424 if (smbc_parse_path(context
, fname
,
2425 server
, sizeof(server
),
2426 share
, sizeof(share
),
2429 password
, sizeof(password
),
2435 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2437 fstrcpy(workgroup
, context
->workgroup
);
2439 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2443 return -1; /* errno set by smbc_server */
2447 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2449 mode = aDIR | aRONLY;
2452 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2454 if (strcmp(path, "\\") == 0) {
2456 mode = aDIR | aRONLY;
2462 smbc_stat_printjob(srv, path, &size, &m_time);
2463 c_time = a_time = m_time;
2468 if (!cli_mkdir(&srv
->cli
, path
)) {
2470 errno
= smbc_errno(context
, &srv
->cli
);
2480 * Our list function simply checks to see if a directory is not empty
2483 static int smbc_rmdir_dirempty
= True
;
2485 static void rmdir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
2488 if (strncmp(finfo
->name
, ".", 1) != 0 && strncmp(finfo
->name
, "..", 2) != 0)
2489 smbc_rmdir_dirempty
= False
;
2494 * Routine to remove a directory
2497 static int smbc_rmdir_ctx(SMBCCTX
*context
, const char *fname
)
2500 fstring server
, share
, user
, password
, workgroup
;
2503 if (!context
|| !context
->internal
||
2504 !context
->internal
->_initialized
) {
2518 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
2520 if (smbc_parse_path(context
, fname
,
2521 server
, sizeof(server
),
2522 share
, sizeof(share
),
2525 password
, sizeof(password
),
2532 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2534 fstrcpy(workgroup
, context
->workgroup
);
2536 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2540 return -1; /* errno set by smbc_server */
2544 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2546 mode = aDIR | aRONLY;
2549 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2551 if (strcmp(path, "\\") == 0) {
2553 mode = aDIR | aRONLY;
2559 smbc_stat_printjob(srv, path, &size, &m_time);
2560 c_time = a_time = m_time;
2565 if (!cli_rmdir(&srv
->cli
, path
)) {
2567 errno
= smbc_errno(context
, &srv
->cli
);
2569 if (errno
== EACCES
) { /* Check if the dir empty or not */
2571 pstring lpath
; /* Local storage to avoid buffer overflows */
2573 smbc_rmdir_dirempty
= True
; /* Make this so ... */
2575 pstrcpy(lpath
, path
);
2576 pstrcat(lpath
, "\\*");
2578 if (cli_list(&srv
->cli
, lpath
, aDIR
| aSYSTEM
| aHIDDEN
, rmdir_list_fn
,
2581 /* Fix errno to ignore latest error ... */
2583 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2584 smbc_errno(context
, &srv
->cli
)));
2589 if (smbc_rmdir_dirempty
)
2605 * Routine to return the current directory position
2608 static off_t
smbc_telldir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2610 off_t ret_val
; /* Squash warnings about cast */
2612 if (!context
|| !context
->internal
||
2613 !context
->internal
->_initialized
) {
2620 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2627 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2635 * We return the pointer here as the offset
2637 ret_val
= (int)dir
->dir_next
;
2643 * A routine to run down the list and see if the entry is OK
2646 struct smbc_dir_list
*smbc_check_dir_ent(struct smbc_dir_list
*list
,
2647 struct smbc_dirent
*dirent
)
2650 /* Run down the list looking for what we want */
2654 struct smbc_dir_list
*tmp
= list
;
2658 if (tmp
->dirent
== dirent
)
2667 return NULL
; /* Not found, or an error */
2673 * Routine to seek on a directory
2676 static int smbc_lseekdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, off_t offset
)
2678 long int l_offset
= offset
; /* Handle problems of size */
2679 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
2680 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
2682 if (!context
|| !context
->internal
||
2683 !context
->internal
->_initialized
) {
2690 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2697 /* Now, check what we were passed and see if it is OK ... */
2699 if (dirent
== NULL
) { /* Seek to the begining of the list */
2701 dir
->dir_next
= dir
->dir_list
;
2706 /* Now, run down the list and make sure that the entry is OK */
2707 /* This may need to be changed if we change the format of the list */
2709 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
2711 errno
= EINVAL
; /* Bad entry */
2716 dir
->dir_next
= list_ent
;
2723 * Routine to fstat a dir
2726 static int smbc_fstatdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct stat
*st
)
2729 if (!context
|| !context
->internal
||
2730 !context
->internal
->_initialized
) {
2737 /* No code yet ... */
2743 int smbc_chmod_ctx(SMBCCTX
*context
, const char *fname
, mode_t newmode
)
2746 fstring server
, share
, user
, password
, workgroup
;
2750 if (!context
|| !context
->internal
||
2751 !context
->internal
->_initialized
) {
2753 errno
= EINVAL
; /* Best I can think of ... */
2765 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, newmode
));
2767 if (smbc_parse_path(context
, fname
,
2768 server
, sizeof(server
),
2769 share
, sizeof(share
),
2772 password
, sizeof(password
),
2778 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2780 fstrcpy(workgroup
, context
->workgroup
);
2782 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2785 return -1; /* errno set by smbc_server */
2790 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= aRONLY
;
2791 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= aARCH
;
2792 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= aSYSTEM
;
2793 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= aHIDDEN
;
2795 if (!cli_setatr(&srv
->cli
, path
, mode
, 0)) {
2796 errno
= smbc_errno(context
, &srv
->cli
);
2803 int smbc_utimes_ctx(SMBCCTX
*context
, const char *fname
, struct timeval
*tbuf
)
2806 fstring server
, share
, user
, password
, workgroup
;
2809 time_t t
= (tbuf
== NULL
? time(NULL
) : tbuf
->tv_sec
);
2811 if (!context
|| !context
->internal
||
2812 !context
->internal
->_initialized
) {
2814 errno
= EINVAL
; /* Best I can think of ... */
2826 DEBUG(4, ("smbc_utimes(%s, [%s])\n", fname
, ctime(&t
)));
2828 if (smbc_parse_path(context
, fname
,
2829 server
, sizeof(server
),
2830 share
, sizeof(share
),
2833 password
, sizeof(password
),
2839 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2841 fstrcpy(workgroup
, context
->workgroup
);
2843 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2846 return -1; /* errno set by smbc_server */
2849 if (!smbc_getatr(context
, srv
, path
,
2856 if (!cli_setatr(&srv
->cli
, path
, mode
, t
)) {
2857 /* some servers always refuse directory changes */
2858 if (!(mode
& aDIR
)) {
2859 errno
= smbc_errno(context
, &srv
->cli
);
2868 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
2869 However NT4 gives a "The information may have been modified by a
2870 computer running Windows NT 5.0" if denied ACEs do not appear before
2873 static int ace_compare(SEC_ACE
*ace1
, SEC_ACE
*ace2
)
2875 if (sec_ace_equal(ace1
, ace2
))
2878 if (ace1
->type
!= ace2
->type
)
2879 return ace2
->type
- ace1
->type
;
2881 if (sid_compare(&ace1
->trustee
, &ace2
->trustee
))
2882 return sid_compare(&ace1
->trustee
, &ace2
->trustee
);
2884 if (ace1
->flags
!= ace2
->flags
)
2885 return ace1
->flags
- ace2
->flags
;
2887 if (ace1
->info
.mask
!= ace2
->info
.mask
)
2888 return ace1
->info
.mask
- ace2
->info
.mask
;
2890 if (ace1
->size
!= ace2
->size
)
2891 return ace1
->size
- ace2
->size
;
2893 return memcmp(ace1
, ace2
, sizeof(SEC_ACE
));
2897 static void sort_acl(SEC_ACL
*the_acl
)
2900 if (!the_acl
) return;
2902 qsort(the_acl
->ace
, the_acl
->num_aces
, sizeof(the_acl
->ace
[0]), QSORT_CAST ace_compare
);
2904 for (i
=1;i
<the_acl
->num_aces
;) {
2905 if (sec_ace_equal(&the_acl
->ace
[i
-1], &the_acl
->ace
[i
])) {
2907 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
2908 the_acl
->ace
[j
] = the_acl
->ace
[j
+1];
2910 the_acl
->num_aces
--;
2917 /* convert a SID to a string, either numeric or username/group */
2918 static void convert_sid_to_string(struct cli_state
*ipc_cli
,
2924 char **domains
= NULL
;
2925 char **names
= NULL
;
2926 uint32
*types
= NULL
;
2928 sid_to_string(str
, sid
);
2930 if (numeric
) return; /* no lookup desired */
2932 /* Ask LSA to convert the sid to a name */
2934 if (!NT_STATUS_IS_OK(cli_lsa_lookup_sids(ipc_cli
, ipc_cli
->mem_ctx
,
2935 pol
, 1, sid
, &domains
,
2937 !domains
|| !domains
[0] || !names
|| !names
[0]) {
2943 slprintf(str
, sizeof(fstring
) - 1, "%s%s%s",
2944 domains
[0], lp_winbind_separator(),
2948 /* convert a string to a SID, either numeric or username/group */
2949 static BOOL
convert_string_to_sid(struct cli_state
*ipc_cli
,
2955 uint32
*types
= NULL
;
2956 DOM_SID
*sids
= NULL
;
2960 if (strncmp(str
, "S-", 2) == 0) {
2961 return string_to_sid(sid
, str
);
2968 if (!NT_STATUS_IS_OK(cli_lsa_lookup_names(ipc_cli
, ipc_cli
->mem_ctx
,
2969 pol
, 1, &str
, &sids
,
2975 sid_copy(sid
, &sids
[0]);
2982 /* parse an ACE in the same format as print_ace() */
2983 static BOOL
parse_ace(struct cli_state
*ipc_cli
,
2992 unsigned atype
, aflags
, amask
;
2995 const struct perm_value
*v
;
3001 /* These values discovered by inspection */
3002 static const struct perm_value special_values
[] = {
3003 { "R", 0x00120089 },
3004 { "W", 0x00120116 },
3005 { "X", 0x001200a0 },
3006 { "D", 0x00010000 },
3007 { "P", 0x00040000 },
3008 { "O", 0x00080000 },
3012 static const struct perm_value standard_values
[] = {
3013 { "READ", 0x001200a9 },
3014 { "CHANGE", 0x001301bf },
3015 { "FULL", 0x001f01ff },
3021 p
= strchr_m(str
,':');
3022 if (!p
) return False
;
3025 /* Try to parse numeric form */
3027 if (sscanf(p
, "%i/%i/%i", &atype
, &aflags
, &amask
) == 3 &&
3028 convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
3032 /* Try to parse text form */
3034 if (!convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
3039 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
3043 if (StrnCaseCmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
3044 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
3045 } else if (StrnCaseCmp(tok
, "DENIED", strlen("DENIED")) == 0) {
3046 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
3051 /* Only numeric form accepted for flags at present */
3053 if (!(next_token(&cp
, tok
, "/", sizeof(fstring
)) &&
3054 sscanf(tok
, "%i", &aflags
))) {
3058 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
3062 if (strncmp(tok
, "0x", 2) == 0) {
3063 if (sscanf(tok
, "%i", &amask
) != 1) {
3069 for (v
= standard_values
; v
->perm
; v
++) {
3070 if (strcmp(tok
, v
->perm
) == 0) {
3081 for (v
= special_values
; v
->perm
; v
++) {
3082 if (v
->perm
[0] == *p
) {
3088 if (!found
) return False
;
3098 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
3102 /* add an ACE to a list of ACEs in a SEC_ACL */
3103 static BOOL
add_ace(SEC_ACL
**the_acl
, SEC_ACE
*ace
, TALLOC_CTX
*ctx
)
3108 (*the_acl
) = make_sec_acl(ctx
, 3, 1, ace
);
3112 aces
= calloc(1+(*the_acl
)->num_aces
,sizeof(SEC_ACE
));
3113 memcpy(aces
, (*the_acl
)->ace
, (*the_acl
)->num_aces
* sizeof(SEC_ACE
));
3114 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(SEC_ACE
));
3115 new = make_sec_acl(ctx
,(*the_acl
)->revision
,1+(*the_acl
)->num_aces
, aces
);
3122 /* parse a ascii version of a security descriptor */
3123 static SEC_DESC
*sec_desc_parse(TALLOC_CTX
*ctx
,
3124 struct cli_state
*ipc_cli
,
3129 const char *p
= str
;
3133 DOM_SID
*grp_sid
=NULL
, *owner_sid
=NULL
;
3137 while (next_token(&p
, tok
, "\t,\r\n", sizeof(tok
))) {
3139 if (StrnCaseCmp(tok
,"REVISION:", 9) == 0) {
3140 revision
= strtol(tok
+9, NULL
, 16);
3144 if (StrnCaseCmp(tok
,"OWNER:", 6) == 0) {
3145 owner_sid
= (DOM_SID
*)calloc(1, sizeof(DOM_SID
));
3147 !convert_string_to_sid(ipc_cli
, pol
,
3149 owner_sid
, tok
+6)) {
3150 DEBUG(5, ("Failed to parse owner sid\n"));
3156 if (StrnCaseCmp(tok
,"OWNER+:", 7) == 0) {
3157 owner_sid
= (DOM_SID
*)calloc(1, sizeof(DOM_SID
));
3159 !convert_string_to_sid(ipc_cli
, pol
,
3161 owner_sid
, tok
+7)) {
3162 DEBUG(5, ("Failed to parse owner sid\n"));
3168 if (StrnCaseCmp(tok
,"GROUP:", 6) == 0) {
3169 grp_sid
= (DOM_SID
*)calloc(1, sizeof(DOM_SID
));
3171 !convert_string_to_sid(ipc_cli
, pol
,
3174 DEBUG(5, ("Failed to parse group sid\n"));
3180 if (StrnCaseCmp(tok
,"GROUP+:", 7) == 0) {
3181 grp_sid
= (DOM_SID
*)calloc(1, sizeof(DOM_SID
));
3183 !convert_string_to_sid(ipc_cli
, pol
,
3186 DEBUG(5, ("Failed to parse group sid\n"));
3192 if (StrnCaseCmp(tok
,"ACL:", 4) == 0) {
3194 if (!parse_ace(ipc_cli
, pol
, &ace
, numeric
, tok
+4)) {
3195 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
3198 if(!add_ace(&dacl
, &ace
, ctx
)) {
3199 DEBUG(5, ("Failed to add ACL %s\n", tok
));
3205 if (StrnCaseCmp(tok
,"ACL+:", 5) == 0) {
3207 if (!parse_ace(ipc_cli
, pol
, &ace
, False
, tok
+5)) {
3208 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
3211 if(!add_ace(&dacl
, &ace
, ctx
)) {
3212 DEBUG(5, ("Failed to add ACL %s\n", tok
));
3218 DEBUG(5, ("Failed to parse security descriptor\n"));
3222 ret
= make_sec_desc(ctx
, revision
, SEC_DESC_SELF_RELATIVE
,
3223 owner_sid
, grp_sid
, NULL
, dacl
, &sd_size
);
3226 SAFE_FREE(owner_sid
);
3232 /*****************************************************
3233 retrieve the acls for a file
3234 *******************************************************/
3235 static int cacl_get(TALLOC_CTX
*ctx
, struct cli_state
*cli
,
3236 struct cli_state
*ipc_cli
, POLICY_HND
*pol
,
3237 char *filename
, char *name
, char *buf
, int bufsize
)
3243 BOOL numeric
= True
;
3244 BOOL determine_size
= (bufsize
== 0);
3250 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
3253 DEBUG(5, ("cacl_get failed to open %s: %s\n",
3254 filename
, cli_errstr(cli
)));
3259 sd
= cli_query_secdesc(cli
, fnum
, ctx
);
3262 DEBUG(5, ("cacl_get Failed to query old descriptor\n"));
3267 cli_close(cli
, fnum
);
3269 all
= (*name
== '*');
3270 numeric
= (* (name
+ strlen(name
) - 1) != '+');
3275 if (determine_size
) {
3276 p
= talloc_asprintf(ctx
,
3277 "REVISION:%d", sd
->revision
);
3284 n
= snprintf(buf
, bufsize
,
3285 "REVISION:%d", sd
->revision
);
3287 } else if (StrCaseCmp(name
, "revision") == 0) {
3288 if (determine_size
) {
3289 p
= talloc_asprintf(ctx
, "%d", sd
->revision
);
3296 n
= snprintf(buf
, bufsize
, "%d", sd
->revision
);
3300 if (!determine_size
&& n
> bufsize
) {
3308 /* Get owner and group sid */
3310 if (sd
->owner_sid
) {
3311 convert_sid_to_string(ipc_cli
, pol
,
3312 sidstr
, numeric
, sd
->owner_sid
);
3314 fstrcpy(sidstr
, "");
3318 if (determine_size
) {
3319 p
= talloc_asprintf(ctx
, ",OWNER:%s", sidstr
);
3326 n
= snprintf(buf
, bufsize
, ",OWNER:%s", sidstr
);
3328 } else if (StrnCaseCmp(name
, "owner", 5) == 0) {
3329 if (determine_size
) {
3330 p
= talloc_asprintf(ctx
, "%s", sidstr
);
3337 n
= snprintf(buf
, bufsize
, "%s", sidstr
);
3341 if (!determine_size
&& n
> bufsize
) {
3350 convert_sid_to_string(ipc_cli
, pol
,
3351 sidstr
, numeric
, sd
->grp_sid
);
3353 fstrcpy(sidstr
, "");
3357 if (determine_size
) {
3358 p
= talloc_asprintf(ctx
, ",GROUP:%s", sidstr
);
3365 n
= snprintf(buf
, bufsize
, ",GROUP:%s", sidstr
);
3367 } else if (StrnCaseCmp(name
, "group", 5) == 0) {
3368 if (determine_size
) {
3369 p
= talloc_asprintf(ctx
, "%s", sidstr
);
3376 n
= snprintf(buf
, bufsize
, "%s", sidstr
);
3380 if (!determine_size
&& n
> bufsize
) {
3388 /* Add aces to value buffer */
3389 for (i
= 0; sd
->dacl
&& i
< sd
->dacl
->num_aces
; i
++) {
3391 SEC_ACE
*ace
= &sd
->dacl
->ace
[i
];
3392 convert_sid_to_string(ipc_cli
, pol
,
3393 sidstr
, numeric
, &ace
->trustee
);
3396 if (determine_size
) {
3397 p
= talloc_asprintf(ctx
,
3398 ",ACL:%s:%d/%d/0x%08x",
3409 n
= snprintf(buf
, bufsize
,
3410 ",ACL:%s:%d/%d/0x%08x",
3416 } else if ((StrnCaseCmp(name
, "acl", 3) == 0 &&
3417 StrCaseCmp(name
+ 3, sidstr
) == 0) ||
3418 (StrnCaseCmp(name
, "acl+", 4) == 0 &&
3419 StrCaseCmp(name
+ 4, sidstr
) == 0)) {
3420 if (determine_size
) {
3421 p
= talloc_asprintf(ctx
,
3432 n
= snprintf(buf
, bufsize
,
3434 ace
->type
, ace
->flags
, ace
->info
.mask
);
3454 /*****************************************************
3455 set the ACLs on a file given an ascii description
3456 *******************************************************/
3457 static int cacl_set(TALLOC_CTX
*ctx
, struct cli_state
*cli
,
3458 struct cli_state
*ipc_cli
, POLICY_HND
*pol
,
3459 const char *filename
, const char *the_acl
,
3460 int mode
, int flags
)
3464 SEC_DESC
*sd
= NULL
, *old
;
3465 SEC_ACL
*dacl
= NULL
;
3466 DOM_SID
*owner_sid
= NULL
;
3467 DOM_SID
*grp_sid
= NULL
;
3472 BOOL numeric
= True
;
3474 /* the_acl will be null for REMOVE_ALL operations */
3476 numeric
= ((p
= strchr(the_acl
, ':')) != NULL
&&
3480 /* if this is to set the entire ACL... */
3481 if (*the_acl
== '*') {
3482 /* ... then increment past the first colon */
3486 sd
= sec_desc_parse(ctx
, ipc_cli
, pol
,
3487 numeric
, (char *) the_acl
);
3495 /* The desired access below is the only one I could find that works
3496 with NT4, W2KP and Samba */
3498 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
3501 DEBUG(5, ("cacl_set failed to open %s: %s\n",
3502 filename
, cli_errstr(cli
)));
3507 old
= cli_query_secdesc(cli
, fnum
, ctx
);
3510 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
3515 cli_close(cli
, fnum
);
3518 case SMBC_XATTR_MODE_REMOVE_ALL
:
3519 old
->dacl
->num_aces
= 0;
3520 SAFE_FREE(old
->dacl
->ace
);
3521 SAFE_FREE(old
->dacl
);
3526 case SMBC_XATTR_MODE_REMOVE
:
3527 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
3530 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
3531 if (sec_ace_equal(&sd
->dacl
->ace
[i
],
3532 &old
->dacl
->ace
[j
])) {
3534 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
3535 old
->dacl
->ace
[k
] = old
->dacl
->ace
[k
+1];
3537 old
->dacl
->num_aces
--;
3538 if (old
->dacl
->num_aces
== 0) {
3539 SAFE_FREE(old
->dacl
->ace
);
3540 SAFE_FREE(old
->dacl
);
3557 case SMBC_XATTR_MODE_ADD
:
3558 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
3561 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
3562 if (sid_equal(&sd
->dacl
->ace
[i
].trustee
,
3563 &old
->dacl
->ace
[j
].trustee
)) {
3564 if (!(flags
& SMBC_XATTR_FLAG_CREATE
)) {
3569 old
->dacl
->ace
[j
] = sd
->dacl
->ace
[i
];
3575 if (!found
&& (flags
& SMBC_XATTR_FLAG_REPLACE
)) {
3581 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
3582 add_ace(&old
->dacl
, &sd
->dacl
->ace
[i
], ctx
);
3588 case SMBC_XATTR_MODE_SET
:
3590 owner_sid
= old
->owner_sid
;
3591 grp_sid
= old
->grp_sid
;
3595 case SMBC_XATTR_MODE_CHOWN
:
3596 owner_sid
= sd
->owner_sid
;
3599 case SMBC_XATTR_MODE_CHGRP
:
3600 grp_sid
= sd
->grp_sid
;
3604 /* Denied ACE entries must come before allowed ones */
3605 sort_acl(old
->dacl
);
3607 /* Create new security descriptor and set it */
3608 sd
= make_sec_desc(ctx
, old
->revision
, SEC_DESC_SELF_RELATIVE
,
3609 owner_sid
, grp_sid
, NULL
, dacl
, &sd_size
);
3611 fnum
= cli_nt_create(cli
, filename
,
3612 WRITE_DAC_ACCESS
| WRITE_OWNER_ACCESS
);
3615 DEBUG(5, ("cacl_set failed to open %s: %s\n",
3616 filename
, cli_errstr(cli
)));
3621 if (!cli_set_secdesc(cli
, fnum
, sd
)) {
3622 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli
)));
3629 cli_close(cli
, fnum
);
3639 int smbc_setxattr_ctx(SMBCCTX
*context
,
3649 fstring server
, share
, user
, password
, workgroup
;
3654 if (!context
|| !context
->internal
||
3655 !context
->internal
->_initialized
) {
3657 errno
= EINVAL
; /* Best I can think of ... */
3669 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
3670 fname
, name
, (int) size
, (char *) value
));
3672 if (smbc_parse_path(context
, fname
,
3673 server
, sizeof(server
),
3674 share
, sizeof(share
),
3677 password
, sizeof(password
),
3683 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3685 fstrcpy(workgroup
, context
->workgroup
);
3687 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
3689 return -1; /* errno set by smbc_server */
3692 ipc_srv
= smbc_attr_server(context
, server
, share
,
3693 workgroup
, user
, password
,
3699 ctx
= talloc_init("smbc_setxattr");
3706 * Are they asking to set an access control element or to set
3707 * the entire access control list?
3709 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
3710 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
3711 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
3712 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
3713 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
3717 talloc_asprintf(ctx
, "%s:%s", name
+19, (char *) value
);
3722 ret
= cacl_set(ctx
, &srv
->cli
,
3723 &ipc_srv
->cli
, &pol
, path
,
3726 ? SMBC_XATTR_MODE_SET
3727 : SMBC_XATTR_MODE_ADD
),
3730 talloc_destroy(ctx
);
3735 * Are they asking to set the owner?
3737 if (StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
3738 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0) {
3742 talloc_asprintf(ctx
, "%s:%s", name
+19, (char *) value
);
3747 ret
= cacl_set(ctx
, &srv
->cli
,
3748 &ipc_srv
->cli
, &pol
, path
,
3749 namevalue
, SMBC_XATTR_MODE_CHOWN
, 0);
3751 talloc_destroy(ctx
);
3756 * Are they asking to set the group?
3758 if (StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
3759 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0) {
3763 talloc_asprintf(ctx
, "%s:%s", name
+19, (char *) value
);
3768 ret
= cacl_set(ctx
, &srv
->cli
,
3769 &ipc_srv
->cli
, &pol
, path
,
3770 namevalue
, SMBC_XATTR_MODE_CHOWN
, 0);
3772 talloc_destroy(ctx
);
3776 /* Unsupported attribute name */
3777 talloc_destroy(ctx
);
3782 int smbc_getxattr_ctx(SMBCCTX
*context
,
3791 fstring server
, share
, user
, password
, workgroup
;
3796 if (!context
|| !context
->internal
||
3797 !context
->internal
->_initialized
) {
3799 errno
= EINVAL
; /* Best I can think of ... */
3811 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname
, name
));
3813 if (smbc_parse_path(context
, fname
,
3814 server
, sizeof(server
),
3815 share
, sizeof(share
),
3818 password
, sizeof(password
),
3824 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3826 fstrcpy(workgroup
, context
->workgroup
);
3828 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
3830 return -1; /* errno set by smbc_server */
3833 ipc_srv
= smbc_attr_server(context
, server
, share
,
3834 workgroup
, user
, password
,
3840 ctx
= talloc_init("smbc:getxattr");
3846 /* Are they requesting a supported attribute? */
3847 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
3848 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
3849 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
3850 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
3851 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
3852 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
3853 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
3854 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
3855 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
3858 ret
= cacl_get(ctx
, &srv
->cli
,
3859 &ipc_srv
->cli
, &pol
,
3860 (char *) path
, (char *) name
+ 19,
3861 (char *) value
, size
);
3862 if (ret
< 0 && errno
== 0) {
3863 errno
= smbc_errno(context
, &srv
->cli
);
3865 talloc_destroy(ctx
);
3869 /* Unsupported attribute name */
3870 talloc_destroy(ctx
);
3876 int smbc_removexattr_ctx(SMBCCTX
*context
,
3883 fstring server
, share
, user
, password
, workgroup
;
3888 if (!context
|| !context
->internal
||
3889 !context
->internal
->_initialized
) {
3891 errno
= EINVAL
; /* Best I can think of ... */
3903 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname
, name
));
3905 if (smbc_parse_path(context
, fname
,
3906 server
, sizeof(server
),
3907 share
, sizeof(share
),
3910 password
, sizeof(password
),
3916 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3918 fstrcpy(workgroup
, context
->workgroup
);
3920 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
3922 return -1; /* errno set by smbc_server */
3925 ipc_srv
= smbc_attr_server(context
, server
, share
,
3926 workgroup
, user
, password
,
3932 ipc_srv
= smbc_attr_server(context
, server
, share
,
3933 workgroup
, user
, password
,
3939 ctx
= talloc_init("smbc_removexattr");
3945 /* Are they asking to set the entire ACL? */
3946 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
3947 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0) {
3950 ret
= cacl_set(ctx
, &srv
->cli
,
3951 &ipc_srv
->cli
, &pol
, path
,
3952 NULL
, SMBC_XATTR_MODE_REMOVE_ALL
, 0);
3953 talloc_destroy(ctx
);
3958 * Are they asking to remove one or more spceific security descriptor
3961 if (StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
3962 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
3963 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
3964 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
3965 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
3966 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
3967 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
3970 ret
= cacl_set(ctx
, &srv
->cli
,
3971 &ipc_srv
->cli
, &pol
, path
,
3972 name
+ 19, SMBC_XATTR_MODE_REMOVE
, 0);
3973 talloc_destroy(ctx
);
3977 /* Unsupported attribute name */
3978 talloc_destroy(ctx
);
3983 int smbc_listxattr_ctx(SMBCCTX
*context
,
3989 * This isn't quite what listxattr() is supposed to do. This returns
3990 * the complete set of attributes, always, rather than only those
3991 * attribute names which actually exist for a file. Hmmm...
3993 const char supported
[] =
3994 "system.nt_sec_desc.revision\0"
3995 "system.nt_sec_desc.owner\0"
3996 "system.nt_sec_desc.owner+\0"
3997 "system.nt_sec_desc.group\0"
3998 "system.nt_sec_desc.group+\0"
3999 "system.nt_sec_desc.acl\0"
4000 "system.nt_sec_desc.acl+\0"
4001 "system.nt_sec_desc.*\0"
4002 "system.nt_sec_desc.*+\0"
4006 return sizeof(supported
);
4009 if (sizeof(supported
) > size
) {
4014 /* this can't be strcpy() because there are embedded null characters */
4015 memcpy(list
, supported
, sizeof(supported
));
4016 return sizeof(supported
);
4021 * Open a print file to be written to by other calls
4024 static SMBCFILE
*smbc_open_print_job_ctx(SMBCCTX
*context
, const char *fname
)
4026 fstring server
, share
, user
, password
;
4029 if (!context
|| !context
->internal
||
4030 !context
->internal
->_initialized
) {
4044 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
4046 if (smbc_parse_path(context
, fname
,
4047 server
, sizeof(server
),
4048 share
, sizeof(share
),
4051 password
, sizeof(password
),
4057 /* What if the path is empty, or the file exists? */
4059 return context
->open(context
, fname
, O_WRONLY
, 666);
4064 * Routine to print a file on a remote server ...
4066 * We open the file, which we assume to be on a remote server, and then
4067 * copy it to a print file on the share specified by printq.
4070 static int smbc_print_file_ctx(SMBCCTX
*c_file
, const char *fname
, SMBCCTX
*c_print
, const char *printq
)
4072 SMBCFILE
*fid1
, *fid2
;
4073 int bytes
, saverr
, tot_bytes
= 0;
4076 if (!c_file
|| !c_file
->internal
->_initialized
|| !c_print
||
4077 !c_print
->internal
->_initialized
) {
4084 if (!fname
&& !printq
) {
4091 /* Try to open the file for reading ... */
4093 if ((int)(fid1
= c_file
->open(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
4095 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
4096 return -1; /* smbc_open sets errno */
4100 /* Now, try to open the printer file for writing */
4102 if ((int)(fid2
= c_print
->open_print_job(c_print
, printq
)) < 0) {
4104 saverr
= errno
; /* Save errno */
4105 c_file
->close(c_file
, fid1
);
4111 while ((bytes
= c_file
->read(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
4115 if ((c_print
->write(c_print
, fid2
, buf
, bytes
)) < 0) {
4118 c_file
->close(c_file
, fid1
);
4119 c_print
->close(c_print
, fid2
);
4128 c_file
->close(c_file
, fid1
); /* We have to close these anyway */
4129 c_print
->close(c_print
, fid2
);
4143 * Routine to list print jobs on a printer share ...
4146 static int smbc_list_print_jobs_ctx(SMBCCTX
*context
, const char *fname
, smbc_list_print_job_fn fn
)
4149 fstring server
, share
, user
, password
, workgroup
;
4152 if (!context
|| !context
->internal
||
4153 !context
->internal
->_initialized
) {
4167 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
4169 if (smbc_parse_path(context
, fname
,
4170 server
, sizeof(server
),
4171 share
, sizeof(share
),
4174 password
, sizeof(password
),
4180 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
4182 fstrcpy(workgroup
, context
->workgroup
);
4184 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
4188 return -1; /* errno set by smbc_server */
4192 if (cli_print_queue(&srv
->cli
, (void (*)(struct print_job_info
*))fn
) < 0) {
4194 errno
= smbc_errno(context
, &srv
->cli
);
4204 * Delete a print job from a remote printer share
4207 static int smbc_unlink_print_job_ctx(SMBCCTX
*context
, const char *fname
, int id
)
4210 fstring server
, share
, user
, password
, workgroup
;
4214 if (!context
|| !context
->internal
||
4215 !context
->internal
->_initialized
) {
4229 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
4231 if (smbc_parse_path(context
, fname
,
4232 server
, sizeof(server
),
4233 share
, sizeof(share
),
4236 password
, sizeof(password
),
4242 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
4244 fstrcpy(workgroup
, context
->workgroup
);
4246 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
4250 return -1; /* errno set by smbc_server */
4254 if ((err
= cli_printjob_del(&srv
->cli
, id
)) != 0) {
4257 errno
= smbc_errno(context
, &srv
->cli
);
4258 else if (err
== ERRnosuchprintjob
)
4269 * Get a new empty handle to fill in with your own info
4271 SMBCCTX
* smbc_new_context(void)
4275 context
= malloc(sizeof(SMBCCTX
));
4281 ZERO_STRUCTP(context
);
4283 context
->internal
= malloc(sizeof(struct smbc_internal_data
));
4284 if (!context
->internal
) {
4289 ZERO_STRUCTP(context
->internal
);
4292 /* ADD REASONABLE DEFAULTS */
4294 context
->timeout
= 20000; /* 20 seconds */
4296 context
->open
= smbc_open_ctx
;
4297 context
->creat
= smbc_creat_ctx
;
4298 context
->read
= smbc_read_ctx
;
4299 context
->write
= smbc_write_ctx
;
4300 context
->close
= smbc_close_ctx
;
4301 context
->unlink
= smbc_unlink_ctx
;
4302 context
->rename
= smbc_rename_ctx
;
4303 context
->lseek
= smbc_lseek_ctx
;
4304 context
->stat
= smbc_stat_ctx
;
4305 context
->fstat
= smbc_fstat_ctx
;
4306 context
->opendir
= smbc_opendir_ctx
;
4307 context
->closedir
= smbc_closedir_ctx
;
4308 context
->readdir
= smbc_readdir_ctx
;
4309 context
->getdents
= smbc_getdents_ctx
;
4310 context
->mkdir
= smbc_mkdir_ctx
;
4311 context
->rmdir
= smbc_rmdir_ctx
;
4312 context
->telldir
= smbc_telldir_ctx
;
4313 context
->lseekdir
= smbc_lseekdir_ctx
;
4314 context
->fstatdir
= smbc_fstatdir_ctx
;
4315 context
->chmod
= smbc_chmod_ctx
;
4316 context
->utimes
= smbc_utimes_ctx
;
4317 context
->setxattr
= smbc_setxattr_ctx
;
4318 context
->getxattr
= smbc_getxattr_ctx
;
4319 context
->removexattr
= smbc_removexattr_ctx
;
4320 context
->listxattr
= smbc_listxattr_ctx
;
4321 context
->open_print_job
= smbc_open_print_job_ctx
;
4322 context
->print_file
= smbc_print_file_ctx
;
4323 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
4324 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
4326 context
->callbacks
.check_server_fn
= smbc_check_server
;
4327 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
4329 smbc_default_cache_functions(context
);
4337 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
4338 * and thus you'll be leaking memory if not handled properly.
4341 int smbc_free_context(SMBCCTX
* context
, int shutdown_ctx
)
4350 DEBUG(1,("Performing aggressive shutdown.\n"));
4352 f
= context
->internal
->_files
;
4354 context
->close(context
, f
);
4357 context
->internal
->_files
= NULL
;
4359 /* First try to remove the servers the nice way. */
4360 if (context
->callbacks
.purge_cached_fn(context
)) {
4363 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
4364 s
= context
->internal
->_servers
;
4366 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n", s
, s
->cli
.fd
));
4367 cli_shutdown(&s
->cli
);
4368 context
->callbacks
.remove_cached_srv_fn(context
, s
);
4370 DLIST_REMOVE(context
->internal
->_servers
, s
);
4374 context
->internal
->_servers
= NULL
;
4378 /* This is the polite way */
4379 if (context
->callbacks
.purge_cached_fn(context
)) {
4380 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
4384 if (context
->internal
->_servers
) {
4385 DEBUG(1, ("Active servers in context, free_context failed.\n"));
4389 if (context
->internal
->_files
) {
4390 DEBUG(1, ("Active files in context, free_context failed.\n"));
4396 /* Things we have to clean up */
4397 SAFE_FREE(context
->workgroup
);
4398 SAFE_FREE(context
->netbios_name
);
4399 SAFE_FREE(context
->user
);
4401 DEBUG(3, ("Context %p succesfully freed\n", context
));
4402 SAFE_FREE(context
->internal
);
4409 * Initialise the library etc
4411 * We accept a struct containing handle information.
4412 * valid values for info->debug from 0 to 100,
4413 * and insist that info->fn must be non-null.
4415 SMBCCTX
* smbc_init_context(SMBCCTX
* context
)
4419 char *user
= NULL
, *home
= NULL
;
4421 if (!context
|| !context
->internal
) {
4426 /* Do not initialise the same client twice */
4427 if (context
->internal
->_initialized
) {
4431 if (!context
->callbacks
.auth_fn
|| context
->debug
< 0 || context
->debug
> 100) {
4438 if (!smbc_initialized
) {
4439 /* Do some library wide intialisations the first time we get called */
4441 /* Set this to what the user wants */
4442 DEBUGLEVEL
= context
->debug
;
4444 setup_logging( "libsmbclient", True
);
4446 /* Here we would open the smb.conf file if needed ... */
4448 home
= getenv("HOME");
4450 slprintf(conf
, sizeof(conf
), "%s/.smb/smb.conf", home
);
4452 load_interfaces(); /* Load the list of interfaces ... */
4454 in_client
= True
; /* FIXME, make a param */
4456 if (!lp_load(conf
, True
, False
, False
)) {
4459 * Well, if that failed, try the dyn_CONFIGFILE
4460 * Which points to the standard locn, and if that
4461 * fails, silently ignore it and use the internal
4465 if (!lp_load(dyn_CONFIGFILE
, True
, False
, False
)) {
4466 DEBUG(5, ("Could not load either config file: %s or %s\n",
4467 conf
, dyn_CONFIGFILE
));
4471 reopen_logs(); /* Get logging working ... */
4474 * Block SIGPIPE (from lib/util_sock.c: write())
4475 * It is not needed and should not stop execution
4477 BlockSignals(True
, SIGPIPE
);
4479 /* Done with one-time initialisation */
4480 smbc_initialized
= 1;
4484 if (!context
->user
) {
4486 * FIXME: Is this the best way to get the user info?
4488 user
= getenv("USER");
4489 /* walk around as "guest" if no username can be found */
4490 if (!user
) context
->user
= strdup("guest");
4491 else context
->user
= strdup(user
);
4494 if (!context
->netbios_name
) {
4496 * We try to get our netbios name from the config. If that fails we fall
4497 * back on constructing our netbios name from our hostname etc
4499 if (global_myname()) {
4500 context
->netbios_name
= strdup(global_myname());
4504 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
4507 context
->netbios_name
= malloc(17);
4508 if (!context
->netbios_name
) {
4512 slprintf(context
->netbios_name
, 16, "smbc%s%d", context
->user
, pid
);
4516 DEBUG(1, ("Using netbios name %s.\n", context
->netbios_name
));
4518 if (!context
->workgroup
) {
4519 if (lp_workgroup()) {
4520 context
->workgroup
= strdup(lp_workgroup());
4523 /* TODO: Think about a decent default workgroup */
4524 context
->workgroup
= strdup("samba");
4528 DEBUG(1, ("Using workgroup %s.\n", context
->workgroup
));
4530 /* shortest timeout is 1 second */
4531 if (context
->timeout
> 0 && context
->timeout
< 1000)
4532 context
->timeout
= 1000;
4535 * FIXME: Should we check the function pointers here?
4538 context
->internal
->_initialized
= 1;