Fixes to libsmbclient so it will work when browsing real Windows systems which
[Samba/gbeck.git] / source3 / libsmb / libsmbclient.c
blob67d5b921000091116e56d98e86fbacf766d1ba1e
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 SMB client library implementation
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Richard Sharpe 2000
7 Copyright (C) John Terpstra 2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
25 #include "libsmbclient.h"
27 /* Structure for servers ... Held here so we don't need an include ...
28 * May be better to put in an include file
31 struct smbc_server {
32 struct smbc_server *next, *prev;
33 struct cli_state cli;
34 dev_t dev;
35 char *server_name;
36 char *share_name;
37 char *workgroup;
38 char *username;
39 BOOL no_pathinfo2;
42 /* Keep directory entries in a list */
43 struct smbc_dir_list {
44 struct smbc_dir_list *next;
45 struct smbc_dirent *dirent;
48 struct smbc_file {
49 int cli_fd;
50 int smbc_fd;
51 char *fname;
52 off_t offset;
53 struct smbc_server *srv;
54 BOOL file;
55 struct smbc_dir_list *dir_list, *dir_end, *dir_next;
56 int dir_type, dir_error;
59 extern BOOL in_client;
60 static int smbc_initialized = 0;
61 static smbc_get_auth_data_fn smbc_auth_fn = NULL;
62 static int smbc_debug;
63 static int smbc_start_fd;
64 static int smbc_max_fd = 10000;
65 static struct smbc_file **smbc_file_table;
66 static struct smbc_server *smbc_srvs;
67 static pstring my_netbios_name;
68 static pstring smbc_user;
71 * Clean up a filename by removing redundent stuff
74 static void
75 smbc_clean_fname(char *name)
77 char *p, *p2;
78 int l;
79 int modified = 1;
81 if (!name) return;
83 while (modified) {
84 modified = 0;
86 DEBUG(5,("cleaning %s\n", name));
88 if ((p=strstr(name,"/./"))) {
89 modified = 1;
90 while (*p) {
91 p[0] = p[2];
92 p++;
96 if ((p=strstr(name,"//"))) {
97 modified = 1;
98 while (*p) {
99 p[0] = p[1];
100 p++;
104 if (strcmp(name,"/../")==0) {
105 modified = 1;
106 name[1] = 0;
109 if ((p=strstr(name,"/../"))) {
110 modified = 1;
111 for (p2=(p>name?p-1:p);p2>name;p2--) {
112 if (p2[0] == '/') break;
114 while (*p2) {
115 p2[0] = p2[3];
116 p2++;
120 if (strcmp(name,"/..")==0) {
121 modified = 1;
122 name[1] = 0;
125 l = strlen(name);
126 p = l>=3?(name+l-3):name;
127 if (strcmp(p,"/..")==0) {
128 modified = 1;
129 for (p2=p-1;p2>name;p2--) {
130 if (p2[0] == '/') break;
132 if (p2==name) {
133 p[0] = '/';
134 p[1] = 0;
135 } else {
136 p2[0] = 0;
140 l = strlen(name);
141 p = l>=2?(name+l-2):name;
142 if (strcmp(p,"/.")==0) {
143 if (p == name) {
144 p[1] = 0;
145 } else {
146 p[0] = 0;
150 if (strncmp(p=name,"./",2) == 0) {
151 modified = 1;
152 do {
153 p[0] = p[2];
154 } while (*p++);
157 l = strlen(p=name);
158 if (l > 1 && p[l-1] == '/') {
159 modified = 1;
160 p[l-1] = 0;
166 * Function to parse a path and turn it into components
168 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
170 * smb:// means show all the workgroups
171 * smb://name/ means, if name<1D> exists, list servers in workgroup,
172 * else, if name<20> exists, list all shares for server ...
175 static const char *smbc_prefix = "smb:";
177 static int
178 smbc_parse_path(const char *fname, char *server, char *share, char *path,
179 char *user, char *password) /* FIXME, lengths of strings */
181 static pstring s;
182 pstring userinfo;
183 char *p;
184 int len;
185 fstring workgroup;
187 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
188 pstrcpy(s, fname);
190 /* clean_fname(s); causing problems ... */
192 /* see if it has the right prefix */
193 len = strlen(smbc_prefix);
194 if (strncmp(s,smbc_prefix,len) ||
195 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
197 p = s + len;
199 /* Watch the test below, we are testing to see if we should exit */
201 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
203 return -1;
207 p += 2; /* Skip the // or \\ */
209 if (*p == (char)0)
210 return 0;
212 if (*p == '/') {
214 strncpy(server, lp_workgroup(), 16); /* FIXME: Danger here */
215 return 0;
220 * ok, its for us. Now parse out the server, share etc.
222 * However, we want to parse out [[domain;]user[:password]@] if it
223 * exists ...
226 if (strchr(p, '@')) {
227 pstring username, passwd, domain;
228 char *u = userinfo;
230 next_token(&p, userinfo, "@", sizeof(fstring));
232 username[0] = passwd[0] = domain[0] = 0;
234 if (strchr(u, ';')) {
236 next_token(&u, domain, ";", sizeof(fstring));
240 if (strchr(u, ':')) {
242 next_token(&u, username, ":", sizeof(fstring));
244 pstrcpy(passwd, u);
247 else {
249 pstrcpy(username, u);
253 if (username[0])
254 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
256 if (passwd[0])
257 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
261 if (!next_token(&p, server, "/", sizeof(fstring))) {
263 return -1;
267 if (*p == (char)0) return 0; /* That's it ... */
269 if (!next_token(&p, share, "/", sizeof(fstring))) {
271 return -1;
275 pstrcpy(path, p);
277 all_string_sub(path, "/", "\\", 0);
279 return 0;
283 * Convert an SMB error into a UNIX error ...
286 int smbc_errno(struct cli_state *c)
288 uint8 eclass;
289 uint32 ecode;
290 int ret;
292 ret = cli_error(c, &eclass, &ecode, NULL);
294 if (ret) {
295 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
296 (int)eclass, (int)ecode, (int)ecode, ret));
298 return ret;
302 * Connect to a server, possibly on an existing connection
304 * Here, what we want to do is: If the server and username
305 * match an existing connection, reuse that, otherwise, establish a
306 * new connection.
308 * If we have to create a new connection, call the auth_fn to get the
309 * info we need, unless the username and password were passed in.
312 struct smbc_server *smbc_server(char *server, char *share,
313 char *workgroup, char *username,
314 char *password)
316 struct smbc_server *srv=NULL;
317 struct cli_state c;
318 struct nmb_name called, calling;
319 char *p, *server_n = server;
320 fstring group;
321 pstring ipenv;
322 struct in_addr ip;
323 extern struct in_addr ipzero;
325 ip = ipzero;
326 ZERO_STRUCT(c);
328 /* try to use an existing connection */
329 for (srv=smbc_srvs;srv;srv=srv->next) {
330 if (strcmp(server,srv->server_name)==0 &&
331 strcmp(share,srv->share_name)==0 &&
332 strcmp(workgroup,srv->workgroup)==0 &&
333 strcmp(username, srv->username) == 0)
334 return srv;
337 if (server[0] == 0) {
338 errno = EPERM;
339 return NULL;
343 * Pick up the auth info here, once we know we need to connect
344 * But only if we do not have a username and password ...
347 if (!username[0] || !password[0])
348 smbc_auth_fn(server, share, workgroup, sizeof(fstring),
349 username, sizeof(fstring), password, sizeof(fstring));
352 * However, smbc_auth_fn may have picked up info relating to an
353 * existing connection, so try for an existing connection again ...
356 for (srv=smbc_srvs;srv;srv=srv->next) {
357 if (strcmp(server,srv->server_name)==0 &&
358 strcmp(share,srv->share_name)==0 &&
359 strcmp(workgroup,srv->workgroup)==0 &&
360 strcmp(username, srv->username) == 0)
361 return srv;
364 make_nmb_name(&calling, my_netbios_name, 0x0);
365 make_nmb_name(&called , server, 0x20);
367 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
369 if ((p=strchr(server_n,'#')) &&
370 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
371 struct in_addr sip;
372 pstring s;
374 fstrcpy(group, server_n);
375 p = strchr(group,'#');
376 *p = 0;
380 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
382 again:
383 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
385 ip = ipzero;
387 /* have to open a new connection */
388 if (!cli_initialise(&c) || !cli_connect(&c, server_n, &ip)) {
389 errno = ENOENT;
390 return NULL;
393 if (!cli_session_request(&c, &calling, &called)) {
394 cli_shutdown(&c);
395 if (strcmp(called.name, "*SMBSERVER")) {
396 make_nmb_name(&called , "*SMBSERVER", 0x20);
397 goto again;
399 errno = ENOENT;
400 return NULL;
403 DEBUG(4,(" session request ok\n"));
405 if (!cli_negprot(&c)) {
406 cli_shutdown(&c);
407 errno = ENOENT;
408 return NULL;
411 if (!cli_session_setup(&c, username,
412 password, strlen(password),
413 password, strlen(password),
414 workgroup) &&
415 /* try an anonymous login if it failed */
416 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
417 cli_shutdown(&c);
418 errno = EPERM;
419 return NULL;
422 DEBUG(4,(" session setup ok\n"));
424 if (!cli_send_tconX(&c, share, "?????",
425 password, strlen(password)+1)) {
426 errno = smbc_errno(&c);
427 cli_shutdown(&c);
428 return NULL;
431 DEBUG(4,(" tconx ok\n"));
433 srv = (struct smbc_server *)malloc(sizeof(*srv));
434 if (!srv) {
435 errno = ENOMEM;
436 goto failed;
439 ZERO_STRUCTP(srv);
441 srv->cli = c;
443 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
445 srv->server_name = strdup(server);
446 if (!srv->server_name) {
447 errno = ENOMEM;
448 goto failed;
451 srv->share_name = strdup(share);
452 if (!srv->share_name) {
453 errno = ENOMEM;
454 goto failed;
457 srv->workgroup = strdup(workgroup);
458 if (!srv->workgroup) {
459 errno = ENOMEM;
460 goto failed;
463 srv->username = strdup(username);
464 if (!srv->username) {
465 errno = ENOMEM;
466 goto failed;
469 DLIST_ADD(smbc_srvs, srv);
471 return srv;
473 failed:
474 cli_shutdown(&c);
475 if (!srv) return NULL;
477 if (srv->server_name) free(srv->server_name);
478 if (srv->share_name) free(srv->share_name);
479 free(srv);
480 return NULL;
484 *Initialise the library etc
486 * We accept valiv values for debug from 0 to 100,
487 * and insist that fn must be non-null.
490 int smbc_init(smbc_get_auth_data_fn fn, int debug)
492 pstring conf;
493 int p, pid;
494 char *user = NULL, *host = NULL, *home = NULL, *pname="libsmbclient";
497 * Next lot ifdef'd out until test suite fixed ...
500 if (!fn || debug < 0 || debug > 100) {
502 errno = EINVAL;
503 return -1;
507 smbc_initialized = 1;
508 smbc_auth_fn = fn;
509 smbc_debug = debug;
511 DEBUGLEVEL = -1;
513 setup_logging(pname, False);
516 * We try to construct our netbios name from our hostname etc
519 user = getenv("USER");
520 if (!user) user = ""; /* FIXME: What to do about this? */
523 * FIXME: Is this the best way to get the user info? */
525 pstrcpy(smbc_user, user); /* Save for use elsewhere */
527 pid = getpid();
530 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
533 slprintf(my_netbios_name, 16, "smbc%s%d", user, pid);
535 charset_initialise();
537 /* Here we would open the smb.conf file if needed ... */
539 home = getenv("HOME");
541 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
543 load_interfaces(); /* Load the list of interfaces ... */
545 in_client = True; /* FIXME, make a param */
547 if (!lp_load(conf, True, False, False)) {
550 * Hmmm, what the hell do we do here ... we could not parse the
551 * config file ... We must return an error ... and keep info around
552 * about why we failed
555 errno = ENOENT; /* Hmmm, what error resp does lp_load return ? */
556 return -1;
560 codepage_initialise(lp_client_code_page()); /* Get a codepage */
562 reopen_logs(); /* Get logging working ... */
564 name_register_wins(my_netbios_name, 0);
567 * Now initialize the file descriptor array and figure out what the
568 * max open files is, so we can return FD's that are above the max
569 * open file, and separated by a guard band
572 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
573 do {
574 struct rlimit rlp;
576 if (getrlimit(RLIMIT_NOFILE, &rlp)) {
578 DEBUG(0, ("smbc_init: getrlimit(1) for RLIMIT_NOFILE failed with error %s\n", strerror(errno)));
580 smbc_start_fd = 1000000;
581 smbc_max_fd = 10000; /* FIXME, should be a define ... */
584 else {
586 smbc_start_fd = rlp.rlim_max + 10000; /* Leave a guard space of 10,000 */
587 smbc_max_fd = 10000;
590 } while ( 0 );
591 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
593 smbc_start_fd = 1000000;
594 smbc_max_fd = 10000; /* FIXME, should be a define ... */
596 #endif
598 smbc_file_table = malloc(smbc_max_fd * sizeof(struct smbc_file *));
600 for (p = 0; p < smbc_max_fd; p++)
601 smbc_file_table[p] = NULL;
603 if (!smbc_file_table)
604 return ENOMEM;
606 return 0; /* Success */
611 * Routine to open() a file ...
614 int smbc_open(const char *fname, int flags, mode_t mode)
616 fstring server, share, user, password;
617 pstring path;
618 struct smbc_server *srv = NULL;
619 struct smbc_file *file = NULL;
620 int fd;
622 if (!smbc_initialized) {
624 errno = EUCLEAN; /* Best I can think of ... */
625 return -1;
629 if (!fname) {
631 errno = EINVAL;
632 return -1;
636 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
638 if (user[0] == (char)0) pstrcpy(user, smbc_user);
640 srv = smbc_server(server, share, lp_workgroup(), user, password);
642 if (!srv) {
644 if (errno == EPERM) errno = EACCES;
645 return -1; /* smbc_server sets errno */
649 if (path[strlen(path) - 1] == '\\') {
651 fd = -1;
654 else {
656 int slot = 0;
658 /* Find a free slot first */
660 while (smbc_file_table[slot])
661 slot++;
663 if (slot > smbc_max_fd) {
665 errno = ENOMEM; /* FIXME, is this best? */
666 return -1;
670 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
672 if (!smbc_file_table[slot]) {
674 errno = ENOMEM;
675 return -1;
679 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
681 /* Handle the error ... */
683 free(smbc_file_table[slot]);
684 smbc_file_table[slot] = NULL;
685 errno = smbc_errno(&srv->cli);
686 return -1;
690 /* Fill in file struct */
692 smbc_file_table[slot]->cli_fd = fd;
693 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
694 smbc_file_table[slot]->fname = strdup(fname);
695 smbc_file_table[slot]->srv = srv;
696 smbc_file_table[slot]->offset = 0;
697 smbc_file_table[slot]->file = True;
699 return smbc_file_table[slot]->smbc_fd;
703 /* Check if opendir needed ... */
705 if (fd == -1) {
706 int eno = 0;
708 eno = smbc_errno(&srv->cli);
709 fd = smbc_opendir(fname);
710 if (fd < 0) errno = eno;
711 return fd;
715 return 1; /* Success, with fd ... */
717 failed:
719 /*FIXME, clean up things ... */
720 return -1;
725 * Routine to create a file
728 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
730 int smbc_creat(const char *path, mode_t mode)
733 if (!smbc_initialized) {
735 errno = EUCLEAN;
736 return -1;
740 return smbc_open(path, creat_bits, mode);
744 * Routine to read() a file ...
747 ssize_t smbc_read(int fd, void *buf, size_t count)
749 struct smbc_file *fe;
750 int ret;
752 if (!smbc_initialized) {
754 errno = EUCLEAN;
755 return -1;
759 DEBUG(4, ("smbc_read(%d, %d)\n", fd, (int)count));
761 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
763 errno = EBADF;
764 return -1;
768 fe = smbc_file_table[fd - smbc_start_fd];
770 if (!fe->file) {
772 errno = EBADF;
773 return -1;
777 ret = cli_read(&fe->srv->cli, fe->cli_fd, buf, fe->offset, count);
779 if (ret < 0) {
781 errno = smbc_errno(&fe->srv->cli);
782 return -1;
786 fe->offset += ret;
788 DEBUG(4, (" --> %d\n", ret));
790 return ret; /* Success, ret bytes of data ... */
795 * Routine to write() a file ...
798 ssize_t smbc_write(int fd, void *buf, size_t count)
800 int ret;
801 struct smbc_file *fe;
803 if (!smbc_initialized) {
805 errno = EUCLEAN;
806 return -1;
810 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
812 errno = EBADF;
813 return -1;
817 fe = smbc_file_table[fd - smbc_start_fd];
819 ret = cli_write(&fe->srv->cli, fe->cli_fd, 0, buf, fe->offset, count);
821 if (ret < 0) {
823 errno = smbc_errno(&fe->srv->cli);
824 return -1;
828 fe->offset += ret;
830 return ret; /* Success, 0 bytes of data ... */
834 * Routine to close() a file ...
837 int smbc_close(int fd)
839 struct smbc_file *fe;
841 if (!smbc_initialized) {
843 errno = EUCLEAN;
844 return -1;
848 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
850 errno = EBADF;
851 return -1;
855 fe = smbc_file_table[fd - smbc_start_fd];
857 if (!fe->file) {
859 return smbc_closedir(fd);
863 if (!cli_close(&fe->srv->cli, fe->cli_fd)) {
865 errno = smbc_errno(&fe->srv->cli); /* FIXME, should we deallocate slot? */
866 return -1;
870 free(fe);
871 smbc_file_table[fd - smbc_start_fd] = NULL;
873 return 0;
877 * Routine to unlink() a file
880 int smbc_unlink(const char *fname)
882 fstring server, share, user, password;
883 pstring path;
884 struct smbc_server *srv = NULL;
886 if (!smbc_initialized) {
888 errno = EUCLEAN; /* Best I can think of ... */
889 return -1;
893 if (!fname) {
895 errno = EINVAL;
896 return -1;
900 smbc_parse_path(fname, server, share, path, user, password); /* FIXME, check errors */
902 if (user[0] == (char)0) pstrcpy(user, smbc_user);
904 srv = smbc_server(server, share, lp_workgroup(), user, password);
906 if (!srv) {
908 return -1; /* smbc_server sets errno */
912 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
914 int job = smbc_stat_printjob(srv, path, NULL, NULL);
915 if (job == -1) {
917 return -1;
920 if (cli_printjob_del(&srv->cli, job) != 0) {
922 return -1;
925 } else */
927 if (!cli_unlink(&srv->cli, path)) {
929 errno = smbc_errno(&srv->cli);
931 if (errno == EACCES) { /* Check if the file is a directory */
933 int err, saverr = errno;
934 struct stat st;
935 size_t size = 0;
936 uint16 mode = 0;
937 time_t m_time = 0, a_time = 0, c_time = 0;
938 SMB_INO_T ino = 0;
940 if (!smbc_getatr(srv, path, &mode, &size,
941 &c_time, &a_time, &m_time, &ino)) {
943 /* Hmmm, bad error ... What? */
945 errno = smbc_errno(&srv->cli);
946 return -1;
949 else {
951 if (IS_DOS_DIR(mode))
952 errno = EISDIR;
953 else
954 errno = saverr; /* Restore this */
959 return -1;
963 return 0; /* Success ... */
968 * Routine to rename() a file
971 int smbc_rename(const char *oname, const char *nname)
973 fstring server1, share1, server2, share2, user1, user2, password1, password2;
974 pstring path1, path2;
975 struct smbc_server *srv = NULL;
977 if (!smbc_initialized) {
979 errno = EUCLEAN; /* Best I can think of ... */
980 return -1;
984 if (!oname || !nname) {
986 errno = EINVAL;
987 return -1;
991 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
993 smbc_parse_path(oname, server1, share1, path1, user1, password1);
995 if (user1[0] == (char)0) pstrcpy(user1, smbc_user);
997 smbc_parse_path(nname, server2, share2, path2, user2, password2);
999 if (user2[0] == (char)0) pstrcpy(user2, smbc_user);
1001 if (strcmp(server1, server2) || strcmp(share1, share2) ||
1002 strcmp(user1, user2)) {
1004 /* Can't rename across file systems, or users?? */
1006 errno = EXDEV;
1007 return -1;
1011 srv = smbc_server(server1, share1, lp_workgroup(), user1, password1);
1012 if (!srv) {
1014 return -1;
1018 if (!cli_rename(&srv->cli, path1, path2)) {
1019 int eno = smbc_errno(&srv->cli);
1021 if (eno != EEXIST ||
1022 !cli_unlink(&srv->cli, path2) ||
1023 !cli_rename(&srv->cli, path1, path2)) {
1025 errno = eno;
1026 return -1;
1031 return 0; /* Success */
1036 * A routine to lseek() a file
1039 off_t smbc_lseek(int fd, off_t offset, int whence)
1041 struct smbc_file *fe;
1042 size_t size;
1044 if (!smbc_initialized) {
1046 errno = EUCLEAN;
1047 return -1;
1051 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1053 errno = EBADF;
1054 return -1;
1058 fe = smbc_file_table[fd - smbc_start_fd];
1060 if (!fe->file) {
1062 return smbc_lseekdir(fd, offset, whence);
1066 switch (whence) {
1067 case SEEK_SET:
1068 fe->offset = offset;
1069 break;
1071 case SEEK_CUR:
1072 fe->offset += offset;
1073 break;
1075 case SEEK_END:
1076 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1077 NULL, NULL, NULL) &&
1078 !cli_getattrE(&fe->srv->cli, fe->cli_fd, NULL, &size, NULL, NULL,
1079 NULL)) {
1081 errno = EINVAL;
1082 return -1;
1084 fe->offset = size + offset;
1085 break;
1087 default:
1088 errno = EINVAL;
1089 break;
1093 return fe->offset;
1098 * Generate an inode number from file name for those things that need it
1101 static
1102 ino_t smbc_inode(const char *name)
1105 if (!*name) return 2; /* FIXME, why 2 ??? */
1106 return (ino_t)str_checksum(name);
1111 * Routine to put basic stat info into a stat structure ... Used by stat and
1112 * fstat below.
1115 static
1116 int smbc_setup_stat(struct stat *st, char *fname, size_t size, int mode)
1119 st->st_mode = 0;
1121 if (IS_DOS_DIR(mode)) {
1122 st->st_mode = SMBC_DIR_MODE;
1123 } else {
1124 st->st_mode = SMBC_FILE_MODE;
1127 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1128 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1129 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1130 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1132 st->st_size = size;
1133 st->st_blksize = 512;
1134 st->st_blocks = (size+511)/512;
1135 st->st_uid = getuid();
1136 st->st_gid = getgid();
1138 if (IS_DOS_DIR(mode)) {
1139 st->st_nlink = 2;
1140 } else {
1141 st->st_nlink = 1;
1144 if (st->st_ino == 0) {
1145 st->st_ino = smbc_inode(fname);
1150 * Get info from an SMB server on a file. Use a qpathinfo call first
1151 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1154 BOOL smbc_getatr(struct smbc_server *srv, char *path,
1155 uint16 *mode, size_t *size,
1156 time_t *c_time, time_t *a_time, time_t *m_time,
1157 SMB_INO_T *ino)
1160 if (!smbc_initialized) {
1162 errno = EUCLEAN;
1163 return -1;
1167 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1169 if (!srv->no_pathinfo2 &&
1170 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1171 size, mode, ino)) return True;
1173 /* if this is NT then don't bother with the getatr */
1174 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
1176 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1177 a_time = c_time = m_time;
1178 srv->no_pathinfo2 = True;
1179 return True;
1181 return False;
1185 * Routine to stat a file given a name
1188 int smbc_stat(const char *fname, struct stat *st)
1190 struct smbc_server *srv;
1191 fstring server, share, user, password;
1192 pstring path;
1193 time_t m_time = 0, a_time = 0, c_time = 0;
1194 size_t size = 0;
1195 uint16 mode = 0;
1196 SMB_INO_T ino = 0;
1198 if (!smbc_initialized) {
1200 errno = EUCLEAN; /* Best I can think of ... */
1201 return -1;
1205 if (!fname) {
1207 errno = EINVAL;
1208 return -1;
1212 DEBUG(4, ("smbc_stat(%s)\n", fname));
1214 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
1216 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1218 srv = smbc_server(server, share, lp_workgroup(), user, password);
1220 if (!srv) {
1222 return -1; /* errno set by smbc_server */
1226 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1228 mode = aDIR | aRONLY;
1231 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1233 if (strcmp(path, "\\") == 0) {
1235 mode = aDIR | aRONLY;
1238 else {
1240 mode = aRONLY;
1241 smbc_stat_printjob(srv, path, &size, &m_time);
1242 c_time = a_time = m_time;
1245 else { */
1247 if (!smbc_getatr(srv, path, &mode, &size,
1248 &c_time, &a_time, &m_time, &ino)) {
1250 errno = smbc_errno(&srv->cli);
1251 return -1;
1255 /* } */
1257 st->st_ino = ino;
1259 smbc_setup_stat(st, path, size, mode);
1261 st->st_atime = a_time;
1262 st->st_ctime = c_time;
1263 st->st_mtime = m_time;
1264 st->st_dev = srv->dev;
1266 return 0;
1271 * Routine to stat a file given an fd
1274 int smbc_fstat(int fd, struct stat *st)
1276 struct smbc_file *fe;
1277 time_t c_time, a_time, m_time;
1278 size_t size;
1279 uint16 mode;
1280 SMB_INO_T ino = 0;
1282 if (!smbc_initialized) {
1284 errno = EUCLEAN;
1285 return -1;
1289 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1291 errno = EBADF;
1292 return -1;
1296 fe = smbc_file_table[fd - smbc_start_fd];
1298 if (!fe->file) {
1300 return smbc_fstatdir(fd, st);
1304 if (!cli_qfileinfo(&fe->srv->cli, fe->cli_fd,
1305 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1306 !cli_getattrE(&fe->srv->cli, fe->cli_fd,
1307 &mode, &size, &c_time, &a_time, &m_time)) {
1309 errno = EINVAL;
1310 return -1;
1314 st->st_ino = ino;
1316 smbc_setup_stat(st, fe->fname, size, mode);
1318 st->st_atime = a_time;
1319 st->st_ctime = c_time;
1320 st->st_mtime = m_time;
1321 st->st_dev = fe->srv->dev;
1323 return 0;
1328 * Routine to open a directory
1330 * We want to allow:
1332 * smb: which should list all the workgroups available
1333 * smb:workgroup
1334 * smb:workgroup//server
1335 * smb://server
1336 * smb://server/share
1339 static void smbc_remove_dir(struct smbc_file *dir)
1341 struct smbc_dir_list *d,*f;
1343 d = dir->dir_list;
1344 while (d) {
1346 f = d; d = d->next;
1348 if (f->dirent) free(f->dirent);
1349 free(f);
1353 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1357 static int add_dirent(struct smbc_file *dir, const char *name, const char *comment, uint32 type)
1359 struct smbc_dirent *dirent;
1360 int size;
1363 * Allocate space for the dirent, which must be increased by the
1364 * size of the name and the comment and 1 for the null on the comment.
1365 * The null on the name is already accounted for.
1368 size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1369 (comment?strlen(comment):0) + 1;
1371 dirent = malloc(size);
1373 if (!dirent) {
1375 dir->dir_error = ENOMEM;
1376 return -1;
1380 if (dir->dir_list == NULL) {
1382 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1383 if (!dir->dir_list) {
1385 free(dirent);
1386 dir->dir_error = ENOMEM;
1387 return -1;
1391 dir->dir_end = dir->dir_next = dir->dir_list;
1394 else {
1396 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1398 if (!dir->dir_end) {
1400 free(dirent);
1401 dir->dir_error = ENOMEM;
1402 return -1;
1406 dir->dir_end = dir->dir_end->next;
1410 dir->dir_end->next = NULL;
1411 dir->dir_end->dirent = dirent;
1413 dirent->smbc_type = type;
1414 dirent->namelen = (name?strlen(name):0);
1415 dirent->commentlen = (comment?strlen(comment):0);
1416 dirent->dirlen = size;
1418 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1420 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1421 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1423 return 0;
1427 static void
1428 list_fn(const char *name, uint32 type, const char *comment, void *state)
1430 struct smbc_file *dir = (struct smbc_file *)state;
1431 int dirent_type;
1433 /* We need to process the type a little ... */
1435 if (dir->dir_type == SMBC_FILE_SHARE) {
1437 switch (type) {
1438 case 0: /* Directory tree */
1439 dirent_type = SMBC_FILE_SHARE;
1440 break;
1442 case 1:
1443 dirent_type = SMBC_PRINTER_SHARE;
1444 break;
1446 case 2:
1447 dirent_type = SMBC_COMMS_SHARE;
1448 break;
1450 case 3:
1451 dirent_type = SMBC_IPC_SHARE;
1452 break;
1454 default:
1455 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1456 break;
1460 else dirent_type = dir->dir_type;
1462 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1464 /* An error occurred, what do we do? */
1470 static void
1471 dir_list_fn(file_info *finfo, const char *mask, void *state)
1474 fprintf(stderr, "Finfo->name=%s, mask=%s\n", finfo->name, mask);
1475 if (add_dirent((struct smbc_file *)state, finfo->name, "",
1476 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1478 /* Handle an error ... */
1484 int smbc_opendir(const char *fname)
1486 struct in_addr addr;
1487 fstring server, share, user, password;
1488 pstring path;
1489 struct smbc_server *srv = NULL;
1490 struct in_addr rem_ip;
1491 int slot = 0;
1492 uint8 eclass;
1493 uint32 ecode;
1495 if (!smbc_initialized) {
1497 errno = EUCLEAN;
1498 return -1;
1502 if (!fname) {
1504 errno = EINVAL;
1505 return -1;
1509 if (smbc_parse_path(fname, server, share, path, user, password)) {
1511 errno = EINVAL;
1512 return -1;
1516 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1518 /* Get a file entry ... */
1520 slot = 0;
1522 while (smbc_file_table[slot])
1523 slot++;
1525 if (slot > smbc_max_fd) {
1527 errno = ENOMEM;
1528 return -1; /* FIXME, ... move into a func */
1532 smbc_file_table[slot] = malloc(sizeof(struct smbc_file));
1534 if (!smbc_file_table[slot]) {
1536 errno = ENOMEM;
1537 return -1;
1541 smbc_file_table[slot]->cli_fd = 0;
1542 smbc_file_table[slot]->smbc_fd = slot + smbc_start_fd;
1543 smbc_file_table[slot]->fname = strdup(fname);
1544 smbc_file_table[slot]->srv = NULL;
1545 smbc_file_table[slot]->offset = 0;
1546 smbc_file_table[slot]->file = False;
1547 smbc_file_table[slot]->dir_list =
1548 smbc_file_table[slot]->dir_next =
1549 smbc_file_table[slot]->dir_end = NULL;
1551 if (server[0] == (char)0) {
1553 if (share[0] != (char)0 || path[0] != (char)0) {
1555 errno = EINVAL;
1556 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1557 smbc_file_table[slot] = NULL;
1558 return -1;
1562 /* We have server and share and path empty ... so list the workgroups */
1564 /* fprintf(stderr, "Workgroup is: %s\n", lp_workgroup()); */
1565 cli_get_backup_server(my_netbios_name, lp_workgroup(), server, sizeof(server));
1567 smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;
1570 * Get a connection to IPC$ on the server if we do not already have one
1573 srv = smbc_server(server, "IPC$", lp_workgroup(), user, password);
1575 if (!srv) {
1577 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1578 smbc_file_table[slot] = NULL;
1579 return -1;
1583 smbc_file_table[slot]->srv = srv;
1585 /* Now, list the stuff ... */
1587 if (!cli_NetServerEnum(&srv->cli, lp_workgroup(), 0x80000000, list_fn,
1588 (void *)smbc_file_table[slot])) {
1590 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1591 smbc_file_table[slot] = NULL;
1592 errno = cli_error(&srv->cli, &eclass, &ecode, NULL);
1593 return -1;
1597 else { /* Server not an empty string ... Check the rest and see what gives */
1599 if (share[0] == (char)0) {
1601 if (path[0] != (char)0) { /* Should not have empty share with path */
1603 errno = EINVAL;
1604 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1605 smbc_file_table[slot] = NULL;
1606 return -1;
1610 /* Check to see if <server><1D> translates, or <server><20> translates */
1612 if (resolve_name(server, &rem_ip, 0x1d)) { /* Found LMB */
1613 pstring buserver;
1615 smbc_file_table[slot]->dir_type = SMBC_SERVER;
1618 * Get the backup list ...
1621 cli_get_backup_server(my_netbios_name, server, buserver, sizeof(buserver));
1624 * Get a connection to IPC$ on the server if we do not already have one
1627 srv = smbc_server(buserver, "IPC$", lp_workgroup(), user, password);
1629 if (!srv) {
1631 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1632 smbc_file_table[slot] = NULL; /* FIXME: Memory leaks ... */
1633 return -1;
1637 smbc_file_table[slot]->srv = srv;
1639 /* Now, list the servers ... */
1641 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1642 (void *)smbc_file_table[slot])) {
1644 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1645 smbc_file_table[slot] = NULL;
1646 errno = cli_error(&srv->cli, &eclass, &ecode, NULL);
1647 return -1;
1652 else {
1654 if (resolve_name(server, &rem_ip, 0x20)) {
1656 /* Now, list the shares ... */
1658 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1660 srv = smbc_server(server, "IPC$", lp_workgroup(), user, password);
1662 if (!srv) {
1664 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1665 smbc_file_table[slot] = NULL;
1666 return -1;
1670 smbc_file_table[slot]->srv = srv;
1672 /* Now, list the servers ... */
1674 if (cli_RNetShareEnum(&srv->cli, list_fn,
1675 (void *)smbc_file_table[slot]) < 0) {
1677 errno = cli_error(&srv->cli, &eclass, &ecode, NULL);
1678 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1679 smbc_file_table[slot] = NULL;
1680 return -1;
1685 else {
1687 errno = EINVAL;
1688 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1689 smbc_file_table[slot] = NULL;
1690 return -1;
1697 else { /* The server and share are specified ... work from there ... */
1699 /* Well, we connect to the server and list the directory */
1701 smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;
1703 srv = smbc_server(server, share, lp_workgroup(), user, password);
1705 if (!srv) {
1707 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1708 smbc_file_table[slot] = NULL;
1709 return -1;
1713 smbc_file_table[slot]->srv = srv;
1715 /* Now, list the files ... */
1717 pstrcat(path, "\\*");
1719 if (!cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1720 (void *)smbc_file_table[slot])) {
1722 if (smbc_file_table[slot]) free(smbc_file_table[slot]);
1723 smbc_file_table[slot] = NULL;
1724 errno = smbc_errno(&srv->cli);
1725 return -1;
1732 return smbc_file_table[slot]->smbc_fd;
1737 * Routine to close a directory
1740 int smbc_closedir(int fd)
1742 struct smbc_file *fe;
1744 if (!smbc_initialized) {
1746 errno = EUCLEAN;
1747 return -1;
1751 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1753 errno = EBADF;
1754 return -1;
1758 fe = smbc_file_table[fd - smbc_start_fd];
1760 if (!fe) {
1762 errno = ENOENT; /* FIXME: Is this correct */
1763 return -1;
1767 smbc_remove_dir(fe); /* Clean it up */
1769 if (fe) free(fe); /* Free the space too */
1771 smbc_file_table[fd - smbc_start_fd] = NULL;
1773 return 0;
1778 * Routine to get a directory entry
1781 static char smbc_local_dirent[512]; /* Make big enough */
1783 struct smbc_dirent *smbc_readdir(unsigned int fd)
1785 struct smbc_file *fe;
1786 struct smbc_dirent *dirp, *dirent;
1788 /* Check that all is ok first ... */
1790 if (!smbc_initialized) {
1792 errno = EUCLEAN;
1793 return NULL;
1797 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1799 errno = EBADF;
1800 return NULL;
1804 fe = smbc_file_table[fd - smbc_start_fd];
1806 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1808 errno = ENOTDIR;
1809 return NULL;
1813 if (!fe->dir_next)
1814 return NULL;
1815 else {
1817 dirent = fe->dir_next->dirent;
1819 if (!dirent) {
1821 errno = ENOENT;
1822 return NULL;
1826 /* Hmmm, do I even need to copy it? */
1828 bcopy(dirent, smbc_local_dirent, dirent->dirlen); /* Copy the dirent */
1830 dirp = (struct smbc_dirent *)smbc_local_dirent;
1832 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1834 fe->dir_next = fe->dir_next->next;
1836 return (struct smbc_dirent *)smbc_local_dirent;
1842 * Routine to get directory entries
1845 int smbc_getdents(unsigned int fd, struct smbc_dirent *dirp, int count)
1847 struct smbc_file *fe;
1848 struct smbc_dir_list *dir;
1849 int rem = count, reqd;
1851 /* Check that all is ok first ... */
1853 if (!smbc_initialized) {
1855 errno = EUCLEAN;
1856 return -1;
1860 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
1862 errno = EBADF;
1863 return -1;
1867 fe = smbc_file_table[fd - smbc_start_fd];
1869 if (fe->file != False) { /* FIXME, should be dir, perhaps */
1871 errno = ENOTDIR;
1872 return -1;
1877 * Now, retrieve the number of entries that will fit in what was passed
1878 * We have to figure out if the info is in the list, or we need to
1879 * send a request to the server to get the info.
1882 while ((dir = fe->dir_next)) {
1883 struct smbc_dirent *dirent;
1885 if (!dir->dirent) {
1887 errno = ENOENT; /* Bad error */
1888 return -1;
1892 if (rem < (reqd = (sizeof(struct smbc_dirent) + dir->dirent->namelen +
1893 dir->dirent->commentlen + 1))) {
1895 if (rem < count) { /* We managed to copy something */
1897 errno = 0;
1898 return count - rem;
1901 else { /* Nothing copied ... */
1903 errno = EINVAL; /* Not enough space ... */
1904 return -1;
1910 dirent = dir->dirent;
1912 bcopy(dirent, dirp, reqd); /* Copy the data in ... */
1914 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1916 (char *)dirp += reqd;
1918 rem -= reqd;
1920 fe->dir_next = dir = dir -> next;
1923 if (rem == count)
1924 return 0;
1925 else
1926 return count - rem;
1931 * Routine to create a directory ...
1934 int smbc_mkdir(const char *fname, mode_t mode)
1936 struct smbc_server *srv;
1937 fstring server, share, user, password;
1938 pstring path;
1940 if (!smbc_initialized) {
1942 errno = EUCLEAN;
1943 return -1;
1947 if (!fname) {
1949 errno = EINVAL;
1950 return -1;
1954 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1956 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
1958 if (user[0] == (char)0) pstrcpy(user, smbc_user);
1960 srv = smbc_server(server, share, lp_workgroup(), user, password);
1962 if (!srv) {
1964 return -1; /* errno set by smbc_server */
1968 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1970 mode = aDIR | aRONLY;
1973 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1975 if (strcmp(path, "\\") == 0) {
1977 mode = aDIR | aRONLY;
1980 else {
1982 mode = aRONLY;
1983 smbc_stat_printjob(srv, path, &size, &m_time);
1984 c_time = a_time = m_time;
1987 else { */
1989 if (!cli_mkdir(&srv->cli, path)) {
1991 errno = smbc_errno(&srv->cli);
1992 return -1;
1996 return 0;
2001 * Routine to remove a directory
2004 int smbc_rmdir(const char *fname)
2006 struct smbc_server *srv;
2007 fstring server, share, user, password;
2008 pstring path;
2010 if (!smbc_initialized) {
2012 errno = EUCLEAN;
2013 return -1;
2017 if (!fname) {
2019 errno = EINVAL;
2020 return -1;
2024 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2026 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2028 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2030 srv = smbc_server(server, share, lp_workgroup(), user, password);
2032 if (!srv) {
2034 return -1; /* errno set by smbc_server */
2038 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2040 mode = aDIR | aRONLY;
2043 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2045 if (strcmp(path, "\\") == 0) {
2047 mode = aDIR | aRONLY;
2050 else {
2052 mode = aRONLY;
2053 smbc_stat_printjob(srv, path, &size, &m_time);
2054 c_time = a_time = m_time;
2057 else { */
2059 if (!cli_rmdir(&srv->cli, path)) {
2061 errno = smbc_errno(&srv->cli);
2062 return -1;
2066 return 0;
2071 * Routine to return the current directory position
2074 off_t smbc_telldir(int fd)
2076 struct smbc_file *fe;
2078 if (!smbc_initialized) {
2080 errno = EUCLEAN;
2081 return -1;
2085 if (fd < smbc_start_fd || fd >= (smbc_start_fd + smbc_max_fd)) {
2087 errno = EBADF;
2088 return -1;
2092 fe = smbc_file_table[fd - smbc_start_fd];
2094 if (fe->file != False) { /* FIXME, should be dir, perhaps */
2096 errno = ENOTDIR;
2097 return -1;
2101 return (off_t) fe->dir_next;
2106 * Routine to seek on a directory
2109 int smbc_lseekdir(int fd, off_t offset, int whence)
2112 if (!smbc_initialized) {
2114 errno = EUCLEAN;
2115 return -1;
2119 return 0;
2124 * Routine to fstat a dir
2127 int smbc_fstatdir(int fd, struct stat *st)
2130 if (!smbc_initialized) {
2132 errno = EUCLEAN;
2133 return -1;
2137 return 0;
2142 * Routine to print a file on a remote server ...
2144 * We open the file, which we assume to be on a remote server, and then
2145 * copy it to a print file on the share specified by printq.
2148 int smbc_print_file(const char *fname, const char *printq)
2150 int fid1, fid2, bytes, saverr, tot_bytes = 0;
2151 char buf[4096];
2153 if (!smbc_initialized) {
2155 errno = EUCLEAN;
2156 return -1;
2160 if (!fname && !printq) {
2162 errno = EINVAL;
2163 return -1;
2167 /* Try to open the file for reading ... */
2169 if ((fid1 = smbc_open(fname, O_RDONLY, 0666)) < 0) {
2171 return -1; /* smbc_open sets errno */
2175 /* Now, try to open the printer file for writing */
2177 if ((fid2 = smbc_open_print_job(printq)) < 0) {
2179 saverr = errno; /* Save errno */
2180 smbc_close(fid1);
2181 errno = saverr;
2182 return -1;
2186 while ((bytes = smbc_read(fid1, buf, sizeof(buf))) > 0) {
2188 tot_bytes += bytes;
2190 if ((smbc_write(fid2, buf, bytes)) < 0) {
2192 saverr = errno;
2193 smbc_close(fid1);
2194 smbc_close(fid2);
2195 errno = saverr;
2201 saverr = errno;
2203 smbc_close(fid1); /* We have to close these anyway */
2204 smbc_close(fid2);
2206 if (bytes < 0) {
2208 errno = saverr;
2209 return -1;
2213 return tot_bytes;
2218 * Open a print file to be written to by other calls
2221 int smbc_open_print_job(const char *fname)
2223 struct smbc_server *srv;
2224 fstring server, share, user, password;
2225 pstring path;
2227 if (!smbc_initialized) {
2229 errno = EUCLEAN;
2230 return -1;
2234 if (!fname) {
2236 errno = EINVAL;
2237 return -1;
2241 DEBUG(4, ("smbc_open_print_job(%s)\n", fname));
2243 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2245 /* What if the path is empty, or the file exists? */
2247 return smbc_open(fname, O_WRONLY, 666);
2252 * Routine to list print jobs on a printer share ...
2255 int smbc_list_print_jobs(const char *fname, void (*fn)(struct print_job_info *))
2257 struct smbc_server *srv;
2258 fstring server, share, user, password;
2259 pstring path;
2261 if (!smbc_initialized) {
2263 errno = EUCLEAN;
2264 return -1;
2268 if (!fname) {
2270 errno = EINVAL;
2271 return -1;
2275 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2277 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2279 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2281 srv = smbc_server(server, share, lp_workgroup(), user, password);
2283 if (!srv) {
2285 return -1; /* errno set by smbc_server */
2289 if (cli_print_queue(&srv->cli, fn) < 0) {
2291 errno = smbc_errno(&srv->cli);
2292 return -1;
2296 return 0;
2301 * Delete a print job from a remote printer share
2304 int smbc_unlink_print_job(const char *fname, int id)
2306 struct smbc_server *srv;
2307 fstring server, share, user, password;
2308 pstring path;
2310 if (!smbc_initialized) {
2312 errno = EUCLEAN;
2313 return -1;
2317 if (!fname) {
2319 errno = EINVAL;
2320 return -1;
2324 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2326 smbc_parse_path(fname, server, share, path, user, password); /*FIXME, errors*/
2328 if (user[0] == (char)0) pstrcpy(user, smbc_user);
2330 srv = smbc_server(server, share, lp_workgroup(), user, password);
2332 if (!srv) {
2334 return -1; /* errno set by smbc_server */
2338 if (cli_printjob_del(&srv->cli, id) < 0) {
2340 errno = smbc_errno(&srv->cli);
2341 return -1;
2345 return 0;