File: context.c

package info (click to toggle)
pysmbc 1.0.15.3-0.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 260 kB
  • ctags: 248
  • sloc: ansic: 2,031; python: 741; makefile: 24
file content (1268 lines) | stat: -rw-r--r-- 35,531 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
/* -*- Mode: C; c-file-style: "gnu" -*-
 * pysmbc - Python bindings for libsmbclient
 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2010, 2011, 2012, 2013  Red Hat, Inc
 * Copyright (C) 2010  Open Source Solution Technology Corporation
 * Copyright (C) 2010  Patrick Geltinger <patlkli@patlkli.org>
 * Authors:
 *  Tim Waugh <twaugh@redhat.com>
 *  Tsukasa Hamano <hamano@osstech.co.jp>
 *  Patrick Geltinger <patlkli@patlkli.org>
 *  Roberto Polli <rpolli@babel.it>
 *  Fabio Isgro' <fisgro@babel.it>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <Python.h>
#include <string.h>
#include "smbcmodule.h"
#include "context.h"
#include "dir.h"
#include "file.h"

static void
auth_fn (SMBCCTX *ctx,
	 const char *server, const char *share,
	 char *workgroup, int wgmaxlen,
	 char *username, int unmaxlen,
	 char *password, int pwmaxlen)
{
  PyObject *args;
  PyObject *kwds;
  PyObject *result;
  Context *self;
  const char *use_workgroup, *use_username, *use_password;

  debugprintf ("-> auth_fn (server=%s, share=%s)\n",
	       server ? server : "",
	       share ? share : "");

  self = smbc_getOptionUserData (ctx);
  if (self->auth_fn == NULL)
    {
      debugprintf ("<- auth_fn (), no callback\n");
      return;
    }

  if (!server || !*server)
    {
      debugprintf ("<- auth_fn(), no server\n");
      return;
    }

  args = Py_BuildValue ("(sssss)", server, share, workgroup,
			username, password);
  kwds = PyDict_New ();

  result = PyObject_Call (self->auth_fn, args, kwds);
  Py_DECREF (args);
  Py_DECREF (kwds);
  if (result == NULL)
    {
      debugprintf ("<- auth_fn(), failed callback\n");
      return;
    }

  if (!PyArg_ParseTuple (result, "sss",
			 &use_workgroup,
			 &use_username,
			 &use_password))
    {
      Py_DECREF (result);
      debugprintf ("<- auth_fn(), incorrect callback result\n");
      return;
    }

  strncpy (workgroup, use_workgroup, wgmaxlen);
  strncpy (username, use_username, unmaxlen);
  strncpy (password, use_password, pwmaxlen);
  Py_DECREF (result);
  debugprintf ("<- auth_fn(), got callback result\n");
}

/////////////
// Context //
/////////////

static PyObject *
Context_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
{
  Context *self;
  self = (Context *) type->tp_alloc (type, 0);
  if (self != NULL)
    self->context = NULL;

  return (PyObject *) self;
}

static int
Context_init (Context *self, PyObject *args, PyObject *kwds)
{
  PyObject *auth = NULL;
  int debug = 0;
  SMBCCTX *ctx;
  static char *kwlist[] =
    {
      "auth_fn",
      "debug",
      NULL
    };

  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|Oi", kwlist,
				    &auth, &debug))
    {
      return -1;
    }

  if (auth)
    {
      if (!PyCallable_Check (auth))
	{
	  PyErr_SetString (PyExc_TypeError, "auth_fn must be callable");
	  return -1;
	}

      Py_XINCREF (auth);
      self->auth_fn = auth;
    }

  debugprintf ("-> Context_init ()\n");

  errno = 0;
  ctx = smbc_new_context ();
  if (ctx == NULL)
    {
      PyErr_SetFromErrno (PyExc_RuntimeError);
      debugprintf ("<- Context_init() EXCEPTION\n");
      return -1;
    }

  smbc_setDebug (ctx, debug);

  self->context = ctx;
  smbc_setOptionUserData (ctx, self);
  if (auth)
    smbc_setFunctionAuthDataWithContext (ctx, auth_fn);

  if (smbc_init_context (ctx) == NULL)
    {
      PyErr_SetFromErrno (PyExc_RuntimeError);
      smbc_free_context (ctx, 0);
      debugprintf ("<- Context_init() EXCEPTION\n");
      return -1;
    }

  debugprintf ("%p <- Context_init() = 0\n", self->context);
  return 0;
}

static void
Context_dealloc (Context *self)
{
  if (self->context)
    {
      debugprintf ("%p smbc_free_context()\n", self->context);
      smbc_free_context (self->context, 1);
    }

  Py_TYPE(self)->tp_free ((PyObject *) self);
}

static PyObject *
Context_set_credentials_with_fallback (Context *self, PyObject *args)
{
  char *workgroup = NULL;
  char *user = NULL;
  char *password = NULL;
  debugprintf ("%p -> Context_set_credentials_with_fallback()\n",
	       self->context);
  if (!PyArg_ParseTuple (args, "sss", &workgroup, &user, &password))
    {
      debugprintf ("%p <- Context_open() EXCEPTION\n", self->context);
      return NULL;
    }

  smbc_set_credentials_with_fallback (self->context,
				      workgroup,
				      user,
				      password);
  debugprintf ("%p <- Context_set_credentials_with_fallback()\n",
	       self->context);
  Py_RETURN_NONE;
}

static PyObject *
Context_open (Context *self, PyObject *args)
{
  PyObject *largs, *lkwlist;
  char *uri;
  File *file;
  int flags = 0;
  int mode = 0;
  smbc_open_fn fn;

  debugprintf ("%p -> Context_open()\n", self->context);
  if (!PyArg_ParseTuple (args, "s|ii", &uri, &flags, &mode))
    {
      debugprintf ("%p <- Context_open() EXCEPTION\n", self->context);
      return NULL;
    }

  largs = Py_BuildValue ("()");
  lkwlist = PyDict_New ();
  PyDict_SetItemString (lkwlist, "context", (PyObject *) self);
  file = (File *)smbc_FileType.tp_new (&smbc_FileType, largs, lkwlist);
  if (!file)
    {
      return PyErr_NoMemory ();
    }

  if (smbc_FileType.tp_init ((PyObject *)file, largs, lkwlist) < 0)
    {
      smbc_FileType.tp_dealloc ((PyObject *)file);
      debugprintf ("%p <- Context_open() EXCEPTION\n", self->context);
      // already set error
      return NULL;
    }

  fn = smbc_getFunctionOpen (self->context);
  errno = 0;
  file->file = (*fn) (self->context, uri, (int)flags, (mode_t)mode);
  if (!file->file)
    {
      pysmbc_SetFromErrno ();
      smbc_FileType.tp_dealloc ((PyObject *)file);
      file = NULL;
    }

  Py_DECREF (largs);
  Py_DECREF (lkwlist);
  debugprintf ("%p <- Context_open() = File\n", self->context);
  return (PyObject *)file;
}

static PyObject *
Context_creat (Context *self, PyObject *args)
{
  PyObject *largs, *lkwlist;
  char *uri;
  int mode = 0;
  File *file;
  smbc_creat_fn fn;

  if (!PyArg_ParseTuple (args, "s|i", &uri, &mode))
    {
      return NULL;
    }

  largs = Py_BuildValue ("()");
  lkwlist = PyDict_New ();
  PyDict_SetItemString (lkwlist, "context", (PyObject *) self);
  file = (File *)smbc_FileType.tp_new (&smbc_FileType, largs, lkwlist);
  if (!file)
    {
      return PyErr_NoMemory();
    }

  if (smbc_FileType.tp_init ((PyObject *)file, largs, lkwlist) < 0)
    {
      smbc_FileType.tp_dealloc ((PyObject *)file);
      return NULL;
    }

  fn = smbc_getFunctionCreat (self->context);
  errno = 0;
  file->file = (*fn) (self->context, uri, mode);
  if (!file->file)
    {
      pysmbc_SetFromErrno ();
      smbc_FileType.tp_dealloc ((PyObject *)file);
      file = NULL;
    }

  Py_DECREF (largs);
  Py_DECREF (lkwlist);
  return (PyObject *)file;
}

static PyObject *
Context_unlink (Context *self, PyObject *args)
{
  int ret;
  char *uri = NULL;
  smbc_unlink_fn fn;

  if(!PyArg_ParseTuple (args, "s", &uri))
    {
      return NULL;
    }

  fn = smbc_getFunctionUnlink (self->context);
  errno = 0;
  ret = (*fn) (self->context, uri);
  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return PyLong_FromLong (ret);
}

static PyObject *
Context_rename (Context *self, PyObject *args)
{
  int ret;
  char *ouri = NULL;
  char *nuri = NULL;
  Context *nctx = NULL;
  smbc_rename_fn fn;

  if (!PyArg_ParseTuple (args, "ss|O", &ouri, &nuri, &nctx))
    {
      return NULL;
    }

  fn = smbc_getFunctionRename(self->context);
  errno = 0;
  if (nctx && nctx->context)
    {
      ret = (*fn) (self->context, ouri, nctx->context, nuri);
    }
  else
    {
      ret = (*fn) (self->context, ouri, self->context, nuri);
    }

  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return PyLong_FromLong (ret);
}

static PyObject *
Context_opendir (Context *self, PyObject *args)
{
  PyObject *largs, *lkwlist;
  PyObject *uri;
  PyObject *dir;

  debugprintf ("%p -> Context_opendir()\n", self->context);
  if (!PyArg_ParseTuple (args, "O", &uri))
    {
      debugprintf ("%p <- Context_opendir() EXCEPTION\n", self->context);
      return NULL;
    }

  largs = Py_BuildValue ("()");
  lkwlist = PyDict_New ();
  PyDict_SetItemString (lkwlist, "context", (PyObject *) self);
  PyDict_SetItemString (lkwlist, "uri", uri);
  dir = smbc_DirType.tp_new (&smbc_DirType, largs, lkwlist);
  if (smbc_DirType.tp_init (dir, largs, lkwlist) < 0)
    {
      smbc_DirType.tp_dealloc (dir);
      debugprintf ("%p <- Context_opendir() EXCEPTION\n", self->context);
      return NULL;
    }

  Py_DECREF (largs);
  Py_DECREF (lkwlist);
  debugprintf ("%p <- Context_opendir() = Dir\n", self->context);
  return dir;
}

static PyObject *
Context_mkdir (Context *self, PyObject *args)
{
  int ret;
  char *uri = NULL;
  unsigned int mode = 0;
  smbc_mkdir_fn fn;

  if (!PyArg_ParseTuple (args, "s|I", &uri, &mode))
    {
      return NULL;
    }

  fn = smbc_getFunctionMkdir (self->context);
  errno = 0;
  ret = (*fn) (self->context, uri, mode);
  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return PyLong_FromLong (ret);
}

static PyObject *
Context_rmdir (Context *self, PyObject *args)
{
  int ret;
  char *uri = NULL;
  smbc_rmdir_fn fn;

  if (!PyArg_ParseTuple (args, "s", &uri))
    {
      return NULL;
    }

  fn = smbc_getFunctionRmdir (self->context);
  errno = 0;
  ret = (*fn) (self->context, uri);
  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return PyLong_FromLong (ret);
}

static PyObject *
Context_stat (Context *self, PyObject *args)
{
  int ret;
  char *uri = NULL;
  smbc_stat_fn fn;
  struct stat st;

  if (!PyArg_ParseTuple (args, "s", &uri))
    {
      return NULL;
    }

  fn = smbc_getFunctionStat (self->context);
  errno = 0;
  ret = (*fn) (self->context, uri, &st);
  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return Py_BuildValue ("(IKKKIIKIII)",
			st.st_mode,
			(unsigned long long)st.st_ino,
			(unsigned long long)st.st_dev,
			(unsigned long long)st.st_nlink,
			st.st_uid,
			st.st_gid,
			st.st_size,
			st.st_atime,
			st.st_mtime,
			st.st_ctime);
}

static PyObject *
Context_chmod (Context *self, PyObject *args)
{
  int ret;
  char *uri = NULL;
  mode_t mode = 0;
  smbc_chmod_fn fn;

  if (!PyArg_ParseTuple (args, "si", &uri, &mode))
    {
      return NULL;
    }

  errno = 0;
  fn = smbc_getFunctionChmod (self->context);
  ret = (*fn) (self->context, uri, mode);
  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return PyLong_FromLong (ret);
}


/**
 * Wrapper for the smbc_getxattr() smbclient function. From libsmbclient.h
 * @author fisgro@babel.it, rpolli@babel.it
 *
 * @param uri			The smb url of the file or directory to get extended
 *                  attributes for.
 *
 * @param name      The name of an attribute to be retrieved.  Names are of
 *                  one of the following forms:
 *
 *                     system.nt_sec_desc.<attribute name>
 *                     system.nt_sec_desc.*
 *                     system.nt_sec_desc.*+
 */
