aboutsummaryrefslogtreecommitdiff
path: root/src/lib/krb4/mac_stubs.c
blob: b83e92d10fa83bab1a9a74f4287660fb74f1b5e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* 
 * mac_stubs.c
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Macintosh oopserating system stub interface for Kerberos.
 * Applications call these routines, which then call the driver to do the work.
 */

#define	DEFINE_SOCKADDR		/* For struct hostent, <netdb.h>, etc */
#include "krb.h"
#include "krb_driver.h"	/* Mac driver interface */

#include <string.h>
#include <stddef.h>
#include <Files.h>
#include <Devices.h>

/* We export the driver reference under the name mac_stubs_kdriver,
   but for convenience throughout this code, we call it "kdriver",
   which was its name when it was static.  */
short mac_stubs_kdriver = 0;		/* .Kerberos driver ref */
#define	kdriver mac_stubs_kdriver

ParamBlockRec pb[1];
struct krbHiParmBlock khipb[1];
struct krbParmBlock klopb[1];

short lowcall (long cscode, krbParmBlock *klopb, short kdriver)
{
	short s;
	ParamBlockRec pb;
	
	memset (&pb, 0, sizeof(ParamBlockRec));
	*(long *)pb.cntrlParam.csParam = (long)klopb;
	pb.cntrlParam.ioCompletion = nil;
	pb.cntrlParam.ioCRefNum = kdriver;
	pb.cntrlParam.csCode = cscode;
	
	if (s = PBControl(&pb, false))
		return KFAILURE;
	if (s = pb.cntrlParam.ioResult)
		return -(s - cKrbKerberosErrBlock);	/* Restore krb err code from driver err */

	return KSUCCESS;
}


short hicall (long cscode, krbHiParmBlock *khipb, short kdriver)
{
	short s;
	ParamBlockRec pb;
	memset(&pb, 0, sizeof(ParamBlockRec));
	*(long *)pb.cntrlParam.csParam = (long)khipb;
	pb.cntrlParam.ioCompletion = nil;
	pb.cntrlParam.ioCRefNum = kdriver;

	pb.cntrlParam.csCode = cscode;
	if (s = PBControl(&pb, false))
		return KFAILURE;
	if (s = pb.cntrlParam.ioResult)
		return -(s - cKrbKerberosErrBlock);	/* Restore krb err code from driver err */

	return KSUCCESS;
}


int INTERFACE
krb_start_session (x)
	char *x;
{
	short s;
	
	/*
	 * Open the .Kerberos driver if not already open
	 */
	if (!kdriver) {
		s = OpenDriver("\p.Kerberos", &kdriver);
		if (s) {
			return KFAILURE;	/* Improve this error code */
		}
	}

	return KSUCCESS;
}


int INTERFACE
krb_end_session (x)
	char *x;
{
	short s;

#if 0 /* This driver doesn't want to be closed.  FIXME, is this OK? */
	if (kdriver) {
		s = CloseDriver(kdriver);
		if (s)
			return KFAILURE;
		kdriver = 0;
	}
#endif
	return KSUCCESS;
}


char * INTERFACE
krb_realmofhost (host)
	char *host;
{
	short s;
	ParamBlockRec pb;
	static char realm[REALM_SZ];

	memset(klopb, 0, sizeof(*klopb));
	klopb->host = host;
	klopb->uRealm = realm;
	
	/* FIXME jcm - no error handling for return value of lowcall in krb_realmofhost */
	s = lowcall (cKrbGetRealm , klopb, kdriver);

	return realm;
}

int INTERFACE
krb_get_lrealm (realm, n)
	char *realm;
	int n;
{
	short s;
	ParamBlockRec pb;

	if (n != 1)
		return KFAILURE;

	memset(klopb, 0, sizeof(*klopb));
	klopb->uRealm = realm;

	s = lowcall (cKrbGetLocalRealm, klopb, kdriver);
	return s;
		
}


int INTERFACE
kname_parse (name, instance, realm, fullname)
	char *name, *instance, *realm, *fullname;
{
	short s;
	ParamBlockRec pb;

	memset(klopb, 0, sizeof(*klopb));
	klopb->uName = name;
	klopb->uInstance = instance;
	klopb->uRealm = realm;
	klopb->fullname = fullname;

	s = lowcall (cKrbKnameParse, klopb, kdriver);
	return s;
}

const char* INTERFACE
krb_get_err_text (error_code)
	int error_code;
{
	short s;
	
	memset(klopb, 0, sizeof(*klopb));
	klopb->admin = error_code;	
	s = lowcall (cKrbGetErrText, klopb, kdriver);
	if (s != KSUCCESS)
		return "Error in get_err_text";	
	return klopb->uName;
}


