Some fixes to URL syntax from coolo.
[Samba/gbeck.git] / source3 / libsmb / libsmbclient.c
blob916e388fe07070ab01bc5b4916cf3c25d19bdc0b
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;
55 static int
56 hex2int( unsigned int _char )
58 if ( _char >= 'A' && _char <='F')
59 return _char - 'A' + 10;
60 if ( _char >= 'a' && _char <='f')
61 return _char - 'a' + 10;
62 if ( _char >= '0' && _char <='9')
63 return _char - '0';
64 return -1;
67 static void
68 decode_urlpart(char *segment)
70 int old_length = strlen(segment);
71 int new_length = 0;
72 int new_length2 = 0;
73 int i = 0;
74 pstring new_segment;
75 char *new_usegment = 0;
77 if ( !old_length ) {
78 return;
81 /* make a copy of the old one */
82 new_usegment = (char*)malloc( old_length * 3 + 1 );
84 while( i < old_length ) {
85 int bReencode = False;
86 unsigned char character = segment[ i++ ];
87 if ((character <= ' ') || (character > 127))
88 bReencode = True;
90 new_usegment [ new_length2++ ] = character;
91 if (character == '%' ) {
92 int a = i+1 < old_length ? hex2int( segment[i] ) : -1;
93 int b = i+1 < old_length ? hex2int( segment[i+1] ) : -1;
94 if ((a == -1) || (b == -1)) { /* Only replace if sequence is valid */
95 /* Contains stray %, make sure to re-encode! */
96 bReencode = True;
97 } else {
98 /* Valid %xx sequence */
99 character = a * 16 + b; /* Replace with value of %dd */
100 if (!character)
101 break; /* Stop at %00 */
103 new_usegment [ new_length2++ ] = (unsigned char) segment[i++];
104 new_usegment [ new_length2++ ] = (unsigned char) segment[i++];
107 if (bReencode) {
108 unsigned int c = character / 16;
109 new_length2--;
110 new_usegment [ new_length2++ ] = '%';
112 c += (c > 9) ? ('A' - 10) : '0';
113 new_usegment[ new_length2++ ] = c;
115 c = character % 16;
116 c += (c > 9) ? ('A' - 10) : '0';
117 new_usegment[ new_length2++ ] = c;
120 new_segment [ new_length++ ] = character;
122 new_segment [ new_length ] = 0;
124 /* this assumes (very safely) that removing %aa sequences
125 only shortens the string */
126 strncpy(segment, new_segment, old_length);
128 free(new_usegment);
132 * Function to parse a path and turn it into components
134 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
136 * smb:// means show all the workgroups
137 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
138 * else, if name<20> exists, list all shares for server ...
141 static const char *smbc_prefix = "smb:";
143 static int
144 smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
145 char *user, char *password) /* FIXME, lengths of strings */
147 static pstring s;
148 pstring userinfo;
149 const char *p;
150 char *q, *r;
151 int len;
153 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
154 pstrcpy(s, fname);
156 /* clean_fname(s); causing problems ... */
158 /* see if it has the right prefix */
159 len = strlen(smbc_prefix);
160 if (strncmp(s,smbc_prefix,len) ||
161 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
163 p = s + len;
165 /* Watch the test below, we are testing to see if we should exit */
167 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
169 return -1;
173 p += 2; /* Skip the // or \\ */
175 if (*p == (char)0)
176 goto decoding;
178 if (*p == '/') {
180 strncpy(server, context->workgroup,
181 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
182 return 0;
187 * ok, its for us. Now parse out the server, share etc.
189 * However, we want to parse out [[domain;]user[:password]@] if it
190 * exists ...
193 /* check that '@' occurs before '/', if '/' exists at all */
194 q = strchr_m(p, '@');
195 r = strchr_m(p, '/');
196 if (q && (!r || q < r)) {
197 pstring username, passwd, domain;
198 const char *u = userinfo;
200 next_token(&p, userinfo, "@", sizeof(fstring));
202 username[0] = passwd[0] = domain[0] = 0;
204 if (strchr_m(u, ';')) {
206 next_token(&u, domain, ";", sizeof(fstring));
210 if (strchr_m(u, ':')) {
212 next_token(&u, username, ":", sizeof(fstring));
214 pstrcpy(passwd, u);
217 else {
219 pstrcpy(username, u);
223 if (username[0])
224 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
226 if (passwd[0])
227 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
231 if (!next_token(&p, server, "/", sizeof(fstring))) {
233 return -1;
237 if (*p == (char)0) goto decoding; /* That's it ... */
239 if (!next_token(&p, share, "/", sizeof(fstring))) {
241 return -1;
245 pstrcpy(path, p);
247 all_string_sub(path, "/", "\\", 0);
249 decoding:
250 decode_urlpart(path);
251 decode_urlpart(server);
252 decode_urlpart(share);
253 decode_urlpart(user);
254 decode_urlpart(password);
256 return 0;
260 * Convert an SMB error into a UNIX error ...
263 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
265 int ret = cli_errno(c);
267 if (cli_is_dos_error(c)) {
268 uint8 eclass;
269 uint32 ecode;
271 cli_dos_error(c, &eclass, &ecode);
273 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
274 (int)eclass, (int)ecode, (int)ecode, ret));
275 } else {
276 NTSTATUS status;
278 status = cli_nt_error(c);
280 DEBUG(3,("smbc errno %s -> %d\n",
281 nt_errstr(status), ret));
284 return ret;
288 * Check a server_fd.
289 * returns 0 if the server is in shape. Returns 1 on error
291 * Also useable outside libsmbclient to enable external cache
292 * to do some checks too.
294 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
296 if ( send_keepalive(server->cli.fd) == False )
297 return 1;
299 /* connection is ok */
300 return 0;
304 * Remove a server from the cached server list it's unused.
305 * On success, 0 is returned. 1 is returned if the server could not be removed.
307 * Also useable outside libsmbclient
309 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
311 SMBCFILE * file;
313 /* are we being fooled ? */
314 if (!context || !context->internal ||
315 !context->internal->_initialized || !srv) return 1;
318 /* Check all open files/directories for a relation with this server */
319 for (file = context->internal->_files; file; file=file->next) {
320 if (file->srv == srv) {
321 /* Still used */
322 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
323 srv, file));
324 return 1;
328 DLIST_REMOVE(context->internal->_servers, srv);
330 cli_shutdown(&srv->cli);
332 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
334 context->callbacks.remove_cached_srv_fn(context, srv);
336 SAFE_FREE(srv);
338 return 0;
342 * Connect to a server, possibly on an existing connection
344 * Here, what we want to do is: If the server and username
345 * match an existing connection, reuse that, otherwise, establish a
346 * new connection.
348 * If we have to create a new connection, call the auth_fn to get the
349 * info we need, unless the username and password were passed in.
352 SMBCSRV *smbc_server(SMBCCTX *context,
353 char *server, char *share,
354 char *workgroup, char *username,
355 char *password)
357 SMBCSRV *srv=NULL;
358 int auth_called = 0;
359 struct cli_state c;
360 struct nmb_name called, calling;
361 char *p, *server_n = server;
362 fstring group;
363 pstring ipenv;
364 struct in_addr ip;
365 int tried_reverse = 0;
367 zero_ip(&ip);
368 ZERO_STRUCT(c);
370 if (server[0] == 0) {
371 errno = EPERM;
372 return NULL;
375 check_server_cache:
377 srv = context->callbacks.get_cached_srv_fn(context, server, share,
378 workgroup, username);
380 if (!auth_called && !srv && (!username[0] || !password[0])) {
381 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
382 username, sizeof(fstring), password, sizeof(fstring));
384 * However, smbc_auth_fn may have picked up info relating to an
385 * existing connection, so try for an existing connection again ...
387 auth_called = 1;
388 goto check_server_cache;
392 if (srv) {
393 if (context->callbacks.check_server_fn(context, srv)) {
395 * This server is no good anymore
396 * Try to remove it and check for more possible servers in the cache
398 if (context->callbacks.remove_unused_server_fn(context, srv)) {
400 * We could not remove the server completely, remove it from the cache
401 * so we will not get it again. It will be removed when the last file/dir
402 * is closed.
404 context->callbacks.remove_cached_srv_fn(context, srv);
408 * Maybe there are more cached connections to this server
410 goto check_server_cache;
412 return srv;
415 make_nmb_name(&calling, context->netbios_name, 0x0);
416 make_nmb_name(&called , server, 0x20);
418 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
420 if ((p=strchr_m(server_n,'#')) &&
421 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
423 fstrcpy(group, server_n);
424 p = strchr_m(group,'#');
425 *p = 0;
429 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
431 again:
432 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
434 zero_ip(&ip);
436 /* have to open a new connection */
437 if (!cli_initialise(&c)) {
438 errno = ENOENT;
439 return NULL;
442 c.timeout = context->timeout;
444 if (!cli_connect(&c, server_n, &ip)) {
445 cli_shutdown(&c);
446 errno = ENOENT;
447 return NULL;
450 if (!cli_session_request(&c, &calling, &called)) {
451 cli_shutdown(&c);
452 if (strcmp(called.name, "*SMBSERVER")) {
453 make_nmb_name(&called , "*SMBSERVER", 0x20);
454 goto again;
456 else { /* Try one more time, but ensure we don't loop */
458 /* Only try this if server is an IP address ... */
460 if (is_ipaddress(server) && !tried_reverse) {
461 fstring remote_name;
462 struct in_addr rem_ip;
464 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
465 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
466 errno = ENOENT;
467 return NULL;
470 tried_reverse++; /* Yuck */
472 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
473 make_nmb_name(&called, remote_name, 0x20);
474 goto again;
480 errno = ENOENT;
481 return NULL;
484 DEBUG(4,(" session request ok\n"));
486 if (!cli_negprot(&c)) {
487 cli_shutdown(&c);
488 errno = ENOENT;
489 return NULL;
492 if (!cli_session_setup(&c, username,
493 password, strlen(password),
494 password, strlen(password),
495 workgroup) &&
496 /* try an anonymous login if it failed */
497 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
498 cli_shutdown(&c);
499 errno = EPERM;
500 return NULL;
503 DEBUG(4,(" session setup ok\n"));
505 if (!cli_send_tconX(&c, share, "?????",
506 password, strlen(password)+1)) {
507 errno = smbc_errno(context, &c);
508 cli_shutdown(&c);
509 return NULL;
512 DEBUG(4,(" tconx ok\n"));
515 * Ok, we have got a nice connection
516 * Let's find a free server_fd
519 srv = (SMBCSRV *)malloc(sizeof(*srv));
520 if (!srv) {
521 errno = ENOMEM;
522 goto failed;
525 ZERO_STRUCTP(srv);
526 srv->cli = c;
527 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
529 /* now add it to the cache (internal or external) */
530 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
531 DEBUG(3, (" Failed to add server to cache\n"));
532 goto failed;
536 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
537 server, share, srv));
539 return srv;
541 failed:
542 cli_shutdown(&c);
543 if (!srv) return NULL;
545 SAFE_FREE(srv);
546 return NULL;
550 * Routine to open() a file ...
553 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
555 fstring server, share, user, password, workgroup;
556 pstring path;
557 SMBCSRV *srv = NULL;
558 SMBCFILE *file = NULL;
559 int fd;
561 if (!context || !context->internal ||
562 !context->internal->_initialized) {
564 errno = EINVAL; /* Best I can think of ... */
565 return NULL;
569 if (!fname) {
571 errno = EINVAL;
572 return NULL;
576 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
578 if (user[0] == (char)0) fstrcpy(user, context->user);
580 fstrcpy(workgroup, context->workgroup);
582 srv = smbc_server(context, server, share, workgroup, user, password);
584 if (!srv) {
586 if (errno == EPERM) errno = EACCES;
587 return NULL; /* smbc_server sets errno */
591 /* Hmmm, the test for a directory is suspect here ... FIXME */
593 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
595 fd = -1;
598 else {
600 file = malloc(sizeof(SMBCFILE));
602 if (!file) {
604 errno = ENOMEM;
605 return NULL;
609 ZERO_STRUCTP(file);
611 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
613 /* Handle the error ... */
615 SAFE_FREE(file);
616 errno = smbc_errno(context, &srv->cli);
617 return NULL;
621 /* Fill in file struct */
623 file->cli_fd = fd;
624 file->fname = strdup(fname);
625 file->srv = srv;
626 file->offset = 0;
627 file->file = True;
629 DLIST_ADD(context->internal->_files, file);
630 return file;
634 /* Check if opendir needed ... */
636 if (fd == -1) {
637 int eno = 0;
639 eno = smbc_errno(context, &srv->cli);
640 file = context->opendir(context, fname);
641 if (!file) errno = eno;
642 return file;
646 errno = EINVAL; /* FIXME, correct errno ? */
647 return NULL;
652 * Routine to create a file
655 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
657 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
660 if (!context || !context->internal ||
661 !context->internal->_initialized) {
663 errno = EINVAL;
664 return NULL;
668 return smbc_open_ctx(context, path, creat_bits, mode);
672 * Routine to read() a file ...
675 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
677 int ret;
679 if (!context || !context->internal ||
680 !context->internal->_initialized) {
682 errno = EINVAL;
683 return -1;
687 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
689 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
691 errno = EBADF;
692 return -1;
696 /* Check that the buffer exists ... */
698 if (buf == NULL) {
700 errno = EINVAL;
701 return -1;
705 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
707 if (ret < 0) {
709 errno = smbc_errno(context, &file->srv->cli);
710 return -1;
714 file->offset += ret;
716 DEBUG(4, (" --> %d\n", ret));
718 return ret; /* Success, ret bytes of data ... */
723 * Routine to write() a file ...
726 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
728 int ret;
730 if (!context || !context->internal ||
731 !context->internal->_initialized) {
733 errno = EINVAL;
734 return -1;
738 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
740 errno = EBADF;
741 return -1;
745 /* Check that the buffer exists ... */
747 if (buf == NULL) {
749 errno = EINVAL;
750 return -1;
754 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
756 if (ret <= 0) {
758 errno = smbc_errno(context, &file->srv->cli);
759 return -1;
763 file->offset += ret;
765 return ret; /* Success, 0 bytes of data ... */
769 * Routine to close() a file ...
772 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
774 SMBCSRV *srv;
776 if (!context || !context->internal ||
777 !context->internal->_initialized) {
779 errno = EINVAL;
780 return -1;
784 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
786 errno = EBADF;
787 return -1;
791 /* IS a dir ... */
792 if (!file->file) {
794 return context->closedir(context, file);
798 if (!cli_close(&file->srv->cli, file->cli_fd)) {
800 DEBUG(3, ("cli_close failed on %s. purging server.\n",
801 file->fname));
802 /* Deallocate slot and remove the server
803 * from the server cache if unused */
804 errno = smbc_errno(context, &file->srv->cli);
805 srv = file->srv;
806 DLIST_REMOVE(context->internal->_files, file);
807 SAFE_FREE(file->fname);
808 SAFE_FREE(file);
809 context->callbacks.remove_unused_server_fn(context, srv);
811 return -1;
815 if (!file->file) {
817 return context->closedir(context, file);
821 if (!cli_close(&file->srv->cli, file->cli_fd)) {
822 DEBUG(3, ("cli_close failed on %s. purging server.\n",
823 file->fname));
824 /* Deallocate slot and remove the server
825 * from the server cache if unused */
826 errno = smbc_errno(context, &file->srv->cli);
827 srv = file->srv;
828 DLIST_REMOVE(context->internal->_files, file);
829 SAFE_FREE(file->fname);
830 SAFE_FREE(file);
831 context->callbacks.remove_unused_server_fn(context, srv);
833 return -1;
836 DLIST_REMOVE(context->internal->_files, file);
837 SAFE_FREE(file->fname);
838 SAFE_FREE(file);
840 return 0;
844 * Get info from an SMB server on a file. Use a qpathinfo call first
845 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
847 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
848 uint16 *mode, size_t *size,
849 time_t *c_time, time_t *a_time, time_t *m_time,
850 SMB_INO_T *ino)
853 if (!context || !context->internal ||
854 !context->internal->_initialized) {
856 errno = EINVAL;
857 return -1;
861 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
863 if (!srv->no_pathinfo2 &&
864 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
865 size, mode, ino)) return True;
867 /* if this is NT then don't bother with the getatr */
868 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
870 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
871 a_time = c_time = m_time;
872 srv->no_pathinfo2 = True;
873 return True;
876 return False;
881 * Routine to unlink() a file
884 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
886 fstring server, share, user, password, workgroup;
887 pstring path;
888 SMBCSRV *srv = NULL;
890 if (!context || !context->internal ||
891 !context->internal->_initialized) {
893 errno = EINVAL; /* Best I can think of ... */
894 return -1;
898 if (!fname) {
900 errno = EINVAL;
901 return -1;
905 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
907 if (user[0] == (char)0) fstrcpy(user, context->user);
909 fstrcpy(workgroup, context->workgroup);
911 srv = smbc_server(context, server, share, workgroup, user, password);
913 if (!srv) {
915 return -1; /* smbc_server sets errno */
919 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
921 int job = smbc_stat_printjob(srv, path, NULL, NULL);
922 if (job == -1) {
924 return -1;
927 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
930 return -1;
933 } else */
935 if (!cli_unlink(&srv->cli, path)) {
937 errno = smbc_errno(context, &srv->cli);
939 if (errno == EACCES) { /* Check if the file is a directory */
941 int saverr = errno;
942 size_t size = 0;
943 uint16 mode = 0;
944 time_t m_time = 0, a_time = 0, c_time = 0;
945 SMB_INO_T ino = 0;
947 if (!smbc_getatr(context, srv, path, &mode, &size,
948 &c_time, &a_time, &m_time, &ino)) {
950 /* Hmmm, bad error ... What? */
952 errno = smbc_errno(context, &srv->cli);
953 return -1;
956 else {
958 if (IS_DOS_DIR(mode))
959 errno = EISDIR;
960 else
961 errno = saverr; /* Restore this */
966 return -1;
970 return 0; /* Success ... */
975 * Routine to rename() a file
978 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
979 SMBCCTX *ncontext, const char *nname)
981 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
982 pstring path1, path2;
983 SMBCSRV *srv = NULL;
985 if (!ocontext || !ncontext ||
986 !ocontext->internal || !ncontext->internal ||
987 !ocontext->internal->_initialized ||
988 !ncontext->internal->_initialized) {
990 errno = EINVAL; /* Best I can think of ... */
991 return -1;
995 if (!oname || !nname) {
997 errno = EINVAL;
998 return -1;
1002 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1004 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
1006 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1008 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
1010 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1012 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1013 strcmp(user1, user2)) {
1015 /* Can't rename across file systems, or users?? */
1017 errno = EXDEV;
1018 return -1;
1022 fstrcpy(workgroup, ocontext->workgroup);
1023 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1024 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
1025 if (!srv) {
1027 return -1;
1031 if (!cli_rename(&srv->cli, path1, path2)) {
1032 int eno = smbc_errno(ocontext, &srv->cli);
1034 if (eno != EEXIST ||
1035 !cli_unlink(&srv->cli, path2) ||
1036 !cli_rename(&srv->cli, path1, path2)) {
1038 errno = eno;
1039 return -1;
1044 return 0; /* Success */
1049 * A routine to lseek() a file
1052 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
1054 size_t size;
1056 if (!context || !context->internal ||
1057 !context->internal->_initialized) {
1059 errno = EINVAL;
1060 return -1;
1064 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1066 errno = EBADF;
1067 return -1;
1071 if (!file->file) {
1073 errno = EINVAL;
1074 return -1; /* Can't lseek a dir ... */
1078 switch (whence) {
1079 case SEEK_SET:
1080 file->offset = offset;
1081 break;
1083 case SEEK_CUR:
1084 file->offset += offset;
1085 break;
1087 case SEEK_END:
1088 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1089 NULL, NULL, NULL) &&
1090 !cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1091 NULL)) {
1093 errno = EINVAL;
1094 return -1;
1096 file->offset = size + offset;
1097 break;
1099 default:
1100 errno = EINVAL;
1101 break;
1105 return file->offset;
1110 * Generate an inode number from file name for those things that need it
1113 static
1114 ino_t smbc_inode(SMBCCTX *context, const char *name)
1117 if (!context || !context->internal ||
1118 !context->internal->_initialized) {
1120 errno = EINVAL;
1121 return -1;
1125 if (!*name) return 2; /* FIXME, why 2 ??? */
1126 return (ino_t)str_checksum(name);
1131 * Routine to put basic stat info into a stat structure ... Used by stat and
1132 * fstat below.
1135 static
1136 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1139 st->st_mode = 0;
1141 if (IS_DOS_DIR(mode)) {
1142 st->st_mode = SMBC_DIR_MODE;
1143 } else {
1144 st->st_mode = SMBC_FILE_MODE;
1147 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1148 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1149 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1150 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1152 st->st_size = size;
1153 st->st_blksize = 512;
1154 st->st_blocks = (size+511)/512;
1155 st->st_uid = getuid();
1156 st->st_gid = getgid();
1158 if (IS_DOS_DIR(mode)) {
1159 st->st_nlink = 2;
1160 } else {
1161 st->st_nlink = 1;
1164 if (st->st_ino == 0) {
1165 st->st_ino = smbc_inode(context, fname);
1168 return True; /* FIXME: Is this needed ? */
1173 * Routine to stat a file given a name
1176 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1178 SMBCSRV *srv;
1179 fstring server, share, user, password, workgroup;
1180 pstring path;
1181 time_t m_time = 0, a_time = 0, c_time = 0;
1182 size_t size = 0;
1183 uint16 mode = 0;
1184 SMB_INO_T ino = 0;
1186 if (!context || !context->internal ||
1187 !context->internal->_initialized) {
1189 errno = EINVAL; /* Best I can think of ... */
1190 return -1;
1194 if (!fname) {
1196 errno = EINVAL;
1197 return -1;
1201 DEBUG(4, ("smbc_stat(%s)\n", fname));
1203 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1205 if (user[0] == (char)0) fstrcpy(user, context->user);
1207 fstrcpy(workgroup, context->workgroup);
1209 srv = smbc_server(context, server, share, workgroup, user, password);
1211 if (!srv) {
1213 return -1; /* errno set by smbc_server */
1217 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1219 mode = aDIR | aRONLY;
1222 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1224 if (strcmp(path, "\\") == 0) {
1226 mode = aDIR | aRONLY;
1229 else {
1231 mode = aRONLY;
1232 smbc_stat_printjob(srv, path, &size, &m_time);
1233 c_time = a_time = m_time;
1236 else { */
1238 if (!smbc_getatr(context, srv, path, &mode, &size,
1239 &c_time, &a_time, &m_time, &ino)) {
1241 errno = smbc_errno(context, &srv->cli);
1242 return -1;
1246 st->st_ino = ino;
1248 smbc_setup_stat(context, st, path, size, mode);
1250 st->st_atime = a_time;
1251 st->st_ctime = c_time;
1252 st->st_mtime = m_time;
1253 st->st_dev = srv->dev;
1255 return 0;
1260 * Routine to stat a file given an fd
1263 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1265 time_t c_time, a_time, m_time;
1266 size_t size;
1267 uint16 mode;
1268 SMB_INO_T ino = 0;
1270 if (!context || !context->internal ||
1271 !context->internal->_initialized) {
1273 errno = EINVAL;
1274 return -1;
1278 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1280 errno = EBADF;
1281 return -1;
1285 if (!file->file) {
1287 return context->fstatdir(context, file, st);
1291 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1292 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1293 !cli_getattrE(&file->srv->cli, file->cli_fd,
1294 &mode, &size, &c_time, &a_time, &m_time)) {
1296 errno = EINVAL;
1297 return -1;
1301 st->st_ino = ino;
1303 smbc_setup_stat(context, st, file->fname, size, mode);
1305 st->st_atime = a_time;
1306 st->st_ctime = c_time;
1307 st->st_mtime = m_time;
1308 st->st_dev = file->srv->dev;
1310 return 0;
1315 * Routine to open a directory
1317 * We want to allow:
1319 * smb: which should list all the workgroups available
1320 * smb:workgroup
1321 * smb:workgroup//server
1322 * smb://server
1323 * smb://server/share
1324 * smb://<IP-addr> which should list shares on server
1325 * smb://<IP-addr>/share which should list files on share
1328 static void smbc_remove_dir(SMBCFILE *dir)
1330 struct smbc_dir_list *d,*f;
1332 d = dir->dir_list;
1333 while (d) {
1335 f = d; d = d->next;
1337 SAFE_FREE(f->dirent);
1338 SAFE_FREE(f);
1342 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1346 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1348 struct smbc_dirent *dirent;
1349 int size;
1352 * Allocate space for the dirent, which must be increased by the
1353 * size of the name and the comment and 1 for the null on the comment.
1354 * The null on the name is already accounted for.
1357 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1358 (comment?strlen(comment):0) + 1;
1360 dirent = malloc(size);
1362 if (!dirent) {
1364 dir->dir_error = ENOMEM;
1365 return -1;
1369 ZERO_STRUCTP(dirent);
1371 if (dir->dir_list == NULL) {
1373 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1374 if (!dir->dir_list) {
1376 SAFE_FREE(dirent);
1377 dir->dir_error = ENOMEM;
1378 return -1;
1381 ZERO_STRUCTP(dir->dir_list);
1383 dir->dir_end = dir->dir_next = dir->dir_list;
1386 else {
1388 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1390 if (!dir->dir_end->next) {
1392 SAFE_FREE(dirent);
1393 dir->dir_error = ENOMEM;
1394 return -1;
1397 ZERO_STRUCTP(dir->dir_end->next);
1399 dir->dir_end = dir->dir_end->next;
1403 dir->dir_end->next = NULL;
1404 dir->dir_end->dirent = dirent;
1406 dirent->smbc_type = type;
1407 dirent->namelen = (name?strlen(name):0);
1408 dirent->commentlen = (comment?strlen(comment):0);
1409 dirent->dirlen = size;
1411 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1413 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1414 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1416 return 0;
1420 static void
1421 list_fn(const char *name, uint32 type, const char *comment, void *state)
1423 SMBCFILE *dir = (SMBCFILE *)state;
1424 int dirent_type;
1426 /* We need to process the type a little ... */
1428 if (dir->dir_type == SMBC_FILE_SHARE) {
1430 switch (type) {
1431 case 0: /* Directory tree */
1432 dirent_type = SMBC_FILE_SHARE;
1433 break;
1435 case 1:
1436 dirent_type = SMBC_PRINTER_SHARE;
1437 break;
1439 case 2:
1440 dirent_type = SMBC_COMMS_SHARE;
1441 break;
1443 case 3:
1444 dirent_type = SMBC_IPC_SHARE;
1445 break;
1447 default:
1448 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1449 break;
1452 else dirent_type = dir->dir_type;
1454 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1456 /* An error occurred, what do we do? */
1457 /* FIXME: Add some code here */
1463 static void
1464 dir_list_fn(file_info *finfo, const char *mask, void *state)
1467 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1468 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1470 /* Handle an error ... */
1472 /* FIXME: Add some code ... */
1478 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1480 fstring server, share, user, password, workgroup;
1481 pstring path;
1482 SMBCSRV *srv = NULL;
1483 SMBCFILE *dir = NULL;
1484 struct in_addr rem_ip;
1486 if (!context || !context->internal ||
1487 !context->internal->_initialized) {
1489 errno = EINVAL;
1490 return NULL;
1494 if (!fname) {
1496 errno = EINVAL;
1497 return NULL;
1501 if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1503 errno = EINVAL;
1504 return NULL;
1508 if (user[0] == (char)0) fstrcpy(user, context->user);
1510 fstrcpy(workgroup, context->workgroup);
1512 dir = malloc(sizeof(*dir));
1514 if (!dir) {
1516 errno = ENOMEM;
1517 return NULL;
1521 ZERO_STRUCTP(dir);
1523 dir->cli_fd = 0;
1524 dir->fname = strdup(fname);
1525 dir->srv = NULL;
1526 dir->offset = 0;
1527 dir->file = False;
1528 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1530 if (server[0] == (char)0) {
1532 if (share[0] != (char)0 || path[0] != (char)0) {
1534 errno = EINVAL;
1535 if (dir) {
1536 SAFE_FREE(dir->fname);
1537 SAFE_FREE(dir);
1539 return NULL;
1543 /* We have server and share and path empty ... so list the workgroups */
1544 /* first try to get the LMB for our workgroup, and if that fails, */
1545 /* try the DMB */
1547 if (!(resolve_name(context->workgroup, &rem_ip, 0x1d) ||
1548 resolve_name(context->workgroup, &rem_ip, 0x1b))) {
1550 errno = EINVAL; /* Something wrong with smb.conf? */
1551 return NULL;
1555 dir->dir_type = SMBC_WORKGROUP;
1557 /* find the name of the server ... */
1559 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1561 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server));
1562 errno = EINVAL;
1563 return NULL;
1568 * Get a connection to IPC$ on the server if we do not already have one
1571 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1573 if (!srv) {
1575 if (dir) {
1576 SAFE_FREE(dir->fname);
1577 SAFE_FREE(dir);
1580 return NULL;
1584 dir->srv = srv;
1586 /* Now, list the stuff ... */
1588 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1589 (void *)dir)) {
1591 if (dir) {
1592 SAFE_FREE(dir->fname);
1593 SAFE_FREE(dir);
1595 errno = cli_errno(&srv->cli);
1597 return NULL;
1601 else { /* Server not an empty string ... Check the rest and see what gives */
1603 if (share[0] == (char)0) {
1605 if (path[0] != (char)0) { /* Should not have empty share with path */
1607 errno = EINVAL;
1608 if (dir) {
1609 SAFE_FREE(dir->fname);
1610 SAFE_FREE(dir);
1612 return NULL;
1616 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1617 /* However, we check to see if <server> is an IP address first */
1619 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1620 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
1621 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
1622 pstring buserver;
1624 dir->dir_type = SMBC_SERVER;
1627 * Get the backup list ...
1631 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1633 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
1634 errno = EPERM; /* FIXME, is this correct */
1635 return NULL;
1640 * Get a connection to IPC$ on the server if we do not already have one
1643 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
1645 if (!srv) {
1647 if (dir) {
1648 SAFE_FREE(dir->fname);
1649 SAFE_FREE(dir);
1651 return NULL;
1655 dir->srv = srv;
1657 /* Now, list the servers ... */
1659 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1660 (void *)dir)) {
1662 if (dir) {
1663 SAFE_FREE(dir->fname);
1664 SAFE_FREE(dir);
1666 errno = cli_errno(&srv->cli);
1667 return NULL;
1672 else {
1674 if (resolve_name(server, &rem_ip, 0x20)) {
1676 /* Now, list the shares ... */
1678 dir->dir_type = SMBC_FILE_SHARE;
1680 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1682 if (!srv) {
1684 if (dir) {
1685 SAFE_FREE(dir->fname);
1686 SAFE_FREE(dir);
1688 return NULL;
1692 dir->srv = srv;
1694 /* Now, list the servers ... */
1696 if (cli_RNetShareEnum(&srv->cli, list_fn,
1697 (void *)dir) < 0) {
1699 errno = cli_errno(&srv->cli);
1700 if (dir) {
1701 SAFE_FREE(dir->fname);
1702 SAFE_FREE(dir);
1704 return NULL;
1709 else {
1711 errno = ENODEV; /* Neither the workgroup nor server exists */
1712 if (dir) {
1713 SAFE_FREE(dir->fname);
1714 SAFE_FREE(dir);
1716 return NULL;
1723 else { /* The server and share are specified ... work from there ... */
1725 /* Well, we connect to the server and list the directory */
1727 dir->dir_type = SMBC_FILE_SHARE;
1729 srv = smbc_server(context, server, share, workgroup, user, password);
1731 if (!srv) {
1733 if (dir) {
1734 SAFE_FREE(dir->fname);
1735 SAFE_FREE(dir);
1737 return NULL;
1741 dir->srv = srv;
1743 /* Now, list the files ... */
1745 pstrcat(path, "\\*");
1747 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1748 (void *)dir) < 0) {
1750 if (dir) {
1751 SAFE_FREE(dir->fname);
1752 SAFE_FREE(dir);
1754 errno = smbc_errno(context, &srv->cli);
1755 return NULL;
1762 DLIST_ADD(context->internal->_files, dir);
1763 return dir;
1768 * Routine to close a directory
1771 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1774 if (!context || !context->internal ||
1775 !context->internal->_initialized) {
1777 errno = EINVAL;
1778 return -1;
1782 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1784 errno = EBADF;
1785 return -1;
1789 smbc_remove_dir(dir); /* Clean it up */
1791 DLIST_REMOVE(context->internal->_files, dir);
1793 if (dir) {
1795 SAFE_FREE(dir->fname);
1796 SAFE_FREE(dir); /* Free the space too */
1800 return 0;
1805 * Routine to get a directory entry
1808 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1810 struct smbc_dirent *dirp, *dirent;
1812 /* Check that all is ok first ... */
1814 if (!context || !context->internal ||
1815 !context->internal->_initialized) {
1817 errno = EINVAL;
1818 return NULL;
1822 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1824 errno = EBADF;
1825 return NULL;
1829 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1831 errno = ENOTDIR;
1832 return NULL;
1836 if (!dir->dir_next)
1837 return NULL;
1838 else {
1840 dirent = dir->dir_next->dirent;
1842 if (!dirent) {
1844 errno = ENOENT;
1845 return NULL;
1849 /* Hmmm, do I even need to copy it? */
1851 memcpy(context->internal->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1852 dirp = (struct smbc_dirent *)context->internal->_dirent;
1853 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1854 dir->dir_next = dir->dir_next->next;
1856 return (struct smbc_dirent *)context->internal->_dirent;
1862 * Routine to get directory entries
1865 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1867 struct smbc_dir_list *dirlist;
1868 int rem = count, reqd;
1869 char *ndir = (char *)dirp;
1871 /* Check that all is ok first ... */
1873 if (!context || !context->internal ||
1874 !context->internal->_initialized) {
1876 errno = EINVAL;
1877 return -1;
1881 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1883 errno = EBADF;
1884 return -1;
1888 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1890 errno = ENOTDIR;
1891 return -1;
1896 * Now, retrieve the number of entries that will fit in what was passed
1897 * We have to figure out if the info is in the list, or we need to
1898 * send a request to the server to get the info.
1901 while ((dirlist = dir->dir_next)) {
1902 struct smbc_dirent *dirent;
1904 if (!dirlist->dirent) {
1906 errno = ENOENT; /* Bad error */
1907 return -1;
1911 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1912 dirlist->dirent->commentlen + 1))) {
1914 if (rem < count) { /* We managed to copy something */
1916 errno = 0;
1917 return count - rem;
1920 else { /* Nothing copied ... */
1922 errno = EINVAL; /* Not enough space ... */
1923 return -1;
1929 dirent = dirlist->dirent;
1931 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1933 ((struct smbc_dirent *)ndir)->comment =
1934 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1936 ndir += reqd;
1938 rem -= reqd;
1940 dir->dir_next = dirlist = dirlist -> next;
1943 if (rem == count)
1944 return 0;
1945 else
1946 return count - rem;
1951 * Routine to create a directory ...
1954 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1956 SMBCSRV *srv;
1957 fstring server, share, user, password, workgroup;
1958 pstring path;
1960 if (!context || !context->internal ||
1961 !context->internal->_initialized) {
1963 errno = EINVAL;
1964 return -1;
1968 if (!fname) {
1970 errno = EINVAL;
1971 return -1;
1975 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1977 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1979 if (user[0] == (char)0) fstrcpy(user, context->user);
1981 fstrcpy(workgroup, context->workgroup);
1983 srv = smbc_server(context, server, share, workgroup, user, password);
1985 if (!srv) {
1987 return -1; /* errno set by smbc_server */
1991 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1993 mode = aDIR | aRONLY;
1996 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1998 if (strcmp(path, "\\") == 0) {
2000 mode = aDIR | aRONLY;
2003 else {
2005 mode = aRONLY;
2006 smbc_stat_printjob(srv, path, &size, &m_time);
2007 c_time = a_time = m_time;
2010 else { */
2012 if (!cli_mkdir(&srv->cli, path)) {
2014 errno = smbc_errno(context, &srv->cli);
2015 return -1;
2019 return 0;
2024 * Our list function simply checks to see if a directory is not empty
2027 static int smbc_rmdir_dirempty = True;
2029 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
2032 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2033 smbc_rmdir_dirempty = False;
2038 * Routine to remove a directory
2041 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
2043 SMBCSRV *srv;
2044 fstring server, share, user, password, workgroup;
2045 pstring path;
2047 if (!context || !context->internal ||
2048 !context->internal->_initialized) {
2050 errno = EINVAL;
2051 return -1;
2055 if (!fname) {
2057 errno = EINVAL;
2058 return -1;
2062 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2064 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2066 if (user[0] == (char)0) fstrcpy(user, context->user);
2068 fstrcpy(workgroup, context->workgroup);
2070 srv = smbc_server(context, server, share, workgroup, user, password);
2072 if (!srv) {
2074 return -1; /* errno set by smbc_server */
2078 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2080 mode = aDIR | aRONLY;
2083 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2085 if (strcmp(path, "\\") == 0) {
2087 mode = aDIR | aRONLY;
2090 else {
2092 mode = aRONLY;
2093 smbc_stat_printjob(srv, path, &size, &m_time);
2094 c_time = a_time = m_time;
2097 else { */
2099 if (!cli_rmdir(&srv->cli, path)) {
2101 errno = smbc_errno(context, &srv->cli);
2103 if (errno == EACCES) { /* Check if the dir empty or not */
2105 pstring lpath; /* Local storage to avoid buffer overflows */
2107 smbc_rmdir_dirempty = True; /* Make this so ... */
2109 pstrcpy(lpath, path);
2110 pstrcat(lpath, "\\*");
2112 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2113 NULL) < 0) {
2115 /* Fix errno to ignore latest error ... */
2117 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2118 smbc_errno(context, &srv->cli)));
2119 errno = EACCES;
2123 if (smbc_rmdir_dirempty)
2124 errno = EACCES;
2125 else
2126 errno = ENOTEMPTY;
2130 return -1;
2134 return 0;
2139 * Routine to return the current directory position
2142 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2145 if (!context || !context->internal ||
2146 !context->internal->_initialized) {
2148 errno = EINVAL;
2149 return -1;
2153 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2155 errno = EBADF;
2156 return -1;
2160 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2162 errno = ENOTDIR;
2163 return -1;
2167 return (off_t) dir->dir_next;
2172 * A routine to run down the list and see if the entry is OK
2175 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2176 struct smbc_dirent *dirent)
2179 /* Run down the list looking for what we want */
2181 if (dirent) {
2183 struct smbc_dir_list *tmp = list;
2185 while (tmp) {
2187 if (tmp->dirent == dirent)
2188 return tmp;
2190 tmp = tmp->next;
2196 return NULL; /* Not found, or an error */
2202 * Routine to seek on a directory
2205 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2207 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2208 struct smbc_dir_list *list_ent = NULL;
2210 if (!context || !context->internal ||
2211 !context->internal->_initialized) {
2213 errno = EINVAL;
2214 return -1;
2218 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2220 errno = ENOTDIR;
2221 return -1;
2225 /* Now, check what we were passed and see if it is OK ... */
2227 if (dirent == NULL) { /* Seek to the begining of the list */
2229 dir->dir_next = dir->dir_list;
2230 return 0;
2234 /* Now, run down the list and make sure that the entry is OK */
2235 /* This may need to be changed if we change the format of the list */
2237 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2239 errno = EINVAL; /* Bad entry */
2240 return -1;
2244 dir->dir_next = list_ent;
2246 return 0;
2251 * Routine to fstat a dir
2254 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2257 if (!context || !context->internal ||
2258 !context->internal->_initialized) {
2260 errno = EINVAL;
2261 return -1;
2265 /* No code yet ... */
2267 return 0;
2272 * Open a print file to be written to by other calls
2275 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2277 fstring server, share, user, password;
2278 pstring path;
2280 if (!context || !context->internal ||
2281 !context->internal->_initialized) {
2283 errno = EINVAL;
2284 return NULL;
2288 if (!fname) {
2290 errno = EINVAL;
2291 return NULL;
2295 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2297 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2299 /* What if the path is empty, or the file exists? */
2301 return context->open(context, fname, O_WRONLY, 666);
2306 * Routine to print a file on a remote server ...
2308 * We open the file, which we assume to be on a remote server, and then
2309 * copy it to a print file on the share specified by printq.
2312 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2314 SMBCFILE *fid1, *fid2;
2315 int bytes, saverr, tot_bytes = 0;
2316 char buf[4096];
2318 if (!c_file || !c_file->internal->_initialized || !c_print ||
2319 !c_print->internal->_initialized) {
2321 errno = EINVAL;
2322 return -1;
2326 if (!fname && !printq) {
2328 errno = EINVAL;
2329 return -1;
2333 /* Try to open the file for reading ... */
2335 if ((fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2337 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2338 return -1; /* smbc_open sets errno */
2342 /* Now, try to open the printer file for writing */
2344 if ((fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2346 saverr = errno; /* Save errno */
2347 c_file->close(c_file, fid1);
2348 errno = saverr;
2349 return -1;
2353 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2355 tot_bytes += bytes;
2357 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2359 saverr = errno;
2360 c_file->close(c_file, fid1);
2361 c_print->close(c_print, fid2);
2362 errno = saverr;
2368 saverr = errno;
2370 c_file->close(c_file, fid1); /* We have to close these anyway */
2371 c_print->close(c_print, fid2);
2373 if (bytes < 0) {
2375 errno = saverr;
2376 return -1;
2380 return tot_bytes;
2385 * Routine to list print jobs on a printer share ...
2388 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
2390 SMBCSRV *srv;
2391 fstring server, share, user, password, workgroup;
2392 pstring path;
2394 if (!context || !context->internal ||
2395 !context->internal->_initialized) {
2397 errno = EINVAL;
2398 return -1;
2402 if (!fname) {
2404 errno = EINVAL;
2405 return -1;
2409 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2411 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2413 if (user[0] == (char)0) fstrcpy(user, context->user);
2415 fstrcpy(workgroup, context->workgroup);
2417 srv = smbc_server(context, server, share, workgroup, user, password);
2419 if (!srv) {
2421 return -1; /* errno set by smbc_server */
2425 if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
2427 errno = smbc_errno(context, &srv->cli);
2428 return -1;
2432 return 0;
2437 * Delete a print job from a remote printer share
2440 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2442 SMBCSRV *srv;
2443 fstring server, share, user, password, workgroup;
2444 pstring path;
2445 int err;
2447 if (!context || !context->internal ||
2448 !context->internal->_initialized) {
2450 errno = EINVAL;
2451 return -1;
2455 if (!fname) {
2457 errno = EINVAL;
2458 return -1;
2462 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2464 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2466 if (user[0] == (char)0) fstrcpy(user, context->user);
2468 fstrcpy(workgroup, context->workgroup);
2470 srv = smbc_server(context, server, share, workgroup, user, password);
2472 if (!srv) {
2474 return -1; /* errno set by smbc_server */
2478 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2480 if (err < 0)
2481 errno = smbc_errno(context, &srv->cli);
2482 else if (err == ERRnosuchprintjob)
2483 errno = EINVAL;
2484 return -1;
2488 return 0;
2493 * Get a new empty handle to fill in with your own info
2495 SMBCCTX * smbc_new_context(void)
2497 SMBCCTX * context;
2499 context = malloc(sizeof(SMBCCTX));
2500 if (!context) {
2501 errno = ENOMEM;
2502 return NULL;
2505 ZERO_STRUCTP(context);
2507 context->internal = malloc(sizeof(struct smbc_internal_data));
2508 if (!context->internal) {
2509 errno = ENOMEM;
2510 return NULL;
2513 ZERO_STRUCTP(context->internal);
2516 /* ADD REASONABLE DEFAULTS */
2517 context->debug = 0;
2518 context->timeout = 20000; /* 20 seconds */
2520 context->open = smbc_open_ctx;
2521 context->creat = smbc_creat_ctx;
2522 context->read = smbc_read_ctx;
2523 context->write = smbc_write_ctx;
2524 context->close = smbc_close_ctx;
2525 context->unlink = smbc_unlink_ctx;
2526 context->rename = smbc_rename_ctx;
2527 context->lseek = smbc_lseek_ctx;
2528 context->stat = smbc_stat_ctx;
2529 context->fstat = smbc_fstat_ctx;
2530 context->opendir = smbc_opendir_ctx;
2531 context->closedir = smbc_closedir_ctx;
2532 context->readdir = smbc_readdir_ctx;
2533 context->getdents = smbc_getdents_ctx;
2534 context->mkdir = smbc_mkdir_ctx;
2535 context->rmdir = smbc_rmdir_ctx;
2536 context->telldir = smbc_telldir_ctx;
2537 context->lseekdir = smbc_lseekdir_ctx;
2538 context->fstatdir = smbc_fstatdir_ctx;
2539 context->open_print_job = smbc_open_print_job_ctx;
2540 context->print_file = smbc_print_file_ctx;
2541 context->list_print_jobs = smbc_list_print_jobs_ctx;
2542 context->unlink_print_job = smbc_unlink_print_job_ctx;
2544 context->callbacks.check_server_fn = smbc_check_server;
2545 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2547 smbc_default_cache_functions(context);
2549 return context;
2553 * Free a context
2555 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2556 * and thus you'll be leaking memory if not handled properly.
2559 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2561 if (!context) {
2562 errno = EBADF;
2563 return 1;
2566 if (shutdown_ctx) {
2567 SMBCFILE * f;
2568 DEBUG(1,("Performing aggressive shutdown.\n"));
2570 f = context->internal->_files;
2571 while (f) {
2572 context->close(context, f);
2573 f = f->next;
2575 context->internal->_files = NULL;
2577 /* First try to remove the servers the nice way. */
2578 if (context->callbacks.purge_cached_fn(context)) {
2579 SMBCSRV * s;
2580 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2581 s = context->internal->_servers;
2582 while (s) {
2583 cli_shutdown(&s->cli);
2584 context->callbacks.remove_cached_srv_fn(context, s);
2585 SAFE_FREE(s);
2586 s = s->next;
2588 context->internal->_servers = NULL;
2591 else {
2592 /* This is the polite way */
2593 if (context->callbacks.purge_cached_fn(context)) {
2594 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2595 errno = EBUSY;
2596 return 1;
2598 if (context->internal->_servers) {
2599 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2600 errno = EBUSY;
2601 return 1;
2603 if (context->internal->_files) {
2604 DEBUG(1, ("Active files in context, free_context failed.\n"));
2605 errno = EBUSY;
2606 return 1;
2610 /* Things we have to clean up */
2611 SAFE_FREE(context->workgroup);
2612 SAFE_FREE(context->netbios_name);
2613 SAFE_FREE(context->user);
2615 DEBUG(3, ("Context %p succesfully freed\n", context));
2616 SAFE_FREE(context->internal);
2617 SAFE_FREE(context);
2618 return 0;
2623 * Initialise the library etc
2625 * We accept a struct containing handle information.
2626 * valid values for info->debug from 0 to 100,
2627 * and insist that info->fn must be non-null.
2629 SMBCCTX * smbc_init_context(SMBCCTX * context)
2631 pstring conf;
2632 int pid;
2633 char *user = NULL, *home = NULL;
2635 if (!context || !context->internal) {
2636 errno = EBADF;
2637 return NULL;
2640 /* Do not initialise the same client twice */
2641 if (context->internal->_initialized) {
2642 return 0;
2645 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2647 errno = EINVAL;
2648 return NULL;
2652 if (!smbc_initialized) {
2653 /* Do some library wide intialisations the first time we get called */
2655 /* Do we still need this ? */
2656 DEBUGLEVEL = 10;
2658 setup_logging( "libsmbclient", False);
2660 /* Here we would open the smb.conf file if needed ... */
2662 home = getenv("HOME");
2664 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2666 load_interfaces(); /* Load the list of interfaces ... */
2668 in_client = True; /* FIXME, make a param */
2670 if (!lp_load(conf, True, False, False)) {
2673 * Hmmm, what the hell do we do here ... we could not parse the
2674 * config file ... We must return an error ... and keep info around
2675 * about why we failed
2678 errno = ENOENT; /* FIXME: Figure out the correct error response */
2679 return NULL;
2682 reopen_logs(); /* Get logging working ... */
2685 * Block SIGPIPE (from lib/util_sock.c: write())
2686 * It is not needed and should not stop execution
2688 BlockSignals(True, SIGPIPE);
2690 /* Done with one-time initialisation */
2691 smbc_initialized = 1;
2695 if (!context->user) {
2697 * FIXME: Is this the best way to get the user info?
2699 user = getenv("USER");
2700 /* walk around as "guest" if no username can be found */
2701 if (!user) context->user = strdup("guest");
2702 else context->user = strdup(user);
2705 if (!context->netbios_name) {
2707 * We try to get our netbios name from the config. If that fails we fall
2708 * back on constructing our netbios name from our hostname etc
2710 if (global_myname()) {
2711 context->netbios_name = strdup(global_myname());
2713 else {
2715 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2717 pid = sys_getpid();
2718 context->netbios_name = malloc(17);
2719 if (!context->netbios_name) {
2720 errno = ENOMEM;
2721 return NULL;
2723 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2726 DEBUG(0,("Using netbios name %s.\n", context->netbios_name));
2729 if (!context->workgroup) {
2730 if (lp_workgroup()) {
2731 context->workgroup = strdup(lp_workgroup());
2733 else {
2734 /* TODO: Think about a decent default workgroup */
2735 context->workgroup = strdup("samba");
2738 DEBUG(0,("Using workgroup %s.\n", context->workgroup));
2740 /* shortest timeout is 1 second */
2741 if (context->timeout > 0 && context->timeout < 1000)
2742 context->timeout = 1000;
2745 * FIXME: Should we check the function pointers here?
2748 context->internal->_initialized = 1;
2750 return context;