2 * Unix SMB/CIFS implementation.
3 * cacusermgr utility functions.
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 /*prints usage and quits*/
25 printf(" cacusermgr [options] server\n\n");
27 printf(" -u USERNAME Username to login with\n");
28 printf(" -d/-w DOMAIN Domain name\n");
29 printf(" -D LEVEL Debug level\n");
30 printf(" -h Print this message\n");
35 /*initializes values in the server handle from the command line returns 0 if there is a problem, non-zero if everything is ok*/
36 int process_cmd_line(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, int argc, char **argv) {
39 if(!hnd || !mem_ctx || !argc)
42 while( (op = getopt(argc, argv, "u:U:d:w:W:D:h")) != -1) {
44 case 'u': /*username*/
47 strncpy(hnd->username, optarg, sizeof(fstring));
52 case 'd': /*domain name*/
56 strncpy(hnd->domain, optarg, sizeof(fstring));
61 case 'D': /*debug level*/
63 hnd->debug = atoi(optarg);
74 printf("Unknown option -%c\n", op);
82 /*whatever is less should be the server*/
83 strncpy(hnd->server, argv[optind], sizeof(fstring));
88 void mgr_getline(fstring line) {
90 fgets(line, sizeof(fstring), stdin);
92 if(line[strlen(line) - 1] == '\n')
93 line[strlen(line) - 1] = '\0';
97 /*this is pretty similar to the other get_auth_data_fn's*/
98 void mgr_GetAuthDataFn(const char * pServer,
108 char temp[sizeof(fstring)];
110 static char authUsername[sizeof(fstring)];
111 static char authWorkgroup[sizeof(fstring)];
112 static char authPassword[sizeof(fstring)];
113 static char authSet = 0;
119 strncpy(pWorkgroup, authWorkgroup, maxLenWorkgroup - 1);
120 strncpy(pUsername, authUsername, maxLenUsername - 1);
121 strncpy(pPassword, authPassword, maxLenPassword - 1);
125 if(pWorkgroup[0] != '\0') {
126 strncpy(authWorkgroup, pWorkgroup, maxLenWorkgroup - 1);
129 d_printf("Domain: [%s] ", pWorkgroup);
130 mgr_getline(pWorkgroup);
134 strncpy(pWorkgroup, temp, maxLenWorkgroup - 1);
135 strncpy(authWorkgroup, temp, maxLenWorkgroup - 1);
140 if(pUsername[0] != '\0') {
141 strncpy(authUsername, pUsername, maxLenUsername - 1);
144 d_printf("Username: [%s] ", pUsername);
145 mgr_getline(pUsername);
147 if (temp[strlen(temp) - 1] == '\n') /* A new line? */
149 temp[strlen(temp) - 1] = '\0';
154 strncpy(pUsername, temp, maxLenUsername - 1);
155 strncpy(authUsername, pUsername, maxLenUsername - 1);
158 if(pPassword[0] != '\0') {
159 strncpy(authPassword, pPassword, maxLenPassword - 1);
162 pass = getpass("Password: ");
165 if (temp[strlen(temp) - 1] == '\n') /* A new line? */
167 temp[strlen(temp) - 1] = '\0';
171 strncpy(pPassword, temp, maxLenPassword - 1);
172 strncpy(authPassword, pPassword, maxLenPassword - 1);
179 void mgr_page(uint32 line_count) {
181 if( (line_count % DEFAULT_SCREEN_LINES) != 0)
184 printf("--Press enter to continue--\n");
188 /*reads a line from stdin, figures out if it is a RID or name, gets a CacLookupRidsRecord and then returns the type*/
189 uint32 rid_or_name(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd, uint32 *rid, char **name) {
195 struct SamGetNamesFromRids getnames;
196 struct SamGetRidsFromNames getrids;
200 if(strncmp(line, "0x", 2) == 0) {
201 /*then this is a RID*/
202 sscanf( (line + 2), "%x", rid);
206 /*then this is a name*/
207 *name = talloc_strdup(mem_ctx, line);
211 ZERO_STRUCT(getnames);
213 getnames.in.dom_hnd = dom_hnd;
214 getnames.in.rids = rid;
215 getnames.in.num_rids = 1;
217 cac_SamGetNamesFromRids(hnd, mem_ctx, &getnames);
219 if(getnames.out.num_names > 0)
220 rid_type = getnames.out.map[0].type;
224 ZERO_STRUCT(getrids);
226 getrids.in.dom_hnd = dom_hnd;
227 getrids.in.names = name;
228 getrids.in.num_names = 1;
230 cac_SamGetRidsFromNames(hnd, mem_ctx, &getrids);
232 if(getrids.out.num_rids > 0) {
233 rid_type = getrids.out.map[0].type;
235 /*send back the RID so cac_SamOpenXX() doesn't have to look it up*/
236 *rid = getrids.out.map[0].rid;
243 /*print's out some common error messages*/
244 void printerr(const char *msg, NTSTATUS status) {
245 if(NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED))
246 printf("%s You do not have sufficient rights.\n", msg);
248 else if(NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER))
249 printf("%s No such user.\n", msg);
251 else if(NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_GROUP))
252 printf("%s No such group.\n", msg);
254 else if(NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS))
255 printf("%s User already exists.\n", msg);
257 else if(NT_STATUS_EQUAL(status, NT_STATUS_GROUP_EXISTS))
258 printf("%s Group already exists.\n", msg);
261 printf("%s %s.\n", msg, nt_errstr(status));
264 char *get_new_password(TALLOC_CTX *mem_ctx) {
267 pass1 = getpass("Enter new password: ");
269 return talloc_strdup(mem_ctx, pass1);
272 void print_rid_list(uint32 *rids, char **names, uint32 num_rids) {
278 printf(" RID Name\n");
280 while(i < num_rids) {
281 printf("[0x%x] [%s]\n", rids[i], names[i]);
289 void print_lookup_records(CacLookupRidsRecord *map, uint32 num_rids) {
295 printf("RID Name\n");
297 while(i < num_rids) {
299 printf("[0x%x] [%s]\n", map[i].rid, map[i].name);
308 int list_groups(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
309 struct SamEnumGroups eg;
311 if(!hnd || !mem_ctx || !dom_hnd)
315 eg.in.dom_hnd = dom_hnd;
317 while(cac_SamEnumGroups(hnd, mem_ctx, &eg))
318 print_rid_list(eg.out.rids, eg.out.names, eg.out.num_groups);
320 if(CAC_OP_FAILED(hnd->status)) {
321 printerr("Could not enumerate groups.", hnd->status);
328 void list_users(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
329 struct SamEnumUsers eu;
331 if(!hnd || !mem_ctx || !dom_hnd)
335 eu.in.dom_hnd = dom_hnd;
337 while(cac_SamEnumUsers(hnd, mem_ctx, &eu))
338 print_rid_list(eu.out.rids, eu.out.names, eu.out.num_users);
340 if(CAC_OP_FAILED(hnd->status))
341 printerr("Could not enumerate users.", hnd->status);