2ced48a4662cb8ec594d437c7b938df1b1e11361
[openafs.git] / src / auth / copyauth.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
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <string.h>
17 #include <sys/types.h>
18 #include "auth.h"
19 #ifdef AFS_NT40_ENV
20 #include <winsock2.h>
21 #else
22 #include <netinet/in.h>
23 #endif
24 #include "cellconfig.h"
25 #include <afs/afsutil.h>
26
27 #include "AFS_component_version_number.c"
28
29 char whoami[256];
30
31 int
32 main(int argc, char **argv)
33 {
34     char localName[64];
35     register afs_int32 code;
36     register char *cname;
37     struct afsconf_dir *tdir;
38     struct ktc_principal tserver;
39     struct ktc_token token;
40
41     strcpy(whoami, argv[0]);
42
43     if (argc <= 1) {
44         printf
45             ("%s: copies a file system ticket from the local cell to another cell\n",
46              whoami);
47         printf("%s: usage is 'setauth <new-cell>\n", whoami);
48         exit(1);
49     }
50
51     cname = argv[1];
52
53     /* lookup the name of the local cell */
54     tdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH);
55     if (!tdir) {
56         printf("copyauth: can't open dir %s\n", AFSDIR_CLIENT_ETC_DIRPATH);
57         exit(1);
58     }
59     code = afsconf_GetLocalCell(tdir, localName, sizeof(localName));
60     if (code) {
61         printf("%s: can't determine local cell name\n", whoami);
62         exit(1);
63     }
64     /* done with configuration stuff now */
65     afsconf_Close(tdir);
66
67
68     /* get ticket in local cell */
69     strcpy(tserver.cell, localName);
70     strcpy(tserver.name, "afs");
71     tserver.instance[0] = 0;
72     code = ktc_GetToken(&tserver, &token, sizeof(token), NULL);
73     if (code) {
74         printf
75             ("%s: failed to get '%s' service ticket in cell '%s' (code %d)\n",
76              whoami, tserver.name, tserver.cell, code);
77         exit(1);
78     }
79
80     /* and now set the ticket in the new cell */
81     strcpy(tserver.cell, argv[1]);
82     code = ktc_SetToken(&tserver, &token, NULL, 0);
83     if (code) {
84         printf
85             ("%s: failed to set ticket (code %d), are you sure you're authenticated?\n",
86              whoami, code);
87         exit(1);
88     }
89
90     /* all done */
91     printf("Authentication established for cell %s.\n", cname);
92     exit(0);
93 }