xref: /illumos-gate/usr/src/test/smbsrv-tests/tests/smb_sid/large_sids.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2022 Tintri by DDN, Inc. All rights reserved.
14  */
15 
16 /*
17  * Test usr/src/common/smbsrv/smb_sid.c with large SIDs
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <smbsrv/smb_sid.h>
24 #include <limits.h>
25 
26 void
27 test_sid(const char *sidstr, uint8_t idauth, const uint32_t *subauths,
28     size_t subauth_cnt)
29 {
30 	char newstr[1024];
31 	smb_sid_t *sid;
32 	int i;
33 
34 	sid = smb_sid_fromstr(sidstr);
35 	if (!smb_sid_isvalid(sid)) {
36 		fprintf(stderr, "SID %s not valid: %p\n", sidstr, sid);
37 		exit(1);
38 	}
39 
40 	smb_sid_tostr(sid, newstr);
41 
42 	if (strncmp(sidstr, newstr, sizeof (newstr)) != 0) {
43 		fprintf(stderr, "SID %s did not match decoded SID %s\n",
44 		    sidstr, newstr);
45 		exit(5);
46 	}
47 
48 	if (subauths == NULL) {
49 		smb_sid_free(sid);
50 		return;
51 	}
52 
53 	if (sid->sid_authority[5] != idauth) {
54 		fprintf(stderr, "Wrong SID authority %u (expected %u): %s\n",
55 		    sid->sid_authority, idauth, sidstr);
56 		exit(2);
57 	}
58 
59 	if (sid->sid_subauthcnt != subauth_cnt) {
60 		fprintf(stderr, "Wrong subauthcnt %u (expected %u): %s\n",
61 		    sid->sid_subauthcnt, subauth_cnt, sidstr);
62 		exit(3);
63 	}
64 
65 	for (i = 0; i < subauth_cnt; i++) {
66 		if (sid->sid_subauth[i] != subauths[i]) {
67 			fprintf(stderr,
68 			    "Wrong subauthcnt %u (expected %u): %s\n",
69 			    sid->sid_subauthcnt, subauth_cnt, sidstr);
70 			exit(4);
71 		}
72 	}
73 
74 	smb_sid_free(sid);
75 }
76 
77 int
78 main(int argc, char *argv[])
79 {
80 	char sid[1024];
81 	uint32_t subauths[NT_SID_SUBAUTH_MAX];
82 	size_t len = sizeof (sid);
83 	int off = 0;
84 	int i, idauth;
85 
86 	if (argc > 1) {
87 		test_sid(argv[1], 0, NULL, 0);
88 		goto out;
89 	}
90 
91 	for (idauth = 2; idauth <= UINT8_MAX; idauth += 11) {
92 		off = snprintf(&sid[0], len, "S-1-%u", idauth);
93 		for (i = 0; i < NT_SID_SUBAUTH_MAX; i++) {
94 			subauths[i] = arc4random();
95 			off += snprintf(&sid[off], len - off,
96 			    "-%u", subauths[i]);
97 		}
98 		test_sid(sid, idauth, subauths, NT_SID_SUBAUTH_MAX);
99 	}
100 
101 out:
102 	printf("success!\n");
103 	return (0);
104 }
105