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 2 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, write to the Free Software Foundation, Inc., 675
19 * Mass Ave, Cambridge, MA 02139, USA. */
21 #include "cacusermgr.h"
23 #define DEFAULT_MENU_LINES 15
26 void create_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
27 struct SamCreateUser cu;
28 struct SamCreateGroup cg;
33 if(!hnd || !mem_ctx || !dom_hnd) {
34 printf("No Handle to SAM.\n");
40 while(in[0] != 'c' && in[0] != 'C' && in[0] != 'q' && in[0] != 'Q') {
42 printf("[u] Create User\n");
43 printf("[g] Create Group\n");
44 printf("[m] Create Machine Account\n");
45 printf("[c] Cancel\n\n");
53 case 'u': /*create user*/
56 cu.in.dom_hnd = dom_hnd;
57 cu.in.acb_mask = ACB_NORMAL;
59 printf("Enter name: ");
61 cu.in.name = talloc_strdup(mem_ctx, tmp);
63 if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
64 printerr("Could not create user.", hnd->status);
67 user_menu(hnd, mem_ctx, dom_hnd, cu.out.user_hnd);
70 /*this will break the loop and send us back to the main menu*/
74 case 'g': /*create group*/
77 cg.in.dom_hnd = dom_hnd;
78 cg.in.access = MAXIMUM_ALLOWED_ACCESS;
80 printf("Enter name: ");
82 cg.in.name = talloc_strdup(mem_ctx, tmp);
84 if(!cac_SamCreateGroup(hnd, mem_ctx, &cg)) {
85 printerr("Could not create group.", hnd->status);
88 group_menu(hnd, mem_ctx, dom_hnd, cg.out.group_hnd);
91 /*this will break the loop and send us back to the main menu*/
95 case 'm': /*create machine account*/
98 cu.in.dom_hnd = dom_hnd;
99 cu.in.acb_mask = ACB_WSTRUST;
101 printf("Enter machine name: ");
104 /*make sure we have a $ on the end*/
105 if(tmp[strlen(tmp) - 1] != '$')
106 cu.in.name = talloc_asprintf(mem_ctx, "%s$", tmp);
108 cu.in.name = talloc_strdup(mem_ctx, tmp);
110 strlower_m(cu.in.name);
112 printf("Creating account: %s\n", cu.in.name);
114 if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
115 printerr("Could not create account.", hnd->status);
118 user_menu(hnd, mem_ctx, dom_hnd, cu.out.user_hnd);
121 /*this will break the loop and send us back to the main menu*/
133 printf("Invalid option\n");
140 void main_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
145 struct SamOpenUser openu;
146 struct SamOpenGroup openg;
147 struct SamEnumUsers enumu;
148 struct SamEnumGroups enumg;
149 struct SamFlush flush;
154 if(!hnd || !mem_ctx || !dom_hnd) {
155 printf("No handle to SAM.\n");
159 /*initialize this here and don't worry about it later*/
161 flush.in.dom_hnd = dom_hnd;
165 /*handle the menu and commands*/
166 while(in[0] != 'q' && in[0] != 'Q') {
169 printf("[o] Open User or Group\n");
170 printf("[c] Create Account or Group\n");
171 printf("[u] List Users\n");
172 printf("[g] List Groups\n");
173 printf("[m] List Machine Accounts\n");
174 printf("[q] Quit\n\n");
183 case 'o': /*open user or group*/
185 printf("Enter RID or Name: ");
186 rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &rid, &name);
188 if(rid_type == CAC_USER_RID) {
190 openu.in.dom_hnd = dom_hnd;
192 openu.in.access = MAXIMUM_ALLOWED_ACCESS;
194 if(!cac_SamOpenUser(hnd, mem_ctx, &openu))
195 printerr("Could not open user.", hnd->status);
197 user_menu(hnd, mem_ctx, dom_hnd, openu.out.user_hnd);
199 if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
200 printerr("Lost handle while flushing SAM.", hnd->status);
206 else if(rid_type == CAC_GROUP_RID) {
208 openg.in.dom_hnd = dom_hnd;
210 openg.in.access = MAXIMUM_ALLOWED_ACCESS;
212 if(!cac_SamOpenGroup(hnd, mem_ctx, &openg))
213 printerr("Could not open group.", hnd->status);
215 group_menu(hnd, mem_ctx, dom_hnd, openg.out.group_hnd);
217 if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
218 printerr("Lost handle while flushing SAM.", hnd->status);
225 printf("Unknown RID/Name.\n");
230 case 'c': /*create account/group*/
232 create_menu(hnd, mem_ctx, dom_hnd);
233 if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
234 printerr("Lost handle while flushing SAM.", hnd->status);
240 case 'u': /*list users*/
243 enumu.in.dom_hnd = dom_hnd;
244 enumu.in.acb_mask = ACB_NORMAL;
247 while(cac_SamEnumUsers(hnd, mem_ctx, &enumu)) {
248 print_rid_list(enumu.out.rids, enumu.out.names, enumu.out.num_users);
250 if(CAC_OP_FAILED(hnd->status))
251 printerr("Error occured while enumerating users.", hnd->status);
254 case 'g': /*list groups*/
257 enumg.in.dom_hnd = dom_hnd;
259 while(cac_SamEnumGroups(hnd, mem_ctx, &enumg)) {
260 print_rid_list( enumg.out.rids, enumg.out.names, enumg.out.num_groups);
263 if(CAC_OP_FAILED(hnd->status))
264 printerr("Error occured while enumerating groups.", hnd->status);
267 case 'm': /*list machine accounts*/
270 enumu.in.dom_hnd = dom_hnd;
271 enumu.in.acb_mask = ACB_WSTRUST;
274 while(cac_SamEnumUsers(hnd, mem_ctx, &enumu)) {
275 print_rid_list( enumu.out.rids, enumu.out.names, enumu.out.num_users);
277 if(CAC_OP_FAILED(hnd->status))
278 printerr("Error occured while enumerating accounts.", hnd->status);
287 printf("Invalid Command.\n");
292 int main(int argc, char **argv) {
293 CacServerHandle *hnd = NULL;
294 TALLOC_CTX *mem_ctx = NULL;
296 struct SamOpenDomain sod;
298 mem_ctx = talloc_init("cacusermgr");
300 printf("Could not initialize Talloc Context\n");
304 /**first initialize the server handle with what we have*/
305 hnd = cac_NewServerHandle(True);
307 printf("Could not create server handle\n");
311 /*fill in the blanks*/
312 if(!process_cmd_line(hnd, mem_ctx, argc, argv))
315 if(!cac_Connect(hnd, NULL)) {
316 printf("Could not connect to server %s. %s\n", hnd->server, nt_errstr(hnd->status));
320 /*open the domain sam*/
322 sod.in.access = MAXIMUM_ALLOWED_ACCESS;
324 if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
325 printf("Could not open handle to domain SAM. %s\n", nt_errstr(hnd->status));
329 main_menu(hnd, mem_ctx, sod.out.dom_hnd);
334 cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
337 cac_SamClose(hnd, mem_ctx, sod.out.sam);
341 talloc_destroy(mem_ctx);