The fixes from Tom plus a minor update from me.
[Samba.git] / source / libsmb / libsmbclient.c
blobdd46749a5a9386bb1f6310a26aafed67fa684e80
1 /*
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.
24 #include "includes.h"
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);
33 /*
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 ?
37 * -- Tom
39 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
40 if (!p || !list) return False;
41 do {
42 if (p == list) return True;
43 list = list->next;
44 } while (list);
45 return False;
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:";
68 static int
69 smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
70 char *user, char *password) /* FIXME, lengths of strings */
72 static pstring s;
73 pstring userinfo;
74 char *p;
75 char *q, *r;
76 int len;
78 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
79 pstrcpy(s, fname);
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: ? */
88 p = s + len;
90 /* Watch the test below, we are testing to see if we should exit */
92 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
94 return -1;
98 p += 2; /* Skip the // or \\ */
100 if (*p == (char)0)
101 return 0;
103 if (*p == '/') {
105 strncpy(server, context->workgroup,
106 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
107 return 0;
112 * ok, its for us. Now parse out the server, share etc.
114 * However, we want to parse out [[domain;]user[:password]@] if it
115 * exists ...
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;
123 char *u = userinfo;
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));
139 pstrcpy(passwd, u);
142 else {
144 pstrcpy(username, u);
148 if (username[0])
149 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
151 if (passwd[0])
152 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
156 if (!next_token(&p, server, "/", sizeof(fstring))) {
158 return -1;
162 if (*p == (char)0) return 0; /* That's it ... */
164 if (!next_token(&p, share, "/", sizeof(fstring))) {
166 return -1;
170 pstrcpy(path, p);
172 all_string_sub(path, "/", "\\", 0);
174 return 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)) {
186 uint8 eclass;
187 uint32 ecode;
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));
193 } else {
194 NTSTATUS status;
196 status = cli_nt_error(c);
198 DEBUG(3,("smbc errno %s -> %d\n",
199 nt_errstr(status), ret));
202 return ret;
206 * Check a server_fd.
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 )
215 return 1;
217 /* connection is ok */
218 return 0;
222 * Remove a server from the cached server list 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)
229 SMBCFILE * file;
231 /* are we being fooled ? */
232 if (!context || !context->internal ||
233 !context->internal->_initialized || !srv) return 1;
236 /* Check all open files/directories for a relation with this server */
237 for (file = context->internal->_files; file; file=file->next) {
238 if (file->srv == srv) {
239 /* Still used */
240 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
241 srv, file));
242 return 1;
246 DLIST_REMOVE(context->internal->_servers, srv);
248 cli_shutdown(&srv->cli);
250 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
252 context->callbacks.remove_cached_srv_fn(context, srv);
254 SAFE_FREE(srv);
256 return 0;
260 * Connect to a server, possibly on an existing connection
262 * Here, what we want to do is: If the server and username
263 * match an existing connection, reuse that, otherwise, establish a
264 * new connection.
266 * If we have to create a new connection, call the auth_fn to get the
267 * info we need, unless the username and password were passed in.
270 SMBCSRV *smbc_server(SMBCCTX *context,
271 char *server, char *share,
272 char *workgroup, char *username,
273 char *password)
275 SMBCSRV *srv=NULL;
276 int auth_called = 0;
277 struct cli_state c;
278 struct nmb_name called, calling;
279 char *p, *server_n = server;
280 fstring group;
281 pstring ipenv;
282 struct in_addr ip;
283 int tried_reverse = 0;
285 zero_ip(&ip);
286 ZERO_STRUCT(c);
288 if (server[0] == 0) {
289 errno = EPERM;
290 return NULL;
293 check_server_cache:
295 srv = context->callbacks.get_cached_srv_fn(context, server, share,
296 workgroup, username);
298 if (!auth_called && !srv && (!username[0] || !password[0])) {
299 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
300 username, sizeof(fstring), password, sizeof(fstring));
302 * However, smbc_auth_fn may have picked up info relating to an
303 * existing connection, so try for an existing connection again ...
305 auth_called = 1;
306 goto check_server_cache;
310 if (srv) {
311 if (context->callbacks.check_server_fn(context, srv)) {
313 * This server is no good anymore
314 * Try to remove it and check for more possible servers in the cache
316 if (context->callbacks.remove_unused_server_fn(context, srv)) {
318 * We could not remove the server completely, remove it from the cache
319 * so we will not get it again. It will be removed when the last file/dir
320 * is closed.
322 context->callbacks.remove_cached_srv_fn(context, srv);
326 * Maybe there are more cached connections to this server
328 goto check_server_cache;
330 return srv;
333 make_nmb_name(&calling, context->netbios_name, 0x0);
334 make_nmb_name(&called , server, 0x20);
336 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
338 if ((p=strchr_m(server_n,'#')) &&
339 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
341 fstrcpy(group, server_n);
342 p = strchr_m(group,'#');
343 *p = 0;
347 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
349 again:
350 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
352 zero_ip(&ip);
354 /* have to open a new connection */
355 if (!cli_initialise(&c)) {
356 errno = ENOENT;
357 return NULL;
360 c.timeout = context->timeout;
362 if (!cli_connect(&c, server_n, &ip)) {
363 cli_shutdown(&c);
364 errno = ENOENT;
365 return NULL;
368 if (!cli_session_request(&c, &calling, &called)) {
369 cli_shutdown(&c);
370 if (strcmp(called.name, "*SMBSERVER")) {
371 make_nmb_name(&called , "*SMBSERVER", 0x20);
372 goto again;
374 else { /* Try one more time, but ensure we don't loop */
376 /* Only try this if server is an IP address ... */
378 if (is_ipaddress(server) && !tried_reverse) {
379 fstring remote_name;
380 struct in_addr rem_ip;
382 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
383 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
384 errno = ENOENT;
385 return NULL;
388 tried_reverse++; /* Yuck */
390 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
391 make_nmb_name(&called, remote_name, 0x20);
392 goto again;
398 errno = ENOENT;
399 return NULL;
402 DEBUG(4,(" session request ok\n"));
404 if (!cli_negprot(&c)) {
405 cli_shutdown(&c);
406 errno = ENOENT;
407 return NULL;
410 if (!cli_session_setup(&c, username,
411 password, strlen(password),
412 password, strlen(password),
413 workgroup) &&
414 /* try an anonymous login if it failed */
415 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
416 cli_shutdown(&c);
417 errno = EPERM;
418 return NULL;
421 DEBUG(4,(" session setup ok\n"));
423 if (!cli_send_tconX(&c, share, "?????",
424 password, strlen(password)+1)) {
425 errno = smbc_errno(context, &c);
426 cli_shutdown(&c);
427 return NULL;
430 DEBUG(4,(" tconx ok\n"));
433 * Ok, we have got a nice connection
434 * Let's find a free server_fd
437 srv = (SMBCSRV *)malloc(sizeof(*srv));
438 if (!srv) {
439 errno = ENOMEM;
440 goto failed;
443 ZERO_STRUCTP(srv);
444 srv->cli = c;
445 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
447 /* now add it to the cache (internal or external) */
448 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
449 DEBUG(3, (" Failed to add server to cache\n"));
450 goto failed;
454 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
455 server, share, srv));
457 return srv;
459 failed:
460 cli_shutdown(&c);
461 if (!srv) return NULL;
463 SAFE_FREE(srv);
464 return NULL;
468 * Routine to open() a file ...
471 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
473 fstring server, share, user, password, workgroup;
474 pstring path;
475 SMBCSRV *srv = NULL;
476 SMBCFILE *file = NULL;
477 int fd;
479 if (!context || !context->internal ||
480 !context->internal->_initialized) {
482 errno = EINVAL; /* Best I can think of ... */
483 return NULL;
487 if (!fname) {
489 errno = EINVAL;
490 return NULL;
494 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
496 if (user[0] == (char)0) pstrcpy(user, context->user);
498 pstrcpy(workgroup, context->workgroup);
500 srv = smbc_server(context, server, share, workgroup, user, password);
502 if (!srv) {
504 if (errno == EPERM) errno = EACCES;
505 return NULL; /* smbc_server sets errno */
509 /* Hmmm, the test for a directory is suspect here ... FIXME */
511 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
513 fd = -1;
516 else {
518 file = malloc(sizeof(SMBCFILE));
520 if (!file) {
522 errno = ENOMEM;
523 return NULL;
527 ZERO_STRUCTP(file);
529 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
531 /* Handle the error ... */
533 SAFE_FREE(file);
534 errno = smbc_errno(context, &srv->cli);
535 return NULL;
539 /* Fill in file struct */
541 file->cli_fd = fd;
542 file->fname = strdup(fname);
543 file->srv = srv;
544 file->offset = 0;
545 file->file = True;
547 DLIST_ADD(context->internal->_files, file);
548 return file;
552 /* Check if opendir needed ... */
554 if (fd == -1) {
555 int eno = 0;
557 eno = smbc_errno(context, &srv->cli);
558 file = context->opendir(context, fname);
559 if (!file) errno = eno;
560 return file;
564 errno = EINVAL; /* FIXME, correct errno ? */
565 return NULL;
570 * Routine to create a file
573 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
575 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
578 if (!context || !context->internal ||
579 !context->internal->_initialized) {
581 errno = EINVAL;
582 return NULL;
586 return smbc_open_ctx(context, path, creat_bits, mode);
590 * Routine to read() a file ...
593 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
595 int ret;
597 if (!context || !context->internal ||
598 !context->internal->_initialized) {
600 errno = EINVAL;
601 return -1;
605 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
607 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
609 errno = EBADF;
610 return -1;
614 /* Check that the buffer exists ... */
616 if (buf == NULL) {
618 errno = EINVAL;
619 return -1;
623 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
625 if (ret < 0) {
627 errno = smbc_errno(context, &file->srv->cli);
628 return -1;
632 file->offset += ret;
634 DEBUG(4, (" --> %d\n", ret));
636 return ret; /* Success, ret bytes of data ... */
641 * Routine to write() a file ...
644 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
646 int ret;
648 if (!context || !context->internal ||
649 !context->internal->_initialized) {
651 errno = EINVAL;
652 return -1;
656 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
658 errno = EBADF;
659 return -1;
663 /* Check that the buffer exists ... */
665 if (buf == NULL) {
667 errno = EINVAL;
668 return -1;
672 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
674 if (ret <= 0) {
676 errno = smbc_errno(context, &file->srv->cli);
677 return -1;
681 file->offset += ret;
683 return ret; /* Success, 0 bytes of data ... */
687 * Routine to close() a file ...
690 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
692 SMBCSRV *srv;
694 if (!context || !context->internal ||
695 !context->internal->_initialized) {
697 errno = EINVAL;
698 return -1;
702 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
704 errno = EBADF;
705 return -1;
709 /* IS a dir ... */
710 if (!file->file) {
712 return context->closedir(context, file);
716 if (!cli_close(&file->srv->cli, file->cli_fd)) {
718 DEBUG(3, ("cli_close failed on %s. purging server.\n",
719 file->fname));
720 /* Deallocate slot and remove the server
721 * from the server cache if unused */
722 errno = smbc_errno(context, &file->srv->cli);
723 srv = file->srv;
724 DLIST_REMOVE(context->internal->_files, file);
725 SAFE_FREE(file->fname);
726 SAFE_FREE(file);
727 context->callbacks.remove_unused_server_fn(context, srv);
729 return -1;
733 if (!file->file) {
735 return context->closedir(context, file);
739 if (!cli_close(&file->srv->cli, file->cli_fd)) {
740 DEBUG(3, ("cli_close failed on %s. purging server.\n",
741 file->fname));
742 /* Deallocate slot and remove the server
743 * from the server cache if unused */
744 errno = smbc_errno(context, &file->srv->cli);
745 srv = file->srv;
746 DLIST_REMOVE(context->internal->_files, file);
747 SAFE_FREE(file->fname);
748 SAFE_FREE(file);
749 context->callbacks.remove_unused_server_fn(context, srv);
751 return -1;
754 DLIST_REMOVE(context->internal->_files, file);
755 SAFE_FREE(file->fname);
756 SAFE_FREE(file);
758 return 0;
762 * Get info from an SMB server on a file. Use a qpathinfo call first
763 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
765 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
766 uint16 *mode, size_t *size,
767 time_t *c_time, time_t *a_time, time_t *m_time,
768 SMB_INO_T *ino)
771 if (!context || !context->internal ||
772 !context->internal->_initialized) {
774 errno = EINVAL;
775 return -1;
779 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
781 if (!srv->no_pathinfo2 &&
782 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
783 size, mode, ino)) return True;
785 /* if this is NT then don't bother with the getatr */
786 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
788 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
789 a_time = c_time = m_time;
790 srv->no_pathinfo2 = True;
791 return True;
794 return False;
799 * Routine to unlink() a file
802 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
804 fstring server, share, user, password, workgroup;
805 pstring path;
806 SMBCSRV *srv = NULL;
808 if (!context || !context->internal ||
809 !context->internal->_initialized) {
811 errno = EINVAL; /* Best I can think of ... */
812 return -1;
816 if (!fname) {
818 errno = EINVAL;
819 return -1;
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);
831 if (!srv) {
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);
840 if (job == -1) {
842 return -1;
845 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
848 return -1;
851 } else */
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 */
859 int saverr = errno;
860 size_t size = 0;
861 uint16 mode = 0;
862 time_t m_time = 0, a_time = 0, c_time = 0;
863 SMB_INO_T ino = 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);
871 return -1;
874 else {
876 if (IS_DOS_DIR(mode))
877 errno = EISDIR;
878 else
879 errno = saverr; /* Restore this */
884 return -1;
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;
901 SMBCSRV *srv = NULL;
903 if (!ocontext || !ncontext ||
904 !ocontext->internal || !ncontext->internal ||
905 !ocontext->internal->_initialized ||
906 !ncontext->internal->_initialized) {
908 errno = EINVAL; /* Best I can think of ... */
909 return -1;
913 if (!oname || !nname) {
915 errno = EINVAL;
916 return -1;
920 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
922 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
924 if (user1[0] == (char)0) pstrcpy(user1, ocontext->user);
926 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
928 if (user2[0] == (char)0) pstrcpy(user2, ncontext->user);
930 if (strcmp(server1, server2) || strcmp(share1, share2) ||
931 strcmp(user1, user2)) {
933 /* Can't rename across file systems, or users?? */
935 errno = EXDEV;
936 return -1;
940 pstrcpy(workgroup, ocontext->workgroup);
941 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
942 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
943 if (!srv) {
945 return -1;
949 if (!cli_rename(&srv->cli, path1, path2)) {
950 int eno = smbc_errno(ocontext, &srv->cli);
952 if (eno != EEXIST ||
953 !cli_unlink(&srv->cli, path2) ||
954 !cli_rename(&srv->cli, path1, path2)) {
956 errno = eno;
957 return -1;
962 return 0; /* Success */
967 * A routine to lseek() a file
970 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
972 size_t size;
974 if (!context || !context->internal ||
975 !context->internal->_initialized) {
977 errno = EINVAL;
978 return -1;
982 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
984 errno = EBADF;
985 return -1;
989 if (!file->file) {
991 errno = EINVAL;
992 return -1; /* Can't lseek a dir ... */
996 switch (whence) {
997 case SEEK_SET:
998 file->offset = offset;
999 break;
1001 case SEEK_CUR:
1002 file->offset += offset;
1003 break;
1005 case SEEK_END:
1006 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1007 NULL, NULL, NULL) &&
1008 !cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1009 NULL)) {
1011 errno = EINVAL;
1012 return -1;
1014 file->offset = size + offset;
1015 break;
1017 default:
1018 errno = EINVAL;
1019 break;
1023 return file->offset;
1028 * Generate an inode number from file name for those things that need it
1031 static
1032 ino_t smbc_inode(SMBCCTX *context, const char *name)
1035 if (!context || !context->internal ||
1036 !context->internal->_initialized) {
1038 errno = EINVAL;
1039 return -1;
1043 if (!*name) return 2; /* FIXME, why 2 ??? */
1044 return (ino_t)str_checksum(name);
1049 * Routine to put basic stat info into a stat structure ... Used by stat and
1050 * fstat below.
1053 static
1054 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1057 st->st_mode = 0;
1059 if (IS_DOS_DIR(mode)) {
1060 st->st_mode = SMBC_DIR_MODE;
1061 } else {
1062 st->st_mode = SMBC_FILE_MODE;
1065 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1066 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1067 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1068 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1070 st->st_size = size;
1071 st->st_blksize = 512;
1072 st->st_blocks = (size+511)/512;
1073 st->st_uid = getuid();
1074 st->st_gid = getgid();
1076 if (IS_DOS_DIR(mode)) {
1077 st->st_nlink = 2;
1078 } else {
1079 st->st_nlink = 1;
1082 if (st->st_ino == 0) {
1083 st->st_ino = smbc_inode(context, fname);
1086 return True; /* FIXME: Is this needed ? */
1091 * Routine to stat a file given a name
1094 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1096 SMBCSRV *srv;
1097 fstring server, share, user, password, workgroup;
1098 pstring path;
1099 time_t m_time = 0, a_time = 0, c_time = 0;
1100 size_t size = 0;
1101 uint16 mode = 0;
1102 SMB_INO_T ino = 0;
1104 if (!context || !context->internal ||
1105 !context->internal->_initialized) {
1107 errno = EINVAL; /* Best I can think of ... */
1108 return -1;
1112 if (!fname) {
1114 errno = EINVAL;
1115 return -1;
1119 DEBUG(4, ("smbc_stat(%s)\n", fname));
1121 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1123 if (user[0] == (char)0) pstrcpy(user, context->user);
1125 pstrcpy(workgroup, context->workgroup);
1127 srv = smbc_server(context, server, share, workgroup, user, password);
1129 if (!srv) {
1131 return -1; /* errno set by smbc_server */
1135 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1137 mode = aDIR | aRONLY;
1140 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1142 if (strcmp(path, "\\") == 0) {
1144 mode = aDIR | aRONLY;
1147 else {
1149 mode = aRONLY;
1150 smbc_stat_printjob(srv, path, &size, &m_time);
1151 c_time = a_time = m_time;
1154 else { */
1156 if (!smbc_getatr(context, srv, path, &mode, &size,
1157 &c_time, &a_time, &m_time, &ino)) {
1159 errno = smbc_errno(context, &srv->cli);
1160 return -1;
1164 st->st_ino = ino;
1166 smbc_setup_stat(context, st, path, size, mode);
1168 st->st_atime = a_time;
1169 st->st_ctime = c_time;
1170 st->st_mtime = m_time;
1171 st->st_dev = srv->dev;
1173 return 0;
1178 * Routine to stat a file given an fd
1181 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1183 time_t c_time, a_time, m_time;
1184 size_t size;
1185 uint16 mode;
1186 SMB_INO_T ino = 0;
1188 if (!context || !context->internal ||
1189 !context->internal->_initialized) {
1191 errno = EINVAL;
1192 return -1;
1196 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1198 errno = EBADF;
1199 return -1;
1203 if (!file->file) {
1205 return context->fstatdir(context, file, st);
1209 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1210 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1211 !cli_getattrE(&file->srv->cli, file->cli_fd,
1212 &mode, &size, &c_time, &a_time, &m_time)) {
1214 errno = EINVAL;
1215 return -1;
1219 st->st_ino = ino;
1221 smbc_setup_stat(context, st, file->fname, size, mode);
1223 st->st_atime = a_time;
1224 st->st_ctime = c_time;
1225 st->st_mtime = m_time;
1226 st->st_dev = file->srv->dev;
1228 return 0;
1233 * Routine to open a directory
1235 * We want to allow:
1237 * smb: which should list all the workgroups available
1238 * smb:workgroup
1239 * smb:workgroup//server
1240 * smb://server
1241 * smb://server/share
1242 * smb://<IP-addr> which should list shares on server
1243 * smb://<IP-addr>/share which should list files on share
1246 static void smbc_remove_dir(SMBCFILE *dir)
1248 struct smbc_dir_list *d,*f;
1250 d = dir->dir_list;
1251 while (d) {
1253 f = d; d = d->next;
1255 SAFE_FREE(f->dirent);
1256 SAFE_FREE(f);
1260 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1264 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1266 struct smbc_dirent *dirent;
1267 int size;
1270 * Allocate space for the dirent, which must be increased by the
1271 * size of the name and the comment and 1 for the null on the comment.
1272 * The null on the name is already accounted for.
1275 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1276 (comment?strlen(comment):0) + 1;
1278 dirent = malloc(size);
1280 if (!dirent) {
1282 dir->dir_error = ENOMEM;
1283 return -1;
1287 ZERO_STRUCTP(dirent);
1289 if (dir->dir_list == NULL) {
1291 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1292 if (!dir->dir_list) {
1294 SAFE_FREE(dirent);
1295 dir->dir_error = ENOMEM;
1296 return -1;
1299 ZERO_STRUCTP(dir->dir_list);
1301 dir->dir_end = dir->dir_next = dir->dir_list;
1304 else {
1306 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1308 if (!dir->dir_end->next) {
1310 SAFE_FREE(dirent);
1311 dir->dir_error = ENOMEM;
1312 return -1;
1315 ZERO_STRUCTP(dir->dir_end->next);
1317 dir->dir_end = dir->dir_end->next;
1321 dir->dir_end->next = NULL;
1322 dir->dir_end->dirent = dirent;
1324 dirent->smbc_type = type;
1325 dirent->namelen = (name?strlen(name):0);
1326 dirent->commentlen = (comment?strlen(comment):0);
1327 dirent->dirlen = size;
1329 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1331 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1332 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1334 return 0;
1338 static void
1339 list_fn(const char *name, uint32 type, const char *comment, void *state)
1341 SMBCFILE *dir = (SMBCFILE *)state;
1342 int dirent_type;
1344 /* We need to process the type a little ... */
1346 if (dir->dir_type == SMBC_FILE_SHARE) {
1348 switch (type) {
1349 case 0: /* Directory tree */
1350 dirent_type = SMBC_FILE_SHARE;
1351 break;
1353 case 1:
1354 dirent_type = SMBC_PRINTER_SHARE;
1355 break;
1357 case 2:
1358 dirent_type = SMBC_COMMS_SHARE;
1359 break;
1361 case 3:
1362 dirent_type = SMBC_IPC_SHARE;
1363 break;
1365 default:
1366 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1367 break;
1370 else dirent_type = dir->dir_type;
1372 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1374 /* An error occurred, what do we do? */
1375 /* FIXME: Add some code here */
1381 static void
1382 dir_list_fn(file_info *finfo, const char *mask, void *state)
1385 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1386 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1388 /* Handle an error ... */
1390 /* FIXME: Add some code ... */
1396 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1398 fstring server, share, user, password, workgroup;
1399 pstring path;
1400 SMBCSRV *srv = NULL;
1401 SMBCFILE *dir = NULL;
1402 struct in_addr rem_ip;
1404 if (!context || !context->internal ||
1405 !context->internal->_initialized) {
1407 errno = EINVAL;
1408 return NULL;
1412 if (!fname) {
1414 errno = EINVAL;
1415 return NULL;
1419 if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1421 errno = EINVAL;
1422 return NULL;
1426 if (user[0] == (char)0) pstrcpy(user, context->user);
1428 pstrcpy(workgroup, context->workgroup);
1430 dir = malloc(sizeof(*dir));
1432 if (!dir) {
1434 errno = ENOMEM;
1435 return NULL;
1439 ZERO_STRUCTP(dir);
1441 dir->cli_fd = 0;
1442 dir->fname = strdup(fname);
1443 dir->srv = NULL;
1444 dir->offset = 0;
1445 dir->file = False;
1446 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1448 if (server[0] == (char)0) {
1450 if (share[0] != (char)0 || path[0] != (char)0) {
1452 errno = EINVAL;
1453 if (dir) {
1454 SAFE_FREE(dir->fname);
1455 SAFE_FREE(dir);
1457 return NULL;
1461 /* We have server and share and path empty ... so list the workgroups */
1462 /* first try to get the LMB for our workgroup, and if that fails, */
1463 /* try the DMB */
1465 if (!(resolve_name(context->workgroup, &rem_ip, 0x1d) ||
1466 resolve_name(context->workgroup, &rem_ip, 0x1b))) {
1468 errno = EINVAL; /* Something wrong with smb.conf? */
1469 return NULL;
1473 dir->dir_type = SMBC_WORKGROUP;
1475 /* find the name of the server ... */
1477 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1479 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server));
1480 errno = EINVAL;
1481 return NULL;
1486 * Get a connection to IPC$ on the server if we do not already have one
1489 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1491 if (!srv) {
1493 if (dir) {
1494 SAFE_FREE(dir->fname);
1495 SAFE_FREE(dir);
1498 return NULL;
1502 dir->srv = srv;
1504 /* Now, list the stuff ... */
1506 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1507 (void *)dir)) {
1509 if (dir) {
1510 SAFE_FREE(dir->fname);
1511 SAFE_FREE(dir);
1513 errno = cli_errno(&srv->cli);
1515 return NULL;
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 */
1525 errno = EINVAL;
1526 if (dir) {
1527 SAFE_FREE(dir->fname);
1528 SAFE_FREE(dir);
1530 return NULL;
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 */
1540 pstring buserver;
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 */
1553 return NULL;
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);
1563 if (!srv) {
1565 if (dir) {
1566 SAFE_FREE(dir->fname);
1567 SAFE_FREE(dir);
1569 return NULL;
1573 dir->srv = srv;
1575 /* Now, list the servers ... */
1577 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1578 (void *)dir)) {
1580 if (dir) {
1581 SAFE_FREE(dir->fname);
1582 SAFE_FREE(dir);
1584 errno = cli_errno(&srv->cli);
1585 return NULL;
1590 else {
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);
1600 if (!srv) {
1602 if (dir) {
1603 SAFE_FREE(dir->fname);
1604 SAFE_FREE(dir);
1606 return NULL;
1610 dir->srv = srv;
1612 /* Now, list the servers ... */
1614 if (cli_RNetShareEnum(&srv->cli, list_fn,
1615 (void *)dir) < 0) {
1617 errno = cli_errno(&srv->cli);
1618 if (dir) {
1619 SAFE_FREE(dir->fname);
1620 SAFE_FREE(dir);
1622 return NULL;
1627 else {
1629 errno = ENODEV; /* Neither the workgroup nor server exists */
1630 if (dir) {
1631 SAFE_FREE(dir->fname);
1632 SAFE_FREE(dir);
1634 return NULL;
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);
1649 if (!srv) {
1651 if (dir) {
1652 SAFE_FREE(dir->fname);
1653 SAFE_FREE(dir);
1655 return NULL;
1659 dir->srv = srv;
1661 /* Now, list the files ... */
1663 pstrcat(path, "\\*");
1665 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1666 (void *)dir) < 0) {
1668 if (dir) {
1669 SAFE_FREE(dir->fname);
1670 SAFE_FREE(dir);
1672 errno = smbc_errno(context, &srv->cli);
1673 return NULL;
1680 DLIST_ADD(context->internal->_files, dir);
1681 return dir;
1686 * Routine to close a directory
1689 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1692 if (!context || !context->internal ||
1693 !context->internal->_initialized) {
1695 errno = EINVAL;
1696 return -1;
1700 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1702 errno = EBADF;
1703 return -1;
1707 smbc_remove_dir(dir); /* Clean it up */
1709 DLIST_REMOVE(context->internal->_files, dir);
1711 if (dir) {
1713 SAFE_FREE(dir->fname);
1714 SAFE_FREE(dir); /* Free the space too */
1718 return 0;
1723 * Routine to get a directory entry
1726 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1728 struct smbc_dirent *dirp, *dirent;
1730 /* Check that all is ok first ... */
1732 if (!context || !context->internal ||
1733 !context->internal->_initialized) {
1735 errno = EINVAL;
1736 return NULL;
1740 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1742 errno = EBADF;
1743 return NULL;
1747 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1749 errno = ENOTDIR;
1750 return NULL;
1754 if (!dir->dir_next)
1755 return NULL;
1756 else {
1758 dirent = dir->dir_next->dirent;
1760 if (!dirent) {
1762 errno = ENOENT;
1763 return NULL;
1767 /* Hmmm, do I even need to copy it? */
1769 memcpy(context->internal->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1770 dirp = (struct smbc_dirent *)context->internal->_dirent;
1771 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1772 dir->dir_next = dir->dir_next->next;
1774 return (struct smbc_dirent *)context->internal->_dirent;
1780 * Routine to get directory entries
1783 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1785 struct smbc_dir_list *dirlist;
1786 int rem = count, reqd;
1787 char *ndir = (char *)dirp;
1789 /* Check that all is ok first ... */
1791 if (!context || !context->internal ||
1792 !context->internal->_initialized) {
1794 errno = EINVAL;
1795 return -1;
1799 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1801 errno = EBADF;
1802 return -1;
1806 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1808 errno = ENOTDIR;
1809 return -1;
1814 * Now, retrieve the number of entries that will fit in what was passed
1815 * We have to figure out if the info is in the list, or we need to
1816 * send a request to the server to get the info.
1819 while ((dirlist = dir->dir_next)) {
1820 struct smbc_dirent *dirent;
1822 if (!dirlist->dirent) {
1824 errno = ENOENT; /* Bad error */
1825 return -1;
1829 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1830 dirlist->dirent->commentlen + 1))) {
1832 if (rem < count) { /* We managed to copy something */
1834 errno = 0;
1835 return count - rem;
1838 else { /* Nothing copied ... */
1840 errno = EINVAL; /* Not enough space ... */
1841 return -1;
1847 dirent = dirlist->dirent;
1849 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1851 ((struct smbc_dirent *)ndir)->comment =
1852 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1854 ndir += reqd;
1856 rem -= reqd;
1858 dir->dir_next = dirlist = dirlist -> next;
1861 if (rem == count)
1862 return 0;
1863 else
1864 return count - rem;
1869 * Routine to create a directory ...
1872 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1874 SMBCSRV *srv;
1875 fstring server, share, user, password, workgroup;
1876 pstring path;
1878 if (!context || !context->internal ||
1879 !context->internal->_initialized) {
1881 errno = EINVAL;
1882 return -1;
1886 if (!fname) {
1888 errno = EINVAL;
1889 return -1;
1893 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1895 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1897 if (user[0] == (char)0) pstrcpy(user, context->user);
1899 pstrcpy(workgroup, context->workgroup);
1901 srv = smbc_server(context, server, share, workgroup, user, password);
1903 if (!srv) {
1905 return -1; /* errno set by smbc_server */
1909 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1911 mode = aDIR | aRONLY;
1914 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1916 if (strcmp(path, "\\") == 0) {
1918 mode = aDIR | aRONLY;
1921 else {
1923 mode = aRONLY;
1924 smbc_stat_printjob(srv, path, &size, &m_time);
1925 c_time = a_time = m_time;
1928 else { */
1930 if (!cli_mkdir(&srv->cli, path)) {
1932 errno = smbc_errno(context, &srv->cli);
1933 return -1;
1937 return 0;
1942 * Our list function simply checks to see if a directory is not empty
1945 static int smbc_rmdir_dirempty = True;
1947 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
1950 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
1951 smbc_rmdir_dirempty = False;
1956 * Routine to remove a directory
1959 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
1961 SMBCSRV *srv;
1962 fstring server, share, user, password, workgroup;
1963 pstring path;
1965 if (!context || !context->internal ||
1966 !context->internal->_initialized) {
1968 errno = EINVAL;
1969 return -1;
1973 if (!fname) {
1975 errno = EINVAL;
1976 return -1;
1980 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1982 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1984 if (user[0] == (char)0) pstrcpy(user, context->user);
1986 pstrcpy(workgroup, context->workgroup);
1988 srv = smbc_server(context, server, share, workgroup, user, password);
1990 if (!srv) {
1992 return -1; /* errno set by smbc_server */
1996 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1998 mode = aDIR | aRONLY;
2001 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2003 if (strcmp(path, "\\") == 0) {
2005 mode = aDIR | aRONLY;
2008 else {
2010 mode = aRONLY;
2011 smbc_stat_printjob(srv, path, &size, &m_time);
2012 c_time = a_time = m_time;
2015 else { */
2017 if (!cli_rmdir(&srv->cli, path)) {
2019 errno = smbc_errno(context, &srv->cli);
2021 if (errno == EACCES) { /* Check if the dir empty or not */
2023 pstring lpath; /* Local storage to avoid buffer overflows */
2025 smbc_rmdir_dirempty = True; /* Make this so ... */
2027 pstrcpy(lpath, path);
2028 pstrcat(lpath, "\\*");
2030 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2031 NULL) < 0) {
2033 /* Fix errno to ignore latest error ... */
2035 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2036 smbc_errno(context, &srv->cli)));
2037 errno = EACCES;
2041 if (smbc_rmdir_dirempty)
2042 errno = EACCES;
2043 else
2044 errno = ENOTEMPTY;
2048 return -1;
2052 return 0;
2057 * Routine to return the current directory position
2060 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2063 if (!context || !context->internal ||
2064 !context->internal->_initialized) {
2066 errno = EINVAL;
2067 return -1;
2071 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2073 errno = EBADF;
2074 return -1;
2078 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2080 errno = ENOTDIR;
2081 return -1;
2085 return (off_t) dir->dir_next;
2090 * A routine to run down the list and see if the entry is OK
2093 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2094 struct smbc_dirent *dirent)
2097 /* Run down the list looking for what we want */
2099 if (dirent) {
2101 struct smbc_dir_list *tmp = list;
2103 while (tmp) {
2105 if (tmp->dirent == dirent)
2106 return tmp;
2108 tmp = tmp->next;
2114 return NULL; /* Not found, or an error */
2120 * Routine to seek on a directory
2123 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2125 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2126 struct smbc_dir_list *list_ent = NULL;
2128 if (!context || !context->internal ||
2129 !context->internal->_initialized) {
2131 errno = EINVAL;
2132 return -1;
2136 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2138 errno = ENOTDIR;
2139 return -1;
2143 /* Now, check what we were passed and see if it is OK ... */
2145 if (dirent == NULL) { /* Seek to the begining of the list */
2147 dir->dir_next = dir->dir_list;
2148 return 0;
2152 /* Now, run down the list and make sure that the entry is OK */
2153 /* This may need to be changed if we change the format of the list */
2155 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2157 errno = EINVAL; /* Bad entry */
2158 return -1;
2162 dir->dir_next = list_ent;
2164 return 0;
2169 * Routine to fstat a dir
2172 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2175 if (!context || !context->internal ||
2176 !context->internal->_initialized) {
2178 errno = EINVAL;
2179 return -1;
2183 /* No code yet ... */
2185 return 0;
2190 * Open a print file to be written to by other calls
2193 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2195 fstring server, share, user, password;
2196 pstring path;
2198 if (!context || !context->internal ||
2199 !context->internal->_initialized) {
2201 errno = EINVAL;
2202 return NULL;
2206 if (!fname) {
2208 errno = EINVAL;
2209 return NULL;
2213 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2215 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2217 /* What if the path is empty, or the file exists? */
2219 return context->open(context, fname, O_WRONLY, 666);
2224 * Routine to print a file on a remote server ...
2226 * We open the file, which we assume to be on a remote server, and then
2227 * copy it to a print file on the share specified by printq.
2230 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2232 SMBCFILE *fid1, *fid2;
2233 int bytes, saverr, tot_bytes = 0;
2234 char buf[4096];
2236 if (!c_file || !c_file->internal->_initialized || !c_print ||
2237 !c_print->internal->_initialized) {
2239 errno = EINVAL;
2240 return -1;
2244 if (!fname && !printq) {
2246 errno = EINVAL;
2247 return -1;
2251 /* Try to open the file for reading ... */
2253 if ((fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2255 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2256 return -1; /* smbc_open sets errno */
2260 /* Now, try to open the printer file for writing */
2262 if ((fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2264 saverr = errno; /* Save errno */
2265 c_file->close(c_file, fid1);
2266 errno = saverr;
2267 return -1;
2271 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2273 tot_bytes += bytes;
2275 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2277 saverr = errno;
2278 c_file->close(c_file, fid1);
2279 c_print->close(c_print, fid2);
2280 errno = saverr;
2286 saverr = errno;
2288 c_file->close(c_file, fid1); /* We have to close these anyway */
2289 c_print->close(c_print, fid2);
2291 if (bytes < 0) {
2293 errno = saverr;
2294 return -1;
2298 return tot_bytes;
2303 * Routine to list print jobs on a printer share ...
2306 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
2308 SMBCSRV *srv;
2309 fstring server, share, user, password, workgroup;
2310 pstring path;
2312 if (!context || !context->internal ||
2313 !context->internal->_initialized) {
2315 errno = EINVAL;
2316 return -1;
2320 if (!fname) {
2322 errno = EINVAL;
2323 return -1;
2327 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2329 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2331 if (user[0] == (char)0) pstrcpy(user, context->user);
2333 pstrcpy(workgroup, context->workgroup);
2335 srv = smbc_server(context, server, share, workgroup, user, password);
2337 if (!srv) {
2339 return -1; /* errno set by smbc_server */
2343 if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
2345 errno = smbc_errno(context, &srv->cli);
2346 return -1;
2350 return 0;
2355 * Delete a print job from a remote printer share
2358 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2360 SMBCSRV *srv;
2361 fstring server, share, user, password, workgroup;
2362 pstring path;
2363 int err;
2365 if (!context || !context->internal ||
2366 !context->internal->_initialized) {
2368 errno = EINVAL;
2369 return -1;
2373 if (!fname) {
2375 errno = EINVAL;
2376 return -1;
2380 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2382 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2384 if (user[0] == (char)0) pstrcpy(user, context->user);
2386 pstrcpy(workgroup, context->workgroup);
2388 srv = smbc_server(context, server, share, workgroup, user, password);
2390 if (!srv) {
2392 return -1; /* errno set by smbc_server */
2396 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2398 if (err < 0)
2399 errno = smbc_errno(context, &srv->cli);
2400 else if (err == ERRnosuchprintjob)
2401 errno = EINVAL;
2402 return -1;
2406 return 0;
2411 * Get a new empty handle to fill in with your own info
2413 SMBCCTX * smbc_new_context(void)
2415 SMBCCTX * context;
2417 context = malloc(sizeof(SMBCCTX));
2418 if (!context) {
2419 errno = ENOMEM;
2420 return NULL;
2423 ZERO_STRUCTP(context);
2425 context->internal = malloc(sizeof(struct smbc_internal_data));
2426 if (!context->internal) {
2427 errno = ENOMEM;
2428 return NULL;
2431 ZERO_STRUCTP(context->internal);
2434 /* ADD REASONABLE DEFAULTS */
2435 context->debug = 0;
2436 context->timeout = 20000; /* 20 seconds */
2438 context->open = smbc_open_ctx;
2439 context->creat = smbc_creat_ctx;
2440 context->read = smbc_read_ctx;
2441 context->write = smbc_write_ctx;
2442 context->close = smbc_close_ctx;
2443 context->unlink = smbc_unlink_ctx;
2444 context->rename = smbc_rename_ctx;
2445 context->lseek = smbc_lseek_ctx;
2446 context->stat = smbc_stat_ctx;
2447 context->fstat = smbc_fstat_ctx;
2448 context->opendir = smbc_opendir_ctx;
2449 context->closedir = smbc_closedir_ctx;
2450 context->readdir = smbc_readdir_ctx;
2451 context->getdents = smbc_getdents_ctx;
2452 context->mkdir = smbc_mkdir_ctx;
2453 context->rmdir = smbc_rmdir_ctx;
2454 context->telldir = smbc_telldir_ctx;
2455 context->lseekdir = smbc_lseekdir_ctx;
2456 context->fstatdir = smbc_fstatdir_ctx;
2457 context->open_print_job = smbc_open_print_job_ctx;
2458 context->print_file = smbc_print_file_ctx;
2459 context->list_print_jobs = smbc_list_print_jobs_ctx;
2460 context->unlink_print_job = smbc_unlink_print_job_ctx;
2462 context->callbacks.check_server_fn = smbc_check_server;
2463 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2465 smbc_default_cache_functions(context);
2467 return context;
2471 * Free a context
2473 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2474 * and thus you'll be leaking memory if not handled properly.
2477 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2479 if (!context) {
2480 errno = EBADF;
2481 return 1;
2484 if (shutdown_ctx) {
2485 SMBCFILE * f;
2486 DEBUG(1,("Performing aggressive shutdown.\n"));
2488 f = context->internal->_files;
2489 while (f) {
2490 context->close(context, f);
2491 f = f->next;
2493 context->internal->_files = NULL;
2495 /* First try to remove the servers the nice way. */
2496 if (context->callbacks.purge_cached_fn(context)) {
2497 SMBCSRV * s;
2498 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2499 s = context->internal->_servers;
2500 while (s) {
2501 cli_shutdown(&s->cli);
2502 context->callbacks.remove_cached_srv_fn(context, s);
2503 SAFE_FREE(s);
2504 s = s->next;
2506 context->internal->_servers = NULL;
2509 else {
2510 /* This is the polite way */
2511 if (context->callbacks.purge_cached_fn(context)) {
2512 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2513 errno = EBUSY;
2514 return 1;
2516 if (context->internal->_servers) {
2517 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2518 errno = EBUSY;
2519 return 1;
2521 if (context->internal->_files) {
2522 DEBUG(1, ("Active files in context, free_context failed.\n"));
2523 errno = EBUSY;
2524 return 1;
2528 /* Things we have to clean up */
2529 SAFE_FREE(context->workgroup);
2530 SAFE_FREE(context->netbios_name);
2531 SAFE_FREE(context->user);
2533 DEBUG(3, ("Context %p succesfully freed\n", context));
2534 SAFE_FREE(context->internal);
2535 SAFE_FREE(context);
2536 return 0;
2541 * Initialise the library etc
2543 * We accept a struct containing handle information.
2544 * valid values for info->debug from 0 to 100,
2545 * and insist that info->fn must be non-null.
2547 SMBCCTX * smbc_init_context(SMBCCTX * context)
2549 pstring conf;
2550 int pid;
2551 char *user = NULL, *home = NULL;
2553 if (!context || !context->internal) {
2554 errno = EBADF;
2555 return NULL;
2558 /* Do not initialise the same client twice */
2559 if (context->internal->_initialized) {
2560 return 0;
2563 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2565 errno = EINVAL;
2566 return NULL;
2570 if (!smbc_initialized) {
2571 /* Do some library wide intialisations the first time we get called */
2573 /* Do we still need this ? */
2574 DEBUGLEVEL = 10;
2576 setup_logging( "libsmbclient", False);
2578 /* Here we would open the smb.conf file if needed ... */
2580 home = getenv("HOME");
2582 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2584 load_interfaces(); /* Load the list of interfaces ... */
2586 in_client = True; /* FIXME, make a param */
2588 if (!lp_load(conf, True, False, False)) {
2591 * Hmmm, what the hell do we do here ... we could not parse the
2592 * config file ... We must return an error ... and keep info around
2593 * about why we failed
2596 errno = ENOENT; /* FIXME: Figure out the correct error response */
2597 return NULL;
2600 reopen_logs(); /* Get logging working ... */
2603 * Block SIGPIPE (from lib/util_sock.c: write())
2604 * It is not needed and should not stop execution
2606 BlockSignals(True, SIGPIPE);
2608 /* Done with one-time initialisation */
2609 smbc_initialized = 1;
2613 if (!context->user) {
2615 * FIXME: Is this the best way to get the user info?
2617 user = getenv("USER");
2618 /* walk around as "guest" if no username can be found */
2619 if (!user) context->user = strdup("guest");
2620 else context->user = strdup(user);
2623 if (!context->netbios_name) {
2625 * We try to get our netbios name from the config. If that fails we fall
2626 * back on constructing our netbios name from our hostname etc
2628 if (global_myname) {
2629 context->netbios_name = strdup(global_myname);
2631 else {
2633 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2635 pid = sys_getpid();
2636 context->netbios_name = malloc(17);
2637 if (!context->netbios_name) {
2638 errno = ENOMEM;
2639 return NULL;
2641 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2644 DEBUG(0,("Using netbios name %s.\n", context->netbios_name));
2647 if (!context->workgroup) {
2648 if (lp_workgroup()) {
2649 context->workgroup = strdup(lp_workgroup());
2651 else {
2652 /* TODO: Think about a decent default workgroup */
2653 context->workgroup = strdup("samba");
2656 DEBUG(0,("Using workgroup %s.\n", context->workgroup));
2658 /* shortest timeout is 1 second */
2659 if (context->timeout > 0 && context->timeout < 1000)
2660 context->timeout = 1000;
2663 * FIXME: Should we check the function pointers here?
2666 context->internal->_initialized = 1;
2668 return context;