static PyObject *
Context_getxattr (Context *self, PyObject *args)
{
  int ret;
  char *uri = NULL;
  char *name = NULL;
  char value[1024];
  static smbc_getxattr_fn fn;

  bzero(value, sizeof (value));

  // smbc_getxattr takes two string parameters
  if (!PyArg_ParseTuple (args, "ss", &uri, &name))
    {
      return NULL;
    }

  errno = 0;
  fn = smbc_getFunctionGetxattr(self->context);
  ret = (*fn)(self->context, uri, name, value, sizeof (value));

  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
  }

  return PyUnicode_FromString (value);
}


/**
 * Wrapper for the smbc_setxattr() smbclient function. From libsmbclient.h
 * @author fisgro@babel.it, rpolli@babel.it
 *
 * @param uri			The smb url of the file or directory to set extended
 *                  attributes for.
 *
 * @param name      The name of an attribute to be retrieved.  Names are of
 *                  one of the following forms:
 *
 *                     system.nt_sec_desc.<attribute name>
 * 		       system.nt_sec_desc.<attribute name>+
 * 		       system.nt_sec_desc.revision DANGEROUS!!!
 * 		       system.nt_sec_desc.owner
 * 		       system.nt_sec_desc.owner+
 * 		       system.nt_sec_desc.group
 * 		       system.nt_sec_desc.group+
 *                     system.nt_sec_desc.*
 *                     system.nt_sec_desc.*+
 * 		       system.nt_sec_desc.ACL:<type>/<flags>/<mask>
 *
 * @param value     The value to be assigned to the specified attribute name.
 *                  This buffer should contain only the attribute value if the
 *                  name was of the "system.nt_sec_desc.<attribute_name>"
 *                  form.  If the name was of the "system.nt_sec_desc.*" form
 *                  then a complete security descriptor, with name:value pairs
 *                  separated by tabs, commas, or newlines (not spaces!),
 *                  should be provided in this value buffer.  A complete
 *                  security descriptor will contain one or more entries
 *                  selected from the following:
 *
 *                    REVISION:<revision number>
 *                    OWNER:<sid or name>
 *                    GROUP:<sid or name>
 *                    ACL:<sid or name>:<type>/<flags>/<mask>
 *
 *                  The  revision of the ACL specifies the internal Windows NT
 *                  ACL revision for the security descriptor. If not specified
 *                  it defaults to  1.  Using values other than 1 may cause
 *                  strange behaviour.
 *
 *                  The owner and group specify the owner and group sids for
 *                  the object. If the attribute name (either '*+' with a
 *                  complete security descriptor, or individual 'owner+' or
 *                  'group+' attribute names) ended with a plus sign, the
 *                  specified name is resolved to a SID value, using the
 *                  server on which the file or directory resides.  Otherwise,
 *                  the value should be provided in SID-printable format as
 *                  S-1-x-y-z, and is used directly.  The <sid or name>
 *                  associated with the ACL: attribute should be provided
 *                  similarly.
 * @return          0 on success, < 0 on error with errno set:
 *                  - EINVAL  The client library is not properly initialized
 *                            or one of the parameters is not of a correct
 *                            form
 *                  - ENOMEM No memory was available for internal needs
 *                  - EEXIST  If the attribute already exists and the flag
 *                            SMBC_XATTR_FLAG_CREAT was specified
 *                  - ENOATTR If the attribute does not exist and the flag
 *                            SMBC_XATTR_FLAG_REPLACE was specified
 *                  - EPERM   Permission was denied.
 *                  - ENOTSUP The referenced file system does not support
 *                            extended attributes
 *
  */

