1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
41 #include <sys/param.h>
42 #include <sys/utsname.h>
44 #include <sys/socket.h>
45 #include <arpa/inet.h>
46 #include <sys/types.h>
62 #ifdef HAVE_GETHOSTBYNAME
67 # include "lgss_utils.h"
69 # include "err_util.h"
74 const char * lustre_svc_name[] =
76 [LUSTRE_GSS_SVC_MDS] = "MDS",
77 [LUSTRE_GSS_SVC_OSS] = "OSS",
80 /****************************************
82 ****************************************/
84 static struct __sem_s {
89 [GSSD_CLI] = { "client", 0x3a92d473, 0 },
90 [GSSD_SVC] = { "server", 0x3b92d473, 0 },
93 void gssd_init_unique(int type)
95 struct __sem_s *sem = &sems[type];
98 assert(type == GSSD_CLI || type == GSSD_SVC);
101 sem->sem_id = semget(sem->sem_key, 1, IPC_CREAT | IPC_EXCL | 0700);
102 if (sem->sem_id == -1) {
103 if (errno != EEXIST) {
104 printerr(0, "Create sem: %s\n", strerror(errno));
108 /* already exist. Note there's still a small window racing
109 * with other processes, due to the stupid semaphore semantics.
111 sem->sem_id = semget(sem->sem_key, 0, 0700);
112 if (sem->sem_id == -1) {
113 if (errno == ENOENT) {
114 printerr(0, "another instance just exit, "
119 printerr(0, "Obtain sem: %s\n", strerror(errno));
125 if (semctl(sem->sem_id, 0, SETVAL, val) == -1) {
126 printerr(0, "Initialize sem: %s\n",
134 sembuf.sem_flg = IPC_NOWAIT | SEM_UNDO;
136 if (semop(sem->sem_id, &sembuf, 1) != 0) {
137 if (errno == EAGAIN) {
138 printerr(0, "Another instance is running, exit\n");
141 printerr(0, "Grab sem: %s\n", strerror(errno));
145 printerr(2, "Successfully created %s global identity\n", sem->name);
148 void gssd_exit_unique(int type)
150 assert(type == GSSD_CLI || type == GSSD_SVC);
153 * do nothing. we can't remove the sem here, otherwise the race
154 * window would be much bigger. So it's sad we have to leave the
155 * sem in the system forever.
159 /****************************************
160 * client side resolvation: *
161 * lnd/netid/nid => hostname *
162 ****************************************/
164 char gethostname_ex[PATH_MAX] = GSSD_DEFAULT_GETHOSTNAME_EX;
166 typedef int lnd_nid2hostname_t(char *lnd, uint32_t net, uint32_t addr,
167 char *buf, int buflen);
169 /* FIXME what about IPv6? */
171 int ipv4_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
172 char *buf, int buflen)
177 ent = gethostbyaddr(&addr, sizeof(addr), AF_INET);
179 printerr(0, "%s: can't resolve 0x%x\n", lnd, addr);
182 if (strlen(ent->h_name) >= buflen) {
183 printerr(0, "%s: name too long: %s\n", lnd, ent->h_name);
186 strcpy(buf, ent->h_name);
188 printerr(2, "%s: net 0x%x, addr 0x%x => %s\n",
189 lnd, net, addr, buf);
194 int lolnd_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
195 char *buf, int buflen)
201 printerr(0, "%s: addr is 0x%x, we expect 0\n", lnd, addr);
206 printerr(0, "%s: failed obtain local machine name\n", lnd);
210 ent = gethostbyname(uts.nodename);
212 printerr(0, "%s: failed obtain canonical name of %s\n",
217 if (strlen(ent->h_name) >= buflen) {
218 printerr(0, "%s: name too long: %s\n", lnd, ent->h_name);
221 strcpy(buf, ent->h_name);
223 printerr(3, "%s: addr 0x%x => %s\n", lnd, addr, buf);
227 static int is_space(char c)
229 return (c == ' ' || c == '\t' || c == '\n');
233 int external_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
234 char *namebuf, int namebuflen)
236 const int bufsize = PATH_MAX + 256;
237 char buf[bufsize], *head, *tail;
240 sprintf(buf, "%s %s 0x%x 0x%x", gethostname_ex, lnd, net, addr);
241 printerr(2, "cmd: %s\n", buf);
243 fghn = popen(buf, "r");
245 printerr(0, "failed to call %s\n", gethostname_ex);
249 head = fgets(buf, bufsize, fghn);
251 printerr(0, "can't read from %s\n", gethostname_ex);
254 if (pclose(fghn) == -1)
255 printerr(1, "pclose failed, continue\n");
257 /* trim head/tail space */
258 while (is_space(*head))
261 tail = head + strlen(head);
263 printerr(0, "no output from %s\n", gethostname_ex);
266 while (is_space(*(tail - 1)))
269 printerr(0, "output are all space from %s\n", gethostname_ex);
274 /* start with '@' means error msg */
275 if (head[0] == '@') {
276 printerr(0, "error from %s: %s\n", gethostname_ex, &head[1]);
280 if (tail - head > namebuflen) {
281 printerr(0, "external hostname too long: %s\n", head);
285 printerr(2, "%s: net 0x%x, addr 0x%x => %s\n",
286 lnd, net, addr, head);
287 strcpy(namebuf, head);
291 struct convert_struct {
293 lnd_nid2hostname_t *nid2name;
296 static struct convert_struct converter[] = {
297 [0] = { "UNUSED0", NULL},
298 [QSWLND] = { "QSWLND", external_nid2hostname},
299 [SOCKLND] = { "SOCKLND", ipv4_nid2hostname },
300 [GMLND] = { "GMLND", external_nid2hostname},
301 [PTLLND] = { "PTLLND", external_nid2hostname },
302 [O2IBLND] = { "O2IBLND", ipv4_nid2hostname },
303 [CIBLND] = { "CIBLND", external_nid2hostname },
304 [OPENIBLND] = { "OPENIBLND",external_nid2hostname },
305 [IIBLND] = { "IIBLND", external_nid2hostname },
306 [LOLND] = { "LOLND", lolnd_nid2hostname },
307 [RALND] = { "RALND", external_nid2hostname },
308 [VIBLND] = { "VIBLND", external_nid2hostname },
309 [MXLND] = { "MXLND", external_nid2hostname },
312 #define LND_MAX (sizeof(converter) / sizeof(converter[0]))
314 int lnet_nid2hostname(lnet_nid_t nid, char *buf, int buflen)
316 uint32_t lnd, net, addr;
318 addr = LNET_NIDADDR(nid);
319 net = LNET_NIDNET(nid);
320 lnd = LNET_NETTYP(net);
322 if (lnd >= LND_MAX) {
323 printerr(0, "ERROR: Unrecognized LND %u\n", lnd);
327 if (converter[lnd].nid2name == NULL) {
328 printerr(0, "ERROR: %s converter not ready\n",
329 converter[lnd].name);
333 return converter[lnd].nid2name(converter[lnd].name, net, addr,
338 /****************************************
339 * user mapping database handling *
341 ****************************************/
343 #define MAPPING_GROW_SIZE 512
344 #define MAX_LINE_LEN 256
346 struct user_map_item {
347 char *principal; /* NULL means match all */
352 struct user_mapping {
354 struct user_map_item *items;
357 static struct user_mapping mapping = {0, NULL};
358 /* FIXME to be finished: monitor change of mapping database */
359 static int mapping_mtime = 0;
361 void cleanup_mapping(void)
364 for (; mapping.nitems > 0; mapping.nitems--)
365 if (mapping.items[mapping.nitems-1].principal)
366 free(mapping.items[mapping.nitems-1].principal);
369 mapping.items = NULL;
373 static int grow_mapping(int nitems)
375 struct user_map_item *new;
376 int oldsize, newsize;
378 oldsize = (mapping.nitems * sizeof(struct user_map_item) +
379 MAPPING_GROW_SIZE - 1) / MAPPING_GROW_SIZE;
380 newsize = (nitems * sizeof(struct user_map_item) +
381 MAPPING_GROW_SIZE - 1) / MAPPING_GROW_SIZE;
382 while (newsize <= oldsize)
385 newsize *= MAPPING_GROW_SIZE;
386 new = malloc(newsize);
388 printerr(0, "can't alloc mapping size %d\n", newsize);
393 memcpy(new, mapping.items,
394 mapping.nitems * sizeof(struct user_map_item));
401 uid_t parse_uid(char *uidstr)
407 pw = getpwnam(uidstr);
411 uid = strtol(uidstr, &p, 0);
418 static int read_mapping_db(void)
420 char princ[MAX_LINE_LEN];
421 char nid_str[MAX_LINE_LEN];
422 char dest[MAX_LINE_LEN];
423 char linebuf[MAX_LINE_LEN];
429 /* cleanup old mappings */
432 f = fopen(MAPPING_DATABASE_FILE, "r");
434 printerr(0, "can't open mapping database: %s\n",
435 MAPPING_DATABASE_FILE);
439 while ((line = fgets(linebuf, MAX_LINE_LEN, f)) != NULL) {
442 if (strlen(line) >= MAX_LINE_LEN) {
443 printerr(0, "invalid mapping db: line too long (%d)\n",
448 if (sscanf(line, "%s %s %s", princ, nid_str, dest) != 3) {
449 printerr(0, "mapping db: syntax error\n");
453 if (!strcmp(princ, "*")) {
456 name = strdup(princ);
458 printerr(0, "fail to dup str %s\n", princ);
463 if (!strcmp(nid_str, "*")) {
466 nid = libcfs_str2nid(nid_str);
467 if (nid == LNET_NID_ANY) {
468 printerr(0, "fail to parse nid %s\n", nid_str);
475 dest_uid = parse_uid(dest);
476 if (dest_uid == -1) {
477 printerr(0, "no valid user: %s\n", dest);
483 if (grow_mapping(mapping.nitems + 1)) {
484 printerr(0, "fail to grow mapping to %d\n",
492 mapping.items[mapping.nitems].principal = name;
493 mapping.items[mapping.nitems].nid = nid;
494 mapping.items[mapping.nitems].uid = dest_uid;
496 printerr(1, "add mapping: %s(%s/0x%llx) ==> %d\n",
497 name, nid_str, nid, dest_uid);
504 static inline int mapping_changed(void)
508 if (stat(MAPPING_DATABASE_FILE, &st) == -1) {
509 /* stat failed, treat it like doesn't exist or be removed */
510 if (mapping_mtime == 0) {
513 printerr(0, "Warning: stat %s failed: %s\n",
514 MAPPING_DATABASE_FILE, strerror(errno));
521 if (st.st_mtime != mapping_mtime) {
522 mapping_mtime = st.st_mtime;
529 int lookup_mapping(char *princ, lnet_nid_t nid, uid_t *uid)
535 /* FIXME race condition here */
536 if (mapping_changed()) {
537 if (read_mapping_db())
538 printerr(0, "all remote users will be denied\n");
541 for (n = 0; n < mapping.nitems; n++) {
542 struct user_map_item *entry = &mapping.items[n];
544 if (entry->nid != LNET_NID_ANY && entry->nid != nid)
546 if (!entry->principal || !strcasecmp(entry->principal, princ)) {
547 printerr(1, "found mapping: %s ==> %d\n",
554 printerr(2, "no mapping for %s/%#Lx\n", princ, nid);