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