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/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;
51 * Is the logging working / configfile read ?
53 static int smbc_initialized = 0;
56 * Function to parse a path and turn it into components
58 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
60 * smb:// means show all the workgroups
61 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
62 * else, if name<20> exists, list all shares for server ...
65 static const char *smbc_prefix = "smb:";
68 smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
69 char *user, char *password) /* FIXME, lengths of strings */
77 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
80 /* clean_fname(s); causing problems ... */
82 /* see if it has the right prefix */
83 len = strlen(smbc_prefix);
84 if (strncmp(s,smbc_prefix,len) ||
85 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
89 /* Watch the test below, we are testing to see if we should exit */
91 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
97 p += 2; /* Skip the // or \\ */
104 strncpy(server, context->workgroup,
105 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
111 * ok, its for us. Now parse out the server, share etc.
113 * However, we want to parse out [[domain;]user[:password]@] if it
117 /* check that '@' occurs before '/', if '/' exists at all */
118 q = strchr_m(p, '@');
119 r = strchr_m(p, '/');
120 if (q && (!r || q < r)) {
121 pstring username, passwd, domain;
124 next_token(&p, userinfo, "@", sizeof(fstring));
126 username[0] = passwd[0] = domain[0] = 0;
128 if (strchr_m(u, ';')) {
130 next_token(&u, domain, ";", sizeof(fstring));
134 if (strchr_m(u, ':')) {
136 next_token(&u, username, ":", sizeof(fstring));
143 pstrcpy(username, u);
148 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
151 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
155 if (!next_token(&p, server, "/", sizeof(fstring))) {
161 if (*p == (char)0) return 0; /* That's it ... */
163 if (!next_token(&p, share, "/", sizeof(fstring))) {
171 all_string_sub(path, "/", "\\", 0);
177 * Convert an SMB error into a UNIX error ...
180 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
182 int ret = cli_errno(c);
184 if (cli_is_dos_error(c)) {
188 cli_dos_error(c, &eclass, &ecode);
190 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
191 (int)eclass, (int)ecode, (int)ecode, ret));
195 status = cli_nt_error(c);
197 DEBUG(3,("smbc errno %s -> %d\n",
198 nt_errstr(status), ret));
206 * returns 0 if the server is in shape. Returns 1 on error
208 * Also useable outside libsmbclient to enable external cache
209 * to do some checks too.
211 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
213 if ( send_keepalive(server->cli.fd) == False )
216 /* connection is ok */
221 * Remove a server from the list server_table if it's unused.
222 * On success, 0 is returned. 1 is returned if the server could not be removed.
224 * Also useable outside libsmbclient
226 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
230 /* are we being fooled ? */
231 if (!context || !context->_initialized || !srv) return 1;
234 /* Check all open files/directories for a relation with this server */
235 for (file = context->_files; file; file=file->next) {
236 if (file->srv == srv) {
238 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
244 DLIST_REMOVE(context->_servers, srv);
246 cli_shutdown(&srv->cli);
248 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
250 context->callbacks.remove_cached_srv_fn(context, srv);
258 * Connect to a server, possibly on an existing connection
260 * Here, what we want to do is: If the server and username
261 * match an existing connection, reuse that, otherwise, establish a
264 * If we have to create a new connection, call the auth_fn to get the
265 * info we need, unless the username and password were passed in.
268 SMBCSRV *smbc_server(SMBCCTX *context,
269 char *server, char *share,
270 char *workgroup, char *username,
276 struct nmb_name called, calling;
277 char *p, *server_n = server;
281 int tried_reverse = 0;
286 if (server[0] == 0) {
293 srv = context->callbacks.get_cached_srv_fn(context, server, share,
294 workgroup, username);
296 if (!auth_called && !srv && (!username[0] || !password[0])) {
297 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
298 username, sizeof(fstring), password, sizeof(fstring));
300 * However, smbc_auth_fn may have picked up info relating to an
301 * existing connection, so try for an existing connection again ...
304 goto check_server_cache;
309 if (context->callbacks.check_server_fn(context, srv)) {
311 * This server is no good anymore
312 * Try to remove it and check for more possible servers in the cache
314 if (context->callbacks.remove_unused_server_fn(context, srv)) {
316 * We could not remove the server completely, remove it from the cache
317 * so we will not get it again. It will be removed when the last file/dir
320 context->callbacks.remove_cached_srv_fn(context, srv);
324 * Maybe there are more cached connections to this server
326 goto check_server_cache;
331 make_nmb_name(&calling, context->netbios_name, 0x0);
332 make_nmb_name(&called , server, 0x20);
334 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
336 if ((p=strchr_m(server_n,'#')) &&
337 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
339 fstrcpy(group, server_n);
340 p = strchr_m(group,'#');
345 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
348 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
352 /* have to open a new connection */
353 if (!cli_initialise(&c)) {
358 c.timeout = context->timeout;
360 if (!cli_connect(&c, server_n, &ip)) {
366 if (!cli_session_request(&c, &calling, &called)) {
368 if (strcmp(called.name, "*SMBSERVER")) {
369 make_nmb_name(&called , "*SMBSERVER", 0x20);
372 else { /* Try one more time, but ensure we don't loop */
374 /* Only try this if server is an IP address ... */
376 if (is_ipaddress(server) && !tried_reverse) {
378 struct in_addr rem_ip;
380 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
381 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
386 tried_reverse++; /* Yuck */
388 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
389 make_nmb_name(&called, remote_name, 0x20);
400 DEBUG(4,(" session request ok\n"));
402 if (!cli_negprot(&c)) {
408 if (!cli_session_setup(&c, username,
409 password, strlen(password),
410 password, strlen(password),
412 /* try an anonymous login if it failed */
413 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
419 DEBUG(4,(" session setup ok\n"));
421 if (!cli_send_tconX(&c, share, "?????",
422 password, strlen(password)+1)) {
423 errno = smbc_errno(context, &c);
428 DEBUG(4,(" tconx ok\n"));
431 * Ok, we have got a nice connection
432 * Let's find a free server_fd
435 srv = (SMBCSRV *)malloc(sizeof(*srv));
443 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
445 /* now add it to the cache (internal or external) */
446 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
447 DEBUG(3, (" Failed to add server to cache\n"));
452 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
453 server, share, srv));
459 if (!srv) return NULL;
466 * Routine to open() a file ...
469 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
471 fstring server, share, user, password, workgroup;
474 SMBCFILE *file = NULL;
477 if (!context || !context->_initialized) {
479 errno = EINVAL; /* Best I can think of ... */
491 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
493 if (user[0] == (char)0) pstrcpy(user, context->user);
495 pstrcpy(workgroup, context->workgroup);
497 srv = smbc_server(context, server, share, workgroup, user, password);
501 if (errno == EPERM) errno = EACCES;
502 return NULL; /* smbc_server sets errno */
506 /* Hmmm, the test for a directory is suspect here ... FIXME */
508 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
515 file = malloc(sizeof(SMBCFILE));
526 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
528 /* Handle the error ... */
531 errno = smbc_errno(context, &srv->cli);
536 /* Fill in file struct */
539 file->fname = strdup(fname);
544 DLIST_ADD(context->_files, file);
549 /* Check if opendir needed ... */
554 eno = smbc_errno(context, &srv->cli);
555 file = context->opendir(context, fname);
556 if (!file) errno = eno;
561 errno = EINVAL; /* FIXME, correct errno ? */
567 * Routine to create a file
570 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
572 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
575 if (!context || !context->_initialized) {
582 return smbc_open_ctx(context, path, creat_bits, mode);
586 * Routine to read() a file ...
589 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
593 if (!context || !context->_initialized) {
600 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
602 if (!file || !DLIST_CONTAINS(context->_files, file)) {
609 /* Check that the buffer exists ... */
618 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
622 errno = smbc_errno(context, &file->srv->cli);
629 DEBUG(4, (" --> %d\n", ret));
631 return ret; /* Success, ret bytes of data ... */
636 * Routine to write() a file ...
639 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
643 if (!context || context->_initialized) {
650 if (!file || !DLIST_CONTAINS(context->_files, file)) {
657 /* Check that the buffer exists ... */
666 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
670 errno = smbc_errno(context, &file->srv->cli);
677 return ret; /* Success, 0 bytes of data ... */
681 * Routine to close() a file ...
684 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
688 if (!context || !context->_initialized) {
695 if (!file || !DLIST_CONTAINS(context->_files, file)) {
705 return context->closedir(context, file);
709 if (!cli_close(&file->srv->cli, file->cli_fd)) {
711 DEBUG(3, ("cli_close failed on %s. purging server.\n",
713 /* Deallocate slot and remove the server
714 * from the server cache if unused */
715 errno = smbc_errno(context, &file->srv->cli);
717 DLIST_REMOVE(context->_files, file);
718 SAFE_FREE(file->fname);
720 context->callbacks.remove_unused_server_fn(context, srv);
728 return context->closedir(context, file);
732 if (!cli_close(&file->srv->cli, file->cli_fd)) {
733 DEBUG(3, ("cli_close failed on %s. purging server.\n",
735 /* Deallocate slot and remove the server
736 * from the server cache if unused */
737 errno = smbc_errno(context, &file->srv->cli);
739 DLIST_REMOVE(context->_files, file);
740 SAFE_FREE(file->fname);
742 context->callbacks.remove_unused_server_fn(context, srv);
747 DLIST_REMOVE(context->_files, file);
748 SAFE_FREE(file->fname);
755 * Get info from an SMB server on a file. Use a qpathinfo call first
756 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
758 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
759 uint16 *mode, size_t *size,
760 time_t *c_time, time_t *a_time, time_t *m_time,
764 if (!context || !context->_initialized) {
771 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
773 if (!srv->no_pathinfo2 &&
774 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
775 size, mode, ino)) return True;
777 /* if this is NT then don't bother with the getatr */
778 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
780 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
781 a_time = c_time = m_time;
782 srv->no_pathinfo2 = True;
791 * Routine to unlink() a file
794 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
796 fstring server, share, user, password, workgroup;
800 if (!context || context->_initialized) {
802 errno = EINVAL; /* Best I can think of ... */
814 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
816 if (user[0] == (char)0) pstrcpy(user, context->user);
818 pstrcpy(workgroup, context->workgroup);
820 srv = smbc_server(context, server, share, workgroup, user, password);
824 return -1; /* smbc_server sets errno */
828 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
830 int job = smbc_stat_printjob(srv, path, NULL, NULL);
836 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
844 if (!cli_unlink(&srv->cli, path)) {
846 errno = smbc_errno(context, &srv->cli);
848 if (errno == EACCES) { /* Check if the file is a directory */
853 time_t m_time = 0, a_time = 0, c_time = 0;
856 if (!smbc_getatr(context, srv, path, &mode, &size,
857 &c_time, &a_time, &m_time, &ino)) {
859 /* Hmmm, bad error ... What? */
861 errno = smbc_errno(context, &srv->cli);
867 if (IS_DOS_DIR(mode))
870 errno = saverr; /* Restore this */
879 return 0; /* Success ... */
884 * Routine to rename() a file
887 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
888 SMBCCTX *ncontext, const char *nname)
890 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
891 pstring path1, path2;
894 if (!ocontext || !ncontext ||
895 !ocontext->_initialized || !ncontext->_initialized) {
897 errno = EINVAL; /* Best I can think of ... */
902 if (!oname || !nname) {
909 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
911 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
913 if (user1[0] == (char)0) pstrcpy(user1, ocontext->user);
915 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
917 if (user2[0] == (char)0) pstrcpy(user2, ncontext->user);
919 if (strcmp(server1, server2) || strcmp(share1, share2) ||
920 strcmp(user1, user2)) {
922 /* Can't rename across file systems, or users?? */
929 pstrcpy(workgroup, ocontext->workgroup);
930 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
931 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
938 if (!cli_rename(&srv->cli, path1, path2)) {
939 int eno = smbc_errno(ocontext, &srv->cli);
942 !cli_unlink(&srv->cli, path2) ||
943 !cli_rename(&srv->cli, path1, path2)) {
951 return 0; /* Success */
956 * A routine to lseek() a file
959 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
963 if (!context || !context->_initialized) {
970 if (!file || !DLIST_CONTAINS(context->_files, file)) {
980 return -1; /* Can't lseek a dir ... */
986 file->offset = offset;
990 file->offset += offset;
994 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
996 !cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1002 file->offset = size + offset;
1011 return file->offset;
1016 * Generate an inode number from file name for those things that need it
1020 ino_t smbc_inode(SMBCCTX *context, const char *name)
1023 if (!context || !context->_initialized) {
1030 if (!*name) return 2; /* FIXME, why 2 ??? */
1031 return (ino_t)str_checksum(name);
1036 * Routine to put basic stat info into a stat structure ... Used by stat and
1041 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1046 if (IS_DOS_DIR(mode)) {
1047 st->st_mode = SMBC_DIR_MODE;
1049 st->st_mode = SMBC_FILE_MODE;
1052 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1053 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1054 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1055 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1058 st->st_blksize = 512;
1059 st->st_blocks = (size+511)/512;
1060 st->st_uid = getuid();
1061 st->st_gid = getgid();
1063 if (IS_DOS_DIR(mode)) {
1069 if (st->st_ino == 0) {
1070 st->st_ino = smbc_inode(context, fname);
1073 return True; /* FIXME: Is this needed ? */
1078 * Routine to stat a file given a name
1081 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1084 fstring server, share, user, password, workgroup;
1086 time_t m_time = 0, a_time = 0, c_time = 0;
1091 if (!context || !context->_initialized) {
1093 errno = EINVAL; /* Best I can think of ... */
1105 DEBUG(4, ("smbc_stat(%s)\n", fname));
1107 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1109 if (user[0] == (char)0) pstrcpy(user, context->user);
1111 pstrcpy(workgroup, context->workgroup);
1113 srv = smbc_server(context, server, share, workgroup, user, password);
1117 return -1; /* errno set by smbc_server */
1121 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1123 mode = aDIR | aRONLY;
1126 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1128 if (strcmp(path, "\\") == 0) {
1130 mode = aDIR | aRONLY;
1136 smbc_stat_printjob(srv, path, &size, &m_time);
1137 c_time = a_time = m_time;
1142 if (!smbc_getatr(context, srv, path, &mode, &size,
1143 &c_time, &a_time, &m_time, &ino)) {
1145 errno = smbc_errno(context, &srv->cli);
1152 smbc_setup_stat(context, st, path, size, mode);
1154 st->st_atime = a_time;
1155 st->st_ctime = c_time;
1156 st->st_mtime = m_time;
1157 st->st_dev = srv->dev;
1164 * Routine to stat a file given an fd
1167 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1169 time_t c_time, a_time, m_time;
1174 if (!context || !context->_initialized) {
1181 if (!file || !DLIST_CONTAINS(context->_files, file)) {
1190 return context->fstatdir(context, file, st);
1194 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1195 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1196 !cli_getattrE(&file->srv->cli, file->cli_fd,
1197 &mode, &size, &c_time, &a_time, &m_time)) {
1206 smbc_setup_stat(context, st, file->fname, size, mode);
1208 st->st_atime = a_time;
1209 st->st_ctime = c_time;
1210 st->st_mtime = m_time;
1211 st->st_dev = file->srv->dev;
1218 * Routine to open a directory
1222 * smb: which should list all the workgroups available
1224 * smb:workgroup//server
1226 * smb://server/share
1227 * smb://<IP-addr> which should list shares on server
1228 * smb://<IP-addr>/share which should list files on share
1231 static void smbc_remove_dir(SMBCFILE *dir)
1233 struct smbc_dir_list *d,*f;
1240 SAFE_FREE(f->dirent);
1245 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1249 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1251 struct smbc_dirent *dirent;
1255 * Allocate space for the dirent, which must be increased by the
1256 * size of the name and the comment and 1 for the null on the comment.
1257 * The null on the name is already accounted for.
1260 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1261 (comment?strlen(comment):0) + 1;
1263 dirent = malloc(size);
1267 dir->dir_error = ENOMEM;
1272 ZERO_STRUCTP(dirent);
1274 ZERO_STRUCTP(dirent);
1277 if (dir->dir_list == NULL) {
1279 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1280 if (!dir->dir_list) {
1283 dir->dir_error = ENOMEM;
1287 ZERO_STRUCTP(dir->dir_list);
1289 dir->dir_end = dir->dir_next = dir->dir_list;
1294 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1296 if (!dir->dir_end->next) {
1299 dir->dir_error = ENOMEM;
1303 ZERO_STRUCTP(dir->dir_end->next);
1305 dir->dir_end = dir->dir_end->next;
1309 dir->dir_end->next = NULL;
1310 dir->dir_end->dirent = dirent;
1312 dirent->smbc_type = type;
1313 dirent->namelen = (name?strlen(name):0);
1314 dirent->commentlen = (comment?strlen(comment):0);
1315 dirent->dirlen = size;
1317 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1319 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1320 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1327 list_fn(const char *name, uint32 type, const char *comment, void *state)
1329 SMBCFILE *dir = (SMBCFILE *)state;
1332 /* We need to process the type a little ... */
1334 if (dir->dir_type == SMBC_FILE_SHARE) {
1337 case 0: /* Directory tree */
1338 dirent_type = SMBC_FILE_SHARE;
1342 dirent_type = SMBC_PRINTER_SHARE;
1346 dirent_type = SMBC_COMMS_SHARE;
1350 dirent_type = SMBC_IPC_SHARE;
1354 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1357 ZERO_STRUCTP(dir->dir_list);
1360 else dirent_type = dir->dir_type;
1362 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1364 /* An error occurred, what do we do? */
1365 /* FIXME: Add some code here */
1372 dir_list_fn(file_info *finfo, const char *mask, void *state)
1375 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1376 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1378 /* Handle an error ... */
1380 /* FIXME: Add some code ... */
1386 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1388 fstring server, share, user, password, workgroup;
1390 SMBCSRV *srv = NULL;
1391 SMBCFILE *dir = NULL;
1392 struct in_addr rem_ip;
1395 if (!context || !context->_initialized) {
1409 if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1416 if (user[0] == (char)0) pstrcpy(user, context->user);
1418 pstrcpy(workgroup, context->workgroup);
1420 dir = malloc(sizeof(*dir));
1432 dir->fname = strdup(fname);
1436 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1438 if (server[0] == (char)0) {
1440 if (share[0] != (char)0 || path[0] != (char)0) {
1444 SAFE_FREE(dir->fname);
1451 /* We have server and share and path empty ... so list the workgroups */
1452 /* first try to get the LMB for our workgroup, and if that fails, */
1455 if (!(resolve_name(context->workgroup, &rem_ip, 0x1d) ||
1456 resolve_name(context->workgroup, &rem_ip, 0x1b))) {
1458 errno = EINVAL; /* Something wrong with smb.conf? */
1463 dir->dir_type = SMBC_WORKGROUP;
1465 /* find the name of the server ... */
1467 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1469 DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server));
1476 * Get a connection to IPC$ on the server if we do not already have one
1479 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1484 SAFE_FREE(dir->fname);
1491 ZERO_STRUCTP(dir->dir_end);
1495 /* Now, list the stuff ... */
1497 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1501 SAFE_FREE(dir->fname);
1504 errno = cli_errno(&srv->cli);
1510 else { /* Server not an empty string ... Check the rest and see what gives */
1512 if (share[0] == (char)0) {
1514 if (path[0] != (char)0) { /* Should not have empty share with path */
1518 SAFE_FREE(dir->fname);
1525 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1526 /* However, we check to see if <server> is an IP address first */
1528 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1529 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
1530 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
1533 dir->dir_type = SMBC_SERVER;
1536 * Get the backup list ...
1540 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1542 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
1543 errno = EPERM; /* FIXME, is this correct */
1549 * Get a connection to IPC$ on the server if we do not already have one
1552 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
1557 SAFE_FREE(dir->fname);
1566 /* Now, list the servers ... */
1568 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1572 SAFE_FREE(dir->fname);
1575 errno = cli_errno(&srv->cli);
1583 if (resolve_name(server, &rem_ip, 0x20)) {
1585 /* Now, list the shares ... */
1587 dir->dir_type = SMBC_FILE_SHARE;
1589 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1594 SAFE_FREE(dir->fname);
1603 /* Now, list the servers ... */
1605 if (cli_RNetShareEnum(&srv->cli, list_fn,
1608 errno = cli_errno(&srv->cli);
1610 SAFE_FREE(dir->fname);
1620 errno = ENODEV; /* Neither the workgroup nor server exists */
1622 SAFE_FREE(dir->fname);
1632 else { /* The server and share are specified ... work from there ... */
1634 /* Well, we connect to the server and list the directory */
1636 dir->dir_type = SMBC_FILE_SHARE;
1638 srv = smbc_server(context, server, share, workgroup, user, password);
1643 SAFE_FREE(dir->fname);
1652 /* Now, list the files ... */
1654 pstrcat(path, "\\*");
1656 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1660 SAFE_FREE(dir->fname);
1663 errno = smbc_errno(context, &srv->cli);
1671 DLIST_ADD(context->_files, dir);
1677 * Routine to close a directory
1680 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1683 if (!context || !context->_initialized) {
1690 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1697 smbc_remove_dir(dir); /* Clean it up */
1699 DLIST_REMOVE(context->_files, dir);
1703 SAFE_FREE(dir->fname);
1704 SAFE_FREE(dir); /* Free the space too */
1713 * Routine to get a directory entry
1716 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1718 struct smbc_dirent *dirp, *dirent;
1720 /* Check that all is ok first ... */
1722 if (!context || !context->_initialized) {
1729 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1736 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1747 dirent = dir->dir_next->dirent;
1756 /* Hmmm, do I even need to copy it? */
1758 memcpy(context->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1759 dirp = (struct smbc_dirent *)context->_dirent;
1760 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1761 dir->dir_next = dir->dir_next->next;
1763 return (struct smbc_dirent *)context->_dirent;
1769 * Routine to get directory entries
1772 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1774 struct smbc_dir_list *dirlist;
1775 int rem = count, reqd;
1776 char *ndir = (char *)dirp;
1778 /* Check that all is ok first ... */
1780 if (!context || !context->_initialized) {
1787 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1794 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1802 * Now, retrieve the number of entries that will fit in what was passed
1803 * We have to figure out if the info is in the list, or we need to
1804 * send a request to the server to get the info.
1807 while ((dirlist = dir->dir_next)) {
1808 struct smbc_dirent *dirent;
1810 if (!dirlist->dirent) {
1812 errno = ENOENT; /* Bad error */
1817 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1818 dirlist->dirent->commentlen + 1))) {
1820 if (rem < count) { /* We managed to copy something */
1826 else { /* Nothing copied ... */
1828 errno = EINVAL; /* Not enough space ... */
1835 dirent = dirlist->dirent;
1837 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1839 ((struct smbc_dirent *)ndir)->comment =
1840 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1846 dir->dir_next = dirlist = dirlist -> next;
1857 * Routine to create a directory ...
1860 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1863 fstring server, share, user, password, workgroup;
1866 if (!context || !context->_initialized) {
1880 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1882 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1884 if (user[0] == (char)0) pstrcpy(user, context->user);
1886 pstrcpy(workgroup, context->workgroup);
1888 srv = smbc_server(context, server, share, workgroup, user, password);
1892 return -1; /* errno set by smbc_server */
1896 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1898 mode = aDIR | aRONLY;
1901 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1903 if (strcmp(path, "\\") == 0) {
1905 mode = aDIR | aRONLY;
1911 smbc_stat_printjob(srv, path, &size, &m_time);
1912 c_time = a_time = m_time;
1917 if (!cli_mkdir(&srv->cli, path)) {
1919 errno = smbc_errno(context, &srv->cli);
1929 * Our list function simply checks to see if a directory is not empty
1932 static int smbc_rmdir_dirempty = True;
1934 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
1937 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
1938 smbc_rmdir_dirempty = False;
1943 * Routine to remove a directory
1946 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
1949 fstring server, share, user, password, workgroup;
1952 if (!context || !context->_initialized) {
1966 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1968 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1970 if (user[0] == (char)0) pstrcpy(user, context->user);
1972 pstrcpy(workgroup, context->workgroup);
1974 srv = smbc_server(context, server, share, workgroup, user, password);
1978 return -1; /* errno set by smbc_server */
1982 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1984 mode = aDIR | aRONLY;
1987 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1989 if (strcmp(path, "\\") == 0) {
1991 mode = aDIR | aRONLY;
1997 smbc_stat_printjob(srv, path, &size, &m_time);
1998 c_time = a_time = m_time;
2003 if (!cli_rmdir(&srv->cli, path)) {
2005 errno = smbc_errno(context, &srv->cli);
2007 if (errno == EACCES) { /* Check if the dir empty or not */
2009 pstring lpath; /* Local storage to avoid buffer overflows */
2011 smbc_rmdir_dirempty = True; /* Make this so ... */
2013 pstrcpy(lpath, path);
2014 pstrcat(lpath, "\\*");
2016 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2019 /* Fix errno to ignore latest error ... */
2021 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2022 smbc_errno(context, &srv->cli)));
2027 if (smbc_rmdir_dirempty)
2043 * Routine to return the current directory position
2046 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2049 if (!context || !context->_initialized) {
2056 if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
2063 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2070 return (off_t) dir->dir_next;
2075 * A routine to run down the list and see if the entry is OK
2078 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2079 struct smbc_dirent *dirent)
2082 /* Run down the list looking for what we want */
2086 struct smbc_dir_list *tmp = list;
2090 if (tmp->dirent == dirent)
2099 return NULL; /* Not found, or an error */
2105 * Routine to seek on a directory
2108 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2110 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2111 struct smbc_dir_list *list_ent = NULL;
2113 if (!context || !context->_initialized) {
2120 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2127 /* Now, check what we were passed and see if it is OK ... */
2129 if (dirent == NULL) { /* Seek to the begining of the list */
2131 dir->dir_next = dir->dir_list;
2136 /* Now, run down the list and make sure that the entry is OK */
2137 /* This may need to be changed if we change the format of the list */
2139 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2141 errno = EINVAL; /* Bad entry */
2146 dir->dir_next = list_ent;
2153 * Routine to fstat a dir
2156 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2159 if (context || !context->_initialized) {
2166 /* No code yet ... */
2173 * Open a print file to be written to by other calls
2176 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2178 fstring server, share, user, password;
2181 if (!context || context->_initialized) {
2195 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2197 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2199 /* What if the path is empty, or the file exists? */
2201 return context->open(context, fname, O_WRONLY, 666);
2206 * Routine to print a file on a remote server ...
2208 * We open the file, which we assume to be on a remote server, and then
2209 * copy it to a print file on the share specified by printq.
2212 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2214 SMBCFILE *fid1, *fid2;
2215 int bytes, saverr, tot_bytes = 0;
2218 if (!c_file || !c_file->_initialized || !c_print ||
2219 !c_print->_initialized) {
2226 if (!fname && !printq) {
2233 /* Try to open the file for reading ... */
2235 if ((fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2237 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2238 return -1; /* smbc_open sets errno */
2242 /* Now, try to open the printer file for writing */
2244 if ((fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2246 saverr = errno; /* Save errno */
2247 c_file->close(c_file, fid1);
2253 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2257 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2260 c_file->close(c_file, fid1);
2261 c_print->close(c_print, fid2);
2270 c_file->close(c_file, fid1); /* We have to close these anyway */
2271 c_print->close(c_print, fid2);
2285 * Routine to list print jobs on a printer share ...
2288 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, void (*fn)(struct print_job_info *))
2291 fstring server, share, user, password, workgroup;
2294 if (!context || !context->_initialized) {
2308 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2310 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2312 if (user[0] == (char)0) pstrcpy(user, context->user);
2314 pstrcpy(workgroup, context->workgroup);
2316 srv = smbc_server(context, server, share, workgroup, user, password);
2320 return -1; /* errno set by smbc_server */
2324 if (cli_print_queue(&srv->cli, fn) < 0) {
2326 errno = smbc_errno(context, &srv->cli);
2336 * Delete a print job from a remote printer share
2339 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2342 fstring server, share, user, password, workgroup;
2346 if (!context || !context->_initialized) {
2360 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2362 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2364 if (user[0] == (char)0) pstrcpy(user, context->user);
2366 pstrcpy(workgroup, context->workgroup);
2368 srv = smbc_server(context, server, share, workgroup, user, password);
2372 return -1; /* errno set by smbc_server */
2376 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2379 errno = smbc_errno(context, &srv->cli);
2380 else if (err == ERRnosuchprintjob)
2391 * Get a new empty handle to fill in with your own info
2393 SMBCCTX * smbc_new_context(void)
2397 context = malloc(sizeof(*context));
2403 ZERO_STRUCTP(context);
2405 /* ADD REASONABLE DEFAULTS */
2407 context->timeout = 20000; /* 20 seconds */
2409 context->open = smbc_open_ctx;
2410 context->creat = smbc_creat_ctx;
2411 context->read = smbc_read_ctx;
2412 context->write = smbc_write_ctx;
2413 context->close = smbc_close_ctx;
2414 context->unlink = smbc_unlink_ctx;
2415 context->rename = smbc_rename_ctx;
2416 context->lseek = smbc_lseek_ctx;
2417 context->stat = smbc_stat_ctx;
2418 context->fstat = smbc_fstat_ctx;
2419 context->opendir = smbc_opendir_ctx;
2420 context->closedir = smbc_closedir_ctx;
2421 context->readdir = smbc_readdir_ctx;
2422 context->getdents = smbc_getdents_ctx;
2423 context->mkdir = smbc_mkdir_ctx;
2424 context->rmdir = smbc_rmdir_ctx;
2425 context->telldir = smbc_telldir_ctx;
2426 context->lseekdir = smbc_lseekdir_ctx;
2427 context->fstatdir = smbc_fstatdir_ctx;
2428 context->open_print_job = smbc_open_print_job_ctx;
2429 context->print_file = smbc_print_file_ctx;
2430 context->list_print_jobs = smbc_list_print_jobs_ctx;
2431 context->unlink_print_job = smbc_unlink_print_job_ctx;
2433 context->callbacks.check_server_fn = smbc_check_server;
2434 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2436 smbc_default_cache_functions(context);
2444 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2445 * and thus you'll be leaking memory if not handled properly.
2448 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2457 DEBUG(1,("Performing aggressive shutdown.\n"));
2459 f = context->_files;
2461 context->close(context, f);
2464 context->_files = NULL;
2466 /* First try to remove the servers the nice way. */
2467 if (context->callbacks.purge_cached_fn(context)) {
2469 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2470 s = context->_servers;
2472 cli_shutdown(&s->cli);
2473 context->callbacks.remove_cached_srv_fn(context, s);
2477 context->_servers = NULL;
2481 /* This is the polite way */
2482 if (context->callbacks.purge_cached_fn(context)) {
2483 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2487 if (context->_servers) {
2488 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2492 if (context->_files) {
2493 DEBUG(1, ("Active files in context, free_context failed.\n"));
2499 /* Things we have to clean up */
2500 SAFE_FREE(context->workgroup);
2501 SAFE_FREE(context->netbios_name);
2502 SAFE_FREE(context->user);
2504 DEBUG(3, ("Context %p succesfully freed\n", context));
2511 * Initialise the library etc
2513 * We accept a struct containing handle information.
2514 * valid values for info->debug from 0 to 100,
2515 * and insist that info->fn must be non-null.
2517 SMBCCTX * smbc_init_context(SMBCCTX * context)
2521 char *user = NULL, *home = NULL;
2528 /* Do not initialise the same client twice */
2529 if (context->_initialized) {
2533 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2540 if (!smbc_initialized) {
2541 /* Do some library wide intialisations the first time we get called */
2543 /* Do we still need this ? */
2546 setup_logging( "libsmbclient", False);
2548 /* Here we would open the smb.conf file if needed ... */
2550 home = getenv("HOME");
2552 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2554 load_interfaces(); /* Load the list of interfaces ... */
2556 in_client = True; /* FIXME, make a param */
2558 if (!lp_load(conf, True, False, False)) {
2561 * Hmmm, what the hell do we do here ... we could not parse the
2562 * config file ... We must return an error ... and keep info around
2563 * about why we failed
2566 errno = ENOENT; /* FIXME: Figure out the correct error response */
2570 reopen_logs(); /* Get logging working ... */
2573 * Block SIGPIPE (from lib/util_sock.c: write())
2574 * It is not needed and should not stop execution
2576 BlockSignals(True, SIGPIPE);
2578 /* Done with one-time initialisation */
2579 smbc_initialized = 1;
2583 if (!context->user) {
2585 * FIXME: Is this the best way to get the user info?
2587 user = getenv("USER");
2588 /* walk around as "guest" if no username can be found */
2589 if (!user) context->user = strdup("guest");
2590 else context->user = strdup(user);
2593 if (!context->netbios_name) {
2595 * We try to get our netbios name from the config. If that fails we fall
2596 * back on constructing our netbios name from our hostname etc
2598 if (global_myname()) {
2599 context->netbios_name = strdup(global_myname());
2603 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2606 context->netbios_name = malloc(17);
2607 if (!context->netbios_name) {
2611 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2614 DEBUG(0,("Using netbios name %s.\n", context->netbios_name));
2617 if (!context->workgroup) {
2618 if (lp_workgroup()) {
2619 context->workgroup = strdup(lp_workgroup());
2622 /* TODO: Think about a decent default workgroup */
2623 context->workgroup = strdup("samba");
2626 DEBUG(0,("Using workgroup %s.\n", context->workgroup));
2628 /* shortest timeout is 1 second */
2629 if (context->timeout > 0 && context->timeout < 1000)
2630 context->timeout = 1000;
2633 * FIXME: Should we check the function pointers here?
2636 context->_initialized = 1;