2 Unix SMB/CIFS implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000
6 Copyright (C) John Terpstra 2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libsmbclient.h"
26 /* Structure for servers ... Held here so we don't need an include ...
27 * May be better to put in an include file
31 struct smbc_server *next, *prev;
41 /* Keep directory entries in a list */
42 struct smbc_dir_list {
43 struct smbc_dir_list *next;
44 struct smbc_dirent *dirent;
52 struct smbc_server *srv;
54 struct smbc_dir_list *dir_list, *dir_end, *dir_next;
55 int dir_type, dir_error;
58 int smbc_fstatdir(int fd, struct stat *st); /* Forward decl */
59 BOOL smbc_getatr(struct smbc_server *srv, char *path,
60 uint16 *mode, size_t *size,
61 time_t *c_time, time_t *a_time, time_t *m_time,
64 extern BOOL in_client;
65 extern pstring global_myname;
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 struct smbc_file **smbc_file_table;
71 static struct smbc_server *smbc_srvs;
72 static pstring my_netbios_name;
73 static pstring smbc_user;
76 * Function to parse a path and turn it into components
78 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
80 * smb:// means show all the workgroups
81 * smb://name/ means, if name<1D> exists, list servers in workgroup,
82 * else, if name<20> exists, list all shares for server ...
85 static const char *smbc_prefix = "smb:";
88 smbc_parse_path(const char *fname, char *server, char *share, char *path,
89 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 /* check that '@' occurs before '/', if '/' exists at all */
137 q = strchr_m(p, '@');
138 r = strchr_m(p, '/');
139 if (q && (!r || q < r)) {
140 pstring username, passwd, domain;
143 next_token(&p, userinfo, "@", sizeof(fstring));
145 username[0] = passwd[0] = domain[0] = 0;
147 if (strchr_m(u, ';')) {
149 next_token(&u, domain, ";", sizeof(fstring));
153 if (strchr_m(u, ':')) {
155 next_token(&u, username, ":", sizeof(fstring));
162 pstrcpy(username, u);
167 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
170 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
174 if (!next_token(&p, server, "/", sizeof(fstring))) {
180 if (*p == (char)0) return 0; /* That's it ... */
182 if (!next_token(&p, share, "/", sizeof(fstring))) {
190 all_string_sub(path, "/", "\\", 0);
196 * Convert an SMB error into a UNIX error ...
199 int smbc_errno(struct cli_state *c)
203 if (cli_is_dos_error(c)) {
207 cli_dos_error(c, &eclass, &ecode);
208 ret = cli_errno_from_dos(eclass, ecode);
210 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
211 (int)eclass, (int)ecode, (int)ecode, ret));
215 status = cli_nt_error(c);
216 ret = cli_errno_from_nt(status);
218 DEBUG(3,("smbc errno %s -> %d\n",
219 nt_errstr(status), ret));
226 * Connect to a server, possibly on an existing connection
228 * Here, what we want to do is: If the server and username
229 * match an existing connection, reuse that, otherwise, establish a
232 * If we have to create a new connection, call the auth_fn to get the
233 * info we need, unless the username and password were passed in.
236 struct smbc_server *smbc_server(char *server, char *share,
237 char *workgroup, char *username,
240 struct smbc_server *srv=NULL;
242 struct nmb_name called, calling;
243 char *p, *server_n = server;
251 /* try to use an existing connection */
252 for (srv=smbc_srvs;srv;srv=srv->next) {
253 if (strcmp(server,srv->server_name)==0 &&
254 strcmp(share,srv->share_name)==0 &&
255 strcmp(workgroup,srv->workgroup)==0 &&
256 strcmp(username, srv->username) == 0)
260 if (server[0] == 0) {
266 * Pick up the auth info here, once we know we need to connect
267 * But only if we do not have a username and password ...
270 if (!username[0] || !password[0])
271 smbc_auth_fn(server, share, workgroup, sizeof(fstring),
272 username, sizeof(fstring), password, sizeof(fstring));
275 * However, smbc_auth_fn may have picked up info relating to an
276 * existing connection, so try for an existing connection again ...
279 for (srv=smbc_srvs;srv;srv=srv->next) {
280 if (strcmp(server,srv->server_name)==0 &&
281 strcmp(share,srv->share_name)==0 &&
282 strcmp(workgroup,srv->workgroup)==0 &&
283 strcmp(username, srv->username) == 0)
287 make_nmb_name(&calling, my_netbios_name, 0x0);
288 make_nmb_name(&called , server, 0x20);
290 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
292 if ((p=strchr_m(server_n,'#')) &&
293 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
295 fstrcpy(group, server_n);
296 p = strchr_m(group,'#');
301 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
304 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
308 /* have to open a new connection */
309 if (!cli_initialise(&c) || !cli_connect(&c, server_n, &ip)) {
310 if (c.initialised) cli_shutdown(&c);
315 if (!cli_session_request(&c, &calling, &called)) {
317 if (strcmp(called.name, "*SMBSERVER")) {
318 make_nmb_name(&called , "*SMBSERVER", 0x20);
325 DEBUG(4,(" session request ok\n"));
327 if (!cli_negprot(&c)) {
333 if (!cli_session_setup(&c, username,
334 password, strlen(password),
335 password, strlen(password),
337 /* try an anonymous login if it failed */
338 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
344 DEBUG(4,(" session setup ok\n"));
346 if (!cli_send_tconX(&c, share, "?????",
347 password, strlen(password)+1)) {
348 errno = smbc_errno(&c);
353 DEBUG(4,(" tconx ok\n"));
355 srv = (struct smbc_server *)malloc(sizeof(*srv));
365 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
367 srv->server_name = strdup(server);
368 if (!srv->server_name) {
373 srv->share_name = strdup(share);
374 if (!srv->share_name) {
379 srv->workgroup = strdup(workgroup);
380 if (!srv->workgroup) {
385 srv->username = strdup(username);
386 if (!srv->username) {
391 DLIST_ADD(smbc_srvs, srv);
397 if (!srv) return NULL;
399 SAFE_FREE(srv->server_name);
400 SAFE_FREE(srv->share_name);
401 SAFE_FREE(srv->workgroup);
402 SAFE_FREE(srv->username);
408 *Remove a server from the list smbc_srvs if it's unused -- Tom (tom@ninja.nl)
412 BOOL smbc_remove_unused_server(struct smbc_server * s)
416 /* are we being fooled ? */
417 if (!s) return False;
419 /* close all open files/directories on this server */
420 for (p = 0; p < SMBC_MAX_FD; p++) {
421 if (smbc_file_table[p] &&
422 smbc_file_table[p]->srv == s) {
423 /* Still used .. DARN */
424 DEBUG(3, ("smbc_remove_usused_server: %x still used by %s (%d).\n", (int) s,
425 smbc_file_table[p]->fname, smbc_file_table[p]->smbc_fd));
430 cli_shutdown(&s->cli);
432 SAFE_FREE(s->username);
433 SAFE_FREE(s->workgroup);
434 SAFE_FREE(s->server_name);
435 SAFE_FREE(s->share_name);
436 DLIST_REMOVE(smbc_srvs, s);
437 DEBUG(3, ("smbc_remove_usused_server: %x removed.\n", (int) s));
443 *Initialise the library etc
445 * We accept valid values for debug from 0 to 100,
446 * and insist that fn must be non-null.
449 int smbc_init(smbc_get_auth_data_fn fn, int debug)
453 char *user = NULL, *home = NULL, *pname="libsmbclient";
455 if (!fn || debug < 0 || debug > 100) {
462 if (smbc_initialized) { /* Don't go through this if we have already done it */
468 smbc_initialized = 1;
470 /* smbc_debug = debug; */
474 setup_logging(pname, False);
476 /* Here we would open the smb.conf file if needed ... */
478 home = getenv("HOME");
480 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
482 load_interfaces(); /* Load the list of interfaces ... */
484 in_client = True; /* FIXME, make a param */
486 if (!lp_load(conf, True, False, False)) {
489 * Hmmm, what the hell do we do here ... we could not parse the
490 * config file ... We must return an error ... and keep info around
491 * about why we failed
494 errno = ENOENT; /* FIXME: Figure out the correct error response */
499 reopen_logs(); /* Get logging working ... */
502 * FIXME: Is this the best way to get the user info?
505 user = getenv("USER");
506 /* walk around as "guest" if no username can be found */
507 if (!user) user = strdup("guest");
508 pstrcpy(smbc_user, user); /* Save for use elsewhere */
511 * We try to get our netbios name from the config. If that fails we fall
512 * back on constructing our netbios name from our hostname etc
515 pstrcpy(my_netbios_name, global_myname);
519 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
522 slprintf(my_netbios_name, 16, "smbc%s%d", user, pid);
524 DEBUG(0,("Using netbios name %s.\n", my_netbios_name));
526 name_register_wins(my_netbios_name, 0);
529 * Now initialize the file descriptor array and figure out what the
530 * max open files is, so we can return FD's that are above the max
531 * open file, and separated by a guard band
534 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
538 if (getrlimit(RLIMIT_NOFILE, &rlp)) {
540 DEBUG(0, ("smbc_init: getrlimit(1) for RLIMIT_NOFILE failed with error %s\n", strerror(errno)));
542 smbc_start_fd = 1000000;
547 smbc_start_fd = rlp.rlim_max + 10000; /* Leave a guard space of 10,000 */
551 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
553 smbc_start_fd = 1000000;
557 smbc_file_table = malloc(SMBC_MAX_FD * sizeof(struct smbc_file *));
559 for (p = 0; p < SMBC_MAX_FD; p++)
560 smbc_file_table[p] = NULL;
562 if (!smbc_file_table)
565 return 0; /* Success */
570 * Routine to open() a file ...
573 int smbc_open(const char *fname, int flags, mode_t mode)
575 fstring server, share, user, password, workgroup;
577 struct smbc_server *srv = NULL;
580 if (!smbc_initialized) {
582 errno = EINVAL; /* Best I can think of ... */
594 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
596 if (user[0] == (char)0) pstrcpy(user, smbc_user);
598 pstrcpy(workgroup, lp_workgroup());
600 srv = smbc_server(server, share, workgroup, user, password);
604 if (errno == EPERM) errno = EACCES;
605 return -1; /* smbc_server sets errno */
609 /* Hmmm, the test for a directory is suspect here ... FIXME */
611 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
620 /* Find a free slot first */
622 while (smbc_file_table[slot])
625 if (slot > SMBC_MAX_FD) {
627 errno = ENOMEM; /* FIXME, is this best? */
632 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
634 if (!smbc_file_table[slot]) {
641 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
643 /* Handle the error ... */
645 SAFE_FREE(smbc_file_table[slot]);
646 errno = smbc_errno(&srv->cli);
651 /* Fill in file struct */
653 smbc_file_table[slot]->cli_fd = fd;
654 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
655 smbc_file_table[slot]->fname = strdup(fname);
656 smbc_file_table[slot]->srv = srv;
657 smbc_file_table[slot]->offset = 0;
658 smbc_file_table[slot]->file = True;
660 return smbc_file_table[slot]->smbc_fd;
664 /* Check if opendir needed ... */
669 eno = smbc_errno(&srv->cli);
670 fd = smbc_opendir(fname);
671 if (fd < 0) errno = eno;
676 return 1; /* Success, with fd ... */
681 * Routine to create a file
684 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
686 int smbc_creat(const char *path, mode_t mode)
689 if (!smbc_initialized) {
696 return smbc_open(path, creat_bits, mode);
700 * Routine to read() a file ...
703 ssize_t smbc_read(int fd, void *buf, size_t count)
705 struct smbc_file *fe;
708 if (!smbc_initialized) {
715 DEBUG(4, ("smbc_read(%d, %d)\n", fd, (int)count));
717 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
724 /* Check that the buffer exists ... */
733 fe = smbc_file_table[fd - smbc_start_fd];
735 if (!fe || !fe->file) {
742 ret = cli_read(&fe->srv->cli, fe->cli_fd, buf, fe->offset, count);
746 errno = smbc_errno(&fe->srv->cli);
753 DEBUG(4, (" --> %d\n", ret));
755 return ret; /* Success, ret bytes of data ... */
760 * Routine to write() a file ...
763 ssize_t smbc_write(int fd, void *buf, size_t count)
766 struct smbc_file *fe;
768 if (!smbc_initialized) {
775 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
782 /* Check that the buffer exists ... */
791 fe = smbc_file_table[fd - smbc_start_fd];
793 if (!fe || !fe->file) {
800 ret = cli_write(&fe->srv->cli, fe->cli_fd, 0, buf, fe->offset, count);
804 errno = smbc_errno(&fe->srv->cli);
811 return ret; /* Success, 0 bytes of data ... */
815 * Routine to close() a file ...
818 int smbc_close(int fd)
820 struct smbc_file *fe;
821 struct smbc_server *srv;
823 if (!smbc_initialized) {
830 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
837 fe = smbc_file_table[fd - smbc_start_fd];
848 return smbc_closedir(fd);
852 if (!cli_close(&fe->srv->cli, fe->cli_fd)) {
854 DEBUG(3, ("cli_close failed on %s (%d). purging server.\n",
855 fe->fname, fe->smbc_fd));
856 /* Deallocate slot and remove the server
857 * from the server cache if unused */
858 errno = smbc_errno(&fe->srv->cli);
860 SAFE_FREE(fe->fname);
862 smbc_file_table[fd - smbc_start_fd] = NULL;
863 smbc_remove_unused_server(srv);
869 SAFE_FREE(fe->fname);
871 smbc_file_table[fd - smbc_start_fd] = NULL;
877 * Routine to unlink() a file
880 int smbc_unlink(const char *fname)
882 fstring server, share, user, password, workgroup;
884 struct smbc_server *srv = NULL;
886 if (!smbc_initialized) {
888 errno = EINVAL; /* Best I can think of ... */
900 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
902 if (user[0] == (char)0) pstrcpy(user, smbc_user);
904 pstrcpy(workgroup, lp_workgroup());
906 srv = smbc_server(server, share, workgroup, user, password);
910 return -1; /* smbc_server sets errno */
914 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
916 int job = smbc_stat_printjob(srv, path, NULL, NULL);
922 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
930 if (!cli_unlink(&srv->cli, path)) {
932 errno = smbc_errno(&srv->cli);
934 if (errno == EACCES) { /* Check if the file is a directory */
939 time_t m_time = 0, a_time = 0, c_time = 0;
942 if (!smbc_getatr(srv, path, &mode, &size,
943 &c_time, &a_time, &m_time, &ino)) {
945 /* Hmmm, bad error ... What? */
947 errno = smbc_errno(&srv->cli);
953 if (IS_DOS_DIR(mode))
956 errno = saverr; /* Restore this */
965 return 0; /* Success ... */
970 * Routine to rename() a file
973 int smbc_rename(const char *oname, const char *nname)
975 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
976 pstring path1, path2;
977 struct smbc_server *srv = NULL;
979 if (!smbc_initialized) {
981 errno = EINVAL; /* Best I can think of ... */
986 if (!oname || !nname) {
993 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
995 smbc_parse_path(oname, server1, share1, path1, user1, password1);
997 if (user1[0] == (char)0) pstrcpy(user1, smbc_user);
999 smbc_parse_path(nname, server2, share2, path2, user2, password2);
1001 if (user2[0] == (char)0) pstrcpy(user2, smbc_user);
1003 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1004 strcmp(user1, user2)) {
1006 /* Can't rename across file systems, or users?? */
1013 pstrcpy(workgroup, lp_workgroup());
1015 srv = smbc_server(server1, share1, workgroup, user1, password1);
1022 if (!cli_rename(&srv->cli, path1, path2)) {
1023 int eno = smbc_errno(&srv->cli);
1025 if (eno != EEXIST ||
1026 !cli_unlink(&srv->cli, path2) ||
1027 !cli_rename(&srv->cli, path1, path2)) {
1035 return 0; /* Success */
1040 * A routine to lseek() a file
1043 off_t smbc_lseek(int fd, off_t offset, int whence)
1045 struct smbc_file *fe;
1048 if (!smbc_initialized) {
1055 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1062 fe = smbc_file_table[fd - smbc_start_fd];
1074 return -1; /* Can't lseek a dir ... */
1080 fe->offset = offset;
1084 fe->offset += offset;
1088 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1089 NULL, NULL, NULL) &&
1090 !cli_getattrE(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1096 fe->offset = size + offset;
1110 * Generate an inode number from file name for those things that need it
1114 ino_t smbc_inode(const char *name)
1117 if (!*name) return 2; /* FIXME, why 2 ??? */
1118 return (ino_t)str_checksum(name);
1123 * Routine to put basic stat info into a stat structure ... Used by stat and
1128 int smbc_setup_stat(struct stat *st, char *fname, size_t size, int mode)
1133 if (IS_DOS_DIR(mode)) {
1134 st->st_mode = SMBC_DIR_MODE;
1136 st->st_mode = SMBC_FILE_MODE;
1139 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1140 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1141 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1142 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1145 st->st_blksize = 512;
1146 st->st_blocks = (size+511)/512;
1147 st->st_uid = getuid();
1148 st->st_gid = getgid();
1150 if (IS_DOS_DIR(mode)) {
1156 if (st->st_ino == 0) {
1157 st->st_ino = smbc_inode(fname);
1160 return True; /* FIXME: Is this needed ? */
1165 * Get info from an SMB server on a file. Use a qpathinfo call first
1166 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1169 BOOL smbc_getatr(struct smbc_server *srv, char *path,
1170 uint16 *mode, size_t *size,
1171 time_t *c_time, time_t *a_time, time_t *m_time,
1175 if (!smbc_initialized) {
1182 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1184 if (!srv->no_pathinfo2 &&
1185 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1186 size, mode, ino)) return True;
1188 /* if this is NT then don't bother with the getatr */
1189 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
1191 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1192 a_time = c_time = m_time;
1193 srv->no_pathinfo2 = True;
1200 * Routine to stat a file given a name
1203 int smbc_stat(const char *fname, struct stat *st)
1205 struct smbc_server *srv;
1206 fstring server, share, user, password, workgroup;
1208 time_t m_time = 0, a_time = 0, c_time = 0;
1213 if (!smbc_initialized) {
1215 errno = EINVAL; /* Best I can think of ... */
1227 DEBUG(4, ("smbc_stat(%s)\n", fname));
1229 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
1231 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1233 pstrcpy(workgroup, lp_workgroup());
1235 srv = smbc_server(server, share, workgroup, user, password);
1239 return -1; /* errno set by smbc_server */
1243 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1245 mode = aDIR | aRONLY;
1248 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1250 if (strcmp(path, "\\") == 0) {
1252 mode = aDIR | aRONLY;
1258 smbc_stat_printjob(srv, path, &size, &m_time);
1259 c_time = a_time = m_time;
1264 if (!smbc_getatr(srv, path, &mode, &size,
1265 &c_time, &a_time, &m_time, &ino)) {
1267 errno = smbc_errno(&srv->cli);
1276 smbc_setup_stat(st, path, size, mode);
1278 st->st_atime = a_time;
1279 st->st_ctime = c_time;
1280 st->st_mtime = m_time;
1281 st->st_dev = srv->dev;
1288 * Routine to stat a file given an fd
1291 int smbc_fstat(int fd, struct stat *st)
1293 struct smbc_file *fe;
1294 time_t c_time, a_time, m_time;
1299 if (!smbc_initialized) {
1306 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1313 fe = smbc_file_table[fd - smbc_start_fd];
1324 return smbc_fstatdir(fd, st);
1328 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd,
1329 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1330 !cli_getattrE(&fe->srv->cli, fe->cli_fd,
1331 &mode, &size, &c_time, &a_time, &m_time)) {
1340 smbc_setup_stat(st, fe->fname, size, mode);
1342 st->st_atime = a_time;
1343 st->st_ctime = c_time;
1344 st->st_mtime = m_time;
1345 st->st_dev = fe->srv->dev;
1352 * Routine to open a directory
1356 * smb: which should list all the workgroups available
1358 * smb:workgroup//server
1360 * smb://server/share
1361 * smb://<IP-addr> which should list shares on server
1362 * smb://<IP-addr>/share which should list files on share
1365 static void smbc_remove_dir(struct smbc_file *dir)
1367 struct smbc_dir_list *d,*f;
1374 SAFE_FREE(f->dirent);
1379 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1383 static int add_dirent(struct smbc_file *dir, const char *name, const char *comment, uint32 type)
1385 struct smbc_dirent *dirent;
1389 * Allocate space for the dirent, which must be increased by the
1390 * size of the name and the comment and 1 for the null on the comment.
1391 * The null on the name is already accounted for.
1394 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1395 (comment?strlen(comment):0) + 1;
1397 dirent = malloc(size);
1401 dir->dir_error = ENOMEM;
1406 if (dir->dir_list == NULL) {
1408 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1409 if (!dir->dir_list) {
1412 dir->dir_error = ENOMEM;
1417 dir->dir_end = dir->dir_next = dir->dir_list;
1422 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1424 if (!dir->dir_end) {
1427 dir->dir_error = ENOMEM;
1432 dir->dir_end = dir->dir_end->next;
1436 dir->dir_end->next = NULL;
1437 dir->dir_end->dirent = dirent;
1439 dirent->smbc_type = type;
1440 dirent->namelen = (name?strlen(name):0);
1441 dirent->commentlen = (comment?strlen(comment):0);
1442 dirent->dirlen = size;
1444 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1446 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1447 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1454 list_fn(const char *name, uint32 type, const char *comment, void *state)
1456 struct smbc_file *dir = (struct smbc_file *)state;
1459 /* We need to process the type a little ... */
1461 if (dir->dir_type == SMBC_FILE_SHARE) {
1464 case 0: /* Directory tree */
1465 dirent_type = SMBC_FILE_SHARE;
1469 dirent_type = SMBC_PRINTER_SHARE;
1473 dirent_type = SMBC_COMMS_SHARE;
1477 dirent_type = SMBC_IPC_SHARE;
1481 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1486 else dirent_type = dir->dir_type;
1488 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1490 /* An error occurred, what do we do? */
1491 /* FIXME: Add some code here */
1498 dir_list_fn(file_info *finfo, const char *mask, void *state)
1501 if (add_dirent((struct smbc_file *)state, finfo->name, "",
1502 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1504 /* Handle an error ... */
1505 /* FIXME: Add some code ... */
1511 int smbc_opendir(const char *fname)
1513 fstring server, share, user, password, workgroup;
1515 struct smbc_server *srv = NULL;
1516 struct in_addr rem_ip;
1519 if (!smbc_initialized) {
1533 if (smbc_parse_path(fname, server, share, path, user, password)) {
1540 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1542 pstrcpy(workgroup, lp_workgroup());
1544 /* Get a file entry ... */
1548 while (smbc_file_table[slot])
1551 if (slot > SMBC_MAX_FD) {
1554 return -1; /* FIXME, ... move into a func */
1558 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
1560 if (!smbc_file_table[slot]) {
1567 smbc_file_table[slot]->cli_fd = 0;
1568 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
1569 smbc_file_table[slot]->fname = strdup(fname);
1570 smbc_file_table[slot]->srv = NULL;
1571 smbc_file_table[slot]->offset = 0;
1572 smbc_file_table[slot]->file = False;
1573 smbc_file_table[slot]->dir_list =
1574 smbc_file_table[slot]->dir_next =
1575 smbc_file_table[slot]->dir_end = NULL;
1577 if (server[0] == (char)0) {
1579 if (share[0] != (char)0 || path[0] != (char)0) {
1582 if (smbc_file_table[slot]) {
1583 SAFE_FREE(smbc_file_table[slot]->fname);
1584 SAFE_FREE(smbc_file_table[slot]);
1590 /* We have server and share and path empty ... so list the workgroups */
1592 if (!resolve_name(lp_workgroup(), &rem_ip, 0x1d)) {
1594 errno = EINVAL; /* Something wrong with smb.conf? */
1599 smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;
1601 /* find the name of the server ... */
1603 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1605 DEBUG(0, ("Could not get the name of local master browser for server %s\n", server));
1612 * Get a connection to IPC$ on the server if we do not already have one
1615 srv = smbc_server(server, "IPC$", workgroup, user, password);
1619 if (smbc_file_table[slot]) {
1620 SAFE_FREE(smbc_file_table[slot]->fname);
1621 SAFE_FREE(smbc_file_table[slot]);
1627 smbc_file_table[slot]->srv = srv;
1629 /* Now, list the stuff ... */
1631 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1632 (void *)smbc_file_table[slot])) {
1634 if (smbc_file_table[slot]) {
1635 SAFE_FREE(smbc_file_table[slot]->fname);
1636 SAFE_FREE(smbc_file_table[slot]);
1638 errno = cli_errno(&srv->cli);
1643 else { /* Server not an empty string ... Check the rest and see what gives */
1645 if (share[0] == (char)0) {
1647 if (path[0] != (char)0) { /* Should not have empty share with path */
1650 if (smbc_file_table[slot]) {
1651 SAFE_FREE(smbc_file_table[slot]->fname);
1652 SAFE_FREE(smbc_file_table[slot]);
1658 /* Check to see if <server><1D> translates, or <server><20> translates */
1659 /* However, we check to see if <server> is an IP address first */
1661 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1662 resolve_name(server, &rem_ip, 0x1d)) { /* Found LMB */
1665 smbc_file_table[slot]->dir_type = SMBC_SERVER;
1668 * Get the backup list ...
1672 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1674 DEBUG(0, ("Could not get name of local master browser %s\n", server));
1675 errno = EPERM; /* FIXME, is this correct */
1681 * Get a connection to IPC$ on the server if we do not already have one
1684 srv = smbc_server(buserver, "IPC$", workgroup, user, password);
1688 if (smbc_file_table[slot]) {
1689 SAFE_FREE(smbc_file_table[slot]->fname);
1690 SAFE_FREE(smbc_file_table[slot]);
1696 smbc_file_table[slot]->srv = srv;
1698 /* Now, list the servers ... */
1700 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1701 (void *)smbc_file_table[slot])) {
1703 if (smbc_file_table[slot]) {
1704 SAFE_FREE(smbc_file_table[slot]->fname);
1705 SAFE_FREE(smbc_file_table[slot]);
1707 errno = cli_errno(&srv->cli);
1715 if (resolve_name(server, &rem_ip, 0x20)) {
1717 /* Now, list the shares ... */
1719 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1721 srv = smbc_server(server, "IPC$", workgroup, user, password);
1725 if (smbc_file_table[slot]) {
1726 SAFE_FREE(smbc_file_table[slot]->fname);
1727 SAFE_FREE(smbc_file_table[slot]);
1733 smbc_file_table[slot]->srv = srv;
1735 /* Now, list the servers ... */
1737 if (cli_RNetShareEnum(&srv->cli, list_fn,
1738 (void *)smbc_file_table[slot]) < 0) {
1740 errno = cli_errno(&srv->cli);
1741 if (smbc_file_table[slot]) {
1742 SAFE_FREE(smbc_file_table[slot]->fname);
1743 SAFE_FREE(smbc_file_table[slot]);
1752 errno = ENODEV; /* Neither the workgroup nor server exists */
1753 if (smbc_file_table[slot]) {
1754 SAFE_FREE(smbc_file_table[slot]->fname);
1755 SAFE_FREE(smbc_file_table[slot]);
1764 else { /* The server and share are specified ... work from there ... */
1766 /* Well, we connect to the server and list the directory */
1768 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1770 srv = smbc_server(server, share, workgroup, user, password);
1774 if (smbc_file_table[slot]) {
1775 SAFE_FREE(smbc_file_table[slot]->fname);
1776 SAFE_FREE(smbc_file_table[slot]);
1782 smbc_file_table[slot]->srv = srv;
1784 /* Now, list the files ... */
1786 pstrcat(path, "\\*");
1788 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1789 (void *)smbc_file_table[slot]) < 0) {
1791 if (smbc_file_table[slot]) {
1792 SAFE_FREE(smbc_file_table[slot]->fname);
1793 SAFE_FREE(smbc_file_table[slot]);
1795 errno = smbc_errno(&srv->cli);
1803 return smbc_file_table[slot]->smbc_fd;
1808 * Routine to close a directory
1811 int smbc_closedir(int fd)
1813 struct smbc_file *fe;
1815 if (!smbc_initialized) {
1822 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1829 fe = smbc_file_table[fd - smbc_start_fd];
1838 smbc_remove_dir(fe); /* Clean it up */
1842 SAFE_FREE(fe->fname);
1843 SAFE_FREE(fe); /* Free the space too */
1847 smbc_file_table[fd - smbc_start_fd] = NULL;
1854 * Routine to get a directory entry
1857 static char smbc_local_dirent[512]; /* Make big enough */
1859 struct smbc_dirent *smbc_readdir(unsigned int fd)
1861 struct smbc_file *fe;
1862 struct smbc_dirent *dirp, *dirent;
1864 /* Check that all is ok first ... */
1866 if (!smbc_initialized) {
1873 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1880 fe = smbc_file_table[fd - smbc_start_fd];
1889 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1900 dirent = fe->dir_next->dirent;
1909 /* Hmmm, do I even need to copy it? */
1911 memcpy(smbc_local_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1913 dirp = (struct smbc_dirent *)smbc_local_dirent;
1915 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1917 fe->dir_next = fe->dir_next->next;
1919 return (struct smbc_dirent *)smbc_local_dirent;
1925 * Routine to get directory entries
1928 int smbc_getdents(unsigned int fd, struct smbc_dirent *dirp, int count)
1930 struct smbc_file *fe;
1931 struct smbc_dir_list *dir;
1932 int rem = count, reqd;
1933 char *ndir = (char *)dirp;
1935 /* Check that all is ok first ... */
1937 if (!smbc_initialized) {
1944 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1951 fe = smbc_file_table[fd - smbc_start_fd];
1960 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1968 * Now, retrieve the number of entries that will fit in what was passed
1969 * We have to figure out if the info is in the list, or we need to
1970 * send a request to the server to get the info.
1973 while ((dir = fe->dir_next)) {
1974 struct smbc_dirent *dirent;
1978 errno = ENOENT; /* Bad error */
1983 if (rem < (reqd = (sizeof(struct smbc_dirent) + dir->dirent->namelen +
1984 dir->dirent->commentlen + 1))) {
1986 if (rem < count) { /* We managed to copy something */
1992 else { /* Nothing copied ... */
1994 errno = EINVAL; /* Not enough space ... */
2001 dirent = dir->dirent;
2003 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
2005 ((struct smbc_dirent *)ndir)->comment =
2006 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
2012 fe->dir_next = dir = dir -> next;
2023 * Routine to create a directory ...
2026 int smbc_mkdir(const char *fname, mode_t mode)
2028 struct smbc_server *srv;
2029 fstring server, share, user, password, workgroup;
2032 if (!smbc_initialized) {
2046 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
2048 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2050 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2052 pstrcpy(workgroup, lp_workgroup());
2054 srv = smbc_server(server, share, workgroup, user, password);
2058 return -1; /* errno set by smbc_server */
2062 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2064 mode = aDIR | aRONLY;
2067 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2069 if (strcmp(path, "\\") == 0) {
2071 mode = aDIR | aRONLY;
2077 smbc_stat_printjob(srv, path, &size, &m_time);
2078 c_time = a_time = m_time;
2083 if (!cli_mkdir(&srv->cli, path)) {
2085 errno = smbc_errno(&srv->cli);
2095 * Our list function simply checks to see if a directory is not empty
2098 static int smbc_rmdir_dirempty = True;
2100 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
2103 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2104 smbc_rmdir_dirempty = False;
2109 * Routine to remove a directory
2112 int smbc_rmdir(const char *fname)
2114 struct smbc_server *srv;
2115 fstring server, share, user, password, workgroup;
2118 if (!smbc_initialized) {
2132 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2134 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2136 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2138 pstrcpy(workgroup, lp_workgroup());
2140 srv = smbc_server(server, share, workgroup, user, password);
2144 return -1; /* errno set by smbc_server */
2148 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2150 mode = aDIR | aRONLY;
2153 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2155 if (strcmp(path, "\\") == 0) {
2157 mode = aDIR | aRONLY;
2163 smbc_stat_printjob(srv, path, &size, &m_time);
2164 c_time = a_time = m_time;
2169 if (!cli_rmdir(&srv->cli, path)) {
2171 errno = smbc_errno(&srv->cli);
2173 if (errno == EACCES) { /* Check if the dir empty or not */
2175 pstring lpath; /* Local storage to avoid buffer overflows */
2177 smbc_rmdir_dirempty = True; /* Make this so ... */
2179 pstrcpy(lpath, path);
2180 pstrcat(lpath, "\\*");
2182 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2185 /* Fix errno to ignore latest error ... */
2187 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2188 smbc_errno(&srv->cli)));
2193 if (smbc_rmdir_dirempty)
2209 * Routine to return the current directory position
2212 off_t smbc_telldir(int fd)
2214 struct smbc_file *fe;
2216 if (!smbc_initialized) {
2223 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
2230 fe = smbc_file_table[fd - smbc_start_fd];
2239 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2246 return (off_t) fe->dir_next;
2251 * A routine to run down the list and see if the entry is OK
2254 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2255 struct smbc_dirent *dirent)
2258 /* Run down the list looking for what we want */
2262 struct smbc_dir_list *tmp = list;
2266 if (tmp->dirent == dirent)
2275 return NULL; /* Not found, or an error */
2281 * Routine to seek on a directory
2284 int smbc_lseekdir(int fd, off_t offset)
2286 struct smbc_file *fe;
2287 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2288 struct smbc_dir_list *list_ent = NULL;
2290 if (!smbc_initialized) {
2297 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
2304 fe = smbc_file_table[fd - smbc_start_fd];
2313 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2320 /* Now, check what we were passed and see if it is OK ... */
2322 if (dirent == NULL) { /* Seek to the begining of the list */
2324 fe->dir_next = fe->dir_list;
2329 /* Now, run down the list and make sure that the entry is OK */
2330 /* This may need to be changed if we change the format of the list */
2332 if ((list_ent = smbc_check_dir_ent(fe->dir_list, dirent)) == NULL) {
2334 errno = EINVAL; /* Bad entry */
2339 fe->dir_next = list_ent;
2346 * Routine to fstat a dir
2349 int smbc_fstatdir(int fd, struct stat *st)
2352 if (!smbc_initialized) {
2359 /* No code yet ... */
2366 * Routine to print a file on a remote server ...
2368 * We open the file, which we assume to be on a remote server, and then
2369 * copy it to a print file on the share specified by printq.
2372 int smbc_print_file(const char *fname, const char *printq)
2374 int fid1, fid2, bytes, saverr, tot_bytes = 0;
2377 if (!smbc_initialized) {
2384 if (!fname && !printq) {
2391 /* Try to open the file for reading ... */
2393 if ((fid1 = smbc_open(fname, O_RDONLY, 0666)) < 0) {
2395 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2396 return -1; /* smbc_open sets errno */
2400 /* Now, try to open the printer file for writing */
2402 if ((fid2 = smbc_open_print_job(printq)) < 0) {
2404 saverr = errno; /* Save errno */
2411 while ((bytes = smbc_read(fid1, buf, sizeof(buf))) > 0) {
2415 if ((smbc_write(fid2, buf, bytes)) < 0) {
2428 smbc_close(fid1); /* We have to close these anyway */
2443 * Open a print file to be written to by other calls
2446 int smbc_open_print_job(const char *fname)
2448 fstring server, share, user, password;
2451 if (!smbc_initialized) {
2465 DEBUG(4, ("smbc_open_print_job(%s)\n", fname));
2467 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2469 /* What if the path is empty, or the file exists? */
2471 return smbc_open(fname, O_WRONLY, 666);
2476 * Routine to list print jobs on a printer share ...
2479 int smbc_list_print_jobs(const char *fname, void (*fn)(struct print_job_info *))
2481 struct smbc_server *srv;
2482 fstring server, share, user, password, workgroup;
2485 if (!smbc_initialized) {
2499 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2501 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2503 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2505 pstrcpy(workgroup, lp_workgroup());
2507 srv = smbc_server(server, share, workgroup, user, password);
2511 return -1; /* errno set by smbc_server */
2515 if (cli_print_queue(&srv->cli, fn) < 0) {
2517 errno = smbc_errno(&srv->cli);
2527 * Delete a print job from a remote printer share
2530 int smbc_unlink_print_job(const char *fname, int id)
2532 struct smbc_server *srv;
2533 fstring server, share, user, password, workgroup;
2537 if (!smbc_initialized) {
2551 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2553 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2555 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2557 pstrcpy(workgroup, lp_workgroup());
2559 srv = smbc_server(server, share, workgroup, user, password);
2563 return -1; /* errno set by smbc_server */
2567 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2570 errno = smbc_errno(&srv->cli);
2571 else if (err == ERRnosuchprintjob)