2 Unix SMB/Netbios implementation.
4 SMB client library implementation
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Richard Sharpe 2000
7 Copyright (C) John Terpstra 2000
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.
25 #include "libsmbclient.h"
27 /* Structure for servers ... Held here so we don't need an include ...
28 * May be better to put in an include file
32 struct smbc_server *next, *prev;
42 /* Keep directory entries in a list */
43 struct smbc_dir_list {
44 struct smbc_dir_list *next;
45 struct smbc_dirent *dirent;
53 struct smbc_server *srv;
55 struct smbc_dir_list *dir_list, *dir_end, *dir_next;
56 int dir_type, dir_error;
59 int smbc_fstatdir(int fd, struct stat *st); /* Forward decl */
60 BOOL smbc_getatr(struct smbc_server *srv, char *path,
61 uint16 *mode, size_t *size,
62 time_t *c_time, time_t *a_time, time_t *m_time,
65 extern BOOL in_client;
66 static int smbc_initialized = 0;
67 static smbc_get_auth_data_fn smbc_auth_fn = NULL;
68 /*static int smbc_debug;*/
69 static int smbc_start_fd;
70 static int smbc_max_fd = 10000;
71 static struct smbc_file **smbc_file_table;
72 static struct smbc_server *smbc_srvs;
73 static pstring my_netbios_name;
74 static pstring smbc_user;
77 * Function to parse a path and turn it into components
79 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
81 * smb:// means show all the workgroups
82 * smb://name/ means, if name<1D> exists, list servers in workgroup,
83 * else, if name<20> exists, list all shares for server ...
86 static const char *smbc_prefix = "smb:";
89 smbc_parse_path(const char *fname, char *server, char *share, char *path,
90 char *user, char *password) /* FIXME, lengths of strings */
97 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
100 /* clean_fname(s); causing problems ... */
102 /* see if it has the right prefix */
103 len = strlen(smbc_prefix);
104 if (strncmp(s,smbc_prefix,len) ||
105 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
109 /* Watch the test below, we are testing to see if we should exit */
111 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
117 p += 2; /* Skip the // or \\ */
124 strncpy(server, (char *)lp_workgroup(), 16); /* FIXME: Danger here */
130 * ok, its for us. Now parse out the server, share etc.
132 * However, we want to parse out [[domain;]user[:password]@] if it
136 if (strchr_m(p, '@')) {
137 pstring username, passwd, domain;
140 next_token(&p, userinfo, "@", sizeof(fstring));
142 username[0] = passwd[0] = domain[0] = 0;
144 if (strchr_m(u, ';')) {
146 next_token(&u, domain, ";", sizeof(fstring));
150 if (strchr_m(u, ':')) {
152 next_token(&u, username, ":", sizeof(fstring));
159 pstrcpy(username, u);
164 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
167 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
171 if (!next_token(&p, server, "/", sizeof(fstring))) {
177 if (*p == (char)0) return 0; /* That's it ... */
179 if (!next_token(&p, share, "/", sizeof(fstring))) {
187 all_string_sub(path, "/", "\\", 0);
193 * Convert an SMB error into a UNIX error ...
196 int smbc_errno(struct cli_state *c)
200 if (cli_is_dos_error(c)) {
204 cli_dos_error(c, &eclass, &ecode);
205 ret = cli_errno_from_dos(eclass, ecode);
207 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
208 (int)eclass, (int)ecode, (int)ecode, ret));
212 status = cli_nt_error(c);
213 ret = cli_errno_from_nt(status);
215 DEBUG(3,("smbc errno 0x%08x -> %d\n",
223 * Connect to a server, possibly on an existing connection
225 * Here, what we want to do is: If the server and username
226 * match an existing connection, reuse that, otherwise, establish a
229 * If we have to create a new connection, call the auth_fn to get the
230 * info we need, unless the username and password were passed in.
233 struct smbc_server *smbc_server(char *server, char *share,
234 char *workgroup, char *username,
237 struct smbc_server *srv=NULL;
239 struct nmb_name called, calling;
240 char *p, *server_n = server;
244 extern struct in_addr ipzero;
249 /* try to use an existing connection */
250 for (srv=smbc_srvs;srv;srv=srv->next) {
251 if (strcmp(server,srv->server_name)==0 &&
252 strcmp(share,srv->share_name)==0 &&
253 strcmp(workgroup,srv->workgroup)==0 &&
254 strcmp(username, srv->username) == 0)
258 if (server[0] == 0) {
264 * Pick up the auth info here, once we know we need to connect
265 * But only if we do not have a username and password ...
268 if (!username[0] || !password[0])
269 smbc_auth_fn(server, share, workgroup, sizeof(fstring),
270 username, sizeof(fstring), password, sizeof(fstring));
273 * However, smbc_auth_fn may have picked up info relating to an
274 * existing connection, so try for an existing connection again ...
277 for (srv=smbc_srvs;srv;srv=srv->next) {
278 if (strcmp(server,srv->server_name)==0 &&
279 strcmp(share,srv->share_name)==0 &&
280 strcmp(workgroup,srv->workgroup)==0 &&
281 strcmp(username, srv->username) == 0)
285 make_nmb_name(&calling, my_netbios_name, 0x0);
286 make_nmb_name(&called , server, 0x20);
288 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
290 if ((p=strchr_m(server_n,'#')) &&
291 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
293 fstrcpy(group, server_n);
294 p = strchr_m(group,'#');
299 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
302 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
306 /* have to open a new connection */
307 if (!cli_initialise(&c) || !cli_connect(&c, server_n, &ip)) {
312 if (!cli_session_request(&c, &calling, &called)) {
314 if (strcmp(called.name, "*SMBSERVER")) {
315 make_nmb_name(&called , "*SMBSERVER", 0x20);
322 DEBUG(4,(" session request ok\n"));
324 if (!cli_negprot(&c)) {
330 if (!cli_session_setup(&c, username,
331 password, strlen(password),
332 password, strlen(password),
334 /* try an anonymous login if it failed */
335 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
341 DEBUG(4,(" session setup ok\n"));
343 if (!cli_send_tconX(&c, share, "?????",
344 password, strlen(password)+1)) {
345 errno = smbc_errno(&c);
350 DEBUG(4,(" tconx ok\n"));
352 srv = (struct smbc_server *)malloc(sizeof(*srv));
362 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
364 srv->server_name = strdup(server);
365 if (!srv->server_name) {
370 srv->share_name = strdup(share);
371 if (!srv->share_name) {
376 srv->workgroup = strdup(workgroup);
377 if (!srv->workgroup) {
382 srv->username = strdup(username);
383 if (!srv->username) {
388 DLIST_ADD(smbc_srvs, srv);
394 if (!srv) return NULL;
396 if (srv->server_name) free(srv->server_name);
397 if (srv->share_name) free(srv->share_name);
403 *Initialise the library etc
405 * We accept valiv values for debug from 0 to 100,
406 * and insist that fn must be non-null.
409 int smbc_init(smbc_get_auth_data_fn fn, int debug)
413 char *user = NULL, *home = NULL, *pname="libsmbclient";
416 * Next lot ifdef'd out until test suite fixed ...
419 if (!fn || debug < 0 || debug > 100) {
426 if (smbc_initialized) { /* Don't go through this if we have already done it */
432 smbc_initialized = 1;
434 /* smbc_debug = debug; */
438 setup_logging(pname, False);
441 * We try to construct our netbios name from our hostname etc
444 user = getenv("USER");
445 if (!user) user = ""; /* FIXME: What to do about this? */
448 * FIXME: Is this the best way to get the user info? */
450 pstrcpy(smbc_user, user); /* Save for use elsewhere */
455 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
458 slprintf(my_netbios_name, 16, "smbc%s%d", user, pid);
460 /* Here we would open the smb.conf file if needed ... */
462 home = getenv("HOME");
464 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
466 load_interfaces(); /* Load the list of interfaces ... */
468 in_client = True; /* FIXME, make a param */
470 if (!lp_load(conf, True, False, False)) {
473 * Hmmm, what the hell do we do here ... we could not parse the
474 * config file ... We must return an error ... and keep info around
475 * about why we failed
478 errno = ENOENT; /* FIXME: Figure out the correct error response */
483 reopen_logs(); /* Get logging working ... */
485 name_register_wins(my_netbios_name, 0);
488 * Now initialize the file descriptor array and figure out what the
489 * max open files is, so we can return FD's that are above the max
490 * open file, and separated by a guard band
493 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
497 if (getrlimit(RLIMIT_NOFILE, &rlp)) {
499 DEBUG(0, ("smbc_init: getrlimit(1) for RLIMIT_NOFILE failed with error %s\n", strerror(errno)));
501 smbc_start_fd = 1000000;
502 smbc_max_fd = 10000; /* FIXME, should be a define ... */
507 smbc_start_fd = rlp.rlim_max + 10000; /* Leave a guard space of 10,000 */
512 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
514 smbc_start_fd = 1000000;
515 smbc_max_fd = 10000; /* FIXME, should be a define ... */
519 smbc_file_table = malloc(smbc_max_fd * sizeof(struct smbc_file *));
521 for (p = 0; p < smbc_max_fd; p++)
522 smbc_file_table[p] = NULL;
524 if (!smbc_file_table)
527 return 0; /* Success */
532 * Routine to open() a file ...
535 int smbc_open(const char *fname, int flags, mode_t mode)
537 fstring server, share, user, password;
539 struct smbc_server *srv = NULL;
542 if (!smbc_initialized) {
544 errno = EINVAL; /* Best I can think of ... */
556 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
558 if (user[0] == (char)0) pstrcpy(user, smbc_user);
560 srv = smbc_server(server, share, lp_workgroup(), user, password);
564 if (errno == EPERM) errno = EACCES;
565 return -1; /* smbc_server sets errno */
569 /* Hmmm, the test for a directory is suspect here ... FIXME */
571 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
580 /* Find a free slot first */
582 while (smbc_file_table[slot])
585 if (slot > smbc_max_fd) {
587 errno = ENOMEM; /* FIXME, is this best? */
592 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
594 if (!smbc_file_table[slot]) {
601 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
603 /* Handle the error ... */
605 free(smbc_file_table[slot]);
606 smbc_file_table[slot] = NULL;
607 errno = smbc_errno(&srv->cli);
612 /* Fill in file struct */
614 smbc_file_table[slot]->cli_fd = fd;
615 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
616 smbc_file_table[slot]->fname = strdup(fname);
617 smbc_file_table[slot]->srv = srv;
618 smbc_file_table[slot]->offset = 0;
619 smbc_file_table[slot]->file = True;
621 return smbc_file_table[slot]->smbc_fd;
625 /* Check if opendir needed ... */
630 eno = smbc_errno(&srv->cli);
631 fd = smbc_opendir(fname);
632 if (fd < 0) errno = eno;
637 return 1; /* Success, with fd ... */
642 * Routine to create a file
645 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
647 int smbc_creat(const char *path, mode_t mode)
650 if (!smbc_initialized) {
657 return smbc_open(path, creat_bits, mode);
661 * Routine to read() a file ...
664 ssize_t smbc_read(int fd, void *buf, size_t count)
666 struct smbc_file *fe;
669 if (!smbc_initialized) {
676 DEBUG(4, ("smbc_read(%d, %d)\n", fd, (int)count));
678 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
685 /* Check that the buffer exists ... */
694 fe = smbc_file_table[fd - smbc_start_fd];
696 if (!fe || !fe->file) {
703 ret = cli_read(&fe->srv->cli, fe->cli_fd, buf, fe->offset, count);
707 errno = smbc_errno(&fe->srv->cli);
714 DEBUG(4, (" --> %d\n", ret));
716 return ret; /* Success, ret bytes of data ... */
721 * Routine to write() a file ...
724 ssize_t smbc_write(int fd, void *buf, size_t count)
727 struct smbc_file *fe;
729 if (!smbc_initialized) {
736 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
743 /* Check that the buffer exists ... */
752 fe = smbc_file_table[fd - smbc_start_fd];
754 if (!fe || !fe->file) {
761 ret = cli_write(&fe->srv->cli, fe->cli_fd, 0, buf, fe->offset, count);
765 errno = smbc_errno(&fe->srv->cli);
772 return ret; /* Success, 0 bytes of data ... */
776 * Routine to close() a file ...
779 int smbc_close(int fd)
781 struct smbc_file *fe;
783 if (!smbc_initialized) {
790 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
797 fe = smbc_file_table[fd - smbc_start_fd];
808 return smbc_closedir(fd);
812 if (!cli_close(&fe->srv->cli, fe->cli_fd)) {
814 errno = smbc_errno(&fe->srv->cli); /* FIXME, should we deallocate slot? */
819 if (fe->fname) free(fe->fname);
821 smbc_file_table[fd - smbc_start_fd] = NULL;
827 * Routine to unlink() a file
830 int smbc_unlink(const char *fname)
832 fstring server, share, user, password;
834 struct smbc_server *srv = NULL;
836 if (!smbc_initialized) {
838 errno = EINVAL; /* Best I can think of ... */
850 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
852 if (user[0] == (char)0) pstrcpy(user, smbc_user);
854 srv = smbc_server(server, share, lp_workgroup(), user, password);
858 return -1; /* smbc_server sets errno */
862 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
864 int job = smbc_stat_printjob(srv, path, NULL, NULL);
870 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
878 if (!cli_unlink(&srv->cli, path)) {
880 errno = smbc_errno(&srv->cli);
882 if (errno == EACCES) { /* Check if the file is a directory */
887 time_t m_time = 0, a_time = 0, c_time = 0;
890 if (!smbc_getatr(srv, path, &mode, &size,
891 &c_time, &a_time, &m_time, &ino)) {
893 /* Hmmm, bad error ... What? */
895 errno = smbc_errno(&srv->cli);
901 if (IS_DOS_DIR(mode))
904 errno = saverr; /* Restore this */
913 return 0; /* Success ... */
918 * Routine to rename() a file
921 int smbc_rename(const char *oname, const char *nname)
923 fstring server1, share1, server2, share2, user1, user2, password1, password2;
924 pstring path1, path2;
925 struct smbc_server *srv = NULL;
927 if (!smbc_initialized) {
929 errno = EINVAL; /* Best I can think of ... */
934 if (!oname || !nname) {
941 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
943 smbc_parse_path(oname, server1, share1, path1, user1, password1);
945 if (user1[0] == (char)0) pstrcpy(user1, smbc_user);
947 smbc_parse_path(nname, server2, share2, path2, user2, password2);
949 if (user2[0] == (char)0) pstrcpy(user2, smbc_user);
951 if (strcmp(server1, server2) || strcmp(share1, share2) ||
952 strcmp(user1, user2)) {
954 /* Can't rename across file systems, or users?? */
961 srv = smbc_server(server1, share1, lp_workgroup(), user1, password1);
968 if (!cli_rename(&srv->cli, path1, path2)) {
969 int eno = smbc_errno(&srv->cli);
972 !cli_unlink(&srv->cli, path2) ||
973 !cli_rename(&srv->cli, path1, path2)) {
981 return 0; /* Success */
986 * A routine to lseek() a file
989 off_t smbc_lseek(int fd, off_t offset, int whence)
991 struct smbc_file *fe;
994 if (!smbc_initialized) {
1001 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1008 fe = smbc_file_table[fd - smbc_start_fd];
1020 return -1; /* Can't lseek a dir ... */
1026 fe->offset = offset;
1030 fe->offset += offset;
1034 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1035 NULL, NULL, NULL) &&
1036 !cli_getattrE(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1042 fe->offset = size + offset;
1056 * Generate an inode number from file name for those things that need it
1060 ino_t smbc_inode(const char *name)
1063 if (!*name) return 2; /* FIXME, why 2 ??? */
1064 return (ino_t)str_checksum(name);
1069 * Routine to put basic stat info into a stat structure ... Used by stat and
1074 int smbc_setup_stat(struct stat *st, char *fname, size_t size, int mode)
1079 if (IS_DOS_DIR(mode)) {
1080 st->st_mode = SMBC_DIR_MODE;
1082 st->st_mode = SMBC_FILE_MODE;
1085 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1086 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1087 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1088 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1091 st->st_blksize = 512;
1092 st->st_blocks = (size+511)/512;
1093 st->st_uid = getuid();
1094 st->st_gid = getgid();
1096 if (IS_DOS_DIR(mode)) {
1102 if (st->st_ino == 0) {
1103 st->st_ino = smbc_inode(fname);
1106 return True; /* FIXME: Is this needed ? */
1111 * Get info from an SMB server on a file. Use a qpathinfo call first
1112 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1115 BOOL smbc_getatr(struct smbc_server *srv, char *path,
1116 uint16 *mode, size_t *size,
1117 time_t *c_time, time_t *a_time, time_t *m_time,
1121 if (!smbc_initialized) {
1128 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1130 if (!srv->no_pathinfo2 &&
1131 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1132 size, mode, ino)) return True;
1134 /* if this is NT then don't bother with the getatr */
1135 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
1137 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1138 a_time = c_time = m_time;
1139 srv->no_pathinfo2 = True;
1146 * Routine to stat a file given a name
1149 int smbc_stat(const char *fname, struct stat *st)
1151 struct smbc_server *srv;
1152 fstring server, share, user, password;
1154 time_t m_time = 0, a_time = 0, c_time = 0;
1159 if (!smbc_initialized) {
1161 errno = EINVAL; /* Best I can think of ... */
1173 DEBUG(4, ("smbc_stat(%s)\n", fname));
1175 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
1177 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1179 srv = smbc_server(server, share, lp_workgroup(), user, password);
1183 return -1; /* errno set by smbc_server */
1187 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1189 mode = aDIR | aRONLY;
1192 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1194 if (strcmp(path, "\\") == 0) {
1196 mode = aDIR | aRONLY;
1202 smbc_stat_printjob(srv, path, &size, &m_time);
1203 c_time = a_time = m_time;
1208 if (!smbc_getatr(srv, path, &mode, &size,
1209 &c_time, &a_time, &m_time, &ino)) {
1211 errno = smbc_errno(&srv->cli);
1220 smbc_setup_stat(st, path, size, mode);
1222 st->st_atime = a_time;
1223 st->st_ctime = c_time;
1224 st->st_mtime = m_time;
1225 st->st_dev = srv->dev;
1232 * Routine to stat a file given an fd
1235 int smbc_fstat(int fd, struct stat *st)
1237 struct smbc_file *fe;
1238 time_t c_time, a_time, m_time;
1243 if (!smbc_initialized) {
1250 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1257 fe = smbc_file_table[fd - smbc_start_fd];
1268 return smbc_fstatdir(fd, st);
1272 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd,
1273 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1274 !cli_getattrE(&fe->srv->cli, fe->cli_fd,
1275 &mode, &size, &c_time, &a_time, &m_time)) {
1284 smbc_setup_stat(st, fe->fname, size, mode);
1286 st->st_atime = a_time;
1287 st->st_ctime = c_time;
1288 st->st_mtime = m_time;
1289 st->st_dev = fe->srv->dev;
1296 * Routine to open a directory
1300 * smb: which should list all the workgroups available
1302 * smb:workgroup//server
1304 * smb://server/share
1307 static void smbc_remove_dir(struct smbc_file *dir)
1309 struct smbc_dir_list *d,*f;
1316 if (f->dirent) free(f->dirent);
1321 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1325 static int add_dirent(struct smbc_file *dir, const char *name, const char *comment, uint32 type)
1327 struct smbc_dirent *dirent;
1331 * Allocate space for the dirent, which must be increased by the
1332 * size of the name and the comment and 1 for the null on the comment.
1333 * The null on the name is already accounted for.
1336 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1337 (comment?strlen(comment):0) + 1;
1339 dirent = malloc(size);
1343 dir->dir_error = ENOMEM;
1348 if (dir->dir_list == NULL) {
1350 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1351 if (!dir->dir_list) {
1354 dir->dir_error = ENOMEM;
1359 dir->dir_end = dir->dir_next = dir->dir_list;
1364 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1366 if (!dir->dir_end) {
1369 dir->dir_error = ENOMEM;
1374 dir->dir_end = dir->dir_end->next;
1378 dir->dir_end->next = NULL;
1379 dir->dir_end->dirent = dirent;
1381 dirent->smbc_type = type;
1382 dirent->namelen = (name?strlen(name):0);
1383 dirent->commentlen = (comment?strlen(comment):0);
1384 dirent->dirlen = size;
1386 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1388 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1389 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1396 list_fn(const char *name, uint32 type, const char *comment, void *state)
1398 struct smbc_file *dir = (struct smbc_file *)state;
1401 /* We need to process the type a little ... */
1403 if (dir->dir_type == SMBC_FILE_SHARE) {
1406 case 0: /* Directory tree */
1407 dirent_type = SMBC_FILE_SHARE;
1411 dirent_type = SMBC_PRINTER_SHARE;
1415 dirent_type = SMBC_COMMS_SHARE;
1419 dirent_type = SMBC_IPC_SHARE;
1423 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1428 else dirent_type = dir->dir_type;
1430 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1432 /* An error occurred, what do we do? */
1439 dir_list_fn(file_info *finfo, const char *mask, void *state)
1442 /* fprintf(stderr, "Finfo->name=%s, mask=%s, mode=%0X\n", finfo->name, mask, finfo->mode);*/
1443 if (add_dirent((struct smbc_file *)state, finfo->name, "",
1444 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1446 /* Handle an error ... */
1452 int smbc_opendir(const char *fname)
1454 fstring server, share, user, password;
1456 struct smbc_server *srv = NULL;
1457 struct in_addr rem_ip;
1460 if (!smbc_initialized) {
1474 if (smbc_parse_path(fname, server, share, path, user, password)) {
1481 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1483 /* Get a file entry ... */
1487 while (smbc_file_table[slot])
1490 if (slot > smbc_max_fd) {
1493 return -1; /* FIXME, ... move into a func */
1497 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
1499 if (!smbc_file_table[slot]) {
1506 smbc_file_table[slot]->cli_fd = 0;
1507 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
1508 smbc_file_table[slot]->fname = strdup(fname);
1509 smbc_file_table[slot]->srv = NULL;
1510 smbc_file_table[slot]->offset = 0;
1511 smbc_file_table[slot]->file = False;
1512 smbc_file_table[slot]->dir_list =
1513 smbc_file_table[slot]->dir_next =
1514 smbc_file_table[slot]->dir_end = NULL;
1516 if (server[0] == (char)0) {
1518 if (share[0] != (char)0 || path[0] != (char)0) {
1521 if (smbc_file_table[slot]) {
1522 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1523 free(smbc_file_table[slot]);
1525 smbc_file_table[slot] = NULL;
1530 /* We have server and share and path empty ... so list the workgroups */
1532 /*cli_get_backup_server(my_netbios_name, lp_workgroup(), server, sizeof(server));*/
1534 if (!resolve_name(lp_workgroup(), &rem_ip, 0x1d)) {
1536 errno = EINVAL; /* Something wrong with smb.conf? */
1541 smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;
1543 /* find the name of the server ... */
1545 if (!name_status_find(0, rem_ip, server)) {
1547 fprintf(stderr, "Could not get the name of local master browser ...\n");
1554 * Get a connection to IPC$ on the server if we do not already have one
1557 srv = smbc_server(server, "IPC$", lp_workgroup(), user, password);
1561 if (smbc_file_table[slot]) {
1562 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1563 free(smbc_file_table[slot]);
1565 smbc_file_table[slot] = NULL;
1570 smbc_file_table[slot]->srv = srv;
1572 /* Now, list the stuff ... */
1574 if (!cli_NetServerEnum(&srv->cli, lp_workgroup(), 0x80000000, list_fn,
1575 (void *)smbc_file_table[slot])) {
1577 if (smbc_file_table[slot]) {
1578 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1579 free(smbc_file_table[slot]);
1581 smbc_file_table[slot] = NULL;
1582 errno = cli_errno(&srv->cli);
1587 else { /* Server not an empty string ... Check the rest and see what gives */
1589 if (share[0] == (char)0) {
1591 if (path[0] != (char)0) { /* Should not have empty share with path */
1594 if (smbc_file_table[slot]) {
1595 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1596 free(smbc_file_table[slot]);
1598 smbc_file_table[slot] = NULL;
1603 /* Check to see if <server><1D> translates, or <server><20> translates */
1605 if (resolve_name(server, &rem_ip, 0x1d)) { /* Found LMB */
1608 smbc_file_table[slot]->dir_type = SMBC_SERVER;
1611 * Get the backup list ...
1614 /*cli_get_backup_server(my_netbios_name, server, buserver, sizeof(buserver)); */
1616 if (!name_status_find(0, rem_ip, buserver)) {
1618 fprintf(stderr, "Could not get name of local master browser ...\n");
1619 errno = EPERM; /* FIXME, is this correct */
1625 * Get a connection to IPC$ on the server if we do not already have one
1628 srv = smbc_server(buserver, "IPC$", lp_workgroup(), user, password);
1632 if (smbc_file_table[slot]) {
1633 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1634 free(smbc_file_table[slot]);
1636 smbc_file_table[slot] = NULL; /* FIXME: Memory leaks ... */
1641 smbc_file_table[slot]->srv = srv;
1643 /* Now, list the servers ... */
1645 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1646 (void *)smbc_file_table[slot])) {
1648 if (smbc_file_table[slot]) {
1649 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1650 free(smbc_file_table[slot]);
1652 smbc_file_table[slot] = NULL;
1653 errno = cli_errno(&srv->cli);
1661 if (resolve_name(server, &rem_ip, 0x20)) {
1663 /* Now, list the shares ... */
1665 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1667 srv = smbc_server(server, "IPC$", lp_workgroup(), user, password);
1671 if (smbc_file_table[slot]) {
1672 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1673 free(smbc_file_table[slot]);
1675 smbc_file_table[slot] = NULL;
1680 smbc_file_table[slot]->srv = srv;
1682 /* Now, list the servers ... */
1684 if (cli_RNetShareEnum(&srv->cli, list_fn,
1685 (void *)smbc_file_table[slot]) < 0) {
1687 errno = cli_errno(&srv->cli);
1688 if (smbc_file_table[slot]) {
1689 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1690 free(smbc_file_table[slot]);
1692 smbc_file_table[slot] = NULL;
1700 errno = ENODEV; /* Neither the workgroup nor server exists */
1701 if (smbc_file_table[slot]) {
1702 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1703 free(smbc_file_table[slot]);
1705 smbc_file_table[slot] = NULL;
1713 else { /* The server and share are specified ... work from there ... */
1715 /* Well, we connect to the server and list the directory */
1717 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1719 srv = smbc_server(server, share, lp_workgroup(), user, password);
1723 if (smbc_file_table[slot]) {
1724 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1725 free(smbc_file_table[slot]);
1727 smbc_file_table[slot] = NULL;
1732 smbc_file_table[slot]->srv = srv;
1734 /* Now, list the files ... */
1736 pstrcat(path, "\\*");
1738 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1739 (void *)smbc_file_table[slot]) < 0) {
1741 if (smbc_file_table[slot]) {
1742 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1743 free(smbc_file_table[slot]);
1745 smbc_file_table[slot] = NULL;
1746 errno = smbc_errno(&srv->cli);
1754 return smbc_file_table[slot]->smbc_fd;
1759 * Routine to close a directory
1762 int smbc_closedir(int fd)
1764 struct smbc_file *fe;
1766 if (!smbc_initialized) {
1773 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1780 fe = smbc_file_table[fd - smbc_start_fd];
1789 smbc_remove_dir(fe); /* Clean it up */
1793 if (fe->fname) free(fe->fname);
1794 free(fe); /* Free the space too */
1798 smbc_file_table[fd - smbc_start_fd] = NULL;
1805 * Routine to get a directory entry
1808 static char smbc_local_dirent[512]; /* Make big enough */
1810 struct smbc_dirent *smbc_readdir(unsigned int fd)
1812 struct smbc_file *fe;
1813 struct smbc_dirent *dirp, *dirent;
1815 /* Check that all is ok first ... */
1817 if (!smbc_initialized) {
1824 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1831 fe = smbc_file_table[fd - smbc_start_fd];
1840 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1851 dirent = fe->dir_next->dirent;
1860 /* Hmmm, do I even need to copy it? */
1862 bcopy(dirent, smbc_local_dirent, dirent->dirlen); /* Copy the dirent */
1864 dirp = (struct smbc_dirent *)smbc_local_dirent;
1866 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1868 fe->dir_next = fe->dir_next->next;
1870 return (struct smbc_dirent *)smbc_local_dirent;
1876 * Routine to get directory entries
1879 int smbc_getdents(unsigned int fd, struct smbc_dirent *dirp, int count)
1881 struct smbc_file *fe;
1882 struct smbc_dir_list *dir;
1883 int rem = count, reqd;
1884 char *ndir = (char *)dirp;
1886 /* Check that all is ok first ... */
1888 if (!smbc_initialized) {
1895 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1902 fe = smbc_file_table[fd - smbc_start_fd];
1911 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1919 * Now, retrieve the number of entries that will fit in what was passed
1920 * We have to figure out if the info is in the list, or we need to
1921 * send a request to the server to get the info.
1924 while ((dir = fe->dir_next)) {
1925 struct smbc_dirent *dirent;
1929 errno = ENOENT; /* Bad error */
1934 if (rem < (reqd = (sizeof(struct smbc_dirent) + dir->dirent->namelen +
1935 dir->dirent->commentlen + 1))) {
1937 if (rem < count) { /* We managed to copy something */
1943 else { /* Nothing copied ... */
1945 errno = EINVAL; /* Not enough space ... */
1952 dirent = dir->dirent;
1954 bcopy(dirent, ndir, reqd); /* Copy the data in ... */
1956 ((struct smbc_dirent *)ndir)->comment =
1957 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1963 fe->dir_next = dir = dir -> next;
1974 * Routine to create a directory ...
1977 int smbc_mkdir(const char *fname, mode_t mode)
1979 struct smbc_server *srv;
1980 fstring server, share, user, password;
1983 if (!smbc_initialized) {
1997 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1999 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2001 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2003 srv = smbc_server(server, share, lp_workgroup(), user, password);
2007 return -1; /* errno set by smbc_server */
2011 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2013 mode = aDIR | aRONLY;
2016 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2018 if (strcmp(path, "\\") == 0) {
2020 mode = aDIR | aRONLY;
2026 smbc_stat_printjob(srv, path, &size, &m_time);
2027 c_time = a_time = m_time;
2032 if (!cli_mkdir(&srv->cli, path)) {
2034 errno = smbc_errno(&srv->cli);
2044 * Our list function simply checks to see if a directory is not empty
2047 static int smbc_rmdir_dirempty = True;
2049 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
2052 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2053 smbc_rmdir_dirempty = False;
2058 * Routine to remove a directory
2061 int smbc_rmdir(const char *fname)
2063 struct smbc_server *srv;
2064 fstring server, share, user, password;
2067 if (!smbc_initialized) {
2081 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2083 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2085 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2087 srv = smbc_server(server, share, lp_workgroup(), user, password);
2091 return -1; /* errno set by smbc_server */
2095 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2097 mode = aDIR | aRONLY;
2100 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2102 if (strcmp(path, "\\") == 0) {
2104 mode = aDIR | aRONLY;
2110 smbc_stat_printjob(srv, path, &size, &m_time);
2111 c_time = a_time = m_time;
2116 if (!cli_rmdir(&srv->cli, path)) {
2118 errno = smbc_errno(&srv->cli);
2120 if (errno == EACCES) { /* Check if the dir empty or not */
2122 pstring lpath; /* Local storage to avoid buffer overflows */
2124 smbc_rmdir_dirempty = True; /* Make this so ... */
2126 pstrcpy(lpath, path);
2127 pstrcat(lpath, "\\*");
2129 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2132 /* Fix errno to ignore latest error ... */
2134 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2135 smbc_errno(&srv->cli)));
2140 if (smbc_rmdir_dirempty)
2156 * Routine to return the current directory position
2159 off_t smbc_telldir(int fd)
2161 struct smbc_file *fe;
2163 if (!smbc_initialized) {
2170 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
2177 fe = smbc_file_table[fd - smbc_start_fd];
2186 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2193 return (off_t) fe->dir_next;
2198 * A routine to run down the list and see if the entry is OK
2201 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2202 struct smbc_dirent *dirent)
2205 /* Run down the list looking for what we want */
2209 struct smbc_dir_list *tmp = list;
2213 if (tmp->dirent == dirent)
2222 return NULL; /* Not found, or an error */
2228 * Routine to seek on a directory
2231 int smbc_lseekdir(int fd, off_t offset)
2233 struct smbc_file *fe;
2234 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2235 struct smbc_dir_list *list_ent = NULL;
2237 if (!smbc_initialized) {
2244 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
2251 fe = smbc_file_table[fd - smbc_start_fd];
2260 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2267 /* Now, check what we were passed and see if it is OK ... */
2269 if (dirent == NULL) { /* Seek to the begining of the list */
2271 fe->dir_next = fe->dir_list;
2276 /* Now, run down the list and make sure that the entry is OK */
2277 /* This may need to be changed if we change the format of the list */
2279 if ((list_ent = smbc_check_dir_ent(fe->dir_list, dirent)) == NULL) {
2281 errno = EINVAL; /* Bad entry */
2286 fe->dir_next = list_ent;
2293 * Routine to fstat a dir
2296 int smbc_fstatdir(int fd, struct stat *st)
2299 if (!smbc_initialized) {
2306 /* No code yet ... */
2313 * Routine to print a file on a remote server ...
2315 * We open the file, which we assume to be on a remote server, and then
2316 * copy it to a print file on the share specified by printq.
2319 int smbc_print_file(const char *fname, const char *printq)
2321 int fid1, fid2, bytes, saverr, tot_bytes = 0;
2324 if (!smbc_initialized) {
2331 if (!fname && !printq) {
2338 /* Try to open the file for reading ... */
2340 if ((fid1 = smbc_open(fname, O_RDONLY, 0666)) < 0) {
2342 fprintf(stderr, "Error, fname=%s, errno=%i\n", fname, errno);
2343 return -1; /* smbc_open sets errno */
2347 /* Now, try to open the printer file for writing */
2349 if ((fid2 = smbc_open_print_job(printq)) < 0) {
2351 saverr = errno; /* Save errno */
2358 while ((bytes = smbc_read(fid1, buf, sizeof(buf))) > 0) {
2362 if ((smbc_write(fid2, buf, bytes)) < 0) {
2375 smbc_close(fid1); /* We have to close these anyway */
2390 * Open a print file to be written to by other calls
2393 int smbc_open_print_job(const char *fname)
2395 fstring server, share, user, password;
2398 if (!smbc_initialized) {
2412 DEBUG(4, ("smbc_open_print_job(%s)\n", fname));
2414 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2416 /* What if the path is empty, or the file exists? */
2418 return smbc_open(fname, O_WRONLY, 666);
2423 * Routine to list print jobs on a printer share ...
2426 int smbc_list_print_jobs(const char *fname, void (*fn)(struct print_job_info *))
2428 struct smbc_server *srv;
2429 fstring server, share, user, password;
2432 if (!smbc_initialized) {
2446 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2448 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2450 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2452 srv = smbc_server(server, share, lp_workgroup(), user, password);
2456 return -1; /* errno set by smbc_server */
2460 if (cli_print_queue(&srv->cli, fn) < 0) {
2462 errno = smbc_errno(&srv->cli);
2472 * Delete a print job from a remote printer share
2475 int smbc_unlink_print_job(const char *fname, int id)
2477 struct smbc_server *srv;
2478 fstring server, share, user, password;
2482 if (!smbc_initialized) {
2496 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2498 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2500 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2502 srv = smbc_server(server, share, lp_workgroup(), user, password);
2506 return -1; /* errno set by smbc_server */
2510 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2513 errno = smbc_errno(&srv->cli);
2514 else if (err == ERRnosuchprintjob)