int INTERFACE
krb_get_pw_in_tkt(user,instance,realm,service,sinstance,life,password)
    char *user, *instance, *realm, *service, *sinstance;
    int life;
    char *password;
{
	short s;
	
	memset(klopb, 0, sizeof(*klopb));
	klopb->uName = user;	
	klopb->uInstance = instance;
	klopb->uRealm = realm;
	klopb->sName = service;
	klopb->sInstance = sinstance;
	klopb->admin = life;
	klopb->fullname = password;
	
	s = lowcall (cKrbGetPwInTkt, klopb, kdriver);
	return s;
}


/* FIXME:  For now, we handle the preauth version exactly the same
   as the non-preauth.   */
krb_get_pw_in_tkt_preauth(user,instance,realm,service,sinstance,life,password)
    char *user, *instance, *realm, *service, *sinstance;
    int life;
    char *password;
{
	short s;
	
	memset(klopb, 0, sizeof(*klopb));
	klopb->uName = user;	
	klopb->uInstance = instance;
	klopb->uRealm = realm;
	klopb->sName = service;
	klopb->sInstance = sinstance;
	klopb->admin = life;
	klopb->fullname = password;
	
	s = lowcall (cKrbGetPwInTkt, klopb, kdriver);
	return s;
}



char* INTERFACE
krb_get_default_user (void)
{
	short s;
	static char return_name[MAX_K_NAME_SZ];
	
	memset(khipb, 0, sizeof(*khipb));
	khipb->user = return_name;
	s = hicall (cKrbGetUserName, khipb, kdriver);
	if (s != KSUCCESS)
		return 0;
	return return_name;
}


int INTERFACE
krb_set_default_user (uName)
	char* uName;
{
	short s;
	
	memset(khipb, 0, sizeof(*khipb));
	khipb->user = uName;
	s = hicall (cKrbSetUserName, khipb, kdriver);
	return s;
}

int INTERFACE
krb_get_cred (name, instance, realm, cr)
	char *name;
	char *instance;
	char *realm;
	CREDENTIALS *cr;
{
	short s;
	
	memset(klopb, 0, sizeof(*klopb));
	
	strcpy(cr->service, name);
	strcpy(cr->instance, instance);
	strcpy(cr->realm, realm);
	
	klopb->cred = cr;

	s = lowcall (cKrbGetCredentials, klopb, kdriver);
	return s;
}

int INTERFACE
krb_save_credentials (sname, sinstance, srealm, session, 
			lifetime, kvno,ticket, issue_date)
	char *sname;		/* service name */
	char *sinstance;	/* service instance */
	char *srealm;		/* service realm */
	C_Block session;	/* Session key */
	int lifetime;		/* Lifetime */
	int kvno;			/* Key version number */
    KTEXT ticket; 	    /* The ticket itself */
	long issue_date;	/* The issue time */
	
{
	short s;
	CREDENTIALS cr;
	
	strcpy(cr.service, sname);
	strcpy(cr.instance, sinstance);
	strcpy(cr.realm, srealm);
	memcpy(cr.session, session, sizeof(C_Block));
	cr.lifetime = lifetime;
	cr.kvno = kvno;
	cr.ticket_st = *ticket;
	cr.issue_date = issue_date;
	
	memset(klopb, 0, sizeof(*klopb));
	klopb->cred = &cr;

	s = lowcall (cKrbAddCredentials, klopb, kdriver);
	return s;
}


int INTERFACE
krb_delete_cred (sname, sinstance, srealm)
	char *sname;
	char *sinstance;
	char *srealm;
{
	short s;
	
	memset(klopb, 0, sizeof(*klopb));
	
	klopb->sName = sname;
	klopb->sInstance = sinstance;
	klopb->sRealm = srealm;
	
	s = lowcall (cKrbDeleteCredentials, klopb, kdriver);
	return s;
}

int INTERFACE
dest_tkt (cachename)
	char *cachename;		/* This parameter is ignored. */
{
	short s;
	
	memset(klopb, 0, sizeof(*klopb));
	s = lowcall (cKrbDeleteAllSessions, klopb, kdriver);
	return s;
}

/* 
 *	returns service name, service instance and realm of the nth credential. 
 *  credential numbering is 1 based.
 */

int INTERFACE
krb_get_nth_cred (sname, sinstance, srealm, n)
	char *sname;
	char *sinstance;
	char *srealm;
	int n;
{
	short s;
	
	memset(klopb, 0, sizeof(*klopb));
	
	klopb->sName = sname;
	klopb->sInstance = sinstance;
	klopb->sRealm = srealm;
	klopb->itemNumber = &n;
	
	s = lowcall (cKrbGetNthCredentials, klopb, kdriver);
	return s;
}

/*
 * Return the number of credentials in the current credential cache (ticket cache).
 * On error, returns -1. 
 */
int INTERFACE
krb_get_num_cred ()
{
	int s;
	int n;
	
	memset(klopb, 0, sizeof(*klopb));
	klopb->itemNumber = &n;
	
	s = lowcall (cKrbGetNumCredentials, klopb, kdriver);
	if (s) 
		return -1;
	return *(klopb->itemNumber);
}



