/*
Unix SMB/Netbios implementation.
- Version 2.0
SMB client library implementation
Copyright (C) Andrew Tridgell 1998
- Copyright (C) Richard Sharpe 2000
+ Copyright (C) Richard Sharpe 2000, 2002
Copyright (C) John Terpstra 2000
+ Copyright (C) Tom Jansen (Ninja ISD) 2002
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
*/
#include "includes.h"
-#include "libsmbclient.h"
-/* Structure for servers ... Held here so we don't need an include ...
- * May be better to put in an include file
+#include "../include/libsmb_internal.h"
+
+/*
+ * Functions exported by libsmb_cache.c that we need here
*/
+int smbc_default_cache_functions(SMBCCTX *context);
-struct smbc_server {
- struct smbc_server *next, *prev;
- struct cli_state cli;
- dev_t dev;
- char *server_name;
- char *share_name;
- char *workgroup;
- char *username;
- BOOL no_pathinfo2;
-};
-
-/* Keep directory entries in a list */
-struct smbc_dir_list {
- struct smbc_dir_list *next;
- struct smbc_dirent *dirent;
-};
-
-struct smbc_file {
- int cli_fd;
- int smbc_fd;
- char *fname;
- off_t offset;
- struct smbc_server *srv;
- BOOL file;
- struct smbc_dir_list *dir_list, *dir_end, *dir_next;
- int dir_type, dir_error;
-};
+/*
+ * check if an element is part of the list.
+ * FIXME: Does not belong here !
+ * Can anyone put this in a macro in dlinklist.h ?
+ * -- Tom
+ */
+static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
+ if (!p || !list) return False;
+ do {
+ if (p == list) return True;
+ list = list->next;
+ } while (list);
+ return False;
+}
extern BOOL in_client;
-static int smbc_initialized = 0;
-static smbc_get_auth_data_fn smbc_auth_fn = NULL;
-static int smbc_debug;
-static int smbc_start_fd;
-static int smbc_max_fd = 10000;
-static struct smbc_file **smbc_file_table;
-static struct smbc_server *smbc_srvs;
-static pstring my_netbios_name;
-static pstring smbc_user;
/*
- * Clean up a filename by removing redundent stuff
+ * Is the logging working / configfile read ?
*/
+static int smbc_initialized = 0;
-static void
-smbc_clean_fname(char *name)
+static int
+hex2int( unsigned int _char )
{
- char *p, *p2;
- int l;
- int modified = 1;
+ if ( _char >= 'A' && _char <='F')
+ return _char - 'A' + 10;
+ if ( _char >= 'a' && _char <='f')
+ return _char - 'a' + 10;
+ if ( _char >= '0' && _char <='9')
+ return _char - '0';
+ return -1;
+}
- if (!name) return;
-
- while (modified) {
- modified = 0;
+static void
+decode_urlpart(char *segment, size_t sizeof_segment)
+{
+ int old_length = strlen(segment);
+ int new_length = 0;
+ int new_length2 = 0;
+ int i = 0;
+ pstring new_segment;
+ char *new_usegment = 0;
+
+ if ( !old_length ) {
+ return;
+ }
- DEBUG(5,("cleaning %s\n", name));
+ /* make a copy of the old one */
+ new_usegment = (char*)malloc( old_length * 3 + 1 );
+
+ while( i < old_length ) {
+ int bReencode = False;
+ unsigned char character = segment[ i++ ];
+ if ((character <= ' ') || (character > 127))
+ bReencode = True;
+
+ new_usegment [ new_length2++ ] = character;
+ if (character == '%' ) {
+ int a = i+1 < old_length ? hex2int( segment[i] ) : -1;
+ int b = i+1 < old_length ? hex2int( segment[i+1] ) : -1;
+ if ((a == -1) || (b == -1)) { /* Only replace if sequence is valid */
+ /* Contains stray %, make sure to re-encode! */
+ bReencode = True;
+ } else {
+ /* Valid %xx sequence */
+ character = a * 16 + b; /* Replace with value of %dd */
+ if (!character)
+ break; /* Stop at %00 */
+
+ new_usegment [ new_length2++ ] = (unsigned char) segment[i++];
+ new_usegment [ new_length2++ ] = (unsigned char) segment[i++];
+ }
+ }
+ if (bReencode) {
+ unsigned int c = character / 16;
+ new_length2--;
+ new_usegment [ new_length2++ ] = '%';
- if ((p=strstr(name,"/./"))) {
- modified = 1;
- while (*p) {
- p[0] = p[2];
- p++;
- }
- }
+ c += (c > 9) ? ('A' - 10) : '0';
+ new_usegment[ new_length2++ ] = c;
- if ((p=strstr(name,"//"))) {
- modified = 1;
- while (*p) {
- p[0] = p[1];
- p++;
- }
- }
+ c = character % 16;
+ c += (c > 9) ? ('A' - 10) : '0';
+ new_usegment[ new_length2++ ] = c;
+ }
- if (strcmp(name,"/../")==0) {
- modified = 1;
- name[1] = 0;
- }
-
- if ((p=strstr(name,"/../"))) {
- modified = 1;
- for (p2=(p>name?p-1:p);p2>name;p2--) {
- if (p2[0] == '/') break;
- }
- while (*p2) {
- p2[0] = p2[3];
- p2++;
- }
+ new_segment [ new_length++ ] = character;
}
+ new_segment [ new_length ] = 0;
- if (strcmp(name,"/..")==0) {
- modified = 1;
- name[1] = 0;
- }
+ free(new_usegment);
- l = strlen(name);
- p = l>=3?(name+l-3):name;
- if (strcmp(p,"/..")==0) {
- modified = 1;
- for (p2=p-1;p2>name;p2--) {
- if (p2[0] == '/') break;
- }
- if (p2==name) {
- p[0] = '/';
- p[1] = 0;
- } else {
- p2[0] = 0;
- }
- }
-
- l = strlen(name);
- p = l>=2?(name+l-2):name;
- if (strcmp(p,"/.")==0) {
- if (p == name) {
- p[1] = 0;
- } else {
- p[0] = 0;
- }
- }
-
- if (strncmp(p=name,"./",2) == 0) {
- modified = 1;
- do {
- p[0] = p[2];
- } while (*p++);
- }
-
- l = strlen(p=name);
- if (l > 1 && p[l-1] == '/') {
- modified = 1;
- p[l-1] = 0;
- }
- }
+ /* realloc it with unix charset */
+ pull_utf8_allocate((void**)&new_usegment, new_segment);
+
+ /* this assumes (very safely) that removing %aa sequences
+ only shortens the string */
+ strncpy(segment, new_usegment, sizeof_segment);
+
+ free(new_usegment);
}
/*
* We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
*
* smb:// means show all the workgroups
- * smb://name/ means, if name<1D> exists, list servers in workgroup,
+ * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
* else, if name<20> exists, list all shares for server ...
*/
static const char *smbc_prefix = "smb:";
static int
-smbc_parse_path(const char *fname, char *server, char *share, char *path,
+smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
char *user, char *password) /* FIXME, lengths of strings */
{
- static pstring s;
- pstring userinfo;
- char *p;
- int len;
- fstring workgroup;
-
- server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
- pstrcpy(s, fname);
+ static pstring s;
+ pstring userinfo;
+ const char *p;
+ char *q, *r;
+ int len;
- /* clean_fname(s); causing problems ... */
+ server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
+ pstrcpy(s, fname);
- /* see if it has the right prefix */
- len = strlen(smbc_prefix);
- if (strncmp(s,smbc_prefix,len) ||
- (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
+ /* clean_fname(s); causing problems ... */
- p = s + len;
+ /* see if it has the right prefix */
+ len = strlen(smbc_prefix);
+ if (strncmp(s,smbc_prefix,len) ||
+ (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
- /* Watch the test below, we are testing to see if we should exit */
+ p = s + len;
- if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
+ /* Watch the test below, we are testing to see if we should exit */
- return -1;
+ if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
- }
+ return -1;
- p += 2; /* Skip the // or \\ */
+ }
- if (*p == (char)0)
- return 0;
+ p += 2; /* Skip the // or \\ */
- if (*p == '/') {
+ if (*p == (char)0)
+ goto decoding;
- strncpy(server, lp_workgroup(), 16); /* FIXME: Danger here */
- return 0;
+ if (*p == '/') {
- }
+ strncpy(server, context->workgroup,
+ (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
+ return 0;
+
+ }
- /*
- * ok, its for us. Now parse out the server, share etc.
- *
- * However, we want to parse out [[domain;]user[:password]@] if it
- * exists ...
- */
+ /*
+ * ok, its for us. Now parse out the server, share etc.
+ *
+ * However, we want to parse out [[domain;]user[:password]@] if it
+ * exists ...
+ */
- if (strchr(p, '@')) {
- pstring username, passwd, domain;
- char *u = userinfo;
+ /* check that '@' occurs before '/', if '/' exists at all */
+ q = strchr_m(p, '@');
+ r = strchr_m(p, '/');
+ if (q && (!r || q < r)) {
+ pstring username, passwd, domain;
+ const char *u = userinfo;
- next_token(&p, userinfo, "@", sizeof(fstring));
+ next_token(&p, userinfo, "@", sizeof(fstring));
- username[0] = passwd[0] = domain[0] = 0;
+ username[0] = passwd[0] = domain[0] = 0;
- if (strchr(u, ';')) {
+ if (strchr_m(u, ';')) {
- next_token(&u, domain, ";", sizeof(fstring));
+ next_token(&u, domain, ";", sizeof(fstring));
- }
+ }
- if (strchr(u, ':')) {
+ if (strchr_m(u, ':')) {
- next_token(&u, username, ":", sizeof(fstring));
+ next_token(&u, username, ":", sizeof(fstring));
- pstrcpy(passwd, u);
+ pstrcpy(passwd, u);
- }
- else {
+ }
+ else {
- pstrcpy(username, u);
+ pstrcpy(username, u);
- }
+ }
- if (username[0])
- strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
+ if (username[0])
+ strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
- if (passwd[0])
- strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
+ if (passwd[0])
+ strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
- }
+ }
- if (!next_token(&p, server, "/", sizeof(fstring))) {
+ if (!next_token(&p, server, "/", sizeof(fstring))) {
- return -1;
+ return -1;
- }
+ }
- if (*p == (char)0) return 0; /* That's it ... */
+ if (*p == (char)0) goto decoding; /* That's it ... */
- if (!next_token(&p, share, "/", sizeof(fstring))) {
+ if (!next_token(&p, share, "/", sizeof(fstring))) {
- return -1;
+ return -1;
+
+ }
- }
+ pstrcpy(path, p);
- pstrcpy(path, p);
-
- all_string_sub(path, "/", "\\", 0);
+ all_string_sub(path, "/", "\\", 0);
- return 0;
+ decoding:
+ decode_urlpart(path, sizeof(pstring));
+ decode_urlpart(server, sizeof(fstring));
+ decode_urlpart(share, sizeof(fstring));
+ decode_urlpart(user, sizeof(fstring));
+ decode_urlpart(password, sizeof(fstring));
+
+ return 0;
}
/*
* Convert an SMB error into a UNIX error ...
*/
-int smbc_errno(struct cli_state *c)
+static int smbc_errno(SMBCCTX *context, struct cli_state *c)
{
- uint8 eclass;
- uint32 ecode;
- int ret;
+ int ret = cli_errno(c);
+
+ if (cli_is_dos_error(c)) {
+ uint8 eclass;
+ uint32 ecode;
- ret = cli_error(c, &eclass, &ecode, NULL);
+ cli_dos_error(c, &eclass, &ecode);
+
+ DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
+ (int)eclass, (int)ecode, (int)ecode, ret));
+ } else {
+ NTSTATUS status;
+
+ status = cli_nt_error(c);
+
+ DEBUG(3,("smbc errno %s -> %d\n",
+ nt_errstr(status), ret));
+ }
- if (ret) {
- DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
- (int)eclass, (int)ecode, (int)ecode, ret));
- }
return ret;
}
-/*
- * Connect to a server, possibly on an existing connection
- *
- * Here, what we want to do is: If the server and username
- * match an existing connection, reuse that, otherwise, establish a
- * new connection.
- *
- * If we have to create a new connection, call the auth_fn to get the
- * info we need, unless the username and password were passed in.
+/*
+ * Check a server_fd.
+ * returns 0 if the server is in shape. Returns 1 on error
+ *
+ * Also useable outside libsmbclient to enable external cache
+ * to do some checks too.
*/
-
-struct smbc_server *smbc_server(char *server, char *share,
- char *workgroup, char *username,
- char *password)
+int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
{
- struct smbc_server *srv=NULL;
- struct cli_state c;
- struct nmb_name called, calling;
- char *p, *server_n = server;
- fstring group;
- pstring ipenv;
- struct in_addr ip;
- extern struct in_addr ipzero;
-
- ip = ipzero;
- ZERO_STRUCT(c);
-
- /* try to use an existing connection */
- for (srv=smbc_srvs;srv;srv=srv->next) {
- if (strcmp(server,srv->server_name)==0 &&
- strcmp(share,srv->share_name)==0 &&
- strcmp(workgroup,srv->workgroup)==0 &&
- strcmp(username, srv->username) == 0)
- return srv;
- }
-
- if (server[0] == 0) {
- errno = EPERM;
- return NULL;
- }
-
- /*
- * Pick up the auth info here, once we know we need to connect
- * But only if we do not have a username and password ...
- */
-
- if (!username[0] || !password[0])
- smbc_auth_fn(server, share, workgroup, sizeof(fstring),
- username, sizeof(fstring), password, sizeof(fstring));
-
- /*
- * However, smbc_auth_fn may have picked up info relating to an
- * existing connection, so try for an existing connection again ...
- */
-
- for (srv=smbc_srvs;srv;srv=srv->next) {
- if (strcmp(server,srv->server_name)==0 &&
- strcmp(share,srv->share_name)==0 &&
- strcmp(workgroup,srv->workgroup)==0 &&
- strcmp(username, srv->username) == 0)
- return srv;
- }
-
- make_nmb_name(&calling, my_netbios_name, 0x0);
- make_nmb_name(&called , server, 0x20);
-
- DEBUG(4,("server_n=[%s] server=[%s]\n", server_n, server));
-
- if ((p=strchr(server_n,'#')) &&
- (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
- struct in_addr sip;
- pstring s;
-
- fstrcpy(group, server_n);
- p = strchr(group,'#');
- *p = 0;
-
- }
-
- DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
-
- again:
- slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
-
- ip = ipzero;
-
- /* have to open a new connection */
- if (!cli_initialise(&c) || !cli_connect(&c, server_n, &ip)) {
- errno = ENOENT;
- return NULL;
- }
-
- if (!cli_session_request(&c, &calling, &called)) {
- cli_shutdown(&c);
- if (strcmp(called.name, "*SMBSERVER")) {
- make_nmb_name(&called , "*SMBSERVER", 0x20);
- goto again;
- }
- errno = ENOENT;
- return NULL;
- }
-
- DEBUG(4,(" session request ok\n"));
-
- if (!cli_negprot(&c)) {
- cli_shutdown(&c);
- errno = ENOENT;
- return NULL;
- }
-
- if (!cli_session_setup(&c, username,
- password, strlen(password),
- password, strlen(password),
- workgroup) &&
- /* try an anonymous login if it failed */
- !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
- cli_shutdown(&c);
- errno = EPERM;
- return NULL;
- }
-
- DEBUG(4,(" session setup ok\n"));
-
- if (!cli_send_tconX(&c, share, "?????",
- password, strlen(password)+1)) {
- errno = smbc_errno(&c);
- cli_shutdown(&c);
- return NULL;
- }
-
- DEBUG(4,(" tconx ok\n"));
-
- srv = (struct smbc_server *)malloc(sizeof(*srv));
- if (!srv) {
- errno = ENOMEM;
- goto failed;
- }
-
- ZERO_STRUCTP(srv);
+ if ( send_keepalive(server->cli.fd) == False )
+ return 1;
- srv->cli = c;
-
- srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
+ /* connection is ok */
+ return 0;
+}
- srv->server_name = strdup(server);
- if (!srv->server_name) {
- errno = ENOMEM;
- goto failed;
- }
+/*
+ * Remove a server from the cached server list it's unused.
+ * On success, 0 is returned. 1 is returned if the server could not be removed.
+ *
+ * Also useable outside libsmbclient
+ */
+int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
+{
+ SMBCFILE * file;
- srv->share_name = strdup(share);
- if (!srv->share_name) {
- errno = ENOMEM;
- goto failed;
- }
+ /* are we being fooled ? */
+ if (!context || !context->internal ||
+ !context->internal->_initialized || !srv) return 1;
- srv->workgroup = strdup(workgroup);
- if (!srv->workgroup) {
- errno = ENOMEM;
- goto failed;
- }
+
+ /* Check all open files/directories for a relation with this server */
+ for (file = context->internal->_files; file; file=file->next) {
+ if (file->srv == srv) {
+ /* Still used */
+ DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
+ srv, file));
+ return 1;
+ }
+ }
- srv->username = strdup(username);
- if (!srv->username) {
- errno = ENOMEM;
- goto failed;
- }
+ DLIST_REMOVE(context->internal->_servers, srv);
- DLIST_ADD(smbc_srvs, srv);
+ cli_shutdown(&srv->cli);
- return srv;
+ DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
- failed:
- cli_shutdown(&c);
- if (!srv) return NULL;
-
- if (srv->server_name) free(srv->server_name);
- if (srv->share_name) free(srv->share_name);
- free(srv);
- return NULL;
+ context->callbacks.remove_cached_srv_fn(context, srv);
+
+ SAFE_FREE(srv);
+
+ return 0;
}
/*
- *Initialise the library etc
+ * Connect to a server, possibly on an existing connection
+ *
+ * Here, what we want to do is: If the server and username
+ * match an existing connection, reuse that, otherwise, establish a
+ * new connection.
*
- * We accept valiv values for debug from 0 to 100,
- * and insist that fn must be non-null.
+ * If we have to create a new connection, call the auth_fn to get the
+ * info we need, unless the username and password were passed in.
*/
-int smbc_init(smbc_get_auth_data_fn fn, const char *wgroup, int debug)
+SMBCSRV *smbc_server(SMBCCTX *context,
+ const char *server, const char *share,
+ fstring workgroup, fstring username,
+ fstring password)
{
- static pstring workgroup;
- pstring conf;
- int p, pid;
- char *user = NULL, *host = NULL, *home = NULL, *pname="libsmbclient";
-
- /*
- * Next lot ifdef'd out until test suite fixed ...
- */
-#ifdef 0
- if (!fn || debug < 0 || debug > 100) {
-
- errno = EINVAL;
- return -1;
-
- }
-#endif
+ SMBCSRV *srv=NULL;
+ int auth_called = 0;
+ struct cli_state c;
+ struct nmb_name called, calling;
+ char *p;
+ const char *server_n = server;
+ fstring group;
+ pstring ipenv;
+ struct in_addr ip;
+ int tried_reverse = 0;
+
+ zero_ip(&ip);
+ ZERO_STRUCT(c);
- smbc_initialized = 1;
- smbc_auth_fn = fn;
- smbc_debug = debug;
+ if (server[0] == 0) {
+ errno = EPERM;
+ return NULL;
+ }
- DEBUGLEVEL = -1;
+ check_server_cache:
- setup_logging(pname, False);
+ srv = context->callbacks.get_cached_srv_fn(context, server, share,
+ workgroup, username);
+
+ if (!auth_called && !srv && (!username[0] || !password[0])) {
+ context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
+ username, sizeof(fstring), password, sizeof(fstring));
+ /*
+ * However, smbc_auth_fn may have picked up info relating to an
+ * existing connection, so try for an existing connection again ...
+ */
+ auth_called = 1;
+ goto check_server_cache;
+
+ }
+
+ if (srv) {
+ if (context->callbacks.check_server_fn(context, srv)) {
+ /*
+ * This server is no good anymore
+ * Try to remove it and check for more possible servers in the cache
+ */
+ if (context->callbacks.remove_unused_server_fn(context, srv)) {
+ /*
+ * We could not remove the server completely, remove it from the cache
+ * so we will not get it again. It will be removed when the last file/dir
+ * is closed.
+ */
+ context->callbacks.remove_cached_srv_fn(context, srv);
+ }
+
+ /*
+ * Maybe there are more cached connections to this server
+ */
+ goto check_server_cache;
+ }
+ return srv;
+ }
+
+ make_nmb_name(&calling, context->netbios_name, 0x0);
+ make_nmb_name(&called , server, 0x20);
+
+ DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
+
+ if ((p=strchr_m(server_n,'#')) &&
+ (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
+
+ fstrcpy(group, server_n);
+ p = strchr_m(group,'#');
+ *p = 0;
+
+ }
- /*
- * We try to construct our netbios name from our hostname etc
- */
+ DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
- user = getenv("USER");
- if (!user) user = ""; /* FIXME: What to do about this? */
+ again:
+ slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
- /*
- * FIXME: Is this the best way to get the user info? */
+ zero_ip(&ip);
- pstrcpy(smbc_user, user); /* Save for use elsewhere */
+ /* have to open a new connection */
+ if (!cli_initialise(&c)) {
+ errno = ENOENT;
+ return NULL;
+ }
- pid = getpid();
+ c.timeout = context->timeout;
- /*
- * Hmmm, I want to get hostname as well, but I am too lazy for the moment
- */
+ if (!cli_connect(&c, server_n, &ip)) {
+ cli_shutdown(&c);
+ errno = ENOENT;
+ return NULL;
+ }
- slprintf(my_netbios_name, 16, "smbc%s%d", user, pid);
- pstrcpy(workgroup, wgroup);
+ if (!cli_session_request(&c, &calling, &called)) {
+ cli_shutdown(&c);
+ if (strcmp(called.name, "*SMBSERVER")) {
+ make_nmb_name(&called , "*SMBSERVER", 0x20);
+ goto again;
+ }
+ else { /* Try one more time, but ensure we don't loop */
- charset_initialise();
+ /* Only try this if server is an IP address ... */
- /* Here we would open the smb.conf file if needed ... */
+ if (is_ipaddress(server) && !tried_reverse) {
+ fstring remote_name;
+ struct in_addr rem_ip;
- home = getenv("HOME");
+ if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
+ DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
+ errno = ENOENT;
+ return NULL;
+ }
- slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
+ tried_reverse++; /* Yuck */
- load_interfaces(); /* Load the list of interfaces ... */
+ if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
+ make_nmb_name(&called, remote_name, 0x20);
+ goto again;
+ }
- in_client = True; /* FIXME, make a param */
+ }
+ }
+ errno = ENOENT;
+ return NULL;
+ }
+ DEBUG(4,(" session request ok\n"));
+
+ if (!cli_negprot(&c)) {
+ cli_shutdown(&c);
+ errno = ENOENT;
+ return NULL;
+ }
- if (!lp_load(conf, True, False, False)) {
-
- /*
- * Hmmm, what the hell do we do here ... we could not parse the
- * config file ... We must return an error ... and keep info around
- * about why we failed
- */
- /*
- errno = ENOENT; /* Hmmm, what error resp does lp_load return ? */
- return -1;
-
- }
-
- codepage_initialise(lp_client_code_page()); /* Get a codepage */
-
- reopen_logs(); /* Get logging working ... */
-
- /*
- * Now initialize the file descriptor array and figure out what the
- * max open files is, so we can return FD's that are above the max
- * open file, and separated by a guard band
- */
-
-#if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
- do {
- struct rlimit rlp;
-
- if (getrlimit(RLIMIT_NOFILE, &rlp)) {
-
- DEBUG(0, ("smbc_init: getrlimit(1) for RLIMIT_NOFILE failed with error %s\n", strerror(errno)));
-
- smbc_start_fd = 1000000;
- smbc_max_fd = 10000; /* FIXME, should be a define ... */
-
- }
- else {
-
- smbc_start_fd = rlp.rlim_max + 10000; /* Leave a guard space of 10,000 */
- smbc_max_fd = 10000;
+ if (!cli_session_setup(&c, username,
+ password, strlen(password),
+ password, strlen(password),
+ workgroup) &&
+ /* try an anonymous login if it failed */
+ !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
+ cli_shutdown(&c);
+ errno = EPERM;
+ return NULL;
+ }
- }
- } while ( 0 );
-#else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
+ DEBUG(4,(" session setup ok\n"));
- smbc_start_fd = 1000000;
- smbc_max_fd = 10000; /* FIXME, should be a define ... */
+ if (!cli_send_tconX(&c, share, "?????",
+ password, strlen(password)+1)) {
+ errno = smbc_errno(context, &c);
+ cli_shutdown(&c);
+ return NULL;
+ }
+
+ DEBUG(4,(" tconx ok\n"));
+
+ /*
+ * Ok, we have got a nice connection
+ * Let's find a free server_fd
+ */
-#endif
+ srv = (SMBCSRV *)malloc(sizeof(*srv));
+ if (!srv) {
+ errno = ENOMEM;
+ goto failed;
+ }
- smbc_file_table = malloc(smbc_max_fd * sizeof(struct smbc_file *));
+ ZERO_STRUCTP(srv);
+ srv->cli = c;
+ srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
- for (p = 0; p < smbc_max_fd; p++)
- smbc_file_table[p] = NULL;
+ /* now add it to the cache (internal or external) */
+ if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
+ DEBUG(3, (" Failed to add server to cache\n"));
+ goto failed;
+ }
- if (!smbc_file_table)
- return ENOMEM;
+
+ DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
+ server, share, srv));
- return 0; /* Success */
+ return srv;
+ failed:
+ cli_shutdown(&c);
+ if (!srv) return NULL;
+
+ SAFE_FREE(srv);
+ return NULL;
}
/*
* Routine to open() a file ...
*/
-int smbc_open(const char *fname, int flags, mode_t mode)
+static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
{
- fstring server, share, user, password;
- pstring path;
- struct smbc_server *srv = NULL;
- struct smbc_file *file = NULL;
- int fd;
+ fstring server, share, user, password, workgroup;
+ pstring path;
+ SMBCSRV *srv = NULL;
+ SMBCFILE *file = NULL;
+ int fd;
- if (!smbc_initialized) {
-
- errno = EUCLEAN; /* Best I can think of ... */
- return -1;
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- }
+ errno = EINVAL; /* Best I can think of ... */
+ return NULL;
- if (!fname) {
+ }
- errno = EINVAL;
- return -1;
+ if (!fname) {
- }
+ errno = EINVAL;
+ return NULL;
- smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
+ }
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
- srv = smbc_server(server, share, lp_workgroup(), user, password);
+ if (user[0] == (char)0) fstrcpy(user, context->user);
- if (!srv) {
+ fstrcpy(workgroup, context->workgroup);
- if (errno == EPERM) errno = EACCES;
- return -1; /* smbc_server sets errno */
+ srv = smbc_server(context, server, share, workgroup, user, password);
- }
+ if (!srv) {
- if (path[strlen(path) - 1] == '\\') {
+ if (errno == EPERM) errno = EACCES;
+ return NULL; /* smbc_server sets errno */
- fd = -1;
-
- }
- else {
-
- int slot = 0;
-
- /* Find a free slot first */
-
- while (smbc_file_table[slot])
- slot++;
-
- if (slot > smbc_max_fd) {
-
- errno = ENOMEM; /* FIXME, is this best? */
- return -1;
+ }
- }
+ /* Hmmm, the test for a directory is suspect here ... FIXME */
- smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
+ if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
+
+ fd = -1;
- if (!smbc_file_table[slot]) {
+ }
+ else {
+
+ file = malloc(sizeof(SMBCFILE));
- errno = ENOMEM;
- return -1;
+ if (!file) {
- }
+ errno = ENOMEM;
+ return NULL;
- if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
+ }
- /* Handle the error ... */
+ ZERO_STRUCTP(file);
- free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- errno = smbc_errno(&srv->cli);
- return -1;
+ if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
- }
+ /* Handle the error ... */
- /* Fill in file struct */
+ SAFE_FREE(file);
+ errno = smbc_errno(context, &srv->cli);
+ return NULL;
- smbc_file_table[slot]->cli_fd = fd;
- smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
- smbc_file_table[slot]->fname = strdup(fname);
- smbc_file_table[slot]->srv = srv;
- smbc_file_table[slot]->offset = 0;
- smbc_file_table[slot]->file = True;
+ }
- return smbc_file_table[slot]->smbc_fd;
+ /* Fill in file struct */
- }
+ file->cli_fd = fd;
+ file->fname = strdup(fname);
+ file->srv = srv;
+ file->offset = 0;
+ file->file = True;
- /* Check if opendir needed ... */
+ DLIST_ADD(context->internal->_files, file);
+ return file;
- if (fd == -1) {
- int eno = 0;
+ }
- eno = smbc_errno(&srv->cli);
- fd = smbc_opendir(fname);
- if (fd < 0) errno = eno;
- return fd;
+ /* Check if opendir needed ... */
- }
+ if (fd == -1) {
+ int eno = 0;
- return 1; /* Success, with fd ... */
+ eno = smbc_errno(context, &srv->cli);
+ file = context->opendir(context, fname);
+ if (!file) errno = eno;
+ return file;
- failed:
+ }
- /*FIXME, clean up things ... */
- return -1;
+ errno = EINVAL; /* FIXME, correct errno ? */
+ return NULL;
}
static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
-int smbc_creat(const char *path, mode_t mode)
+static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
{
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
+ errno = EINVAL;
+ return NULL;
- }
+ }
- return smbc_open(path, creat_bits, mode);
+ return smbc_open_ctx(context, path, creat_bits, mode);
}
/*
* Routine to read() a file ...
*/
-ssize_t smbc_read(int fd, void *buf, size_t count)
+static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
{
- struct smbc_file *fe;
- int ret;
+ int ret;
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- DEBUG(4, ("smbc_read(%d, %d)\n", fd, (int)count));
+ DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ /* Check that the buffer exists ... */
- if (!fe->file) {
+ if (buf == NULL) {
- errno = EBADF;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- ret = cli_read(&fe->srv->cli, fe->cli_fd, buf, fe->offset, count);
+ ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
- if (ret < 0) {
+ if (ret < 0) {
- errno = smbc_errno(&fe->srv->cli);
- return -1;
+ errno = smbc_errno(context, &file->srv->cli);
+ return -1;
- }
+ }
- fe->offset += ret;
+ file->offset += ret;
- DEBUG(4, (" --> %d\n", ret));
+ DEBUG(4, (" --> %d\n", ret));
- return ret; /* Success, ret bytes of data ... */
+ return ret; /* Success, ret bytes of data ... */
}
* Routine to write() a file ...
*/
-ssize_t smbc_write(int fd, void *buf, size_t count)
+static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
{
- int ret;
- struct smbc_file *fe;
+ int ret;
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ /* Check that the buffer exists ... */
- ret = cli_write(&fe->srv->cli, fe->cli_fd, 0, buf, fe->offset, count);
+ if (buf == NULL) {
- if (ret < 0) {
+ errno = EINVAL;
+ return -1;
- errno = smbc_errno(&fe->srv->cli);
- return -1;
+ }
+
+ ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
+
+ if (ret <= 0) {
+
+ errno = smbc_errno(context, &file->srv->cli);
+ return -1;
- }
+ }
- fe->offset += ret;
+ file->offset += ret;
- return ret; /* Success, 0 bytes of data ... */
+ return ret; /* Success, 0 bytes of data ... */
}
/*
* Routine to close() a file ...
*/
-int smbc_close(int fd)
+static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
{
- struct smbc_file *fe;
+ SMBCSRV *srv;
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ /* IS a dir ... */
+ if (!file->file) {
+
+ return context->closedir(context, file);
- if (!fe->file) {
+ }
- return smbc_closedir(fd);
+ if (!cli_close(&file->srv->cli, file->cli_fd)) {
- }
+ DEBUG(3, ("cli_close failed on %s. purging server.\n",
+ file->fname));
+ /* Deallocate slot and remove the server
+ * from the server cache if unused */
+ errno = smbc_errno(context, &file->srv->cli);
+ srv = file->srv;
+ DLIST_REMOVE(context->internal->_files, file);
+ SAFE_FREE(file->fname);
+ SAFE_FREE(file);
+ context->callbacks.remove_unused_server_fn(context, srv);
- if (!cli_close(&fe->srv->cli, fe->cli_fd)) {
+ return -1;
- errno = smbc_errno(&fe->srv->cli); /* FIXME, should we deallocate slot? */
- return -1;
+ }
+
+ DLIST_REMOVE(context->internal->_files, file);
+ SAFE_FREE(file->fname);
+ SAFE_FREE(file);
+
+ return 0;
+}
+
+/*
+ * Get info from an SMB server on a file. Use a qpathinfo call first
+ * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
+ */
+static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
+ uint16 *mode, size_t *size,
+ time_t *c_time, time_t *a_time, time_t *m_time,
+ SMB_INO_T *ino)
+{
+
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
+
+ errno = EINVAL;
+ return -1;
+
+ }
- }
+ DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
+
+ if (!srv->no_pathinfo2 &&
+ cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
+ size, mode, ino)) return True;
+
+ /* if this is NT then don't bother with the getatr */
+ if (srv->cli.capabilities & CAP_NT_SMBS) return False;
+
+ if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
+ a_time = c_time = m_time;
+ srv->no_pathinfo2 = True;
+ return True;
+ }
- free(fe);
- smbc_file_table[fd - smbc_start_fd] = NULL;
+ return False;
- return 0;
}
/*
* Routine to unlink() a file
*/
-int smbc_unlink(const char *fname)
+static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
{
- fstring server, share, user, password;
- pstring path;
- struct smbc_server *srv = NULL;
+ fstring server, share, user, password, workgroup;
+ pstring path;
+ SMBCSRV *srv = NULL;
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN; /* Best I can think of ... */
- return -1;
+ errno = EINVAL; /* Best I can think of ... */
+ return -1;
+
+ }
- }
+ if (!fname) {
- if (!fname) {
+ errno = EINVAL;
+ return -1;
- errno = EINVAL;
- return -1;
+ }
- }
+ smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
- smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
+ if (user[0] == (char)0) fstrcpy(user, context->user);
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ fstrcpy(workgroup, context->workgroup);
- srv = smbc_server(server, share, lp_workgroup(), user, password);
+ srv = smbc_server(context, server, share, workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- return -1; /* smbc_server sets errno */
+ return -1; /* smbc_server sets errno */
- }
+ }
- /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
+ /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
int job = smbc_stat_printjob(srv, path, NULL, NULL);
if (job == -1) {
return -1;
}
- if (cli_printjob_del(&srv->cli, job) != 0) {
+ if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
+
return -1;
}
} else */
- if (!cli_unlink(&srv->cli, path)) {
+ if (!cli_unlink(&srv->cli, path)) {
- errno = smbc_errno(&srv->cli);
+ errno = smbc_errno(context, &srv->cli);
- if (errno == EACCES) { /* Check if the file is a directory */
+ if (errno == EACCES) { /* Check if the file is a directory */
- int err, saverr = errno;
- struct stat st;
- size_t size = 0;
- uint16 mode = 0;
- time_t m_time = 0, a_time = 0, c_time = 0;
- SMB_INO_T ino = 0;
+ int saverr = errno;
+ size_t size = 0;
+ uint16 mode = 0;
+ time_t m_time = 0, a_time = 0, c_time = 0;
+ SMB_INO_T ino = 0;
- if (!smbc_getatr(srv, path, &mode, &size,
- &c_time, &a_time, &m_time, &ino)) {
+ if (!smbc_getatr(context, srv, path, &mode, &size,
+ &c_time, &a_time, &m_time, &ino)) {
- /* Hmmm, bad error ... What? */
+ /* Hmmm, bad error ... What? */
- errno = smbc_errno(&srv->cli);
- return -1;
+ errno = smbc_errno(context, &srv->cli);
+ return -1;
- }
- else {
+ }
+ else {
- if (IS_DOS_DIR(mode))
- errno = EISDIR;
- else
- errno = saverr; /* Restore this */
+ if (IS_DOS_DIR(mode))
+ errno = EISDIR;
+ else
+ errno = saverr; /* Restore this */
- }
- }
+ }
+ }
- return -1;
+ return -1;
- }
+ }
- return 0; /* Success ... */
+ return 0; /* Success ... */
}
* Routine to rename() a file
*/
-int smbc_rename(const char *oname, const char *nname)
+static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
+ SMBCCTX *ncontext, const char *nname)
{
- fstring server1, share1, server2, share2, user1, user2, password1, password2;
- pstring path1, path2;
- struct smbc_server *srv = NULL;
-
- if (!smbc_initialized) {
+ fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
+ pstring path1, path2;
+ SMBCSRV *srv = NULL;
- errno = EUCLEAN; /* Best I can think of ... */
- return -1;
+ if (!ocontext || !ncontext ||
+ !ocontext->internal || !ncontext->internal ||
+ !ocontext->internal->_initialized ||
+ !ncontext->internal->_initialized) {
- }
+ errno = EINVAL; /* Best I can think of ... */
+ return -1;
- if (!oname || !nname) {
+ }
+
+ if (!oname || !nname) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
-
- DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
+ }
+
+ DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
- smbc_parse_path(oname, server1, share1, path1, user1, password1);
+ smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
- if (user1[0] == (char)0) pstrcpy(user1, smbc_user);
+ if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
- smbc_parse_path(nname, server2, share2, path2, user2, password2);
+ smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
- if (user2[0] == (char)0) pstrcpy(user2, smbc_user);
+ if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
- if (strcmp(server1, server2) || strcmp(share1, share2) ||
- strcmp(user1, user2)) {
+ if (strcmp(server1, server2) || strcmp(share1, share2) ||
+ strcmp(user1, user2)) {
- /* Can't rename across file systems, or users?? */
+ /* Can't rename across file systems, or users?? */
- errno = EXDEV;
- return -1;
+ errno = EXDEV;
+ return -1;
- }
+ }
- srv = smbc_server(server1, share1, lp_workgroup(), user1, password1);
- if (!srv) {
+ fstrcpy(workgroup, ocontext->workgroup);
+ /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
+ srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
+ if (!srv) {
- return -1;
+ return -1;
- }
+ }
- if (!cli_rename(&srv->cli, path1, path2)) {
- int eno = smbc_errno(&srv->cli);
+ if (!cli_rename(&srv->cli, path1, path2)) {
+ int eno = smbc_errno(ocontext, &srv->cli);
- if (eno != EEXIST ||
- !cli_unlink(&srv->cli, path2) ||
- !cli_rename(&srv->cli, path1, path2)) {
+ if (eno != EEXIST ||
+ !cli_unlink(&srv->cli, path2) ||
+ !cli_rename(&srv->cli, path1, path2)) {
- errno = eno;
- return -1;
+ errno = eno;
+ return -1;
- }
- }
+ }
+ }
- return 0; /* Success */
+ return 0; /* Success */
}
* A routine to lseek() a file
*/
-off_t smbc_lseek(int fd, off_t offset, int whence)
+static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
{
- struct smbc_file *fe;
- size_t size;
+ size_t size;
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
+ errno = EINVAL;
+ return -1;
+
+ }
- }
+ if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ errno = EBADF;
+ return -1;
- errno = EBADF;
- return -1;
+ }
- }
+ if (!file->file) {
- fe = smbc_file_table[fd - smbc_start_fd];
+ errno = EINVAL;
+ return -1; /* Can't lseek a dir ... */
- if (!fe->file) {
+ }
- return smbc_lseekdir(fd, offset, whence);
+ switch (whence) {
+ case SEEK_SET:
+ file->offset = offset;
+ break;
+
+ case SEEK_CUR:
+ file->offset += offset;
+ break;
+
+ case SEEK_END:
+ if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
+ NULL, NULL, NULL))
+ {
+ SMB_BIG_UINT b_size = size;
+ if (!cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &b_size, NULL, NULL,
+ NULL))
+ {
+ errno = EINVAL;
+ return -1;
+ } else
+ size = b_size;
+ }
+ file->offset = size + offset;
+ break;
+
+ default:
+ errno = EINVAL;
+ break;
- }
+ }
- switch (whence) {
- case SEEK_SET:
- fe->offset = offset;
- break;
+ return file->offset;
- case SEEK_CUR:
- fe->offset += offset;
- break;
+}
- case SEEK_END:
- if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
- NULL, NULL, NULL) &&
- !cli_getattrE(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
- NULL)) {
+/*
+ * Generate an inode number from file name for those things that need it
+ */
- errno = EINVAL;
- return -1;
- }
- fe->offset = size + offset;
- break;
-
- default:
- errno = EINVAL;
- break;
+static
+ino_t smbc_inode(SMBCCTX *context, const char *name)
+{
- }
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- return fe->offset;
+ errno = EINVAL;
+ return -1;
-}
-
-/*
- * Generate an inode number from file name for those things that need it
- */
-
-static
-ino_t smbc_inode(const char *name)
-{
+ }
- if (!*name) return 2; /* FIXME, why 2 ??? */
- return (ino_t)str_checksum(name);
+ if (!*name) return 2; /* FIXME, why 2 ??? */
+ return (ino_t)str_checksum(name);
}
*/
static
-int smbc_setup_stat(struct stat *st, char *fname, size_t size, int mode)
-{
-
- st->st_mode = 0;
-
- if (IS_DOS_DIR(mode)) {
- st->st_mode = SMBC_DIR_MODE;
- } else {
- st->st_mode = SMBC_FILE_MODE;
- }
-
- if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
- if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
- if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
- if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
-
- st->st_size = size;
- st->st_blksize = 512;
- st->st_blocks = (size+511)/512;
- st->st_uid = getuid();
- st->st_gid = getgid();
-
- if (IS_DOS_DIR(mode)) {
- st->st_nlink = 2;
- } else {
- st->st_nlink = 1;
- }
-
- if (st->st_ino == 0) {
- st->st_ino = smbc_inode(fname);
- }
-}
-
-/*
- * Get info from an SMB server on a file. Use a qpathinfo call first
- * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
- */
-
-BOOL smbc_getatr(struct smbc_server *srv, char *path,
- uint16 *mode, size_t *size,
- time_t *c_time, time_t *a_time, time_t *m_time,
- SMB_INO_T *ino)
+int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
{
+
+ st->st_mode = 0;
- if (!smbc_initialized) {
+ if (IS_DOS_DIR(mode)) {
+ st->st_mode = SMBC_DIR_MODE;
+ } else {
+ st->st_mode = SMBC_FILE_MODE;
+ }
- errno = EUCLEAN;
- return -1;
+ if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
+ if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
+ if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
+ if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
+
+ st->st_size = size;
+ st->st_blksize = 512;
+ st->st_blocks = (size+511)/512;
+ st->st_uid = getuid();
+ st->st_gid = getgid();
+
+ if (IS_DOS_DIR(mode)) {
+ st->st_nlink = 2;
+ } else {
+ st->st_nlink = 1;
+ }
- }
+ if (st->st_ino == 0) {
+ st->st_ino = smbc_inode(context, fname);
+ }
+
+ return True; /* FIXME: Is this needed ? */
- DEBUG(4,("sending qpathinfo\n"));
-
- if (!srv->no_pathinfo2 &&
- cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
- size, mode, ino)) return True;
-
- /* if this is NT then don't bother with the getatr */
- if (srv->cli.capabilities & CAP_NT_SMBS) return False;
-
- if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
- a_time = c_time = m_time;
- srv->no_pathinfo2 = True;
- return True;
- }
- return False;
}
/*
* Routine to stat a file given a name
*/
-int smbc_stat(const char *fname, struct stat *st)
+static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
{
- struct smbc_server *srv;
- fstring server, share, user, password;
- pstring path;
- time_t m_time = 0, a_time = 0, c_time = 0;
- size_t size = 0;
- uint16 mode = 0;
- SMB_INO_T ino = 0;
-
- if (!smbc_initialized) {
-
- errno = EUCLEAN; /* Best I can think of ... */
- return -1;
-
- }
+ SMBCSRV *srv;
+ fstring server, share, user, password, workgroup;
+ pstring path;
+ time_t m_time = 0, a_time = 0, c_time = 0;
+ size_t size = 0;
+ uint16 mode = 0;
+ SMB_INO_T ino = 0;
+
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
+
+ errno = EINVAL; /* Best I can think of ... */
+ return -1;
+
+ }
- if (!fname) {
+ if (!fname) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- DEBUG(4, ("stat(%s)\n", fname));
-
- smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
-
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ DEBUG(4, ("smbc_stat(%s)\n", fname));
- srv = smbc_server(server, share, lp_workgroup(), user, password);
+ smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
- if (!srv) {
+ if (user[0] == (char)0) fstrcpy(user, context->user);
- return -1; /* errno set by smbc_server */
+ fstrcpy(workgroup, context->workgroup);
- }
+ srv = smbc_server(context, server, share, workgroup, user, password);
- /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
-
- mode = aDIR | aRONLY;
+ if (!srv) {
- }
- else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
+ return -1; /* errno set by smbc_server */
- if (strcmp(path, "\\") == 0) {
+ }
- mode = aDIR | aRONLY;
+ /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
- }
- else {
+ mode = aDIR | aRONLY;
- mode = aRONLY;
- smbc_stat_printjob(srv, path, &size, &m_time);
- c_time = a_time = m_time;
+ }
+ else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
+
+ if (strcmp(path, "\\") == 0) {
+
+ mode = aDIR | aRONLY;
- }
- else { */
+ }
+ else {
- if (!smbc_getatr(srv, path, &mode, &size,
- &c_time, &a_time, &m_time, &ino)) {
+ mode = aRONLY;
+ smbc_stat_printjob(srv, path, &size, &m_time);
+ c_time = a_time = m_time;
- errno = smbc_errno(&srv->cli);
- return -1;
+ }
+ else { */
- }
+ if (!smbc_getatr(context, srv, path, &mode, &size,
+ &c_time, &a_time, &m_time, &ino)) {
- /* } */
+ errno = smbc_errno(context, &srv->cli);
+ return -1;
+
+ }
- st->st_ino = ino;
+ st->st_ino = ino;
- smbc_setup_stat(st, path, size, mode);
+ smbc_setup_stat(context, st, path, size, mode);
- st->st_atime = a_time;
- st->st_ctime = c_time;
- st->st_mtime = m_time;
- st->st_dev = srv->dev;
+ st->st_atime = a_time;
+ st->st_ctime = c_time;
+ st->st_mtime = m_time;
+ st->st_dev = srv->dev;
- return 0;
+ return 0;
}
* Routine to stat a file given an fd
*/
-int smbc_fstat(int fd, struct stat *st)
+static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
{
- struct smbc_file *fe;
- time_t c_time, a_time, m_time;
- size_t size;
- uint16 mode;
- SMB_INO_T ino = 0;
+ time_t c_time, a_time, m_time;
+ size_t size;
+ uint16 mode;
+ SMB_INO_T ino = 0;
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
-
- }
+ errno = EINVAL;
+ return -1;
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ }
- errno = EBADF;
- return -1;
+ if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
- }
+ errno = EBADF;
+ return -1;
- fe = smbc_file_table[fd - smbc_start_fd];
+ }
- if (!fe->file) {
+ if (!file->file) {
- return smbc_fstatdir(fd, st);
+ return context->fstatdir(context, file, st);
- }
+ }
- if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd,
- &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
- !cli_getattrE(&fe->srv->cli, fe->cli_fd,
- &mode, &size, &c_time, &a_time, &m_time)) {
+ if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
+ &mode, &size, &c_time, &a_time, &m_time, NULL, &ino)) {
+ SMB_BIG_UINT b_size = size;
+ if (!cli_getattrE(&file->srv->cli, file->cli_fd,
+ &mode, &b_size, &c_time, &a_time, &m_time)) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
+ } else
+ size = b_size;
- }
+ }
- st->st_ino = ino;
+ st->st_ino = ino;
- smbc_setup_stat(st, fe->fname, size, mode);
+ smbc_setup_stat(context, st, file->fname, size, mode);
- st->st_atime = a_time;
- st->st_ctime = c_time;
- st->st_mtime = m_time;
- st->st_dev = fe->srv->dev;
+ st->st_atime = a_time;
+ st->st_ctime = c_time;
+ st->st_mtime = m_time;
+ st->st_dev = file->srv->dev;
- return 0;
+ return 0;
}
* smb:workgroup//server
* smb://server
* smb://server/share
+ * smb://<IP-addr> which should list shares on server
+ * smb://<IP-addr>/share which should list files on share
*/
-static void smbc_remove_dir(struct smbc_file *dir)
+static void smbc_remove_dir(SMBCFILE *dir)
{
- struct smbc_dir_list *d,*f;
+ struct smbc_dir_list *d,*f;
- d = dir->dir_list;
- while (d) {
+ d = dir->dir_list;
+ while (d) {
- f = d; d = d->next;
+ f = d; d = d->next;
- if (f->dirent) free(f->dirent);
- free(f);
+ SAFE_FREE(f->dirent);
+ SAFE_FREE(f);
- }
+ }
- dir->dir_list = dir->dir_end = dir->dir_next = NULL;
+ dir->dir_list = dir->dir_end = dir->dir_next = NULL;
}
-static int add_dirent(struct smbc_file *dir, const char *name, const char *comment, uint32 type)
+static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
{
- struct smbc_dirent *dirent;
- int size;
+ struct smbc_dirent *dirent;
+ int size;
+ char *u_name = NULL, *u_comment = NULL;
+ size_t u_name_len = 0, u_comment_len = 0;
- /*
- * Allocate space for the dirent, which must be increased by the
- * size of the name and the comment and 1 for the null on the comment.
- * The null on the name is already accounted for.
- */
+ if (name)
+ u_name_len = push_utf8_allocate(&u_name, name);
+ if (comment)
+ u_comment_len = push_utf8_allocate(&u_comment, comment);
- size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
- (comment?strlen(comment):0) + 1;
+ /*
+ * Allocate space for the dirent, which must be increased by the
+ * size of the name and the comment and 1 for the null on the comment.
+ * The null on the name is already accounted for.
+ */
+
+ size = sizeof(struct smbc_dirent) + u_name_len + u_comment_len + 1;
- dirent = malloc(size);
+ dirent = malloc(size);
- if (!dirent) {
+ if (!dirent) {
- dir->dir_error = ENOMEM;
- return -1;
+ dir->dir_error = ENOMEM;
+ return -1;
- }
+ }
- if (dir->dir_list == NULL) {
+ ZERO_STRUCTP(dirent);
- dir->dir_list = malloc(sizeof(struct smbc_dir_list));
- if (!dir->dir_list) {
+ if (dir->dir_list == NULL) {
- free(dirent);
- dir->dir_error = ENOMEM;
- return -1;
+ dir->dir_list = malloc(sizeof(struct smbc_dir_list));
+ if (!dir->dir_list) {
- }
+ SAFE_FREE(dirent);
+ dir->dir_error = ENOMEM;
+ return -1;
- dir->dir_end = dir->dir_next = dir->dir_list;
-
- }
- else {
-
- dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
-
- if (!dir->dir_end) {
+ }
+ ZERO_STRUCTP(dir->dir_list);
- free(dirent);
- dir->dir_error = ENOMEM;
- return -1;
+ dir->dir_end = dir->dir_next = dir->dir_list;
+
+ }
+ else {
- }
+ dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
+
+ if (!dir->dir_end->next) {
+
+ SAFE_FREE(dirent);
+ dir->dir_error = ENOMEM;
+ return -1;
- dir->dir_end = dir->dir_end->next;
+ }
+ ZERO_STRUCTP(dir->dir_end->next);
- }
+ dir->dir_end = dir->dir_end->next;
- dir->dir_end->next = NULL;
- dir->dir_end->dirent = dirent;
+ }
- dirent->smbc_type = type;
- dirent->namelen = (name?strlen(name):0);
- dirent->commentlen = (comment?strlen(comment):0);
- dirent->dirlen = size;
+ dir->dir_end->next = NULL;
+ dir->dir_end->dirent = dirent;
+
+ dirent->smbc_type = type;
+ dirent->namelen = u_name_len;
+ dirent->commentlen = u_comment_len;
+ dirent->dirlen = size;
- strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
+ strncpy(dirent->name, (u_name?u_name:""), dirent->namelen + 1);
- dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
- strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
+ dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
+ strncpy(dirent->comment, (u_comment?u_comment:""), dirent->commentlen + 1);
+
+ SAFE_FREE(u_comment);
+ SAFE_FREE(u_name);
- return 0;
+ return 0;
}
static void
list_fn(const char *name, uint32 type, const char *comment, void *state)
{
- struct smbc_file *dir = (struct smbc_file *)state;
- int dirent_type;
+ SMBCFILE *dir = (SMBCFILE *)state;
+ int dirent_type;
- /* We need to process the type a little ... */
+ /* We need to process the type a little ... */
- if (dir->dir_type == SMBC_FILE_SHARE) {
+ if (dir->dir_type == SMBC_FILE_SHARE) {
+
+ switch (type) {
+ case 0: /* Directory tree */
+ dirent_type = SMBC_FILE_SHARE;
+ break;
+
+ case 1:
+ dirent_type = SMBC_PRINTER_SHARE;
+ break;
+
+ case 2:
+ dirent_type = SMBC_COMMS_SHARE;
+ break;
+
+ case 3:
+ dirent_type = SMBC_IPC_SHARE;
+ break;
+
+ default:
+ dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
+ break;
+ }
+ }
+ else dirent_type = dir->dir_type;
- switch (type) {
- case 0: /* Directory tree */
- dirent_type = SMBC_FILE_SHARE;
- break;
+ if (add_dirent(dir, name, comment, dirent_type) < 0) {
- case 1:
- dirent_type = SMBC_PRINTER_SHARE;
- break;
+ /* An error occurred, what do we do? */
+ /* FIXME: Add some code here */
- case 2:
- dirent_type = SMBC_COMMS_SHARE;
- break;
+ }
- case 3:
- dirent_type = SMBC_IPC_SHARE;
- break;
+}
- default:
- dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
- break;
- }
+static void
+dir_list_fn(file_info *finfo, const char *mask, void *state)
+{
- }
- else dirent_type = dir->dir_type;
+ if (add_dirent((SMBCFILE *)state, finfo->name, "",
+ (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
- if (add_dirent(dir, name, comment, dirent_type) < 0) {
+ /* Handle an error ... */
- /* An error occurred, what do we do? */
+ /* FIXME: Add some code ... */
- }
+ }
}
-static void
-dir_list_fn(file_info *finfo, const char *mask, void *state)
+static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
{
+ fstring server, share, user, password;
+ pstring workgroup;
+ pstring path;
+ SMBCSRV *srv = NULL;
+ SMBCFILE *dir = NULL;
+ struct in_addr rem_ip;
+
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
+ DEBUG(4, ("no valid context\n"));
+ errno = EINVAL;
+ return NULL;
- fprintf(stderr, "Finfo->name=%s, mask=%s\n", finfo->name, mask);
- if (add_dirent((struct smbc_file *)state, finfo->name, "",
- (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
+ }
- /* Handle an error ... */
+ if (!fname) {
+ DEBUG(4, ("no valid fname\n"));
+ errno = EINVAL;
+ return NULL;
+ }
- }
+ if (smbc_parse_path(context, fname, server, share, path, user, password)) {
+ DEBUG(4, ("no valid path\n"));
+ errno = EINVAL;
+ return NULL;
+ }
-}
+ DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s'\n", fname, server, share, path));
-int smbc_opendir(const char *fname)
-{
- struct in_addr addr;
- fstring server, share, user, password;
- pstring path;
- struct smbc_server *srv = NULL;
- struct in_addr rem_ip;
- int slot = 0;
- uint8 eclass;
- uint32 ecode;
-
- if (!smbc_initialized) {
-
- errno = EUCLEAN;
- return -1;
+ if (user[0] == (char)0) fstrcpy(user, context->user);
- }
+ pstrcpy(workgroup, context->workgroup);
- if (!fname) {
-
- errno = EINVAL;
- return -1;
+ dir = malloc(sizeof(*dir));
- }
+ if (!dir) {
- if (smbc_parse_path(fname, server, share, path, user, password)) {
+ errno = ENOMEM;
+ return NULL;
- errno = EINVAL;
- return -1;
+ }
- }
+ ZERO_STRUCTP(dir);
+
+ dir->cli_fd = 0;
+ dir->fname = strdup(fname);
+ dir->srv = NULL;
+ dir->offset = 0;
+ dir->file = False;
+ dir->dir_list = dir->dir_next = dir->dir_end = NULL;
+
+ if (server[0] == (char)0) {
+ struct in_addr server_ip;
+ if (share[0] != (char)0 || path[0] != (char)0) {
+
+ errno = EINVAL;
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ return NULL;
+ }
+
+ /* We have server and share and path empty ... so list the workgroups */
+ /* first try to get the LMB for our workgroup, and if that fails, */
+ /* try the DMB */
+
+ pstrcpy(workgroup, lp_workgroup());
+
+ if (!find_master_ip(workgroup, &server_ip)) {
+ struct user_auth_info u_info;
+ struct cli_state *cli;
+
+ DEBUG(4, ("Unable to find master browser for workgroup %s\n",
+ workgroup));
+
+ /* find the name of the server ... */
+ pstrcpy(u_info.username, user);
+ pstrcpy(u_info.password, password);
+
+ if (!(cli = get_ipc_connect_master_ip_bcast(workgroup, &u_info))) {
+ DEBUG(4, ("Unable to find master browser by "
+ "broadcast\n"));
+ errno = ENOENT;
+ return NULL;
+ }
+
+ fstrcpy(server, cli->desthost);
+
+ cli_shutdown(cli);
+ } else {
+ if (!name_status_find("*", 0, 0, server_ip, server)) {
+ errno = ENOENT;
+ return NULL;
+ }
+ }
+
+ DEBUG(4, ("using workgroup %s %s\n", workgroup, server));
+
+ /*
+ * Get a connection to IPC$ on the server if we do not already have one
+ */
+
+ srv = smbc_server(context, server, "IPC$", workgroup, user, password);
+
+ if (!srv) {
+
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ return NULL;
+ }
+
+ dir->srv = srv;
+ dir->dir_type = SMBC_WORKGROUP;
+
+ /* Now, list the stuff ... */
+
+ if (!cli_NetServerEnum(&srv->cli, workgroup, SV_TYPE_DOMAIN_ENUM, list_fn,
+ (void *)dir)) {
+
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ errno = cli_errno(&srv->cli);
+
+ return NULL;
+
+ }
+ }
+ else { /* Server not an empty string ... Check the rest and see what gives */
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ if (share[0] == (char)0) {
- /* Get a file entry ... */
+ if (path[0] != (char)0) { /* Should not have empty share with path */
- slot = 0;
+ errno = EINVAL;
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ return NULL;
+
+ }
- while (smbc_file_table[slot])
- slot++;
+ /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
+ /* However, we check to see if <server> is an IP address first */
- if (slot > smbc_max_fd) {
+ if (!is_ipaddress(server) && /* Not an IP addr so check next */
+ (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
+ resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
+ pstring buserver;
- errno = ENOMEM;
- return -1; /* FIXME, ... move into a func */
-
- }
+ dir->dir_type = SMBC_SERVER;
- smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
+ /*
+ * Get the backup list ...
+ */
- if (!smbc_file_table[slot]) {
- errno = ENOMEM;
- return -1;
+ if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
- }
+ DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
+ errno = EPERM; /* FIXME, is this correct */
+ return NULL;
- smbc_file_table[slot]->cli_fd = 0;
- smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
- smbc_file_table[slot]->fname = strdup(fname);
- smbc_file_table[slot]->srv = NULL;
- smbc_file_table[slot]->offset = 0;
- smbc_file_table[slot]->file = False;
- smbc_file_table[slot]->dir_list =
- smbc_file_table[slot]->dir_next =
- smbc_file_table[slot]->dir_end = NULL;
+ }
- if (server[0] == (char)0) {
+ /*
+ * Get a connection to IPC$ on the server if we do not already have one
+ */
- if (share[0] != (char)0 || path[0] != (char)0) {
-
- errno = EINVAL;
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- return -1;
+ srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
- }
+ if (!srv) {
+ DEBUG(0, ("got no contact to IPC$\n"));
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ return NULL;
- /* We have server and share and path empty ... so list the workgroups */
+ }
- /* fprintf(stderr, "Workgroup is: %s\n", lp_workgroup()); */
- cli_get_backup_server(my_netbios_name, lp_workgroup(), server, sizeof(server));
+ dir->srv = srv;
- smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;
+ /* Now, list the servers ... */
- /*
- * Get a connection to IPC$ on the server if we do not already have one
- */
+ if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
+ (void *)dir)) {
- srv = smbc_server(server, "IPC$", lp_workgroup(), user, password);
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ errno = cli_errno(&srv->cli);
+ return NULL;
+
+ }
- if (!srv) {
+ }
+ else {
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- return -1;
+ if (resolve_name(server, &rem_ip, 0x20)) {
- }
+ /* Now, list the shares ... */
- smbc_file_table[slot]->srv = srv;
+ dir->dir_type = SMBC_FILE_SHARE;
- /* Now, list the stuff ... */
+ srv = smbc_server(context, server, "IPC$", workgroup, user, password);
- if (!cli_NetServerEnum(&srv->cli, lp_workgroup(), 0x80000000, list_fn,
- (void *)smbc_file_table[slot])) {
+ if (!srv) {
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- errno = cli_error(&srv->cli, &eclass, &ecode, NULL);
- return -1;
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ return NULL;
- }
- }
- else { /* Server not an empty string ... Check the rest and see what gives */
+ }
- if (share[0] == (char)0) {
+ dir->srv = srv;
- if (path[0] != (char)0) { /* Should not have empty share with path */
+ /* Now, list the servers ... */
- errno = EINVAL;
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- return -1;
-
- }
+ if (cli_RNetShareEnum(&srv->cli, list_fn,
+ (void *)dir) < 0) {
- /* Check to see if <server><1D> translates, or <server><20> translates */
+ errno = cli_errno(&srv->cli);
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ return NULL;
- if (resolve_name(server, &rem_ip, 0x1d)) { /* Found LMB */
- pstring buserver;
+ }
- smbc_file_table[slot]->dir_type = SMBC_SERVER;
+ }
+ else {
- /*
- * Get the backup list ...
- */
+ errno = ENODEV; /* Neither the workgroup nor server exists */
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ return NULL;
- cli_get_backup_server(my_netbios_name, server, buserver, sizeof(buserver));
+ }
- /*
- * Get a connection to IPC$ on the server if we do not already have one
- */
+ }
- srv = smbc_server(buserver, "IPC$", lp_workgroup(), user, password);
+ }
+ else { /* The server and share are specified ... work from there ... */
- if (!srv) {
+ /* Well, we connect to the server and list the directory */
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL; /* FIXME: Memory leaks ... */
- return -1;
+ dir->dir_type = SMBC_FILE_SHARE;
- }
+ srv = smbc_server(context, server, share, workgroup, user, password);
- smbc_file_table[slot]->srv = srv;
+ if (!srv) {
- /* Now, list the servers ... */
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ return NULL;
- if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
- (void *)smbc_file_table[slot])) {
+ }
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- errno = cli_error(&srv->cli, &eclass, &ecode, NULL);
- return -1;
+ dir->srv = srv;
- }
+ /* Now, list the files ... */
- }
- else {
+ pstrcat(path, "\\*");
- if (resolve_name(server, &rem_ip, 0x20)) {
+ if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
+ (void *)dir) < 0) {
- /* Now, list the shares ... */
+ if (dir) {
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir);
+ }
+ errno = smbc_errno(context, &srv->cli);
+ return NULL;
- smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
+ }
+ }
- srv = smbc_server(server, "IPC$", lp_workgroup(), user, password);
+ }
- if (!srv) {
+ DLIST_ADD(context->internal->_files, dir);
+ return dir;
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- return -1;
+}
- }
+/*
+ * Routine to close a directory
+ */
- smbc_file_table[slot]->srv = srv;
+static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
+{
- /* Now, list the servers ... */
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- if (cli_RNetShareEnum(&srv->cli, list_fn,
- (void *)smbc_file_table[slot]) < 0) {
+ errno = EINVAL;
+ return -1;
- errno = cli_error(&srv->cli, &eclass, &ecode, NULL);
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- return -1;
+ }
- }
+ if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
+ errno = EBADF;
+ return -1;
+
}
- else {
- errno = EINVAL;
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- return -1;
+ smbc_remove_dir(dir); /* Clean it up */
+
+ DLIST_REMOVE(context->internal->_files, dir);
+
+ if (dir) {
+
+ SAFE_FREE(dir->fname);
+ SAFE_FREE(dir); /* Free the space too */
}
- }
+ return 0;
- }
- else { /* The server and share are specified ... work from there ... */
+}
- /* Well, we connect to the server and list the directory */
+/*
+ * Routine to get a directory entry
+ */
- smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
+struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
+{
+ struct smbc_dirent *dirp, *dirent;
- srv = smbc_server(server, share, lp_workgroup(), user, password);
+ /* Check that all is ok first ... */
- if (!srv) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- return -1;
+ errno = EINVAL;
+ return NULL;
- }
+ }
- smbc_file_table[slot]->srv = srv;
+ if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
- /* Now, list the files ... */
+ errno = EBADF;
+ return NULL;
- pstrcat(path, "\\*");
+ }
- if (!cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
- (void *)smbc_file_table[slot])) {
+ if (dir->file != False) { /* FIXME, should be dir, perhaps */
- if (smbc_file_table[slot]) free(smbc_file_table[slot]);
- smbc_file_table[slot] = NULL;
- errno = smbc_errno(&srv->cli);
- return -1;
+ errno = ENOTDIR;
+ return NULL;
- }
- }
+ }
+
+ if (!dir->dir_next)
+ return NULL;
+ else {
- }
+ dirent = dir->dir_next->dirent;
- return smbc_file_table[slot]->smbc_fd;
+ if (!dirent) {
+
+ errno = ENOENT;
+ return NULL;
+
+ }
+
+ /* Hmmm, do I even need to copy it? */
+
+ memcpy(context->internal->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
+ dirp = (struct smbc_dirent *)context->internal->_dirent;
+ dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
+ dir->dir_next = dir->dir_next->next;
+
+ return (struct smbc_dirent *)context->internal->_dirent;
+ }
}
/*
- * Routine to close a directory
+ * Routine to get directory entries
*/
-int smbc_closedir(int fd)
+static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
{
- struct smbc_file *fe;
+ struct smbc_dir_list *dirlist;
+ int rem = count, reqd;
+ char *ndir = (char *)dirp;
- if (!smbc_initialized) {
+ /* Check that all is ok first ... */
- errno = EUCLEAN;
- return -1;
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- }
+ errno = EINVAL;
+ return -1;
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ }
- errno = EBADF;
- return -1;
+ if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
+
+ errno = EBADF;
+ return -1;
+
+ }
- }
+ if (dir->file != False) { /* FIXME, should be dir, perhaps */
- fe = smbc_file_table[fd - smbc_start_fd];
+ errno = ENOTDIR;
+ return -1;
- if (!fe) {
+ }
- errno = ENOENT; /* FIXME: Is this correct */
- return -1;
+ /*
+ * Now, retrieve the number of entries that will fit in what was passed
+ * We have to figure out if the info is in the list, or we need to
+ * send a request to the server to get the info.
+ */
+
+ while ((dirlist = dir->dir_next)) {
+ struct smbc_dirent *dirent;
+
+ if (!dirlist->dirent) {
- }
+ errno = ENOENT; /* Bad error */
+ return -1;
- smbc_remove_dir(fe); /* Clean it up */
+ }
- if (fe) free(fe); /* Free the space too */
+ if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
+ dirlist->dirent->commentlen + 1))) {
- smbc_file_table[fd - smbc_start_fd] = NULL;
+ if (rem < count) { /* We managed to copy something */
- return 0;
+ errno = 0;
+ return count - rem;
+
+ }
+ else { /* Nothing copied ... */
+
+ errno = EINVAL; /* Not enough space ... */
+ return -1;
+
+ }
+
+ }
+
+ dirent = dirlist->dirent;
+
+ memcpy(ndir, dirent, reqd); /* Copy the data in ... */
+
+ ((struct smbc_dirent *)ndir)->comment =
+ (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
+
+ ndir += reqd;
+
+ rem -= reqd;
+
+ dir->dir_next = dirlist = dirlist -> next;
+ }
+
+ if (rem == count)
+ return 0;
+ else
+ return count - rem;
}
/*
- * Routine to get a directory entry
+ * Routine to create a directory ...
*/
-static char smbc_local_dirent[512]; /* Make big enough */
-
-struct smbc_dirent *smbc_readdir(unsigned int fd)
+static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
{
- struct smbc_file *fe;
- struct smbc_dirent *dirp, *dirent;
+ SMBCSRV *srv;
+ fstring server, share, user, password, workgroup;
+ pstring path;
+
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
+
+ errno = EINVAL;
+ return -1;
+
+ }
- /* Check that all is ok first ... */
+ if (!fname) {
- if (!smbc_initialized) {
+ errno = EINVAL;
+ return -1;
- errno = EUCLEAN;
- return NULL;
+ }
+
+ DEBUG(4, ("smbc_mkdir(%s)\n", fname));
- }
+ smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (user[0] == (char)0) fstrcpy(user, context->user);
- errno = EBADF;
- return NULL;
+ fstrcpy(workgroup, context->workgroup);
- }
+ srv = smbc_server(context, server, share, workgroup, user, password);
- fe = smbc_file_table[fd - smbc_start_fd];
+ if (!srv) {
- if (fe->file != False) { /* FIXME, should be dir, perhaps */
+ return -1; /* errno set by smbc_server */
- errno = ENOTDIR;
- return NULL;
+ }
- }
+ /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
- if (!fe->dir_next)
- return NULL;
- else {
+ mode = aDIR | aRONLY;
- dirent = fe->dir_next->dirent;
+ }
+ else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
- if (!dirent) {
+ if (strcmp(path, "\\") == 0) {
- errno = ENOENT;
- return NULL;
+ mode = aDIR | aRONLY;
- }
+ }
+ else {
- /* Hmmm, do I even need to copy it? */
+ mode = aRONLY;
+ smbc_stat_printjob(srv, path, &size, &m_time);
+ c_time = a_time = m_time;
- bcopy(dirent, smbc_local_dirent, dirent->dirlen); /* Copy the dirent */
+ }
+ else { */
- dirp = (struct smbc_dirent *)smbc_local_dirent;
+ if (!cli_mkdir(&srv->cli, path)) {
- dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
-
- fe->dir_next = fe->dir_next->next;
+ errno = smbc_errno(context, &srv->cli);
+ return -1;
- return (struct smbc_dirent *)smbc_local_dirent;
- }
+ }
+
+ return 0;
}
/*
- * Routine to get directory entries
+ * Our list function simply checks to see if a directory is not empty
*/
-int smbc_getdents(unsigned int fd, struct smbc_dirent *dirp, int count)
+static int smbc_rmdir_dirempty = True;
+
+static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
{
- struct smbc_file *fe;
- struct smbc_dir_list *dir;
- int rem = count, reqd;
- /* Check that all is ok first ... */
+ if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
+ smbc_rmdir_dirempty = False;
- if (!smbc_initialized) {
+}
- errno = EUCLEAN;
- return -1;
+/*
+ * Routine to remove a directory
+ */
- }
+static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
+{
+ SMBCSRV *srv;
+ fstring server, share, user, password, workgroup;
+ pstring path;
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EBADF;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ if (!fname) {
- if (fe->file != False) { /* FIXME, should be dir, perhaps */
+ errno = EINVAL;
+ return -1;
- errno = ENOTDIR;
- return -1;
+ }
+
+ DEBUG(4, ("smbc_rmdir(%s)\n", fname));
- }
+ smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
- /*
- * Now, retrieve the number of entries that will fit in what was passed
- * We have to figure out if the info is in the list, or we need to
- * send a request to the server to get the info.
- */
+ if (user[0] == (char)0) fstrcpy(user, context->user);
- while ((dir = fe->dir_next)) {
- struct smbc_dirent *dirent;
+ fstrcpy(workgroup, context->workgroup);
- if (!dir->dirent) {
+ srv = smbc_server(context, server, share, workgroup, user, password);
- errno = ENOENT; /* Bad error */
- return -1;
+ if (!srv) {
- }
+ return -1; /* errno set by smbc_server */
+
+ }
- if (rem < (reqd = (sizeof(struct smbc_dirent) + dir->dirent->namelen +
- dir->dirent->commentlen + 1))) {
+ /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
- if (rem < count) { /* We managed to copy something */
+ mode = aDIR | aRONLY;
- errno = 0;
- return count - rem;
+ }
+ else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
- }
- else { /* Nothing copied ... */
+ if (strcmp(path, "\\") == 0) {
- errno = EINVAL; /* Not enough space ... */
- return -1;
+ mode = aDIR | aRONLY;
- }
+ }
+ else {
- }
+ mode = aRONLY;
+ smbc_stat_printjob(srv, path, &size, &m_time);
+ c_time = a_time = m_time;
+
+ }
+ else { */
- dirent = dir->dirent;
+ if (!cli_rmdir(&srv->cli, path)) {
- bcopy(dirent, dirp, reqd); /* Copy the data in ... */
-
- dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
+ errno = smbc_errno(context, &srv->cli);
+
+ if (errno == EACCES) { /* Check if the dir empty or not */
+
+ pstring lpath; /* Local storage to avoid buffer overflows */
+
+ smbc_rmdir_dirempty = True; /* Make this so ... */
+
+ pstrcpy(lpath, path);
+ pstrcat(lpath, "\\*");
- (char *)dirp += reqd;
+ if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
+ NULL) < 0) {
- rem -= reqd;
+ /* Fix errno to ignore latest error ... */
- fe->dir_next = dir = dir -> next;
- }
+ DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
+ smbc_errno(context, &srv->cli)));
+ errno = EACCES;
- if (rem == count)
- return 0;
- else
- return count - rem;
+ }
+
+ if (smbc_rmdir_dirempty)
+ errno = EACCES;
+ else
+ errno = ENOTEMPTY;
+
+ }
+
+ return -1;
+
+ }
+
+ return 0;
}
/*
- * Routine to create a directory ...
+ * Routine to return the current directory position
*/
-int smbc_mkdir(const char *fname, mode_t mode)
+static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
{
- struct smbc_server *srv;
- fstring server, share, user, password;
- pstring path;
+ off_t ret_val; /* Squash warnings about cast */
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (!fname) {
+ if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
- errno = EINVAL;
- return -1;
+ errno = EBADF;
+ return -1;
- }
-
- DEBUG(4, ("stat(%s)\n", fname));
+ }
+
+ if (dir->file != False) { /* FIXME, should be dir, perhaps */
+
+ errno = ENOTDIR;
+ return -1;
+
+ }
+
+ /*
+ * We return the pointer here as the offset
+ */
+ ret_val = (int)dir->dir_next;
+ return ret_val;
- smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
+}
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+/*
+ * A routine to run down the list and see if the entry is OK
+ */
- srv = smbc_server(server, share, lp_workgroup(), user, password);
+struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
+ struct smbc_dirent *dirent)
+{
- if (!srv) {
+ /* Run down the list looking for what we want */
- return -1; /* errno set by smbc_server */
+ if (dirent) {
- }
+ struct smbc_dir_list *tmp = list;
- /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
+ while (tmp) {
- mode = aDIR | aRONLY;
+ if (tmp->dirent == dirent)
+ return tmp;
- }
- else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
+ tmp = tmp->next;
- if (strcmp(path, "\\") == 0) {
+ }
- mode = aDIR | aRONLY;
+ }
- }
- else {
+ return NULL; /* Not found, or an error */
- mode = aRONLY;
- smbc_stat_printjob(srv, path, &size, &m_time);
- c_time = a_time = m_time;
+}
- }
- else { */
- if (!cli_mkdir(&srv->cli, path)) {
+/*
+ * Routine to seek on a directory
+ */
- errno = smbc_errno(&srv->cli);
- return -1;
+static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
+{
+ long int l_offset = offset; /* Handle problems of size */
+ struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
+ struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
+
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
+
+ errno = EINVAL;
+ return -1;
+
+ }
+
+ if (dir->file != False) { /* FIXME, should be dir, perhaps */
+
+ errno = ENOTDIR;
+ return -1;
+
+ }
+
+ /* Now, check what we were passed and see if it is OK ... */
+
+ if (dirent == NULL) { /* Seek to the begining of the list */
+
+ dir->dir_next = dir->dir_list;
+ return 0;
+
+ }
- }
+ /* Now, run down the list and make sure that the entry is OK */
+ /* This may need to be changed if we change the format of the list */
- return 0;
+ if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
+
+ errno = EINVAL; /* Bad entry */
+ return -1;
+
+ }
+
+ dir->dir_next = list_ent;
+
+ return 0;
}
/*
- * Routine to remove a directory
+ * Routine to fstat a dir
*/
-int smbc_rmdir(const char *fname)
+static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
{
- struct smbc_server *srv;
- fstring server, share, user, password;
- pstring path;
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (!fname) {
+ /* No code yet ... */
- errno = EINVAL;
- return -1;
+ return 0;
- }
+}
+
+/*
+ * Open a print file to be written to by other calls
+ */
+
+static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
+{
+ fstring server, share, user, password;
+ pstring path;
+
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
+
+ errno = EINVAL;
+ return NULL;
+
+ }
+
+ if (!fname) {
+
+ errno = EINVAL;
+ return NULL;
+
+ }
- DEBUG(4, ("stat(%s)\n", fname));
+ DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
+
+ smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
- smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
+ /* What if the path is empty, or the file exists? */
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ return context->open(context, fname, O_WRONLY, 666);
- srv = smbc_server(server, share, lp_workgroup(), user, password);
+}
- if (!srv) {
+/*
+ * Routine to print a file on a remote server ...
+ *
+ * We open the file, which we assume to be on a remote server, and then
+ * copy it to a print file on the share specified by printq.
+ */
- return -1; /* errno set by smbc_server */
+static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
+{
+ SMBCFILE *fid1, *fid2;
+ int bytes, saverr, tot_bytes = 0;
+ char buf[4096];
- }
+ if (!c_file || !c_file->internal->_initialized || !c_print ||
+ !c_print->internal->_initialized) {
- /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
+ errno = EINVAL;
+ return -1;
- mode = aDIR | aRONLY;
+ }
- }
- else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
+ if (!fname && !printq) {
- if (strcmp(path, "\\") == 0) {
+ errno = EINVAL;
+ return -1;
- mode = aDIR | aRONLY;
+ }
- }
- else {
+ /* Try to open the file for reading ... */
- mode = aRONLY;
- smbc_stat_printjob(srv, path, &size, &m_time);
- c_time = a_time = m_time;
+ if ((int)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
+
+ DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
+ return -1; /* smbc_open sets errno */
+
+ }
- }
- else { */
+ /* Now, try to open the printer file for writing */
- if (!cli_rmdir(&srv->cli, path)) {
+ if ((int)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
- errno = smbc_errno(&srv->cli);
- return -1;
+ saverr = errno; /* Save errno */
+ c_file->close(c_file, fid1);
+ errno = saverr;
+ return -1;
+
+ }
- }
+ while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
+
+ tot_bytes += bytes;
+
+ if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
+
+ saverr = errno;
+ c_file->close(c_file, fid1);
+ c_print->close(c_print, fid2);
+ errno = saverr;
+
+ }
+
+ }
+
+ saverr = errno;
+
+ c_file->close(c_file, fid1); /* We have to close these anyway */
+ c_print->close(c_print, fid2);
+
+ if (bytes < 0) {
+
+ errno = saverr;
+ return -1;
+
+ }
- return 0;
+ return tot_bytes;
}
/*
- * Routine to return the current directory position
+ * Routine to list print jobs on a printer share ...
*/
-off_t smbc_telldir(int fd)
+static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
{
- struct smbc_file *fe;
+ SMBCSRV *srv;
+ fstring server, share, user, password, workgroup;
+ pstring path;
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (!fname) {
+
+ errno = EINVAL;
+ return -1;
- errno = EBADF;
- return -1;
+ }
+
+ DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
- }
+ smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
- fe = smbc_file_table[fd - smbc_start_fd];
+ if (user[0] == (char)0) fstrcpy(user, context->user);
+
+ fstrcpy(workgroup, context->workgroup);
- if (fe->file != False) { /* FIXME, should be dir, perhaps */
+ srv = smbc_server(context, server, share, workgroup, user, password);
- errno = ENOTDIR;
- return -1;
+ if (!srv) {
+
+ return -1; /* errno set by smbc_server */
+
+ }
- }
+ if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
- return (off_t) fe->dir_next;
+ errno = smbc_errno(context, &srv->cli);
+ return -1;
+
+ }
+
+ return 0;
}
/*
- * Routine to seek on a directory
+ * Delete a print job from a remote printer share
*/
-int smbc_lseekdir(int fd, off_t offset, int whence)
+static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
{
+ SMBCSRV *srv;
+ fstring server, share, user, password, workgroup;
+ pstring path;
+ int err;
- if (!smbc_initialized) {
+ if (!context || !context->internal ||
+ !context->internal->_initialized) {
- errno = EUCLEAN;
- return -1;
+ errno = EINVAL;
+ return -1;
+
+ }
+
+ if (!fname) {
+
+ errno = EINVAL;
+ return -1;
+
+ }
+
+ DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
- }
+ smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
- return 0;
+ if (user[0] == (char)0) fstrcpy(user, context->user);
+
+ fstrcpy(workgroup, context->workgroup);
+
+ srv = smbc_server(context, server, share, workgroup, user, password);
+
+ if (!srv) {
+
+ return -1; /* errno set by smbc_server */
+
+ }
+
+ if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
+
+ if (err < 0)
+ errno = smbc_errno(context, &srv->cli);
+ else if (err == ERRnosuchprintjob)
+ errno = EINVAL;
+ return -1;
+
+ }
+
+ return 0;
}
/*
- * Routine to fstat a dir
+ * Get a new empty handle to fill in with your own info
+ */
+SMBCCTX * smbc_new_context(void)
+{
+ SMBCCTX * context;
+
+ context = malloc(sizeof(SMBCCTX));
+ if (!context) {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ ZERO_STRUCTP(context);
+
+ context->internal = malloc(sizeof(struct smbc_internal_data));
+ if (!context->internal) {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ ZERO_STRUCTP(context->internal);
+
+
+ /* ADD REASONABLE DEFAULTS */
+ context->debug = 0;
+ context->timeout = 20000; /* 20 seconds */
+
+ context->open = smbc_open_ctx;
+ context->creat = smbc_creat_ctx;
+ context->read = smbc_read_ctx;
+ context->write = smbc_write_ctx;
+ context->close = smbc_close_ctx;
+ context->unlink = smbc_unlink_ctx;
+ context->rename = smbc_rename_ctx;
+ context->lseek = smbc_lseek_ctx;
+ context->stat = smbc_stat_ctx;
+ context->fstat = smbc_fstat_ctx;
+ context->opendir = smbc_opendir_ctx;
+ context->closedir = smbc_closedir_ctx;
+ context->readdir = smbc_readdir_ctx;
+ context->getdents = smbc_getdents_ctx;
+ context->mkdir = smbc_mkdir_ctx;
+ context->rmdir = smbc_rmdir_ctx;
+ context->telldir = smbc_telldir_ctx;
+ context->lseekdir = smbc_lseekdir_ctx;
+ context->fstatdir = smbc_fstatdir_ctx;
+ context->open_print_job = smbc_open_print_job_ctx;
+ context->print_file = smbc_print_file_ctx;
+ context->list_print_jobs = smbc_list_print_jobs_ctx;
+ context->unlink_print_job = smbc_unlink_print_job_ctx;
+
+ context->callbacks.check_server_fn = smbc_check_server;
+ context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
+
+ smbc_default_cache_functions(context);
+
+ return context;
+}
+
+/*
+ * Free a context
+ *
+ * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
+ * and thus you'll be leaking memory if not handled properly.
+ *
*/
+int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
+{
+ if (!context) {
+ errno = EBADF;
+ return 1;
+ }
+
+ if (shutdown_ctx) {
+ SMBCFILE * f;
+ DEBUG(1,("Performing aggressive shutdown.\n"));
+
+ f = context->internal->_files;
+ while (f) {
+ context->close(context, f);
+ f = f->next;
+ }
+ context->internal->_files = NULL;
+
+ /* First try to remove the servers the nice way. */
+ if (context->callbacks.purge_cached_fn(context)) {
+ SMBCSRV * s;
+ DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
+ s = context->internal->_servers;
+ while (s) {
+ cli_shutdown(&s->cli);
+ context->callbacks.remove_cached_srv_fn(context, s);
+ SAFE_FREE(s);
+ s = s->next;
+ }
+ context->internal->_servers = NULL;
+ }
+ }
+ else {
+ /* This is the polite way */
+ if (context->callbacks.purge_cached_fn(context)) {
+ DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
+ errno = EBUSY;
+ return 1;
+ }
+ if (context->internal->_servers) {
+ DEBUG(1, ("Active servers in context, free_context failed.\n"));
+ errno = EBUSY;
+ return 1;
+ }
+ if (context->internal->_files) {
+ DEBUG(1, ("Active files in context, free_context failed.\n"));
+ errno = EBUSY;
+ return 1;
+ }
+ }
+
+ /* Things we have to clean up */
+ SAFE_FREE(context->workgroup);
+ SAFE_FREE(context->netbios_name);
+ SAFE_FREE(context->user);
+
+ DEBUG(3, ("Context %p succesfully freed\n", context));
+ SAFE_FREE(context->internal);
+ SAFE_FREE(context);
+ return 0;
+}
-int smbc_fstatdir(int fd, struct stat *st)
+
+/*
+ * Initialise the library etc
+ *
+ * We accept a struct containing handle information.
+ * valid values for info->debug from 0 to 100,
+ * and insist that info->fn must be non-null.
+ */
+SMBCCTX * smbc_init_context(SMBCCTX * context)
{
+ pstring conf;
+ int pid;
+ char *user = NULL, *home = NULL;
- if (!smbc_initialized) {
+ if (!context || !context->internal) {
+ errno = EBADF;
+ return NULL;
+ }
- errno = EUCLEAN;
- return -1;
+ /* Do not initialise the same client twice */
+ if (context->internal->_initialized) {
+ return 0;
+ }
+
+ if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
+
+ errno = EINVAL;
+ return NULL;
+
+ }
+
+ if (!smbc_initialized) {
+ /* Do some library wide intialisations the first time we get called */
- }
+ /* Set this to what the user wants */
+ DEBUGLEVEL = context->debug;
+
+ setup_logging( "libsmbclient", True);
+
+ /* Here we would open the smb.conf file if needed ... */
+
+ home = getenv("HOME");
+
+ slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
+
+ load_interfaces(); /* Load the list of interfaces ... */
+
+ in_client = True; /* FIXME, make a param */
+
+ if (!lp_load(conf, True, False, False)) {
- return 0;
+ /*
+ * Well, if that failed, try the dyn_CONFIGFILE
+ * Which points to the standard locn, and if that
+ * fails, silently ignore it and use the internal
+ * defaults ...
+ */
+ if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
+ DEBUG(5, ("Could not load either config file: %s or %s\n",
+ conf, dyn_CONFIGFILE));
+ }
+ }
+
+ reopen_logs(); /* Get logging working ... */
+
+ /*
+ * Block SIGPIPE (from lib/util_sock.c: write())
+ * It is not needed and should not stop execution
+ */
+ BlockSignals(True, SIGPIPE);
+
+ /* Done with one-time initialisation */
+ smbc_initialized = 1;
+
+ }
+
+ if (!context->user) {
+ /*
+ * FIXME: Is this the best way to get the user info?
+ */
+ user = getenv("USER");
+ /* walk around as "guest" if no username can be found */
+ if (!user) context->user = strdup("guest");
+ else context->user = strdup(user);
+ }
+
+ if (!context->netbios_name) {
+ /*
+ * We try to get our netbios name from the config. If that fails we fall
+ * back on constructing our netbios name from our hostname etc
+ */
+ if (global_myname()) {
+ context->netbios_name = strdup(global_myname());
+ }
+ else {
+ /*
+ * Hmmm, I want to get hostname as well, but I am too lazy for the moment
+ */
+ pid = sys_getpid();
+ context->netbios_name = malloc(17);
+ if (!context->netbios_name) {
+ errno = ENOMEM;
+ return NULL;
+ }
+ slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
+ }
+ }
+
+ DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
+
+ if (!context->workgroup) {
+ if (lp_workgroup()) {
+ context->workgroup = strdup(lp_workgroup());
+ }
+ else {
+ /* TODO: Think about a decent default workgroup */
+ context->workgroup = strdup("samba");
+ }
+ }
+
+ DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
+
+ /* shortest timeout is 1 second */
+ if (context->timeout > 0 && context->timeout < 1000)
+ context->timeout = 1000;
+
+ /*
+ * FIXME: Should we check the function pointers here?
+ */
+
+ context->internal->_initialized = 1;
+
+ return context;
}