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