summaryrefslogtreecommitdiffstats
path: root/support/nfs/keytab.c
blob: e33ddedc48598a236fb74d8e967e39f8fa1d7298 (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
/*
 * support/nfs/keytab.c
 *
 * Manage the nfskeys database.
 *
 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
 */

#include "config.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <ctype.h>
#include "xmalloc.h"
#include "nfslib.h"
#include "exportfs.h"
#include "xio.h"

static FILE	*cfp = NULL;

int
setnfskeyent(char *fname)
{
	if (cfp)
		fclose(cfp);
	if (!fname)
		fname = _PATH_NFSKEYS;
	cfp = fsetnfskeyent(fname, "r");
	return cfp != NULL;
}

FILE *
fsetnfskeyent(char *fname, char *type)
{
#if 0
	FILE	*fp;

	if ((fp = fopen(fname, type)) == NULL)
		xlog(L_ERROR, "can't open %s for %sing\n",
				fname, type[0] == 'r'? "read" : "writ");
	return fp;
#else
	return fopen(fname, type);
#endif
}

struct nfskeyent *
getnfskeyent(void)
{
	return fgetnfskeyent(cfp);
}

struct nfskeyent *
fgetnfskeyent(FILE *fp)
{
	static struct nfskeyent ke;

	if (!fp)
		return NULL;

	do {
		if (fread(&ke, sizeof(ke), 1, fp) != 1)
			return NULL;
	} while(ke.k_hostname[0] == '\0');
	return &ke;
}

void
endnfskeyent(void)
{
	if (cfp)
		fclose(cfp);
	cfp = NULL;
}

void
fendnfskeyent(FILE *fp)
{
	if (fp)
		fclose(fp);
}

void
fputnfskeyent(FILE *fp, struct nfskeyent *kep)
{
	fwrite(kep, sizeof(*kep), 1, fp);
}

int
getnfskeytype(char *st)
{
	if (!strcasecmp(st, "null"))
		return CLE_KEY_NULL;
	if (!strcasecmp(st, "md5"))
		return CLE_KEY_MD5;
	if (!strcasecmp(st, "sha"))
		return CLE_KEY_SHA;
	return CLE_KEY_NONE;
}

char *
getnfskeyname(int type)
{
	switch (type) {
	case CLE_KEY_NONE:
		return "none";
	case CLE_KEY_NULL:
		return "null";
	case CLE_KEY_MD5:
		return "md5";
	case CLE_KEY_SHA:
		return "sha";
	}
	return "unk";
}

int
getnfskeysize(int type)
{
	switch (type) {
	case CLE_KEY_MD5:
		return 16;
	case CLE_KEY_SHA:
		return 20;
	}
	return 0;
}