-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathepdump.c
2040 lines (1640 loc) · 61.4 KB
/
epdump.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
/*******************************************************************************
* epmap.c - Enumerate local dynamic endpoints on a remote endpoint mapper.
* An endpoint mapper is a service on a remote procedure call (RPC) server that
* maintains a database of dynamic endpoints and allows clients to map an
* interface/object UUID pair to a local dynamic endpoint. This trivial tool
* can be used to identify services that have registered with DCE/RPC endpoint
* mapper. Usage: epmap [-p port] hostname.
*
* Endpoint Mapper interface: e1af8308-5d1f-11c9-91a4-08002b14a0fa
*
* WIP, I have to fix several things.
*
* Output example:
* epdump -p 135 192.168.1.4
*
* Binding to portmapper: 192.168.1.4[135] ...
* Querying Endpoint Mapper Database...
*
* UUID: d95afe70-a6d5-4259-822e-2c84da1ddb0d
* ncacn_ip_tcp:192.168.1.4[49152]
*
* UUID: 367abb81-9844-35f1-ad32-98f038001003
* ncacn_ip_tcp:192.168.1.4[49162]
*
* UUID: b58aa02e-2884-4e97-8176-4ee06d794184
* ncacn_np:192.168.1.4[\\pipe\trkwks]
*
* UUID: b25a52bf-e5dd-4f4a-aea6-8ca7272a0e86 KeyIso
* ncacn_np:192.168.1.4[\\pipe\lsass]
*
* UUID: b25a52bf-e5dd-4f4a-aea6-8ca7272a0e86 KeyIso
* ncacn_np:192.168.1.4[\\PIPE\protected_storage]
*
* UUID: 12345778-1234-abcd-ef00-0123456789ac
* ncacn_np:192.168.1.4[\\pipe\lsass]
*
* UUID: 12345778-1234-abcd-ef00-0123456789ac
* ncacn_np:192.168.1.4[\\PIPE\protected_storage]
*
* UUID: 12345778-1234-abcd-ef00-0123456789ac
* ncacn_ip_tcp:192.168.1.4[49155]
*
*
*
*
*
******************************************************************************/
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef unsigned char byte;
typedef struct uuid {
uint32_t time_low;
uint16_t time_mid;
uint16_t time_hi_and_version;
uint8_t clock_seq_hi_and_reserved;
uint8_t clock_seq_low;
byte node[6];
} uuid_t, *uuid_p_t;
#define UUID_SIZE 16
typedef uint16_t p_context_id_t;
typedef struct p_syntax_id {
uuid_t if_uuid;
uint32_t if_version; /* Major/Minor version. */
} p_syntax_id_t;
/* Elements in a presentation context list. */
typedef struct _p_cont_elem {
p_context_id_t p_cont_id;
uint8_t n_transfer_syn;
uint8_t reserved;
p_syntax_id_t abstract_syntax;
p_syntax_id_t *transfer_syntaxes;
} p_cont_elem_t;
/* Presentation context list. */
typedef struct p_cont_list {
uint8_t n_context_elem;
uint8_t reserved;
uint16_t reserved2;
p_cont_elem_t *p_cont_elem;
} p_cont_list_t;
/* Common Authentication Verifier. */
typedef struct auth_verifier_co {
/* Restore 4-byte alignment */
/* uint8_t auth_pad[]; */
uint8_t auth_type;
uint8_t auth_level;
uint8_t auth_pad_length;
uint8_t auth_reserved;
uint8_t auth_context_id;
uint8_t *auth_value; /* Size is auth_length. */
} auth_verifier_co_t;
/* BIND PDU header */
typedef struct rpcconn_bind_hdr {
uint8_t rpc_vers; /* RPC version. */
uint8_t rpc_vers_minor; /* Minor version. */
uint8_t ptype; /* Bind PDU. */
uint8_t pfc_flags; /* PFC Flags. */
uint8_t packed_drep[4]; /* NDR data representation format label. */
uint16_t frag_length; /* Total length of fragment. */
uint16_t auth_length; /* Length of auth_value. */
uint32_t call_id; /* Call identifier. */
uint16_t max_xmit_frag; /* Max transmit fragment size. */
uint16_t max_recv_frag; /* Max receive fragment size. */
uint32_t assoc_group_id; /* Incarnation of client-server assoc group. */
p_cont_list_t p_context_elem; /* Presentation context list. */
auth_verifier_co_t auth_verifier; /* if auth_length != 0. */
} rpcconn_bind_hdr_t;
typedef struct port_any {
uint16_t length;
char *port_spec;
} port_any_t;
/* Result of a presentation context negotiation. */
typedef uint16_t p_cont_def_result_t;
/* Reason for rejection of a context element. */
typedef uint16_t p_provider_reason_t;
typedef struct p_result {
p_cont_def_result_t result;
p_provider_reason_t reason;
p_syntax_id_t transfer_syntax;
} p_result_t;
typedef struct p_result_list {
uint8_t n_results;
uint8_t reserved;
uint16_t reserved2;
p_result_t *p_results;
} p_result_list_t;
/* BIND_ACK PDU header */
typedef struct rpcconn_bind_ack_hdr {
uint8_t rpc_vers;
uint8_t rpc_vers_minor;
uint8_t ptype;
uint8_t pfc_flags;
byte packed_drep[4];
uint16_t frag_length;
uint16_t auth_length;
uint32_t call_id;
uint16_t max_xmit_frag;
uint16_t max_recv_frag;
uint32_t assoc_group_id; /* Returned assoc_group_id. */
port_any_t sec_addr; /* Optional secondary address. */
/* Restore 8-octet alignment. */
/* uint8_t pad2[]; */
p_result_list_t p_result_list; /* Variable size. */
auth_verifier_co_t auth_verifier; /* if auth_length != 0. */
} rpcconn_bind_ack_hdr_t;
/* Presentation context reject */
typedef uint16_t p_reject_reason_t;
typedef struct version {
uint8_t major;
uint8_t minor;
} version_t;
typedef version_t p_rt_version_t;
typedef struct p_rt_versions_supported {
uint8_t n_protocols;
p_rt_version_t *p_protocols;
} p_rt_versions_supported_t;
/* BIND NAK PDU header */
typedef struct rpcconn_bind_nak_hdr {
uint8_t rpc_vers;
uint8_t rpc_vers_minor;
uint8_t ptype;
uint8_t pfc_flags;
byte packed_drep[4];
uint16_t frag_length;
uint16_t auth_length;
uint32_t call_id;
p_reject_reason_t provider_reject_reason;
p_rt_versions_supported_t *versions; /* if reject reason is 4. */
} rpcconn_bind_nak_hdr_t;
/* Reasons for rejection of an association in the bind nak PDU. */
#define REASON_NOT_SPECIFIED 0
#define TEMPORARY_CONGESTION 1
#define LOCAL_LIMIT_EXCEEDED 2
#define CALLED_PADDR_UNKNOWN 3 /* Not used. */
#define PROTOCOL_VERSION_NOT_SUPPORTED 4
#define DEFAULT_CONTEXT_NOT_SUPPORTED 5 /* Not used. */
#define USER_DATA_NOT_READABLE 6 /* Not used. */
#define NO_PSAP_AVAILABLE 7 /* Not used. */
/* REQUEST PDU header */
typedef struct rpcconn_request_hdr {
uint8_t rpc_vers; /* RPC version. */
uint8_t rpc_vers_minor; /* Minor version. */
uint8_t ptype; /* Request PDU. */
uint8_t pfc_flags; /* PFC Flags. */
uint8_t packed_drep[4]; /* NDR data representation format label. */
uint16_t frag_length; /* Total length of fragment. */
uint16_t auth_length; /* Length of auth_value. */
uint32_t call_id; /* Call identifier. */
uint32_t alloc_hint; /* Allocation hint. */
p_context_id_t p_cont_id; /* Presentation context. */
uint16_t opnum; /* Operation # within the interface! */
uuid_t object; /* Only present if the PFC_OBJECT_UUID field is non zero. */
/* Stub data, 8-octet aligned. */
auth_verifier_co_t auth_verifier; /* if auth_length != 0 */
} rpcconn_request_hdr_t;
/* RESPONSE PDU header */
typedef struct rpcconn_response {
uint8_t rpc_vers;
uint8_t rpc_vers_minor;
uint8_t ptype;
uint8_t pfc_flags;
uint8_t packed_drep[4];
uint16_t frag_length;
uint16_t auth_length;
uint32_t call_id;
uint32_t alloc_hint;
p_context_id_t p_cont_id;
uint8_t cancel_count;
uint8_t reserved;
/* Stub data here, 8-octet aligned. */
auth_verifier_co_t auth_verifier; /* if auth_length != 0. */
} rpcconn_response_hdr_t;
/* FAULT PDU header */
typedef struct rpcconn_fault_hdr {
uint8_t rpc_vers;
uint8_t rpc_vers_minor;
uint8_t ptype;
uint8_t pfc_flags;
byte packed_drep[4];
uint16_t frag_length;
uint16_t auth_length;
uint32_t call_id;
uint32_t alloc_hint;
p_context_id_t p_cont_id; /* Presentation context. */
uint8_t cancel_count; /* Received cancel count. */
uint8_t reserved;
uint32_t status; /* Run-time fault code or zero. */
uint8_t reserved2[4];
/* Stub data here, 8-octet aligned. */
auth_verifier_co_t auth_verifier; /* if auth_length != 0. */
} rpcconn_fault_hdr_t;
/* SHUTDOWN PDU header */
typedef struct rpcconn_shutdown_hdr {
uint8_t rpc_vers;
uint8_t rpc_vers_minor;
uint8_t ptype;
uint8_t pfc_flags;
byte packed_drep[4];
uint16_t frag_length;
uint16_t auth_length;
uint32_t call_id;
} rpcconn_shutdown_hdr_t;
#define DEFAULT_EPMAP_PORT 135
/* PDU types */
#define RPC_PTYPE_REQUEST 0x00 /* CO/CL */
#define RPC_PTYPE_PING 0x01 /* CL */
#define RPC_PTYPE_RESPONSE 0x02 /* CO/CL */
#define RPC_PTYPE_FAULT 0x03 /* CO/CL */
#define RPC_PTYPE_WORKING 0x04 /* CL */
#define RPC_PTYPE_NOCALL 0x05 /* CL */
#define RPC_PTYPE_REJECT 0x06 /* CL */
#define RPC_PTYPE_ACK 0x07 /* CL */
#define RPC_PTYPE_CL_CANCEL 0x08 /* CL */
#define RPC_PTYPE_FACK 0x09 /* CL */
#define RPC_PTYPE_CANCEL_ACK 0x0a /* CL */
#define RPC_PTYPE_BIND 0x0b /* CO */
#define RPC_PTYPE_BIND_ACK 0x0c /* CO */
#define RPC_PTYPE_BIND_NAK 0x0d /* CO */
#define RPC_PTYPE_ALTER_CTX 0x0e /* CO */
#define RPC_PTYPE_ALTER_CTX_RESP 0x0f /* CO */
#define RPC_PTYPE_SHUTDOWN 0x11 /* CO */
#define RPC_PTYPE_CO_CANCEL 0x12 /* CO */
#define RPC_PTYPE_ORPHANED 0x13 /* CO */
/* PFC FLAGS */
#define PFC_FIRST_FRAG 0x01 /* First fragment. */
#define PFC_LAST_FRAG 0x02 /* Last fragment. */
#define PFC_PENDING_CANCEL 0x04 /* Cancel was pending at sender. */
#define PFC_RESERVED_1 0x08
#define PFC_CONC_MPX 0x10 /* Supports concurrent multiplexing.*/
#define PFC_DID_NOT_EXECUTE 0x20 /* Fault packet. */
#define PFC_MAYBE 0x40 /* "Maybe" call semantics requested. */
#define PFC_OBJECT_UUID 0x80 /* A non-nil object UUID is present. */
/* DCE/RPC Endpoint Mapper Protocol. */
/* Reference: pubs.opengroup.org/onlinepubs/009629399/apdxo.htm */
#define EPT_INSERT 0
#define EPT_DELETE 1
#define EPT_LOOKUP 2 /* Lookup entries in an endpoint map. */
#define EPT_MAP 4
#define EPT_LOOKUP_FREE 5
typedef struct ept_lookup_handle {
uint32_t attributes;
uuid_t uuid;
} ept_lookup_handle_t;
/**********************************
typedef struct ndr_context_handle {
uint32_t context_handle_attributes;
uuid_t context_handle_uuid;
} ndr_context_handle_t;
**********************************/
/* Inquiry types. */
#define RPC_C_EP_ALL_ELTS 0 /* Return all elements from the endpoint map. */
#define RPC_C_EP_MATCH_BY_IF 1
#define RPC_C_EP_MATCH_BY_OBJ 2
#define RPC_C_EP_MATCH_BY_BOTH 3
#define RPC_C_VERS_ALL 1
#define RPC_C_VERS_COMPATIBLE 2
#define RPC_C_VERS_EXACT 3
#define RPC_C_VERS_MAJOR_ONLY 4
#define RPC_C_VERS_UPTO 5
#define RPC_C_ERROR_STRING_LEN 256
#define RPC_C_MGMT_INQ_IF_IDS 0
#define RPC_C_MGMT_INQ_PRINC_NAME 1
#define RPC_C_MGMT_INQ_STATS 2
#define RPC_C_MGMT_IS_SERVER_LISTEN 3
#define RPC_C_MGMT_STOP_SERVER_LISTEN 4
typedef struct ept_lookup {
uint32_t inquiry_type;
uint32_t object_referent_id;
uuid_t object_uuid;
uint32_t interface_referent_id;
uuid_t interface_uuid;
uint16_t version_major;
uint16_t version_minor;
uint32_t vers_option;
uuid_t handle;
uint32_t max_entries;
} ept_lookup_t;
#define EPT_MAX_ANNOTATION_SIZE 64
typedef struct ept_entry {
uuid_t object;
int tower;
char annotation[EPT_MAX_ANNOTATION_SIZE];
} ept_entry_t, *ept_entry_p_t;
/* Internal "opaque" objects. */
typedef struct buffer {
void *data; /* SND and RCV buffers. */
size_t bufsize; /* Allocated bytes. */
size_t offset;
size_t length; /* Length of the marshalled packet. */
int eof;
uint32_t index;
} buffer_t;
/* Internal state. */
typedef struct epmap {
SOCKET sockfd;
char *server;
uint16_t port;
struct sockaddr_in sin;
buffer_t buffer[2];
uint32_t call_id;
uint32_t assoc_group; /* This is usually ignored. */
ept_lookup_handle_t handle;
int state;
p_reject_reason_t reason; /* Rejection reason code in the bind_nak PDU. */
uint32_t status; /* Run-time fault code or zero (fault PDU). */
int wsacode; /* wsa code */
} epmap_t;
/* Error and status codes. $fixme */
#define EPMAP_EOK 0x000 /* Operation completed successfully. */
#define EPMAP_ENOMEM 0x200 /* A call to malloc() failed. */
#define EPMAP_EINVAL 0x201 /* An invalid argument was passed to a library function. */
#define EPMAP_EBADPTR 0x202 /* An invalid pointer was detected. */
#define EPMAP_EWSAINIT 0x203 /* WSAStartup() initialization failed. */
#define EPMAP_EDNSFAIL 0x204 /* Could not resolve host name. */
#define EPMAP_ESOCKET 0x205 /* Could not create socket or connect to server. */
#define EPMAP_ESEND 0x206 /* A call to send() failed. */
#define EPMAP_ERECV 0x207 /* A call to recv() failed. */
#define EPMAP_EACK 0x300 /* BIND-ACK PDU */
#define EPMAP_ENAK 0x301 /* BIND-NAK PDU */
#define EPMAP_EFAULT 0x302 /* Received a FAULT PDU. */
#define EPMAP_EPROTO 0x303 /* Generic protocol error. */
#define EPMAP_ENODATA 0x304 /* Status returned by response $fixme */
#define EPMAP_EDEBUG 0x400
#define EPMAPAPI extern
EPMAPAPI void epmap_destroy(epmap_t *epmap)
{
buffer_t *buffer = NULL;
int i;
if (epmap != NULL) {
if (epmap->sockfd != INVALID_SOCKET) {
closesocket(epmap->sockfd);
WSACleanup();
}
for (i = 0; i < 2; i++) {
buffer = &epmap->buffer[i];
if (buffer->data != NULL) {
buffer->bufsize = 0;
buffer->length = 0;
free(buffer->data);
}
}
free(epmap);
}
}
EPMAPAPI epmap_t *epmap_init(size_t snd_len, size_t rcv_len)
{
epmap_t *epmap = NULL;
buffer_t *buffer = NULL;
int i;
epmap = (epmap_t *)malloc(sizeof(epmap_t));
if (epmap == NULL) {
return NULL;
}
epmap->sockfd = INVALID_SOCKET;
epmap->server = NULL;
epmap->port = 0;
epmap->call_id = 0x00000000;
/* UUID for the ept_lookup_handle generated by the endpoint mapper. */
epmap->handle.uuid.time_low = 0;
epmap->handle.uuid.time_mid = 0;
epmap->handle.uuid.time_hi_and_version = 0;
epmap->handle.uuid.clock_seq_hi_and_reserved = 0;
epmap->handle.uuid.clock_seq_low = 0;
for (i = 0; i < sizeof(epmap->handle.uuid.node); i++)
epmap->handle.uuid.node[i] = 0;
epmap->state = 0;
epmap->reason = 0;
epmap->status = 0;
epmap->wsacode = 0;
for (i = 0; i < 2; i++) {
buffer = &epmap->buffer[i];
//////buffer->bufsize = !i ? snd_len : rcv_len;
buffer->bufsize = 8192; /* Size of allocated bytes. */
buffer->data = (void *)malloc(buffer->bufsize);
if (buffer->data == NULL) {
epmap_destroy(epmap);
return NULL;
}
memset(buffer->data, '\0', buffer->bufsize);
buffer->length = 0;
buffer->offset = 0;
buffer->eof = 0;
}
return epmap;
}
static int winsock_init(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int error = 0;
wVersionRequested = MAKEWORD(2, 2);
error = WSAStartup(wVersionRequested, &wsaData);
if (error != 0)
return error;
/* Confirm that the WinSock DLL supports 2.2. */
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
WSACleanup();
return WSAVERNOTSUPPORTED;
}
return error;
}
static SOCKET create_socket(const char *server, uint16_t port, struct sockaddr_in *sin, int *ecode)
{
SOCKET SocketID = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo *ptr = NULL;
struct addrinfo hints;
int error;
char szport[5+1];
memset(&hints, '\0', sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
_snprintf(szport, 5, "%u", port);
szport[5] = '\0'; /* Redundant. */
*ecode = 0;
error = getaddrinfo(server, szport, &hints, &result);
if (error != 0) {
*ecode = WSAGetLastError();
return INVALID_SOCKET;
}
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
SocketID = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (SocketID == INVALID_SOCKET) {
*ecode = WSAGetLastError();
freeaddrinfo(result);
WSACleanup();
return INVALID_SOCKET;
}
error = connect(SocketID, ptr->ai_addr, (int)ptr->ai_addrlen);
if (error == SOCKET_ERROR) {
/* Get WSA error here before closesocket resets it. */
*ecode = WSAGetLastError();
closesocket(SocketID);
SocketID = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (SocketID == INVALID_SOCKET)
return INVALID_SOCKET;
else {
int sinlen = sizeof(struct sockaddr_in);
getpeername(SocketID, sin, &sinlen);
}
return SocketID;
}
static int epmap_connect(epmap_t *epmap, const char *server, uint16_t port)
{
int result;
if (server == NULL || strlen(server) > 128)
return EPMAP_EINVAL;
epmap->server = server;
epmap->port = port;
/* Initialize Windows Sockets. */
result = winsock_init();
if (result != 0) {
/* Use WSASetLastError? */
return EPMAP_EWSAINIT;
}
int ecode;
epmap->sockfd = create_socket(epmap->server, epmap->port, &epmap->sin, &ecode);
if (epmap->sockfd == INVALID_SOCKET) {
return ((ecode << 12) | EPMAP_ESOCKET);
}
return EPMAP_EOK;
}
static int epmap_send(epmap_t *epmap)
{
buffer_t *buffer = &epmap->buffer[0];
size_t length;
int n = 0;
length = buffer->length;
buffer->offset = 0;
while (length > 0 && n != SOCKET_ERROR) {
n = send(epmap->sockfd, (uint8_t *)buffer->data+buffer->offset, length, 0);
if (n == SOCKET_ERROR) {
return EPMAP_ESEND;
}
length -= n;
buffer->offset += n;
}
buffer->length = 0;
return EPMAP_EOK;
}
/* $fixme: Implement a proper finite state machine. */
/*****************
static int _epmap_recv(epmap_t *epmap)
{
buffer_t *buffer = &epmap->buffer[1];
int n;
int bufsize = 0;
uint8_t *ptr = NULL;
int length = 8;
ptr = (uint8_t *)buffer->data + length;
// Get at least 8 bytes, decode the PDU length to retrieve the rest of
// the packet.
bufsize = buffer->bufsize;
buffer->length = 0;
while(length > 0) {
n = recv(epmap->sockfd, buffer->data, bufsize, 0);
if (n == SOCKET_ERROR)
return SOCKET_ERROR;
buffer->length += n; Data we have in the buffer.
if (buffer->length >= 10) {
Decode length.
if (!flag) {
length = ptr[0] | ((ptr[1] << 8) & 0xff00);
bufsize = length - buffer->length; Length of PDU minus what we have buffered.
flag = 10000;
}
length -= n;
Set new length.
} else {
length -= n;
}
}
return buffer->length;
}
*************/
/* This is wrong but it will do, for now. */
static int epmap_recv(epmap_t *epmap)
{
buffer_t *buffer = &epmap->buffer[1];
int n;
n = recv(epmap->sockfd, buffer->data, buffer->bufsize, 0);
if (n == SOCKET_ERROR) {
return EPMAP_ERECV;
}
buffer->length = n;
return EPMAP_EOK;
}
static void w_byte(epmap_t *epmap, unsigned char value)
{
buffer_t *buffer = &epmap->buffer[0];
unsigned char *ptr = (unsigned char *)buffer->data;
buffer->eof = buffer->offset >= buffer->bufsize;
if (!buffer->eof) {
ptr += buffer->offset;
*ptr = value;
buffer->offset++;
ptr = NULL;
}
}
static void ndr_wle8(epmap_t *epmap, uint8_t value)
{
w_byte(epmap, value);
}
static void ndr_wle16(epmap_t *epmap, uint16_t value)
{
ndr_wle8(epmap, value & 0xff);
ndr_wle8(epmap, (value >> 8) & 0xff);
}
static void ndr_wle32(epmap_t *epmap, uint32_t value)
{
ndr_wle8(epmap, value & 0xff);
ndr_wle8(epmap, (value >> 8) & 0xff);
ndr_wle8(epmap, (value >> 16) & 0xff);
ndr_wle8(epmap, (value >> 24) & 0xff);
}
static int r_byte(epmap_t *epmap)
{
buffer_t *buffer = &epmap->buffer[1];
unsigned char *ptr = (unsigned char *)buffer->data;
int value = EOF;
buffer->eof = buffer->offset >= buffer->length;
if (!buffer->eof) {
ptr += buffer->offset;
value = *ptr;
buffer->offset++;
ptr = NULL;
}
return value;
}
static uint8_t ndr_rle8(epmap_t *epmap)
{
return (uint8_t)r_byte(epmap);
}
static uint16_t ndr_rle16(epmap_t *epmap)
{
uint16_t value;
value = (uint16_t)ndr_rle8(epmap) & 0x00ff;
value |= ((uint16_t)ndr_rle8(epmap) << 8) & 0xff00;
return value;
}
static uint32_t ndr_rle32(epmap_t *epmap)
{
uint32_t value;
value = (uint32_t)ndr_rle8(epmap) & 0x000000ff;
value |= ((uint32_t)ndr_rle8(epmap) << 8) & 0x0000ff00;
value |= ((uint32_t)ndr_rle8(epmap) << 16) & 0x00ff0000;
value |= ((uint32_t)ndr_rle8(epmap) << 24) & 0xff000000;
return value;
}
static int buffer_seek(epmap_t *epmap, int index, size_t offset, int whence)
{
buffer_t *buffer = NULL;
if (index != 0 && index != 1)
return EPMAP_EINVAL;
buffer = &epmap->buffer[index];
buffer->eof = 0;
if (whence == SEEK_SET) {
buffer->offset = offset;
} else if (whence == SEEK_CUR) {
buffer->offset += offset;
}
return EPMAP_EOK;
}
static int buffer_rewind(epmap_t *epmap)
{
buffer_t *buffer = &epmap->buffer[0];
buffer->offset = 0;
return buffer_seek(epmap, 0, 0, SEEK_SET);
}
static size_t buffer_tell(epmap_t *epmap)
{
buffer_t *buffer = &epmap->buffer[0];
return buffer->offset;
}
int ndr_encode_uuid(epmap_t *epmap, const uuid_t *uuid)
{
int i;
ndr_wle32(epmap, uuid->time_low);
ndr_wle16(epmap, uuid->time_mid);
ndr_wle16(epmap, uuid->time_hi_and_version);
ndr_wle8(epmap, uuid->clock_seq_hi_and_reserved);
ndr_wle8(epmap, uuid->clock_seq_low);
for (i = 0; i < sizeof(uuid->node); i++)
w_byte(epmap, uuid->node[i]);
return 0;
}
static int ndr_decode_uuid(epmap_t *epmap, uuid_t *uuid)
{
int i;
uuid->time_low = ndr_rle32(epmap);
uuid->time_mid = ndr_rle16(epmap);
uuid->time_hi_and_version = ndr_rle16(epmap);
uuid->clock_seq_hi_and_reserved = ndr_rle8(epmap);
uuid->clock_seq_low = ndr_rle8(epmap);
for (i = 0; i < sizeof(uuid->node); i++)
uuid->node[i] = r_byte(epmap);
return EPMAP_EOK;
}
static unsigned long _strtoul(const char *str, size_t length)
{
unsigned long value = 0;
int nibble;
int i;
for (i = 0; i < length; i++) {
if (*str >= 0x41 && *str <= 0x46)
nibble = *str - 0x41 + 0x0a;
else if (*str >= 0x61 && *str <= 0x66)
nibble = *str - 0x61 + 0x0a;
else if (*str >= 0x30 && *str <= 0x39)
nibble = *str - 0x30;
else
return value;
value = value << 4;
value |= nibble;
str++;
}
return value;
}
/* Example: "c9ac6db5-82b7-4e55-ae8a-e464ed7b4277" */
static size_t epmap_string_to_uuid(uuid_t *uuid, const char *string)
{
const char *ptr = string;
size_t length;
int i;
/* Check whether the string length is sensible. */
if (strlen(ptr) != 36)
return 0;
length = sizeof(uuid->time_low) << 1;
uuid->time_low = _strtoul(ptr, length);
ptr += length;
if (*ptr++ != '-')
return 0;
length = sizeof(uuid->time_mid) << 1;
uuid->time_mid = _strtoul(ptr, length);
ptr += length;
if (*ptr++ != '-')
return 0;
length = sizeof(uuid->time_hi_and_version) << 1;
uuid->time_hi_and_version = _strtoul(ptr, length);
ptr += length;
if (*ptr++ != '-')
return 0;
length = sizeof(uuid->clock_seq_hi_and_reserved) << 1;
uuid->clock_seq_hi_and_reserved = _strtoul(ptr, length);
ptr += length;
length = sizeof(uuid->clock_seq_low) << 1;
uuid->clock_seq_low = _strtoul(ptr, length);
ptr += length;
if (*ptr++ != '-')
return 0;
for (i = 0; i < sizeof(uuid->node); i++) {
uuid->node[i] = _strtoul(ptr, sizeof(uuid->node[i]) << 1);
ptr += sizeof(uuid->node[i]) << 1;
}
return (size_t)(ptr - string);
}
static int uuid_is_nil(uuid_t *uuid)
{
return (uuid->time_low == 0 && uuid->time_mid == 0 &&
uuid->time_hi_and_version == 0 &&
uuid->clock_seq_hi_and_reserved == 0 &&
uuid->clock_seq_low == 0 &&
uuid->node[0] == 0 && uuid->node[1] == 0 &&
uuid->node[2] == 0 && uuid->node[3] == 0 &&
uuid->node[4] == 0 && uuid->node[5] == 0);
}
static int uuid_compare(uuid_t *uuid1, uuid_t *uuid2)
{
return !(uuid1->time_low == uuid2->time_low &&
uuid1->time_mid == uuid2->time_mid &&
uuid1->time_hi_and_version == uuid2->time_hi_and_version &&
uuid1->clock_seq_hi_and_reserved == uuid2->clock_seq_hi_and_reserved &&
uuid1->clock_seq_low == uuid2->clock_seq_low &&
uuid1->node[0] == uuid2->node[0] &&
uuid1->node[1] == uuid2->node[1] &&
uuid1->node[2] == uuid2->node[2] &&
uuid1->node[3] == uuid2->node[3] &&
uuid1->node[4] == uuid2->node[4] &&
uuid1->node[5] == uuid2->node[5]);
}
/* Encode the BIND PDU to be sent to the endpoint portmapper. */
static int epmap_encode_bind(epmap_t *epmap, rpcconn_bind_hdr_t *bind)
{
buffer_t *buffer = &epmap->buffer[0];
p_cont_elem_t *p_cont_elem = NULL;
p_syntax_id_t *syntax = NULL;
uuid_t *uuid = NULL;
int i, j;
buffer_rewind(epmap);
ndr_wle8(epmap, bind->rpc_vers);
ndr_wle8(epmap, bind->rpc_vers_minor);
ndr_wle8(epmap, bind->ptype);
ndr_wle8(epmap, bind->pfc_flags);
for (i = 0; i < sizeof(bind->packed_drep); i++)
w_byte(epmap, bind->packed_drep[i]);
ndr_wle16(epmap, bind->frag_length);
ndr_wle16(epmap, bind->auth_length);
ndr_wle32(epmap, bind->call_id);
ndr_wle16(epmap, bind->max_xmit_frag);
ndr_wle16(epmap, bind->max_recv_frag);
ndr_wle32(epmap, bind->assoc_group_id);
ndr_wle8(epmap, bind->p_context_elem.n_context_elem);
ndr_wle8(epmap, bind->p_context_elem.reserved);
ndr_wle16(epmap, bind->p_context_elem.reserved2);
for (i = 0; i < bind->p_context_elem.n_context_elem; i++) {
p_cont_elem = &bind->p_context_elem.p_cont_elem[i];
ndr_wle16(epmap, p_cont_elem->p_cont_id);
ndr_wle8(epmap, p_cont_elem->n_transfer_syn);
ndr_wle8(epmap, p_cont_elem->reserved);
uuid = &p_cont_elem->abstract_syntax.if_uuid;
ndr_encode_uuid(epmap, uuid);
ndr_wle32(epmap, p_cont_elem->abstract_syntax.if_version);
for (j = 0; j < p_cont_elem->n_transfer_syn; j++) {
uuid = &p_cont_elem->transfer_syntaxes[j].if_uuid;
ndr_encode_uuid(epmap, uuid);
ndr_wle32(epmap, p_cont_elem->transfer_syntaxes[j].if_version);
}
}
buffer->length = buffer_tell(epmap);
/* Make sure we haven't attempted to write beyond the end. */
if (buffer->eof)
return 0;
/* Encode the correct PDU length. */
buffer->offset = 8;
ndr_wle16(epmap, buffer->length);
return buffer->length;
}
/* Decode the BIND ACK PDU.
* The server should accept, at most, one of the transfer syntaxes. If one of
* the client proposed transfer syntaxes matches the server's preferred transfer
* syntax, then that syntax is accepted.
*/
static int epmap_decode_bind_ack(epmap_t *epmap)
{
rpcconn_bind_ack_hdr_t ack;
p_result_t *ctx_result = NULL;
p_syntax_id_t *syntax = NULL;