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 extern pstring global_myname;
67 static int smbc_initialized = 0;
68 static smbc_get_auth_data_fn smbc_auth_fn = NULL;
69 /*static int smbc_debug;*/
70 static int smbc_start_fd;
71 static int smbc_max_fd = 10000;
72 static struct smbc_file **smbc_file_table;
73 static struct smbc_server *smbc_srvs;
74 static pstring my_netbios_name;
75 static pstring smbc_user;
78 * Function to parse a path and turn it into components
80 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
82 * smb:// means show all the workgroups
83 * smb://name/ means, if name<1D> exists, list servers in workgroup,
84 * else, if name<20> exists, list all shares for server ...
87 static const char *smbc_prefix = "smb:";
90 smbc_parse_path(const char *fname, char *server, char *share, char *path,
91 char *user, char *password) /* FIXME, lengths of strings */
99 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
102 /* clean_fname(s); causing problems ... */
104 /* see if it has the right prefix */
105 len = strlen(smbc_prefix);
106 if (strncmp(s,smbc_prefix,len) ||
107 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
111 /* Watch the test below, we are testing to see if we should exit */
113 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
119 p += 2; /* Skip the // or \\ */
126 strncpy(server, (char *)lp_workgroup(), 16); /* FIXME: Danger here */
132 * ok, its for us. Now parse out the server, share etc.
134 * However, we want to parse out [[domain;]user[:password]@] if it
138 /* check that '@' occurs before '/', if '/' exists at all */
139 q = strchr_m(p, '@');
140 r = strchr_m(p, '/');
141 if (q && (!r || q < r)) {
142 pstring username, passwd, domain;
145 next_token(&p, userinfo, "@", sizeof(fstring));
147 username[0] = passwd[0] = domain[0] = 0;
149 if (strchr_m(u, ';')) {
151 next_token(&u, domain, ";", sizeof(fstring));
155 if (strchr_m(u, ':')) {
157 next_token(&u, username, ":", sizeof(fstring));
164 pstrcpy(username, u);
169 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
172 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
176 if (!next_token(&p, server, "/", sizeof(fstring))) {
182 if (*p == (char)0) return 0; /* That's it ... */
184 if (!next_token(&p, share, "/", sizeof(fstring))) {
192 all_string_sub(path, "/", "\\", 0);
198 * Convert an SMB error into a UNIX error ...
201 int smbc_errno(struct cli_state *c)
205 if (cli_is_dos_error(c)) {
209 cli_dos_error(c, &eclass, &ecode);
210 ret = cli_errno_from_dos(eclass, ecode);
212 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
213 (int)eclass, (int)ecode, (int)ecode, ret));
217 status = cli_nt_error(c);
218 ret = cli_errno_from_nt(status);
220 DEBUG(3,("smbc errno %s -> %d\n",
221 get_nt_error_msg(status), ret));
228 * Connect to a server, possibly on an existing connection
230 * Here, what we want to do is: If the server and username
231 * match an existing connection, reuse that, otherwise, establish a
234 * If we have to create a new connection, call the auth_fn to get the
235 * info we need, unless the username and password were passed in.
238 struct smbc_server *smbc_server(char *server, char *share,
239 char *workgroup, char *username,
242 struct smbc_server *srv=NULL;
244 struct nmb_name called, calling;
245 char *p, *server_n = server;
249 extern struct in_addr ipzero;
254 /* try to use an existing connection */
255 for (srv=smbc_srvs;srv;srv=srv->next) {
256 if (strcmp(server,srv->server_name)==0 &&
257 strcmp(share,srv->share_name)==0 &&
258 strcmp(workgroup,srv->workgroup)==0 &&
259 strcmp(username, srv->username) == 0)
263 if (server[0] == 0) {
269 * Pick up the auth info here, once we know we need to connect
270 * But only if we do not have a username and password ...
273 if (!username[0] || !password[0])
274 smbc_auth_fn(server, share, workgroup, sizeof(fstring),
275 username, sizeof(fstring), password, sizeof(fstring));
278 * However, smbc_auth_fn may have picked up info relating to an
279 * existing connection, so try for an existing connection again ...
282 for (srv=smbc_srvs;srv;srv=srv->next) {
283 if (strcmp(server,srv->server_name)==0 &&
284 strcmp(share,srv->share_name)==0 &&
285 strcmp(workgroup,srv->workgroup)==0 &&
286 strcmp(username, srv->username) == 0)
290 make_nmb_name(&calling, my_netbios_name, 0x0);
291 make_nmb_name(&called , server, 0x20);
293 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
295 if ((p=strchr_m(server_n,'#')) &&
296 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
298 fstrcpy(group, server_n);
299 p = strchr_m(group,'#');
304 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
307 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
311 /* have to open a new connection */
312 if (!cli_initialise(&c) || !cli_connect(&c, server_n, &ip)) {
317 if (!cli_session_request(&c, &calling, &called)) {
319 if (strcmp(called.name, "*SMBSERVER")) {
320 make_nmb_name(&called , "*SMBSERVER", 0x20);
327 DEBUG(4,(" session request ok\n"));
329 if (!cli_negprot(&c)) {
335 if (!cli_session_setup(&c, username,
336 password, strlen(password),
337 password, strlen(password),
339 /* try an anonymous login if it failed */
340 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
346 DEBUG(4,(" session setup ok\n"));
348 if (!cli_send_tconX(&c, share, "?????",
349 password, strlen(password)+1)) {
350 errno = smbc_errno(&c);
355 DEBUG(4,(" tconx ok\n"));
357 srv = (struct smbc_server *)malloc(sizeof(*srv));
367 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
369 srv->server_name = strdup(server);
370 if (!srv->server_name) {
375 srv->share_name = strdup(share);
376 if (!srv->share_name) {
381 srv->workgroup = strdup(workgroup);
382 if (!srv->workgroup) {
387 srv->username = strdup(username);
388 if (!srv->username) {
393 DLIST_ADD(smbc_srvs, srv);
399 if (!srv) return NULL;
401 SAFE_FREE(srv->server_name);
402 SAFE_FREE(srv->share_name);
403 SAFE_FREE(srv->workgroup);
404 SAFE_FREE(srv->username);
410 *Remove a server from the list smbc_srvs if it's unused -- Tom (tom@ninja.nl)
414 BOOL smbc_remove_unused_server(struct smbc_server * s)
418 /* are we being fooled ? */
419 if (!s) return False;
421 /* close all open files/directories on this server */
422 for (p = 0; p < SMBC_MAX_FD; p++) {
423 if (smbc_file_table[p] &&
424 smbc_file_table[p]->srv == s) {
425 /* Still used .. DARN */
426 DEBUG(3, ("smbc_remove_usused_server: %x still used by %s (%d).\n", (int) s,
427 smbc_file_table[p]->fname, smbc_file_table[p]->smbc_fd));
432 cli_shutdown(&s->cli);
434 SAFE_FREE(s->username);
435 SAFE_FREE(s->workgroup);
436 SAFE_FREE(s->server_name);
437 SAFE_FREE(s->share_name);
438 DLIST_REMOVE(smbc_srvs, s);
439 DEBUG(3, ("smbc_remove_usused_server: %x removed.\n", (int) s));
445 *Initialise the library etc
447 * We accept valid values for debug from 0 to 100,
448 * and insist that fn must be non-null.
451 int smbc_init(smbc_get_auth_data_fn fn, int debug)
455 char *user = NULL, *home = NULL, *pname="libsmbclient";
457 if (!fn || debug < 0 || debug > 100) {
464 if (smbc_initialized) { /* Don't go through this if we have already done it */
470 smbc_initialized = 1;
472 /* smbc_debug = debug; */
476 setup_logging(pname, False);
478 /* Here we would open the smb.conf file if needed ... */
480 home = getenv("HOME");
482 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
484 load_interfaces(); /* Load the list of interfaces ... */
486 in_client = True; /* FIXME, make a param */
488 if (!lp_load(conf, True, False, False)) {
491 * Hmmm, what the hell do we do here ... we could not parse the
492 * config file ... We must return an error ... and keep info around
493 * about why we failed
496 errno = ENOENT; /* FIXME: Figure out the correct error response */
501 reopen_logs(); /* Get logging working ... */
504 * FIXME: Is this the best way to get the user info?
507 user = getenv("USER");
508 /* walk around as "guest" if no username can be found */
509 if (!user) user = strdup("guest");
510 pstrcpy(smbc_user, user); /* Save for use elsewhere */
513 * We try to get our netbios name from the config. If that fails we fall
514 * back on constructing our netbios name from our hostname etc
517 pstrcpy(my_netbios_name, global_myname);
521 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
524 slprintf(my_netbios_name, 16, "smbc%s%d", user, pid);
526 DEBUG(0,("Using netbios name %s.\n", my_netbios_name));
528 name_register_wins(my_netbios_name, 0);
531 * Now initialize the file descriptor array and figure out what the
532 * max open files is, so we can return FD's that are above the max
533 * open file, and separated by a guard band
536 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
540 if (getrlimit(RLIMIT_NOFILE, &rlp)) {
542 DEBUG(0, ("smbc_init: getrlimit(1) for RLIMIT_NOFILE failed with error %s\n", strerror(errno)));
544 smbc_start_fd = 1000000;
549 smbc_start_fd = rlp.rlim_max + 10000; /* Leave a guard space of 10,000 */
553 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
555 smbc_start_fd = 1000000;
559 smbc_file_table = malloc(SMBC_MAX_FD * sizeof(struct smbc_file *));
561 for (p = 0; p < SMBC_MAX_FD; p++)
562 smbc_file_table[p] = NULL;
564 if (!smbc_file_table)
567 return 0; /* Success */
572 * Routine to open() a file ...
575 int smbc_open(const char *fname, int flags, mode_t mode)
577 fstring server, share, user, password, workgroup;
579 struct smbc_server *srv = NULL;
582 if (!smbc_initialized) {
584 errno = EINVAL; /* Best I can think of ... */
596 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
598 if (user[0] == (char)0) pstrcpy(user, smbc_user);
600 pstrcpy(workgroup, lp_workgroup());
602 srv = smbc_server(server, share, workgroup, user, password);
606 if (errno == EPERM) errno = EACCES;
607 return -1; /* smbc_server sets errno */
611 /* Hmmm, the test for a directory is suspect here ... FIXME */
613 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
622 /* Find a free slot first */
624 while (smbc_file_table[slot])
627 if (slot > SMBC_MAX_FD) {
629 errno = ENOMEM; /* FIXME, is this best? */
634 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
636 if (!smbc_file_table[slot]) {
643 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
645 /* Handle the error ... */
647 SAFE_FREE(smbc_file_table[slot]);
648 errno = smbc_errno(&srv->cli);
653 /* Fill in file struct */
655 smbc_file_table[slot]->cli_fd = fd;
656 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
657 smbc_file_table[slot]->fname = strdup(fname);
658 smbc_file_table[slot]->srv = srv;
659 smbc_file_table[slot]->offset = 0;
660 smbc_file_table[slot]->file = True;
662 return smbc_file_table[slot]->smbc_fd;
666 /* Check if opendir needed ... */
671 eno = smbc_errno(&srv->cli);
672 fd = smbc_opendir(fname);
673 if (fd < 0) errno = eno;
678 return 1; /* Success, with fd ... */
683 * Routine to create a file
686 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
688 int smbc_creat(const char *path, mode_t mode)
691 if (!smbc_initialized) {
698 return smbc_open(path, creat_bits, mode);
702 * Routine to read() a file ...
705 ssize_t smbc_read(int fd, void *buf, size_t count)
707 struct smbc_file *fe;
710 if (!smbc_initialized) {
717 DEBUG(4, ("smbc_read(%d, %d)\n", fd, (int)count));
719 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
726 /* Check that the buffer exists ... */
735 fe = smbc_file_table[fd - smbc_start_fd];
737 if (!fe || !fe->file) {
744 ret = cli_read(&fe->srv->cli, fe->cli_fd, buf, fe->offset, count);
748 errno = smbc_errno(&fe->srv->cli);
755 DEBUG(4, (" --> %d\n", ret));
757 return ret; /* Success, ret bytes of data ... */
762 * Routine to write() a file ...
765 ssize_t smbc_write(int fd, void *buf, size_t count)
768 struct smbc_file *fe;
770 if (!smbc_initialized) {
777 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
784 /* Check that the buffer exists ... */
793 fe = smbc_file_table[fd - smbc_start_fd];
795 if (!fe || !fe->file) {
802 ret = cli_write(&fe->srv->cli, fe->cli_fd, 0, buf, fe->offset, count);
806 errno = smbc_errno(&fe->srv->cli);
813 return ret; /* Success, 0 bytes of data ... */
817 * Routine to close() a file ...
820 int smbc_close(int fd)
822 struct smbc_file *fe;
823 struct smbc_server *srv;
825 if (!smbc_initialized) {
832 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
839 fe = smbc_file_table[fd - smbc_start_fd];
850 return smbc_closedir(fd);
854 if (!cli_close(&fe->srv->cli, fe->cli_fd)) {
856 DEBUG(3, ("cli_close failed on %s (%d). purging server.\n",
857 fe->fname, fe->smbc_fd));
858 /* Deallocate slot and remove the server
859 * from the server cache if unused */
860 errno = smbc_errno(&fe->srv->cli);
862 SAFE_FREE(fe->fname);
864 smbc_file_table[fd - smbc_start_fd] = NULL;
865 smbc_remove_unused_server(srv);
871 SAFE_FREE(fe->fname);
873 smbc_file_table[fd - smbc_start_fd] = NULL;
879 * Routine to unlink() a file
882 int smbc_unlink(const char *fname)
884 fstring server, share, user, password, workgroup;
886 struct smbc_server *srv = NULL;
888 if (!smbc_initialized) {
890 errno = EINVAL; /* Best I can think of ... */
902 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
904 if (user[0] == (char)0) pstrcpy(user, smbc_user);
906 pstrcpy(workgroup, lp_workgroup());
908 srv = smbc_server(server, share, workgroup, user, password);
912 return -1; /* smbc_server sets errno */
916 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
918 int job = smbc_stat_printjob(srv, path, NULL, NULL);
924 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
932 if (!cli_unlink(&srv->cli, path)) {
934 errno = smbc_errno(&srv->cli);
936 if (errno == EACCES) { /* Check if the file is a directory */
941 time_t m_time = 0, a_time = 0, c_time = 0;
944 if (!smbc_getatr(srv, path, &mode, &size,
945 &c_time, &a_time, &m_time, &ino)) {
947 /* Hmmm, bad error ... What? */
949 errno = smbc_errno(&srv->cli);
955 if (IS_DOS_DIR(mode))
958 errno = saverr; /* Restore this */
967 return 0; /* Success ... */
972 * Routine to rename() a file
975 int smbc_rename(const char *oname, const char *nname)
977 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
978 pstring path1, path2;
979 struct smbc_server *srv = NULL;
981 if (!smbc_initialized) {
983 errno = EINVAL; /* Best I can think of ... */
988 if (!oname || !nname) {
995 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
997 smbc_parse_path(oname, server1, share1, path1, user1, password1);
999 if (user1[0] == (char)0) pstrcpy(user1, smbc_user);
1001 smbc_parse_path(nname, server2, share2, path2, user2, password2);
1003 if (user2[0] == (char)0) pstrcpy(user2, smbc_user);
1005 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1006 strcmp(user1, user2)) {
1008 /* Can't rename across file systems, or users?? */
1015 pstrcpy(workgroup, lp_workgroup());
1017 srv = smbc_server(server1, share1, workgroup, user1, password1);
1024 if (!cli_rename(&srv->cli, path1, path2)) {
1025 int eno = smbc_errno(&srv->cli);
1027 if (eno != EEXIST ||
1028 !cli_unlink(&srv->cli, path2) ||
1029 !cli_rename(&srv->cli, path1, path2)) {
1037 return 0; /* Success */
1042 * A routine to lseek() a file
1045 off_t smbc_lseek(int fd, off_t offset, int whence)
1047 struct smbc_file *fe;
1050 if (!smbc_initialized) {
1057 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1064 fe = smbc_file_table[fd - smbc_start_fd];
1076 return -1; /* Can't lseek a dir ... */
1082 fe->offset = offset;
1086 fe->offset += offset;
1090 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1091 NULL, NULL, NULL) &&
1092 !cli_getattrE(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1098 fe->offset = size + offset;
1112 * Generate an inode number from file name for those things that need it
1116 ino_t smbc_inode(const char *name)
1119 if (!*name) return 2; /* FIXME, why 2 ??? */
1120 return (ino_t)str_checksum(name);
1125 * Routine to put basic stat info into a stat structure ... Used by stat and
1130 int smbc_setup_stat(struct stat *st, char *fname, size_t size, int mode)
1135 if (IS_DOS_DIR(mode)) {
1136 st->st_mode = SMBC_DIR_MODE;
1138 st->st_mode = SMBC_FILE_MODE;
1141 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1142 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1143 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1144 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1147 st->st_blksize = 512;
1148 st->st_blocks = (size+511)/512;
1149 st->st_uid = getuid();
1150 st->st_gid = getgid();
1152 if (IS_DOS_DIR(mode)) {
1158 if (st->st_ino == 0) {
1159 st->st_ino = smbc_inode(fname);
1162 return True; /* FIXME: Is this needed ? */
1167 * Get info from an SMB server on a file. Use a qpathinfo call first
1168 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1171 BOOL smbc_getatr(struct smbc_server *srv, char *path,
1172 uint16 *mode, size_t *size,
1173 time_t *c_time, time_t *a_time, time_t *m_time,
1177 if (!smbc_initialized) {
1184 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1186 if (!srv->no_pathinfo2 &&
1187 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1188 size, mode, ino)) return True;
1190 /* if this is NT then don't bother with the getatr */
1191 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
1193 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1194 a_time = c_time = m_time;
1195 srv->no_pathinfo2 = True;
1202 * Routine to stat a file given a name
1205 int smbc_stat(const char *fname, struct stat *st)
1207 struct smbc_server *srv;
1208 fstring server, share, user, password, workgroup;
1210 time_t m_time = 0, a_time = 0, c_time = 0;
1215 if (!smbc_initialized) {
1217 errno = EINVAL; /* Best I can think of ... */
1229 DEBUG(4, ("smbc_stat(%s)\n", fname));
1231 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
1233 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1235 pstrcpy(workgroup, lp_workgroup());
1237 srv = smbc_server(server, share, workgroup, user, password);
1241 return -1; /* errno set by smbc_server */
1245 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1247 mode = aDIR | aRONLY;
1250 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1252 if (strcmp(path, "\\") == 0) {
1254 mode = aDIR | aRONLY;
1260 smbc_stat_printjob(srv, path, &size, &m_time);
1261 c_time = a_time = m_time;
1266 if (!smbc_getatr(srv, path, &mode, &size,
1267 &c_time, &a_time, &m_time, &ino)) {
1269 errno = smbc_errno(&srv->cli);
1278 smbc_setup_stat(st, path, size, mode);
1280 st->st_atime = a_time;
1281 st->st_ctime = c_time;
1282 st->st_mtime = m_time;
1283 st->st_dev = srv->dev;
1290 * Routine to stat a file given an fd
1293 int smbc_fstat(int fd, struct stat *st)
1295 struct smbc_file *fe;
1296 time_t c_time, a_time, m_time;
1301 if (!smbc_initialized) {
1308 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1315 fe = smbc_file_table[fd - smbc_start_fd];
1326 return smbc_fstatdir(fd, st);
1330 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd,
1331 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1332 !cli_getattrE(&fe->srv->cli, fe->cli_fd,
1333 &mode, &size, &c_time, &a_time, &m_time)) {
1342 smbc_setup_stat(st, fe->fname, size, mode);
1344 st->st_atime = a_time;
1345 st->st_ctime = c_time;
1346 st->st_mtime = m_time;
1347 st->st_dev = fe->srv->dev;
1354 * Routine to open a directory
1358 * smb: which should list all the workgroups available
1360 * smb:workgroup//server
1362 * smb://server/share
1363 * smb://<IP-addr> which should list shares on server
1364 * smb://<IP-addr>/share which should list files on share
1367 static void smbc_remove_dir(struct smbc_file *dir)
1369 struct smbc_dir_list *d,*f;
1376 SAFE_FREE(f->dirent);
1381 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1385 static int add_dirent(struct smbc_file *dir, const char *name, const char *comment, uint32 type)
1387 struct smbc_dirent *dirent;
1391 * Allocate space for the dirent, which must be increased by the
1392 * size of the name and the comment and 1 for the null on the comment.
1393 * The null on the name is already accounted for.
1396 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1397 (comment?strlen(comment):0) + 1;
1399 dirent = malloc(size);
1403 dir->dir_error = ENOMEM;
1408 if (dir->dir_list == NULL) {
1410 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1411 if (!dir->dir_list) {
1414 dir->dir_error = ENOMEM;
1419 dir->dir_end = dir->dir_next = dir->dir_list;
1424 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1426 if (!dir->dir_end) {
1429 dir->dir_error = ENOMEM;
1434 dir->dir_end = dir->dir_end->next;
1438 dir->dir_end->next = NULL;
1439 dir->dir_end->dirent = dirent;
1441 dirent->smbc_type = type;
1442 dirent->namelen = (name?strlen(name):0);
1443 dirent->commentlen = (comment?strlen(comment):0);
1444 dirent->dirlen = size;
1446 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1448 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1449 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1456 list_fn(const char *name, uint32 type, const char *comment, void *state)
1458 struct smbc_file *dir = (struct smbc_file *)state;
1461 /* We need to process the type a little ... */
1463 if (dir->dir_type == SMBC_FILE_SHARE) {
1466 case 0: /* Directory tree */
1467 dirent_type = SMBC_FILE_SHARE;
1471 dirent_type = SMBC_PRINTER_SHARE;
1475 dirent_type = SMBC_COMMS_SHARE;
1479 dirent_type = SMBC_IPC_SHARE;
1483 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1488 else dirent_type = dir->dir_type;
1490 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1492 /* An error occurred, what do we do? */
1493 /* FIXME: Add some code here */
1500 dir_list_fn(file_info *finfo, const char *mask, void *state)
1503 if (add_dirent((struct smbc_file *)state, finfo->name, "",
1504 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1506 /* Handle an error ... */
1507 /* FIXME: Add some code ... */
1513 int smbc_opendir(const char *fname)
1515 fstring server, share, user, password, workgroup;
1517 struct smbc_server *srv = NULL;
1518 struct in_addr rem_ip;
1521 if (!smbc_initialized) {
1535 if (smbc_parse_path(fname, server, share, path, user, password)) {
1542 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1544 pstrcpy(workgroup, lp_workgroup());
1546 /* Get a file entry ... */
1550 while (smbc_file_table[slot])
1553 if (slot > SMBC_MAX_FD) {
1556 return -1; /* FIXME, ... move into a func */
1560 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
1562 if (!smbc_file_table[slot]) {
1569 smbc_file_table[slot]->cli_fd = 0;
1570 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
1571 smbc_file_table[slot]->fname = strdup(fname);
1572 smbc_file_table[slot]->srv = NULL;
1573 smbc_file_table[slot]->offset = 0;
1574 smbc_file_table[slot]->file = False;
1575 smbc_file_table[slot]->dir_list =
1576 smbc_file_table[slot]->dir_next =
1577 smbc_file_table[slot]->dir_end = NULL;
1579 if (server[0] == (char)0) {
1581 if (share[0] != (char)0 || path[0] != (char)0) {
1584 if (smbc_file_table[slot]) {
1585 SAFE_FREE(smbc_file_table[slot]->fname);
1586 SAFE_FREE(smbc_file_table[slot]);
1592 /* We have server and share and path empty ... so list the workgroups */
1594 if (!resolve_name(lp_workgroup(), &rem_ip, 0x1d)) {
1596 errno = EINVAL; /* Something wrong with smb.conf? */
1601 smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;
1603 /* find the name of the server ... */
1605 if (!name_status_find(0, rem_ip, server)) {
1607 DEBUG(0, ("Could not get the name of local master browser for server %s\n", server));
1614 * Get a connection to IPC$ on the server if we do not already have one
1617 srv = smbc_server(server, "IPC$", workgroup, user, password);
1621 if (smbc_file_table[slot]) {
1622 SAFE_FREE(smbc_file_table[slot]->fname);
1623 SAFE_FREE(smbc_file_table[slot]);
1629 smbc_file_table[slot]->srv = srv;
1631 /* Now, list the stuff ... */
1633 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1634 (void *)smbc_file_table[slot])) {
1636 if (smbc_file_table[slot]) {
1637 SAFE_FREE(smbc_file_table[slot]->fname);
1638 SAFE_FREE(smbc_file_table[slot]);
1640 errno = cli_errno(&srv->cli);
1645 else { /* Server not an empty string ... Check the rest and see what gives */
1647 if (share[0] == (char)0) {
1649 if (path[0] != (char)0) { /* Should not have empty share with path */
1652 if (smbc_file_table[slot]) {
1653 SAFE_FREE(smbc_file_table[slot]->fname);
1654 SAFE_FREE(smbc_file_table[slot]);
1660 /* Check to see if <server><1D> translates, or <server><20> translates */
1661 /* However, we check to see if <server> is an IP address first */
1663 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1664 resolve_name(server, &rem_ip, 0x1d)) { /* Found LMB */
1667 smbc_file_table[slot]->dir_type = SMBC_SERVER;
1670 * Get the backup list ...
1674 if (!name_status_find(0, rem_ip, buserver)) {
1676 DEBUG(0, ("Could not get name of local master browser %s\n", server));
1677 errno = EPERM; /* FIXME, is this correct */
1683 * Get a connection to IPC$ on the server if we do not already have one
1686 srv = smbc_server(buserver, "IPC$", workgroup, user, password);
1690 if (smbc_file_table[slot]) {
1691 SAFE_FREE(smbc_file_table[slot]->fname);
1692 SAFE_FREE(smbc_file_table[slot]);
1698 smbc_file_table[slot]->srv = srv;
1700 /* Now, list the servers ... */
1702 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1703 (void *)smbc_file_table[slot])) {
1705 if (smbc_file_table[slot]) {
1706 SAFE_FREE(smbc_file_table[slot]->fname);
1707 SAFE_FREE(smbc_file_table[slot]);
1709 errno = cli_errno(&srv->cli);
1717 if (resolve_name(server, &rem_ip, 0x20)) {
1719 /* Now, list the shares ... */
1721 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1723 srv = smbc_server(server, "IPC$", workgroup, user, password);
1727 if (smbc_file_table[slot]) {
1728 SAFE_FREE(smbc_file_table[slot]->fname);
1729 SAFE_FREE(smbc_file_table[slot]);
1735 smbc_file_table[slot]->srv = srv;
1737 /* Now, list the servers ... */
1739 if (cli_RNetShareEnum(&srv->cli, list_fn,
1740 (void *)smbc_file_table[slot]) < 0) {
1742 errno = cli_errno(&srv->cli);
1743 if (smbc_file_table[slot]) {
1744 SAFE_FREE(smbc_file_table[slot]->fname);
1745 SAFE_FREE(smbc_file_table[slot]);
1754 errno = ENODEV; /* Neither the workgroup nor server exists */
1755 if (smbc_file_table[slot]) {
1756 SAFE_FREE(smbc_file_table[slot]->fname);
1757 SAFE_FREE(smbc_file_table[slot]);
1766 else { /* The server and share are specified ... work from there ... */
1768 /* Well, we connect to the server and list the directory */
1770 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1772 srv = smbc_server(server, share, workgroup, user, password);
1776 if (smbc_file_table[slot]) {
1777 SAFE_FREE(smbc_file_table[slot]->fname);
1778 SAFE_FREE(smbc_file_table[slot]);
1784 smbc_file_table[slot]->srv = srv;
1786 /* Now, list the files ... */
1788 pstrcat(path, "\\*");
1790 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1791 (void *)smbc_file_table[slot]) < 0) {
1793 if (smbc_file_table[slot]) {
1794 SAFE_FREE(smbc_file_table[slot]->fname);
1795 SAFE_FREE(smbc_file_table[slot]);
1797 errno = smbc_errno(&srv->cli);
1805 return smbc_file_table[slot]->smbc_fd;
1810 * Routine to close a directory
1813 int smbc_closedir(int fd)
1815 struct smbc_file *fe;
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 smbc_remove_dir(fe); /* Clean it up */
1844 SAFE_FREE(fe->fname);
1845 SAFE_FREE(fe); /* Free the space too */
1849 smbc_file_table[fd - smbc_start_fd] = NULL;
1856 * Routine to get a directory entry
1859 static char smbc_local_dirent[512]; /* Make big enough */
1861 struct smbc_dirent *smbc_readdir(unsigned int fd)
1863 struct smbc_file *fe;
1864 struct smbc_dirent *dirp, *dirent;
1866 /* Check that all is ok first ... */
1868 if (!smbc_initialized) {
1875 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1882 fe = smbc_file_table[fd - smbc_start_fd];
1891 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1902 dirent = fe->dir_next->dirent;
1911 /* Hmmm, do I even need to copy it? */
1913 bcopy(dirent, smbc_local_dirent, dirent->dirlen); /* Copy the dirent */
1915 dirp = (struct smbc_dirent *)smbc_local_dirent;
1917 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1919 fe->dir_next = fe->dir_next->next;
1921 return (struct smbc_dirent *)smbc_local_dirent;
1927 * Routine to get directory entries
1930 int smbc_getdents(unsigned int fd, struct smbc_dirent *dirp, int count)
1932 struct smbc_file *fe;
1933 struct smbc_dir_list *dir;
1934 int rem = count, reqd;
1935 char *ndir = (char *)dirp;
1937 /* Check that all is ok first ... */
1939 if (!smbc_initialized) {
1946 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1953 fe = smbc_file_table[fd - smbc_start_fd];
1962 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1970 * Now, retrieve the number of entries that will fit in what was passed
1971 * We have to figure out if the info is in the list, or we need to
1972 * send a request to the server to get the info.
1975 while ((dir = fe->dir_next)) {
1976 struct smbc_dirent *dirent;
1980 errno = ENOENT; /* Bad error */
1985 if (rem < (reqd = (sizeof(struct smbc_dirent) + dir->dirent->namelen +
1986 dir->dirent->commentlen + 1))) {
1988 if (rem < count) { /* We managed to copy something */
1994 else { /* Nothing copied ... */
1996 errno = EINVAL; /* Not enough space ... */
2003 dirent = dir->dirent;
2005 bcopy(dirent, ndir, reqd); /* Copy the data in ... */
2007 ((struct smbc_dirent *)ndir)->comment =
2008 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
2014 fe->dir_next = dir = dir -> next;
2025 * Routine to create a directory ...
2028 int smbc_mkdir(const char *fname, mode_t mode)
2030 struct smbc_server *srv;
2031 fstring server, share, user, password, workgroup;
2034 if (!smbc_initialized) {
2048 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
2050 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2052 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2054 pstrcpy(workgroup, lp_workgroup());
2056 srv = smbc_server(server, share, workgroup, user, password);
2060 return -1; /* errno set by smbc_server */
2064 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2066 mode = aDIR | aRONLY;
2069 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2071 if (strcmp(path, "\\") == 0) {
2073 mode = aDIR | aRONLY;
2079 smbc_stat_printjob(srv, path, &size, &m_time);
2080 c_time = a_time = m_time;
2085 if (!cli_mkdir(&srv->cli, path)) {
2087 errno = smbc_errno(&srv->cli);
2097 * Our list function simply checks to see if a directory is not empty
2100 static int smbc_rmdir_dirempty = True;
2102 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
2105 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2106 smbc_rmdir_dirempty = False;
2111 * Routine to remove a directory
2114 int smbc_rmdir(const char *fname)
2116 struct smbc_server *srv;
2117 fstring server, share, user, password, workgroup;
2120 if (!smbc_initialized) {
2134 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2136 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2138 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2140 pstrcpy(workgroup, lp_workgroup());
2142 srv = smbc_server(server, share, workgroup, user, password);
2146 return -1; /* errno set by smbc_server */
2150 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2152 mode = aDIR | aRONLY;
2155 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2157 if (strcmp(path, "\\") == 0) {
2159 mode = aDIR | aRONLY;
2165 smbc_stat_printjob(srv, path, &size, &m_time);
2166 c_time = a_time = m_time;
2171 if (!cli_rmdir(&srv->cli, path)) {
2173 errno = smbc_errno(&srv->cli);
2175 if (errno == EACCES) { /* Check if the dir empty or not */
2177 pstring lpath; /* Local storage to avoid buffer overflows */
2179 smbc_rmdir_dirempty = True; /* Make this so ... */
2181 pstrcpy(lpath, path);
2182 pstrcat(lpath, "\\*");
2184 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2187 /* Fix errno to ignore latest error ... */
2189 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2190 smbc_errno(&srv->cli)));
2195 if (smbc_rmdir_dirempty)
2211 * Routine to return the current directory position
2214 off_t smbc_telldir(int fd)
2216 struct smbc_file *fe;
2218 if (!smbc_initialized) {
2225 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
2232 fe = smbc_file_table[fd - smbc_start_fd];
2241 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2248 return (off_t) fe->dir_next;
2253 * A routine to run down the list and see if the entry is OK
2256 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2257 struct smbc_dirent *dirent)
2260 /* Run down the list looking for what we want */
2264 struct smbc_dir_list *tmp = list;
2268 if (tmp->dirent == dirent)
2277 return NULL; /* Not found, or an error */
2283 * Routine to seek on a directory
2286 int smbc_lseekdir(int fd, off_t offset)
2288 struct smbc_file *fe;
2289 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2290 struct smbc_dir_list *list_ent = NULL;
2292 if (!smbc_initialized) {
2299 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
2306 fe = smbc_file_table[fd - smbc_start_fd];
2315 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2322 /* Now, check what we were passed and see if it is OK ... */
2324 if (dirent == NULL) { /* Seek to the begining of the list */
2326 fe->dir_next = fe->dir_list;
2331 /* Now, run down the list and make sure that the entry is OK */
2332 /* This may need to be changed if we change the format of the list */
2334 if ((list_ent = smbc_check_dir_ent(fe->dir_list, dirent)) == NULL) {
2336 errno = EINVAL; /* Bad entry */
2341 fe->dir_next = list_ent;
2348 * Routine to fstat a dir
2351 int smbc_fstatdir(int fd, struct stat *st)
2354 if (!smbc_initialized) {
2361 /* No code yet ... */
2368 * Routine to print a file on a remote server ...
2370 * We open the file, which we assume to be on a remote server, and then
2371 * copy it to a print file on the share specified by printq.
2374 int smbc_print_file(const char *fname, const char *printq)
2376 int fid1, fid2, bytes, saverr, tot_bytes = 0;
2379 if (!smbc_initialized) {
2386 if (!fname && !printq) {
2393 /* Try to open the file for reading ... */
2395 if ((fid1 = smbc_open(fname, O_RDONLY, 0666)) < 0) {
2397 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2398 return -1; /* smbc_open sets errno */
2402 /* Now, try to open the printer file for writing */
2404 if ((fid2 = smbc_open_print_job(printq)) < 0) {
2406 saverr = errno; /* Save errno */
2413 while ((bytes = smbc_read(fid1, buf, sizeof(buf))) > 0) {
2417 if ((smbc_write(fid2, buf, bytes)) < 0) {
2430 smbc_close(fid1); /* We have to close these anyway */
2445 * Open a print file to be written to by other calls
2448 int smbc_open_print_job(const char *fname)
2450 fstring server, share, user, password;
2453 if (!smbc_initialized) {
2467 DEBUG(4, ("smbc_open_print_job(%s)\n", fname));
2469 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2471 /* What if the path is empty, or the file exists? */
2473 return smbc_open(fname, O_WRONLY, 666);
2478 * Routine to list print jobs on a printer share ...
2481 int smbc_list_print_jobs(const char *fname, void (*fn)(struct print_job_info *))
2483 struct smbc_server *srv;
2484 fstring server, share, user, password, workgroup;
2487 if (!smbc_initialized) {
2501 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2503 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2505 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2507 pstrcpy(workgroup, lp_workgroup());
2509 srv = smbc_server(server, share, workgroup, user, password);
2513 return -1; /* errno set by smbc_server */
2517 if (cli_print_queue(&srv->cli, fn) < 0) {
2519 errno = smbc_errno(&srv->cli);
2529 * Delete a print job from a remote printer share
2532 int smbc_unlink_print_job(const char *fname, int id)
2534 struct smbc_server *srv;
2535 fstring server, share, user, password, workgroup;
2539 if (!smbc_initialized) {
2553 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2555 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2557 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2559 pstrcpy(workgroup, lp_workgroup());
2561 srv = smbc_server(server, share, workgroup, user, password);
2565 return -1; /* errno set by smbc_server */
2569 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2572 errno = smbc_errno(&srv->cli);
2573 else if (err == ERRnosuchprintjob)