87ca030b10384ed212520b2b0595bf3023e7aa9d
[samba.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    Copyright (C) Derrell Lipman 2003, 2004
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 #include "include/libsmb_internal.h"
28
29
30 /*
31  * DOS Attribute values (used internally)
32  */
33 typedef struct DOS_ATTR_DESC
34 {
35     int mode;
36     unsigned long long size;
37     time_t a_time;
38     time_t c_time;
39     time_t m_time;
40     unsigned long long inode;
41 } DOS_ATTR_DESC;
42
43
44 /*
45  * Internal flags for extended attributes
46  */
47
48 /* internal mode values */
49 #define SMBC_XATTR_MODE_ADD          1
50 #define SMBC_XATTR_MODE_REMOVE       2
51 #define SMBC_XATTR_MODE_REMOVE_ALL   3
52 #define SMBC_XATTR_MODE_SET          4
53 #define SMBC_XATTR_MODE_CHOWN        5
54 #define SMBC_XATTR_MODE_CHGRP        6
55
56 #define CREATE_ACCESS_READ      READ_CONTROL_ACCESS
57
58 /*We should test for this in configure ... */
59 #ifndef ENOTSUP
60 #define ENOTSUP EOPNOTSUPP
61 #endif
62
63 /*
64  * Functions exported by libsmb_cache.c that we need here
65  */
66 int smbc_default_cache_functions(SMBCCTX *context);
67
68 /* 
69  * check if an element is part of the list. 
70  * FIXME: Does not belong here !  
71  * Can anyone put this in a macro in dlinklist.h ?
72  * -- Tom
73  */
74 static int DLIST_CONTAINS(SMBCFILE * list, SMBCFILE *p) {
75         if (!p || !list) return False;
76         do {
77                 if (p == list) return True;
78                 list = list->next;
79         } while (list);
80         return False;
81 }
82
83 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file);
84 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence);
85
86 extern BOOL in_client;
87
88 /*
89  * Is the logging working / configfile read ? 
90  */
91 static int smbc_initialized = 0;
92
93 static int 
94 hex2int( unsigned int _char )
95 {
96     if ( _char >= 'A' && _char <='F')
97         return _char - 'A' + 10;
98     if ( _char >= 'a' && _char <='f')
99         return _char - 'a' + 10;
100     if ( _char >= '0' && _char <='9')
101         return _char - '0';
102     return -1;
103 }
104
105 /*
106  * smbc_urldecode()
107  *
108  * Convert strings of %xx to their single character equivalent.  Each 'x' must
109  * be a valid hexadecimal digit, or that % sequence is left undecoded.
110  *
111  * dest may, but need not be, the same pointer as src.
112  *
113  * Returns the number of % sequences which could not be converted due to lack
114  * of two following hexadecimal digits.
115  */
116 int
117 smbc_urldecode(char *dest, char * src, size_t max_dest_len)
118 {
119         int old_length = strlen(src);
120         int i = 0;
121         int err_count = 0;
122         pstring temp;
123         char * p;
124
125         if ( old_length == 0 ) {
126                 return 0;
127         }
128
129         p = temp;
130         while ( i < old_length ) {
131                 unsigned char character = src[ i++ ];
132
133                 if (character == '%') {
134                         int a = i+1 < old_length ? hex2int( src[i] ) : -1;
135                         int b = i+1 < old_length ? hex2int( src[i+1] ) : -1;
136
137                         /* Replace valid sequence */
138                         if (a != -1 && b != -1) {
139
140                                 /* Replace valid %xx sequence with %dd */
141                                 character = (a * 16) + b;
142
143                                 if (character == '\0') {
144                                         break; /* Stop at %00 */
145                                 }
146
147                                 i += 2;
148                         } else {
149
150                                 err_count++;
151                         }
152                 }
153
154                 *p++ = character;
155         }
156
157         *p = '\0';
158
159         strncpy(dest, temp, max_dest_len);
160
161         return err_count;
162 }
163
164 /*
165  * smbc_urlencode()
166  *
167  * Convert any characters not specifically allowed in a URL into their %xx
168  * equivalent.
169  *
170  * Returns the remaining buffer length.
171  */
172 int
173 smbc_urlencode(char * dest, char * src, int max_dest_len)
174 {
175         char hex[] = "0123456789ABCDEF";
176
177         for (; *src != '\0' && max_dest_len >= 3; src++) {
178
179                 if ((*src < '0' &&
180                      *src != '-' &&
181                      *src != '.') ||
182                     (*src > '9' &&
183                      *src < 'A') ||
184                     (*src > 'Z' &&
185                      *src < 'a' &&
186                      *src != '_') ||
187                     (*src > 'z')) {
188                         *dest++ = '%';
189                         *dest++ = hex[(*src >> 4) & 0x0f];
190                         *dest++ = hex[*src & 0x0f];
191                         max_dest_len -= 3;
192                 } else {
193                         *dest++ = *src;
194                         max_dest_len--;
195                 }
196         }
197
198         *dest++ = '\0';
199         max_dest_len--;
200         
201         return max_dest_len;
202 }
203
204 /*
205  * Function to parse a path and turn it into components
206  *
207  * The general format of an SMB URI is explain in Christopher Hertel's CIFS
208  * book, at http://ubiqx.org/cifs/Appendix-D.html.  We accept a subset of the
209  * general format ("smb:" only; we do not look for "cifs:").
210  *
211  *
212  * We accept:
213  *  smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]][?options]
214  *
215  * Meaning of URLs:
216  *
217  * smb://           Show all workgroups.
218  *
219  *                  The method of locating the list of workgroups varies
220  *                  depending upon the setting of the context variable
221  *                  context->options.browse_max_lmb_count.  This value
222  *                  determine the maximum number of local master browsers to
223  *                  query for the list of workgroups.  In order to ensure that
224  *                  a complete list of workgroups is obtained, all master
225  *                  browsers must be queried, but if there are many
226  *                  workgroups, the time spent querying can begin to add up.
227  *                  For small networks (not many workgroups), it is suggested
228  *                  that this variable be set to 0, indicating query all local
229  *                  master browsers.  When the network has many workgroups, a
230  *                  reasonable setting for this variable might be around 3.
231  *
232  * smb://name/      if name<1D> or name<1B> exists, list servers in
233  *                  workgroup, else, if name<20> exists, list all shares
234  *                  for server ...
235  *
236  * If "options" are provided, this function returns the entire option list as a
237  * string, for later parsing by the caller.  Note that currently, no options
238  * are supported.
239  */
240
241 static const char *smbc_prefix = "smb:";
242
243 static int
244 smbc_parse_path(SMBCCTX *context,
245                 const char *fname,
246                 char *server, int server_len,
247                 char *share, int share_len,
248                 char *path, int path_len,
249                 char *user, int user_len,
250                 char *password, int password_len,
251                 char *options, int options_len)
252 {
253         static pstring s;
254         pstring userinfo;
255         const char *p;
256         char *q, *r;
257         int len;
258
259         server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
260         if (options != NULL && options_len > 0) {
261                 options[0] = (char)0;
262         }
263         pstrcpy(s, fname);
264
265         /* see if it has the right prefix */
266         len = strlen(smbc_prefix);
267         if (strncmp(s,smbc_prefix,len) || (s[len] != '/' && s[len] != 0)) {
268                 return -1; /* What about no smb: ? */
269         }
270
271         p = s + len;
272
273         /* Watch the test below, we are testing to see if we should exit */
274
275         if (strncmp(p, "//", 2) && strncmp(p, "\\\\", 2)) {
276
277                 DEBUG(1, ("Invalid path (does not begin with smb://"));
278                 return -1;
279
280         }
281
282         p += 2;  /* Skip the double slash */
283
284         /* See if any options were specified */
285         if ((q = strrchr(p, '?')) != NULL ) {
286                 /* There are options.  Null terminate here and point to them */
287                 *q++ = '\0';
288                 
289                 DEBUG(4, ("Found options '%s'", q));
290
291                 /* Copy the options */
292                 if (options != NULL && options_len > 0) {
293                         safe_strcpy(options, q, options_len - 1);
294                 }
295         }
296
297         if (*p == (char)0)
298             goto decoding;
299
300         if (*p == '/') {
301
302                 strncpy(server, context->workgroup, 
303                         (strlen(context->workgroup) < 16)?strlen(context->workgroup):16);
304                 return 0;
305                 
306         }
307
308         /*
309          * ok, its for us. Now parse out the server, share etc. 
310          *
311          * However, we want to parse out [[domain;]user[:password]@] if it
312          * exists ...
313          */
314
315         /* check that '@' occurs before '/', if '/' exists at all */
316         q = strchr_m(p, '@');
317         r = strchr_m(p, '/');
318         if (q && (!r || q < r)) {
319                 pstring username, passwd, domain;
320                 const char *u = userinfo;
321
322                 next_token(&p, userinfo, "@", sizeof(fstring));
323
324                 username[0] = passwd[0] = domain[0] = 0;
325
326                 if (strchr_m(u, ';')) {
327       
328                         next_token(&u, domain, ";", sizeof(fstring));
329
330                 }
331
332                 if (strchr_m(u, ':')) {
333
334                         next_token(&u, username, ":", sizeof(fstring));
335
336                         pstrcpy(passwd, u);
337
338                 }
339                 else {
340
341                         pstrcpy(username, u);
342
343                 }
344
345                 if (username[0])
346                         strncpy(user, username, user_len);  /* FIXME, domain */
347
348                 if (passwd[0])
349                         strncpy(password, passwd, password_len);
350
351         }
352
353         if (!next_token(&p, server, "/", sizeof(fstring))) {
354
355                 return -1;
356
357         }
358
359         if (*p == (char)0) goto decoding;  /* That's it ... */
360   
361         if (!next_token(&p, share, "/", sizeof(fstring))) {
362
363                 return -1;
364
365         }
366
367         safe_strcpy(path, p, path_len - 1);
368
369         all_string_sub(path, "/", "\\", 0);
370
371  decoding:
372         (void) smbc_urldecode(path, path, path_len);
373         (void) smbc_urldecode(server, server, server_len);
374         (void) smbc_urldecode(share, share, share_len);
375         (void) smbc_urldecode(user, user, user_len);
376         (void) smbc_urldecode(password, password, password_len);
377
378         return 0;
379 }
380
381 /*
382  * Verify that the options specified in a URL are valid
383  */
384 static int smbc_check_options(char *server, char *share, char *path, char *options)
385 {
386         DEBUG(4, ("smbc_check_options(): server='%s' share='%s' path='%s' options='%s'\n", server, share, path, options));
387
388         /* No options at all is always ok */
389         if (! *options) return 0;
390
391         /* Currently, we don't support any options. */
392         return -1;
393 }
394
395 /*
396  * Convert an SMB error into a UNIX error ...
397  */
398 static int smbc_errno(SMBCCTX *context, struct cli_state *c)
399 {
400         int ret = cli_errno(c);
401         
402         if (cli_is_dos_error(c)) {
403                 uint8 eclass;
404                 uint32 ecode;
405
406                 cli_dos_error(c, &eclass, &ecode);
407                 
408                 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n", 
409                          (int)eclass, (int)ecode, (int)ecode, ret));
410         } else {
411                 NTSTATUS status;
412
413                 status = cli_nt_error(c);
414
415                 DEBUG(3,("smbc errno %s -> %d\n",
416                          nt_errstr(status), ret));
417         }
418
419         return ret;
420 }
421
422 /* 
423  * Check a server_fd.
424  * returns 0 if the server is in shape. Returns 1 on error 
425  * 
426  * Also useable outside libsmbclient to enable external cache
427  * to do some checks too.
428  */
429 int smbc_check_server(SMBCCTX * context, SMBCSRV * server) 
430 {
431         if ( send_keepalive(server->cli.fd) == False )
432                 return 1;
433
434         /* connection is ok */
435         return 0;
436 }
437
438 /* 
439  * Remove a server from the cached server list it's unused.
440  * On success, 0 is returned. 1 is returned if the server could not be removed.
441  * 
442  * Also useable outside libsmbclient
443  */
444 int smbc_remove_unused_server(SMBCCTX * context, SMBCSRV * srv)
445 {
446         SMBCFILE * file;
447
448         /* are we being fooled ? */
449         if (!context || !context->internal ||
450             !context->internal->_initialized || !srv) return 1;
451
452         
453         /* Check all open files/directories for a relation with this server */
454         for (file = context->internal->_files; file; file=file->next) {
455                 if (file->srv == srv) {
456                         /* Still used */
457                         DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n", 
458                                   srv, file));
459                         return 1;
460                 }
461         }
462
463         DLIST_REMOVE(context->internal->_servers, srv);
464
465         cli_shutdown(&srv->cli);
466
467         DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
468
469         context->callbacks.remove_cached_srv_fn(context, srv);
470         
471         return 0;
472 }
473
474 SMBCSRV *find_server(SMBCCTX *context,
475                      const char *server,
476                      const char *share,
477                      fstring workgroup,
478                      fstring username,
479                      fstring password)
480 {
481         SMBCSRV *srv;
482         int auth_called = 0;
483         
484  check_server_cache:
485
486         srv = context->callbacks.get_cached_srv_fn(context, server, share, 
487                                                    workgroup, username);
488
489         if (!auth_called && !srv && (!username[0] || !password[0])) {
490                 context->callbacks.auth_fn(server, share,
491                                            workgroup, sizeof(fstring),
492                                            username, sizeof(fstring),
493                                            password, sizeof(fstring));
494                 /*
495                  * However, smbc_auth_fn may have picked up info relating to
496                  * an existing connection, so try for an existing connection
497                  * again ...
498                  */
499                 auth_called = 1;
500                 goto check_server_cache;
501                 
502         }
503         
504         if (srv) {
505                 if (context->callbacks.check_server_fn(context, srv)) {
506                         /*
507                          * This server is no good anymore 
508                          * Try to remove it and check for more possible
509                          * servers in the cache
510                          */
511                         if (context->callbacks.remove_unused_server_fn(context,
512                                                                        srv)) { 
513                                 /*
514                                  * We could not remove the server completely,
515                                  * remove it from the cache so we will not get
516                                  * it again. It will be removed when the last
517                                  * file/dir is closed.
518                                  */
519                                 context->callbacks.remove_cached_srv_fn(context,
520                                                                         srv);
521                         }
522                         
523                         /*
524                          * Maybe there are more cached connections to this
525                          * server
526                          */
527                         goto check_server_cache; 
528                 }
529
530                 return srv;
531         }
532
533         return NULL;
534 }
535
536 /*
537  * Connect to a server, possibly on an existing connection
538  *
539  * Here, what we want to do is: If the server and username
540  * match an existing connection, reuse that, otherwise, establish a 
541  * new connection.
542  *
543  * If we have to create a new connection, call the auth_fn to get the
544  * info we need, unless the username and password were passed in.
545  */
546
547 SMBCSRV *smbc_server(SMBCCTX *context,
548                      const char *server, const char *share, 
549                      fstring workgroup, fstring username, 
550                      fstring password)
551 {
552         SMBCSRV *srv=NULL;
553         struct cli_state c;
554         struct nmb_name called, calling;
555         const char *server_n = server;
556         pstring ipenv;
557         struct in_addr ip;
558         int tried_reverse = 0;
559         int port_try_first;
560         int port_try_next;
561   
562         zero_ip(&ip);
563         ZERO_STRUCT(c);
564
565         if (server[0] == 0) {
566                 errno = EPERM;
567                 return NULL;
568         }
569
570         srv = find_server(context, server, share,
571                           workgroup, username, password);
572
573         /*
574          * If we found a connection and we're only allowed one share per
575          * server...
576          */
577         if (srv && *share != '\0' && context->options.one_share_per_server) {
578
579                 /*
580                  * ... then if there's no current connection to the share,
581                  * connect to it.  find_server(), or rather the function
582                  * pointed to by context->callbacks.get_cached_srv_fn which
583                  * was called by find_server(), will have issued a tree
584                  * disconnect if the requested share is not the same as the
585                  * one that was already connected.
586                  */
587                 if (srv->cli.cnum == (uint16) -1) {
588                         /* Ensure we have accurate auth info */
589                         context->callbacks.auth_fn(server, share,
590                                                    workgroup, sizeof(fstring),
591                                                    username, sizeof(fstring),
592                                                    password, sizeof(fstring));
593
594                         if (! cli_send_tconX(&srv->cli, share, "?????",
595                                              password, strlen(password)+1)) {
596                         
597                                 errno = smbc_errno(context, &srv->cli);
598                                 cli_shutdown(&srv->cli);
599                                 context->callbacks.remove_cached_srv_fn(context, srv);
600                                 srv = NULL;
601                         }
602
603                         /* Regenerate the dev value since it's based on both server and share */
604                         if (srv) {
605                                 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
606                         }
607                 }
608         }
609         
610         /* If we have a connection... */
611         if (srv) {
612
613                 /* ... then we're done here.  Give 'em what they came for. */
614                 return srv;
615         }
616
617         make_nmb_name(&calling, context->netbios_name, 0x0);
618         make_nmb_name(&called , server, 0x20);
619
620         DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n, server));
621   
622         DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
623
624  again:
625         slprintf(ipenv,sizeof(ipenv)-1,"HOST_%s", server_n);
626
627         zero_ip(&ip);
628
629         /* have to open a new connection */
630         if (!cli_initialise(&c)) {
631                 errno = ENOMEM;
632                 return NULL;
633         }
634
635         if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
636                 c.use_kerberos = True;
637         }
638         if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
639                 c.fallback_after_kerberos = True;
640         }
641
642         c.timeout = context->timeout;
643
644         /*
645          * Force use of port 139 for first try if share is $IPC, empty, or
646          * null, so browse lists can work
647          */
648         if (share == NULL || *share == '\0' || strcmp(share, "IPC$") == 0)
649         {
650                 port_try_first = 139;
651                 port_try_next = 445;
652         }
653         else
654         {
655                 port_try_first = 445;
656                 port_try_next = 139;
657         }
658
659         c.port = port_try_first;
660
661         if (!cli_connect(&c, server_n, &ip)) {
662
663                 /* First connection attempt failed.  Try alternate port. */
664                 c.port = port_try_next;
665
666                 if (!cli_connect(&c, server_n, &ip)) {
667                         cli_shutdown(&c);
668                         errno = ETIMEDOUT;
669                         return NULL;
670                 }
671         }
672
673         if (!cli_session_request(&c, &calling, &called)) {
674                 cli_shutdown(&c);
675                 if (strcmp(called.name, "*SMBSERVER")) {
676                         make_nmb_name(&called , "*SMBSERVER", 0x20);
677                         goto again;
678                 }
679                 else {  /* Try one more time, but ensure we don't loop */
680
681                   /* Only try this if server is an IP address ... */
682
683                   if (is_ipaddress(server) && !tried_reverse) {
684                     fstring remote_name;
685                     struct in_addr rem_ip;
686
687                     if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
688                       DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
689                       errno = ETIMEDOUT;
690                       return NULL;
691                     }
692
693                     tried_reverse++; /* Yuck */
694
695                     if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
696                       make_nmb_name(&called, remote_name, 0x20);
697                       goto again;
698                     }
699
700
701                   }
702                 }
703                 errno = ETIMEDOUT;
704                 return NULL;
705         }
706   
707         DEBUG(4,(" session request ok\n"));
708   
709         if (!cli_negprot(&c)) {
710                 cli_shutdown(&c);
711                 errno = ETIMEDOUT;
712                 return NULL;
713         }
714
715         if (!cli_session_setup(&c, username, 
716                                password, strlen(password),
717                                password, strlen(password),
718                                workgroup) &&
719                         /* Try an anonymous login if it failed and this was allowed by flags. */
720                         ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
721                         !cli_session_setup(&c, "", "", 1,"", 0, workgroup))) {
722                 cli_shutdown(&c);
723                 errno = EPERM;
724                 return NULL;
725         }
726
727         DEBUG(4,(" session setup ok\n"));
728
729         if (!cli_send_tconX(&c, share, "?????",
730                             password, strlen(password)+1)) {
731                 errno = smbc_errno(context, &c);
732                 cli_shutdown(&c);
733                 return NULL;
734         }
735   
736         DEBUG(4,(" tconx ok\n"));
737   
738         /*
739          * Ok, we have got a nice connection
740          * Let's find a free server_fd 
741          */
742
743         srv = SMB_MALLOC_P(SMBCSRV);
744         if (!srv) {
745                 errno = ENOMEM;
746                 goto failed;
747         }
748
749         ZERO_STRUCTP(srv);
750         srv->cli = c;
751         srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
752
753         /* now add it to the cache (internal or external)  */
754         /* Let the cache function set errno if it wants to */
755         errno = 0;
756         if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
757                 int saved_errno = errno;
758                 DEBUG(3, (" Failed to add server to cache\n"));
759                 errno = saved_errno;
760                 if (errno == 0) {
761                         errno = ENOMEM;
762                 }
763                 goto failed;
764         }
765         
766         DEBUG(2, ("Server connect ok: //%s/%s: %p\n", 
767                   server, share, srv));
768
769         DLIST_ADD(context->internal->_servers, srv);
770         return srv;
771
772  failed:
773         cli_shutdown(&c);
774         if (!srv) return NULL;
775   
776         SAFE_FREE(srv);
777         return NULL;
778 }
779
780 /*
781  * Connect to a server for getting/setting attributes, possibly on an existing
782  * connection.  This works similarly to smbc_server().
783  */
784 SMBCSRV *smbc_attr_server(SMBCCTX *context,
785                           const char *server, const char *share, 
786                           fstring workgroup,
787                           fstring username, fstring password,
788                           POLICY_HND *pol)
789 {
790         struct in_addr ip;
791         struct cli_state *ipc_cli;
792         NTSTATUS nt_status;
793         SMBCSRV *ipc_srv=NULL;
794
795         /*
796          * See if we've already created this special connection.  Reference our
797          * "special" share name '*IPC$', which is an impossible real share name
798          * due to the leading asterisk.
799          */
800         ipc_srv = find_server(context, server, "*IPC$",
801                               workgroup, username, password);
802         if (!ipc_srv) {
803
804                 /* We didn't find a cached connection.  Get the password */
805                 if (*password == '\0') {
806                         /* ... then retrieve it now. */
807                         context->callbacks.auth_fn(server, share,
808                                                    workgroup, sizeof(fstring),
809                                                    username, sizeof(fstring),
810                                                    password, sizeof(fstring));
811                 }
812         
813                 zero_ip(&ip);
814                 nt_status = cli_full_connection(&ipc_cli,
815                                                 global_myname(), server, 
816                                                 &ip, 0, "IPC$", "?????",  
817                                                 username, workgroup,
818                                                 password, 0,
819                                                 Undefined, NULL);
820                 if (! NT_STATUS_IS_OK(nt_status)) {
821                         DEBUG(1,("cli_full_connection failed! (%s)\n",
822                                  nt_errstr(nt_status)));
823                         errno = ENOTSUP;
824                         return NULL;
825                 }
826
827                 if (!cli_nt_session_open(ipc_cli, PI_LSARPC)) {
828                         DEBUG(1, ("cli_nt_session_open fail!\n"));
829                         errno = ENOTSUP;
830                         cli_shutdown(ipc_cli);
831                         return NULL;
832                 }
833
834                 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
835                    but NT sends 0x2000000 so we might as well do it too. */
836         
837                 nt_status = cli_lsa_open_policy(ipc_cli,
838                                                 ipc_cli->mem_ctx,
839                                                 True, 
840                                                 GENERIC_EXECUTE_ACCESS,
841                                                 pol);
842         
843                 if (!NT_STATUS_IS_OK(nt_status)) {
844                         errno = smbc_errno(context, ipc_cli);
845                         cli_shutdown(ipc_cli);
846                         return NULL;
847                 }
848
849                 ipc_srv = SMB_MALLOC_P(SMBCSRV);
850                 if (!ipc_srv) {
851                         errno = ENOMEM;
852                         cli_shutdown(ipc_cli);
853                         return NULL;
854                 }
855
856                 ZERO_STRUCTP(ipc_srv);
857                 ipc_srv->cli = *ipc_cli;
858
859                 free(ipc_cli);
860
861                 /* now add it to the cache (internal or external) */
862
863                 errno = 0;      /* let cache function set errno if it likes */
864                 if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
865                                                          server,
866                                                          "*IPC$",
867                                                          workgroup,
868                                                          username)) {
869                         DEBUG(3, (" Failed to add server to cache\n"));
870                         if (errno == 0) {
871                                 errno = ENOMEM;
872                         }
873                         cli_shutdown(&ipc_srv->cli);
874                         free(ipc_srv);
875                         return NULL;
876                 }
877
878                 DLIST_ADD(context->internal->_servers, ipc_srv);
879         }
880
881         return ipc_srv;
882 }
883
884 /*
885  * Routine to open() a file ...
886  */
887
888 static SMBCFILE *smbc_open_ctx(SMBCCTX *context, const char *fname, int flags, mode_t mode)
889 {
890         fstring server, share, user, password, workgroup;
891         pstring path;
892         SMBCSRV *srv   = NULL;
893         SMBCFILE *file = NULL;
894         int fd;
895
896         if (!context || !context->internal ||
897             !context->internal->_initialized) {
898
899                 errno = EINVAL;  /* Best I can think of ... */
900                 return NULL;
901
902         }
903
904         if (!fname) {
905
906                 errno = EINVAL;
907                 return NULL;
908
909         }
910
911         if (smbc_parse_path(context, fname,
912                             server, sizeof(server),
913                             share, sizeof(share),
914                             path, sizeof(path),
915                             user, sizeof(user),
916                             password, sizeof(password),
917                             NULL, 0)) {
918                 errno = EINVAL;
919                 return NULL;
920         }
921
922         if (user[0] == (char)0) fstrcpy(user, context->user);
923
924         fstrcpy(workgroup, context->workgroup);
925
926         srv = smbc_server(context, server, share, workgroup, user, password);
927
928         if (!srv) {
929
930                 if (errno == EPERM) errno = EACCES;
931                 return NULL;  /* smbc_server sets errno */
932     
933         }
934
935         /* Hmmm, the test for a directory is suspect here ... FIXME */
936
937         if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
938     
939                 fd = -1;
940
941         }
942         else {
943           
944                 file = SMB_MALLOC_P(SMBCFILE);
945
946                 if (!file) {
947
948                         errno = ENOMEM;
949                         return NULL;
950
951                 }
952
953                 ZERO_STRUCTP(file);
954
955                 if ((fd = cli_open(&srv->cli, path, flags, DENY_NONE)) < 0) {
956
957                         /* Handle the error ... */
958
959                         SAFE_FREE(file);
960                         errno = smbc_errno(context, &srv->cli);
961                         return NULL;
962
963                 }
964
965                 /* Fill in file struct */
966
967                 file->cli_fd  = fd;
968                 file->fname   = SMB_STRDUP(fname);
969                 file->srv     = srv;
970                 file->offset  = 0;
971                 file->file    = True;
972
973                 DLIST_ADD(context->internal->_files, file);
974
975                 /*
976                  * If the file was opened in O_APPEND mode, all write
977                  * operations should be appended to the file.  To do that,
978                  * though, using this protocol, would require a getattrE()
979                  * call for each and every write, to determine where the end
980                  * of the file is. (There does not appear to be an append flag
981                  * in the protocol.)  Rather than add all of that overhead of
982                  * retrieving the current end-of-file offset prior to each
983                  * write operation, we'll assume that most append operations
984                  * will continuously write, so we'll just set the offset to
985                  * the end of the file now and hope that's adequate.
986                  *
987                  * Note to self: If this proves inadequate, and O_APPEND
988                  * should, in some cases, be forced for each write, add a
989                  * field in the context options structure, for
990                  * "strict_append_mode" which would select between the current
991                  * behavior (if FALSE) or issuing a getattrE() prior to each
992                  * write and forcing the write to the end of the file (if
993                  * TRUE).  Adding that capability will likely require adding
994                  * an "append" flag into the _SMBCFILE structure to track
995                  * whether a file was opened in O_APPEND mode.  -- djl
996                  */
997                 if (flags & O_APPEND) {
998                         if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
999                                 (void) smbc_close_ctx(context, file);
1000                                 errno = ENXIO;
1001                                 return NULL;
1002                         }
1003                 }
1004
1005                 return file;
1006
1007         }
1008
1009         /* Check if opendir needed ... */
1010
1011         if (fd == -1) {
1012                 int eno = 0;
1013
1014                 eno = smbc_errno(context, &srv->cli);
1015                 file = context->opendir(context, fname);
1016                 if (!file) errno = eno;
1017                 return file;
1018
1019         }
1020
1021         errno = EINVAL; /* FIXME, correct errno ? */
1022         return NULL;
1023
1024 }
1025
1026 /*
1027  * Routine to create a file 
1028  */
1029
1030 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1031
1032 static SMBCFILE *smbc_creat_ctx(SMBCCTX *context, const char *path, mode_t mode)
1033 {
1034
1035         if (!context || !context->internal ||
1036             !context->internal->_initialized) {
1037
1038                 errno = EINVAL;
1039                 return NULL;
1040
1041         }
1042
1043         return smbc_open_ctx(context, path, creat_bits, mode);
1044 }
1045
1046 /*
1047  * Routine to read() a file ...
1048  */
1049
1050 static ssize_t smbc_read_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
1051 {
1052         int ret;
1053
1054         /*
1055          * offset:
1056          *
1057          * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1058          * appears to pass file->offset (which is type off_t) differently than
1059          * a local variable of type off_t.  Using local variable "offset" in
1060          * the call to cli_read() instead of file->offset fixes a problem
1061          * retrieving data at an offset greater than 4GB.
1062          */
1063         off_t offset = file->offset;
1064
1065         if (!context || !context->internal ||
1066             !context->internal->_initialized) {
1067
1068                 errno = EINVAL;
1069                 return -1;
1070
1071         }
1072
1073         DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1074
1075         if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1076
1077                 errno = EBADF;
1078                 return -1;
1079
1080         }
1081
1082         /* Check that the buffer exists ... */
1083
1084         if (buf == NULL) {
1085
1086                 errno = EINVAL;
1087                 return -1;
1088
1089         }
1090
1091         ret = cli_read(&file->srv->cli, file->cli_fd, buf, offset, count);
1092
1093         if (ret < 0) {
1094
1095                 errno = smbc_errno(context, &file->srv->cli);
1096                 return -1;
1097
1098         }
1099
1100         file->offset += ret;
1101
1102         DEBUG(4, ("  --> %d\n", ret));
1103
1104         return ret;  /* Success, ret bytes of data ... */
1105
1106 }
1107
1108 /*
1109  * Routine to write() a file ...
1110  */
1111
1112 static ssize_t smbc_write_ctx(SMBCCTX *context, SMBCFILE *file, void *buf, size_t count)
1113 {
1114         int ret;
1115         off_t offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1116
1117         if (!context || !context->internal ||
1118             !context->internal->_initialized) {
1119
1120                 errno = EINVAL;
1121                 return -1;
1122
1123         }
1124
1125         if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1126
1127                 errno = EBADF;
1128                 return -1;
1129     
1130         }
1131
1132         /* Check that the buffer exists ... */
1133
1134         if (buf == NULL) {
1135
1136                 errno = EINVAL;
1137                 return -1;
1138
1139         }
1140
1141         ret = cli_write(&file->srv->cli, file->cli_fd, 0, buf, offset, count);
1142
1143         if (ret <= 0) {
1144
1145                 errno = smbc_errno(context, &file->srv->cli);
1146                 return -1;
1147
1148         }
1149
1150         file->offset += ret;
1151
1152         return ret;  /* Success, 0 bytes of data ... */
1153 }
1154  
1155 /*
1156  * Routine to close() a file ...
1157  */
1158
1159 static int smbc_close_ctx(SMBCCTX *context, SMBCFILE *file)
1160 {
1161         SMBCSRV *srv; 
1162
1163         if (!context || !context->internal ||
1164             !context->internal->_initialized) {
1165
1166                 errno = EINVAL;
1167                 return -1;
1168
1169         }
1170
1171         if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1172    
1173                 errno = EBADF;
1174                 return -1;
1175
1176         }
1177
1178         /* IS a dir ... */
1179         if (!file->file) {
1180                 
1181                 return context->closedir(context, file);
1182
1183         }
1184
1185         if (!cli_close(&file->srv->cli, file->cli_fd)) {
1186
1187                 DEBUG(3, ("cli_close failed on %s. purging server.\n", 
1188                           file->fname));
1189                 /* Deallocate slot and remove the server 
1190                  * from the server cache if unused */
1191                 errno = smbc_errno(context, &file->srv->cli);  
1192                 srv = file->srv;
1193                 DLIST_REMOVE(context->internal->_files, file);
1194                 SAFE_FREE(file->fname);
1195                 SAFE_FREE(file);
1196                 context->callbacks.remove_unused_server_fn(context, srv);
1197
1198                 return -1;
1199
1200         }
1201
1202         DLIST_REMOVE(context->internal->_files, file);
1203         SAFE_FREE(file->fname);
1204         SAFE_FREE(file);
1205
1206         return 0;
1207 }
1208
1209 /*
1210  * Get info from an SMB server on a file. Use a qpathinfo call first
1211  * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1212  */
1213 static BOOL smbc_getatr(SMBCCTX * context, SMBCSRV *srv, char *path, 
1214                  uint16 *mode, SMB_OFF_T *size, 
1215                  time_t *c_time, time_t *a_time, time_t *m_time,
1216                  SMB_INO_T *ino)
1217 {
1218
1219         if (!context || !context->internal ||
1220             !context->internal->_initialized) {
1221  
1222                 errno = EINVAL;
1223                 return -1;
1224  
1225         }
1226
1227         DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1228   
1229         if (!srv->no_pathinfo2 &&
1230             cli_qpathinfo2(&srv->cli, path, c_time, a_time, m_time, NULL,
1231                            size, mode, ino)) return True;
1232
1233         /* if this is NT then don't bother with the getatr */
1234         if (srv->cli.capabilities & CAP_NT_SMBS) {
1235                 errno = EPERM;
1236                 return False;
1237         }
1238
1239         if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
1240                 a_time = c_time = m_time;
1241                 srv->no_pathinfo2 = True;
1242                 return True;
1243         }
1244
1245         errno = EPERM;
1246         return False;
1247
1248 }
1249
1250 /*
1251  * Routine to unlink() a file
1252  */
1253
1254 static int smbc_unlink_ctx(SMBCCTX *context, const char *fname)
1255 {
1256         fstring server, share, user, password, workgroup;
1257         pstring path;
1258         SMBCSRV *srv = NULL;
1259
1260         if (!context || !context->internal ||
1261             !context->internal->_initialized) {
1262
1263                 errno = EINVAL;  /* Best I can think of ... */
1264                 return -1;
1265
1266         }
1267
1268         if (!fname) {
1269
1270                 errno = EINVAL;
1271                 return -1;
1272
1273         }
1274
1275         if (smbc_parse_path(context, fname,
1276                             server, sizeof(server),
1277                             share, sizeof(share),
1278                             path, sizeof(path),
1279                             user, sizeof(user),
1280                             password, sizeof(password),
1281                             NULL, 0)) {
1282                 errno = EINVAL;
1283                 return -1;
1284         }
1285
1286         if (user[0] == (char)0) fstrcpy(user, context->user);
1287
1288         fstrcpy(workgroup, context->workgroup);
1289
1290         srv = smbc_server(context, server, share, workgroup, user, password);
1291
1292         if (!srv) {
1293
1294                 return -1;  /* smbc_server sets errno */
1295
1296         }
1297
1298         /*  if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
1299
1300     int job = smbc_stat_printjob(srv, path, NULL, NULL);
1301     if (job == -1) {
1302
1303       return -1;
1304
1305     }
1306     if ((err = cli_printjob_del(&srv->cli, job)) != 0) {
1307
1308     
1309       return -1;
1310
1311     }
1312     } else */
1313
1314         if (!cli_unlink(&srv->cli, path)) {
1315
1316                 errno = smbc_errno(context, &srv->cli);
1317
1318                 if (errno == EACCES) { /* Check if the file is a directory */
1319
1320                         int saverr = errno;
1321                         SMB_OFF_T size = 0;
1322                         uint16 mode = 0;
1323                         time_t m_time = 0, a_time = 0, c_time = 0;
1324                         SMB_INO_T ino = 0;
1325
1326                         if (!smbc_getatr(context, srv, path, &mode, &size,
1327                                          &c_time, &a_time, &m_time, &ino)) {
1328
1329                                 /* Hmmm, bad error ... What? */
1330
1331                                 errno = smbc_errno(context, &srv->cli);
1332                                 return -1;
1333
1334                         }
1335                         else {
1336
1337                                 if (IS_DOS_DIR(mode))
1338                                         errno = EISDIR;
1339                                 else
1340                                         errno = saverr;  /* Restore this */
1341
1342                         }
1343                 }
1344
1345                 return -1;
1346
1347         }
1348
1349         return 0;  /* Success ... */
1350
1351 }
1352
1353 /*
1354  * Routine to rename() a file
1355  */
1356
1357 static int smbc_rename_ctx(SMBCCTX *ocontext, const char *oname, 
1358                            SMBCCTX *ncontext, const char *nname)
1359 {
1360         fstring server1, share1, server2, share2, user1, user2, password1, password2, workgroup;
1361         pstring path1, path2;
1362         SMBCSRV *srv = NULL;
1363
1364         if (!ocontext || !ncontext || 
1365             !ocontext->internal || !ncontext->internal ||
1366             !ocontext->internal->_initialized || 
1367             !ncontext->internal->_initialized) {
1368
1369                 errno = EINVAL;  /* Best I can think of ... */
1370                 return -1;
1371
1372         }
1373         
1374         if (!oname || !nname) {
1375
1376                 errno = EINVAL;
1377                 return -1;
1378
1379         }
1380         
1381         DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
1382
1383         smbc_parse_path(ocontext, oname,
1384                         server1, sizeof(server1),
1385                         share1, sizeof(share1),
1386                         path1, sizeof(path1),
1387                         user1, sizeof(user1),
1388                         password1, sizeof(password1),
1389                         NULL, 0);
1390
1391         if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
1392
1393         smbc_parse_path(ncontext, nname,
1394                         server2, sizeof(server2),
1395                         share2, sizeof(share2),
1396                         path2, sizeof(path2),
1397                         user2, sizeof(user2),
1398                         password2, sizeof(password2),
1399                         NULL, 0);
1400
1401         if (user2[0] == (char)0) fstrcpy(user2, ncontext->user);
1402
1403         if (strcmp(server1, server2) || strcmp(share1, share2) ||
1404             strcmp(user1, user2)) {
1405
1406                 /* Can't rename across file systems, or users?? */
1407
1408                 errno = EXDEV;
1409                 return -1;
1410
1411         }
1412
1413         fstrcpy(workgroup, ocontext->workgroup);
1414         /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */ 
1415         srv = smbc_server(ocontext, server1, share1, workgroup, user1, password1);
1416         if (!srv) {
1417
1418                 return -1;
1419
1420         }
1421
1422         if (!cli_rename(&srv->cli, path1, path2)) {
1423                 int eno = smbc_errno(ocontext, &srv->cli);
1424
1425                 if (eno != EEXIST ||
1426                     !cli_unlink(&srv->cli, path2) ||
1427                     !cli_rename(&srv->cli, path1, path2)) {
1428
1429                         errno = eno;
1430                         return -1;
1431
1432                 }
1433         }
1434
1435         return 0; /* Success */
1436
1437 }
1438
1439 /*
1440  * A routine to lseek() a file
1441  */
1442
1443 static off_t smbc_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence)
1444 {
1445         SMB_OFF_T size;
1446
1447         if (!context || !context->internal ||
1448             !context->internal->_initialized) {
1449
1450                 errno = EINVAL;
1451                 return -1;
1452                 
1453         }
1454
1455         if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1456
1457                 errno = EBADF;
1458                 return -1;
1459
1460         }
1461
1462         if (!file->file) {
1463
1464                 errno = EINVAL;
1465                 return -1;      /* Can't lseek a dir ... */
1466
1467         }
1468
1469         switch (whence) {
1470         case SEEK_SET:
1471                 file->offset = offset;
1472                 break;
1473
1474         case SEEK_CUR:
1475                 file->offset += offset;
1476                 break;
1477
1478         case SEEK_END:
1479                 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd, NULL, &size, NULL, NULL,
1480                                    NULL, NULL, NULL)) 
1481                 {
1482                     SMB_BIG_UINT b_size = size;
1483                     if (!cli_getattrE(&file->srv->cli, file->cli_fd, NULL, &b_size, NULL, NULL,
1484                                       NULL)) 
1485                     {
1486                         errno = EINVAL;
1487                         return -1;
1488                     } else
1489                         size = b_size;
1490                 }
1491                 file->offset = size + offset;
1492                 break;
1493
1494         default:
1495                 errno = EINVAL;
1496                 break;
1497
1498         }
1499
1500         return file->offset;
1501
1502 }
1503
1504 /* 
1505  * Generate an inode number from file name for those things that need it
1506  */
1507
1508 static
1509 ino_t smbc_inode(SMBCCTX *context, const char *name)
1510 {
1511
1512         if (!context || !context->internal ||
1513             !context->internal->_initialized) {
1514
1515                 errno = EINVAL;
1516                 return -1;
1517
1518         }
1519
1520         if (!*name) return 2; /* FIXME, why 2 ??? */
1521         return (ino_t)str_checksum(name);
1522
1523 }
1524
1525 /*
1526  * Routine to put basic stat info into a stat structure ... Used by stat and
1527  * fstat below.
1528  */
1529
1530 static
1531 int smbc_setup_stat(SMBCCTX *context, struct stat *st, char *fname,
1532                     SMB_OFF_T size, int mode)
1533 {
1534         
1535         st->st_mode = 0;
1536
1537         if (IS_DOS_DIR(mode)) {
1538                 st->st_mode = SMBC_DIR_MODE;
1539         } else {
1540                 st->st_mode = SMBC_FILE_MODE;
1541         }
1542
1543         if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
1544         if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
1545         if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
1546         if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
1547
1548         st->st_size = size;
1549 #ifdef HAVE_STAT_ST_BLKSIZE
1550         st->st_blksize = 512;
1551 #endif
1552 #ifdef HAVE_STAT_ST_BLOCKS
1553         st->st_blocks = (size+511)/512;
1554 #endif
1555         st->st_uid = getuid();
1556         st->st_gid = getgid();
1557
1558         if (IS_DOS_DIR(mode)) {
1559                 st->st_nlink = 2;
1560         } else {
1561                 st->st_nlink = 1;
1562         }
1563
1564         if (st->st_ino == 0) {
1565                 st->st_ino = smbc_inode(context, fname);
1566         }
1567         
1568         return True;  /* FIXME: Is this needed ? */
1569
1570 }
1571
1572 /*
1573  * Routine to stat a file given a name
1574  */
1575
1576 static int smbc_stat_ctx(SMBCCTX *context, const char *fname, struct stat *st)
1577 {
1578         SMBCSRV *srv;
1579         fstring server, share, user, password, workgroup;
1580         pstring path;
1581         time_t m_time = 0, a_time = 0, c_time = 0;
1582         SMB_OFF_T size = 0;
1583         uint16 mode = 0;
1584         SMB_INO_T ino = 0;
1585
1586         if (!context || !context->internal ||
1587             !context->internal->_initialized) {
1588
1589                 errno = EINVAL;  /* Best I can think of ... */
1590                 return -1;
1591     
1592         }
1593
1594         if (!fname) {
1595
1596                 errno = EINVAL;
1597                 return -1;
1598
1599         }
1600   
1601         DEBUG(4, ("smbc_stat(%s)\n", fname));
1602
1603         if (smbc_parse_path(context, fname,
1604                             server, sizeof(server),
1605                             share, sizeof(share),
1606                             path, sizeof(path),
1607                             user, sizeof(user),
1608                             password, sizeof(password),
1609                             NULL, 0)) {
1610                 errno = EINVAL;
1611                 return -1;
1612         }
1613
1614         if (user[0] == (char)0) fstrcpy(user, context->user);
1615
1616         fstrcpy(workgroup, context->workgroup);
1617
1618         srv = smbc_server(context, server, share, workgroup, user, password);
1619
1620         if (!srv) {
1621                 return -1;  /* errno set by smbc_server */
1622         }
1623
1624         if (!smbc_getatr(context, srv, path, &mode, &size, 
1625                          &c_time, &a_time, &m_time, &ino)) {
1626
1627                 errno = smbc_errno(context, &srv->cli);
1628                 return -1;
1629                 
1630         }
1631
1632         st->st_ino = ino;
1633
1634         smbc_setup_stat(context, st, path, size, mode);
1635
1636         st->st_atime = a_time;
1637         st->st_ctime = c_time;
1638         st->st_mtime = m_time;
1639         st->st_dev   = srv->dev;
1640
1641         return 0;
1642
1643 }
1644
1645 /*
1646  * Routine to stat a file given an fd
1647  */
1648
1649 static int smbc_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st)
1650 {
1651         time_t c_time, a_time, m_time;
1652         SMB_OFF_T size;
1653         uint16 mode;
1654         SMB_INO_T ino = 0;
1655
1656         if (!context || !context->internal ||
1657             !context->internal->_initialized) {
1658
1659                 errno = EINVAL;
1660                 return -1;
1661
1662         }
1663
1664         if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1665
1666                 errno = EBADF;
1667                 return -1;
1668
1669         }
1670
1671         if (!file->file) {
1672
1673                 return context->fstatdir(context, file, st);
1674
1675         }
1676
1677         if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1678                            &mode, &size, &c_time, &a_time, &m_time, NULL, &ino)) {
1679             if (!cli_getattrE(&file->srv->cli, file->cli_fd,
1680                           &mode, &size, &c_time, &a_time, &m_time)) {
1681
1682                 errno = EINVAL;
1683                 return -1;
1684             }
1685         }
1686
1687         st->st_ino = ino;
1688
1689         smbc_setup_stat(context, st, file->fname, size, mode);
1690
1691         st->st_atime = a_time;
1692         st->st_ctime = c_time;
1693         st->st_mtime = m_time;
1694         st->st_dev = file->srv->dev;
1695
1696         return 0;
1697
1698 }
1699
1700 /*
1701  * Routine to open a directory
1702  * We accept the URL syntax explained in smbc_parse_path(), above.
1703  */
1704
1705 static void smbc_remove_dir(SMBCFILE *dir)
1706 {
1707         struct smbc_dir_list *d,*f;
1708
1709         d = dir->dir_list;
1710         while (d) {
1711
1712                 f = d; d = d->next;
1713
1714                 SAFE_FREE(f->dirent);
1715                 SAFE_FREE(f);
1716
1717         }
1718
1719         dir->dir_list = dir->dir_end = dir->dir_next = NULL;
1720
1721 }
1722
1723 static int add_dirent(SMBCFILE *dir, const char *name, const char *comment, uint32 type)
1724 {
1725         struct smbc_dirent *dirent;
1726         int size;
1727         int name_length = (name == NULL ? 0 : strlen(name));
1728         int comment_len = (comment == NULL ? 0 : strlen(comment));
1729
1730         /*
1731          * Allocate space for the dirent, which must be increased by the 
1732          * size of the name and the comment and 1 each for the null terminator.
1733          */
1734
1735         size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
1736     
1737         dirent = SMB_MALLOC(size);
1738
1739         if (!dirent) {
1740
1741                 dir->dir_error = ENOMEM;
1742                 return -1;
1743
1744         }
1745
1746         ZERO_STRUCTP(dirent);
1747
1748         if (dir->dir_list == NULL) {
1749
1750                 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
1751                 if (!dir->dir_list) {
1752
1753                         SAFE_FREE(dirent);
1754                         dir->dir_error = ENOMEM;
1755                         return -1;
1756
1757                 }
1758                 ZERO_STRUCTP(dir->dir_list);
1759
1760                 dir->dir_end = dir->dir_next = dir->dir_list;
1761         }
1762         else {
1763
1764                 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
1765                 
1766                 if (!dir->dir_end->next) {
1767                         
1768                         SAFE_FREE(dirent);
1769                         dir->dir_error = ENOMEM;
1770                         return -1;
1771
1772                 }
1773                 ZERO_STRUCTP(dir->dir_end->next);
1774
1775                 dir->dir_end = dir->dir_end->next;
1776         }
1777
1778         dir->dir_end->next = NULL;
1779         dir->dir_end->dirent = dirent;
1780         
1781         dirent->smbc_type = type;
1782         dirent->namelen = name_length;
1783         dirent->commentlen = comment_len;
1784         dirent->dirlen = size;
1785   
1786         strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
1787
1788         dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
1789         strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
1790         
1791         return 0;
1792
1793 }
1794
1795 static void
1796 list_unique_wg_fn(const char *name, uint32 type, const char *comment, void *state)
1797 {
1798         SMBCFILE *dir = (SMBCFILE *)state;
1799         struct smbc_dir_list *dir_list;
1800         struct smbc_dirent *dirent;
1801         int dirent_type;
1802         int do_remove = 0;
1803
1804         dirent_type = dir->dir_type;
1805
1806         if (add_dirent(dir, name, comment, dirent_type) < 0) {
1807
1808                 /* An error occurred, what do we do? */
1809                 /* FIXME: Add some code here */
1810         }
1811
1812         /* Point to the one just added */
1813         dirent = dir->dir_end->dirent;
1814
1815         /* See if this was a duplicate */
1816         for (dir_list = dir->dir_list;
1817              dir_list != dir->dir_end;
1818              dir_list = dir_list->next) {
1819                 if (! do_remove &&
1820                     strcmp(dir_list->dirent->name, dirent->name) == 0) {
1821                         /* Duplicate.  End end of list need to be removed. */
1822                         do_remove = 1;
1823                 }
1824
1825                 if (do_remove && dir_list->next == dir->dir_end) {
1826                         /* Found the end of the list.  Remove it. */
1827                         dir->dir_end = dir_list;
1828                         free(dir_list->next);
1829                         dir_list->next = NULL;
1830                         break;
1831                 }
1832         }
1833 }
1834
1835 static void
1836 list_fn(const char *name, uint32 type, const char *comment, void *state)
1837 {
1838         SMBCFILE *dir = (SMBCFILE *)state;
1839         int dirent_type;
1840
1841         /* We need to process the type a little ... */
1842
1843         if (dir->dir_type == SMBC_FILE_SHARE) {
1844                 
1845                 switch (type) {
1846                 case 0: /* Directory tree */
1847                         dirent_type = SMBC_FILE_SHARE;
1848                         break;
1849
1850                 case 1:
1851                         dirent_type = SMBC_PRINTER_SHARE;
1852                         break;
1853
1854                 case 2:
1855                         dirent_type = SMBC_COMMS_SHARE;
1856                         break;
1857
1858                 case 3:
1859                         dirent_type = SMBC_IPC_SHARE;
1860                         break;
1861
1862                 default:
1863                         dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
1864                         break;
1865                 }
1866         }
1867         else dirent_type = dir->dir_type;
1868
1869         if (add_dirent(dir, name, comment, dirent_type) < 0) {
1870
1871                 /* An error occurred, what do we do? */
1872                 /* FIXME: Add some code here */
1873
1874         }
1875 }
1876
1877 static void
1878 dir_list_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
1879 {
1880
1881         if (add_dirent((SMBCFILE *)state, finfo->name, "", 
1882                        (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
1883
1884                 /* Handle an error ... */
1885
1886                 /* FIXME: Add some code ... */
1887
1888         } 
1889
1890 }
1891
1892 static SMBCFILE *smbc_opendir_ctx(SMBCCTX *context, const char *fname)
1893 {
1894         fstring server, share, user, password, options;
1895         pstring workgroup;
1896         pstring path;
1897         SMBCSRV *srv  = NULL;
1898         SMBCFILE *dir = NULL;
1899         struct in_addr rem_ip;
1900
1901         if (!context || !context->internal ||
1902             !context->internal->_initialized) {
1903                 DEBUG(4, ("no valid context\n"));
1904                 errno = EINVAL + 8192;
1905                 return NULL;
1906
1907         }
1908
1909         if (!fname) {
1910                 DEBUG(4, ("no valid fname\n"));
1911                 errno = EINVAL + 8193;
1912                 return NULL;
1913         }
1914
1915         if (smbc_parse_path(context, fname,
1916                             server, sizeof(server),
1917                             share, sizeof(share),
1918                             path, sizeof(path),
1919                             user, sizeof(user),
1920                             password, sizeof(password),
1921                             options, sizeof(options))) {
1922                 DEBUG(4, ("no valid path\n"));
1923                 errno = EINVAL + 8194;
1924                 return NULL;
1925         }
1926
1927         DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s' options='%s'\n", fname, server, share, path, options));
1928
1929         /* Ensure the options are valid */
1930         if (smbc_check_options(server, share, path, options)) {
1931                 DEBUG(4, ("unacceptable options (%s)\n", options));
1932                 errno = EINVAL + 8195;
1933                 return NULL;
1934         }
1935
1936         if (user[0] == (char)0) fstrcpy(user, context->user);
1937
1938         pstrcpy(workgroup, context->workgroup);
1939
1940         dir = SMB_MALLOC_P(SMBCFILE);
1941
1942         if (!dir) {
1943
1944                 errno = ENOMEM;
1945                 return NULL;
1946
1947         }
1948
1949         ZERO_STRUCTP(dir);
1950
1951         dir->cli_fd   = 0;
1952         dir->fname    = SMB_STRDUP(fname);
1953         dir->srv      = NULL;
1954         dir->offset   = 0;
1955         dir->file     = False;
1956         dir->dir_list = dir->dir_next = dir->dir_end = NULL;
1957
1958         if (server[0] == (char)0) {
1959
1960                 int i;
1961                 int count;
1962                 int max_lmb_count;
1963                 struct ip_service *ip_list;
1964                 struct ip_service server_addr;
1965                 struct user_auth_info u_info;
1966                 struct cli_state *cli;
1967
1968                 if (share[0] != (char)0 || path[0] != (char)0) {
1969
1970                         errno = EINVAL + 8196;
1971                         if (dir) {
1972                                 SAFE_FREE(dir->fname);
1973                                 SAFE_FREE(dir);
1974                         }
1975                         return NULL;
1976                 }
1977
1978                 /* Determine how many local master browsers to query */
1979                 max_lmb_count = (context->options.browse_max_lmb_count == 0
1980                                  ? INT_MAX
1981                                  : context->options.browse_max_lmb_count);
1982
1983                 pstrcpy(u_info.username, user);
1984                 pstrcpy(u_info.password, password);
1985
1986                 /*
1987                  * We have server and share and path empty but options
1988                  * requesting that we scan all master browsers for their list
1989                  * of workgroups/domains.  This implies that we must first try
1990                  * broadcast queries to find all master browsers, and if that
1991                  * doesn't work, then try our other methods which return only
1992                  * a single master browser.
1993                  */
1994
1995                 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
1996                         if (!find_master_ip(workgroup, &server_addr.ip)) {
1997
1998                                 errno = ENOENT;
1999                                 return NULL;
2000                         }
2001
2002                         ip_list = &server_addr;
2003                         count = 1;
2004                 }
2005
2006                 for (i = 0; i < count && i < max_lmb_count; i++) {
2007                         DEBUG(99, ("Found master browser %d of %d: %s\n", i+1, MAX(count, max_lmb_count), inet_ntoa(ip_list[i].ip)));
2008                         
2009                         cli = get_ipc_connect_master_ip(&ip_list[i], workgroup, &u_info);
2010                         /* cli == NULL is the master browser refused to talk or 
2011                            could not be found */
2012                         if ( !cli )
2013                                 continue;
2014
2015                         fstrcpy(server, cli->desthost);
2016                         cli_shutdown(cli);
2017
2018                         DEBUG(4, ("using workgroup %s %s\n", workgroup, server));
2019
2020                         /*
2021                          * For each returned master browser IP address, get a
2022                          * connection to IPC$ on the server if we do not
2023                          * already have one, and determine the
2024                          * workgroups/domains that it knows about.
2025                          */
2026                 
2027                         srv = smbc_server(context, server,
2028                                           "IPC$", workgroup, user, password);
2029                         if (!srv) {
2030                                 continue;
2031                         }
2032                 
2033                         dir->srv = srv;
2034                         dir->dir_type = SMBC_WORKGROUP;
2035
2036                         /* Now, list the stuff ... */
2037                         
2038                         if (!cli_NetServerEnum(&srv->cli, workgroup, SV_TYPE_DOMAIN_ENUM, list_unique_wg_fn,
2039                                                (void *)dir)) {
2040                                 
2041                                 continue;
2042                         }
2043                 }
2044         } else { 
2045                 /*
2046                  * Server not an empty string ... Check the rest and see what
2047                  * gives
2048                  */
2049                 if (share[0] == (char)0) {
2050
2051                         if (path[0] != (char)0) { /* Should not have empty share with path */
2052
2053                                 errno = EINVAL + 8197;
2054                                 if (dir) {
2055                                         SAFE_FREE(dir->fname);
2056                                         SAFE_FREE(dir);
2057                                 }
2058                                 return NULL;
2059         
2060                         }
2061
2062                         /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
2063                         /* However, we check to see if <server> is an IP address first */
2064
2065                         if (!is_ipaddress(server) &&  /* Not an IP addr so check next */
2066                             (resolve_name(server, &rem_ip, 0x1d) ||   /* Found LMB */
2067                                     resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
2068                                 fstring buserver;
2069
2070                                 dir->dir_type = SMBC_SERVER;
2071
2072                                 /*
2073                                  * Get the backup list ...
2074                                  */
2075
2076
2077                                 if (!name_status_find(server, 0, 0, rem_ip, buserver)) {
2078
2079                                         DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
2080                                         errno = EPERM;  /* FIXME, is this correct */
2081                                         return NULL;
2082
2083                                 }
2084
2085                                 /*
2086                                  * Get a connection to IPC$ on the server if we do not already have one
2087                                  */
2088
2089                                 srv = smbc_server(context, buserver, "IPC$", workgroup, user, password);
2090
2091                                 if (!srv) {
2092                                         DEBUG(0, ("got no contact to IPC$\n"));
2093                                         if (dir) {
2094                                                 SAFE_FREE(dir->fname);
2095                                                 SAFE_FREE(dir);
2096                                         }
2097                                         return NULL;
2098
2099                                 }
2100
2101                                 dir->srv = srv;
2102
2103                                 /* Now, list the servers ... */
2104
2105                                 if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
2106                                                        (void *)dir)) {
2107
2108                                         if (dir) {
2109                                                 SAFE_FREE(dir->fname);
2110                                                 SAFE_FREE(dir);
2111                                         }
2112                                         return NULL;
2113                                         
2114                                 }
2115                         }
2116                         else {
2117
2118                                 if (resolve_name(server, &rem_ip, 0x20)) {
2119
2120                                         /* Now, list the shares ... */
2121
2122                                         dir->dir_type = SMBC_FILE_SHARE;
2123
2124                                         srv = smbc_server(context, server, "IPC$", workgroup, user, password);
2125
2126                                         if (!srv) {
2127
2128                                                 if (dir) {
2129                                                         SAFE_FREE(dir->fname);
2130                                                         SAFE_FREE(dir);
2131                                                 }
2132                                                 return NULL;
2133
2134                                         }
2135
2136                                         dir->srv = srv;
2137
2138                                         /* Now, list the servers ... */
2139
2140                                         if (cli_RNetShareEnum(&srv->cli, list_fn, 
2141                                                               (void *)dir) < 0) {
2142
2143                                                 errno = cli_errno(&srv->cli);
2144                                                 if (dir) {
2145                                                         SAFE_FREE(dir->fname);
2146                                                         SAFE_FREE(dir);
2147                                                 }
2148                                                 return NULL;
2149
2150                                         }
2151
2152                                 }
2153                                 else {
2154
2155                                         errno = ECONNREFUSED;   /* Neither the workgroup nor server exists */
2156                                         if (dir) {
2157                                                 SAFE_FREE(dir->fname);
2158                                                 SAFE_FREE(dir);
2159                                         }
2160                                         return NULL;
2161
2162                                 }
2163
2164                         }
2165
2166                 }
2167                 else { /* The server and share are specified ... work from there ... */
2168
2169                         /* Well, we connect to the server and list the directory */
2170
2171                         dir->dir_type = SMBC_FILE_SHARE;
2172
2173                         srv = smbc_server(context, server, share, workgroup, user, password);
2174
2175                         if (!srv) {
2176
2177                                 if (dir) {
2178                                         SAFE_FREE(dir->fname);
2179                                         SAFE_FREE(dir);
2180                                 }
2181                                 return NULL;
2182
2183                         }
2184
2185                         dir->srv = srv;
2186
2187                         /* Now, list the files ... */
2188
2189                         pstrcat(path, "\\*");
2190
2191                         if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn, 
2192                                      (void *)dir) < 0) {
2193
2194                                 if (dir) {
2195                                         SAFE_FREE(dir->fname);
2196                                         SAFE_FREE(dir);
2197                                 }
2198                                 errno = smbc_errno(context, &srv->cli);
2199                                 return NULL;
2200
2201                         }
2202                 }
2203
2204         }
2205
2206         DLIST_ADD(context->internal->_files, dir);
2207         return dir;
2208
2209 }
2210
2211 /*
2212  * Routine to close a directory
2213  */
2214
2215 static int smbc_closedir_ctx(SMBCCTX *context, SMBCFILE *dir)
2216 {
2217
2218         if (!context || !context->internal ||
2219             !context->internal->_initialized) {
2220
2221                 errno = EINVAL;
2222                 return -1;
2223
2224         }
2225
2226         if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2227
2228                 errno = EBADF;
2229                 return -1;
2230     
2231         }
2232
2233         smbc_remove_dir(dir); /* Clean it up */
2234
2235         DLIST_REMOVE(context->internal->_files, dir);
2236
2237         if (dir) {
2238
2239                 SAFE_FREE(dir->fname);
2240                 SAFE_FREE(dir);    /* Free the space too */
2241         }
2242
2243         return 0;
2244
2245 }
2246
2247 static void smbc_readdir_internal(SMBCCTX * context,
2248                                   struct smbc_dirent *dest,
2249                                   struct smbc_dirent *src,
2250                                   int max_namebuf_len)
2251 {
2252         if (context->options.urlencode_readdir_entries) {
2253
2254                 /* url-encode the name.  get back remaining buffer space */
2255                 max_namebuf_len =
2256                         smbc_urlencode(dest->name, src->name, max_namebuf_len);
2257
2258                 /* We now know the name length */
2259                 dest->namelen = strlen(dest->name);
2260
2261                 /* Save the pointer to the beginning of the comment */
2262                 dest->comment = dest->name + dest->namelen + 1;
2263
2264                 /* Copy the comment */
2265                 strncpy(dest->comment, src->comment, max_namebuf_len);
2266
2267                 /* Ensure the comment is null terminated */
2268                 if (max_namebuf_len > src->commentlen) {
2269                         dest->comment[src->commentlen] = '\0';
2270                 } else {
2271                         dest->comment[max_namebuf_len - 1] = '\0';
2272                 }
2273
2274                 /* Save other fields */
2275                 dest->smbc_type = src->smbc_type;
2276                 dest->commentlen = strlen(dest->comment);
2277                 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
2278                                 (char *) dest);
2279         } else {
2280
2281                 /* No encoding.  Just copy the entry as is. */
2282                 memcpy(dest, src, src->dirlen);
2283                 dest->comment = (char *)(&dest->name + src->namelen + 1);
2284         }
2285         
2286 }
2287
2288 /*
2289  * Routine to get a directory entry
2290  */
2291
2292 struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir)
2293 {
2294         int maxlen;
2295         struct smbc_dirent *dirp, *dirent;
2296
2297         /* Check that all is ok first ... */
2298
2299         if (!context || !context->internal ||
2300             !context->internal->_initialized) {
2301
2302                 errno = EINVAL;
2303                 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
2304                 return NULL;
2305
2306         }
2307
2308         if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2309
2310                 errno = EBADF;
2311                 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
2312                 return NULL;
2313
2314         }
2315
2316         if (dir->file != False) { /* FIXME, should be dir, perhaps */
2317
2318                 errno = ENOTDIR;
2319                 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
2320                 return NULL;
2321
2322         }
2323
2324         if (!dir->dir_next) {
2325                 return NULL;
2326         }
2327
2328         dirent = dir->dir_next->dirent;
2329         if (!dirent) {
2330
2331                 errno = ENOENT;
2332                 return NULL;
2333
2334         }
2335
2336         dirp = (struct smbc_dirent *)context->internal->_dirent;
2337         maxlen = (sizeof(context->internal->_dirent) -
2338                   sizeof(struct smbc_dirent));
2339
2340         smbc_readdir_internal(context, dirp, dirent, maxlen);
2341
2342         dir->dir_next = dir->dir_next->next;
2343
2344         return dirp;
2345 }
2346
2347 /*
2348  * Routine to get directory entries
2349  */
2350
2351 static int smbc_getdents_ctx(SMBCCTX *context,
2352                              SMBCFILE *dir,
2353                              struct smbc_dirent *dirp,
2354                              int count)
2355 {
2356         int rem = count;
2357         int reqd;
2358         int maxlen;
2359         char *ndir = (char *)dirp;
2360         struct smbc_dir_list *dirlist;
2361
2362         /* Check that all is ok first ... */
2363
2364         if (!context || !context->internal ||
2365             !context->internal->_initialized) {
2366
2367                 errno = EINVAL;
2368                 return -1;
2369
2370         }
2371
2372         if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2373
2374                 errno = EBADF;
2375                 return -1;
2376     
2377         }
2378
2379         if (dir->file != False) { /* FIXME, should be dir, perhaps */
2380
2381                 errno = ENOTDIR;
2382                 return -1;
2383
2384         }
2385
2386         /* 
2387          * Now, retrieve the number of entries that will fit in what was passed
2388          * We have to figure out if the info is in the list, or we need to 
2389          * send a request to the server to get the info.
2390          */
2391
2392         while ((dirlist = dir->dir_next)) {
2393                 struct smbc_dirent *dirent;
2394
2395                 if (!dirlist->dirent) {
2396
2397                         errno = ENOENT;  /* Bad error */
2398                         return -1;
2399
2400                 }
2401
2402                 /* Do urlencoding of next entry, if so selected */
2403                 dirent = (struct smbc_dirent *)context->internal->_dirent;
2404                 maxlen = (sizeof(context->internal->_dirent) -
2405                           sizeof(struct smbc_dirent));
2406                 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
2407
2408                 reqd = dirent->dirlen;
2409
2410                 if (rem < reqd) {
2411
2412                         if (rem < count) { /* We managed to copy something */
2413
2414                                 errno = 0;
2415                                 return count - rem;
2416
2417                         }
2418                         else { /* Nothing copied ... */
2419
2420                                 errno = EINVAL;  /* Not enough space ... */
2421                                 return -1;
2422
2423                         }
2424
2425                 }
2426
2427                 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
2428     
2429                 ((struct smbc_dirent *)ndir)->comment = 
2430                         (char *)(&((struct smbc_dirent *)ndir)->name +
2431                                  dirent->namelen +
2432                                  1);
2433
2434                 ndir += reqd;
2435
2436                 rem -= reqd;
2437
2438                 dir->dir_next = dirlist = dirlist -> next;
2439         }
2440
2441         if (rem == count)
2442                 return 0;
2443         else 
2444                 return count - rem;
2445
2446 }
2447
2448 /*
2449  * Routine to create a directory ...
2450  */
2451
2452 static int smbc_mkdir_ctx(SMBCCTX *context, const char *fname, mode_t mode)
2453 {
2454         SMBCSRV *srv;
2455         fstring server, share, user, password, workgroup;
2456         pstring path;
2457
2458         if (!context || !context->internal || 
2459             !context->internal->_initialized) {
2460
2461                 errno = EINVAL;
2462                 return -1;
2463
2464         }
2465
2466         if (!fname) {
2467
2468                 errno = EINVAL;
2469                 return -1;
2470
2471         }
2472   
2473         DEBUG(4, ("smbc_mkdir(%s)\n", fname));
2474
2475         if (smbc_parse_path(context, fname,
2476                             server, sizeof(server),
2477                             share, sizeof(share),
2478                             path, sizeof(path),
2479                             user, sizeof(user),
2480                             password, sizeof(password),
2481                             NULL, 0)) {
2482                 errno = EINVAL;
2483                 return -1;
2484         }
2485
2486         if (user[0] == (char)0) fstrcpy(user, context->user);
2487
2488         fstrcpy(workgroup, context->workgroup);
2489
2490         srv = smbc_server(context, server, share, workgroup, user, password);
2491
2492         if (!srv) {
2493
2494                 return -1;  /* errno set by smbc_server */
2495
2496         }
2497
2498         if (!cli_mkdir(&srv->cli, path)) {
2499
2500                 errno = smbc_errno(context, &srv->cli);
2501                 return -1;
2502
2503         } 
2504
2505         return 0;
2506
2507 }
2508
2509 /*
2510  * Our list function simply checks to see if a directory is not empty
2511  */
2512
2513 static int smbc_rmdir_dirempty = True;
2514
2515 static void rmdir_list_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
2516 {
2517
2518         if (strncmp(finfo->name, ".", 1) != 0 && strncmp(finfo->name, "..", 2) != 0)
2519                 smbc_rmdir_dirempty = False;
2520
2521 }
2522
2523 /*
2524  * Routine to remove a directory
2525  */
2526
2527 static int smbc_rmdir_ctx(SMBCCTX *context, const char *fname)
2528 {
2529         SMBCSRV *srv;
2530         fstring server, share, user, password, workgroup;
2531         pstring path;
2532
2533         if (!context || !context->internal || 
2534             !context->internal->_initialized) {
2535
2536                 errno = EINVAL;
2537                 return -1;
2538
2539         }
2540
2541         if (!fname) {
2542
2543                 errno = EINVAL;
2544                 return -1;
2545
2546         }
2547   
2548         DEBUG(4, ("smbc_rmdir(%s)\n", fname));
2549
2550         if (smbc_parse_path(context, fname,
2551                             server, sizeof(server),
2552                             share, sizeof(share),
2553                             path, sizeof(path),
2554                             user, sizeof(user),
2555                             password, sizeof(password),
2556                             NULL, 0))
2557         {
2558                 errno = EINVAL;
2559                 return -1;
2560         }
2561
2562         if (user[0] == (char)0) fstrcpy(user, context->user);
2563
2564         fstrcpy(workgroup, context->workgroup);
2565
2566         srv = smbc_server(context, server, share, workgroup, user, password);
2567
2568         if (!srv) {
2569
2570                 return -1;  /* errno set by smbc_server */
2571
2572         }
2573
2574         /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2575
2576            mode = aDIR | aRONLY;
2577
2578            }
2579            else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2580
2581            if (strcmp(path, "\\") == 0) {
2582
2583            mode = aDIR | aRONLY;
2584
2585            }
2586            else {
2587
2588            mode = aRONLY;
2589            smbc_stat_printjob(srv, path, &size, &m_time);
2590            c_time = a_time = m_time;
2591            
2592            }
2593            else { */
2594
2595         if (!cli_rmdir(&srv->cli, path)) {
2596
2597                 errno = smbc_errno(context, &srv->cli);
2598
2599                 if (errno == EACCES) {  /* Check if the dir empty or not */
2600
2601                         pstring lpath; /* Local storage to avoid buffer overflows */
2602
2603                         smbc_rmdir_dirempty = True;  /* Make this so ... */
2604
2605                         pstrcpy(lpath, path);
2606                         pstrcat(lpath, "\\*");
2607
2608                         if (cli_list(&srv->cli, lpath, aDIR | aSYSTEM | aHIDDEN, rmdir_list_fn,
2609                                      NULL) < 0) {
2610
2611                                 /* Fix errno to ignore latest error ... */
2612
2613                                 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n", 
2614                                           smbc_errno(context, &srv->cli)));
2615                                 errno = EACCES;
2616
2617                         }
2618
2619                         if (smbc_rmdir_dirempty)
2620                                 errno = EACCES;
2621                         else
2622                                 errno = ENOTEMPTY;
2623
2624                 }
2625
2626                 return -1;
2627
2628         } 
2629
2630         return 0;
2631
2632 }
2633
2634 /*
2635  * Routine to return the current directory position
2636  */
2637
2638 static off_t smbc_telldir_ctx(SMBCCTX *context, SMBCFILE *dir)
2639 {
2640         off_t ret_val; /* Squash warnings about cast */
2641
2642         if (!context || !context->internal ||
2643             !context->internal->_initialized) {
2644
2645                 errno = EINVAL;
2646                 return -1;
2647
2648         }
2649
2650         if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
2651
2652                 errno = EBADF;
2653                 return -1;
2654
2655         }
2656
2657         if (dir->file != False) { /* FIXME, should be dir, perhaps */
2658
2659                 errno = ENOTDIR;
2660                 return -1;
2661
2662         }
2663
2664         /*
2665          * We return the pointer here as the offset
2666          */
2667         ret_val = (int)dir->dir_next;
2668         return ret_val;
2669
2670 }
2671
2672 /*
2673  * A routine to run down the list and see if the entry is OK
2674  */
2675
2676 struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list, 
2677                                          struct smbc_dirent *dirent)
2678 {
2679
2680         /* Run down the list looking for what we want */
2681
2682         if (dirent) {
2683
2684                 struct smbc_dir_list *tmp = list;
2685
2686                 while (tmp) {
2687
2688                         if (tmp->dirent == dirent)
2689                                 return tmp;
2690
2691                         tmp = tmp->next;
2692
2693                 }
2694
2695         }
2696
2697         return NULL;  /* Not found, or an error */
2698
2699 }
2700
2701
2702 /*
2703  * Routine to seek on a directory
2704  */
2705
2706 static int smbc_lseekdir_ctx(SMBCCTX *context, SMBCFILE *dir, off_t offset)
2707 {
2708         long int l_offset = offset;  /* Handle problems of size */
2709         struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
2710         struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
2711
2712         if (!context || !context->internal ||
2713             !context->internal->_initialized) {
2714
2715                 errno = EINVAL;
2716                 return -1;
2717
2718         }
2719
2720         if (dir->file != False) { /* FIXME, should be dir, perhaps */
2721
2722                 errno = ENOTDIR;
2723                 return -1;
2724
2725         }
2726
2727         /* Now, check what we were passed and see if it is OK ... */
2728
2729         if (dirent == NULL) {  /* Seek to the begining of the list */
2730
2731                 dir->dir_next = dir->dir_list;
2732                 return 0;
2733
2734         }
2735
2736         /* Now, run down the list and make sure that the entry is OK       */
2737         /* This may need to be changed if we change the format of the list */
2738
2739         if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
2740
2741                 errno = EINVAL;   /* Bad entry */
2742                 return -1;
2743
2744         }
2745
2746         dir->dir_next = list_ent;
2747
2748         return 0; 
2749
2750 }
2751
2752 /*
2753  * Routine to fstat a dir
2754  */
2755
2756 static int smbc_fstatdir_ctx(SMBCCTX *context, SMBCFILE *dir, struct stat *st)
2757 {
2758
2759         if (!context || !context->internal || 
2760             !context->internal->_initialized) {
2761
2762                 errno = EINVAL;
2763                 return -1;
2764
2765         }
2766
2767         /* No code yet ... */
2768
2769         return 0;
2770
2771 }
2772
2773 int smbc_chmod_ctx(SMBCCTX *context, const char *fname, mode_t newmode)
2774 {
2775         SMBCSRV *srv;
2776         fstring server, share, user, password, workgroup;
2777         pstring path;
2778         uint16 mode;
2779
2780         if (!context || !context->internal ||
2781             !context->internal->_initialized) {
2782
2783                 errno = EINVAL;  /* Best I can think of ... */
2784                 return -1;
2785     
2786         }
2787
2788         if (!fname) {
2789
2790                 errno = EINVAL;
2791                 return -1;
2792
2793         }
2794   
2795         DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
2796
2797         if (smbc_parse_path(context, fname,
2798                             server, sizeof(server),
2799                             share, sizeof(share),
2800                             path, sizeof(path),
2801                             user, sizeof(user),
2802                             password, sizeof(password),
2803                             NULL, 0)) {
2804                 errno = EINVAL;
2805                 return -1;
2806         }
2807
2808         if (user[0] == (char)0) fstrcpy(user, context->user);
2809
2810         fstrcpy(workgroup, context->workgroup);
2811
2812         srv = smbc_server(context, server, share, workgroup, user, password);
2813
2814         if (!srv) {
2815                 return -1;  /* errno set by smbc_server */
2816         }
2817
2818         mode = 0;
2819
2820         if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
2821         if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
2822         if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
2823         if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
2824
2825         if (!cli_setatr(&srv->cli, path, mode, 0)) {
2826                 errno = smbc_errno(context, &srv->cli);
2827                 return -1;
2828         }
2829         
2830         return 0;
2831 }
2832
2833 int smbc_utimes_ctx(SMBCCTX *context, const char *fname, struct timeval *tbuf)
2834 {
2835         SMBCSRV *srv;
2836         fstring server, share, user, password, workgroup;
2837         pstring path;
2838         uint16 mode;
2839         time_t t = (tbuf == NULL ? time(NULL) : tbuf->tv_sec);
2840
2841         if (!context || !context->internal ||
2842             !context->internal->_initialized) {
2843
2844                 errno = EINVAL;  /* Best I can think of ... */
2845                 return -1;
2846     
2847         }
2848
2849         if (!fname) {
2850
2851                 errno = EINVAL;
2852                 return -1;
2853
2854         }
2855   
2856         DEBUG(4, ("smbc_utimes(%s, [%s])\n", fname, ctime(&t)));
2857
2858         if (smbc_parse_path(context, fname,
2859                             server, sizeof(server),
2860                             share, sizeof(share),
2861                             path, sizeof(path),
2862                             user, sizeof(user),
2863                             password, sizeof(password),
2864                             NULL, 0)) {
2865                 errno = EINVAL;
2866                 return -1;
2867         }
2868
2869         if (user[0] == (char)0) fstrcpy(user, context->user);
2870
2871         fstrcpy(workgroup, context->workgroup);
2872
2873         srv = smbc_server(context, server, share, workgroup, user, password);
2874
2875         if (!srv) {
2876                 return -1;  /* errno set by smbc_server */
2877         }
2878
2879         if (!smbc_getatr(context, srv, path,
2880                          &mode, NULL,
2881                          NULL, NULL, NULL,
2882                          NULL)) {
2883                 return -1;
2884         }
2885
2886         if (!cli_setatr(&srv->cli, path, mode, t)) {
2887                 /* some servers always refuse directory changes */
2888                 if (!(mode & aDIR)) {
2889                         errno = smbc_errno(context, &srv->cli);
2890                         return -1;
2891                 }
2892         }
2893
2894         return 0;
2895 }
2896
2897
2898 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
2899    However NT4 gives a "The information may have been modified by a
2900    computer running Windows NT 5.0" if denied ACEs do not appear before
2901    allowed ACEs. */
2902
2903 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
2904 {
2905         if (sec_ace_equal(ace1, ace2)) 
2906                 return 0;
2907
2908         if (ace1->type != ace2->type) 
2909                 return ace2->type - ace1->type;
2910
2911         if (sid_compare(&ace1->trustee, &ace2->trustee)) 
2912                 return sid_compare(&ace1->trustee, &ace2->trustee);
2913
2914         if (ace1->flags != ace2->flags) 
2915                 return ace1->flags - ace2->flags;
2916
2917         if (ace1->info.mask != ace2->info.mask) 
2918                 return ace1->info.mask - ace2->info.mask;
2919
2920         if (ace1->size != ace2->size) 
2921                 return ace1->size - ace2->size;
2922
2923         return memcmp(ace1, ace2, sizeof(SEC_ACE));
2924 }
2925
2926
2927 static void sort_acl(SEC_ACL *the_acl)
2928 {
2929         uint32 i;
2930         if (!the_acl) return;
2931
2932         qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
2933
2934         for (i=1;i<the_acl->num_aces;) {
2935                 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
2936                         int j;
2937                         for (j=i; j<the_acl->num_aces-1; j++) {
2938                                 the_acl->ace[j] = the_acl->ace[j+1];
2939                         }
2940                         the_acl->num_aces--;
2941                 } else {
2942                         i++;
2943                 }
2944         }
2945 }
2946
2947 /* convert a SID to a string, either numeric or username/group */
2948 static void convert_sid_to_string(struct cli_state *ipc_cli,
2949                                   POLICY_HND *pol,
2950                                   fstring str,
2951                                   BOOL numeric,
2952                                   DOM_SID *sid)
2953 {
2954         char **domains = NULL;
2955         char **names = NULL;
2956         uint32 *types = NULL;
2957
2958         sid_to_string(str, sid);
2959
2960         if (numeric) return;     /* no lookup desired */
2961         
2962         /* Ask LSA to convert the sid to a name */
2963
2964         if (!NT_STATUS_IS_OK(cli_lsa_lookup_sids(ipc_cli, ipc_cli->mem_ctx,  
2965                                                  pol, 1, sid, &domains, 
2966                                                  &names, &types)) ||
2967             !domains || !domains[0] || !names || !names[0]) {
2968                 return;
2969         }
2970
2971         /* Converted OK */
2972
2973         slprintf(str, sizeof(fstring) - 1, "%s%s%s",
2974                  domains[0], lp_winbind_separator(),
2975                  names[0]);
2976 }
2977
2978 /* convert a string to a SID, either numeric or username/group */
2979 static BOOL convert_string_to_sid(struct cli_state *ipc_cli,
2980                                   POLICY_HND *pol,
2981                                   BOOL numeric,
2982                                   DOM_SID *sid,
2983                                   const char *str)
2984 {
2985         uint32 *types = NULL;
2986         DOM_SID *sids = NULL;
2987         BOOL result = True;
2988
2989         if (numeric) {
2990                 if (strncmp(str, "S-", 2) == 0) {
2991                         return string_to_sid(sid, str);
2992                 }
2993
2994                 result = False;
2995                 goto done;
2996         }
2997
2998         if (!NT_STATUS_IS_OK(cli_lsa_lookup_names(ipc_cli, ipc_cli->mem_ctx, 
2999                                                   pol, 1, &str, &sids, 
3000                                                   &types))) {
3001                 result = False;
3002                 goto done;
3003         }
3004
3005         sid_copy(sid, &sids[0]);
3006  done:
3007
3008         return result;
3009 }
3010
3011
3012 /* parse an ACE in the same format as print_ace() */
3013 static BOOL parse_ace(struct cli_state *ipc_cli,
3014                       POLICY_HND *pol,
3015                       SEC_ACE *ace,
3016                       BOOL numeric,
3017                       char *str)
3018 {
3019         char *p;
3020         const char *cp;
3021         fstring tok;
3022         unsigned atype, aflags, amask;
3023         DOM_SID sid;
3024         SEC_ACCESS mask;
3025         const struct perm_value *v;
3026         struct perm_value {
3027                 const char *perm;
3028                 uint32 mask;
3029         };
3030
3031         /* These values discovered by inspection */
3032         static const struct perm_value special_values[] = {
3033                 { "R", 0x00120089 },
3034                 { "W", 0x00120116 },
3035                 { "X", 0x001200a0 },
3036                 { "D", 0x00010000 },
3037                 { "P", 0x00040000 },
3038                 { "O", 0x00080000 },
3039                 { NULL, 0 },
3040         };
3041
3042         static const struct perm_value standard_values[] = {
3043                 { "READ",   0x001200a9 },
3044                 { "CHANGE", 0x001301bf },
3045                 { "FULL",   0x001f01ff },
3046                 { NULL, 0 },
3047         };
3048
3049
3050         ZERO_STRUCTP(ace);
3051         p = strchr_m(str,':');
3052         if (!p) return False;
3053         *p = '\0';
3054         p++;
3055         /* Try to parse numeric form */
3056
3057         if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
3058             convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3059                 goto done;
3060         }
3061
3062         /* Try to parse text form */
3063
3064         if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
3065                 return False;
3066         }
3067
3068         cp = p;
3069         if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3070                 return False;
3071         }
3072
3073         if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
3074                 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
3075         } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
3076                 atype = SEC_ACE_TYPE_ACCESS_DENIED;
3077         } else {
3078                 return False;
3079         }
3080
3081         /* Only numeric form accepted for flags at present */
3082
3083         if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
3084               sscanf(tok, "%i", &aflags))) {
3085                 return False;
3086         }
3087
3088         if (!next_token(&cp, tok, "/", sizeof(fstring))) {
3089                 return False;
3090         }
3091
3092         if (strncmp(tok, "0x", 2) == 0) {
3093                 if (sscanf(tok, "%i", &amask) != 1) {
3094                         return False;
3095                 }
3096                 goto done;
3097         }
3098
3099         for (v = standard_values; v->perm; v++) {
3100                 if (strcmp(tok, v->perm) == 0) {
3101                         amask = v->mask;
3102                         goto done;
3103                 }
3104         }
3105
3106         p = tok;
3107
3108         while(*p) {
3109                 BOOL found = False;
3110
3111                 for (v = special_values; v->perm; v++) {
3112                         if (v->perm[0] == *p) {
3113                                 amask |= v->mask;
3114                                 found = True;
3115                         }
3116                 }
3117
3118                 if (!found) return False;
3119                 p++;
3120         }
3121
3122         if (*p) {
3123                 return False;
3124         }
3125
3126  done:
3127         mask.mask = amask;
3128         init_sec_ace(ace, &sid, atype, mask, aflags);
3129         return True;
3130 }
3131
3132 /* add an ACE to a list of ACEs in a SEC_ACL */
3133 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace, TALLOC_CTX *ctx)
3134 {
3135         SEC_ACL *new;
3136         SEC_ACE *aces;
3137         if (! *the_acl) {
3138                 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
3139                 return True;
3140         }
3141
3142         aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces);
3143         memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
3144         memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
3145         new = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
3146         SAFE_FREE(aces);
3147         (*the_acl) = new;
3148         return True;
3149 }
3150
3151
3152 /* parse a ascii version of a security descriptor */
3153 static SEC_DESC *sec_desc_parse(TALLOC_CTX *ctx,
3154                                 struct cli_state *ipc_cli,
3155                                 POLICY_HND *pol,
3156                                 BOOL numeric,
3157                                 char *str)
3158 {
3159         const char *p = str;
3160         fstring tok;
3161         SEC_DESC *ret;
3162         size_t sd_size;
3163         DOM_SID *grp_sid=NULL, *owner_sid=NULL;
3164         SEC_ACL *dacl=NULL;
3165         int revision=1;
3166
3167         while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
3168
3169                 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
3170                         revision = strtol(tok+9, NULL, 16);
3171                         continue;
3172                 }
3173
3174                 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
3175                         owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3176                         if (!owner_sid ||
3177                             !convert_string_to_sid(ipc_cli, pol,
3178                                                    numeric,
3179                                                    owner_sid, tok+6)) {
3180                                 DEBUG(5, ("Failed to parse owner sid\n"));
3181                                 return NULL;
3182                         }
3183                         continue;
3184                 }
3185
3186                 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
3187                         owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3188                         if (!owner_sid ||
3189                             !convert_string_to_sid(ipc_cli, pol,
3190                                                    False,
3191                                                    owner_sid, tok+7)) {
3192                                 DEBUG(5, ("Failed to parse owner sid\n"));
3193                                 return NULL;
3194                         }
3195                         continue;
3196                 }
3197
3198                 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
3199                         grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3200                         if (!grp_sid ||
3201                             !convert_string_to_sid(ipc_cli, pol,
3202                                                    numeric,
3203                                                    grp_sid, tok+6)) {
3204                                 DEBUG(5, ("Failed to parse group sid\n"));
3205                                 return NULL;
3206                         }
3207                         continue;
3208                 }
3209
3210                 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
3211                         grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
3212                         if (!grp_sid ||
3213                             !convert_string_to_sid(ipc_cli, pol,
3214                                                    False,
3215                                                    grp_sid, tok+6)) {
3216                                 DEBUG(5, ("Failed to parse group sid\n"));
3217                                 return NULL;
3218                         }
3219                         continue;
3220                 }
3221
3222                 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
3223                         SEC_ACE ace;
3224                         if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
3225                                 DEBUG(5, ("Failed to parse ACL %s\n", tok));
3226                                 return NULL;
3227                         }
3228                         if(!add_ace(&dacl, &ace, ctx)) {
3229                                 DEBUG(5, ("Failed to add ACL %s\n", tok));
3230                                 return NULL;
3231                         }
3232                         continue;
3233                 }
3234
3235                 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
3236                         SEC_ACE ace;
3237                         if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
3238                                 DEBUG(5, ("Failed to parse ACL %s\n", tok));
3239                                 return NULL;
3240                         }
3241                         if(!add_ace(&dacl, &ace, ctx)) {
3242                                 DEBUG(5, ("Failed to add ACL %s\n", tok));
3243                                 return NULL;
3244                         }
3245                         continue;
3246                 }
3247
3248                 DEBUG(5, ("Failed to parse security descriptor\n"));
3249                 return NULL;
3250         }
3251
3252         ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE, 
3253                             owner_sid, grp_sid, NULL, dacl, &sd_size);
3254
3255         SAFE_FREE(grp_sid);
3256         SAFE_FREE(owner_sid);
3257
3258         return ret;
3259 }
3260
3261
3262 /* Obtain the current dos attributes */
3263 static DOS_ATTR_DESC *dos_attr_query(SMBCCTX *context,
3264                                      TALLOC_CTX *ctx,
3265                                      const char *filename,
3266                                      SMBCSRV *srv)
3267 {
3268         time_t m_time = 0, a_time = 0, c_time = 0;
3269         SMB_OFF_T size = 0;
3270         uint16 mode = 0;
3271         SMB_INO_T inode = 0;
3272         DOS_ATTR_DESC *ret;
3273     
3274         ret = talloc(ctx, sizeof(DOS_ATTR_DESC));
3275         if (!ret) {
3276                 errno = ENOMEM;
3277                 return NULL;
3278         }
3279
3280         /* Obtain the DOS attributes */
3281         if (!smbc_getatr(context, srv, (char *) filename, &mode, &size, 
3282                          &c_time, &a_time, &m_time, &inode)) {
3283         
3284                 errno = smbc_errno(context, &srv->cli);
3285                 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
3286                 return NULL;
3287         
3288         }
3289                 
3290         ret->mode = mode;
3291         ret->size = size;
3292         ret->a_time = a_time;
3293         ret->c_time = c_time;
3294         ret->m_time = m_time;
3295         ret->inode = inode;
3296
3297         return ret;
3298 }
3299
3300
3301 /* parse a ascii version of a security descriptor */
3302 static void dos_attr_parse(SMBCCTX *context,
3303                            DOS_ATTR_DESC *dad,
3304                            SMBCSRV *srv,
3305                            char *str)
3306 {
3307         const char *p = str;
3308         fstring tok;
3309
3310         while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
3311
3312                 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
3313                         dad->mode = strtol(tok+5, NULL, 16);
3314                         continue;
3315                 }
3316
3317                 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
3318                         dad->size = strtoll(tok+5, NULL, 10);
3319                         continue;
3320                 }
3321
3322                 if (StrnCaseCmp(tok, "A_TIME:", 7) == 0) {
3323                         dad->a_time = strtoll(tok+7, NULL, 10);
3324                         continue;
3325                 }
3326
3327                 if (StrnCaseCmp(tok, "C_TIME:", 7) == 0) {
3328                         dad->c_time = strtoll(tok+7, NULL, 10);
3329                         continue;
3330                 }
3331
3332                 if (StrnCaseCmp(tok, "M_TIME:", 7) == 0) {
3333                         dad->m_time = strtoll(tok+7, NULL, 10);
3334                         continue;
3335                 }
3336
3337                 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
3338                         dad->inode = strtoll(tok+6, NULL, 10);
3339                         continue;
3340                 }
3341         }
3342 }
3343
3344
3345 /***************************************************** 
3346 retrieve the acls for a file
3347 *******************************************************/
3348 static int cacl_get(SMBCCTX *context, TALLOC_CTX *ctx, SMBCSRV *srv,
3349                     struct cli_state *ipc_cli, POLICY_HND *pol,
3350                     char *filename, char *name, char *buf, int bufsize)
3351 {
3352         uint32 i;
3353         int n = 0;
3354         int n_used;
3355         BOOL all;
3356         BOOL all_nt;
3357         BOOL all_dos;
3358         BOOL some_nt;
3359         BOOL some_dos;
3360         BOOL numeric = True;
3361         BOOL determine_size = (bufsize == 0);
3362         int fnum = -1;
3363         SEC_DESC *sd;
3364         fstring sidstr;
3365         char *p;
3366         time_t m_time = 0, a_time = 0, c_time = 0;
3367         SMB_OFF_T size = 0;
3368         uint16 mode = 0;
3369         SMB_INO_T ino = 0;
3370         struct cli_state *cli = &srv->cli;
3371
3372         all = (StrnCaseCmp(name, "system.*", 8) == 0);
3373         all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
3374         all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
3375         some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
3376         some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
3377         numeric = (* (name + strlen(name) - 1) != '+');
3378
3379         n_used = 0;
3380
3381         /*
3382          * If we are (possibly) talking to an NT or new system and some NT
3383          * attributes have been requested...
3384          */
3385         if (ipc_cli && (all || some_nt)) {
3386                 /* Point to the portion after "system.nt_sec_desc." */
3387                 name += 19;     /* if (all) this will be invalid but unused */
3388
3389                 /* ... then obtain any NT attributes which were requested */
3390                 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
3391
3392                 if (fnum == -1) {
3393                         DEBUG(5, ("cacl_get failed to open %s: %s\n",
3394                                   filename, cli_errstr(cli)));
3395                         errno = 0;
3396                         return -1;
3397                 }
3398
3399                 sd = cli_query_secdesc(cli, fnum, ctx);
3400
3401                 if (!sd) {
3402                         DEBUG(5,
3403                               ("cacl_get Failed to query old descriptor\n"));
3404                         errno = 0;
3405                         return -1;
3406                 }
3407
3408                 cli_close(cli, fnum);
3409
3410                 if (all || all_nt) {
3411                         if (determine_size) {
3412                                 p = talloc_asprintf(ctx,
3413                                                     "REVISION:%d",
3414                                                     sd->revision);
3415                                 if (!p) {
3416                                         errno = ENOMEM;
3417                                         return -1;
3418                                 }
3419                                 n = strlen(p);
3420                         } else {
3421                                 n = snprintf(buf, bufsize,
3422                                              "REVISION:%d", sd->revision);
3423                         }
3424                 } else if (StrCaseCmp(name, "revision") == 0) {
3425                         if (determine_size) {
3426                                 p = talloc_asprintf(ctx, "%d", sd->revision);
3427                                 if (!p) {
3428                                         errno = ENOMEM;
3429                                         return -1;
3430                                 }
3431                                 n = strlen(p);
3432                         } else {
3433                                 n = snprintf(buf, bufsize, "%d", sd->revision);
3434                         }
3435                 }
3436         
3437                 if (!determine_size && n > bufsize) {
3438                         errno = ERANGE;
3439                         return -1;
3440                 }
3441                 buf += n;
3442                 n_used += n;
3443                 bufsize -= n;
3444
3445                 /* Get owner and group sid */
3446
3447                 if (sd->owner_sid) {
3448                         convert_sid_to_string(ipc_cli, pol,
3449                                               sidstr, numeric, sd->owner_sid);
3450                 } else {
3451                         fstrcpy(sidstr, "");
3452                 }
3453
3454                 if (all || all_nt) {
3455                         if (determine_size) {
3456                                 p = talloc_asprintf(ctx, ",OWNER:%s", sidstr);
3457                                 if (!p) {
3458                                         errno = ENOMEM;
3459                                         return -1;
3460                                 }
3461                                 n = strlen(p);
3462                         } else {
3463                                 n = snprintf(buf, bufsize,
3464                                              ",OWNER:%s", sidstr);
3465                         }
3466                 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
3467                         if (determine_size) {
3468                                 p = talloc_asprintf(ctx, "%s", sidstr);
3469                                 if (!p) {
3470                                         errno = ENOMEM;
3471                                         return -1;
3472                                 }
3473                                 n = strlen(p);
3474                         } else {
3475                                 n = snprintf(buf, bufsize, "%s", sidstr);
3476                         }
3477                 }
3478
3479                 if (!determine_size && n > bufsize) {
3480                         errno = ERANGE;
3481                         return -1;
3482                 }
3483                 buf += n;
3484                 n_used += n;
3485                 bufsize -= n;
3486
3487                 if (sd->grp_sid) {
3488                         convert_sid_to_string(ipc_cli, pol,
3489                                               sidstr, numeric, sd->grp_sid);
3490                 } else {
3491                         fstrcpy(sidstr, "");
3492                 }
3493
3494                 if (all || all_nt) {
3495                         if (determine_size) {
3496                                 p = talloc_asprintf(ctx, ",GROUP:%s", sidstr);
3497                                 if (!p) {
3498                                         errno = ENOMEM;
3499                                         return -1;
3500                                 }
3501                                 n = strlen(p);
3502                         } else {
3503                                 n = snprintf(buf, bufsize,
3504                                              ",GROUP:%s", sidstr);
3505                         }
3506                 } else if (StrnCaseCmp(name, "group", 5) == 0) {
3507                         if (determine_size) {
3508                                 p = talloc_asprintf(ctx, "%s", sidstr);
3509                                 if (!p) {
3510                                         errno = ENOMEM;
3511                                         return -1;
3512                                 }
3513                                 n = strlen(p);
3514                         } else {
3515                                 n = snprintf(buf, bufsize, "%s", sidstr);
3516                         }
3517                 }
3518
3519                 if (!determine_size && n > bufsize) {
3520                         errno = ERANGE;
3521                         return -1;
3522                 }
3523                 buf += n;
3524                 n_used += n;
3525                 bufsize -= n;
3526
3527                 /* Add aces to value buffer  */
3528                 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
3529
3530                         SEC_ACE *ace = &sd->dacl->ace[i];
3531                         convert_sid_to_string(ipc_cli, pol,
3532                                               sidstr, numeric, &ace->trustee);
3533
3534                         if (all || all_nt) {
3535                                 if (determine_size) {
3536                                         p = talloc_asprintf(ctx, 
3537                                                             ",ACL:"
3538                                                             "%s:%d/%d/0x%08x", 
3539                                                             sidstr,
3540                                                             ace->type,
3541                                                             ace->flags,
3542                                                             ace->info.mask);
3543                                         if (!p) {
3544                                                 errno = ENOMEM;
3545                                                 return -1;
3546                                         }
3547                                         n = strlen(p);
3548                                 } else {
3549                                         n = snprintf(buf, bufsize,
3550                                                      ",ACL:%s:%d/%d/0x%08x", 
3551                                                      sidstr,
3552                                                      ace->type,
3553                                                      ace->flags,
3554                                                      ace->info.mask);
3555                                 }
3556                         } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
3557                                     StrCaseCmp(name + 3, sidstr) == 0) ||
3558                                    (StrnCaseCmp(name, "acl+", 4) == 0 &&
3559                                     StrCaseCmp(name + 4, sidstr) == 0)) {
3560                                 if (determine_size) {
3561                                         p = talloc_asprintf(ctx, 
3562                                                             "%d/%d/0x%08x", 
3563                                                             ace->type,
3564                                                             ace->flags,
3565                                                             ace->info.mask);
3566                                         if (!p) {
3567                                                 errno = ENOMEM;
3568                                                 return -1;
3569                                         }
3570                                         n = strlen(p);
3571                                 } else {
3572                                         n = snprintf(buf, bufsize,
3573                                                      "%d/%d/0x%08x", 
3574                                                      ace->type,
3575                                                      ace->flags,
3576                                                      ace->info.mask);
3577                                 }
3578                         }
3579                         if (n > bufsize) {
3580                                 errno = ERANGE;
3581                                 return -1;
3582                         }
3583                         buf += n;
3584                         n_used += n;
3585                         bufsize -= n;
3586                 }
3587
3588                 /* Restore name pointer to its original value */
3589                 name -= 19;
3590         }
3591
3592         if (all || some_dos) {
3593                 /* Point to the portion after "system.dos_attr." */
3594                 name += 16;     /* if (all) this will be invalid but unused */
3595
3596                 /* Obtain the DOS attributes */
3597                 if (!smbc_getatr(context, srv, filename, &mode, &size, 
3598                                  &c_time, &a_time, &m_time, &ino)) {
3599                         
3600                         errno = smbc_errno(context, &srv->cli);
3601                         return -1;
3602                         
3603                 }
3604                 
3605                 if (all || all_dos) {
3606                         if (determine_size) {
3607                                 p = talloc_asprintf(ctx,
3608                                                     "%sMODE:0x%x",
3609                                                     (ipc_cli &&
3610                                                      (all || some_nt)
3611                                                      ? ","
3612                                                      : ""),
3613                                                     mode);
3614                                 if (!p) {
3615                                         errno = ENOMEM;
3616                                         return -1;
3617                                 }
3618                                 n = strlen(p);
3619                         } else {
3620                                 n = snprintf(buf, bufsize,
3621                                              "%sMODE:0x%x",
3622                                              (ipc_cli &&
3623                                               (all || some_nt)
3624                                               ? ","
3625                                               : ""),
3626                                              mode);
3627                         }
3628                 } else if (StrCaseCmp(name, "mode") == 0) {
3629                         if (determine_size) {
3630                                 p = talloc_asprintf(ctx, "0x%x", mode);
3631                                 if (!p) {
3632                                         errno = ENOMEM;
3633                                         return -1;
3634                                 }
3635                                 n = strlen(p);
3636                         } else {
3637                                 n = snprintf(buf, bufsize, "0x%x", mode);
3638                         }
3639                 }
3640         
3641                 if (!determine_size && n > bufsize) {
3642                         errno = ERANGE;
3643                         return -1;
3644                 }
3645                 buf += n;
3646                 n_used += n;
3647                 bufsize -= n;
3648
3649                 if (all || all_dos) {
3650                         if (determine_size) {
3651                                 p = talloc_asprintf(ctx,
3652                                                     ",SIZE:%llu",
3653                                                     (unsigned long long) size);
3654                                 if (!p) {
3655                                         errno = ENOMEM;
3656                                         return -1;
3657                                 }
3658                                 n = strlen(p);
3659                         } else {
3660                                 n = snprintf(buf, bufsize,
3661                                              ",SIZE:%llu",
3662                                              (unsigned long long) size);
3663                         }
3664                 } else if (StrCaseCmp(name, "size") == 0) {
3665                         if (determine_size) {
3666                                 p = talloc_asprintf(ctx,
3667                                                     "%llu",
3668                                                     (unsigned long long) size);
3669                                 if (!p) {
3670                                         errno = ENOMEM;
3671                                         return -1;
3672                                 }
3673                                 n = strlen(p);
3674                         } else {
3675                                 n = snprintf(buf, bufsize,
3676                                              "%llu",
3677                                              (unsigned long long) size);
3678                         }
3679                 }
3680         
3681                 if (!determine_size && n > bufsize) {
3682                         errno = ERANGE;
3683                         return -1;
3684                 }
3685                 buf += n;
3686                 n_used += n;
3687                 bufsize -= n;
3688
3689                 if (all || all_dos) {
3690                         if (determine_size) {
3691                                 p = talloc_asprintf(ctx,
3692                                                     ",C_TIME:%lu", c_time);
3693                                 if (!p) {
3694                                         errno = ENOMEM;
3695                                         return -1;
3696                                 }
3697                                 n = strlen(p);
3698                         } else {
3699                                 n = snprintf(buf, bufsize,
3700                                              ",C_TIME:%lu", c_time);
3701                         }
3702                 } else if (StrCaseCmp(name, "c_time") == 0) {
3703                         if (determine_size) {
3704                                 p = talloc_asprintf(ctx, "%lu", c_time);
3705                                 if (!p) {
3706                                         errno = ENOMEM;
3707                                         return -1;
3708                                 }
3709                                 n = strlen(p);
3710                         } else {
3711                                 n = snprintf(buf, bufsize, "%lu", c_time);
3712                         }
3713                 }
3714         
3715                 if (!determine_size && n > bufsize) {
3716                         errno = ERANGE;
3717                         return -1;
3718                 }
3719                 buf += n;
3720                 n_used += n;
3721                 bufsize -= n;
3722
3723                 if (all || all_dos) {
3724                         if (determine_size) {
3725                                 p = talloc_asprintf(ctx,
3726                                                     ",A_TIME:%lu", a_time);
3727                                 if (!p) {
3728                                         errno = ENOMEM;
3729                                         return -1;
3730                                 }
3731                                 n = strlen(p);
3732                         } else {
3733                                 n = snprintf(buf, bufsize,
3734                                              ",A_TIME:%lu", a_time);
3735                         }
3736                 } else if (StrCaseCmp(name, "a_time") == 0) {
3737                         if (determine_size) {
3738                                 p = talloc_asprintf(ctx, "%lu", a_time);
3739                                 if (!p) {
3740                                         errno = ENOMEM;
3741                                         return -1;
3742                                 }
3743                                 n = strlen(p);
3744                         } else {
3745                                 n = snprintf(buf, bufsize, "%lu", a_time);
3746                         }
3747                 }
3748         
3749                 if (!determine_size && n > bufsize) {
3750                         errno = ERANGE;
3751                         return -1;
3752                 }
3753                 buf += n;
3754                 n_used += n;
3755                 bufsize -= n;
3756
3757                 if (all || all_dos) {
3758                         if (determine_size) {
3759                                 p = talloc_asprintf(ctx,
3760                                                     ",M_TIME:%lu", m_time);
3761                                 if (!p) {
3762                                         errno = ENOMEM;
3763                                         return -1;
3764                                 }
3765                                 n = strlen(p);
3766                         } else {
3767                                 n = snprintf(buf, bufsize,
3768                                              ",M_TIME:%lu", m_time);
3769                         }
3770                 } else if (StrCaseCmp(name, "m_time") == 0) {
3771                         if (determine_size) {
3772                                 p = talloc_asprintf(ctx, "%lu", m_time);
3773                                 if (!p) {
3774                                         errno = ENOMEM;
3775                                         return -1;
3776                                 }
3777                                 n = strlen(p);
3778                         } else {
3779                                 n = snprintf(buf, bufsize, "%lu", m_time);
3780                         }
3781                 }
3782         
3783                 if (!determine_size && n > bufsize) {
3784                         errno = ERANGE;
3785                         return -1;
3786                 }
3787                 buf += n;
3788                 n_used += n;
3789                 bufsize -= n;
3790
3791                 if (all || all_dos) {
3792                         if (determine_size) {
3793                                 p = talloc_asprintf(ctx,
3794                                                     ",INODE:%llu",
3795                                                     (unsigned long long) ino);
3796                                 if (!p) {
3797                                         errno = ENOMEM;
3798                                         return -1;
3799                                 }
3800                                 n = strlen(p);
3801                         } else {
3802                                 n = snprintf(buf, bufsize,
3803                                              ",INODE:%llu",
3804                                              (unsigned long long) ino);
3805                         }
3806                 } else if (StrCaseCmp(name, "inode") == 0) {
3807                         if (determine_size) {
3808                                 p = talloc_asprintf(ctx,
3809                                                     "%llu",
3810                                                     (unsigned long long) ino);
3811                                 if (!p) {
3812                                         errno = ENOMEM;
3813                                         return -1;
3814                                 }
3815                                 n = strlen(p);
3816                         } else {
3817                                 n = snprintf(buf, bufsize,
3818                                              "%llu",
3819                                              (unsigned long long) ino);
3820                         }
3821                 }
3822         
3823                 if (!determine_size && n > bufsize) {
3824                         errno = ERANGE;
3825                         return -1;
3826                 }
3827                 buf += n;
3828                 n_used += n;
3829                 bufsize -= n;
3830
3831                 /* Restore name pointer to its original value */
3832                 name -= 16;
3833         }
3834
3835         if (n_used == 0) {
3836                 errno = ENOATTR;
3837                 return -1;
3838         }
3839
3840         return n_used;
3841 }
3842
3843
3844 /***************************************************** 
3845 set the ACLs on a file given an ascii description
3846 *******************************************************/
3847 static int cacl_set(TALLOC_CTX *ctx, struct cli_state *cli,
3848                     struct cli_state *ipc_cli, POLICY_HND *pol,
3849                     const char *filename, const char *the_acl,
3850                     int mode, int flags)
3851 {
3852         int fnum;
3853         int err = 0;
3854         SEC_DESC *sd = NULL, *old;
3855         SEC_ACL *dacl = NULL;
3856         DOM_SID *owner_sid = NULL; 
3857         DOM_SID *grp_sid = NULL;
3858         uint32 i, j;
3859         size_t sd_size;
3860         int ret = 0;
3861         char *p;
3862         BOOL numeric = True;
3863
3864         /* the_acl will be null for REMOVE_ALL operations */
3865         if (the_acl) {
3866                 numeric = ((p = strchr(the_acl, ':')) != NULL &&
3867                            p > the_acl &&
3868                            p[-1] != '+');
3869
3870                 /* if this is to set the entire ACL... */
3871                 if (*the_acl == '*') {
3872                         /* ... then increment past the first colon */
3873                         the_acl = p + 1;
3874                 }
3875
3876                 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric, (char *) the_acl);
3877
3878                 if (!sd) {
3879                         errno = EINVAL;
3880                         return -1;
3881                 }
3882         }
3883
3884         /* The desired access below is the only one I could find that works
3885            with NT4, W2KP and Samba */
3886
3887         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
3888
3889         if (fnum == -1) {
3890                 DEBUG(5, ("cacl_set failed to open %s: %s\n",
3891                           filename, cli_errstr(cli)));
3892                 errno = 0;
3893                 return -1;
3894         }
3895
3896         old = cli_query_secdesc(cli, fnum, ctx);
3897
3898         if (!old) {
3899                 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
3900                 errno = 0;
3901                 return -1;
3902         }
3903
3904         cli_close(cli, fnum);
3905
3906         switch (mode) {
3907         case SMBC_XATTR_MODE_REMOVE_ALL:
3908                 old->dacl->num_aces = 0;
3909                 SAFE_FREE(old->dacl->ace);
3910                 SAFE_FREE(old->dacl);
3911                 old->off_dacl = 0;
3912                 dacl = old->dacl;
3913                 break;
3914
3915         case SMBC_XATTR_MODE_REMOVE:
3916                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
3917                         BOOL found = False;
3918
3919                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
3920                                 if (sec_ace_equal(&sd->dacl->ace[i],
3921                                                   &old->dacl->ace[j])) {
3922                                         uint32 k;
3923                                         for (k=j; k<old->dacl->num_aces-1;k++) {
3924                                                 old->dacl->ace[k] = old->dacl->ace[k+1];
3925                                         }
3926                                         old->dacl->num_aces--;
3927                                         if (old->dacl->num_aces == 0) {
3928                                                 SAFE_FREE(old->dacl->ace);
3929                                                 SAFE_FREE(old->dacl);
3930                                                 old->off_dacl = 0;
3931                                         }
3932                                         found = True;
3933                                         dacl = old->dacl;
3934                                         break;
3935                                 }
3936                         }
3937
3938                         if (!found) {
3939                                 err = ENOATTR;
3940                                 ret = -1;
3941                                 goto failed;
3942                         }
3943                 }
3944                 break;
3945
3946         case SMBC_XATTR_MODE_ADD:
3947                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
3948                         BOOL found = False;
3949
3950                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
3951                                 if (sid_equal(&sd->dacl->ace[i].trustee,
3952                                               &old->dacl->ace[j].trustee)) {
3953                                         if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
3954                                                 err = EEXIST;
3955                                                 ret = -1;
3956                                                 goto failed;
3957                                         }
3958                                         old->dacl->ace[j] = sd->dacl->ace[i];
3959                                         ret = -1;
3960                                         found = True;
3961                                 }
3962                         }
3963
3964                         if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
3965                                 err = ENOATTR;
3966                                 ret = -1;
3967                                 goto failed;
3968                         }
3969                         
3970                         for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
3971                                 add_ace(&old->dacl, &sd->dacl->ace[i], ctx);
3972                         }
3973                 }
3974                 dacl = old->dacl;
3975                 break;
3976
3977         case SMBC_XATTR_MODE_SET:
3978                 old = sd;
3979                 owner_sid = old->owner_sid;
3980                 grp_sid = old->grp_sid;
3981                 dacl = old->dacl;
3982                 break;
3983
3984         case SMBC_XATTR_MODE_CHOWN:
3985                 owner_sid = sd->owner_sid;
3986                 break;
3987
3988         case SMBC_XATTR_MODE_CHGRP:
3989                 grp_sid = sd->grp_sid;
3990                 break;
3991         }
3992
3993         /* Denied ACE entries must come before allowed ones */
3994         sort_acl(old->dacl);
3995
3996         /* Create new security descriptor and set it */
3997         sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE, 
3998                            owner_sid, grp_sid, NULL, dacl, &sd_size);
3999
4000         fnum = cli_nt_create(cli, filename,
4001                              WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
4002
4003         if (fnum == -1) {
4004                 DEBUG(5, ("cacl_set failed to open %s: %s\n",
4005                           filename, cli_errstr(cli)));
4006                 errno = 0;
4007                 return -1;
4008         }
4009
4010         if (!cli_set_secdesc(cli, fnum, sd)) {
4011                 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
4012                 ret = -1;
4013         }
4014
4015         /* Clean up */
4016
4017  failed:
4018         cli_close(cli, fnum);
4019
4020         if (err != 0) {
4021                 errno = err;
4022         }
4023         
4024         return ret;
4025 }
4026
4027
4028 int smbc_setxattr_ctx(SMBCCTX *context,
4029                       const char *fname,
4030                       const char *name,
4031                       const void *value,
4032                       size_t size,
4033                       int flags)
4034 {
4035         int ret;
4036         int ret2;
4037         SMBCSRV *srv;
4038         SMBCSRV *ipc_srv;
4039         fstring server, share, user, password, workgroup;
4040         pstring path;
4041         TALLOC_CTX *ctx;
4042         POLICY_HND pol;
4043         DOS_ATTR_DESC *dad;
4044
4045         if (!context || !context->internal ||
4046             !context->internal->_initialized) {
4047
4048                 errno = EINVAL;  /* Best I can think of ... */
4049                 return -1;
4050     
4051         }
4052
4053         if (!fname) {
4054
4055                 errno = EINVAL;
4056                 return -1;
4057
4058         }
4059   
4060         DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n", fname, name, (int) size, (const char*)value));
4061
4062         if (smbc_parse_path(context, fname,
4063                             server, sizeof(server),
4064                             share, sizeof(share),
4065                             path, sizeof(path),
4066                             user, sizeof(user),
4067                             password, sizeof(password),
4068                             NULL, 0)) {
4069                 errno = EINVAL;
4070                 return -1;
4071         }
4072
4073         if (user[0] == (char)0) fstrcpy(user, context->user);
4074
4075         fstrcpy(workgroup, context->workgroup);
4076
4077         srv = smbc_server(context, server, share, workgroup, user, password);
4078         if (!srv) {
4079                 return -1;  /* errno set by smbc_server */
4080         }
4081
4082         if (! srv->no_nt_session) {
4083                 ipc_srv = smbc_attr_server(context, server, share,
4084                                            workgroup, user, password,
4085                                            &pol);
4086                 srv->no_nt_session = True;
4087         } else {
4088                 ipc_srv = NULL;
4089         }
4090         
4091         ctx = talloc_init("smbc_setxattr");
4092         if (!ctx) {
4093                 errno = ENOMEM;
4094                 return -1;
4095         }
4096
4097         /*
4098          * Are they asking to set the entire set of known attributes?
4099          */
4100         if (StrCaseCmp(name, "system.*") == 0 ||
4101             StrCaseCmp(name, "system.*+") == 0) {
4102                 /* Yup. */
4103                 char *namevalue =
4104                         talloc_asprintf(ctx, "%s:%s", name+7, (const char *) value);
4105                 if (! namevalue) {
4106                         errno = ENOMEM;
4107                         ret = -1;
4108                         return -1;
4109                 }
4110
4111                 if (ipc_srv) {
4112                         ret = cacl_set(ctx, &srv->cli,
4113                                        &ipc_srv->cli, &pol, path,
4114                                        namevalue,
4115                                        (*namevalue == '*'
4116                                         ? SMBC_XATTR_MODE_SET
4117                                         : SMBC_XATTR_MODE_ADD),
4118                                        flags);
4119                 } else {
4120                         ret = 0;
4121                 }
4122
4123                 /* get a DOS Attribute Descriptor with current attributes */
4124                 dad = dos_attr_query(context, ctx, path, srv);
4125                 if (dad) {
4126                         /* Overwrite old with new, using what was provided */
4127                         dos_attr_parse(context, dad, srv, namevalue);
4128
4129                         /* Set the new DOS attributes */
4130 #if 0                           /* not yet implemented */
4131                         if (! cli_setpathinfo(&srv->cli, path,
4132                                               dad->c_time,
4133                                               dad->a_time,
4134                                               dad->m_time,
4135                                               dad->mode)) {
4136                                 if (!cli_setatr(&srv->cli, path,
4137                                                 dad->mode, dad->m_time)) {
4138                                         errno = smbc_errno(context, &srv->cli);
4139                                 }
4140                         }
4141 #else
4142                         if (!cli_setatr(&srv->cli, path,
4143                                         dad->mode, dad->m_time)) {
4144                                 errno = smbc_errno(context, &srv->cli);
4145                         }
4146 #endif
4147                 }
4148
4149                 /* we only fail if both NT and DOS sets failed */
4150                 if (ret < 0 && ! dad) {
4151                         ret = -1; /* in case dad was null */
4152                 }
4153                 else {
4154                         ret = 0;
4155                 }
4156
4157                 talloc_destroy(ctx);
4158                 return ret;
4159         }
4160
4161         /*
4162          * Are they asking to set an access control element or to set
4163          * the entire access control list?
4164          */
4165         if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4166             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
4167             StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4168             StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4169             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
4170
4171                 /* Yup. */
4172                 char *namevalue =
4173                         talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4174
4175                 if (! ipc_srv) {
4176                         ret = -1; /* errno set by smbc_server() */
4177                 }
4178                 else if (! namevalue) {
4179                         errno = ENOMEM;
4180                         ret = -1;
4181                 } else {
4182                         ret = cacl_set(ctx, &srv->cli,
4183                                        &ipc_srv->cli, &pol, path,
4184                                        namevalue,
4185                                        (*namevalue == '*'
4186                                         ? SMBC_XATTR_MODE_SET
4187                                         : SMBC_XATTR_MODE_ADD),
4188                                        flags);
4189                 }
4190                 talloc_destroy(ctx);
4191                 return ret;
4192         }
4193
4194         /*
4195          * Are they asking to set the owner?
4196          */
4197         if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4198             StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
4199
4200                 /* Yup. */
4201                 char *namevalue =
4202                         talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4203
4204                 if (! ipc_srv) {
4205                         
4206                         ret = -1; /* errno set by smbc_server() */
4207                 }
4208                 else if (! namevalue) {
4209                         errno = ENOMEM;
4210                         ret = -1;
4211                 } else {
4212                         ret = cacl_set(ctx, &srv->cli,
4213                                        &ipc_srv->cli, &pol, path,
4214                                        namevalue, SMBC_XATTR_MODE_CHOWN, 0);
4215                 }
4216                 talloc_destroy(ctx);
4217                 return ret;
4218         }
4219
4220         /*
4221          * Are they asking to set the group?
4222          */
4223         if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4224             StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
4225
4226                 /* Yup. */
4227                 char *namevalue =
4228                         talloc_asprintf(ctx, "%s:%s", name+19, (const char *) value);
4229
4230                 if (! ipc_srv) {
4231                         /* errno set by smbc_server() */
4232                         ret = -1;
4233                 }
4234                 else if (! namevalue) {
4235                         errno = ENOMEM;
4236                         ret = -1;
4237                 } else {
4238                         ret = cacl_set(ctx, &srv->cli,
4239                                        &ipc_srv->cli, &pol, path,
4240                                        namevalue, SMBC_XATTR_MODE_CHOWN, 0);
4241                 }
4242                 talloc_destroy(ctx);
4243                 return ret;
4244         }
4245
4246         /*
4247          * Are they asking to set a DOS attribute?
4248          */
4249         if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
4250             StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
4251             StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
4252             StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
4253             StrCaseCmp(name, "system.dos_attr.m_time") == 0) {
4254
4255                 /* get a DOS Attribute Descriptor with current attributes */
4256                 dad = dos_attr_query(context, ctx, path, srv);
4257                 if (dad) {
4258                         char *namevalue =
4259                                 talloc_asprintf(ctx, "%s:%s", name+16, (const char *) value);
4260                         if (! namevalue) {
4261                                 errno = ENOMEM;
4262                                 ret = -1;
4263                         } else {
4264                                 /* Overwrite old with provided new params */
4265                                 dos_attr_parse(context, dad, srv, namevalue);
4266
4267                                 /* Set the new DOS attributes */
4268 #if 0                           /* not yet implemented */
4269                                 ret2 = cli_setpathinfo(&srv->cli, path,
4270                                                        dad->c_time,
4271                                                        dad->a_time,
4272                                                        dad->m_time,
4273                                                        dad->mode);
4274                                 if (! ret2) {
4275                                         ret2 = cli_setatr(&srv->cli, path,
4276                                                           dad->mode,
4277                                                           dad->m_time);
4278                                         if (! ret2) {
4279                                                 errno = smbc_errno(context,
4280                                                                    &srv->cli);
4281                                         }
4282                                 }
4283 #else
4284                                 ret2 = cli_setatr(&srv->cli, path,
4285                                                   dad->mode, dad->m_time);
4286                                 if (! ret2) {
4287                                         errno = smbc_errno(context, &srv->cli);
4288                                 }
4289 #endif
4290
4291                                 /* ret2 has True (success) / False (failure) */
4292                                 if (ret2) {
4293                                         ret = 0;
4294                                 } else {
4295                                         ret = -1;
4296                                 }
4297                         }
4298                 } else {
4299                         ret = -1;
4300                 }
4301
4302                 talloc_destroy(ctx);
4303                 return ret;
4304         }
4305
4306         /* Unsupported attribute name */
4307         talloc_destroy(ctx);
4308         errno = EINVAL;
4309         return -1;
4310 }
4311
4312 int smbc_getxattr_ctx(SMBCCTX *context,
4313                       const char *fname,
4314                       const char *name,
4315                       const void *value,
4316                       size_t size)
4317 {
4318         int ret;
4319         SMBCSRV *srv;
4320         SMBCSRV *ipc_srv;
4321         fstring server, share, user, password, workgroup;
4322         pstring path;
4323         TALLOC_CTX *ctx;
4324         POLICY_HND pol;
4325
4326
4327         if (!context || !context->internal ||
4328             !context->internal->_initialized) {
4329
4330                 errno = EINVAL;  /* Best I can think of ... */
4331                 return -1;
4332     
4333         }
4334
4335         if (!fname) {
4336
4337                 errno = EINVAL;
4338                 return -1;
4339
4340         }
4341   
4342         DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
4343
4344         if (smbc_parse_path(context, fname,
4345                             server, sizeof(server),
4346                             share, sizeof(share),
4347                             path, sizeof(path),
4348                             user, sizeof(user),
4349                             password, sizeof(password),
4350                             NULL, 0)) {
4351                 errno = EINVAL;
4352                 return -1;
4353         }
4354
4355         if (user[0] == (char)0) fstrcpy(user, context->user);
4356
4357         fstrcpy(workgroup, context->workgroup);
4358
4359         srv = smbc_server(context, server, share, workgroup, user, password);
4360         if (!srv) {
4361                 return -1;  /* errno set by smbc_server */
4362         }
4363
4364         if (! srv->no_nt_session) {
4365                 ipc_srv = smbc_attr_server(context, server, share,
4366                                            workgroup, user, password,
4367                                            &pol);
4368                 if (! ipc_srv) {
4369                         srv->no_nt_session = True;
4370                 }
4371         } else {
4372                 ipc_srv = NULL;
4373         }
4374         
4375         ctx = talloc_init("smbc:getxattr");
4376         if (!ctx) {
4377                 errno = ENOMEM;
4378                 return -1;
4379         }
4380
4381         /* Are they requesting a supported attribute? */
4382         if (StrCaseCmp(name, "system.*") == 0 ||
4383             StrCaseCmp(name, "system.*+") == 0 ||
4384             StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4385             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
4386             StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4387             StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4388             StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
4389             StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4390             StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
4391             StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4392             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
4393             StrCaseCmp(name, "system.dos_attr.*") == 0 ||
4394             StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
4395             StrCaseCmp(name, "system.dos_attr.size") == 0 ||
4396             StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
4397             StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
4398             StrCaseCmp(name, "system.dos_attr.m_time") == 0 ||
4399             StrCaseCmp(name, "system.dos_attr.inode") == 0) {
4400
4401                 /* Yup. */
4402                 ret = cacl_get(context, ctx, srv,
4403                                ipc_srv == NULL ? NULL : &ipc_srv->cli, 
4404                                &pol, path, (char *) name, (char *) value, size);
4405                 if (ret < 0 && errno == 0) {
4406                         errno = smbc_errno(context, &srv->cli);
4407                 }
4408                 talloc_destroy(ctx);
4409                 return ret;
4410         }
4411
4412         /* Unsupported attribute name */
4413         talloc_destroy(ctx);
4414         errno = EINVAL;
4415         return -1;
4416 }
4417
4418
4419 int smbc_removexattr_ctx(SMBCCTX *context,
4420                       const char *fname,
4421                       const char *name)
4422 {
4423         int ret;
4424         SMBCSRV *srv;
4425         SMBCSRV *ipc_srv;
4426         fstring server, share, user, password, workgroup;
4427         pstring path;
4428         TALLOC_CTX *ctx;
4429         POLICY_HND pol;
4430
4431         if (!context || !context->internal ||
4432             !context->internal->_initialized) {
4433
4434                 errno = EINVAL;  /* Best I can think of ... */
4435                 return -1;
4436     
4437         }
4438
4439         if (!fname) {
4440
4441                 errno = EINVAL;
4442                 return -1;
4443
4444         }
4445   
4446         DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
4447
4448         if (smbc_parse_path(context, fname,
4449                             server, sizeof(server),
4450                             share, sizeof(share),
4451                             path, sizeof(path),
4452                             user, sizeof(user),
4453                             password, sizeof(password),
4454                             NULL, 0)) {
4455                 errno = EINVAL;
4456                 return -1;
4457         }
4458
4459         if (user[0] == (char)0) fstrcpy(user, context->user);
4460
4461         fstrcpy(workgroup, context->workgroup);
4462
4463         srv = smbc_server(context, server, share, workgroup, user, password);
4464         if (!srv) {
4465                 return -1;  /* errno set by smbc_server */
4466         }
4467
4468         if (! srv->no_nt_session) {
4469                 ipc_srv = smbc_attr_server(context, server, share,
4470                                            workgroup, user, password,
4471                                            &pol);
4472                 srv->no_nt_session = True;
4473         } else {
4474                 ipc_srv = NULL;
4475         }
4476         
4477         if (! ipc_srv) {
4478                 return -1; /* errno set by smbc_attr_server */
4479         }
4480
4481         ctx = talloc_init("smbc_removexattr");
4482         if (!ctx) {
4483                 errno = ENOMEM;
4484                 return -1;
4485         }
4486
4487         /* Are they asking to set the entire ACL? */
4488         if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
4489             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
4490
4491                 /* Yup. */
4492                 ret = cacl_set(ctx, &srv->cli,
4493                                &ipc_srv->cli, &pol, path,
4494                                NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
4495                 talloc_destroy(ctx);
4496                 return ret;
4497         }
4498
4499         /*
4500          * Are they asking to remove one or more spceific security descriptor
4501          * attributes?
4502          */
4503         if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
4504             StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
4505             StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
4506             StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
4507             StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
4508             StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
4509             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
4510
4511                 /* Yup. */
4512                 ret = cacl_set(ctx, &srv->cli,
4513                                &ipc_srv->cli, &pol, path,
4514                                name + 19, SMBC_XATTR_MODE_REMOVE, 0);
4515                 talloc_destroy(ctx);
4516                 return ret;
4517         }
4518
4519         /* Unsupported attribute name */
4520         talloc_destroy(ctx);
4521         errno = EINVAL;
4522         return -1;
4523 }
4524
4525 int smbc_listxattr_ctx(SMBCCTX *context,
4526                        const char *fname,
4527                        char *list,
4528                        size_t size)
4529 {
4530         /*
4531          * This isn't quite what listxattr() is supposed to do.  This returns
4532          * the complete set of attribute names, always, rather than only those
4533          * attribute names which actually exist for a file.  Hmmm...
4534          */
4535         const char supported[] =
4536                 "system.*\0"
4537                 "system.*+\0"
4538                 "system.nt_sec_desc.revision\0"
4539                 "system.nt_sec_desc.owner\0"
4540                 "system.nt_sec_desc.owner+\0"
4541                 "system.nt_sec_desc.group\0"
4542                 "system.nt_sec_desc.group+\0"
4543                 "system.nt_sec_desc.acl\0"
4544                 "system.nt_sec_desc.acl+\0"
4545                 "system.nt_sec_desc.*\0"
4546                 "system.nt_sec_desc.*+\0"
4547                 "system.dos_attr.*\0"
4548                 "system.dos_attr.mode\0"
4549                 "system.dos_attr.c_time\0"
4550                 "system.dos_attr.a_time\0"
4551                 "system.dos_attr.m_time\0"
4552                 ;
4553
4554         if (size == 0) {
4555                 return sizeof(supported);
4556         }
4557
4558         if (sizeof(supported) > size) {
4559                 errno = ERANGE;
4560                 return -1;
4561         }
4562
4563         /* this can't be strcpy() because there are embedded null characters */
4564         memcpy(list, supported, sizeof(supported));
4565         return sizeof(supported);
4566 }
4567
4568
4569 /*
4570  * Open a print file to be written to by other calls
4571  */
4572
4573 static SMBCFILE *smbc_open_print_job_ctx(SMBCCTX *context, const char *fname)
4574 {
4575         fstring server, share, user, password;
4576         pstring path;
4577         
4578         if (!context || !context->internal ||
4579             !context->internal->_initialized) {
4580
4581                 errno = EINVAL;
4582                 return NULL;
4583     
4584         }
4585
4586         if (!fname) {
4587
4588                 errno = EINVAL;
4589                 return NULL;
4590
4591         }
4592   
4593         DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
4594
4595         if (smbc_parse_path(context, fname,
4596                             server, sizeof(server),
4597                             share, sizeof(share),
4598                             path, sizeof(path),
4599                             user, sizeof(user),
4600                             password, sizeof(password),
4601                             NULL, 0)) {
4602                 errno = EINVAL;
4603                 return NULL;
4604         }
4605
4606         /* What if the path is empty, or the file exists? */
4607
4608         return context->open(context, fname, O_WRONLY, 666);
4609
4610 }
4611
4612 /*
4613  * Routine to print a file on a remote server ...
4614  *
4615  * We open the file, which we assume to be on a remote server, and then
4616  * copy it to a print file on the share specified by printq.
4617  */
4618
4619 static int smbc_print_file_ctx(SMBCCTX *c_file, const char *fname, SMBCCTX *c_print, const char *printq)
4620 {
4621         SMBCFILE *fid1, *fid2;
4622         int bytes, saverr, tot_bytes = 0;
4623         char buf[4096];
4624
4625         if (!c_file || !c_file->internal->_initialized || !c_print ||
4626             !c_print->internal->_initialized) {
4627
4628                 errno = EINVAL;
4629                 return -1;
4630
4631         }
4632
4633         if (!fname && !printq) {
4634
4635                 errno = EINVAL;
4636                 return -1;
4637
4638         }
4639
4640         /* Try to open the file for reading ... */
4641
4642         if ((int)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
4643                 
4644                 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
4645                 return -1;  /* smbc_open sets errno */
4646                 
4647         }
4648
4649         /* Now, try to open the printer file for writing */
4650
4651         if ((int)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
4652
4653                 saverr = errno;  /* Save errno */
4654                 c_file->close(c_file, fid1);
4655                 errno = saverr;
4656                 return -1;
4657
4658         }
4659
4660         while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
4661
4662                 tot_bytes += bytes;
4663
4664                 if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
4665
4666                         saverr = errno;
4667                         c_file->close(c_file, fid1);
4668                         c_print->close(c_print, fid2);
4669                         errno = saverr;
4670
4671                 }
4672
4673         }
4674
4675         saverr = errno;
4676
4677         c_file->close(c_file, fid1);  /* We have to close these anyway */
4678         c_print->close(c_print, fid2);
4679
4680         if (bytes < 0) {
4681
4682                 errno = saverr;
4683                 return -1;
4684
4685         }
4686
4687         return tot_bytes;
4688
4689 }
4690
4691 /*
4692  * Routine to list print jobs on a printer share ...
4693  */
4694
4695 static int smbc_list_print_jobs_ctx(SMBCCTX *context, const char *fname, smbc_list_print_job_fn fn)
4696 {
4697         SMBCSRV *srv;
4698         fstring server, share, user, password, workgroup;
4699         pstring path;
4700
4701         if (!context || !context->internal ||
4702             !context->internal->_initialized) {
4703
4704                 errno = EINVAL;
4705                 return -1;
4706
4707         }
4708
4709         if (!fname) {
4710                 
4711                 errno = EINVAL;
4712                 return -1;
4713
4714         }
4715   
4716         DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
4717
4718         if (smbc_parse_path(context, fname,
4719                             server, sizeof(server),
4720                             share, sizeof(share),
4721                             path, sizeof(path),
4722                             user, sizeof(user),
4723                             password, sizeof(password),
4724                             NULL, 0)) {
4725                 errno = EINVAL;
4726                 return -1;
4727         }
4728
4729         if (user[0] == (char)0) fstrcpy(user, context->user);
4730         
4731         fstrcpy(workgroup, context->workgroup);
4732
4733         srv = smbc_server(context, server, share, workgroup, user, password);
4734
4735         if (!srv) {
4736
4737                 return -1;  /* errno set by smbc_server */
4738
4739         }
4740
4741         if (cli_print_queue(&srv->cli, (void (*)(struct print_job_info *))fn) < 0) {
4742
4743                 errno = smbc_errno(context, &srv->cli);
4744                 return -1;
4745
4746         }
4747         
4748         return 0;
4749
4750 }
4751
4752 /*
4753  * Delete a print job from a remote printer share
4754  */
4755
4756 static int smbc_unlink_print_job_ctx(SMBCCTX *context, const char *fname, int id)
4757 {
4758         SMBCSRV *srv;
4759         fstring server, share, user, password, workgroup;
4760         pstring path;
4761         int err;
4762
4763         if (!context || !context->internal ||
4764             !context->internal->_initialized) {
4765
4766                 errno = EINVAL;
4767                 return -1;
4768
4769         }
4770
4771         if (!fname) {
4772
4773                 errno = EINVAL;
4774                 return -1;
4775
4776         }
4777   
4778         DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
4779
4780         if (smbc_parse_path(context, fname,
4781                             server, sizeof(server),
4782                             share, sizeof(share),
4783                             path, sizeof(path),
4784                             user, sizeof(user),
4785                             password, sizeof(password),
4786                             NULL, 0)) {
4787                 errno = EINVAL;
4788                 return -1;
4789         }
4790
4791         if (user[0] == (char)0) fstrcpy(user, context->user);
4792
4793         fstrcpy(workgroup, context->workgroup);
4794
4795         srv = smbc_server(context, server, share, workgroup, user, password);
4796
4797         if (!srv) {
4798
4799                 return -1;  /* errno set by smbc_server */
4800
4801         }
4802
4803         if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
4804
4805                 if (err < 0)
4806                         errno = smbc_errno(context, &srv->cli);
4807                 else if (err == ERRnosuchprintjob)
4808                         errno = EINVAL;
4809                 return -1;
4810
4811         }
4812
4813         return 0;
4814
4815 }
4816
4817 /*
4818  * Get a new empty handle to fill in with your own info 
4819  */
4820 SMBCCTX * smbc_new_context(void)
4821 {
4822         SMBCCTX * context;
4823
4824         context = SMB_MALLOC_P(SMBCCTX);
4825         if (!context) {
4826                 errno = ENOMEM;
4827                 return NULL;
4828         }
4829
4830         ZERO_STRUCTP(context);
4831
4832         context->internal = SMB_MALLOC_P(struct smbc_internal_data);
4833         if (!context->internal) {
4834                 errno = ENOMEM;
4835                 return NULL;
4836         }
4837
4838         ZERO_STRUCTP(context->internal);
4839
4840         
4841         /* ADD REASONABLE DEFAULTS */
4842         context->debug            = 0;
4843         context->timeout          = 20000; /* 20 seconds */
4844
4845         context->options.browse_max_lmb_count      = 3;    /* # LMBs to query */
4846         context->options.urlencode_readdir_entries = False;/* backward compat */
4847         context->options.one_share_per_server      = False;/* backward compat */
4848
4849         context->open                              = smbc_open_ctx;
4850         context->creat                             = smbc_creat_ctx;
4851         context->read                              = smbc_read_ctx;
4852         context->write                             = smbc_write_ctx;
4853         context->close                             = smbc_close_ctx;
4854         context->unlink                            = smbc_unlink_ctx;
4855         context->rename                            = smbc_rename_ctx;
4856         context->lseek                             = smbc_lseek_ctx;
4857         context->stat                              = smbc_stat_ctx;
4858         context->fstat                             = smbc_fstat_ctx;
4859         context->opendir                           = smbc_opendir_ctx;
4860         context->closedir                          = smbc_closedir_ctx;
4861         context->readdir                           = smbc_readdir_ctx;
4862         context->getdents                          = smbc_getdents_ctx;
4863         context->mkdir                             = smbc_mkdir_ctx;
4864         context->rmdir                             = smbc_rmdir_ctx;
4865         context->telldir                           = smbc_telldir_ctx;
4866         context->lseekdir                          = smbc_lseekdir_ctx;
4867         context->fstatdir                          = smbc_fstatdir_ctx;
4868         context->chmod                             = smbc_chmod_ctx;
4869         context->utimes                            = smbc_utimes_ctx;
4870         context->setxattr                          = smbc_setxattr_ctx;
4871         context->getxattr                          = smbc_getxattr_ctx;
4872         context->removexattr                       = smbc_removexattr_ctx;
4873         context->listxattr                         = smbc_listxattr_ctx;
4874         context->open_print_job                    = smbc_open_print_job_ctx;
4875         context->print_file                        = smbc_print_file_ctx;
4876         context->list_print_jobs                   = smbc_list_print_jobs_ctx;
4877         context->unlink_print_job                  = smbc_unlink_print_job_ctx;
4878
4879         context->callbacks.check_server_fn         = smbc_check_server;
4880         context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
4881
4882         smbc_default_cache_functions(context);
4883
4884         return context;
4885 }
4886
4887 /* 
4888  * Free a context
4889  *
4890  * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed 
4891  * and thus you'll be leaking memory if not handled properly.
4892  *
4893  */
4894 int smbc_free_context(SMBCCTX * context, int shutdown_ctx)
4895 {
4896         if (!context) {
4897                 errno = EBADF;
4898                 return 1;
4899         }
4900         
4901         if (shutdown_ctx) {
4902                 SMBCFILE * f;
4903                 DEBUG(1,("Performing aggressive shutdown.\n"));
4904                 
4905                 f = context->internal->_files;
4906                 while (f) {
4907                         context->close(context, f);
4908                         f = f->next;
4909                 }
4910                 context->internal->_files = NULL;
4911
4912                 /* First try to remove the servers the nice way. */
4913                 if (context->callbacks.purge_cached_fn(context)) {
4914                         SMBCSRV * s;
4915                         SMBCSRV * next;
4916                         DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
4917                         s = context->internal->_servers;
4918                         while (s) {
4919                                 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n", s, s->cli.fd));
4920                                 cli_shutdown(&s->cli);
4921                                 context->callbacks.remove_cached_srv_fn(context, s);
4922                                 next = s->next;
4923                                 DLIST_REMOVE(context->internal->_servers, s);
4924                                 SAFE_FREE(s);
4925                                 s = next;
4926                         }
4927                         context->internal->_servers = NULL;
4928                 }
4929         }
4930         else {
4931                 /* This is the polite way */    
4932                 if (context->callbacks.purge_cached_fn(context)) {
4933                         DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
4934                         errno = EBUSY;
4935                         return 1;
4936                 }
4937                 if (context->internal->_servers) {
4938                         DEBUG(1, ("Active servers in context, free_context failed.\n"));
4939                         errno = EBUSY;
4940                         return 1;
4941                 }
4942                 if (context->internal->_files) {
4943                         DEBUG(1, ("Active files in context, free_context failed.\n"));
4944                         errno = EBUSY;
4945                         return 1;
4946                 }               
4947         }
4948
4949         /* Things we have to clean up */
4950         SAFE_FREE(context->workgroup);
4951         SAFE_FREE(context->netbios_name);
4952         SAFE_FREE(context->user);
4953         
4954         DEBUG(3, ("Context %p succesfully freed\n", context));
4955         SAFE_FREE(context->internal);
4956         SAFE_FREE(context);
4957         return 0;
4958 }
4959
4960
4961 /*
4962  * Initialise the library etc 
4963  *
4964  * We accept a struct containing handle information.
4965  * valid values for info->debug from 0 to 100,
4966  * and insist that info->fn must be non-null.
4967  */
4968 SMBCCTX * smbc_init_context(SMBCCTX * context)
4969 {
4970         pstring conf;
4971         int pid;
4972         char *user = NULL, *home = NULL;
4973
4974         if (!context || !context->internal) {
4975                 errno = EBADF;
4976                 return NULL;
4977         }
4978
4979         /* Do not initialise the same client twice */
4980         if (context->internal->_initialized) { 
4981                 return 0;
4982         }
4983
4984         if (!context->callbacks.auth_fn || context->debug < 0 || context->debug > 100) {
4985
4986                 errno = EINVAL;
4987                 return NULL;
4988
4989         }
4990
4991         if (!smbc_initialized) {
4992                 /* Do some library wide intialisations the first time we get called */
4993
4994                 /* Set this to what the user wants */
4995                 DEBUGLEVEL = context->debug;
4996                 
4997                 setup_logging( "libsmbclient", True);
4998
4999                 /* Here we would open the smb.conf file if needed ... */
5000                 
5001                 home = getenv("HOME");
5002
5003                 slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
5004                 
5005                 load_interfaces();  /* Load the list of interfaces ... */
5006                 
5007                 in_client = True; /* FIXME, make a param */
5008
5009                 if (!lp_load(conf, True, False, False)) {
5010
5011                         /*
5012                          * Well, if that failed, try the dyn_CONFIGFILE
5013                          * Which points to the standard locn, and if that
5014                          * fails, silently ignore it and use the internal
5015                          * defaults ...
5016                          */
5017
5018                    if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
5019                       DEBUG(5, ("Could not load either config file: %s or %s\n",
5020                              conf, dyn_CONFIGFILE));
5021                    }
5022                 }
5023
5024                 reopen_logs();  /* Get logging working ... */
5025         
5026                 /* 
5027                  * Block SIGPIPE (from lib/util_sock.c: write())  
5028                  * It is not needed and should not stop execution 
5029                  */
5030                 BlockSignals(True, SIGPIPE);
5031                 
5032                 /* Done with one-time initialisation */
5033                 smbc_initialized = 1; 
5034
5035         }
5036         
5037         if (!context->user) {
5038                 /*
5039                  * FIXME: Is this the best way to get the user info? 
5040                  */
5041                 user = getenv("USER");
5042                 /* walk around as "guest" if no username can be found */
5043                 if (!user) context->user = SMB_STRDUP("guest");
5044                 else context->user = SMB_STRDUP(user);
5045         }
5046
5047         if (!context->netbios_name) {
5048                 /*
5049                  * We try to get our netbios name from the config. If that fails we fall
5050                  * back on constructing our netbios name from our hostname etc
5051                  */
5052                 if (global_myname()) {
5053                         context->netbios_name = SMB_STRDUP(global_myname());
5054                 }
5055                 else {
5056                         /*
5057                          * Hmmm, I want to get hostname as well, but I am too lazy for the moment
5058                          */
5059                         pid = sys_getpid();
5060                         context->netbios_name = SMB_MALLOC(17);
5061                         if (!context->netbios_name) {
5062                                 errno = ENOMEM;
5063                                 return NULL;
5064                         }
5065                         slprintf(context->netbios_name, 16, "smbc%s%d", context->user, pid);
5066                 }
5067         }
5068
5069         DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
5070
5071         if (!context->workgroup) {
5072                 if (lp_workgroup()) {
5073                         context->workgroup = SMB_STRDUP(lp_workgroup());
5074                 }
5075                 else {
5076                         /* TODO: Think about a decent default workgroup */
5077                         context->workgroup = SMB_STRDUP("samba");
5078                 }
5079         }
5080
5081         DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
5082                                         
5083         /* shortest timeout is 1 second */
5084         if (context->timeout > 0 && context->timeout < 1000) 
5085                 context->timeout = 1000;
5086
5087         /*
5088          * FIXME: Should we check the function pointers here? 
5089          */
5090
5091         context->internal->_initialized = 1;
5092         
5093         return context;
5094 }
5095
5096
5097 /* Return the verion of samba, and thus libsmbclient */
5098 const char *
5099 smbc_version(void)
5100 {
5101         return samba_version_string();
5102 }