sync'ing up for 3.0alpha20 release
[Samba.git] / source / libsmb / libsmbclient.c
blob44cba611d2f147ad1f1b9c2a2cb78559b854e2cd
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 "libsmbclient.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 list server_table if it's unused.
223 * On success, 0 is returned. 1 is returned if the server could not be removed.
225 * Also useable outside libsmbclient
227 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
229 SMBCFILE * file;
231 /* are we being fooled ? */
232 if (!context || !context->_initialized || !srv) return 1;
235 /* Check all open files/directories for a relation with this server */
236 for (file = context->_files; file; file=file->next) {
237 if (file->srv == srv) {
238 /* Still used */
239 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
240 srv, file));
241 return 1;
245 DLIST_REMOVE(context->_servers, srv);
247 cli_shutdown(&srv->cli);
249 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
251 context->callbacks.remove_cached_srv_fn(context, srv);
253 SAFE_FREE(srv);
255 return 0;
259 * Connect to a server, possibly on an existing connection
261 * Here, what we want to do is: If the server and username
262 * match an existing connection, reuse that, otherwise, establish a
263 * new connection.
265 * If we have to create a new connection, call the auth_fn to get the
266 * info we need, unless the username and password were passed in.
269 SMBCSRV *smbc_server(SMBCCTX *context,
270 char *server, char *share,
271 char *workgroup, char *username,
272 char *password)
274 SMBCSRV *srv=NULL;
275 int auth_called = 0;
276 struct cli_state c;
277 struct nmb_name called, calling;
278 char *p, *server_n = server;
279 fstring group;
280 pstring ipenv;
281 struct in_addr ip;
282 int tried_reverse = 0;
284 zero_ip(&ip);
285 ZERO_STRUCT(c);
287 if (server[0] == 0) {
288 errno = EPERM;
289 return NULL;
292 check_server_cache:
294 srv = context->callbacks.get_cached_srv_fn(context, server, share,
295 workgroup, username);
297 if (!auth_called && !srv && (!username[0] || !password[0])) {
298 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
299 username, sizeof(fstring), password, sizeof(fstring));
301 * However, smbc_auth_fn may have picked up info relating to an
302 * existing connection, so try for an existing connection again ...
304 auth_called = 1;
305 goto check_server_cache;
309 if (srv) {
310 if (context->callbacks.check_server_fn(context, srv)) {
312 * This server is no good anymore
313 * Try to remove it and check for more possible servers in the cache
315 if (context->callbacks.remove_unused_server_fn(context, srv)) {
317 * We could not remove the server completely, remove it from the cache
318 * so we will not get it again. It will be removed when the last file/dir
319 * is closed.
321 context->callbacks.remove_cached_srv_fn(context, srv);
325 * Maybe there are more cached connections to this server
327 goto check_server_cache;
329 return srv;
332 make_nmb_name(&calling, context->netbios_name, 0x0);
333 make_nmb_name(&called , server, 0x20);
335 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
337 if ((p=strchr_m(server_n,'#')) &&
338 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
340 fstrcpy(group, server_n);
341 p = strchr_m(group,'#');
342 *p = 0;
346 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
348 again:
349 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
351 zero_ip(&ip);
353 /* have to open a new connection */
354 if (!cli_initialise(&c)) {
355 errno = ENOENT;
356 return NULL;
359 c.timeout = context->timeout;
361 if (!cli_connect(&c, server_n, &ip)) {
362 cli_shutdown(&c);
363 errno = ENOENT;
364 return NULL;
367 if (!cli_session_request(&c, &calling, &called)) {
368 cli_shutdown(&c);
369 if (strcmp(called.name, "*SMBSERVER")) {
370 make_nmb_name(&called , "*SMBSERVER", 0x20);
371 goto again;
373 else { /* Try one more time, but ensure we don't loop */
375 /* Only try this if server is an IP address ... */
377 if (is_ipaddress(server) && !tried_reverse) {
378 fstring remote_name;
379 struct in_addr rem_ip;
381 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
382 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
383 errno = ENOENT;
384 return NULL;
387 tried_reverse++; /* Yuck */
389 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
390 make_nmb_name(&called, remote_name, 0x20);
391 goto again;
397 errno = ENOENT;
398 return NULL;
401 DEBUG(4,(" session request ok\n"));
403 if (!cli_negprot(&c)) {
404 cli_shutdown(&c);
405 errno = ENOENT;
406 return NULL;
409 if (!cli_session_setup(&c, username,
410 password, strlen(password),
411 password, strlen(password),
412 workgroup) &&
413 /* try an anonymous login if it failed */
414 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
415 cli_shutdown(&c);
416 errno = EPERM;
417 return NULL;
420 DEBUG(4,(" session setup ok\n"));
422 if (!cli_send_tconX(&c, share, "?????",
423 password, strlen(password)+1)) {
424 errno = smbc_errno(context, &c);
425 cli_shutdown(&c);
426 return NULL;
429 DEBUG(4,(" tconx ok\n"));
432 * Ok, we have got a nice connection
433 * Let's find a free server_fd
436 srv = (SMBCSRV *)malloc(sizeof(*srv));
437 if (!srv) {
438 errno = ENOMEM;
439 goto failed;
442 ZERO_STRUCTP(srv);
443 srv->cli = c;
444 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
446 /* now add it to the cache (internal or external) */
447 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
448 DEBUG(3, (" Failed to add server to cache\n"));
449 goto failed;
453 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
454 server, share, srv));
456 return srv;
458 failed:
459 cli_shutdown(&c);
460 if (!srv) return NULL;
462 SAFE_FREE(srv);
463 return NULL;
467 * Routine to open() a file ...
470 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
472 fstring server, share, user, password, workgroup;
473 pstring path;
474 SMBCSRV *srv = NULL;
475 SMBCFILE *file = NULL;
476 int fd;
478 if (!context || !context->_initialized) {
480 errno = EINVAL; /* Best I can think of ... */
481 return NULL;
485 if (!fname) {
487 errno = EINVAL;
488 return NULL;
492 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
494 if (user[0] == (char)0) pstrcpy(user, context->user);
496 pstrcpy(workgroup, context->workgroup);
498 srv = smbc_server(context, server, share, workgroup, user, password);
500 if (!srv) {
502 if (errno == EPERM) errno = EACCES;
503 return NULL; /* smbc_server sets errno */
507 /* Hmmm, the test for a directory is suspect here ... FIXME */
509 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
511 fd = -1;
514 else {
516 file = malloc(sizeof(SMBCFILE));
518 if (!file) {
520 errno = ENOMEM;
521 return NULL;
525 ZERO_STRUCTP(file);
527 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
529 /* Handle the error ... */
531 SAFE_FREE(file);
532 errno = smbc_errno(context, &srv->cli);
533 return NULL;
537 /* Fill in file struct */
539 file->cli_fd = fd;
540 file->fname = strdup(fname);
541 file->srv = srv;
542 file->offset = 0;
543 file->file = True;
545 DLIST_ADD(context->_files, file);
546 return file;
550 /* Check if opendir needed ... */
552 if (fd == -1) {
553 int eno = 0;
555 eno = smbc_errno(context, &srv->cli);
556 file = context->opendir(context, fname);
557 if (!file) errno = eno;
558 return file;
562 errno = EINVAL; /* FIXME, correct errno ? */
563 return NULL;
568 * Routine to create a file
571 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
573 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
576 if (!context || !context->_initialized) {
578 errno = EINVAL;
579 return NULL;
583 return smbc_open_ctx(context, path, creat_bits, mode);
587 * Routine to read() a file ...
590 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
592 int ret;
594 if (!context || !context->_initialized) {
596 errno = EINVAL;
597 return -1;
601 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
603 if (!file || !DLIST_CONTAINS(context->_files, file)) {
605 errno = EBADF;
606 return -1;
610 /* Check that the buffer exists ... */
612 if (buf == NULL) {
614 errno = EINVAL;
615 return -1;
619 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
621 if (ret < 0) {
623 errno = smbc_errno(context, &file->srv->cli);
624 return -1;
628 file->offset += ret;
630 DEBUG(4, (" --> %d\n", ret));
632 return ret; /* Success, ret bytes of data ... */
637 * Routine to write() a file ...
640 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
642 int ret;
644 if (!context || context->_initialized) {
646 errno = EINVAL;
647 return -1;
651 if (!file || !DLIST_CONTAINS(context->_files, file)) {
653 errno = EBADF;
654 return -1;
658 /* Check that the buffer exists ... */
660 if (buf == NULL) {
662 errno = EINVAL;
663 return -1;
667 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
669 if (ret <= 0) {
671 errno = smbc_errno(context, &file->srv->cli);
672 return -1;
676 file->offset += ret;
678 return ret; /* Success, 0 bytes of data ... */
682 * Routine to close() a file ...
685 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
687 SMBCSRV *srv;
689 if (!context || !context->_initialized) {
691 errno = EINVAL;
692 return -1;
696 if (!file || !DLIST_CONTAINS(context->_files, file)) {
698 errno = EBADF;
699 return -1;
703 /* IS a dir ... */
704 if (!file->file) {
706 return context->closedir(context, file);
710 if (!cli_close(&file->srv->cli, file->cli_fd)) {
712 DEBUG(3, ("cli_close failed on %s. purging server.\n",
713 file->fname));
714 /* Deallocate slot and remove the server
715 * from the server cache if unused */
716 errno = smbc_errno(context, &file->srv->cli);
717 srv = file->srv;
718 DLIST_REMOVE(context->_files, file);
719 SAFE_FREE(file->fname);
720 SAFE_FREE(file);
721 context->callbacks.remove_unused_server_fn(context, srv);
723 return -1;
727 if (!file->file) {
729 return context->closedir(context, file);
733 if (!cli_close(&file->srv->cli, file->cli_fd)) {
734 DEBUG(3, ("cli_close failed on %s. purging server.\n",
735 file->fname));
736 /* Deallocate slot and remove the server
737 * from the server cache if unused */
738 errno = smbc_errno(context, &file->srv->cli);
739 srv = file->srv;
740 DLIST_REMOVE(context->_files, file);
741 SAFE_FREE(file->fname);
742 SAFE_FREE(file);
743 context->callbacks.remove_unused_server_fn(context, srv);
745 return -1;
748 DLIST_REMOVE(context->_files, file);
749 SAFE_FREE(file->fname);
750 SAFE_FREE(file);
752 return 0;
756 * Get info from an SMB server on a file. Use a qpathinfo call first
757 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
759 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
760 uint16 *mode, size_t *size,
761 time_t *c_time, time_t *a_time, time_t *m_time,
762 SMB_INO_T *ino)
765 if (!context || !context->_initialized) {
767 errno = EINVAL;
768 return -1;
772 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
774 if (!srv->no_pathinfo2 &&
775 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
776 size, mode, ino)) return True;
778 /* if this is NT then don't bother with the getatr */
779 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
781 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
782 a_time = c_time = m_time;
783 srv->no_pathinfo2 = True;
784 return True;
787 return False;
792 * Routine to unlink() a file
795 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
797 fstring server, share, user, password, workgroup;
798 pstring path;
799 SMBCSRV *srv = NULL;
801 if (!context || context->_initialized) {
803 errno = EINVAL; /* Best I can think of ... */
804 return -1;
808 if (!fname) {
810 errno = EINVAL;
811 return -1;
815 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
817 if (user[0] == (char)0) pstrcpy(user, context->user);
819 pstrcpy(workgroup, context->workgroup);
821 srv = smbc_server(context, server, share, workgroup, user, password);
823 if (!srv) {
825 return -1; /* smbc_server sets errno */
829 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
831 int job = smbc_stat_printjob(srv, path, NULL, NULL);
832 if (job == -1) {
834 return -1;
837 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
840 return -1;
843 } else */
845 if (!cli_unlink(&srv->cli, path)) {
847 errno = smbc_errno(context, &srv->cli);
849 if (errno == EACCES) { /* Check if the file is a directory */
851 int saverr = errno;
852 size_t size = 0;
853 uint16 mode = 0;
854 time_t m_time = 0, a_time = 0, c_time = 0;
855 SMB_INO_T ino = 0;
857 if (!smbc_getatr(context, srv, path, &mode, &size,
858 &c_time, &a_time, &m_time, &ino)) {
860 /* Hmmm, bad error ... What? */
862 errno = smbc_errno(context, &srv->cli);
863 return -1;
866 else {
868 if (IS_DOS_DIR(mode))
869 errno = EISDIR;
870 else
871 errno = saverr; /* Restore this */
876 return -1;
880 return 0; /* Success ... */
885 * Routine to rename() a file
888 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
889 SMBCCTX *ncontext, const char *nname)
891 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
892 pstring path1, path2;
893 SMBCSRV *srv = NULL;
895 if (!ocontext || !ncontext ||
896 !ocontext->_initialized || !ncontext->_initialized) {
898 errno = EINVAL; /* Best I can think of ... */
899 return -1;
903 if (!oname || !nname) {
905 errno = EINVAL;
906 return -1;
910 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
912 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
914 if (user1[0] == (char)0) pstrcpy(user1, ocontext->user);
916 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
918 if (user2[0] == (char)0) pstrcpy(user2, ncontext->user);
920 if (strcmp(server1, server2) || strcmp(share1, share2) ||
921 strcmp(user1, user2)) {
923 /* Can't rename across file systems, or users?? */
925 errno = EXDEV;
926 return -1;
930 pstrcpy(workgroup, ocontext->workgroup);
931 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
932 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
933 if (!srv) {
935 return -1;
939 if (!cli_rename(&srv->cli, path1, path2)) {
940 int eno = smbc_errno(ocontext, &srv->cli);
942 if (eno != EEXIST ||
943 !cli_unlink(&srv->cli, path2) ||
944 !cli_rename(&srv->cli, path1, path2)) {
946 errno = eno;
947 return -1;
952 return 0; /* Success */
957 * A routine to lseek() a file
960 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
962 size_t size;
964 if (!context || !context->_initialized) {
966 errno = EINVAL;
967 return -1;
971 if (!file || !DLIST_CONTAINS(context->_files, file)) {
973 errno = EBADF;
974 return -1;
978 if (!file->file) {
980 errno = EINVAL;
981 return -1; /* Can't lseek a dir ... */
985 switch (whence) {
986 case SEEK_SET:
987 file->offset = offset;
988 break;
990 case SEEK_CUR:
991 file->offset += offset;
992 break;
994 case SEEK_END:
995 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
996 NULL, NULL, NULL) &&
997 !cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
998 NULL)) {
1000 errno = EINVAL;
1001 return -1;
1003 file->offset = size + offset;
1004 break;
1006 default:
1007 errno = EINVAL;
1008 break;
1012 return file->offset;
1017 * Generate an inode number from file name for those things that need it
1020 static
1021 ino_t smbc_inode(SMBCCTX *context, const char *name)
1024 if (!context || !context->_initialized) {
1026 errno = EINVAL;
1027 return -1;
1031 if (!*name) return 2; /* FIXME, why 2 ??? */
1032 return (ino_t)str_checksum(name);
1037 * Routine to put basic stat info into a stat structure ... Used by stat and
1038 * fstat below.
1041 static
1042 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1045 st->st_mode = 0;
1047 if (IS_DOS_DIR(mode)) {
1048 st->st_mode = SMBC_DIR_MODE;
1049 } else {
1050 st->st_mode = SMBC_FILE_MODE;
1053 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1054 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1055 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1056 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1058 st->st_size = size;
1059 st->st_blksize = 512;
1060 st->st_blocks = (size+511)/512;
1061 st->st_uid = getuid();
1062 st->st_gid = getgid();
1064 if (IS_DOS_DIR(mode)) {
1065 st->st_nlink = 2;
1066 } else {
1067 st->st_nlink = 1;
1070 if (st->st_ino == 0) {
1071 st->st_ino = smbc_inode(context, fname);
1074 return True; /* FIXME: Is this needed ? */
1079 * Routine to stat a file given a name
1082 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1084 SMBCSRV *srv;
1085 fstring server, share, user, password, workgroup;
1086 pstring path;
1087 time_t m_time = 0, a_time = 0, c_time = 0;
1088 size_t size = 0;
1089 uint16 mode = 0;
1090 SMB_INO_T ino = 0;
1092 if (!context || !context->_initialized) {
1094 errno = EINVAL; /* Best I can think of ... */
1095 return -1;
1099 if (!fname) {
1101 errno = EINVAL;
1102 return -1;
1106 DEBUG(4, ("smbc_stat(%s)\n", fname));
1108 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1110 if (user[0] == (char)0) pstrcpy(user, context->user);
1112 pstrcpy(workgroup, context->workgroup);
1114 srv = smbc_server(context, server, share, workgroup, user, password);
1116 if (!srv) {
1118 return -1; /* errno set by smbc_server */
1122 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1124 mode = aDIR | aRONLY;
1127 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1129 if (strcmp(path, "\\") == 0) {
1131 mode = aDIR | aRONLY;
1134 else {
1136 mode = aRONLY;
1137 smbc_stat_printjob(srv, path, &size, &m_time);
1138 c_time = a_time = m_time;
1141 else { */
1143 if (!smbc_getatr(context, srv, path, &mode, &size,
1144 &c_time, &a_time, &m_time, &ino)) {
1146 errno = smbc_errno(context, &srv->cli);
1147 return -1;
1151 st->st_ino = ino;
1153 smbc_setup_stat(context, st, path, size, mode);
1155 st->st_atime = a_time;
1156 st->st_ctime = c_time;
1157 st->st_mtime = m_time;
1158 st->st_dev = srv->dev;
1160 return 0;
1165 * Routine to stat a file given an fd
1168 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1170 time_t c_time, a_time, m_time;
1171 size_t size;
1172 uint16 mode;
1173 SMB_INO_T ino = 0;
1175 if (!context || !context->_initialized) {
1177 errno = EINVAL;
1178 return -1;
1182 if (!file || !DLIST_CONTAINS(context->_files, file)) {
1184 errno = EBADF;
1185 return -1;
1189 if (!file->file) {
1191 return context->fstatdir(context, file, st);
1195 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1196 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1197 !cli_getattrE(&file->srv->cli, file->cli_fd,
1198 &mode, &size, &c_time, &a_time, &m_time)) {
1200 errno = EINVAL;
1201 return -1;
1205 st->st_ino = ino;
1207 smbc_setup_stat(context, st, file->fname, size, mode);
1209 st->st_atime = a_time;
1210 st->st_ctime = c_time;
1211 st->st_mtime = m_time;
1212 st->st_dev = file->srv->dev;
1214 return 0;
1219 * Routine to open a directory
1221 * We want to allow:
1223 * smb: which should list all the workgroups available
1224 * smb:workgroup
1225 * smb:workgroup//server
1226 * smb://server
1227 * smb://server/share
1228 * smb://<IP-addr> which should list shares on server
1229 * smb://<IP-addr>/share which should list files on share
1232 static void smbc_remove_dir(SMBCFILE *dir)
1234 struct smbc_dir_list *d,*f;
1236 d = dir->dir_list;
1237 while (d) {
1239 f = d; d = d->next;
1241 SAFE_FREE(f->dirent);
1242 SAFE_FREE(f);
1246 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1250 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1252 struct smbc_dirent *dirent;
1253 int size;
1256 * Allocate space for the dirent, which must be increased by the
1257 * size of the name and the comment and 1 for the null on the comment.
1258 * The null on the name is already accounted for.
1261 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1262 (comment?strlen(comment):0) + 1;
1264 dirent = malloc(size);
1266 if (!dirent) {
1268 dir->dir_error = ENOMEM;
1269 return -1;
1273 ZERO_STRUCTP(dirent);
1275 ZERO_STRUCTP(dirent);
1278 if (dir->dir_list == NULL) {
1280 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1281 if (!dir->dir_list) {
1283 SAFE_FREE(dirent);
1284 dir->dir_error = ENOMEM;
1285 return -1;
1288 ZERO_STRUCTP(dir->dir_list);
1290 dir->dir_end = dir->dir_next = dir->dir_list;
1293 else {
1295 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1297 if (!dir->dir_end->next) {
1299 SAFE_FREE(dirent);
1300 dir->dir_error = ENOMEM;
1301 return -1;
1304 ZERO_STRUCTP(dir->dir_end->next);
1306 dir->dir_end = dir->dir_end->next;
1310 dir->dir_end->next = NULL;
1311 dir->dir_end->dirent = dirent;
1313 dirent->smbc_type = type;
1314 dirent->namelen = (name?strlen(name):0);
1315 dirent->commentlen = (comment?strlen(comment):0);
1316 dirent->dirlen = size;
1318 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1320 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1321 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1323 return 0;
1327 static void
1328 list_fn(const char *name, uint32 type, const char *comment, void *state)
1330 SMBCFILE *dir = (SMBCFILE *)state;
1331 int dirent_type;
1333 /* We need to process the type a little ... */
1335 if (dir->dir_type == SMBC_FILE_SHARE) {
1337 switch (type) {
1338 case 0: /* Directory tree */
1339 dirent_type = SMBC_FILE_SHARE;
1340 break;
1342 case 1:
1343 dirent_type = SMBC_PRINTER_SHARE;
1344 break;
1346 case 2:
1347 dirent_type = SMBC_COMMS_SHARE;
1348 break;
1350 case 3:
1351 dirent_type = SMBC_IPC_SHARE;
1352 break;
1354 default:
1355 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1356 break;
1358 ZERO_STRUCTP(dir->dir_list);
1361 else dirent_type = dir->dir_type;
1363 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1365 /* An error occurred, what do we do? */
1366 /* FIXME: Add some code here */
1372 static void
1373 dir_list_fn(file_info *finfo, const char *mask, void *state)
1376 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1377 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1379 /* Handle an error ... */
1381 /* FIXME: Add some code ... */
1387 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1389 fstring server, share, user, password, workgroup;
1390 pstring path;
1391 SMBCSRV *srv = NULL;
1392 SMBCFILE *dir = NULL;
1393 struct in_addr rem_ip;
1394 int slot = 0;
1396 if (!context || !context->_initialized) {
1398 errno = EINVAL;
1399 return NULL;
1403 if (!fname) {
1405 errno = EINVAL;
1406 return NULL;
1410 if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1412 errno = EINVAL;
1413 return NULL;
1417 if (user[0] == (char)0) pstrcpy(user, context->user);
1419 pstrcpy(workgroup, context->workgroup);
1421 dir = malloc(sizeof(*dir));
1423 if (!dir) {
1425 errno = ENOMEM;
1426 return NULL;
1430 ZERO_STRUCTP(dir);
1432 dir->cli_fd = 0;
1433 dir->fname = strdup(fname);
1434 dir->srv = NULL;
1435 dir->offset = 0;
1436 dir->file = False;
1437 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1439 if (server[0] == (char)0) {
1441 if (share[0] != (char)0 || path[0] != (char)0) {
1443 errno = EINVAL;
1444 if (dir) {
1445 SAFE_FREE(dir->fname);
1446 SAFE_FREE(dir);
1448 return NULL;
1452 /* We have server and share and path empty ... so list the workgroups */
1453 /* first try to get the LMB for our workgroup, and if that fails, */
1454 /* try the DMB */
1456 if (!(resolve_name(context->workgroup, &rem_ip, 0x1d) ||
1457 resolve_name(context->workgroup, &rem_ip, 0x1b))) {
1459 errno = EINVAL; /* Something wrong with smb.conf? */
1460 return NULL;
1464 dir->dir_type = SMBC_WORKGROUP;
1466 /* find the name of the server ... */
1468 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1470 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server));
1471 errno = EINVAL;
1472 return NULL;
1477 * Get a connection to IPC$ on the server if we do not already have one
1480 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1482 if (!srv) {
1484 if (dir) {
1485 SAFE_FREE(dir->fname);
1486 SAFE_FREE(dir);
1489 return NULL;
1492 ZERO_STRUCTP(dir->dir_end);
1494 dir->srv = srv;
1496 /* Now, list the stuff ... */
1498 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1499 (void *)dir)) {
1501 if (dir) {
1502 SAFE_FREE(dir->fname);
1503 SAFE_FREE(dir);
1505 errno = cli_errno(&srv->cli);
1507 return NULL;
1511 else { /* Server not an empty string ... Check the rest and see what gives */
1513 if (share[0] == (char)0) {
1515 if (path[0] != (char)0) { /* Should not have empty share with path */
1517 errno = EINVAL;
1518 if (dir) {
1519 SAFE_FREE(dir->fname);
1520 SAFE_FREE(dir);
1522 return NULL;
1526 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1527 /* However, we check to see if <server> is an IP address first */
1529 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1530 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
1531 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
1532 pstring buserver;
1534 dir->dir_type = SMBC_SERVER;
1537 * Get the backup list ...
1541 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1543 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
1544 errno = EPERM; /* FIXME, is this correct */
1545 return NULL;
1550 * Get a connection to IPC$ on the server if we do not already have one
1553 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
1555 if (!srv) {
1557 if (dir) {
1558 SAFE_FREE(dir->fname);
1559 SAFE_FREE(dir);
1561 return NULL;
1565 dir->srv = srv;
1567 /* Now, list the servers ... */
1569 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1570 (void *)dir)) {
1572 if (dir) {
1573 SAFE_FREE(dir->fname);
1574 SAFE_FREE(dir);
1576 errno = cli_errno(&srv->cli);
1577 return NULL;
1582 else {
1584 if (resolve_name(server, &rem_ip, 0x20)) {
1586 /* Now, list the shares ... */
1588 dir->dir_type = SMBC_FILE_SHARE;
1590 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1592 if (!srv) {
1594 if (dir) {
1595 SAFE_FREE(dir->fname);
1596 SAFE_FREE(dir);
1598 return NULL;
1602 dir->srv = srv;
1604 /* Now, list the servers ... */
1606 if (cli_RNetShareEnum(&srv->cli, list_fn,
1607 (void *)dir) < 0) {
1609 errno = cli_errno(&srv->cli);
1610 if (dir) {
1611 SAFE_FREE(dir->fname);
1612 SAFE_FREE(dir);
1614 return NULL;
1619 else {
1621 errno = ENODEV; /* Neither the workgroup nor server exists */
1622 if (dir) {
1623 SAFE_FREE(dir->fname);
1624 SAFE_FREE(dir);
1626 return NULL;
1633 else { /* The server and share are specified ... work from there ... */
1635 /* Well, we connect to the server and list the directory */
1637 dir->dir_type = SMBC_FILE_SHARE;
1639 srv = smbc_server(context, server, share, workgroup, user, password);
1641 if (!srv) {
1643 if (dir) {
1644 SAFE_FREE(dir->fname);
1645 SAFE_FREE(dir);
1647 return NULL;
1651 dir->srv = srv;
1653 /* Now, list the files ... */
1655 pstrcat(path, "\\*");
1657 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1658 (void *)dir) < 0) {
1660 if (dir) {
1661 SAFE_FREE(dir->fname);
1662 SAFE_FREE(dir);
1664 errno = smbc_errno(context, &srv->cli);
1665 return NULL;
1672 DLIST_ADD(context->_files, dir);
1673 return dir;
1678 * Routine to close a directory
1681 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1684 if (!context || !context->_initialized) {
1686 errno = EINVAL;
1687 return -1;
1691 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1693 errno = EBADF;
1694 return -1;
1698 smbc_remove_dir(dir); /* Clean it up */
1700 DLIST_REMOVE(context->_files, dir);
1702 if (dir) {
1704 SAFE_FREE(dir->fname);
1705 SAFE_FREE(dir); /* Free the space too */
1709 return 0;
1714 * Routine to get a directory entry
1717 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1719 struct smbc_dirent *dirp, *dirent;
1721 /* Check that all is ok first ... */
1723 if (!context || !context->_initialized) {
1725 errno = EINVAL;
1726 return NULL;
1730 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1732 errno = EBADF;
1733 return NULL;
1737 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1739 errno = ENOTDIR;
1740 return NULL;
1744 if (!dir->dir_next)
1745 return NULL;
1746 else {
1748 dirent = dir->dir_next->dirent;
1750 if (!dirent) {
1752 errno = ENOENT;
1753 return NULL;
1757 /* Hmmm, do I even need to copy it? */
1759 memcpy(context->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1760 dirp = (struct smbc_dirent *)context->_dirent;
1761 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1762 dir->dir_next = dir->dir_next->next;
1764 return (struct smbc_dirent *)context->_dirent;
1770 * Routine to get directory entries
1773 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1775 struct smbc_dir_list *dirlist;
1776 int rem = count, reqd;
1777 char *ndir = (char *)dirp;
1779 /* Check that all is ok first ... */
1781 if (!context || !context->_initialized) {
1783 errno = EINVAL;
1784 return -1;
1788 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1790 errno = EBADF;
1791 return -1;
1795 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1797 errno = ENOTDIR;
1798 return -1;
1803 * Now, retrieve the number of entries that will fit in what was passed
1804 * We have to figure out if the info is in the list, or we need to
1805 * send a request to the server to get the info.
1808 while ((dirlist = dir->dir_next)) {
1809 struct smbc_dirent *dirent;
1811 if (!dirlist->dirent) {
1813 errno = ENOENT; /* Bad error */
1814 return -1;
1818 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1819 dirlist->dirent->commentlen + 1))) {
1821 if (rem < count) { /* We managed to copy something */
1823 errno = 0;
1824 return count - rem;
1827 else { /* Nothing copied ... */
1829 errno = EINVAL; /* Not enough space ... */
1830 return -1;
1836 dirent = dirlist->dirent;
1838 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1840 ((struct smbc_dirent *)ndir)->comment =
1841 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1843 ndir += reqd;
1845 rem -= reqd;
1847 dir->dir_next = dirlist = dirlist -> next;
1850 if (rem == count)
1851 return 0;
1852 else
1853 return count - rem;
1858 * Routine to create a directory ...
1861 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1863 SMBCSRV *srv;
1864 fstring server, share, user, password, workgroup;
1865 pstring path;
1867 if (!context || !context->_initialized) {
1869 errno = EINVAL;
1870 return -1;
1874 if (!fname) {
1876 errno = EINVAL;
1877 return -1;
1881 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1883 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1885 if (user[0] == (char)0) pstrcpy(user, context->user);
1887 pstrcpy(workgroup, context->workgroup);
1889 srv = smbc_server(context, server, share, workgroup, user, password);
1891 if (!srv) {
1893 return -1; /* errno set by smbc_server */
1897 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1899 mode = aDIR | aRONLY;
1902 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1904 if (strcmp(path, "\\") == 0) {
1906 mode = aDIR | aRONLY;
1909 else {
1911 mode = aRONLY;
1912 smbc_stat_printjob(srv, path, &size, &m_time);
1913 c_time = a_time = m_time;
1916 else { */
1918 if (!cli_mkdir(&srv->cli, path)) {
1920 errno = smbc_errno(context, &srv->cli);
1921 return -1;
1925 return 0;
1930 * Our list function simply checks to see if a directory is not empty
1933 static int smbc_rmdir_dirempty = True;
1935 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
1938 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
1939 smbc_rmdir_dirempty = False;
1944 * Routine to remove a directory
1947 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
1949 SMBCSRV *srv;
1950 fstring server, share, user, password, workgroup;
1951 pstring path;
1953 if (!context || !context->_initialized) {
1955 errno = EINVAL;
1956 return -1;
1960 if (!fname) {
1962 errno = EINVAL;
1963 return -1;
1967 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1969 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1971 if (user[0] == (char)0) pstrcpy(user, context->user);
1973 pstrcpy(workgroup, context->workgroup);
1975 srv = smbc_server(context, server, share, workgroup, user, password);
1977 if (!srv) {
1979 return -1; /* errno set by smbc_server */
1983 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1985 mode = aDIR | aRONLY;
1988 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1990 if (strcmp(path, "\\") == 0) {
1992 mode = aDIR | aRONLY;
1995 else {
1997 mode = aRONLY;
1998 smbc_stat_printjob(srv, path, &size, &m_time);
1999 c_time = a_time = m_time;
2002 else { */
2004 if (!cli_rmdir(&srv->cli, path)) {
2006 errno = smbc_errno(context, &srv->cli);
2008 if (errno == EACCES) { /* Check if the dir empty or not */
2010 pstring lpath; /* Local storage to avoid buffer overflows */
2012 smbc_rmdir_dirempty = True; /* Make this so ... */
2014 pstrcpy(lpath, path);
2015 pstrcat(lpath, "\\*");
2017 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2018 NULL) < 0) {
2020 /* Fix errno to ignore latest error ... */
2022 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2023 smbc_errno(context, &srv->cli)));
2024 errno = EACCES;
2028 if (smbc_rmdir_dirempty)
2029 errno = EACCES;
2030 else
2031 errno = ENOTEMPTY;
2035 return -1;
2039 return 0;
2044 * Routine to return the current directory position
2047 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2050 if (!context || !context->_initialized) {
2052 errno = EINVAL;
2053 return -1;
2057 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
2059 errno = EBADF;
2060 return -1;
2064 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2066 errno = ENOTDIR;
2067 return -1;
2071 return (off_t) dir->dir_next;
2076 * A routine to run down the list and see if the entry is OK
2079 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2080 struct smbc_dirent *dirent)
2083 /* Run down the list looking for what we want */
2085 if (dirent) {
2087 struct smbc_dir_list *tmp = list;
2089 while (tmp) {
2091 if (tmp->dirent == dirent)
2092 return tmp;
2094 tmp = tmp->next;
2100 return NULL; /* Not found, or an error */
2106 * Routine to seek on a directory
2109 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2111 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2112 struct smbc_dir_list *list_ent = NULL;
2114 if (!context || !context->_initialized) {
2116 errno = EINVAL;
2117 return -1;
2121 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2123 errno = ENOTDIR;
2124 return -1;
2128 /* Now, check what we were passed and see if it is OK ... */
2130 if (dirent == NULL) { /* Seek to the begining of the list */
2132 dir->dir_next = dir->dir_list;
2133 return 0;
2137 /* Now, run down the list and make sure that the entry is OK */
2138 /* This may need to be changed if we change the format of the list */
2140 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2142 errno = EINVAL; /* Bad entry */
2143 return -1;
2147 dir->dir_next = list_ent;
2149 return 0;
2154 * Routine to fstat a dir
2157 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2160 if (context || !context->_initialized) {
2162 errno = EINVAL;
2163 return -1;
2167 /* No code yet ... */
2169 return 0;
2174 * Open a print file to be written to by other calls
2177 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2179 fstring server, share, user, password;
2180 pstring path;
2182 if (!context || context->_initialized) {
2184 errno = EINVAL;
2185 return NULL;
2189 if (!fname) {
2191 errno = EINVAL;
2192 return NULL;
2196 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2198 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2200 /* What if the path is empty, or the file exists? */
2202 return context->open(context, fname, O_WRONLY, 666);
2207 * Routine to print a file on a remote server ...
2209 * We open the file, which we assume to be on a remote server, and then
2210 * copy it to a print file on the share specified by printq.
2213 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2215 SMBCFILE *fid1, *fid2;
2216 int bytes, saverr, tot_bytes = 0;
2217 char buf[4096];
2219 if (!c_file || !c_file->_initialized || !c_print ||
2220 !c_print->_initialized) {
2222 errno = EINVAL;
2223 return -1;
2227 if (!fname && !printq) {
2229 errno = EINVAL;
2230 return -1;
2234 /* Try to open the file for reading ... */
2236 if ((fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2238 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2239 return -1; /* smbc_open sets errno */
2243 /* Now, try to open the printer file for writing */
2245 if ((fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2247 saverr = errno; /* Save errno */
2248 c_file->close(c_file, fid1);
2249 errno = saverr;
2250 return -1;
2254 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2256 tot_bytes += bytes;
2258 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2260 saverr = errno;
2261 c_file->close(c_file, fid1);
2262 c_print->close(c_print, fid2);
2263 errno = saverr;
2269 saverr = errno;
2271 c_file->close(c_file, fid1); /* We have to close these anyway */
2272 c_print->close(c_print, fid2);
2274 if (bytes < 0) {
2276 errno = saverr;
2277 return -1;
2281 return tot_bytes;
2286 * Routine to list print jobs on a printer share ...
2289 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, void (*fn)(struct print_job_info *))
2291 SMBCSRV *srv;
2292 fstring server, share, user, password, workgroup;
2293 pstring path;
2295 if (!context || !context->_initialized) {
2297 errno = EINVAL;
2298 return -1;
2302 if (!fname) {
2304 errno = EINVAL;
2305 return -1;
2309 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2311 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2313 if (user[0] == (char)0) pstrcpy(user, context->user);
2315 pstrcpy(workgroup, context->workgroup);
2317 srv = smbc_server(context, server, share, workgroup, user, password);
2319 if (!srv) {
2321 return -1; /* errno set by smbc_server */
2325 if (cli_print_queue(&srv->cli, fn) < 0) {
2327 errno = smbc_errno(context, &srv->cli);
2328 return -1;
2332 return 0;
2337 * Delete a print job from a remote printer share
2340 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2342 SMBCSRV *srv;
2343 fstring server, share, user, password, workgroup;
2344 pstring path;
2345 int err;
2347 if (!context || !context->_initialized) {
2349 errno = EINVAL;
2350 return -1;
2354 if (!fname) {
2356 errno = EINVAL;
2357 return -1;
2361 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2363 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2365 if (user[0] == (char)0) pstrcpy(user, context->user);
2367 pstrcpy(workgroup, context->workgroup);
2369 srv = smbc_server(context, server, share, workgroup, user, password);
2371 if (!srv) {
2373 return -1; /* errno set by smbc_server */
2377 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2379 if (err < 0)
2380 errno = smbc_errno(context, &srv->cli);
2381 else if (err == ERRnosuchprintjob)
2382 errno = EINVAL;
2383 return -1;
2387 return 0;
2392 * Get a new empty handle to fill in with your own info
2394 SMBCCTX * smbc_new_context(void)
2396 SMBCCTX * context;
2398 context = malloc(sizeof(*context));
2399 if (!context) {
2400 errno = ENOMEM;
2401 return NULL;
2404 ZERO_STRUCTP(context);
2406 /* ADD REASONABLE DEFAULTS */
2407 context->debug = 0;
2408 context->timeout = 20000; /* 20 seconds */
2410 context->open = smbc_open_ctx;
2411 context->creat = smbc_creat_ctx;
2412 context->read = smbc_read_ctx;
2413 context->write = smbc_write_ctx;
2414 context->close = smbc_close_ctx;
2415 context->unlink = smbc_unlink_ctx;
2416 context->rename = smbc_rename_ctx;
2417 context->lseek = smbc_lseek_ctx;
2418 context->stat = smbc_stat_ctx;
2419 context->fstat = smbc_fstat_ctx;
2420 context->opendir = smbc_opendir_ctx;
2421 context->closedir = smbc_closedir_ctx;
2422 context->readdir = smbc_readdir_ctx;
2423 context->getdents = smbc_getdents_ctx;
2424 context->mkdir = smbc_mkdir_ctx;
2425 context->rmdir = smbc_rmdir_ctx;
2426 context->telldir = smbc_telldir_ctx;
2427 context->lseekdir = smbc_lseekdir_ctx;
2428 context->fstatdir = smbc_fstatdir_ctx;
2429 context->open_print_job = smbc_open_print_job_ctx;
2430 context->print_file = smbc_print_file_ctx;
2431 context->list_print_jobs = smbc_list_print_jobs_ctx;
2432 context->unlink_print_job = smbc_unlink_print_job_ctx;
2434 context->callbacks.check_server_fn = smbc_check_server;
2435 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2437 smbc_default_cache_functions(context);
2439 return context;
2443 * Free a context
2445 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2446 * and thus you'll be leaking memory if not handled properly.
2449 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2451 if (!context) {
2452 errno = EBADF;
2453 return 1;
2456 if (shutdown_ctx) {
2457 SMBCFILE * f;
2458 DEBUG(1,("Performing aggressive shutdown.\n"));
2460 f = context->_files;
2461 while (f) {
2462 context->close(context, f);
2463 f = f->next;
2465 context->_files = NULL;
2467 /* First try to remove the servers the nice way. */
2468 if (context->callbacks.purge_cached_fn(context)) {
2469 SMBCSRV * s;
2470 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2471 s = context->_servers;
2472 while (s) {
2473 cli_shutdown(&s->cli);
2474 context->callbacks.remove_cached_srv_fn(context, s);
2475 SAFE_FREE(s);
2476 s = s->next;
2478 context->_servers = NULL;
2481 else {
2482 /* This is the polite way */
2483 if (context->callbacks.purge_cached_fn(context)) {
2484 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2485 errno = EBUSY;
2486 return 1;
2488 if (context->_servers) {
2489 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2490 errno = EBUSY;
2491 return 1;
2493 if (context->_files) {
2494 DEBUG(1, ("Active files in context, free_context failed.\n"));
2495 errno = EBUSY;
2496 return 1;
2500 /* Things we have to clean up */
2501 SAFE_FREE(context->workgroup);
2502 SAFE_FREE(context->netbios_name);
2503 SAFE_FREE(context->user);
2505 DEBUG(3, ("Context %p succesfully freed\n", context));
2506 SAFE_FREE(context);
2507 return 0;
2512 * Initialise the library etc
2514 * We accept a struct containing handle information.
2515 * valid values for info->debug from 0 to 100,
2516 * and insist that info->fn must be non-null.
2518 SMBCCTX * smbc_init_context(SMBCCTX * context)
2520 pstring conf;
2521 int pid;
2522 char *user = NULL, *home = NULL;
2524 if (!context) {
2525 errno = EBADF;
2526 return NULL;
2529 /* Do not initialise the same client twice */
2530 if (context->_initialized) {
2531 return 0;
2534 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2536 errno = EINVAL;
2537 return NULL;
2541 if (!smbc_initialized) {
2542 /* Do some library wide intialisations the first time we get called */
2544 /* Do we still need this ? */
2545 DEBUGLEVEL = 10;
2547 setup_logging( "libsmbclient", False);
2549 /* Here we would open the smb.conf file if needed ... */
2551 home = getenv("HOME");
2553 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2555 load_interfaces(); /* Load the list of interfaces ... */
2557 in_client = True; /* FIXME, make a param */
2559 if (!lp_load(conf, True, False, False)) {
2562 * Hmmm, what the hell do we do here ... we could not parse the
2563 * config file ... We must return an error ... and keep info around
2564 * about why we failed
2567 errno = ENOENT; /* FIXME: Figure out the correct error response */
2568 return NULL;
2571 reopen_logs(); /* Get logging working ... */
2574 * Block SIGPIPE (from lib/util_sock.c: write())
2575 * It is not needed and should not stop execution
2577 BlockSignals(True, SIGPIPE);
2579 /* Done with one-time initialisation */
2580 smbc_initialized = 1;
2584 if (!context->user) {
2586 * FIXME: Is this the best way to get the user info?
2588 user = getenv("USER");
2589 /* walk around as "guest" if no username can be found */
2590 if (!user) context->user = strdup("guest");
2591 else context->user = strdup(user);
2594 if (!context->netbios_name) {
2596 * We try to get our netbios name from the config. If that fails we fall
2597 * back on constructing our netbios name from our hostname etc
2599 if (global_myname) {
2600 context->netbios_name = strdup(global_myname);
2602 else {
2604 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2606 pid = sys_getpid();
2607 context->netbios_name = malloc(17);
2608 if (!context->netbios_name) {
2609 errno = ENOMEM;
2610 return NULL;
2612 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2615 DEBUG(0,("Using netbios name %s.\n", context->netbios_name));
2618 if (!context->workgroup) {
2619 if (lp_workgroup()) {
2620 context->workgroup = strdup(lp_workgroup());
2622 else {
2623 /* TODO: Think about a decent default workgroup */
2624 context->workgroup = strdup("samba");
2627 DEBUG(0,("Using workgroup %s.\n", context->workgroup));
2629 /* shortest timeout is 1 second */
2630 if (context->timeout > 0 && context->timeout < 1000)
2631 context->timeout = 1000;
2634 * FIXME: Should we check the function pointers here?
2637 context->_initialized = 1;
2639 return context;