Fix the handling of smb.conf in libsmbclient.
[Samba/gebeck_regimport.git] / source3 / libsmb / libsmbclient.c
blob69c4d8f7a77cd1ab5f075e48e06763fbb7369d67
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, size_t sizeof_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 free(new_usegment);
126 /* realloc it with unix charset */
127 pull_utf8_allocate((void**)&new_usegment, new_segment);
129 /* this assumes (very safely) that removing %aa sequences
130 only shortens the string */
131 strncpy(segment, new_usegment, sizeof_segment);
133 free(new_usegment);
137 * Function to parse a path and turn it into components
139 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
141 * smb:// means show all the workgroups
142 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
143 * else, if name<20> exists, list all shares for server ...
146 static const char *smbc_prefix = "smb:";
148 static int
149 smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
150 char *user, char *password) /* FIXME, lengths of strings */
152 static pstring s;
153 pstring userinfo;
154 const char *p;
155 char *q, *r;
156 int len;
158 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
159 pstrcpy(s, fname);
161 /* clean_fname(s); causing problems ... */
163 /* see if it has the right prefix */
164 len = strlen(smbc_prefix);
165 if (strncmp(s,smbc_prefix,len) ||
166 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
168 p = s + len;
170 /* Watch the test below, we are testing to see if we should exit */
172 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
174 return -1;
178 p += 2; /* Skip the // or \\ */
180 if (*p == (char)0)
181 goto decoding;
183 if (*p == '/') {
185 strncpy(server, context->workgroup,
186 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
187 return 0;
192 * ok, its for us. Now parse out the server, share etc.
194 * However, we want to parse out [[domain;]user[:password]@] if it
195 * exists ...
198 /* check that '@' occurs before '/', if '/' exists at all */
199 q = strchr_m(p, '@');
200 r = strchr_m(p, '/');
201 if (q && (!r || q < r)) {
202 pstring username, passwd, domain;
203 const char *u = userinfo;
205 next_token(&p, userinfo, "@", sizeof(fstring));
207 username[0] = passwd[0] = domain[0] = 0;
209 if (strchr_m(u, ';')) {
211 next_token(&u, domain, ";", sizeof(fstring));
215 if (strchr_m(u, ':')) {
217 next_token(&u, username, ":", sizeof(fstring));
219 pstrcpy(passwd, u);
222 else {
224 pstrcpy(username, u);
228 if (username[0])
229 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
231 if (passwd[0])
232 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
236 if (!next_token(&p, server, "/", sizeof(fstring))) {
238 return -1;
242 if (*p == (char)0) goto decoding; /* That's it ... */
244 if (!next_token(&p, share, "/", sizeof(fstring))) {
246 return -1;
250 pstrcpy(path, p);
252 all_string_sub(path, "/", "\\", 0);
254 decoding:
255 decode_urlpart(path, sizeof(pstring));
256 decode_urlpart(server, sizeof(fstring));
257 decode_urlpart(share, sizeof(fstring));
258 decode_urlpart(user, sizeof(fstring));
259 decode_urlpart(password, sizeof(fstring));
261 return 0;
265 * Convert an SMB error into a UNIX error ...
268 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
270 int ret = cli_errno(c);
272 if (cli_is_dos_error(c)) {
273 uint8 eclass;
274 uint32 ecode;
276 cli_dos_error(c, &eclass, &ecode);
278 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
279 (int)eclass, (int)ecode, (int)ecode, ret));
280 } else {
281 NTSTATUS status;
283 status = cli_nt_error(c);
285 DEBUG(3,("smbc errno %s -> %d\n",
286 nt_errstr(status), ret));
289 return ret;
293 * Check a server_fd.
294 * returns 0 if the server is in shape. Returns 1 on error
296 * Also useable outside libsmbclient to enable external cache
297 * to do some checks too.
299 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
301 if ( send_keepalive(server->cli.fd) == False )
302 return 1;
304 /* connection is ok */
305 return 0;
309 * Remove a server from the cached server list it's unused.
310 * On success, 0 is returned. 1 is returned if the server could not be removed.
312 * Also useable outside libsmbclient
314 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
316 SMBCFILE * file;
318 /* are we being fooled ? */
319 if (!context || !context->internal ||
320 !context->internal->_initialized || !srv) return 1;
323 /* Check all open files/directories for a relation with this server */
324 for (file = context->internal->_files; file; file=file->next) {
325 if (file->srv == srv) {
326 /* Still used */
327 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
328 srv, file));
329 return 1;
333 DLIST_REMOVE(context->internal->_servers, srv);
335 cli_shutdown(&srv->cli);
337 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
339 context->callbacks.remove_cached_srv_fn(context, srv);
341 SAFE_FREE(srv);
343 return 0;
347 * Connect to a server, possibly on an existing connection
349 * Here, what we want to do is: If the server and username
350 * match an existing connection, reuse that, otherwise, establish a
351 * new connection.
353 * If we have to create a new connection, call the auth_fn to get the
354 * info we need, unless the username and password were passed in.
357 SMBCSRV *smbc_server(SMBCCTX *context,
358 const char *server, const char *share,
359 fstring workgroup, fstring username,
360 fstring password)
362 SMBCSRV *srv=NULL;
363 int auth_called = 0;
364 struct cli_state c;
365 struct nmb_name called, calling;
366 char *p;
367 const char *server_n = server;
368 fstring group;
369 pstring ipenv;
370 struct in_addr ip;
371 int tried_reverse = 0;
373 zero_ip(&ip);
374 ZERO_STRUCT(c);
376 if (server[0] == 0) {
377 errno = EPERM;
378 return NULL;
381 check_server_cache:
383 srv = context->callbacks.get_cached_srv_fn(context, server, share,
384 workgroup, username);
386 if (!auth_called && !srv && (!username[0] || !password[0])) {
387 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
388 username, sizeof(fstring), password, sizeof(fstring));
390 * However, smbc_auth_fn may have picked up info relating to an
391 * existing connection, so try for an existing connection again ...
393 auth_called = 1;
394 goto check_server_cache;
398 if (srv) {
399 if (context->callbacks.check_server_fn(context, srv)) {
401 * This server is no good anymore
402 * Try to remove it and check for more possible servers in the cache
404 if (context->callbacks.remove_unused_server_fn(context, srv)) {
406 * We could not remove the server completely, remove it from the cache
407 * so we will not get it again. It will be removed when the last file/dir
408 * is closed.
410 context->callbacks.remove_cached_srv_fn(context, srv);
414 * Maybe there are more cached connections to this server
416 goto check_server_cache;
418 return srv;
421 make_nmb_name(&calling, context->netbios_name, 0x0);
422 make_nmb_name(&called , server, 0x20);
424 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
426 if ((p=strchr_m(server_n,'#')) &&
427 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
429 fstrcpy(group, server_n);
430 p = strchr_m(group,'#');
431 *p = 0;
435 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
437 again:
438 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
440 zero_ip(&ip);
442 /* have to open a new connection */
443 if (!cli_initialise(&c)) {
444 errno = ENOENT;
445 return NULL;
448 c.timeout = context->timeout;
450 if (!cli_connect(&c, server_n, &ip)) {
451 cli_shutdown(&c);
452 errno = ENOENT;
453 return NULL;
456 if (!cli_session_request(&c, &calling, &called)) {
457 cli_shutdown(&c);
458 if (strcmp(called.name, "*SMBSERVER")) {
459 make_nmb_name(&called , "*SMBSERVER", 0x20);
460 goto again;
462 else { /* Try one more time, but ensure we don't loop */
464 /* Only try this if server is an IP address ... */
466 if (is_ipaddress(server) && !tried_reverse) {
467 fstring remote_name;
468 struct in_addr rem_ip;
470 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
471 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
472 errno = ENOENT;
473 return NULL;
476 tried_reverse++; /* Yuck */
478 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
479 make_nmb_name(&called, remote_name, 0x20);
480 goto again;
486 errno = ENOENT;
487 return NULL;
490 DEBUG(4,(" session request ok\n"));
492 if (!cli_negprot(&c)) {
493 cli_shutdown(&c);
494 errno = ENOENT;
495 return NULL;
498 if (!cli_session_setup(&c, username,
499 password, strlen(password),
500 password, strlen(password),
501 workgroup) &&
502 /* try an anonymous login if it failed */
503 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
504 cli_shutdown(&c);
505 errno = EPERM;
506 return NULL;
509 DEBUG(4,(" session setup ok\n"));
511 if (!cli_send_tconX(&c, share, "?????",
512 password, strlen(password)+1)) {
513 errno = smbc_errno(context, &c);
514 cli_shutdown(&c);
515 return NULL;
518 DEBUG(4,(" tconx ok\n"));
521 * Ok, we have got a nice connection
522 * Let's find a free server_fd
525 srv = (SMBCSRV *)malloc(sizeof(*srv));
526 if (!srv) {
527 errno = ENOMEM;
528 goto failed;
531 ZERO_STRUCTP(srv);
532 srv->cli = c;
533 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
535 /* now add it to the cache (internal or external) */
536 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
537 DEBUG(3, (" Failed to add server to cache\n"));
538 goto failed;
542 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
543 server, share, srv));
545 return srv;
547 failed:
548 cli_shutdown(&c);
549 if (!srv) return NULL;
551 SAFE_FREE(srv);
552 return NULL;
556 * Routine to open() a file ...
559 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
561 fstring server, share, user, password, workgroup;
562 pstring path;
563 SMBCSRV *srv = NULL;
564 SMBCFILE *file = NULL;
565 int fd;
567 if (!context || !context->internal ||
568 !context->internal->_initialized) {
570 errno = EINVAL; /* Best I can think of ... */
571 return NULL;
575 if (!fname) {
577 errno = EINVAL;
578 return NULL;
582 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
584 if (user[0] == (char)0) fstrcpy(user, context->user);
586 fstrcpy(workgroup, context->workgroup);
588 srv = smbc_server(context, server, share, workgroup, user, password);
590 if (!srv) {
592 if (errno == EPERM) errno = EACCES;
593 return NULL; /* smbc_server sets errno */
597 /* Hmmm, the test for a directory is suspect here ... FIXME */
599 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
601 fd = -1;
604 else {
606 file = malloc(sizeof(SMBCFILE));
608 if (!file) {
610 errno = ENOMEM;
611 return NULL;
615 ZERO_STRUCTP(file);
617 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
619 /* Handle the error ... */
621 SAFE_FREE(file);
622 errno = smbc_errno(context, &srv->cli);
623 return NULL;
627 /* Fill in file struct */
629 file->cli_fd = fd;
630 file->fname = strdup(fname);
631 file->srv = srv;
632 file->offset = 0;
633 file->file = True;
635 DLIST_ADD(context->internal->_files, file);
636 return file;
640 /* Check if opendir needed ... */
642 if (fd == -1) {
643 int eno = 0;
645 eno = smbc_errno(context, &srv->cli);
646 file = context->opendir(context, fname);
647 if (!file) errno = eno;
648 return file;
652 errno = EINVAL; /* FIXME, correct errno ? */
653 return NULL;
658 * Routine to create a file
661 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
663 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
666 if (!context || !context->internal ||
667 !context->internal->_initialized) {
669 errno = EINVAL;
670 return NULL;
674 return smbc_open_ctx(context, path, creat_bits, mode);
678 * Routine to read() a file ...
681 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
683 int ret;
685 if (!context || !context->internal ||
686 !context->internal->_initialized) {
688 errno = EINVAL;
689 return -1;
693 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
695 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
697 errno = EBADF;
698 return -1;
702 /* Check that the buffer exists ... */
704 if (buf == NULL) {
706 errno = EINVAL;
707 return -1;
711 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
713 if (ret < 0) {
715 errno = smbc_errno(context, &file->srv->cli);
716 return -1;
720 file->offset += ret;
722 DEBUG(4, (" --> %d\n", ret));
724 return ret; /* Success, ret bytes of data ... */
729 * Routine to write() a file ...
732 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
734 int ret;
736 if (!context || !context->internal ||
737 !context->internal->_initialized) {
739 errno = EINVAL;
740 return -1;
744 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
746 errno = EBADF;
747 return -1;
751 /* Check that the buffer exists ... */
753 if (buf == NULL) {
755 errno = EINVAL;
756 return -1;
760 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
762 if (ret <= 0) {
764 errno = smbc_errno(context, &file->srv->cli);
765 return -1;
769 file->offset += ret;
771 return ret; /* Success, 0 bytes of data ... */
775 * Routine to close() a file ...
778 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
780 SMBCSRV *srv;
782 if (!context || !context->internal ||
783 !context->internal->_initialized) {
785 errno = EINVAL;
786 return -1;
790 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
792 errno = EBADF;
793 return -1;
797 /* IS a dir ... */
798 if (!file->file) {
800 return context->closedir(context, file);
804 if (!cli_close(&file->srv->cli, file->cli_fd)) {
806 DEBUG(3, ("cli_close failed on %s. purging server.\n",
807 file->fname));
808 /* Deallocate slot and remove the server
809 * from the server cache if unused */
810 errno = smbc_errno(context, &file->srv->cli);
811 srv = file->srv;
812 DLIST_REMOVE(context->internal->_files, file);
813 SAFE_FREE(file->fname);
814 SAFE_FREE(file);
815 context->callbacks.remove_unused_server_fn(context, srv);
817 return -1;
821 DLIST_REMOVE(context->internal->_files, file);
822 SAFE_FREE(file->fname);
823 SAFE_FREE(file);
825 return 0;
829 * Get info from an SMB server on a file. Use a qpathinfo call first
830 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
832 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
833 uint16 *mode, size_t *size,
834 time_t *c_time, time_t *a_time, time_t *m_time,
835 SMB_INO_T *ino)
838 if (!context || !context->internal ||
839 !context->internal->_initialized) {
841 errno = EINVAL;
842 return -1;
846 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
848 if (!srv->no_pathinfo2 &&
849 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
850 size, mode, ino)) return True;
852 /* if this is NT then don't bother with the getatr */
853 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
855 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
856 a_time = c_time = m_time;
857 srv->no_pathinfo2 = True;
858 return True;
861 return False;
866 * Routine to unlink() a file
869 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
871 fstring server, share, user, password, workgroup;
872 pstring path;
873 SMBCSRV *srv = NULL;
875 if (!context || !context->internal ||
876 !context->internal->_initialized) {
878 errno = EINVAL; /* Best I can think of ... */
879 return -1;
883 if (!fname) {
885 errno = EINVAL;
886 return -1;
890 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
892 if (user[0] == (char)0) fstrcpy(user, context->user);
894 fstrcpy(workgroup, context->workgroup);
896 srv = smbc_server(context, server, share, workgroup, user, password);
898 if (!srv) {
900 return -1; /* smbc_server sets errno */
904 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
906 int job = smbc_stat_printjob(srv, path, NULL, NULL);
907 if (job == -1) {
909 return -1;
912 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
915 return -1;
918 } else */
920 if (!cli_unlink(&srv->cli, path)) {
922 errno = smbc_errno(context, &srv->cli);
924 if (errno == EACCES) { /* Check if the file is a directory */
926 int saverr = errno;
927 size_t size = 0;
928 uint16 mode = 0;
929 time_t m_time = 0, a_time = 0, c_time = 0;
930 SMB_INO_T ino = 0;
932 if (!smbc_getatr(context, srv, path, &mode, &size,
933 &c_time, &a_time, &m_time, &ino)) {
935 /* Hmmm, bad error ... What? */
937 errno = smbc_errno(context, &srv->cli);
938 return -1;
941 else {
943 if (IS_DOS_DIR(mode))
944 errno = EISDIR;
945 else
946 errno = saverr; /* Restore this */
951 return -1;
955 return 0; /* Success ... */
960 * Routine to rename() a file
963 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
964 SMBCCTX *ncontext, const char *nname)
966 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
967 pstring path1, path2;
968 SMBCSRV *srv = NULL;
970 if (!ocontext || !ncontext ||
971 !ocontext->internal || !ncontext->internal ||
972 !ocontext->internal->_initialized ||
973 !ncontext->internal->_initialized) {
975 errno = EINVAL; /* Best I can think of ... */
976 return -1;
980 if (!oname || !nname) {
982 errno = EINVAL;
983 return -1;
987 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
989 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
991 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
993 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
995 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
997 if (strcmp(server1, server2) || strcmp(share1, share2) ||
998 strcmp(user1, user2)) {
1000 /* Can't rename across file systems, or users?? */
1002 errno = EXDEV;
1003 return -1;
1007 fstrcpy(workgroup, ocontext->workgroup);
1008 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1009 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
1010 if (!srv) {
1012 return -1;
1016 if (!cli_rename(&srv->cli, path1, path2)) {
1017 int eno = smbc_errno(ocontext, &srv->cli);
1019 if (eno != EEXIST ||
1020 !cli_unlink(&srv->cli, path2) ||
1021 !cli_rename(&srv->cli, path1, path2)) {
1023 errno = eno;
1024 return -1;
1029 return 0; /* Success */
1034 * A routine to lseek() a file
1037 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
1039 size_t size;
1041 if (!context || !context->internal ||
1042 !context->internal->_initialized) {
1044 errno = EINVAL;
1045 return -1;
1049 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1051 errno = EBADF;
1052 return -1;
1056 if (!file->file) {
1058 errno = EINVAL;
1059 return -1; /* Can't lseek a dir ... */
1063 switch (whence) {
1064 case SEEK_SET:
1065 file->offset = offset;
1066 break;
1068 case SEEK_CUR:
1069 file->offset += offset;
1070 break;
1072 case SEEK_END:
1073 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1074 NULL, NULL, NULL))
1076 SMB_BIG_UINT b_size = size;
1077 if (!cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &b_size, NULL, NULL,
1078 NULL))
1080 errno = EINVAL;
1081 return -1;
1082 } else
1083 size = b_size;
1085 file->offset = size + offset;
1086 break;
1088 default:
1089 errno = EINVAL;
1090 break;
1094 return file->offset;
1099 * Generate an inode number from file name for those things that need it
1102 static
1103 ino_t smbc_inode(SMBCCTX *context, const char *name)
1106 if (!context || !context->internal ||
1107 !context->internal->_initialized) {
1109 errno = EINVAL;
1110 return -1;
1114 if (!*name) return 2; /* FIXME, why 2 ??? */
1115 return (ino_t)str_checksum(name);
1120 * Routine to put basic stat info into a stat structure ... Used by stat and
1121 * fstat below.
1124 static
1125 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1128 st->st_mode = 0;
1130 if (IS_DOS_DIR(mode)) {
1131 st->st_mode = SMBC_DIR_MODE;
1132 } else {
1133 st->st_mode = SMBC_FILE_MODE;
1136 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1137 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1138 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1139 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1141 st->st_size = size;
1142 st->st_blksize = 512;
1143 st->st_blocks = (size+511)/512;
1144 st->st_uid = getuid();
1145 st->st_gid = getgid();
1147 if (IS_DOS_DIR(mode)) {
1148 st->st_nlink = 2;
1149 } else {
1150 st->st_nlink = 1;
1153 if (st->st_ino == 0) {
1154 st->st_ino = smbc_inode(context, fname);
1157 return True; /* FIXME: Is this needed ? */
1162 * Routine to stat a file given a name
1165 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1167 SMBCSRV *srv;
1168 fstring server, share, user, password, workgroup;
1169 pstring path;
1170 time_t m_time = 0, a_time = 0, c_time = 0;
1171 size_t size = 0;
1172 uint16 mode = 0;
1173 SMB_INO_T ino = 0;
1175 if (!context || !context->internal ||
1176 !context->internal->_initialized) {
1178 errno = EINVAL; /* Best I can think of ... */
1179 return -1;
1183 if (!fname) {
1185 errno = EINVAL;
1186 return -1;
1190 DEBUG(4, ("smbc_stat(%s)\n", fname));
1192 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1194 if (user[0] == (char)0) fstrcpy(user, context->user);
1196 fstrcpy(workgroup, context->workgroup);
1198 srv = smbc_server(context, server, share, workgroup, user, password);
1200 if (!srv) {
1202 return -1; /* errno set by smbc_server */
1206 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1208 mode = aDIR | aRONLY;
1211 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1213 if (strcmp(path, "\\") == 0) {
1215 mode = aDIR | aRONLY;
1218 else {
1220 mode = aRONLY;
1221 smbc_stat_printjob(srv, path, &size, &m_time);
1222 c_time = a_time = m_time;
1225 else { */
1227 if (!smbc_getatr(context, srv, path, &mode, &size,
1228 &c_time, &a_time, &m_time, &ino)) {
1230 errno = smbc_errno(context, &srv->cli);
1231 return -1;
1235 st->st_ino = ino;
1237 smbc_setup_stat(context, st, path, size, mode);
1239 st->st_atime = a_time;
1240 st->st_ctime = c_time;
1241 st->st_mtime = m_time;
1242 st->st_dev = srv->dev;
1244 return 0;
1249 * Routine to stat a file given an fd
1252 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1254 time_t c_time, a_time, m_time;
1255 size_t size;
1256 uint16 mode;
1257 SMB_INO_T ino = 0;
1259 if (!context || !context->internal ||
1260 !context->internal->_initialized) {
1262 errno = EINVAL;
1263 return -1;
1267 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1269 errno = EBADF;
1270 return -1;
1274 if (!file->file) {
1276 return context->fstatdir(context, file, st);
1280 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1281 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino)) {
1282 SMB_BIG_UINT b_size = size;
1283 if (!cli_getattrE(&file->srv->cli, file->cli_fd,
1284 &mode, &b_size, &c_time, &a_time, &m_time)) {
1286 errno = EINVAL;
1287 return -1;
1288 } else
1289 size = b_size;
1293 st->st_ino = ino;
1295 smbc_setup_stat(context, st, file->fname, size, mode);
1297 st->st_atime = a_time;
1298 st->st_ctime = c_time;
1299 st->st_mtime = m_time;
1300 st->st_dev = file->srv->dev;
1302 return 0;
1307 * Routine to open a directory
1309 * We want to allow:
1311 * smb: which should list all the workgroups available
1312 * smb:workgroup
1313 * smb:workgroup//server
1314 * smb://server
1315 * smb://server/share
1316 * smb://<IP-addr> which should list shares on server
1317 * smb://<IP-addr>/share which should list files on share
1320 static void smbc_remove_dir(SMBCFILE *dir)
1322 struct smbc_dir_list *d,*f;
1324 d = dir->dir_list;
1325 while (d) {
1327 f = d; d = d->next;
1329 SAFE_FREE(f->dirent);
1330 SAFE_FREE(f);
1334 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1338 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1340 struct smbc_dirent *dirent;
1341 int size;
1342 char *u_name = NULL, *u_comment = NULL;
1343 size_t u_name_len = 0, u_comment_len = 0;
1345 if (name)
1346 u_name_len = push_utf8_allocate(&u_name, name);
1347 if (comment)
1348 u_comment_len = push_utf8_allocate(&u_comment, comment);
1351 * Allocate space for the dirent, which must be increased by the
1352 * size of the name and the comment and 1 for the null on the comment.
1353 * The null on the name is already accounted for.
1356 size = sizeof(struct smbc_dirent) + u_name_len + u_comment_len + 1;
1358 dirent = malloc(size);
1360 if (!dirent) {
1362 dir->dir_error = ENOMEM;
1363 return -1;
1367 ZERO_STRUCTP(dirent);
1369 if (dir->dir_list == NULL) {
1371 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1372 if (!dir->dir_list) {
1374 SAFE_FREE(dirent);
1375 dir->dir_error = ENOMEM;
1376 return -1;
1379 ZERO_STRUCTP(dir->dir_list);
1381 dir->dir_end = dir->dir_next = dir->dir_list;
1384 else {
1386 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1388 if (!dir->dir_end->next) {
1390 SAFE_FREE(dirent);
1391 dir->dir_error = ENOMEM;
1392 return -1;
1395 ZERO_STRUCTP(dir->dir_end->next);
1397 dir->dir_end = dir->dir_end->next;
1401 dir->dir_end->next = NULL;
1402 dir->dir_end->dirent = dirent;
1404 dirent->smbc_type = type;
1405 dirent->namelen = u_name_len;
1406 dirent->commentlen = u_comment_len;
1407 dirent->dirlen = size;
1409 strncpy(dirent->name, (u_name?u_name:""), dirent->namelen + 1);
1411 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1412 strncpy(dirent->comment, (u_comment?u_comment:""), dirent->commentlen + 1);
1414 SAFE_FREE(u_comment);
1415 SAFE_FREE(u_name);
1417 return 0;
1421 static void
1422 list_fn(const char *name, uint32 type, const char *comment, void *state)
1424 SMBCFILE *dir = (SMBCFILE *)state;
1425 int dirent_type;
1427 /* We need to process the type a little ... */
1429 if (dir->dir_type == SMBC_FILE_SHARE) {
1431 switch (type) {
1432 case 0: /* Directory tree */
1433 dirent_type = SMBC_FILE_SHARE;
1434 break;
1436 case 1:
1437 dirent_type = SMBC_PRINTER_SHARE;
1438 break;
1440 case 2:
1441 dirent_type = SMBC_COMMS_SHARE;
1442 break;
1444 case 3:
1445 dirent_type = SMBC_IPC_SHARE;
1446 break;
1448 default:
1449 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1450 break;
1453 else dirent_type = dir->dir_type;
1455 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1457 /* An error occurred, what do we do? */
1458 /* FIXME: Add some code here */
1464 static void
1465 dir_list_fn(file_info *finfo, const char *mask, void *state)
1468 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1469 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1471 /* Handle an error ... */
1473 /* FIXME: Add some code ... */
1479 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1481 fstring server, share, user, password;
1482 pstring workgroup;
1483 pstring path;
1484 SMBCSRV *srv = NULL;
1485 SMBCFILE *dir = NULL;
1486 struct in_addr rem_ip;
1488 if (!context || !context->internal ||
1489 !context->internal->_initialized) {
1490 DEBUG(4, ("no valid context\n"));
1491 errno = EINVAL;
1492 return NULL;
1496 if (!fname) {
1497 DEBUG(4, ("no valid fname\n"));
1498 errno = EINVAL;
1499 return NULL;
1502 if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1503 DEBUG(4, ("no valid path\n"));
1504 errno = EINVAL;
1505 return NULL;
1508 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s'\n", fname, server, share, path));
1510 if (user[0] == (char)0) fstrcpy(user, context->user);
1512 pstrcpy(workgroup, context->workgroup);
1514 dir = malloc(sizeof(*dir));
1516 if (!dir) {
1518 errno = ENOMEM;
1519 return NULL;
1523 ZERO_STRUCTP(dir);
1525 dir->cli_fd = 0;
1526 dir->fname = strdup(fname);
1527 dir->srv = NULL;
1528 dir->offset = 0;
1529 dir->file = False;
1530 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1532 if (server[0] == (char)0) {
1533 struct in_addr server_ip;
1534 if (share[0] != (char)0 || path[0] != (char)0) {
1536 errno = EINVAL;
1537 if (dir) {
1538 SAFE_FREE(dir->fname);
1539 SAFE_FREE(dir);
1541 return NULL;
1544 /* We have server and share and path empty ... so list the workgroups */
1545 /* first try to get the LMB for our workgroup, and if that fails, */
1546 /* try the DMB */
1548 pstrcpy(workgroup, lp_workgroup());
1550 if (!find_master_ip(workgroup, &server_ip)) {
1551 struct user_auth_info u_info;
1552 struct cli_state *cli;
1554 DEBUG(4, ("Unable to find master browser for workgroup %s\n",
1555 workgroup));
1557 /* find the name of the server ... */
1558 pstrcpy(u_info.username, user);
1559 pstrcpy(u_info.password, password);
1561 if (!(cli = get_ipc_connect_master_ip_bcast(workgroup, &u_info))) {
1562 DEBUG(4, ("Unable to find master browser by "
1563 "broadcast\n"));
1564 errno = ENOENT;
1565 return NULL;
1568 fstrcpy(server, cli->desthost);
1570 cli_shutdown(cli);
1571 } else {
1572 if (!name_status_find("*", 0, 0, server_ip, server)) {
1573 errno = ENOENT;
1574 return NULL;
1578 DEBUG(4, ("using workgroup %s %s\n", workgroup, server));
1581 * Get a connection to IPC$ on the server if we do not already have one
1584 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1586 if (!srv) {
1588 if (dir) {
1589 SAFE_FREE(dir->fname);
1590 SAFE_FREE(dir);
1592 return NULL;
1595 dir->srv = srv;
1596 dir->dir_type = SMBC_WORKGROUP;
1598 /* Now, list the stuff ... */
1600 if (!cli_NetServerEnum(&srv->cli, workgroup, SV_TYPE_DOMAIN_ENUM, list_fn,
1601 (void *)dir)) {
1603 if (dir) {
1604 SAFE_FREE(dir->fname);
1605 SAFE_FREE(dir);
1607 errno = cli_errno(&srv->cli);
1609 return NULL;
1613 else { /* Server not an empty string ... Check the rest and see what gives */
1615 if (share[0] == (char)0) {
1617 if (path[0] != (char)0) { /* Should not have empty share with path */
1619 errno = EINVAL;
1620 if (dir) {
1621 SAFE_FREE(dir->fname);
1622 SAFE_FREE(dir);
1624 return NULL;
1628 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1629 /* However, we check to see if <server> is an IP address first */
1631 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1632 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
1633 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
1634 pstring buserver;
1636 dir->dir_type = SMBC_SERVER;
1639 * Get the backup list ...
1643 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1645 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
1646 errno = EPERM; /* FIXME, is this correct */
1647 return NULL;
1652 * Get a connection to IPC$ on the server if we do not already have one
1655 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
1657 if (!srv) {
1658 DEBUG(0, ("got no contact to IPC$\n"));
1659 if (dir) {
1660 SAFE_FREE(dir->fname);
1661 SAFE_FREE(dir);
1663 return NULL;
1667 dir->srv = srv;
1669 /* Now, list the servers ... */
1671 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1672 (void *)dir)) {
1674 if (dir) {
1675 SAFE_FREE(dir->fname);
1676 SAFE_FREE(dir);
1678 errno = cli_errno(&srv->cli);
1679 return NULL;
1684 else {
1686 if (resolve_name(server, &rem_ip, 0x20)) {
1688 /* Now, list the shares ... */
1690 dir->dir_type = SMBC_FILE_SHARE;
1692 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1694 if (!srv) {
1696 if (dir) {
1697 SAFE_FREE(dir->fname);
1698 SAFE_FREE(dir);
1700 return NULL;
1704 dir->srv = srv;
1706 /* Now, list the servers ... */
1708 if (cli_RNetShareEnum(&srv->cli, list_fn,
1709 (void *)dir) < 0) {
1711 errno = cli_errno(&srv->cli);
1712 if (dir) {
1713 SAFE_FREE(dir->fname);
1714 SAFE_FREE(dir);
1716 return NULL;
1721 else {
1723 errno = ENODEV; /* Neither the workgroup nor server exists */
1724 if (dir) {
1725 SAFE_FREE(dir->fname);
1726 SAFE_FREE(dir);
1728 return NULL;
1735 else { /* The server and share are specified ... work from there ... */
1737 /* Well, we connect to the server and list the directory */
1739 dir->dir_type = SMBC_FILE_SHARE;
1741 srv = smbc_server(context, server, share, workgroup, user, password);
1743 if (!srv) {
1745 if (dir) {
1746 SAFE_FREE(dir->fname);
1747 SAFE_FREE(dir);
1749 return NULL;
1753 dir->srv = srv;
1755 /* Now, list the files ... */
1757 pstrcat(path, "\\*");
1759 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1760 (void *)dir) < 0) {
1762 if (dir) {
1763 SAFE_FREE(dir->fname);
1764 SAFE_FREE(dir);
1766 errno = smbc_errno(context, &srv->cli);
1767 return NULL;
1774 DLIST_ADD(context->internal->_files, dir);
1775 return dir;
1780 * Routine to close a directory
1783 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1786 if (!context || !context->internal ||
1787 !context->internal->_initialized) {
1789 errno = EINVAL;
1790 return -1;
1794 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1796 errno = EBADF;
1797 return -1;
1801 smbc_remove_dir(dir); /* Clean it up */
1803 DLIST_REMOVE(context->internal->_files, dir);
1805 if (dir) {
1807 SAFE_FREE(dir->fname);
1808 SAFE_FREE(dir); /* Free the space too */
1812 return 0;
1817 * Routine to get a directory entry
1820 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1822 struct smbc_dirent *dirp, *dirent;
1824 /* Check that all is ok first ... */
1826 if (!context || !context->internal ||
1827 !context->internal->_initialized) {
1829 errno = EINVAL;
1830 return NULL;
1834 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1836 errno = EBADF;
1837 return NULL;
1841 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1843 errno = ENOTDIR;
1844 return NULL;
1848 if (!dir->dir_next)
1849 return NULL;
1850 else {
1852 dirent = dir->dir_next->dirent;
1854 if (!dirent) {
1856 errno = ENOENT;
1857 return NULL;
1861 /* Hmmm, do I even need to copy it? */
1863 memcpy(context->internal->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1864 dirp = (struct smbc_dirent *)context->internal->_dirent;
1865 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1866 dir->dir_next = dir->dir_next->next;
1868 return (struct smbc_dirent *)context->internal->_dirent;
1874 * Routine to get directory entries
1877 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1879 struct smbc_dir_list *dirlist;
1880 int rem = count, reqd;
1881 char *ndir = (char *)dirp;
1883 /* Check that all is ok first ... */
1885 if (!context || !context->internal ||
1886 !context->internal->_initialized) {
1888 errno = EINVAL;
1889 return -1;
1893 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1895 errno = EBADF;
1896 return -1;
1900 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1902 errno = ENOTDIR;
1903 return -1;
1908 * Now, retrieve the number of entries that will fit in what was passed
1909 * We have to figure out if the info is in the list, or we need to
1910 * send a request to the server to get the info.
1913 while ((dirlist = dir->dir_next)) {
1914 struct smbc_dirent *dirent;
1916 if (!dirlist->dirent) {
1918 errno = ENOENT; /* Bad error */
1919 return -1;
1923 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1924 dirlist->dirent->commentlen + 1))) {
1926 if (rem < count) { /* We managed to copy something */
1928 errno = 0;
1929 return count - rem;
1932 else { /* Nothing copied ... */
1934 errno = EINVAL; /* Not enough space ... */
1935 return -1;
1941 dirent = dirlist->dirent;
1943 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1945 ((struct smbc_dirent *)ndir)->comment =
1946 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1948 ndir += reqd;
1950 rem -= reqd;
1952 dir->dir_next = dirlist = dirlist -> next;
1955 if (rem == count)
1956 return 0;
1957 else
1958 return count - rem;
1963 * Routine to create a directory ...
1966 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1968 SMBCSRV *srv;
1969 fstring server, share, user, password, workgroup;
1970 pstring path;
1972 if (!context || !context->internal ||
1973 !context->internal->_initialized) {
1975 errno = EINVAL;
1976 return -1;
1980 if (!fname) {
1982 errno = EINVAL;
1983 return -1;
1987 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1989 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1991 if (user[0] == (char)0) fstrcpy(user, context->user);
1993 fstrcpy(workgroup, context->workgroup);
1995 srv = smbc_server(context, server, share, workgroup, user, password);
1997 if (!srv) {
1999 return -1; /* errno set by smbc_server */
2003 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2005 mode = aDIR | aRONLY;
2008 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2010 if (strcmp(path, "\\") == 0) {
2012 mode = aDIR | aRONLY;
2015 else {
2017 mode = aRONLY;
2018 smbc_stat_printjob(srv, path, &size, &m_time);
2019 c_time = a_time = m_time;
2022 else { */
2024 if (!cli_mkdir(&srv->cli, path)) {
2026 errno = smbc_errno(context, &srv->cli);
2027 return -1;
2031 return 0;
2036 * Our list function simply checks to see if a directory is not empty
2039 static int smbc_rmdir_dirempty = True;
2041 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
2044 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2045 smbc_rmdir_dirempty = False;
2050 * Routine to remove a directory
2053 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
2055 SMBCSRV *srv;
2056 fstring server, share, user, password, workgroup;
2057 pstring path;
2059 if (!context || !context->internal ||
2060 !context->internal->_initialized) {
2062 errno = EINVAL;
2063 return -1;
2067 if (!fname) {
2069 errno = EINVAL;
2070 return -1;
2074 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2076 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2078 if (user[0] == (char)0) fstrcpy(user, context->user);
2080 fstrcpy(workgroup, context->workgroup);
2082 srv = smbc_server(context, server, share, workgroup, user, password);
2084 if (!srv) {
2086 return -1; /* errno set by smbc_server */
2090 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2092 mode = aDIR | aRONLY;
2095 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2097 if (strcmp(path, "\\") == 0) {
2099 mode = aDIR | aRONLY;
2102 else {
2104 mode = aRONLY;
2105 smbc_stat_printjob(srv, path, &size, &m_time);
2106 c_time = a_time = m_time;
2109 else { */
2111 if (!cli_rmdir(&srv->cli, path)) {
2113 errno = smbc_errno(context, &srv->cli);
2115 if (errno == EACCES) { /* Check if the dir empty or not */
2117 pstring lpath; /* Local storage to avoid buffer overflows */
2119 smbc_rmdir_dirempty = True; /* Make this so ... */
2121 pstrcpy(lpath, path);
2122 pstrcat(lpath, "\\*");
2124 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2125 NULL) < 0) {
2127 /* Fix errno to ignore latest error ... */
2129 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2130 smbc_errno(context, &srv->cli)));
2131 errno = EACCES;
2135 if (smbc_rmdir_dirempty)
2136 errno = EACCES;
2137 else
2138 errno = ENOTEMPTY;
2142 return -1;
2146 return 0;
2151 * Routine to return the current directory position
2154 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2156 off_t ret_val; /* Squash warnings about cast */
2158 if (!context || !context->internal ||
2159 !context->internal->_initialized) {
2161 errno = EINVAL;
2162 return -1;
2166 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2168 errno = EBADF;
2169 return -1;
2173 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2175 errno = ENOTDIR;
2176 return -1;
2181 * We return the pointer here as the offset
2183 ret_val = (int)dir->dir_next;
2184 return ret_val;
2189 * A routine to run down the list and see if the entry is OK
2192 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2193 struct smbc_dirent *dirent)
2196 /* Run down the list looking for what we want */
2198 if (dirent) {
2200 struct smbc_dir_list *tmp = list;
2202 while (tmp) {
2204 if (tmp->dirent == dirent)
2205 return tmp;
2207 tmp = tmp->next;
2213 return NULL; /* Not found, or an error */
2219 * Routine to seek on a directory
2222 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2224 long int l_offset = offset; /* Handle problems of size */
2225 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
2226 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
2228 if (!context || !context->internal ||
2229 !context->internal->_initialized) {
2231 errno = EINVAL;
2232 return -1;
2236 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2238 errno = ENOTDIR;
2239 return -1;
2243 /* Now, check what we were passed and see if it is OK ... */
2245 if (dirent == NULL) { /* Seek to the begining of the list */
2247 dir->dir_next = dir->dir_list;
2248 return 0;
2252 /* Now, run down the list and make sure that the entry is OK */
2253 /* This may need to be changed if we change the format of the list */
2255 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2257 errno = EINVAL; /* Bad entry */
2258 return -1;
2262 dir->dir_next = list_ent;
2264 return 0;
2269 * Routine to fstat a dir
2272 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2275 if (!context || !context->internal ||
2276 !context->internal->_initialized) {
2278 errno = EINVAL;
2279 return -1;
2283 /* No code yet ... */
2285 return 0;
2290 * Open a print file to be written to by other calls
2293 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2295 fstring server, share, user, password;
2296 pstring path;
2298 if (!context || !context->internal ||
2299 !context->internal->_initialized) {
2301 errno = EINVAL;
2302 return NULL;
2306 if (!fname) {
2308 errno = EINVAL;
2309 return NULL;
2313 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2315 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2317 /* What if the path is empty, or the file exists? */
2319 return context->open(context, fname, O_WRONLY, 666);
2324 * Routine to print a file on a remote server ...
2326 * We open the file, which we assume to be on a remote server, and then
2327 * copy it to a print file on the share specified by printq.
2330 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2332 SMBCFILE *fid1, *fid2;
2333 int bytes, saverr, tot_bytes = 0;
2334 char buf[4096];
2336 if (!c_file || !c_file->internal->_initialized || !c_print ||
2337 !c_print->internal->_initialized) {
2339 errno = EINVAL;
2340 return -1;
2344 if (!fname && !printq) {
2346 errno = EINVAL;
2347 return -1;
2351 /* Try to open the file for reading ... */
2353 if ((int)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2355 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2356 return -1; /* smbc_open sets errno */
2360 /* Now, try to open the printer file for writing */
2362 if ((int)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2364 saverr = errno; /* Save errno */
2365 c_file->close(c_file, fid1);
2366 errno = saverr;
2367 return -1;
2371 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2373 tot_bytes += bytes;
2375 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2377 saverr = errno;
2378 c_file->close(c_file, fid1);
2379 c_print->close(c_print, fid2);
2380 errno = saverr;
2386 saverr = errno;
2388 c_file->close(c_file, fid1); /* We have to close these anyway */
2389 c_print->close(c_print, fid2);
2391 if (bytes < 0) {
2393 errno = saverr;
2394 return -1;
2398 return tot_bytes;
2403 * Routine to list print jobs on a printer share ...
2406 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
2408 SMBCSRV *srv;
2409 fstring server, share, user, password, workgroup;
2410 pstring path;
2412 if (!context || !context->internal ||
2413 !context->internal->_initialized) {
2415 errno = EINVAL;
2416 return -1;
2420 if (!fname) {
2422 errno = EINVAL;
2423 return -1;
2427 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2429 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2431 if (user[0] == (char)0) fstrcpy(user, context->user);
2433 fstrcpy(workgroup, context->workgroup);
2435 srv = smbc_server(context, server, share, workgroup, user, password);
2437 if (!srv) {
2439 return -1; /* errno set by smbc_server */
2443 if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
2445 errno = smbc_errno(context, &srv->cli);
2446 return -1;
2450 return 0;
2455 * Delete a print job from a remote printer share
2458 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2460 SMBCSRV *srv;
2461 fstring server, share, user, password, workgroup;
2462 pstring path;
2463 int err;
2465 if (!context || !context->internal ||
2466 !context->internal->_initialized) {
2468 errno = EINVAL;
2469 return -1;
2473 if (!fname) {
2475 errno = EINVAL;
2476 return -1;
2480 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2482 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2484 if (user[0] == (char)0) fstrcpy(user, context->user);
2486 fstrcpy(workgroup, context->workgroup);
2488 srv = smbc_server(context, server, share, workgroup, user, password);
2490 if (!srv) {
2492 return -1; /* errno set by smbc_server */
2496 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2498 if (err < 0)
2499 errno = smbc_errno(context, &srv->cli);
2500 else if (err == ERRnosuchprintjob)
2501 errno = EINVAL;
2502 return -1;
2506 return 0;
2511 * Get a new empty handle to fill in with your own info
2513 SMBCCTX * smbc_new_context(void)
2515 SMBCCTX * context;
2517 context = malloc(sizeof(SMBCCTX));
2518 if (!context) {
2519 errno = ENOMEM;
2520 return NULL;
2523 ZERO_STRUCTP(context);
2525 context->internal = malloc(sizeof(struct smbc_internal_data));
2526 if (!context->internal) {
2527 errno = ENOMEM;
2528 return NULL;
2531 ZERO_STRUCTP(context->internal);
2534 /* ADD REASONABLE DEFAULTS */
2535 context->debug = 0;
2536 context->timeout = 20000; /* 20 seconds */
2538 context->open = smbc_open_ctx;
2539 context->creat = smbc_creat_ctx;
2540 context->read = smbc_read_ctx;
2541 context->write = smbc_write_ctx;
2542 context->close = smbc_close_ctx;
2543 context->unlink = smbc_unlink_ctx;
2544 context->rename = smbc_rename_ctx;
2545 context->lseek = smbc_lseek_ctx;
2546 context->stat = smbc_stat_ctx;
2547 context->fstat = smbc_fstat_ctx;
2548 context->opendir = smbc_opendir_ctx;
2549 context->closedir = smbc_closedir_ctx;
2550 context->readdir = smbc_readdir_ctx;
2551 context->getdents = smbc_getdents_ctx;
2552 context->mkdir = smbc_mkdir_ctx;
2553 context->rmdir = smbc_rmdir_ctx;
2554 context->telldir = smbc_telldir_ctx;
2555 context->lseekdir = smbc_lseekdir_ctx;
2556 context->fstatdir = smbc_fstatdir_ctx;
2557 context->open_print_job = smbc_open_print_job_ctx;
2558 context->print_file = smbc_print_file_ctx;
2559 context->list_print_jobs = smbc_list_print_jobs_ctx;
2560 context->unlink_print_job = smbc_unlink_print_job_ctx;
2562 context->callbacks.check_server_fn = smbc_check_server;
2563 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2565 smbc_default_cache_functions(context);
2567 return context;
2571 * Free a context
2573 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2574 * and thus you'll be leaking memory if not handled properly.
2577 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2579 if (!context) {
2580 errno = EBADF;
2581 return 1;
2584 if (shutdown_ctx) {
2585 SMBCFILE * f;
2586 DEBUG(1,("Performing aggressive shutdown.\n"));
2588 f = context->internal->_files;
2589 while (f) {
2590 context->close(context, f);
2591 f = f->next;
2593 context->internal->_files = NULL;
2595 /* First try to remove the servers the nice way. */
2596 if (context->callbacks.purge_cached_fn(context)) {
2597 SMBCSRV * s;
2598 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2599 s = context->internal->_servers;
2600 while (s) {
2601 cli_shutdown(&s->cli);
2602 context->callbacks.remove_cached_srv_fn(context, s);
2603 SAFE_FREE(s);
2604 s = s->next;
2606 context->internal->_servers = NULL;
2609 else {
2610 /* This is the polite way */
2611 if (context->callbacks.purge_cached_fn(context)) {
2612 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2613 errno = EBUSY;
2614 return 1;
2616 if (context->internal->_servers) {
2617 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2618 errno = EBUSY;
2619 return 1;
2621 if (context->internal->_files) {
2622 DEBUG(1, ("Active files in context, free_context failed.\n"));
2623 errno = EBUSY;
2624 return 1;
2628 /* Things we have to clean up */
2629 SAFE_FREE(context->workgroup);
2630 SAFE_FREE(context->netbios_name);
2631 SAFE_FREE(context->user);
2633 DEBUG(3, ("Context %p succesfully freed\n", context));
2634 SAFE_FREE(context->internal);
2635 SAFE_FREE(context);
2636 return 0;
2641 * Initialise the library etc
2643 * We accept a struct containing handle information.
2644 * valid values for info->debug from 0 to 100,
2645 * and insist that info->fn must be non-null.
2647 SMBCCTX * smbc_init_context(SMBCCTX * context)
2649 pstring conf;
2650 int pid;
2651 char *user = NULL, *home = NULL;
2653 if (!context || !context->internal) {
2654 errno = EBADF;
2655 return NULL;
2658 /* Do not initialise the same client twice */
2659 if (context->internal->_initialized) {
2660 return 0;
2663 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2665 errno = EINVAL;
2666 return NULL;
2670 if (!smbc_initialized) {
2671 /* Do some library wide intialisations the first time we get called */
2673 /* Set this to what the user wants */
2674 DEBUGLEVEL = context->debug;
2676 setup_logging( "libsmbclient", True);
2678 /* Here we would open the smb.conf file if needed ... */
2680 home = getenv("HOME");
2682 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2684 load_interfaces(); /* Load the list of interfaces ... */
2686 in_client = True; /* FIXME, make a param */
2688 if (!lp_load(conf, True, False, False)) {
2691 * Well, if that failed, try the dyn_CONFIGFILE
2692 * Which points to the standard locn, and if that
2693 * fails, silently ignore it and use the internal
2694 * defaults ...
2697 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
2698 DEBUG(5, ("Could not load either config file: %s or %s\n",
2699 conf, dyn_CONFIGFILE));
2703 reopen_logs(); /* Get logging working ... */
2706 * Block SIGPIPE (from lib/util_sock.c: write())
2707 * It is not needed and should not stop execution
2709 BlockSignals(True, SIGPIPE);
2711 /* Done with one-time initialisation */
2712 smbc_initialized = 1;
2716 if (!context->user) {
2718 * FIXME: Is this the best way to get the user info?
2720 user = getenv("USER");
2721 /* walk around as "guest" if no username can be found */
2722 if (!user) context->user = strdup("guest");
2723 else context->user = strdup(user);
2726 if (!context->netbios_name) {
2728 * We try to get our netbios name from the config. If that fails we fall
2729 * back on constructing our netbios name from our hostname etc
2731 if (global_myname()) {
2732 context->netbios_name = strdup(global_myname());
2734 else {
2736 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2738 pid = sys_getpid();
2739 context->netbios_name = malloc(17);
2740 if (!context->netbios_name) {
2741 errno = ENOMEM;
2742 return NULL;
2744 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2748 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
2750 if (!context->workgroup) {
2751 if (lp_workgroup()) {
2752 context->workgroup = strdup(lp_workgroup());
2754 else {
2755 /* TODO: Think about a decent default workgroup */
2756 context->workgroup = strdup("samba");
2760 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
2762 /* shortest timeout is 1 second */
2763 if (context->timeout > 0 && context->timeout < 1000)
2764 context->timeout = 1000;
2767 * FIXME: Should we check the function pointers here?
2770 context->internal->_initialized = 1;
2772 return context;