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 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 /*prints usage and quits*/
26 printf(" cacusermgr [options] server\n\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");
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
) {
40 if(!hnd
|| !mem_ctx
|| !argc
)
43 while( (op
= getopt(argc
, argv
, "u:U:d:w:W:D:h")) != -1) {
45 case 'u': /*username*/
48 strncpy(hnd
->username
, optarg
, sizeof(fstring
));
53 case 'd': /*domain name*/
57 strncpy(hnd
->domain
, optarg
, sizeof(fstring
));
62 case 'D': /*debug level*/
64 hnd
->debug
= atoi(optarg
);
75 printf("Unknown option -%c\n", op
);
83 /*whatever is less should be the server*/
84 strncpy(hnd
->server
, argv
[optind
], sizeof(fstring
));
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
,
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;
120 strncpy(pWorkgroup
, authWorkgroup
, maxLenWorkgroup
- 1);
121 strncpy(pUsername
, authUsername
, maxLenUsername
- 1);
122 strncpy(pPassword
, authPassword
, maxLenPassword
- 1);
126 if(pWorkgroup
[0] != '\0') {
127 strncpy(authWorkgroup
, pWorkgroup
, maxLenWorkgroup
- 1);
130 d_printf("Domain: [%s] ", pWorkgroup
);
131 mgr_getline(pWorkgroup
);
135 strncpy(pWorkgroup
, temp
, maxLenWorkgroup
- 1);
136 strncpy(authWorkgroup
, temp
, maxLenWorkgroup
- 1);
141 if(pUsername
[0] != '\0') {
142 strncpy(authUsername
, pUsername
, maxLenUsername
- 1);
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';
155 strncpy(pUsername
, temp
, maxLenUsername
- 1);
156 strncpy(authUsername
, pUsername
, maxLenUsername
- 1);
159 if(pPassword
[0] != '\0') {
160 strncpy(authPassword
, pPassword
, maxLenPassword
- 1);
163 pass
= getpass("Password: ");
166 if (temp
[strlen(temp
) - 1] == '\n') /* A new line? */
168 temp
[strlen(temp
) - 1] = '\0';
172 strncpy(pPassword
, temp
, maxLenPassword
- 1);
173 strncpy(authPassword
, pPassword
, maxLenPassword
- 1);
180 void mgr_page(uint32 line_count
) {
182 if( (line_count
% DEFAULT_SCREEN_LINES
) != 0)
185 printf("--Press enter to continue--\n");
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
) {
196 struct SamGetNamesFromRids getnames
;
197 struct SamGetRidsFromNames getrids
;
201 if(strncmp(line
, "0x", 2) == 0) {
202 /*then this is a RID*/
203 sscanf( (line
+ 2), "%x", rid
);
207 /*then this is a name*/
208 *name
= talloc_strdup(mem_ctx
, line
);
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
;
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
;
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
);
262 printf("%s %s.\n", msg
, nt_errstr(status
));
265 char *get_new_password(TALLOC_CTX
*mem_ctx
) {
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
) {
279 printf(" RID Name\n");
281 while(i
< num_rids
) {
282 printf("[0x%x] [%s]\n", rids
[i
], names
[i
]);
290 void print_lookup_records(CacLookupRidsRecord
*map
, uint32 num_rids
) {
296 printf("RID Name\n");
298 while(i
< num_rids
) {
300 printf("[0x%x] [%s]\n", map
[i
].rid
, map
[i
].name
);
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
)
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
);
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
)
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
);