Fix some multibyte problems that I forgot about.
[Samba/id10ts.git] / source3 / libsmb / libsmbclient.c
blob4100005425e627ad133d1cd14265f5ce7af6ea4a
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 SMB client library implementation
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Richard Sharpe 2000, 2002
7 Copyright (C) John Terpstra 2000
8 Copyright (C) Tom Jansen (Ninja ISD) 2002
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
28 * Define this to get the real SMBCFILE and SMBCSRV structures
30 #define _SMBC_INTERNAL
32 #include "libsmbclient.h"
35 * Functions exported by libsmb_cache.c that we need here
37 int smbc_default_cache_functions(SMBCCTX * context);
39 /*
40 * check if an element is part of the list.
41 * FIXME: Does not belong here !
42 * Can anyone put this in a macro in dlinklist.h ?
43 * -- Tom
45 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
46 if (!p || !list) return False;
47 do {
48 if (p == list) return True;
49 list = list->next;
50 } while (list);
51 return False;
54 extern BOOL in_client;
55 extern pstring global_myname;
58 * Is the logging working / configfile read ?
60 static int smbc_initialized = 0;
63 * Function to parse a path and turn it into components
65 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
67 * smb:// means show all the workgroups
68 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
69 * else, if name<20> exists, list all shares for server ...
72 static const char *smbc_prefix = "smb:";
74 static int
75 smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
76 char *user, char *password) /* FIXME, lengths of strings */
78 static pstring s;
79 pstring userinfo;
80 char *p;
81 char *q, *r;
82 int len;
84 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
85 pstrcpy(s, fname);
87 /* clean_fname(s); causing problems ... */
89 /* see if it has the right prefix */
90 len = strlen(smbc_prefix);
91 if (strncmp(s,smbc_prefix,len) ||
92 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
94 p = s + len;
96 /* Watch the test below, we are testing to see if we should exit */
98 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
100 return -1;
104 p += 2; /* Skip the // or \\ */
106 if (*p == (char)0)
107 return 0;
109 if (*p == '/') {
111 strncpy(server, context->workgroup,
112 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
113 return 0;
118 * ok, its for us. Now parse out the server, share etc.
120 * However, we want to parse out [[domain;]user[:password]@] if it
121 * exists ...
124 /* check that '@' occurs before '/', if '/' exists at all */
125 q = strchr_m(p, '@');
126 r = strchr_m(p, '/');
127 if (q && (!r || q < r)) {
128 pstring username, passwd, domain;
129 char *u = userinfo;
131 next_token(&p, userinfo, "@", sizeof(fstring));
133 username[0] = passwd[0] = domain[0] = 0;
135 if (strchr_m(u, ';')) {
137 next_token(&u, domain, ";", sizeof(fstring));
141 if (strchr_m(u, ':')) {
143 next_token(&u, username, ":", sizeof(fstring));
145 pstrcpy(passwd, u);
148 else {
150 pstrcpy(username, u);
154 if (username[0])
155 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
157 if (passwd[0])
158 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
162 if (!next_token(&p, server, "/", sizeof(fstring))) {
164 return -1;
168 if (*p == (char)0) return 0; /* That's it ... */
170 if (!next_token(&p, share, "/", sizeof(fstring))) {
172 return -1;
176 pstrcpy(path, p);
178 all_string_sub(path, "/", "\\", 0);
180 return 0;
184 * Convert an SMB error into a UNIX error ...
187 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
189 int ret;
191 if (cli_is_dos_error(c)) {
192 uint8 eclass;
193 uint32 ecode;
195 cli_dos_error(c, &eclass, &ecode);
196 ret = cli_errno_from_dos(eclass, ecode);
198 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
199 (int)eclass, (int)ecode, (int)ecode, ret));
200 } else {
201 NTSTATUS status;
203 status = cli_nt_error(c);
204 ret = cli_errno_from_nt(status);
206 DEBUG(3,("smbc errno %s -> %d\n",
207 get_nt_error_msg(status), ret));
210 return ret;
214 * Check a server_fd.
215 * returns 0 if the server is in shape. Returns 1 on error
217 * Also useable outside libsmbclient to enable external cache
218 * to do some checks too.
220 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
222 if ( cli_send_keepalive(&server->cli) == False )
223 return 1;
225 /* connection is ok */
226 return 0;
230 * Remove a server from the list server_table if it's unused.
231 * On success, 0 is returned. 1 is returned if the server could not be removed.
233 * Also useable outside libsmbclient
235 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
237 SMBCFILE * file;
239 /* are we being fooled ? */
240 if (!context || !context->_initialized || !srv) return 1;
243 /* Check all open files/directories for a relation with this server */
244 for (file = context->_files; file; file=file->next) {
245 if (file->srv == srv) {
246 /* Still used */
247 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
248 srv, file));
249 return 1;
253 DLIST_REMOVE(context->_servers, srv);
255 cli_shutdown(&srv->cli);
257 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
259 context->callbacks.remove_cached_srv_fn(context, srv);
261 SAFE_FREE(srv);
263 return 0;
267 * Connect to a server, possibly on an existing connection
269 * Here, what we want to do is: If the server and username
270 * match an existing connection, reuse that, otherwise, establish a
271 * new connection.
273 * If we have to create a new connection, call the auth_fn to get the
274 * info we need, unless the username and password were passed in.
277 SMBCSRV *smbc_server(SMBCCTX *context,
278 char *server, char *share,
279 char *workgroup, char *username,
280 char *password)
282 SMBCSRV *srv=NULL;
283 int auth_called = 0;
284 struct cli_state c;
285 struct nmb_name called, calling;
286 char *p, *server_n = server;
287 fstring group;
288 pstring ipenv;
289 struct in_addr ip;
290 int tried_reverse = 0;
292 zero_ip(&ip);
293 ZERO_STRUCT(c);
295 if (server[0] == 0) {
296 errno = EPERM;
297 return NULL;
300 check_server_cache:
302 srv = context->callbacks.get_cached_srv_fn(context, server, share,
303 workgroup, username);
305 if (!auth_called && !srv && (!username[0] || !password[0])) {
306 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
307 username, sizeof(fstring), password, sizeof(fstring));
309 * However, smbc_auth_fn may have picked up info relating to an
310 * existing connection, so try for an existing connection again ...
312 auth_called = 1;
313 goto check_server_cache;
317 if (srv) {
318 if (context->callbacks.check_server_fn(context, srv)) {
320 * This server is no good anymore
321 * Try to remove it and check for more possible servers in the cache
323 if (context->callbacks.remove_unused_server_fn(context, srv)) {
325 * We could not remove the server completely, remove it from the cache
326 * so we will not get it again. It will be removed when the last file/dir
327 * is closed.
329 context->callbacks.remove_cached_srv_fn(context, srv);
333 * Maybe there are more cached connections to this server
335 goto check_server_cache;
337 return srv;
340 make_nmb_name(&calling, context->netbios_name, 0x0);
341 make_nmb_name(&called , server, 0x20);
343 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
345 if ((p=strchr_m(server_n,'#')) &&
346 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
348 fstrcpy(group, server_n);
349 p = strchr_m(group,'#');
350 *p = 0;
354 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
356 again:
357 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
359 zero_ip(&ip);
361 /* have to open a new connection */
362 if (!cli_initialise(&c)) {
363 errno = ENOENT;
364 return NULL;
367 c.timeout = context->timeout;
369 if (!cli_connect(&c, server_n, &ip)) {
370 cli_shutdown(&c);
371 errno = ENOENT;
372 return NULL;
375 if (!cli_session_request(&c, &calling, &called)) {
376 cli_shutdown(&c);
377 if (strcmp(called.name, "*SMBSERVER")) {
378 make_nmb_name(&called , "*SMBSERVER", 0x20);
379 goto again;
381 else { /* Try one more time, but ensure we don't loop */
383 /* Only try this if server is an IP address ... */
385 if (is_ipaddress(server) && !tried_reverse) {
386 fstring remote_name;
387 struct in_addr rem_ip;
389 if (!inet_aton(server, &rem_ip)) {
390 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
391 errno = ENOENT;
392 return NULL;
395 tried_reverse++; /* Yuck */
397 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
398 make_nmb_name(&called, remote_name, 0x20);
399 goto again;
405 errno = ENOENT;
406 return NULL;
409 DEBUG(4,(" session request ok\n"));
411 if (!cli_negprot(&c)) {
412 cli_shutdown(&c);
413 errno = ENOENT;
414 return NULL;
417 if (!cli_session_setup(&c, username,
418 password, strlen(password),
419 password, strlen(password),
420 workgroup) &&
421 /* try an anonymous login if it failed */
422 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
423 cli_shutdown(&c);
424 errno = EPERM;
425 return NULL;
428 DEBUG(4,(" session setup ok\n"));
430 if (!cli_send_tconX(&c, share, "?????",
431 password, strlen(password)+1)) {
432 errno = smbc_errno(context, &c);
433 cli_shutdown(&c);
434 return NULL;
437 DEBUG(4,(" tconx ok\n"));
440 * Ok, we have got a nice connection
441 * Let's find a free server_fd
444 srv = (SMBCSRV *)malloc(sizeof(*srv));
445 if (!srv) {
446 errno = ENOMEM;
447 goto failed;
450 ZERO_STRUCTP(srv);
451 srv->cli = c;
452 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
454 /* now add it to the cache (internal or external) */
455 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
456 DEBUG(3, (" Failed to add server to cache\n"));
457 goto failed;
461 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
462 server, share, srv));
464 return srv;
466 failed:
467 cli_shutdown(&c);
468 if (!srv) return NULL;
470 SAFE_FREE(srv);
471 return NULL;
475 * Routine to open() a file ...
478 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
480 fstring server, share, user, password, workgroup;
481 pstring path;
482 SMBCSRV *srv = NULL;
483 SMBCFILE *file = NULL;
484 int fd;
486 if (!context || !context->_initialized) {
488 errno = EINVAL; /* Best I can think of ... */
489 return NULL;
493 if (!fname) {
495 errno = EINVAL;
496 return NULL;
500 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
502 if (user[0] == (char)0) pstrcpy(user, context->user);
504 pstrcpy(workgroup, context->workgroup);
506 srv = smbc_server(context, server, share, workgroup, user, password);
508 if (!srv) {
510 if (errno == EPERM) errno = EACCES;
511 return NULL; /* smbc_server sets errno */
515 /* Hmmm, the test for a directory is suspect here ... FIXME */
517 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
519 fd = -1;
522 else {
524 file = malloc(sizeof(SMBCFILE));
526 if (!file) {
528 errno = ENOMEM;
529 return NULL;
533 ZERO_STRUCTP(file);
535 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
537 /* Handle the error ... */
539 SAFE_FREE(file);
540 errno = smbc_errno(context, &srv->cli);
541 return NULL;
545 /* Fill in file struct */
547 file->cli_fd = fd;
548 file->fname = strdup(fname);
549 file->srv = srv;
550 file->offset = 0;
551 file->file = True;
553 DLIST_ADD(context->_files, file);
554 return file;
558 /* Check if opendir needed ... */
560 if (fd == -1) {
561 int eno = 0;
563 eno = smbc_errno(context, &srv->cli);
564 file = context->opendir(context, fname);
565 if (!file) errno = eno;
566 return file;
570 errno = EINVAL; /* FIXME, correct errno ? */
571 return NULL;
576 * Routine to create a file
579 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
581 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
584 if (!context || !context->_initialized) {
586 errno = EINVAL;
587 return NULL;
591 return smbc_open_ctx(context, path, creat_bits, mode);
595 * Routine to read() a file ...
598 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
600 int ret;
602 if (!context || !context->_initialized) {
604 errno = EINVAL;
605 return -1;
609 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
611 if (!file || !DLIST_CONTAINS(context->_files, file)) {
613 errno = EBADF;
614 return -1;
618 /* Check that the buffer exists ... */
620 if (buf == NULL) {
622 errno = EINVAL;
623 return -1;
627 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
629 if (ret < 0) {
631 errno = smbc_errno(context, &file->srv->cli);
632 return -1;
636 file->offset += ret;
638 DEBUG(4, (" --> %d\n", ret));
640 return ret; /* Success, ret bytes of data ... */
645 * Routine to write() a file ...
648 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
650 int ret;
652 if (!context || context->_initialized) {
654 errno = EINVAL;
655 return -1;
659 if (!file || !DLIST_CONTAINS(context->_files, file)) {
661 errno = EBADF;
662 return -1;
666 /* Check that the buffer exists ... */
668 if (buf == NULL) {
670 errno = EINVAL;
671 return -1;
675 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
677 if (ret <= 0) {
679 errno = smbc_errno(context, &file->srv->cli);
680 return -1;
684 file->offset += ret;
686 return ret; /* Success, 0 bytes of data ... */
690 * Routine to close() a file ...
693 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
695 SMBCSRV *srv;
697 if (!context || !context->_initialized) {
699 errno = EINVAL;
700 return -1;
704 if (!file || !DLIST_CONTAINS(context->_files, file)) {
706 errno = EBADF;
707 return -1;
711 /* IS a dir ... */
712 if (!file->file) {
714 return context->closedir(context, file);
718 if (!cli_close(&file->srv->cli, file->cli_fd)) {
720 DEBUG(3, ("cli_close failed on %s. purging server.\n",
721 file->fname));
722 /* Deallocate slot and remove the server
723 * from the server cache if unused */
724 errno = smbc_errno(context, &file->srv->cli);
725 srv = file->srv;
726 DLIST_REMOVE(context->_files, file);
727 SAFE_FREE(file->fname);
728 SAFE_FREE(file);
729 context->callbacks.remove_unused_server_fn(context, srv);
731 return -1;
735 if (!file->file) {
737 return context->closedir(context, file);
741 if (!cli_close(&file->srv->cli, file->cli_fd)) {
742 DEBUG(3, ("cli_close failed on %s. purging server.\n",
743 file->fname));
744 /* Deallocate slot and remove the server
745 * from the server cache if unused */
746 errno = smbc_errno(context, &file->srv->cli);
747 srv = file->srv;
748 DLIST_REMOVE(context->_files, file);
749 SAFE_FREE(file->fname);
750 SAFE_FREE(file);
751 context->callbacks.remove_unused_server_fn(context, srv);
753 return -1;
756 DLIST_REMOVE(context->_files, file);
757 SAFE_FREE(file->fname);
758 SAFE_FREE(file);
760 return 0;
764 * Get info from an SMB server on a file. Use a qpathinfo call first
765 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
767 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
768 uint16 *mode, size_t *size,
769 time_t *c_time, time_t *a_time, time_t *m_time,
770 SMB_INO_T *ino)
773 if (!context || !context->_initialized) {
775 errno = EINVAL;
776 return -1;
780 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
782 if (!srv->no_pathinfo2 &&
783 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
784 size, mode, ino)) return True;
786 /* if this is NT then don't bother with the getatr */
787 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
789 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
790 a_time = c_time = m_time;
791 srv->no_pathinfo2 = True;
792 return True;
795 return False;
800 * Routine to unlink() a file
803 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
805 fstring server, share, user, password, workgroup;
806 pstring path;
807 SMBCSRV *srv = NULL;
809 if (!context || context->_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->_initialized || !ncontext->_initialized) {
906 errno = EINVAL; /* Best I can think of ... */
907 return -1;
911 if (!oname || !nname) {
913 errno = EINVAL;
914 return -1;
918 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
920 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
922 if (user1[0] == (char)0) pstrcpy(user1, ocontext->user);
924 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
926 if (user2[0] == (char)0) pstrcpy(user2, ncontext->user);
928 if (strcmp(server1, server2) || strcmp(share1, share2) ||
929 strcmp(user1, user2)) {
931 /* Can't rename across file systems, or users?? */
933 errno = EXDEV;
934 return -1;
938 pstrcpy(workgroup, ocontext->workgroup);
939 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
940 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
941 if (!srv) {
943 return -1;
947 if (!cli_rename(&srv->cli, path1, path2)) {
948 int eno = smbc_errno(ocontext, &srv->cli);
950 if (eno != EEXIST ||
951 !cli_unlink(&srv->cli, path2) ||
952 !cli_rename(&srv->cli, path1, path2)) {
954 errno = eno;
955 return -1;
960 return 0; /* Success */
965 * A routine to lseek() a file
968 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
970 size_t size;
972 if (!context || !context->_initialized) {
974 errno = EINVAL;
975 return -1;
979 if (!file || !DLIST_CONTAINS(context->_files, file)) {
981 errno = EBADF;
982 return -1;
986 if (!file->file) {
988 errno = EINVAL;
989 return -1; /* Can't lseek a dir ... */
993 switch (whence) {
994 case SEEK_SET:
995 file->offset = offset;
996 break;
998 case SEEK_CUR:
999 file->offset += offset;
1000 break;
1002 case SEEK_END:
1003 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1004 NULL, NULL, NULL) &&
1005 !cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1006 NULL)) {
1008 errno = EINVAL;
1009 return -1;
1011 file->offset = size + offset;
1012 break;
1014 default:
1015 errno = EINVAL;
1016 break;
1020 return file->offset;
1025 * Generate an inode number from file name for those things that need it
1028 static
1029 ino_t smbc_inode(SMBCCTX *context, const char *name)
1032 if (!context || !context->_initialized) {
1034 errno = EINVAL;
1035 return -1;
1039 if (!*name) return 2; /* FIXME, why 2 ??? */
1040 return (ino_t)str_checksum(name);
1045 * Routine to put basic stat info into a stat structure ... Used by stat and
1046 * fstat below.
1049 static
1050 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1053 st->st_mode = 0;
1055 if (IS_DOS_DIR(mode)) {
1056 st->st_mode = SMBC_DIR_MODE;
1057 } else {
1058 st->st_mode = SMBC_FILE_MODE;
1061 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1062 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1063 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1064 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1066 st->st_size = size;
1067 st->st_blksize = 512;
1068 st->st_blocks = (size+511)/512;
1069 st->st_uid = getuid();
1070 st->st_gid = getgid();
1072 if (IS_DOS_DIR(mode)) {
1073 st->st_nlink = 2;
1074 } else {
1075 st->st_nlink = 1;
1078 if (st->st_ino == 0) {
1079 st->st_ino = smbc_inode(context, fname);
1082 return True; /* FIXME: Is this needed ? */
1087 * Routine to stat a file given a name
1090 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1092 SMBCSRV *srv;
1093 fstring server, share, user, password, workgroup;
1094 pstring path;
1095 time_t m_time = 0, a_time = 0, c_time = 0;
1096 size_t size = 0;
1097 uint16 mode = 0;
1098 SMB_INO_T ino = 0;
1100 if (!context || !context->_initialized) {
1102 errno = EINVAL; /* Best I can think of ... */
1103 return -1;
1107 if (!fname) {
1109 errno = EINVAL;
1110 return -1;
1114 DEBUG(4, ("smbc_stat(%s)\n", fname));
1116 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1118 if (user[0] == (char)0) pstrcpy(user, context->user);
1120 pstrcpy(workgroup, context->workgroup);
1122 srv = smbc_server(context, server, share, workgroup, user, password);
1124 if (!srv) {
1126 return -1; /* errno set by smbc_server */
1130 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1132 mode = aDIR | aRONLY;
1135 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1137 if (strcmp(path, "\\") == 0) {
1139 mode = aDIR | aRONLY;
1142 else {
1144 mode = aRONLY;
1145 smbc_stat_printjob(srv, path, &size, &m_time);
1146 c_time = a_time = m_time;
1149 else { */
1151 if (!smbc_getatr(context, srv, path, &mode, &size,
1152 &c_time, &a_time, &m_time, &ino)) {
1154 errno = smbc_errno(context, &srv->cli);
1155 return -1;
1159 st->st_ino = ino;
1161 smbc_setup_stat(context, st, path, size, mode);
1163 st->st_atime = a_time;
1164 st->st_ctime = c_time;
1165 st->st_mtime = m_time;
1166 st->st_dev = srv->dev;
1168 return 0;
1173 * Routine to stat a file given an fd
1176 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1178 time_t c_time, a_time, m_time;
1179 size_t size;
1180 uint16 mode;
1181 SMB_INO_T ino = 0;
1183 if (!context || !context->_initialized) {
1185 errno = EINVAL;
1186 return -1;
1190 if (!file || !DLIST_CONTAINS(context->_files, file)) {
1192 errno = EBADF;
1193 return -1;
1197 if (!file->file) {
1199 return context->fstatdir(context, file, st);
1203 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1204 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1205 !cli_getattrE(&file->srv->cli, file->cli_fd,
1206 &mode, &size, &c_time, &a_time, &m_time)) {
1208 errno = EINVAL;
1209 return -1;
1213 st->st_ino = ino;
1215 smbc_setup_stat(context, st, file->fname, size, mode);
1217 st->st_atime = a_time;
1218 st->st_ctime = c_time;
1219 st->st_mtime = m_time;
1220 st->st_dev = file->srv->dev;
1222 return 0;
1227 * Routine to open a directory
1229 * We want to allow:
1231 * smb: which should list all the workgroups available
1232 * smb:workgroup
1233 * smb:workgroup//server
1234 * smb://server
1235 * smb://server/share
1236 * smb://<IP-addr> which should list shares on server
1237 * smb://<IP-addr>/share which should list files on share
1240 static void smbc_remove_dir(SMBCFILE *dir)
1242 struct smbc_dir_list *d,*f;
1244 d = dir->dir_list;
1245 while (d) {
1247 f = d; d = d->next;
1249 SAFE_FREE(f->dirent);
1250 SAFE_FREE(f);
1254 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1258 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1260 struct smbc_dirent *dirent;
1261 int size;
1264 * Allocate space for the dirent, which must be increased by the
1265 * size of the name and the comment and 1 for the null on the comment.
1266 * The null on the name is already accounted for.
1269 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1270 (comment?strlen(comment):0) + 1;
1272 dirent = malloc(size);
1274 if (!dirent) {
1276 dir->dir_error = ENOMEM;
1277 return -1;
1281 ZERO_STRUCTP(dirent);
1283 ZERO_STRUCTP(dirent);
1286 if (dir->dir_list == NULL) {
1288 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1289 if (!dir->dir_list) {
1291 SAFE_FREE(dirent);
1292 dir->dir_error = ENOMEM;
1293 return -1;
1296 ZERO_STRUCTP(dir->dir_list);
1298 dir->dir_end = dir->dir_next = dir->dir_list;
1301 else {
1303 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1305 if (!dir->dir_end->next) {
1307 SAFE_FREE(dirent);
1308 dir->dir_error = ENOMEM;
1309 return -1;
1312 ZERO_STRUCTP(dir->dir_end->next);
1314 dir->dir_end = dir->dir_end->next;
1318 dir->dir_end->next = NULL;
1319 dir->dir_end->dirent = dirent;
1321 dirent->smbc_type = type;
1322 dirent->namelen = (name?strlen(name):0);
1323 dirent->commentlen = (comment?strlen(comment):0);
1324 dirent->dirlen = size;
1326 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1328 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1329 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1331 return 0;
1335 static void
1336 list_fn(const char *name, uint32 type, const char *comment, void *state)
1338 SMBCFILE *dir = (SMBCFILE *)state;
1339 int dirent_type;
1341 /* We need to process the type a little ... */
1343 if (dir->dir_type == SMBC_FILE_SHARE) {
1345 switch (type) {
1346 case 0: /* Directory tree */
1347 dirent_type = SMBC_FILE_SHARE;
1348 break;
1350 case 1:
1351 dirent_type = SMBC_PRINTER_SHARE;
1352 break;
1354 case 2:
1355 dirent_type = SMBC_COMMS_SHARE;
1356 break;
1358 case 3:
1359 dirent_type = SMBC_IPC_SHARE;
1360 break;
1362 default:
1363 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1364 break;
1366 ZERO_STRUCTP(dir->dir_list);
1369 else dirent_type = dir->dir_type;
1371 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1373 /* An error occurred, what do we do? */
1374 /* FIXME: Add some code here */
1380 static void
1381 dir_list_fn(file_info *finfo, const char *mask, void *state)
1384 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1385 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1387 /* Handle an error ... */
1389 /* FIXME: Add some code ... */
1395 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1397 fstring server, share, user, password, workgroup;
1398 pstring path;
1399 SMBCSRV *srv = NULL;
1400 SMBCFILE *dir = NULL;
1401 struct in_addr rem_ip;
1402 int slot = 0;
1404 if (!context || !context->_initialized) {
1406 errno = EINVAL;
1407 return NULL;
1411 if (!fname) {
1413 errno = EINVAL;
1414 return NULL;
1418 if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1420 errno = EINVAL;
1421 return NULL;
1425 if (user[0] == (char)0) pstrcpy(user, context->user);
1427 pstrcpy(workgroup, context->workgroup);
1429 dir = malloc(sizeof(*dir));
1431 if (!dir) {
1433 errno = ENOMEM;
1434 return NULL;
1438 ZERO_STRUCTP(dir);
1440 dir->cli_fd = 0;
1441 dir->fname = strdup(fname);
1442 dir->srv = NULL;
1443 dir->offset = 0;
1444 dir->file = False;
1445 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1447 if (server[0] == (char)0) {
1449 if (share[0] != (char)0 || path[0] != (char)0) {
1451 errno = EINVAL;
1452 if (dir) {
1453 SAFE_FREE(dir->fname);
1454 SAFE_FREE(dir);
1456 return NULL;
1460 /* We have server and share and path empty ... so list the workgroups */
1461 /* first try to get the LMB for our workgroup, and if that fails, */
1462 /* try the DMB */
1464 if (!(resolve_name(context->workgroup, &rem_ip, 0x1d) ||
1465 resolve_name(context->workgroup, &rem_ip, 0x1b))) {
1467 errno = EINVAL; /* Something wrong with smb.conf? */
1468 return NULL;
1472 dir->dir_type = SMBC_WORKGROUP;
1474 /* find the name of the server ... */
1476 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1478 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server));
1479 errno = EINVAL;
1480 return NULL;
1485 * Get a connection to IPC$ on the server if we do not already have one
1488 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1490 if (!srv) {
1492 if (dir) {
1493 SAFE_FREE(dir->fname);
1494 SAFE_FREE(dir);
1497 return NULL;
1500 ZERO_STRUCTP(dir->dir_end);
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->_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->_initialized) {
1694 errno = EINVAL;
1695 return -1;
1699 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1701 errno = EBADF;
1702 return -1;
1706 smbc_remove_dir(dir); /* Clean it up */
1708 DLIST_REMOVE(context->_files, dir);
1710 if (dir) {
1712 SAFE_FREE(dir->fname);
1713 SAFE_FREE(dir); /* Free the space too */
1717 return 0;
1722 * Routine to get a directory entry
1725 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1727 struct smbc_dirent *dirp, *dirent;
1729 /* Check that all is ok first ... */
1731 if (!context || !context->_initialized) {
1733 errno = EINVAL;
1734 return NULL;
1738 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1740 errno = EBADF;
1741 return NULL;
1745 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1747 errno = ENOTDIR;
1748 return NULL;
1752 if (!dir->dir_next)
1753 return NULL;
1754 else {
1756 dirent = dir->dir_next->dirent;
1758 if (!dirent) {
1760 errno = ENOENT;
1761 return NULL;
1765 /* Hmmm, do I even need to copy it? */
1767 memcpy(context->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1768 dirp = (struct smbc_dirent *)context->_dirent;
1769 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1770 dir->dir_next = dir->dir_next->next;
1772 return (struct smbc_dirent *)context->_dirent;
1778 * Routine to get directory entries
1781 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1783 struct smbc_dir_list *dirlist;
1784 int rem = count, reqd;
1785 char *ndir = (char *)dirp;
1787 /* Check that all is ok first ... */
1789 if (!context || !context->_initialized) {
1791 errno = EINVAL;
1792 return -1;
1796 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1798 errno = EBADF;
1799 return -1;
1803 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1805 errno = ENOTDIR;
1806 return -1;
1811 * Now, retrieve the number of entries that will fit in what was passed
1812 * We have to figure out if the info is in the list, or we need to
1813 * send a request to the server to get the info.
1816 while ((dirlist = dir->dir_next)) {
1817 struct smbc_dirent *dirent;
1819 if (!dirlist->dirent) {
1821 errno = ENOENT; /* Bad error */
1822 return -1;
1826 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1827 dirlist->dirent->commentlen + 1))) {
1829 if (rem < count) { /* We managed to copy something */
1831 errno = 0;
1832 return count - rem;
1835 else { /* Nothing copied ... */
1837 errno = EINVAL; /* Not enough space ... */
1838 return -1;
1844 dirent = dirlist->dirent;
1846 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1848 ((struct smbc_dirent *)ndir)->comment =
1849 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1851 ndir += reqd;
1853 rem -= reqd;
1855 dir->dir_next = dirlist = dirlist -> next;
1858 if (rem == count)
1859 return 0;
1860 else
1861 return count - rem;
1866 * Routine to create a directory ...
1869 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1871 SMBCSRV *srv;
1872 fstring server, share, user, password, workgroup;
1873 pstring path;
1875 if (!context || !context->_initialized) {
1877 errno = EINVAL;
1878 return -1;
1882 if (!fname) {
1884 errno = EINVAL;
1885 return -1;
1889 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1891 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1893 if (user[0] == (char)0) pstrcpy(user, context->user);
1895 pstrcpy(workgroup, context->workgroup);
1897 srv = smbc_server(context, server, share, workgroup, user, password);
1899 if (!srv) {
1901 return -1; /* errno set by smbc_server */
1905 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1907 mode = aDIR | aRONLY;
1910 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1912 if (strcmp(path, "\\") == 0) {
1914 mode = aDIR | aRONLY;
1917 else {
1919 mode = aRONLY;
1920 smbc_stat_printjob(srv, path, &size, &m_time);
1921 c_time = a_time = m_time;
1924 else { */
1926 if (!cli_mkdir(&srv->cli, path)) {
1928 errno = smbc_errno(context, &srv->cli);
1929 return -1;
1933 return 0;
1938 * Our list function simply checks to see if a directory is not empty
1941 static int smbc_rmdir_dirempty = True;
1943 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
1946 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
1947 smbc_rmdir_dirempty = False;
1952 * Routine to remove a directory
1955 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
1957 SMBCSRV *srv;
1958 fstring server, share, user, password, workgroup;
1959 pstring path;
1961 if (!context || !context->_initialized) {
1963 errno = EINVAL;
1964 return -1;
1968 if (!fname) {
1970 errno = EINVAL;
1971 return -1;
1975 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1977 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1979 if (user[0] == (char)0) pstrcpy(user, context->user);
1981 pstrcpy(workgroup, context->workgroup);
1983 srv = smbc_server(context, server, share, workgroup, user, password);
1985 if (!srv) {
1987 return -1; /* errno set by smbc_server */
1991 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1993 mode = aDIR | aRONLY;
1996 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1998 if (strcmp(path, "\\") == 0) {
2000 mode = aDIR | aRONLY;
2003 else {
2005 mode = aRONLY;
2006 smbc_stat_printjob(srv, path, &size, &m_time);
2007 c_time = a_time = m_time;
2010 else { */
2012 if (!cli_rmdir(&srv->cli, path)) {
2014 errno = smbc_errno(context, &srv->cli);
2016 if (errno == EACCES) { /* Check if the dir empty or not */
2018 pstring lpath; /* Local storage to avoid buffer overflows */
2020 smbc_rmdir_dirempty = True; /* Make this so ... */
2022 pstrcpy(lpath, path);
2023 pstrcat(lpath, "\\*");
2025 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2026 NULL) < 0) {
2028 /* Fix errno to ignore latest error ... */
2030 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2031 smbc_errno(context, &srv->cli)));
2032 errno = EACCES;
2036 if (smbc_rmdir_dirempty)
2037 errno = EACCES;
2038 else
2039 errno = ENOTEMPTY;
2043 return -1;
2047 return 0;
2052 * Routine to return the current directory position
2055 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2058 if (!context || !context->_initialized) {
2060 errno = EINVAL;
2061 return -1;
2065 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
2067 errno = EBADF;
2068 return -1;
2072 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2074 errno = ENOTDIR;
2075 return -1;
2079 return (off_t) dir->dir_next;
2084 * A routine to run down the list and see if the entry is OK
2087 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2088 struct smbc_dirent *dirent)
2091 /* Run down the list looking for what we want */
2093 if (dirent) {
2095 struct smbc_dir_list *tmp = list;
2097 while (tmp) {
2099 if (tmp->dirent == dirent)
2100 return tmp;
2102 tmp = tmp->next;
2108 return NULL; /* Not found, or an error */
2114 * Routine to seek on a directory
2117 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2119 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2120 struct smbc_dir_list *list_ent = NULL;
2122 if (!context || !context->_initialized) {
2124 errno = EINVAL;
2125 return -1;
2129 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2131 errno = ENOTDIR;
2132 return -1;
2136 /* Now, check what we were passed and see if it is OK ... */
2138 if (dirent == NULL) { /* Seek to the begining of the list */
2140 dir->dir_next = dir->dir_list;
2141 return 0;
2145 /* Now, run down the list and make sure that the entry is OK */
2146 /* This may need to be changed if we change the format of the list */
2148 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2150 errno = EINVAL; /* Bad entry */
2151 return -1;
2155 dir->dir_next = list_ent;
2157 return 0;
2162 * Routine to fstat a dir
2165 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2168 if (context || !context->_initialized) {
2170 errno = EINVAL;
2171 return -1;
2175 /* No code yet ... */
2177 return 0;
2182 * Open a print file to be written to by other calls
2185 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2187 fstring server, share, user, password;
2188 pstring path;
2190 if (!context || context->_initialized) {
2192 errno = EINVAL;
2193 return NULL;
2197 if (!fname) {
2199 errno = EINVAL;
2200 return NULL;
2204 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2206 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2208 /* What if the path is empty, or the file exists? */
2210 return context->open(context, fname, O_WRONLY, 666);
2215 * Routine to print a file on a remote server ...
2217 * We open the file, which we assume to be on a remote server, and then
2218 * copy it to a print file on the share specified by printq.
2221 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2223 SMBCFILE *fid1, *fid2;
2224 int bytes, saverr, tot_bytes = 0;
2225 char buf[4096];
2227 if (!c_file || !c_file->_initialized || !c_print ||
2228 !c_print->_initialized) {
2230 errno = EINVAL;
2231 return -1;
2235 if (!fname && !printq) {
2237 errno = EINVAL;
2238 return -1;
2242 /* Try to open the file for reading ... */
2244 if ((fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2246 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2247 return -1; /* smbc_open sets errno */
2251 /* Now, try to open the printer file for writing */
2253 if ((fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2255 saverr = errno; /* Save errno */
2256 c_file->close(c_file, fid1);
2257 errno = saverr;
2258 return -1;
2262 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2264 tot_bytes += bytes;
2266 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2268 saverr = errno;
2269 c_file->close(c_file, fid1);
2270 c_print->close(c_print, fid2);
2271 errno = saverr;
2277 saverr = errno;
2279 c_file->close(c_file, fid1); /* We have to close these anyway */
2280 c_print->close(c_print, fid2);
2282 if (bytes < 0) {
2284 errno = saverr;
2285 return -1;
2289 return tot_bytes;
2294 * Routine to list print jobs on a printer share ...
2297 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, void (*fn)(struct print_job_info *))
2299 SMBCSRV *srv;
2300 fstring server, share, user, password, workgroup;
2301 pstring path;
2303 if (!context || !context->_initialized) {
2305 errno = EINVAL;
2306 return -1;
2310 if (!fname) {
2312 errno = EINVAL;
2313 return -1;
2317 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2319 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2321 if (user[0] == (char)0) pstrcpy(user, context->user);
2323 pstrcpy(workgroup, context->workgroup);
2325 srv = smbc_server(context, server, share, workgroup, user, password);
2327 if (!srv) {
2329 return -1; /* errno set by smbc_server */
2333 if (cli_print_queue(&srv->cli, fn) < 0) {
2335 errno = smbc_errno(context, &srv->cli);
2336 return -1;
2340 return 0;
2345 * Delete a print job from a remote printer share
2348 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2350 SMBCSRV *srv;
2351 fstring server, share, user, password, workgroup;
2352 pstring path;
2353 int err;
2355 if (!context || !context->_initialized) {
2357 errno = EINVAL;
2358 return -1;
2362 if (!fname) {
2364 errno = EINVAL;
2365 return -1;
2369 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2371 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2373 if (user[0] == (char)0) pstrcpy(user, context->user);
2375 pstrcpy(workgroup, context->workgroup);
2377 srv = smbc_server(context, server, share, workgroup, user, password);
2379 if (!srv) {
2381 return -1; /* errno set by smbc_server */
2385 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2387 if (err < 0)
2388 errno = smbc_errno(context, &srv->cli);
2389 else if (err == ERRnosuchprintjob)
2390 errno = EINVAL;
2391 return -1;
2395 return 0;
2400 * Get a new empty handle to fill in with your own info
2402 SMBCCTX * smbc_new_context(void)
2404 SMBCCTX * context;
2406 context = malloc(sizeof(*context));
2407 if (!context) {
2408 errno = ENOMEM;
2409 return NULL;
2412 ZERO_STRUCTP(context);
2414 /* ADD REASONABLE DEFAULTS */
2415 context->debug = 0;
2416 context->timeout = 20000; /* 20 seconds */
2418 context->open = smbc_open_ctx;
2419 context->creat = smbc_creat_ctx;
2420 context->read = smbc_read_ctx;
2421 context->write = smbc_write_ctx;
2422 context->close = smbc_close_ctx;
2423 context->unlink = smbc_unlink_ctx;
2424 context->rename = smbc_rename_ctx;
2425 context->lseek = smbc_lseek_ctx;
2426 context->stat = smbc_stat_ctx;
2427 context->fstat = smbc_fstat_ctx;
2428 context->opendir = smbc_opendir_ctx;
2429 context->closedir = smbc_closedir_ctx;
2430 context->readdir = smbc_readdir_ctx;
2431 context->getdents = smbc_getdents_ctx;
2432 context->mkdir = smbc_mkdir_ctx;
2433 context->rmdir = smbc_rmdir_ctx;
2434 context->telldir = smbc_telldir_ctx;
2435 context->lseekdir = smbc_lseekdir_ctx;
2436 context->fstatdir = smbc_fstatdir_ctx;
2437 context->open_print_job = smbc_open_print_job_ctx;
2438 context->print_file = smbc_print_file_ctx;
2439 context->list_print_jobs = smbc_list_print_jobs_ctx;
2440 context->unlink_print_job = smbc_unlink_print_job_ctx;
2442 context->callbacks.check_server_fn = smbc_check_server;
2443 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2445 smbc_default_cache_functions(context);
2447 return context;
2451 * Free a context
2453 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2454 * and thus you'll be leaking memory if not handled properly.
2457 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2459 if (!context) {
2460 errno = EBADF;
2461 return 1;
2464 if (shutdown_ctx) {
2465 SMBCFILE * f;
2466 DEBUG(1,("Performing aggressive shutdown.\n"));
2468 f = context->_files;
2469 while (f) {
2470 context->close(context, f);
2471 f = f->next;
2473 context->_files = NULL;
2475 /* First try to remove the servers the nice way. */
2476 if (context->callbacks.purge_cached_fn(context)) {
2477 SMBCSRV * s;
2478 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2479 s = context->_servers;
2480 while (s) {
2481 cli_shutdown(&s->cli);
2482 context->callbacks.remove_cached_srv_fn(context, s);
2483 SAFE_FREE(s);
2484 s = s->next;
2486 context->_servers = NULL;
2489 else {
2490 /* This is the polite way */
2491 if (context->callbacks.purge_cached_fn(context)) {
2492 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2493 errno = EBUSY;
2494 return 1;
2496 if (context->_servers) {
2497 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2498 errno = EBUSY;
2499 return 1;
2501 if (context->_files) {
2502 DEBUG(1, ("Active files in context, free_context failed.\n"));
2503 errno = EBUSY;
2504 return 1;
2508 /* Things we have to clean up */
2509 SAFE_FREE(context->workgroup);
2510 SAFE_FREE(context->netbios_name);
2511 SAFE_FREE(context->user);
2513 DEBUG(3, ("Context %p succesfully freed\n", context));
2514 SAFE_FREE(context);
2515 return 0;
2520 * Initialise the library etc
2522 * We accept a struct containing handle information.
2523 * valid values for info->debug from 0 to 100,
2524 * and insist that info->fn must be non-null.
2526 SMBCCTX * smbc_init_context(SMBCCTX * context)
2528 pstring conf;
2529 int pid;
2530 char *user = NULL, *home = NULL;
2532 if (!context) {
2533 errno = EBADF;
2534 return NULL;
2537 /* Do not initialise the same client twice */
2538 if (context->_initialized) {
2539 return 0;
2542 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2544 errno = EINVAL;
2545 return NULL;
2549 if (!smbc_initialized) {
2550 /* Do some library wide intialisations the first time we get called */
2552 /* Do we still need this ? */
2553 DEBUGLEVEL = 10;
2555 setup_logging( "libsmbclient", False);
2557 /* Here we would open the smb.conf file if needed ... */
2559 home = getenv("HOME");
2561 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2563 load_interfaces(); /* Load the list of interfaces ... */
2565 in_client = True; /* FIXME, make a param */
2567 if (!lp_load(conf, True, False, False)) {
2570 * Hmmm, what the hell do we do here ... we could not parse the
2571 * config file ... We must return an error ... and keep info around
2572 * about why we failed
2575 errno = ENOENT; /* FIXME: Figure out the correct error response */
2576 return NULL;
2579 reopen_logs(); /* Get logging working ... */
2582 * Block SIGPIPE (from lib/util_sock.c: write())
2583 * It is not needed and should not stop execution
2585 BlockSignals(True, SIGPIPE);
2587 /* Done with one-time initialisation */
2588 smbc_initialized = 1;
2592 if (!context->user) {
2594 * FIXME: Is this the best way to get the user info?
2596 user = getenv("USER");
2597 /* walk around as "guest" if no username can be found */
2598 if (!user) context->user = strdup("guest");
2599 else context->user = strdup(user);
2602 if (!context->netbios_name) {
2604 * We try to get our netbios name from the config. If that fails we fall
2605 * back on constructing our netbios name from our hostname etc
2607 if (global_myname) {
2608 context->netbios_name = strdup(global_myname);
2610 else {
2612 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2614 pid = sys_getpid();
2615 context->netbios_name = malloc(17);
2616 if (!context->netbios_name) {
2617 errno = ENOMEM;
2618 return NULL;
2620 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2623 DEBUG(0,("Using netbios name %s.\n", context->netbios_name));
2626 if (!context->workgroup) {
2627 if (lp_workgroup()) {
2628 context->workgroup = strdup(lp_workgroup());
2630 else {
2631 /* TODO: Think about a decent default workgroup */
2632 context->workgroup = strdup("samba");
2635 DEBUG(0,("Using workgroup %s.\n", context->workgroup));
2639 * I think we can do this more than once for the same name without
2640 * being shot but who am I? -- Tom
2641 * Actually, we probably don't want to register a name,
2642 * but one day the user might want to be able to do so. RJS
2644 if (0) {
2645 name_register_wins(context->netbios_name, 0);
2648 /* shortest timeout is 1 second */
2649 if (context->timeout > 0 && context->timeout < 1000)
2650 context->timeout = 1000;
2653 * FIXME: Should we check the function pointers here?
2656 context->_initialized = 1;
2658 return context;