2 * support/nfs/nfsclient.c
4 * Parse the nfsclients file.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
21 static XFILE *cfp = NULL;
22 static int *squash_uids = NULL,
24 static int squash_uidlen = 0,
26 static char *hosts = NULL;
28 static int parsesquash(char *list, int **idp, int *lenp);
29 static int parsenum(char **cpp);
30 static int parsekey(struct nfskey *keyp, char *str);
31 static int hexdigit(char c);
32 static int gettag(char *tag, int len);
33 static int getattr(char *attr, int alen, char *value, int vlen);
34 static void syntaxerr(char *msg);
37 #define isblank(c) ((c) == ' ' || (c) == '\t')
41 setnfsclntent(char *fname)
46 fname = _PATH_NFSCLIENTS;
47 if ((cfp = xfopen(fname)) == NULL)
48 xlog(L_ERROR, "can't open %s for reading", fname);
54 static struct nfsclntent cle;
55 static char *hostptr = NULL;
56 char attr[32], val[512], *sp;
70 if ((ok = gettag(cle.c_tag, sizeof(cle.c_tag))) < 0)
71 syntaxerr("expected tag");
75 cle.c_hostname[0] = '\0';
76 cle.c_fhkey.k_type = CLE_KEY_NONE;
77 cle.c_mapping = CLE_MAP_IDENT;
85 squash_uids = squash_gids = NULL;
86 squash_uidlen = squash_gidlen = 0;
89 if ((ok = getattr(attr, sizeof(attr), val, sizeof(val))) < 0)
93 if (attr[0] == 'h' && !strcmp(attr, "hosts")) {
94 int l0 = hosts? strlen(hosts) : 0;
96 hosts = (char *) xrealloc(hosts, l0+strlen(val)+2);
99 strcpy(hosts+l0, val);
101 if (attr[0] == 'f' && !strcmp(attr, "fhmac")) {
102 if (!parsekey(&cle.c_fhkey, val))
105 if (attr[0] == 'm' && !strcmp(attr, "mapping")) {
106 if (!strcmp(val, "identity"))
107 cle.c_mapping = CLE_MAP_IDENT;
108 else if (!strcmp(val, "file"))
109 cle.c_mapping = CLE_MAP_FILE;
110 else if (!strcmp(val, "daemon"))
111 cle.c_mapping = CLE_MAP_UGIDD;
113 syntaxerr("invalid mapping type");
117 if (attr[0] == 's' && !strcmp(attr, "squash_uids")) {
118 if (!parsesquash(val, &squash_uids, &squash_uidlen))
121 if (attr[0] == 's' && !strcmp(attr, "squash_gids")) {
122 if (!parsesquash(val, &squash_gids, &squash_gidlen))
125 if (attr[0] == 'a' && !strcmp(attr, "anonuid"))
126 cle.c_anonuid = atoi(val);
128 if (attr[0] == 'a' && !strcmp(attr, "anongid"))
129 cle.c_anongid = atoi(val);
131 syntaxerr("unknown attribute");
134 cle.c_squashuids = squash_uids;
135 cle.c_squashgids = squash_gids;
137 /* This is the anon entry */
139 if (strcmp(cle.c_tag, "anonymous")) {
140 xlog(L_ERROR, "nfsclients entry %s allows anonymous "
141 "access. Ignored.", cle.c_tag);
149 if (*hostptr == ':' && strcmp(cle.c_tag, "anonymous")) {
150 xlog(L_ERROR, "nfsclients entry %s allows anonymous "
151 "access. Ignored.", cle.c_tag);
152 while (*hostptr == ':')
156 /* Ignore trailing colons */
163 hostptr = strchr(hostptr, ':');
166 strncpy(cle.c_hostname, sp, sizeof(cle.c_hostname) - 1);
167 cle.c_hostname [sizeof(cle.c_hostname) - 1] = '\0';
189 parsekey(struct nfskey *keyp, char *str)
195 if ((sp = strchr(str, ':')) != NULL)
197 if (!strcmp(str, "null"))
198 keyp->k_type = CLE_KEY_NULL;
199 else if (!strcmp(str, "md5"))
200 keyp->k_type = CLE_KEY_MD5;
201 else if (!strcmp(str, "sha"))
202 keyp->k_type = CLE_KEY_SHA;
204 syntaxerr("unknown key type");
207 if (keyp->k_type == CLE_KEY_NULL) {
210 syntaxerr("unexpected key data for null key");
213 if ((l = strlen(sp)) & 1) {
214 syntaxerr("odd key length");
219 for (i = 0; i < l && i < sizeof(keyp->k_key); i++, sp += 2) {
220 if ((x0 = hexdigit(sp[0])) == 0xff ||
221 (x1 = hexdigit(sp[1])) == 0xff) {
222 syntaxerr("bad key digit");
225 keyp->k_key[i] = (x0 << 4) | x1;
236 if ((c = tolower(c)) >= '0' && c <= '9')
238 if (c >= 'a' && c <= 'f')
244 parsesquash(char *list, int **idp, int *lenp)
259 if (id0 == -1 || id1 == -1) {
260 syntaxerr("uid/gid -1 not permitted");
264 id = (int *) xrealloc(id, (len + 9) * sizeof(*id));
270 syntaxerr("bad uid/gid list");
290 while (isdigit(**cpp))
292 c = **cpp; **cpp = '\0'; num = atoi(cp); **cpp = c;
297 gettag(char *tag, int len)
300 return xgettok(cfp, ':', tag, len);
304 getattr(char *attr, int alen, char *value, int vlen)
309 if ((ok = xgettok(cfp, '=', attr, alen)) < 0)
310 xlog(L_ERROR, "error parsing attribute");
315 return xgettok(cfp, 0, value, vlen);
321 xlog(L_ERROR, "syntax error in nfsclients file (line %d): %s",