static PyObject*
Context_setxattr (Context *self, PyObject *args)
{
  int ret;
  char *uri = NULL;
  char *name = NULL;
  char *value = NULL;
  unsigned int flags;
  static smbc_setxattr_fn fn;


  if (!PyArg_ParseTuple (args, "sssi", &uri, &name, &value, &flags))
    {
      return NULL;
    }

  if (!value)
    {
      return NULL;
    }

  errno = 0;
  fn = smbc_getFunctionSetxattr (self->context);

  ret = (*fn)(self->context, uri, name, value, strlen (value), flags);

  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return PyLong_FromLong (ret);
}




static PyObject *
Context_getDebug (Context *self, void *closure)
{
  int d = smbc_getDebug (self->context);
  return PyLong_FromLong (d);
}

static int
Context_setDebug (Context *self, PyObject *value, void *closure)
{
  int d;

#if PY_MAJOR_VERSION < 3
  if (PyInt_Check (value))
    value = PyLong_FromLong (PyInt_AsLong (value));
#endif

  if (!PyLong_Check (value))
    {
      PyErr_SetString (PyExc_TypeError, "must be int");
      return -1;
    }

  d = PyLong_AsLong (value);
  smbc_setDebug (self->context, d);
  return 0;
}

static PyObject *
Context_getNetbiosName (Context *self, void *closure)
{
  const char *netbios_name = smbc_getNetbiosName (self->context);
  return PyUnicode_FromString (netbios_name);
}

