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/
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>
63 # include "lgss_utils.h"
65 # include "err_util.h"
70 const char * lustre_svc_name[] =
72 [LUSTRE_GSS_SVC_MGS] = "MGS",
73 [LUSTRE_GSS_SVC_MDS] = "MDS",
74 [LUSTRE_GSS_SVC_OSS] = "OSS",
77 /****************************************
79 ****************************************/
81 static struct __sem_s {
86 [GSSD_CLI] = { "client", 0x3a92d473, 0 },
87 [GSSD_SVC] = { "server", 0x3b92d473, 0 },
90 void gssd_init_unique(int type)
92 struct __sem_s *sem = &sems[type];
95 assert(type == GSSD_CLI || type == GSSD_SVC);
98 sem->sem_id = semget(sem->sem_key, 1, IPC_CREAT | IPC_EXCL | 0700);
99 if (sem->sem_id == -1) {
100 if (errno != EEXIST) {
101 printerr(0, "Create sem: %s\n", strerror(errno));
105 /* already exist. Note there's still a small window racing
106 * with other processes, due to the stupid semaphore semantics.
108 sem->sem_id = semget(sem->sem_key, 0, 0700);
109 if (sem->sem_id == -1) {
110 if (errno == ENOENT) {
111 printerr(0, "another instance just exit, "
116 printerr(0, "Obtain sem: %s\n", strerror(errno));
122 if (semctl(sem->sem_id, 0, SETVAL, val) == -1) {
123 printerr(0, "Initialize sem: %s\n",
131 sembuf.sem_flg = IPC_NOWAIT | SEM_UNDO;
133 if (semop(sem->sem_id, &sembuf, 1) != 0) {
134 if (errno == EAGAIN) {
135 printerr(0, "Another instance is running, exit\n");
138 printerr(0, "Grab sem: %s\n", strerror(errno));
142 printerr(2, "Successfully created %s global identity\n", sem->name);
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(0, "%s: can't resolve 0x%x\n", lnd, addr);
179 if (strlen(ent->h_name) >= buflen) {
180 printerr(0, "%s: name too long: %s\n", lnd, ent->h_name);
183 strcpy(buf, ent->h_name);
185 printerr(2, "%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(0, "%s: addr is 0x%x, we expect 0\n", lnd, addr);
203 printerr(0, "%s: failed obtain local machine name\n", lnd);
207 ent = gethostbyname(uts.nodename);
209 printerr(0, "%s: failed obtain canonical name of %s\n",
214 if (strlen(ent->h_name) >= buflen) {
215 printerr(0, "%s: name too long: %s\n", lnd, ent->h_name);
218 strcpy(buf, ent->h_name);
220 printerr(3, "%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(2, "cmd: %s\n", buf);
240 fghn = popen(buf, "r");
242 printerr(0, "failed to call %s\n", gethostname_ex);
246 head = fgets(buf, bufsize, fghn);
248 printerr(0, "can't read from %s\n", gethostname_ex);
251 if (pclose(fghn) == -1)
252 printerr(1, "pclose failed, continue\n");
254 /* trim head/tail space */
255 while (is_space(*head))
258 tail = head + strlen(head);
260 printerr(0, "no output from %s\n", gethostname_ex);
263 while (is_space(*(tail - 1)))
266 printerr(0, "output are all space from %s\n", gethostname_ex);
271 /* start with '@' means error msg */
272 if (head[0] == '@') {
273 printerr(0, "error from %s: %s\n", gethostname_ex, &head[1]);
277 if (tail - head > namebuflen) {
278 printerr(0, "external hostname too long: %s\n", head);
282 printerr(2, "%s: net 0x%x, addr 0x%x => %s\n",
283 lnd, net, addr, head);
284 strcpy(namebuf, head);
288 struct convert_struct {
290 lnd_nid2hostname_t *nid2name;
293 static struct convert_struct converter[] = {
294 [0] = { .name = "UNUSED0" },
295 [SOCKLND] = { .name = "SOCKLND", .nid2name = ipv4_nid2hostname },
296 [O2IBLND] = { .name = "O2IBLND", .nid2name = ipv4_nid2hostname },
297 [LOLND] = { .name = "LOLND", .nid2name = lolnd_nid2hostname },
298 [PTL4LND] = { .name = "PTL4LND", .nid2name = external_nid2hostname }
301 #define LND_MAX (sizeof(converter) / sizeof(converter[0]))
303 int lnet_nid2hostname(lnet_nid_t nid, char *buf, int buflen)
305 uint32_t lnd, net, addr;
307 addr = LNET_NIDADDR(nid);
308 net = LNET_NIDNET(nid);
309 lnd = LNET_NETTYP(net);
311 if (lnd >= LND_MAX) {
312 printerr(0, "ERROR: Unrecognized LND %u\n", lnd);
316 if (converter[lnd].nid2name == NULL) {
317 printerr(0, "ERROR: %s converter not ready\n",
318 converter[lnd].name);
322 return converter[lnd].nid2name(converter[lnd].name, net, addr,
327 /****************************************
328 * user mapping database handling *
330 ****************************************/
332 #define MAPPING_GROW_SIZE 512
333 #define MAX_LINE_LEN 256
335 struct user_map_item {
336 char *principal; /* NULL means match all */
341 struct user_mapping {
343 struct user_map_item *items;
346 static struct user_mapping mapping;
347 /* FIXME to be finished: monitor change of mapping database */
348 static int mapping_mtime = 0;
350 void cleanup_mapping(void)
353 for (; mapping.nitems > 0; mapping.nitems--)
354 if (mapping.items[mapping.nitems-1].principal)
355 free(mapping.items[mapping.nitems-1].principal);
358 mapping.items = NULL;
362 static int grow_mapping(int nitems)
364 struct user_map_item *new;
365 int oldsize, newsize;
367 oldsize = (mapping.nitems * sizeof(struct user_map_item) +
368 MAPPING_GROW_SIZE - 1) / MAPPING_GROW_SIZE;
369 newsize = (nitems * sizeof(struct user_map_item) +
370 MAPPING_GROW_SIZE - 1) / MAPPING_GROW_SIZE;
371 while (newsize <= oldsize)
374 newsize *= MAPPING_GROW_SIZE;
375 new = malloc(newsize);
377 printerr(0, "can't alloc mapping size %d\n", newsize);
382 memcpy(new, mapping.items,
383 mapping.nitems * sizeof(struct user_map_item));
390 uid_t parse_uid(char *uidstr)
396 pw = getpwnam(uidstr);
400 uid = strtol(uidstr, &p, 0);
407 static int read_mapping_db(void)
409 char princ[MAX_LINE_LEN];
410 char nid_str[MAX_LINE_LEN];
411 char dest[MAX_LINE_LEN];
412 char linebuf[MAX_LINE_LEN];
418 /* cleanup old mappings */
421 f = fopen(MAPPING_DATABASE_FILE, "r");
423 printerr(0, "can't open mapping database: %s\n",
424 MAPPING_DATABASE_FILE);
428 while ((line = fgets(linebuf, MAX_LINE_LEN, f)) != NULL) {
431 if (sscanf(line, "%s %s %s", princ, nid_str, dest) != 3) {
432 printerr(0, "mapping db: syntax error\n");
436 if (!strcmp(princ, "*")) {
439 name = strdup(princ);
441 printerr(0, "fail to dup str %s\n", princ);
446 if (!strcmp(nid_str, "*")) {
449 nid = libcfs_str2nid(nid_str);
450 if (nid == LNET_NID_ANY) {
451 printerr(0, "fail to parse nid %s\n", nid_str);
458 dest_uid = parse_uid(dest);
459 if (dest_uid == -1) {
460 printerr(0, "no valid user: %s\n", dest);
466 if (grow_mapping(mapping.nitems + 1)) {
467 printerr(0, "fail to grow mapping to %d\n",
475 mapping.items[mapping.nitems].principal = name;
476 mapping.items[mapping.nitems].nid = nid;
477 mapping.items[mapping.nitems].uid = dest_uid;
479 printerr(1, "add mapping: %s(%s/0x%llx) ==> %d\n",
480 name, nid_str, nid, dest_uid);
487 static inline int mapping_changed(void)
491 if (stat(MAPPING_DATABASE_FILE, &st) == -1) {
492 /* stat failed, treat it like doesn't exist or be removed */
493 if (mapping_mtime == 0) {
496 printerr(0, "Warning: stat %s failed: %s\n",
497 MAPPING_DATABASE_FILE, strerror(errno));
504 if (st.st_mtime != mapping_mtime) {
505 mapping_mtime = st.st_mtime;
512 int lookup_mapping(char *princ, lnet_nid_t nid, uid_t *uid)
518 /* FIXME race condition here */
519 if (mapping_changed()) {
520 if (read_mapping_db())
521 printerr(0, "all remote users will be denied\n");
524 for (n = 0; n < mapping.nitems; n++) {
525 struct user_map_item *entry = &mapping.items[n];
527 if (entry->nid != LNET_NID_ANY && entry->nid != nid)
529 if (!entry->principal || !strcasecmp(entry->principal, princ)) {
530 printerr(1, "found mapping: %s ==> %d\n",
537 printerr(2, "no mapping for %s/%#Lx\n", princ, nid);