2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
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.
26 #include "../include/libsmb_internal.h"
29 * Functions exported by libsmb_cache.c that we need here
31 int smbc_default_cache_functions(SMBCCTX *context);
34 * check if an element is part of the list.
35 * FIXME: Does not belong here !
36 * Can anyone put this in a macro in dlinklist.h ?
39 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
40 if (!p || !list) return False;
42 if (p == list) return True;
48 extern BOOL in_client;
51 * Is the logging working / configfile read ?
53 static int smbc_initialized = 0;
56 hex2int( unsigned int _char )
58 if ( _char >= 'A' && _char <='F')
59 return _char - 'A' + 10;
60 if ( _char >= 'a' && _char <='f')
61 return _char - 'a' + 10;
62 if ( _char >= '0' && _char <='9')
68 decode_urlpart(char *segment, size_t sizeof_segment)
70 int old_length = strlen(segment);
75 char *new_usegment = 0;
81 /* make a copy of the old one */
82 new_usegment = (char*)malloc( old_length * 3 + 1 );
84 while( i < old_length ) {
85 int bReencode = False;
86 unsigned char character = segment[ i++ ];
87 if ((character <= ' ') || (character > 127))
90 new_usegment [ new_length2++ ] = character;
91 if (character == '%' ) {
92 int a = i+1 < old_length ? hex2int( segment[i] ) : -1;
93 int b = i+1 < old_length ? hex2int( segment[i+1] ) : -1;
94 if ((a == -1) || (b == -1)) { /* Only replace if sequence is valid */
95 /* Contains stray %, make sure to re-encode! */
98 /* Valid %xx sequence */
99 character = a * 16 + b; /* Replace with value of %dd */
101 break; /* Stop at %00 */
103 new_usegment [ new_length2++ ] = (unsigned char) segment[i++];
104 new_usegment [ new_length2++ ] = (unsigned char) segment[i++];
108 unsigned int c = character / 16;
110 new_usegment [ new_length2++ ] = '%';
112 c += (c > 9) ? ('A' - 10) : '0';
113 new_usegment[ new_length2++ ] = c;
116 c += (c > 9) ? ('A' - 10) : '0';
117 new_usegment[ new_length2++ ] = c;
120 new_segment [ new_length++ ] = character;
122 new_segment [ new_length ] = 0;
126 /* realloc it with unix charset */
127 pull_utf8_allocate((void**)&new_usegment, new_segment);
129 /* this assumes (very safely) that removing %aa sequences
130 only shortens the string */
131 strncpy(segment, new_usegment, sizeof_segment);
137 * Function to parse a path and turn it into components
139 * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
141 * smb:// means show all the workgroups
142 * smb://name/ means, if name<1D> or name<1B> exists, list servers in workgroup,
143 * else, if name<20> exists, list all shares for server ...
146 static const char *smbc_prefix = "smb:";
149 smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
150 char *user, char *password) /* FIXME, lengths of strings */
158 server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
161 /* clean_fname(s); causing problems ... */
163 /* see if it has the right prefix */
164 len = strlen(smbc_prefix);
165 if (strncmp(s,smbc_prefix,len) ||
166 (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
170 /* Watch the test below, we are testing to see if we should exit */
172 if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
178 p += 2; /* Skip the // or \\ */
185 strncpy(server, context->workgroup,
186 (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
192 * ok, its for us. Now parse out the server, share etc.
194 * However, we want to parse out [[domain;]user[:password]@] if it
198 /* check that '@' occurs before '/', if '/' exists at all */
199 q = strchr_m(p, '@');
200 r = strchr_m(p, '/');
201 if (q && (!r || q < r)) {
202 pstring username, passwd, domain;
203 const char *u = userinfo;
205 next_token(&p, userinfo, "@", sizeof(fstring));
207 username[0] = passwd[0] = domain[0] = 0;
209 if (strchr_m(u, ';')) {
211 next_token(&u, domain, ";", sizeof(fstring));
215 if (strchr_m(u, ':')) {
217 next_token(&u, username, ":", sizeof(fstring));
224 pstrcpy(username, u);
229 strncpy(user, username, sizeof(fstring)); /* FIXME, size and domain */
232 strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
236 if (!next_token(&p, server, "/", sizeof(fstring))) {
242 if (*p == (char)0) goto decoding; /* That's it ... */
244 if (!next_token(&p, share, "/", sizeof(fstring))) {
252 all_string_sub(path, "/", "\\", 0);
255 decode_urlpart(path, sizeof(pstring));
256 decode_urlpart(server, sizeof(fstring));
257 decode_urlpart(share, sizeof(fstring));
258 decode_urlpart(user, sizeof(fstring));
259 decode_urlpart(password, sizeof(fstring));
265 * Convert an SMB error into a UNIX error ...
268 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
270 int ret = cli_errno(c);
272 if (cli_is_dos_error(c)) {
276 cli_dos_error(c, &eclass, &ecode);
278 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
279 (int)eclass, (int)ecode, (int)ecode, ret));
283 status = cli_nt_error(c);
285 DEBUG(3,("smbc errno %s -> %d\n",
286 nt_errstr(status), ret));
294 * returns 0 if the server is in shape. Returns 1 on error
296 * Also useable outside libsmbclient to enable external cache
297 * to do some checks too.
299 int smbc_check_server(SMBCCTX * context, SMBCSRV * server)
301 if ( send_keepalive(server->cli.fd) == False )
304 /* connection is ok */
309 * Remove a server from the cached server list it's unused.
310 * On success, 0 is returned. 1 is returned if the server could not be removed.
312 * Also useable outside libsmbclient
314 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
318 /* are we being fooled ? */
319 if (!context || !context->internal ||
320 !context->internal->_initialized || !srv) return 1;
323 /* Check all open files/directories for a relation with this server */
324 for (file = context->internal->_files; file; file=file->next) {
325 if (file->srv == srv) {
327 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
333 DLIST_REMOVE(context->internal->_servers, srv);
335 cli_shutdown(&srv->cli);
337 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
339 context->callbacks.remove_cached_srv_fn(context, srv);
347 * Connect to a server, possibly on an existing connection
349 * Here, what we want to do is: If the server and username
350 * match an existing connection, reuse that, otherwise, establish a
353 * If we have to create a new connection, call the auth_fn to get the
354 * info we need, unless the username and password were passed in.
357 SMBCSRV *smbc_server(SMBCCTX *context,
358 const char *server, const char *share,
359 fstring workgroup, fstring username,
365 struct nmb_name called, calling;
367 const char *server_n = server;
371 int tried_reverse = 0;
376 if (server[0] == 0) {
383 srv = context->callbacks.get_cached_srv_fn(context, server, share,
384 workgroup, username);
386 if (!auth_called && !srv && (!username[0] || !password[0])) {
387 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
388 username, sizeof(fstring), password, sizeof(fstring));
390 * However, smbc_auth_fn may have picked up info relating to an
391 * existing connection, so try for an existing connection again ...
394 goto check_server_cache;
399 if (context->callbacks.check_server_fn(context, srv)) {
401 * This server is no good anymore
402 * Try to remove it and check for more possible servers in the cache
404 if (context->callbacks.remove_unused_server_fn(context, srv)) {
406 * We could not remove the server completely, remove it from the cache
407 * so we will not get it again. It will be removed when the last file/dir
410 context->callbacks.remove_cached_srv_fn(context, srv);
414 * Maybe there are more cached connections to this server
416 goto check_server_cache;
421 make_nmb_name(&calling, context->netbios_name, 0x0);
422 make_nmb_name(&called , server, 0x20);
424 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
426 if ((p=strchr_m(server_n,'#')) &&
427 (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
429 fstrcpy(group, server_n);
430 p = strchr_m(group,'#');
435 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
438 slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
442 /* have to open a new connection */
443 if (!cli_initialise(&c)) {
448 c.timeout = context->timeout;
450 if (!cli_connect(&c, server_n, &ip)) {
456 if (!cli_session_request(&c, &calling, &called)) {
458 if (strcmp(called.name, "*SMBSERVER")) {
459 make_nmb_name(&called , "*SMBSERVER", 0x20);
462 else { /* Try one more time, but ensure we don't loop */
464 /* Only try this if server is an IP address ... */
466 if (is_ipaddress(server) && !tried_reverse) {
468 struct in_addr rem_ip;
470 if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
471 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
476 tried_reverse++; /* Yuck */
478 if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
479 make_nmb_name(&called, remote_name, 0x20);
490 DEBUG(4,(" session request ok\n"));
492 if (!cli_negprot(&c)) {
498 if (!cli_session_setup(&c, username,
499 password, strlen(password),
500 password, strlen(password),
502 /* try an anonymous login if it failed */
503 !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
509 DEBUG(4,(" session setup ok\n"));
511 if (!cli_send_tconX(&c, share, "?????",
512 password, strlen(password)+1)) {
513 errno = smbc_errno(context, &c);
518 DEBUG(4,(" tconx ok\n"));
521 * Ok, we have got a nice connection
522 * Let's find a free server_fd
525 srv = (SMBCSRV *)malloc(sizeof(*srv));
533 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
535 /* now add it to the cache (internal or external) */
536 if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
537 DEBUG(3, (" Failed to add server to cache\n"));
542 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
543 server, share, srv));
549 if (!srv) return NULL;
556 * Routine to open() a file ...
559 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
561 fstring server, share, user, password, workgroup;
564 SMBCFILE *file = NULL;
567 if (!context || !context->internal ||
568 !context->internal->_initialized) {
570 errno = EINVAL; /* Best I can think of ... */
582 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
584 if (user[0] == (char)0) fstrcpy(user, context->user);
586 fstrcpy(workgroup, context->workgroup);
588 srv = smbc_server(context, server, share, workgroup, user, password);
592 if (errno == EPERM) errno = EACCES;
593 return NULL; /* smbc_server sets errno */
597 /* Hmmm, the test for a directory is suspect here ... FIXME */
599 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
606 file = malloc(sizeof(SMBCFILE));
617 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
619 /* Handle the error ... */
622 errno = smbc_errno(context, &srv->cli);
627 /* Fill in file struct */
630 file->fname = strdup(fname);
635 DLIST_ADD(context->internal->_files, file);
640 /* Check if opendir needed ... */
645 eno = smbc_errno(context, &srv->cli);
646 file = context->opendir(context, fname);
647 if (!file) errno = eno;
652 errno = EINVAL; /* FIXME, correct errno ? */
658 * Routine to create a file
661 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
663 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
666 if (!context || !context->internal ||
667 !context->internal->_initialized) {
674 return smbc_open_ctx(context, path, creat_bits, mode);
678 * Routine to read() a file ...
681 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
685 if (!context || !context->internal ||
686 !context->internal->_initialized) {
693 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
695 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
702 /* Check that the buffer exists ... */
711 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
715 errno = smbc_errno(context, &file->srv->cli);
722 DEBUG(4, (" --> %d\n", ret));
724 return ret; /* Success, ret bytes of data ... */
729 * Routine to write() a file ...
732 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
736 if (!context || !context->internal ||
737 !context->internal->_initialized) {
744 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
751 /* Check that the buffer exists ... */
760 ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
764 errno = smbc_errno(context, &file->srv->cli);
771 return ret; /* Success, 0 bytes of data ... */
775 * Routine to close() a file ...
778 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
782 if (!context || !context->internal ||
783 !context->internal->_initialized) {
790 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
800 return context->closedir(context, file);
804 if (!cli_close(&file->srv->cli, file->cli_fd)) {
806 DEBUG(3, ("cli_close failed on %s. purging server.\n",
808 /* Deallocate slot and remove the server
809 * from the server cache if unused */
810 errno = smbc_errno(context, &file->srv->cli);
812 DLIST_REMOVE(context->internal->_files, file);
813 SAFE_FREE(file->fname);
815 context->callbacks.remove_unused_server_fn(context, srv);
821 DLIST_REMOVE(context->internal->_files, file);
822 SAFE_FREE(file->fname);
829 * Get info from an SMB server on a file. Use a qpathinfo call first
830 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
832 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path,
833 uint16 *mode, size_t *size,
834 time_t *c_time, time_t *a_time, time_t *m_time,
838 if (!context || !context->internal ||
839 !context->internal->_initialized) {
846 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
848 if (!srv->no_pathinfo2 &&
849 cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
850 size, mode, ino)) return True;
852 /* if this is NT then don't bother with the getatr */
853 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
855 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
856 a_time = c_time = m_time;
857 srv->no_pathinfo2 = True;
866 * Routine to unlink() a file
869 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
871 fstring server, share, user, password, workgroup;
875 if (!context || !context->internal ||
876 !context->internal->_initialized) {
878 errno = EINVAL; /* Best I can think of ... */
890 smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
892 if (user[0] == (char)0) fstrcpy(user, context->user);
894 fstrcpy(workgroup, context->workgroup);
896 srv = smbc_server(context, server, share, workgroup, user, password);
900 return -1; /* smbc_server sets errno */
904 /* if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
906 int job = smbc_stat_printjob(srv, path, NULL, NULL);
912 if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
920 if (!cli_unlink(&srv->cli, path)) {
922 errno = smbc_errno(context, &srv->cli);
924 if (errno == EACCES) { /* Check if the file is a directory */
929 time_t m_time = 0, a_time = 0, c_time = 0;
932 if (!smbc_getatr(context, srv, path, &mode, &size,
933 &c_time, &a_time, &m_time, &ino)) {
935 /* Hmmm, bad error ... What? */
937 errno = smbc_errno(context, &srv->cli);
943 if (IS_DOS_DIR(mode))
946 errno = saverr; /* Restore this */
955 return 0; /* Success ... */
960 * Routine to rename() a file
963 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname,
964 SMBCCTX *ncontext, const char *nname)
966 fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
967 pstring path1, path2;
970 if (!ocontext || !ncontext ||
971 !ocontext->internal || !ncontext->internal ||
972 !ocontext->internal->_initialized ||
973 !ncontext->internal->_initialized) {
975 errno = EINVAL; /* Best I can think of ... */
980 if (!oname || !nname) {
987 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
989 smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
991 if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
993 smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
995 if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
997 if (strcmp(server1, server2) || strcmp(share1, share2) ||
998 strcmp(user1, user2)) {
1000 /* Can't rename across file systems, or users?? */
1007 fstrcpy(workgroup, ocontext->workgroup);
1008 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1009 srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
1016 if (!cli_rename(&srv->cli, path1, path2)) {
1017 int eno = smbc_errno(ocontext, &srv->cli);
1019 if (eno != EEXIST ||
1020 !cli_unlink(&srv->cli, path2) ||
1021 !cli_rename(&srv->cli, path1, path2)) {
1029 return 0; /* Success */
1034 * A routine to lseek() a file
1037 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
1041 if (!context || !context->internal ||
1042 !context->internal->_initialized) {
1049 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1059 return -1; /* Can't lseek a dir ... */
1065 file->offset = offset;
1069 file->offset += offset;
1073 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1076 SMB_BIG_UINT b_size = size;
1077 if (!cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &b_size, NULL, NULL,
1085 file->offset = size + offset;
1094 return file->offset;
1099 * Generate an inode number from file name for those things that need it
1103 ino_t smbc_inode(SMBCCTX *context, const char *name)
1106 if (!context || !context->internal ||
1107 !context->internal->_initialized) {
1114 if (!*name) return 2; /* FIXME, why 2 ??? */
1115 return (ino_t)str_checksum(name);
1120 * Routine to put basic stat info into a stat structure ... Used by stat and
1125 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1130 if (IS_DOS_DIR(mode)) {
1131 st->st_mode = SMBC_DIR_MODE;
1133 st->st_mode = SMBC_FILE_MODE;
1136 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1137 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1138 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1139 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1142 st->st_blksize = 512;
1143 st->st_blocks = (size+511)/512;
1144 st->st_uid = getuid();
1145 st->st_gid = getgid();
1147 if (IS_DOS_DIR(mode)) {
1153 if (st->st_ino == 0) {
1154 st->st_ino = smbc_inode(context, fname);
1157 return True; /* FIXME: Is this needed ? */
1162 * Routine to stat a file given a name
1165 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1168 fstring server, share, user, password, workgroup;
1170 time_t m_time = 0, a_time = 0, c_time = 0;
1175 if (!context || !context->internal ||
1176 !context->internal->_initialized) {
1178 errno = EINVAL; /* Best I can think of ... */
1190 DEBUG(4, ("smbc_stat(%s)\n", fname));
1192 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1194 if (user[0] == (char)0) fstrcpy(user, context->user);
1196 fstrcpy(workgroup, context->workgroup);
1198 srv = smbc_server(context, server, share, workgroup, user, password);
1202 return -1; /* errno set by smbc_server */
1206 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1208 mode = aDIR | aRONLY;
1211 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1213 if (strcmp(path, "\\") == 0) {
1215 mode = aDIR | aRONLY;
1221 smbc_stat_printjob(srv, path, &size, &m_time);
1222 c_time = a_time = m_time;
1227 if (!smbc_getatr(context, srv, path, &mode, &size,
1228 &c_time, &a_time, &m_time, &ino)) {
1230 errno = smbc_errno(context, &srv->cli);
1237 smbc_setup_stat(context, st, path, size, mode);
1239 st->st_atime = a_time;
1240 st->st_ctime = c_time;
1241 st->st_mtime = m_time;
1242 st->st_dev = srv->dev;
1249 * Routine to stat a file given an fd
1252 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1254 time_t c_time, a_time, m_time;
1259 if (!context || !context->internal ||
1260 !context->internal->_initialized) {
1267 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1276 return context->fstatdir(context, file, st);
1280 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1281 &mode, &size, &c_time, &a_time, &m_time, NULL, &ino)) {
1282 SMB_BIG_UINT b_size = size;
1283 if (!cli_getattrE(&file->srv->cli, file->cli_fd,
1284 &mode, &b_size, &c_time, &a_time, &m_time)) {
1295 smbc_setup_stat(context, st, file->fname, size, mode);
1297 st->st_atime = a_time;
1298 st->st_ctime = c_time;
1299 st->st_mtime = m_time;
1300 st->st_dev = file->srv->dev;
1307 * Routine to open a directory
1311 * smb: which should list all the workgroups available
1313 * smb:workgroup//server
1315 * smb://server/share
1316 * smb://<IP-addr> which should list shares on server
1317 * smb://<IP-addr>/share which should list files on share
1320 static void smbc_remove_dir(SMBCFILE *dir)
1322 struct smbc_dir_list *d,*f;
1329 SAFE_FREE(f->dirent);
1334 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1338 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1340 struct smbc_dirent *dirent;
1342 char *u_name = NULL, *u_comment = NULL;
1343 size_t u_name_len = 0, u_comment_len = 0;
1346 u_name_len = push_utf8_allocate(&u_name, name);
1348 u_comment_len = push_utf8_allocate(&u_comment, comment);
1351 * Allocate space for the dirent, which must be increased by the
1352 * size of the name and the comment and 1 for the null on the comment.
1353 * The null on the name is already accounted for.
1356 size = sizeof(struct smbc_dirent) + u_name_len + u_comment_len + 1;
1358 dirent = malloc(size);
1362 dir->dir_error = ENOMEM;
1367 ZERO_STRUCTP(dirent);
1369 if (dir->dir_list == NULL) {
1371 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1372 if (!dir->dir_list) {
1375 dir->dir_error = ENOMEM;
1379 ZERO_STRUCTP(dir->dir_list);
1381 dir->dir_end = dir->dir_next = dir->dir_list;
1386 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1388 if (!dir->dir_end->next) {
1391 dir->dir_error = ENOMEM;
1395 ZERO_STRUCTP(dir->dir_end->next);
1397 dir->dir_end = dir->dir_end->next;
1401 dir->dir_end->next = NULL;
1402 dir->dir_end->dirent = dirent;
1404 dirent->smbc_type = type;
1405 dirent->namelen = u_name_len;
1406 dirent->commentlen = u_comment_len;
1407 dirent->dirlen = size;
1409 strncpy(dirent->name, (u_name?u_name:""), dirent->namelen + 1);
1411 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1412 strncpy(dirent->comment, (u_comment?u_comment:""), dirent->commentlen + 1);
1414 SAFE_FREE(u_comment);
1422 list_fn(const char *name, uint32 type, const char *comment, void *state)
1424 SMBCFILE *dir = (SMBCFILE *)state;
1427 /* We need to process the type a little ... */
1429 if (dir->dir_type == SMBC_FILE_SHARE) {
1432 case 0: /* Directory tree */
1433 dirent_type = SMBC_FILE_SHARE;
1437 dirent_type = SMBC_PRINTER_SHARE;
1441 dirent_type = SMBC_COMMS_SHARE;
1445 dirent_type = SMBC_IPC_SHARE;
1449 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1453 else dirent_type = dir->dir_type;
1455 if (add_dirent(dir, name, comment, dirent_type) < 0) {
1457 /* An error occurred, what do we do? */
1458 /* FIXME: Add some code here */
1465 dir_list_fn(file_info *finfo, const char *mask, void *state)
1468 if (add_dirent((SMBCFILE *)state, finfo->name, "",
1469 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1471 /* Handle an error ... */
1473 /* FIXME: Add some code ... */
1480 /* Return the IP address and workgroup of a master browser on the
1483 static BOOL find_master_ip_bcast(pstring workgroup, struct in_addr *server_ip)
1485 struct in_addr *ip_list;
1488 /* Go looking for workgroups by broadcasting on the local network */
1490 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
1494 for (i = count-1; i < count; i++) {
1495 static fstring name;
1497 DEBUG(0, ("name_status_find %d %s\n", i, inet_ntoa(ip_list[i])));
1499 if (!name_status_find("*", 0, 0x1d, ip_list[i], name))
1502 if (!find_master_ip(name, server_ip))
1505 pstrcpy(workgroup, name);
1507 DEBUG(4, ("found master browser %s, %s\n",
1508 name, inet_ntoa(ip_list[i])));
1516 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1518 fstring server, share, user, password;
1521 SMBCSRV *srv = NULL;
1522 SMBCFILE *dir = NULL;
1523 struct in_addr rem_ip;
1525 if (!context || !context->internal ||
1526 !context->internal->_initialized) {
1527 DEBUG(4, ("no valid context\n"));
1534 DEBUG(4, ("no valid fname\n"));
1539 if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1540 DEBUG(4, ("no valid path\n"));
1545 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s'\n", fname, server, share, path));
1547 if (user[0] == (char)0) fstrcpy(user, context->user);
1549 pstrcpy(workgroup, context->workgroup);
1551 dir = malloc(sizeof(*dir));
1563 dir->fname = strdup(fname);
1567 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1569 if (server[0] == (char)0) {
1570 struct in_addr server_ip;
1571 DEBUG(4, ("empty server\n"));
1572 if (share[0] != (char)0 || path[0] != (char)0) {
1573 DEBUG(4,("share %d path %d\n", share[0], path[0]));
1576 SAFE_FREE(dir->fname);
1582 /* We have server and share and path empty ... so list the workgroups */
1583 /* first try to get the LMB for our workgroup, and if that fails, */
1586 pstrcpy(workgroup, lp_workgroup());
1588 if (!find_master_ip(lp_workgroup(), &server_ip)) {
1589 DEBUG(4, ("Unable to find master browser for workgroup %s\n",
1591 if (!find_master_ip_bcast(workgroup, &server_ip)) {
1592 DEBUG(4, ("Unable to find master browser by "
1600 * Get a connection to IPC$ on the server if we do not already have one
1603 srv = smbc_server(context, inet_ntoa(server_ip), "IPC$", workgroup, user, password);
1608 SAFE_FREE(dir->fname);
1615 dir->dir_type = SMBC_WORKGROUP;
1617 /* Now, list the stuff ... */
1619 if (!cli_NetServerEnum(&srv->cli, workgroup, SV_TYPE_DOMAIN_ENUM, list_fn,
1623 SAFE_FREE(dir->fname);
1626 errno = cli_errno(&srv->cli);
1632 else { /* Server not an empty string ... Check the rest and see what gives */
1634 if (share[0] == (char)0) {
1636 if (path[0] != (char)0) { /* Should not have empty share with path */
1640 SAFE_FREE(dir->fname);
1647 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1648 /* However, we check to see if <server> is an IP address first */
1650 if (!is_ipaddress(server) && /* Not an IP addr so check next */
1651 (resolve_name(server, &rem_ip, 0x1d) || /* Found LMB */
1652 resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
1655 dir->dir_type = SMBC_SERVER;
1658 * Get the backup list ...
1662 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1664 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
1665 errno = EPERM; /* FIXME, is this correct */
1671 * Get a connection to IPC$ on the server if we do not already have one
1674 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
1677 DEBUG(0, ("got no contact to IPC$\n"));
1679 SAFE_FREE(dir->fname);
1688 /* Now, list the servers ... */
1690 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1694 SAFE_FREE(dir->fname);
1697 errno = cli_errno(&srv->cli);
1705 if (resolve_name(server, &rem_ip, 0x20)) {
1707 /* Now, list the shares ... */
1709 dir->dir_type = SMBC_FILE_SHARE;
1711 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1716 SAFE_FREE(dir->fname);
1725 /* Now, list the servers ... */
1727 if (cli_RNetShareEnum(&srv->cli, list_fn,
1730 errno = cli_errno(&srv->cli);
1732 SAFE_FREE(dir->fname);
1742 errno = ENODEV; /* Neither the workgroup nor server exists */
1744 SAFE_FREE(dir->fname);
1754 else { /* The server and share are specified ... work from there ... */
1756 /* Well, we connect to the server and list the directory */
1758 dir->dir_type = SMBC_FILE_SHARE;
1760 srv = smbc_server(context, server, share, workgroup, user, password);
1765 SAFE_FREE(dir->fname);
1774 /* Now, list the files ... */
1776 pstrcat(path, "\\*");
1778 if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn,
1782 SAFE_FREE(dir->fname);
1785 errno = smbc_errno(context, &srv->cli);
1793 DLIST_ADD(context->internal->_files, dir);
1799 * Routine to close a directory
1802 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1805 if (!context || !context->internal ||
1806 !context->internal->_initialized) {
1813 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1820 smbc_remove_dir(dir); /* Clean it up */
1822 DLIST_REMOVE(context->internal->_files, dir);
1826 SAFE_FREE(dir->fname);
1827 SAFE_FREE(dir); /* Free the space too */
1836 * Routine to get a directory entry
1839 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1841 struct smbc_dirent *dirp, *dirent;
1843 /* Check that all is ok first ... */
1845 if (!context || !context->internal ||
1846 !context->internal->_initialized) {
1853 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1860 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1871 dirent = dir->dir_next->dirent;
1880 /* Hmmm, do I even need to copy it? */
1882 memcpy(context->internal->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1883 dirp = (struct smbc_dirent *)context->internal->_dirent;
1884 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1885 dir->dir_next = dir->dir_next->next;
1887 return (struct smbc_dirent *)context->internal->_dirent;
1893 * Routine to get directory entries
1896 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1898 struct smbc_dir_list *dirlist;
1899 int rem = count, reqd;
1900 char *ndir = (char *)dirp;
1902 /* Check that all is ok first ... */
1904 if (!context || !context->internal ||
1905 !context->internal->_initialized) {
1912 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
1919 if (dir->file != False) { /* FIXME, should be dir, perhaps */
1927 * Now, retrieve the number of entries that will fit in what was passed
1928 * We have to figure out if the info is in the list, or we need to
1929 * send a request to the server to get the info.
1932 while ((dirlist = dir->dir_next)) {
1933 struct smbc_dirent *dirent;
1935 if (!dirlist->dirent) {
1937 errno = ENOENT; /* Bad error */
1942 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen +
1943 dirlist->dirent->commentlen + 1))) {
1945 if (rem < count) { /* We managed to copy something */
1951 else { /* Nothing copied ... */
1953 errno = EINVAL; /* Not enough space ... */
1960 dirent = dirlist->dirent;
1962 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1964 ((struct smbc_dirent *)ndir)->comment =
1965 (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1971 dir->dir_next = dirlist = dirlist -> next;
1982 * Routine to create a directory ...
1985 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1988 fstring server, share, user, password, workgroup;
1991 if (!context || !context->internal ||
1992 !context->internal->_initialized) {
2006 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
2008 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2010 if (user[0] == (char)0) fstrcpy(user, context->user);
2012 fstrcpy(workgroup, context->workgroup);
2014 srv = smbc_server(context, server, share, workgroup, user, password);
2018 return -1; /* errno set by smbc_server */
2022 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2024 mode = aDIR | aRONLY;
2027 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2029 if (strcmp(path, "\\") == 0) {
2031 mode = aDIR | aRONLY;
2037 smbc_stat_printjob(srv, path, &size, &m_time);
2038 c_time = a_time = m_time;
2043 if (!cli_mkdir(&srv->cli, path)) {
2045 errno = smbc_errno(context, &srv->cli);
2055 * Our list function simply checks to see if a directory is not empty
2058 static int smbc_rmdir_dirempty = True;
2060 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
2063 if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2064 smbc_rmdir_dirempty = False;
2069 * Routine to remove a directory
2072 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
2075 fstring server, share, user, password, workgroup;
2078 if (!context || !context->internal ||
2079 !context->internal->_initialized) {
2093 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2095 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2097 if (user[0] == (char)0) fstrcpy(user, context->user);
2099 fstrcpy(workgroup, context->workgroup);
2101 srv = smbc_server(context, server, share, workgroup, user, password);
2105 return -1; /* errno set by smbc_server */
2109 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2111 mode = aDIR | aRONLY;
2114 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2116 if (strcmp(path, "\\") == 0) {
2118 mode = aDIR | aRONLY;
2124 smbc_stat_printjob(srv, path, &size, &m_time);
2125 c_time = a_time = m_time;
2130 if (!cli_rmdir(&srv->cli, path)) {
2132 errno = smbc_errno(context, &srv->cli);
2134 if (errno == EACCES) { /* Check if the dir empty or not */
2136 pstring lpath; /* Local storage to avoid buffer overflows */
2138 smbc_rmdir_dirempty = True; /* Make this so ... */
2140 pstrcpy(lpath, path);
2141 pstrcat(lpath, "\\*");
2143 if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2146 /* Fix errno to ignore latest error ... */
2148 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
2149 smbc_errno(context, &srv->cli)));
2154 if (smbc_rmdir_dirempty)
2170 * Routine to return the current directory position
2173 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2175 off_t ret_val; /* Squash warnings about cast */
2177 if (!context || !context->internal ||
2178 !context->internal->_initialized) {
2185 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2192 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2200 * We return the pointer here as the offset
2202 ret_val = (int)dir->dir_next;
2208 * A routine to run down the list and see if the entry is OK
2211 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list,
2212 struct smbc_dirent *dirent)
2215 /* Run down the list looking for what we want */
2219 struct smbc_dir_list *tmp = list;
2223 if (tmp->dirent == dirent)
2232 return NULL; /* Not found, or an error */
2238 * Routine to seek on a directory
2241 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2243 long int l_offset = offset; /* Handle problems of size */
2244 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
2245 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
2247 if (!context || !context->internal ||
2248 !context->internal->_initialized) {
2255 if (dir->file != False) { /* FIXME, should be dir, perhaps */
2262 /* Now, check what we were passed and see if it is OK ... */
2264 if (dirent == NULL) { /* Seek to the begining of the list */
2266 dir->dir_next = dir->dir_list;
2271 /* Now, run down the list and make sure that the entry is OK */
2272 /* This may need to be changed if we change the format of the list */
2274 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2276 errno = EINVAL; /* Bad entry */
2281 dir->dir_next = list_ent;
2288 * Routine to fstat a dir
2291 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2294 if (!context || !context->internal ||
2295 !context->internal->_initialized) {
2302 /* No code yet ... */
2309 * Open a print file to be written to by other calls
2312 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2314 fstring server, share, user, password;
2317 if (!context || !context->internal ||
2318 !context->internal->_initialized) {
2332 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2334 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2336 /* What if the path is empty, or the file exists? */
2338 return context->open(context, fname, O_WRONLY, 666);
2343 * Routine to print a file on a remote server ...
2345 * We open the file, which we assume to be on a remote server, and then
2346 * copy it to a print file on the share specified by printq.
2349 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2351 SMBCFILE *fid1, *fid2;
2352 int bytes, saverr, tot_bytes = 0;
2355 if (!c_file || !c_file->internal->_initialized || !c_print ||
2356 !c_print->internal->_initialized) {
2363 if (!fname && !printq) {
2370 /* Try to open the file for reading ... */
2372 if ((int)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2374 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2375 return -1; /* smbc_open sets errno */
2379 /* Now, try to open the printer file for writing */
2381 if ((int)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2383 saverr = errno; /* Save errno */
2384 c_file->close(c_file, fid1);
2390 while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2394 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2397 c_file->close(c_file, fid1);
2398 c_print->close(c_print, fid2);
2407 c_file->close(c_file, fid1); /* We have to close these anyway */
2408 c_print->close(c_print, fid2);
2422 * Routine to list print jobs on a printer share ...
2425 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
2428 fstring server, share, user, password, workgroup;
2431 if (!context || !context->internal ||
2432 !context->internal->_initialized) {
2446 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2448 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2450 if (user[0] == (char)0) fstrcpy(user, context->user);
2452 fstrcpy(workgroup, context->workgroup);
2454 srv = smbc_server(context, server, share, workgroup, user, password);
2458 return -1; /* errno set by smbc_server */
2462 if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
2464 errno = smbc_errno(context, &srv->cli);
2474 * Delete a print job from a remote printer share
2477 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2480 fstring server, share, user, password, workgroup;
2484 if (!context || !context->internal ||
2485 !context->internal->_initialized) {
2499 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2501 smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2503 if (user[0] == (char)0) fstrcpy(user, context->user);
2505 fstrcpy(workgroup, context->workgroup);
2507 srv = smbc_server(context, server, share, workgroup, user, password);
2511 return -1; /* errno set by smbc_server */
2515 if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2518 errno = smbc_errno(context, &srv->cli);
2519 else if (err == ERRnosuchprintjob)
2530 * Get a new empty handle to fill in with your own info
2532 SMBCCTX * smbc_new_context(void)
2536 context = malloc(sizeof(SMBCCTX));
2542 ZERO_STRUCTP(context);
2544 context->internal = malloc(sizeof(struct smbc_internal_data));
2545 if (!context->internal) {
2550 ZERO_STRUCTP(context->internal);
2553 /* ADD REASONABLE DEFAULTS */
2555 context->timeout = 20000; /* 20 seconds */
2557 context->open = smbc_open_ctx;
2558 context->creat = smbc_creat_ctx;
2559 context->read = smbc_read_ctx;
2560 context->write = smbc_write_ctx;
2561 context->close = smbc_close_ctx;
2562 context->unlink = smbc_unlink_ctx;
2563 context->rename = smbc_rename_ctx;
2564 context->lseek = smbc_lseek_ctx;
2565 context->stat = smbc_stat_ctx;
2566 context->fstat = smbc_fstat_ctx;
2567 context->opendir = smbc_opendir_ctx;
2568 context->closedir = smbc_closedir_ctx;
2569 context->readdir = smbc_readdir_ctx;
2570 context->getdents = smbc_getdents_ctx;
2571 context->mkdir = smbc_mkdir_ctx;
2572 context->rmdir = smbc_rmdir_ctx;
2573 context->telldir = smbc_telldir_ctx;
2574 context->lseekdir = smbc_lseekdir_ctx;
2575 context->fstatdir = smbc_fstatdir_ctx;
2576 context->open_print_job = smbc_open_print_job_ctx;
2577 context->print_file = smbc_print_file_ctx;
2578 context->list_print_jobs = smbc_list_print_jobs_ctx;
2579 context->unlink_print_job = smbc_unlink_print_job_ctx;
2581 context->callbacks.check_server_fn = smbc_check_server;
2582 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2584 smbc_default_cache_functions(context);
2592 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
2593 * and thus you'll be leaking memory if not handled properly.
2596 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2605 DEBUG(1,("Performing aggressive shutdown.\n"));
2607 f = context->internal->_files;
2609 context->close(context, f);
2612 context->internal->_files = NULL;
2614 /* First try to remove the servers the nice way. */
2615 if (context->callbacks.purge_cached_fn(context)) {
2617 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2618 s = context->internal->_servers;
2620 cli_shutdown(&s->cli);
2621 context->callbacks.remove_cached_srv_fn(context, s);
2625 context->internal->_servers = NULL;
2629 /* This is the polite way */
2630 if (context->callbacks.purge_cached_fn(context)) {
2631 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2635 if (context->internal->_servers) {
2636 DEBUG(1, ("Active servers in context, free_context failed.\n"));
2640 if (context->internal->_files) {
2641 DEBUG(1, ("Active files in context, free_context failed.\n"));
2647 /* Things we have to clean up */
2648 SAFE_FREE(context->workgroup);
2649 SAFE_FREE(context->netbios_name);
2650 SAFE_FREE(context->user);
2652 DEBUG(3, ("Context %p succesfully freed\n", context));
2653 SAFE_FREE(context->internal);
2660 * Initialise the library etc
2662 * We accept a struct containing handle information.
2663 * valid values for info->debug from 0 to 100,
2664 * and insist that info->fn must be non-null.
2666 SMBCCTX * smbc_init_context(SMBCCTX * context)
2670 char *user = NULL, *home = NULL;
2672 if (!context || !context->internal) {
2677 /* Do not initialise the same client twice */
2678 if (context->internal->_initialized) {
2682 if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2689 if (!smbc_initialized) {
2690 /* Do some library wide intialisations the first time we get called */
2692 /* Do we still need this ? */
2695 setup_logging( "libsmbclient", True);
2697 /* Here we would open the smb.conf file if needed ... */
2699 home = getenv("HOME");
2701 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2703 load_interfaces(); /* Load the list of interfaces ... */
2705 in_client = True; /* FIXME, make a param */
2707 if (!lp_load(conf, True, False, False)) {
2710 * Hmmm, what the hell do we do here ... we could not parse the
2711 * config file ... We must return an error ... and keep info around
2712 * about why we failed
2715 errno = ENOENT; /* FIXME: Figure out the correct error response */
2719 reopen_logs(); /* Get logging working ... */
2722 * Block SIGPIPE (from lib/util_sock.c: write())
2723 * It is not needed and should not stop execution
2725 BlockSignals(True, SIGPIPE);
2727 /* Done with one-time initialisation */
2728 smbc_initialized = 1;
2732 if (!context->user) {
2734 * FIXME: Is this the best way to get the user info?
2736 user = getenv("USER");
2737 /* walk around as "guest" if no username can be found */
2738 if (!user) context->user = strdup("guest");
2739 else context->user = strdup(user);
2742 if (!context->netbios_name) {
2744 * We try to get our netbios name from the config. If that fails we fall
2745 * back on constructing our netbios name from our hostname etc
2747 if (global_myname()) {
2748 context->netbios_name = strdup(global_myname());
2752 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2755 context->netbios_name = malloc(17);
2756 if (!context->netbios_name) {
2760 slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2764 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
2766 if (!context->workgroup) {
2767 if (lp_workgroup()) {
2768 context->workgroup = strdup(lp_workgroup());
2771 /* TODO: Think about a decent default workgroup */
2772 context->workgroup = strdup("samba");
2776 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
2778 /* shortest timeout is 1 second */
2779 if (context->timeout > 0 && context->timeout < 1000)
2780 context->timeout = 1000;
2783 * FIXME: Should we check the function pointers here?
2786 context->internal->_initialized = 1;