4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
37 #include <sys/param.h>
38 #include <sys/utsname.h>
40 #include <sys/socket.h>
41 #include <arpa/inet.h>
42 #include <sys/types.h>
62 #include <lnet/nidstr.h>
64 # include "lgss_utils.h"
66 # include "err_util.h"
71 const char * lustre_svc_name[] =
73 [LUSTRE_GSS_SVC_MGS] = "MGS",
74 [LUSTRE_GSS_SVC_MDS] = "MDS",
75 [LUSTRE_GSS_SVC_OSS] = "OSS",
78 /****************************************
80 ****************************************/
82 static struct __sem_s {
87 [GSSD_CLI] = { "client", 0x3a92d473, 0 },
88 [GSSD_SVC] = { "server", 0x3b92d473, 0 },
91 void gssd_init_unique(int type)
93 struct __sem_s *sem = &sems[type];
96 assert(type == GSSD_CLI || type == GSSD_SVC);
99 sem->sem_id = semget(sem->sem_key, 1, IPC_CREAT | IPC_EXCL | 0700);
100 if (sem->sem_id == -1) {
101 if (errno != EEXIST) {
102 printerr(0, "Create sem: %s\n", strerror(errno));
106 /* already exist. Note there's still a small window racing
107 * with other processes, due to the stupid semaphore semantics.
109 sem->sem_id = semget(sem->sem_key, 0, 0700);
110 if (sem->sem_id == -1) {
111 if (errno == ENOENT) {
112 printerr(0, "another instance just exit, "
117 printerr(0, "Obtain sem: %s\n", strerror(errno));
123 if (semctl(sem->sem_id, 0, SETVAL, val) == -1) {
124 printerr(0, "Initialize sem: %s\n",
132 sembuf.sem_flg = IPC_NOWAIT | SEM_UNDO;
134 if (semop(sem->sem_id, &sembuf, 1) != 0) {
135 if (errno == EAGAIN) {
136 printerr(0, "Another instance is running, exit\n");
139 printerr(0, "Grab sem: %s\n", strerror(errno));
143 printerr(2, "Successfully created %s global identity\n", sem->name);
146 void gssd_exit_unique(int type)
148 assert(type == GSSD_CLI || type == GSSD_SVC);
151 * do nothing. we can't remove the sem here, otherwise the race
152 * window would be much bigger. So it's sad we have to leave the
153 * sem in the system forever.
157 /****************************************
158 * client side resolvation: *
159 * lnd/netid/nid => hostname *
160 ****************************************/
162 char gethostname_ex[PATH_MAX] = GSSD_DEFAULT_GETHOSTNAME_EX;
164 typedef int lnd_nid2hostname_t(char *lnd, uint32_t net, uint32_t addr,
165 char *buf, int buflen);
167 /* FIXME what about IPv6? */
169 int ipv4_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
170 char *buf, int buflen)
175 ent = gethostbyaddr(&addr, sizeof(addr), AF_INET);
177 printerr(0, "%s: can't resolve 0x%x\n", lnd, addr);
180 if (strlen(ent->h_name) >= buflen) {
181 printerr(0, "%s: name too long: %s\n", lnd, ent->h_name);
184 strcpy(buf, ent->h_name);
186 printerr(2, "%s: net 0x%x, addr 0x%x => %s\n",
187 lnd, net, addr, buf);
192 int lolnd_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
193 char *buf, int buflen)
199 printerr(0, "%s: addr is 0x%x, we expect 0\n", lnd, addr);
204 printerr(0, "%s: failed obtain local machine name\n", lnd);
208 ent = gethostbyname(uts.nodename);
210 printerr(0, "%s: failed obtain canonical name of %s\n",
215 if (strlen(ent->h_name) >= buflen) {
216 printerr(0, "%s: name too long: %s\n", lnd, ent->h_name);
219 strcpy(buf, ent->h_name);
221 printerr(3, "%s: addr 0x%x => %s\n", lnd, addr, buf);
225 static int is_space(char c)
227 return (c == ' ' || c == '\t' || c == '\n');
231 int external_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
232 char *namebuf, int namebuflen)
234 const int bufsize = PATH_MAX + 256;
235 char buf[bufsize], *head, *tail;
238 sprintf(buf, "%s %s 0x%x 0x%x", gethostname_ex, lnd, net, addr);
239 printerr(2, "cmd: %s\n", buf);
241 fghn = popen(buf, "r");
243 printerr(0, "failed to call %s\n", gethostname_ex);
247 head = fgets(buf, bufsize, fghn);
249 printerr(0, "can't read from %s\n", gethostname_ex);
252 if (pclose(fghn) == -1)
253 printerr(1, "pclose failed, continue\n");
255 /* trim head/tail space */
256 while (is_space(*head))
259 tail = head + strlen(head);
261 printerr(0, "no output from %s\n", gethostname_ex);
264 while (is_space(*(tail - 1)))
267 printerr(0, "output are all space from %s\n", gethostname_ex);
272 /* start with '@' means error msg */
273 if (head[0] == '@') {
274 printerr(0, "error from %s: %s\n", gethostname_ex, &head[1]);
278 if (tail - head > namebuflen) {
279 printerr(0, "external hostname too long: %s\n", head);
283 printerr(2, "%s: net 0x%x, addr 0x%x => %s\n",
284 lnd, net, addr, head);
285 strcpy(namebuf, head);
289 struct convert_struct {
291 lnd_nid2hostname_t *nid2name;
294 static struct convert_struct converter[] = {
295 [0] = { "UNUSED0", NULL},
296 [SOCKLND] = { "SOCKLND", ipv4_nid2hostname },
297 [O2IBLND] = { "O2IBLND", ipv4_nid2hostname },
298 [LOLND] = { "LOLND", lolnd_nid2hostname },
299 [PTL4LND] = { "PTL4LND", external_nid2hostname },
302 #define LND_MAX (sizeof(converter) / sizeof(converter[0]))
304 int lnet_nid2hostname(lnet_nid_t nid, char *buf, int buflen)
306 uint32_t lnd, net, addr;
308 addr = LNET_NIDADDR(nid);
309 net = LNET_NIDNET(nid);
310 lnd = LNET_NETTYP(net);
312 if (lnd >= LND_MAX) {
313 printerr(0, "ERROR: Unrecognized LND %u\n", lnd);
317 if (converter[lnd].nid2name == NULL) {
318 printerr(0, "ERROR: %s converter not ready\n",
319 converter[lnd].name);
323 return converter[lnd].nid2name(converter[lnd].name, net, addr,
328 /****************************************
329 * user mapping database handling *
331 ****************************************/
333 #define MAPPING_GROW_SIZE 512
334 #define MAX_LINE_LEN 256
336 struct user_map_item {
337 char *principal; /* NULL means match all */
342 struct user_mapping {
344 struct user_map_item *items;
347 static struct user_mapping mapping = {0, NULL};
348 /* FIXME to be finished: monitor change of mapping database */
349 static int mapping_mtime = 0;
351 void cleanup_mapping(void)
354 for (; mapping.nitems > 0; mapping.nitems--)
355 if (mapping.items[mapping.nitems-1].principal)
356 free(mapping.items[mapping.nitems-1].principal);
359 mapping.items = NULL;
363 static int grow_mapping(int nitems)
365 struct user_map_item *new;
366 int oldsize, newsize;
368 oldsize = (mapping.nitems * sizeof(struct user_map_item) +
369 MAPPING_GROW_SIZE - 1) / MAPPING_GROW_SIZE;
370 newsize = (nitems * sizeof(struct user_map_item) +
371 MAPPING_GROW_SIZE - 1) / MAPPING_GROW_SIZE;
372 while (newsize <= oldsize)
375 newsize *= MAPPING_GROW_SIZE;
376 new = malloc(newsize);
378 printerr(0, "can't alloc mapping size %d\n", newsize);
383 memcpy(new, mapping.items,
384 mapping.nitems * sizeof(struct user_map_item));
391 uid_t parse_uid(char *uidstr)
397 pw = getpwnam(uidstr);
401 uid = strtol(uidstr, &p, 0);
408 static int read_mapping_db(void)
410 char princ[MAX_LINE_LEN];
411 char nid_str[MAX_LINE_LEN];
412 char dest[MAX_LINE_LEN];
413 char linebuf[MAX_LINE_LEN];
419 /* cleanup old mappings */
422 f = fopen(MAPPING_DATABASE_FILE, "r");
424 printerr(0, "can't open mapping database: %s\n",
425 MAPPING_DATABASE_FILE);
429 while ((line = fgets(linebuf, MAX_LINE_LEN, f)) != NULL) {
432 if (sscanf(line, "%s %s %s", princ, nid_str, dest) != 3) {
433 printerr(0, "mapping db: syntax error\n");
437 if (!strcmp(princ, "*")) {
440 name = strdup(princ);
442 printerr(0, "fail to dup str %s\n", princ);
447 if (!strcmp(nid_str, "*")) {
450 nid = libcfs_str2nid(nid_str);
451 if (nid == LNET_NID_ANY) {
452 printerr(0, "fail to parse nid %s\n", nid_str);
459 dest_uid = parse_uid(dest);
460 if (dest_uid == -1) {
461 printerr(0, "no valid user: %s\n", dest);
467 if (grow_mapping(mapping.nitems + 1)) {
468 printerr(0, "fail to grow mapping to %d\n",
476 mapping.items[mapping.nitems].principal = name;
477 mapping.items[mapping.nitems].nid = nid;
478 mapping.items[mapping.nitems].uid = dest_uid;
480 printerr(1, "add mapping: %s(%s/0x%llx) ==> %d\n",
481 name, nid_str, nid, dest_uid);
488 static inline int mapping_changed(void)
492 if (stat(MAPPING_DATABASE_FILE, &st) == -1) {
493 /* stat failed, treat it like doesn't exist or be removed */
494 if (mapping_mtime == 0) {
497 printerr(0, "Warning: stat %s failed: %s\n",
498 MAPPING_DATABASE_FILE, strerror(errno));
505 if (st.st_mtime != mapping_mtime) {
506 mapping_mtime = st.st_mtime;
513 int lookup_mapping(char *princ, lnet_nid_t nid, uid_t *uid)
519 /* FIXME race condition here */
520 if (mapping_changed()) {
521 if (read_mapping_db())
522 printerr(0, "all remote users will be denied\n");
525 for (n = 0; n < mapping.nitems; n++) {
526 struct user_map_item *entry = &mapping.items[n];
528 if (entry->nid != LNET_NID_ANY && entry->nid != nid)
530 if (!entry->principal || !strcasecmp(entry->principal, princ)) {
531 printerr(1, "found mapping: %s ==> %d\n",
538 printerr(2, "no mapping for %s/%#Lx\n", princ, nid);