static int
Context_setNetbiosName (Context *self, PyObject *value, void *closure)
{
  wchar_t *w_name;
  size_t chars;
  char *name;
  size_t bytes;
  ssize_t written;

#if PY_MAJOR_VERSION < 3
  if (PyString_Check (value))
    value = PyUnicode_FromString (PyString_AsString (value));
#endif

  if (!PyUnicode_Check (value))
    {
      PyErr_SetString (PyExc_TypeError, "must be string");
      return -1;
    }

  chars = PyUnicode_GetSize (value); /* not including NUL */
  w_name = malloc ((chars + 1) * sizeof (wchar_t));
  if (!w_name)
    {
      PyErr_NoMemory ();
      return -1;
    }

#if PY_MAJOR_VERSION < 3
  if (PyUnicode_AsWideChar ((PyUnicodeObject *) value, w_name, chars) == -1)
#else
  if (PyUnicode_AsWideChar (value, w_name, chars) == -1)
#endif
    {
      free (w_name);
      return -1;
    }

  w_name[chars] = L'\0';
  bytes = MB_CUR_MAX * chars + 1; /* extra byte for NUL */
  name = malloc (bytes);
  if (!name)
    {
      free (w_name);
      PyErr_NoMemory ();
      return -1;
    }

  written = wcstombs (name, w_name, bytes);
  free (w_name);

  if (written == -1)
    name[0] = '\0';
  else
    /* NUL-terminate it (this is why we allocated an extra byte) */
    name[written] = '\0';

  smbc_setNetbiosName (self->context, name);
  // Don't free name: the API function just takes a reference(!)
  return 0;
}