/* GetNthRealmMap
   yields the Nth mapping of a net or host to a Kerberos realm 
	  -> itemNumber 	which mapping, traditionally the first
	  -> host	   		host or net
	  -> uRealm    		pointer to buffer that will receive realm name
*/

OSErr INTERFACE
GetNthRealmMap(n, netorhost, realm)
	int n;
	char *netorhost;
	char *realm;
{
	int s;
	memset(klopb, 0, sizeof(*klopb));
	klopb->itemNumber = &n;
	klopb->host = netorhost;
	klopb->uRealm = realm;
	
	s = lowcall (cKrbGetNthRealmMap, klopb, kdriver);
	return s;
}

/* GetNthServerMap
   yields Nth realm-server mapping
   -> itemNumber		which mapping should be returned
   -> uRealm			pointer to buffer that will receive realm name	
   -> host				pointer to buffer that will receive server name
   -> admin				pointer to admin flag
 */
	
OSErr	INTERFACE
GetNthServerMap(n, realm, server, admin)	
    int n;
    char *realm;
    char *server; 
    int *admin;
{
	int s;
	memset(klopb, 0, sizeof(*klopb));
	klopb->itemNumber = &n;
	klopb->uRealm = realm;
	klopb->host = server;
	klopb->adminReturn = admin;

	s = lowcall (cKrbGetNthServerMap, klopb, kdriver);
	return s;
}



/* krb_get_ticket_for_service
 * Gets a ticket and returns it to application in buf
	  -> service		Formal Kerberos name of service
	  -> buf		Buffer to receive ticket
	  -> checksum		checksum for this service
	 <-> buflen		length of ticket buffer (must be at least
					1258 bytes)
	 <-  sessionKey		for internal use
	 <-  schedule		for internal use

 * Result is:
 *   GC_NOTKT		if there is no matching TGT in the cache
 *   MK_AP_TGTEXP	if the matching TGT is expired
 * Other errors possible.  These could cause a dialogue with the user
 * to get a new TGT.
 */ 

int INTERFACE
krb_get_ticket_for_service (serviceName, buf, buflen, checksum, sessionKey,
		schedule, version, includeVersion)
	char *serviceName;
	char *buf;
	unsigned KRB4_32 *buflen;
	int checksum;
	des_cblock sessionKey;
	Key_schedule schedule;
	char *version;
	int includeVersion;
{
	short s;

	if (includeVersion)
		return KFAILURE;		/* Not implmented in the kclient driver iface */
	
	memset(khipb, 0, sizeof(*khipb));
	khipb->service = serviceName;
	khipb->buf = buf;
	khipb->buflen = *buflen;
	khipb->checksum = checksum;

	s = hicall (cKrbGetTicketForService, khipb, kdriver);
	/* These are ARRAYS in the hiparmblock, for some reason! */
	memcpy (sessionKey, khipb->sessionKey, sizeof (khipb[0].sessionKey));
	memcpy (schedule,   khipb->schedule,   sizeof (khipb[0].schedule));
	*buflen = khipb->buflen;
	return s;
}


/* 	krb_get_tf_fullname -- return name, instance and realm of the
	principal in the current ticket file. The ticket file name is not 
	currently used for anything since there is only one credentials 
	cache/ticket file
*/

int INTERFACE
krb_get_tf_fullname (tktfile, name, instance, realm)
  char *tktfile;
  char *name;
  char *instance;
  char *realm;

{
	short s;
	memset (klopb, 0, sizeof(*klopb));
	klopb->fullname = tktfile;
	klopb->uName = name;
	klopb->uInstance = instance;
	klopb->uRealm = realm;
	
	s = lowcall (cKrbGetTfFullname, klopb, kdriver);
	return s;
}



#if 0
	xbzero(khipb, sizeof(krbHiParmBlock));
	khipb->service = (char *)cannon;
	khipb->buf = (char *)buf;				/* where to build it */
	khipb->checksum = 0;
	khipb->buflen = sizeof(buf);
	if (s = hicall(cKrbGetTicketForService, khipb, kdriver))
		return s;
	xbcopy(khipb->sessionKey, sessionKey, sizeof(sessionKey));	/* save the session key */
	/*
	 * cKrbGetTicketForService put a longword buffer length into the buffer
	 * which we don't want, so we ignore it.
     * Make room for first 3 bytes which preceed the auth data.
	 */
	cp = &buf[4-3];						/* skip long, make room for 3 bytes */
	cp[0] = tp[0];						/* copy type and modifier */
	cp[1] = tp[1];
	cp[2] = KRB_AUTH;					/* suboption command */
	len = khipb->buflen - sizeof(long) + 3; /* data - 4 + 3 */

#endif /* 0 */