Add some castiness for Don McCall.
[Samba/gebeck_regimport.git] / source3 / libsmb / libsmbclient.c
blob92353d8c308236f128313ea77fba56f63cd0a0da
1 /*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 #include "../include/libsmb_internal.h"
29 * Functions exported by libsmb_cache.c that we need here
31 int smbc_default_cache_functions(SMBCCTX *context);
33 /*
34 * check if an element is part of the list.
35 * FIXME: Does not belong here !
36 * Can anyone put this in a macro in dlinklist.h ?
37 * -- Tom
39 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
40 if (!p || !list) return False;
41 do {
42 if (p == list) return True;
43 list = list->next;
44 } while (list);
45 return False;
48 extern BOOL in_client;
51 * Is the logging working / configfile read ?
53 static int smbc_initialized = 0;
56 * Function to parse a path and turn it into components
58 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
60 * smb:// means show all the workgroups
61 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
62 * else, if name<20> exists, list all shares for server ...
65 static const char *smbc_prefix = "smb:";
67 static int
68 smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
69 char *user, char *password) /* FIXME, lengths of strings */
71 static pstring s;
72 pstring userinfo;
73 const char *p;
74 char *q, *r;
75 int len;
77 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
78 pstrcpy(s, fname);
80 /* clean_fname(s); causing problems ... */
82 /* see if it has the right prefix */
83 len = strlen(smbc_prefix);
84 if (strncmp(s,smbc_prefix,len) ||
85 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
87 p = s + len;
89 /* Watch the test below, we are testing to see if we should exit */
91 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
93 return -1;
97 p += 2; /* Skip the // or \\ */
99 if (*p == (char)0)
100 return 0;
102 if (*p == '/') {
104 strncpy(server, context->workgroup,
105 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
106 return 0;
111 * ok, its for us. Now parse out the server, share etc.
113 * However, we want to parse out [[domain;]user[:password]@] if it
114 * exists ...
117 /* check that '@' occurs before '/', if '/' exists at all */
118 q = strchr_m(p, '@');
119 r = strchr_m(p, '/');
120 if (q && (!r || q < r)) {
121 pstring username, passwd, domain;
122 const char *u = userinfo;
124 next_token(&p, userinfo, "@", sizeof(fstring));
126 username[0] = passwd[0] = domain[0] = 0;
128 if (strchr_m(u, ';')) {
130 next_token(&u, domain, ";", sizeof(fstring));
134 if (strchr_m(u, ':')) {
136 next_token(&u, username, ":", sizeof(fstring));
138 pstrcpy(passwd, u);
141 else {
143 pstrcpy(username, u);
147 if (username[0])
148 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
150 if (passwd[0])
151 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
155 if (!next_token(&p, server, "/", sizeof(fstring))) {
157 return -1;
161 if (*p == (char)0) return 0; /* That's it ... */
163 if (!next_token(&p, share, "/", sizeof(fstring))) {
165 return -1;
169 pstrcpy(path, p);
171 all_string_sub(path, "/", "\\", 0);
173 return 0;
177 * Convert an SMB error into a UNIX error ...
180 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
182 int ret = cli_errno(c);
184 if (cli_is_dos_error(c)) {
185 uint8 eclass;
186 uint32 ecode;
188 cli_dos_error(c, &eclass, &ecode);
190 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
191 (int)eclass, (int)ecode, (int)ecode, ret));
192 } else {
193 NTSTATUS status;
195 status = cli_nt_error(c);
197 DEBUG(3,("smbc errno %s -> %d\n",
198 nt_errstr(status), ret));
201 return ret;
205 * Check a server_fd.
206 * returns 0 if the server is in shape. Returns 1 on error
208 * Also useable outside libsmbclient to enable external cache
209 * to do some checks too.
211 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
213 if ( send_keepalive(server->cli.fd) == False )
214 return 1;
216 /* connection is ok */
217 return 0;
221 * Remove a server from the cached server list it's unused.
222 * On success, 0 is returned. 1 is returned if the server could not be removed.
224 * Also useable outside libsmbclient
226 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
228 SMBCFILE * file;
230 /* are we being fooled ? */
231 if (!context || !context->internal ||
232 !context->internal->_initialized || !srv) return 1;
235 /* Check all open files/directories for a relation with this server */
236 for (file = context->internal->_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->internal->_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->internal ||
479 !context->internal->_initialized) {
481 errno = EINVAL; /* Best I can think of ... */
482 return NULL;
486 if (!fname) {
488 errno = EINVAL;
489 return NULL;
493 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
495 if (user[0] == (char)0) fstrcpy(user, context->user);
497 fstrcpy(workgroup, context->workgroup);
499 srv = smbc_server(context, server, share, workgroup, user, password);
501 if (!srv) {
503 if (errno == EPERM) errno = EACCES;
504 return NULL; /* smbc_server sets errno */
508 /* Hmmm, the test for a directory is suspect here ... FIXME */
510 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
512 fd = -1;
515 else {
517 file = malloc(sizeof(SMBCFILE));
519 if (!file) {
521 errno = ENOMEM;
522 return NULL;
526 ZERO_STRUCTP(file);
528 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
530 /* Handle the error ... */
532 SAFE_FREE(file);
533 errno = smbc_errno(context, &srv->cli);
534 return NULL;
538 /* Fill in file struct */
540 file->cli_fd = fd;
541 file->fname = strdup(fname);
542 file->srv = srv;
543 file->offset = 0;
544 file->file = True;
546 DLIST_ADD(context->internal->_files, file);
547 return file;
551 /* Check if opendir needed ... */
553 if (fd == -1) {
554 int eno = 0;
556 eno = smbc_errno(context, &srv->cli);
557 file = context->opendir(context, fname);
558 if (!file) errno = eno;
559 return file;
563 errno = EINVAL; /* FIXME, correct errno ? */
564 return NULL;
569 * Routine to create a file
572 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
574 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
577 if (!context || !context->internal ||
578 !context->internal->_initialized) {
580 errno = EINVAL;
581 return NULL;
585 return smbc_open_ctx(context, path, creat_bits, mode);
589 * Routine to read() a file ...
592 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
594 int ret;
596 if (!context || !context->internal ||
597 !context->internal->_initialized) {
599 errno = EINVAL;
600 return -1;
604 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
606 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
608 errno = EBADF;
609 return -1;
613 /* Check that the buffer exists ... */
615 if (buf == NULL) {
617 errno = EINVAL;
618 return -1;
622 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
624 if (ret < 0) {
626 errno = smbc_errno(context, &file->srv->cli);
627 return -1;
631 file->offset += ret;
633 DEBUG(4, (" --> %d\n", ret));
635 return ret; /* Success, ret bytes of data ... */
640 * Routine to write() a file ...
643 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
645 int ret;
647 if (!context || !context->internal ||
648 !context->internal->_initialized) {
650 errno = EINVAL;
651 return -1;
655 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
657 errno = EBADF;
658 return -1;
662 /* Check that the buffer exists ... */
664 if (buf == NULL) {
666 errno = EINVAL;
667 return -1;
671 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
673 if (ret <= 0) {
675 errno = smbc_errno(context, &file->srv->cli);
676 return -1;
680 file->offset += ret;
682 return ret; /* Success, 0 bytes of data ... */
686 * Routine to close() a file ...
689 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
691 SMBCSRV *srv;
693 if (!context || !context->internal ||
694 !context->internal->_initialized) {
696 errno = EINVAL;
697 return -1;
701 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
703 errno = EBADF;
704 return -1;
708 /* IS a dir ... */
709 if (!file->file) {
711 return context->closedir(context, file);
715 if (!cli_close(&file->srv->cli, file->cli_fd)) {
717 DEBUG(3, ("cli_close failed on %s. purging server.\n",
718 file->fname));
719 /* Deallocate slot and remove the server
720 * from the server cache if unused */
721 errno = smbc_errno(context, &file->srv->cli);
722 srv = file->srv;
723 DLIST_REMOVE(context->internal->_files, file);
724 SAFE_FREE(file->fname);
725 SAFE_FREE(file);
726 context->callbacks.remove_unused_server_fn(context, srv);
728 return -1;
732 if (!file->file) {
734 return context->closedir(context, file);
738 if (!cli_close(&file->srv->cli, file->cli_fd)) {
739 DEBUG(3, ("cli_close failed on %s. purging server.\n",
740 file->fname));
741 /* Deallocate slot and remove the server
742 * from the server cache if unused */
743 errno = smbc_errno(context, &file->srv->cli);
744 srv = file->srv;
745 DLIST_REMOVE(context->internal->_files, file);
746 SAFE_FREE(file->fname);
747 SAFE_FREE(file);
748 context->callbacks.remove_unused_server_fn(context, srv);
750 return -1;
753 DLIST_REMOVE(context->internal->_files, file);
754 SAFE_FREE(file->fname);
755 SAFE_FREE(file);
757 return 0;
761 * Get info from an SMB server on a file. Use a qpathinfo call first
762 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
764 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
765 uint16 *mode, size_t *size,
766 time_t *c_time, time_t *a_time, time_t *m_time,
767 SMB_INO_T *ino)
770 if (!context || !context->internal ||
771 !context->internal->_initialized) {
773 errno = EINVAL;
774 return -1;
778 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
780 if (!srv->no_pathinfo2 &&
781 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
782 size, mode, ino)) return True;
784 /* if this is NT then don't bother with the getatr */
785 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
787 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
788 a_time = c_time = m_time;
789 srv->no_pathinfo2 = True;
790 return True;
793 return False;
798 * Routine to unlink() a file
801 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
803 fstring server, share, user, password, workgroup;
804 pstring path;
805 SMBCSRV *srv = NULL;
807 if (!context || !context->internal ||
808 !context->internal->_initialized) {
810 errno = EINVAL; /* Best I can think of ... */
811 return -1;
815 if (!fname) {
817 errno = EINVAL;
818 return -1;
822 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
824 if (user[0] == (char)0) fstrcpy(user, context->user);
826 fstrcpy(workgroup, context->workgroup);
828 srv = smbc_server(context, server, share, workgroup, user, password);
830 if (!srv) {
832 return -1; /* smbc_server sets errno */
836 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
838 int job = smbc_stat_printjob(srv, path, NULL, NULL);
839 if (job == -1) {
841 return -1;
844 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
847 return -1;
850 } else */
852 if (!cli_unlink(&srv->cli, path)) {
854 errno = smbc_errno(context, &srv->cli);
856 if (errno == EACCES) { /* Check if the file is a directory */
858 int saverr = errno;
859 size_t size = 0;
860 uint16 mode = 0;
861 time_t m_time = 0, a_time = 0, c_time = 0;
862 SMB_INO_T ino = 0;
864 if (!smbc_getatr(context, srv, path, &mode, &size,
865 &c_time, &a_time, &m_time, &ino)) {
867 /* Hmmm, bad error ... What? */
869 errno = smbc_errno(context, &srv->cli);
870 return -1;
873 else {
875 if (IS_DOS_DIR(mode))
876 errno = EISDIR;
877 else
878 errno = saverr; /* Restore this */
883 return -1;
887 return 0; /* Success ... */
892 * Routine to rename() a file
895 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
896 SMBCCTX *ncontext, const char *nname)
898 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
899 pstring path1, path2;
900 SMBCSRV *srv = NULL;
902 if (!ocontext || !ncontext ||
903 !ocontext->internal || !ncontext->internal ||
904 !ocontext->internal->_initialized ||
905 !ncontext->internal->_initialized) {
907 errno = EINVAL; /* Best I can think of ... */
908 return -1;
912 if (!oname || !nname) {
914 errno = EINVAL;
915 return -1;
919 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
921 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
923 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
925 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
927 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
929 if (strcmp(server1, server2) || strcmp(share1, share2) ||
930 strcmp(user1, user2)) {
932 /* Can't rename across file systems, or users?? */
934 errno = EXDEV;
935 return -1;
939 fstrcpy(workgroup, ocontext->workgroup);
940 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
941 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
942 if (!srv) {
944 return -1;
948 if (!cli_rename(&srv->cli, path1, path2)) {
949 int eno = smbc_errno(ocontext, &srv->cli);
951 if (eno != EEXIST ||
952 !cli_unlink(&srv->cli, path2) ||
953 !cli_rename(&srv->cli, path1, path2)) {
955 errno = eno;
956 return -1;
961 return 0; /* Success */
966 * A routine to lseek() a file
969 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
971 size_t size;
973 if (!context || !context->internal ||
974 !context->internal->_initialized) {
976 errno = EINVAL;
977 return -1;
981 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
983 errno = EBADF;
984 return -1;
988 if (!file->file) {
990 errno = EINVAL;
991 return -1; /* Can't lseek a dir ... */
995 switch (whence) {
996 case SEEK_SET:
997 file->offset = offset;
998 break;
1000 case SEEK_CUR:
1001 file->offset += offset;
1002 break;
1004 case SEEK_END:
1005 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1006 NULL, NULL, NULL) &&
1007 !cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1008 NULL)) {
1010 errno = EINVAL;
1011 return -1;
1013 file->offset = size + offset;
1014 break;
1016 default:
1017 errno = EINVAL;
1018 break;
1022 return file->offset;
1027 * Generate an inode number from file name for those things that need it
1030 static
1031 ino_t smbc_inode(SMBCCTX *context, const char *name)
1034 if (!context || !context->internal ||
1035 !context->internal->_initialized) {
1037 errno = EINVAL;
1038 return -1;
1042 if (!*name) return 2; /* FIXME, why 2 ??? */
1043 return (ino_t)str_checksum(name);
1048 * Routine to put basic stat info into a stat structure ... Used by stat and
1049 * fstat below.
1052 static
1053 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1056 st->st_mode = 0;
1058 if (IS_DOS_DIR(mode)) {
1059 st->st_mode = SMBC_DIR_MODE;
1060 } else {
1061 st->st_mode = SMBC_FILE_MODE;
1064 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1065 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1066 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1067 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1069 st->st_size = size;
1070 st->st_blksize = 512;
1071 st->st_blocks = (size+511)/512;
1072 st->st_uid = getuid();
1073 st->st_gid = getgid();
1075 if (IS_DOS_DIR(mode)) {
1076 st->st_nlink = 2;
1077 } else {
1078 st->st_nlink = 1;
1081 if (st->st_ino == 0) {
1082 st->st_ino = smbc_inode(context, fname);
1085 return True; /* FIXME: Is this needed ? */
1090 * Routine to stat a file given a name
1093 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1095 SMBCSRV *srv;
1096 fstring server, share, user, password, workgroup;
1097 pstring path;
1098 time_t m_time = 0, a_time = 0, c_time = 0;
1099 size_t size = 0;
1100 uint16 mode = 0;
1101 SMB_INO_T ino = 0;
1103 if (!context || !context->internal ||
1104 !context->internal->_initialized) {
1106 errno = EINVAL; /* Best I can think of ... */
1107 return -1;
1111 if (!fname) {
1113 errno = EINVAL;
1114 return -1;
1118 DEBUG(4, ("smbc_stat(%s)\n", fname));
1120 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1122 if (user[0] == (char)0) fstrcpy(user, context->user);
1124 fstrcpy(workgroup, context->workgroup);
1126 srv = smbc_server(context, server, share, workgroup, user, password);
1128 if (!srv) {
1130 return -1; /* errno set by smbc_server */
1134 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1136 mode = aDIR | aRONLY;
1139 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1141 if (strcmp(path, "\\") == 0) {
1143 mode = aDIR | aRONLY;
1146 else {
1148 mode = aRONLY;
1149 smbc_stat_printjob(srv, path, &size, &m_time);
1150 c_time = a_time = m_time;
1153 else { */
1155 if (!smbc_getatr(context, srv, path, &mode, &size,
1156 &c_time, &a_time, &m_time, &ino)) {
1158 errno = smbc_errno(context, &srv->cli);
1159 return -1;
1163 st->st_ino = ino;
1165 smbc_setup_stat(context, st, path, size, mode);
1167 st->st_atime = a_time;
1168 st->st_ctime = c_time;
1169 st->st_mtime = m_time;
1170 st->st_dev = srv->dev;
1172 return 0;
1177 * Routine to stat a file given an fd
1180 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1182 time_t c_time, a_time, m_time;
1183 size_t size;
1184 uint16 mode;
1185 SMB_INO_T ino = 0;
1187 if (!context || !context->internal ||
1188 !context->internal->_initialized) {
1190 errno = EINVAL;
1191 return -1;
1195 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1197 errno = EBADF;
1198 return -1;
1202 if (!file->file) {
1204 return context->fstatdir(context, file, st);
1208 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1209 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1210 !cli_getattrE(&file->srv->cli, file->cli_fd,
1211 &mode, &size, &c_time, &a_time, &m_time)) {
1213 errno = EINVAL;
1214 return -1;
1218 st->st_ino = ino;
1220 smbc_setup_stat(context, st, file->fname, size, mode);
1222 st->st_atime = a_time;
1223 st->st_ctime = c_time;
1224 st->st_mtime = m_time;
1225 st->st_dev = file->srv->dev;
1227 return 0;
1232 * Routine to open a directory
1234 * We want to allow:
1236 * smb: which should list all the workgroups available
1237 * smb:workgroup
1238 * smb:workgroup//server
1239 * smb://server
1240 * smb://server/share
1241 * smb://<IP-addr> which should list shares on server
1242 * smb://<IP-addr>/share which should list files on share
1245 static void smbc_remove_dir(SMBCFILE *dir)
1247 struct smbc_dir_list *d,*f;
1249 d = dir->dir_list;
1250 while (d) {
1252 f = d; d = d->next;
1254 SAFE_FREE(f->dirent);
1255 SAFE_FREE(f);
1259 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1263 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1265 struct smbc_dirent *dirent;
1266 int size;
1269 * Allocate space for the dirent, which must be increased by the
1270 * size of the name and the comment and 1 for the null on the comment.
1271 * The null on the name is already accounted for.
1274 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1275 (comment?strlen(comment):0) + 1;
1277 dirent = malloc(size);
1279 if (!dirent) {
1281 dir->dir_error = ENOMEM;
1282 return -1;
1286 ZERO_STRUCTP(dirent);
1288 if (dir->dir_list == NULL) {
1290 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1291 if (!dir->dir_list) {
1293 SAFE_FREE(dirent);
1294 dir->dir_error = ENOMEM;
1295 return -1;
1298 ZERO_STRUCTP(dir->dir_list);
1300 dir->dir_end = dir->dir_next = dir->dir_list;
1303 else {
1305 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1307 if (!dir->dir_end->next) {
1309 SAFE_FREE(dirent);
1310 dir->dir_error = ENOMEM;
1311 return -1;
1314 ZERO_STRUCTP(dir->dir_end->next);
1316 dir->dir_end = dir->dir_end->next;
1320 dir->dir_end->next = NULL;
1321 dir->dir_end->dirent = dirent;
1323 dirent->smbc_type = type;
1324 dirent->namelen = (name?strlen(name):0);
1325 dirent->commentlen = (comment?strlen(comment):0);
1326 dirent->dirlen = size;
1328 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1330 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1331 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1333 return 0;
1337 static void
1338 list_fn(const char *name, uint32 type, const char *comment, void *state)
1340 SMBCFILE *dir = (SMBCFILE *)state;
1341 int dirent_type;
1343 /* We need to process the type a little ... */
1345 if (dir->dir_type == SMBC_FILE_SHARE) {
1347 switch (type) {
1348 case 0: /* Directory tree */
1349 dirent_type = SMBC_FILE_SHARE;
1350 break;
1352 case 1:
1353 dirent_type = SMBC_PRINTER_SHARE;
1354 break;
1356 case 2:
1357 dirent_type = SMBC_COMMS_SHARE;
1358 break;
1360 case 3:
1361 dirent_type = SMBC_IPC_SHARE;
1362 break;
1364 default:
1365 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1366 break;
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;
1403 if (!context || !context->internal ||
1404 !context->internal->_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) fstrcpy(user, context->user);
1427 fstrcpy(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;
1501 dir->srv = srv;
1503 /* Now, list the stuff ... */
1505 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1506 (void *)dir)) {
1508 if (dir) {
1509 SAFE_FREE(dir->fname);
1510 SAFE_FREE(dir);
1512 errno = cli_errno(&srv->cli);
1514 return NULL;
1518 else { /* Server not an empty string ... Check the rest and see what gives */
1520 if (share[0] == (char)0) {
1522 if (path[0] != (char)0) { /* Should not have empty share with path */
1524 errno = EINVAL;
1525 if (dir) {
1526 SAFE_FREE(dir->fname);
1527 SAFE_FREE(dir);
1529 return NULL;
1533 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1534 /* However, we check to see if <server> is an IP address first */
1536 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1537 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
1538 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
1539 pstring buserver;
1541 dir->dir_type = SMBC_SERVER;
1544 * Get the backup list ...
1548 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1550 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
1551 errno = EPERM; /* FIXME, is this correct */
1552 return NULL;
1557 * Get a connection to IPC$ on the server if we do not already have one
1560 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
1562 if (!srv) {
1564 if (dir) {
1565 SAFE_FREE(dir->fname);
1566 SAFE_FREE(dir);
1568 return NULL;
1572 dir->srv = srv;
1574 /* Now, list the servers ... */
1576 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1577 (void *)dir)) {
1579 if (dir) {
1580 SAFE_FREE(dir->fname);
1581 SAFE_FREE(dir);
1583 errno = cli_errno(&srv->cli);
1584 return NULL;
1589 else {
1591 if (resolve_name(server, &rem_ip, 0x20)) {
1593 /* Now, list the shares ... */
1595 dir->dir_type = SMBC_FILE_SHARE;
1597 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1599 if (!srv) {
1601 if (dir) {
1602 SAFE_FREE(dir->fname);
1603 SAFE_FREE(dir);
1605 return NULL;
1609 dir->srv = srv;
1611 /* Now, list the servers ... */
1613 if (cli_RNetShareEnum(&srv->cli, list_fn,
1614 (void *)dir) < 0) {
1616 errno = cli_errno(&srv->cli);
1617 if (dir) {
1618 SAFE_FREE(dir->fname);
1619 SAFE_FREE(dir);
1621 return NULL;
1626 else {
1628 errno = ENODEV; /* Neither the workgroup nor server exists */
1629 if (dir) {
1630 SAFE_FREE(dir->fname);
1631 SAFE_FREE(dir);
1633 return NULL;
1640 else { /* The server and share are specified ... work from there ... */
1642 /* Well, we connect to the server and list the directory */
1644 dir->dir_type = SMBC_FILE_SHARE;
1646 srv = smbc_server(context, server, share, workgroup, user, password);
1648 if (!srv) {
1650 if (dir) {
1651 SAFE_FREE(dir->fname);
1652 SAFE_FREE(dir);
1654 return NULL;
1658 dir->srv = srv;
1660 /* Now, list the files ... */
1662 pstrcat(path, "\\*");
1664 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1665 (void *)dir) < 0) {
1667 if (dir) {
1668 SAFE_FREE(dir->fname);
1669 SAFE_FREE(dir);
1671 errno = smbc_errno(context, &srv->cli);
1672 return NULL;
1679 DLIST_ADD(context->internal->_files, dir);
1680 return dir;
1685 * Routine to close a directory
1688 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1691 if (!context || !context->internal ||
1692 !context->internal->_initialized) {
1694 errno = EINVAL;
1695 return -1;
1699 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1701 errno = EBADF;
1702 return -1;
1706 smbc_remove_dir(dir); /* Clean it up */
1708 DLIST_REMOVE(context->internal->_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->internal ||
1732 !context->internal->_initialized) {
1734 errno = EINVAL;
1735 return NULL;
1739 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1741 errno = EBADF;
1742 return NULL;
1746 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1748 errno = ENOTDIR;
1749 return NULL;
1753 if (!dir->dir_next)
1754 return NULL;
1755 else {
1757 dirent = dir->dir_next->dirent;
1759 if (!dirent) {
1761 errno = ENOENT;
1762 return NULL;
1766 /* Hmmm, do I even need to copy it? */
1768 memcpy(context->internal->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1769 dirp = (struct smbc_dirent *)context->internal->_dirent;
1770 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1771 dir->dir_next = dir->dir_next->next;
1773 return (struct smbc_dirent *)context->internal->_dirent;
1779 * Routine to get directory entries
1782 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1784 struct smbc_dir_list *dirlist;
1785 int rem = count, reqd;
1786 char *ndir = (char *)dirp;
1788 /* Check that all is ok first ... */
1790 if (!context || !context->internal ||
1791 !context->internal->_initialized) {
1793 errno = EINVAL;
1794 return -1;
1798 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1800 errno = EBADF;
1801 return -1;
1805 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1807 errno = ENOTDIR;
1808 return -1;
1813 * Now, retrieve the number of entries that will fit in what was passed
1814 * We have to figure out if the info is in the list, or we need to
1815 * send a request to the server to get the info.
1818 while ((dirlist = dir->dir_next)) {
1819 struct smbc_dirent *dirent;
1821 if (!dirlist->dirent) {
1823 errno = ENOENT; /* Bad error */
1824 return -1;
1828 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1829 dirlist->dirent->commentlen + 1))) {
1831 if (rem < count) { /* We managed to copy something */
1833 errno = 0;
1834 return count - rem;
1837 else { /* Nothing copied ... */
1839 errno = EINVAL; /* Not enough space ... */
1840 return -1;
1846 dirent = dirlist->dirent;
1848 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1850 ((struct smbc_dirent *)ndir)->comment =
1851 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1853 ndir += reqd;
1855 rem -= reqd;
1857 dir->dir_next = dirlist = dirlist -> next;
1860 if (rem == count)
1861 return 0;
1862 else
1863 return count - rem;
1868 * Routine to create a directory ...
1871 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1873 SMBCSRV *srv;
1874 fstring server, share, user, password, workgroup;
1875 pstring path;
1877 if (!context || !context->internal ||
1878 !context->internal->_initialized) {
1880 errno = EINVAL;
1881 return -1;
1885 if (!fname) {
1887 errno = EINVAL;
1888 return -1;
1892 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1894 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1896 if (user[0] == (char)0) fstrcpy(user, context->user);
1898 fstrcpy(workgroup, context->workgroup);
1900 srv = smbc_server(context, server, share, workgroup, user, password);
1902 if (!srv) {
1904 return -1; /* errno set by smbc_server */
1908 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1910 mode = aDIR | aRONLY;
1913 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1915 if (strcmp(path, "\\") == 0) {
1917 mode = aDIR | aRONLY;
1920 else {
1922 mode = aRONLY;
1923 smbc_stat_printjob(srv, path, &size, &m_time);
1924 c_time = a_time = m_time;
1927 else { */
1929 if (!cli_mkdir(&srv->cli, path)) {
1931 errno = smbc_errno(context, &srv->cli);
1932 return -1;
1936 return 0;
1941 * Our list function simply checks to see if a directory is not empty
1944 static int smbc_rmdir_dirempty = True;
1946 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
1949 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
1950 smbc_rmdir_dirempty = False;
1955 * Routine to remove a directory
1958 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
1960 SMBCSRV *srv;
1961 fstring server, share, user, password, workgroup;
1962 pstring path;
1964 if (!context || !context->internal ||
1965 !context->internal->_initialized) {
1967 errno = EINVAL;
1968 return -1;
1972 if (!fname) {
1974 errno = EINVAL;
1975 return -1;
1979 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1981 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1983 if (user[0] == (char)0) fstrcpy(user, context->user);
1985 fstrcpy(workgroup, context->workgroup);
1987 srv = smbc_server(context, server, share, workgroup, user, password);
1989 if (!srv) {
1991 return -1; /* errno set by smbc_server */
1995 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1997 mode = aDIR | aRONLY;
2000 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2002 if (strcmp(path, "\\") == 0) {
2004 mode = aDIR | aRONLY;
2007 else {
2009 mode = aRONLY;
2010 smbc_stat_printjob(srv, path, &size, &m_time);
2011 c_time = a_time = m_time;
2014 else { */
2016 if (!cli_rmdir(&srv->cli, path)) {
2018 errno = smbc_errno(context, &srv->cli);
2020 if (errno == EACCES) { /* Check if the dir empty or not */
2022 pstring lpath; /* Local storage to avoid buffer overflows */
2024 smbc_rmdir_dirempty = True; /* Make this so ... */
2026 pstrcpy(lpath, path);
2027 pstrcat(lpath, "\\*");
2029 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2030 NULL) < 0) {
2032 /* Fix errno to ignore latest error ... */
2034 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2035 smbc_errno(context, &srv->cli)));
2036 errno = EACCES;
2040 if (smbc_rmdir_dirempty)
2041 errno = EACCES;
2042 else
2043 errno = ENOTEMPTY;
2047 return -1;
2051 return 0;
2056 * Routine to return the current directory position
2059 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2062 if (!context || !context->internal ||
2063 !context->internal->_initialized) {
2065 errno = EINVAL;
2066 return -1;
2070 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2072 errno = EBADF;
2073 return -1;
2077 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2079 errno = ENOTDIR;
2080 return -1;
2084 return (off_t) dir->dir_next;
2089 * A routine to run down the list and see if the entry is OK
2092 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2093 struct smbc_dirent *dirent)
2096 /* Run down the list looking for what we want */
2098 if (dirent) {
2100 struct smbc_dir_list *tmp = list;
2102 while (tmp) {
2104 if (tmp->dirent == dirent)
2105 return tmp;
2107 tmp = tmp->next;
2113 return NULL; /* Not found, or an error */
2119 * Routine to seek on a directory
2122 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2124 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2125 struct smbc_dir_list *list_ent = NULL;
2127 if (!context || !context->internal ||
2128 !context->internal->_initialized) {
2130 errno = EINVAL;
2131 return -1;
2135 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2137 errno = ENOTDIR;
2138 return -1;
2142 /* Now, check what we were passed and see if it is OK ... */
2144 if (dirent == NULL) { /* Seek to the begining of the list */
2146 dir->dir_next = dir->dir_list;
2147 return 0;
2151 /* Now, run down the list and make sure that the entry is OK */
2152 /* This may need to be changed if we change the format of the list */
2154 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2156 errno = EINVAL; /* Bad entry */
2157 return -1;
2161 dir->dir_next = list_ent;
2163 return 0;
2168 * Routine to fstat a dir
2171 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2174 if (!context || !context->internal ||
2175 !context->internal->_initialized) {
2177 errno = EINVAL;
2178 return -1;
2182 /* No code yet ... */
2184 return 0;
2189 * Open a print file to be written to by other calls
2192 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2194 fstring server, share, user, password;
2195 pstring path;
2197 if (!context || !context->internal ||
2198 !context->internal->_initialized) {
2200 errno = EINVAL;
2201 return NULL;
2205 if (!fname) {
2207 errno = EINVAL;
2208 return NULL;
2212 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2214 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2216 /* What if the path is empty, or the file exists? */
2218 return context->open(context, fname, O_WRONLY, 666);
2223 * Routine to print a file on a remote server ...
2225 * We open the file, which we assume to be on a remote server, and then
2226 * copy it to a print file on the share specified by printq.
2229 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2231 SMBCFILE *fid1, *fid2;
2232 int bytes, saverr, tot_bytes = 0;
2233 char buf[4096];
2235 if (!c_file || !c_file->internal->_initialized || !c_print ||
2236 !c_print->internal->_initialized) {
2238 errno = EINVAL;
2239 return -1;
2243 if (!fname && !printq) {
2245 errno = EINVAL;
2246 return -1;
2250 /* Try to open the file for reading ... */
2252 if ((int)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2254 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2255 return -1; /* smbc_open sets errno */
2259 /* Now, try to open the printer file for writing */
2261 if ((int)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2263 saverr = errno; /* Save errno */
2264 c_file->close(c_file, fid1);
2265 errno = saverr;
2266 return -1;
2270 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2272 tot_bytes += bytes;
2274 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2276 saverr = errno;
2277 c_file->close(c_file, fid1);
2278 c_print->close(c_print, fid2);
2279 errno = saverr;
2285 saverr = errno;
2287 c_file->close(c_file, fid1); /* We have to close these anyway */
2288 c_print->close(c_print, fid2);
2290 if (bytes < 0) {
2292 errno = saverr;
2293 return -1;
2297 return tot_bytes;
2302 * Routine to list print jobs on a printer share ...
2305 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
2307 SMBCSRV *srv;
2308 fstring server, share, user, password, workgroup;
2309 pstring path;
2311 if (!context || !context->internal ||
2312 !context->internal->_initialized) {
2314 errno = EINVAL;
2315 return -1;
2319 if (!fname) {
2321 errno = EINVAL;
2322 return -1;
2326 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2328 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2330 if (user[0] == (char)0) fstrcpy(user, context->user);
2332 fstrcpy(workgroup, context->workgroup);
2334 srv = smbc_server(context, server, share, workgroup, user, password);
2336 if (!srv) {
2338 return -1; /* errno set by smbc_server */
2342 if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
2344 errno = smbc_errno(context, &srv->cli);
2345 return -1;
2349 return 0;
2354 * Delete a print job from a remote printer share
2357 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2359 SMBCSRV *srv;
2360 fstring server, share, user, password, workgroup;
2361 pstring path;
2362 int err;
2364 if (!context || !context->internal ||
2365 !context->internal->_initialized) {
2367 errno = EINVAL;
2368 return -1;
2372 if (!fname) {
2374 errno = EINVAL;
2375 return -1;
2379 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2381 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2383 if (user[0] == (char)0) fstrcpy(user, context->user);
2385 fstrcpy(workgroup, context->workgroup);
2387 srv = smbc_server(context, server, share, workgroup, user, password);
2389 if (!srv) {
2391 return -1; /* errno set by smbc_server */
2395 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2397 if (err < 0)
2398 errno = smbc_errno(context, &srv->cli);
2399 else if (err == ERRnosuchprintjob)
2400 errno = EINVAL;
2401 return -1;
2405 return 0;
2410 * Get a new empty handle to fill in with your own info
2412 SMBCCTX * smbc_new_context(void)
2414 SMBCCTX * context;
2416 context = malloc(sizeof(SMBCCTX));
2417 if (!context) {
2418 errno = ENOMEM;
2419 return NULL;
2422 ZERO_STRUCTP(context);
2424 context->internal = malloc(sizeof(struct smbc_internal_data));
2425 if (!context->internal) {
2426 errno = ENOMEM;
2427 return NULL;
2430 ZERO_STRUCTP(context->internal);
2433 /* ADD REASONABLE DEFAULTS */
2434 context->debug = 0;
2435 context->timeout = 20000; /* 20 seconds */
2437 context->open = smbc_open_ctx;
2438 context->creat = smbc_creat_ctx;
2439 context->read = smbc_read_ctx;
2440 context->write = smbc_write_ctx;
2441 context->close = smbc_close_ctx;
2442 context->unlink = smbc_unlink_ctx;
2443 context->rename = smbc_rename_ctx;
2444 context->lseek = smbc_lseek_ctx;
2445 context->stat = smbc_stat_ctx;
2446 context->fstat = smbc_fstat_ctx;
2447 context->opendir = smbc_opendir_ctx;
2448 context->closedir = smbc_closedir_ctx;
2449 context->readdir = smbc_readdir_ctx;
2450 context->getdents = smbc_getdents_ctx;
2451 context->mkdir = smbc_mkdir_ctx;
2452 context->rmdir = smbc_rmdir_ctx;
2453 context->telldir = smbc_telldir_ctx;
2454 context->lseekdir = smbc_lseekdir_ctx;
2455 context->fstatdir = smbc_fstatdir_ctx;
2456 context->open_print_job = smbc_open_print_job_ctx;
2457 context->print_file = smbc_print_file_ctx;
2458 context->list_print_jobs = smbc_list_print_jobs_ctx;
2459 context->unlink_print_job = smbc_unlink_print_job_ctx;
2461 context->callbacks.check_server_fn = smbc_check_server;
2462 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2464 smbc_default_cache_functions(context);
2466 return context;
2470 * Free a context
2472 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2473 * and thus you'll be leaking memory if not handled properly.
2476 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2478 if (!context) {
2479 errno = EBADF;
2480 return 1;
2483 if (shutdown_ctx) {
2484 SMBCFILE * f;
2485 DEBUG(1,("Performing aggressive shutdown.\n"));
2487 f = context->internal->_files;
2488 while (f) {
2489 context->close(context, f);
2490 f = f->next;
2492 context->internal->_files = NULL;
2494 /* First try to remove the servers the nice way. */
2495 if (context->callbacks.purge_cached_fn(context)) {
2496 SMBCSRV * s;
2497 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2498 s = context->internal->_servers;
2499 while (s) {
2500 cli_shutdown(&s->cli);
2501 context->callbacks.remove_cached_srv_fn(context, s);
2502 SAFE_FREE(s);
2503 s = s->next;
2505 context->internal->_servers = NULL;
2508 else {
2509 /* This is the polite way */
2510 if (context->callbacks.purge_cached_fn(context)) {
2511 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2512 errno = EBUSY;
2513 return 1;
2515 if (context->internal->_servers) {
2516 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2517 errno = EBUSY;
2518 return 1;
2520 if (context->internal->_files) {
2521 DEBUG(1, ("Active files in context, free_context failed.\n"));
2522 errno = EBUSY;
2523 return 1;
2527 /* Things we have to clean up */
2528 SAFE_FREE(context->workgroup);
2529 SAFE_FREE(context->netbios_name);
2530 SAFE_FREE(context->user);
2532 DEBUG(3, ("Context %p succesfully freed\n", context));
2533 SAFE_FREE(context->internal);
2534 SAFE_FREE(context);
2535 return 0;
2540 * Initialise the library etc
2542 * We accept a struct containing handle information.
2543 * valid values for info->debug from 0 to 100,
2544 * and insist that info->fn must be non-null.
2546 SMBCCTX * smbc_init_context(SMBCCTX * context)
2548 pstring conf;
2549 int pid;
2550 char *user = NULL, *home = NULL;
2552 if (!context || !context->internal) {
2553 errno = EBADF;
2554 return NULL;
2557 /* Do not initialise the same client twice */
2558 if (context->internal->_initialized) {
2559 return 0;
2562 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2564 errno = EINVAL;
2565 return NULL;
2569 if (!smbc_initialized) {
2570 /* Do some library wide intialisations the first time we get called */
2572 /* Do we still need this ? */
2573 DEBUGLEVEL = 10;
2575 setup_logging( "libsmbclient", False);
2577 /* Here we would open the smb.conf file if needed ... */
2579 home = getenv("HOME");
2581 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2583 load_interfaces(); /* Load the list of interfaces ... */
2585 in_client = True; /* FIXME, make a param */
2587 if (!lp_load(conf, True, False, False)) {
2590 * Hmmm, what the hell do we do here ... we could not parse the
2591 * config file ... We must return an error ... and keep info around
2592 * about why we failed
2595 errno = ENOENT; /* FIXME: Figure out the correct error response */
2596 return NULL;
2599 reopen_logs(); /* Get logging working ... */
2602 * Block SIGPIPE (from lib/util_sock.c: write())
2603 * It is not needed and should not stop execution
2605 BlockSignals(True, SIGPIPE);
2607 /* Done with one-time initialisation */
2608 smbc_initialized = 1;
2612 if (!context->user) {
2614 * FIXME: Is this the best way to get the user info?
2616 user = getenv("USER");
2617 /* walk around as "guest" if no username can be found */
2618 if (!user) context->user = strdup("guest");
2619 else context->user = strdup(user);
2622 if (!context->netbios_name) {
2624 * We try to get our netbios name from the config. If that fails we fall
2625 * back on constructing our netbios name from our hostname etc
2627 if (global_myname()) {
2628 context->netbios_name = strdup(global_myname());
2630 else {
2632 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2634 pid = sys_getpid();
2635 context->netbios_name = malloc(17);
2636 if (!context->netbios_name) {
2637 errno = ENOMEM;
2638 return NULL;
2640 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2643 DEBUG(0,("Using netbios name %s.\n", context->netbios_name));
2646 if (!context->workgroup) {
2647 if (lp_workgroup()) {
2648 context->workgroup = strdup(lp_workgroup());
2650 else {
2651 /* TODO: Think about a decent default workgroup */
2652 context->workgroup = strdup("samba");
2655 DEBUG(0,("Using workgroup %s.\n", context->workgroup));
2657 /* shortest timeout is 1 second */
2658 if (context->timeout > 0 && context->timeout < 1000)
2659 context->timeout = 1000;
2662 * FIXME: Should we check the function pointers here?
2665 context->internal->_initialized = 1;
2667 return context;