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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "../include/libsmb_internal.h"
29 * Functions exported by libsmb_cache.c that we need here
31 int smbc_default_cache_functions(SMBCCTX
*context
);
34 * check if an element is part of the list.
35 * FIXME: Does not belong here !
36 * Can anyone put this in a macro in dlinklist.h ?
39 static int DLIST_CONTAINS(SMBCFILE
* list
, SMBCFILE
*p
) {
40 if (!p
|| !list
) return False
;
42 if (p
== list
) return True
;
48 extern BOOL in_client
;
51 * Is the logging working / configfile read ?
53 static int smbc_initialized
= 0;
56 hex2int( unsigned int _char
)
58 if ( _char
>= 'A' && _char
<='F')
59 return _char
- 'A' + 10;
60 if ( _char
>= 'a' && _char
<='f')
61 return _char
- 'a' + 10;
62 if ( _char
>= '0' && _char
<='9')
68 decode_urlpart(char *segment
)
70 int old_length
= strlen(segment
);
75 char *new_usegment
= 0;
81 /* make a copy of the old one */
82 new_usegment
= (char*)malloc( old_length
* 3 + 1 );
84 while( i
< old_length
) {
85 int bReencode
= False
;
86 unsigned char character
= segment
[ i
++ ];
87 if ((character
<= ' ') || (character
> 127))
90 new_usegment
[ new_length2
++ ] = character
;
91 if (character
== '%' ) {
92 int a
= i
+1 < old_length
? hex2int( segment
[i
] ) : -1;
93 int b
= i
+1 < old_length
? hex2int( segment
[i
+1] ) : -1;
94 if ((a
== -1) || (b
== -1)) { /* Only replace if sequence is valid */
95 /* Contains stray %, make sure to re-encode! */
98 /* Valid %xx sequence */
99 character
= a
* 16 + b
; /* Replace with value of %dd */
101 break; /* Stop at %00 */
103 new_usegment
[ new_length2
++ ] = (unsigned char) segment
[i
++];
104 new_usegment
[ new_length2
++ ] = (unsigned char) segment
[i
++];
108 unsigned int c
= character
/ 16;
110 new_usegment
[ new_length2
++ ] = '%';
112 c
+= (c
> 9) ? ('A' - 10) : '0';
113 new_usegment
[ new_length2
++ ] = c
;
116 c
+= (c
> 9) ? ('A' - 10) : '0';
117 new_usegment
[ new_length2
++ ] = c
;
120 new_segment
[ new_length
++ ] = character
;
122 new_segment
[ new_length
] = 0;
124 /* this assumes (very safely) that removing %aa sequences
125 only shortens the string */
126 strncpy(segment
, new_segment
, old_length
);
132 * Function to parse a path and turn it into components
134 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
136 * smb:// means show all the workgroups
137 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
138 * else, if name<20> exists, list all shares for server ...
141 static const char *smbc_prefix
= "smb:";
144 smbc_parse_path(SMBCCTX
*context
, const char *fname
, char *server
, char *share
, char *path
,
145 char *user
, char *password
) /* FIXME, lengths of strings */
153 server
[0] = share
[0] = path
[0] = user
[0] = password
[0] = (char)0;
156 /* clean_fname(s); causing problems ... */
158 /* see if it has the right prefix */
159 len
= strlen(smbc_prefix
);
160 if (strncmp(s
,smbc_prefix
,len
) ||
161 (s
[len
] != '/' && s
[len
] != 0)) return -1; /* What about no smb: ? */
165 /* Watch the test below, we are testing to see if we should exit */
167 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
173 p
+= 2; /* Skip the // or \\ */
180 strncpy(server
, context
->workgroup
,
181 (strlen(context
->workgroup
) < 16)?strlen(context
->workgroup
):16);
187 * ok, its for us. Now parse out the server, share etc.
189 * However, we want to parse out [[domain;]user[:password]@] if it
193 /* check that '@' occurs before '/', if '/' exists at all */
194 q
= strchr_m(p
, '@');
195 r
= strchr_m(p
, '/');
196 if (q
&& (!r
|| q
< r
)) {
197 pstring username
, passwd
, domain
;
198 const char *u
= userinfo
;
200 next_token(&p
, userinfo
, "@", sizeof(fstring
));
202 username
[0] = passwd
[0] = domain
[0] = 0;
204 if (strchr_m(u
, ';')) {
206 next_token(&u
, domain
, ";", sizeof(fstring
));
210 if (strchr_m(u
, ':')) {
212 next_token(&u
, username
, ":", sizeof(fstring
));
219 pstrcpy(username
, u
);
224 strncpy(user
, username
, sizeof(fstring
)); /* FIXME, size and domain */
227 strncpy(password
, passwd
, sizeof(fstring
)); /* FIXME, size */
231 if (!next_token(&p
, server
, "/", sizeof(fstring
))) {
237 if (*p
== (char)0) goto decoding
; /* That's it ... */
239 if (!next_token(&p
, share
, "/", sizeof(fstring
))) {
247 all_string_sub(path
, "/", "\\", 0);
250 decode_urlpart(path
);
251 decode_urlpart(server
);
252 decode_urlpart(share
);
253 decode_urlpart(user
);
254 decode_urlpart(password
);
260 * Convert an SMB error into a UNIX error ...
263 static int smbc_errno(SMBCCTX
*context
, struct cli_state
*c
)
265 int ret
= cli_errno(c
);
267 if (cli_is_dos_error(c
)) {
271 cli_dos_error(c
, &eclass
, &ecode
);
273 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
274 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
278 status
= cli_nt_error(c
);
280 DEBUG(3,("smbc errno %s -> %d\n",
281 nt_errstr(status
), ret
));
289 * returns 0 if the server is in shape. Returns 1 on error
291 * Also useable outside libsmbclient to enable external cache
292 * to do some checks too.
294 int smbc_check_server(SMBCCTX
* context
, SMBCSRV
* server
)
296 if ( send_keepalive(server
->cli
.fd
) == False
)
299 /* connection is ok */
304 * Remove a server from the cached server list it's unused.
305 * On success, 0 is returned. 1 is returned if the server could not be removed.
307 * Also useable outside libsmbclient
309 int smbc_remove_unused_server(SMBCCTX
* context
, SMBCSRV
* srv
)
313 /* are we being fooled ? */
314 if (!context
|| !context
->internal
||
315 !context
->internal
->_initialized
|| !srv
) return 1;
318 /* Check all open files/directories for a relation with this server */
319 for (file
= context
->internal
->_files
; file
; file
=file
->next
) {
320 if (file
->srv
== srv
) {
322 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
328 DLIST_REMOVE(context
->internal
->_servers
, srv
);
330 cli_shutdown(&srv
->cli
);
332 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
334 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
342 * Connect to a server, possibly on an existing connection
344 * Here, what we want to do is: If the server and username
345 * match an existing connection, reuse that, otherwise, establish a
348 * If we have to create a new connection, call the auth_fn to get the
349 * info we need, unless the username and password were passed in.
352 SMBCSRV
*smbc_server(SMBCCTX
*context
,
353 char *server
, char *share
,
354 char *workgroup
, char *username
,
360 struct nmb_name called
, calling
;
361 char *p
, *server_n
= server
;
365 int tried_reverse
= 0;
370 if (server
[0] == 0) {
377 srv
= context
->callbacks
.get_cached_srv_fn(context
, server
, share
,
378 workgroup
, username
);
380 if (!auth_called
&& !srv
&& (!username
[0] || !password
[0])) {
381 context
->callbacks
.auth_fn(server
, share
, workgroup
, sizeof(fstring
),
382 username
, sizeof(fstring
), password
, sizeof(fstring
));
384 * However, smbc_auth_fn may have picked up info relating to an
385 * existing connection, so try for an existing connection again ...
388 goto check_server_cache
;
393 if (context
->callbacks
.check_server_fn(context
, srv
)) {
395 * This server is no good anymore
396 * Try to remove it and check for more possible servers in the cache
398 if (context
->callbacks
.remove_unused_server_fn(context
, srv
)) {
400 * We could not remove the server completely, remove it from the cache
401 * so we will not get it again. It will be removed when the last file/dir
404 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
408 * Maybe there are more cached connections to this server
410 goto check_server_cache
;
415 make_nmb_name(&calling
, context
->netbios_name
, 0x0);
416 make_nmb_name(&called
, server
, 0x20);
418 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n
, server
));
420 if ((p
=strchr_m(server_n
,'#')) &&
421 (strcmp(p
+1,"1D")==0 || strcmp(p
+1,"01")==0)) {
423 fstrcpy(group
, server_n
);
424 p
= strchr_m(group
,'#');
429 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
432 slprintf(ipenv
,sizeof(ipenv
)-1,"HOST_%s", server_n
);
436 /* have to open a new connection */
437 if (!cli_initialise(&c
)) {
442 c
.timeout
= context
->timeout
;
444 if (!cli_connect(&c
, server_n
, &ip
)) {
450 if (!cli_session_request(&c
, &calling
, &called
)) {
452 if (strcmp(called
.name
, "*SMBSERVER")) {
453 make_nmb_name(&called
, "*SMBSERVER", 0x20);
456 else { /* Try one more time, but ensure we don't loop */
458 /* Only try this if server is an IP address ... */
460 if (is_ipaddress(server
) && !tried_reverse
) {
462 struct in_addr rem_ip
;
464 if ((rem_ip
.s_addr
=inet_addr(server
)) == INADDR_NONE
) {
465 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server
));
470 tried_reverse
++; /* Yuck */
472 if (name_status_find("*", 0, 0, rem_ip
, remote_name
)) {
473 make_nmb_name(&called
, remote_name
, 0x20);
484 DEBUG(4,(" session request ok\n"));
486 if (!cli_negprot(&c
)) {
492 if (!cli_session_setup(&c
, username
,
493 password
, strlen(password
),
494 password
, strlen(password
),
496 /* try an anonymous login if it failed */
497 !cli_session_setup(&c
, "", "", 1,"", 0, workgroup
)) {
503 DEBUG(4,(" session setup ok\n"));
505 if (!cli_send_tconX(&c
, share
, "?????",
506 password
, strlen(password
)+1)) {
507 errno
= smbc_errno(context
, &c
);
512 DEBUG(4,(" tconx ok\n"));
515 * Ok, we have got a nice connection
516 * Let's find a free server_fd
519 srv
= (SMBCSRV
*)malloc(sizeof(*srv
));
527 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
529 /* now add it to the cache (internal or external) */
530 if (context
->callbacks
.add_cached_srv_fn(context
, srv
, server
, share
, workgroup
, username
)) {
531 DEBUG(3, (" Failed to add server to cache\n"));
536 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
537 server
, share
, srv
));
543 if (!srv
) return NULL
;
550 * Routine to open() a file ...
553 static SMBCFILE
*smbc_open_ctx(SMBCCTX
*context
, const char *fname
, int flags
, mode_t mode
)
555 fstring server
, share
, user
, password
, workgroup
;
558 SMBCFILE
*file
= NULL
;
561 if (!context
|| !context
->internal
||
562 !context
->internal
->_initialized
) {
564 errno
= EINVAL
; /* Best I can think of ... */
576 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
578 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
580 fstrcpy(workgroup
, context
->workgroup
);
582 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
586 if (errno
== EPERM
) errno
= EACCES
;
587 return NULL
; /* smbc_server sets errno */
591 /* Hmmm, the test for a directory is suspect here ... FIXME */
593 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
600 file
= malloc(sizeof(SMBCFILE
));
611 if ((fd
= cli_open(&srv
->cli
, path
, flags
, DENY_NONE
)) < 0) {
613 /* Handle the error ... */
616 errno
= smbc_errno(context
, &srv
->cli
);
621 /* Fill in file struct */
624 file
->fname
= strdup(fname
);
629 DLIST_ADD(context
->internal
->_files
, file
);
634 /* Check if opendir needed ... */
639 eno
= smbc_errno(context
, &srv
->cli
);
640 file
= context
->opendir(context
, fname
);
641 if (!file
) errno
= eno
;
646 errno
= EINVAL
; /* FIXME, correct errno ? */
652 * Routine to create a file
655 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
657 static SMBCFILE
*smbc_creat_ctx(SMBCCTX
*context
, const char *path
, mode_t mode
)
660 if (!context
|| !context
->internal
||
661 !context
->internal
->_initialized
) {
668 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
672 * Routine to read() a file ...
675 static ssize_t
smbc_read_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
679 if (!context
|| !context
->internal
||
680 !context
->internal
->_initialized
) {
687 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
689 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
696 /* Check that the buffer exists ... */
705 ret
= cli_read(&file
->srv
->cli
, file
->cli_fd
, buf
, file
->offset
, count
);
709 errno
= smbc_errno(context
, &file
->srv
->cli
);
716 DEBUG(4, (" --> %d\n", ret
));
718 return ret
; /* Success, ret bytes of data ... */
723 * Routine to write() a file ...
726 static ssize_t
smbc_write_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
730 if (!context
|| !context
->internal
||
731 !context
->internal
->_initialized
) {
738 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
745 /* Check that the buffer exists ... */
754 ret
= cli_write(&file
->srv
->cli
, file
->cli_fd
, 0, buf
, file
->offset
, count
);
758 errno
= smbc_errno(context
, &file
->srv
->cli
);
765 return ret
; /* Success, 0 bytes of data ... */
769 * Routine to close() a file ...
772 static int smbc_close_ctx(SMBCCTX
*context
, SMBCFILE
*file
)
776 if (!context
|| !context
->internal
||
777 !context
->internal
->_initialized
) {
784 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
794 return context
->closedir(context
, file
);
798 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
800 DEBUG(3, ("cli_close failed on %s. purging server.\n",
802 /* Deallocate slot and remove the server
803 * from the server cache if unused */
804 errno
= smbc_errno(context
, &file
->srv
->cli
);
806 DLIST_REMOVE(context
->internal
->_files
, file
);
807 SAFE_FREE(file
->fname
);
809 context
->callbacks
.remove_unused_server_fn(context
, srv
);
817 return context
->closedir(context
, file
);
821 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
822 DEBUG(3, ("cli_close failed on %s. purging server.\n",
824 /* Deallocate slot and remove the server
825 * from the server cache if unused */
826 errno
= smbc_errno(context
, &file
->srv
->cli
);
828 DLIST_REMOVE(context
->internal
->_files
, file
);
829 SAFE_FREE(file
->fname
);
831 context
->callbacks
.remove_unused_server_fn(context
, srv
);
836 DLIST_REMOVE(context
->internal
->_files
, file
);
837 SAFE_FREE(file
->fname
);
844 * Get info from an SMB server on a file. Use a qpathinfo call first
845 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
847 static BOOL
smbc_getatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
848 uint16
*mode
, size_t *size
,
849 time_t *c_time
, time_t *a_time
, time_t *m_time
,
853 if (!context
|| !context
->internal
||
854 !context
->internal
->_initialized
) {
861 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
863 if (!srv
->no_pathinfo2
&&
864 cli_qpathinfo2(&srv
->cli
, path
, c_time
, a_time
, m_time
, NULL
,
865 size
, mode
, ino
)) return True
;
867 /* if this is NT then don't bother with the getatr */
868 if (srv
->cli
.capabilities
& CAP_NT_SMBS
) return False
;
870 if (cli_getatr(&srv
->cli
, path
, mode
, size
, m_time
)) {
871 a_time
= c_time
= m_time
;
872 srv
->no_pathinfo2
= True
;
881 * Routine to unlink() a file
884 static int smbc_unlink_ctx(SMBCCTX
*context
, const char *fname
)
886 fstring server
, share
, user
, password
, workgroup
;
890 if (!context
|| !context
->internal
||
891 !context
->internal
->_initialized
) {
893 errno
= EINVAL
; /* Best I can think of ... */
905 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
907 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
909 fstrcpy(workgroup
, context
->workgroup
);
911 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
915 return -1; /* smbc_server sets errno */
919 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
921 int job = smbc_stat_printjob(srv, path, NULL, NULL);
927 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
935 if (!cli_unlink(&srv
->cli
, path
)) {
937 errno
= smbc_errno(context
, &srv
->cli
);
939 if (errno
== EACCES
) { /* Check if the file is a directory */
944 time_t m_time
= 0, a_time
= 0, c_time
= 0;
947 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
948 &c_time
, &a_time
, &m_time
, &ino
)) {
950 /* Hmmm, bad error ... What? */
952 errno
= smbc_errno(context
, &srv
->cli
);
958 if (IS_DOS_DIR(mode
))
961 errno
= saverr
; /* Restore this */
970 return 0; /* Success ... */
975 * Routine to rename() a file
978 static int smbc_rename_ctx(SMBCCTX
*ocontext
, const char *oname
,
979 SMBCCTX
*ncontext
, const char *nname
)
981 fstring server1
, share1
, server2
, share2
, user1
, user2
, password1
, password2
, workgroup
;
982 pstring path1
, path2
;
985 if (!ocontext
|| !ncontext
||
986 !ocontext
->internal
|| !ncontext
->internal
||
987 !ocontext
->internal
->_initialized
||
988 !ncontext
->internal
->_initialized
) {
990 errno
= EINVAL
; /* Best I can think of ... */
995 if (!oname
|| !nname
) {
1002 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1004 smbc_parse_path(ocontext
, oname
, server1
, share1
, path1
, user1
, password1
);
1006 if (user1
[0] == (char)0) fstrcpy(user1
, ocontext
->user
);
1008 smbc_parse_path(ncontext
, nname
, server2
, share2
, path2
, user2
, password2
);
1010 if (user2
[0] == (char)0) fstrcpy(user2
, ncontext
->user
);
1012 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1013 strcmp(user1
, user2
)) {
1015 /* Can't rename across file systems, or users?? */
1022 fstrcpy(workgroup
, ocontext
->workgroup
);
1023 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1024 srv
= smbc_server(ocontext
, server1
, share1
, workgroup
, user1
, password1
);
1031 if (!cli_rename(&srv
->cli
, path1
, path2
)) {
1032 int eno
= smbc_errno(ocontext
, &srv
->cli
);
1034 if (eno
!= EEXIST
||
1035 !cli_unlink(&srv
->cli
, path2
) ||
1036 !cli_rename(&srv
->cli
, path1
, path2
)) {
1044 return 0; /* Success */
1049 * A routine to lseek() a file
1052 static off_t
smbc_lseek_ctx(SMBCCTX
*context
, SMBCFILE
*file
, off_t offset
, int whence
)
1056 if (!context
|| !context
->internal
||
1057 !context
->internal
->_initialized
) {
1064 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1074 return -1; /* Can't lseek a dir ... */
1080 file
->offset
= offset
;
1084 file
->offset
+= offset
;
1088 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1089 NULL
, NULL
, NULL
) &&
1090 !cli_getattrE(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1096 file
->offset
= size
+ offset
;
1105 return file
->offset
;
1110 * Generate an inode number from file name for those things that need it
1114 ino_t
smbc_inode(SMBCCTX
*context
, const char *name
)
1117 if (!context
|| !context
->internal
||
1118 !context
->internal
->_initialized
) {
1125 if (!*name
) return 2; /* FIXME, why 2 ??? */
1126 return (ino_t
)str_checksum(name
);
1131 * Routine to put basic stat info into a stat structure ... Used by stat and
1136 int smbc_setup_stat(SMBCCTX
*context
, struct stat
*st
, char *fname
, size_t size
, int mode
)
1141 if (IS_DOS_DIR(mode
)) {
1142 st
->st_mode
= SMBC_DIR_MODE
;
1144 st
->st_mode
= SMBC_FILE_MODE
;
1147 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
1148 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
1149 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
1150 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
1153 st
->st_blksize
= 512;
1154 st
->st_blocks
= (size
+511)/512;
1155 st
->st_uid
= getuid();
1156 st
->st_gid
= getgid();
1158 if (IS_DOS_DIR(mode
)) {
1164 if (st
->st_ino
== 0) {
1165 st
->st_ino
= smbc_inode(context
, fname
);
1168 return True
; /* FIXME: Is this needed ? */
1173 * Routine to stat a file given a name
1176 static int smbc_stat_ctx(SMBCCTX
*context
, const char *fname
, struct stat
*st
)
1179 fstring server
, share
, user
, password
, workgroup
;
1181 time_t m_time
= 0, a_time
= 0, c_time
= 0;
1186 if (!context
|| !context
->internal
||
1187 !context
->internal
->_initialized
) {
1189 errno
= EINVAL
; /* Best I can think of ... */
1201 DEBUG(4, ("smbc_stat(%s)\n", fname
));
1203 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1205 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1207 fstrcpy(workgroup
, context
->workgroup
);
1209 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1213 return -1; /* errno set by smbc_server */
1217 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1219 mode = aDIR | aRONLY;
1222 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1224 if (strcmp(path, "\\") == 0) {
1226 mode = aDIR | aRONLY;
1232 smbc_stat_printjob(srv, path, &size, &m_time);
1233 c_time = a_time = m_time;
1238 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1239 &c_time
, &a_time
, &m_time
, &ino
)) {
1241 errno
= smbc_errno(context
, &srv
->cli
);
1248 smbc_setup_stat(context
, st
, path
, size
, mode
);
1250 st
->st_atime
= a_time
;
1251 st
->st_ctime
= c_time
;
1252 st
->st_mtime
= m_time
;
1253 st
->st_dev
= srv
->dev
;
1260 * Routine to stat a file given an fd
1263 static int smbc_fstat_ctx(SMBCCTX
*context
, SMBCFILE
*file
, struct stat
*st
)
1265 time_t c_time
, a_time
, m_time
;
1270 if (!context
|| !context
->internal
||
1271 !context
->internal
->_initialized
) {
1278 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1287 return context
->fstatdir(context
, file
, st
);
1291 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
,
1292 &mode
, &size
, &c_time
, &a_time
, &m_time
, NULL
, &ino
) &&
1293 !cli_getattrE(&file
->srv
->cli
, file
->cli_fd
,
1294 &mode
, &size
, &c_time
, &a_time
, &m_time
)) {
1303 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
1305 st
->st_atime
= a_time
;
1306 st
->st_ctime
= c_time
;
1307 st
->st_mtime
= m_time
;
1308 st
->st_dev
= file
->srv
->dev
;
1315 * Routine to open a directory
1319 * smb: which should list all the workgroups available
1321 * smb:workgroup//server
1323 * smb://server/share
1324 * smb://<IP-addr> which should list shares on server
1325 * smb://<IP-addr>/share which should list files on share
1328 static void smbc_remove_dir(SMBCFILE
*dir
)
1330 struct smbc_dir_list
*d
,*f
;
1337 SAFE_FREE(f
->dirent
);
1342 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
1346 static int add_dirent(SMBCFILE
*dir
, const char *name
, const char *comment
, uint32 type
)
1348 struct smbc_dirent
*dirent
;
1352 * Allocate space for the dirent, which must be increased by the
1353 * size of the name and the comment and 1 for the null on the comment.
1354 * The null on the name is already accounted for.
1357 size
= sizeof(struct smbc_dirent
) + (name
?strlen(name
):0) +
1358 (comment
?strlen(comment
):0) + 1;
1360 dirent
= malloc(size
);
1364 dir
->dir_error
= ENOMEM
;
1369 ZERO_STRUCTP(dirent
);
1371 if (dir
->dir_list
== NULL
) {
1373 dir
->dir_list
= malloc(sizeof(struct smbc_dir_list
));
1374 if (!dir
->dir_list
) {
1377 dir
->dir_error
= ENOMEM
;
1381 ZERO_STRUCTP(dir
->dir_list
);
1383 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
1388 dir
->dir_end
->next
= malloc(sizeof(struct smbc_dir_list
));
1390 if (!dir
->dir_end
->next
) {
1393 dir
->dir_error
= ENOMEM
;
1397 ZERO_STRUCTP(dir
->dir_end
->next
);
1399 dir
->dir_end
= dir
->dir_end
->next
;
1403 dir
->dir_end
->next
= NULL
;
1404 dir
->dir_end
->dirent
= dirent
;
1406 dirent
->smbc_type
= type
;
1407 dirent
->namelen
= (name
?strlen(name
):0);
1408 dirent
->commentlen
= (comment
?strlen(comment
):0);
1409 dirent
->dirlen
= size
;
1411 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
1413 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
1414 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
1421 list_fn(const char *name
, uint32 type
, const char *comment
, void *state
)
1423 SMBCFILE
*dir
= (SMBCFILE
*)state
;
1426 /* We need to process the type a little ... */
1428 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
1431 case 0: /* Directory tree */
1432 dirent_type
= SMBC_FILE_SHARE
;
1436 dirent_type
= SMBC_PRINTER_SHARE
;
1440 dirent_type
= SMBC_COMMS_SHARE
;
1444 dirent_type
= SMBC_IPC_SHARE
;
1448 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
1452 else dirent_type
= dir
->dir_type
;
1454 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
1456 /* An error occurred, what do we do? */
1457 /* FIXME: Add some code here */
1464 dir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
1467 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
1468 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
1470 /* Handle an error ... */
1472 /* FIXME: Add some code ... */
1478 static SMBCFILE
*smbc_opendir_ctx(SMBCCTX
*context
, const char *fname
)
1480 fstring server
, share
, user
, password
, workgroup
;
1482 SMBCSRV
*srv
= NULL
;
1483 SMBCFILE
*dir
= NULL
;
1484 struct in_addr rem_ip
;
1486 if (!context
|| !context
->internal
||
1487 !context
->internal
->_initialized
) {
1501 if (smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
)) {
1508 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1510 fstrcpy(workgroup
, context
->workgroup
);
1512 dir
= malloc(sizeof(*dir
));
1524 dir
->fname
= strdup(fname
);
1528 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
1530 if (server
[0] == (char)0) {
1532 if (share
[0] != (char)0 || path
[0] != (char)0) {
1536 SAFE_FREE(dir
->fname
);
1543 /* We have server and share and path empty ... so list the workgroups */
1544 /* first try to get the LMB for our workgroup, and if that fails, */
1547 if (!(resolve_name(context
->workgroup
, &rem_ip
, 0x1d) ||
1548 resolve_name(context
->workgroup
, &rem_ip
, 0x1b))) {
1550 errno
= EINVAL
; /* Something wrong with smb.conf? */
1555 dir
->dir_type
= SMBC_WORKGROUP
;
1557 /* find the name of the server ... */
1559 if (!name_status_find("*", 0, 0, rem_ip
, server
)) {
1561 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server
));
1568 * Get a connection to IPC$ on the server if we do not already have one
1571 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1576 SAFE_FREE(dir
->fname
);
1586 /* Now, list the stuff ... */
1588 if (!cli_NetServerEnum(&srv
->cli
, workgroup
, 0x80000000, list_fn
,
1592 SAFE_FREE(dir
->fname
);
1595 errno
= cli_errno(&srv
->cli
);
1601 else { /* Server not an empty string ... Check the rest and see what gives */
1603 if (share
[0] == (char)0) {
1605 if (path
[0] != (char)0) { /* Should not have empty share with path */
1609 SAFE_FREE(dir
->fname
);
1616 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1617 /* However, we check to see if <server> is an IP address first */
1619 if (!is_ipaddress(server
) && /* Not an IP addr so check next */
1620 (resolve_name(server
, &rem_ip
, 0x1d) || /* Found LMB */
1621 resolve_name(server
, &rem_ip
, 0x1b) )) { /* Found DMB */
1624 dir
->dir_type
= SMBC_SERVER
;
1627 * Get the backup list ...
1631 if (!name_status_find("*", 0, 0, rem_ip
, buserver
)) {
1633 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server
));
1634 errno
= EPERM
; /* FIXME, is this correct */
1640 * Get a connection to IPC$ on the server if we do not already have one
1643 srv
= smbc_server(context
, buserver
, "IPC$", workgroup
, user
, password
);
1648 SAFE_FREE(dir
->fname
);
1657 /* Now, list the servers ... */
1659 if (!cli_NetServerEnum(&srv
->cli
, server
, 0x0000FFFE, list_fn
,
1663 SAFE_FREE(dir
->fname
);
1666 errno
= cli_errno(&srv
->cli
);
1674 if (resolve_name(server
, &rem_ip
, 0x20)) {
1676 /* Now, list the shares ... */
1678 dir
->dir_type
= SMBC_FILE_SHARE
;
1680 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1685 SAFE_FREE(dir
->fname
);
1694 /* Now, list the servers ... */
1696 if (cli_RNetShareEnum(&srv
->cli
, list_fn
,
1699 errno
= cli_errno(&srv
->cli
);
1701 SAFE_FREE(dir
->fname
);
1711 errno
= ENODEV
; /* Neither the workgroup nor server exists */
1713 SAFE_FREE(dir
->fname
);
1723 else { /* The server and share are specified ... work from there ... */
1725 /* Well, we connect to the server and list the directory */
1727 dir
->dir_type
= SMBC_FILE_SHARE
;
1729 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1734 SAFE_FREE(dir
->fname
);
1743 /* Now, list the files ... */
1745 pstrcat(path
, "\\*");
1747 if (cli_list(&srv
->cli
, path
, aDIR
| aSYSTEM
| aHIDDEN
, dir_list_fn
,
1751 SAFE_FREE(dir
->fname
);
1754 errno
= smbc_errno(context
, &srv
->cli
);
1762 DLIST_ADD(context
->internal
->_files
, dir
);
1768 * Routine to close a directory
1771 static int smbc_closedir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
1774 if (!context
|| !context
->internal
||
1775 !context
->internal
->_initialized
) {
1782 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
1789 smbc_remove_dir(dir
); /* Clean it up */
1791 DLIST_REMOVE(context
->internal
->_files
, dir
);
1795 SAFE_FREE(dir
->fname
);
1796 SAFE_FREE(dir
); /* Free the space too */
1805 * Routine to get a directory entry
1808 struct smbc_dirent
*smbc_readdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
1810 struct smbc_dirent
*dirp
, *dirent
;
1812 /* Check that all is ok first ... */
1814 if (!context
|| !context
->internal
||
1815 !context
->internal
->_initialized
) {
1822 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
1829 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1840 dirent
= dir
->dir_next
->dirent
;
1849 /* Hmmm, do I even need to copy it? */
1851 memcpy(context
->internal
->_dirent
, dirent
, dirent
->dirlen
); /* Copy the dirent */
1852 dirp
= (struct smbc_dirent
*)context
->internal
->_dirent
;
1853 dirp
->comment
= (char *)(&dirp
->name
+ dirent
->namelen
+ 1);
1854 dir
->dir_next
= dir
->dir_next
->next
;
1856 return (struct smbc_dirent
*)context
->internal
->_dirent
;
1862 * Routine to get directory entries
1865 static int smbc_getdents_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct smbc_dirent
*dirp
, int count
)
1867 struct smbc_dir_list
*dirlist
;
1868 int rem
= count
, reqd
;
1869 char *ndir
= (char *)dirp
;
1871 /* Check that all is ok first ... */
1873 if (!context
|| !context
->internal
||
1874 !context
->internal
->_initialized
) {
1881 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
1888 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1896 * Now, retrieve the number of entries that will fit in what was passed
1897 * We have to figure out if the info is in the list, or we need to
1898 * send a request to the server to get the info.
1901 while ((dirlist
= dir
->dir_next
)) {
1902 struct smbc_dirent
*dirent
;
1904 if (!dirlist
->dirent
) {
1906 errno
= ENOENT
; /* Bad error */
1911 if (rem
< (reqd
= (sizeof(struct smbc_dirent
) + dirlist
->dirent
->namelen
+
1912 dirlist
->dirent
->commentlen
+ 1))) {
1914 if (rem
< count
) { /* We managed to copy something */
1920 else { /* Nothing copied ... */
1922 errno
= EINVAL
; /* Not enough space ... */
1929 dirent
= dirlist
->dirent
;
1931 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
1933 ((struct smbc_dirent
*)ndir
)->comment
=
1934 (char *)(&((struct smbc_dirent
*)ndir
)->name
+ dirent
->namelen
+ 1);
1940 dir
->dir_next
= dirlist
= dirlist
-> next
;
1951 * Routine to create a directory ...
1954 static int smbc_mkdir_ctx(SMBCCTX
*context
, const char *fname
, mode_t mode
)
1957 fstring server
, share
, user
, password
, workgroup
;
1960 if (!context
|| !context
->internal
||
1961 !context
->internal
->_initialized
) {
1975 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1977 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1979 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1981 fstrcpy(workgroup
, context
->workgroup
);
1983 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1987 return -1; /* errno set by smbc_server */
1991 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1993 mode = aDIR | aRONLY;
1996 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1998 if (strcmp(path, "\\") == 0) {
2000 mode = aDIR | aRONLY;
2006 smbc_stat_printjob(srv, path, &size, &m_time);
2007 c_time = a_time = m_time;
2012 if (!cli_mkdir(&srv
->cli
, path
)) {
2014 errno
= smbc_errno(context
, &srv
->cli
);
2024 * Our list function simply checks to see if a directory is not empty
2027 static int smbc_rmdir_dirempty
= True
;
2029 static void rmdir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
2032 if (strncmp(finfo
->name
, ".", 1) != 0 && strncmp(finfo
->name
, "..", 2) != 0)
2033 smbc_rmdir_dirempty
= False
;
2038 * Routine to remove a directory
2041 static int smbc_rmdir_ctx(SMBCCTX
*context
, const char *fname
)
2044 fstring server
, share
, user
, password
, workgroup
;
2047 if (!context
|| !context
->internal
||
2048 !context
->internal
->_initialized
) {
2062 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
2064 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2066 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2068 fstrcpy(workgroup
, context
->workgroup
);
2070 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2074 return -1; /* errno set by smbc_server */
2078 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2080 mode = aDIR | aRONLY;
2083 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2085 if (strcmp(path, "\\") == 0) {
2087 mode = aDIR | aRONLY;
2093 smbc_stat_printjob(srv, path, &size, &m_time);
2094 c_time = a_time = m_time;
2099 if (!cli_rmdir(&srv
->cli
, path
)) {
2101 errno
= smbc_errno(context
, &srv
->cli
);
2103 if (errno
== EACCES
) { /* Check if the dir empty or not */
2105 pstring lpath
; /* Local storage to avoid buffer overflows */
2107 smbc_rmdir_dirempty
= True
; /* Make this so ... */
2109 pstrcpy(lpath
, path
);
2110 pstrcat(lpath
, "\\*");
2112 if (cli_list(&srv
->cli
, lpath
, aDIR
| aSYSTEM
| aHIDDEN
, rmdir_list_fn
,
2115 /* Fix errno to ignore latest error ... */
2117 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2118 smbc_errno(context
, &srv
->cli
)));
2123 if (smbc_rmdir_dirempty
)
2139 * Routine to return the current directory position
2142 static off_t
smbc_telldir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2145 if (!context
|| !context
->internal
||
2146 !context
->internal
->_initialized
) {
2153 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2160 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2167 return (off_t
) dir
->dir_next
;
2172 * A routine to run down the list and see if the entry is OK
2175 struct smbc_dir_list
*smbc_check_dir_ent(struct smbc_dir_list
*list
,
2176 struct smbc_dirent
*dirent
)
2179 /* Run down the list looking for what we want */
2183 struct smbc_dir_list
*tmp
= list
;
2187 if (tmp
->dirent
== dirent
)
2196 return NULL
; /* Not found, or an error */
2202 * Routine to seek on a directory
2205 static int smbc_lseekdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, off_t offset
)
2207 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)offset
;
2208 struct smbc_dir_list
*list_ent
= NULL
;
2210 if (!context
|| !context
->internal
||
2211 !context
->internal
->_initialized
) {
2218 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2225 /* Now, check what we were passed and see if it is OK ... */
2227 if (dirent
== NULL
) { /* Seek to the begining of the list */
2229 dir
->dir_next
= dir
->dir_list
;
2234 /* Now, run down the list and make sure that the entry is OK */
2235 /* This may need to be changed if we change the format of the list */
2237 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
2239 errno
= EINVAL
; /* Bad entry */
2244 dir
->dir_next
= list_ent
;
2251 * Routine to fstat a dir
2254 static int smbc_fstatdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct stat
*st
)
2257 if (!context
|| !context
->internal
||
2258 !context
->internal
->_initialized
) {
2265 /* No code yet ... */
2272 * Open a print file to be written to by other calls
2275 static SMBCFILE
*smbc_open_print_job_ctx(SMBCCTX
*context
, const char *fname
)
2277 fstring server
, share
, user
, password
;
2280 if (!context
|| !context
->internal
||
2281 !context
->internal
->_initialized
) {
2295 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
2297 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2299 /* What if the path is empty, or the file exists? */
2301 return context
->open(context
, fname
, O_WRONLY
, 666);
2306 * Routine to print a file on a remote server ...
2308 * We open the file, which we assume to be on a remote server, and then
2309 * copy it to a print file on the share specified by printq.
2312 static int smbc_print_file_ctx(SMBCCTX
*c_file
, const char *fname
, SMBCCTX
*c_print
, const char *printq
)
2314 SMBCFILE
*fid1
, *fid2
;
2315 int bytes
, saverr
, tot_bytes
= 0;
2318 if (!c_file
|| !c_file
->internal
->_initialized
|| !c_print
||
2319 !c_print
->internal
->_initialized
) {
2326 if (!fname
&& !printq
) {
2333 /* Try to open the file for reading ... */
2335 if ((fid1
= c_file
->open(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
2337 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
2338 return -1; /* smbc_open sets errno */
2342 /* Now, try to open the printer file for writing */
2344 if ((fid2
= c_print
->open_print_job(c_print
, printq
)) < 0) {
2346 saverr
= errno
; /* Save errno */
2347 c_file
->close(c_file
, fid1
);
2353 while ((bytes
= c_file
->read(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
2357 if ((c_print
->write(c_print
, fid2
, buf
, bytes
)) < 0) {
2360 c_file
->close(c_file
, fid1
);
2361 c_print
->close(c_print
, fid2
);
2370 c_file
->close(c_file
, fid1
); /* We have to close these anyway */
2371 c_print
->close(c_print
, fid2
);
2385 * Routine to list print jobs on a printer share ...
2388 static int smbc_list_print_jobs_ctx(SMBCCTX
*context
, const char *fname
, smbc_list_print_job_fn fn
)
2391 fstring server
, share
, user
, password
, workgroup
;
2394 if (!context
|| !context
->internal
||
2395 !context
->internal
->_initialized
) {
2409 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
2411 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2413 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2415 fstrcpy(workgroup
, context
->workgroup
);
2417 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2421 return -1; /* errno set by smbc_server */
2425 if (cli_print_queue(&srv
->cli
, (void (*)(struct print_job_info
*))fn
) < 0) {
2427 errno
= smbc_errno(context
, &srv
->cli
);
2437 * Delete a print job from a remote printer share
2440 static int smbc_unlink_print_job_ctx(SMBCCTX
*context
, const char *fname
, int id
)
2443 fstring server
, share
, user
, password
, workgroup
;
2447 if (!context
|| !context
->internal
||
2448 !context
->internal
->_initialized
) {
2462 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
2464 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2466 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2468 fstrcpy(workgroup
, context
->workgroup
);
2470 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2474 return -1; /* errno set by smbc_server */
2478 if ((err
= cli_printjob_del(&srv
->cli
, id
)) != 0) {
2481 errno
= smbc_errno(context
, &srv
->cli
);
2482 else if (err
== ERRnosuchprintjob
)
2493 * Get a new empty handle to fill in with your own info
2495 SMBCCTX
* smbc_new_context(void)
2499 context
= malloc(sizeof(SMBCCTX
));
2505 ZERO_STRUCTP(context
);
2507 context
->internal
= malloc(sizeof(struct smbc_internal_data
));
2508 if (!context
->internal
) {
2513 ZERO_STRUCTP(context
->internal
);
2516 /* ADD REASONABLE DEFAULTS */
2518 context
->timeout
= 20000; /* 20 seconds */
2520 context
->open
= smbc_open_ctx
;
2521 context
->creat
= smbc_creat_ctx
;
2522 context
->read
= smbc_read_ctx
;
2523 context
->write
= smbc_write_ctx
;
2524 context
->close
= smbc_close_ctx
;
2525 context
->unlink
= smbc_unlink_ctx
;
2526 context
->rename
= smbc_rename_ctx
;
2527 context
->lseek
= smbc_lseek_ctx
;
2528 context
->stat
= smbc_stat_ctx
;
2529 context
->fstat
= smbc_fstat_ctx
;
2530 context
->opendir
= smbc_opendir_ctx
;
2531 context
->closedir
= smbc_closedir_ctx
;
2532 context
->readdir
= smbc_readdir_ctx
;
2533 context
->getdents
= smbc_getdents_ctx
;
2534 context
->mkdir
= smbc_mkdir_ctx
;
2535 context
->rmdir
= smbc_rmdir_ctx
;
2536 context
->telldir
= smbc_telldir_ctx
;
2537 context
->lseekdir
= smbc_lseekdir_ctx
;
2538 context
->fstatdir
= smbc_fstatdir_ctx
;
2539 context
->open_print_job
= smbc_open_print_job_ctx
;
2540 context
->print_file
= smbc_print_file_ctx
;
2541 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
2542 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
2544 context
->callbacks
.check_server_fn
= smbc_check_server
;
2545 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
2547 smbc_default_cache_functions(context
);
2555 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2556 * and thus you'll be leaking memory if not handled properly.
2559 int smbc_free_context(SMBCCTX
* context
, int shutdown_ctx
)
2568 DEBUG(1,("Performing aggressive shutdown.\n"));
2570 f
= context
->internal
->_files
;
2572 context
->close(context
, f
);
2575 context
->internal
->_files
= NULL
;
2577 /* First try to remove the servers the nice way. */
2578 if (context
->callbacks
.purge_cached_fn(context
)) {
2580 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2581 s
= context
->internal
->_servers
;
2583 cli_shutdown(&s
->cli
);
2584 context
->callbacks
.remove_cached_srv_fn(context
, s
);
2588 context
->internal
->_servers
= NULL
;
2592 /* This is the polite way */
2593 if (context
->callbacks
.purge_cached_fn(context
)) {
2594 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2598 if (context
->internal
->_servers
) {
2599 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2603 if (context
->internal
->_files
) {
2604 DEBUG(1, ("Active files in context, free_context failed.\n"));
2610 /* Things we have to clean up */
2611 SAFE_FREE(context
->workgroup
);
2612 SAFE_FREE(context
->netbios_name
);
2613 SAFE_FREE(context
->user
);
2615 DEBUG(3, ("Context %p succesfully freed\n", context
));
2616 SAFE_FREE(context
->internal
);
2623 * Initialise the library etc
2625 * We accept a struct containing handle information.
2626 * valid values for info->debug from 0 to 100,
2627 * and insist that info->fn must be non-null.
2629 SMBCCTX
* smbc_init_context(SMBCCTX
* context
)
2633 char *user
= NULL
, *home
= NULL
;
2635 if (!context
|| !context
->internal
) {
2640 /* Do not initialise the same client twice */
2641 if (context
->internal
->_initialized
) {
2645 if (!context
->callbacks
.auth_fn
|| context
->debug
< 0 || context
->debug
> 100) {
2652 if (!smbc_initialized
) {
2653 /* Do some library wide intialisations the first time we get called */
2655 /* Do we still need this ? */
2658 setup_logging( "libsmbclient", False
);
2660 /* Here we would open the smb.conf file if needed ... */
2662 home
= getenv("HOME");
2664 slprintf(conf
, sizeof(conf
), "%s/.smb/smb.conf", home
);
2666 load_interfaces(); /* Load the list of interfaces ... */
2668 in_client
= True
; /* FIXME, make a param */
2670 if (!lp_load(conf
, True
, False
, False
)) {
2673 * Hmmm, what the hell do we do here ... we could not parse the
2674 * config file ... We must return an error ... and keep info around
2675 * about why we failed
2678 errno
= ENOENT
; /* FIXME: Figure out the correct error response */
2682 reopen_logs(); /* Get logging working ... */
2685 * Block SIGPIPE (from lib/util_sock.c: write())
2686 * It is not needed and should not stop execution
2688 BlockSignals(True
, SIGPIPE
);
2690 /* Done with one-time initialisation */
2691 smbc_initialized
= 1;
2695 if (!context
->user
) {
2697 * FIXME: Is this the best way to get the user info?
2699 user
= getenv("USER");
2700 /* walk around as "guest" if no username can be found */
2701 if (!user
) context
->user
= strdup("guest");
2702 else context
->user
= strdup(user
);
2705 if (!context
->netbios_name
) {
2707 * We try to get our netbios name from the config. If that fails we fall
2708 * back on constructing our netbios name from our hostname etc
2710 if (global_myname()) {
2711 context
->netbios_name
= strdup(global_myname());
2715 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2718 context
->netbios_name
= malloc(17);
2719 if (!context
->netbios_name
) {
2723 slprintf(context
->netbios_name
, 16, "smbc%s%d", context
->user
, pid
);
2726 DEBUG(0,("Using netbios name %s.\n", context
->netbios_name
));
2729 if (!context
->workgroup
) {
2730 if (lp_workgroup()) {
2731 context
->workgroup
= strdup(lp_workgroup());
2734 /* TODO: Think about a decent default workgroup */
2735 context
->workgroup
= strdup("samba");
2738 DEBUG(0,("Using workgroup %s.\n", context
->workgroup
));
2740 /* shortest timeout is 1 second */
2741 if (context
->timeout
> 0 && context
->timeout
< 1000)
2742 context
->timeout
= 1000;
2745 * FIXME: Should we check the function pointers here?
2748 context
->internal
->_initialized
= 1;