forked from mhammond/pywin32
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin32netmodule.cpp
More file actions
1226 lines (1127 loc) · 47.5 KB
/
win32netmodule.cpp
File metadata and controls
1226 lines (1127 loc) · 47.5 KB
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
/***********************************************************
win32net.cpp -- module for interface into Network API
NOTE: The Network API for NT uses UNICODE. Therefore, you
can not simply pass python strings to the API functioms - some
conversion is required.
Note: The NET functions have their own set of error codes in 2100-2200
range. The system error functions do not always apply.
i.e. GetLastError may be useless.
REV HISTORY:
Original version: Mark Hammond's Build 109 distribution.
October, 98: rewrote PyNetUserChangePassword - changed error handling - needs testing
rewrote PyNetUserGetGroups - fixed enumeration - tested
rewrote PyNetUserGetLocalGroups - fixed enumeration - tested
added PyNetShareEnum1 - exported as NetShareEnum - check assumptions - tested
PyNetMessageBufferSend - didn't touch
November, 98 Cleaned up return Lists (removed Integer count as redundant)
******************************************************************/
// @doc
#include "PyWinTypes.h"
#include "lm.h"
#include "lmuseflg.h"
#include "win32net.h"
#include "assert.h"
#if WINVER >= 0x0500
NetGetJoinInformationfunc pfnNetGetJoinInformation = NULL;
#endif
/*****************************************************************************/
/* error helpers */
PyObject *ReturnNetError(char *fnName, long err /*=0*/) { return PyWin_SetAPIError(fnName, err); };
BOOL FindNET_STRUCT(DWORD level, PyNET_STRUCT *pBase, PyNET_STRUCT **ppRet)
{
for (; pBase->entries; pBase++) {
if (level == pBase->level) {
*ppRet = pBase;
return TRUE;
}
}
PyErr_SetString(PyExc_ValueError, "This information level is not supported");
return FALSE;
}
void PyObject_FreeNET_STRUCT(PyNET_STRUCT *pI, BYTE *pBuf)
{
if (pBuf == NULL)
return;
// Free all the strings.
PyNET_STRUCT_ITEM *pItem;
for (pItem = pI->entries; pItem->attrname != NULL; pItem++) {
switch (pItem->type) {
case NSI_WSTR:
if (*((WCHAR **)(pBuf + pItem->off)))
PyWinObject_FreeWCHAR(*((WCHAR **)(pBuf + pItem->off)));
break;
case NSI_HOURS:
if (*((char **)(pBuf + pItem->off)))
free(*((char **)(pBuf + pItem->off)));
break;
case NSI_SID:
if (*((SID **)(pBuf + pItem->off)))
free(*((SID **)(pBuf + pItem->off)));
break;
case NSI_SECURITY_DESCRIPTOR:
if (*((SECURITY_DESCRIPTOR **)(pBuf + pItem->off)))
free(*((SECURITY_DESCRIPTOR **)(pBuf + pItem->off)));
break;
default:
break;
}
}
free(pBuf);
}
BOOL PyObject_AsNET_STRUCT(PyObject *ob, PyNET_STRUCT *pI, BYTE **ppRet)
{
BOOL ok = FALSE;
if (!PyMapping_Check(ob)) {
PyErr_SetString(PyExc_TypeError, "The object must be a mapping");
return FALSE;
}
// allocate the structure, and wipe it to zero.
BYTE *buf = (BYTE *)malloc(pI->structsize);
memset(buf, 0, pI->structsize);
PyNET_STRUCT_ITEM *pItem;
for (pItem = pI->entries; pItem->attrname != NULL; pItem++) {
PyObject *subob = PyMapping_GetItemString(ob, pItem->attrname);
if (subob == NULL) {
PyErr_Clear();
// See if it is OK.
if (pItem->reqd) {
PyErr_Format(PyExc_ValueError, "The mapping does not have the required attribute '%s'",
pItem->attrname);
goto done;
}
}
else {
switch (pItem->type) {
case NSI_WSTR:
WCHAR *wsz;
if (!PyWinObject_AsWCHAR(subob, &wsz, !pItem->reqd)) {
Py_DECREF(subob);
goto done;
}
*((WCHAR **)(buf + pItem->off)) = wsz;
break;
case NSI_DWORD:
*((DWORD *)(buf + pItem->off)) = PyLong_AsUnsignedLongMask(subob);
if (*((DWORD *)(buf + pItem->off)) == -1 && PyErr_Occurred()) {
PyErr_Clear();
PyErr_Format(PyExc_TypeError, "The mapping attribute '%s' must be an unsigned 32 bit int",
pItem->attrname);
Py_DECREF(subob);
goto done;
}
break;
case NSI_LONG:
*((LONG *)(buf + pItem->off)) = PyLong_AsLong(subob);
if (*((LONG *)(buf + pItem->off)) == -1 && PyErr_Occurred()) {
PyErr_Clear();
PyErr_Format(PyExc_TypeError, "The mapping attribute '%s' must be an integer", pItem->attrname);
Py_DECREF(subob);
goto done;
}
break;
case NSI_BOOL:
*((BOOL *)(buf + pItem->off)) = PyObject_IsTrue(subob);
if (*((BOOL *)(buf + pItem->off)) == -1 && PyErr_Occurred()) {
PyErr_Clear();
PyErr_Format(PyExc_TypeError, "The mapping attribute '%s' must be boolean", pItem->attrname);
Py_DECREF(subob);
goto done;
}
break;
case NSI_HOURS:
if (subob != Py_None) {
if (!PyBytes_Check(subob) || PyBytes_Size(subob) != 21) {
PyErr_Format(PyExc_TypeError,
"The mapping attribute '%s' must be a string of exactly length 21",
pItem->attrname);
Py_DECREF(subob);
goto done;
}
*((char **)(buf + pItem->off)) = (char *)malloc(21);
memcpy(*((char **)(buf + pItem->off)), PyBytes_AsString(subob), 21);
}
break;
case NSI_SID: {
PSID pSIDsrc;
if (!PyWinObject_AsSID(subob, &pSIDsrc, TRUE)) {
Py_DECREF(subob);
goto done;
}
PSID *ppSIDdest = ((PSID *)(buf + pItem->off));
size_t len = GetLengthSid(pSIDsrc);
*ppSIDdest = (SID *)malloc(len);
memcpy(*ppSIDdest, pSIDsrc, len);
} break;
case NSI_SECURITY_DESCRIPTOR: {
PSECURITY_DESCRIPTOR pSDsrc;
if (!PyWinObject_AsSECURITY_DESCRIPTOR(subob, &pSDsrc, TRUE)) {
Py_DECREF(subob);
goto done;
}
PSECURITY_DESCRIPTOR *ppSDdest = ((PSECURITY_DESCRIPTOR *)(buf + pItem->off));
if (pSDsrc == NULL)
*ppSDdest = NULL;
else {
size_t len = GetSecurityDescriptorLength(pSDsrc);
*ppSDdest = (PSECURITY_DESCRIPTOR)malloc(len);
memcpy(*ppSDdest, pSDsrc, len);
}
} break;
default:
PyErr_SetString(PyExc_RuntimeError, "invalid internal data type");
Py_DECREF(subob);
goto done;
}
Py_DECREF(subob);
}
}
ok = TRUE;
done:
if (!ok) {
PyObject_FreeNET_STRUCT(pI, buf);
return FALSE;
}
*ppRet = buf;
return TRUE;
}
PyObject *PyObject_FromNET_STRUCT(PyNET_STRUCT *pI, BYTE *buf)
{
PyObject *ret = PyDict_New();
PyNET_STRUCT_ITEM *pItem;
for (pItem = pI->entries; pItem->attrname != NULL; pItem++) {
PyObject *newObj = NULL;
switch (pItem->type) {
case NSI_WSTR:
newObj = PyWinObject_FromWCHAR(*((WCHAR **)(buf + pItem->off)));
break;
case NSI_DWORD:
newObj = PyLong_FromUnsignedLong(*((DWORD *)(buf + pItem->off)));
break;
case NSI_LONG:
newObj = PyLong_FromLong(*((LONG *)(buf + pItem->off)));
break;
case NSI_BOOL:
newObj = *((BOOL *)(buf + pItem->off)) ? Py_True : Py_False;
Py_INCREF(newObj);
break;
case NSI_HOURS: {
char *data = *((char **)(buf + pItem->off));
if (data) {
newObj = PyBytes_FromStringAndSize(data, 21);
}
else {
newObj = Py_None;
Py_INCREF(Py_None);
}
break;
}
case NSI_SID:
newObj = PyWinObject_FromSID(*((SID **)(buf + pItem->off)));
break;
case NSI_SECURITY_DESCRIPTOR:
newObj = PyWinObject_FromSECURITY_DESCRIPTOR(*((PSECURITY_DESCRIPTOR *)(buf + pItem->off)));
break;
default:
PyErr_SetString(PyExc_RuntimeError, "invalid internal data");
break;
}
if (newObj == NULL) {
Py_DECREF(ret);
return NULL;
}
PyMapping_SetItemString(ret, pItem->attrname, newObj);
Py_DECREF(newObj);
}
return ret;
}
PyObject *PyDoSimpleEnum(PyObject *self, PyObject *args, PFNSIMPLEENUM pfn, char *fnname, PyNET_STRUCT *pInfos)
{
WCHAR *szServer = NULL;
PyObject *obServer;
PyObject *ret = NULL;
PyNET_STRUCT *pInfo;
DWORD err;
DWORD dwPrefLen = MAX_PREFERRED_LENGTH;
DWORD level;
BOOL ok = FALSE;
DWORD_PTR resumeHandle = 0;
DWORD numRead, i;
PyObject *list, *obResumeHandle = Py_None;
BYTE *buf = NULL;
DWORD totalEntries = 0;
if (!PyArg_ParseTuple(args, "Oi|Oi", &obServer, &level, &obResumeHandle, &dwPrefLen))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (obResumeHandle != Py_None && !PyWinLong_AsDWORD_PTR(obResumeHandle, &resumeHandle))
goto done;
if (!FindNET_STRUCT(level, pInfos, &pInfo))
goto done;
Py_BEGIN_ALLOW_THREADS
/* Bad resume handles etc can cause access violations here - catch them. */
PYWINTYPES_TRY
{
err = (*pfn)(szServer, level, &buf, dwPrefLen, &numRead, &totalEntries, &resumeHandle);
}
PYWINTYPES_EXCEPT { err = ERROR_INVALID_PARAMETER; }
Py_END_ALLOW_THREADS if (err != 0 && err != ERROR_MORE_DATA)
{
ReturnNetError(fnname, err);
goto done;
}
list = PyList_New(numRead);
if (list == NULL)
goto done;
for (i = 0; i < numRead; i++) {
PyObject *sub = PyObject_FromNET_STRUCT(pInfo, buf + (i * pInfo->structsize));
if (sub == NULL)
goto done;
PyList_SetItem(list, i, sub);
}
resumeHandle = err == 0 ? 0 : resumeHandle;
ret = Py_BuildValue("OlN", list, totalEntries, PyWinObject_FromDWORD_PTR(resumeHandle));
Py_DECREF(list);
ok = TRUE;
done:
if (buf)
NetApiBufferFree(buf);
if (!ok) {
Py_XDECREF(ret);
ret = NULL;
}
PyWinObject_FreeWCHAR(szServer);
return ret;
}
PyObject *PyDoNamedEnum(PyObject *self, PyObject *args, PFNNAMEDENUM pfn, char *fnname, PyNET_STRUCT *pInfos)
{
WCHAR *szServer = NULL, *szGroup = NULL;
PyObject *obServer, *obGroup;
PyObject *ret = NULL;
PyNET_STRUCT *pInfo;
DWORD err;
DWORD dwPrefLen = 4096;
DWORD level;
BOOL ok = FALSE;
DWORD_PTR resumeHandle = 0;
PyObject *obResumeHandle = Py_None;
DWORD numRead, i;
PyObject *list;
BYTE *buf = NULL;
DWORD totalEntries = 0;
if (!PyArg_ParseTuple(args, "OOi|Oi", &obServer, &obGroup, &level, &obResumeHandle, &dwPrefLen))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obGroup, &szGroup, FALSE))
goto done;
if (obResumeHandle != Py_None && !PyWinLong_AsDWORD_PTR(obResumeHandle, &resumeHandle))
goto done;
if (!FindNET_STRUCT(level, pInfos, &pInfo))
goto done;
Py_BEGIN_ALLOW_THREADS err =
(*pfn)(szServer, szGroup, level, &buf, dwPrefLen, &numRead, &totalEntries, &resumeHandle);
Py_END_ALLOW_THREADS if (err != 0 && err != ERROR_MORE_DATA)
{
ReturnNetError(fnname, err);
goto done;
}
list = PyList_New(numRead);
if (list == NULL)
goto done;
for (i = 0; i < numRead; i++) {
PyObject *sub = PyObject_FromNET_STRUCT(pInfo, buf + (i * pInfo->structsize));
if (sub == NULL)
goto done;
PyList_SetItem(list, i, sub);
}
resumeHandle = err == 0 ? 0 : resumeHandle;
ret = Py_BuildValue("OlN", list, totalEntries, PyWinObject_FromDWORD_PTR(resumeHandle));
Py_DECREF(list);
ok = TRUE;
done:
if (buf)
NetApiBufferFree(buf);
if (!ok) {
Py_XDECREF(ret);
ret = NULL;
}
PyWinObject_FreeWCHAR(szServer);
PyWinObject_FreeWCHAR(szGroup);
return ret;
}
PyObject *PyDoGroupSet(PyObject *self, PyObject *args, PFNGROUPSET pfn, char *fnname, PyNET_STRUCT *pInfos)
{
WCHAR *szServer = NULL;
WCHAR *szGroup = NULL;
PyObject *obServer, *obGroup, *obData;
PyObject *ret = NULL;
PyObject *members_tuple = NULL;
DWORD level;
DWORD err = 0;
BYTE *buf = NULL;
BYTE **ppTempObjects = NULL;
DWORD i, numEntries;
PyNET_STRUCT *pI;
if (!PyArg_ParseTuple(args, "OOiO", &obServer, &obGroup, &level, &obData))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obGroup, &szGroup, FALSE))
goto done;
if (!FindNET_STRUCT(level, pInfos, &pI))
goto done;
members_tuple = PyWinSequence_Tuple(obData, &numEntries);
if (members_tuple == NULL) {
PyErr_SetString(PyExc_TypeError, "Data must be a sequence of dictionaries");
goto done;
}
ppTempObjects = new BYTE *[numEntries];
memset(ppTempObjects, 0, sizeof(BYTE *) * numEntries);
for (i = 0; i < numEntries; i++) {
PyObject *sub = PyTuple_GET_ITEM(members_tuple, i);
if (!PyObject_AsNET_STRUCT(sub, pI, ppTempObjects + i))
goto done;
}
// OK - all objects are ok, and we are holding the buffers.
// copy to our own buffer
buf = new BYTE[numEntries * pI->structsize];
if (buf == NULL) {
PyErr_SetString(PyExc_MemoryError, "Allocating buffer for members");
goto done;
}
for (i = 0; i < numEntries; i++) memcpy(buf + (i * pI->structsize), ppTempObjects[i], pI->structsize);
Py_BEGIN_ALLOW_THREADS err = (*pfn)(szServer, szGroup, level, buf, numEntries);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError(fnname, err);
goto done;
}
ret = Py_None;
Py_INCREF(Py_None);
done:
PyWinObject_FreeWCHAR(szServer);
PyWinObject_FreeWCHAR(szGroup);
Py_XDECREF(members_tuple);
if (ppTempObjects) {
for (i = 0; i < numEntries; i++) {
PyObject_FreeNET_STRUCT(pI, ppTempObjects[i]);
}
delete[] ppTempObjects;
}
delete[] buf;
return ret;
}
PyObject *PyDoGetInfo(PyObject *self, PyObject *args, PFNGETINFO pfn, char *fnname, PyNET_STRUCT *pInfos)
{
WCHAR *szServer = NULL;
WCHAR *szName = NULL;
PyObject *obName, *obServer;
PyNET_STRUCT *pInfo;
BYTE *buf = NULL;
PyObject *ret = NULL;
int typ;
DWORD err;
if (!PyArg_ParseTuple(args, "OOi", &obServer, &obName, &typ))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obName, &szName, FALSE))
goto done;
if (!FindNET_STRUCT(typ, pInfos, &pInfo))
goto done;
Py_BEGIN_ALLOW_THREADS err = (*pfn)(szServer, szName, typ, &buf);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError(fnname, err);
goto done;
}
ret = PyObject_FromNET_STRUCT(pInfo, buf);
done:
if (buf)
NetApiBufferFree(buf);
PyWinObject_FreeWCHAR(szServer);
PyWinObject_FreeWCHAR(szName);
return ret;
}
PyObject *PyDoGetModalsInfo(PyObject *self, PyObject *args, PFNGETMODALSINFO pfn, char *fnname, PyNET_STRUCT *pInfos)
{
WCHAR *szServer = NULL;
PyObject *obServer;
PyNET_STRUCT *pInfo;
BYTE *buf = NULL;
PyObject *ret = NULL;
int typ;
DWORD err;
if (!PyArg_ParseTuple(args, "Oi", &obServer, &typ))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!FindNET_STRUCT(typ, pInfos, &pInfo))
goto done;
Py_BEGIN_ALLOW_THREADS err = (*pfn)(szServer, typ, &buf);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError(fnname, err);
goto done;
}
ret = PyObject_FromNET_STRUCT(pInfo, buf);
done:
if (buf)
NetApiBufferFree(buf);
PyWinObject_FreeWCHAR(szServer);
return ret;
}
/*****************************************************************************/
// @pymethod |win32net|NetMessageBufferSend|sends a string to a registered message alias.
/******************************************************************************/
PyObject *PyNetMessageBufferSend(PyObject *self, PyObject *args)
{
DWORD rc;
TCHAR *serverName = NULL;
TCHAR *msgName = NULL;
TCHAR *fromName = NULL;
TCHAR *message = NULL;
PyObject *obServerName, *obMsgName, *obFromName, *obMessage;
PyObject *ret = NULL;
DWORD msgLen;
if (!PyArg_ParseTuple(args, "OOOO:NetMessageBufferSend",
&obServerName, // @pyparm string|domain||Specifies the name of the remote server on which the
// function is to execute. None or empty string the local computer.
&obMsgName, // @pyparm string|userName||Specifies the message name to which the message
// buffer should be sent.
&obFromName, // @pyparm string|fromName||The user the message is to come from, or None for
// the current user.
&obMessage)) // @pyparm string|message||The message text
return NULL;
if (!PyWinObject_AsTCHAR(obServerName, &serverName, TRUE))
goto done;
if (!PyWinObject_AsTCHAR(obMsgName, &msgName, FALSE))
goto done;
if (!PyWinObject_AsTCHAR(obFromName, &fromName, TRUE))
goto done;
if (!PyWinObject_AsTCHAR(obMessage, &message, FALSE, &msgLen))
goto done;
Py_BEGIN_ALLOW_THREADS
// message is "BYTE *", but still expects Unicode? Wonder why not LPTSTR like the other string args?
rc = NetMessageBufferSend(serverName, msgName, fromName, (BYTE *)message, msgLen * sizeof(TCHAR));
Py_END_ALLOW_THREADS if (rc)
{
ReturnNetError("NetMessageBufferSend", rc); // @pyseeapi NetMessageBufferSend
goto done;
}
Py_INCREF(Py_None);
ret = Py_None;
done:
PyWinObject_FreeTCHAR(serverName);
PyWinObject_FreeTCHAR(msgName);
PyWinObject_FreeTCHAR(fromName);
PyWinObject_FreeTCHAR(message);
return ret;
}
// @pymethod |win32net|NetMessageNameAdd|Adds a message alias for specified machine
PyObject *PyNetMessageNameAdd(PyObject *self, PyObject *args)
{
NET_API_STATUS err;
WCHAR *server = NULL, *alias = NULL;
PyObject *observer = NULL, *obalias = NULL, *ret = NULL;
// @pyparm str/unicode|server||Name of server on which to execute - leading backslashes required on NT - local
// machine used if None
// @pyparm str/unicode|msgname||Message alias to add, 15 characters max
if (!PyArg_ParseTuple(args, "OO:NetMessageNameAdd", &observer, &obalias))
goto done;
if (!PyWinObject_AsWCHAR(observer, &server, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obalias, &alias, FALSE))
goto done;
err = NetMessageNameAdd(server, alias);
if (err == NERR_Success) {
Py_INCREF(Py_None);
ret = Py_None;
}
else
ReturnNetError("NetMessageNameAdd", err);
done:
if (server != NULL)
PyWinObject_FreeWCHAR(server);
if (alias != NULL)
PyWinObject_FreeWCHAR(alias);
return ret;
}
// @pymethod |win32net|NetMessageNameDel|Removes a message alias for specified machine
PyObject *PyNetMessageNameDel(PyObject *self, PyObject *args)
{
NET_API_STATUS err;
WCHAR *server = NULL, *alias = NULL;
PyObject *observer = NULL, *obalias = NULL, *ret = NULL;
// @pyparm str/unicode|server||Name of server on which to execute - leading backslashes required on NT - local
// machine used if None
// @pyparm str/unicode|msgname||Message alias to delete for specified machine
if (!PyArg_ParseTuple(args, "OO:NetMessageNameDel", &observer, &obalias))
goto done;
if (!PyWinObject_AsWCHAR(observer, &server, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obalias, &alias, FALSE))
goto done;
err = NetMessageNameDel(server, alias);
if (err == NERR_Success) {
Py_INCREF(Py_None);
ret = Py_None;
}
else
ReturnNetError("NetMessageNameDel", err);
done:
if (server != NULL)
PyWinObject_FreeWCHAR(server);
if (alias != NULL)
PyWinObject_FreeWCHAR(alias);
return ret;
}
// @pymethod |win32net|NetMessageNameEnum|Lists aliases for a computer
PyObject *PyNetMessageNameEnum(PyObject *self, PyObject *args)
{
NET_API_STATUS err = ERROR_MORE_DATA;
DWORD maxlen = MAX_PREFERRED_LENGTH, level = 0;
#ifdef Py_DEBUG
maxlen = 128;
#endif
DWORD entriesread = 0, totalentries = 0, resume_handle = 0;
DWORD msg_ind;
WCHAR *server = NULL;
BYTE *buf;
MSG_INFO_0 *pmsg0;
PyObject *observer = NULL, *ret = NULL, *msg_item = NULL;
if (!PyArg_ParseTuple(args, "|O:NetMessageNameEnum", &observer))
return NULL;
// @pyparm str/unicode|Server||Name of server on which to execute - leading backslashes required on NT - local
// machine used if None
if (observer != NULL)
if (!PyWinObject_AsWCHAR(observer, &server, TRUE))
return NULL;
ret = PyList_New(0);
if (!ret)
return NULL;
while (TRUE) {
buf = NULL;
err = NetMessageNameEnum(server, level, &buf, maxlen, &entriesread, &totalentries, &resume_handle);
if ((err == NERR_Success) || (err == ERROR_MORE_DATA)) {
pmsg0 = (MSG_INFO_0 *)buf;
for (msg_ind = 0; msg_ind < entriesread; msg_ind++) {
msg_item = PyWinObject_FromWCHAR(pmsg0->msgi0_name);
if (!msg_item) {
Py_DECREF(ret);
ret = NULL;
break;
}
PyList_Append(ret, msg_item);
Py_DECREF(msg_item);
pmsg0++;
}
}
else {
ReturnNetError("NetMessageNameEnum", err);
Py_DECREF(ret);
ret = NULL;
}
if (buf)
NetApiBufferFree(buf);
// With certain buffer size/return size combinations, function can actually return
// ERROR_MORE_DATA when done, while setting resume_handle to 0, resulting in an infinite
// loop if you use only the return code
if ((ret == NULL) || (resume_handle == 0))
break;
}
if (server != NULL)
PyWinObject_FreeWCHAR(server);
return ret;
}
PyObject *PyDoSetInfo(PyObject *self, PyObject *args, PFNSETINFO pfn, char *fnname, PyNET_STRUCT *pInfos)
{
WCHAR *szServer = NULL;
WCHAR *szName = NULL;
PyObject *obName, *obServer, *obData;
PyNET_STRUCT *pInfo;
BYTE *buf = NULL;
PyObject *ret = NULL;
int typ;
DWORD err = 0;
if (!PyArg_ParseTuple(args, "OOiO", &obServer, &obName, &typ, &obData))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obName, &szName, FALSE))
goto done;
if (!FindNET_STRUCT(typ, pInfos, &pInfo))
goto done;
if (!PyObject_AsNET_STRUCT(obData, pInfo, &buf))
goto done;
Py_BEGIN_ALLOW_THREADS err = (*pfn)(szServer, szName, typ, buf, NULL);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError(fnname, err);
goto done;
}
ret = Py_None;
Py_INCREF(ret);
done:
if (buf)
PyObject_FreeNET_STRUCT(pInfo, buf);
PyWinObject_FreeWCHAR(szServer);
PyWinObject_FreeWCHAR(szName);
return ret;
}
PyObject *PyDoSetModalsInfo(PyObject *self, PyObject *args, PFNSETMODALSINFO pfn, char *fnname, PyNET_STRUCT *pInfos)
{
WCHAR *szServer = NULL;
PyObject *obServer, *obData;
PyNET_STRUCT *pInfo;
BYTE *buf = NULL;
PyObject *ret = NULL;
int typ;
DWORD err = 0;
if (!PyArg_ParseTuple(args, "OiO", &obServer, &typ, &obData))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!FindNET_STRUCT(typ, pInfos, &pInfo))
goto done;
if (!PyObject_AsNET_STRUCT(obData, pInfo, &buf))
goto done;
Py_BEGIN_ALLOW_THREADS err = (*pfn)(szServer, typ, buf, NULL);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError(fnname, err);
goto done;
}
ret = Py_None;
Py_INCREF(ret);
done:
if (buf)
PyObject_FreeNET_STRUCT(pInfo, buf);
PyWinObject_FreeWCHAR(szServer);
return ret;
}
PyObject *PyDoAdd(PyObject *self, PyObject *args, PFNADD pfn, char *fnname, PyNET_STRUCT *pInfos)
{
WCHAR *szServer = NULL;
PyObject *obServer, *obData;
PyNET_STRUCT *pInfo;
BYTE *buf = NULL;
PyObject *ret = NULL;
int typ;
DWORD err = 0;
if (!PyArg_ParseTuple(args, "OiO", &obServer, &typ, &obData))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!FindNET_STRUCT(typ, pInfos, &pInfo))
goto done;
if (!PyObject_AsNET_STRUCT(obData, pInfo, &buf))
goto done;
Py_BEGIN_ALLOW_THREADS err = (*pfn)(szServer, typ, buf, NULL);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError(fnname, err);
goto done;
}
ret = Py_None;
Py_INCREF(ret);
done:
if (buf)
PyObject_FreeNET_STRUCT(pInfo, buf);
PyWinObject_FreeWCHAR(szServer);
return ret;
}
PyObject *PyDoDel(PyObject *self, PyObject *args, PFNDEL pfn, char *fnname)
{
WCHAR *szServer = NULL;
WCHAR *szName = NULL;
PyObject *obName, *obServer;
PyObject *ret = NULL;
DWORD err = 0;
if (!PyArg_ParseTuple(args, "OO", &obServer, &obName))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obName, &szName, FALSE))
goto done;
Py_BEGIN_ALLOW_THREADS err = (*pfn)(szServer, szName);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError(fnname, err);
goto done;
}
ret = Py_None;
Py_INCREF(Py_None);
done:
PyWinObject_FreeWCHAR(szServer);
PyWinObject_FreeWCHAR(szName);
return ret;
}
PyObject *PyDoGroupDelMembers(PyObject *self, PyObject *args)
{
WCHAR *szServer = NULL;
WCHAR *szGroup = NULL;
PyObject *obServer, *obGroup, *obData;
PyObject *ret = NULL;
PyObject *members_tuple = NULL;
DWORD err = 0;
BYTE *buf = NULL;
DWORD i, numEntries;
DWORD level = 3;
LOCALGROUP_MEMBERS_INFO_3 *plgrminfo;
if (!PyArg_ParseTuple(args, "OOO", &obServer, &obGroup, &obData))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obGroup, &szGroup, FALSE))
goto done;
members_tuple = PyWinSequence_Tuple(obData, &numEntries);
if (members_tuple == NULL) {
PyErr_SetString(PyExc_TypeError, "Data must be a sequence of dictionaries");
goto done;
}
plgrminfo = new LOCALGROUP_MEMBERS_INFO_3[numEntries];
if (plgrminfo == NULL) {
PyErr_NoMemory();
goto done;
}
// XXX - todo - we should allow a list of LOCALGROUP_MEMBER_INFO items *or* strings
memset(plgrminfo, 0, sizeof(LOCALGROUP_MEMBERS_INFO_3) * numEntries);
for (i = 0; i < numEntries; i++) {
PyObject *sub = PyTuple_GET_ITEM(members_tuple, i);
if (!PyWinObject_AsWCHAR(sub, &plgrminfo[i].lgrmi3_domainandname))
goto done;
}
Py_BEGIN_ALLOW_THREADS err = NetLocalGroupDelMembers(szServer, szGroup, 3, (BYTE *)plgrminfo, numEntries);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError("NetLocalGroupDelMembers", err);
goto done;
}
ret = Py_None;
Py_INCREF(Py_None);
done:
PyWinObject_FreeWCHAR(szServer);
PyWinObject_FreeWCHAR(szGroup);
Py_XDECREF(members_tuple);
if (plgrminfo) {
for (i = 0; i < numEntries; i++) {
if (plgrminfo[i].lgrmi3_domainandname) {
PyWinObject_FreeWCHAR(plgrminfo[i].lgrmi3_domainandname);
}
}
delete[] plgrminfo;
}
return ret;
}
/* Other misc functions */
// @pymethod <o PyUnicode>|win32net|NetGetDCName|Returns the name of the primary domain controller (PDC).
PyObject *PyNetGetDCName(PyObject *self, PyObject *args)
{
PyObject *obServer = Py_None, *obDomain = Py_None;
WCHAR *szServer = NULL, *szDomain = NULL, *result = NULL;
PyObject *ret = NULL;
NET_API_STATUS err;
// @pyparm <o PyUnicode>|server|None|Specifies the name of the remote server on which the function is to execute. If
// this parameter is None, the local computer is used.
// @pyparm <o PyUnicode>|domain|None|Specifies the name of the domain. If this parameter is None, the name of the
// domain controller for the primary domain is used.
if (!PyArg_ParseTuple(args, "|OO", &obServer, &obDomain))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obDomain, &szDomain, TRUE))
goto done;
Py_BEGIN_ALLOW_THREADS err = NetGetDCName(szServer, szDomain, (LPBYTE *)&result);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError("NetGetDCName", err);
goto done;
}
ret = PyWinObject_FromWCHAR(result);
done:
PyWinObject_FreeWCHAR(szServer);
PyWinObject_FreeWCHAR(szDomain);
NetApiBufferFree(result);
return ret;
}
// @pymethod <o PyUnicode>|win32net|NetGetAnyDCName|Returns the name of any domain controller trusted by the specified
// server.
PyObject *PyNetGetAnyDCName(PyObject *self, PyObject *args)
{
PyObject *obServer = Py_None, *obDomain = Py_None;
WCHAR *szServer = NULL, *szDomain = NULL, *result = NULL;
PyObject *ret = NULL;
NET_API_STATUS err;
// @pyparm <o PyUnicode>|server|None|Specifies the name of the remote server on which the function is to execute. If
// this parameter is None, the local computer is used.
// @pyparm <o PyUnicode>|domain|None|Specifies the name of the domain. If this parameter is None, the name of the
// domain controller for the primary domain is used.
if (!PyArg_ParseTuple(args, "|OO", &obServer, &obDomain))
return NULL;
if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE))
goto done;
if (!PyWinObject_AsWCHAR(obDomain, &szDomain, TRUE))
goto done;
Py_BEGIN_ALLOW_THREADS err = NetGetAnyDCName(szServer, szDomain, (LPBYTE *)&result);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError("NetGetAnyDCName", err);
goto done;
}
ret = PyWinObject_FromWCHAR(result);
done:
PyWinObject_FreeWCHAR(szServer);
PyWinObject_FreeWCHAR(szDomain);
NetApiBufferFree(result);
return ret;
}
#if WINVER >= 0x0500
// @pymethod <o PyUnicode>, int|win32net|NetGetJoinInformation|Retrieves join status information for the specified
// computer.
static PyObject *PyNetGetJoinInformation(PyObject *self, PyObject *args)
{
PyObject *obServer = Py_None;
WCHAR *server = NULL;
;
WCHAR *result = NULL;
PyObject *ret = NULL;
NET_API_STATUS err;
NETSETUP_JOIN_STATUS status;
if (!PyArg_ParseTuple(args, "|O:NetGetJoinInformation", &obServer))
return NULL;
if (pfnNetGetJoinInformation == NULL) {
PyErr_SetString(PyExc_NotImplementedError, "NetGetJoinInformation does not exist on this platform");
goto done;
}
if (!PyWinObject_AsWCHAR(obServer, &server, TRUE))
goto done;
Py_BEGIN_ALLOW_THREADS err = (*pfnNetGetJoinInformation)(server, &result, &status);
Py_END_ALLOW_THREADS if (err)
{
ReturnNetError("NetGetJoinInformation", err);
goto done;
}
ret = Py_BuildValue("Nl", PyWinObject_FromWCHAR(result), status);
done:
PyWinObject_FreeWCHAR(server);
NetApiBufferFree(result);
return ret;
}
#endif // WINVER
/*************************************************************************************************************
**
*************************************************************************************************************/
extern PyObject *PyNetUserAdd(PyObject *self, PyObject *args);
extern PyObject *PyNetUserSetInfo(PyObject *self, PyObject *args);
extern PyObject *PyNetUserGetInfo(PyObject *self, PyObject *args);
extern PyObject *PyNetUserDel(PyObject *self, PyObject *args);
extern PyObject *PyNetUserEnum(PyObject *self, PyObject *args);
extern PyObject *PyNetUserChangePassword(PyObject *self, PyObject *args);
extern PyObject *PyNetUserGetLocalGroups(PyObject *self, PyObject *args);
extern PyObject *PyNetUserGetGroups(PyObject *self, PyObject *args);
extern PyObject *PyNetUserModalsGet(PyObject *self, PyObject *args);
extern PyObject *PyNetUserModalsSet(PyObject *self, PyObject *args);