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 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 */
98 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
101 /* clean_fname(s); causing problems ... */
103 /* see if it has the right prefix */
104 len = strlen(smbc_prefix);
105 if (strncmp(s,smbc_prefix,len) ||
106 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
110 /* Watch the test below, we are testing to see if we should exit */
112 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
118 p += 2; /* Skip the // or \\ */
125 strncpy(server, (char *)lp_workgroup(), 16); /* FIXME: Danger here */
131 * ok, its for us. Now parse out the server, share etc.
133 * However, we want to parse out [[domain;]user[:password]@] if it
137 /* check that '@' occurs before '/', if '/' exists at all */
138 q = strchr_m(p, '@');
139 r = strchr_m(p, '/');
140 if (q && (!r || q < r)) {
141 pstring username, passwd, domain;
144 next_token(&p, userinfo, "@", sizeof(fstring));
146 username[0] = passwd[0] = domain[0] = 0;
148 if (strchr_m(u, ';')) {
150 next_token(&u, domain, ";", sizeof(fstring));
154 if (strchr_m(u, ':')) {
156 next_token(&u, username, ":", sizeof(fstring));
163 pstrcpy(username, u);
168 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
171 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
175 if (!next_token(&p, server, "/", sizeof(fstring))) {
181 if (*p == (char)0) return 0; /* That's it ... */
183 if (!next_token(&p, share, "/", sizeof(fstring))) {
191 all_string_sub(path, "/", "\\", 0);
197 * Convert an SMB error into a UNIX error ...
200 int smbc_errno(struct cli_state *c)
204 if (cli_is_dos_error(c)) {
208 cli_dos_error(c, &eclass, &ecode);
209 ret = cli_errno_from_dos(eclass, ecode);
211 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
212 (int)eclass, (int)ecode, (int)ecode, ret));
216 status = cli_nt_error(c);
217 ret = cli_errno_from_nt(status);
219 DEBUG(3,("smbc errno %s -> %d\n",
220 get_nt_error_msg(status), ret));
227 * Connect to a server, possibly on an existing connection
229 * Here, what we want to do is: If the server and username
230 * match an existing connection, reuse that, otherwise, establish a
233 * If we have to create a new connection, call the auth_fn to get the
234 * info we need, unless the username and password were passed in.
237 struct smbc_server *smbc_server(char *server, char *share,
238 char *workgroup, char *username,
241 struct smbc_server *srv=NULL;
243 struct nmb_name called, calling;
244 char *p, *server_n = server;
252 /* try to use an existing connection */
253 for (srv=smbc_srvs;srv;srv=srv->next) {
254 if (strcmp(server,srv->server_name)==0 &&
255 strcmp(share,srv->share_name)==0 &&
256 strcmp(workgroup,srv->workgroup)==0 &&
257 strcmp(username, srv->username) == 0)
261 if (server[0] == 0) {
267 * Pick up the auth info here, once we know we need to connect
268 * But only if we do not have a username and password ...
271 if (!username[0] || !password[0])
272 smbc_auth_fn(server, share, workgroup, sizeof(fstring),
273 username, sizeof(fstring), password, sizeof(fstring));
276 * However, smbc_auth_fn may have picked up info relating to an
277 * existing connection, so try for an existing connection again ...
280 for (srv=smbc_srvs;srv;srv=srv->next) {
281 if (strcmp(server,srv->server_name)==0 &&
282 strcmp(share,srv->share_name)==0 &&
283 strcmp(workgroup,srv->workgroup)==0 &&
284 strcmp(username, srv->username) == 0)
288 make_nmb_name(&calling, my_netbios_name, 0x0);
289 make_nmb_name(&called , server, 0x20);
291 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
293 if ((p=strchr_m(server_n,'#')) &&
294 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
296 fstrcpy(group, server_n);
297 p = strchr_m(group,'#');
302 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
305 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
309 /* have to open a new connection */
310 if (!cli_initialise(&c) || !cli_connect(&c, server_n, &ip)) {
311 if (c.initialised) cli_shutdown(&c);
316 if (!cli_session_request(&c, &calling, &called)) {
318 if (strcmp(called.name, "*SMBSERVER")) {
319 make_nmb_name(&called , "*SMBSERVER", 0x20);
326 DEBUG(4,(" session request ok\n"));
328 if (!cli_negprot(&c)) {
334 if (!cli_session_setup(&c, username,
335 password, strlen(password),
336 password, strlen(password),
338 /* try an anonymous login if it failed */
339 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
345 DEBUG(4,(" session setup ok\n"));
347 if (!cli_send_tconX(&c, share, "?????",
348 password, strlen(password)+1)) {
349 errno = smbc_errno(&c);
354 DEBUG(4,(" tconx ok\n"));
356 srv = (struct smbc_server *)malloc(sizeof(*srv));
366 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
368 srv->server_name = strdup(server);
369 if (!srv->server_name) {
374 srv->share_name = strdup(share);
375 if (!srv->share_name) {
380 srv->workgroup = strdup(workgroup);
381 if (!srv->workgroup) {
386 srv->username = strdup(username);
387 if (!srv->username) {
392 DLIST_ADD(smbc_srvs, srv);
398 if (!srv) return NULL;
400 SAFE_FREE(srv->server_name);
401 SAFE_FREE(srv->share_name);
402 SAFE_FREE(srv->workgroup);
403 SAFE_FREE(srv->username);
409 *Remove a server from the list smbc_srvs if it's unused -- Tom (tom@ninja.nl)
413 BOOL smbc_remove_unused_server(struct smbc_server * s)
417 /* are we being fooled ? */
418 if (!s) return False;
420 /* close all open files/directories on this server */
421 for (p = 0; p < SMBC_MAX_FD; p++) {
422 if (smbc_file_table[p] &&
423 smbc_file_table[p]->srv == s) {
424 /* Still used .. DARN */
425 DEBUG(3, ("smbc_remove_usused_server: %x still used by %s (%d).\n", (int) s,
426 smbc_file_table[p]->fname, smbc_file_table[p]->smbc_fd));
431 cli_shutdown(&s->cli);
433 SAFE_FREE(s->username);
434 SAFE_FREE(s->workgroup);
435 SAFE_FREE(s->server_name);
436 SAFE_FREE(s->share_name);
437 DLIST_REMOVE(smbc_srvs, s);
438 DEBUG(3, ("smbc_remove_usused_server: %x removed.\n", (int) s));
444 *Initialise the library etc
446 * We accept valid values for debug from 0 to 100,
447 * and insist that fn must be non-null.
450 int smbc_init(smbc_get_auth_data_fn fn, int debug)
454 char *user = NULL, *home = NULL, *pname="libsmbclient";
456 if (!fn || debug < 0 || debug > 100) {
463 if (smbc_initialized) { /* Don't go through this if we have already done it */
469 smbc_initialized = 1;
471 /* smbc_debug = debug; */
475 setup_logging(pname, False);
477 /* Here we would open the smb.conf file if needed ... */
479 home = getenv("HOME");
481 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
483 load_interfaces(); /* Load the list of interfaces ... */
485 in_client = True; /* FIXME, make a param */
487 if (!lp_load(conf, True, False, False)) {
490 * Hmmm, what the hell do we do here ... we could not parse the
491 * config file ... We must return an error ... and keep info around
492 * about why we failed
495 errno = ENOENT; /* FIXME: Figure out the correct error response */
500 reopen_logs(); /* Get logging working ... */
503 * FIXME: Is this the best way to get the user info?
506 user = getenv("USER");
507 /* walk around as "guest" if no username can be found */
508 if (!user) user = strdup("guest");
509 pstrcpy(smbc_user, user); /* Save for use elsewhere */
512 * We try to get our netbios name from the config. If that fails we fall
513 * back on constructing our netbios name from our hostname etc
516 pstrcpy(my_netbios_name, global_myname);
520 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
523 slprintf(my_netbios_name, 16, "smbc%s%d", user, pid);
525 DEBUG(0,("Using netbios name %s.\n", my_netbios_name));
527 name_register_wins(my_netbios_name, 0);
530 * Now initialize the file descriptor array and figure out what the
531 * max open files is, so we can return FD's that are above the max
532 * open file, and separated by a guard band
535 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
539 if (getrlimit(RLIMIT_NOFILE, &rlp)) {
541 DEBUG(0, ("smbc_init: getrlimit(1) for RLIMIT_NOFILE failed with error %s\n", strerror(errno)));
543 smbc_start_fd = 1000000;
548 smbc_start_fd = rlp.rlim_max + 10000; /* Leave a guard space of 10,000 */
552 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
554 smbc_start_fd = 1000000;
558 smbc_file_table = malloc(SMBC_MAX_FD * sizeof(struct smbc_file *));
560 for (p = 0; p < SMBC_MAX_FD; p++)
561 smbc_file_table[p] = NULL;
563 if (!smbc_file_table)
566 return 0; /* Success */
571 * Routine to open() a file ...
574 int smbc_open(const char *fname, int flags, mode_t mode)
576 fstring server, share, user, password, workgroup;
578 struct smbc_server *srv = NULL;
581 if (!smbc_initialized) {
583 errno = EINVAL; /* Best I can think of ... */
595 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
597 if (user[0] == (char)0) pstrcpy(user, smbc_user);
599 pstrcpy(workgroup, lp_workgroup());
601 srv = smbc_server(server, share, workgroup, user, password);
605 if (errno == EPERM) errno = EACCES;
606 return -1; /* smbc_server sets errno */
610 /* Hmmm, the test for a directory is suspect here ... FIXME */
612 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
621 /* Find a free slot first */
623 while (smbc_file_table[slot])
626 if (slot > SMBC_MAX_FD) {
628 errno = ENOMEM; /* FIXME, is this best? */
633 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
635 if (!smbc_file_table[slot]) {
642 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
644 /* Handle the error ... */
646 SAFE_FREE(smbc_file_table[slot]);
647 errno = smbc_errno(&srv->cli);
652 /* Fill in file struct */
654 smbc_file_table[slot]->cli_fd = fd;
655 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
656 smbc_file_table[slot]->fname = strdup(fname);
657 smbc_file_table[slot]->srv = srv;
658 smbc_file_table[slot]->offset = 0;
659 smbc_file_table[slot]->file = True;
661 return smbc_file_table[slot]->smbc_fd;
665 /* Check if opendir needed ... */
670 eno = smbc_errno(&srv->cli);
671 fd = smbc_opendir(fname);
672 if (fd < 0) errno = eno;
677 return 1; /* Success, with fd ... */
682 * Routine to create a file
685 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
687 int smbc_creat(const char *path, mode_t mode)
690 if (!smbc_initialized) {
697 return smbc_open(path, creat_bits, mode);
701 * Routine to read() a file ...
704 ssize_t smbc_read(int fd, void *buf, size_t count)
706 struct smbc_file *fe;
709 if (!smbc_initialized) {
716 DEBUG(4, ("smbc_read(%d, %d)\n", fd, (int)count));
718 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
725 /* Check that the buffer exists ... */
734 fe = smbc_file_table[fd - smbc_start_fd];
736 if (!fe || !fe->file) {
743 ret = cli_read(&fe->srv->cli, fe->cli_fd, buf, fe->offset, count);
747 errno = smbc_errno(&fe->srv->cli);
754 DEBUG(4, (" --> %d\n", ret));
756 return ret; /* Success, ret bytes of data ... */
761 * Routine to write() a file ...
764 ssize_t smbc_write(int fd, void *buf, size_t count)
767 struct smbc_file *fe;
769 if (!smbc_initialized) {
776 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
783 /* Check that the buffer exists ... */
792 fe = smbc_file_table[fd - smbc_start_fd];
794 if (!fe || !fe->file) {
801 ret = cli_write(&fe->srv->cli, fe->cli_fd, 0, buf, fe->offset, count);
805 errno = smbc_errno(&fe->srv->cli);
812 return ret; /* Success, 0 bytes of data ... */
816 * Routine to close() a file ...
819 int smbc_close(int fd)
821 struct smbc_file *fe;
822 struct smbc_server *srv;
824 if (!smbc_initialized) {
831 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
838 fe = smbc_file_table[fd - smbc_start_fd];
849 return smbc_closedir(fd);
853 if (!cli_close(&fe->srv->cli, fe->cli_fd)) {
855 DEBUG(3, ("cli_close failed on %s (%d). purging server.\n",
856 fe->fname, fe->smbc_fd));
857 /* Deallocate slot and remove the server
858 * from the server cache if unused */
859 errno = smbc_errno(&fe->srv->cli);
861 SAFE_FREE(fe->fname);
863 smbc_file_table[fd - smbc_start_fd] = NULL;
864 smbc_remove_unused_server(srv);
870 SAFE_FREE(fe->fname);
872 smbc_file_table[fd - smbc_start_fd] = NULL;
878 * Routine to unlink() a file
881 int smbc_unlink(const char *fname)
883 fstring server, share, user, password, workgroup;
885 struct smbc_server *srv = NULL;
887 if (!smbc_initialized) {
889 errno = EINVAL; /* Best I can think of ... */
901 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
903 if (user[0] == (char)0) pstrcpy(user, smbc_user);
905 pstrcpy(workgroup, lp_workgroup());
907 srv = smbc_server(server, share, workgroup, user, password);
911 return -1; /* smbc_server sets errno */
915 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
917 int job = smbc_stat_printjob(srv, path, NULL, NULL);
923 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
931 if (!cli_unlink(&srv->cli, path)) {
933 errno = smbc_errno(&srv->cli);
935 if (errno == EACCES) { /* Check if the file is a directory */
940 time_t m_time = 0, a_time = 0, c_time = 0;
943 if (!smbc_getatr(srv, path, &mode, &size,
944 &c_time, &a_time, &m_time, &ino)) {
946 /* Hmmm, bad error ... What? */
948 errno = smbc_errno(&srv->cli);
954 if (IS_DOS_DIR(mode))
957 errno = saverr; /* Restore this */
966 return 0; /* Success ... */
971 * Routine to rename() a file
974 int smbc_rename(const char *oname, const char *nname)
976 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
977 pstring path1, path2;
978 struct smbc_server *srv = NULL;
980 if (!smbc_initialized) {
982 errno = EINVAL; /* Best I can think of ... */
987 if (!oname || !nname) {
994 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
996 smbc_parse_path(oname, server1, share1, path1, user1, password1);
998 if (user1[0] == (char)0) pstrcpy(user1, smbc_user);
1000 smbc_parse_path(nname, server2, share2, path2, user2, password2);
1002 if (user2[0] == (char)0) pstrcpy(user2, smbc_user);
1004 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1005 strcmp(user1, user2)) {
1007 /* Can't rename across file systems, or users?? */
1014 pstrcpy(workgroup, lp_workgroup());
1016 srv = smbc_server(server1, share1, workgroup, user1, password1);
1023 if (!cli_rename(&srv->cli, path1, path2)) {
1024 int eno = smbc_errno(&srv->cli);
1026 if (eno != EEXIST ||
1027 !cli_unlink(&srv->cli, path2) ||
1028 !cli_rename(&srv->cli, path1, path2)) {
1036 return 0; /* Success */
1041 * A routine to lseek() a file
1044 off_t smbc_lseek(int fd, off_t offset, int whence)
1046 struct smbc_file *fe;
1049 if (!smbc_initialized) {
1056 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1063 fe = smbc_file_table[fd - smbc_start_fd];
1075 return -1; /* Can't lseek a dir ... */
1081 fe->offset = offset;
1085 fe->offset += offset;
1089 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1090 NULL, NULL, NULL) &&
1091 !cli_getattrE(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1097 fe->offset = size + offset;
1111 * Generate an inode number from file name for those things that need it
1115 ino_t smbc_inode(const char *name)
1118 if (!*name) return 2; /* FIXME, why 2 ??? */
1119 return (ino_t)str_checksum(name);
1124 * Routine to put basic stat info into a stat structure ... Used by stat and
1129 int smbc_setup_stat(struct stat *st, char *fname, size_t size, int mode)
1134 if (IS_DOS_DIR(mode)) {
1135 st->st_mode = SMBC_DIR_MODE;
1137 st->st_mode = SMBC_FILE_MODE;
1140 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1141 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1142 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1143 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1146 st->st_blksize = 512;
1147 st->st_blocks = (size+511)/512;
1148 st->st_uid = getuid();
1149 st->st_gid = getgid();
1151 if (IS_DOS_DIR(mode)) {
1157 if (st->st_ino == 0) {
1158 st->st_ino = smbc_inode(fname);
1161 return True; /* FIXME: Is this needed ? */
1166 * Get info from an SMB server on a file. Use a qpathinfo call first
1167 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1170 BOOL smbc_getatr(struct smbc_server *srv, char *path,
1171 uint16 *mode, size_t *size,
1172 time_t *c_time, time_t *a_time, time_t *m_time,
1176 if (!smbc_initialized) {
1183 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1185 if (!srv->no_pathinfo2 &&
1186 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1187 size, mode, ino)) return True;
1189 /* if this is NT then don't bother with the getatr */
1190 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
1192 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1193 a_time = c_time = m_time;
1194 srv->no_pathinfo2 = True;
1201 * Routine to stat a file given a name
1204 int smbc_stat(const char *fname, struct stat *st)
1206 struct smbc_server *srv;
1207 fstring server, share, user, password, workgroup;
1209 time_t m_time = 0, a_time = 0, c_time = 0;
1214 if (!smbc_initialized) {
1216 errno = EINVAL; /* Best I can think of ... */
1228 DEBUG(4, ("smbc_stat(%s)\n", fname));
1230 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
1232 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1234 pstrcpy(workgroup, lp_workgroup());
1236 srv = smbc_server(server, share, workgroup, user, password);
1240 return -1; /* errno set by smbc_server */
1244 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1246 mode = aDIR | aRONLY;
1249 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1251 if (strcmp(path, "\\") == 0) {
1253 mode = aDIR | aRONLY;
1259 smbc_stat_printjob(srv, path, &size, &m_time);
1260 c_time = a_time = m_time;
1265 if (!smbc_getatr(srv, path, &mode, &size,
1266 &c_time, &a_time, &m_time, &ino)) {
1268 errno = smbc_errno(&srv->cli);
1277 smbc_setup_stat(st, path, size, mode);
1279 st->st_atime = a_time;
1280 st->st_ctime = c_time;
1281 st->st_mtime = m_time;
1282 st->st_dev = srv->dev;
1289 * Routine to stat a file given an fd
1292 int smbc_fstat(int fd, struct stat *st)
1294 struct smbc_file *fe;
1295 time_t c_time, a_time, m_time;
1300 if (!smbc_initialized) {
1307 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1314 fe = smbc_file_table[fd - smbc_start_fd];
1325 return smbc_fstatdir(fd, st);
1329 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd,
1330 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1331 !cli_getattrE(&fe->srv->cli, fe->cli_fd,
1332 &mode, &size, &c_time, &a_time, &m_time)) {
1341 smbc_setup_stat(st, fe->fname, size, mode);
1343 st->st_atime = a_time;
1344 st->st_ctime = c_time;
1345 st->st_mtime = m_time;
1346 st->st_dev = fe->srv->dev;
1353 * Routine to open a directory
1357 * smb: which should list all the workgroups available
1359 * smb:workgroup//server
1361 * smb://server/share
1362 * smb://<IP-addr> which should list shares on server
1363 * smb://<IP-addr>/share which should list files on share
1366 static void smbc_remove_dir(struct smbc_file *dir)
1368 struct smbc_dir_list *d,*f;
1375 SAFE_FREE(f->dirent);
1380 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1384 static int add_dirent(struct smbc_file *dir, const char *name, const char *comment, uint32 type)
1386 struct smbc_dirent *dirent;
1390 * Allocate space for the dirent, which must be increased by the
1391 * size of the name and the comment and 1 for the null on the comment.
1392 * The null on the name is already accounted for.
1395 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1396 (comment?strlen(comment):0) + 1;
1398 dirent = malloc(size);
1402 dir->dir_error = ENOMEM;
1407 if (dir->dir_list == NULL) {
1409 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1410 if (!dir->dir_list) {
1413 dir->dir_error = ENOMEM;
1418 dir->dir_end = dir->dir_next = dir->dir_list;
1423 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1425 if (!dir->dir_end) {
1428 dir->dir_error = ENOMEM;
1433 dir->dir_end = dir->dir_end->next;
1437 dir->dir_end->next = NULL;
1438 dir->dir_end->dirent = dirent;
1440 dirent->smbc_type = type;
1441 dirent->namelen = (name?strlen(name):0);
1442 dirent->commentlen = (comment?strlen(comment):0);
1443 dirent->dirlen = size;
1445 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1447 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1448 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1455 list_fn(const char *name, uint32 type, const char *comment, void *state)
1457 struct smbc_file *dir = (struct smbc_file *)state;
1460 /* We need to process the type a little ... */
1462 if (dir->dir_type == SMBC_FILE_SHARE) {
1465 case 0: /* Directory tree */
1466 dirent_type = SMBC_FILE_SHARE;
1470 dirent_type = SMBC_PRINTER_SHARE;
1474 dirent_type = SMBC_COMMS_SHARE;
1478 dirent_type = SMBC_IPC_SHARE;
1482 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1487 else dirent_type = dir->dir_type;
1489 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1491 /* An error occurred, what do we do? */
1492 /* FIXME: Add some code here */
1499 dir_list_fn(file_info *finfo, const char *mask, void *state)
1502 if (add_dirent((struct smbc_file *)state, finfo->name, "",
1503 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1505 /* Handle an error ... */
1506 /* FIXME: Add some code ... */
1512 int smbc_opendir(const char *fname)
1514 fstring server, share, user, password, workgroup;
1516 struct smbc_server *srv = NULL;
1517 struct in_addr rem_ip;
1520 if (!smbc_initialized) {
1534 if (smbc_parse_path(fname, server, share, path, user, password)) {
1541 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1543 pstrcpy(workgroup, lp_workgroup());
1545 /* Get a file entry ... */
1549 while (smbc_file_table[slot])
1552 if (slot > SMBC_MAX_FD) {
1555 return -1; /* FIXME, ... move into a func */
1559 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
1561 if (!smbc_file_table[slot]) {
1568 smbc_file_table[slot]->cli_fd = 0;
1569 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
1570 smbc_file_table[slot]->fname = strdup(fname);
1571 smbc_file_table[slot]->srv = NULL;
1572 smbc_file_table[slot]->offset = 0;
1573 smbc_file_table[slot]->file = False;
1574 smbc_file_table[slot]->dir_list =
1575 smbc_file_table[slot]->dir_next =
1576 smbc_file_table[slot]->dir_end = NULL;
1578 if (server[0] == (char)0) {
1580 if (share[0] != (char)0 || path[0] != (char)0) {
1583 if (smbc_file_table[slot]) {
1584 SAFE_FREE(smbc_file_table[slot]->fname);
1585 SAFE_FREE(smbc_file_table[slot]);
1591 /* We have server and share and path empty ... so list the workgroups */
1593 if (!resolve_name(lp_workgroup(), &rem_ip, 0x1d)) {
1595 errno = EINVAL; /* Something wrong with smb.conf? */
1600 smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;
1602 /* find the name of the server ... */
1604 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1606 DEBUG(0, ("Could not get the name of local master browser for server %s\n", server));
1613 * Get a connection to IPC$ on the server if we do not already have one
1616 srv = smbc_server(server, "IPC$", workgroup, user, password);
1620 if (smbc_file_table[slot]) {
1621 SAFE_FREE(smbc_file_table[slot]->fname);
1622 SAFE_FREE(smbc_file_table[slot]);
1628 smbc_file_table[slot]->srv = srv;
1630 /* Now, list the stuff ... */
1632 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1633 (void *)smbc_file_table[slot])) {
1635 if (smbc_file_table[slot]) {
1636 SAFE_FREE(smbc_file_table[slot]->fname);
1637 SAFE_FREE(smbc_file_table[slot]);
1639 errno = cli_errno(&srv->cli);
1644 else { /* Server not an empty string ... Check the rest and see what gives */
1646 if (share[0] == (char)0) {
1648 if (path[0] != (char)0) { /* Should not have empty share with path */
1651 if (smbc_file_table[slot]) {
1652 SAFE_FREE(smbc_file_table[slot]->fname);
1653 SAFE_FREE(smbc_file_table[slot]);
1659 /* Check to see if <server><1D> translates, or <server><20> translates */
1660 /* However, we check to see if <server> is an IP address first */
1662 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1663 resolve_name(server, &rem_ip, 0x1d)) { /* Found LMB */
1666 smbc_file_table[slot]->dir_type = SMBC_SERVER;
1669 * Get the backup list ...
1673 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1675 DEBUG(0, ("Could not get name of local master browser %s\n", server));
1676 errno = EPERM; /* FIXME, is this correct */
1682 * Get a connection to IPC$ on the server if we do not already have one
1685 srv = smbc_server(buserver, "IPC$", workgroup, user, password);
1689 if (smbc_file_table[slot]) {
1690 SAFE_FREE(smbc_file_table[slot]->fname);
1691 SAFE_FREE(smbc_file_table[slot]);
1697 smbc_file_table[slot]->srv = srv;
1699 /* Now, list the servers ... */
1701 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1702 (void *)smbc_file_table[slot])) {
1704 if (smbc_file_table[slot]) {
1705 SAFE_FREE(smbc_file_table[slot]->fname);
1706 SAFE_FREE(smbc_file_table[slot]);
1708 errno = cli_errno(&srv->cli);
1716 if (resolve_name(server, &rem_ip, 0x20)) {
1718 /* Now, list the shares ... */
1720 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1722 srv = smbc_server(server, "IPC$", workgroup, user, password);
1726 if (smbc_file_table[slot]) {
1727 SAFE_FREE(smbc_file_table[slot]->fname);
1728 SAFE_FREE(smbc_file_table[slot]);
1734 smbc_file_table[slot]->srv = srv;
1736 /* Now, list the servers ... */
1738 if (cli_RNetShareEnum(&srv->cli, list_fn,
1739 (void *)smbc_file_table[slot]) < 0) {
1741 errno = cli_errno(&srv->cli);
1742 if (smbc_file_table[slot]) {
1743 SAFE_FREE(smbc_file_table[slot]->fname);
1744 SAFE_FREE(smbc_file_table[slot]);
1753 errno = ENODEV; /* Neither the workgroup nor server exists */
1754 if (smbc_file_table[slot]) {
1755 SAFE_FREE(smbc_file_table[slot]->fname);
1756 SAFE_FREE(smbc_file_table[slot]);
1765 else { /* The server and share are specified ... work from there ... */
1767 /* Well, we connect to the server and list the directory */
1769 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1771 srv = smbc_server(server, share, workgroup, user, password);
1775 if (smbc_file_table[slot]) {
1776 SAFE_FREE(smbc_file_table[slot]->fname);
1777 SAFE_FREE(smbc_file_table[slot]);
1783 smbc_file_table[slot]->srv = srv;
1785 /* Now, list the files ... */
1787 pstrcat(path, "\\*");
1789 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1790 (void *)smbc_file_table[slot]) < 0) {
1792 if (smbc_file_table[slot]) {
1793 SAFE_FREE(smbc_file_table[slot]->fname);
1794 SAFE_FREE(smbc_file_table[slot]);
1796 errno = smbc_errno(&srv->cli);
1804 return smbc_file_table[slot]->smbc_fd;
1809 * Routine to close a directory
1812 int smbc_closedir(int fd)
1814 struct smbc_file *fe;
1816 if (!smbc_initialized) {
1823 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1830 fe = smbc_file_table[fd - smbc_start_fd];
1839 smbc_remove_dir(fe); /* Clean it up */
1843 SAFE_FREE(fe->fname);
1844 SAFE_FREE(fe); /* Free the space too */
1848 smbc_file_table[fd - smbc_start_fd] = NULL;
1855 * Routine to get a directory entry
1858 static char smbc_local_dirent[512]; /* Make big enough */
1860 struct smbc_dirent *smbc_readdir(unsigned int fd)
1862 struct smbc_file *fe;
1863 struct smbc_dirent *dirp, *dirent;
1865 /* Check that all is ok first ... */
1867 if (!smbc_initialized) {
1874 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1881 fe = smbc_file_table[fd - smbc_start_fd];
1890 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1901 dirent = fe->dir_next->dirent;
1910 /* Hmmm, do I even need to copy it? */
1912 bcopy(dirent, smbc_local_dirent, dirent->dirlen); /* Copy the dirent */
1914 dirp = (struct smbc_dirent *)smbc_local_dirent;
1916 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1918 fe->dir_next = fe->dir_next->next;
1920 return (struct smbc_dirent *)smbc_local_dirent;
1926 * Routine to get directory entries
1929 int smbc_getdents(unsigned int fd, struct smbc_dirent *dirp, int count)
1931 struct smbc_file *fe;
1932 struct smbc_dir_list *dir;
1933 int rem = count, reqd;
1934 char *ndir = (char *)dirp;
1936 /* Check that all is ok first ... */
1938 if (!smbc_initialized) {
1945 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
1952 fe = smbc_file_table[fd - smbc_start_fd];
1961 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1969 * Now, retrieve the number of entries that will fit in what was passed
1970 * We have to figure out if the info is in the list, or we need to
1971 * send a request to the server to get the info.
1974 while ((dir = fe->dir_next)) {
1975 struct smbc_dirent *dirent;
1979 errno = ENOENT; /* Bad error */
1984 if (rem < (reqd = (sizeof(struct smbc_dirent) + dir->dirent->namelen +
1985 dir->dirent->commentlen + 1))) {
1987 if (rem < count) { /* We managed to copy something */
1993 else { /* Nothing copied ... */
1995 errno = EINVAL; /* Not enough space ... */
2002 dirent = dir->dirent;
2004 bcopy(dirent, ndir, reqd); /* Copy the data in ... */
2006 ((struct smbc_dirent *)ndir)->comment =
2007 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
2013 fe->dir_next = dir = dir -> next;
2024 * Routine to create a directory ...
2027 int smbc_mkdir(const char *fname, mode_t mode)
2029 struct smbc_server *srv;
2030 fstring server, share, user, password, workgroup;
2033 if (!smbc_initialized) {
2047 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
2049 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2051 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2053 pstrcpy(workgroup, lp_workgroup());
2055 srv = smbc_server(server, share, workgroup, user, password);
2059 return -1; /* errno set by smbc_server */
2063 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2065 mode = aDIR | aRONLY;
2068 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2070 if (strcmp(path, "\\") == 0) {
2072 mode = aDIR | aRONLY;
2078 smbc_stat_printjob(srv, path, &size, &m_time);
2079 c_time = a_time = m_time;
2084 if (!cli_mkdir(&srv->cli, path)) {
2086 errno = smbc_errno(&srv->cli);
2096 * Our list function simply checks to see if a directory is not empty
2099 static int smbc_rmdir_dirempty = True;
2101 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
2104 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2105 smbc_rmdir_dirempty = False;
2110 * Routine to remove a directory
2113 int smbc_rmdir(const char *fname)
2115 struct smbc_server *srv;
2116 fstring server, share, user, password, workgroup;
2119 if (!smbc_initialized) {
2133 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2135 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2137 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2139 pstrcpy(workgroup, lp_workgroup());
2141 srv = smbc_server(server, share, workgroup, user, password);
2145 return -1; /* errno set by smbc_server */
2149 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2151 mode = aDIR | aRONLY;
2154 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2156 if (strcmp(path, "\\") == 0) {
2158 mode = aDIR | aRONLY;
2164 smbc_stat_printjob(srv, path, &size, &m_time);
2165 c_time = a_time = m_time;
2170 if (!cli_rmdir(&srv->cli, path)) {
2172 errno = smbc_errno(&srv->cli);
2174 if (errno == EACCES) { /* Check if the dir empty or not */
2176 pstring lpath; /* Local storage to avoid buffer overflows */
2178 smbc_rmdir_dirempty = True; /* Make this so ... */
2180 pstrcpy(lpath, path);
2181 pstrcat(lpath, "\\*");
2183 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2186 /* Fix errno to ignore latest error ... */
2188 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2189 smbc_errno(&srv->cli)));
2194 if (smbc_rmdir_dirempty)
2210 * Routine to return the current directory position
2213 off_t smbc_telldir(int fd)
2215 struct smbc_file *fe;
2217 if (!smbc_initialized) {
2224 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
2231 fe = smbc_file_table[fd - smbc_start_fd];
2240 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2247 return (off_t) fe->dir_next;
2252 * A routine to run down the list and see if the entry is OK
2255 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2256 struct smbc_dirent *dirent)
2259 /* Run down the list looking for what we want */
2263 struct smbc_dir_list *tmp = list;
2267 if (tmp->dirent == dirent)
2276 return NULL; /* Not found, or an error */
2282 * Routine to seek on a directory
2285 int smbc_lseekdir(int fd, off_t offset)
2287 struct smbc_file *fe;
2288 struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2289 struct smbc_dir_list *list_ent = NULL;
2291 if (!smbc_initialized) {
2298 if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
2305 fe = smbc_file_table[fd - smbc_start_fd];
2314 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2321 /* Now, check what we were passed and see if it is OK ... */
2323 if (dirent == NULL) { /* Seek to the begining of the list */
2325 fe->dir_next = fe->dir_list;
2330 /* Now, run down the list and make sure that the entry is OK */
2331 /* This may need to be changed if we change the format of the list */
2333 if ((list_ent = smbc_check_dir_ent(fe->dir_list, dirent)) == NULL) {
2335 errno = EINVAL; /* Bad entry */
2340 fe->dir_next = list_ent;
2347 * Routine to fstat a dir
2350 int smbc_fstatdir(int fd, struct stat *st)
2353 if (!smbc_initialized) {
2360 /* No code yet ... */
2367 * Routine to print a file on a remote server ...
2369 * We open the file, which we assume to be on a remote server, and then
2370 * copy it to a print file on the share specified by printq.
2373 int smbc_print_file(const char *fname, const char *printq)
2375 int fid1, fid2, bytes, saverr, tot_bytes = 0;
2378 if (!smbc_initialized) {
2385 if (!fname && !printq) {
2392 /* Try to open the file for reading ... */
2394 if ((fid1 = smbc_open(fname, O_RDONLY, 0666)) < 0) {
2396 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2397 return -1; /* smbc_open sets errno */
2401 /* Now, try to open the printer file for writing */
2403 if ((fid2 = smbc_open_print_job(printq)) < 0) {
2405 saverr = errno; /* Save errno */
2412 while ((bytes = smbc_read(fid1, buf, sizeof(buf))) > 0) {
2416 if ((smbc_write(fid2, buf, bytes)) < 0) {
2429 smbc_close(fid1); /* We have to close these anyway */
2444 * Open a print file to be written to by other calls
2447 int smbc_open_print_job(const char *fname)
2449 fstring server, share, user, password;
2452 if (!smbc_initialized) {
2466 DEBUG(4, ("smbc_open_print_job(%s)\n", fname));
2468 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2470 /* What if the path is empty, or the file exists? */
2472 return smbc_open(fname, O_WRONLY, 666);
2477 * Routine to list print jobs on a printer share ...
2480 int smbc_list_print_jobs(const char *fname, void (*fn)(struct print_job_info *))
2482 struct smbc_server *srv;
2483 fstring server, share, user, password, workgroup;
2486 if (!smbc_initialized) {
2500 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2502 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2504 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2506 pstrcpy(workgroup, lp_workgroup());
2508 srv = smbc_server(server, share, workgroup, user, password);
2512 return -1; /* errno set by smbc_server */
2516 if (cli_print_queue(&srv->cli, fn) < 0) {
2518 errno = smbc_errno(&srv->cli);
2528 * Delete a print job from a remote printer share
2531 int smbc_unlink_print_job(const char *fname, int id)
2533 struct smbc_server *srv;
2534 fstring server, share, user, password, workgroup;
2538 if (!smbc_initialized) {
2552 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2554 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2556 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2558 pstrcpy(workgroup, lp_workgroup());
2560 srv = smbc_server(server, share, workgroup, user, password);
2564 return -1; /* errno set by smbc_server */
2568 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2571 errno = smbc_errno(&srv->cli);
2572 else if (err == ERRnosuchprintjob)