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.
26 #include "libsmbclient.h"
29 * Functions exported by libsmb_cache.c that we need here
31 int smbc_default_cache_functions(SMBCCTX *context);
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 ?
39 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
40 if (!p || !list) return False;
42 if (p == list) return True;
48 extern BOOL in_client;
49 extern pstring global_myname;
52 * Is the logging working / configfile read ?
54 static int smbc_initialized = 0;
57 * Function to parse a path and turn it into components
59 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
61 * smb:// means show all the workgroups
62 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
63 * else, if name<20> exists, list all shares for server ...
66 static const char *smbc_prefix = "smb:";
69 smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
70 char *user, char *password) /* FIXME, lengths of strings */
78 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
81 /* clean_fname(s); causing problems ... */
83 /* see if it has the right prefix */
84 len = strlen(smbc_prefix);
85 if (strncmp(s,smbc_prefix,len) ||
86 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
90 /* Watch the test below, we are testing to see if we should exit */
92 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
98 p += 2; /* Skip the // or \\ */
105 strncpy(server, context->workgroup,
106 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
112 * ok, its for us. Now parse out the server, share etc.
114 * However, we want to parse out [[domain;]user[:password]@] if it
118 /* check that '@' occurs before '/', if '/' exists at all */
119 q = strchr_m(p, '@');
120 r = strchr_m(p, '/');
121 if (q && (!r || q < r)) {
122 pstring username, passwd, domain;
125 next_token(&p, userinfo, "@", sizeof(fstring));
127 username[0] = passwd[0] = domain[0] = 0;
129 if (strchr_m(u, ';')) {
131 next_token(&u, domain, ";", sizeof(fstring));
135 if (strchr_m(u, ':')) {
137 next_token(&u, username, ":", sizeof(fstring));
144 pstrcpy(username, u);
149 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
152 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
156 if (!next_token(&p, server, "/", sizeof(fstring))) {
162 if (*p == (char)0) return 0; /* That's it ... */
164 if (!next_token(&p, share, "/", sizeof(fstring))) {
172 all_string_sub(path, "/", "\\", 0);
178 * Convert an SMB error into a UNIX error ...
181 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
185 if (cli_is_dos_error(c)) {
189 cli_dos_error(c, &eclass, &ecode);
190 ret = cli_errno_from_dos(eclass, ecode);
192 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
193 (int)eclass, (int)ecode, (int)ecode, ret));
197 status = cli_nt_error(c);
198 ret = cli_errno_from_nt(status);
200 DEBUG(3,("smbc errno %s -> %d\n",
201 get_nt_error_msg(status), ret));
209 * returns 0 if the server is in shape. Returns 1 on error
211 * Also useable outside libsmbclient to enable external cache
212 * to do some checks too.
214 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
216 if ( cli_send_keepalive(&server->cli) == False )
219 /* connection is ok */
224 * Remove a server from the list server_table if it's unused.
225 * On success, 0 is returned. 1 is returned if the server could not be removed.
227 * Also useable outside libsmbclient
229 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
233 /* are we being fooled ? */
234 if (!context || !context->_initialized || !srv) return 1;
237 /* Check all open files/directories for a relation with this server */
238 for (file = context->_files; file; file=file->next) {
239 if (file->srv == srv) {
241 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
247 DLIST_REMOVE(context->_servers, srv);
249 cli_shutdown(&srv->cli);
251 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
253 context->callbacks.remove_cached_srv_fn(context, srv);
261 * Connect to a server, possibly on an existing connection
263 * Here, what we want to do is: If the server and username
264 * match an existing connection, reuse that, otherwise, establish a
267 * If we have to create a new connection, call the auth_fn to get the
268 * info we need, unless the username and password were passed in.
271 SMBCSRV *smbc_server(SMBCCTX *context,
272 char *server, char *share,
273 char *workgroup, char *username,
279 struct nmb_name called, calling;
280 char *p, *server_n = server;
284 int tried_reverse = 0;
289 if (server[0] == 0) {
296 srv = context->callbacks.get_cached_srv_fn(context, server, share,
297 workgroup, username);
299 if (!auth_called && !srv && (!username[0] || !password[0])) {
300 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
301 username, sizeof(fstring), password, sizeof(fstring));
303 * However, smbc_auth_fn may have picked up info relating to an
304 * existing connection, so try for an existing connection again ...
307 goto check_server_cache;
312 if (context->callbacks.check_server_fn(context, srv)) {
314 * This server is no good anymore
315 * Try to remove it and check for more possible servers in the cache
317 if (context->callbacks.remove_unused_server_fn(context, srv)) {
319 * We could not remove the server completely, remove it from the cache
320 * so we will not get it again. It will be removed when the last file/dir
323 context->callbacks.remove_cached_srv_fn(context, srv);
327 * Maybe there are more cached connections to this server
329 goto check_server_cache;
334 make_nmb_name(&calling, context->netbios_name, 0x0);
335 make_nmb_name(&called , server, 0x20);
337 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
339 if ((p=strchr_m(server_n,'#')) &&
340 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
342 fstrcpy(group, server_n);
343 p = strchr_m(group,'#');
348 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
351 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
355 /* have to open a new connection */
356 if (!cli_initialise(&c)) {
361 c.timeout = context->timeout;
363 if (!cli_connect(&c, server_n, &ip)) {
369 if (!cli_session_request(&c, &calling, &called)) {
371 if (strcmp(called.name, "*SMBSERVER")) {
372 make_nmb_name(&called , "*SMBSERVER", 0x20);
375 else { /* Try one more time, but ensure we don't loop */
377 /* Only try this if server is an IP address ... */
379 if (is_ipaddress(server) && !tried_reverse) {
381 struct in_addr rem_ip;
383 if (!inet_aton(server, &rem_ip)) {
384 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
389 tried_reverse++; /* Yuck */
391 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
392 make_nmb_name(&called, remote_name, 0x20);
403 DEBUG(4,(" session request ok\n"));
405 if (!cli_negprot(&c)) {
411 if (!cli_session_setup(&c, username,
412 password, strlen(password),
413 password, strlen(password),
415 /* try an anonymous login if it failed */
416 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
422 DEBUG(4,(" session setup ok\n"));
424 if (!cli_send_tconX(&c, share, "?????",
425 password, strlen(password)+1)) {
426 errno = smbc_errno(context, &c);
431 DEBUG(4,(" tconx ok\n"));
434 * Ok, we have got a nice connection
435 * Let's find a free server_fd
438 srv = (SMBCSRV *)malloc(sizeof(*srv));
446 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
448 /* now add it to the cache (internal or external) */
449 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
450 DEBUG(3, (" Failed to add server to cache\n"));
455 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
456 server, share, srv));
462 if (!srv) return NULL;
469 * Routine to open() a file ...
472 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
474 fstring server, share, user, password, workgroup;
477 SMBCFILE *file = NULL;
480 if (!context || !context->_initialized) {
482 errno = EINVAL; /* Best I can think of ... */
494 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
496 if (user[0] == (char)0) pstrcpy(user, context->user);
498 pstrcpy(workgroup, context->workgroup);
500 srv = smbc_server(context, server, share, workgroup, user, password);
504 if (errno == EPERM) errno = EACCES;
505 return NULL; /* smbc_server sets errno */
509 /* Hmmm, the test for a directory is suspect here ... FIXME */
511 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
518 file = malloc(sizeof(SMBCFILE));
529 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
531 /* Handle the error ... */
534 errno = smbc_errno(context, &srv->cli);
539 /* Fill in file struct */
542 file->fname = strdup(fname);
547 DLIST_ADD(context->_files, file);
552 /* Check if opendir needed ... */
557 eno = smbc_errno(context, &srv->cli);
558 file = context->opendir(context, fname);
559 if (!file) errno = eno;
564 errno = EINVAL; /* FIXME, correct errno ? */
570 * Routine to create a file
573 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
575 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
578 if (!context || !context->_initialized) {
585 return smbc_open_ctx(context, path, creat_bits, mode);
589 * Routine to read() a file ...
592 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
596 if (!context || !context->_initialized) {
603 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
605 if (!file || !DLIST_CONTAINS(context->_files, file)) {
612 /* Check that the buffer exists ... */
621 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
625 errno = smbc_errno(context, &file->srv->cli);
632 DEBUG(4, (" --> %d\n", ret));
634 return ret; /* Success, ret bytes of data ... */
639 * Routine to write() a file ...
642 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
646 if (!context || context->_initialized) {
653 if (!file || !DLIST_CONTAINS(context->_files, file)) {
660 /* Check that the buffer exists ... */
669 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
673 errno = smbc_errno(context, &file->srv->cli);
680 return ret; /* Success, 0 bytes of data ... */
684 * Routine to close() a file ...
687 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
691 if (!context || !context->_initialized) {
698 if (!file || !DLIST_CONTAINS(context->_files, file)) {
708 return context->closedir(context, file);
712 if (!cli_close(&file->srv->cli, file->cli_fd)) {
714 DEBUG(3, ("cli_close failed on %s. purging server.\n",
716 /* Deallocate slot and remove the server
717 * from the server cache if unused */
718 errno = smbc_errno(context, &file->srv->cli);
720 DLIST_REMOVE(context->_files, file);
721 SAFE_FREE(file->fname);
723 context->callbacks.remove_unused_server_fn(context, srv);
731 return context->closedir(context, file);
735 if (!cli_close(&file->srv->cli, file->cli_fd)) {
736 DEBUG(3, ("cli_close failed on %s. purging server.\n",
738 /* Deallocate slot and remove the server
739 * from the server cache if unused */
740 errno = smbc_errno(context, &file->srv->cli);
742 DLIST_REMOVE(context->_files, file);
743 SAFE_FREE(file->fname);
745 context->callbacks.remove_unused_server_fn(context, srv);
750 DLIST_REMOVE(context->_files, file);
751 SAFE_FREE(file->fname);
758 * Get info from an SMB server on a file. Use a qpathinfo call first
759 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
761 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
762 uint16 *mode, size_t *size,
763 time_t *c_time, time_t *a_time, time_t *m_time,
767 if (!context || !context->_initialized) {
774 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
776 if (!srv->no_pathinfo2 &&
777 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
778 size, mode, ino)) return True;
780 /* if this is NT then don't bother with the getatr */
781 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
783 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
784 a_time = c_time = m_time;
785 srv->no_pathinfo2 = True;
794 * Routine to unlink() a file
797 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
799 fstring server, share, user, password, workgroup;
803 if (!context || context->_initialized) {
805 errno = EINVAL; /* Best I can think of ... */
817 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
819 if (user[0] == (char)0) pstrcpy(user, context->user);
821 pstrcpy(workgroup, context->workgroup);
823 srv = smbc_server(context, server, share, workgroup, user, password);
827 return -1; /* smbc_server sets errno */
831 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
833 int job = smbc_stat_printjob(srv, path, NULL, NULL);
839 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
847 if (!cli_unlink(&srv->cli, path)) {
849 errno = smbc_errno(context, &srv->cli);
851 if (errno == EACCES) { /* Check if the file is a directory */
856 time_t m_time = 0, a_time = 0, c_time = 0;
859 if (!smbc_getatr(context, srv, path, &mode, &size,
860 &c_time, &a_time, &m_time, &ino)) {
862 /* Hmmm, bad error ... What? */
864 errno = smbc_errno(context, &srv->cli);
870 if (IS_DOS_DIR(mode))
873 errno = saverr; /* Restore this */
882 return 0; /* Success ... */
887 * Routine to rename() a file
890 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
891 SMBCCTX *ncontext, const char *nname)
893 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
894 pstring path1, path2;
897 if (!ocontext || !ncontext ||
898 !ocontext->_initialized || !ncontext->_initialized) {
900 errno = EINVAL; /* Best I can think of ... */
905 if (!oname || !nname) {
912 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
914 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
916 if (user1[0] == (char)0) pstrcpy(user1, ocontext->user);
918 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
920 if (user2[0] == (char)0) pstrcpy(user2, ncontext->user);
922 if (strcmp(server1, server2) || strcmp(share1, share2) ||
923 strcmp(user1, user2)) {
925 /* Can't rename across file systems, or users?? */
932 pstrcpy(workgroup, ocontext->workgroup);
933 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
934 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
941 if (!cli_rename(&srv->cli, path1, path2)) {
942 int eno = smbc_errno(ocontext, &srv->cli);
945 !cli_unlink(&srv->cli, path2) ||
946 !cli_rename(&srv->cli, path1, path2)) {
954 return 0; /* Success */
959 * A routine to lseek() a file
962 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
966 if (!context || !context->_initialized) {
973 if (!file || !DLIST_CONTAINS(context->_files, file)) {
983 return -1; /* Can't lseek a dir ... */
989 file->offset = offset;
993 file->offset += offset;
997 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
999 !cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1005 file->offset = size + offset;
1014 return file->offset;
1019 * Generate an inode number from file name for those things that need it
1023 ino_t smbc_inode(SMBCCTX *context, const char *name)
1026 if (!context || !context->_initialized) {
1033 if (!*name) return 2; /* FIXME, why 2 ??? */
1034 return (ino_t)str_checksum(name);
1039 * Routine to put basic stat info into a stat structure ... Used by stat and
1044 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1049 if (IS_DOS_DIR(mode)) {
1050 st->st_mode = SMBC_DIR_MODE;
1052 st->st_mode = SMBC_FILE_MODE;
1055 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1056 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1057 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1058 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1061 st->st_blksize = 512;
1062 st->st_blocks = (size+511)/512;
1063 st->st_uid = getuid();
1064 st->st_gid = getgid();
1066 if (IS_DOS_DIR(mode)) {
1072 if (st->st_ino == 0) {
1073 st->st_ino = smbc_inode(context, fname);
1076 return True; /* FIXME: Is this needed ? */
1081 * Routine to stat a file given a name
1084 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1087 fstring server, share, user, password, workgroup;
1089 time_t m_time = 0, a_time = 0, c_time = 0;
1094 if (!context || !context->_initialized) {
1096 errno = EINVAL; /* Best I can think of ... */
1108 DEBUG(4, ("smbc_stat(%s)\n", fname));
1110 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1112 if (user[0] == (char)0) pstrcpy(user, context->user);
1114 pstrcpy(workgroup, context->workgroup);
1116 srv = smbc_server(context, server, share, workgroup, user, password);
1120 return -1; /* errno set by smbc_server */
1124 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1126 mode = aDIR | aRONLY;
1129 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1131 if (strcmp(path, "\\") == 0) {
1133 mode = aDIR | aRONLY;
1139 smbc_stat_printjob(srv, path, &size, &m_time);
1140 c_time = a_time = m_time;
1145 if (!smbc_getatr(context, srv, path, &mode, &size,
1146 &c_time, &a_time, &m_time, &ino)) {
1148 errno = smbc_errno(context, &srv->cli);
1155 smbc_setup_stat(context, st, path, size, mode);
1157 st->st_atime = a_time;
1158 st->st_ctime = c_time;
1159 st->st_mtime = m_time;
1160 st->st_dev = srv->dev;
1167 * Routine to stat a file given an fd
1170 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1172 time_t c_time, a_time, m_time;
1177 if (!context || !context->_initialized) {
1184 if (!file || !DLIST_CONTAINS(context->_files, file)) {
1193 return context->fstatdir(context, file, st);
1197 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1198 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1199 !cli_getattrE(&file->srv->cli, file->cli_fd,
1200 &mode, &size, &c_time, &a_time, &m_time)) {
1209 smbc_setup_stat(context, st, file->fname, size, mode);
1211 st->st_atime = a_time;
1212 st->st_ctime = c_time;
1213 st->st_mtime = m_time;
1214 st->st_dev = file->srv->dev;
1221 * Routine to open a directory
1225 * smb: which should list all the workgroups available
1227 * smb:workgroup//server
1229 * smb://server/share
1230 * smb://<IP-addr> which should list shares on server
1231 * smb://<IP-addr>/share which should list files on share
1234 static void smbc_remove_dir(SMBCFILE *dir)
1236 struct smbc_dir_list *d,*f;
1243 SAFE_FREE(f->dirent);
1248 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1252 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1254 struct smbc_dirent *dirent;
1258 * Allocate space for the dirent, which must be increased by the
1259 * size of the name and the comment and 1 for the null on the comment.
1260 * The null on the name is already accounted for.
1263 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1264 (comment?strlen(comment):0) + 1;
1266 dirent = malloc(size);
1270 dir->dir_error = ENOMEM;
1275 ZERO_STRUCTP(dirent);
1277 ZERO_STRUCTP(dirent);
1280 if (dir->dir_list == NULL) {
1282 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1283 if (!dir->dir_list) {
1286 dir->dir_error = ENOMEM;
1290 ZERO_STRUCTP(dir->dir_list);
1292 dir->dir_end = dir->dir_next = dir->dir_list;
1297 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1299 if (!dir->dir_end->next) {
1302 dir->dir_error = ENOMEM;
1306 ZERO_STRUCTP(dir->dir_end->next);
1308 dir->dir_end = dir->dir_end->next;
1312 dir->dir_end->next = NULL;
1313 dir->dir_end->dirent = dirent;
1315 dirent->smbc_type = type;
1316 dirent->namelen = (name?strlen(name):0);
1317 dirent->commentlen = (comment?strlen(comment):0);
1318 dirent->dirlen = size;
1320 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1322 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1323 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1330 list_fn(const char *name, uint32 type, const char *comment, void *state)
1332 SMBCFILE *dir = (SMBCFILE *)state;
1335 /* We need to process the type a little ... */
1337 if (dir->dir_type == SMBC_FILE_SHARE) {
1340 case 0: /* Directory tree */
1341 dirent_type = SMBC_FILE_SHARE;
1345 dirent_type = SMBC_PRINTER_SHARE;
1349 dirent_type = SMBC_COMMS_SHARE;
1353 dirent_type = SMBC_IPC_SHARE;
1357 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1360 ZERO_STRUCTP(dir->dir_list);
1363 else dirent_type = dir->dir_type;
1365 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1367 /* An error occurred, what do we do? */
1368 /* FIXME: Add some code here */
1375 dir_list_fn(file_info *finfo, const char *mask, void *state)
1378 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1379 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1381 /* Handle an error ... */
1383 /* FIXME: Add some code ... */
1389 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1391 fstring server, share, user, password, workgroup;
1393 SMBCSRV *srv = NULL;
1394 SMBCFILE *dir = NULL;
1395 struct in_addr rem_ip;
1398 if (!context || !context->_initialized) {
1412 if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1419 if (user[0] == (char)0) pstrcpy(user, context->user);
1421 pstrcpy(workgroup, context->workgroup);
1423 dir = malloc(sizeof(*dir));
1435 dir->fname = strdup(fname);
1439 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1441 if (server[0] == (char)0) {
1443 if (share[0] != (char)0 || path[0] != (char)0) {
1447 SAFE_FREE(dir->fname);
1454 /* We have server and share and path empty ... so list the workgroups */
1455 /* first try to get the LMB for our workgroup, and if that fails, */
1458 if (!(resolve_name(context->workgroup, &rem_ip, 0x1d) ||
1459 resolve_name(context->workgroup, &rem_ip, 0x1b))) {
1461 errno = EINVAL; /* Something wrong with smb.conf? */
1466 dir->dir_type = SMBC_WORKGROUP;
1468 /* find the name of the server ... */
1470 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1472 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server));
1479 * Get a connection to IPC$ on the server if we do not already have one
1482 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1487 SAFE_FREE(dir->fname);
1494 ZERO_STRUCTP(dir->dir_end);
1498 /* Now, list the stuff ... */
1500 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1504 SAFE_FREE(dir->fname);
1507 errno = cli_errno(&srv->cli);
1513 else { /* Server not an empty string ... Check the rest and see what gives */
1515 if (share[0] == (char)0) {
1517 if (path[0] != (char)0) { /* Should not have empty share with path */
1521 SAFE_FREE(dir->fname);
1528 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1529 /* However, we check to see if <server> is an IP address first */
1531 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1532 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
1533 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
1536 dir->dir_type = SMBC_SERVER;
1539 * Get the backup list ...
1543 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1545 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
1546 errno = EPERM; /* FIXME, is this correct */
1552 * Get a connection to IPC$ on the server if we do not already have one
1555 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
1560 SAFE_FREE(dir->fname);
1569 /* Now, list the servers ... */
1571 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1575 SAFE_FREE(dir->fname);
1578 errno = cli_errno(&srv->cli);
1586 if (resolve_name(server, &rem_ip, 0x20)) {
1588 /* Now, list the shares ... */
1590 dir->dir_type = SMBC_FILE_SHARE;
1592 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1597 SAFE_FREE(dir->fname);
1606 /* Now, list the servers ... */
1608 if (cli_RNetShareEnum(&srv->cli, list_fn,
1611 errno = cli_errno(&srv->cli);
1613 SAFE_FREE(dir->fname);
1623 errno = ENODEV; /* Neither the workgroup nor server exists */
1625 SAFE_FREE(dir->fname);
1635 else { /* The server and share are specified ... work from there ... */
1637 /* Well, we connect to the server and list the directory */
1639 dir->dir_type = SMBC_FILE_SHARE;
1641 srv = smbc_server(context, server, share, workgroup, user, password);
1646 SAFE_FREE(dir->fname);
1655 /* Now, list the files ... */
1657 pstrcat(path, "\\*");
1659 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1663 SAFE_FREE(dir->fname);
1666 errno = smbc_errno(context, &srv->cli);
1674 DLIST_ADD(context->_files, dir);
1680 * Routine to close a directory
1683 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1686 if (!context || !context->_initialized) {
1693 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1700 smbc_remove_dir(dir); /* Clean it up */
1702 DLIST_REMOVE(context->_files, dir);
1706 SAFE_FREE(dir->fname);
1707 SAFE_FREE(dir); /* Free the space too */
1716 * Routine to get a directory entry
1719 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1721 struct smbc_dirent *dirp, *dirent;
1723 /* Check that all is ok first ... */
1725 if (!context || !context->_initialized) {
1732 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1739 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1750 dirent = dir->dir_next->dirent;
1759 /* Hmmm, do I even need to copy it? */
1761 memcpy(context->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1762 dirp = (struct smbc_dirent *)context->_dirent;
1763 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1764 dir->dir_next = dir->dir_next->next;
1766 return (struct smbc_dirent *)context->_dirent;
1772 * Routine to get directory entries
1775 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1777 struct smbc_dir_list *dirlist;
1778 int rem = count, reqd;
1779 char *ndir = (char *)dirp;
1781 /* Check that all is ok first ... */
1783 if (!context || !context->_initialized) {
1790 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1797 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1805 * Now, retrieve the number of entries that will fit in what was passed
1806 * We have to figure out if the info is in the list, or we need to
1807 * send a request to the server to get the info.
1810 while ((dirlist = dir->dir_next)) {
1811 struct smbc_dirent *dirent;
1813 if (!dirlist->dirent) {
1815 errno = ENOENT; /* Bad error */
1820 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1821 dirlist->dirent->commentlen + 1))) {
1823 if (rem < count) { /* We managed to copy something */
1829 else { /* Nothing copied ... */
1831 errno = EINVAL; /* Not enough space ... */
1838 dirent = dirlist->dirent;
1840 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1842 ((struct smbc_dirent *)ndir)->comment =
1843 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1849 dir->dir_next = dirlist = dirlist -> next;
1860 * Routine to create a directory ...
1863 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1866 fstring server, share, user, password, workgroup;
1869 if (!context || !context->_initialized) {
1883 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1885 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1887 if (user[0] == (char)0) pstrcpy(user, context->user);
1889 pstrcpy(workgroup, context->workgroup);
1891 srv = smbc_server(context, server, share, workgroup, user, password);
1895 return -1; /* errno set by smbc_server */
1899 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1901 mode = aDIR | aRONLY;
1904 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1906 if (strcmp(path, "\\") == 0) {
1908 mode = aDIR | aRONLY;
1914 smbc_stat_printjob(srv, path, &size, &m_time);
1915 c_time = a_time = m_time;
1920 if (!cli_mkdir(&srv->cli, path)) {
1922 errno = smbc_errno(context, &srv->cli);
1932 * Our list function simply checks to see if a directory is not empty
1935 static int smbc_rmdir_dirempty = True;
1937 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
1940 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
1941 smbc_rmdir_dirempty = False;
1946 * Routine to remove a directory
1949 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
1952 fstring server, share, user, password, workgroup;
1955 if (!context || !context->_initialized) {
1969 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1971 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1973 if (user[0] == (char)0) pstrcpy(user, context->user);
1975 pstrcpy(workgroup, context->workgroup);
1977 srv = smbc_server(context, server, share, workgroup, user, password);
1981 return -1; /* errno set by smbc_server */
1985 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1987 mode = aDIR | aRONLY;
1990 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1992 if (strcmp(path, "\\") == 0) {
1994 mode = aDIR | aRONLY;
2000 smbc_stat_printjob(srv, path, &size, &m_time);
2001 c_time = a_time = m_time;
2006 if (!cli_rmdir(&srv->cli, path)) {
2008 errno = smbc_errno(context, &srv->cli);
2010 if (errno == EACCES) { /* Check if the dir empty or not */
2012 pstring lpath; /* Local storage to avoid buffer overflows */
2014 smbc_rmdir_dirempty = True; /* Make this so ... */
2016 pstrcpy(lpath, path);
2017 pstrcat(lpath, "\\*");
2019 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2022 /* Fix errno to ignore latest error ... */
2024 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2025 smbc_errno(context, &srv->cli)));
2030 if (smbc_rmdir_dirempty)
2046 * Routine to return the current directory position
2049 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2052 if (!context || !context->_initialized) {
2059 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
2066 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2073 return (off_t) dir->dir_next;
2078 * A routine to run down the list and see if the entry is OK
2081 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2082 struct smbc_dirent *dirent)
2085 /* Run down the list looking for what we want */
2089 struct smbc_dir_list *tmp = list;
2093 if (tmp->dirent == dirent)
2102 return NULL; /* Not found, or an error */
2108 * Routine to seek on a directory
2111 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2113 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2114 struct smbc_dir_list *list_ent = NULL;
2116 if (!context || !context->_initialized) {
2123 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2130 /* Now, check what we were passed and see if it is OK ... */
2132 if (dirent == NULL) { /* Seek to the begining of the list */
2134 dir->dir_next = dir->dir_list;
2139 /* Now, run down the list and make sure that the entry is OK */
2140 /* This may need to be changed if we change the format of the list */
2142 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2144 errno = EINVAL; /* Bad entry */
2149 dir->dir_next = list_ent;
2156 * Routine to fstat a dir
2159 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2162 if (context || !context->_initialized) {
2169 /* No code yet ... */
2176 * Open a print file to be written to by other calls
2179 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2181 fstring server, share, user, password;
2184 if (!context || context->_initialized) {
2198 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2200 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2202 /* What if the path is empty, or the file exists? */
2204 return context->open(context, fname, O_WRONLY, 666);
2209 * Routine to print a file on a remote server ...
2211 * We open the file, which we assume to be on a remote server, and then
2212 * copy it to a print file on the share specified by printq.
2215 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2217 SMBCFILE *fid1, *fid2;
2218 int bytes, saverr, tot_bytes = 0;
2221 if (!c_file || !c_file->_initialized || !c_print ||
2222 !c_print->_initialized) {
2229 if (!fname && !printq) {
2236 /* Try to open the file for reading ... */
2238 if ((fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2240 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2241 return -1; /* smbc_open sets errno */
2245 /* Now, try to open the printer file for writing */
2247 if ((fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2249 saverr = errno; /* Save errno */
2250 c_file->close(c_file, fid1);
2256 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2260 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2263 c_file->close(c_file, fid1);
2264 c_print->close(c_print, fid2);
2273 c_file->close(c_file, fid1); /* We have to close these anyway */
2274 c_print->close(c_print, fid2);
2288 * Routine to list print jobs on a printer share ...
2291 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, void (*fn)(struct print_job_info *))
2294 fstring server, share, user, password, workgroup;
2297 if (!context || !context->_initialized) {
2311 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2313 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2315 if (user[0] == (char)0) pstrcpy(user, context->user);
2317 pstrcpy(workgroup, context->workgroup);
2319 srv = smbc_server(context, server, share, workgroup, user, password);
2323 return -1; /* errno set by smbc_server */
2327 if (cli_print_queue(&srv->cli, fn) < 0) {
2329 errno = smbc_errno(context, &srv->cli);
2339 * Delete a print job from a remote printer share
2342 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2345 fstring server, share, user, password, workgroup;
2349 if (!context || !context->_initialized) {
2363 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2365 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2367 if (user[0] == (char)0) pstrcpy(user, context->user);
2369 pstrcpy(workgroup, context->workgroup);
2371 srv = smbc_server(context, server, share, workgroup, user, password);
2375 return -1; /* errno set by smbc_server */
2379 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2382 errno = smbc_errno(context, &srv->cli);
2383 else if (err == ERRnosuchprintjob)
2394 * Get a new empty handle to fill in with your own info
2396 SMBCCTX * smbc_new_context(void)
2400 context = malloc(sizeof(*context));
2406 ZERO_STRUCTP(context);
2408 /* ADD REASONABLE DEFAULTS */
2410 context->timeout = 20000; /* 20 seconds */
2412 context->open = smbc_open_ctx;
2413 context->creat = smbc_creat_ctx;
2414 context->read = smbc_read_ctx;
2415 context->write = smbc_write_ctx;
2416 context->close = smbc_close_ctx;
2417 context->unlink = smbc_unlink_ctx;
2418 context->rename = smbc_rename_ctx;
2419 context->lseek = smbc_lseek_ctx;
2420 context->stat = smbc_stat_ctx;
2421 context->fstat = smbc_fstat_ctx;
2422 context->opendir = smbc_opendir_ctx;
2423 context->closedir = smbc_closedir_ctx;
2424 context->readdir = smbc_readdir_ctx;
2425 context->getdents = smbc_getdents_ctx;
2426 context->mkdir = smbc_mkdir_ctx;
2427 context->rmdir = smbc_rmdir_ctx;
2428 context->telldir = smbc_telldir_ctx;
2429 context->lseekdir = smbc_lseekdir_ctx;
2430 context->fstatdir = smbc_fstatdir_ctx;
2431 context->open_print_job = smbc_open_print_job_ctx;
2432 context->print_file = smbc_print_file_ctx;
2433 context->list_print_jobs = smbc_list_print_jobs_ctx;
2434 context->unlink_print_job = smbc_unlink_print_job_ctx;
2436 context->callbacks.check_server_fn = smbc_check_server;
2437 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2439 smbc_default_cache_functions(context);
2447 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2448 * and thus you'll be leaking memory if not handled properly.
2451 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2460 DEBUG(1,("Performing aggressive shutdown.\n"));
2462 f = context->_files;
2464 context->close(context, f);
2467 context->_files = NULL;
2469 /* First try to remove the servers the nice way. */
2470 if (context->callbacks.purge_cached_fn(context)) {
2472 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2473 s = context->_servers;
2475 cli_shutdown(&s->cli);
2476 context->callbacks.remove_cached_srv_fn(context, s);
2480 context->_servers = NULL;
2484 /* This is the polite way */
2485 if (context->callbacks.purge_cached_fn(context)) {
2486 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2490 if (context->_servers) {
2491 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2495 if (context->_files) {
2496 DEBUG(1, ("Active files in context, free_context failed.\n"));
2502 /* Things we have to clean up */
2503 SAFE_FREE(context->workgroup);
2504 SAFE_FREE(context->netbios_name);
2505 SAFE_FREE(context->user);
2507 DEBUG(3, ("Context %p succesfully freed\n", context));
2514 * Initialise the library etc
2516 * We accept a struct containing handle information.
2517 * valid values for info->debug from 0 to 100,
2518 * and insist that info->fn must be non-null.
2520 SMBCCTX * smbc_init_context(SMBCCTX * context)
2524 char *user = NULL, *home = NULL;
2531 /* Do not initialise the same client twice */
2532 if (context->_initialized) {
2536 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2543 if (!smbc_initialized) {
2544 /* Do some library wide intialisations the first time we get called */
2546 /* Do we still need this ? */
2549 setup_logging( "libsmbclient", False);
2551 /* Here we would open the smb.conf file if needed ... */
2553 home = getenv("HOME");
2555 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2557 load_interfaces(); /* Load the list of interfaces ... */
2559 in_client = True; /* FIXME, make a param */
2561 if (!lp_load(conf, True, False, False)) {
2564 * Hmmm, what the hell do we do here ... we could not parse the
2565 * config file ... We must return an error ... and keep info around
2566 * about why we failed
2569 errno = ENOENT; /* FIXME: Figure out the correct error response */
2573 reopen_logs(); /* Get logging working ... */
2576 * Block SIGPIPE (from lib/util_sock.c: write())
2577 * It is not needed and should not stop execution
2579 BlockSignals(True, SIGPIPE);
2581 /* Done with one-time initialisation */
2582 smbc_initialized = 1;
2586 if (!context->user) {
2588 * FIXME: Is this the best way to get the user info?
2590 user = getenv("USER");
2591 /* walk around as "guest" if no username can be found */
2592 if (!user) context->user = strdup("guest");
2593 else context->user = strdup(user);
2596 if (!context->netbios_name) {
2598 * We try to get our netbios name from the config. If that fails we fall
2599 * back on constructing our netbios name from our hostname etc
2601 if (global_myname) {
2602 context->netbios_name = strdup(global_myname);
2606 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2609 context->netbios_name = malloc(17);
2610 if (!context->netbios_name) {
2614 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2617 DEBUG(0,("Using netbios name %s.\n", context->netbios_name));
2620 if (!context->workgroup) {
2621 if (lp_workgroup()) {
2622 context->workgroup = strdup(lp_workgroup());
2625 /* TODO: Think about a decent default workgroup */
2626 context->workgroup = strdup("samba");
2629 DEBUG(0,("Using workgroup %s.\n", context->workgroup));
2631 /* shortest timeout is 1 second */
2632 if (context->timeout > 0 && context->timeout < 1000)
2633 context->timeout = 1000;
2636 * FIXME: Should we check the function pointers here?
2639 context->_initialized = 1;