2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "../include/libsmb_internal.h"
29 * Functions exported by libsmb_cache.c that we need here
31 int smbc_default_cache_functions(SMBCCTX
*context
);
34 * check if an element is part of the list.
35 * FIXME: Does not belong here !
36 * Can anyone put this in a macro in dlinklist.h ?
39 static int DLIST_CONTAINS(SMBCFILE
* list
, SMBCFILE
*p
) {
40 if (!p
|| !list
) return False
;
42 if (p
== list
) return True
;
48 extern BOOL in_client
;
51 * Is the logging working / configfile read ?
53 static int smbc_initialized
= 0;
56 hex2int( unsigned int _char
)
58 if ( _char
>= 'A' && _char
<='F')
59 return _char
- 'A' + 10;
60 if ( _char
>= 'a' && _char
<='f')
61 return _char
- 'a' + 10;
62 if ( _char
>= '0' && _char
<='9')
68 decode_urlpart(char *segment
, size_t sizeof_segment
)
70 int old_length
= strlen(segment
);
75 char *new_usegment
= 0;
81 /* make a copy of the old one */
82 new_usegment
= (char*)malloc( old_length
* 3 + 1 );
84 while( i
< old_length
) {
85 int bReencode
= False
;
86 unsigned char character
= segment
[ i
++ ];
87 if ((character
<= ' ') || (character
> 127))
90 new_usegment
[ new_length2
++ ] = character
;
91 if (character
== '%' ) {
92 int a
= i
+1 < old_length
? hex2int( segment
[i
] ) : -1;
93 int b
= i
+1 < old_length
? hex2int( segment
[i
+1] ) : -1;
94 if ((a
== -1) || (b
== -1)) { /* Only replace if sequence is valid */
95 /* Contains stray %, make sure to re-encode! */
98 /* Valid %xx sequence */
99 character
= a
* 16 + b
; /* Replace with value of %dd */
101 break; /* Stop at %00 */
103 new_usegment
[ new_length2
++ ] = (unsigned char) segment
[i
++];
104 new_usegment
[ new_length2
++ ] = (unsigned char) segment
[i
++];
108 unsigned int c
= character
/ 16;
110 new_usegment
[ new_length2
++ ] = '%';
112 c
+= (c
> 9) ? ('A' - 10) : '0';
113 new_usegment
[ new_length2
++ ] = c
;
116 c
+= (c
> 9) ? ('A' - 10) : '0';
117 new_usegment
[ new_length2
++ ] = c
;
120 new_segment
[ new_length
++ ] = character
;
122 new_segment
[ new_length
] = 0;
126 /* realloc it with unix charset */
127 pull_utf8_allocate((void**)&new_usegment
, new_segment
);
129 /* this assumes (very safely) that removing %aa sequences
130 only shortens the string */
131 strncpy(segment
, new_usegment
, sizeof_segment
);
137 * Function to parse a path and turn it into components
139 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
141 * smb:// means show all the workgroups
142 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
143 * else, if name<20> exists, list all shares for server ...
146 static const char *smbc_prefix
= "smb:";
149 smbc_parse_path(SMBCCTX
*context
, const char *fname
, char *server
, char *share
, char *path
,
150 char *user
, char *password
) /* FIXME, lengths of strings */
158 server
[0] = share
[0] = path
[0] = user
[0] = password
[0] = (char)0;
161 /* clean_fname(s); causing problems ... */
163 /* see if it has the right prefix */
164 len
= strlen(smbc_prefix
);
165 if (strncmp(s
,smbc_prefix
,len
) ||
166 (s
[len
] != '/' && s
[len
] != 0)) return -1; /* What about no smb: ? */
170 /* Watch the test below, we are testing to see if we should exit */
172 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
178 p
+= 2; /* Skip the // or \\ */
185 strncpy(server
, context
->workgroup
,
186 (strlen(context
->workgroup
) < 16)?strlen(context
->workgroup
):16);
192 * ok, its for us. Now parse out the server, share etc.
194 * However, we want to parse out [[domain;]user[:password]@] if it
198 /* check that '@' occurs before '/', if '/' exists at all */
199 q
= strchr_m(p
, '@');
200 r
= strchr_m(p
, '/');
201 if (q
&& (!r
|| q
< r
)) {
202 pstring username
, passwd
, domain
;
203 const char *u
= userinfo
;
205 next_token(&p
, userinfo
, "@", sizeof(fstring
));
207 username
[0] = passwd
[0] = domain
[0] = 0;
209 if (strchr_m(u
, ';')) {
211 next_token(&u
, domain
, ";", sizeof(fstring
));
215 if (strchr_m(u
, ':')) {
217 next_token(&u
, username
, ":", sizeof(fstring
));
224 pstrcpy(username
, u
);
229 strncpy(user
, username
, sizeof(fstring
)); /* FIXME, size and domain */
232 strncpy(password
, passwd
, sizeof(fstring
)); /* FIXME, size */
236 if (!next_token(&p
, server
, "/", sizeof(fstring
))) {
242 if (*p
== (char)0) goto decoding
; /* That's it ... */
244 if (!next_token(&p
, share
, "/", sizeof(fstring
))) {
252 all_string_sub(path
, "/", "\\", 0);
255 decode_urlpart(path
, sizeof(pstring
));
256 decode_urlpart(server
, sizeof(fstring
));
257 decode_urlpart(share
, sizeof(fstring
));
258 decode_urlpart(user
, sizeof(fstring
));
259 decode_urlpart(password
, sizeof(fstring
));
265 * Convert an SMB error into a UNIX error ...
268 static int smbc_errno(SMBCCTX
*context
, struct cli_state
*c
)
270 int ret
= cli_errno(c
);
272 if (cli_is_dos_error(c
)) {
276 cli_dos_error(c
, &eclass
, &ecode
);
278 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
279 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
283 status
= cli_nt_error(c
);
285 DEBUG(3,("smbc errno %s -> %d\n",
286 nt_errstr(status
), ret
));
294 * returns 0 if the server is in shape. Returns 1 on error
296 * Also useable outside libsmbclient to enable external cache
297 * to do some checks too.
299 int smbc_check_server(SMBCCTX
* context
, SMBCSRV
* server
)
301 if ( send_keepalive(server
->cli
.fd
) == False
)
304 /* connection is ok */
309 * Remove a server from the cached server list it's unused.
310 * On success, 0 is returned. 1 is returned if the server could not be removed.
312 * Also useable outside libsmbclient
314 int smbc_remove_unused_server(SMBCCTX
* context
, SMBCSRV
* srv
)
318 /* are we being fooled ? */
319 if (!context
|| !context
->internal
||
320 !context
->internal
->_initialized
|| !srv
) return 1;
323 /* Check all open files/directories for a relation with this server */
324 for (file
= context
->internal
->_files
; file
; file
=file
->next
) {
325 if (file
->srv
== srv
) {
327 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
333 DLIST_REMOVE(context
->internal
->_servers
, srv
);
335 cli_shutdown(&srv
->cli
);
337 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
339 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
347 * Connect to a server, possibly on an existing connection
349 * Here, what we want to do is: If the server and username
350 * match an existing connection, reuse that, otherwise, establish a
353 * If we have to create a new connection, call the auth_fn to get the
354 * info we need, unless the username and password were passed in.
357 SMBCSRV
*smbc_server(SMBCCTX
*context
,
358 const char *server
, const char *share
,
359 fstring workgroup
, fstring username
,
365 struct nmb_name called
, calling
;
367 const char *server_n
= server
;
371 int tried_reverse
= 0;
376 if (server
[0] == 0) {
383 srv
= context
->callbacks
.get_cached_srv_fn(context
, server
, share
,
384 workgroup
, username
);
386 if (!auth_called
&& !srv
&& (!username
[0] || !password
[0])) {
387 context
->callbacks
.auth_fn(server
, share
, workgroup
, sizeof(fstring
),
388 username
, sizeof(fstring
), password
, sizeof(fstring
));
390 * However, smbc_auth_fn may have picked up info relating to an
391 * existing connection, so try for an existing connection again ...
394 goto check_server_cache
;
399 if (context
->callbacks
.check_server_fn(context
, srv
)) {
401 * This server is no good anymore
402 * Try to remove it and check for more possible servers in the cache
404 if (context
->callbacks
.remove_unused_server_fn(context
, srv
)) {
406 * We could not remove the server completely, remove it from the cache
407 * so we will not get it again. It will be removed when the last file/dir
410 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
414 * Maybe there are more cached connections to this server
416 goto check_server_cache
;
421 make_nmb_name(&calling
, context
->netbios_name
, 0x0);
422 make_nmb_name(&called
, server
, 0x20);
424 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n
, server
));
426 if ((p
=strchr_m(server_n
,'#')) &&
427 (strcmp(p
+1,"1D")==0 || strcmp(p
+1,"01")==0)) {
429 fstrcpy(group
, server_n
);
430 p
= strchr_m(group
,'#');
435 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
438 slprintf(ipenv
,sizeof(ipenv
)-1,"HOST_%s", server_n
);
442 /* have to open a new connection */
443 if (!cli_initialise(&c
)) {
448 c
.timeout
= context
->timeout
;
450 if (!cli_connect(&c
, server_n
, &ip
)) {
456 if (!cli_session_request(&c
, &calling
, &called
)) {
458 if (strcmp(called
.name
, "*SMBSERVER")) {
459 make_nmb_name(&called
, "*SMBSERVER", 0x20);
462 else { /* Try one more time, but ensure we don't loop */
464 /* Only try this if server is an IP address ... */
466 if (is_ipaddress(server
) && !tried_reverse
) {
468 struct in_addr rem_ip
;
470 if ((rem_ip
.s_addr
=inet_addr(server
)) == INADDR_NONE
) {
471 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server
));
476 tried_reverse
++; /* Yuck */
478 if (name_status_find("*", 0, 0, rem_ip
, remote_name
)) {
479 make_nmb_name(&called
, remote_name
, 0x20);
490 DEBUG(4,(" session request ok\n"));
492 if (!cli_negprot(&c
)) {
498 if (!cli_session_setup(&c
, username
,
499 password
, strlen(password
),
500 password
, strlen(password
),
502 /* try an anonymous login if it failed */
503 !cli_session_setup(&c
, "", "", 1,"", 0, workgroup
)) {
509 DEBUG(4,(" session setup ok\n"));
511 if (!cli_send_tconX(&c
, share
, "?????",
512 password
, strlen(password
)+1)) {
513 errno
= smbc_errno(context
, &c
);
518 DEBUG(4,(" tconx ok\n"));
521 * Ok, we have got a nice connection
522 * Let's find a free server_fd
525 srv
= (SMBCSRV
*)malloc(sizeof(*srv
));
533 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
535 /* now add it to the cache (internal or external) */
536 if (context
->callbacks
.add_cached_srv_fn(context
, srv
, server
, share
, workgroup
, username
)) {
537 DEBUG(3, (" Failed to add server to cache\n"));
542 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
543 server
, share
, srv
));
549 if (!srv
) return NULL
;
556 * Routine to open() a file ...
559 static SMBCFILE
*smbc_open_ctx(SMBCCTX
*context
, const char *fname
, int flags
, mode_t mode
)
561 fstring server
, share
, user
, password
, workgroup
;
564 SMBCFILE
*file
= NULL
;
567 if (!context
|| !context
->internal
||
568 !context
->internal
->_initialized
) {
570 errno
= EINVAL
; /* Best I can think of ... */
582 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
584 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
586 fstrcpy(workgroup
, context
->workgroup
);
588 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
592 if (errno
== EPERM
) errno
= EACCES
;
593 return NULL
; /* smbc_server sets errno */
597 /* Hmmm, the test for a directory is suspect here ... FIXME */
599 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
606 file
= malloc(sizeof(SMBCFILE
));
617 if ((fd
= cli_open(&srv
->cli
, path
, flags
, DENY_NONE
)) < 0) {
619 /* Handle the error ... */
622 errno
= smbc_errno(context
, &srv
->cli
);
627 /* Fill in file struct */
630 file
->fname
= strdup(fname
);
635 DLIST_ADD(context
->internal
->_files
, file
);
640 /* Check if opendir needed ... */
645 eno
= smbc_errno(context
, &srv
->cli
);
646 file
= context
->opendir(context
, fname
);
647 if (!file
) errno
= eno
;
652 errno
= EINVAL
; /* FIXME, correct errno ? */
658 * Routine to create a file
661 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
663 static SMBCFILE
*smbc_creat_ctx(SMBCCTX
*context
, const char *path
, mode_t mode
)
666 if (!context
|| !context
->internal
||
667 !context
->internal
->_initialized
) {
674 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
678 * Routine to read() a file ...
681 static ssize_t
smbc_read_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
685 if (!context
|| !context
->internal
||
686 !context
->internal
->_initialized
) {
693 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
695 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
702 /* Check that the buffer exists ... */
711 ret
= cli_read(&file
->srv
->cli
, file
->cli_fd
, buf
, file
->offset
, count
);
715 errno
= smbc_errno(context
, &file
->srv
->cli
);
722 DEBUG(4, (" --> %d\n", ret
));
724 return ret
; /* Success, ret bytes of data ... */
729 * Routine to write() a file ...
732 static ssize_t
smbc_write_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
736 if (!context
|| !context
->internal
||
737 !context
->internal
->_initialized
) {
744 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
751 /* Check that the buffer exists ... */
760 ret
= cli_write(&file
->srv
->cli
, file
->cli_fd
, 0, buf
, file
->offset
, count
);
764 errno
= smbc_errno(context
, &file
->srv
->cli
);
771 return ret
; /* Success, 0 bytes of data ... */
775 * Routine to close() a file ...
778 static int smbc_close_ctx(SMBCCTX
*context
, SMBCFILE
*file
)
782 if (!context
|| !context
->internal
||
783 !context
->internal
->_initialized
) {
790 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
800 return context
->closedir(context
, file
);
804 if (!cli_close(&file
->srv
->cli
, file
->cli_fd
)) {
806 DEBUG(3, ("cli_close failed on %s. purging server.\n",
808 /* Deallocate slot and remove the server
809 * from the server cache if unused */
810 errno
= smbc_errno(context
, &file
->srv
->cli
);
812 DLIST_REMOVE(context
->internal
->_files
, file
);
813 SAFE_FREE(file
->fname
);
815 context
->callbacks
.remove_unused_server_fn(context
, srv
);
821 DLIST_REMOVE(context
->internal
->_files
, file
);
822 SAFE_FREE(file
->fname
);
829 * Get info from an SMB server on a file. Use a qpathinfo call first
830 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
832 static BOOL
smbc_getatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
833 uint16
*mode
, size_t *size
,
834 time_t *c_time
, time_t *a_time
, time_t *m_time
,
838 if (!context
|| !context
->internal
||
839 !context
->internal
->_initialized
) {
846 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
848 if (!srv
->no_pathinfo2
&&
849 cli_qpathinfo2(&srv
->cli
, path
, c_time
, a_time
, m_time
, NULL
,
850 size
, mode
, ino
)) return True
;
852 /* if this is NT then don't bother with the getatr */
853 if (srv
->cli
.capabilities
& CAP_NT_SMBS
) return False
;
855 if (cli_getatr(&srv
->cli
, path
, mode
, size
, m_time
)) {
856 a_time
= c_time
= m_time
;
857 srv
->no_pathinfo2
= True
;
866 * Routine to unlink() a file
869 static int smbc_unlink_ctx(SMBCCTX
*context
, const char *fname
)
871 fstring server
, share
, user
, password
, workgroup
;
875 if (!context
|| !context
->internal
||
876 !context
->internal
->_initialized
) {
878 errno
= EINVAL
; /* Best I can think of ... */
890 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /* FIXME, check errors */
892 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
894 fstrcpy(workgroup
, context
->workgroup
);
896 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
900 return -1; /* smbc_server sets errno */
904 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
906 int job = smbc_stat_printjob(srv, path, NULL, NULL);
912 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
920 if (!cli_unlink(&srv
->cli
, path
)) {
922 errno
= smbc_errno(context
, &srv
->cli
);
924 if (errno
== EACCES
) { /* Check if the file is a directory */
929 time_t m_time
= 0, a_time
= 0, c_time
= 0;
932 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
933 &c_time
, &a_time
, &m_time
, &ino
)) {
935 /* Hmmm, bad error ... What? */
937 errno
= smbc_errno(context
, &srv
->cli
);
943 if (IS_DOS_DIR(mode
))
946 errno
= saverr
; /* Restore this */
955 return 0; /* Success ... */
960 * Routine to rename() a file
963 static int smbc_rename_ctx(SMBCCTX
*ocontext
, const char *oname
,
964 SMBCCTX
*ncontext
, const char *nname
)
966 fstring server1
, share1
, server2
, share2
, user1
, user2
, password1
, password2
, workgroup
;
967 pstring path1
, path2
;
970 if (!ocontext
|| !ncontext
||
971 !ocontext
->internal
|| !ncontext
->internal
||
972 !ocontext
->internal
->_initialized
||
973 !ncontext
->internal
->_initialized
) {
975 errno
= EINVAL
; /* Best I can think of ... */
980 if (!oname
|| !nname
) {
987 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
989 smbc_parse_path(ocontext
, oname
, server1
, share1
, path1
, user1
, password1
);
991 if (user1
[0] == (char)0) fstrcpy(user1
, ocontext
->user
);
993 smbc_parse_path(ncontext
, nname
, server2
, share2
, path2
, user2
, password2
);
995 if (user2
[0] == (char)0) fstrcpy(user2
, ncontext
->user
);
997 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
998 strcmp(user1
, user2
)) {
1000 /* Can't rename across file systems, or users?? */
1007 fstrcpy(workgroup
, ocontext
->workgroup
);
1008 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1009 srv
= smbc_server(ocontext
, server1
, share1
, workgroup
, user1
, password1
);
1016 if (!cli_rename(&srv
->cli
, path1
, path2
)) {
1017 int eno
= smbc_errno(ocontext
, &srv
->cli
);
1019 if (eno
!= EEXIST
||
1020 !cli_unlink(&srv
->cli
, path2
) ||
1021 !cli_rename(&srv
->cli
, path1
, path2
)) {
1029 return 0; /* Success */
1034 * A routine to lseek() a file
1037 static off_t
smbc_lseek_ctx(SMBCCTX
*context
, SMBCFILE
*file
, off_t offset
, int whence
)
1041 if (!context
|| !context
->internal
||
1042 !context
->internal
->_initialized
) {
1049 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1059 return -1; /* Can't lseek a dir ... */
1065 file
->offset
= offset
;
1069 file
->offset
+= offset
;
1073 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1076 SMB_BIG_UINT b_size
= size
;
1077 if (!cli_getattrE(&file
->srv
->cli
, file
->cli_fd
, NULL
, &b_size
, NULL
, NULL
,
1085 file
->offset
= size
+ offset
;
1094 return file
->offset
;
1099 * Generate an inode number from file name for those things that need it
1103 ino_t
smbc_inode(SMBCCTX
*context
, const char *name
)
1106 if (!context
|| !context
->internal
||
1107 !context
->internal
->_initialized
) {
1114 if (!*name
) return 2; /* FIXME, why 2 ??? */
1115 return (ino_t
)str_checksum(name
);
1120 * Routine to put basic stat info into a stat structure ... Used by stat and
1125 int smbc_setup_stat(SMBCCTX
*context
, struct stat
*st
, char *fname
, size_t size
, int mode
)
1130 if (IS_DOS_DIR(mode
)) {
1131 st
->st_mode
= SMBC_DIR_MODE
;
1133 st
->st_mode
= SMBC_FILE_MODE
;
1136 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
1137 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
1138 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
1139 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
1142 st
->st_blksize
= 512;
1143 st
->st_blocks
= (size
+511)/512;
1144 st
->st_uid
= getuid();
1145 st
->st_gid
= getgid();
1147 if (IS_DOS_DIR(mode
)) {
1153 if (st
->st_ino
== 0) {
1154 st
->st_ino
= smbc_inode(context
, fname
);
1157 return True
; /* FIXME: Is this needed ? */
1162 * Routine to stat a file given a name
1165 static int smbc_stat_ctx(SMBCCTX
*context
, const char *fname
, struct stat
*st
)
1168 fstring server
, share
, user
, password
, workgroup
;
1170 time_t m_time
= 0, a_time
= 0, c_time
= 0;
1175 if (!context
|| !context
->internal
||
1176 !context
->internal
->_initialized
) {
1178 errno
= EINVAL
; /* Best I can think of ... */
1190 DEBUG(4, ("smbc_stat(%s)\n", fname
));
1192 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1194 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1196 fstrcpy(workgroup
, context
->workgroup
);
1198 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1202 return -1; /* errno set by smbc_server */
1206 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1208 mode = aDIR | aRONLY;
1211 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1213 if (strcmp(path, "\\") == 0) {
1215 mode = aDIR | aRONLY;
1221 smbc_stat_printjob(srv, path, &size, &m_time);
1222 c_time = a_time = m_time;
1227 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1228 &c_time
, &a_time
, &m_time
, &ino
)) {
1230 errno
= smbc_errno(context
, &srv
->cli
);
1237 smbc_setup_stat(context
, st
, path
, size
, mode
);
1239 st
->st_atime
= a_time
;
1240 st
->st_ctime
= c_time
;
1241 st
->st_mtime
= m_time
;
1242 st
->st_dev
= srv
->dev
;
1249 * Routine to stat a file given an fd
1252 static int smbc_fstat_ctx(SMBCCTX
*context
, SMBCFILE
*file
, struct stat
*st
)
1254 time_t c_time
, a_time
, m_time
;
1259 if (!context
|| !context
->internal
||
1260 !context
->internal
->_initialized
) {
1267 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1276 return context
->fstatdir(context
, file
, st
);
1280 if (!cli_qfileinfo(&file
->srv
->cli
, file
->cli_fd
,
1281 &mode
, &size
, &c_time
, &a_time
, &m_time
, NULL
, &ino
)) {
1282 SMB_BIG_UINT b_size
= size
;
1283 if (!cli_getattrE(&file
->srv
->cli
, file
->cli_fd
,
1284 &mode
, &b_size
, &c_time
, &a_time
, &m_time
)) {
1295 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
1297 st
->st_atime
= a_time
;
1298 st
->st_ctime
= c_time
;
1299 st
->st_mtime
= m_time
;
1300 st
->st_dev
= file
->srv
->dev
;
1307 * Routine to open a directory
1311 * smb: which should list all the workgroups available
1313 * smb:workgroup//server
1315 * smb://server/share
1316 * smb://<IP-addr> which should list shares on server
1317 * smb://<IP-addr>/share which should list files on share
1320 static void smbc_remove_dir(SMBCFILE
*dir
)
1322 struct smbc_dir_list
*d
,*f
;
1329 SAFE_FREE(f
->dirent
);
1334 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
1338 static int add_dirent(SMBCFILE
*dir
, const char *name
, const char *comment
, uint32 type
)
1340 struct smbc_dirent
*dirent
;
1342 char *u_name
= NULL
, *u_comment
= NULL
;
1343 size_t u_name_len
= 0, u_comment_len
= 0;
1346 u_name_len
= push_utf8_allocate(&u_name
, name
);
1348 u_comment_len
= push_utf8_allocate(&u_comment
, comment
);
1351 * Allocate space for the dirent, which must be increased by the
1352 * size of the name and the comment and 1 for the null on the comment.
1353 * The null on the name is already accounted for.
1356 size
= sizeof(struct smbc_dirent
) + u_name_len
+ u_comment_len
+ 1;
1358 dirent
= malloc(size
);
1362 dir
->dir_error
= ENOMEM
;
1367 ZERO_STRUCTP(dirent
);
1369 if (dir
->dir_list
== NULL
) {
1371 dir
->dir_list
= malloc(sizeof(struct smbc_dir_list
));
1372 if (!dir
->dir_list
) {
1375 dir
->dir_error
= ENOMEM
;
1379 ZERO_STRUCTP(dir
->dir_list
);
1381 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
1386 dir
->dir_end
->next
= malloc(sizeof(struct smbc_dir_list
));
1388 if (!dir
->dir_end
->next
) {
1391 dir
->dir_error
= ENOMEM
;
1395 ZERO_STRUCTP(dir
->dir_end
->next
);
1397 dir
->dir_end
= dir
->dir_end
->next
;
1401 dir
->dir_end
->next
= NULL
;
1402 dir
->dir_end
->dirent
= dirent
;
1404 dirent
->smbc_type
= type
;
1405 dirent
->namelen
= u_name_len
;
1406 dirent
->commentlen
= u_comment_len
;
1407 dirent
->dirlen
= size
;
1409 strncpy(dirent
->name
, (u_name
?u_name
:""), dirent
->namelen
+ 1);
1411 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
1412 strncpy(dirent
->comment
, (u_comment
?u_comment
:""), dirent
->commentlen
+ 1);
1414 SAFE_FREE(u_comment
);
1422 list_fn(const char *name
, uint32 type
, const char *comment
, void *state
)
1424 SMBCFILE
*dir
= (SMBCFILE
*)state
;
1427 /* We need to process the type a little ... */
1429 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
1432 case 0: /* Directory tree */
1433 dirent_type
= SMBC_FILE_SHARE
;
1437 dirent_type
= SMBC_PRINTER_SHARE
;
1441 dirent_type
= SMBC_COMMS_SHARE
;
1445 dirent_type
= SMBC_IPC_SHARE
;
1449 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
1453 else dirent_type
= dir
->dir_type
;
1455 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
1457 /* An error occurred, what do we do? */
1458 /* FIXME: Add some code here */
1465 dir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
1468 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
1469 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
1471 /* Handle an error ... */
1473 /* FIXME: Add some code ... */
1479 static SMBCFILE
*smbc_opendir_ctx(SMBCCTX
*context
, const char *fname
)
1481 fstring server
, share
, user
, password
;
1484 SMBCSRV
*srv
= NULL
;
1485 SMBCFILE
*dir
= NULL
;
1486 struct in_addr rem_ip
;
1488 if (!context
|| !context
->internal
||
1489 !context
->internal
->_initialized
) {
1490 DEBUG(4, ("no valid context\n"));
1497 DEBUG(4, ("no valid fname\n"));
1502 if (smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
)) {
1503 DEBUG(4, ("no valid path\n"));
1508 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s'\n", fname
, server
, share
, path
));
1510 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1512 pstrcpy(workgroup
, context
->workgroup
);
1514 dir
= malloc(sizeof(*dir
));
1526 dir
->fname
= strdup(fname
);
1530 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
1532 if (server
[0] == (char)0) {
1533 struct in_addr server_ip
;
1534 if (share
[0] != (char)0 || path
[0] != (char)0) {
1538 SAFE_FREE(dir
->fname
);
1544 /* We have server and share and path empty ... so list the workgroups */
1545 /* first try to get the LMB for our workgroup, and if that fails, */
1548 pstrcpy(workgroup
, lp_workgroup());
1550 if (!find_master_ip(workgroup
, &server_ip
)) {
1551 struct user_auth_info u_info
;
1552 struct cli_state
*cli
;
1554 DEBUG(4, ("Unable to find master browser for workgroup %s\n",
1557 /* find the name of the server ... */
1558 pstrcpy(u_info
.username
, user
);
1559 pstrcpy(u_info
.password
, password
);
1561 if (!(cli
= get_ipc_connect_master_ip_bcast(workgroup
, &u_info
))) {
1562 DEBUG(4, ("Unable to find master browser by "
1568 fstrcpy(server
, cli
->desthost
);
1572 if (!name_status_find("*", 0, 0, server_ip
, server
)) {
1578 DEBUG(4, ("using workgroup %s %s\n", workgroup
, server
));
1581 * Get a connection to IPC$ on the server if we do not already have one
1584 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1589 SAFE_FREE(dir
->fname
);
1596 dir
->dir_type
= SMBC_WORKGROUP
;
1598 /* Now, list the stuff ... */
1600 if (!cli_NetServerEnum(&srv
->cli
, workgroup
, SV_TYPE_DOMAIN_ENUM
, list_fn
,
1604 SAFE_FREE(dir
->fname
);
1607 errno
= cli_errno(&srv
->cli
);
1613 else { /* Server not an empty string ... Check the rest and see what gives */
1615 if (share
[0] == (char)0) {
1617 if (path
[0] != (char)0) { /* Should not have empty share with path */
1621 SAFE_FREE(dir
->fname
);
1628 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1629 /* However, we check to see if <server> is an IP address first */
1631 if (!is_ipaddress(server
) && /* Not an IP addr so check next */
1632 (resolve_name(server
, &rem_ip
, 0x1d) || /* Found LMB */
1633 resolve_name(server
, &rem_ip
, 0x1b) )) { /* Found DMB */
1636 dir
->dir_type
= SMBC_SERVER
;
1639 * Get the backup list ...
1643 if (!name_status_find("*", 0, 0, rem_ip
, buserver
)) {
1645 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server
));
1646 errno
= EPERM
; /* FIXME, is this correct */
1652 * Get a connection to IPC$ on the server if we do not already have one
1655 srv
= smbc_server(context
, buserver
, "IPC$", workgroup
, user
, password
);
1658 DEBUG(0, ("got no contact to IPC$\n"));
1660 SAFE_FREE(dir
->fname
);
1669 /* Now, list the servers ... */
1671 if (!cli_NetServerEnum(&srv
->cli
, server
, 0x0000FFFE, list_fn
,
1675 SAFE_FREE(dir
->fname
);
1678 errno
= cli_errno(&srv
->cli
);
1686 if (resolve_name(server
, &rem_ip
, 0x20)) {
1688 /* Now, list the shares ... */
1690 dir
->dir_type
= SMBC_FILE_SHARE
;
1692 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
1697 SAFE_FREE(dir
->fname
);
1706 /* Now, list the servers ... */
1708 if (cli_RNetShareEnum(&srv
->cli
, list_fn
,
1711 errno
= cli_errno(&srv
->cli
);
1713 SAFE_FREE(dir
->fname
);
1723 errno
= ENODEV
; /* Neither the workgroup nor server exists */
1725 SAFE_FREE(dir
->fname
);
1735 else { /* The server and share are specified ... work from there ... */
1737 /* Well, we connect to the server and list the directory */
1739 dir
->dir_type
= SMBC_FILE_SHARE
;
1741 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1746 SAFE_FREE(dir
->fname
);
1755 /* Now, list the files ... */
1757 pstrcat(path
, "\\*");
1759 if (cli_list(&srv
->cli
, path
, aDIR
| aSYSTEM
| aHIDDEN
, dir_list_fn
,
1763 SAFE_FREE(dir
->fname
);
1766 errno
= smbc_errno(context
, &srv
->cli
);
1774 DLIST_ADD(context
->internal
->_files
, dir
);
1780 * Routine to close a directory
1783 static int smbc_closedir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
1786 if (!context
|| !context
->internal
||
1787 !context
->internal
->_initialized
) {
1794 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
1801 smbc_remove_dir(dir
); /* Clean it up */
1803 DLIST_REMOVE(context
->internal
->_files
, dir
);
1807 SAFE_FREE(dir
->fname
);
1808 SAFE_FREE(dir
); /* Free the space too */
1817 * Routine to get a directory entry
1820 struct smbc_dirent
*smbc_readdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
1822 struct smbc_dirent
*dirp
, *dirent
;
1824 /* Check that all is ok first ... */
1826 if (!context
|| !context
->internal
||
1827 !context
->internal
->_initialized
) {
1834 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
1841 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1852 dirent
= dir
->dir_next
->dirent
;
1861 /* Hmmm, do I even need to copy it? */
1863 memcpy(context
->internal
->_dirent
, dirent
, dirent
->dirlen
); /* Copy the dirent */
1864 dirp
= (struct smbc_dirent
*)context
->internal
->_dirent
;
1865 dirp
->comment
= (char *)(&dirp
->name
+ dirent
->namelen
+ 1);
1866 dir
->dir_next
= dir
->dir_next
->next
;
1868 return (struct smbc_dirent
*)context
->internal
->_dirent
;
1874 * Routine to get directory entries
1877 static int smbc_getdents_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct smbc_dirent
*dirp
, int count
)
1879 struct smbc_dir_list
*dirlist
;
1880 int rem
= count
, reqd
;
1881 char *ndir
= (char *)dirp
;
1883 /* Check that all is ok first ... */
1885 if (!context
|| !context
->internal
||
1886 !context
->internal
->_initialized
) {
1893 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
1900 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
1908 * Now, retrieve the number of entries that will fit in what was passed
1909 * We have to figure out if the info is in the list, or we need to
1910 * send a request to the server to get the info.
1913 while ((dirlist
= dir
->dir_next
)) {
1914 struct smbc_dirent
*dirent
;
1916 if (!dirlist
->dirent
) {
1918 errno
= ENOENT
; /* Bad error */
1923 if (rem
< (reqd
= (sizeof(struct smbc_dirent
) + dirlist
->dirent
->namelen
+
1924 dirlist
->dirent
->commentlen
+ 1))) {
1926 if (rem
< count
) { /* We managed to copy something */
1932 else { /* Nothing copied ... */
1934 errno
= EINVAL
; /* Not enough space ... */
1941 dirent
= dirlist
->dirent
;
1943 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
1945 ((struct smbc_dirent
*)ndir
)->comment
=
1946 (char *)(&((struct smbc_dirent
*)ndir
)->name
+ dirent
->namelen
+ 1);
1952 dir
->dir_next
= dirlist
= dirlist
-> next
;
1963 * Routine to create a directory ...
1966 static int smbc_mkdir_ctx(SMBCCTX
*context
, const char *fname
, mode_t mode
)
1969 fstring server
, share
, user
, password
, workgroup
;
1972 if (!context
|| !context
->internal
||
1973 !context
->internal
->_initialized
) {
1987 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
1989 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
1991 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1993 fstrcpy(workgroup
, context
->workgroup
);
1995 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1999 return -1; /* errno set by smbc_server */
2003 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2005 mode = aDIR | aRONLY;
2008 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2010 if (strcmp(path, "\\") == 0) {
2012 mode = aDIR | aRONLY;
2018 smbc_stat_printjob(srv, path, &size, &m_time);
2019 c_time = a_time = m_time;
2024 if (!cli_mkdir(&srv
->cli
, path
)) {
2026 errno
= smbc_errno(context
, &srv
->cli
);
2036 * Our list function simply checks to see if a directory is not empty
2039 static int smbc_rmdir_dirempty
= True
;
2041 static void rmdir_list_fn(file_info
*finfo
, const char *mask
, void *state
)
2044 if (strncmp(finfo
->name
, ".", 1) != 0 && strncmp(finfo
->name
, "..", 2) != 0)
2045 smbc_rmdir_dirempty
= False
;
2050 * Routine to remove a directory
2053 static int smbc_rmdir_ctx(SMBCCTX
*context
, const char *fname
)
2056 fstring server
, share
, user
, password
, workgroup
;
2059 if (!context
|| !context
->internal
||
2060 !context
->internal
->_initialized
) {
2074 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
2076 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2078 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2080 fstrcpy(workgroup
, context
->workgroup
);
2082 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2086 return -1; /* errno set by smbc_server */
2090 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2092 mode = aDIR | aRONLY;
2095 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2097 if (strcmp(path, "\\") == 0) {
2099 mode = aDIR | aRONLY;
2105 smbc_stat_printjob(srv, path, &size, &m_time);
2106 c_time = a_time = m_time;
2111 if (!cli_rmdir(&srv
->cli
, path
)) {
2113 errno
= smbc_errno(context
, &srv
->cli
);
2115 if (errno
== EACCES
) { /* Check if the dir empty or not */
2117 pstring lpath
; /* Local storage to avoid buffer overflows */
2119 smbc_rmdir_dirempty
= True
; /* Make this so ... */
2121 pstrcpy(lpath
, path
);
2122 pstrcat(lpath
, "\\*");
2124 if (cli_list(&srv
->cli
, lpath
, aDIR
| aSYSTEM
| aHIDDEN
, rmdir_list_fn
,
2127 /* Fix errno to ignore latest error ... */
2129 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2130 smbc_errno(context
, &srv
->cli
)));
2135 if (smbc_rmdir_dirempty
)
2151 * Routine to return the current directory position
2154 static off_t
smbc_telldir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2156 off_t ret_val
; /* Squash warnings about cast */
2158 if (!context
|| !context
->internal
||
2159 !context
->internal
->_initialized
) {
2166 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2173 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2181 * We return the pointer here as the offset
2183 ret_val
= (int)dir
->dir_next
;
2189 * A routine to run down the list and see if the entry is OK
2192 struct smbc_dir_list
*smbc_check_dir_ent(struct smbc_dir_list
*list
,
2193 struct smbc_dirent
*dirent
)
2196 /* Run down the list looking for what we want */
2200 struct smbc_dir_list
*tmp
= list
;
2204 if (tmp
->dirent
== dirent
)
2213 return NULL
; /* Not found, or an error */
2219 * Routine to seek on a directory
2222 static int smbc_lseekdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, off_t offset
)
2224 long int l_offset
= offset
; /* Handle problems of size */
2225 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
2226 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
2228 if (!context
|| !context
->internal
||
2229 !context
->internal
->_initialized
) {
2236 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2243 /* Now, check what we were passed and see if it is OK ... */
2245 if (dirent
== NULL
) { /* Seek to the begining of the list */
2247 dir
->dir_next
= dir
->dir_list
;
2252 /* Now, run down the list and make sure that the entry is OK */
2253 /* This may need to be changed if we change the format of the list */
2255 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
2257 errno
= EINVAL
; /* Bad entry */
2262 dir
->dir_next
= list_ent
;
2269 * Routine to fstat a dir
2272 static int smbc_fstatdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct stat
*st
)
2275 if (!context
|| !context
->internal
||
2276 !context
->internal
->_initialized
) {
2283 /* No code yet ... */
2290 * Open a print file to be written to by other calls
2293 static SMBCFILE
*smbc_open_print_job_ctx(SMBCCTX
*context
, const char *fname
)
2295 fstring server
, share
, user
, password
;
2298 if (!context
|| !context
->internal
||
2299 !context
->internal
->_initialized
) {
2313 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
2315 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2317 /* What if the path is empty, or the file exists? */
2319 return context
->open(context
, fname
, O_WRONLY
, 666);
2324 * Routine to print a file on a remote server ...
2326 * We open the file, which we assume to be on a remote server, and then
2327 * copy it to a print file on the share specified by printq.
2330 static int smbc_print_file_ctx(SMBCCTX
*c_file
, const char *fname
, SMBCCTX
*c_print
, const char *printq
)
2332 SMBCFILE
*fid1
, *fid2
;
2333 int bytes
, saverr
, tot_bytes
= 0;
2336 if (!c_file
|| !c_file
->internal
->_initialized
|| !c_print
||
2337 !c_print
->internal
->_initialized
) {
2344 if (!fname
&& !printq
) {
2351 /* Try to open the file for reading ... */
2353 if ((int)(fid1
= c_file
->open(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
2355 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
2356 return -1; /* smbc_open sets errno */
2360 /* Now, try to open the printer file for writing */
2362 if ((int)(fid2
= c_print
->open_print_job(c_print
, printq
)) < 0) {
2364 saverr
= errno
; /* Save errno */
2365 c_file
->close(c_file
, fid1
);
2371 while ((bytes
= c_file
->read(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
2375 if ((c_print
->write(c_print
, fid2
, buf
, bytes
)) < 0) {
2378 c_file
->close(c_file
, fid1
);
2379 c_print
->close(c_print
, fid2
);
2388 c_file
->close(c_file
, fid1
); /* We have to close these anyway */
2389 c_print
->close(c_print
, fid2
);
2403 * Routine to list print jobs on a printer share ...
2406 static int smbc_list_print_jobs_ctx(SMBCCTX
*context
, const char *fname
, smbc_list_print_job_fn fn
)
2409 fstring server
, share
, user
, password
, workgroup
;
2412 if (!context
|| !context
->internal
||
2413 !context
->internal
->_initialized
) {
2427 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
2429 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2431 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2433 fstrcpy(workgroup
, context
->workgroup
);
2435 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2439 return -1; /* errno set by smbc_server */
2443 if (cli_print_queue(&srv
->cli
, (void (*)(struct print_job_info
*))fn
) < 0) {
2445 errno
= smbc_errno(context
, &srv
->cli
);
2455 * Delete a print job from a remote printer share
2458 static int smbc_unlink_print_job_ctx(SMBCCTX
*context
, const char *fname
, int id
)
2461 fstring server
, share
, user
, password
, workgroup
;
2465 if (!context
|| !context
->internal
||
2466 !context
->internal
->_initialized
) {
2480 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
2482 smbc_parse_path(context
, fname
, server
, share
, path
, user
, password
); /*FIXME, errors*/
2484 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2486 fstrcpy(workgroup
, context
->workgroup
);
2488 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2492 return -1; /* errno set by smbc_server */
2496 if ((err
= cli_printjob_del(&srv
->cli
, id
)) != 0) {
2499 errno
= smbc_errno(context
, &srv
->cli
);
2500 else if (err
== ERRnosuchprintjob
)
2511 * Get a new empty handle to fill in with your own info
2513 SMBCCTX
* smbc_new_context(void)
2517 context
= malloc(sizeof(SMBCCTX
));
2523 ZERO_STRUCTP(context
);
2525 context
->internal
= malloc(sizeof(struct smbc_internal_data
));
2526 if (!context
->internal
) {
2531 ZERO_STRUCTP(context
->internal
);
2534 /* ADD REASONABLE DEFAULTS */
2536 context
->timeout
= 20000; /* 20 seconds */
2538 context
->open
= smbc_open_ctx
;
2539 context
->creat
= smbc_creat_ctx
;
2540 context
->read
= smbc_read_ctx
;
2541 context
->write
= smbc_write_ctx
;
2542 context
->close
= smbc_close_ctx
;
2543 context
->unlink
= smbc_unlink_ctx
;
2544 context
->rename
= smbc_rename_ctx
;
2545 context
->lseek
= smbc_lseek_ctx
;
2546 context
->stat
= smbc_stat_ctx
;
2547 context
->fstat
= smbc_fstat_ctx
;
2548 context
->opendir
= smbc_opendir_ctx
;
2549 context
->closedir
= smbc_closedir_ctx
;
2550 context
->readdir
= smbc_readdir_ctx
;
2551 context
->getdents
= smbc_getdents_ctx
;
2552 context
->mkdir
= smbc_mkdir_ctx
;
2553 context
->rmdir
= smbc_rmdir_ctx
;
2554 context
->telldir
= smbc_telldir_ctx
;
2555 context
->lseekdir
= smbc_lseekdir_ctx
;
2556 context
->fstatdir
= smbc_fstatdir_ctx
;
2557 context
->open_print_job
= smbc_open_print_job_ctx
;
2558 context
->print_file
= smbc_print_file_ctx
;
2559 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
2560 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
2562 context
->callbacks
.check_server_fn
= smbc_check_server
;
2563 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
2565 smbc_default_cache_functions(context
);
2573 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2574 * and thus you'll be leaking memory if not handled properly.
2577 int smbc_free_context(SMBCCTX
* context
, int shutdown_ctx
)
2586 DEBUG(1,("Performing aggressive shutdown.\n"));
2588 f
= context
->internal
->_files
;
2590 context
->close(context
, f
);
2593 context
->internal
->_files
= NULL
;
2595 /* First try to remove the servers the nice way. */
2596 if (context
->callbacks
.purge_cached_fn(context
)) {
2598 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2599 s
= context
->internal
->_servers
;
2601 cli_shutdown(&s
->cli
);
2602 context
->callbacks
.remove_cached_srv_fn(context
, s
);
2606 context
->internal
->_servers
= NULL
;
2610 /* This is the polite way */
2611 if (context
->callbacks
.purge_cached_fn(context
)) {
2612 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2616 if (context
->internal
->_servers
) {
2617 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2621 if (context
->internal
->_files
) {
2622 DEBUG(1, ("Active files in context, free_context failed.\n"));
2628 /* Things we have to clean up */
2629 SAFE_FREE(context
->workgroup
);
2630 SAFE_FREE(context
->netbios_name
);
2631 SAFE_FREE(context
->user
);
2633 DEBUG(3, ("Context %p succesfully freed\n", context
));
2634 SAFE_FREE(context
->internal
);
2641 * Initialise the library etc
2643 * We accept a struct containing handle information.
2644 * valid values for info->debug from 0 to 100,
2645 * and insist that info->fn must be non-null.
2647 SMBCCTX
* smbc_init_context(SMBCCTX
* context
)
2651 char *user
= NULL
, *home
= NULL
;
2653 if (!context
|| !context
->internal
) {
2658 /* Do not initialise the same client twice */
2659 if (context
->internal
->_initialized
) {
2663 if (!context
->callbacks
.auth_fn
|| context
->debug
< 0 || context
->debug
> 100) {
2670 if (!smbc_initialized
) {
2671 /* Do some library wide intialisations the first time we get called */
2673 /* Set this to what the user wants */
2674 DEBUGLEVEL
= context
->debug
;
2676 setup_logging( "libsmbclient", True
);
2678 /* Here we would open the smb.conf file if needed ... */
2680 home
= getenv("HOME");
2682 slprintf(conf
, sizeof(conf
), "%s/.smb/smb.conf", home
);
2684 load_interfaces(); /* Load the list of interfaces ... */
2686 in_client
= True
; /* FIXME, make a param */
2688 if (!lp_load(conf
, True
, False
, False
)) {
2691 * Hmmm, what the hell do we do here ... we could not parse the
2692 * config file ... We must return an error ... and keep info around
2693 * about why we failed
2696 errno
= ENOENT
; /* FIXME: Figure out the correct error response */
2700 reopen_logs(); /* Get logging working ... */
2703 * Block SIGPIPE (from lib/util_sock.c: write())
2704 * It is not needed and should not stop execution
2706 BlockSignals(True
, SIGPIPE
);
2708 /* Done with one-time initialisation */
2709 smbc_initialized
= 1;
2713 if (!context
->user
) {
2715 * FIXME: Is this the best way to get the user info?
2717 user
= getenv("USER");
2718 /* walk around as "guest" if no username can be found */
2719 if (!user
) context
->user
= strdup("guest");
2720 else context
->user
= strdup(user
);
2723 if (!context
->netbios_name
) {
2725 * We try to get our netbios name from the config. If that fails we fall
2726 * back on constructing our netbios name from our hostname etc
2728 if (global_myname()) {
2729 context
->netbios_name
= strdup(global_myname());
2733 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2736 context
->netbios_name
= malloc(17);
2737 if (!context
->netbios_name
) {
2741 slprintf(context
->netbios_name
, 16, "smbc%s%d", context
->user
, pid
);
2745 DEBUG(1, ("Using netbios name %s.\n", context
->netbios_name
));
2747 if (!context
->workgroup
) {
2748 if (lp_workgroup()) {
2749 context
->workgroup
= strdup(lp_workgroup());
2752 /* TODO: Think about a decent default workgroup */
2753 context
->workgroup
= strdup("samba");
2757 DEBUG(1, ("Using workgroup %s.\n", context
->workgroup
));
2759 /* shortest timeout is 1 second */
2760 if (context
->timeout
> 0 && context
->timeout
< 1000)
2761 context
->timeout
= 1000;
2764 * FIXME: Should we check the function pointers here?
2767 context
->internal
->_initialized
= 1;