1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetLocalGroupDelMembers query
4  *  Copyright (C) Guenther Deschner 2008
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <sys/types.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <netapi.h>
27 
28 #include "common.h"
29 
main(int argc,const char ** argv)30 int main(int argc, const char **argv)
31 {
32 	NET_API_STATUS status;
33 	struct libnetapi_ctx *ctx = NULL;
34 	const char *hostname = NULL;
35 	const char *groupname = NULL;
36 	struct LOCALGROUP_MEMBERS_INFO_0 *g0;
37 	struct LOCALGROUP_MEMBERS_INFO_3 *g3;
38 	uint32_t total_entries = 0;
39 	uint8_t *buffer = NULL;
40 	uint32_t level = 3;
41 	const char **names = NULL;
42 	int i = 0;
43 
44 	poptContext pc;
45 	int opt;
46 
47 	struct poptOption long_options[] = {
48 		POPT_AUTOHELP
49 		POPT_COMMON_LIBNETAPI_EXAMPLES
50 		POPT_TABLEEND
51 	};
52 
53 	status = libnetapi_init(&ctx);
54 	if (status != 0) {
55 		return status;
56 	}
57 
58 	pc = poptGetContext("localgroup_delmembers", argc, argv, long_options, 0);
59 
60 	poptSetOtherOptionHelp(pc, "hostname groupname member1 member2 ...");
61 	while((opt = poptGetNextOpt(pc)) != -1) {
62 	}
63 
64 	if (!poptPeekArg(pc)) {
65 		poptPrintHelp(pc, stderr, 0);
66 		goto out;
67 	}
68 	hostname = poptGetArg(pc);
69 
70 	if (!poptPeekArg(pc)) {
71 		poptPrintHelp(pc, stderr, 0);
72 		goto out;
73 	}
74 	groupname = poptGetArg(pc);
75 
76 	if (!poptPeekArg(pc)) {
77 		poptPrintHelp(pc, stderr, 0);
78 		goto out;
79 	}
80 
81 	names = poptGetArgs(pc);
82 	for (i=0; names[i] != NULL; i++) {
83 		total_entries++;
84 	}
85 
86 	switch (level) {
87 		case 0:
88 			status = NetApiBufferAllocate(sizeof(struct LOCALGROUP_MEMBERS_INFO_0) * total_entries,
89 						      (void **)&g0);
90 			if (status) {
91 				printf("NetApiBufferAllocate failed with: %s\n",
92 					libnetapi_get_error_string(ctx, status));
93 				goto out;
94 			}
95 
96 			for (i=0; i<total_entries; i++) {
97 				if (!ConvertStringSidToSid(names[i], &g0[i].lgrmi0_sid)) {
98 					printf("could not convert sid\n");
99 					goto out;
100 				}
101 			}
102 
103 			buffer = (uint8_t *)g0;
104 			break;
105 		case 3:
106 			status = NetApiBufferAllocate(sizeof(struct LOCALGROUP_MEMBERS_INFO_3) * total_entries,
107 						      (void **)&g3);
108 			if (status) {
109 				printf("NetApiBufferAllocate failed with: %s\n",
110 					libnetapi_get_error_string(ctx, status));
111 				goto out;
112 			}
113 
114 			for (i=0; i<total_entries; i++) {
115 				g3[i].lgrmi3_domainandname = names[i];
116 			}
117 
118 			buffer = (uint8_t *)g3;
119 			break;
120 		default:
121 			break;
122 	}
123 
124 	/* NetLocalGroupDelMembers */
125 
126 	status = NetLocalGroupDelMembers(hostname,
127 					 groupname,
128 					 level,
129 					 buffer,
130 					 total_entries);
131 	if (status != 0) {
132 		printf("NetLocalGroupDelMembers failed with: %s\n",
133 			libnetapi_get_error_string(ctx, status));
134 	}
135 
136  out:
137 	libnetapi_free(ctx);
138 	poptFreeContext(pc);
139 
140 	return status;
141 }
142