summaryrefslogtreecommitdiff
path: root/source3/libsmb
diff options
context:
space:
mode:
authorRichard Sharpe <sharpe@samba.org>2001-11-21 03:55:59 +0000
committerRichard Sharpe <sharpe@samba.org>2001-11-21 03:55:59 +0000
commit7c74cc5cab1d9db431197192a44b5fc08e0412f1 (patch)
tree40b70a435153d5d2323c3f3e07f74c42bf645b5b /source3/libsmb
parent96d884cc0f42ea694f7648ae12a3643bd1322a2b (diff)
downloadsamba-7c74cc5cab1d9db431197192a44b5fc08e0412f1.tar.gz
samba-7c74cc5cab1d9db431197192a44b5fc08e0412f1.tar.bz2
samba-7c74cc5cab1d9db431197192a44b5fc08e0412f1.zip
Fix up libsmbclient in head.
Apply the patches from Tom Jansen, get rid of fprintfs and change them to DEBUGs, etc ... (This used to be commit 7ac404c85303c9c3fbd48054fc4876bd4bc1567b)
Diffstat (limited to 'source3/libsmb')
-rw-r--r--source3/libsmb/libsmbclient.c2779
1 files changed, 1413 insertions, 1366 deletions
diff --git a/source3/libsmb/libsmbclient.c b/source3/libsmb/libsmbclient.c
index d6c37ff9b2..74a42990f7 100644
--- a/source3/libsmb/libsmbclient.c
+++ b/source3/libsmb/libsmbclient.c
@@ -29,31 +29,31 @@
*/
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;
+ 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_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;
+ 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;
};
int smbc_fstatdir(int fd, struct stat *st); /* Forward decl */
@@ -63,6 +63,7 @@ BOOL smbc_getatr(struct smbc_server *srv, char *path,
SMB_INO_T *ino);
extern BOOL in_client;
+extern pstring global_myname;
static int smbc_initialized = 0;
static smbc_get_auth_data_fn smbc_auth_fn = NULL;
/*static int smbc_debug;*/
@@ -89,108 +90,108 @@ static int
smbc_parse_path(const char *fname, char *server, char *share, char *path,
char *user, char *password) /* FIXME, lengths of strings */
{
- static pstring s;
- pstring userinfo;
- char *p;
- char *q, *r;
- int len;
+ static pstring s;
+ pstring userinfo;
+ char *p;
+ char *q, *r;
+ int len;
- server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
- pstrcpy(s, fname);
+ server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
+ pstrcpy(s, fname);
- /* clean_fname(s); causing problems ... */
+ /* clean_fname(s); causing problems ... */
- /* 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: ? */
+ /* 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: ? */
- p = s + len;
+ p = s + len;
- /* Watch the test below, we are testing to see if we should exit */
+ /* Watch the test below, we are testing to see if we should exit */
- if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
+ if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
- return -1;
+ return -1;
- }
-
- p += 2; /* Skip the // or \\ */
+ }
- if (*p == (char)0)
- return 0;
+ p += 2; /* Skip the // or \\ */
- if (*p == '/') {
+ if (*p == (char)0)
+ return 0;
- strncpy(server, (char *)lp_workgroup(), 16); /* FIXME: Danger here */
- return 0;
+ if (*p == '/') {
- }
+ strncpy(server, (char *)lp_workgroup(), 16); /* FIXME: Danger here */
+ 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 ...
+ */
- /* 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;
- 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;
+ 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_m(u, ';')) {
+ if (strchr_m(u, ';')) {
- next_token(&u, domain, ";", sizeof(fstring));
-
- }
-
- if (strchr_m(u, ':')) {
+ next_token(&u, domain, ";", sizeof(fstring));
+
+ }
- next_token(&u, username, ":", sizeof(fstring));
+ if (strchr_m(u, ':')) {
- pstrcpy(passwd, u);
+ next_token(&u, username, ":", sizeof(fstring));
- }
- else {
+ pstrcpy(passwd, u);
- pstrcpy(username, u);
+ }
+ else {
- }
+ pstrcpy(username, u);
- if (username[0])
- strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
+ }
- if (passwd[0])
- strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
+ if (username[0])
+ strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
- }
+ if (passwd[0])
+ strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
- if (!next_token(&p, server, "/", sizeof(fstring))) {
-
- return -1;
+ }
- }
+ if (!next_token(&p, server, "/", sizeof(fstring))) {
+
+ return -1;
+
+ }
- if (*p == (char)0) return 0; /* That's it ... */
+ if (*p == (char)0) return 0; /* 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;
+ return 0;
}
/*
@@ -238,297 +239,332 @@ struct smbc_server *smbc_server(char *server, char *share,
char *workgroup, char *username,
char *password)
{
- 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;
+ 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,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
+ 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,("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)) {
+ 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;
+ fstrcpy(group, server_n);
+ p = strchr_m(group,'#');
+ *p = 0;
- }
+ }
- DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
+ 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;
- }
+ 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"));
+ 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;
- }
+ 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"));
+ DEBUG(4,(" tconx ok\n"));
- srv = (struct smbc_server *)malloc(sizeof(*srv));
- if (!srv) {
- errno = ENOMEM;
- goto failed;
- }
+ srv = (struct smbc_server *)malloc(sizeof(*srv));
+ if (!srv) {
+ errno = ENOMEM;
+ goto failed;
+ }
- ZERO_STRUCTP(srv);
+ ZERO_STRUCTP(srv);
- srv->cli = c;
+ srv->cli = c;
- srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
+ srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
- srv->server_name = strdup(server);
- if (!srv->server_name) {
- errno = ENOMEM;
- goto failed;
- }
+ srv->server_name = strdup(server);
+ if (!srv->server_name) {
+ errno = ENOMEM;
+ goto failed;
+ }
- srv->share_name = strdup(share);
- if (!srv->share_name) {
- errno = ENOMEM;
- goto failed;
- }
+ srv->share_name = strdup(share);
+ if (!srv->share_name) {
+ errno = ENOMEM;
+ goto failed;
+ }
- srv->workgroup = strdup(workgroup);
- if (!srv->workgroup) {
- errno = ENOMEM;
- goto failed;
- }
+ srv->workgroup = strdup(workgroup);
+ if (!srv->workgroup) {
+ errno = ENOMEM;
+ goto failed;
+ }
- srv->username = strdup(username);
- if (!srv->username) {
- errno = ENOMEM;
- goto failed;
- }
+ srv->username = strdup(username);
+ if (!srv->username) {
+ errno = ENOMEM;
+ goto failed;
+ }
- DLIST_ADD(smbc_srvs, srv);
+ DLIST_ADD(smbc_srvs, srv);
- return srv;
+ return srv;
failed:
- cli_shutdown(&c);
- if (!srv) return NULL;
+ cli_shutdown(&c);
+ if (!srv) return NULL;
- SAFE_FREE(srv->server_name);
- SAFE_FREE(srv->share_name);
- SAFE_FREE(srv);
- return NULL;
+ SAFE_FREE(srv->server_name);
+ SAFE_FREE(srv->share_name);
+ SAFE_FREE(srv->workgroup);
+ SAFE_FREE(srv->username);
+ SAFE_FREE(srv);
+ return NULL;
+}
+
+/*
+ *Remove a server from the list smbc_srvs if it's unused -- Tom (tom@ninja.nl)
+ *
+ * We accept a *srv
+ */
+BOOL smbc_remove_unused_server(struct smbc_server * s)
+{
+ int p;
+
+ /* are we being fooled ? */
+ if (!s) return False;
+
+ /* close all open files/directories on this server */
+ for (p = 0; p < SMBC_MAX_FD; p++) {
+ if (smbc_file_table[p] &&
+ smbc_file_table[p]->srv == s) {
+ /* Still used .. DARN */
+ DEBUG(3, ("smbc_remove_usused_server: %x still used by %s (%d).\n", (int) s,
+ smbc_file_table[p]->fname, smbc_file_table[p]->smbc_fd));
+ return False;
+ }
+ }
+
+ cli_shutdown(&s->cli);
+
+ SAFE_FREE(s->username);
+ SAFE_FREE(s->workgroup);
+ SAFE_FREE(s->server_name);
+ SAFE_FREE(s->share_name);
+ DLIST_REMOVE(smbc_srvs, s);
+ DEBUG(3, ("smbc_remove_usused_server: %x removed.\n", (int) s));
+ SAFE_FREE(s);
+ return True;
}
/*
*Initialise the library etc
*
- * We accept valiv values for debug from 0 to 100,
+ * We accept valid values for debug from 0 to 100,
* and insist that fn must be non-null.
*/
int smbc_init(smbc_get_auth_data_fn fn, int debug)
{
- pstring conf;
- int p, pid;
- char *user = NULL, *home = NULL, *pname="libsmbclient";
-
- /*
- * Next lot ifdef'd out until test suite fixed ...
- */
-
- if (!fn || debug < 0 || debug > 100) {
-
- errno = EINVAL;
- return -1;
-
- }
+ pstring conf;
+ int p, pid;
+ char *user = NULL, *home = NULL, *pname="libsmbclient";
- if (smbc_initialized) { /* Don't go through this if we have already done it */
+ if (!fn || debug < 0 || debug > 100) {
- return 0;
+ errno = EINVAL;
+ return -1;
- }
-
- smbc_initialized = 1;
- smbc_auth_fn = fn;
- /* smbc_debug = debug; */
-
- DEBUGLEVEL = -1;
+ }
- setup_logging(pname, False);
+ if (smbc_initialized) { /* Don't go through this if we have already done it */
- /*
- * We try to construct our netbios name from our hostname etc
- */
+ return 0;
- user = getenv("USER");
- if (!user) user = ""; /* FIXME: What to do about this? */
+ }
- /*
- * FIXME: Is this the best way to get the user info? */
+ smbc_initialized = 1;
+ smbc_auth_fn = fn;
+ /* smbc_debug = debug; */
- pstrcpy(smbc_user, user); /* Save for use elsewhere */
+ DEBUGLEVEL = -1;
- pid = getpid();
+ setup_logging(pname, False);
- /*
- * Hmmm, I want to get hostname as well, but I am too lazy for the moment
- */
+ /* Here we would open the smb.conf file if needed ... */
- slprintf(my_netbios_name, 16, "smbc%s%d", user, pid);
+ home = getenv("HOME");
- /* Here we would open the smb.conf file if needed ... */
+ slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
- home = getenv("HOME");
+ load_interfaces(); /* Load the list of interfaces ... */
- slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
+ in_client = True; /* FIXME, make a param */
- load_interfaces(); /* Load the list of interfaces ... */
+ if (!lp_load(conf, True, False, False)) {
- in_client = True; /* FIXME, make a param */
+ /*
+ * 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; /* FIXME: Figure out the correct error response */
+ return -1;
- 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; /* FIXME: Figure out the correct error response */
- return -1;
+ reopen_logs(); /* Get logging working ... */
- }
+ /*
+ * FIXME: Is this the best way to get the user info?
+ */
- reopen_logs(); /* Get logging working ... */
+ user = getenv("USER");
+ /* walk around as "guest" if no username can be found */
+ if (!user) user = strdup("guest");
+ pstrcpy(smbc_user, user); /* Save for use elsewhere */
+
+ /*
+ * 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) {
+ pstrcpy(my_netbios_name, global_myname);
+ }
+ else {
+ /*
+ * Hmmm, I want to get hostname as well, but I am too lazy for the moment
+ */
+ pid = getpid();
+ slprintf(my_netbios_name, 16, "smbc%s%d", user, pid);
+ }
+ DEBUG(0,("Using netbios name %s.\n", my_netbios_name));
- name_register_wins(my_netbios_name, 0);
+ name_register_wins(my_netbios_name, 0);
- /*
- * 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
- */
+ /*
+ * 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;
+ do {
+ struct rlimit rlp;
- if (getrlimit(RLIMIT_NOFILE, &rlp)) {
+ if (getrlimit(RLIMIT_NOFILE, &rlp)) {
- DEBUG(0, ("smbc_init: getrlimit(1) for RLIMIT_NOFILE failed with error %s\n", strerror(errno)));
+ 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 ... */
+ smbc_start_fd = 1000000;
- }
- else {
+ }
+ else {
- smbc_start_fd = rlp.rlim_max + 10000; /* Leave a guard space of 10,000 */
- smbc_max_fd = 10000;
+ smbc_start_fd = rlp.rlim_max + 10000; /* Leave a guard space of 10,000 */
- }
- } while ( 0 );
+ }
+ } while ( 0 );
#else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
- smbc_start_fd = 1000000;
- smbc_max_fd = 10000; /* FIXME, should be a define ... */
+ smbc_start_fd = 1000000;
#endif
- smbc_file_table = malloc(smbc_max_fd * sizeof(struct smbc_file *));
+ smbc_file_table = malloc(SMBC_MAX_FD * sizeof(struct smbc_file *));
- for (p = 0; p < smbc_max_fd; p++)
- smbc_file_table[p] = NULL;
+ for (p = 0; p < SMBC_MAX_FD; p++)
+ smbc_file_table[p] = NULL;
- if (!smbc_file_table)
- return ENOMEM;
+ if (!smbc_file_table)
+ return ENOMEM;
- return 0; /* Success */
+ return 0; /* Success */
}
@@ -538,108 +574,108 @@ int smbc_init(smbc_get_auth_data_fn fn, int debug)
int smbc_open(const char *fname, int flags, mode_t mode)
{
- fstring server, share, user, password, workgroup;
- pstring path;
- struct smbc_server *srv = NULL;
- int fd;
+ fstring server, share, user, password, workgroup;
+ pstring path;
+ struct smbc_server *srv = NULL;
+ int fd;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL; /* 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(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) pstrcpy(user, smbc_user);
+ if (user[0] == (char)0) pstrcpy(user, smbc_user);
- pstrcpy(workgroup, lp_workgroup());
+ pstrcpy(workgroup, lp_workgroup());
- srv = smbc_server(server, share, workgroup, user, password);
+ srv = smbc_server(server, share, workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- if (errno == EPERM) errno = EACCES;
- return -1; /* smbc_server sets errno */
+ if (errno == EPERM) errno = EACCES;
+ return -1; /* smbc_server sets errno */
- }
+ }
- /* Hmmm, the test for a directory is suspect here ... FIXME */
+ /* Hmmm, the test for a directory is suspect here ... FIXME */
- if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
+ if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
- fd = -1;
+ fd = -1;
- }
- else {
+ }
+ else {
- int slot = 0;
+ int slot = 0;
- /* Find a free slot first */
+ /* Find a free slot first */
- while (smbc_file_table[slot])
- slot++;
+ while (smbc_file_table[slot])
+ slot++;
- if (slot > smbc_max_fd) {
+ if (slot > SMBC_MAX_FD) {
- errno = ENOMEM; /* FIXME, is this best? */
- return -1;
+ errno = ENOMEM; /* FIXME, is this best? */
+ return -1;
- }
+ }
- smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
+ smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
- if (!smbc_file_table[slot]) {
+ if (!smbc_file_table[slot]) {
- errno = ENOMEM;
- return -1;
+ errno = ENOMEM;
+ return -1;
- }
+ }
- if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
+ if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
- /* Handle the error ... */
+ /* Handle the error ... */
- SAFE_FREE(smbc_file_table[slot]);
- errno = smbc_errno(&srv->cli);
- return -1;
+ SAFE_FREE(smbc_file_table[slot]);
+ errno = smbc_errno(&srv->cli);
+ return -1;
- }
+ }
- /* Fill in file struct */
+ /* Fill in file struct */
- 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;
+ 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;
+ return smbc_file_table[slot]->smbc_fd;
- }
+ }
- /* Check if opendir needed ... */
+ /* Check if opendir needed ... */
- if (fd == -1) {
- int eno = 0;
+ if (fd == -1) {
+ int eno = 0;
- eno = smbc_errno(&srv->cli);
- fd = smbc_opendir(fname);
- if (fd < 0) errno = eno;
- return fd;
+ eno = smbc_errno(&srv->cli);
+ fd = smbc_opendir(fname);
+ if (fd < 0) errno = eno;
+ return fd;
- }
+ }
- return 1; /* Success, with fd ... */
+ return 1; /* Success, with fd ... */
}
@@ -652,14 +688,14 @@ static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this
int smbc_creat(const char *path, mode_t mode)
{
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- return smbc_open(path, creat_bits, mode);
+ return smbc_open(path, creat_bits, mode);
}
/*
@@ -668,57 +704,57 @@ int smbc_creat(const char *path, mode_t mode)
ssize_t smbc_read(int fd, void *buf, size_t count)
{
- struct smbc_file *fe;
- int ret;
+ struct smbc_file *fe;
+ int ret;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- DEBUG(4, ("smbc_read(%d, %d)\n", fd, (int)count));
+ DEBUG(4, ("smbc_read(%d, %d)\n", fd, (int)count));
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- /* Check that the buffer exists ... */
+ /* Check that the buffer exists ... */
- if (buf == NULL) {
+ if (buf == NULL) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe || !fe->file) {
+ if (!fe || !fe->file) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- ret = cli_read(&fe->srv->cli, fe->cli_fd, buf, fe->offset, count);
+ ret = cli_read(&fe->srv->cli, fe->cli_fd, buf, fe->offset, count);
- if (ret < 0) {
+ if (ret < 0) {
- errno = smbc_errno(&fe->srv->cli);
- return -1;
+ errno = smbc_errno(&fe->srv->cli);
+ return -1;
- }
+ }
- fe->offset += ret;
+ fe->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 ... */
}
@@ -728,53 +764,53 @@ ssize_t smbc_read(int fd, void *buf, size_t count)
ssize_t smbc_write(int fd, void *buf, size_t count)
{
- int ret;
- struct smbc_file *fe;
+ int ret;
+ struct smbc_file *fe;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- /* Check that the buffer exists ... */
+ /* Check that the buffer exists ... */
- if (buf == NULL) {
+ if (buf == NULL) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe || !fe->file) {
+ if (!fe || !fe->file) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- ret = cli_write(&fe->srv->cli, fe->cli_fd, 0, buf, fe->offset, count);
+ ret = cli_write(&fe->srv->cli, fe->cli_fd, 0, buf, fe->offset, count);
- if (ret <= 0) {
+ if (ret <= 0) {
- errno = smbc_errno(&fe->srv->cli);
- return -1;
+ errno = smbc_errno(&fe->srv->cli);
+ return -1;
- }
+ }
- fe->offset += ret;
+ fe->offset += ret;
- return ret; /* Success, 0 bytes of data ... */
+ return ret; /* Success, 0 bytes of data ... */
}
/*
@@ -783,49 +819,60 @@ ssize_t smbc_write(int fd, void *buf, size_t count)
int smbc_close(int fd)
{
- struct smbc_file *fe;
+ struct smbc_file *fe;
+ struct smbc_server *srv;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe) {
+ if (!fe) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- if (!fe->file) {
+ if (!fe->file) {
- return smbc_closedir(fd);
+ return smbc_closedir(fd);
- }
+ }
+
+ if (!cli_close(&fe->srv->cli, fe->cli_fd)) {
- if (!cli_close(&fe->srv->cli, fe->cli_fd)) {
+ DEBUG(3, ("cli_close failed on %s (%d). purging server.\n",
+ fe->fname, fe->smbc_fd));
+ /* Deallocate slot and remove the server
+ * from the server cache if unused */
+ errno = smbc_errno(&fe->srv->cli);
+ srv = fe->srv;
+ SAFE_FREE(fe->fname);
+ SAFE_FREE(fe);
+ smbc_file_table[fd - smbc_start_fd] = NULL;
+ smbc_remove_unused_server(srv);
- errno = smbc_errno(&fe->srv->cli); /* FIXME, should we deallocate slot? */
- return -1;
+ return -1;
- }
+ }
- SAFE_FREE(fe->fname);
- SAFE_FREE(fe);
- smbc_file_table[fd - smbc_start_fd] = NULL;
+ SAFE_FREE(fe->fname);
+ SAFE_FREE(fe);
+ smbc_file_table[fd - smbc_start_fd] = NULL;
- return 0;
+ return 0;
}
/*
@@ -834,90 +881,90 @@ int smbc_close(int fd)
int smbc_unlink(const char *fname)
{
- fstring server, share, user, password, workgroup;
- pstring path;
- struct smbc_server *srv = NULL;
+ fstring server, share, user, password, workgroup;
+ pstring path;
+ struct smbc_server *srv = NULL;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL; /* 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(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) pstrcpy(user, smbc_user);
+ if (user[0] == (char)0) pstrcpy(user, smbc_user);
- pstrcpy(workgroup, lp_workgroup());
+ pstrcpy(workgroup, lp_workgroup());
- srv = smbc_server(server, share, workgroup, user, password);
+ srv = smbc_server(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) {
+ int job = smbc_stat_printjob(srv, path, NULL, NULL);
+ if (job == -1) {
- return -1;
+ return -1;
- }
- if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
+ }
+ if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
- return -1;
+ return -1;
- }
- } else */
+ }
+ } else */
- if (!cli_unlink(&srv->cli, path)) {
+ if (!cli_unlink(&srv->cli, path)) {
- errno = smbc_errno(&srv->cli);
+ errno = smbc_errno(&srv->cli);
- if (errno == EACCES) { /* Check if the file is a directory */
+ if (errno == EACCES) { /* Check if the file is a directory */
- 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;
+ 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(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(&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 ... */
}
@@ -927,67 +974,67 @@ int smbc_unlink(const char *fname)
int smbc_rename(const char *oname, const char *nname)
{
- fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
- pstring path1, path2;
- struct smbc_server *srv = NULL;
+ fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
+ pstring path1, path2;
+ struct smbc_server *srv = NULL;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL; /* Best I can think of ... */
- return -1;
+ 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(oname, server1, share1, path1, user1, password1);
- if (user1[0] == (char)0) pstrcpy(user1, smbc_user);
+ if (user1[0] == (char)0) pstrcpy(user1, smbc_user);
- smbc_parse_path(nname, server2, share2, path2, user2, password2);
+ smbc_parse_path(nname, server2, share2, path2, user2, password2);
- if (user2[0] == (char)0) pstrcpy(user2, smbc_user);
+ if (user2[0] == (char)0) pstrcpy(user2, smbc_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;
- }
+ }
- pstrcpy(workgroup, lp_workgroup());
+ pstrcpy(workgroup, lp_workgroup());
- srv = smbc_server(server1, share1, workgroup, user1, password1);
- if (!srv) {
+ srv = smbc_server(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(&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 */
}
@@ -997,67 +1044,67 @@ int smbc_rename(const char *oname, const char *nname)
off_t smbc_lseek(int fd, off_t offset, int whence)
{
- struct smbc_file *fe;
- size_t size;
+ struct smbc_file *fe;
+ size_t size;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe) {
+ if (!fe) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- if (!fe->file) {
+ if (!fe->file) {
- errno = EINVAL;
- return -1; /* Can't lseek a dir ... */
+ errno = EINVAL;
+ return -1; /* Can't lseek a dir ... */
- }
+ }
- switch (whence) {
- case SEEK_SET:
- fe->offset = offset;
- break;
+ switch (whence) {
+ case SEEK_SET:
+ fe->offset = offset;
+ break;
- case SEEK_CUR:
- fe->offset += offset;
- break;
+ 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)) {
+ 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)) {
- errno = EINVAL;
- return -1;
- }
- fe->offset = size + offset;
- break;
+ errno = EINVAL;
+ return -1;
+ }
+ fe->offset = size + offset;
+ break;
- default:
- errno = EINVAL;
- break;
+ default:
+ errno = EINVAL;
+ break;
- }
+ }
- return fe->offset;
+ return fe->offset;
}
@@ -1069,8 +1116,8 @@ 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);
}
@@ -1083,36 +1130,36 @@ 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_mode = 0;
- 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_mode = SMBC_DIR_MODE;
+ } else {
+ st->st_mode = SMBC_FILE_MODE;
+ }
- if (IS_DOS_DIR(mode)) {
- st->st_nlink = 2;
- } else {
- st->st_nlink = 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(fname);
- }
+ if (st->st_ino == 0) {
+ st->st_ino = smbc_inode(fname);
+ }
- return True; /* FIXME: Is this needed ? */
+ return True; /* FIXME: Is this needed ? */
}
@@ -1127,28 +1174,28 @@ BOOL smbc_getatr(struct smbc_server *srv, char *path,
SMB_INO_T *ino)
{
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
+ 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;
- }
- return False;
+ 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;
}
/*
@@ -1157,85 +1204,85 @@ BOOL smbc_getatr(struct smbc_server *srv, char *path,
int smbc_stat(const char *fname, struct stat *st)
{
- struct smbc_server *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;
+ struct smbc_server *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 (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL; /* 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;
- }
+ }
- DEBUG(4, ("smbc_stat(%s)\n", fname));
+ DEBUG(4, ("smbc_stat(%s)\n", fname));
- smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
+ smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ if (user[0] == (char)0) pstrcpy(user, smbc_user);
- pstrcpy(workgroup, lp_workgroup());
+ pstrcpy(workgroup, lp_workgroup());
- srv = smbc_server(server, share, workgroup, user, password);
+ srv = smbc_server(server, share, workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- return -1; /* errno set by smbc_server */
+ return -1; /* errno set by smbc_server */
- }
+ }
- /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
+ /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
- mode = aDIR | aRONLY;
+ mode = aDIR | aRONLY;
- }
- else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
+ }
+ else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
- if (strcmp(path, "\\") == 0) {
+ if (strcmp(path, "\\") == 0) {
- mode = aDIR | aRONLY;
+ mode = aDIR | aRONLY;
- }
- else {
+ }
+ else {
- mode = aRONLY;
- smbc_stat_printjob(srv, path, &size, &m_time);
- c_time = a_time = m_time;
+ mode = aRONLY;
+ smbc_stat_printjob(srv, path, &size, &m_time);
+ c_time = a_time = m_time;
- }
- else { */
+ }
+ else { */
- if (!smbc_getatr(srv, path, &mode, &size,
- &c_time, &a_time, &m_time, &ino)) {
+ if (!smbc_getatr(srv, path, &mode, &size,
+ &c_time, &a_time, &m_time, &ino)) {
- errno = smbc_errno(&srv->cli);
- return -1;
+ errno = smbc_errno(&srv->cli);
+ return -1;
- }
+ }
- /* } */
+ /* } */
- st->st_ino = ino;
+ st->st_ino = ino;
- smbc_setup_stat(st, path, size, mode);
+ smbc_setup_stat(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;
}
@@ -1245,61 +1292,61 @@ int smbc_stat(const char *fname, struct stat *st)
int smbc_fstat(int fd, 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;
+ struct smbc_file *fe;
+ time_t c_time, a_time, m_time;
+ size_t size;
+ uint16 mode;
+ SMB_INO_T ino = 0;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe) {
+ if (!fe) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- if (!fe->file) {
+ if (!fe->file) {
- return smbc_fstatdir(fd, st);
+ return smbc_fstatdir(fd, 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(&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)) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- st->st_ino = ino;
+ st->st_ino = ino;
- smbc_setup_stat(st, fe->fname, size, mode);
+ smbc_setup_stat(st, fe->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 = fe->srv->dev;
- return 0;
+ return 0;
}
@@ -1313,136 +1360,139 @@ int smbc_fstat(int fd, struct stat *st)
* 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)
{
- 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;
- SAFE_FREE(f->dirent);
- SAFE_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)
{
- struct smbc_dirent *dirent;
- int size;
+ struct smbc_dirent *dirent;
+ int size;
- /*
- * 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.
- */
+ /*
+ * 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) + (name?strlen(name):0) +
- (comment?strlen(comment):0) + 1;
+ size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
+ (comment?strlen(comment):0) + 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) {
+ if (dir->dir_list == NULL) {
- dir->dir_list = malloc(sizeof(struct smbc_dir_list));
- if (!dir->dir_list) {
+ dir->dir_list = malloc(sizeof(struct smbc_dir_list));
+ if (!dir->dir_list) {
- SAFE_FREE(dirent);
- dir->dir_error = ENOMEM;
- return -1;
+ SAFE_FREE(dirent);
+ dir->dir_error = ENOMEM;
+ return -1;
- }
+ }
- dir->dir_end = dir->dir_next = dir->dir_list;
+ dir->dir_end = dir->dir_next = dir->dir_list;
- }
- else {
+ }
+ else {
- dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
+ dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
- if (!dir->dir_end) {
+ if (!dir->dir_end) {
- SAFE_FREE(dirent);
- dir->dir_error = ENOMEM;
- return -1;
+ SAFE_FREE(dirent);
+ dir->dir_error = ENOMEM;
+ return -1;
- }
+ }
- dir->dir_end = dir->dir_end->next;
+ dir->dir_end = dir->dir_end->next;
- }
+ }
- dir->dir_end->next = NULL;
- dir->dir_end->dirent = dirent;
+ 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;
+ dirent->smbc_type = type;
+ dirent->namelen = (name?strlen(name):0);
+ dirent->commentlen = (comment?strlen(comment):0);
+ dirent->dirlen = size;
- strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
+ strncpy(dirent->name, (name?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, (comment?comment:""), dirent->commentlen + 1);
- 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;
+ struct smbc_file *dir = (struct smbc_file *)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;
+ switch (type) {
+ case 0: /* Directory tree */
+ dirent_type = SMBC_FILE_SHARE;
+ break;
- case 1:
- dirent_type = SMBC_PRINTER_SHARE;
- break;
+ case 1:
+ dirent_type = SMBC_PRINTER_SHARE;
+ break;
- case 2:
- dirent_type = SMBC_COMMS_SHARE;
- break;
+ case 2:
+ dirent_type = SMBC_COMMS_SHARE;
+ break;
- case 3:
- dirent_type = SMBC_IPC_SHARE;
- break;
+ case 3:
+ dirent_type = SMBC_IPC_SHARE;
+ break;
- default:
- dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
- break;
- }
+ default:
+ dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
+ break;
+ }
- }
- else dirent_type = dir->dir_type;
+ }
+ else dirent_type = dir->dir_type;
- if (add_dirent(dir, name, comment, dirent_type) < 0) {
+ if (add_dirent(dir, name, comment, dirent_type) < 0) {
- /* An error occurred, what do we do? */
+ /* An error occurred, what do we do? */
+ /* FIXME: Add some code here */
- }
+ }
}
@@ -1450,312 +1500,309 @@ static void
dir_list_fn(file_info *finfo, const char *mask, void *state)
{
- /* fprintf(stderr, "Finfo->name=%s, mask=%s, mode=%0X\n", finfo->name, mask, finfo->mode);*/
- if (add_dirent((struct smbc_file *)state, finfo->name, "",
- (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
+ if (add_dirent((struct smbc_file *)state, finfo->name, "",
+ (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
- /* Handle an error ... */
+ /* Handle an error ... */
+ /* FIXME: Add some code ... */
- }
+ }
}
int smbc_opendir(const char *fname)
{
- fstring server, share, user, password, workgroup;
- pstring path;
- struct smbc_server *srv = NULL;
- struct in_addr rem_ip;
- int slot = 0;
+ fstring server, share, user, password, workgroup;
+ pstring path;
+ struct smbc_server *srv = NULL;
+ struct in_addr rem_ip;
+ int slot = 0;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (!fname) {
+ if (!fname) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (smbc_parse_path(fname, server, share, path, user, password)) {
+ if (smbc_parse_path(fname, server, share, path, user, password)) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ if (user[0] == (char)0) pstrcpy(user, smbc_user);
- pstrcpy(workgroup, lp_workgroup());
+ pstrcpy(workgroup, lp_workgroup());
- /* Get a file entry ... */
+ /* Get a file entry ... */
- slot = 0;
+ slot = 0;
- while (smbc_file_table[slot])
- slot++;
+ while (smbc_file_table[slot])
+ slot++;
- if (slot > smbc_max_fd) {
+ if (slot > SMBC_MAX_FD) {
- errno = ENOMEM;
- return -1; /* FIXME, ... move into a func */
+ errno = ENOMEM;
+ return -1; /* FIXME, ... move into a func */
- }
+ }
- smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
+ smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
- if (!smbc_file_table[slot]) {
+ if (!smbc_file_table[slot]) {
- errno = ENOMEM;
- return -1;
+ errno = ENOMEM;
+ return -1;
- }
+ }
- 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;
+ 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) {
+ if (server[0] == (char)0) {
- if (share[0] != (char)0 || path[0] != (char)0) {
+ if (share[0] != (char)0 || path[0] != (char)0) {
- errno = EINVAL;
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- return -1;
-
- }
+ errno = EINVAL;
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ return -1;
- /* We have server and share and path empty ... so list the workgroups */
+ }
- /*cli_get_backup_server(my_netbios_name, workgroup, server, sizeof(server));*/
+ /* We have server and share and path empty ... so list the workgroups */
- if (!resolve_name(lp_workgroup(), &rem_ip, 0x1d)) {
+ if (!resolve_name(lp_workgroup(), &rem_ip, 0x1d)) {
- errno = EINVAL; /* Something wrong with smb.conf? */
- return -1;
+ errno = EINVAL; /* Something wrong with smb.conf? */
+ return -1;
- }
+ }
- smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;
+ smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;
- /* find the name of the server ... */
+ /* find the name of the server ... */
- if (!name_status_find(0, rem_ip, server)) {
+ if (!name_status_find(0, rem_ip, server)) {
- fprintf(stderr, "Could not get the name of local master browser ...\n");
- errno = EINVAL;
- return -1;
+ DEBUG(0, ("Could not get the name of local master browser for server %s\n", server);
+ errno = EINVAL;
+ return -1;
- }
+ }
- /*
- * Get a connection to IPC$ on the server if we do not already have one
- */
+ /*
+ * Get a connection to IPC$ on the server if we do not already have one
+ */
- srv = smbc_server(server, "IPC$", workgroup, user, password);
+ srv = smbc_server(server, "IPC$", workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- return -1;
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ return -1;
- }
+ }
- smbc_file_table[slot]->srv = srv;
+ smbc_file_table[slot]->srv = srv;
- /* Now, list the stuff ... */
+ /* Now, list the stuff ... */
- if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
- (void *)smbc_file_table[slot])) {
+ if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
+ (void *)smbc_file_table[slot])) {
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- errno = cli_errno(&srv->cli);
- return -1;
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ errno = cli_errno(&srv->cli);
+ return -1;
- }
- }
- else { /* Server not an empty string ... Check the rest and see what gives */
+ }
+ }
+ else { /* Server not an empty string ... Check the rest and see what gives */
- if (share[0] == (char)0) {
+ if (share[0] == (char)0) {
- if (path[0] != (char)0) { /* Should not have empty share with path */
+ if (path[0] != (char)0) { /* Should not have empty share with path */
- errno = EINVAL;
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- return -1;
+ errno = EINVAL;
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ return -1;
- }
+ }
- /* Check to see if <server><1D> translates, or <server><20> translates */
- /* However, we check to see if <server> is an IP address first */
+ /* Check to see if <server><1D> translates, or <server><20> translates */
+ /* However, we check to see if <server> is an IP address first */
- if (!is_ipaddress(server) && /* Not an IP addr so check next */
- resolve_name(server, &rem_ip, 0x1d)) { /* Found LMB */
- pstring buserver;
+ if (!is_ipaddress(server) && /* Not an IP addr so check next */
+ resolve_name(server, &rem_ip, 0x1d)) { /* Found LMB */
+ pstring buserver;
- smbc_file_table[slot]->dir_type = SMBC_SERVER;
+ smbc_file_table[slot]->dir_type = SMBC_SERVER;
- /*
- * Get the backup list ...
- */
+ /*
+ * Get the backup list ...
+ */
- /*cli_get_backup_server(my_netbios_name, server, buserver, sizeof(buserver)); */
- if (!name_status_find(0, rem_ip, buserver)) {
+ if (!name_status_find(0, rem_ip, buserver)) {
- fprintf(stderr, "Could not get name of local master browser ...\n");
- errno = EPERM; /* FIXME, is this correct */
- return -1;
+ DEBUG(0, ("Could not get name of local master browser %s\n", server));
+ errno = EPERM; /* FIXME, is this correct */
+ return -1;
- }
+ }
- /*
- * Get a connection to IPC$ on the server if we do not already have one
- */
+ /*
+ * Get a connection to IPC$ on the server if we do not already have one
+ */
- srv = smbc_server(buserver, "IPC$", workgroup, user, password);
+ srv = smbc_server(buserver, "IPC$", workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- return -1;
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ return -1;
- }
+ }
- smbc_file_table[slot]->srv = srv;
+ smbc_file_table[slot]->srv = srv;
- /* Now, list the servers ... */
+ /* Now, list the servers ... */
- if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
- (void *)smbc_file_table[slot])) {
+ if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
+ (void *)smbc_file_table[slot])) {
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- errno = cli_errno(&srv->cli);
- return -1;
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ errno = cli_errno(&srv->cli);
+ return -1;
- }
+ }
- }
- else {
+ }
+ else {
- if (resolve_name(server, &rem_ip, 0x20)) {
+ if (resolve_name(server, &rem_ip, 0x20)) {
- /* Now, list the shares ... */
+ /* Now, list the shares ... */
- smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
+ smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
- srv = smbc_server(server, "IPC$", workgroup, user, password);
+ srv = smbc_server(server, "IPC$", workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- return -1;
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ return -1;
- }
+ }
- smbc_file_table[slot]->srv = srv;
+ smbc_file_table[slot]->srv = srv;
- /* Now, list the servers ... */
+ /* Now, list the servers ... */
- if (cli_RNetShareEnum(&srv->cli, list_fn,
- (void *)smbc_file_table[slot]) < 0) {
+ if (cli_RNetShareEnum(&srv->cli, list_fn,
+ (void *)smbc_file_table[slot]) < 0) {
- errno = cli_errno(&srv->cli);
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- return -1;
+ errno = cli_errno(&srv->cli);
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ return -1;
- }
+ }
- }
- else {
+ }
+ else {
- errno = ENODEV; /* Neither the workgroup nor server exists */
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- return -1;
+ errno = ENODEV; /* Neither the workgroup nor server exists */
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ return -1;
- }
+ }
- }
+ }
- }
- else { /* The server and share are specified ... work from there ... */
+ }
+ else { /* The server and share are specified ... work from there ... */
- /* Well, we connect to the server and list the directory */
+ /* Well, we connect to the server and list the directory */
- smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
+ smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
- srv = smbc_server(server, share, workgroup, user, password);
+ srv = smbc_server(server, share, workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- return -1;
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ return -1;
- }
+ }
- smbc_file_table[slot]->srv = srv;
+ smbc_file_table[slot]->srv = srv;
- /* Now, list the files ... */
+ /* Now, list the files ... */
- pstrcat(path, "\\*");
+ pstrcat(path, "\\*");
- if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
- (void *)smbc_file_table[slot]) < 0) {
+ if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
+ (void *)smbc_file_table[slot]) < 0) {
- if (smbc_file_table[slot]) {
- SAFE_FREE(smbc_file_table[slot]->fname);
- SAFE_FREE(smbc_file_table[slot]);
- }
- errno = smbc_errno(&srv->cli);
- return -1;
+ if (smbc_file_table[slot]) {
+ SAFE_FREE(smbc_file_table[slot]->fname);
+ SAFE_FREE(smbc_file_table[slot]);
+ }
+ errno = smbc_errno(&srv->cli);
+ return -1;
- }
- }
+ }
+ }
- }
+ }
- return smbc_file_table[slot]->smbc_fd;
+ return smbc_file_table[slot]->smbc_fd;
}
@@ -1765,43 +1812,43 @@ int smbc_opendir(const char *fname)
int smbc_closedir(int fd)
{
- struct smbc_file *fe;
+ struct smbc_file *fe;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe) {
+ if (!fe) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- smbc_remove_dir(fe); /* Clean it up */
+ smbc_remove_dir(fe); /* Clean it up */
- if (fe) {
+ if (fe) {
- SAFE_FREE(fe->fname);
- SAFE_FREE(fe); /* Free the space too */
+ SAFE_FREE(fe->fname);
+ SAFE_FREE(fe); /* Free the space too */
- }
+ }
- smbc_file_table[fd - smbc_start_fd] = NULL;
+ smbc_file_table[fd - smbc_start_fd] = NULL;
- return 0;
+ return 0;
}
@@ -1813,66 +1860,66 @@ static char smbc_local_dirent[512]; /* Make big enough */
struct smbc_dirent *smbc_readdir(unsigned int fd)
{
- struct smbc_file *fe;
- struct smbc_dirent *dirp, *dirent;
+ struct smbc_file *fe;
+ struct smbc_dirent *dirp, *dirent;
- /* Check that all is ok first ... */
+ /* Check that all is ok first ... */
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return NULL;
+ errno = EINVAL;
+ return NULL;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return NULL;
+ errno = EBADF;
+ return NULL;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe) {
+ if (!fe) {
- errno = EBADF;
- return NULL;
+ errno = EBADF;
+ return NULL;
- }
+ }
- if (fe->file != False) { /* FIXME, should be dir, perhaps */
+ if (fe->file != False) { /* FIXME, should be dir, perhaps */
- errno = ENOTDIR;
- return NULL;
+ errno = ENOTDIR;
+ return NULL;
- }
+ }
- if (!fe->dir_next)
- return NULL;
- else {
+ if (!fe->dir_next)
+ return NULL;
+ else {
- dirent = fe->dir_next->dirent;
+ dirent = fe->dir_next->dirent;
- if (!dirent) {
+ if (!dirent) {
- errno = ENOENT;
- return NULL;
+ errno = ENOENT;
+ return NULL;
- }
+ }
- /* Hmmm, do I even need to copy it? */
+ /* Hmmm, do I even need to copy it? */
- bcopy(dirent, smbc_local_dirent, dirent->dirlen); /* Copy the dirent */
+ bcopy(dirent, smbc_local_dirent, dirent->dirlen); /* Copy the dirent */
- dirp = (struct smbc_dirent *)smbc_local_dirent;
+ dirp = (struct smbc_dirent *)smbc_local_dirent;
- dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
+ dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
- fe->dir_next = fe->dir_next->next;
+ fe->dir_next = fe->dir_next->next;
- return (struct smbc_dirent *)smbc_local_dirent;
- }
+ return (struct smbc_dirent *)smbc_local_dirent;
+ }
}
@@ -1882,95 +1929,95 @@ struct smbc_dirent *smbc_readdir(unsigned int fd)
int smbc_getdents(unsigned int fd, struct smbc_dirent *dirp, int count)
{
- struct smbc_file *fe;
- struct smbc_dir_list *dir;
- int rem = count, reqd;
- char *ndir = (char *)dirp;
+ struct smbc_file *fe;
+ struct smbc_dir_list *dir;
+ int rem = count, reqd;
+ char *ndir = (char *)dirp;
- /* Check that all is ok first ... */
+ /* Check that all is ok first ... */
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe) {
+ if (!fe) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- if (fe->file != False) { /* FIXME, should be dir, perhaps */
+ if (fe->file != False) { /* FIXME, should be dir, perhaps */
- errno = ENOTDIR;
- return -1;
+ errno = ENOTDIR;
+ 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.
- */
+ /*
+ * 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 ((dir = fe->dir_next)) {
- struct smbc_dirent *dirent;
+ while ((dir = fe->dir_next)) {
+ struct smbc_dirent *dirent;
- if (!dir->dirent) {
+ if (!dir->dirent) {
- errno = ENOENT; /* Bad error */
- return -1;
+ errno = ENOENT; /* Bad error */
+ return -1;
- }
+ }
- if (rem < (reqd = (sizeof(struct smbc_dirent) + dir->dirent->namelen +
- dir->dirent->commentlen + 1))) {
+ if (rem < (reqd = (sizeof(struct smbc_dirent) + dir->dirent->namelen +
+ dir->dirent->commentlen + 1))) {
- if (rem < count) { /* We managed to copy something */
+ if (rem < count) { /* We managed to copy something */
- errno = 0;
- return count - rem;
+ errno = 0;
+ return count - rem;
- }
- else { /* Nothing copied ... */
+ }
+ else { /* Nothing copied ... */
- errno = EINVAL; /* Not enough space ... */
- return -1;
+ errno = EINVAL; /* Not enough space ... */
+ return -1;
- }
+ }
- }
+ }
- dirent = dir->dirent;
+ dirent = dir->dirent;
- bcopy(dirent, ndir, reqd); /* Copy the data in ... */
+ bcopy(dirent, ndir, reqd); /* Copy the data in ... */
- ((struct smbc_dirent *)ndir)->comment =
- (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
+ ((struct smbc_dirent *)ndir)->comment =
+ (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
- ndir += reqd;
+ ndir += reqd;
- rem -= reqd;
+ rem -= reqd;
- fe->dir_next = dir = dir -> next;
- }
+ fe->dir_next = dir = dir -> next;
+ }
- if (rem == count)
- return 0;
- else
- return count - rem;
+ if (rem == count)
+ return 0;
+ else
+ return count - rem;
}
@@ -1980,69 +2027,69 @@ int smbc_getdents(unsigned int fd, struct smbc_dirent *dirp, int count)
int smbc_mkdir(const char *fname, mode_t mode)
{
- struct smbc_server *srv;
- fstring server, share, user, password, workgroup;
- pstring path;
+ struct smbc_server *srv;
+ fstring server, share, user, password, workgroup;
+ pstring path;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (!fname) {
+ if (!fname) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- DEBUG(4, ("smbc_mkdir(%s)\n", fname));
+ DEBUG(4, ("smbc_mkdir(%s)\n", fname));
- smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
+ smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ if (user[0] == (char)0) pstrcpy(user, smbc_user);
- pstrcpy(workgroup, lp_workgroup());
+ pstrcpy(workgroup, lp_workgroup());
- srv = smbc_server(server, share, workgroup, user, password);
+ srv = smbc_server(server, share, workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- return -1; /* errno set by smbc_server */
+ return -1; /* errno set by smbc_server */
- }
+ }
- /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
+ /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
- mode = aDIR | aRONLY;
+ mode = aDIR | aRONLY;
- }
- else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
+ }
+ else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
- if (strcmp(path, "\\") == 0) {
+ if (strcmp(path, "\\") == 0) {
- mode = aDIR | aRONLY;
+ mode = aDIR | aRONLY;
- }
- else {
+ }
+ else {
- mode = aRONLY;
- smbc_stat_printjob(srv, path, &size, &m_time);
- c_time = a_time = m_time;
+ mode = aRONLY;
+ smbc_stat_printjob(srv, path, &size, &m_time);
+ c_time = a_time = m_time;
- }
- else { */
+ }
+ else { */
- if (!cli_mkdir(&srv->cli, path)) {
+ if (!cli_mkdir(&srv->cli, path)) {
- errno = smbc_errno(&srv->cli);
- return -1;
+ errno = smbc_errno(&srv->cli);
+ return -1;
- }
+ }
- return 0;
+ return 0;
}
@@ -2055,8 +2102,8 @@ static int smbc_rmdir_dirempty = True;
static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
{
- if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
- smbc_rmdir_dirempty = False;
+ if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
+ smbc_rmdir_dirempty = False;
}
@@ -2066,97 +2113,97 @@ static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
int smbc_rmdir(const char *fname)
{
- struct smbc_server *srv;
- fstring server, share, user, password, workgroup;
- pstring path;
+ struct smbc_server *srv;
+ fstring server, share, user, password, workgroup;
+ pstring path;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (!fname) {
+ if (!fname) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- DEBUG(4, ("smbc_rmdir(%s)\n", fname));
+ DEBUG(4, ("smbc_rmdir(%s)\n", fname));
- smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
+ smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ if (user[0] == (char)0) pstrcpy(user, smbc_user);
- pstrcpy(workgroup, lp_workgroup());
+ pstrcpy(workgroup, lp_workgroup());
- srv = smbc_server(server, share, workgroup, user, password);
+ srv = smbc_server(server, share, workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- return -1; /* errno set by smbc_server */
+ return -1; /* errno set by smbc_server */
- }
+ }
- /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
+ /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
- mode = aDIR | aRONLY;
+ mode = aDIR | aRONLY;
- }
- else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
+ }
+ else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
- if (strcmp(path, "\\") == 0) {
+ if (strcmp(path, "\\") == 0) {
- mode = aDIR | aRONLY;
+ mode = aDIR | aRONLY;
- }
- else {
+ }
+ else {
- mode = aRONLY;
- smbc_stat_printjob(srv, path, &size, &m_time);
- c_time = a_time = m_time;
+ mode = aRONLY;
+ smbc_stat_printjob(srv, path, &size, &m_time);
+ c_time = a_time = m_time;
- }
- else { */
+ }
+ else { */
- if (!cli_rmdir(&srv->cli, path)) {
+ if (!cli_rmdir(&srv->cli, path)) {
- errno = smbc_errno(&srv->cli);
+ errno = smbc_errno(&srv->cli);
- if (errno == EACCES) { /* Check if the dir empty or not */
+ if (errno == EACCES) { /* Check if the dir empty or not */
- pstring lpath; /* Local storage to avoid buffer overflows */
+ pstring lpath; /* Local storage to avoid buffer overflows */
- smbc_rmdir_dirempty = True; /* Make this so ... */
+ smbc_rmdir_dirempty = True; /* Make this so ... */
- pstrcpy(lpath, path);
- pstrcat(lpath, "\\*");
+ pstrcpy(lpath, path);
+ pstrcat(lpath, "\\*");
- if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
- NULL) < 0) {
+ if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
+ NULL) < 0) {
- /* Fix errno to ignore latest error ... */
+ /* Fix errno to ignore latest error ... */
- DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
- smbc_errno(&srv->cli)));
- errno = EACCES;
+ DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
+ smbc_errno(&srv->cli)));
+ errno = EACCES;
- }
+ }
- if (smbc_rmdir_dirempty)
- errno = EACCES;
- else
- errno = ENOTEMPTY;
+ if (smbc_rmdir_dirempty)
+ errno = EACCES;
+ else
+ errno = ENOTEMPTY;
- }
+ }
- return -1;
+ return -1;
- }
+ }
- return 0;
+ return 0;
}
@@ -2166,39 +2213,39 @@ int smbc_rmdir(const char *fname)
off_t smbc_telldir(int fd)
{
- struct smbc_file *fe;
+ struct smbc_file *fe;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe) {
+ if (!fe) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- if (fe->file != False) { /* FIXME, should be dir, perhaps */
+ if (fe->file != False) { /* FIXME, should be dir, perhaps */
- errno = ENOTDIR;
- return -1;
+ errno = ENOTDIR;
+ return -1;
- }
+ }
- return (off_t) fe->dir_next;
+ return (off_t) fe->dir_next;
}
@@ -2210,24 +2257,24 @@ struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
struct smbc_dirent *dirent)
{
- /* Run down the list looking for what we want */
+ /* Run down the list looking for what we want */
- if (dirent) {
+ if (dirent) {
- struct smbc_dir_list *tmp = list;
+ struct smbc_dir_list *tmp = list;
- while (tmp) {
+ while (tmp) {
- if (tmp->dirent == dirent)
- return tmp;
+ if (tmp->dirent == dirent)
+ return tmp;
- tmp = tmp->next;
+ tmp = tmp->next;
- }
+ }
- }
+ }
- return NULL; /* Not found, or an error */
+ return NULL; /* Not found, or an error */
}
@@ -2238,62 +2285,62 @@ struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
int smbc_lseekdir(int fd, off_t offset)
{
- struct smbc_file *fe;
- struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
- struct smbc_dir_list *list_ent = NULL;
+ struct smbc_file *fe;
+ struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
+ struct smbc_dir_list *list_ent = NULL;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
+ if (fd < smbc_start_fd || fd >= (smbc_start_fd + SMBC_MAX_FD)) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- fe = smbc_file_table[fd - smbc_start_fd];
+ fe = smbc_file_table[fd - smbc_start_fd];
- if (!fe) {
+ if (!fe) {
- errno = EBADF;
- return -1;
+ errno = EBADF;
+ return -1;
- }
+ }
- if (fe->file != False) { /* FIXME, should be dir, perhaps */
+ if (fe->file != False) { /* FIXME, should be dir, perhaps */
- errno = ENOTDIR;
- return -1;
+ errno = ENOTDIR;
+ return -1;
- }
+ }
- /* Now, check what we were passed and see if it is OK ... */
+ /* Now, check what we were passed and see if it is OK ... */
- if (dirent == NULL) { /* Seek to the begining of the list */
+ if (dirent == NULL) { /* Seek to the begining of the list */
- fe->dir_next = fe->dir_list;
- return 0;
+ fe->dir_next = fe->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 */
+ /* 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 */
- if ((list_ent = smbc_check_dir_ent(fe->dir_list, dirent)) == NULL) {
+ if ((list_ent = smbc_check_dir_ent(fe->dir_list, dirent)) == NULL) {
- errno = EINVAL; /* Bad entry */
- return -1;
+ errno = EINVAL; /* Bad entry */
+ return -1;
- }
+ }
- fe->dir_next = list_ent;
+ fe->dir_next = list_ent;
- return 0;
+ return 0;
}
@@ -2304,16 +2351,16 @@ int smbc_lseekdir(int fd, off_t offset)
int smbc_fstatdir(int fd, struct stat *st)
{
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- /* No code yet ... */
+ /* No code yet ... */
- return 0;
+ return 0;
}
@@ -2326,71 +2373,71 @@ int smbc_fstatdir(int fd, struct stat *st)
int smbc_print_file(const char *fname, const char *printq)
{
- int fid1, fid2, bytes, saverr, tot_bytes = 0;
- char buf[4096];
+ int fid1, fid2, bytes, saverr, tot_bytes = 0;
+ char buf[4096];
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (!fname && !printq) {
+ if (!fname && !printq) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- /* Try to open the file for reading ... */
+ /* Try to open the file for reading ... */
- if ((fid1 = smbc_open(fname, O_RDONLY, 0666)) < 0) {
+ if ((fid1 = smbc_open(fname, O_RDONLY, 0666)) < 0) {
- fprintf(stderr, "Error, fname=%s, errno=%i\n", fname, errno);
- return -1; /* smbc_open sets errno */
+ DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
+ return -1; /* smbc_open sets errno */
- }
+ }
- /* Now, try to open the printer file for writing */
+ /* Now, try to open the printer file for writing */
- if ((fid2 = smbc_open_print_job(printq)) < 0) {
+ if ((fid2 = smbc_open_print_job(printq)) < 0) {
- saverr = errno; /* Save errno */
- smbc_close(fid1);
- errno = saverr;
- return -1;
+ saverr = errno; /* Save errno */
+ smbc_close(fid1);
+ errno = saverr;
+ return -1;
- }
+ }
- while ((bytes = smbc_read(fid1, buf, sizeof(buf))) > 0) {
+ while ((bytes = smbc_read(fid1, buf, sizeof(buf))) > 0) {
- tot_bytes += bytes;
+ tot_bytes += bytes;
- if ((smbc_write(fid2, buf, bytes)) < 0) {
+ if ((smbc_write(fid2, buf, bytes)) < 0) {
- saverr = errno;
- smbc_close(fid1);
- smbc_close(fid2);
- errno = saverr;
+ saverr = errno;
+ smbc_close(fid1);
+ smbc_close(fid2);
+ errno = saverr;
- }
+ }
- }
+ }
- saverr = errno;
+ saverr = errno;
- smbc_close(fid1); /* We have to close these anyway */
- smbc_close(fid2);
+ smbc_close(fid1); /* We have to close these anyway */
+ smbc_close(fid2);
- if (bytes < 0) {
+ if (bytes < 0) {
- errno = saverr;
- return -1;
+ errno = saverr;
+ return -1;
- }
+ }
- return tot_bytes;
+ return tot_bytes;
}
@@ -2400,30 +2447,30 @@ int smbc_print_file(const char *fname, const char *printq)
int smbc_open_print_job(const char *fname)
{
- fstring server, share, user, password;
- pstring path;
+ fstring server, share, user, password;
+ pstring path;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (!fname) {
+ if (!fname) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- DEBUG(4, ("smbc_open_print_job(%s)\n", fname));
+ DEBUG(4, ("smbc_open_print_job(%s)\n", fname));
- smbc_parse_path(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? */
+ /* What if the path is empty, or the file exists? */
- return smbc_open(fname, O_WRONLY, 666);
+ return smbc_open(fname, O_WRONLY, 666);
}
@@ -2433,48 +2480,48 @@ int smbc_open_print_job(const char *fname)
int smbc_list_print_jobs(const char *fname, void (*fn)(struct print_job_info *))
{
- struct smbc_server *srv;
- fstring server, share, user, password, workgroup;
- pstring path;
+ struct smbc_server *srv;
+ fstring server, share, user, password, workgroup;
+ pstring path;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (!fname) {
+ if (!fname) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
+ DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
- smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
+ smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ if (user[0] == (char)0) pstrcpy(user, smbc_user);
- pstrcpy(workgroup, lp_workgroup());
+ pstrcpy(workgroup, lp_workgroup());
- srv = smbc_server(server, share, workgroup, user, password);
+ srv = smbc_server(server, share, workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- return -1; /* errno set by smbc_server */
+ return -1; /* errno set by smbc_server */
- }
+ }
- if (cli_print_queue(&srv->cli, fn) < 0) {
+ if (cli_print_queue(&srv->cli, fn) < 0) {
- errno = smbc_errno(&srv->cli);
- return -1;
+ errno = smbc_errno(&srv->cli);
+ return -1;
- }
+ }
- return 0;
+ return 0;
}
@@ -2484,53 +2531,53 @@ int smbc_list_print_jobs(const char *fname, void (*fn)(struct print_job_info *))
int smbc_unlink_print_job(const char *fname, int id)
{
- struct smbc_server *srv;
- fstring server, share, user, password, workgroup;
- pstring path;
- int err;
+ struct smbc_server *srv;
+ fstring server, share, user, password, workgroup;
+ pstring path;
+ int err;
- if (!smbc_initialized) {
+ if (!smbc_initialized) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- if (!fname) {
+ if (!fname) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
- }
+ }
- DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
+ DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
- smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
+ smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
- if (user[0] == (char)0) pstrcpy(user, smbc_user);
+ if (user[0] == (char)0) pstrcpy(user, smbc_user);
- pstrcpy(workgroup, lp_workgroup());
+ pstrcpy(workgroup, lp_workgroup());
- srv = smbc_server(server, share, workgroup, user, password);
+ srv = smbc_server(server, share, workgroup, user, password);
- if (!srv) {
+ if (!srv) {
- return -1; /* errno set by smbc_server */
+ return -1; /* errno set by smbc_server */
- }
+ }
- if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
+ if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
- if (err < 0)
- errno = smbc_errno(&srv->cli);
- else if (err == ERRnosuchprintjob)
- errno = EINVAL;
- return -1;
+ if (err < 0)
+ errno = smbc_errno(&srv->cli);
+ else if (err == ERRnosuchprintjob)
+ errno = EINVAL;
+ return -1;
- }
+ }
- return 0;
+ return 0;
}