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 * Function to parse a path and turn it into components
58 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
60 * smb:// means show all the workgroups
61 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
62 * else, if name<20> exists, list all shares for server ...
65 static const char *smbc_prefix
= "smb:";
68 smbc_parse_path(SMBCCTX
*context
, const char *fname
, char *server
, char *share
, char *path
,
69 char *user
, char *password
) /* FIXME, lengths of strings */
77 server
[0] = share
[0] = path
[0] = user
[0] = password
[0] = (char)0;
80 /* clean_fname(s); causing problems ... */
82 /* see if it has the right prefix */
83 len
= strlen(smbc_prefix
);
84 if (strncmp(s
,smbc_prefix
,len
) ||
85 (s
[len
] != '/' && s
[len
] != 0)) return -1; /* What about no smb: ? */
89 /* Watch the test below, we are testing to see if we should exit */
91 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
97 p
+= 2; /* Skip the // or \\ */
104 strncpy(server
, context
->workgroup
,
105 (strlen(context
->workgroup
) < 16)?strlen(context
->workgroup
):16);
111 * ok, its for us. Now parse out the server, share etc.
113 * However, we want to parse out [[domain;]user[:password]@] if it
117 /* check that '@' occurs before '/', if '/' exists at all */
118 q
= strchr_m(p
, '@');
119 r
= strchr_m(p
, '/');
120 if (q
&& (!r
|| q
< r
)) {
121 pstring username
, passwd
, domain
;
122 const char *u
= userinfo
;
124 next_token(&p
, userinfo
, "@", sizeof(fstring
));
126 username
[0] = passwd
[0] = domain
[0] = 0;
128 if (strchr_m(u
, ';')) {
130 next_token(&u
, domain
, ";", sizeof(fstring
));
134 if (strchr_m(u
, ':')) {
136 next_token(&u
, username
, ":", sizeof(fstring
));
143 pstrcpy(username
, u
);
148 strncpy(user
, username
, sizeof(fstring
)); /* FIXME, size and domain */
151 strncpy(password
, passwd
, sizeof(fstring
)); /* FIXME, size */
155 if (!next_token(&p
, server
, "/", sizeof(fstring
))) {
161 if (*p
== (char)0) return 0; /* That's it ... */
163 if (!next_token(&p
, share
, "/", sizeof(fstring
))) {
171 all_string_sub(path
, "/", "\\", 0);
177 * Convert an SMB error into a UNIX error ...
180 static int smbc_errno(SMBCCTX
*context
, struct cli_state
*c
)
182 int ret
= cli_errno(c
);
184 if (cli_is_dos_error(c
)) {
188 cli_dos_error(c
, &eclass
, &ecode
);
190 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
191 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
195 status
= cli_nt_error(c
);
197 DEBUG(3,("smbc errno %s -> %d\n",
198 nt_errstr(status
), ret
));
206 * returns 0 if the server is in shape. Returns 1 on error
208 * Also useable outside libsmbclient to enable external cache
209 * to do some checks too.
211 int smbc_check_server(SMBCCTX
* context
, SMBCSRV
* server
)
213 if ( send_keepalive(server
->cli
.fd
) == False
)
216 /* connection is ok */
221 * Remove a server from the cached server list it's unused.
222 * On success, 0 is returned. 1 is returned if the server could not be removed.
224 * Also useable outside libsmbclient
226 int smbc_remove_unused_server(SMBCCTX
* context
, SMBCSRV
* srv
)
230 /* are we being fooled ? */
231 if (!context
|| !context
->internal
||
232 !context
->internal
->_initialized
|| !srv
) return 1;
235 /* Check all open files/directories for a relation with this server */
236 for (file
= context
->internal
->_files
; file
; file
=file
->next
) {
237 if (file
->srv
== srv
) {
239 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
245 DLIST_REMOVE(context
->internal
->_servers
, srv
);
247 cli_shutdown(&srv
->cli
);
249 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
251 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
259 * Connect to a server, possibly on an existing connection
261 * Here, what we want to do is: If the server and username
262 * match an existing connection, reuse that, otherwise, establish a
265 * If we have to create a new connection, call the auth_fn to get the
266 * info we need, unless the username and password were passed in.
269 SMBCSRV
*smbc_server(SMBCCTX
*context
,
270 char *server
, char *share
,
271 char *workgroup
, char *username
,
277 struct nmb_name called
, calling
;
278 char *p
, *server_n
= server
;
282 int tried_reverse
= 0;
287 if (server
[0] == 0) {
294 srv
= context
->callbacks
.get_cached_srv_fn(context
, server
, share
,
295 workgroup
, username
);
297 if (!auth_called
&& !srv
&& (!username
[0] || !password
[0])) {
298 context
->callbacks
.auth_fn(server
, share
, workgroup
, sizeof(fstring
),
299 username
, sizeof(fstring
), password
, sizeof(fstring
));
301 * However, smbc_auth_fn may have picked up info relating to an
302 * existing connection, so try for an existing connection again ...
305 goto check_server_cache
;
310 if (context
->callbacks
.check_server_fn(context
, srv
)) {
312 * This server is no good anymore
313 * Try to remove it and check for more possible servers in the cache
315 if (context
->callbacks
.remove_unused_server_fn(context
, srv
)) {
317 * We could not remove the server completely, remove it from the cache
318 * so we will not get it again. It will be removed when the last file/dir
321 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
325 * Maybe there are more cached connections to this server
327 goto check_server_cache
;
332 make_nmb_name(&calling
, context
->netbios_name
, 0x0);
333 make_nmb_name(&called
, server
, 0x20);
335 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n
, server
));
337 if ((p
=strchr_m(server_n
,'#')) &&
338 (strcmp(p
+1,"1D")==0 || strcmp(p
+1,"01")==0)) {
340 fstrcpy(group
, server_n
);
341 p
= strchr_m(group
,'#');
346 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
349 slprintf(ipenv
,sizeof(ipenv
)-1,"HOST_%s", server_n
);
353 /* have to open a new connection */
354 if (!cli_initialise(&c
)) {
359 c
.timeout
= context
->timeout
;
361 if (!cli_connect(&c
, server_n
, &ip
)) {
367 if (!cli_session_request(&c
, &calling
, &called
)) {
369 if (strcmp(called
.name
, "*SMBSERVER")) {
370 make_nmb_name(&called
, "*SMBSERVER", 0x20);
373 else { /* Try one more time, but ensure we don't loop */
375 /* Only try this if server is an IP address ... */
377 if (is_ipaddress(server
) && !tried_reverse
) {
379 struct in_addr rem_ip
;
381 if ((rem_ip
.s_addr
=inet_addr(server
)) == INADDR_NONE
) {
382 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server
));
387 tried_reverse
++; /* Yuck */
389 if (name_status_find("*", 0, 0, rem_ip
, remote_name
)) {
390 make_nmb_name(&called
, remote_name
, 0x20);
401 DEBUG(4,(" session request ok\n"));
403 if (!cli_negprot(&c
)) {
409 if (!cli_session_setup(&c
, username
,
410 password
, strlen(password
),
411 password
, strlen(password
),
413 /* try an anonymous login if it failed */
414 !cli_session_setup(&c
, "", "", 1,"", 0, workgroup
)) {
420 DEBUG(4,(" session setup ok\n"));
422 if (!cli_send_tconX(&c
, share
, "?????",
423 password
, strlen(password
)+1)) {
424 errno
= smbc_errno(context
, &c
);
429 DEBUG(4,(" tconx ok\n"));
432 * Ok, we have got a nice connection
433 * Let's find a free server_fd
436 srv
= (SMBCSRV
*)malloc(sizeof(*srv
));
444 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
446 /* now add it to the cache (internal or external) */
447 if (context
->callbacks
.add_cached_srv_fn(context
, srv
, server
, share
, workgroup
, username
)) {
448 DEBUG(3, (" Failed to add server to cache\n"));
453 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
454 server
, share
, srv
));
460 if (!srv
) return NULL
;
467 * Routine to open() a file ...
470 static SMBCFILE
*smbc_open_ctx(SMBCCTX
*context
, const char *fname
, int flags
, mode_t mode
)
472 fstring server
, share
, user
, password
, workgroup
;
475 SMBCFILE
*file
= NULL
;
478 if (!context
|| !context
->internal
||
479 !context
->internal
->_initialized
) {
481 errno
= EINVAL
; /* Best I can think of ... */
493 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
495 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
497 pstrcpy(workgroup
, context
->workgroup
);
499 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
503 if (errno
== EPERM
) errno
= EACCES
;
504 return NULL
; /* smbc_server sets errno */
508 /* Hmmm, the test for a directory is suspect here ... FIXME */
510 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
517 file
= malloc(sizeof(SMBCFILE
));
528 if ((fd
= cli_open(&srv
->cli
, path
, flags
, DENY_NONE
)) < 0) {
530 /* Handle the error ... */
533 errno
= smbc_errno(context
, &srv
->cli
);
538 /* Fill in file struct */
541 file
->fname
= strdup(fname
);
546 DLIST_ADD(context
->internal
->_files
, file
);
551 /* Check if opendir needed ... */
556 eno
= smbc_errno(context
, &srv
->cli
);
557 file
= context
->opendir(context
, fname
);
558 if (!file
) errno
= eno
;
563 errno
= EINVAL
; /* FIXME, correct errno ? */
569 * Routine to create a file
572 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
574 static SMBCFILE
*smbc_creat_ctx(SMBCCTX
*context
, const char *path
, mode_t mode
)
577 if (!context
|| !context
->internal
||
578 !context
->internal
->_initialized
) {
585 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
589 * Routine to read() a file ...
592 static ssize_t
smbc_read_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
596 if (!context
|| !context
->internal
||
597 !context
->internal
->_initialized
) {
604 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
606 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
613 /* Check that the buffer exists ... */
622 ret
= cli_read(&file
->srv
->cli
, file
->cli_fd
, buf
, file
->offset
, count
);
626 errno
= smbc_errno(context
, &file
->srv
->cli
);
633 DEBUG(4, (" --> %d\n", ret
));
635 return ret
; /* Success, ret bytes of data ... */
640 * Routine to write() a file ...
643 static ssize_t
smbc_write_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
647 if (!context
|| !context
->internal
||
648 !context
->internal
->_initialized
) {
655 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
662 /* Check that the buffer exists ... */
671 ret
= cli_write(&file
->srv
->cli
, file
->cli_fd
, 0, buf
, file
->offset
, count
);
675 errno
= smbc_errno(context
, &file
->srv
->cli
);
682 return ret
; /* Success, 0 bytes of data ... */
686 * Routine to close() a file ...
689 static int smbc_close_ctx(SMBCCTX
*context
, SMBCFILE
*file
)
693 if (!context
|| !context
->internal
||
694 !context
->internal
->_initialized
) {
701 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
711 return context
->closedir(context
, file
);
715 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
717 DEBUG(3, ("cli_close failed on %s. purging server.\n",
719 /* Deallocate slot and remove the server
720 * from the server cache if unused */
721 errno
= smbc_errno(context
, &file
->srv
->cli
);
723 DLIST_REMOVE(context
->internal
->_files
, file
);
724 SAFE_FREE(file
->fname
);
726 context
->callbacks
.remove_unused_server_fn(context
, srv
);
734 return context
->closedir(context
, file
);
738 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
739 DEBUG(3, ("cli_close failed on %s. purging server.\n",
741 /* Deallocate slot and remove the server
742 * from the server cache if unused */
743 errno
= smbc_errno(context
, &file
->srv
->cli
);
745 DLIST_REMOVE(context
->internal
->_files
, file
);
746 SAFE_FREE(file
->fname
);
748 context
->callbacks
.remove_unused_server_fn(context
, srv
);
753 DLIST_REMOVE(context
->internal
->_files
, file
);
754 SAFE_FREE(file
->fname
);
761 * Get info from an SMB server on a file. Use a qpathinfo call first
762 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
764 static BOOL
smbc_getatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
765 uint16
*mode
, size_t *size
,
766 time_t *c_time
, time_t *a_time
, time_t *m_time
,
770 if (!context
|| !context
->internal
||
771 !context
->internal
->_initialized
) {
778 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
780 if (!srv
->no_pathinfo2
&&
781 cli_qpathinfo2(&srv
->cli
, path
, c_time
, a_time
, m_time
, NULL
,
782 size
, mode
, ino
)) return True
;
784 /* if this is NT then don't bother with the getatr */
785 if (srv
->cli
.capabilities
& CAP_NT_SMBS
) return False
;
787 if (cli_getatr(&srv
->cli
, path
, mode
, size
, m_time
)) {
788 a_time
= c_time
= m_time
;
789 srv
->no_pathinfo2
= True
;
798 * Routine to unlink() a file
801 static int smbc_unlink_ctx(SMBCCTX
*context
, const char *fname
)
803 fstring server
, share
, user
, password
, workgroup
;
807 if (!context
|| !context
->internal
||
808 !context
->internal
->_initialized
) {
810 errno
= EINVAL
; /* Best I can think of ... */
822 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
824 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
826 pstrcpy(workgroup
, context
->workgroup
);
828 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
832 return -1; /* smbc_server sets errno */
836 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
838 int job = smbc_stat_printjob(srv, path, NULL, NULL);
844 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
852 if (!cli_unlink(&srv
->cli
, path
)) {
854 errno
= smbc_errno(context
, &srv
->cli
);
856 if (errno
== EACCES
) { /* Check if the file is a directory */
861 time_t m_time
= 0, a_time
= 0, c_time
= 0;
864 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
865 &c_time
, &a_time
, &m_time
, &ino
)) {
867 /* Hmmm, bad error ... What? */
869 errno
= smbc_errno(context
, &srv
->cli
);
875 if (IS_DOS_DIR(mode
))
878 errno
= saverr
; /* Restore this */
887 return 0; /* Success ... */
892 * Routine to rename() a file
895 static int smbc_rename_ctx(SMBCCTX
*ocontext
, const char *oname
,
896 SMBCCTX
*ncontext
, const char *nname
)
898 fstring server1
, share1
, server2
, share2
, user1
, user2
, password1
, password2
, workgroup
;
899 pstring path1
, path2
;
902 if (!ocontext
|| !ncontext
||
903 !ocontext
->internal
|| !ncontext
->internal
||
904 !ocontext
->internal
->_initialized
||
905 !ncontext
->internal
->_initialized
) {
907 errno
= EINVAL
; /* Best I can think of ... */
912 if (!oname
|| !nname
) {
919 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
921 smbc_parse_path(ocontext
, oname
, server1
, share1
, path1
, user1
, password1
);
923 if (user1
[0] == (char)0) pstrcpy(user1
, ocontext
->user
);
925 smbc_parse_path(ncontext
, nname
, server2
, share2
, path2
, user2
, password2
);
927 if (user2
[0] == (char)0) pstrcpy(user2
, ncontext
->user
);
929 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
930 strcmp(user1
, user2
)) {
932 /* Can't rename across file systems, or users?? */
939 pstrcpy(workgroup
, ocontext
->workgroup
);
940 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
941 srv
= smbc_server(ocontext
, server1
, share1
, workgroup
, user1
, password1
);
948 if (!cli_rename(&srv
->cli
, path1
, path2
)) {
949 int eno
= smbc_errno(ocontext
, &srv
->cli
);
952 !cli_unlink(&srv
->cli
, path2
) ||
953 !cli_rename(&srv
->cli
, path1
, path2
)) {
961 return 0; /* Success */
966 * A routine to lseek() a file
969 static off_t
smbc_lseek_ctx(SMBCCTX
*context
, SMBCFILE
*file
, off_t offset
, int whence
)
973 if (!context
|| !context
->internal
||
974 !context
->internal
->_initialized
) {
981 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
991 return -1; /* Can't lseek a dir ... */
997 file
->offset
= offset
;
1001 file
->offset
+= offset
;
1005 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1006 NULL
, NULL
, NULL
) &&
1007 !cli_getattrE(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1013 file
->offset
= size
+ offset
;
1022 return file
->offset
;
1027 * Generate an inode number from file name for those things that need it
1031 ino_t
smbc_inode(SMBCCTX
*context
, const char *name
)
1034 if (!context
|| !context
->internal
||
1035 !context
->internal
->_initialized
) {
1042 if (!*name
) return 2; /* FIXME, why 2 ??? */
1043 return (ino_t
)str_checksum(name
);
1048 * Routine to put basic stat info into a stat structure ... Used by stat and
1053 int smbc_setup_stat(SMBCCTX
*context
, struct stat
*st
, char *fname
, size_t size
, int mode
)
1058 if (IS_DOS_DIR(mode
)) {
1059 st
->st_mode
= SMBC_DIR_MODE
;
1061 st
->st_mode
= SMBC_FILE_MODE
;
1064 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
1065 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
1066 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
1067 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
1070 st
->st_blksize
= 512;
1071 st
->st_blocks
= (size
+511)/512;
1072 st
->st_uid
= getuid();
1073 st
->st_gid
= getgid();
1075 if (IS_DOS_DIR(mode
)) {
1081 if (st
->st_ino
== 0) {
1082 st
->st_ino
= smbc_inode(context
, fname
);
1085 return True
; /* FIXME: Is this needed ? */
1090 * Routine to stat a file given a name
1093 static int smbc_stat_ctx(SMBCCTX
*context
, const char *fname
, struct stat
*st
)
1096 fstring server
, share
, user
, password
, workgroup
;
1098 time_t m_time
= 0, a_time
= 0, c_time
= 0;
1103 if (!context
|| !context
->internal
||
1104 !context
->internal
->_initialized
) {
1106 errno
= EINVAL
; /* Best I can think of ... */
1118 DEBUG(4, ("smbc_stat(%s)\n", fname
));
1120 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1122 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1124 pstrcpy(workgroup
, context
->workgroup
);
1126 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1130 return -1; /* errno set by smbc_server */
1134 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1136 mode = aDIR | aRONLY;
1139 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1141 if (strcmp(path, "\\") == 0) {
1143 mode = aDIR | aRONLY;
1149 smbc_stat_printjob(srv, path, &size, &m_time);
1150 c_time = a_time = m_time;
1155 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1156 &c_time
, &a_time
, &m_time
, &ino
)) {
1158 errno
= smbc_errno(context
, &srv
->cli
);
1165 smbc_setup_stat(context
, st
, path
, size
, mode
);
1167 st
->st_atime
= a_time
;
1168 st
->st_ctime
= c_time
;
1169 st
->st_mtime
= m_time
;
1170 st
->st_dev
= srv
->dev
;
1177 * Routine to stat a file given an fd
1180 static int smbc_fstat_ctx(SMBCCTX
*context
, SMBCFILE
*file
, struct stat
*st
)
1182 time_t c_time
, a_time
, m_time
;
1187 if (!context
|| !context
->internal
||
1188 !context
->internal
->_initialized
) {
1195 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1204 return context
->fstatdir(context
, file
, st
);
1208 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
,
1209 &mode
, &size
, &c_time
, &a_time
, &m_time
, NULL
, &ino
) &&
1210 !cli_getattrE(&file
->srv
->cli
, file
->cli_fd
,
1211 &mode
, &size
, &c_time
, &a_time
, &m_time
)) {
1220 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
1222 st
->st_atime
= a_time
;
1223 st
->st_ctime
= c_time
;
1224 st
->st_mtime
= m_time
;
1225 st
->st_dev
= file
->srv
->dev
;
1232 * Routine to open a directory
1236 * smb: which should list all the workgroups available
1238 * smb:workgroup//server
1240 * smb://server/share
1241 * smb://<IP-addr> which should list shares on server
1242 * smb://<IP-addr>/share which should list files on share
1245 static void smbc_remove_dir(SMBCFILE
*dir
)
1247 struct smbc_dir_list
*d
,*f
;
1254 SAFE_FREE(f
->dirent
);
1259 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
1263 static int add_dirent(SMBCFILE
*dir
, const char *name
, const char *comment
, uint32 type
)
1265 struct smbc_dirent
*dirent
;
1269 * Allocate space for the dirent, which must be increased by the
1270 * size of the name and the comment and 1 for the null on the comment.
1271 * The null on the name is already accounted for.
1274 size
= sizeof(struct smbc_dirent
) + (name
?strlen(name
):0) +
1275 (comment
?strlen(comment
):0) + 1;
1277 dirent
= malloc(size
);
1281 dir
->dir_error
= ENOMEM
;
1286 ZERO_STRUCTP(dirent
);
1288 if (dir
->dir_list
== NULL
) {
1290 dir
->dir_list
= malloc(sizeof(struct smbc_dir_list
));
1291 if (!dir
->dir_list
) {
1294 dir
->dir_error
= ENOMEM
;
1298 ZERO_STRUCTP(dir
->dir_list
);
1300 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
1305 dir
->dir_end
->next
= malloc(sizeof(struct smbc_dir_list
));
1307 if (!dir
->dir_end
->next
) {
1310 dir
->dir_error
= ENOMEM
;
1314 ZERO_STRUCTP(dir
->dir_end
->next
);
1316 dir
->dir_end
= dir
->dir_end
->next
;
1320 dir
->dir_end
->next
= NULL
;
1321 dir
->dir_end
->dirent
= dirent
;
1323 dirent
->smbc_type
= type
;
1324 dirent
->namelen
= (name
?strlen(name
):0);
1325 dirent
->commentlen
= (comment
?strlen(comment
):0);
1326 dirent
->dirlen
= size
;
1328 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
1330 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
1331 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
1338 list_fn(const char *name
, uint32 type
, const char *comment
, void *state
)
1340 SMBCFILE
*dir
= (SMBCFILE
*)state
;
1343 /* We need to process the type a little ... */
1345 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
1348 case 0: /* Directory tree */
1349 dirent_type
= SMBC_FILE_SHARE
;
1353 dirent_type
= SMBC_PRINTER_SHARE
;
1357 dirent_type
= SMBC_COMMS_SHARE
;
1361 dirent_type
= SMBC_IPC_SHARE
;
1365 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
1369 else dirent_type
= dir
->dir_type
;
1371 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
1373 /* An error occurred, what do we do? */
1374 /* FIXME: Add some code here */
1381 dir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
1384 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
1385 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
1387 /* Handle an error ... */
1389 /* FIXME: Add some code ... */
1395 static SMBCFILE
*smbc_opendir_ctx(SMBCCTX
*context
, const char *fname
)
1397 fstring server
, share
, user
, password
, workgroup
;
1399 SMBCSRV
*srv
= NULL
;
1400 SMBCFILE
*dir
= NULL
;
1401 struct in_addr rem_ip
;
1403 if (!context
|| !context
->internal
||
1404 !context
->internal
->_initialized
) {
1418 if (smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
)) {
1425 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1427 pstrcpy(workgroup
, context
->workgroup
);
1429 dir
= malloc(sizeof(*dir
));
1441 dir
->fname
= strdup(fname
);
1445 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
1447 if (server
[0] == (char)0) {
1449 if (share
[0] != (char)0 || path
[0] != (char)0) {
1453 SAFE_FREE(dir
->fname
);
1460 /* We have server and share and path empty ... so list the workgroups */
1461 /* first try to get the LMB for our workgroup, and if that fails, */
1464 if (!(resolve_name(context
->workgroup
, &rem_ip
, 0x1d) ||
1465 resolve_name(context
->workgroup
, &rem_ip
, 0x1b))) {
1467 errno
= EINVAL
; /* Something wrong with smb.conf? */
1472 dir
->dir_type
= SMBC_WORKGROUP
;
1474 /* find the name of the server ... */
1476 if (!name_status_find("*", 0, 0, rem_ip
, server
)) {
1478 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server
));
1485 * Get a connection to IPC$ on the server if we do not already have one
1488 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1493 SAFE_FREE(dir
->fname
);
1503 /* Now, list the stuff ... */
1505 if (!cli_NetServerEnum(&srv
->cli
, workgroup
, 0x80000000, list_fn
,
1509 SAFE_FREE(dir
->fname
);
1512 errno
= cli_errno(&srv
->cli
);
1518 else { /* Server not an empty string ... Check the rest and see what gives */
1520 if (share
[0] == (char)0) {
1522 if (path
[0] != (char)0) { /* Should not have empty share with path */
1526 SAFE_FREE(dir
->fname
);
1533 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1534 /* However, we check to see if <server> is an IP address first */
1536 if (!is_ipaddress(server
) && /* Not an IP addr so check next */
1537 (resolve_name(server
, &rem_ip
, 0x1d) || /* Found LMB */
1538 resolve_name(server
, &rem_ip
, 0x1b) )) { /* Found DMB */
1541 dir
->dir_type
= SMBC_SERVER
;
1544 * Get the backup list ...
1548 if (!name_status_find("*", 0, 0, rem_ip
, buserver
)) {
1550 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server
));
1551 errno
= EPERM
; /* FIXME, is this correct */
1557 * Get a connection to IPC$ on the server if we do not already have one
1560 srv
= smbc_server(context
, buserver
, "IPC$", workgroup
, user
, password
);
1565 SAFE_FREE(dir
->fname
);
1574 /* Now, list the servers ... */
1576 if (!cli_NetServerEnum(&srv
->cli
, server
, 0x0000FFFE, list_fn
,
1580 SAFE_FREE(dir
->fname
);
1583 errno
= cli_errno(&srv
->cli
);
1591 if (resolve_name(server
, &rem_ip
, 0x20)) {
1593 /* Now, list the shares ... */
1595 dir
->dir_type
= SMBC_FILE_SHARE
;
1597 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1602 SAFE_FREE(dir
->fname
);
1611 /* Now, list the servers ... */
1613 if (cli_RNetShareEnum(&srv
->cli
, list_fn
,
1616 errno
= cli_errno(&srv
->cli
);
1618 SAFE_FREE(dir
->fname
);
1628 errno
= ENODEV
; /* Neither the workgroup nor server exists */
1630 SAFE_FREE(dir
->fname
);
1640 else { /* The server and share are specified ... work from there ... */
1642 /* Well, we connect to the server and list the directory */
1644 dir
->dir_type
= SMBC_FILE_SHARE
;
1646 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1651 SAFE_FREE(dir
->fname
);
1660 /* Now, list the files ... */
1662 pstrcat(path
, "\\*");
1664 if (cli_list(&srv
->cli
, path
, aDIR
| aSYSTEM
| aHIDDEN
, dir_list_fn
,
1668 SAFE_FREE(dir
->fname
);
1671 errno
= smbc_errno(context
, &srv
->cli
);
1679 DLIST_ADD(context
->internal
->_files
, dir
);
1685 * Routine to close a directory
1688 static int smbc_closedir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
1691 if (!context
|| !context
->internal
||
1692 !context
->internal
->_initialized
) {
1699 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
1706 smbc_remove_dir(dir
); /* Clean it up */
1708 DLIST_REMOVE(context
->internal
->_files
, dir
);
1712 SAFE_FREE(dir
->fname
);
1713 SAFE_FREE(dir
); /* Free the space too */
1722 * Routine to get a directory entry
1725 struct smbc_dirent
*smbc_readdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
1727 struct smbc_dirent
*dirp
, *dirent
;
1729 /* Check that all is ok first ... */
1731 if (!context
|| !context
->internal
||
1732 !context
->internal
->_initialized
) {
1739 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
1746 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1757 dirent
= dir
->dir_next
->dirent
;
1766 /* Hmmm, do I even need to copy it? */
1768 memcpy(context
->internal
->_dirent
, dirent
, dirent
->dirlen
); /* Copy the dirent */
1769 dirp
= (struct smbc_dirent
*)context
->internal
->_dirent
;
1770 dirp
->comment
= (char *)(&dirp
->name
+ dirent
->namelen
+ 1);
1771 dir
->dir_next
= dir
->dir_next
->next
;
1773 return (struct smbc_dirent
*)context
->internal
->_dirent
;
1779 * Routine to get directory entries
1782 static int smbc_getdents_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct smbc_dirent
*dirp
, int count
)
1784 struct smbc_dir_list
*dirlist
;
1785 int rem
= count
, reqd
;
1786 char *ndir
= (char *)dirp
;
1788 /* Check that all is ok first ... */
1790 if (!context
|| !context
->internal
||
1791 !context
->internal
->_initialized
) {
1798 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
1805 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1813 * Now, retrieve the number of entries that will fit in what was passed
1814 * We have to figure out if the info is in the list, or we need to
1815 * send a request to the server to get the info.
1818 while ((dirlist
= dir
->dir_next
)) {
1819 struct smbc_dirent
*dirent
;
1821 if (!dirlist
->dirent
) {
1823 errno
= ENOENT
; /* Bad error */
1828 if (rem
< (reqd
= (sizeof(struct smbc_dirent
) + dirlist
->dirent
->namelen
+
1829 dirlist
->dirent
->commentlen
+ 1))) {
1831 if (rem
< count
) { /* We managed to copy something */
1837 else { /* Nothing copied ... */
1839 errno
= EINVAL
; /* Not enough space ... */
1846 dirent
= dirlist
->dirent
;
1848 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
1850 ((struct smbc_dirent
*)ndir
)->comment
=
1851 (char *)(&((struct smbc_dirent
*)ndir
)->name
+ dirent
->namelen
+ 1);
1857 dir
->dir_next
= dirlist
= dirlist
-> next
;
1868 * Routine to create a directory ...
1871 static int smbc_mkdir_ctx(SMBCCTX
*context
, const char *fname
, mode_t mode
)
1874 fstring server
, share
, user
, password
, workgroup
;
1877 if (!context
|| !context
->internal
||
1878 !context
->internal
->_initialized
) {
1892 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1894 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1896 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1898 pstrcpy(workgroup
, context
->workgroup
);
1900 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1904 return -1; /* errno set by smbc_server */
1908 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1910 mode = aDIR | aRONLY;
1913 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1915 if (strcmp(path, "\\") == 0) {
1917 mode = aDIR | aRONLY;
1923 smbc_stat_printjob(srv, path, &size, &m_time);
1924 c_time = a_time = m_time;
1929 if (!cli_mkdir(&srv
->cli
, path
)) {
1931 errno
= smbc_errno(context
, &srv
->cli
);
1941 * Our list function simply checks to see if a directory is not empty
1944 static int smbc_rmdir_dirempty
= True
;
1946 static void rmdir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
1949 if (strncmp(finfo
->name
, ".", 1) != 0 && strncmp(finfo
->name
, "..", 2) != 0)
1950 smbc_rmdir_dirempty
= False
;
1955 * Routine to remove a directory
1958 static int smbc_rmdir_ctx(SMBCCTX
*context
, const char *fname
)
1961 fstring server
, share
, user
, password
, workgroup
;
1964 if (!context
|| !context
->internal
||
1965 !context
->internal
->_initialized
) {
1979 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1981 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1983 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1985 pstrcpy(workgroup
, context
->workgroup
);
1987 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1991 return -1; /* errno set by smbc_server */
1995 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1997 mode = aDIR | aRONLY;
2000 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2002 if (strcmp(path, "\\") == 0) {
2004 mode = aDIR | aRONLY;
2010 smbc_stat_printjob(srv, path, &size, &m_time);
2011 c_time = a_time = m_time;
2016 if (!cli_rmdir(&srv
->cli
, path
)) {
2018 errno
= smbc_errno(context
, &srv
->cli
);
2020 if (errno
== EACCES
) { /* Check if the dir empty or not */
2022 pstring lpath
; /* Local storage to avoid buffer overflows */
2024 smbc_rmdir_dirempty
= True
; /* Make this so ... */
2026 pstrcpy(lpath
, path
);
2027 pstrcat(lpath
, "\\*");
2029 if (cli_list(&srv
->cli
, lpath
, aDIR
| aSYSTEM
| aHIDDEN
, rmdir_list_fn
,
2032 /* Fix errno to ignore latest error ... */
2034 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2035 smbc_errno(context
, &srv
->cli
)));
2040 if (smbc_rmdir_dirempty
)
2056 * Routine to return the current directory position
2059 static off_t
smbc_telldir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2062 if (!context
|| !context
->internal
||
2063 !context
->internal
->_initialized
) {
2070 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2077 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2084 return (off_t
) dir
->dir_next
;
2089 * A routine to run down the list and see if the entry is OK
2092 struct smbc_dir_list
*smbc_check_dir_ent(struct smbc_dir_list
*list
,
2093 struct smbc_dirent
*dirent
)
2096 /* Run down the list looking for what we want */
2100 struct smbc_dir_list
*tmp
= list
;
2104 if (tmp
->dirent
== dirent
)
2113 return NULL
; /* Not found, or an error */
2119 * Routine to seek on a directory
2122 static int smbc_lseekdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, off_t offset
)
2124 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)offset
;
2125 struct smbc_dir_list
*list_ent
= NULL
;
2127 if (!context
|| !context
->internal
||
2128 !context
->internal
->_initialized
) {
2135 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2142 /* Now, check what we were passed and see if it is OK ... */
2144 if (dirent
== NULL
) { /* Seek to the begining of the list */
2146 dir
->dir_next
= dir
->dir_list
;
2151 /* Now, run down the list and make sure that the entry is OK */
2152 /* This may need to be changed if we change the format of the list */
2154 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
2156 errno
= EINVAL
; /* Bad entry */
2161 dir
->dir_next
= list_ent
;
2168 * Routine to fstat a dir
2171 static int smbc_fstatdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct stat
*st
)
2174 if (!context
|| !context
->internal
||
2175 !context
->internal
->_initialized
) {
2182 /* No code yet ... */
2189 * Open a print file to be written to by other calls
2192 static SMBCFILE
*smbc_open_print_job_ctx(SMBCCTX
*context
, const char *fname
)
2194 fstring server
, share
, user
, password
;
2197 if (!context
|| !context
->internal
||
2198 !context
->internal
->_initialized
) {
2212 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
2214 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2216 /* What if the path is empty, or the file exists? */
2218 return context
->open(context
, fname
, O_WRONLY
, 666);
2223 * Routine to print a file on a remote server ...
2225 * We open the file, which we assume to be on a remote server, and then
2226 * copy it to a print file on the share specified by printq.
2229 static int smbc_print_file_ctx(SMBCCTX
*c_file
, const char *fname
, SMBCCTX
*c_print
, const char *printq
)
2231 SMBCFILE
*fid1
, *fid2
;
2232 int bytes
, saverr
, tot_bytes
= 0;
2235 if (!c_file
|| !c_file
->internal
->_initialized
|| !c_print
||
2236 !c_print
->internal
->_initialized
) {
2243 if (!fname
&& !printq
) {
2250 /* Try to open the file for reading ... */
2252 if ((fid1
= c_file
->open(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
2254 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
2255 return -1; /* smbc_open sets errno */
2259 /* Now, try to open the printer file for writing */
2261 if ((fid2
= c_print
->open_print_job(c_print
, printq
)) < 0) {
2263 saverr
= errno
; /* Save errno */
2264 c_file
->close(c_file
, fid1
);
2270 while ((bytes
= c_file
->read(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
2274 if ((c_print
->write(c_print
, fid2
, buf
, bytes
)) < 0) {
2277 c_file
->close(c_file
, fid1
);
2278 c_print
->close(c_print
, fid2
);
2287 c_file
->close(c_file
, fid1
); /* We have to close these anyway */
2288 c_print
->close(c_print
, fid2
);
2302 * Routine to list print jobs on a printer share ...
2305 static int smbc_list_print_jobs_ctx(SMBCCTX
*context
, const char *fname
, smbc_list_print_job_fn fn
)
2308 fstring server
, share
, user
, password
, workgroup
;
2311 if (!context
|| !context
->internal
||
2312 !context
->internal
->_initialized
) {
2326 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
2328 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2330 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
2332 pstrcpy(workgroup
, context
->workgroup
);
2334 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2338 return -1; /* errno set by smbc_server */
2342 if (cli_print_queue(&srv
->cli
, (void (*)(struct print_job_info
*))fn
) < 0) {
2344 errno
= smbc_errno(context
, &srv
->cli
);
2354 * Delete a print job from a remote printer share
2357 static int smbc_unlink_print_job_ctx(SMBCCTX
*context
, const char *fname
, int id
)
2360 fstring server
, share
, user
, password
, workgroup
;
2364 if (!context
|| !context
->internal
||
2365 !context
->internal
->_initialized
) {
2379 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
2381 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2383 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
2385 pstrcpy(workgroup
, context
->workgroup
);
2387 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2391 return -1; /* errno set by smbc_server */
2395 if ((err
= cli_printjob_del(&srv
->cli
, id
)) != 0) {
2398 errno
= smbc_errno(context
, &srv
->cli
);
2399 else if (err
== ERRnosuchprintjob
)
2410 * Get a new empty handle to fill in with your own info
2412 SMBCCTX
* smbc_new_context(void)
2416 context
= malloc(sizeof(SMBCCTX
));
2422 ZERO_STRUCTP(context
);
2424 context
->internal
= malloc(sizeof(struct smbc_internal_data
));
2425 if (!context
->internal
) {
2430 ZERO_STRUCTP(context
->internal
);
2433 /* ADD REASONABLE DEFAULTS */
2435 context
->timeout
= 20000; /* 20 seconds */
2437 context
->open
= smbc_open_ctx
;
2438 context
->creat
= smbc_creat_ctx
;
2439 context
->read
= smbc_read_ctx
;
2440 context
->write
= smbc_write_ctx
;
2441 context
->close
= smbc_close_ctx
;
2442 context
->unlink
= smbc_unlink_ctx
;
2443 context
->rename
= smbc_rename_ctx
;
2444 context
->lseek
= smbc_lseek_ctx
;
2445 context
->stat
= smbc_stat_ctx
;
2446 context
->fstat
= smbc_fstat_ctx
;
2447 context
->opendir
= smbc_opendir_ctx
;
2448 context
->closedir
= smbc_closedir_ctx
;
2449 context
->readdir
= smbc_readdir_ctx
;
2450 context
->getdents
= smbc_getdents_ctx
;
2451 context
->mkdir
= smbc_mkdir_ctx
;
2452 context
->rmdir
= smbc_rmdir_ctx
;
2453 context
->telldir
= smbc_telldir_ctx
;
2454 context
->lseekdir
= smbc_lseekdir_ctx
;
2455 context
->fstatdir
= smbc_fstatdir_ctx
;
2456 context
->open_print_job
= smbc_open_print_job_ctx
;
2457 context
->print_file
= smbc_print_file_ctx
;
2458 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
2459 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
2461 context
->callbacks
.check_server_fn
= smbc_check_server
;
2462 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
2464 smbc_default_cache_functions(context
);
2472 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2473 * and thus you'll be leaking memory if not handled properly.
2476 int smbc_free_context(SMBCCTX
* context
, int shutdown_ctx
)
2485 DEBUG(1,("Performing aggressive shutdown.\n"));
2487 f
= context
->internal
->_files
;
2489 context
->close(context
, f
);
2492 context
->internal
->_files
= NULL
;
2494 /* First try to remove the servers the nice way. */
2495 if (context
->callbacks
.purge_cached_fn(context
)) {
2497 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2498 s
= context
->internal
->_servers
;
2500 cli_shutdown(&s
->cli
);
2501 context
->callbacks
.remove_cached_srv_fn(context
, s
);
2505 context
->internal
->_servers
= NULL
;
2509 /* This is the polite way */
2510 if (context
->callbacks
.purge_cached_fn(context
)) {
2511 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2515 if (context
->internal
->_servers
) {
2516 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2520 if (context
->internal
->_files
) {
2521 DEBUG(1, ("Active files in context, free_context failed.\n"));
2527 /* Things we have to clean up */
2528 SAFE_FREE(context
->workgroup
);
2529 SAFE_FREE(context
->netbios_name
);
2530 SAFE_FREE(context
->user
);
2532 DEBUG(3, ("Context %p succesfully freed\n", context
));
2533 SAFE_FREE(context
->internal
);
2540 * Initialise the library etc
2542 * We accept a struct containing handle information.
2543 * valid values for info->debug from 0 to 100,
2544 * and insist that info->fn must be non-null.
2546 SMBCCTX
* smbc_init_context(SMBCCTX
* context
)
2550 char *user
= NULL
, *home
= NULL
;
2552 if (!context
|| !context
->internal
) {
2557 /* Do not initialise the same client twice */
2558 if (context
->internal
->_initialized
) {
2562 if (!context
->callbacks
.auth_fn
|| context
->debug
< 0 || context
->debug
> 100) {
2569 if (!smbc_initialized
) {
2570 /* Do some library wide intialisations the first time we get called */
2572 /* Do we still need this ? */
2575 setup_logging( "libsmbclient", False
);
2577 /* Here we would open the smb.conf file if needed ... */
2579 home
= getenv("HOME");
2581 slprintf(conf
, sizeof(conf
), "%s/.smb/smb.conf", home
);
2583 load_interfaces(); /* Load the list of interfaces ... */
2585 in_client
= True
; /* FIXME, make a param */
2587 if (!lp_load(conf
, True
, False
, False
)) {
2590 * Hmmm, what the hell do we do here ... we could not parse the
2591 * config file ... We must return an error ... and keep info around
2592 * about why we failed
2595 errno
= ENOENT
; /* FIXME: Figure out the correct error response */
2599 reopen_logs(); /* Get logging working ... */
2602 * Block SIGPIPE (from lib/util_sock.c: write())
2603 * It is not needed and should not stop execution
2605 BlockSignals(True
, SIGPIPE
);
2607 /* Done with one-time initialisation */
2608 smbc_initialized
= 1;
2612 if (!context
->user
) {
2614 * FIXME: Is this the best way to get the user info?
2616 user
= getenv("USER");
2617 /* walk around as "guest" if no username can be found */
2618 if (!user
) context
->user
= strdup("guest");
2619 else context
->user
= strdup(user
);
2622 if (!context
->netbios_name
) {
2624 * We try to get our netbios name from the config. If that fails we fall
2625 * back on constructing our netbios name from our hostname etc
2627 if (global_myname()) {
2628 context
->netbios_name
= strdup(global_myname());
2632 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2635 context
->netbios_name
= malloc(17);
2636 if (!context
->netbios_name
) {
2640 slprintf(context
->netbios_name
, 16, "smbc%s%d", context
->user
, pid
);
2643 DEBUG(0,("Using netbios name %s.\n", context
->netbios_name
));
2646 if (!context
->workgroup
) {
2647 if (lp_workgroup()) {
2648 context
->workgroup
= strdup(lp_workgroup());
2651 /* TODO: Think about a decent default workgroup */
2652 context
->workgroup
= strdup("samba");
2655 DEBUG(0,("Using workgroup %s.\n", context
->workgroup
));
2657 /* shortest timeout is 1 second */
2658 if (context
->timeout
> 0 && context
->timeout
< 1000)
2659 context
->timeout
= 1000;
2662 * FIXME: Should we check the function pointers here?
2665 context
->internal
->_initialized
= 1;