2 * Unix SMB/CIFS implementation.
3 * cacusermgr main implementation.
5 * Copyright (C) Chris Nicholls 2005
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 3 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, see <http://www.gnu.org/licenses/>. */
20 #include "cacusermgr.h"
22 #define DEFAULT_MENU_LINES 15
25 void create_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
26 struct SamCreateUser cu;
27 struct SamCreateGroup cg;
32 if(!hnd || !mem_ctx || !dom_hnd) {
33 printf("No Handle to SAM.\n");
39 while(in[0] != 'c' && in[0] != 'C' && in[0] != 'q' && in[0] != 'Q') {
41 printf("[u] Create User\n");
42 printf("[g] Create Group\n");
43 printf("[m] Create Machine Account\n");
44 printf("[c] Cancel\n\n");
52 case 'u': /*create user*/
55 cu.in.dom_hnd = dom_hnd;
56 cu.in.acb_mask = ACB_NORMAL;
58 printf("Enter name: ");
60 cu.in.name = talloc_strdup(mem_ctx, tmp);
62 if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
63 printerr("Could not create user.", hnd->status);
66 user_menu(hnd, mem_ctx, dom_hnd, cu.out.user_hnd);
69 /*this will break the loop and send us back to the main menu*/
73 case 'g': /*create group*/
76 cg.in.dom_hnd = dom_hnd;
77 cg.in.access = MAXIMUM_ALLOWED_ACCESS;
79 printf("Enter name: ");
81 cg.in.name = talloc_strdup(mem_ctx, tmp);
83 if(!cac_SamCreateGroup(hnd, mem_ctx, &cg)) {
84 printerr("Could not create group.", hnd->status);
87 group_menu(hnd, mem_ctx, dom_hnd, cg.out.group_hnd);
90 /*this will break the loop and send us back to the main menu*/
94 case 'm': /*create machine account*/
97 cu.in.dom_hnd = dom_hnd;
98 cu.in.acb_mask = ACB_WSTRUST;
100 printf("Enter machine name: ");
103 /*make sure we have a $ on the end*/
104 if(tmp[strlen(tmp) - 1] != '$')
105 cu.in.name = talloc_asprintf(mem_ctx, "%s$", tmp);
107 cu.in.name = talloc_strdup(mem_ctx, tmp);
109 strlower_m(cu.in.name);
111 printf("Creating account: %s\n", cu.in.name);
113 if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
114 printerr("Could not create account.", hnd->status);
117 user_menu(hnd, mem_ctx, dom_hnd, cu.out.user_hnd);
120 /*this will break the loop and send us back to the main menu*/
132 printf("Invalid option\n");
139 void main_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
144 struct SamOpenUser openu;
145 struct SamOpenGroup openg;
146 struct SamEnumUsers enumu;
147 struct SamEnumGroups enumg;
148 struct SamFlush flush;
153 if(!hnd || !mem_ctx || !dom_hnd) {
154 printf("No handle to SAM.\n");
158 /*initialize this here and don't worry about it later*/
160 flush.in.dom_hnd = dom_hnd;
164 /*handle the menu and commands*/
165 while(in[0] != 'q' && in[0] != 'Q') {
168 printf("[o] Open User or Group\n");
169 printf("[c] Create Account or Group\n");
170 printf("[u] List Users\n");
171 printf("[g] List Groups\n");
172 printf("[m] List Machine Accounts\n");
173 printf("[q] Quit\n\n");
182 case 'o': /*open user or group*/
184 printf("Enter RID or Name: ");
185 rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &rid, &name);
187 if(rid_type == CAC_USER_RID) {
189 openu.in.dom_hnd = dom_hnd;
191 openu.in.access = MAXIMUM_ALLOWED_ACCESS;
193 if(!cac_SamOpenUser(hnd, mem_ctx, &openu))
194 printerr("Could not open user.", hnd->status);
196 user_menu(hnd, mem_ctx, dom_hnd, openu.out.user_hnd);
198 if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
199 printerr("Lost handle while flushing SAM.", hnd->status);
205 else if(rid_type == CAC_GROUP_RID) {
207 openg.in.dom_hnd = dom_hnd;
209 openg.in.access = MAXIMUM_ALLOWED_ACCESS;
211 if(!cac_SamOpenGroup(hnd, mem_ctx, &openg))
212 printerr("Could not open group.", hnd->status);
214 group_menu(hnd, mem_ctx, dom_hnd, openg.out.group_hnd);
216 if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
217 printerr("Lost handle while flushing SAM.", hnd->status);
224 printf("Unknown RID/Name.\n");
229 case 'c': /*create account/group*/
231 create_menu(hnd, mem_ctx, dom_hnd);
232 if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
233 printerr("Lost handle while flushing SAM.", hnd->status);
239 case 'u': /*list users*/
242 enumu.in.dom_hnd = dom_hnd;
243 enumu.in.acb_mask = ACB_NORMAL;
246 while(cac_SamEnumUsers(hnd, mem_ctx, &enumu)) {
247 print_rid_list(enumu.out.rids, enumu.out.names, enumu.out.num_users);
249 if(CAC_OP_FAILED(hnd->status))
250 printerr("Error occured while enumerating users.", hnd->status);
253 case 'g': /*list groups*/
256 enumg.in.dom_hnd = dom_hnd;
258 while(cac_SamEnumGroups(hnd, mem_ctx, &enumg)) {
259 print_rid_list( enumg.out.rids, enumg.out.names, enumg.out.num_groups);
262 if(CAC_OP_FAILED(hnd->status))
263 printerr("Error occured while enumerating groups.", hnd->status);
266 case 'm': /*list machine accounts*/
269 enumu.in.dom_hnd = dom_hnd;
270 enumu.in.acb_mask = ACB_WSTRUST;
273 while(cac_SamEnumUsers(hnd, mem_ctx, &enumu)) {
274 print_rid_list( enumu.out.rids, enumu.out.names, enumu.out.num_users);
276 if(CAC_OP_FAILED(hnd->status))
277 printerr("Error occured while enumerating accounts.", hnd->status);
286 printf("Invalid Command.\n");
291 int main(int argc, char **argv) {
292 CacServerHandle *hnd = NULL;
293 TALLOC_CTX *mem_ctx = NULL;
295 struct SamOpenDomain sod;
297 mem_ctx = talloc_init("cacusermgr");
299 printf("Could not initialize Talloc Context\n");
303 /**first initialize the server handle with what we have*/
304 hnd = cac_NewServerHandle(True);
306 printf("Could not create server handle\n");
310 /*fill in the blanks*/
311 if(!process_cmd_line(hnd, mem_ctx, argc, argv))
314 if(!cac_Connect(hnd, NULL)) {
315 printf("Could not connect to server %s. %s\n", hnd->server, nt_errstr(hnd->status));
319 /*open the domain sam*/
321 sod.in.access = MAXIMUM_ALLOWED_ACCESS;
323 if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
324 printf("Could not open handle to domain SAM. %s\n", nt_errstr(hnd->status));
328 main_menu(hnd, mem_ctx, sod.out.dom_hnd);
333 cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
336 cac_SamClose(hnd, mem_ctx, sod.out.sam);
340 talloc_destroy(mem_ctx);