Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / libadmin / test / afscp.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 *
9 * Portions Copyright (c) 2003 Apple Computer, Inc.
10 */
11
12 /* Test driver for admin functions. */
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16 #include <afs/stds.h>
17
18 #include <roken.h>
19
20 #include <pthread.h>
21
22 #include <rx/rx.h>
23 #include <rx/rxstat.h>
24
25 #include <afs/afs_Admin.h>
26 #include <afs/afs_utilAdmin.h>
27 #include <afs/afs_clientAdmin.h>
28
29 #include <afs/cellconfig.h>
30 #include <afs/cmd.h>
31
32 #include "common.h"
33 #include "bos.h"
34 #include "client.h"
35 #include "kas.h"
36 #include "pts.h"
37 #include "util.h"
38 #include "vos.h"
39
40 /*
41 * Globals
42 */
43
44 void *cellHandle;
45 void *tokenHandle;
46 int existing_tokens = 0;
47
48 /*
49 * Before processing any command, process the common arguments and
50 * get an appropriate cell handle
51 */
52
53 static int
54 MyBeforeProc(struct cmd_syndesc *as, void *arock)
55 {
56 afs_status_t st = 0;
57 int no_auth = 0;
58 char auth_cell[MAXCELLCHARS];
59 char exec_cell[MAXCELLCHARS];
60
61 /*
62 * Check what kind of authentication is necessary based upon
63 * the arguments passed
64 */
65
66 /*
67 * Check for noauth first
68 */
69
70 if (as->parms[NOAUTH_PARAM].items) {
71 no_auth = 1;
72 if (as->parms[USER_PARAM].items) {
73 ERR_EXT("you can't specify both -noauth and -authuser");
74 }
75 if (as->parms[PASSWORD_PARAM].items) {
76 ERR_EXT("you can't specify both -noauth and -authpassword");
77 }
78 if (as->parms[AUTHCELL_PARAM].items) {
79 ERR_EXT("you can't specify both -noauth and -authcell");
80 }
81 if (as->parms[USEEXISTTOKENS_PARAM].items) {
82 ERR_EXT("you can't specify both -noauth and -usetokens");
83 }
84 }
85
86 /*
87 * Check for usetokens
88 */
89
90 if (as->parms[USEEXISTTOKENS_PARAM].items) {
91 existing_tokens = 1;
92 if (as->parms[USER_PARAM].items) {
93 ERR_EXT("you can't specify both -usetokens and -authuser");
94 }
95 if (as->parms[PASSWORD_PARAM].items) {
96 ERR_EXT("you can't specify both -usetokens and -authpassword");
97 }
98 }
99
100 /*
101 * Check for user name password and auth cell
102 * It's ok to specify user name and password, but not auth cell
103 * in that case, we assume that the auth cell is the local cell.
104 */
105
106 if (as->parms[USER_PARAM].items) {
107 if (!as->parms[PASSWORD_PARAM].items) {
108 ERR_EXT
109 ("you must specify -authpassword if you specify -authuser");
110 }
111 if (as->parms[AUTHCELL_PARAM].items) {
112 strcpy(auth_cell, as->parms[AUTHCELL_PARAM].items->data);
113 } else {
114 if (!afsclient_LocalCellGet(auth_cell, &st)) {
115 ERR_ST_EXT("can't get local cell name", st);
116 }
117 }
118 }
119
120 /*
121 * Get the execution cell. If this parameter wasn't passed, we
122 * assume the command should execute in the local cell.
123 */
124
125 if (as->parms[EXECCELL_PARAM].items) {
126 strcpy(exec_cell, as->parms[EXECCELL_PARAM].items->data);
127 } else {
128 if (!afsclient_LocalCellGet(exec_cell, &st)) {
129 ERR_ST_EXT("can't get local cell name", st);
130 }
131 }
132
133 /*
134 * Get a token handle and a cell handle for this invocation
135 */
136
137 if (no_auth) {
138 if (!afsclient_TokenGetNew
139 (auth_cell, (const char *)0, (const char *)0, &tokenHandle,
140 &st)) {
141 ERR_ST_EXT("can't get noauth tokens", st);
142 }
143 } else if (existing_tokens) {
144 if (as->parms[AUTHCELL_PARAM].items) {
145 /* Look for existing tokens for this cell */
146 strcpy(auth_cell, as->parms[AUTHCELL_PARAM].items->data);
147 } else {
148 if (!afsclient_LocalCellGet(auth_cell, &st)) {
149 ERR_ST_EXT("can't get local cell name", st);
150 }
151 }
152 if (!afsclient_TokenGetExisting((const char*)auth_cell, &tokenHandle, &st)) {
153 ERR_ST_EXT("can't find existing tokens", st);
154 }
155 } else {
156 if (!afsclient_TokenGetNew
157 (auth_cell, (const char *)as->parms[USER_PARAM].items->data,
158 (const char *)as->parms[PASSWORD_PARAM].items->data,
159 &tokenHandle, &st)) {
160 ERR_ST_EXT("can't get tokens", st);
161 }
162 }
163
164 if (!afsclient_CellOpen(exec_cell, tokenHandle, &cellHandle, &st)) {
165 ERR_ST_EXT("can't open cell", st);
166 }
167
168 return 0;
169 }
170
171 static int
172 MyAfterProc(struct cmd_syndesc *as,void *arock)
173 {
174
175 afsclient_CellClose(cellHandle, (afs_status_p) 0);
176 afsclient_TokenClose(tokenHandle, (afs_status_p) 0);
177 return 0;
178
179 }
180
181
182 void
183 SetupCommonCmdArgs(struct cmd_syndesc *as)
184 {
185 cmd_Seek(as, USER_PARAM);
186 cmd_AddParm(as, "-authuser", CMD_SINGLE, CMD_OPTIONAL,
187 "user name to use for authentication");
188 cmd_AddParm(as, "-authpassword", CMD_SINGLE, CMD_OPTIONAL,
189 "password to use for authentication");
190 cmd_AddParm(as, "-authcell", CMD_SINGLE, CMD_OPTIONAL,
191 "cell to use for authentication");
192 cmd_AddParm(as, "-execcell", CMD_SINGLE, CMD_OPTIONAL,
193 "cell where command will execute");
194 cmd_AddParm(as, "-noauth", CMD_FLAG, CMD_OPTIONAL,
195 "run this command unauthenticated");
196 cmd_AddParm(as, "-usetokens", CMD_FLAG, CMD_OPTIONAL,
197 "use already existing tokens");
198 }
199
200 int
201 main(int argc, char *argv[])
202 {
203 int code;
204 afs_status_t st;
205
206 /* perform client initialization */
207
208 if (!afsclient_Init(&st)) {
209 ERR_ST_EXT("can't init afs client", st);
210 }
211
212 /* initialize command syntax and globals */
213
214 cmd_SetBeforeProc(MyBeforeProc, NULL);
215 cmd_SetAfterProc(MyAfterProc, NULL);
216 SetupBosAdminCmd();
217 SetupClientAdminCmd();
218 SetupKasAdminCmd();
219 SetupPtsAdminCmd();
220 SetupUtilAdminCmd();
221 SetupVosAdminCmd();
222
223 /* execute command */
224
225 code = cmd_Dispatch(argc, argv);
226
227 return (code);
228 }