comerr: Fix a couple of libtool problems
[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 #ifdef AFS_DARWIN_ENV
48 pthread_mutex_t des_init_mutex = PTHREAD_MUTEX_INITIALIZER;
49 pthread_mutex_t des_random_mutex = PTHREAD_MUTEX_INITIALIZER;
50 pthread_mutex_t rxkad_random_mutex = PTHREAD_MUTEX_INITIALIZER;
51 #endif /* AFS_DARWIN_ENV */
52
53 /*
54  * Before processing any command, process the common arguments and
55  * get an appropriate cell handle
56  */
57
58 static int
59 MyBeforeProc(struct cmd_syndesc *as, void *arock)
60 {
61     afs_status_t st = 0;
62     int no_auth = 0;
63     char auth_cell[MAXCELLCHARS];
64     char exec_cell[MAXCELLCHARS];
65
66     /*
67      * Check what kind of authentication is necessary based upon
68      * the arguments passed
69      */
70
71     /*
72      * Check for noauth first
73      */
74
75     if (as->parms[NOAUTH_PARAM].items) {
76         no_auth = 1;
77         if (as->parms[USER_PARAM].items) {
78             ERR_EXT("you can't specify both -noauth and -authuser");
79         }
80         if (as->parms[PASSWORD_PARAM].items) {
81             ERR_EXT("you can't specify both -noauth and -authpassword");
82         }
83         if (as->parms[AUTHCELL_PARAM].items) {
84             ERR_EXT("you can't specify both -noauth and -authcell");
85         }
86         if (as->parms[USEEXISTTOKENS_PARAM].items) {
87             ERR_EXT("you can't specify both -noauth and -usetokens");
88         }
89     }
90
91     /*
92      * Check for usetokens
93      */
94
95     if (as->parms[USEEXISTTOKENS_PARAM].items) {
96         existing_tokens = 1;
97         if (as->parms[USER_PARAM].items) {
98             ERR_EXT("you can't specify both -usetokens and -authuser");
99         }
100         if (as->parms[PASSWORD_PARAM].items) {
101             ERR_EXT("you can't specify both -usetokens and -authpassword");
102         }
103     }
104
105     /*
106      * Check for user name password and auth cell
107      * It's ok to specify user name and password, but not auth cell
108      * in that case, we assume that the auth cell is the local cell.
109      */
110
111     if (as->parms[USER_PARAM].items) {
112         if (!as->parms[PASSWORD_PARAM].items) {
113             ERR_EXT
114                 ("you must specify -authpassword if you specify -authuser");
115         }
116         if (as->parms[AUTHCELL_PARAM].items) {
117             strcpy(auth_cell, as->parms[AUTHCELL_PARAM].items->data);
118         } else {
119             if (!afsclient_LocalCellGet(auth_cell, &st)) {
120                 ERR_ST_EXT("can't get local cell name", st);
121             }
122         }
123     }
124
125     /*
126      * Get the execution cell.  If this parameter wasn't passed, we
127      * assume the command should execute in the local cell.
128      */
129
130     if (as->parms[EXECCELL_PARAM].items) {
131         strcpy(exec_cell, as->parms[EXECCELL_PARAM].items->data);
132     } else {
133         if (!afsclient_LocalCellGet(exec_cell, &st)) {
134             ERR_ST_EXT("can't get local cell name", st);
135         }
136     }
137
138     /*
139      * Get a token handle and a cell handle for this invocation
140      */
141
142     if (no_auth) {
143         if (!afsclient_TokenGetNew
144             (auth_cell, (const char *)0, (const char *)0, &tokenHandle,
145              &st)) {
146             ERR_ST_EXT("can't get noauth tokens", st);
147         }
148     } else if (existing_tokens) {
149         if (as->parms[AUTHCELL_PARAM].items) {
150             /* Look for existing tokens for this cell */
151             strcpy(auth_cell, as->parms[AUTHCELL_PARAM].items->data);
152         } else {
153             if (!afsclient_LocalCellGet(auth_cell, &st)) {
154                 ERR_ST_EXT("can't get local cell name", st);
155             }
156         }
157         if (!afsclient_TokenGetExisting((const char*)auth_cell, &tokenHandle, &st)) {
158             ERR_ST_EXT("can't find existing tokens", st);
159         }
160     } else {
161         if (!afsclient_TokenGetNew
162             (auth_cell, (const char *)as->parms[USER_PARAM].items->data,
163              (const char *)as->parms[PASSWORD_PARAM].items->data,
164              &tokenHandle, &st)) {
165             ERR_ST_EXT("can't get tokens", st);
166         }
167     }
168
169     if (!afsclient_CellOpen(exec_cell, tokenHandle, &cellHandle, &st)) {
170         ERR_ST_EXT("can't open cell", st);
171     }
172
173     return 0;
174 }
175
176 static int
177 MyAfterProc(struct cmd_syndesc *as,void *arock)
178 {
179
180     afsclient_CellClose(cellHandle, (afs_status_p) 0);
181     afsclient_TokenClose(tokenHandle, (afs_status_p) 0);
182     return 0;
183
184 }
185
186
187 void
188 SetupCommonCmdArgs(struct cmd_syndesc *as)
189 {
190     cmd_Seek(as, USER_PARAM);
191     cmd_AddParm(as, "-authuser", CMD_SINGLE, CMD_OPTIONAL,
192                 "user name to use for authentication");
193     cmd_AddParm(as, "-authpassword", CMD_SINGLE, CMD_OPTIONAL,
194                 "password to use for authentication");
195     cmd_AddParm(as, "-authcell", CMD_SINGLE, CMD_OPTIONAL,
196                 "cell to use for authentication");
197     cmd_AddParm(as, "-execcell", CMD_SINGLE, CMD_OPTIONAL,
198                 "cell where command will execute");
199     cmd_AddParm(as, "-noauth", CMD_FLAG, CMD_OPTIONAL,
200                 "run this command unauthenticated");
201     cmd_AddParm(as, "-usetokens", CMD_FLAG, CMD_OPTIONAL,
202                 "use already existing tokens");
203 }
204
205 int
206 main(int argc, char *argv[])
207 {
208     int code;
209     afs_status_t st;
210
211     /* perform client initialization */
212
213     if (!afsclient_Init(&st)) {
214         ERR_ST_EXT("can't init afs client", st);
215     }
216
217     /* initialize command syntax and globals */
218
219     cmd_SetBeforeProc(MyBeforeProc, NULL);
220     cmd_SetAfterProc(MyAfterProc, NULL);
221     SetupBosAdminCmd();
222     SetupClientAdminCmd();
223     SetupKasAdminCmd();
224     SetupPtsAdminCmd();
225     SetupUtilAdminCmd();
226     SetupVosAdminCmd();
227
228     /* execute command */
229
230     code = cmd_Dispatch(argc, argv);
231
232     return (code);
233 }