static PyObject *
Context_getWorkgroup (Context *self, void *closure)
{
  const char *workgroup = smbc_getWorkgroup (self->context);
  return PyUnicode_FromString (workgroup);
}

static int
Context_setWorkgroup (Context *self, PyObject *value, void *closure)
{
  wchar_t *w_workgroup;
  size_t chars;
  char *workgroup;
  size_t bytes;
  ssize_t written;

#if PY_MAJOR_VERSION < 3
  if (PyString_Check (value))
    value = PyUnicode_FromString (PyString_AsString (value));
#endif

  if (!PyUnicode_Check (value))
    {
      PyErr_SetString (PyExc_TypeError, "must be string");
      return -1;
    }

  chars = PyUnicode_GetSize (value); /* not including NUL */
  w_workgroup = malloc ((chars + 1) * sizeof (wchar_t));
  if (!w_workgroup)
    {
      PyErr_NoMemory ();
      return -1;
    }

#if PY_MAJOR_VERSION < 3
  if (PyUnicode_AsWideChar ((PyUnicodeObject *) value,
			    w_workgroup, chars) == -1)
#else
  if (PyUnicode_AsWideChar (value, w_workgroup, chars) == -1)
#endif
    {
      free (w_workgroup);
      return -1;
    }

  w_workgroup[chars] = L'\0';
  bytes = MB_CUR_MAX * chars + 1; /* extra byte for NUL */
  workgroup = malloc (bytes);
  if (!workgroup)
    {
      free (w_workgroup);
      PyErr_NoMemory ();
      return -1;
    }

  written = wcstombs (workgroup, w_workgroup, bytes);
  free (w_workgroup);

  if (written == -1)
    workgroup[0] = '\0';
  else
    /* NUL-terminate it (this is why we allocated the extra byte) */
    workgroup[written] = '\0';

  smbc_setWorkgroup (self->context, workgroup);
  // Don't free workgroup: the API function just takes a reference(!)
  return 0;
}

static PyObject *
Context_getTimeout (Context *self, void *closure)
{
  int timeout = smbc_getTimeout (self->context);
  return PyLong_FromLong (timeout);
}

static int
Context_setTimeout (Context *self, PyObject *value, void *closure)
{
#if PY_MAJOR_VERSION < 3
  if (!PyInt_Check (value))
#else
  if (!PyLong_Check (value))
#endif
    {
      PyErr_SetString (PyExc_TypeError, "must be long");
      return -1;
    }

#if PY_MAJOR_VERSION < 3
  smbc_setTimeout (self->context, PyInt_AsLong (value));
#else
  smbc_setTimeout (self->context, PyLong_AsLong (value));
#endif
  return 0;
}

static int
Context_setFunctionAuthData (Context *self, PyObject *value, void *closure)
{
  if (!PyCallable_Check (value))
    {
      PyErr_SetString (PyExc_TypeError, "must be callable object");
      return -1;
    }

  Py_XINCREF (value);
  self->auth_fn = value;
  smbc_setFunctionAuthDataWithContext (self->context, auth_fn);
  return 0;
}

static PyObject *
Context_getOptionDebugToStderr (Context *self, void *closure)
{
  smbc_bool b;
  b = smbc_getOptionDebugToStderr (self->context);
  return PyBool_FromLong ((long) b);
}

static int
Context_setOptionDebugToStderr (Context *self, PyObject *value,
				void *closure)
{
  if (!PyBool_Check (value))
    {
      PyErr_SetString (PyExc_TypeError, "must be Boolean");
      return -1;
    }

  smbc_setOptionDebugToStderr (self->context, value == Py_True);
  return 0;
}

