r16176: fix a few typos I missed
[Samba/gbeck.git] / examples / libmsrpc / cacusermgr / util.c
blob1629911812149c2585241893e8a57d69ff0ecf10
1 /*
2 * Unix SMB/CIFS implementation.
3 * cacusermgr utility functions.
5 * Copyright (C) Chris Nicholls 2005
6 *
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
15 * more details.
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 /*prints usage and quits*/
24 void usage() {
25 printf("Usage:\n");
26 printf(" cacusermgr [options] server\n\n");
27 printf("options:\n");
28 printf(" -u USERNAME Username to login with\n");
29 printf(" -d/-w DOMAIN Domain name\n");
30 printf(" -D LEVEL Debug level\n");
31 printf(" -h Print this message\n");
33 exit(1);
36 /*initializes values in the server handle from the command line returns 0 if there is a problem, non-zero if everything is ok*/
37 int process_cmd_line(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, int argc, char **argv) {
38 char op;
40 if(!hnd || !mem_ctx || !argc)
41 return 0;
43 while( (op = getopt(argc, argv, "u:U:d:w:W:D:h")) != -1) {
44 switch(op) {
45 case 'u': /*username*/
46 case 'U':
47 if(optarg)
48 strncpy(hnd->username, optarg, sizeof(fstring));
49 else
50 usage();
51 break;
53 case 'd': /*domain name*/
54 case 'w':
55 case 'W':
56 if(optarg)
57 strncpy(hnd->domain, optarg, sizeof(fstring));
58 else
59 usage();
60 break;
62 case 'D': /*debug level*/
63 if(optarg)
64 hnd->debug = atoi(optarg);
65 else
66 usage();
67 break;
69 case 'h': /*help*/
70 usage();
71 break;
73 case '?':
74 default:
75 printf("Unknown option -%c\n", op);
76 usage();
80 if(optind >= argc)
81 usage();
83 /*whatever is less should be the server*/
84 strncpy(hnd->server, argv[optind], sizeof(fstring));
86 return 1;
89 void mgr_getline(fstring line) {
91 fgets(line, sizeof(fstring), stdin);
93 if(line[strlen(line) - 1] == '\n')
94 line[strlen(line) - 1] = '\0';
98 /*this is pretty similar to the other get_auth_data_fn's*/
99 void mgr_GetAuthDataFn(const char * pServer,
100 const char * pShare,
101 char * pWorkgroup,
102 int maxLenWorkgroup,
103 char * pUsername,
104 int maxLenUsername,
105 char * pPassword,
106 int maxLenPassword)
109 char temp[sizeof(fstring)];
111 static char authUsername[sizeof(fstring)];
112 static char authWorkgroup[sizeof(fstring)];
113 static char authPassword[sizeof(fstring)];
114 static char authSet = 0;
116 char *pass = NULL;
118 if (authSet)
120 strncpy(pWorkgroup, authWorkgroup, maxLenWorkgroup - 1);
121 strncpy(pUsername, authUsername, maxLenUsername - 1);
122 strncpy(pPassword, authPassword, maxLenPassword - 1);
124 else
126 if(pWorkgroup[0] != '\0') {
127 strncpy(authWorkgroup, pWorkgroup, maxLenWorkgroup - 1);
129 else {
130 d_printf("Domain: [%s] ", pWorkgroup);
131 mgr_getline(pWorkgroup);
133 if (temp[0] != '\0')
135 strncpy(pWorkgroup, temp, maxLenWorkgroup - 1);
136 strncpy(authWorkgroup, temp, maxLenWorkgroup - 1);
141 if(pUsername[0] != '\0') {
142 strncpy(authUsername, pUsername, maxLenUsername - 1);
144 else {
145 d_printf("Username: [%s] ", pUsername);
146 mgr_getline(pUsername);
148 if (temp[strlen(temp) - 1] == '\n') /* A new line? */
150 temp[strlen(temp) - 1] = '\0';
153 if (temp[0] != '\0')
155 strncpy(pUsername, temp, maxLenUsername - 1);
156 strncpy(authUsername, pUsername, maxLenUsername - 1);
159 if(pPassword[0] != '\0') {
160 strncpy(authPassword, pPassword, maxLenPassword - 1);
162 else {
163 pass = getpass("Password: ");
164 if (pass)
165 fstrcpy(temp, pass);
166 if (temp[strlen(temp) - 1] == '\n') /* A new line? */
168 temp[strlen(temp) - 1] = '\0';
170 if (temp[0] != '\0')
172 strncpy(pPassword, temp, maxLenPassword - 1);
173 strncpy(authPassword, pPassword, maxLenPassword - 1);
176 authSet = 1;
180 void mgr_page(uint32 line_count) {
182 if( (line_count % DEFAULT_SCREEN_LINES) != 0)
183 return;
185 printf("--Press enter to continue--\n");
186 getchar();
189 /*reads a line from stdin, figures out if it is a RID or name, gets a CacLookupRidsRecord and then returns the type*/
190 uint32 rid_or_name(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd, uint32 *rid, char **name) {
191 fstring line;
193 BOOL is_rid = False;
194 uint32 rid_type = 0;
196 struct SamGetNamesFromRids getnames;
197 struct SamGetRidsFromNames getrids;
199 mgr_getline(line);
201 if(strncmp(line, "0x", 2) == 0) {
202 /*then this is a RID*/
203 sscanf( (line + 2), "%x", rid);
204 is_rid = True;
206 else {
207 /*then this is a name*/
208 *name = talloc_strdup(mem_ctx, line);
211 if(is_rid) {
212 ZERO_STRUCT(getnames);
214 getnames.in.dom_hnd = dom_hnd;
215 getnames.in.rids = rid;
216 getnames.in.num_rids = 1;
218 cac_SamGetNamesFromRids(hnd, mem_ctx, &getnames);
220 if(getnames.out.num_names > 0)
221 rid_type = getnames.out.map[0].type;
224 else {
225 ZERO_STRUCT(getrids);
227 getrids.in.dom_hnd = dom_hnd;
228 getrids.in.names = name;
229 getrids.in.num_names = 1;
231 cac_SamGetRidsFromNames(hnd, mem_ctx, &getrids);
233 if(getrids.out.num_rids > 0) {
234 rid_type = getrids.out.map[0].type;
236 /*send back the RID so cac_SamOpenXX() doesn't have to look it up*/
237 *rid = getrids.out.map[0].rid;
241 return rid_type;
244 /*print's out some common error messages*/
245 void printerr(const char *msg, NTSTATUS status) {
246 if(NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED))
247 printf("%s You do not have sufficient rights.\n", msg);
249 else if(NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER))
250 printf("%s No such user.\n", msg);
252 else if(NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_GROUP))
253 printf("%s No such group.\n", msg);
255 else if(NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS))
256 printf("%s User already exists.\n", msg);
258 else if(NT_STATUS_EQUAL(status, NT_STATUS_GROUP_EXISTS))
259 printf("%s Group already exists.\n", msg);
261 else
262 printf("%s %s.\n", msg, nt_errstr(status));
265 char *get_new_password(TALLOC_CTX *mem_ctx) {
266 char *pass1 = NULL;
268 pass1 = getpass("Enter new password: ");
270 return talloc_strdup(mem_ctx, pass1);
273 void print_rid_list(uint32 *rids, char **names, uint32 num_rids) {
274 uint32 i = 0;
276 if(!names || !rids)
277 return;
279 printf(" RID Name\n");
281 while(i < num_rids) {
282 printf("[0x%x] [%s]\n", rids[i], names[i]);
284 i++;
286 mgr_page(i);
290 void print_lookup_records(CacLookupRidsRecord *map, uint32 num_rids) {
291 uint32 i = 0;
293 if(!map)
294 return;
296 printf("RID Name\n");
298 while(i < num_rids) {
299 if(map[i].found) {
300 printf("[0x%x] [%s]\n", map[i].rid, map[i].name);
303 i++;
305 mgr_page(i);
309 int list_groups(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
310 struct SamEnumGroups eg;
312 if(!hnd || !mem_ctx || !dom_hnd)
313 return 0;
315 ZERO_STRUCT(eg);
316 eg.in.dom_hnd = dom_hnd;
318 while(cac_SamEnumGroups(hnd, mem_ctx, &eg))
319 print_rid_list(eg.out.rids, eg.out.names, eg.out.num_groups);
321 if(CAC_OP_FAILED(hnd->status)) {
322 printerr("Could not enumerate groups.", hnd->status);
323 return 0;
326 return 1;
329 void list_users(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
330 struct SamEnumUsers eu;
332 if(!hnd || !mem_ctx || !dom_hnd)
333 return;
335 ZERO_STRUCT(eu);
336 eu.in.dom_hnd = dom_hnd;
338 while(cac_SamEnumUsers(hnd, mem_ctx, &eu))
339 print_rid_list(eu.out.rids, eu.out.names, eu.out.num_users);
341 if(CAC_OP_FAILED(hnd->status))
342 printerr("Could not enumerate users.", hnd->status);