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 "../include/libsmb_internal.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)
183 int ret = cli_errno(c);
185 if (cli_is_dos_error(c)) {
189 cli_dos_error(c, &eclass, &ecode);
191 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
192 (int)eclass, (int)ecode, (int)ecode, ret));
196 status = cli_nt_error(c);
198 DEBUG(3,("smbc errno %s -> %d\n",
199 nt_errstr(status), ret));
207 * returns 0 if the server is in shape. Returns 1 on error
209 * Also useable outside libsmbclient to enable external cache
210 * to do some checks too.
212 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
214 if ( send_keepalive(server->cli.fd) == False )
217 /* connection is ok */
222 * Remove a server from the cached server list it's unused.
223 * On success, 0 is returned. 1 is returned if the server could not be removed.
225 * Also useable outside libsmbclient
227 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
231 /* are we being fooled ? */
232 if (!context || !context->internal ||
233 !context->internal->_initialized || !srv) return 1;
236 /* Check all open files/directories for a relation with this server */
237 for (file = context->internal->_files; file; file=file->next) {
238 if (file->srv == srv) {
240 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
246 DLIST_REMOVE(context->internal->_servers, srv);
248 cli_shutdown(&srv->cli);
250 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
252 context->callbacks.remove_cached_srv_fn(context, srv);
260 * Connect to a server, possibly on an existing connection
262 * Here, what we want to do is: If the server and username
263 * match an existing connection, reuse that, otherwise, establish a
266 * If we have to create a new connection, call the auth_fn to get the
267 * info we need, unless the username and password were passed in.
270 SMBCSRV *smbc_server(SMBCCTX *context,
271 char *server, char *share,
272 char *workgroup, char *username,
278 struct nmb_name called, calling;
279 char *p, *server_n = server;
283 int tried_reverse = 0;
288 if (server[0] == 0) {
295 srv = context->callbacks.get_cached_srv_fn(context, server, share,
296 workgroup, username);
298 if (!auth_called && !srv && (!username[0] || !password[0])) {
299 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
300 username, sizeof(fstring), password, sizeof(fstring));
302 * However, smbc_auth_fn may have picked up info relating to an
303 * existing connection, so try for an existing connection again ...
306 goto check_server_cache;
311 if (context->callbacks.check_server_fn(context, srv)) {
313 * This server is no good anymore
314 * Try to remove it and check for more possible servers in the cache
316 if (context->callbacks.remove_unused_server_fn(context, srv)) {
318 * We could not remove the server completely, remove it from the cache
319 * so we will not get it again. It will be removed when the last file/dir
322 context->callbacks.remove_cached_srv_fn(context, srv);
326 * Maybe there are more cached connections to this server
328 goto check_server_cache;
333 make_nmb_name(&calling, context->netbios_name, 0x0);
334 make_nmb_name(&called , server, 0x20);
336 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
338 if ((p=strchr_m(server_n,'#')) &&
339 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
341 fstrcpy(group, server_n);
342 p = strchr_m(group,'#');
347 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
350 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
354 /* have to open a new connection */
355 if (!cli_initialise(&c)) {
360 c.timeout = context->timeout;
362 if (!cli_connect(&c, server_n, &ip)) {
368 if (!cli_session_request(&c, &calling, &called)) {
370 if (strcmp(called.name, "*SMBSERVER")) {
371 make_nmb_name(&called , "*SMBSERVER", 0x20);
374 else { /* Try one more time, but ensure we don't loop */
376 /* Only try this if server is an IP address ... */
378 if (is_ipaddress(server) && !tried_reverse) {
380 struct in_addr rem_ip;
382 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
383 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
388 tried_reverse++; /* Yuck */
390 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
391 make_nmb_name(&called, remote_name, 0x20);
402 DEBUG(4,(" session request ok\n"));
404 if (!cli_negprot(&c)) {
410 if (!cli_session_setup(&c, username,
411 password, strlen(password),
412 password, strlen(password),
414 /* try an anonymous login if it failed */
415 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
421 DEBUG(4,(" session setup ok\n"));
423 if (!cli_send_tconX(&c, share, "?????",
424 password, strlen(password)+1)) {
425 errno = smbc_errno(context, &c);
430 DEBUG(4,(" tconx ok\n"));
433 * Ok, we have got a nice connection
434 * Let's find a free server_fd
437 srv = (SMBCSRV *)malloc(sizeof(*srv));
445 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
447 /* now add it to the cache (internal or external) */
448 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
449 DEBUG(3, (" Failed to add server to cache\n"));
454 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
455 server, share, srv));
461 if (!srv) return NULL;
468 * Routine to open() a file ...
471 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
473 fstring server, share, user, password, workgroup;
476 SMBCFILE *file = NULL;
479 if (!context || !context->internal ||
480 !context->internal->_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->internal->_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->internal ||
579 !context->internal->_initialized) {
586 return smbc_open_ctx(context, path, creat_bits, mode);
590 * Routine to read() a file ...
593 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
597 if (!context || !context->internal ||
598 !context->internal->_initialized) {
605 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
607 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
614 /* Check that the buffer exists ... */
623 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
627 errno = smbc_errno(context, &file->srv->cli);
634 DEBUG(4, (" --> %d\n", ret));
636 return ret; /* Success, ret bytes of data ... */
641 * Routine to write() a file ...
644 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
648 if (!context || !context->internal ||
649 !context->internal->_initialized) {
656 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
663 /* Check that the buffer exists ... */
672 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
676 errno = smbc_errno(context, &file->srv->cli);
683 return ret; /* Success, 0 bytes of data ... */
687 * Routine to close() a file ...
690 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
694 if (!context || !context->internal ||
695 !context->internal->_initialized) {
702 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
712 return context->closedir(context, file);
716 if (!cli_close(&file->srv->cli, file->cli_fd)) {
718 DEBUG(3, ("cli_close failed on %s. purging server.\n",
720 /* Deallocate slot and remove the server
721 * from the server cache if unused */
722 errno = smbc_errno(context, &file->srv->cli);
724 DLIST_REMOVE(context->internal->_files, file);
725 SAFE_FREE(file->fname);
727 context->callbacks.remove_unused_server_fn(context, srv);
735 return context->closedir(context, file);
739 if (!cli_close(&file->srv->cli, file->cli_fd)) {
740 DEBUG(3, ("cli_close failed on %s. purging server.\n",
742 /* Deallocate slot and remove the server
743 * from the server cache if unused */
744 errno = smbc_errno(context, &file->srv->cli);
746 DLIST_REMOVE(context->internal->_files, file);
747 SAFE_FREE(file->fname);
749 context->callbacks.remove_unused_server_fn(context, srv);
754 DLIST_REMOVE(context->internal->_files, file);
755 SAFE_FREE(file->fname);
762 * Get info from an SMB server on a file. Use a qpathinfo call first
763 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
765 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
766 uint16 *mode, size_t *size,
767 time_t *c_time, time_t *a_time, time_t *m_time,
771 if (!context || !context->internal ||
772 !context->internal->_initialized) {
779 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
781 if (!srv->no_pathinfo2 &&
782 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
783 size, mode, ino)) return True;
785 /* if this is NT then don't bother with the getatr */
786 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
788 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
789 a_time = c_time = m_time;
790 srv->no_pathinfo2 = True;
799 * Routine to unlink() a file
802 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
804 fstring server, share, user, password, workgroup;
808 if (!context || !context->internal ||
809 !context->internal->_initialized) {
811 errno = EINVAL; /* Best I can think of ... */
823 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
825 if (user[0] == (char)0) pstrcpy(user, context->user);
827 pstrcpy(workgroup, context->workgroup);
829 srv = smbc_server(context, server, share, workgroup, user, password);
833 return -1; /* smbc_server sets errno */
837 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
839 int job = smbc_stat_printjob(srv, path, NULL, NULL);
845 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
853 if (!cli_unlink(&srv->cli, path)) {
855 errno = smbc_errno(context, &srv->cli);
857 if (errno == EACCES) { /* Check if the file is a directory */
862 time_t m_time = 0, a_time = 0, c_time = 0;
865 if (!smbc_getatr(context, srv, path, &mode, &size,
866 &c_time, &a_time, &m_time, &ino)) {
868 /* Hmmm, bad error ... What? */
870 errno = smbc_errno(context, &srv->cli);
876 if (IS_DOS_DIR(mode))
879 errno = saverr; /* Restore this */
888 return 0; /* Success ... */
893 * Routine to rename() a file
896 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
897 SMBCCTX *ncontext, const char *nname)
899 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
900 pstring path1, path2;
903 if (!ocontext || !ncontext ||
904 !ocontext->internal || !ncontext->internal ||
905 !ocontext->internal->_initialized ||
906 !ncontext->internal->_initialized) {
908 errno = EINVAL; /* Best I can think of ... */
913 if (!oname || !nname) {
920 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
922 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
924 if (user1[0] == (char)0) pstrcpy(user1, ocontext->user);
926 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
928 if (user2[0] == (char)0) pstrcpy(user2, ncontext->user);
930 if (strcmp(server1, server2) || strcmp(share1, share2) ||
931 strcmp(user1, user2)) {
933 /* Can't rename across file systems, or users?? */
940 pstrcpy(workgroup, ocontext->workgroup);
941 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
942 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
949 if (!cli_rename(&srv->cli, path1, path2)) {
950 int eno = smbc_errno(ocontext, &srv->cli);
953 !cli_unlink(&srv->cli, path2) ||
954 !cli_rename(&srv->cli, path1, path2)) {
962 return 0; /* Success */
967 * A routine to lseek() a file
970 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
974 if (!context || !context->internal ||
975 !context->internal->_initialized) {
982 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
992 return -1; /* Can't lseek a dir ... */
998 file->offset = offset;
1002 file->offset += offset;
1006 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1007 NULL, NULL, NULL) &&
1008 !cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1014 file->offset = size + offset;
1023 return file->offset;
1028 * Generate an inode number from file name for those things that need it
1032 ino_t smbc_inode(SMBCCTX *context, const char *name)
1035 if (!context || !context->internal ||
1036 !context->internal->_initialized) {
1043 if (!*name) return 2; /* FIXME, why 2 ??? */
1044 return (ino_t)str_checksum(name);
1049 * Routine to put basic stat info into a stat structure ... Used by stat and
1054 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1059 if (IS_DOS_DIR(mode)) {
1060 st->st_mode = SMBC_DIR_MODE;
1062 st->st_mode = SMBC_FILE_MODE;
1065 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1066 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1067 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1068 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1071 st->st_blksize = 512;
1072 st->st_blocks = (size+511)/512;
1073 st->st_uid = getuid();
1074 st->st_gid = getgid();
1076 if (IS_DOS_DIR(mode)) {
1082 if (st->st_ino == 0) {
1083 st->st_ino = smbc_inode(context, fname);
1086 return True; /* FIXME: Is this needed ? */
1091 * Routine to stat a file given a name
1094 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1097 fstring server, share, user, password, workgroup;
1099 time_t m_time = 0, a_time = 0, c_time = 0;
1104 if (!context || !context->internal ||
1105 !context->internal->_initialized) {
1107 errno = EINVAL; /* Best I can think of ... */
1119 DEBUG(4, ("smbc_stat(%s)\n", fname));
1121 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1123 if (user[0] == (char)0) pstrcpy(user, context->user);
1125 pstrcpy(workgroup, context->workgroup);
1127 srv = smbc_server(context, server, share, workgroup, user, password);
1131 return -1; /* errno set by smbc_server */
1135 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1137 mode = aDIR | aRONLY;
1140 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1142 if (strcmp(path, "\\") == 0) {
1144 mode = aDIR | aRONLY;
1150 smbc_stat_printjob(srv, path, &size, &m_time);
1151 c_time = a_time = m_time;
1156 if (!smbc_getatr(context, srv, path, &mode, &size,
1157 &c_time, &a_time, &m_time, &ino)) {
1159 errno = smbc_errno(context, &srv->cli);
1166 smbc_setup_stat(context, st, path, size, mode);
1168 st->st_atime = a_time;
1169 st->st_ctime = c_time;
1170 st->st_mtime = m_time;
1171 st->st_dev = srv->dev;
1178 * Routine to stat a file given an fd
1181 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1183 time_t c_time, a_time, m_time;
1188 if (!context || !context->internal ||
1189 !context->internal->_initialized) {
1196 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1205 return context->fstatdir(context, file, st);
1209 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1210 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1211 !cli_getattrE(&file->srv->cli, file->cli_fd,
1212 &mode, &size, &c_time, &a_time, &m_time)) {
1221 smbc_setup_stat(context, st, file->fname, size, mode);
1223 st->st_atime = a_time;
1224 st->st_ctime = c_time;
1225 st->st_mtime = m_time;
1226 st->st_dev = file->srv->dev;
1233 * Routine to open a directory
1237 * smb: which should list all the workgroups available
1239 * smb:workgroup//server
1241 * smb://server/share
1242 * smb://<IP-addr> which should list shares on server
1243 * smb://<IP-addr>/share which should list files on share
1246 static void smbc_remove_dir(SMBCFILE *dir)
1248 struct smbc_dir_list *d,*f;
1255 SAFE_FREE(f->dirent);
1260 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1264 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1266 struct smbc_dirent *dirent;
1270 * Allocate space for the dirent, which must be increased by the
1271 * size of the name and the comment and 1 for the null on the comment.
1272 * The null on the name is already accounted for.
1275 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1276 (comment?strlen(comment):0) + 1;
1278 dirent = malloc(size);
1282 dir->dir_error = ENOMEM;
1287 ZERO_STRUCTP(dirent);
1289 if (dir->dir_list == NULL) {
1291 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1292 if (!dir->dir_list) {
1295 dir->dir_error = ENOMEM;
1299 ZERO_STRUCTP(dir->dir_list);
1301 dir->dir_end = dir->dir_next = dir->dir_list;
1306 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1308 if (!dir->dir_end->next) {
1311 dir->dir_error = ENOMEM;
1315 ZERO_STRUCTP(dir->dir_end->next);
1317 dir->dir_end = dir->dir_end->next;
1321 dir->dir_end->next = NULL;
1322 dir->dir_end->dirent = dirent;
1324 dirent->smbc_type = type;
1325 dirent->namelen = (name?strlen(name):0);
1326 dirent->commentlen = (comment?strlen(comment):0);
1327 dirent->dirlen = size;
1329 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1331 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1332 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1339 list_fn(const char *name, uint32 type, const char *comment, void *state)
1341 SMBCFILE *dir = (SMBCFILE *)state;
1344 /* We need to process the type a little ... */
1346 if (dir->dir_type == SMBC_FILE_SHARE) {
1349 case 0: /* Directory tree */
1350 dirent_type = SMBC_FILE_SHARE;
1354 dirent_type = SMBC_PRINTER_SHARE;
1358 dirent_type = SMBC_COMMS_SHARE;
1362 dirent_type = SMBC_IPC_SHARE;
1366 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1370 else dirent_type = dir->dir_type;
1372 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1374 /* An error occurred, what do we do? */
1375 /* FIXME: Add some code here */
1382 dir_list_fn(file_info *finfo, const char *mask, void *state)
1385 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1386 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1388 /* Handle an error ... */
1390 /* FIXME: Add some code ... */
1396 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1398 fstring server, share, user, password, workgroup;
1400 SMBCSRV *srv = NULL;
1401 SMBCFILE *dir = NULL;
1402 struct in_addr rem_ip;
1404 if (!context || !context->internal ||
1405 !context->internal->_initialized) {
1419 if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1426 if (user[0] == (char)0) pstrcpy(user, context->user);
1428 pstrcpy(workgroup, context->workgroup);
1430 dir = malloc(sizeof(*dir));
1442 dir->fname = strdup(fname);
1446 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1448 if (server[0] == (char)0) {
1450 if (share[0] != (char)0 || path[0] != (char)0) {
1454 SAFE_FREE(dir->fname);
1461 /* We have server and share and path empty ... so list the workgroups */
1462 /* first try to get the LMB for our workgroup, and if that fails, */
1465 if (!(resolve_name(context->workgroup, &rem_ip, 0x1d) ||
1466 resolve_name(context->workgroup, &rem_ip, 0x1b))) {
1468 errno = EINVAL; /* Something wrong with smb.conf? */
1473 dir->dir_type = SMBC_WORKGROUP;
1475 /* find the name of the server ... */
1477 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1479 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server));
1486 * Get a connection to IPC$ on the server if we do not already have one
1489 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1494 SAFE_FREE(dir->fname);
1504 /* Now, list the stuff ... */
1506 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1510 SAFE_FREE(dir->fname);
1513 errno = cli_errno(&srv->cli);
1519 else { /* Server not an empty string ... Check the rest and see what gives */
1521 if (share[0] == (char)0) {
1523 if (path[0] != (char)0) { /* Should not have empty share with path */
1527 SAFE_FREE(dir->fname);
1534 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1535 /* However, we check to see if <server> is an IP address first */
1537 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1538 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
1539 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
1542 dir->dir_type = SMBC_SERVER;
1545 * Get the backup list ...
1549 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1551 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
1552 errno = EPERM; /* FIXME, is this correct */
1558 * Get a connection to IPC$ on the server if we do not already have one
1561 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
1566 SAFE_FREE(dir->fname);
1575 /* Now, list the servers ... */
1577 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1581 SAFE_FREE(dir->fname);
1584 errno = cli_errno(&srv->cli);
1592 if (resolve_name(server, &rem_ip, 0x20)) {
1594 /* Now, list the shares ... */
1596 dir->dir_type = SMBC_FILE_SHARE;
1598 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1603 SAFE_FREE(dir->fname);
1612 /* Now, list the servers ... */
1614 if (cli_RNetShareEnum(&srv->cli, list_fn,
1617 errno = cli_errno(&srv->cli);
1619 SAFE_FREE(dir->fname);
1629 errno = ENODEV; /* Neither the workgroup nor server exists */
1631 SAFE_FREE(dir->fname);
1641 else { /* The server and share are specified ... work from there ... */
1643 /* Well, we connect to the server and list the directory */
1645 dir->dir_type = SMBC_FILE_SHARE;
1647 srv = smbc_server(context, server, share, workgroup, user, password);
1652 SAFE_FREE(dir->fname);
1661 /* Now, list the files ... */
1663 pstrcat(path, "\\*");
1665 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1669 SAFE_FREE(dir->fname);
1672 errno = smbc_errno(context, &srv->cli);
1680 DLIST_ADD(context->internal->_files, dir);
1686 * Routine to close a directory
1689 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1692 if (!context || !context->internal ||
1693 !context->internal->_initialized) {
1700 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1707 smbc_remove_dir(dir); /* Clean it up */
1709 DLIST_REMOVE(context->internal->_files, dir);
1713 SAFE_FREE(dir->fname);
1714 SAFE_FREE(dir); /* Free the space too */
1723 * Routine to get a directory entry
1726 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1728 struct smbc_dirent *dirp, *dirent;
1730 /* Check that all is ok first ... */
1732 if (!context || !context->internal ||
1733 !context->internal->_initialized) {
1740 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1747 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1758 dirent = dir->dir_next->dirent;
1767 /* Hmmm, do I even need to copy it? */
1769 memcpy(context->internal->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1770 dirp = (struct smbc_dirent *)context->internal->_dirent;
1771 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1772 dir->dir_next = dir->dir_next->next;
1774 return (struct smbc_dirent *)context->internal->_dirent;
1780 * Routine to get directory entries
1783 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1785 struct smbc_dir_list *dirlist;
1786 int rem = count, reqd;
1787 char *ndir = (char *)dirp;
1789 /* Check that all is ok first ... */
1791 if (!context || !context->internal ||
1792 !context->internal->_initialized) {
1799 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1806 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1814 * Now, retrieve the number of entries that will fit in what was passed
1815 * We have to figure out if the info is in the list, or we need to
1816 * send a request to the server to get the info.
1819 while ((dirlist = dir->dir_next)) {
1820 struct smbc_dirent *dirent;
1822 if (!dirlist->dirent) {
1824 errno = ENOENT; /* Bad error */
1829 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1830 dirlist->dirent->commentlen + 1))) {
1832 if (rem < count) { /* We managed to copy something */
1838 else { /* Nothing copied ... */
1840 errno = EINVAL; /* Not enough space ... */
1847 dirent = dirlist->dirent;
1849 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1851 ((struct smbc_dirent *)ndir)->comment =
1852 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1858 dir->dir_next = dirlist = dirlist -> next;
1869 * Routine to create a directory ...
1872 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1875 fstring server, share, user, password, workgroup;
1878 if (!context || !context->internal ||
1879 !context->internal->_initialized) {
1893 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1895 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1897 if (user[0] == (char)0) pstrcpy(user, context->user);
1899 pstrcpy(workgroup, context->workgroup);
1901 srv = smbc_server(context, server, share, workgroup, user, password);
1905 return -1; /* errno set by smbc_server */
1909 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1911 mode = aDIR | aRONLY;
1914 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1916 if (strcmp(path, "\\") == 0) {
1918 mode = aDIR | aRONLY;
1924 smbc_stat_printjob(srv, path, &size, &m_time);
1925 c_time = a_time = m_time;
1930 if (!cli_mkdir(&srv->cli, path)) {
1932 errno = smbc_errno(context, &srv->cli);
1942 * Our list function simply checks to see if a directory is not empty
1945 static int smbc_rmdir_dirempty = True;
1947 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
1950 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
1951 smbc_rmdir_dirempty = False;
1956 * Routine to remove a directory
1959 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
1962 fstring server, share, user, password, workgroup;
1965 if (!context || !context->internal ||
1966 !context->internal->_initialized) {
1980 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1982 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1984 if (user[0] == (char)0) pstrcpy(user, context->user);
1986 pstrcpy(workgroup, context->workgroup);
1988 srv = smbc_server(context, server, share, workgroup, user, password);
1992 return -1; /* errno set by smbc_server */
1996 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1998 mode = aDIR | aRONLY;
2001 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2003 if (strcmp(path, "\\") == 0) {
2005 mode = aDIR | aRONLY;
2011 smbc_stat_printjob(srv, path, &size, &m_time);
2012 c_time = a_time = m_time;
2017 if (!cli_rmdir(&srv->cli, path)) {
2019 errno = smbc_errno(context, &srv->cli);
2021 if (errno == EACCES) { /* Check if the dir empty or not */
2023 pstring lpath; /* Local storage to avoid buffer overflows */
2025 smbc_rmdir_dirempty = True; /* Make this so ... */
2027 pstrcpy(lpath, path);
2028 pstrcat(lpath, "\\*");
2030 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2033 /* Fix errno to ignore latest error ... */
2035 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2036 smbc_errno(context, &srv->cli)));
2041 if (smbc_rmdir_dirempty)
2057 * Routine to return the current directory position
2060 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2063 if (!context || !context->internal ||
2064 !context->internal->_initialized) {
2071 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2078 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2085 return (off_t) dir->dir_next;
2090 * A routine to run down the list and see if the entry is OK
2093 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2094 struct smbc_dirent *dirent)
2097 /* Run down the list looking for what we want */
2101 struct smbc_dir_list *tmp = list;
2105 if (tmp->dirent == dirent)
2114 return NULL; /* Not found, or an error */
2120 * Routine to seek on a directory
2123 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2125 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2126 struct smbc_dir_list *list_ent = NULL;
2128 if (!context || !context->internal ||
2129 !context->internal->_initialized) {
2136 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2143 /* Now, check what we were passed and see if it is OK ... */
2145 if (dirent == NULL) { /* Seek to the begining of the list */
2147 dir->dir_next = dir->dir_list;
2152 /* Now, run down the list and make sure that the entry is OK */
2153 /* This may need to be changed if we change the format of the list */
2155 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2157 errno = EINVAL; /* Bad entry */
2162 dir->dir_next = list_ent;
2169 * Routine to fstat a dir
2172 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2175 if (!context || !context->internal ||
2176 !context->internal->_initialized) {
2183 /* No code yet ... */
2190 * Open a print file to be written to by other calls
2193 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2195 fstring server, share, user, password;
2198 if (!context || !context->internal ||
2199 !context->internal->_initialized) {
2213 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2215 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2217 /* What if the path is empty, or the file exists? */
2219 return context->open(context, fname, O_WRONLY, 666);
2224 * Routine to print a file on a remote server ...
2226 * We open the file, which we assume to be on a remote server, and then
2227 * copy it to a print file on the share specified by printq.
2230 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2232 SMBCFILE *fid1, *fid2;
2233 int bytes, saverr, tot_bytes = 0;
2236 if (!c_file || !c_file->internal->_initialized || !c_print ||
2237 !c_print->internal->_initialized) {
2244 if (!fname && !printq) {
2251 /* Try to open the file for reading ... */
2253 if ((fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2255 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2256 return -1; /* smbc_open sets errno */
2260 /* Now, try to open the printer file for writing */
2262 if ((fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2264 saverr = errno; /* Save errno */
2265 c_file->close(c_file, fid1);
2271 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2275 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2278 c_file->close(c_file, fid1);
2279 c_print->close(c_print, fid2);
2288 c_file->close(c_file, fid1); /* We have to close these anyway */
2289 c_print->close(c_print, fid2);
2303 * Routine to list print jobs on a printer share ...
2306 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
2309 fstring server, share, user, password, workgroup;
2312 if (!context || !context->internal ||
2313 !context->internal->_initialized) {
2327 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2329 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2331 if (user[0] == (char)0) pstrcpy(user, context->user);
2333 pstrcpy(workgroup, context->workgroup);
2335 srv = smbc_server(context, server, share, workgroup, user, password);
2339 return -1; /* errno set by smbc_server */
2343 if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
2345 errno = smbc_errno(context, &srv->cli);
2355 * Delete a print job from a remote printer share
2358 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2361 fstring server, share, user, password, workgroup;
2365 if (!context || !context->internal ||
2366 !context->internal->_initialized) {
2380 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2382 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2384 if (user[0] == (char)0) pstrcpy(user, context->user);
2386 pstrcpy(workgroup, context->workgroup);
2388 srv = smbc_server(context, server, share, workgroup, user, password);
2392 return -1; /* errno set by smbc_server */
2396 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2399 errno = smbc_errno(context, &srv->cli);
2400 else if (err == ERRnosuchprintjob)
2411 * Get a new empty handle to fill in with your own info
2413 SMBCCTX * smbc_new_context(void)
2417 context = malloc(sizeof(SMBCCTX));
2423 ZERO_STRUCTP(context);
2425 context->internal = malloc(sizeof(struct smbc_internal_data));
2426 if (!context->internal) {
2431 ZERO_STRUCTP(context->internal);
2434 /* ADD REASONABLE DEFAULTS */
2436 context->timeout = 20000; /* 20 seconds */
2438 context->open = smbc_open_ctx;
2439 context->creat = smbc_creat_ctx;
2440 context->read = smbc_read_ctx;
2441 context->write = smbc_write_ctx;
2442 context->close = smbc_close_ctx;
2443 context->unlink = smbc_unlink_ctx;
2444 context->rename = smbc_rename_ctx;
2445 context->lseek = smbc_lseek_ctx;
2446 context->stat = smbc_stat_ctx;
2447 context->fstat = smbc_fstat_ctx;
2448 context->opendir = smbc_opendir_ctx;
2449 context->closedir = smbc_closedir_ctx;
2450 context->readdir = smbc_readdir_ctx;
2451 context->getdents = smbc_getdents_ctx;
2452 context->mkdir = smbc_mkdir_ctx;
2453 context->rmdir = smbc_rmdir_ctx;
2454 context->telldir = smbc_telldir_ctx;
2455 context->lseekdir = smbc_lseekdir_ctx;
2456 context->fstatdir = smbc_fstatdir_ctx;
2457 context->open_print_job = smbc_open_print_job_ctx;
2458 context->print_file = smbc_print_file_ctx;
2459 context->list_print_jobs = smbc_list_print_jobs_ctx;
2460 context->unlink_print_job = smbc_unlink_print_job_ctx;
2462 context->callbacks.check_server_fn = smbc_check_server;
2463 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2465 smbc_default_cache_functions(context);
2473 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2474 * and thus you'll be leaking memory if not handled properly.
2477 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2486 DEBUG(1,("Performing aggressive shutdown.\n"));
2488 f = context->internal->_files;
2490 context->close(context, f);
2493 context->internal->_files = NULL;
2495 /* First try to remove the servers the nice way. */
2496 if (context->callbacks.purge_cached_fn(context)) {
2498 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2499 s = context->internal->_servers;
2501 cli_shutdown(&s->cli);
2502 context->callbacks.remove_cached_srv_fn(context, s);
2506 context->internal->_servers = NULL;
2510 /* This is the polite way */
2511 if (context->callbacks.purge_cached_fn(context)) {
2512 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2516 if (context->internal->_servers) {
2517 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2521 if (context->internal->_files) {
2522 DEBUG(1, ("Active files in context, free_context failed.\n"));
2528 /* Things we have to clean up */
2529 SAFE_FREE(context->workgroup);
2530 SAFE_FREE(context->netbios_name);
2531 SAFE_FREE(context->user);
2533 DEBUG(3, ("Context %p succesfully freed\n", context));
2534 SAFE_FREE(context->internal);
2541 * Initialise the library etc
2543 * We accept a struct containing handle information.
2544 * valid values for info->debug from 0 to 100,
2545 * and insist that info->fn must be non-null.
2547 SMBCCTX * smbc_init_context(SMBCCTX * context)
2551 char *user = NULL, *home = NULL;
2553 if (!context || !context->internal) {
2558 /* Do not initialise the same client twice */
2559 if (context->internal->_initialized) {
2563 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2570 if (!smbc_initialized) {
2571 /* Do some library wide intialisations the first time we get called */
2573 /* Do we still need this ? */
2576 setup_logging( "libsmbclient", False);
2578 /* Here we would open the smb.conf file if needed ... */
2580 home = getenv("HOME");
2582 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2584 load_interfaces(); /* Load the list of interfaces ... */
2586 in_client = True; /* FIXME, make a param */
2588 if (!lp_load(conf, True, False, False)) {
2591 * Hmmm, what the hell do we do here ... we could not parse the
2592 * config file ... We must return an error ... and keep info around
2593 * about why we failed
2596 errno = ENOENT; /* FIXME: Figure out the correct error response */
2600 reopen_logs(); /* Get logging working ... */
2603 * Block SIGPIPE (from lib/util_sock.c: write())
2604 * It is not needed and should not stop execution
2606 BlockSignals(True, SIGPIPE);
2608 /* Done with one-time initialisation */
2609 smbc_initialized = 1;
2613 if (!context->user) {
2615 * FIXME: Is this the best way to get the user info?
2617 user = getenv("USER");
2618 /* walk around as "guest" if no username can be found */
2619 if (!user) context->user = strdup("guest");
2620 else context->user = strdup(user);
2623 if (!context->netbios_name) {
2625 * We try to get our netbios name from the config. If that fails we fall
2626 * back on constructing our netbios name from our hostname etc
2628 if (global_myname) {
2629 context->netbios_name = strdup(global_myname);
2633 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2636 context->netbios_name = malloc(17);
2637 if (!context->netbios_name) {
2641 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2644 DEBUG(0,("Using netbios name %s.\n", context->netbios_name));
2647 if (!context->workgroup) {
2648 if (lp_workgroup()) {
2649 context->workgroup = strdup(lp_workgroup());
2652 /* TODO: Think about a decent default workgroup */
2653 context->workgroup = strdup("samba");
2656 DEBUG(0,("Using workgroup %s.\n", context->workgroup));
2658 /* shortest timeout is 1 second */
2659 if (context->timeout > 0 && context->timeout < 1000)
2660 context->timeout = 1000;
2663 * FIXME: Should we check the function pointers here?
2666 context->internal->_initialized = 1;