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