SQ
[obnox/samba/samba-obnox.git] / source3 / utils / net_afs.c
1 /*
2    Samba Unix/Linux SMB client library
3    net afs commands
4    Copyright (C) 2003  Volker Lendecke  (vl@samba.org)
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "utils/net.h"
22 #include "utils/net_afs.h"
23 #include "secrets.h"
24 #include "system/filesys.h"
25 #include "lib/afs/afs_funcs.h"
26 #include "lib/afs/afs_settoken.h"
27
28 #ifdef WITH_FAKE_KASERVER
29
30 int net_afs_usage(struct net_context *c, int argc, const char **argv)
31 {
32         d_printf(_("  net afs key filename\n"
33                  "\tImports a OpenAFS KeyFile into our secrets.tdb\n\n"));
34         d_printf(_("  net afs impersonate <user> <cell>\n"
35                  "\tCreates a token for user@cell\n\n"));
36         return -1;
37 }
38
39 int net_afs_key(struct net_context *c, int argc, const char **argv)
40 {
41         int fd;
42         struct afs_keyfile keyfile;
43
44         if (argc != 2) {
45                 d_printf("%s net afs key <keyfile> cell\n", _("Usage:"));
46                 return -1;
47         }
48
49         if (!secrets_init()) {
50                 d_fprintf(stderr, _("Could not open secrets.tdb\n"));
51                 return -1;
52         }
53
54         if ((fd = open(argv[0], O_RDONLY, 0)) < 0) {
55                 d_fprintf(stderr, _("Could not open %s\n"), argv[0]);
56                 return -1;
57         }
58
59         if (read(fd, &keyfile, sizeof(keyfile)) != sizeof(keyfile)) {
60                 d_fprintf(stderr, _("Could not read keyfile\n"));
61                 close(fd);
62                 return -1;
63         }
64         close(fd);
65
66         if (!secrets_store_afs_keyfile(argv[1], &keyfile)) {
67                 d_fprintf(stderr, _("Could not write keyfile to secrets.tdb\n"));
68                 return -1;
69         }
70
71         return 0;
72 }
73
74 int net_afs_impersonate(struct net_context *c, int argc,
75                                const char **argv)
76 {
77         char *token;
78
79         if (argc != 2) {
80                 d_fprintf(stderr, "%s net afs impersonate <user> <cell>\n",
81                           _("Usage:"));
82                 exit(1);
83         }
84
85         token = afs_createtoken_str(argv[0], argv[1]);
86
87         if (token == NULL) {
88                 fprintf(stderr, _("Could not create token\n"));
89                 exit(1);
90         }
91
92         if (!afs_settoken_str(token)) {
93                 fprintf(stderr, _("Could not set token into kernel\n"));
94                 exit(1);
95         }
96
97         printf(_("Success: %s@%s\n"), argv[0], argv[1]);
98         return 0;
99 }
100
101 int net_afs(struct net_context *c, int argc, const char **argv)
102 {
103         struct functable func[] = {
104                 {
105                         "key",
106                         net_afs_key,
107                         NET_TRANSPORT_LOCAL,
108                         N_("Import an OpenAFS keyfile"),
109                         N_("net afs key <filename>\n"
110                            "    Import kefile from <filename>.")
111                 },
112                 {
113                         "impersonate",
114                         net_afs_impersonate,
115                         NET_TRANSPORT_LOCAL,
116                         N_("Get a user token"),
117                         N_("net afs impersonate <user> <cell>\n"
118                            "    Create token for user@cell")
119                 },
120                 {NULL, NULL, 0, NULL, NULL}
121         };
122         return net_run_function(c, argc, argv, "net afs", func);
123 }
124
125 #endif /* WITH_FAKE_KASERVER */