static PyObject *
Context_getOptionNoAutoAnonymousLogin (Context *self, void *closure)
{
  smbc_bool b;
  b = smbc_getOptionNoAutoAnonymousLogin (self->context);
  return PyBool_FromLong ((long) b);
}

static int
Context_setOptionNoAutoAnonymousLogin (Context *self, PyObject *value,
				       void *closure)
{
  if (!PyBool_Check (value))
    {
      PyErr_SetString (PyExc_TypeError, "must be Boolean");
      return -1;
    }

  smbc_setOptionNoAutoAnonymousLogin (self->context, value == Py_True);
  return 0;
}

static PyObject *
Context_getOptionUseKerberos (Context *self, void *closure)
{
  smbc_bool b;
  b = smbc_getOptionUseKerberos (self->context);
  return PyBool_FromLong ((long) b);
}

static int
Context_setOptionUseKerberos (Context *self, PyObject *value,
			      void *closure)
{
  if (!PyBool_Check (value))
    {
      PyErr_SetString (PyExc_TypeError, "must be Boolean");
      return -1;
    }

  smbc_setOptionUseKerberos (self->context, value == Py_True);
  return 0;
}

static PyObject *
Context_getOptionFallbackAfterKerberos (Context *self, void *closure)
{
  smbc_bool b;
  b = smbc_getOptionFallbackAfterKerberos (self->context);
  return PyBool_FromLong ((long) b);
}

static int
Context_setOptionFallbackAfterKerberos (Context *self, PyObject *value,
					void *closure)
{
  if (!PyBool_Check (value))
    {
      PyErr_SetString (PyExc_TypeError, "must be Boolean");
      return -1;
    }

  smbc_setOptionFallbackAfterKerberos (self->context, value == Py_True);
  return 0;
}

PyGetSetDef Context_getseters[] =
  {
    { "debug",
      (getter) Context_getDebug,
      (setter) Context_setDebug,
      "Debug level.",
      NULL },

    { "netbiosName",
      (getter) Context_getNetbiosName,
      (setter) Context_setNetbiosName,
      "Netbios name used for making connections.",
      NULL },

    { "workgroup",
      (getter) Context_getWorkgroup,
      (setter) Context_setWorkgroup,
      "Workgroup used for making connections.",
      NULL },

    { "timeout",
      (getter) Context_getTimeout,
      (setter) Context_setTimeout,
      "Get the timeout used for waiting on connections and response data(in milliseconds)",
      NULL },

    { "functionAuthData",
      (getter) NULL,
      (setter) Context_setFunctionAuthData,
      "Function for obtaining authentication data.",
      NULL },

    { "optionDebugToStderr",
      (getter) Context_getOptionDebugToStderr,
      (setter) Context_setOptionDebugToStderr,
      "Whether to log to standard error instead of standard output.",
      NULL },

    { "optionNoAutoAnonymousLogin",
      (getter) Context_getOptionNoAutoAnonymousLogin,
      (setter) Context_setOptionNoAutoAnonymousLogin,
      "Whether to automatically select anonymous login.",
      NULL },

    { "optionUseKerberos",
      (getter) Context_getOptionUseKerberos,
      (setter) Context_setOptionUseKerberos,
      "Whether to enable use of Kerberos.",
      NULL },

    { "optionFallbackAfterKerberos",
      (getter) Context_getOptionFallbackAfterKerberos,
      (setter) Context_setOptionFallbackAfterKerberos,
      "Whether to fallback after Kerberos.",
      NULL },

    { NULL }
  };

