r2333: check the script is not a 0 lenght string
[Samba/bb.git] / source / lib / afs_settoken.c
blob5c646c72e48028cc59a022d8658d4049b95b1ac6
1 /*
2 * Unix SMB/CIFS implementation.
3 * Generate AFS tickets
4 * Copyright (C) Volker Lendecke 2004
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 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 #ifdef WITH_FAKE_KASERVER
25 #include <afs/stds.h>
26 #include <afs/afs.h>
27 #include <afs/auth.h>
28 #include <afs/venus.h>
29 #include <asm/unistd.h>
30 #include <openssl/des.h>
31 #include <sys/syscall.h>
33 int afs_syscall( int subcall,
34 char * path,
35 int cmd,
36 char * cmarg,
37 int follow)
39 return( syscall( SYS_afs_syscall, subcall, path, cmd, cmarg, follow));
42 struct ClearToken {
43 uint32 AuthHandle;
44 char HandShakeKey[8];
45 uint32 ViceId;
46 uint32 BeginTimestamp;
47 uint32 EndTimestamp;
50 static BOOL afs_decode_token(const char *string, char **cell,
51 DATA_BLOB *ticket, struct ClearToken *ct)
53 DATA_BLOB blob;
54 struct ClearToken result_ct;
56 char *s = strdup(string);
58 char *t;
60 if ((t = strtok(s, "\n")) == NULL) {
61 DEBUG(10, ("strtok failed\n"));
62 return False;
65 *cell = strdup(t);
67 if ((t = strtok(NULL, "\n")) == NULL) {
68 DEBUG(10, ("strtok failed\n"));
69 return False;
72 if (sscanf(t, "%u", &result_ct.AuthHandle) != 1) {
73 DEBUG(10, ("sscanf AuthHandle failed\n"));
74 return False;
77 if ((t = strtok(NULL, "\n")) == NULL) {
78 DEBUG(10, ("strtok failed\n"));
79 return False;
82 blob = base64_decode_data_blob(t);
84 if ( (blob.data == NULL) ||
85 (blob.length != sizeof(result_ct.HandShakeKey) )) {
86 DEBUG(10, ("invalid key: %x/%d\n", (uint32)blob.data,
87 blob.length));
88 return False;
91 memcpy(result_ct.HandShakeKey, blob.data, blob.length);
93 data_blob_free(&blob);
95 if ((t = strtok(NULL, "\n")) == NULL) {
96 DEBUG(10, ("strtok failed\n"));
97 return False;
100 if (sscanf(t, "%u", &result_ct.ViceId) != 1) {
101 DEBUG(10, ("sscanf ViceId failed\n"));
102 return False;
105 if ((t = strtok(NULL, "\n")) == NULL) {
106 DEBUG(10, ("strtok failed\n"));
107 return False;
110 if (sscanf(t, "%u", &result_ct.BeginTimestamp) != 1) {
111 DEBUG(10, ("sscanf BeginTimestamp failed\n"));
112 return False;
115 if ((t = strtok(NULL, "\n")) == NULL) {
116 DEBUG(10, ("strtok failed\n"));
117 return False;
120 if (sscanf(t, "%u", &result_ct.EndTimestamp) != 1) {
121 DEBUG(10, ("sscanf EndTimestamp failed\n"));
122 return False;
125 if ((t = strtok(NULL, "\n")) == NULL) {
126 DEBUG(10, ("strtok failed\n"));
127 return False;
130 blob = base64_decode_data_blob(t);
132 if (blob.data == NULL) {
133 DEBUG(10, ("Could not get ticket\n"));
134 return False;
137 *ticket = blob;
138 *ct = result_ct;
140 return True;
144 Put an AFS token into the Kernel so that it can authenticate against
145 the AFS server. This assumes correct local uid settings.
147 This is currently highly Linux and OpenAFS-specific. The correct API
148 call for this would be ktc_SetToken. But to do that we would have to
149 import a REALLY big bunch of libraries which I would currently like
150 to avoid.
153 static BOOL afs_settoken(const char *cell,
154 const struct ClearToken *ctok,
155 DATA_BLOB ticket)
157 int ret;
158 struct {
159 char *in, *out;
160 uint16 in_size, out_size;
161 } iob;
163 char buf[1024];
164 char *p = buf;
165 int tmp;
167 memcpy(p, &ticket.length, sizeof(uint32));
168 p += sizeof(uint32);
169 memcpy(p, ticket.data, ticket.length);
170 p += ticket.length;
172 tmp = sizeof(struct ClearToken);
173 memcpy(p, &tmp, sizeof(uint32));
174 p += sizeof(uint32);
175 memcpy(p, ctok, tmp);
176 p += tmp;
178 tmp = 0;
180 memcpy(p, &tmp, sizeof(uint32));
181 p += sizeof(uint32);
183 tmp = strlen(cell);
184 if (tmp >= MAXKTCREALMLEN) {
185 DEBUG(1, ("Realm too long\n"));
186 return False;
189 strncpy(p, cell, tmp);
190 p += tmp;
191 *p = 0;
192 p +=1;
194 iob.in = buf;
195 iob.in_size = PTR_DIFF(p,buf);
196 iob.out = buf;
197 iob.out_size = sizeof(buf);
199 #if 0
200 file_save("/tmp/ioctlbuf", iob.in, iob.in_size);
201 #endif
203 ret = afs_syscall(AFSCALL_PIOCTL, 0, VIOCSETTOK, (char *)&iob, 0);
205 DEBUG(10, ("afs VIOCSETTOK returned %d\n", ret));
206 return (ret == 0);
209 BOOL afs_settoken_str(const char *token_string)
211 DATA_BLOB ticket;
212 struct ClearToken ct;
213 BOOL result;
214 char *cell;
216 if (!afs_decode_token(token_string, &cell, &ticket, &ct))
217 return False;
219 if (geteuid() != 0)
220 ct.ViceId = getuid();
222 result = afs_settoken(cell, &ct, ticket);
224 SAFE_FREE(cell);
225 data_blob_free(&ticket);
227 return result;
230 #else
232 BOOL afs_settoken_str(const char *token_string)
234 return False;
237 #endif