This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[sfrench/samba-autobuild/.git] / source / libsmb / libsmbclient.c
1 /* 
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 
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25
26 #include "../include/libsmbclient.h"
27
28 /*
29  * Functions exported by libsmb_cache.c that we need here
30  */
31 int smbc_default_cache_functions(SMBCCTX *context);
32
33 /* 
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 ?
37  * -- Tom
38  */
39 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
40         if (!p || !list) return False;
41         do {
42                 if (p == list) return True;
43                 list = list->next;
44         } while (list);
45         return False;
46 }
47
48 extern BOOL in_client;
49
50 /*
51  * Is the logging working / configfile read ? 
52  */
53 static int smbc_initialized = 0;
54
55 /*
56  * Function to parse a path and turn it into components
57  *
58  * We accept smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
59  * 
60  * smb://       means show all the workgroups
61  * smb://name/  means, if name<1D> or name<1B> exists, list servers in workgroup,
62  *              else, if name<20> exists, list all shares for server ...
63  */
64
65 static const char *smbc_prefix = "smb:";
66
67 static int
68 smbc_parse_path(SMBCCTX *context, const char *fname, char *server, char *share, char *path,
69                 char *user, char *password) /* FIXME, lengths of strings */
70 {
71         static pstring s;
72         pstring userinfo;
73         char *p;
74         char *q, *r;
75         int len;
76
77         server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
78         pstrcpy(s, fname);
79
80         /*  clean_fname(s);  causing problems ... */
81
82         /* see if it has the right prefix */
83         len = strlen(smbc_prefix);
84         if (strncmp(s,smbc_prefix,len) || 
85             (s[len] != '/' && s[len] != 0)) return -1; /* What about no smb: ? */
86
87         p = s + len;
88
89         /* Watch the test below, we are testing to see if we should exit */
90
91         if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
92
93                 return -1;
94
95         }
96
97         p += 2;  /* Skip the // or \\  */
98
99         if (*p == (char)0)
100                 return 0;
101
102         if (*p == '/') {
103
104                 strncpy(server, context->workgroup, 
105                         (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
106                 return 0;
107                 
108         }
109
110         /*
111          * ok, its for us. Now parse out the server, share etc. 
112          *
113          * However, we want to parse out [[domain;]user[:password]@] if it
114          * exists ...
115          */
116
117         /* check that '@' occurs before '/', if '/' exists at all */
118         q = strchr_m(p, '@');
119         r = strchr_m(p, '/');
120         if (q && (!r || q < r)) {
121                 pstring username, passwd, domain;
122                 char *u = userinfo;
123
124                 next_token(&p, userinfo, "@", sizeof(fstring));
125
126                 username[0] = passwd[0] = domain[0] = 0;
127
128                 if (strchr_m(u, ';')) {
129       
130                         next_token(&u, domain, ";", sizeof(fstring));
131
132                 }
133
134                 if (strchr_m(u, ':')) {
135
136                         next_token(&u, username, ":", sizeof(fstring));
137
138                         pstrcpy(passwd, u);
139
140                 }
141                 else {
142
143                         pstrcpy(username, u);
144
145                 }
146
147                 if (username[0])
148                         strncpy(user, username, sizeof(fstring));  /* FIXME, size and domain */
149
150                 if (passwd[0])
151                         strncpy(password, passwd, sizeof(fstring)); /* FIXME, size */
152
153         }
154
155         if (!next_token(&p, server, "/", sizeof(fstring))) {
156
157                 return -1;
158
159         }
160
161         if (*p == (char)0) return 0;  /* That's it ... */
162   
163         if (!next_token(&p, share, "/", sizeof(fstring))) {
164
165                 return -1;
166
167         }
168
169         pstrcpy(path, p);
170   
171         all_string_sub(path, "/", "\\", 0);
172
173         return 0;
174 }
175
176 /*
177  * Convert an SMB error into a UNIX error ...
178  */
179
180 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
181 {
182         int ret = cli_errno(c);
183         
184         if (cli_is_dos_error(c)) {
185                 uint8 eclass;
186                 uint32 ecode;
187
188                 cli_dos_error(c, &eclass, &ecode);
189                 
190                 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n", 
191                          (int)eclass, (int)ecode, (int)ecode, ret));
192         } else {
193                 NTSTATUS status;
194
195                 status = cli_nt_error(c);
196
197                 DEBUG(3,("smbc errno %s -> %d\n",
198                          nt_errstr(status), ret));
199         }
200
201         return ret;
202 }
203
204 /* 
205  * Check a server_fd.
206  * returns 0 if the server is in shape. Returns 1 on error 
207  * 
208  * Also useable outside libsmbclient to enable external cache
209  * to do some checks too.
210  */
211 int smbc_check_server(SMBCCTX * context, SMBCSRV * server) 
212 {
213         if ( send_keepalive(server->cli.fd) == False )
214                 return 1;
215
216         /* connection is ok */
217         return 0;
218 }
219
220 /* 
221  * Remove a server from the list server_table if it's unused.
222  * On success, 0 is returned. 1 is returned if the server could not be removed.
223  * 
224  * Also useable outside libsmbclient
225  */
226 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
227 {
228         SMBCFILE * file;
229
230         /* are we being fooled ? */
231         if (!context || !context->_initialized || !srv) return 1;
232
233         
234         /* Check all open files/directories for a relation with this server */
235         for (file = context->_files; file; file=file->next) {
236                 if (file->srv == srv) {
237                         /* Still used */
238                         DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n", 
239                                   srv, file));
240                         return 1;
241                 }
242         }
243
244         DLIST_REMOVE(context->_servers, srv);
245
246         cli_shutdown(&srv->cli);
247
248         DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
249
250         context->callbacks.remove_cached_srv_fn(context, srv);
251         
252         SAFE_FREE(srv);
253         
254         return 0;
255 }
256
257 /*
258  * Connect to a server, possibly on an existing connection
259  *
260  * Here, what we want to do is: If the server and username
261  * match an existing connection, reuse that, otherwise, establish a 
262  * new connection.
263  *
264  * If we have to create a new connection, call the auth_fn to get the
265  * info we need, unless the username and password were passed in.
266  */
267
268 SMBCSRV *smbc_server(SMBCCTX *context,
269                      char *server, char *share, 
270                      char *workgroup, char *username, 
271                      char *password)
272 {
273         SMBCSRV *srv=NULL;
274         int auth_called = 0;
275         struct cli_state c;
276         struct nmb_name called, calling;
277         char *p, *server_n = server;
278         fstring group;
279         pstring ipenv;
280         struct in_addr ip;
281         int tried_reverse = 0;
282   
283         zero_ip(&ip);
284         ZERO_STRUCT(c);
285
286         if (server[0] == 0) {
287                 errno = EPERM;
288                 return NULL;
289         }
290
291  check_server_cache:
292
293         srv = context->callbacks.get_cached_srv_fn(context, server, share, 
294                                                    workgroup, username);
295         
296         if (!auth_called && !srv && (!username[0] || !password[0])) {
297                 context->callbacks.auth_fn(server, share, workgroup, sizeof(fstring),
298                              username, sizeof(fstring), password, sizeof(fstring));
299                 /* 
300                  * However, smbc_auth_fn may have picked up info relating to an 
301                  * existing connection, so try for an existing connection again ...
302                  */
303                 auth_called = 1;
304                 goto check_server_cache;
305                 
306         }
307         
308         if (srv) {
309                 if (context->callbacks.check_server_fn(context, srv)) {
310                         /* 
311                          * This server is no good anymore 
312                          * Try to remove it and check for more possible servers in the cache 
313                          */
314                         if (context->callbacks.remove_unused_server_fn(context, srv)) { 
315                                 /* 
316                                  * We could not remove the server completely, remove it from the cache
317                                  * so we will not get it again. It will be removed when the last file/dir
318                                  * is closed.
319                                  */
320                                 context->callbacks.remove_cached_srv_fn(context, srv);
321                         }
322                         
323                         /* 
324                          * Maybe there are more cached connections to this server 
325                          */
326                         goto check_server_cache; 
327                 }
328                 return srv;
329         }
330
331         make_nmb_name(&calling, context->netbios_name, 0x0);
332         make_nmb_name(&called , server, 0x20);
333
334         DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
335   
336         if ((p=strchr_m(server_n,'#')) && 
337             (strcmp(p+1,"1D")==0 || strcmp(p+1,"01")==0)) {
338     
339                 fstrcpy(group, server_n);
340                 p = strchr_m(group,'#');
341                 *p = 0;
342                 
343         }
344
345         DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
346
347  again:
348         slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
349
350         zero_ip(&ip);
351
352         /* have to open a new connection */
353         if (!cli_initialise(&c)) {
354                 errno = ENOENT;
355                 return NULL;
356         }
357
358         c.timeout = context->timeout;
359
360         if (!cli_connect(&c, server_n, &ip)) {
361                 cli_shutdown(&c);
362                 errno = ENOENT;
363                 return NULL;
364         }
365
366         if (!cli_session_request(&c, &calling, &called)) {
367                 cli_shutdown(&c);
368                 if (strcmp(called.name, "*SMBSERVER")) {
369                         make_nmb_name(&called , "*SMBSERVER", 0x20);
370                         goto again;
371                 }
372                 else {  /* Try one more time, but ensure we don't loop */
373
374                   /* Only try this if server is an IP address ... */
375
376                   if (is_ipaddress(server) && !tried_reverse) {
377                     fstring remote_name;
378                     struct in_addr rem_ip;
379
380                     if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
381                       DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
382                       errno = ENOENT;
383                       return NULL;
384                     }
385
386                     tried_reverse++; /* Yuck */
387
388                     if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
389                       make_nmb_name(&called, remote_name, 0x20);
390                       goto again;
391                     }
392
393
394                   }
395                 }
396                 errno = ENOENT;
397                 return NULL;
398         }
399   
400         DEBUG(4,(" session request ok\n"));
401   
402         if (!cli_negprot(&c)) {
403                 cli_shutdown(&c);
404                 errno = ENOENT;
405                 return NULL;
406         }
407
408         if (!cli_session_setup(&c, username, 
409                                password, strlen(password),
410                                password, strlen(password),
411                                workgroup) &&
412             /* try an anonymous login if it failed */
413             !cli_session_setup(&c, "", "", 1,"", 0, workgroup)) {
414                 cli_shutdown(&c);
415                 errno = EPERM;
416                 return NULL;
417         }
418
419         DEBUG(4,(" session setup ok\n"));
420
421         if (!cli_send_tconX(&c, share, "?????",
422                             password, strlen(password)+1)) {
423                 errno = smbc_errno(context, &c);
424                 cli_shutdown(&c);
425                 return NULL;
426         }
427   
428         DEBUG(4,(" tconx ok\n"));
429   
430         /*
431          * Ok, we have got a nice connection
432          * Let's find a free server_fd 
433          */
434
435         srv = (SMBCSRV *)malloc(sizeof(*srv));
436         if (!srv) {
437                 errno = ENOMEM;
438                 goto failed;
439         }
440
441         ZERO_STRUCTP(srv);
442         srv->cli = c;
443         srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
444
445         /* now add it to the cache (internal or external) */
446         if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
447                 DEBUG(3, (" Failed to add server to cache\n"));
448                 goto failed;
449         }
450
451         
452         DEBUG(2, ("Server connect ok: //%s/%s: %p\n", 
453                   server, share, srv));
454
455         return srv;
456
457  failed:
458         cli_shutdown(&c);
459         if (!srv) return NULL;
460   
461         SAFE_FREE(srv);
462         return NULL;
463 }
464
465 /*
466  * Routine to open() a file ...
467  */
468
469 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
470 {
471         fstring server, share, user, password, workgroup;
472         pstring path;
473         SMBCSRV *srv   = NULL;
474         SMBCFILE *file = NULL;
475         int fd;
476
477         if (!context || !context->_initialized) {
478
479                 errno = EINVAL;  /* Best I can think of ... */
480                 return NULL;
481
482         }
483
484         if (!fname) {
485
486                 errno = EINVAL;
487                 return NULL;
488
489         }
490
491         smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
492
493         if (user[0] == (char)0) pstrcpy(user, context->user);
494
495         pstrcpy(workgroup, context->workgroup);
496
497         srv = smbc_server(context, server, share, workgroup, user, password);
498
499         if (!srv) {
500
501                 if (errno == EPERM) errno = EACCES;
502                 return NULL;  /* smbc_server sets errno */
503     
504         }
505
506         /* Hmmm, the test for a directory is suspect here ... FIXME */
507
508         if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
509     
510                 fd = -1;
511
512         }
513         else {
514           
515                 file = malloc(sizeof(SMBCFILE));
516
517                 if (!file) {
518
519                         errno = ENOMEM;
520                         return NULL;
521
522                 }
523
524                 ZERO_STRUCTP(file);
525
526                 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
527
528                         /* Handle the error ... */
529
530                         SAFE_FREE(file);
531                         errno = smbc_errno(context, &srv->cli);
532                         return NULL;
533
534                 }
535
536                 /* Fill in file struct */
537
538                 file->cli_fd  = fd;
539                 file->fname   = strdup(fname);
540                 file->srv     = srv;
541                 file->offset  = 0;
542                 file->file    = True;
543
544                 DLIST_ADD(context->_files, file);
545                 return file;
546
547         }
548
549         /* Check if opendir needed ... */
550
551         if (fd == -1) {
552                 int eno = 0;
553
554                 eno = smbc_errno(context, &srv->cli);
555                 file = context->opendir(context, fname);
556                 if (!file) errno = eno;
557                 return file;
558
559         }
560
561         errno = EINVAL; /* FIXME, correct errno ? */
562         return NULL;
563
564 }
565
566 /*
567  * Routine to create a file 
568  */
569
570 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
571
572 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
573 {
574
575         if (!context || !context->_initialized) {
576
577                 errno = EINVAL;
578                 return NULL;
579
580         }
581
582         return smbc_open_ctx(context, path, creat_bits, mode);
583 }
584
585 /*
586  * Routine to read() a file ...
587  */
588
589 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
590 {
591         int ret;
592
593         if (!context || !context->_initialized) {
594
595                 errno = EINVAL;
596                 return -1;
597
598         }
599
600         DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
601
602         if (!file || !DLIST_CONTAINS(context->_files, file)) {
603
604                 errno = EBADF;
605                 return -1;
606
607         }
608
609         /* Check that the buffer exists ... */
610
611         if (buf == NULL) {
612
613                 errno = EINVAL;
614                 return -1;
615
616         }
617
618         ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
619
620         if (ret < 0) {
621
622                 errno = smbc_errno(context, &file->srv->cli);
623                 return -1;
624
625         }
626
627         file->offset += ret;
628
629         DEBUG(4, ("  --> %d\n", ret));
630
631         return ret;  /* Success, ret bytes of data ... */
632
633 }
634
635 /*
636  * Routine to write() a file ...
637  */
638
639 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
640 {
641         int ret;
642
643         if (!context || context->_initialized) {
644
645                 errno = EINVAL;
646                 return -1;
647
648         }
649
650         if (!file || !DLIST_CONTAINS(context->_files, file)) {
651
652                 errno = EBADF;
653                 return -1;
654     
655         }
656
657         /* Check that the buffer exists ... */
658
659         if (buf == NULL) {
660
661                 errno = EINVAL;
662                 return -1;
663
664         }
665
666         ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, file->offset, count);
667
668         if (ret <= 0) {
669
670                 errno = smbc_errno(context, &file->srv->cli);
671                 return -1;
672
673         }
674
675         file->offset += ret;
676
677         return ret;  /* Success, 0 bytes of data ... */
678 }
679  
680 /*
681  * Routine to close() a file ...
682  */
683
684 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
685 {
686         SMBCSRV *srv; 
687
688         if (!context || !context->_initialized) {
689
690                 errno = EINVAL;
691                 return -1;
692
693         }
694
695         if (!file || !DLIST_CONTAINS(context->_files, file)) {
696    
697                 errno = EBADF;
698                 return -1;
699
700         }
701
702         /* IS a dir ... */
703         if (!file->file) {
704                 
705                 return context->closedir(context, file);
706
707         }
708
709         if (!cli_close(&file->srv->cli, file->cli_fd)) {
710
711                 DEBUG(3, ("cli_close failed on %s. purging server.\n", 
712                           file->fname));
713                 /* Deallocate slot and remove the server 
714                  * from the server cache if unused */
715                 errno = smbc_errno(context, &file->srv->cli);  
716                 srv = file->srv;
717                 DLIST_REMOVE(context->_files, file);
718                 SAFE_FREE(file->fname);
719                 SAFE_FREE(file);
720                 context->callbacks.remove_unused_server_fn(context, srv);
721
722                 return -1;
723
724         }
725
726         if (!file->file) {
727
728                 return context->closedir(context, file);
729
730         }
731
732         if (!cli_close(&file->srv->cli, file->cli_fd)) {
733                 DEBUG(3, ("cli_close failed on %s. purging server.\n", 
734                           file->fname));
735                 /* Deallocate slot and remove the server 
736                  * from the server cache if unused */
737                 errno = smbc_errno(context, &file->srv->cli);  
738                 srv = file->srv;
739                 DLIST_REMOVE(context->_files, file);
740                 SAFE_FREE(file->fname);
741                 SAFE_FREE(file);
742                 context->callbacks.remove_unused_server_fn(context, srv);
743
744                 return -1;
745         }
746
747         DLIST_REMOVE(context->_files, file);
748         SAFE_FREE(file->fname);
749         SAFE_FREE(file);
750
751         return 0;
752 }
753
754 /*
755  * Get info from an SMB server on a file. Use a qpathinfo call first
756  * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
757  */
758 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path, 
759                  uint16 *mode, size_t *size, 
760                  time_t *c_time, time_t *a_time, time_t *m_time,
761                  SMB_INO_T *ino)
762 {
763
764         if (!context || !context->_initialized) {
765  
766                 errno = EINVAL;
767                 return -1;
768  
769         }
770
771         DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
772   
773         if (!srv->no_pathinfo2 &&
774             cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
775                            size, mode, ino)) return True;
776
777         /* if this is NT then don't bother with the getatr */
778         if (srv->cli.capabilities & CAP_NT_SMBS) return False;
779
780         if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
781                 a_time = c_time = m_time;
782                 srv->no_pathinfo2 = True;
783                 return True;
784         }
785
786         return False;
787
788 }
789
790 /*
791  * Routine to unlink() a file
792  */
793
794 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
795 {
796         fstring server, share, user, password, workgroup;
797         pstring path;
798         SMBCSRV *srv = NULL;
799
800         if (!context || context->_initialized) {
801
802                 errno = EINVAL;  /* Best I can think of ... */
803                 return -1;
804
805         }
806
807         if (!fname) {
808
809                 errno = EINVAL;
810                 return -1;
811
812         }
813
814         smbc_parse_path(context, fname, server, share, path, user, password); /* FIXME, check errors */
815
816         if (user[0] == (char)0) pstrcpy(user, context->user);
817
818         pstrcpy(workgroup, context->workgroup);
819
820         srv = smbc_server(context, server, share, workgroup, user, password);
821
822         if (!srv) {
823
824                 return -1;  /* smbc_server sets errno */
825
826         }
827
828         /*  if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
829
830     int job = smbc_stat_printjob(srv, path, NULL, NULL);
831     if (job == -1) {
832
833       return -1;
834
835     }
836     if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
837
838     
839       return -1;
840
841     }
842     } else */
843
844         if (!cli_unlink(&srv->cli, path)) {
845
846                 errno = smbc_errno(context, &srv->cli);
847
848                 if (errno == EACCES) { /* Check if the file is a directory */
849
850                         int saverr = errno;
851                         size_t size = 0;
852                         uint16 mode = 0;
853                         time_t m_time = 0, a_time = 0, c_time = 0;
854                         SMB_INO_T ino = 0;
855
856                         if (!smbc_getatr(context, srv, path, &mode, &size,
857                                          &c_time, &a_time, &m_time, &ino)) {
858
859                                 /* Hmmm, bad error ... What? */
860
861                                 errno = smbc_errno(context, &srv->cli);
862                                 return -1;
863
864                         }
865                         else {
866
867                                 if (IS_DOS_DIR(mode))
868                                         errno = EISDIR;
869                                 else
870                                         errno = saverr;  /* Restore this */
871
872                         }
873                 }
874
875                 return -1;
876
877         }
878
879         return 0;  /* Success ... */
880
881 }
882
883 /*
884  * Routine to rename() a file
885  */
886
887 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname, 
888                            SMBCCTX *ncontext, const char *nname)
889 {
890         fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
891         pstring path1, path2;
892         SMBCSRV *srv = NULL;
893
894         if (!ocontext || !ncontext ||
895             !ocontext->_initialized || !ncontext->_initialized) {
896
897                 errno = EINVAL;  /* Best I can think of ... */
898                 return -1;
899
900         }
901         
902         if (!oname || !nname) {
903
904                 errno = EINVAL;
905                 return -1;
906
907         }
908         
909         DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
910
911         smbc_parse_path(ocontext, oname, server1, share1, path1, user1, password1);
912
913         if (user1[0] == (char)0) pstrcpy(user1, ocontext->user);
914
915         smbc_parse_path(ncontext, nname, server2, share2, path2, user2, password2);
916
917         if (user2[0] == (char)0) pstrcpy(user2, ncontext->user);
918
919         if (strcmp(server1, server2) || strcmp(share1, share2) ||
920             strcmp(user1, user2)) {
921
922                 /* Can't rename across file systems, or users?? */
923
924                 errno = EXDEV;
925                 return -1;
926
927         }
928
929         pstrcpy(workgroup, ocontext->workgroup);
930         /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */ 
931         srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
932         if (!srv) {
933
934                 return -1;
935
936         }
937
938         if (!cli_rename(&srv->cli, path1, path2)) {
939                 int eno = smbc_errno(ocontext, &srv->cli);
940
941                 if (eno != EEXIST ||
942                     !cli_unlink(&srv->cli, path2) ||
943                     !cli_rename(&srv->cli, path1, path2)) {
944
945                         errno = eno;
946                         return -1;
947
948                 }
949         }
950
951         return 0; /* Success */
952
953 }
954
955 /*
956  * A routine to lseek() a file
957  */
958
959 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
960 {
961         size_t size;
962
963         if (!context || !context->_initialized) {
964
965                 errno = EINVAL;
966                 return -1;
967                 
968         }
969
970         if (!file || !DLIST_CONTAINS(context->_files, file)) {
971
972                 errno = EBADF;
973                 return -1;
974
975         }
976
977         if (!file->file) {
978
979                 errno = EINVAL;
980                 return -1;      /* Can't lseek a dir ... */
981
982         }
983
984         switch (whence) {
985         case SEEK_SET:
986                 file->offset = offset;
987                 break;
988
989         case SEEK_CUR:
990                 file->offset += offset;
991                 break;
992
993         case SEEK_END:
994                 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
995                                    NULL, NULL, NULL) &&
996                     !cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
997                                   NULL)) {
998
999                         errno = EINVAL;
1000                         return -1;
1001                 }
1002                 file->offset = size + offset;
1003                 break;
1004
1005         default:
1006                 errno = EINVAL;
1007                 break;
1008
1009         }
1010
1011         return file->offset;
1012
1013 }
1014
1015 /* 
1016  * Generate an inode number from file name for those things that need it
1017  */
1018
1019 static
1020 ino_t smbc_inode(SMBCCTX *context, const char *name)
1021 {
1022
1023         if (!context || !context->_initialized) {
1024
1025                 errno = EINVAL;
1026                 return -1;
1027
1028         }
1029
1030         if (!*name) return 2; /* FIXME, why 2 ??? */
1031         return (ino_t)str_checksum(name);
1032
1033 }
1034
1035 /*
1036  * Routine to put basic stat info into a stat structure ... Used by stat and
1037  * fstat below.
1038  */
1039
1040 static
1041 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname, size_t size, int mode)
1042 {
1043         
1044         st->st_mode = 0;
1045
1046         if (IS_DOS_DIR(mode)) {
1047                 st->st_mode = SMBC_DIR_MODE;
1048         } else {
1049                 st->st_mode = SMBC_FILE_MODE;
1050         }
1051
1052         if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1053         if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1054         if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1055         if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1056
1057         st->st_size = size;
1058         st->st_blksize = 512;
1059         st->st_blocks = (size+511)/512;
1060         st->st_uid = getuid();
1061         st->st_gid = getgid();
1062
1063         if (IS_DOS_DIR(mode)) {
1064                 st->st_nlink = 2;
1065         } else {
1066                 st->st_nlink = 1;
1067         }
1068
1069         if (st->st_ino == 0) {
1070                 st->st_ino = smbc_inode(context, fname);
1071         }
1072         
1073         return True;  /* FIXME: Is this needed ? */
1074
1075 }
1076
1077 /*
1078  * Routine to stat a file given a name
1079  */
1080
1081 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1082 {
1083         SMBCSRV *srv;
1084         fstring server, share, user, password, workgroup;
1085         pstring path;
1086         time_t m_time = 0, a_time = 0, c_time = 0;
1087         size_t size = 0;
1088         uint16 mode = 0;
1089         SMB_INO_T ino = 0;
1090
1091         if (!context || !context->_initialized) {
1092
1093                 errno = EINVAL;  /* Best I can think of ... */
1094                 return -1;
1095     
1096         }
1097
1098         if (!fname) {
1099
1100                 errno = EINVAL;
1101                 return -1;
1102
1103         }
1104   
1105         DEBUG(4, ("smbc_stat(%s)\n", fname));
1106
1107         smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1108
1109         if (user[0] == (char)0) pstrcpy(user, context->user);
1110
1111         pstrcpy(workgroup, context->workgroup);
1112
1113         srv = smbc_server(context, server, share, workgroup, user, password);
1114
1115         if (!srv) {
1116
1117                 return -1;  /* errno set by smbc_server */
1118
1119         }
1120
1121         /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1122
1123            mode = aDIR | aRONLY;
1124
1125            }
1126            else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1127            
1128            if (strcmp(path, "\\") == 0) {
1129            
1130            mode = aDIR | aRONLY;
1131
1132            }
1133            else {
1134
1135            mode = aRONLY;
1136            smbc_stat_printjob(srv, path, &size, &m_time);
1137            c_time = a_time = m_time;
1138
1139            }
1140            else { */
1141
1142         if (!smbc_getatr(context, srv, path, &mode, &size, 
1143                          &c_time, &a_time, &m_time, &ino)) {
1144
1145                 errno = smbc_errno(context, &srv->cli);
1146                 return -1;
1147                 
1148         }
1149
1150         st->st_ino = ino;
1151
1152         smbc_setup_stat(context, st, path, size, mode);
1153
1154         st->st_atime = a_time;
1155         st->st_ctime = c_time;
1156         st->st_mtime = m_time;
1157         st->st_dev   = srv->dev;
1158
1159         return 0;
1160
1161 }
1162
1163 /*
1164  * Routine to stat a file given an fd
1165  */
1166
1167 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1168 {
1169         time_t c_time, a_time, m_time;
1170         size_t size;
1171         uint16 mode;
1172         SMB_INO_T ino = 0;
1173
1174         if (!context || !context->_initialized) {
1175
1176                 errno = EINVAL;
1177                 return -1;
1178
1179         }
1180
1181         if (!file || !DLIST_CONTAINS(context->_files, file)) {
1182
1183                 errno = EBADF;
1184                 return -1;
1185
1186         }
1187
1188         if (!file->file) {
1189
1190                 return context->fstatdir(context, file, st);
1191
1192         }
1193
1194         if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1195                            &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) &&
1196             !cli_getattrE(&file->srv->cli, file->cli_fd,
1197                           &mode, &size, &c_time, &a_time, &m_time)) {
1198
1199                 errno = EINVAL;
1200                 return -1;
1201
1202         }
1203
1204         st->st_ino = ino;
1205
1206         smbc_setup_stat(context, st, file->fname, size, mode);
1207
1208         st->st_atime = a_time;
1209         st->st_ctime = c_time;
1210         st->st_mtime = m_time;
1211         st->st_dev = file->srv->dev;
1212
1213         return 0;
1214
1215 }
1216
1217 /*
1218  * Routine to open a directory
1219  *
1220  * We want to allow:
1221  *
1222  * smb: which should list all the workgroups available
1223  * smb:workgroup
1224  * smb:workgroup//server
1225  * smb://server
1226  * smb://server/share
1227  * smb://<IP-addr> which should list shares on server
1228  * smb://<IP-addr>/share which should list files on share
1229  */
1230
1231 static void smbc_remove_dir(SMBCFILE *dir)
1232 {
1233         struct smbc_dir_list *d,*f;
1234
1235         d = dir->dir_list;
1236         while (d) {
1237
1238                 f = d; d = d->next;
1239
1240                 SAFE_FREE(f->dirent);
1241                 SAFE_FREE(f);
1242
1243         }
1244
1245         dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1246
1247 }
1248
1249 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1250 {
1251         struct smbc_dirent *dirent;
1252         int size;
1253
1254         /*
1255          * Allocate space for the dirent, which must be increased by the 
1256          * size of the name and the comment and 1 for the null on the comment.
1257          * The null on the name is already accounted for.
1258          */
1259
1260         size = sizeof(struct smbc_dirent) + (name?strlen(name):0) +
1261                 (comment?strlen(comment):0) + 1; 
1262     
1263         dirent = malloc(size);
1264
1265         if (!dirent) {
1266
1267                 dir->dir_error = ENOMEM;
1268                 return -1;
1269
1270         }
1271
1272         ZERO_STRUCTP(dirent);
1273
1274         ZERO_STRUCTP(dirent);
1275
1276
1277         if (dir->dir_list == NULL) {
1278
1279                 dir->dir_list = malloc(sizeof(struct smbc_dir_list));
1280                 if (!dir->dir_list) {
1281
1282                         SAFE_FREE(dirent);
1283                         dir->dir_error = ENOMEM;
1284                         return -1;
1285
1286                 }
1287                 ZERO_STRUCTP(dir->dir_list);
1288
1289                 dir->dir_end = dir->dir_next = dir->dir_list;
1290   
1291         }
1292         else {
1293
1294                 dir->dir_end->next = malloc(sizeof(struct smbc_dir_list));
1295                 
1296                 if (!dir->dir_end->next) {
1297                         
1298                         SAFE_FREE(dirent);
1299                         dir->dir_error = ENOMEM;
1300                         return -1;
1301
1302                 }
1303                 ZERO_STRUCTP(dir->dir_end->next);
1304
1305                 dir->dir_end = dir->dir_end->next;
1306
1307         }
1308
1309         dir->dir_end->next = NULL;
1310         dir->dir_end->dirent = dirent;
1311         
1312         dirent->smbc_type = type;
1313         dirent->namelen = (name?strlen(name):0);
1314         dirent->commentlen = (comment?strlen(comment):0);
1315         dirent->dirlen = size;
1316   
1317         strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1318
1319         dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1320         strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1321
1322         return 0;
1323
1324 }
1325
1326 static void
1327 list_fn(const char *name, uint32 type, const char *comment, void *state)
1328 {
1329         SMBCFILE *dir = (SMBCFILE *)state;
1330         int dirent_type;
1331
1332         /* We need to process the type a little ... */
1333
1334         if (dir->dir_type == SMBC_FILE_SHARE) {
1335                 
1336                 switch (type) {
1337                 case 0: /* Directory tree */
1338                         dirent_type = SMBC_FILE_SHARE;
1339                         break;
1340
1341                 case 1:
1342                         dirent_type = SMBC_PRINTER_SHARE;
1343                         break;
1344
1345                 case 2:
1346                         dirent_type = SMBC_COMMS_SHARE;
1347                         break;
1348
1349                 case 3:
1350                         dirent_type = SMBC_IPC_SHARE;
1351                         break;
1352
1353                 default:
1354                         dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1355                         break;
1356                 }
1357                 ZERO_STRUCTP(dir->dir_list);
1358
1359         }
1360         else dirent_type = dir->dir_type;
1361
1362         if (add_dirent(dir, name, comment, dirent_type) < 0) {
1363
1364                 /* An error occurred, what do we do? */
1365                 /* FIXME: Add some code here */
1366
1367         }
1368
1369 }
1370
1371 static void
1372 dir_list_fn(file_info *finfo, const char *mask, void *state)
1373 {
1374
1375         if (add_dirent((SMBCFILE *)state, finfo->name, "", 
1376                        (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1377
1378                 /* Handle an error ... */
1379
1380                 /* FIXME: Add some code ... */
1381
1382         } 
1383
1384 }
1385
1386 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1387 {
1388         fstring server, share, user, password, workgroup;
1389         pstring path;
1390         SMBCSRV *srv  = NULL;
1391         SMBCFILE *dir = NULL;
1392         struct in_addr rem_ip;
1393         int slot = 0;
1394
1395         if (!context || !context->_initialized) {
1396
1397                 errno = EINVAL;
1398                 return NULL;
1399
1400         }
1401
1402         if (!fname) {
1403     
1404                 errno = EINVAL;
1405                 return NULL;
1406
1407         }
1408
1409         if (smbc_parse_path(context, fname, server, share, path, user, password)) {
1410
1411                 errno = EINVAL;
1412                 return NULL;
1413
1414         }
1415
1416         if (user[0] == (char)0) pstrcpy(user, context->user);
1417
1418         pstrcpy(workgroup, context->workgroup);
1419
1420         dir = malloc(sizeof(*dir));
1421
1422         if (!dir) {
1423
1424                 errno = ENOMEM;
1425                 return NULL;
1426
1427         }
1428
1429         ZERO_STRUCTP(dir);
1430
1431         dir->cli_fd   = 0;
1432         dir->fname    = strdup(fname);
1433         dir->srv      = NULL;
1434         dir->offset   = 0;
1435         dir->file     = False;
1436         dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1437
1438         if (server[0] == (char)0) {
1439
1440                 if (share[0] != (char)0 || path[0] != (char)0) {
1441     
1442                         errno = EINVAL;
1443                         if (dir) {
1444                                 SAFE_FREE(dir->fname);
1445                                 SAFE_FREE(dir);
1446                         }
1447                         return NULL;
1448
1449                 }
1450
1451                 /* We have server and share and path empty ... so list the workgroups */
1452                 /* first try to get the LMB for our workgroup, and if that fails,     */
1453                 /* try the DMB                                                        */
1454
1455                 if (!(resolve_name(context->workgroup, &rem_ip, 0x1d) ||
1456                       resolve_name(context->workgroup, &rem_ip, 0x1b))) {
1457       
1458                         errno = EINVAL;  /* Something wrong with smb.conf? */
1459                         return NULL;
1460
1461                 }
1462
1463                 dir->dir_type = SMBC_WORKGROUP;
1464
1465                 /* find the name of the server ... */
1466
1467                 if (!name_status_find("*", 0, 0, rem_ip, server)) {
1468
1469                         DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server));
1470                         errno = EINVAL;
1471                         return NULL;
1472
1473                 }
1474
1475                 /*
1476                  * Get a connection to IPC$ on the server if we do not already have one
1477                  */
1478
1479                 srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1480
1481                 if (!srv) {
1482
1483                         if (dir) {
1484                                 SAFE_FREE(dir->fname);
1485                                 SAFE_FREE(dir);
1486                         }
1487                         
1488                         return NULL;
1489
1490                 }
1491                 ZERO_STRUCTP(dir->dir_end);
1492
1493                 dir->srv = srv;
1494
1495                 /* Now, list the stuff ... */
1496
1497                 if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
1498                                        (void *)dir)) {
1499
1500                         if (dir) {
1501                                 SAFE_FREE(dir->fname);
1502                                 SAFE_FREE(dir);
1503                         }
1504                         errno = cli_errno(&srv->cli);
1505
1506                         return NULL;
1507
1508                 }
1509         }
1510         else { /* Server not an empty string ... Check the rest and see what gives */
1511
1512                 if (share[0] == (char)0) {
1513
1514                         if (path[0] != (char)0) { /* Should not have empty share with path */
1515
1516                                 errno = EINVAL;
1517                                 if (dir) {
1518                                         SAFE_FREE(dir->fname);
1519                                         SAFE_FREE(dir);
1520                                 }
1521                                 return NULL;
1522         
1523                         }
1524
1525                         /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
1526                         /* However, we check to see if <server> is an IP address first */
1527
1528                         if (!is_ipaddress(server) &&  /* Not an IP addr so check next */
1529                             (resolve_name(server, &rem_ip, 0x1d) ||   /* Found LMB */
1530                                     resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
1531                                 pstring buserver;
1532
1533                                 dir->dir_type = SMBC_SERVER;
1534
1535                                 /*
1536                                  * Get the backup list ...
1537                                  */
1538
1539
1540                                 if (!name_status_find("*", 0, 0, rem_ip, buserver)) {
1541
1542                                         DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
1543                                         errno = EPERM;  /* FIXME, is this correct */
1544                                         return NULL;
1545
1546                                 }
1547
1548                                 /*
1549                                  * Get a connection to IPC$ on the server if we do not already have one
1550                                  */
1551
1552                                 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
1553
1554                                 if (!srv) {
1555
1556                                         if (dir) {
1557                                                 SAFE_FREE(dir->fname);
1558                                                 SAFE_FREE(dir);
1559                                         }
1560                                         return NULL;
1561
1562                                 }
1563
1564                                 dir->srv = srv;
1565
1566                                 /* Now, list the servers ... */
1567
1568                                 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
1569                                                        (void *)dir)) {
1570
1571                                         if (dir) {
1572                                                 SAFE_FREE(dir->fname);
1573                                                 SAFE_FREE(dir);
1574                                         }
1575                                         errno = cli_errno(&srv->cli);
1576                                         return NULL;
1577                                         
1578                                 }
1579
1580                         }
1581                         else {
1582
1583                                 if (resolve_name(server, &rem_ip, 0x20)) {
1584
1585                                         /* Now, list the shares ... */
1586
1587                                         dir->dir_type = SMBC_FILE_SHARE;
1588
1589                                         srv = smbc_server(context, server, "IPC$", workgroup, user, password);
1590
1591                                         if (!srv) {
1592
1593                                                 if (dir) {
1594                                                         SAFE_FREE(dir->fname);
1595                                                         SAFE_FREE(dir);
1596                                                 }
1597                                                 return NULL;
1598
1599                                         }
1600
1601                                         dir->srv = srv;
1602
1603                                         /* Now, list the servers ... */
1604
1605                                         if (cli_RNetShareEnum(&srv->cli, list_fn, 
1606                                                               (void *)dir) < 0) {
1607
1608                                                 errno = cli_errno(&srv->cli);
1609                                                 if (dir) {
1610                                                         SAFE_FREE(dir->fname);
1611                                                         SAFE_FREE(dir);
1612                                                 }
1613                                                 return NULL;
1614
1615                                         }
1616
1617                                 }
1618                                 else {
1619
1620                                         errno = ENODEV;   /* Neither the workgroup nor server exists */
1621                                         if (dir) {
1622                                                 SAFE_FREE(dir->fname);
1623                                                 SAFE_FREE(dir);
1624                                         }
1625                                         return NULL;
1626
1627                                 }
1628
1629                         }
1630
1631                 }
1632                 else { /* The server and share are specified ... work from there ... */
1633
1634                         /* Well, we connect to the server and list the directory */
1635
1636                         dir->dir_type = SMBC_FILE_SHARE;
1637
1638                         srv = smbc_server(context, server, share, workgroup, user, password);
1639
1640                         if (!srv) {
1641
1642                                 if (dir) {
1643                                         SAFE_FREE(dir->fname);
1644                                         SAFE_FREE(dir);
1645                                 }
1646                                 return NULL;
1647
1648                         }
1649
1650                         dir->srv = srv;
1651
1652                         /* Now, list the files ... */
1653
1654                         pstrcat(path, "\\*");
1655
1656                         if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn, 
1657                                      (void *)dir) < 0) {
1658
1659                                 if (dir) {
1660                                         SAFE_FREE(dir->fname);
1661                                         SAFE_FREE(dir);
1662                                 }
1663                                 errno = smbc_errno(context, &srv->cli);
1664                                 return NULL;
1665
1666                         }
1667                 }
1668
1669         }
1670
1671         DLIST_ADD(context->_files, dir);
1672         return dir;
1673
1674 }
1675
1676 /*
1677  * Routine to close a directory
1678  */
1679
1680 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
1681 {
1682
1683         if (!context || !context->_initialized) {
1684
1685                 errno = EINVAL;
1686                 return -1;
1687
1688         }
1689
1690         if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1691
1692                 errno = EBADF;
1693                 return -1;
1694     
1695         }
1696
1697         smbc_remove_dir(dir); /* Clean it up */
1698
1699         DLIST_REMOVE(context->_files, dir);
1700
1701         if (dir) {
1702
1703                 SAFE_FREE(dir->fname);
1704                 SAFE_FREE(dir);    /* Free the space too */
1705
1706         }
1707
1708         return 0;
1709
1710 }
1711
1712 /*
1713  * Routine to get a directory entry
1714  */
1715
1716 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
1717 {
1718         struct smbc_dirent *dirp, *dirent;
1719
1720         /* Check that all is ok first ... */
1721
1722         if (!context || !context->_initialized) {
1723
1724                 errno = EINVAL;
1725                 return NULL;
1726
1727         }
1728
1729         if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1730
1731                 errno = EBADF;
1732                 return NULL;
1733
1734         }
1735
1736         if (dir->file != False) { /* FIXME, should be dir, perhaps */
1737
1738                 errno = ENOTDIR;
1739                 return NULL;
1740
1741         }
1742
1743         if (!dir->dir_next)
1744                 return NULL;
1745         else {
1746
1747                 dirent = dir->dir_next->dirent;
1748
1749                 if (!dirent) {
1750
1751                         errno = ENOENT;
1752                         return NULL;
1753
1754                 }
1755
1756                 /* Hmmm, do I even need to copy it? */
1757
1758                 memcpy(context->_dirent, dirent, dirent->dirlen); /* Copy the dirent */
1759                 dirp = (struct smbc_dirent *)context->_dirent;
1760                 dirp->comment = (char *)(&dirp->name + dirent->namelen + 1);
1761                 dir->dir_next = dir->dir_next->next;
1762
1763                 return (struct smbc_dirent *)context->_dirent;
1764         }
1765
1766 }
1767
1768 /*
1769  * Routine to get directory entries
1770  */
1771
1772 static int smbc_getdents_ctx(SMBCCTX *context, SMBCFILE *dir, struct smbc_dirent *dirp, int count)
1773 {
1774         struct smbc_dir_list *dirlist;
1775         int rem = count, reqd;
1776         char *ndir = (char *)dirp;
1777
1778         /* Check that all is ok first ... */
1779
1780         if (!context || !context->_initialized) {
1781
1782                 errno = EINVAL;
1783                 return -1;
1784
1785         }
1786
1787         if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
1788
1789                 errno = EBADF;
1790                 return -1;
1791     
1792         }
1793
1794         if (dir->file != False) { /* FIXME, should be dir, perhaps */
1795
1796                 errno = ENOTDIR;
1797                 return -1;
1798
1799         }
1800
1801         /* 
1802          * Now, retrieve the number of entries that will fit in what was passed
1803          * We have to figure out if the info is in the list, or we need to 
1804          * send a request to the server to get the info.
1805          */
1806
1807         while ((dirlist = dir->dir_next)) {
1808                 struct smbc_dirent *dirent;
1809
1810                 if (!dirlist->dirent) {
1811
1812                         errno = ENOENT;  /* Bad error */
1813                         return -1;
1814
1815                 }
1816
1817                 if (rem < (reqd = (sizeof(struct smbc_dirent) + dirlist->dirent->namelen + 
1818                                    dirlist->dirent->commentlen + 1))) {
1819
1820                         if (rem < count) { /* We managed to copy something */
1821
1822                                 errno = 0;
1823                                 return count - rem;
1824
1825                         }
1826                         else { /* Nothing copied ... */
1827
1828                                 errno = EINVAL;  /* Not enough space ... */
1829                                 return -1;
1830
1831                         }
1832
1833                 }
1834
1835                 dirent = dirlist->dirent;
1836
1837                 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
1838     
1839                 ((struct smbc_dirent *)ndir)->comment = 
1840                         (char *)(&((struct smbc_dirent *)ndir)->name + dirent->namelen + 1);
1841
1842                 ndir += reqd;
1843
1844                 rem -= reqd;
1845
1846                 dir->dir_next = dirlist = dirlist -> next;
1847         }
1848
1849         if (rem == count)
1850                 return 0;
1851         else 
1852                 return count - rem;
1853
1854 }
1855
1856 /*
1857  * Routine to create a directory ...
1858  */
1859
1860 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
1861 {
1862         SMBCSRV *srv;
1863         fstring server, share, user, password, workgroup;
1864         pstring path;
1865
1866         if (!context || !context->_initialized) {
1867
1868                 errno = EINVAL;
1869                 return -1;
1870
1871         }
1872
1873         if (!fname) {
1874
1875                 errno = EINVAL;
1876                 return -1;
1877
1878         }
1879   
1880         DEBUG(4, ("smbc_mkdir(%s)\n", fname));
1881
1882         smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1883
1884         if (user[0] == (char)0) pstrcpy(user, context->user);
1885
1886         pstrcpy(workgroup, context->workgroup);
1887
1888         srv = smbc_server(context, server, share, workgroup, user, password);
1889
1890         if (!srv) {
1891
1892                 return -1;  /* errno set by smbc_server */
1893
1894         }
1895
1896         /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1897
1898            mode = aDIR | aRONLY;
1899
1900            }
1901            else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1902
1903            if (strcmp(path, "\\") == 0) {
1904
1905            mode = aDIR | aRONLY;
1906
1907            }
1908            else {
1909
1910            mode = aRONLY;
1911            smbc_stat_printjob(srv, path, &size, &m_time);
1912            c_time = a_time = m_time;
1913
1914            }
1915            else { */
1916
1917         if (!cli_mkdir(&srv->cli, path)) {
1918
1919                 errno = smbc_errno(context, &srv->cli);
1920                 return -1;
1921
1922         } 
1923
1924         return 0;
1925
1926 }
1927
1928 /*
1929  * Our list function simply checks to see if a directory is not empty
1930  */
1931
1932 static int smbc_rmdir_dirempty = True;
1933
1934 static void rmdir_list_fn(file_info *finfo, const char *mask, void *state)
1935 {
1936
1937         if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
1938                 smbc_rmdir_dirempty = False;
1939
1940 }
1941
1942 /*
1943  * Routine to remove a directory
1944  */
1945
1946 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
1947 {
1948         SMBCSRV *srv;
1949         fstring server, share, user, password, workgroup;
1950         pstring path;
1951
1952         if (!context || !context->_initialized) {
1953
1954                 errno = EINVAL;
1955                 return -1;
1956
1957         }
1958
1959         if (!fname) {
1960
1961                 errno = EINVAL;
1962                 return -1;
1963
1964         }
1965   
1966         DEBUG(4, ("smbc_rmdir(%s)\n", fname));
1967
1968         smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
1969
1970         if (user[0] == (char)0) pstrcpy(user, context->user);
1971
1972         pstrcpy(workgroup, context->workgroup);
1973
1974         srv = smbc_server(context, server, share, workgroup, user, password);
1975
1976         if (!srv) {
1977
1978                 return -1;  /* errno set by smbc_server */
1979
1980         }
1981
1982         /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
1983
1984            mode = aDIR | aRONLY;
1985
1986            }
1987            else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1988
1989            if (strcmp(path, "\\") == 0) {
1990
1991            mode = aDIR | aRONLY;
1992
1993            }
1994            else {
1995
1996            mode = aRONLY;
1997            smbc_stat_printjob(srv, path, &size, &m_time);
1998            c_time = a_time = m_time;
1999            
2000            }
2001            else { */
2002
2003         if (!cli_rmdir(&srv->cli, path)) {
2004
2005                 errno = smbc_errno(context, &srv->cli);
2006
2007                 if (errno == EACCES) {  /* Check if the dir empty or not */
2008
2009                         pstring lpath; /* Local storage to avoid buffer overflows */
2010
2011                         smbc_rmdir_dirempty = True;  /* Make this so ... */
2012
2013                         pstrcpy(lpath, path);
2014                         pstrcat(lpath, "\\*");
2015
2016                         if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2017                                      NULL) < 0) {
2018
2019                                 /* Fix errno to ignore latest error ... */
2020
2021                                 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n", 
2022                                           smbc_errno(context, &srv->cli)));
2023                                 errno = EACCES;
2024
2025                         }
2026
2027                         if (smbc_rmdir_dirempty)
2028                                 errno = EACCES;
2029                         else
2030                                 errno = ENOTEMPTY;
2031
2032                 }
2033
2034                 return -1;
2035
2036         } 
2037
2038         return 0;
2039
2040 }
2041
2042 /*
2043  * Routine to return the current directory position
2044  */
2045
2046 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2047 {
2048
2049         if (!context || !context->_initialized) {
2050
2051                 errno = EINVAL;
2052                 return -1;
2053
2054         }
2055
2056         if (!dir || !DLIST_CONTAINS(context->_files, dir)) {
2057
2058                 errno = EBADF;
2059                 return -1;
2060
2061         }
2062
2063         if (dir->file != False) { /* FIXME, should be dir, perhaps */
2064
2065                 errno = ENOTDIR;
2066                 return -1;
2067
2068         }
2069
2070         return (off_t) dir->dir_next;
2071
2072 }
2073
2074 /*
2075  * A routine to run down the list and see if the entry is OK
2076  */
2077
2078 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list, 
2079                                          struct smbc_dirent *dirent)
2080 {
2081
2082         /* Run down the list looking for what we want */
2083
2084         if (dirent) {
2085
2086                 struct smbc_dir_list *tmp = list;
2087
2088                 while (tmp) {
2089
2090                         if (tmp->dirent == dirent)
2091                                 return tmp;
2092
2093                         tmp = tmp->next;
2094
2095                 }
2096
2097         }
2098
2099         return NULL;  /* Not found, or an error */
2100
2101 }
2102
2103
2104 /*
2105  * Routine to seek on a directory
2106  */
2107
2108 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2109 {
2110         struct smbc_dirent *dirent = (struct smbc_dirent *)offset;
2111         struct smbc_dir_list *list_ent = NULL;
2112
2113         if (!context || !context->_initialized) {
2114
2115                 errno = EINVAL;
2116                 return -1;
2117
2118         }
2119
2120         if (dir->file != False) { /* FIXME, should be dir, perhaps */
2121
2122                 errno = ENOTDIR;
2123                 return -1;
2124
2125         }
2126
2127         /* Now, check what we were passed and see if it is OK ... */
2128
2129         if (dirent == NULL) {  /* Seek to the begining of the list */
2130
2131                 dir->dir_next = dir->dir_list;
2132                 return 0;
2133
2134         }
2135
2136         /* Now, run down the list and make sure that the entry is OK       */
2137         /* This may need to be changed if we change the format of the list */
2138
2139         if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2140
2141                 errno = EINVAL;   /* Bad entry */
2142                 return -1;
2143
2144         }
2145
2146         dir->dir_next = list_ent;
2147
2148         return 0; 
2149
2150 }
2151
2152 /*
2153  * Routine to fstat a dir
2154  */
2155
2156 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2157 {
2158
2159         if (context || !context->_initialized) {
2160
2161                 errno = EINVAL;
2162                 return -1;
2163
2164         }
2165
2166         /* No code yet ... */
2167
2168         return 0;
2169
2170 }
2171
2172 /*
2173  * Open a print file to be written to by other calls
2174  */
2175
2176 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
2177 {
2178         fstring server, share, user, password;
2179         pstring path;
2180         
2181         if (!context || context->_initialized) {
2182
2183                 errno = EINVAL;
2184                 return NULL;
2185     
2186         }
2187
2188         if (!fname) {
2189
2190                 errno = EINVAL;
2191                 return NULL;
2192
2193         }
2194   
2195         DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
2196
2197         smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2198
2199         /* What if the path is empty, or the file exists? */
2200
2201         return context->open(context, fname, O_WRONLY, 666);
2202
2203 }
2204
2205 /*
2206  * Routine to print a file on a remote server ...
2207  *
2208  * We open the file, which we assume to be on a remote server, and then
2209  * copy it to a print file on the share specified by printq.
2210  */
2211
2212 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
2213 {
2214         SMBCFILE *fid1, *fid2;
2215         int bytes, saverr, tot_bytes = 0;
2216         char buf[4096];
2217
2218         if (!c_file || !c_file->_initialized || !c_print ||
2219             !c_print->_initialized) {
2220
2221                 errno = EINVAL;
2222                 return -1;
2223
2224         }
2225
2226         if (!fname && !printq) {
2227
2228                 errno = EINVAL;
2229                 return -1;
2230
2231         }
2232
2233         /* Try to open the file for reading ... */
2234
2235         if ((fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
2236                 
2237                 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
2238                 return -1;  /* smbc_open sets errno */
2239                 
2240         }
2241
2242         /* Now, try to open the printer file for writing */
2243
2244         if ((fid2 = c_print->open_print_job(c_print, printq)) < 0) {
2245
2246                 saverr = errno;  /* Save errno */
2247                 c_file->close(c_file, fid1);
2248                 errno = saverr;
2249                 return -1;
2250
2251         }
2252
2253         while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
2254
2255                 tot_bytes += bytes;
2256
2257                 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
2258
2259                         saverr = errno;
2260                         c_file->close(c_file, fid1);
2261                         c_print->close(c_print, fid2);
2262                         errno = saverr;
2263
2264                 }
2265
2266         }
2267
2268         saverr = errno;
2269
2270         c_file->close(c_file, fid1);  /* We have to close these anyway */
2271         c_print->close(c_print, fid2);
2272
2273         if (bytes < 0) {
2274
2275                 errno = saverr;
2276                 return -1;
2277
2278         }
2279
2280         return tot_bytes;
2281
2282 }
2283
2284 /*
2285  * Routine to list print jobs on a printer share ...
2286  */
2287
2288 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, void (*fn)(struct print_job_info *))
2289 {
2290         SMBCSRV *srv;
2291         fstring server, share, user, password, workgroup;
2292         pstring path;
2293
2294         if (!context || !context->_initialized) {
2295
2296                 errno = EINVAL;
2297                 return -1;
2298
2299         }
2300
2301         if (!fname) {
2302                 
2303                 errno = EINVAL;
2304                 return -1;
2305
2306         }
2307   
2308         DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
2309
2310         smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2311
2312         if (user[0] == (char)0) pstrcpy(user, context->user);
2313         
2314         pstrcpy(workgroup, context->workgroup);
2315
2316         srv = smbc_server(context, server, share, workgroup, user, password);
2317
2318         if (!srv) {
2319
2320                 return -1;  /* errno set by smbc_server */
2321
2322         }
2323
2324         if (cli_print_queue(&srv->cli, fn) < 0) {
2325
2326                 errno = smbc_errno(context, &srv->cli);
2327                 return -1;
2328
2329         }
2330         
2331         return 0;
2332
2333 }
2334
2335 /*
2336  * Delete a print job from a remote printer share
2337  */
2338
2339 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
2340 {
2341         SMBCSRV *srv;
2342         fstring server, share, user, password, workgroup;
2343         pstring path;
2344         int err;
2345
2346         if (!context || !context->_initialized) {
2347
2348                 errno = EINVAL;
2349                 return -1;
2350
2351         }
2352
2353         if (!fname) {
2354
2355                 errno = EINVAL;
2356                 return -1;
2357
2358         }
2359   
2360         DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
2361
2362         smbc_parse_path(context, fname, server, share, path, user, password); /*FIXME, errors*/
2363
2364         if (user[0] == (char)0) pstrcpy(user, context->user);
2365
2366         pstrcpy(workgroup, context->workgroup);
2367
2368         srv = smbc_server(context, server, share, workgroup, user, password);
2369
2370         if (!srv) {
2371
2372                 return -1;  /* errno set by smbc_server */
2373
2374         }
2375
2376         if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
2377
2378                 if (err < 0)
2379                         errno = smbc_errno(context, &srv->cli);
2380                 else if (err == ERRnosuchprintjob)
2381                         errno = EINVAL;
2382                 return -1;
2383
2384         }
2385
2386         return 0;
2387
2388 }
2389
2390 /*
2391  * Get a new empty handle to fill in with your own info 
2392  */
2393 SMBCCTX * smbc_new_context(void)
2394 {
2395         SMBCCTX * context;
2396
2397         context = malloc(sizeof(*context));
2398         if (!context) {
2399                 errno = ENOMEM;
2400                 return NULL;
2401         }
2402         
2403         ZERO_STRUCTP(context);
2404
2405         /* ADD REASONABLE DEFAULTS */
2406         context->debug            = 0;
2407         context->timeout          = 20000; /* 20 seconds */
2408
2409         context->open             = smbc_open_ctx;
2410         context->creat            = smbc_creat_ctx;
2411         context->read             = smbc_read_ctx;
2412         context->write            = smbc_write_ctx;
2413         context->close            = smbc_close_ctx;
2414         context->unlink           = smbc_unlink_ctx;
2415         context->rename           = smbc_rename_ctx;
2416         context->lseek            = smbc_lseek_ctx;
2417         context->stat             = smbc_stat_ctx;
2418         context->fstat            = smbc_fstat_ctx;
2419         context->opendir          = smbc_opendir_ctx;
2420         context->closedir         = smbc_closedir_ctx;
2421         context->readdir          = smbc_readdir_ctx;
2422         context->getdents         = smbc_getdents_ctx;
2423         context->mkdir            = smbc_mkdir_ctx;
2424         context->rmdir            = smbc_rmdir_ctx;
2425         context->telldir          = smbc_telldir_ctx;
2426         context->lseekdir         = smbc_lseekdir_ctx;
2427         context->fstatdir         = smbc_fstatdir_ctx;
2428         context->open_print_job   = smbc_open_print_job_ctx;
2429         context->print_file       = smbc_print_file_ctx;
2430         context->list_print_jobs  = smbc_list_print_jobs_ctx;
2431         context->unlink_print_job = smbc_unlink_print_job_ctx;
2432
2433         context->callbacks.check_server_fn      = smbc_check_server;
2434         context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
2435
2436         smbc_default_cache_functions(context);
2437
2438         return context;
2439 }
2440
2441 /* 
2442  * Free a context
2443  *
2444  * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed 
2445  * and thus you'll be leaking memory if not handled properly.
2446  *
2447  */
2448 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
2449 {
2450         if (!context) {
2451                 errno = EBADF;
2452                 return 1;
2453         }
2454         
2455         if (shutdown_ctx) {
2456                 SMBCFILE * f;
2457                 DEBUG(1,("Performing aggressive shutdown.\n"));
2458                 
2459                 f = context->_files;
2460                 while (f) {
2461                         context->close(context, f);
2462                         f = f->next;
2463                 }
2464                 context->_files = NULL;
2465
2466                 /* First try to remove the servers the nice way. */
2467                 if (context->callbacks.purge_cached_fn(context)) {
2468                         SMBCSRV * s;
2469                         DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
2470                         s = context->_servers;
2471                         while (s) {
2472                                 cli_shutdown(&s->cli);
2473                                 context->callbacks.remove_cached_srv_fn(context, s);
2474                                 SAFE_FREE(s);
2475                                 s = s->next;
2476                         }
2477                         context->_servers = NULL;
2478                 }
2479         }
2480         else {
2481                 /* This is the polite way */    
2482                 if (context->callbacks.purge_cached_fn(context)) {
2483                         DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
2484                         errno = EBUSY;
2485                         return 1;
2486                 }
2487                 if (context->_servers) {
2488                         DEBUG(1, ("Active servers in context, free_context failed.\n"));
2489                         errno = EBUSY;
2490                         return 1;
2491                 }
2492                 if (context->_files) {
2493                         DEBUG(1, ("Active files in context, free_context failed.\n"));
2494                         errno = EBUSY;
2495                         return 1;
2496                 }               
2497         }
2498
2499         /* Things we have to clean up */
2500         SAFE_FREE(context->workgroup);
2501         SAFE_FREE(context->netbios_name);
2502         SAFE_FREE(context->user);
2503         
2504         DEBUG(3, ("Context %p succesfully freed\n", context));
2505         SAFE_FREE(context);
2506         return 0;
2507 }
2508
2509
2510 /*
2511  * Initialise the library etc 
2512  *
2513  * We accept a struct containing handle information.
2514  * valid values for info->debug from 0 to 100,
2515  * and insist that info->fn must be non-null.
2516  */
2517 SMBCCTX * smbc_init_context(SMBCCTX * context)
2518 {
2519         pstring conf;
2520         int pid;
2521         char *user = NULL, *home = NULL;
2522
2523         if (!context) {
2524                 errno = EBADF;
2525                 return NULL;
2526         }
2527
2528         /* Do not initialise the same client twice */
2529         if (context->_initialized) { 
2530                 return 0;
2531         }
2532
2533         if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
2534
2535                 errno = EINVAL;
2536                 return NULL;
2537
2538         }
2539
2540         if (!smbc_initialized) {
2541                 /* Do some library wide intialisations the first time we get called */
2542
2543                 /* Do we still need this ? */
2544                 DEBUGLEVEL = 10;
2545                 
2546                 setup_logging( "libsmbclient", False);
2547
2548                 /* Here we would open the smb.conf file if needed ... */
2549                 
2550                 home = getenv("HOME");
2551
2552                 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
2553                 
2554                 load_interfaces();  /* Load the list of interfaces ... */
2555                 
2556                 in_client = True; /* FIXME, make a param */
2557
2558                 if (!lp_load(conf, True, False, False)) {
2559
2560                         /*
2561                          * Hmmm, what the hell do we do here ... we could not parse the
2562                          * config file ... We must return an error ... and keep info around
2563                          * about why we failed
2564                          */
2565                         
2566                         errno = ENOENT; /* FIXME: Figure out the correct error response */
2567                         return NULL;
2568                 }
2569
2570                 reopen_logs();  /* Get logging working ... */
2571         
2572                 /* 
2573                  * Block SIGPIPE (from lib/util_sock.c: write())  
2574                  * It is not needed and should not stop execution 
2575                  */
2576                 BlockSignals(True, SIGPIPE);
2577                 
2578                 /* Done with one-time initialisation */
2579                 smbc_initialized = 1; 
2580
2581         }
2582         
2583         if (!context->user) {
2584                 /*
2585                  * FIXME: Is this the best way to get the user info? 
2586                  */
2587                 user = getenv("USER");
2588                 /* walk around as "guest" if no username can be found */
2589                 if (!user) context->user = strdup("guest");
2590                 else context->user = strdup(user);
2591         }
2592
2593         if (!context->netbios_name) {
2594                 /*
2595                  * We try to get our netbios name from the config. If that fails we fall
2596                  * back on constructing our netbios name from our hostname etc
2597                  */
2598                 if (global_myname()) {
2599                         context->netbios_name = strdup(global_myname());
2600                 }
2601                 else {
2602                         /*
2603                          * Hmmm, I want to get hostname as well, but I am too lazy for the moment
2604                          */
2605                         pid = sys_getpid();
2606                         context->netbios_name = malloc(17);
2607                         if (!context->netbios_name) {
2608                                 errno = ENOMEM;
2609                                 return NULL;
2610                         }
2611                         slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
2612                 }
2613         }
2614         DEBUG(0,("Using netbios name %s.\n", context->netbios_name));
2615         
2616
2617         if (!context->workgroup) {
2618                 if (lp_workgroup()) {
2619                         context->workgroup = strdup(lp_workgroup());
2620                 }
2621                 else {
2622                         /* TODO: Think about a decent default workgroup */
2623                         context->workgroup = strdup("samba");
2624                 }
2625         }
2626         DEBUG(0,("Using workgroup %s.\n", context->workgroup));
2627                                         
2628         /* shortest timeout is 1 second */
2629         if (context->timeout > 0 && context->timeout < 1000) 
2630                 context->timeout = 1000;
2631
2632         /*
2633          * FIXME: Should we check the function pointers here? 
2634          */
2635
2636         context->_initialized = 1;
2637         
2638         return context;
2639 }