PyMethodDef Context_methods[] =
  {
    { "set_credentials_with_fallback",
      (PyCFunction) Context_set_credentials_with_fallback, METH_VARARGS,
      "set_credentials_with_fallback(workgroup, user, password)\n\n"
      "@type workgroup: string\n"
      "@param workgroup: Workgroup of user\n"
      "@type user: string\n"
      "@param user: Username of user\n"
      "@type password: string\n"
      "@param password: Password of user\n" },

    { "opendir",
      (PyCFunction) Context_opendir, METH_VARARGS,
      "opendir(uri) -> Dir\n\n"
      "@type uri: string\n"
      "@param uri: URI to opendir\n"
      "@return: a L{smbc.Dir} object for the URI" },

    { "open",
      (PyCFunction) Context_open, METH_VARARGS,
      "open(uri) -> File\n\n"
      "@type uri: string\n"
      "@param uri: URI to open\n"
      "@return: a L{smbc.File} object for the URI" },

    { "creat",
      (PyCFunction) Context_creat, METH_VARARGS,
      "creat(uri) -> File\n\n"
      "@type uri: string\n"
      "@param uri: URI to creat\n"
      "@return: a L{smbc.File} object for the URI" },

    { "unlink",
      (PyCFunction) Context_unlink, METH_VARARGS,
      "unlink(uri) -> int\n\n"
      "@type uri: string\n"
      "@param uri: URI to unlink\n"
      "@return: 0 on success, < 0 on error" },

    { "rename",
      (PyCFunction) Context_rename, METH_VARARGS,
      "rename(ouri, nuri) -> int\n\n"
      "@type ouri: string\n"
      "@param ouri: The original smb uri\n"
      "@type nuri: string\n"
      "@param nuri: The new smb uri\n"
      "@return: 0 on success, < 0 on error" },

    { "mkdir",
      (PyCFunction) Context_mkdir, METH_VARARGS,
      "mkdir(uri, mode) -> int\n\n"
      "@type uri: string\n"
      "@param uri: URI to mkdir\n"
      "@param mode: Specifies the permissions to use.\n"
      "@return: 0 on success, < 0 on error" },

    { "rmdir",
      (PyCFunction) Context_rmdir, METH_VARARGS,
      "rmdir(uri) -> int\n\n"
      "@type uri: string\n"
      "@param uri: URI to rmdir\n"
      "@return: 0 on success, < 0 on error" },

    { "stat",
      (PyCFunction) Context_stat, METH_VARARGS,
      "stat(uri) -> tuple\n\n"
      "@type uri: string\n"
      "@param uri: URI to get stat information\n"
      "@return: stat information" },

    { "chmod",
      (PyCFunction) Context_chmod, METH_VARARGS,
      "chmod(uri, mode) -> int\n\n"
      "@type uri: string\n"
      "@param uri: URI to chmod\n"
      "@type mode: int\n"
      "@param mode: permissions to set\n"
      "@return: 0 on success, < 0 on error" },

	{ "getxattr",
      (PyCFunction) Context_getxattr, METH_VARARGS,
      "getxattr(uri, the_acl) -> int\n\n"
      "@type uri: string\n"
      "@param uri: URI to scan\n"
      "@type name: string\n"
      "@param name: the acl to get with the following syntax\n"
      "\n"
      "                      system.nt_sec_desc.<attribute name>\n"
"                     system.nt_sec_desc.*\n"
"                     system.nt_sec_desc.*+\n"
"                     \n"
"                  where <attribute name> is one of:\n"
"                  \n"
"                     revision\n"
"                     owner\n"
"                     owner+\n"
"                     group\n"
"                     group+\n"
"                     acl:<name or sid>\n"
"                     acl+:<name or sid>\n"
"                     \n"
"                  In the forms \"system.nt_sec_desc.*\" and\n"
"                  \"system.nt_sec_desc.*+\", the asterisk and plus signs are\n"
"                  literal, i.e. the string is provided exactly as shown, and\n"
"                  the value parameter will return a complete security\n"
"                  descriptor with name:value pairs separated by tabs,\n"
"                  commas, or newlines (not spaces!).\n"
"\n"
"                  The plus sign ('+') indicates that SIDs should be mapped\n"
"                  to names.  Without the plus sign, SIDs are not mapped;\n"
"                  rather they are simply converted to a string format.\n"
      "@return: a string representing the actual extended attributes of the uri" },
      
       { "setxattr",
      (PyCFunction) Context_setxattr, METH_VARARGS,
      "setxattr(uri, the_acl) -> int\n\n"
      "@type uri: string\n"
      "@param uri: URI to modify\n"
      "@type name: string\n"
      "@param name: the acl to set with the following syntax\n"
      "\n"
      "                      system.nt_sec_desc.<attribute name>\n"
"                     system.nt_sec_desc.*\n"
"                     system.nt_sec_desc.*+\n"
"                     \n"
"                  where <attribute name> is one of:\n"
"                  \n"
"                     revision\n"
"                     owner\n"
"                     owner+\n"
"                     group\n"
"                     group+\n"
"                     acl:<name or sid>\n"
"                     acl+:<name or sid>\n"
"                     \n"
"                  In the forms \"system.nt_sec_desc.*\" and\n"
"                  \"system.nt_sec_desc.*+\", the asterisk and plus signs are\n"
"                  literal, i.e. the string is provided exactly as shown, and\n"
"                  the value parameter will return a complete security\n"
"                  descriptor with name:value pairs separated by tabs,\n"
"                  commas, or newlines (not spaces!).\n"
"\n"
"                  The plus sign ('+') indicates that SIDs should be mapped\n"
"                  to names.  Without the plus sign, SIDs are not mapped;\n"
"                  rather they are simply converted to a string format.\n"
      "@type	string\n"
      "@param value - a string representing the acl\n"
      "@type	int\n"
      "@param flags - XATTR_FLAG_CREATE or XATTR_FLAG_REPLACE\n"
      "@return: 0 on success" },
    { NULL } /* Sentinel */
  };
