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(p, '@')) {
137 pstring username, passwd, domain;
140 next_token(&p, userinfo, "@", sizeof(fstring));
142 username[0] = passwd[0] = domain[0] = 0;
144 if (strchr(u, ';')) {
146 next_token(&u, domain, ";", sizeof(fstring));
150 if (strchr(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)
202 ret = cli_error(c, &eclass, &ecode, NULL);
205 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
206 (int)eclass, (int)ecode, (int)ecode, ret));
212 * Connect to a server, possibly on an existing connection
214 * Here, what we want to do is: If the server and username
215 * match an existing connection, reuse that, otherwise, establish a
218 * If we have to create a new connection, call the auth_fn to get the
219 * info we need, unless the username and password were passed in.
222 struct smbc_server *smbc_server(char *server, char *share,
223 char *workgroup, char *username,
226 struct smbc_server *srv=NULL;
228 struct nmb_name called, calling;
229 char *p, *server_n = server;
233 extern struct in_addr ipzero;
238 /* try to use an existing connection */
239 for (srv=smbc_srvs;srv;srv=srv->next) {
240 if (strcmp(server,srv->server_name)==0 &&
241 strcmp(share,srv->share_name)==0 &&
242 strcmp(workgroup,srv->workgroup)==0 &&
243 strcmp(username, srv->username) == 0)
247 if (server[0] == 0) {
253 * Pick up the auth info here, once we know we need to connect
254 * But only if we do not have a username and password ...
257 if (!username[0] || !password[0])
258 smbc_auth_fn(server, share, workgroup, sizeof(fstring),
259 username, sizeof(fstring), password, sizeof(fstring));
262 * However, smbc_auth_fn may have picked up info relating to an
263 * existing connection, so try for an existing connection again ...
266 for (srv=smbc_srvs;srv;srv=srv->next) {
267 if (strcmp(server,srv->server_name)==0 &&
268 strcmp(share,srv->share_name)==0 &&
269 strcmp(workgroup,srv->workgroup)==0 &&
270 strcmp(username, srv->username) == 0)
274 make_nmb_name(&calling, my_netbios_name, 0x0);
275 make_nmb_name(&called , server, 0x20);
277 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
279 if ((p=strchr(server_n,'#')) &&
280 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
282 fstrcpy(group, server_n);
283 p = strchr(group,'#');
288 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
291 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
295 /* have to open a new connection */
296 if (!cli_initialise(&c) || !cli_connect(&c, server_n, &ip)) {
301 if (!cli_session_request(&c, &calling, &called)) {
303 if (strcmp(called.name, "*SMBSERVER")) {
304 make_nmb_name(&called , "*SMBSERVER", 0x20);
311 DEBUG(4,(" session request ok\n"));
313 if (!cli_negprot(&c)) {
319 if (!cli_session_setup(&c, username,
320 password, strlen(password),
321 password, strlen(password),
323 /* try an anonymous login if it failed */
324 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
330 DEBUG(4,(" session setup ok\n"));
332 if (!cli_send_tconX(&c, share, "?????",
333 password, strlen(password)+1)) {
334 errno = smbc_errno(&c);
339 DEBUG(4,(" tconx ok\n"));
341 srv = (struct smbc_server *)malloc(sizeof(*srv));
351 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
353 srv->server_name = strdup(server);
354 if (!srv->server_name) {
359 srv->share_name = strdup(share);
360 if (!srv->share_name) {
365 srv->workgroup = strdup(workgroup);
366 if (!srv->workgroup) {
371 srv->username = strdup(username);
372 if (!srv->username) {
377 DLIST_ADD(smbc_srvs, srv);
383 if (!srv) return NULL;
385 if (srv->server_name) free(srv->server_name);
386 if (srv->share_name) free(srv->share_name);
392 *Initialise the library etc
394 * We accept valiv values for debug from 0 to 100,
395 * and insist that fn must be non-null.
398 int smbc_init(smbc_get_auth_data_fn fn, int debug)
402 char *user = NULL, *home = NULL, *pname="libsmbclient";
405 * Next lot ifdef'd out until test suite fixed ...
408 if (!fn || debug < 0 || debug > 100) {
415 if (smbc_initialized) { /* Don't go through this if we have already done it */
421 smbc_initialized = 1;
423 /* smbc_debug = debug; */
427 setup_logging(pname, False);
430 * We try to construct our netbios name from our hostname etc
433 user = getenv("USER");
434 if (!user) user = ""; /* FIXME: What to do about this? */
437 * FIXME: Is this the best way to get the user info? */
439 pstrcpy(smbc_user, user); /* Save for use elsewhere */
444 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
447 slprintf(my_netbios_name, 16, "smbc%s%d", user, pid);
449 charset_initialise();
451 /* Here we would open the smb.conf file if needed ... */
453 home = getenv("HOME");
455 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
457 load_interfaces(); /* Load the list of interfaces ... */
459 in_client = True; /* FIXME, make a param */
461 if (!lp_load(conf, True, False, False)) {
464 * Hmmm, what the hell do we do here ... we could not parse the
465 * config file ... We must return an error ... and keep info around
466 * about why we failed
469 errno = ENOENT; /* FIXME: Figure out the correct error response */
474 codepage_initialise(lp_client_code_page()); /* Get a codepage */
476 reopen_logs(); /* Get logging working ... */
478 name_register_wins(my_netbios_name, 0);
481 * Now initialize the file descriptor array and figure out what the
482 * max open files is, so we can return FD's that are above the max
483 * open file, and separated by a guard band
486 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
490 if (getrlimit(RLIMIT_NOFILE, &rlp)) {
492 DEBUG(0, ("smbc_init: getrlimit(1) for RLIMIT_NOFILE failed with error %s\n", strerror(errno)));
494 smbc_start_fd = 1000000;
495 smbc_max_fd = 10000; /* FIXME, should be a define ... */
500 smbc_start_fd = rlp.rlim_max + 10000; /* Leave a guard space of 10,000 */
505 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
507 smbc_start_fd = 1000000;
508 smbc_max_fd = 10000; /* FIXME, should be a define ... */
512 smbc_file_table = malloc(smbc_max_fd * sizeof(struct smbc_file *));
514 for (p = 0; p < smbc_max_fd; p++)
515 smbc_file_table[p] = NULL;
517 if (!smbc_file_table)
520 return 0; /* Success */
525 * Routine to open() a file ...
528 int smbc_open(const char *fname, int flags, mode_t mode)
530 fstring server, share, user, password;
532 struct smbc_server *srv = NULL;
535 if (!smbc_initialized) {
537 errno = EINVAL; /* Best I can think of ... */
549 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
551 if (user[0] == (char)0) pstrcpy(user, smbc_user);
553 srv = smbc_server(server, share, lp_workgroup(), user, password);
557 if (errno == EPERM) errno = EACCES;
558 return -1; /* smbc_server sets errno */
562 /* Hmmm, the test for a directory is suspect here ... FIXME */
564 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
573 /* Find a free slot first */
575 while (smbc_file_table[slot])
578 if (slot > smbc_max_fd) {
580 errno = ENOMEM; /* FIXME, is this best? */
585 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
587 if (!smbc_file_table[slot]) {
594 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
596 /* Handle the error ... */
598 free(smbc_file_table[slot]);
599 smbc_file_table[slot] = NULL;
600 errno = smbc_errno(&srv->cli);
605 /* Fill in file struct */
607 smbc_file_table[slot]->cli_fd = fd;
608 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
609 smbc_file_table[slot]->fname = strdup(fname);
610 smbc_file_table[slot]->srv = srv;
611 smbc_file_table[slot]->offset = 0;
612 smbc_file_table[slot]->file = True;
614 return smbc_file_table[slot]->smbc_fd;
618 /* Check if opendir needed ... */
623 eno = smbc_errno(&srv->cli);
624 fd = smbc_opendir(fname);
625 if (fd < 0) errno = eno;
630 return 1; /* Success, with fd ... */
635 * Routine to create a file
638 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
640 int smbc_creat(const char *path, mode_t mode)
643 if (!smbc_initialized) {
650 return smbc_open(path, creat_bits, mode);
654 * Routine to read() a file ...
657 ssize_t smbc_read(int fd, void *buf, size_t count)
659 struct smbc_file *fe;
662 if (!smbc_initialized) {
669 DEBUG(4, ("smbc_read(%d, %d)\n", fd, (int)count));
671 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
678 /* Check that the buffer exists ... */
687 fe = smbc_file_table[fd - smbc_start_fd];
689 if (!fe || !fe->file) {
696 ret = cli_read(&fe->srv->cli, fe->cli_fd, buf, fe->offset, count);
700 errno = smbc_errno(&fe->srv->cli);
707 DEBUG(4, (" --> %d\n", ret));
709 return ret; /* Success, ret bytes of data ... */
714 * Routine to write() a file ...
717 ssize_t smbc_write(int fd, void *buf, size_t count)
720 struct smbc_file *fe;
722 if (!smbc_initialized) {
729 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
736 /* Check that the buffer exists ... */
745 fe = smbc_file_table[fd - smbc_start_fd];
747 if (!fe || !fe->file) {
754 ret = cli_write(&fe->srv->cli, fe->cli_fd, 0, buf, fe->offset, count);
758 errno = smbc_errno(&fe->srv->cli);
765 return ret; /* Success, 0 bytes of data ... */
769 * Routine to close() a file ...
772 int smbc_close(int fd)
774 struct smbc_file *fe;
776 if (!smbc_initialized) {
783 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
790 fe = smbc_file_table[fd - smbc_start_fd];
801 return smbc_closedir(fd);
805 if (!cli_close(&fe->srv->cli, fe->cli_fd)) {
807 errno = smbc_errno(&fe->srv->cli); /* FIXME, should we deallocate slot? */
812 if (fe->fname) free(fe->fname);
814 smbc_file_table[fd - smbc_start_fd] = NULL;
820 * Routine to unlink() a file
823 int smbc_unlink(const char *fname)
825 fstring server, share, user, password;
827 struct smbc_server *srv = NULL;
829 if (!smbc_initialized) {
831 errno = EINVAL; /* Best I can think of ... */
843 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
845 if (user[0] == (char)0) pstrcpy(user, smbc_user);
847 srv = smbc_server(server, share, lp_workgroup(), user, password);
851 return -1; /* smbc_server sets errno */
855 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
857 int job = smbc_stat_printjob(srv, path, NULL, NULL);
863 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
871 if (!cli_unlink(&srv->cli, path)) {
873 errno = smbc_errno(&srv->cli);
875 if (errno == EACCES) { /* Check if the file is a directory */
880 time_t m_time = 0, a_time = 0, c_time = 0;
883 if (!smbc_getatr(srv, path, &mode, &size,
884 &c_time, &a_time, &m_time, &ino)) {
886 /* Hmmm, bad error ... What? */
888 errno = smbc_errno(&srv->cli);
894 if (IS_DOS_DIR(mode))
897 errno = saverr; /* Restore this */
906 return 0; /* Success ... */
911 * Routine to rename() a file
914 int smbc_rename(const char *oname, const char *nname)
916 fstring server1, share1, server2, share2, user1, user2, password1, password2;
917 pstring path1, path2;
918 struct smbc_server *srv = NULL;
920 if (!smbc_initialized) {
922 errno = EINVAL; /* Best I can think of ... */
927 if (!oname || !nname) {
934 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
936 smbc_parse_path(oname, server1, share1, path1, user1, password1);
938 if (user1[0] == (char)0) pstrcpy(user1, smbc_user);
940 smbc_parse_path(nname, server2, share2, path2, user2, password2);
942 if (user2[0] == (char)0) pstrcpy(user2, smbc_user);
944 if (strcmp(server1, server2) || strcmp(share1, share2) ||
945 strcmp(user1, user2)) {
947 /* Can't rename across file systems, or users?? */
954 srv = smbc_server(server1, share1, lp_workgroup(), user1, password1);
961 if (!cli_rename(&srv->cli, path1, path2)) {
962 int eno = smbc_errno(&srv->cli);
965 !cli_unlink(&srv->cli, path2) ||
966 !cli_rename(&srv->cli, path1, path2)) {
974 return 0; /* Success */
979 * A routine to lseek() a file
982 off_t smbc_lseek(int fd, off_t offset, int whence)
984 struct smbc_file *fe;
987 if (!smbc_initialized) {
994 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1001 fe = smbc_file_table[fd - smbc_start_fd];
1013 return -1; /* Can't lseek a dir ... */
1019 fe->offset = offset;
1023 fe->offset += offset;
1027 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1028 NULL, NULL, NULL) &&
1029 !cli_getattrE(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1035 fe->offset = size + offset;
1049 * Generate an inode number from file name for those things that need it
1053 ino_t smbc_inode(const char *name)
1056 if (!*name) return 2; /* FIXME, why 2 ??? */
1057 return (ino_t)str_checksum(name);
1062 * Routine to put basic stat info into a stat structure ... Used by stat and
1067 int smbc_setup_stat(struct stat *st, char *fname, size_t size, int mode)
1072 if (IS_DOS_DIR(mode)) {
1073 st->st_mode = SMBC_DIR_MODE;
1075 st->st_mode = SMBC_FILE_MODE;
1078 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1079 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1080 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1081 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1084 st->st_blksize = 512;
1085 st->st_blocks = (size+511)/512;
1086 st->st_uid = getuid();
1087 st->st_gid = getgid();
1089 if (IS_DOS_DIR(mode)) {
1095 if (st->st_ino == 0) {
1096 st->st_ino = smbc_inode(fname);
1099 return True; /* FIXME: Is this needed ? */
1104 * Get info from an SMB server on a file. Use a qpathinfo call first
1105 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1108 BOOL smbc_getatr(struct smbc_server *srv, char *path,
1109 uint16 *mode, size_t *size,
1110 time_t *c_time, time_t *a_time, time_t *m_time,
1114 if (!smbc_initialized) {
1121 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1123 if (!srv->no_pathinfo2 &&
1124 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1125 size, mode, ino)) return True;
1127 /* if this is NT then don't bother with the getatr */
1128 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
1130 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1131 a_time = c_time = m_time;
1132 srv->no_pathinfo2 = True;
1139 * Routine to stat a file given a name
1142 int smbc_stat(const char *fname, struct stat *st)
1144 struct smbc_server *srv;
1145 fstring server, share, user, password;
1147 time_t m_time = 0, a_time = 0, c_time = 0;
1152 if (!smbc_initialized) {
1154 errno = EINVAL; /* Best I can think of ... */
1166 DEBUG(4, ("smbc_stat(%s)\n", fname));
1168 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
1170 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1172 srv = smbc_server(server, share, lp_workgroup(), user, password);
1176 return -1; /* errno set by smbc_server */
1180 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1182 mode = aDIR | aRONLY;
1185 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1187 if (strcmp(path, "\\") == 0) {
1189 mode = aDIR | aRONLY;
1195 smbc_stat_printjob(srv, path, &size, &m_time);
1196 c_time = a_time = m_time;
1201 if (!smbc_getatr(srv, path, &mode, &size,
1202 &c_time, &a_time, &m_time, &ino)) {
1204 errno = smbc_errno(&srv->cli);
1213 smbc_setup_stat(st, path, size, mode);
1215 st->st_atime = a_time;
1216 st->st_ctime = c_time;
1217 st->st_mtime = m_time;
1218 st->st_dev = srv->dev;
1225 * Routine to stat a file given an fd
1228 int smbc_fstat(int fd, struct stat *st)
1230 struct smbc_file *fe;
1231 time_t c_time, a_time, m_time;
1236 if (!smbc_initialized) {
1243 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1250 fe = smbc_file_table[fd - smbc_start_fd];
1261 return smbc_fstatdir(fd, st);
1265 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd,
1266 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1267 !cli_getattrE(&fe->srv->cli, fe->cli_fd,
1268 &mode, &size, &c_time, &a_time, &m_time)) {
1277 smbc_setup_stat(st, fe->fname, size, mode);
1279 st->st_atime = a_time;
1280 st->st_ctime = c_time;
1281 st->st_mtime = m_time;
1282 st->st_dev = fe->srv->dev;
1289 * Routine to open a directory
1293 * smb: which should list all the workgroups available
1295 * smb:workgroup//server
1297 * smb://server/share
1300 static void smbc_remove_dir(struct smbc_file *dir)
1302 struct smbc_dir_list *d,*f;
1309 if (f->dirent) free(f->dirent);
1314 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1318 static int add_dirent(struct smbc_file *dir, const char *name, const char *comment, uint32 type)
1320 struct smbc_dirent *dirent;
1324 * Allocate space for the dirent, which must be increased by the
1325 * size of the name and the comment and 1 for the null on the comment.
1326 * The null on the name is already accounted for.
1329 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1330 (comment?strlen(comment):0) + 1;
1332 dirent = malloc(size);
1336 dir->dir_error = ENOMEM;
1341 if (dir->dir_list == NULL) {
1343 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1344 if (!dir->dir_list) {
1347 dir->dir_error = ENOMEM;
1352 dir->dir_end = dir->dir_next = dir->dir_list;
1357 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1359 if (!dir->dir_end) {
1362 dir->dir_error = ENOMEM;
1367 dir->dir_end = dir->dir_end->next;
1371 dir->dir_end->next = NULL;
1372 dir->dir_end->dirent = dirent;
1374 dirent->smbc_type = type;
1375 dirent->namelen = (name?strlen(name):0);
1376 dirent->commentlen = (comment?strlen(comment):0);
1377 dirent->dirlen = size;
1379 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1381 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1382 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1389 list_fn(const char *name, uint32 type, const char *comment, void *state)
1391 struct smbc_file *dir = (struct smbc_file *)state;
1394 /* We need to process the type a little ... */
1396 if (dir->dir_type == SMBC_FILE_SHARE) {
1399 case 0: /* Directory tree */
1400 dirent_type = SMBC_FILE_SHARE;
1404 dirent_type = SMBC_PRINTER_SHARE;
1408 dirent_type = SMBC_COMMS_SHARE;
1412 dirent_type = SMBC_IPC_SHARE;
1416 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1421 else dirent_type = dir->dir_type;
1423 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1425 /* An error occurred, what do we do? */
1432 dir_list_fn(file_info *finfo, const char *mask, void *state)
1435 /* fprintf(stderr, "Finfo->name=%s, mask=%s, mode=%0X\n", finfo->name, mask, finfo->mode);*/
1436 if (add_dirent((struct smbc_file *)state, finfo->name, "",
1437 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1439 /* Handle an error ... */
1445 int smbc_opendir(const char *fname)
1447 fstring server, share, user, password;
1449 struct smbc_server *srv = NULL;
1450 struct in_addr rem_ip;
1455 if (!smbc_initialized) {
1469 if (smbc_parse_path(fname, server, share, path, user, password)) {
1476 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1478 /* Get a file entry ... */
1482 while (smbc_file_table[slot])
1485 if (slot > smbc_max_fd) {
1488 return -1; /* FIXME, ... move into a func */
1492 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
1494 if (!smbc_file_table[slot]) {
1501 smbc_file_table[slot]->cli_fd = 0;
1502 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
1503 smbc_file_table[slot]->fname = strdup(fname);
1504 smbc_file_table[slot]->srv = NULL;
1505 smbc_file_table[slot]->offset = 0;
1506 smbc_file_table[slot]->file = False;
1507 smbc_file_table[slot]->dir_list =
1508 smbc_file_table[slot]->dir_next =
1509 smbc_file_table[slot]->dir_end = NULL;
1511 if (server[0] == (char)0) {
1513 if (share[0] != (char)0 || path[0] != (char)0) {
1516 if (smbc_file_table[slot]) {
1517 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1518 free(smbc_file_table[slot]);
1520 smbc_file_table[slot] = NULL;
1525 /* We have server and share and path empty ... so list the workgroups */
1527 /*cli_get_backup_server(my_netbios_name, lp_workgroup(), server, sizeof(server));*/
1529 if (!resolve_name(lp_workgroup(), &rem_ip, 0x1d)) {
1531 errno = EINVAL; /* Something wrong with smb.conf? */
1536 smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;
1538 /* find the name of the server ... */
1540 if (!name_status_find(0, rem_ip, server)) {
1542 fprintf(stderr, "Could not get the name of local master browser ...\n");
1549 * Get a connection to IPC$ on the server if we do not already have one
1552 srv = smbc_server(server, "IPC$", lp_workgroup(), user, password);
1556 if (smbc_file_table[slot]) {
1557 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1558 free(smbc_file_table[slot]);
1560 smbc_file_table[slot] = NULL;
1565 smbc_file_table[slot]->srv = srv;
1567 /* Now, list the stuff ... */
1569 if (!cli_NetServerEnum(&srv->cli, lp_workgroup(), 0x80000000, list_fn,
1570 (void *)smbc_file_table[slot])) {
1572 if (smbc_file_table[slot]) {
1573 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1574 free(smbc_file_table[slot]);
1576 smbc_file_table[slot] = NULL;
1577 errno = cli_error(&srv->cli, &eclass, &ecode, NULL);
1582 else { /* Server not an empty string ... Check the rest and see what gives */
1584 if (share[0] == (char)0) {
1586 if (path[0] != (char)0) { /* Should not have empty share with path */
1589 if (smbc_file_table[slot]) {
1590 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1591 free(smbc_file_table[slot]);
1593 smbc_file_table[slot] = NULL;
1598 /* Check to see if <server><1D> translates, or <server><20> translates */
1600 if (resolve_name(server, &rem_ip, 0x1d)) { /* Found LMB */
1603 smbc_file_table[slot]->dir_type = SMBC_SERVER;
1606 * Get the backup list ...
1609 /*cli_get_backup_server(my_netbios_name, server, buserver, sizeof(buserver)); */
1611 if (!name_status_find(0, rem_ip, buserver)) {
1613 fprintf(stderr, "Could not get name of local master browser ...\n");
1614 errno = EPERM; /* FIXME, is this correct */
1620 * Get a connection to IPC$ on the server if we do not already have one
1623 srv = smbc_server(buserver, "IPC$", lp_workgroup(), user, password);
1627 if (smbc_file_table[slot]) {
1628 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1629 free(smbc_file_table[slot]);
1631 smbc_file_table[slot] = NULL; /* FIXME: Memory leaks ... */
1636 smbc_file_table[slot]->srv = srv;
1638 /* Now, list the servers ... */
1640 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1641 (void *)smbc_file_table[slot])) {
1643 if (smbc_file_table[slot]) {
1644 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1645 free(smbc_file_table[slot]);
1647 smbc_file_table[slot] = NULL;
1648 errno = cli_error(&srv->cli, &eclass, &ecode, NULL);
1656 if (resolve_name(server, &rem_ip, 0x20)) {
1658 /* Now, list the shares ... */
1660 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1662 srv = smbc_server(server, "IPC$", lp_workgroup(), user, password);
1666 if (smbc_file_table[slot]) {
1667 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1668 free(smbc_file_table[slot]);
1670 smbc_file_table[slot] = NULL;
1675 smbc_file_table[slot]->srv = srv;
1677 /* Now, list the servers ... */
1679 if (cli_RNetShareEnum(&srv->cli, list_fn,
1680 (void *)smbc_file_table[slot]) < 0) {
1682 errno = cli_error(&srv->cli, &eclass, &ecode, NULL);
1683 if (smbc_file_table[slot]) {
1684 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1685 free(smbc_file_table[slot]);
1687 smbc_file_table[slot] = NULL;
1695 errno = ENODEV; /* Neither the workgroup nor server exists */
1696 if (smbc_file_table[slot]) {
1697 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1698 free(smbc_file_table[slot]);
1700 smbc_file_table[slot] = NULL;
1708 else { /* The server and share are specified ... work from there ... */
1710 /* Well, we connect to the server and list the directory */
1712 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1714 srv = smbc_server(server, share, lp_workgroup(), user, password);
1718 if (smbc_file_table[slot]) {
1719 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1720 free(smbc_file_table[slot]);
1722 smbc_file_table[slot] = NULL;
1727 smbc_file_table[slot]->srv = srv;
1729 /* Now, list the files ... */
1731 pstrcat(path, "\\*");
1733 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1734 (void *)smbc_file_table[slot]) < 0) {
1736 if (smbc_file_table[slot]) {
1737 if (smbc_file_table[slot]->fname) free(smbc_file_table[slot]->fname);
1738 free(smbc_file_table[slot]);
1740 smbc_file_table[slot] = NULL;
1741 errno = smbc_errno(&srv->cli);
1749 return smbc_file_table[slot]->smbc_fd;
1754 * Routine to close a directory
1757 int smbc_closedir(int fd)
1759 struct smbc_file *fe;
1761 if (!smbc_initialized) {
1768 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1775 fe = smbc_file_table[fd - smbc_start_fd];
1784 smbc_remove_dir(fe); /* Clean it up */
1788 if (fe->fname) free(fe->fname);
1789 free(fe); /* Free the space too */
1793 smbc_file_table[fd - smbc_start_fd] = NULL;
1800 * Routine to get a directory entry
1803 static char smbc_local_dirent[512]; /* Make big enough */
1805 struct smbc_dirent *smbc_readdir(unsigned int fd)
1807 struct smbc_file *fe;
1808 struct smbc_dirent *dirp, *dirent;
1810 /* Check that all is ok first ... */
1812 if (!smbc_initialized) {
1819 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1826 fe = smbc_file_table[fd - smbc_start_fd];
1835 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1846 dirent = fe->dir_next->dirent;
1855 /* Hmmm, do I even need to copy it? */
1857 bcopy(dirent, smbc_local_dirent, dirent->dirlen); /* Copy the dirent */
1859 dirp = (struct smbc_dirent *)smbc_local_dirent;
1861 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1863 fe->dir_next = fe->dir_next->next;
1865 return (struct smbc_dirent *)smbc_local_dirent;
1871 * Routine to get directory entries
1874 int smbc_getdents(unsigned int fd, struct smbc_dirent *dirp, int count)
1876 struct smbc_file *fe;
1877 struct smbc_dir_list *dir;
1878 int rem = count, reqd;
1879 char *ndir = (char *)dirp;
1881 /* Check that all is ok first ... */
1883 if (!smbc_initialized) {
1890 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1897 fe = smbc_file_table[fd - smbc_start_fd];
1906 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1914 * Now, retrieve the number of entries that will fit in what was passed
1915 * We have to figure out if the info is in the list, or we need to
1916 * send a request to the server to get the info.
1919 while ((dir = fe->dir_next)) {
1920 struct smbc_dirent *dirent;
1924 errno = ENOENT; /* Bad error */
1929 if (rem < (reqd = (sizeof(struct smbc_dirent) + dir->dirent->namelen +
1930 dir->dirent->commentlen + 1))) {
1932 if (rem < count) { /* We managed to copy something */
1938 else { /* Nothing copied ... */
1940 errno = EINVAL; /* Not enough space ... */
1947 dirent = dir->dirent;
1949 bcopy(dirent, ndir, reqd); /* Copy the data in ... */
1951 ((struct smbc_dirent *)ndir)->comment =
1952 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1958 fe->dir_next = dir = dir -> next;
1969 * Routine to create a directory ...
1972 int smbc_mkdir(const char *fname, mode_t mode)
1974 struct smbc_server *srv;
1975 fstring server, share, user, password;
1978 if (!smbc_initialized) {
1992 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1994 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
1996 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1998 srv = smbc_server(server, share, lp_workgroup(), user, password);
2002 return -1; /* errno set by smbc_server */
2006 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2008 mode = aDIR | aRONLY;
2011 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2013 if (strcmp(path, "\\") == 0) {
2015 mode = aDIR | aRONLY;
2021 smbc_stat_printjob(srv, path, &size, &m_time);
2022 c_time = a_time = m_time;
2027 if (!cli_mkdir(&srv->cli, path)) {
2029 errno = smbc_errno(&srv->cli);
2039 * Our list function simply checks to see if a directory is not empty
2042 static int smbc_rmdir_dirempty = True;
2044 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
2047 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2048 smbc_rmdir_dirempty = False;
2053 * Routine to remove a directory
2056 int smbc_rmdir(const char *fname)
2058 struct smbc_server *srv;
2059 fstring server, share, user, password;
2062 if (!smbc_initialized) {
2076 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2078 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2080 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2082 srv = smbc_server(server, share, lp_workgroup(), user, password);
2086 return -1; /* errno set by smbc_server */
2090 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2092 mode = aDIR | aRONLY;
2095 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2097 if (strcmp(path, "\\") == 0) {
2099 mode = aDIR | aRONLY;
2105 smbc_stat_printjob(srv, path, &size, &m_time);
2106 c_time = a_time = m_time;
2111 if (!cli_rmdir(&srv->cli, path)) {
2113 errno = smbc_errno(&srv->cli);
2115 if (errno == EACCES) { /* Check if the dir empty or not */
2117 pstring lpath; /* Local storage to avoid buffer overflows */
2119 smbc_rmdir_dirempty = True; /* Make this so ... */
2121 pstrcpy(lpath, path);
2122 pstrcat(lpath, "\\*");
2124 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2127 /* Fix errno to ignore latest error ... */
2129 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2130 smbc_errno(&srv->cli)));
2135 if (smbc_rmdir_dirempty)
2151 * Routine to return the current directory position
2154 off_t smbc_telldir(int fd)
2156 struct smbc_file *fe;
2158 if (!smbc_initialized) {
2165 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
2172 fe = smbc_file_table[fd - smbc_start_fd];
2181 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2188 return (off_t) fe->dir_next;
2193 * A routine to run down the list and see if the entry is OK
2196 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2197 struct smbc_dirent *dirent)
2200 /* Run down the list looking for what we want */
2204 struct smbc_dir_list *tmp = list;
2208 if (tmp->dirent == dirent)
2217 return NULL; /* Not found, or an error */
2223 * Routine to seek on a directory
2226 int smbc_lseekdir(int fd, off_t offset)
2228 struct smbc_file *fe;
2229 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2230 struct smbc_dir_list *list_ent = NULL;
2232 if (!smbc_initialized) {
2239 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
2246 fe = smbc_file_table[fd - smbc_start_fd];
2255 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2262 /* Now, check what we were passed and see if it is OK ... */
2264 if (dirent == NULL) { /* Seek to the begining of the list */
2266 fe->dir_next = fe->dir_list;
2271 /* Now, run down the list and make sure that the entry is OK */
2272 /* This may need to be changed if we change the format of the list */
2274 if ((list_ent = smbc_check_dir_ent(fe->dir_list, dirent)) == NULL) {
2276 errno = EINVAL; /* Bad entry */
2281 fe->dir_next = list_ent;
2288 * Routine to fstat a dir
2291 int smbc_fstatdir(int fd, struct stat *st)
2294 if (!smbc_initialized) {
2301 /* No code yet ... */
2308 * Routine to print a file on a remote server ...
2310 * We open the file, which we assume to be on a remote server, and then
2311 * copy it to a print file on the share specified by printq.
2314 int smbc_print_file(const char *fname, const char *printq)
2316 int fid1, fid2, bytes, saverr, tot_bytes = 0;
2319 if (!smbc_initialized) {
2326 if (!fname && !printq) {
2333 /* Try to open the file for reading ... */
2335 if ((fid1 = smbc_open(fname, O_RDONLY, 0666)) < 0) {
2337 fprintf(stderr, "Error, fname=%s, errno=%i\n", fname, errno);
2338 return -1; /* smbc_open sets errno */
2342 /* Now, try to open the printer file for writing */
2344 if ((fid2 = smbc_open_print_job(printq)) < 0) {
2346 saverr = errno; /* Save errno */
2353 while ((bytes = smbc_read(fid1, buf, sizeof(buf))) > 0) {
2357 if ((smbc_write(fid2, buf, bytes)) < 0) {
2370 smbc_close(fid1); /* We have to close these anyway */
2385 * Open a print file to be written to by other calls
2388 int smbc_open_print_job(const char *fname)
2390 fstring server, share, user, password;
2393 if (!smbc_initialized) {
2407 DEBUG(4, ("smbc_open_print_job(%s)\n", fname));
2409 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2411 /* What if the path is empty, or the file exists? */
2413 return smbc_open(fname, O_WRONLY, 666);
2418 * Routine to list print jobs on a printer share ...
2421 int smbc_list_print_jobs(const char *fname, void (*fn)(struct print_job_info *))
2423 struct smbc_server *srv;
2424 fstring server, share, user, password;
2427 if (!smbc_initialized) {
2441 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2443 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2445 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2447 srv = smbc_server(server, share, lp_workgroup(), user, password);
2451 return -1; /* errno set by smbc_server */
2455 if (cli_print_queue(&srv->cli, fn) < 0) {
2457 errno = smbc_errno(&srv->cli);
2467 * Delete a print job from a remote printer share
2470 int smbc_unlink_print_job(const char *fname, int id)
2472 struct smbc_server *srv;
2473 fstring server, share, user, password;
2477 if (!smbc_initialized) {
2491 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2493 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2495 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2497 srv = smbc_server(server, share, lp_workgroup(), user, password);
2501 return -1; /* errno set by smbc_server */
2505 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2508 errno = smbc_errno(&srv->cli);
2509 else if (err == ERRnosuchprintjob)