-
Notifications
You must be signed in to change notification settings - Fork 22
/
smbclient.c
1963 lines (1761 loc) · 62 KB
/
smbclient.c
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
/* ------------------------------------------------------------------
* This file is part of libsmbclient-php: Samba bindings for PHP.
* Libsmbclient-php is licensed under the BSD 2-clause license:
* ------------------------------------------------------------------
*
* Copyright (c) 2003, Matthew Sachs
* 2009 - 2014, Eduardo Bacchi Kienetz
* 2013 - 2015, Alfred Klomp
* 2015, Remi Collet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* ------------------------------------------------------------------
*/
#define IS_EXT_MODULE
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_smbclient.h"
ZEND_DECLARE_MODULE_GLOBALS(smbclient)
#define PHP_SMBCLIENT_STATE_NAME "smbclient state"
#define PHP_SMBCLIENT_FILE_NAME "smbclient file"
static int le_smbclient_state;
static int le_smbclient_file;
#if PHP_MAJOR_VERSION >= 7
typedef size_t strsize_t;
#else
typedef int strsize_t;
typedef long zend_long;
#endif
enum {
SMBCLIENT_OPT_OPEN_SHAREMODE = 1,
SMBCLIENT_OPT_ENCRYPT_LEVEL = 2,
SMBCLIENT_OPT_CASE_SENSITIVE = 3,
SMBCLIENT_OPT_BROWSE_MAX_LMB_COUNT = 4,
SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES = 5,
/* Ignore OneSharePerServer, not relevant to us. */
SMBCLIENT_OPT_USE_KERBEROS = 6,
SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS = 7,
/* Reverse the sense of this option, the original
* is the confusing "NoAutoAnonymousLogin": */
SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN = 8,
SMBCLIENT_OPT_USE_CCACHE = 9,
SMBCLIENT_OPT_USE_NT_HASH = 10,
SMBCLIENT_OPT_NETBIOS_NAME = 11,
SMBCLIENT_OPT_WORKGROUP = 12,
SMBCLIENT_OPT_USER = 13,
SMBCLIENT_OPT_PORT = 14,
SMBCLIENT_OPT_TIMEOUT = 15,
}
php_smbclient_options;
static char *
find_char (char *start, char *last, char q)
{
char *c;
for (c = start; c <= last; c++) {
if (*c == q) {
return c;
}
}
return NULL;
}
static char *
find_second_char (char *start, char *last, char q)
{
char *c;
if ((c = find_char(start, last, q)) == NULL) {
return NULL;
}
return find_char(c + 1, last, q);
}
static void
astfill (char *start, char *last)
{
char *c;
for (c = start; c <= last; c++) {
*c = '*';
}
}
static void
hide_password (char *url, int len)
{
/* URL should have the form:
* smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
* Replace everything after the second colon and before the next @
* with asterisks. */
char *last = (url + len) - 1;
char *second_colon;
char *slash;
char *at_sign;
if (len <= 0) {
return;
}
if ((second_colon = find_second_char(url, last, ':')) == NULL) {
return;
}
if ((slash = find_char(second_colon + 1, last, '/')) == NULL) {
slash = last + 1;
}
if ((at_sign = find_char(second_colon + 1, last, '@')) == NULL) {
astfill(second_colon + 1, slash - 1);
return;
}
if (at_sign > slash) {
at_sign = slash;
}
astfill(second_colon + 1, at_sign - 1);
}
#if PHP_VERSION_ID < 80000
#include "smbclient_legacy_arginfo.h"
#else
#include "smbclient_arginfo.h"
#endif
zend_module_entry smbclient_module_entry =
{ STANDARD_MODULE_HEADER
, "smbclient" /* name */
, ext_functions /* functions */
, PHP_MINIT(smbclient) /* module_startup_func */
, PHP_MSHUTDOWN(smbclient) /* module_shutdown_func */
, PHP_RINIT(smbclient) /* request_startup_func */
, PHP_RSHUTDOWN(smbclient) /* request_shutdown_func */
, PHP_MINFO(smbclient) /* info_func */
, PHP_SMBCLIENT_VERSION /* version */
, PHP_MODULE_GLOBALS(smbclient)
, PHP_GINIT(smbclient) /* globals ctor */
, NULL /* globals dtor */
, NULL /* post_deactivate_func */
, STANDARD_MODULE_PROPERTIES_EX
} ;
zend_module_entry old_module_entry = {
STANDARD_MODULE_HEADER,
"libsmbclient",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
PHP_SMBCLIENT_VERSION,
STANDARD_MODULE_PROPERTIES,
};
#ifdef COMPILE_DL_SMBCLIENT
ZEND_GET_MODULE(smbclient)
#endif
static void
auth_copy (char *dst, char *src, size_t srclen, size_t maxlen)
{
if (dst == NULL || maxlen == 0) {
return;
}
if (src == NULL || srclen == 0) {
*dst = '\0';
return;
}
if (srclen < maxlen) {
memcpy(dst, src, srclen);
dst[srclen] = '\0';
return;
}
memcpy(dst, src, maxlen - 1);
dst[maxlen - 1] = '\0';
}
static void
smbclient_auth_func (SMBCCTX *ctx, const char *server, const char *share, char *wrkg, int wrkglen, char *user, int userlen, char *pass, int passlen)
{
/* Given context, server and share, return workgroup, username and password.
* String lengths are the max allowable lengths. */
php_smbclient_state *state;
if (ctx == NULL || (state = smbc_getOptionUserData(ctx)) == NULL) {
return;
}
auth_copy(wrkg, state->wrkg, (size_t)state->wrkglen, (size_t)wrkglen);
auth_copy(user, state->user, (size_t)state->userlen, (size_t)userlen);
auth_copy(pass, state->pass, (size_t)state->passlen, (size_t)passlen);
}
void
php_smbclient_state_free (php_smbclient_state *state TSRMLS_DC)
{
/* TODO: if smbc_free_context() returns 0, PHP will leak the handle: */
if (state->ctx != NULL && smbc_free_context(state->ctx, 1) != 0) {
switch (errno) {
case EBUSY: php_error(E_WARNING, "Couldn't destroy SMB context: connection in use"); break;
case EBADF: php_error(E_WARNING, "Couldn't destroy SMB context: invalid handle"); break;
default: php_error(E_WARNING, "Couldn't destroy SMB context: unknown error (%d)", errno); break;
}
}
if (state->wrkg != NULL) {
memset(state->wrkg, 0, state->wrkglen);
efree(state->wrkg);
}
if (state->user != NULL) {
memset(state->user, 0, state->userlen);
efree(state->user);
}
if (state->pass != NULL) {
memset(state->pass, 0, state->passlen);
efree(state->pass);
}
efree(state);
}
static inline void
smbclient_state_dtor (
#if PHP_MAJOR_VERSION >= 7
zend_resource *rsrc
#else
zend_rsrc_list_entry *rsrc
#endif
TSRMLS_DC)
{
php_smbclient_state_free((php_smbclient_state *)rsrc->ptr TSRMLS_CC);
}
static void
smbclient_file_dtor (
#if PHP_VERSION_ID >= 70000
zend_resource *rsrc
#else
zend_rsrc_list_entry *rsrc
#endif
TSRMLS_DC)
{
/* Because libsmbclient's file/dir close functions require a pointer to
* a context which we don't have, we cannot reliably destroy a file
* resource. One way of obtaining the context pointer could be to save
* it in a structure along with the file context, but the pointer could
* grow stale or otherwise spark a race condition. So it seems that the
* best we can do is nothing. The PHP programmer can either manually
* free the file resources, or wait for them to be cleaned up when the
* associated context is destroyed. */
}
PHP_GINIT_FUNCTION(smbclient)
{
smbclient_globals->pool_first = NULL;
}
PHP_MINIT_FUNCTION(smbclient)
{
/* Constants for smbclient_setxattr: */
REGISTER_LONG_CONSTANT("SMBCLIENT_XATTR_CREATE", SMBC_XATTR_FLAG_CREATE, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_XATTR_REPLACE", SMBC_XATTR_FLAG_REPLACE, CONST_PERSISTENT | CONST_CS);
/* Constants for getting/setting options: */
#define SMBCLIENT_CONST(x) REGISTER_LONG_CONSTANT(#x, x, CONST_PERSISTENT | CONST_CS);
SMBCLIENT_CONST(SMBCLIENT_OPT_OPEN_SHAREMODE);
SMBCLIENT_CONST(SMBCLIENT_OPT_ENCRYPT_LEVEL);
SMBCLIENT_CONST(SMBCLIENT_OPT_CASE_SENSITIVE);
SMBCLIENT_CONST(SMBCLIENT_OPT_BROWSE_MAX_LMB_COUNT);
SMBCLIENT_CONST(SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES);
SMBCLIENT_CONST(SMBCLIENT_OPT_USE_KERBEROS);
SMBCLIENT_CONST(SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS);
SMBCLIENT_CONST(SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN);
SMBCLIENT_CONST(SMBCLIENT_OPT_USE_CCACHE);
SMBCLIENT_CONST(SMBCLIENT_OPT_USE_NT_HASH);
SMBCLIENT_CONST(SMBCLIENT_OPT_NETBIOS_NAME);
SMBCLIENT_CONST(SMBCLIENT_OPT_WORKGROUP);
SMBCLIENT_CONST(SMBCLIENT_OPT_USER);
SMBCLIENT_CONST(SMBCLIENT_OPT_PORT);
SMBCLIENT_CONST(SMBCLIENT_OPT_TIMEOUT);
#undef SMBCLIENT_CONST
/* Constants for use with SMBCLIENT_OPT_OPENSHAREMODE: */
REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_DOS", SMBC_SHAREMODE_DENY_DOS, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_ALL", SMBC_SHAREMODE_DENY_ALL, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_WRITE", SMBC_SHAREMODE_DENY_WRITE, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_READ", SMBC_SHAREMODE_DENY_READ, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_NONE", SMBC_SHAREMODE_DENY_NONE, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_FCB", SMBC_SHAREMODE_DENY_FCB, CONST_PERSISTENT | CONST_CS);
/* Constants for use with SMBCLIENT_OPT_ENCRYPTLEVEL: */
REGISTER_LONG_CONSTANT("SMBCLIENT_ENCRYPTLEVEL_NONE", SMBC_ENCRYPTLEVEL_NONE, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_ENCRYPTLEVEL_REQUEST", SMBC_ENCRYPTLEVEL_REQUEST, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_ENCRYPTLEVEL_REQUIRE", SMBC_ENCRYPTLEVEL_REQUIRE, CONST_PERSISTENT | CONST_CS);
/* Constants for the VFS functions: */
REGISTER_LONG_CONSTANT("SMBCLIENT_VFS_RDONLY", SMBC_VFS_FEATURE_RDONLY, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_VFS_DFS", SMBC_VFS_FEATURE_DFS, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_VFS_CASE_INSENSITIVE", SMBC_VFS_FEATURE_CASE_INSENSITIVE, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("SMBCLIENT_VFS_NO_UNIXCIFS", SMBC_VFS_FEATURE_NO_UNIXCIFS, CONST_PERSISTENT | CONST_CS);
le_smbclient_state = zend_register_list_destructors_ex(smbclient_state_dtor, NULL, PHP_SMBCLIENT_STATE_NAME, module_number);
le_smbclient_file = zend_register_list_destructors_ex(smbclient_file_dtor, NULL, PHP_SMBCLIENT_FILE_NAME, module_number);
php_register_url_stream_wrapper("smb", &php_stream_smb_wrapper TSRMLS_CC);
/* register with old name for code using extension_loaded(libsmbclient) */
zend_register_internal_module(&old_module_entry TSRMLS_CC);
return SUCCESS;
}
PHP_RINIT_FUNCTION(smbclient)
{
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(smbclient)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(smbclient)
{
php_smb_pool_cleanup(TSRMLS_C);
return SUCCESS;
}
PHP_MINFO_FUNCTION(smbclient)
{
php_info_print_table_start();
php_info_print_table_row(2, "smbclient Support", "enabled");
php_info_print_table_row(2, "smbclient extension Version", PHP_SMBCLIENT_VERSION);
php_info_print_table_row(2, "libsmbclient library Version", smbc_version());
php_info_print_table_end();
}
static int
ctx_init_getauth (zval *z, char **dest, int *destlen, char *varname)
{
if (*dest != NULL) {
efree(*dest);
*dest = NULL;
}
*destlen = 0;
if (z == NULL) {
return 1;
}
switch (Z_TYPE_P(z))
{
case IS_NULL:
return 1;
#if PHP_MAJOR_VERSION >= 7
case IS_TRUE:
php_error(E_WARNING, "invalid value for %s", varname);
return 0;
case IS_FALSE:
return 1;
#else
case IS_BOOL:
if (Z_LVAL_P(z) == 1) {
php_error(E_WARNING, "invalid value for %s", varname);
return 0;
}
return 1;
#endif
case IS_STRING:
*destlen = Z_STRLEN_P(z);
*dest = estrndup(Z_STRVAL_P(z), *destlen);
return 1;
default:
php_error(E_WARNING, "invalid datatype for %s", varname);
return 0;
}
}
php_smbclient_state *
php_smbclient_state_new (php_stream_context *context, int init TSRMLS_DC)
{
php_smbclient_state *state;
SMBCCTX *ctx;
if ((ctx = smbc_new_context()) == NULL) {
switch (errno) {
case ENOMEM: php_error(E_WARNING, "Couldn't create smbclient state: insufficient memory"); break;
default: php_error(E_WARNING, "Couldn't create smbclient state: unknown error (%d)", errno); break;
};
return NULL;
}
state = emalloc(sizeof(php_smbclient_state));
state->ctx = ctx;
state->wrkg = NULL;
state->user = NULL;
state->pass = NULL;
state->wrkglen = 0;
state->userlen = 0;
state->passlen = 0;
state->err = 0;
smbc_setFunctionAuthDataWithContext(state->ctx, smbclient_auth_func);
/* Must also save a pointer to the state object inside the context, to
* find the state from the context in the auth function: */
smbc_setOptionUserData(ctx, (void *)state);
/* Force full, modern timenames when getting xattrs: */
smbc_setOptionFullTimeNames(state->ctx, 1);
if (context) {
#if PHP_MAJOR_VERSION >= 7
zval *tmpzval;
if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "workgroup"))) {
if (ctx_init_getauth(tmpzval, &state->wrkg, &state->wrkglen, "workgroup") == 0) {
php_smbclient_state_free(state);
return NULL;
}
}
if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "username"))) {
if (ctx_init_getauth(tmpzval, &state->user, &state->userlen, "username") == 0) {
php_smbclient_state_free(state);
return NULL;
}
}
if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "password"))) {
if (ctx_init_getauth(tmpzval, &state->pass, &state->passlen, "password") == 0) {
php_smbclient_state_free(state);
return NULL;
}
}
#if HAVE_SMBC_SETOPTIONPROTOCOLS
if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "minproto"))) {
smbc_setOptionProtocols(state->ctx, Z_STRVAL_P(tmpzval), NULL);
}
if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "maxproto"))) {
smbc_setOptionProtocols(state->ctx, NULL, Z_STRVAL_P(tmpzval));
}
#endif
#else
zval **tmpzval;
if (php_stream_context_get_option(context, "smb", "workgroup", &tmpzval) == SUCCESS) {
if (ctx_init_getauth(*tmpzval, &state->wrkg, &state->wrkglen, "workgroup") == 0) {
php_smbclient_state_free(state TSRMLS_CC);
return NULL;
}
}
if (php_stream_context_get_option(context, "smb", "username", &tmpzval) == SUCCESS) {
if (ctx_init_getauth(*tmpzval, &state->user, &state->userlen, "username") == 0) {
php_smbclient_state_free(state TSRMLS_CC);
return NULL;
}
}
if (php_stream_context_get_option(context, "smb", "password", &tmpzval) == SUCCESS) {
if (ctx_init_getauth(*tmpzval, &state->pass, &state->passlen, "password") == 0) {
php_smbclient_state_free(state TSRMLS_CC);
return NULL;
}
}
#if HAVE_SMBC_SETOPTIONPROTOCOLS
if (php_stream_context_get_option(context, "smb", "minproto", &tmpzval) == SUCCESS) {
smbc_setOptionProtocols(state->ctx, Z_STRVAL_PP(tmpzval), NULL);
}
if (php_stream_context_get_option(context, "smb", "maxproto", &tmpzval) == SUCCESS) {
smbc_setOptionProtocols(state->ctx, NULL, Z_STRVAL_PP(tmpzval));
}
#endif
#endif
}
if (init) {
if (php_smbclient_state_init(state TSRMLS_CC)) {
php_smbclient_state_free(state TSRMLS_CC);
return NULL;
}
}
return state;
}
PHP_FUNCTION(smbclient_state_new)
{
php_smbclient_state *state;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_FALSE;
}
if ((state = php_smbclient_state_new(NULL, 0 TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION >= 7
ZVAL_RES(return_value, zend_register_resource(state, le_smbclient_state));
#else
ZEND_REGISTER_RESOURCE(return_value, state, le_smbclient_state);
#endif
}
PHP_FUNCTION(smbclient_version)
{
if (zend_parse_parameters_none() == FAILURE) {
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION >= 7
RETURN_STRING(PHP_SMBCLIENT_VERSION);
#else
RETURN_STRING(PHP_SMBCLIENT_VERSION, 1);
#endif
}
PHP_FUNCTION(smbclient_library_version)
{
if (zend_parse_parameters_none() == FAILURE) {
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION >= 7
RETURN_STRING(smbc_version());
#else
RETURN_STRING(smbc_version(), 1);
#endif
}
int
php_smbclient_state_init (php_smbclient_state *state TSRMLS_DC)
{
SMBCCTX *ctx;
if ((ctx = smbc_init_context(state->ctx)) != NULL) {
state->ctx = ctx;
return 0;
}
switch (state->err = errno) {
case EBADF: php_error(E_WARNING, "Couldn't init SMB context: null context given"); break;
case ENOMEM: php_error(E_WARNING, "Couldn't init SMB context: insufficient memory"); break;
case ENOENT: php_error(E_WARNING, "Couldn't init SMB context: cannot load smb.conf"); break;
default: php_error(E_WARNING, "Couldn't init SMB context: unknown error (%d)", errno); break;
}
return 1;
}
#if PHP_MAJOR_VERSION >= 7
#define SMB_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \
if ((_state = (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le)) == NULL) { \
RETURN_FALSE; \
}
#else
#define SMB_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \
ZEND_FETCH_RESOURCE(_state, _type, _zval, -1, _name, _le);
#endif
#define STATE_FROM_ZSTATE \
SMB_FETCH_RESOURCE(state, php_smbclient_state *, &zstate, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state); \
if (state == NULL || state->ctx == NULL) { \
php_error(E_WARNING, PHP_SMBCLIENT_STATE_NAME " not found"); \
RETURN_FALSE; \
}
#define FILE_FROM_ZFILE \
SMB_FETCH_RESOURCE(file, SMBCFILE *, &zfile, PHP_SMBCLIENT_FILE_NAME, le_smbclient_file); \
if (file == NULL) { \
php_error(E_WARNING, PHP_SMBCLIENT_FILE_NAME " not found"); \
RETURN_FALSE; \
}
PHP_FUNCTION(smbclient_state_init)
{
zval *zstate;
zval *zwrkg = NULL;
zval *zuser = NULL;
zval *zpass = NULL;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|zzz", &zstate, &zwrkg, &zuser, &zpass) != SUCCESS) {
RETURN_FALSE;
}
SMB_FETCH_RESOURCE(state, php_smbclient_state *, &zstate, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
if (state->ctx == NULL) {
php_error(E_WARNING, "Couldn't init SMB context: context is null");
RETURN_FALSE;
}
if (ctx_init_getauth(zwrkg, &state->wrkg, &state->wrkglen, "workgroup") == 0) {
RETURN_FALSE;
}
if (ctx_init_getauth(zuser, &state->user, &state->userlen, "username") == 0) {
RETURN_FALSE;
}
if (ctx_init_getauth(zpass, &state->pass, &state->passlen, "password") == 0) {
RETURN_FALSE;
}
if (php_smbclient_state_init(state TSRMLS_CC)) {
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(smbclient_state_errno)
{
zval *zstate;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstate) != SUCCESS) {
RETURN_LONG(0);
}
SMB_FETCH_RESOURCE(state, php_smbclient_state *, &zstate, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
RETURN_LONG(state->err);
}
PHP_FUNCTION(smbclient_state_free)
{
zval *zstate;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstate) != SUCCESS) {
RETURN_FALSE;
}
SMB_FETCH_RESOURCE(state, php_smbclient_state *, &zstate, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
if (state->ctx == NULL) {
#if PHP_MAJOR_VERSION >= 7
zend_list_close(Z_RES_P(zstate));
#else
zend_list_delete(Z_LVAL_P(zstate));
#endif
RETURN_TRUE;
}
if (smbc_free_context(state->ctx, 1) == 0) {
state->ctx = NULL;
#if PHP_MAJOR_VERSION >= 7
zend_list_close(Z_RES_P(zstate));
#else
zend_list_delete(Z_LVAL_P(zstate));
#endif
RETURN_TRUE;
}
switch (state->err = errno) {
case EBUSY: php_error(E_WARNING, "Couldn't destroy smbclient state: connection in use"); break;
case EBADF: php_error(E_WARNING, "Couldn't destroy smbclient state: invalid handle"); break;
default: php_error(E_WARNING, "Couldn't destroy smbclient state: unknown error (%d)", errno); break;
}
RETURN_FALSE;
}
PHP_FUNCTION(smbclient_opendir)
{
char *path;
strsize_t path_len;
zval *zstate;
SMBCFILE *dir;
smbc_opendir_fn smbc_opendir;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &path, &path_len) == FAILURE) {
return;
}
STATE_FROM_ZSTATE;
if ((smbc_opendir = smbc_getFunctionOpendir(state->ctx)) == NULL) {
RETURN_FALSE;
}
if ((dir = smbc_opendir(state->ctx, path)) != NULL) {
#if PHP_MAJOR_VERSION >= 7
ZVAL_RES(return_value, zend_register_resource(dir, le_smbclient_file));
#else
ZEND_REGISTER_RESOURCE(return_value, dir, le_smbclient_file);
#endif
return;
}
hide_password(path, path_len);
switch (state->err = errno) {
case EACCES: php_error(E_WARNING, "Couldn't open SMB directory %s: Permission denied", path); break;
case EINVAL: php_error(E_WARNING, "Couldn't open SMB directory %s: Invalid URL", path); break;
case ENOENT: php_error(E_WARNING, "Couldn't open SMB directory %s: Path does not exist", path); break;
case ENOMEM: php_error(E_WARNING, "Couldn't open SMB directory %s: Insufficient memory", path); break;
case ENOTDIR: php_error(E_WARNING, "Couldn't open SMB directory %s: Not a directory", path); break;
case EPERM: php_error(E_WARNING, "Couldn't open SMB directory %s: Workgroup not found", path); break;
case ENODEV: php_error(E_WARNING, "Couldn't open SMB directory %s: Workgroup or server not found", path); break;
default: php_error(E_WARNING, "Couldn't open SMB directory %s: unknown error (%d)", path, errno); break;
}
RETURN_FALSE;
}
static char *
type_to_string (unsigned int type)
{
switch (type) {
case SMBC_WORKGROUP: return "workgroup";
case SMBC_SERVER: return "server";
case SMBC_FILE_SHARE: return "file share";
case SMBC_PRINTER_SHARE: return "printer share";
case SMBC_COMMS_SHARE: return "communication share";
case SMBC_IPC_SHARE: return "IPC share";
case SMBC_DIR: return "directory";
case SMBC_FILE: return "file";
case SMBC_LINK: return "link";
}
return "unknown";
}
PHP_FUNCTION(smbclient_readdir)
{
struct smbc_dirent *dirent;
zval *zstate;
zval *zfile;
SMBCFILE *file;
smbc_readdir_fn smbc_readdir;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zstate, &zfile) == FAILURE) {
return;
}
STATE_FROM_ZSTATE;
FILE_FROM_ZFILE;
if ((smbc_readdir = smbc_getFunctionReaddir(state->ctx)) == NULL) {
RETURN_FALSE;
}
errno = 0;
if ((dirent = smbc_readdir(state->ctx, file)) == NULL) {
switch (state->err = errno) {
case 0: RETURN_FALSE;
case EBADF: php_error(E_WARNING, "Couldn't read " PHP_SMBCLIENT_FILE_NAME ": Not a directory resource"); break;
case EINVAL: php_error(E_WARNING, "Couldn't read " PHP_SMBCLIENT_FILE_NAME ": State resource not initialized"); break;
default: php_error(E_WARNING, "Couldn't read " PHP_SMBCLIENT_FILE_NAME ": unknown error (%d)", errno); break;
}
RETURN_FALSE;
}
array_init(return_value);
#if PHP_MAJOR_VERSION >= 7
add_assoc_string(return_value, "type", type_to_string(dirent->smbc_type));
add_assoc_stringl(return_value, "comment", dirent->comment, dirent->commentlen);
add_assoc_stringl(return_value, "name", dirent->name, dirent->namelen);
#else
add_assoc_string(return_value, "type", type_to_string(dirent->smbc_type), 1);
add_assoc_stringl(return_value, "comment", dirent->comment, dirent->commentlen, 1);
add_assoc_stringl(return_value, "name", dirent->name, dirent->namelen, 1);
#endif
}
PHP_FUNCTION(smbclient_closedir)
{
zval *zstate;
zval *zfile;
SMBCFILE *file;
smbc_closedir_fn smbc_closedir;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zstate, &zfile) == FAILURE) {
return;
}
STATE_FROM_ZSTATE;
FILE_FROM_ZFILE;
if ((smbc_closedir = smbc_getFunctionClosedir(state->ctx)) == NULL) {
RETURN_FALSE;
}
if (smbc_closedir(state->ctx, file) == 0) {
#if PHP_MAJOR_VERSION >= 7
zend_list_close(Z_RES_P(zfile));
#else
zend_list_delete(Z_LVAL_P(zfile));
#endif
RETURN_TRUE;
}
switch (state->err = errno) {
case EBADF: php_error(E_WARNING, "Couldn't close " PHP_SMBCLIENT_FILE_NAME ": Not a directory resource"); break;
default: php_error(E_WARNING, "Couldn't close " PHP_SMBCLIENT_FILE_NAME ": unknown error (%d)", errno); break;
}
RETURN_FALSE;
}
PHP_FUNCTION(smbclient_rename)
{
char *ourl, *nurl;
strsize_t ourl_len, nurl_len;
zval *zstate_old;
zval *zstate_new;
smbc_rename_fn smbc_rename;
php_smbclient_state *state_old;
php_smbclient_state *state_new;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsrs", &zstate_old, &ourl, &ourl_len, &zstate_new, &nurl, &nurl_len) == FAILURE) {
return;
}
SMB_FETCH_RESOURCE(state_old, php_smbclient_state *, &zstate_old, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
SMB_FETCH_RESOURCE(state_new, php_smbclient_state *, &zstate_new, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
if (state_old == NULL || state_old->ctx == NULL) {
php_error(E_WARNING, "old " PHP_SMBCLIENT_STATE_NAME " is null");
RETURN_FALSE;
}
if (state_new == NULL || state_new->ctx == NULL) {
php_error(E_WARNING, "new " PHP_SMBCLIENT_STATE_NAME " is null");
RETURN_FALSE;
}
if ((smbc_rename = smbc_getFunctionRename(state_old->ctx)) == NULL) {
RETURN_FALSE;
}
if (smbc_rename(state_old->ctx, ourl, state_new->ctx, nurl) == 0) {
RETURN_TRUE;
}
hide_password(ourl, ourl_len);
switch (state_old->err = errno) {
case EISDIR: php_error(E_WARNING, "Couldn't rename SMB directory %s: existing url is not a directory", ourl); break;
case EACCES: php_error(E_WARNING, "Couldn't open SMB directory %s: Permission denied", ourl); break;
case EINVAL: php_error(E_WARNING, "Couldn't open SMB directory %s: Invalid URL", ourl); break;
case ENOENT: php_error(E_WARNING, "Couldn't open SMB directory %s: Path does not exist", ourl); break;
case ENOMEM: php_error(E_WARNING, "Couldn't open SMB directory %s: Insufficient memory", ourl); break;
case ENOTDIR: php_error(E_WARNING, "Couldn't open SMB directory %s: Not a directory", ourl); break;
case EPERM: php_error(E_WARNING, "Couldn't open SMB directory %s: Workgroup not found", ourl); break;
case EXDEV: php_error(E_WARNING, "Couldn't open SMB directory %s: Workgroup or server not found", ourl); break;
case EEXIST: php_error(E_WARNING, "Couldn't rename SMB directory %s: new name already exists", ourl); break;
default: php_error(E_WARNING, "Couldn't open SMB directory %s: unknown error (%d)", ourl, errno); break;
}
RETURN_FALSE;
}
PHP_FUNCTION(smbclient_unlink)
{
char *url;
strsize_t url_len;
zval *zstate;
smbc_unlink_fn smbc_unlink;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &url, &url_len) == FAILURE) {
return;
}
STATE_FROM_ZSTATE;
if ((smbc_unlink = smbc_getFunctionUnlink(state->ctx)) == NULL) {
RETURN_FALSE;
}
if (smbc_unlink(state->ctx, url) == 0) {
RETURN_TRUE;
}
hide_password(url, url_len);
switch (state->err = errno) {
case EACCES: php_error(E_WARNING, "Couldn't delete %s: Permission denied", url); break;
case EINVAL: php_error(E_WARNING, "Couldn't delete %s: Invalid URL", url); break;
case ENOENT: php_error(E_WARNING, "Couldn't delete %s: Path does not exist", url); break;
case ENOMEM: php_error(E_WARNING, "Couldn't delete %s: Insufficient memory", url); break;
case EPERM: php_error(E_WARNING, "Couldn't delete %s: Workgroup not found", url); break;
case EISDIR: php_error(E_WARNING, "Couldn't delete %s: It is a Directory (use rmdir instead)", url); break;
case EBUSY: php_error(E_WARNING, "Couldn't delete %s: Device or resource busy", url); break;
default: php_error(E_WARNING, "Couldn't delete %s: unknown error (%d)", url, errno); break;
}
RETURN_FALSE;
}
PHP_FUNCTION(smbclient_mkdir)
{
char *path = NULL;
strsize_t path_len;
zend_long mode = 0777; /* Same as PHP's native mkdir() */
zval *zstate;
smbc_mkdir_fn smbc_mkdir;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zstate, &path, &path_len, &mode) == FAILURE) {
return;
}
STATE_FROM_ZSTATE;
if ((smbc_mkdir = smbc_getFunctionMkdir(state->ctx)) == NULL) {
RETURN_FALSE;
}
if (smbc_mkdir(state->ctx, path, (mode_t)mode) == 0) {
RETURN_TRUE;
}
hide_password(path, path_len);
switch (state->err = errno) {
case EACCES: php_error(E_WARNING, "Couldn't create SMB directory %s: Permission denied", path); break;
case EINVAL: php_error(E_WARNING, "Couldn't create SMB directory %s: Invalid URL", path); break;
case ENOENT: php_error(E_WARNING, "Couldn't create SMB directory %s: Path does not exist", path); break;
case ENOMEM: php_error(E_WARNING, "Couldn't create SMB directory %s: Insufficient memory", path); break;
case EEXIST: php_error(E_WARNING, "Couldn't create SMB directory %s: Directory already exists", path); break;
default: php_error(E_WARNING, "Couldn't create SMB directory %s: unknown error (%d)", path, errno); break;
}
RETURN_FALSE;
}
PHP_FUNCTION(smbclient_rmdir)
{
char *url;
strsize_t url_len;
zval *zstate;
smbc_rmdir_fn smbc_rmdir;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &url, &url_len) == FAILURE) {
return;
}
STATE_FROM_ZSTATE;
if ((smbc_rmdir = smbc_getFunctionRmdir(state->ctx)) == NULL) {
RETURN_FALSE;
}
if (smbc_rmdir(state->ctx, url) == 0) {
RETURN_TRUE;
}
hide_password(url, url_len);
switch (state->err = errno) {
case EACCES: php_error(E_WARNING, "Couldn't delete %s: Permission denied", url); break;
case EINVAL: php_error(E_WARNING, "Couldn't delete %s: Invalid URL", url); break;
case ENOENT: php_error(E_WARNING, "Couldn't delete %s: Path does not exist", url); break;
case ENOMEM: php_error(E_WARNING, "Couldn't delete %s: Insufficient memory", url); break;
case EPERM: php_error(E_WARNING, "Couldn't delete %s: Workgroup not found", url); break;
case ENOTEMPTY: php_error(E_WARNING, "Couldn't delete %s: It is not empty", url); break;
default: php_error(E_WARNING, "Couldn't delete %s: unknown error (%d)", url, errno); break;
}
RETURN_FALSE;
}
PHP_FUNCTION(smbclient_stat)
{
char *file;
struct stat statbuf;
strsize_t file_len;
zval *zstate;
smbc_stat_fn smbc_stat;
php_smbclient_state *state;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &file, &file_len) == FAILURE) {
return;
}
STATE_FROM_ZSTATE;
if ((smbc_stat = smbc_getFunctionStat(state->ctx)) == NULL) {
RETURN_FALSE;
}
if (smbc_stat(state->ctx, file, &statbuf) < 0) {
hide_password(file, file_len);
switch (state->err = errno) {
case ENOENT: php_error(E_WARNING, "Couldn't stat %s: Does not exist", file); break;
case EINVAL: php_error(E_WARNING, "Couldn't stat: null URL or smbc_init failed"); break;
case EACCES: php_error(E_WARNING, "Couldn't stat %s: Permission denied", file); break;
case ENOMEM: php_error(E_WARNING, "Couldn't stat %s: Out of memory", file); break;
case ENOTDIR: php_error(E_WARNING, "Couldn't stat %s: Not a directory", file); break;
default: php_error(E_WARNING, "Couldn't stat %s: unknown error (%d)", file, errno); break;
}
RETURN_FALSE;
}
array_init(return_value);
add_index_long(return_value, 0, statbuf.st_dev);
add_index_long(return_value, 1, statbuf.st_ino);
add_index_long(return_value, 2, statbuf.st_mode);
add_index_long(return_value, 3, statbuf.st_nlink);
add_index_long(return_value, 4, statbuf.st_uid);
add_index_long(return_value, 5, statbuf.st_gid);
add_index_long(return_value, 6, statbuf.st_rdev);
add_index_long(return_value, 7, statbuf.st_size);
add_index_long(return_value, 8, statbuf.st_atime);
add_index_long(return_value, 9, statbuf.st_mtime);
add_index_long(return_value, 10, statbuf.st_ctime);
add_index_long(return_value, 11, statbuf.st_blksize);
add_index_long(return_value, 12, statbuf.st_blocks);
add_assoc_long(return_value, "dev", statbuf.st_dev);
add_assoc_long(return_value, "ino", statbuf.st_ino);
add_assoc_long(return_value, "mode", statbuf.st_mode);
add_assoc_long(return_value, "nlink", statbuf.st_nlink);
add_assoc_long(return_value, "uid", statbuf.st_uid);
add_assoc_long(return_value, "gid", statbuf.st_gid);
add_assoc_long(return_value, "rdev", statbuf.st_rdev);