#if PY_MAJOR_VERSION >= 3
  PyTypeObject smbc_ContextType =
    {
      PyVarObject_HEAD_INIT(NULL, 0)
      "smbc.Context",            /*tp_name*/
      sizeof(Context),           /*tp_basicsize*/
      0,                         /*tp_itemsize*/
      (destructor)Context_dealloc, /*tp_dealloc*/
      0,                         /*tp_print*/
      0,                         /*tp_getattr*/
      0,                         /*tp_setattr*/
      0,                         /*tp_reserved*/
      0,                         /*tp_repr*/
      0,                         /*tp_as_number*/
      0,                         /*tp_as_sequence*/
      0,                         /*tp_as_mapping*/
      0,                         /*tp_hash */
      0,                         /*tp_call*/
      0,                         /*tp_str*/
      0,                         /*tp_getattro*/
      0,                         /*tp_setattro*/
      0,                         /*tp_as_buffer*/
      Py_TPFLAGS_DEFAULT,        /*tp_flags*/
      "SMBC context\n"
      "============\n\n"

      "  A context for libsmbclient calls.\n\n"
      "Optional parameters are:\n\n"
      "auth_fn: a function for collecting authentication details from\n"
      "the user. This is called whenever authentication details are needed.\n"
      "The parameters it will be given are all strings: server, share,\n"
      "workgroup, username, and password (these last two can be ignored).\n"
      "The function should return a tuple of strings: workgroup, username,\n"
      "and password.\n\n"
      "debug: an integer representing the debug level to use.\n"
      "",                        /* tp_doc */
      0,                         /* tp_traverse */
      0,                         /* tp_clear */
      0,                         /* tp_richcompare */
      0,                         /* tp_weaklistoffset */
      0,                         /* tp_iter */
      0,                         /* tp_iternext */
      Context_methods,           /* tp_methods */
      0,                         /* tp_members */
      Context_getseters,         /* tp_getset */
      0,                         /* tp_base */
      0,                         /* tp_dict */
      0,                         /* tp_descr_get */
      0,                         /* tp_descr_set */
      0,                         /* tp_dictoffset */
      (initproc)Context_init,    /* tp_init */
      0,                         /* tp_alloc */
      Context_new,               /* tp_new */
    };
#else
  PyTypeObject smbc_ContextType =
    {
      PyObject_HEAD_INIT(NULL)
      0,                         /*ob_size*/
      "smbc.Context",            /*tp_name*/
      sizeof(Context),           /*tp_basicsize*/
      0,                         /*tp_itemsize*/
      (destructor)Context_dealloc, /*tp_dealloc*/
      0,                         /*tp_print*/
      0,                         /*tp_getattr*/
      0,                         /*tp_setattr*/
      0,                         /*tp_compare*/
      0,                         /*tp_repr*/
      0,                         /*tp_as_number*/
      0,                         /*tp_as_sequence*/
      0,                         /*tp_as_mapping*/
      0,                         /*tp_hash */
      0,                         /*tp_call*/
      0,                         /*tp_str*/
      0,                         /*tp_getattro*/
      0,                         /*tp_setattro*/
      0,                         /*tp_as_buffer*/
      Py_TPFLAGS_DEFAULT,        /*tp_flags*/
      "SMBC context\n"
      "============\n\n"

      "  A context for libsmbclient calls.\n\n"
      "Optional parameters are:\n\n"
      "auth_fn: a function for collecting authentication details from\n"
      "the user. This is called whenever authentication details are needed.\n"
      "The parameters it will be given are all strings: server, share,\n"
      "workgroup, username, and password (these last two can be ignored).\n"
      "The function should return a tuple of strings: workgroup, username,\n"
      "and password.\n\n"
      "debug: an integer representing the debug level to use.\n"
      "",                        /* tp_doc */
      0,                         /* tp_traverse */
      0,                         /* tp_clear */
      0,                         /* tp_richcompare */
      0,                         /* tp_weaklistoffset */
      0,                         /* tp_iter */
      0,                         /* tp_iternext */
      Context_methods,           /* tp_methods */
      0,                         /* tp_members */
      Context_getseters,         /* tp_getset */
      0,                         /* tp_base */
      0,                         /* tp_dict */
      0,                         /* tp_descr_get */
      0,                         /* tp_descr_set */
      0,                         /* tp_dictoffset */
      (initproc)Context_init,    /* tp_init */
      0,                         /* tp_alloc */
      Context_new,               /* tp_new */
    };
#endif