2 Unix SMB/Netbios implementation.
4 SMB client library implementation
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Richard Sharpe 2000, 2002
7 Copyright (C) John Terpstra 2000
8 Copyright (C) Tom Jansen (Ninja ISD) 2002
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.
28 * Define this to get the real SMBCFILE and SMBCSRV structures
30 #define _SMBC_INTERNAL
32 #include "libsmbclient.h"
35 * Functions exported by libsmb_cache.c that we need here
37 int smbc_default_cache_functions(SMBCCTX
* context
);
40 * check if an element is part of the list.
41 * FIXME: Does not belong here !
42 * Can anyone put this in a macro in dlinklist.h ?
45 static int DLIST_CONTAINS(SMBCFILE
* list
, SMBCFILE
*p
) {
46 if (!p
|| !list
) return False
;
48 if (p
== list
) return True
;
54 extern BOOL in_client
;
55 extern pstring global_myname
;
58 * Is the logging working / configfile read ?
60 static int smbc_initialized
= 0;
63 * Function to parse a path and turn it into components
65 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
67 * smb:// means show all the workgroups
68 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
69 * else, if name<20> exists, list all shares for server ...
72 static const char *smbc_prefix
= "smb:";
75 smbc_parse_path(SMBCCTX
*context
, const char *fname
, char *server
, char *share
, char *path
,
76 char *user
, char *password
) /* FIXME, lengths of strings */
84 server
[0] = share
[0] = path
[0] = user
[0] = password
[0] = (char)0;
87 /* clean_fname(s); causing problems ... */
89 /* see if it has the right prefix */
90 len
= strlen(smbc_prefix
);
91 if (strncmp(s
,smbc_prefix
,len
) ||
92 (s
[len
] != '/' && s
[len
] != 0)) return -1; /* What about no smb: ? */
96 /* Watch the test below, we are testing to see if we should exit */
98 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
104 p
+= 2; /* Skip the // or \\ */
111 strncpy(server
, context
->workgroup
,
112 (strlen(context
->workgroup
) < 16)?strlen(context
->workgroup
):16);
118 * ok, its for us. Now parse out the server, share etc.
120 * However, we want to parse out [[domain;]user[:password]@] if it
124 /* check that '@' occurs before '/', if '/' exists at all */
125 q
= strchr_m(p
, '@');
126 r
= strchr_m(p
, '/');
127 if (q
&& (!r
|| q
< r
)) {
128 pstring username
, passwd
, domain
;
131 next_token(&p
, userinfo
, "@", sizeof(fstring
));
133 username
[0] = passwd
[0] = domain
[0] = 0;
135 if (strchr_m(u
, ';')) {
137 next_token(&u
, domain
, ";", sizeof(fstring
));
141 if (strchr_m(u
, ':')) {
143 next_token(&u
, username
, ":", sizeof(fstring
));
150 pstrcpy(username
, u
);
155 strncpy(user
, username
, sizeof(fstring
)); /* FIXME, size and domain */
158 strncpy(password
, passwd
, sizeof(fstring
)); /* FIXME, size */
162 if (!next_token(&p
, server
, "/", sizeof(fstring
))) {
168 if (*p
== (char)0) return 0; /* That's it ... */
170 if (!next_token(&p
, share
, "/", sizeof(fstring
))) {
178 all_string_sub(path
, "/", "\\", 0);
184 * Convert an SMB error into a UNIX error ...
187 static int smbc_errno(SMBCCTX
*context
, struct cli_state
*c
)
191 if (cli_is_dos_error(c
)) {
195 cli_dos_error(c
, &eclass
, &ecode
);
196 ret
= cli_errno_from_dos(eclass
, ecode
);
198 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
199 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
203 status
= cli_nt_error(c
);
204 ret
= cli_errno_from_nt(status
);
206 DEBUG(3,("smbc errno %s -> %d\n",
207 get_nt_error_msg(status
), ret
));
215 * returns 0 if the server is in shape. Returns 1 on error
217 * Also useable outside libsmbclient to enable external cache
218 * to do some checks too.
220 int smbc_check_server(SMBCCTX
* context
, SMBCSRV
* server
)
222 if ( cli_send_keepalive(&server
->cli
) == False
)
225 /* connection is ok */
230 * Remove a server from the list server_table if it's unused.
231 * On success, 0 is returned. 1 is returned if the server could not be removed.
233 * Also useable outside libsmbclient
235 int smbc_remove_unused_server(SMBCCTX
* context
, SMBCSRV
* srv
)
239 /* are we being fooled ? */
240 if (!context
|| !context
->_initialized
|| !srv
) return 1;
243 /* Check all open files/directories for a relation with this server */
244 for (file
= context
->_files
; file
; file
=file
->next
) {
245 if (file
->srv
== srv
) {
247 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
253 DLIST_REMOVE(context
->_servers
, srv
);
255 cli_shutdown(&srv
->cli
);
257 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
259 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
267 * Connect to a server, possibly on an existing connection
269 * Here, what we want to do is: If the server and username
270 * match an existing connection, reuse that, otherwise, establish a
273 * If we have to create a new connection, call the auth_fn to get the
274 * info we need, unless the username and password were passed in.
277 SMBCSRV
*smbc_server(SMBCCTX
*context
,
278 char *server
, char *share
,
279 char *workgroup
, char *username
,
285 struct nmb_name called
, calling
;
286 char *p
, *server_n
= server
;
290 int tried_reverse
= 0;
295 if (server
[0] == 0) {
302 srv
= context
->callbacks
.get_cached_srv_fn(context
, server
, share
,
303 workgroup
, username
);
305 if (!auth_called
&& !srv
&& (!username
[0] || !password
[0])) {
306 context
->callbacks
.auth_fn(server
, share
, workgroup
, sizeof(fstring
),
307 username
, sizeof(fstring
), password
, sizeof(fstring
));
309 * However, smbc_auth_fn may have picked up info relating to an
310 * existing connection, so try for an existing connection again ...
313 goto check_server_cache
;
318 if (context
->callbacks
.check_server_fn(context
, srv
)) {
320 * This server is no good anymore
321 * Try to remove it and check for more possible servers in the cache
323 if (context
->callbacks
.remove_unused_server_fn(context
, srv
)) {
325 * We could not remove the server completely, remove it from the cache
326 * so we will not get it again. It will be removed when the last file/dir
329 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
333 * Maybe there are more cached connections to this server
335 goto check_server_cache
;
340 make_nmb_name(&calling
, context
->netbios_name
, 0x0);
341 make_nmb_name(&called
, server
, 0x20);
343 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n
, server
));
345 if ((p
=strchr_m(server_n
,'#')) &&
346 (strcmp(p
+1,"1D")==0 || strcmp(p
+1,"01")==0)) {
348 fstrcpy(group
, server_n
);
349 p
= strchr_m(group
,'#');
354 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
357 slprintf(ipenv
,sizeof(ipenv
)-1,"HOST_%s", server_n
);
361 /* have to open a new connection */
362 if (!cli_initialise(&c
)) {
367 c
.timeout
= context
->timeout
;
369 if (!cli_connect(&c
, server_n
, &ip
)) {
375 if (!cli_session_request(&c
, &calling
, &called
)) {
377 if (strcmp(called
.name
, "*SMBSERVER")) {
378 make_nmb_name(&called
, "*SMBSERVER", 0x20);
381 else { /* Try one more time, but ensure we don't loop */
383 /* Only try this if server is an IP address ... */
385 if (is_ipaddress(server
) && !tried_reverse
) {
387 struct in_addr rem_ip
;
389 if (!inet_aton(server
, &rem_ip
)) {
390 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server
));
395 tried_reverse
++; /* Yuck */
397 if (name_status_find("*", 0, 0, rem_ip
, remote_name
)) {
398 make_nmb_name(&called
, remote_name
, 0x20);
409 DEBUG(4,(" session request ok\n"));
411 if (!cli_negprot(&c
)) {
417 if (!cli_session_setup(&c
, username
,
418 password
, strlen(password
),
419 password
, strlen(password
),
421 /* try an anonymous login if it failed */
422 !cli_session_setup(&c
, "", "", 1,"", 0, workgroup
)) {
428 DEBUG(4,(" session setup ok\n"));
430 if (!cli_send_tconX(&c
, share
, "?????",
431 password
, strlen(password
)+1)) {
432 errno
= smbc_errno(context
, &c
);
437 DEBUG(4,(" tconx ok\n"));
440 * Ok, we have got a nice connection
441 * Let's find a free server_fd
444 srv
= (SMBCSRV
*)malloc(sizeof(*srv
));
452 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
454 /* now add it to the cache (internal or external) */
455 if (context
->callbacks
.add_cached_srv_fn(context
, srv
, server
, share
, workgroup
, username
)) {
456 DEBUG(3, (" Failed to add server to cache\n"));
461 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
462 server
, share
, srv
));
468 if (!srv
) return NULL
;
475 * Routine to open() a file ...
478 static SMBCFILE
*smbc_open_ctx(SMBCCTX
*context
, const char *fname
, int flags
, mode_t mode
)
480 fstring server
, share
, user
, password
, workgroup
;
483 SMBCFILE
*file
= NULL
;
486 if (!context
|| !context
->_initialized
) {
488 errno
= EINVAL
; /* Best I can think of ... */
500 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
502 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
504 pstrcpy(workgroup
, context
->workgroup
);
506 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
510 if (errno
== EPERM
) errno
= EACCES
;
511 return NULL
; /* smbc_server sets errno */
515 /* Hmmm, the test for a directory is suspect here ... FIXME */
517 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
524 file
= malloc(sizeof(SMBCFILE
));
535 if ((fd
= cli_open(&srv
->cli
, path
, flags
, DENY_NONE
)) < 0) {
537 /* Handle the error ... */
540 errno
= smbc_errno(context
, &srv
->cli
);
545 /* Fill in file struct */
548 file
->fname
= strdup(fname
);
553 DLIST_ADD(context
->_files
, file
);
558 /* Check if opendir needed ... */
563 eno
= smbc_errno(context
, &srv
->cli
);
564 file
= context
->opendir(context
, fname
);
565 if (!file
) errno
= eno
;
570 errno
= EINVAL
; /* FIXME, correct errno ? */
576 * Routine to create a file
579 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
581 static SMBCFILE
*smbc_creat_ctx(SMBCCTX
*context
, const char *path
, mode_t mode
)
584 if (!context
|| !context
->_initialized
) {
591 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
595 * Routine to read() a file ...
598 static ssize_t
smbc_read_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
602 if (!context
|| !context
->_initialized
) {
609 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
611 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
618 /* Check that the buffer exists ... */
627 ret
= cli_read(&file
->srv
->cli
, file
->cli_fd
, buf
, file
->offset
, count
);
631 errno
= smbc_errno(context
, &file
->srv
->cli
);
638 DEBUG(4, (" --> %d\n", ret
));
640 return ret
; /* Success, ret bytes of data ... */
645 * Routine to write() a file ...
648 static ssize_t
smbc_write_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
652 if (!context
|| context
->_initialized
) {
659 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
666 /* Check that the buffer exists ... */
675 ret
= cli_write(&file
->srv
->cli
, file
->cli_fd
, 0, buf
, file
->offset
, count
);
679 errno
= smbc_errno(context
, &file
->srv
->cli
);
686 return ret
; /* Success, 0 bytes of data ... */
690 * Routine to close() a file ...
693 static int smbc_close_ctx(SMBCCTX
*context
, SMBCFILE
*file
)
697 if (!context
|| !context
->_initialized
) {
704 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
714 return context
->closedir(context
, file
);
718 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
720 DEBUG(3, ("cli_close failed on %s. purging server.\n",
722 /* Deallocate slot and remove the server
723 * from the server cache if unused */
724 errno
= smbc_errno(context
, &file
->srv
->cli
);
726 DLIST_REMOVE(context
->_files
, file
);
727 SAFE_FREE(file
->fname
);
729 context
->callbacks
.remove_unused_server_fn(context
, srv
);
737 return context
->closedir(context
, file
);
741 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
742 DEBUG(3, ("cli_close failed on %s. purging server.\n",
744 /* Deallocate slot and remove the server
745 * from the server cache if unused */
746 errno
= smbc_errno(context
, &file
->srv
->cli
);
748 DLIST_REMOVE(context
->_files
, file
);
749 SAFE_FREE(file
->fname
);
751 context
->callbacks
.remove_unused_server_fn(context
, srv
);
756 DLIST_REMOVE(context
->_files
, file
);
757 SAFE_FREE(file
->fname
);
764 * Get info from an SMB server on a file. Use a qpathinfo call first
765 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
767 static BOOL
smbc_getatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
768 uint16
*mode
, size_t *size
,
769 time_t *c_time
, time_t *a_time
, time_t *m_time
,
773 if (!context
|| !context
->_initialized
) {
780 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
782 if (!srv
->no_pathinfo2
&&
783 cli_qpathinfo2(&srv
->cli
, path
, c_time
, a_time
, m_time
, NULL
,
784 size
, mode
, ino
)) return True
;
786 /* if this is NT then don't bother with the getatr */
787 if (srv
->cli
.capabilities
& CAP_NT_SMBS
) return False
;
789 if (cli_getatr(&srv
->cli
, path
, mode
, size
, m_time
)) {
790 a_time
= c_time
= m_time
;
791 srv
->no_pathinfo2
= True
;
800 * Routine to unlink() a file
803 static int smbc_unlink_ctx(SMBCCTX
*context
, const char *fname
)
805 fstring server
, share
, user
, password
, workgroup
;
809 if (!context
|| context
->_initialized
) {
811 errno
= EINVAL
; /* Best I can think of ... */
823 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
825 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
827 pstrcpy(workgroup
, context
->workgroup
);
829 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
833 return -1; /* smbc_server sets errno */
837 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
839 int job = smbc_stat_printjob(srv, path, NULL, NULL);
845 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
853 if (!cli_unlink(&srv
->cli
, path
)) {
855 errno
= smbc_errno(context
, &srv
->cli
);
857 if (errno
== EACCES
) { /* Check if the file is a directory */
862 time_t m_time
= 0, a_time
= 0, c_time
= 0;
865 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
866 &c_time
, &a_time
, &m_time
, &ino
)) {
868 /* Hmmm, bad error ... What? */
870 errno
= smbc_errno(context
, &srv
->cli
);
876 if (IS_DOS_DIR(mode
))
879 errno
= saverr
; /* Restore this */
888 return 0; /* Success ... */
893 * Routine to rename() a file
896 static int smbc_rename_ctx(SMBCCTX
*ocontext
, const char *oname
,
897 SMBCCTX
*ncontext
, const char *nname
)
899 fstring server1
, share1
, server2
, share2
, user1
, user2
, password1
, password2
, workgroup
;
900 pstring path1
, path2
;
903 if (!ocontext
|| !ncontext
||
904 !ocontext
->_initialized
|| !ncontext
->_initialized
) {
906 errno
= EINVAL
; /* Best I can think of ... */
911 if (!oname
|| !nname
) {
918 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
920 smbc_parse_path(ocontext
, oname
, server1
, share1
, path1
, user1
, password1
);
922 if (user1
[0] == (char)0) pstrcpy(user1
, ocontext
->user
);
924 smbc_parse_path(ncontext
, nname
, server2
, share2
, path2
, user2
, password2
);
926 if (user2
[0] == (char)0) pstrcpy(user2
, ncontext
->user
);
928 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
929 strcmp(user1
, user2
)) {
931 /* Can't rename across file systems, or users?? */
938 pstrcpy(workgroup
, ocontext
->workgroup
);
939 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
940 srv
= smbc_server(ocontext
, server1
, share1
, workgroup
, user1
, password1
);
947 if (!cli_rename(&srv
->cli
, path1
, path2
)) {
948 int eno
= smbc_errno(ocontext
, &srv
->cli
);
951 !cli_unlink(&srv
->cli
, path2
) ||
952 !cli_rename(&srv
->cli
, path1
, path2
)) {
960 return 0; /* Success */
965 * A routine to lseek() a file
968 static off_t
smbc_lseek_ctx(SMBCCTX
*context
, SMBCFILE
*file
, off_t offset
, int whence
)
972 if (!context
|| !context
->_initialized
) {
979 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
989 return -1; /* Can't lseek a dir ... */
995 file
->offset
= offset
;
999 file
->offset
+= offset
;
1003 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1004 NULL
, NULL
, NULL
) &&
1005 !cli_getattrE(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1011 file
->offset
= size
+ offset
;
1020 return file
->offset
;
1025 * Generate an inode number from file name for those things that need it
1029 ino_t
smbc_inode(SMBCCTX
*context
, const char *name
)
1032 if (!context
|| !context
->_initialized
) {
1039 if (!*name
) return 2; /* FIXME, why 2 ??? */
1040 return (ino_t
)str_checksum(name
);
1045 * Routine to put basic stat info into a stat structure ... Used by stat and
1050 int smbc_setup_stat(SMBCCTX
*context
, struct stat
*st
, char *fname
, size_t size
, int mode
)
1055 if (IS_DOS_DIR(mode
)) {
1056 st
->st_mode
= SMBC_DIR_MODE
;
1058 st
->st_mode
= SMBC_FILE_MODE
;
1061 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
1062 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
1063 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
1064 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
1067 st
->st_blksize
= 512;
1068 st
->st_blocks
= (size
+511)/512;
1069 st
->st_uid
= getuid();
1070 st
->st_gid
= getgid();
1072 if (IS_DOS_DIR(mode
)) {
1078 if (st
->st_ino
== 0) {
1079 st
->st_ino
= smbc_inode(context
, fname
);
1082 return True
; /* FIXME: Is this needed ? */
1087 * Routine to stat a file given a name
1090 static int smbc_stat_ctx(SMBCCTX
*context
, const char *fname
, struct stat
*st
)
1093 fstring server
, share
, user
, password
, workgroup
;
1095 time_t m_time
= 0, a_time
= 0, c_time
= 0;
1100 if (!context
|| !context
->_initialized
) {
1102 errno
= EINVAL
; /* Best I can think of ... */
1114 DEBUG(4, ("smbc_stat(%s)\n", fname
));
1116 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1118 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1120 pstrcpy(workgroup
, context
->workgroup
);
1122 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1126 return -1; /* errno set by smbc_server */
1130 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1132 mode = aDIR | aRONLY;
1135 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1137 if (strcmp(path, "\\") == 0) {
1139 mode = aDIR | aRONLY;
1145 smbc_stat_printjob(srv, path, &size, &m_time);
1146 c_time = a_time = m_time;
1151 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1152 &c_time
, &a_time
, &m_time
, &ino
)) {
1154 errno
= smbc_errno(context
, &srv
->cli
);
1161 smbc_setup_stat(context
, st
, path
, size
, mode
);
1163 st
->st_atime
= a_time
;
1164 st
->st_ctime
= c_time
;
1165 st
->st_mtime
= m_time
;
1166 st
->st_dev
= srv
->dev
;
1173 * Routine to stat a file given an fd
1176 static int smbc_fstat_ctx(SMBCCTX
*context
, SMBCFILE
*file
, struct stat
*st
)
1178 time_t c_time
, a_time
, m_time
;
1183 if (!context
|| !context
->_initialized
) {
1190 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
1199 return context
->fstatdir(context
, file
, st
);
1203 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
,
1204 &mode
, &size
, &c_time
, &a_time
, &m_time
, NULL
, &ino
) &&
1205 !cli_getattrE(&file
->srv
->cli
, file
->cli_fd
,
1206 &mode
, &size
, &c_time
, &a_time
, &m_time
)) {
1215 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
1217 st
->st_atime
= a_time
;
1218 st
->st_ctime
= c_time
;
1219 st
->st_mtime
= m_time
;
1220 st
->st_dev
= file
->srv
->dev
;
1227 * Routine to open a directory
1231 * smb: which should list all the workgroups available
1233 * smb:workgroup//server
1235 * smb://server/share
1236 * smb://<IP-addr> which should list shares on server
1237 * smb://<IP-addr>/share which should list files on share
1240 static void smbc_remove_dir(SMBCFILE
*dir
)
1242 struct smbc_dir_list
*d
,*f
;
1249 SAFE_FREE(f
->dirent
);
1254 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
1258 static int add_dirent(SMBCFILE
*dir
, const char *name
, const char *comment
, uint32 type
)
1260 struct smbc_dirent
*dirent
;
1264 * Allocate space for the dirent, which must be increased by the
1265 * size of the name and the comment and 1 for the null on the comment.
1266 * The null on the name is already accounted for.
1269 size
= sizeof(struct smbc_dirent
) + (name
?strlen(name
):0) +
1270 (comment
?strlen(comment
):0) + 1;
1272 dirent
= malloc(size
);
1276 dir
->dir_error
= ENOMEM
;
1281 ZERO_STRUCTP(dirent
);
1283 ZERO_STRUCTP(dirent
);
1286 if (dir
->dir_list
== NULL
) {
1288 dir
->dir_list
= malloc(sizeof(struct smbc_dir_list
));
1289 if (!dir
->dir_list
) {
1292 dir
->dir_error
= ENOMEM
;
1296 ZERO_STRUCTP(dir
->dir_list
);
1298 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
1303 dir
->dir_end
->next
= malloc(sizeof(struct smbc_dir_list
));
1305 if (!dir
->dir_end
->next
) {
1308 dir
->dir_error
= ENOMEM
;
1312 ZERO_STRUCTP(dir
->dir_end
->next
);
1314 dir
->dir_end
= dir
->dir_end
->next
;
1318 dir
->dir_end
->next
= NULL
;
1319 dir
->dir_end
->dirent
= dirent
;
1321 dirent
->smbc_type
= type
;
1322 dirent
->namelen
= (name
?strlen(name
):0);
1323 dirent
->commentlen
= (comment
?strlen(comment
):0);
1324 dirent
->dirlen
= size
;
1326 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
1328 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
1329 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
1336 list_fn(const char *name
, uint32 type
, const char *comment
, void *state
)
1338 SMBCFILE
*dir
= (SMBCFILE
*)state
;
1341 /* We need to process the type a little ... */
1343 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
1346 case 0: /* Directory tree */
1347 dirent_type
= SMBC_FILE_SHARE
;
1351 dirent_type
= SMBC_PRINTER_SHARE
;
1355 dirent_type
= SMBC_COMMS_SHARE
;
1359 dirent_type
= SMBC_IPC_SHARE
;
1363 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
1366 ZERO_STRUCTP(dir
->dir_list
);
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
;
1404 if (!context
|| !context
->_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
);
1500 ZERO_STRUCTP(dir
->dir_end
);
1504 /* Now, list the stuff ... */
1506 if (!cli_NetServerEnum(&srv
->cli
, workgroup
, 0x80000000, list_fn
,
1510 SAFE_FREE(dir
->fname
);
1513 errno
= cli_errno(&srv
->cli
);
1519 else { /* Server not an empty string ... Check the rest and see what gives */
1521 if (share
[0] == (char)0) {
1523 if (path
[0] != (char)0) { /* Should not have empty share with path */
1527 SAFE_FREE(dir
->fname
);
1534 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1535 /* However, we check to see if <server> is an IP address first */
1537 if (!is_ipaddress(server
) && /* Not an IP addr so check next */
1538 (resolve_name(server
, &rem_ip
, 0x1d) || /* Found LMB */
1539 resolve_name(server
, &rem_ip
, 0x1b) )) { /* Found DMB */
1542 dir
->dir_type
= SMBC_SERVER
;
1545 * Get the backup list ...
1549 if (!name_status_find("*", 0, 0, rem_ip
, buserver
)) {
1551 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server
));
1552 errno
= EPERM
; /* FIXME, is this correct */
1558 * Get a connection to IPC$ on the server if we do not already have one
1561 srv
= smbc_server(context
, buserver
, "IPC$", workgroup
, user
, password
);
1566 SAFE_FREE(dir
->fname
);
1575 /* Now, list the servers ... */
1577 if (!cli_NetServerEnum(&srv
->cli
, server
, 0x0000FFFE, list_fn
,
1581 SAFE_FREE(dir
->fname
);
1584 errno
= cli_errno(&srv
->cli
);
1592 if (resolve_name(server
, &rem_ip
, 0x20)) {
1594 /* Now, list the shares ... */
1596 dir
->dir_type
= SMBC_FILE_SHARE
;
1598 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1603 SAFE_FREE(dir
->fname
);
1612 /* Now, list the servers ... */
1614 if (cli_RNetShareEnum(&srv
->cli
, list_fn
,
1617 errno
= cli_errno(&srv
->cli
);
1619 SAFE_FREE(dir
->fname
);
1629 errno
= ENODEV
; /* Neither the workgroup nor server exists */
1631 SAFE_FREE(dir
->fname
);
1641 else { /* The server and share are specified ... work from there ... */
1643 /* Well, we connect to the server and list the directory */
1645 dir
->dir_type
= SMBC_FILE_SHARE
;
1647 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1652 SAFE_FREE(dir
->fname
);
1661 /* Now, list the files ... */
1663 pstrcat(path
, "\\*");
1665 if (cli_list(&srv
->cli
, path
, aDIR
| aSYSTEM
| aHIDDEN
, dir_list_fn
,
1669 SAFE_FREE(dir
->fname
);
1672 errno
= smbc_errno(context
, &srv
->cli
);
1680 DLIST_ADD(context
->_files
, dir
);
1686 * Routine to close a directory
1689 static int smbc_closedir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
1692 if (!context
|| !context
->_initialized
) {
1699 if (!dir
|| !DLIST_CONTAINS(context
->_files
, dir
)) {
1706 smbc_remove_dir(dir
); /* Clean it up */
1708 DLIST_REMOVE(context
->_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
->_initialized
) {
1738 if (!dir
|| !DLIST_CONTAINS(context
->_files
, dir
)) {
1745 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1756 dirent
= dir
->dir_next
->dirent
;
1765 /* Hmmm, do I even need to copy it? */
1767 memcpy(context
->_dirent
, dirent
, dirent
->dirlen
); /* Copy the dirent */
1768 dirp
= (struct smbc_dirent
*)context
->_dirent
;
1769 dirp
->comment
= (char *)(&dirp
->name
+ dirent
->namelen
+ 1);
1770 dir
->dir_next
= dir
->dir_next
->next
;
1772 return (struct smbc_dirent
*)context
->_dirent
;
1778 * Routine to get directory entries
1781 static int smbc_getdents_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct smbc_dirent
*dirp
, int count
)
1783 struct smbc_dir_list
*dirlist
;
1784 int rem
= count
, reqd
;
1785 char *ndir
= (char *)dirp
;
1787 /* Check that all is ok first ... */
1789 if (!context
|| !context
->_initialized
) {
1796 if (!dir
|| !DLIST_CONTAINS(context
->_files
, dir
)) {
1803 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1811 * Now, retrieve the number of entries that will fit in what was passed
1812 * We have to figure out if the info is in the list, or we need to
1813 * send a request to the server to get the info.
1816 while ((dirlist
= dir
->dir_next
)) {
1817 struct smbc_dirent
*dirent
;
1819 if (!dirlist
->dirent
) {
1821 errno
= ENOENT
; /* Bad error */
1826 if (rem
< (reqd
= (sizeof(struct smbc_dirent
) + dirlist
->dirent
->namelen
+
1827 dirlist
->dirent
->commentlen
+ 1))) {
1829 if (rem
< count
) { /* We managed to copy something */
1835 else { /* Nothing copied ... */
1837 errno
= EINVAL
; /* Not enough space ... */
1844 dirent
= dirlist
->dirent
;
1846 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
1848 ((struct smbc_dirent
*)ndir
)->comment
=
1849 (char *)(&((struct smbc_dirent
*)ndir
)->name
+ dirent
->namelen
+ 1);
1855 dir
->dir_next
= dirlist
= dirlist
-> next
;
1866 * Routine to create a directory ...
1869 static int smbc_mkdir_ctx(SMBCCTX
*context
, const char *fname
, mode_t mode
)
1872 fstring server
, share
, user
, password
, workgroup
;
1875 if (!context
|| !context
->_initialized
) {
1889 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1891 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1893 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1895 pstrcpy(workgroup
, context
->workgroup
);
1897 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1901 return -1; /* errno set by smbc_server */
1905 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1907 mode = aDIR | aRONLY;
1910 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1912 if (strcmp(path, "\\") == 0) {
1914 mode = aDIR | aRONLY;
1920 smbc_stat_printjob(srv, path, &size, &m_time);
1921 c_time = a_time = m_time;
1926 if (!cli_mkdir(&srv
->cli
, path
)) {
1928 errno
= smbc_errno(context
, &srv
->cli
);
1938 * Our list function simply checks to see if a directory is not empty
1941 static int smbc_rmdir_dirempty
= True
;
1943 static void rmdir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
1946 if (strncmp(finfo
->name
, ".", 1) != 0 && strncmp(finfo
->name
, "..", 2) != 0)
1947 smbc_rmdir_dirempty
= False
;
1952 * Routine to remove a directory
1955 static int smbc_rmdir_ctx(SMBCCTX
*context
, const char *fname
)
1958 fstring server
, share
, user
, password
, workgroup
;
1961 if (!context
|| !context
->_initialized
) {
1975 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1977 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1979 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1981 pstrcpy(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_rmdir(&srv
->cli
, path
)) {
2014 errno
= smbc_errno(context
, &srv
->cli
);
2016 if (errno
== EACCES
) { /* Check if the dir empty or not */
2018 pstring lpath
; /* Local storage to avoid buffer overflows */
2020 smbc_rmdir_dirempty
= True
; /* Make this so ... */
2022 pstrcpy(lpath
, path
);
2023 pstrcat(lpath
, "\\*");
2025 if (cli_list(&srv
->cli
, lpath
, aDIR
| aSYSTEM
| aHIDDEN
, rmdir_list_fn
,
2028 /* Fix errno to ignore latest error ... */
2030 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2031 smbc_errno(context
, &srv
->cli
)));
2036 if (smbc_rmdir_dirempty
)
2052 * Routine to return the current directory position
2055 static off_t
smbc_telldir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2058 if (!context
|| !context
->_initialized
) {
2065 if (!dir
|| !DLIST_CONTAINS(context
->_files
, dir
)) {
2072 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2079 return (off_t
) dir
->dir_next
;
2084 * A routine to run down the list and see if the entry is OK
2087 struct smbc_dir_list
*smbc_check_dir_ent(struct smbc_dir_list
*list
,
2088 struct smbc_dirent
*dirent
)
2091 /* Run down the list looking for what we want */
2095 struct smbc_dir_list
*tmp
= list
;
2099 if (tmp
->dirent
== dirent
)
2108 return NULL
; /* Not found, or an error */
2114 * Routine to seek on a directory
2117 static int smbc_lseekdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, off_t offset
)
2119 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)offset
;
2120 struct smbc_dir_list
*list_ent
= NULL
;
2122 if (!context
|| !context
->_initialized
) {
2129 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2136 /* Now, check what we were passed and see if it is OK ... */
2138 if (dirent
== NULL
) { /* Seek to the begining of the list */
2140 dir
->dir_next
= dir
->dir_list
;
2145 /* Now, run down the list and make sure that the entry is OK */
2146 /* This may need to be changed if we change the format of the list */
2148 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
2150 errno
= EINVAL
; /* Bad entry */
2155 dir
->dir_next
= list_ent
;
2162 * Routine to fstat a dir
2165 static int smbc_fstatdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct stat
*st
)
2168 if (context
|| !context
->_initialized
) {
2175 /* No code yet ... */
2182 * Open a print file to be written to by other calls
2185 static SMBCFILE
*smbc_open_print_job_ctx(SMBCCTX
*context
, const char *fname
)
2187 fstring server
, share
, user
, password
;
2190 if (!context
|| context
->_initialized
) {
2204 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
2206 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2208 /* What if the path is empty, or the file exists? */
2210 return context
->open(context
, fname
, O_WRONLY
, 666);
2215 * Routine to print a file on a remote server ...
2217 * We open the file, which we assume to be on a remote server, and then
2218 * copy it to a print file on the share specified by printq.
2221 static int smbc_print_file_ctx(SMBCCTX
*c_file
, const char *fname
, SMBCCTX
*c_print
, const char *printq
)
2223 SMBCFILE
*fid1
, *fid2
;
2224 int bytes
, saverr
, tot_bytes
= 0;
2227 if (!c_file
|| !c_file
->_initialized
|| !c_print
||
2228 !c_print
->_initialized
) {
2235 if (!fname
&& !printq
) {
2242 /* Try to open the file for reading ... */
2244 if ((fid1
= c_file
->open(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
2246 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
2247 return -1; /* smbc_open sets errno */
2251 /* Now, try to open the printer file for writing */
2253 if ((fid2
= c_print
->open_print_job(c_print
, printq
)) < 0) {
2255 saverr
= errno
; /* Save errno */
2256 c_file
->close(c_file
, fid1
);
2262 while ((bytes
= c_file
->read(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
2266 if ((c_print
->write(c_print
, fid2
, buf
, bytes
)) < 0) {
2269 c_file
->close(c_file
, fid1
);
2270 c_print
->close(c_print
, fid2
);
2279 c_file
->close(c_file
, fid1
); /* We have to close these anyway */
2280 c_print
->close(c_print
, fid2
);
2294 * Routine to list print jobs on a printer share ...
2297 static int smbc_list_print_jobs_ctx(SMBCCTX
*context
, const char *fname
, void (*fn
)(struct print_job_info
*))
2300 fstring server
, share
, user
, password
, workgroup
;
2303 if (!context
|| !context
->_initialized
) {
2317 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
2319 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2321 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
2323 pstrcpy(workgroup
, context
->workgroup
);
2325 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2329 return -1; /* errno set by smbc_server */
2333 if (cli_print_queue(&srv
->cli
, fn
) < 0) {
2335 errno
= smbc_errno(context
, &srv
->cli
);
2345 * Delete a print job from a remote printer share
2348 static int smbc_unlink_print_job_ctx(SMBCCTX
*context
, const char *fname
, int id
)
2351 fstring server
, share
, user
, password
, workgroup
;
2355 if (!context
|| !context
->_initialized
) {
2369 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
2371 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2373 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
2375 pstrcpy(workgroup
, context
->workgroup
);
2377 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2381 return -1; /* errno set by smbc_server */
2385 if ((err
= cli_printjob_del(&srv
->cli
, id
)) != 0) {
2388 errno
= smbc_errno(context
, &srv
->cli
);
2389 else if (err
== ERRnosuchprintjob
)
2400 * Get a new empty handle to fill in with your own info
2402 SMBCCTX
* smbc_new_context(void)
2406 context
= malloc(sizeof(*context
));
2412 ZERO_STRUCTP(context
);
2414 /* ADD REASONABLE DEFAULTS */
2416 context
->timeout
= 20000; /* 20 seconds */
2418 context
->open
= smbc_open_ctx
;
2419 context
->creat
= smbc_creat_ctx
;
2420 context
->read
= smbc_read_ctx
;
2421 context
->write
= smbc_write_ctx
;
2422 context
->close
= smbc_close_ctx
;
2423 context
->unlink
= smbc_unlink_ctx
;
2424 context
->rename
= smbc_rename_ctx
;
2425 context
->lseek
= smbc_lseek_ctx
;
2426 context
->stat
= smbc_stat_ctx
;
2427 context
->fstat
= smbc_fstat_ctx
;
2428 context
->opendir
= smbc_opendir_ctx
;
2429 context
->closedir
= smbc_closedir_ctx
;
2430 context
->readdir
= smbc_readdir_ctx
;
2431 context
->getdents
= smbc_getdents_ctx
;
2432 context
->mkdir
= smbc_mkdir_ctx
;
2433 context
->rmdir
= smbc_rmdir_ctx
;
2434 context
->telldir
= smbc_telldir_ctx
;
2435 context
->lseekdir
= smbc_lseekdir_ctx
;
2436 context
->fstatdir
= smbc_fstatdir_ctx
;
2437 context
->open_print_job
= smbc_open_print_job_ctx
;
2438 context
->print_file
= smbc_print_file_ctx
;
2439 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
2440 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
2442 context
->callbacks
.check_server_fn
= smbc_check_server
;
2443 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
2445 smbc_default_cache_functions(context
);
2453 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2454 * and thus you'll be leaking memory if not handled properly.
2457 int smbc_free_context(SMBCCTX
* context
, int shutdown_ctx
)
2466 DEBUG(1,("Performing aggressive shutdown.\n"));
2468 f
= context
->_files
;
2470 context
->close(context
, f
);
2473 context
->_files
= NULL
;
2475 /* First try to remove the servers the nice way. */
2476 if (context
->callbacks
.purge_cached_fn(context
)) {
2478 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2479 s
= context
->_servers
;
2481 cli_shutdown(&s
->cli
);
2482 context
->callbacks
.remove_cached_srv_fn(context
, s
);
2486 context
->_servers
= NULL
;
2490 /* This is the polite way */
2491 if (context
->callbacks
.purge_cached_fn(context
)) {
2492 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2496 if (context
->_servers
) {
2497 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2501 if (context
->_files
) {
2502 DEBUG(1, ("Active files in context, free_context failed.\n"));
2508 /* Things we have to clean up */
2509 SAFE_FREE(context
->workgroup
);
2510 SAFE_FREE(context
->netbios_name
);
2511 SAFE_FREE(context
->user
);
2513 DEBUG(3, ("Context %p succesfully freed\n", context
));
2520 * Initialise the library etc
2522 * We accept a struct containing handle information.
2523 * valid values for info->debug from 0 to 100,
2524 * and insist that info->fn must be non-null.
2526 SMBCCTX
* smbc_init_context(SMBCCTX
* context
)
2530 char *user
= NULL
, *home
= NULL
;
2537 /* Do not initialise the same client twice */
2538 if (context
->_initialized
) {
2542 if (!context
->callbacks
.auth_fn
|| context
->debug
< 0 || context
->debug
> 100) {
2549 if (!smbc_initialized
) {
2550 /* Do some library wide intialisations the first time we get called */
2552 /* Do we still need this ? */
2555 setup_logging( "libsmbclient", False
);
2557 /* Here we would open the smb.conf file if needed ... */
2559 home
= getenv("HOME");
2561 slprintf(conf
, sizeof(conf
), "%s/.smb/smb.conf", home
);
2563 load_interfaces(); /* Load the list of interfaces ... */
2565 in_client
= True
; /* FIXME, make a param */
2567 if (!lp_load(conf
, True
, False
, False
)) {
2570 * Hmmm, what the hell do we do here ... we could not parse the
2571 * config file ... We must return an error ... and keep info around
2572 * about why we failed
2575 errno
= ENOENT
; /* FIXME: Figure out the correct error response */
2579 reopen_logs(); /* Get logging working ... */
2582 * Block SIGPIPE (from lib/util_sock.c: write())
2583 * It is not needed and should not stop execution
2585 BlockSignals(True
, SIGPIPE
);
2587 /* Done with one-time initialisation */
2588 smbc_initialized
= 1;
2592 if (!context
->user
) {
2594 * FIXME: Is this the best way to get the user info?
2596 user
= getenv("USER");
2597 /* walk around as "guest" if no username can be found */
2598 if (!user
) context
->user
= strdup("guest");
2599 else context
->user
= strdup(user
);
2602 if (!context
->netbios_name
) {
2604 * We try to get our netbios name from the config. If that fails we fall
2605 * back on constructing our netbios name from our hostname etc
2607 if (global_myname
) {
2608 context
->netbios_name
= strdup(global_myname
);
2612 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2615 context
->netbios_name
= malloc(17);
2616 if (!context
->netbios_name
) {
2620 slprintf(context
->netbios_name
, 16, "smbc%s%d", context
->user
, pid
);
2623 DEBUG(0,("Using netbios name %s.\n", context
->netbios_name
));
2626 if (!context
->workgroup
) {
2627 if (lp_workgroup()) {
2628 context
->workgroup
= strdup(lp_workgroup());
2631 /* TODO: Think about a decent default workgroup */
2632 context
->workgroup
= strdup("samba");
2635 DEBUG(0,("Using workgroup %s.\n", context
->workgroup
));
2639 * I think we can do this more than once for the same name without
2640 * being shot but who am I? -- Tom
2641 * Actually, we probably don't want to register a name,
2642 * but one day the user might want to be able to do so. RJS
2645 name_register_wins(context
->netbios_name
, 0);
2648 /* shortest timeout is 1 second */
2649 if (context
->timeout
> 0 && context
->timeout
< 1000)
2650 context
->timeout
= 1000;
2653 * FIXME: Should we check the function pointers here?
2656 context
->_initialized
= 1;