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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
36 #include <sys/param.h>
37 #include <sys/utsname.h>
39 #include <sys/socket.h>
40 #include <arpa/inet.h>
41 #include <sys/types.h>
62 # include "lgss_utils.h"
64 # include "err_util.h"
69 const char * lustre_svc_name[] =
71 [LUSTRE_GSS_SVC_MGS] = "MGS",
72 [LUSTRE_GSS_SVC_MDS] = "MDS",
73 [LUSTRE_GSS_SVC_OSS] = "OSS",
76 /****************************************
78 ****************************************/
80 static struct __sem_s {
85 [GSSD_CLI] = { "client", 0x3a92d473, 0 },
86 [GSSD_SVC] = { "server", 0x3b92d473, 0 },
89 void gssd_init_unique(int type)
91 struct __sem_s *sem = &sems[type];
94 assert(type == GSSD_CLI || type == GSSD_SVC);
97 sem->sem_id = semget(sem->sem_key, 1, IPC_CREAT | IPC_EXCL | 0700);
98 if (sem->sem_id == -1) {
99 if (errno != EEXIST) {
100 printerr(LL_ERR, "Create sem: %s\n", strerror(errno));
104 /* already exist. Note there's still a small window racing
105 * with other processes, due to the stupid semaphore semantics.
107 sem->sem_id = semget(sem->sem_key, 0, 0700);
108 if (sem->sem_id == -1) {
109 if (errno == ENOENT) {
111 "another instance just exit, try again\n");
115 printerr(LL_ERR, "Obtain sem: %s\n", strerror(errno));
121 if (semctl(sem->sem_id, 0, SETVAL, val) == -1) {
122 printerr(LL_ERR, "Initialize sem: %s\n",
130 sembuf.sem_flg = IPC_NOWAIT | SEM_UNDO;
132 if (semop(sem->sem_id, &sembuf, 1) != 0) {
133 if (errno == EAGAIN) {
134 printerr(LL_ERR, "Another instance is running, exit\n");
137 printerr(LL_ERR, "Grab sem: %s\n", strerror(errno));
141 printerr(LL_INFO, "Successfully created %s global identity\n",
145 void gssd_exit_unique(int type)
147 assert(type == GSSD_CLI || type == GSSD_SVC);
150 * do nothing. we can't remove the sem here, otherwise the race
151 * window would be much bigger. So it's sad we have to leave the
152 * sem in the system forever.
156 /****************************************
157 * client side resolvation: *
158 * lnd/netid/nid => hostname *
159 ****************************************/
161 char gethostname_ex[PATH_MAX] = GSSD_DEFAULT_GETHOSTNAME_EX;
163 typedef int lnd_nid2hostname_t(char *lnd, uint32_t net, uint32_t addr,
164 char *buf, int buflen);
166 /* FIXME what about IPv6? */
168 int ipv4_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
169 char *buf, int buflen)
174 ent = gethostbyaddr(&addr, sizeof(addr), AF_INET);
176 printerr(LL_ERR, "%s: can't resolve 0x%x\n", lnd, addr);
179 if (strlen(ent->h_name) >= buflen) {
180 printerr(LL_ERR, "%s: name too long: %s\n", lnd, ent->h_name);
183 strcpy(buf, ent->h_name);
185 printerr(LL_INFO, "%s: net 0x%x, addr 0x%x => %s\n",
186 lnd, net, addr, buf);
191 int lolnd_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
192 char *buf, int buflen)
198 printerr(LL_ERR, "%s: addr is 0x%x, we expect 0\n", lnd, addr);
203 printerr(LL_ERR, "%s: failed obtain local machine name\n", lnd);
207 ent = gethostbyname(uts.nodename);
209 printerr(LL_ERR, "%s: failed obtain canonical name of %s\n",
214 if (strlen(ent->h_name) >= buflen) {
215 printerr(LL_ERR, "%s: name too long: %s\n", lnd, ent->h_name);
218 strcpy(buf, ent->h_name);
220 printerr(LL_DEBUG, "%s: addr 0x%x => %s\n", lnd, addr, buf);
224 static int is_space(char c)
226 return (c == ' ' || c == '\t' || c == '\n');
230 int external_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
231 char *namebuf, int namebuflen)
233 const int bufsize = PATH_MAX + 256;
234 char buf[bufsize], *head, *tail;
237 sprintf(buf, "%s %s 0x%x 0x%x", gethostname_ex, lnd, net, addr);
238 printerr(LL_INFO, "cmd: %s\n", buf);
240 fghn = popen(buf, "r");
242 printerr(LL_ERR, "failed to call %s\n", gethostname_ex);
246 head = fgets(buf, bufsize, fghn);
248 printerr(LL_ERR, "can't read from %s\n", gethostname_ex);
252 if (pclose(fghn) == -1)
253 printerr(LL_WARN, "pclose failed, continue\n");
255 /* trim head/tail space */
256 while (is_space(*head))
259 tail = head + strlen(head);
261 printerr(LL_ERR, "no output from %s\n", gethostname_ex);
264 while (is_space(*(tail - 1)))
267 printerr(LL_ERR, "output are all space from %s\n",
273 /* start with '@' means error msg */
274 if (head[0] == '@') {
275 printerr(LL_ERR, "error from %s: %s\n",
276 gethostname_ex, &head[1]);
280 if (tail - head > namebuflen) {
281 printerr(LL_ERR, "external hostname too long: %s\n", head);
285 printerr(LL_INFO, "%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] = { .name = "UNUSED0" },
298 [SOCKLND] = { .name = "SOCKLND", .nid2name = ipv4_nid2hostname },
299 [O2IBLND] = { .name = "O2IBLND", .nid2name = ipv4_nid2hostname },
300 [LOLND] = { .name = "LOLND", .nid2name = lolnd_nid2hostname },
301 [PTL4LND] = { .name = "PTL4LND", .nid2name = external_nid2hostname },
302 [KFILND] = { .name = "KFILND", .nid2name = ipv4_nid2hostname }
305 #define LND_MAX (sizeof(converter) / sizeof(converter[0]))
307 int lnet_nid2hostname(lnet_nid_t nid, char *buf, int buflen)
309 uint32_t lnd, net, addr;
311 addr = LNET_NIDADDR(nid);
312 net = LNET_NIDNET(nid);
313 lnd = LNET_NETTYP(net);
315 if (lnd >= LND_MAX) {
316 printerr(LL_ERR, "ERROR: Unrecognized LND %u\n", lnd);
320 if (converter[lnd].nid2name == NULL) {
321 printerr(LL_ERR, "ERROR: %s converter not ready\n",
322 converter[lnd].name);
326 return converter[lnd].nid2name(converter[lnd].name, net, addr,
331 /****************************************
332 * user mapping database handling *
334 ****************************************/
336 #define MAPPING_GROW_SIZE 512
337 #define MAX_LINE_LEN 256
339 struct user_map_item {
340 char *principal; /* NULL means match all */
345 struct user_mapping {
347 struct user_map_item *items;
350 static struct user_mapping mapping;
351 /* FIXME to be finished: monitor change of mapping database */
352 static int mapping_mtime = 0;
354 void cleanup_mapping(void)
357 for (; mapping.nitems > 0; mapping.nitems--)
358 if (mapping.items[mapping.nitems-1].principal)
359 free(mapping.items[mapping.nitems-1].principal);
362 mapping.items = NULL;
366 static int grow_mapping(int nitems)
368 struct user_map_item *new;
369 int oldsize, newsize;
371 oldsize = (mapping.nitems * sizeof(struct user_map_item) +
372 MAPPING_GROW_SIZE - 1) / MAPPING_GROW_SIZE;
373 newsize = (nitems * sizeof(struct user_map_item) +
374 MAPPING_GROW_SIZE - 1) / MAPPING_GROW_SIZE;
375 while (newsize <= oldsize)
378 newsize *= MAPPING_GROW_SIZE;
379 new = malloc(newsize);
381 printerr(LL_ERR, "can't alloc mapping size %d\n", newsize);
386 memcpy(new, mapping.items,
387 mapping.nitems * sizeof(struct user_map_item));
394 uid_t parse_uid(char *uidstr)
400 pw = getpwnam(uidstr);
404 uid = strtol(uidstr, &p, 0);
411 static int read_mapping_db(void)
413 char princ[MAX_LINE_LEN];
414 char nid_str[MAX_LINE_LEN];
415 char dest[MAX_LINE_LEN];
416 char linebuf[MAX_LINE_LEN];
422 /* cleanup old mappings */
425 f = fopen(MAPPING_DATABASE_FILE, "r");
427 printerr(LL_ERR, "can't open mapping database: %s\n",
428 MAPPING_DATABASE_FILE);
432 while ((line = fgets(linebuf, MAX_LINE_LEN, f)) != NULL) {
435 if (sscanf(line, "%s %s %s", princ, nid_str, dest) != 3) {
436 printerr(LL_ERR, "mapping db: syntax error\n");
440 if (!strcmp(princ, "*")) {
443 name = strdup(princ);
445 printerr(LL_ERR, "fail to dup str %s\n", princ);
450 if (!strcmp(nid_str, "*")) {
453 nid = libcfs_str2nid(nid_str);
454 if (nid == LNET_NID_ANY) {
455 printerr(LL_ERR, "fail to parse nid %s\n",
463 dest_uid = parse_uid(dest);
464 if (dest_uid == -1) {
465 printerr(LL_ERR, "no valid user: %s\n", dest);
471 if (grow_mapping(mapping.nitems + 1)) {
472 printerr(LL_ERR, "fail to grow mapping to %d\n",
480 mapping.items[mapping.nitems].principal = name;
481 mapping.items[mapping.nitems].nid = nid;
482 mapping.items[mapping.nitems].uid = dest_uid;
484 printerr(LL_WARN, "add mapping: %s(%s/0x%llx) ==> %d\n",
485 name, nid_str, nid, dest_uid);
492 static inline int mapping_changed(void)
496 if (stat(MAPPING_DATABASE_FILE, &st) == -1) {
497 /* stat failed, treat it like doesn't exist or be removed */
498 if (mapping_mtime == 0)
501 printerr(LL_ERR, "stat %s failed: %s\n",
502 MAPPING_DATABASE_FILE, strerror(errno));
508 "Use of idmap.conf is deprecated.\nPlease consider switching to auth_to_local or equivalent as provided by Kerberos for cross-realm trust remapping.\n");
511 if (st.st_mtime != mapping_mtime) {
512 mapping_mtime = st.st_mtime;
519 void load_mapping(void)
521 if (mapping_changed())
522 (void)read_mapping_db();
525 int mapping_empty(void)
527 return !mapping.nitems;
530 int lookup_mapping(char *princ, lnet_nid_t nid, uid_t *uid)
536 /* FIXME race condition here */
537 if (mapping_changed()) {
538 if (read_mapping_db())
539 printerr(LL_ERR, "all remote users will be denied\n");
542 for (n = 0; n < mapping.nitems; n++) {
543 struct user_map_item *entry = &mapping.items[n];
545 if (entry->nid != LNET_NID_ANY && entry->nid != nid)
547 if (!entry->principal || !strcasecmp(entry->principal, princ)) {
548 printerr(LL_WARN, "found mapping: %s ==> %d\n",
555 printerr(LL_INFO, "no mapping for %s/%#Lx\n", princ, nid);