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 "libsmbclient.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
;
49 extern pstring global_myname
;
52 * Is the logging working / configfile read ?
54 static int smbc_initialized
= 0;
57 * Function to parse a path and turn it into components
59 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
61 * smb:// means show all the workgroups
62 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
63 * else, if name<20> exists, list all shares for server ...
66 static const char *smbc_prefix
= "smb:";
69 smbc_parse_path(SMBCCTX
*context
, const char *fname
, char *server
, char *share
, char *path
,
70 char *user
, char *password
) /* FIXME, lengths of strings */
78 server
[0] = share
[0] = path
[0] = user
[0] = password
[0] = (char)0;
81 /* clean_fname(s); causing problems ... */
83 /* see if it has the right prefix */
84 len
= strlen(smbc_prefix
);
85 if (strncmp(s
,smbc_prefix
,len
) ||
86 (s
[len
] != '/' && s
[len
] != 0)) return -1; /* What about no smb: ? */
90 /* Watch the test below, we are testing to see if we should exit */
92 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
98 p
+= 2; /* Skip the // or \\ */
105 strncpy(server
, context
->workgroup
,
106 (strlen(context
->workgroup
) < 16)?strlen(context
->workgroup
):16);
112 * ok, its for us. Now parse out the server, share etc.
114 * However, we want to parse out [[domain;]user[:password]@] if it
118 /* check that '@' occurs before '/', if '/' exists at all */
119 q
= strchr_m(p
, '@');
120 r
= strchr_m(p
, '/');
121 if (q
&& (!r
|| q
< r
)) {
122 pstring username
, passwd
, domain
;
125 next_token(&p
, userinfo
, "@", sizeof(fstring
));
127 username
[0] = passwd
[0] = domain
[0] = 0;
129 if (strchr_m(u
, ';')) {
131 next_token(&u
, domain
, ";", sizeof(fstring
));
135 if (strchr_m(u
, ':')) {
137 next_token(&u
, username
, ":", sizeof(fstring
));
144 pstrcpy(username
, u
);
149 strncpy(user
, username
, sizeof(fstring
)); /* FIXME, size and domain */
152 strncpy(password
, passwd
, sizeof(fstring
)); /* FIXME, size */
156 if (!next_token(&p
, server
, "/", sizeof(fstring
))) {
162 if (*p
== (char)0) return 0; /* That's it ... */
164 if (!next_token(&p
, share
, "/", sizeof(fstring
))) {
172 all_string_sub(path
, "/", "\\", 0);
178 * Convert an SMB error into a UNIX error ...
181 static int smbc_errno(SMBCCTX
*context
, struct cli_state
*c
)
183 int ret
= cli_errno(c
);
185 if (cli_is_dos_error(c
)) {
189 cli_dos_error(c
, &eclass
, &ecode
);
191 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
192 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
196 status
= cli_nt_error(c
);
198 DEBUG(3,("smbc errno %s -> %d\n",
199 nt_errstr(status
), ret
));
207 * returns 0 if the server is in shape. Returns 1 on error
209 * Also useable outside libsmbclient to enable external cache
210 * to do some checks too.
212 int smbc_check_server(SMBCCTX
* context
, SMBCSRV
* server
)
214 if ( send_keepalive(server
->cli
.fd
) == False
)
217 /* connection is ok */
222 * Remove a server from the list server_table if it's unused.
223 * On success, 0 is returned. 1 is returned if the server could not be removed.
225 * Also useable outside libsmbclient
227 int smbc_remove_unused_server(SMBCCTX
* context
, SMBCSRV
* srv
)
231 /* are we being fooled ? */
232 if (!context
|| !context
->_initialized
|| !srv
) return 1;
235 /* Check all open files/directories for a relation with this server */
236 for (file
= context
->_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
->_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
->_initialized
) {
480 errno
= EINVAL
; /* Best I can think of ... */
492 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
494 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
496 pstrcpy(workgroup
, context
->workgroup
);
498 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
502 if (errno
== EPERM
) errno
= EACCES
;
503 return NULL
; /* smbc_server sets errno */
507 /* Hmmm, the test for a directory is suspect here ... FIXME */
509 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
516 file
= malloc(sizeof(SMBCFILE
));
527 if ((fd
= cli_open(&srv
->cli
, path
, flags
, DENY_NONE
)) < 0) {
529 /* Handle the error ... */
532 errno
= smbc_errno(context
, &srv
->cli
);
537 /* Fill in file struct */
540 file
->fname
= strdup(fname
);
545 DLIST_ADD(context
->_files
, file
);
550 /* Check if opendir needed ... */
555 eno
= smbc_errno(context
, &srv
->cli
);
556 file
= context
->opendir(context
, fname
);
557 if (!file
) errno
= eno
;
562 errno
= EINVAL
; /* FIXME, correct errno ? */
568 * Routine to create a file
571 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
573 static SMBCFILE
*smbc_creat_ctx(SMBCCTX
*context
, const char *path
, mode_t mode
)
576 if (!context
|| !context
->_initialized
) {
583 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
587 * Routine to read() a file ...
590 static ssize_t
smbc_read_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
594 if (!context
|| !context
->_initialized
) {
601 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
603 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
610 /* Check that the buffer exists ... */
619 ret
= cli_read(&file
->srv
->cli
, file
->cli_fd
, buf
, file
->offset
, count
);
623 errno
= smbc_errno(context
, &file
->srv
->cli
);
630 DEBUG(4, (" --> %d\n", ret
));
632 return ret
; /* Success, ret bytes of data ... */
637 * Routine to write() a file ...
640 static ssize_t
smbc_write_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
644 if (!context
|| context
->_initialized
) {
651 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
658 /* Check that the buffer exists ... */
667 ret
= cli_write(&file
->srv
->cli
, file
->cli_fd
, 0, buf
, file
->offset
, count
);
671 errno
= smbc_errno(context
, &file
->srv
->cli
);
678 return ret
; /* Success, 0 bytes of data ... */
682 * Routine to close() a file ...
685 static int smbc_close_ctx(SMBCCTX
*context
, SMBCFILE
*file
)
689 if (!context
|| !context
->_initialized
) {
696 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
706 return context
->closedir(context
, file
);
710 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
712 DEBUG(3, ("cli_close failed on %s. purging server.\n",
714 /* Deallocate slot and remove the server
715 * from the server cache if unused */
716 errno
= smbc_errno(context
, &file
->srv
->cli
);
718 DLIST_REMOVE(context
->_files
, file
);
719 SAFE_FREE(file
->fname
);
721 context
->callbacks
.remove_unused_server_fn(context
, srv
);
729 return context
->closedir(context
, file
);
733 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
734 DEBUG(3, ("cli_close failed on %s. purging server.\n",
736 /* Deallocate slot and remove the server
737 * from the server cache if unused */
738 errno
= smbc_errno(context
, &file
->srv
->cli
);
740 DLIST_REMOVE(context
->_files
, file
);
741 SAFE_FREE(file
->fname
);
743 context
->callbacks
.remove_unused_server_fn(context
, srv
);
748 DLIST_REMOVE(context
->_files
, file
);
749 SAFE_FREE(file
->fname
);
756 * Get info from an SMB server on a file. Use a qpathinfo call first
757 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
759 static BOOL
smbc_getatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
760 uint16
*mode
, size_t *size
,
761 time_t *c_time
, time_t *a_time
, time_t *m_time
,
765 if (!context
|| !context
->_initialized
) {
772 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
774 if (!srv
->no_pathinfo2
&&
775 cli_qpathinfo2(&srv
->cli
, path
, c_time
, a_time
, m_time
, NULL
,
776 size
, mode
, ino
)) return True
;
778 /* if this is NT then don't bother with the getatr */
779 if (srv
->cli
.capabilities
& CAP_NT_SMBS
) return False
;
781 if (cli_getatr(&srv
->cli
, path
, mode
, size
, m_time
)) {
782 a_time
= c_time
= m_time
;
783 srv
->no_pathinfo2
= True
;
792 * Routine to unlink() a file
795 static int smbc_unlink_ctx(SMBCCTX
*context
, const char *fname
)
797 fstring server
, share
, user
, password
, workgroup
;
801 if (!context
|| context
->_initialized
) {
803 errno
= EINVAL
; /* Best I can think of ... */
815 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
817 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
819 pstrcpy(workgroup
, context
->workgroup
);
821 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
825 return -1; /* smbc_server sets errno */
829 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
831 int job = smbc_stat_printjob(srv, path, NULL, NULL);
837 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
845 if (!cli_unlink(&srv
->cli
, path
)) {
847 errno
= smbc_errno(context
, &srv
->cli
);
849 if (errno
== EACCES
) { /* Check if the file is a directory */
854 time_t m_time
= 0, a_time
= 0, c_time
= 0;
857 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
858 &c_time
, &a_time
, &m_time
, &ino
)) {
860 /* Hmmm, bad error ... What? */
862 errno
= smbc_errno(context
, &srv
->cli
);
868 if (IS_DOS_DIR(mode
))
871 errno
= saverr
; /* Restore this */
880 return 0; /* Success ... */
885 * Routine to rename() a file
888 static int smbc_rename_ctx(SMBCCTX
*ocontext
, const char *oname
,
889 SMBCCTX
*ncontext
, const char *nname
)
891 fstring server1
, share1
, server2
, share2
, user1
, user2
, password1
, password2
, workgroup
;
892 pstring path1
, path2
;
895 if (!ocontext
|| !ncontext
||
896 !ocontext
->_initialized
|| !ncontext
->_initialized
) {
898 errno
= EINVAL
; /* Best I can think of ... */
903 if (!oname
|| !nname
) {
910 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
912 smbc_parse_path(ocontext
, oname
, server1
, share1
, path1
, user1
, password1
);
914 if (user1
[0] == (char)0) pstrcpy(user1
, ocontext
->user
);
916 smbc_parse_path(ncontext
, nname
, server2
, share2
, path2
, user2
, password2
);
918 if (user2
[0] == (char)0) pstrcpy(user2
, ncontext
->user
);
920 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
921 strcmp(user1
, user2
)) {
923 /* Can't rename across file systems, or users?? */
930 pstrcpy(workgroup
, ocontext
->workgroup
);
931 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
932 srv
= smbc_server(ocontext
, server1
, share1
, workgroup
, user1
, password1
);
939 if (!cli_rename(&srv
->cli
, path1
, path2
)) {
940 int eno
= smbc_errno(ocontext
, &srv
->cli
);
943 !cli_unlink(&srv
->cli
, path2
) ||
944 !cli_rename(&srv
->cli
, path1
, path2
)) {
952 return 0; /* Success */
957 * A routine to lseek() a file
960 static off_t
smbc_lseek_ctx(SMBCCTX
*context
, SMBCFILE
*file
, off_t offset
, int whence
)
964 if (!context
|| !context
->_initialized
) {
971 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
981 return -1; /* Can't lseek a dir ... */
987 file
->offset
= offset
;
991 file
->offset
+= offset
;
995 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
997 !cli_getattrE(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1003 file
->offset
= size
+ offset
;
1012 return file
->offset
;
1017 * Generate an inode number from file name for those things that need it
1021 ino_t
smbc_inode(SMBCCTX
*context
, const char *name
)
1024 if (!context
|| !context
->_initialized
) {
1031 if (!*name
) return 2; /* FIXME, why 2 ??? */
1032 return (ino_t
)str_checksum(name
);
1037 * Routine to put basic stat info into a stat structure ... Used by stat and
1042 int smbc_setup_stat(SMBCCTX
*context
, struct stat
*st
, char *fname
, size_t size
, int mode
)
1047 if (IS_DOS_DIR(mode
)) {
1048 st
->st_mode
= SMBC_DIR_MODE
;
1050 st
->st_mode
= SMBC_FILE_MODE
;
1053 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
1054 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
1055 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
1056 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
1059 st
->st_blksize
= 512;
1060 st
->st_blocks
= (size
+511)/512;
1061 st
->st_uid
= getuid();
1062 st
->st_gid
= getgid();
1064 if (IS_DOS_DIR(mode
)) {
1070 if (st
->st_ino
== 0) {
1071 st
->st_ino
= smbc_inode(context
, fname
);
1074 return True
; /* FIXME: Is this needed ? */
1079 * Routine to stat a file given a name
1082 static int smbc_stat_ctx(SMBCCTX
*context
, const char *fname
, struct stat
*st
)
1085 fstring server
, share
, user
, password
, workgroup
;
1087 time_t m_time
= 0, a_time
= 0, c_time
= 0;
1092 if (!context
|| !context
->_initialized
) {
1094 errno
= EINVAL
; /* Best I can think of ... */
1106 DEBUG(4, ("smbc_stat(%s)\n", fname
));
1108 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1110 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1112 pstrcpy(workgroup
, context
->workgroup
);
1114 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1118 return -1; /* errno set by smbc_server */
1122 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1124 mode = aDIR | aRONLY;
1127 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1129 if (strcmp(path, "\\") == 0) {
1131 mode = aDIR | aRONLY;
1137 smbc_stat_printjob(srv, path, &size, &m_time);
1138 c_time = a_time = m_time;
1143 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1144 &c_time
, &a_time
, &m_time
, &ino
)) {
1146 errno
= smbc_errno(context
, &srv
->cli
);
1153 smbc_setup_stat(context
, st
, path
, size
, mode
);
1155 st
->st_atime
= a_time
;
1156 st
->st_ctime
= c_time
;
1157 st
->st_mtime
= m_time
;
1158 st
->st_dev
= srv
->dev
;
1165 * Routine to stat a file given an fd
1168 static int smbc_fstat_ctx(SMBCCTX
*context
, SMBCFILE
*file
, struct stat
*st
)
1170 time_t c_time
, a_time
, m_time
;
1175 if (!context
|| !context
->_initialized
) {
1182 if (!file
|| !DLIST_CONTAINS(context
->_files
, file
)) {
1191 return context
->fstatdir(context
, file
, st
);
1195 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
,
1196 &mode
, &size
, &c_time
, &a_time
, &m_time
, NULL
, &ino
) &&
1197 !cli_getattrE(&file
->srv
->cli
, file
->cli_fd
,
1198 &mode
, &size
, &c_time
, &a_time
, &m_time
)) {
1207 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
1209 st
->st_atime
= a_time
;
1210 st
->st_ctime
= c_time
;
1211 st
->st_mtime
= m_time
;
1212 st
->st_dev
= file
->srv
->dev
;
1219 * Routine to open a directory
1223 * smb: which should list all the workgroups available
1225 * smb:workgroup//server
1227 * smb://server/share
1228 * smb://<IP-addr> which should list shares on server
1229 * smb://<IP-addr>/share which should list files on share
1232 static void smbc_remove_dir(SMBCFILE
*dir
)
1234 struct smbc_dir_list
*d
,*f
;
1241 SAFE_FREE(f
->dirent
);
1246 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
1250 static int add_dirent(SMBCFILE
*dir
, const char *name
, const char *comment
, uint32 type
)
1252 struct smbc_dirent
*dirent
;
1256 * Allocate space for the dirent, which must be increased by the
1257 * size of the name and the comment and 1 for the null on the comment.
1258 * The null on the name is already accounted for.
1261 size
= sizeof(struct smbc_dirent
) + (name
?strlen(name
):0) +
1262 (comment
?strlen(comment
):0) + 1;
1264 dirent
= malloc(size
);
1268 dir
->dir_error
= ENOMEM
;
1273 ZERO_STRUCTP(dirent
);
1275 ZERO_STRUCTP(dirent
);
1278 if (dir
->dir_list
== NULL
) {
1280 dir
->dir_list
= malloc(sizeof(struct smbc_dir_list
));
1281 if (!dir
->dir_list
) {
1284 dir
->dir_error
= ENOMEM
;
1288 ZERO_STRUCTP(dir
->dir_list
);
1290 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
1295 dir
->dir_end
->next
= malloc(sizeof(struct smbc_dir_list
));
1297 if (!dir
->dir_end
->next
) {
1300 dir
->dir_error
= ENOMEM
;
1304 ZERO_STRUCTP(dir
->dir_end
->next
);
1306 dir
->dir_end
= dir
->dir_end
->next
;
1310 dir
->dir_end
->next
= NULL
;
1311 dir
->dir_end
->dirent
= dirent
;
1313 dirent
->smbc_type
= type
;
1314 dirent
->namelen
= (name
?strlen(name
):0);
1315 dirent
->commentlen
= (comment
?strlen(comment
):0);
1316 dirent
->dirlen
= size
;
1318 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
1320 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
1321 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
1328 list_fn(const char *name
, uint32 type
, const char *comment
, void *state
)
1330 SMBCFILE
*dir
= (SMBCFILE
*)state
;
1333 /* We need to process the type a little ... */
1335 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
1338 case 0: /* Directory tree */
1339 dirent_type
= SMBC_FILE_SHARE
;
1343 dirent_type
= SMBC_PRINTER_SHARE
;
1347 dirent_type
= SMBC_COMMS_SHARE
;
1351 dirent_type
= SMBC_IPC_SHARE
;
1355 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
1358 ZERO_STRUCTP(dir
->dir_list
);
1361 else dirent_type
= dir
->dir_type
;
1363 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
1365 /* An error occurred, what do we do? */
1366 /* FIXME: Add some code here */
1373 dir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
1376 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
1377 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
1379 /* Handle an error ... */
1381 /* FIXME: Add some code ... */
1387 static SMBCFILE
*smbc_opendir_ctx(SMBCCTX
*context
, const char *fname
)
1389 fstring server
, share
, user
, password
, workgroup
;
1391 SMBCSRV
*srv
= NULL
;
1392 SMBCFILE
*dir
= NULL
;
1393 struct in_addr rem_ip
;
1396 if (!context
|| !context
->_initialized
) {
1410 if (smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
)) {
1417 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1419 pstrcpy(workgroup
, context
->workgroup
);
1421 dir
= malloc(sizeof(*dir
));
1433 dir
->fname
= strdup(fname
);
1437 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
1439 if (server
[0] == (char)0) {
1441 if (share
[0] != (char)0 || path
[0] != (char)0) {
1445 SAFE_FREE(dir
->fname
);
1452 /* We have server and share and path empty ... so list the workgroups */
1453 /* first try to get the LMB for our workgroup, and if that fails, */
1456 if (!(resolve_name(context
->workgroup
, &rem_ip
, 0x1d) ||
1457 resolve_name(context
->workgroup
, &rem_ip
, 0x1b))) {
1459 errno
= EINVAL
; /* Something wrong with smb.conf? */
1464 dir
->dir_type
= SMBC_WORKGROUP
;
1466 /* find the name of the server ... */
1468 if (!name_status_find("*", 0, 0, rem_ip
, server
)) {
1470 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server
));
1477 * Get a connection to IPC$ on the server if we do not already have one
1480 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1485 SAFE_FREE(dir
->fname
);
1492 ZERO_STRUCTP(dir
->dir_end
);
1496 /* Now, list the stuff ... */
1498 if (!cli_NetServerEnum(&srv
->cli
, workgroup
, 0x80000000, list_fn
,
1502 SAFE_FREE(dir
->fname
);
1505 errno
= cli_errno(&srv
->cli
);
1511 else { /* Server not an empty string ... Check the rest and see what gives */
1513 if (share
[0] == (char)0) {
1515 if (path
[0] != (char)0) { /* Should not have empty share with path */
1519 SAFE_FREE(dir
->fname
);
1526 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1527 /* However, we check to see if <server> is an IP address first */
1529 if (!is_ipaddress(server
) && /* Not an IP addr so check next */
1530 (resolve_name(server
, &rem_ip
, 0x1d) || /* Found LMB */
1531 resolve_name(server
, &rem_ip
, 0x1b) )) { /* Found DMB */
1534 dir
->dir_type
= SMBC_SERVER
;
1537 * Get the backup list ...
1541 if (!name_status_find("*", 0, 0, rem_ip
, buserver
)) {
1543 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server
));
1544 errno
= EPERM
; /* FIXME, is this correct */
1550 * Get a connection to IPC$ on the server if we do not already have one
1553 srv
= smbc_server(context
, buserver
, "IPC$", workgroup
, user
, password
);
1558 SAFE_FREE(dir
->fname
);
1567 /* Now, list the servers ... */
1569 if (!cli_NetServerEnum(&srv
->cli
, server
, 0x0000FFFE, list_fn
,
1573 SAFE_FREE(dir
->fname
);
1576 errno
= cli_errno(&srv
->cli
);
1584 if (resolve_name(server
, &rem_ip
, 0x20)) {
1586 /* Now, list the shares ... */
1588 dir
->dir_type
= SMBC_FILE_SHARE
;
1590 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1595 SAFE_FREE(dir
->fname
);
1604 /* Now, list the servers ... */
1606 if (cli_RNetShareEnum(&srv
->cli
, list_fn
,
1609 errno
= cli_errno(&srv
->cli
);
1611 SAFE_FREE(dir
->fname
);
1621 errno
= ENODEV
; /* Neither the workgroup nor server exists */
1623 SAFE_FREE(dir
->fname
);
1633 else { /* The server and share are specified ... work from there ... */
1635 /* Well, we connect to the server and list the directory */
1637 dir
->dir_type
= SMBC_FILE_SHARE
;
1639 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1644 SAFE_FREE(dir
->fname
);
1653 /* Now, list the files ... */
1655 pstrcat(path
, "\\*");
1657 if (cli_list(&srv
->cli
, path
, aDIR
| aSYSTEM
| aHIDDEN
, dir_list_fn
,
1661 SAFE_FREE(dir
->fname
);
1664 errno
= smbc_errno(context
, &srv
->cli
);
1672 DLIST_ADD(context
->_files
, dir
);
1678 * Routine to close a directory
1681 static int smbc_closedir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
1684 if (!context
|| !context
->_initialized
) {
1691 if (!dir
|| !DLIST_CONTAINS(context
->_files
, dir
)) {
1698 smbc_remove_dir(dir
); /* Clean it up */
1700 DLIST_REMOVE(context
->_files
, dir
);
1704 SAFE_FREE(dir
->fname
);
1705 SAFE_FREE(dir
); /* Free the space too */
1714 * Routine to get a directory entry
1717 struct smbc_dirent
*smbc_readdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
1719 struct smbc_dirent
*dirp
, *dirent
;
1721 /* Check that all is ok first ... */
1723 if (!context
|| !context
->_initialized
) {
1730 if (!dir
|| !DLIST_CONTAINS(context
->_files
, dir
)) {
1737 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1748 dirent
= dir
->dir_next
->dirent
;
1757 /* Hmmm, do I even need to copy it? */
1759 memcpy(context
->_dirent
, dirent
, dirent
->dirlen
); /* Copy the dirent */
1760 dirp
= (struct smbc_dirent
*)context
->_dirent
;
1761 dirp
->comment
= (char *)(&dirp
->name
+ dirent
->namelen
+ 1);
1762 dir
->dir_next
= dir
->dir_next
->next
;
1764 return (struct smbc_dirent
*)context
->_dirent
;
1770 * Routine to get directory entries
1773 static int smbc_getdents_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct smbc_dirent
*dirp
, int count
)
1775 struct smbc_dir_list
*dirlist
;
1776 int rem
= count
, reqd
;
1777 char *ndir
= (char *)dirp
;
1779 /* Check that all is ok first ... */
1781 if (!context
|| !context
->_initialized
) {
1788 if (!dir
|| !DLIST_CONTAINS(context
->_files
, dir
)) {
1795 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1803 * Now, retrieve the number of entries that will fit in what was passed
1804 * We have to figure out if the info is in the list, or we need to
1805 * send a request to the server to get the info.
1808 while ((dirlist
= dir
->dir_next
)) {
1809 struct smbc_dirent
*dirent
;
1811 if (!dirlist
->dirent
) {
1813 errno
= ENOENT
; /* Bad error */
1818 if (rem
< (reqd
= (sizeof(struct smbc_dirent
) + dirlist
->dirent
->namelen
+
1819 dirlist
->dirent
->commentlen
+ 1))) {
1821 if (rem
< count
) { /* We managed to copy something */
1827 else { /* Nothing copied ... */
1829 errno
= EINVAL
; /* Not enough space ... */
1836 dirent
= dirlist
->dirent
;
1838 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
1840 ((struct smbc_dirent
*)ndir
)->comment
=
1841 (char *)(&((struct smbc_dirent
*)ndir
)->name
+ dirent
->namelen
+ 1);
1847 dir
->dir_next
= dirlist
= dirlist
-> next
;
1858 * Routine to create a directory ...
1861 static int smbc_mkdir_ctx(SMBCCTX
*context
, const char *fname
, mode_t mode
)
1864 fstring server
, share
, user
, password
, workgroup
;
1867 if (!context
|| !context
->_initialized
) {
1881 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1883 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1885 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1887 pstrcpy(workgroup
, context
->workgroup
);
1889 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1893 return -1; /* errno set by smbc_server */
1897 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1899 mode = aDIR | aRONLY;
1902 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1904 if (strcmp(path, "\\") == 0) {
1906 mode = aDIR | aRONLY;
1912 smbc_stat_printjob(srv, path, &size, &m_time);
1913 c_time = a_time = m_time;
1918 if (!cli_mkdir(&srv
->cli
, path
)) {
1920 errno
= smbc_errno(context
, &srv
->cli
);
1930 * Our list function simply checks to see if a directory is not empty
1933 static int smbc_rmdir_dirempty
= True
;
1935 static void rmdir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
1938 if (strncmp(finfo
->name
, ".", 1) != 0 && strncmp(finfo
->name
, "..", 2) != 0)
1939 smbc_rmdir_dirempty
= False
;
1944 * Routine to remove a directory
1947 static int smbc_rmdir_ctx(SMBCCTX
*context
, const char *fname
)
1950 fstring server
, share
, user
, password
, workgroup
;
1953 if (!context
|| !context
->_initialized
) {
1967 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
1969 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1971 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
1973 pstrcpy(workgroup
, context
->workgroup
);
1975 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1979 return -1; /* errno set by smbc_server */
1983 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1985 mode = aDIR | aRONLY;
1988 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1990 if (strcmp(path, "\\") == 0) {
1992 mode = aDIR | aRONLY;
1998 smbc_stat_printjob(srv, path, &size, &m_time);
1999 c_time = a_time = m_time;
2004 if (!cli_rmdir(&srv
->cli
, path
)) {
2006 errno
= smbc_errno(context
, &srv
->cli
);
2008 if (errno
== EACCES
) { /* Check if the dir empty or not */
2010 pstring lpath
; /* Local storage to avoid buffer overflows */
2012 smbc_rmdir_dirempty
= True
; /* Make this so ... */
2014 pstrcpy(lpath
, path
);
2015 pstrcat(lpath
, "\\*");
2017 if (cli_list(&srv
->cli
, lpath
, aDIR
| aSYSTEM
| aHIDDEN
, rmdir_list_fn
,
2020 /* Fix errno to ignore latest error ... */
2022 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2023 smbc_errno(context
, &srv
->cli
)));
2028 if (smbc_rmdir_dirempty
)
2044 * Routine to return the current directory position
2047 static off_t
smbc_telldir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2050 if (!context
|| !context
->_initialized
) {
2057 if (!dir
|| !DLIST_CONTAINS(context
->_files
, dir
)) {
2064 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2071 return (off_t
) dir
->dir_next
;
2076 * A routine to run down the list and see if the entry is OK
2079 struct smbc_dir_list
*smbc_check_dir_ent(struct smbc_dir_list
*list
,
2080 struct smbc_dirent
*dirent
)
2083 /* Run down the list looking for what we want */
2087 struct smbc_dir_list
*tmp
= list
;
2091 if (tmp
->dirent
== dirent
)
2100 return NULL
; /* Not found, or an error */
2106 * Routine to seek on a directory
2109 static int smbc_lseekdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, off_t offset
)
2111 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)offset
;
2112 struct smbc_dir_list
*list_ent
= NULL
;
2114 if (!context
|| !context
->_initialized
) {
2121 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2128 /* Now, check what we were passed and see if it is OK ... */
2130 if (dirent
== NULL
) { /* Seek to the begining of the list */
2132 dir
->dir_next
= dir
->dir_list
;
2137 /* Now, run down the list and make sure that the entry is OK */
2138 /* This may need to be changed if we change the format of the list */
2140 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
2142 errno
= EINVAL
; /* Bad entry */
2147 dir
->dir_next
= list_ent
;
2154 * Routine to fstat a dir
2157 static int smbc_fstatdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct stat
*st
)
2160 if (context
|| !context
->_initialized
) {
2167 /* No code yet ... */
2174 * Open a print file to be written to by other calls
2177 static SMBCFILE
*smbc_open_print_job_ctx(SMBCCTX
*context
, const char *fname
)
2179 fstring server
, share
, user
, password
;
2182 if (!context
|| context
->_initialized
) {
2196 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
2198 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2200 /* What if the path is empty, or the file exists? */
2202 return context
->open(context
, fname
, O_WRONLY
, 666);
2207 * Routine to print a file on a remote server ...
2209 * We open the file, which we assume to be on a remote server, and then
2210 * copy it to a print file on the share specified by printq.
2213 static int smbc_print_file_ctx(SMBCCTX
*c_file
, const char *fname
, SMBCCTX
*c_print
, const char *printq
)
2215 SMBCFILE
*fid1
, *fid2
;
2216 int bytes
, saverr
, tot_bytes
= 0;
2219 if (!c_file
|| !c_file
->_initialized
|| !c_print
||
2220 !c_print
->_initialized
) {
2227 if (!fname
&& !printq
) {
2234 /* Try to open the file for reading ... */
2236 if ((fid1
= c_file
->open(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
2238 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
2239 return -1; /* smbc_open sets errno */
2243 /* Now, try to open the printer file for writing */
2245 if ((fid2
= c_print
->open_print_job(c_print
, printq
)) < 0) {
2247 saverr
= errno
; /* Save errno */
2248 c_file
->close(c_file
, fid1
);
2254 while ((bytes
= c_file
->read(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
2258 if ((c_print
->write(c_print
, fid2
, buf
, bytes
)) < 0) {
2261 c_file
->close(c_file
, fid1
);
2262 c_print
->close(c_print
, fid2
);
2271 c_file
->close(c_file
, fid1
); /* We have to close these anyway */
2272 c_print
->close(c_print
, fid2
);
2286 * Routine to list print jobs on a printer share ...
2289 static int smbc_list_print_jobs_ctx(SMBCCTX
*context
, const char *fname
, void (*fn
)(struct print_job_info
*))
2292 fstring server
, share
, user
, password
, workgroup
;
2295 if (!context
|| !context
->_initialized
) {
2309 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
2311 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2313 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
2315 pstrcpy(workgroup
, context
->workgroup
);
2317 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2321 return -1; /* errno set by smbc_server */
2325 if (cli_print_queue(&srv
->cli
, fn
) < 0) {
2327 errno
= smbc_errno(context
, &srv
->cli
);
2337 * Delete a print job from a remote printer share
2340 static int smbc_unlink_print_job_ctx(SMBCCTX
*context
, const char *fname
, int id
)
2343 fstring server
, share
, user
, password
, workgroup
;
2347 if (!context
|| !context
->_initialized
) {
2361 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
2363 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2365 if (user
[0] == (char)0) pstrcpy(user
, context
->user
);
2367 pstrcpy(workgroup
, context
->workgroup
);
2369 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2373 return -1; /* errno set by smbc_server */
2377 if ((err
= cli_printjob_del(&srv
->cli
, id
)) != 0) {
2380 errno
= smbc_errno(context
, &srv
->cli
);
2381 else if (err
== ERRnosuchprintjob
)
2392 * Get a new empty handle to fill in with your own info
2394 SMBCCTX
* smbc_new_context(void)
2398 context
= malloc(sizeof(*context
));
2404 ZERO_STRUCTP(context
);
2406 /* ADD REASONABLE DEFAULTS */
2408 context
->timeout
= 20000; /* 20 seconds */
2410 context
->open
= smbc_open_ctx
;
2411 context
->creat
= smbc_creat_ctx
;
2412 context
->read
= smbc_read_ctx
;
2413 context
->write
= smbc_write_ctx
;
2414 context
->close
= smbc_close_ctx
;
2415 context
->unlink
= smbc_unlink_ctx
;
2416 context
->rename
= smbc_rename_ctx
;
2417 context
->lseek
= smbc_lseek_ctx
;
2418 context
->stat
= smbc_stat_ctx
;
2419 context
->fstat
= smbc_fstat_ctx
;
2420 context
->opendir
= smbc_opendir_ctx
;
2421 context
->closedir
= smbc_closedir_ctx
;
2422 context
->readdir
= smbc_readdir_ctx
;
2423 context
->getdents
= smbc_getdents_ctx
;
2424 context
->mkdir
= smbc_mkdir_ctx
;
2425 context
->rmdir
= smbc_rmdir_ctx
;
2426 context
->telldir
= smbc_telldir_ctx
;
2427 context
->lseekdir
= smbc_lseekdir_ctx
;
2428 context
->fstatdir
= smbc_fstatdir_ctx
;
2429 context
->open_print_job
= smbc_open_print_job_ctx
;
2430 context
->print_file
= smbc_print_file_ctx
;
2431 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
2432 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
2434 context
->callbacks
.check_server_fn
= smbc_check_server
;
2435 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
2437 smbc_default_cache_functions(context
);
2445 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2446 * and thus you'll be leaking memory if not handled properly.
2449 int smbc_free_context(SMBCCTX
* context
, int shutdown_ctx
)
2458 DEBUG(1,("Performing aggressive shutdown.\n"));
2460 f
= context
->_files
;
2462 context
->close(context
, f
);
2465 context
->_files
= NULL
;
2467 /* First try to remove the servers the nice way. */
2468 if (context
->callbacks
.purge_cached_fn(context
)) {
2470 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2471 s
= context
->_servers
;
2473 cli_shutdown(&s
->cli
);
2474 context
->callbacks
.remove_cached_srv_fn(context
, s
);
2478 context
->_servers
= NULL
;
2482 /* This is the polite way */
2483 if (context
->callbacks
.purge_cached_fn(context
)) {
2484 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2488 if (context
->_servers
) {
2489 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2493 if (context
->_files
) {
2494 DEBUG(1, ("Active files in context, free_context failed.\n"));
2500 /* Things we have to clean up */
2501 SAFE_FREE(context
->workgroup
);
2502 SAFE_FREE(context
->netbios_name
);
2503 SAFE_FREE(context
->user
);
2505 DEBUG(3, ("Context %p succesfully freed\n", context
));
2512 * Initialise the library etc
2514 * We accept a struct containing handle information.
2515 * valid values for info->debug from 0 to 100,
2516 * and insist that info->fn must be non-null.
2518 SMBCCTX
* smbc_init_context(SMBCCTX
* context
)
2522 char *user
= NULL
, *home
= NULL
;
2529 /* Do not initialise the same client twice */
2530 if (context
->_initialized
) {
2534 if (!context
->callbacks
.auth_fn
|| context
->debug
< 0 || context
->debug
> 100) {
2541 if (!smbc_initialized
) {
2542 /* Do some library wide intialisations the first time we get called */
2544 /* Do we still need this ? */
2547 setup_logging( "libsmbclient", False
);
2549 /* Here we would open the smb.conf file if needed ... */
2551 home
= getenv("HOME");
2553 slprintf(conf
, sizeof(conf
), "%s/.smb/smb.conf", home
);
2555 load_interfaces(); /* Load the list of interfaces ... */
2557 in_client
= True
; /* FIXME, make a param */
2559 if (!lp_load(conf
, True
, False
, False
)) {
2562 * Hmmm, what the hell do we do here ... we could not parse the
2563 * config file ... We must return an error ... and keep info around
2564 * about why we failed
2567 errno
= ENOENT
; /* FIXME: Figure out the correct error response */
2571 reopen_logs(); /* Get logging working ... */
2574 * Block SIGPIPE (from lib/util_sock.c: write())
2575 * It is not needed and should not stop execution
2577 BlockSignals(True
, SIGPIPE
);
2579 /* Done with one-time initialisation */
2580 smbc_initialized
= 1;
2584 if (!context
->user
) {
2586 * FIXME: Is this the best way to get the user info?
2588 user
= getenv("USER");
2589 /* walk around as "guest" if no username can be found */
2590 if (!user
) context
->user
= strdup("guest");
2591 else context
->user
= strdup(user
);
2594 if (!context
->netbios_name
) {
2596 * We try to get our netbios name from the config. If that fails we fall
2597 * back on constructing our netbios name from our hostname etc
2599 if (global_myname
) {
2600 context
->netbios_name
= strdup(global_myname
);
2604 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2607 context
->netbios_name
= malloc(17);
2608 if (!context
->netbios_name
) {
2612 slprintf(context
->netbios_name
, 16, "smbc%s%d", context
->user
, pid
);
2615 DEBUG(0,("Using netbios name %s.\n", context
->netbios_name
));
2618 if (!context
->workgroup
) {
2619 if (lp_workgroup()) {
2620 context
->workgroup
= strdup(lp_workgroup());
2623 /* TODO: Think about a decent default workgroup */
2624 context
->workgroup
= strdup("samba");
2627 DEBUG(0,("Using workgroup %s.\n", context
->workgroup
));
2629 /* shortest timeout is 1 second */
2630 if (context
->timeout
> 0 && context
->timeout
< 1000)
2631 context
->timeout
= 1000;
2634 * FIXME: Should we check the function pointers here?
2637 context
->_initialized
= 1;