Whamcloud - gitweb
LU-6142 lustre: mark strings in char arrays as const
[fs/lustre-release.git] / lustre / utils / gss / lsupport.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2014, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #ifndef _GNU_SOURCE
34 #define _GNU_SOURCE
35 #endif
36 #include "config.h"
37 #include <sys/param.h>
38 #include <sys/utsname.h>
39 #include <sys/stat.h>
40 #include <sys/socket.h>
41 #include <arpa/inet.h>
42 #include <sys/types.h>
43 #include <sys/ipc.h>
44 #include <sys/sem.h>
45
46 #include <stdbool.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <pwd.h>
50 #include <grp.h>
51 #include <string.h>
52 #include <dirent.h>
53 #include <poll.h>
54 #include <fcntl.h>
55 #include <signal.h>
56 #include <unistd.h>
57 #include <errno.h>
58 #include <assert.h>
59 #ifdef HAVE_NETDB_H
60 # include <netdb.h>
61 #endif
62 #ifdef _NEW_BUILD_
63 # include "lgss_utils.h"
64 #else
65 # include "err_util.h"
66 # include "gssd.h"
67 #endif
68 #include "lsupport.h"
69
70 const char * lustre_svc_name[] =
71 {
72         [LUSTRE_GSS_SVC_MGS]    = "MGS",
73         [LUSTRE_GSS_SVC_MDS]    = "MDS",
74         [LUSTRE_GSS_SVC_OSS]    = "OSS",
75 };
76
77 /****************************************
78  * exclusive startup                    *
79  ****************************************/
80
81 static struct __sem_s {
82         char           *name;
83         key_t           sem_key;
84         int             sem_id;
85 } sems[2] = {
86         [GSSD_CLI] = { "client",  0x3a92d473, 0 },
87         [GSSD_SVC] = { "server",  0x3b92d473, 0 },
88 };
89
90 void gssd_init_unique(int type)
91 {
92         struct __sem_s *sem = &sems[type];
93         struct sembuf   sembuf;
94
95         assert(type == GSSD_CLI || type == GSSD_SVC);
96
97 again:
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));
102                         exit(-1);
103                 }
104
105                 /* already exist. Note there's still a small window racing
106                  * with other processes, due to the stupid semaphore semantics.
107                  */
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, "
112                                          "try again\n");
113                                 goto again;
114                         }
115
116                         printerr(0, "Obtain sem: %s\n", strerror(errno));
117                         exit(-1);
118                 }
119         } else {
120                 int val = 1;
121
122                 if (semctl(sem->sem_id, 0, SETVAL, val) == -1) {
123                         printerr(0, "Initialize sem: %s\n",
124                                  strerror(errno));
125                         exit(-1);
126                 }
127         }
128
129         sembuf.sem_num = 0;
130         sembuf.sem_op = -1;
131         sembuf.sem_flg = IPC_NOWAIT | SEM_UNDO;
132
133         if (semop(sem->sem_id, &sembuf, 1) != 0) {
134                 if (errno == EAGAIN) {
135                         printerr(0, "Another instance is running, exit\n");
136                         exit(0);
137                 }
138                 printerr(0, "Grab sem: %s\n", strerror(errno));
139                 exit(0);
140         }
141
142         printerr(2, "Successfully created %s global identity\n", sem->name);
143 }
144
145 void gssd_exit_unique(int type)
146 {
147         assert(type == GSSD_CLI || type == GSSD_SVC);
148
149         /*
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.
153          */
154 }
155
156 /****************************************
157  * client side resolvation:             *
158  *    lnd/netid/nid => hostname         *
159  ****************************************/
160
161 char gethostname_ex[PATH_MAX] = GSSD_DEFAULT_GETHOSTNAME_EX;
162
163 typedef int lnd_nid2hostname_t(char *lnd, uint32_t net, uint32_t addr,
164                                char *buf, int buflen);
165
166 /* FIXME what about IPv6? */
167 static
168 int ipv4_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
169                       char *buf, int buflen)
170 {
171         struct hostent  *ent;
172
173         addr = htonl(addr);
174         ent = gethostbyaddr(&addr, sizeof(addr), AF_INET);
175         if (!ent) {
176                 printerr(0, "%s: can't resolve 0x%x\n", lnd, addr);
177                 return -1;
178         }
179         if (strlen(ent->h_name) >= buflen) {
180                 printerr(0, "%s: name too long: %s\n", lnd, ent->h_name);
181                 return -1;
182         }
183         strcpy(buf, ent->h_name);
184
185         printerr(2, "%s: net 0x%x, addr 0x%x => %s\n",
186                  lnd, net, addr, buf);
187         return 0;
188 }
189
190 static
191 int lolnd_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
192                        char *buf, int buflen)
193 {
194         struct utsname   uts;
195         struct hostent  *ent;
196
197         if (addr) {
198                 printerr(0, "%s: addr is 0x%x, we expect 0\n", lnd, addr);
199                 return -1;
200         }
201
202         if (uname(&uts)) {
203                 printerr(0, "%s: failed obtain local machine name\n", lnd);
204                 return -1;
205         }
206
207         ent = gethostbyname(uts.nodename);
208         if (!ent) {
209                 printerr(0, "%s: failed obtain canonical name of %s\n",
210                          lnd, uts.nodename);
211                 return -1;
212         }
213
214         if (strlen(ent->h_name) >= buflen) {
215                 printerr(0, "%s: name too long: %s\n", lnd, ent->h_name);
216                 return -1;
217         }
218         strcpy(buf, ent->h_name);
219
220         printerr(3, "%s: addr 0x%x => %s\n", lnd, addr, buf);
221         return 0;
222 }
223
224 static int is_space(char c)
225 {
226         return (c == ' ' || c == '\t' || c == '\n');
227 }
228
229 static
230 int external_nid2hostname(char *lnd, uint32_t net, uint32_t addr,
231                           char *namebuf, int namebuflen)
232 {
233         const int bufsize = PATH_MAX + 256;
234         char buf[bufsize], *head, *tail;
235         FILE *fghn;
236
237         sprintf(buf, "%s %s 0x%x 0x%x", gethostname_ex, lnd, net, addr);
238         printerr(2, "cmd: %s\n", buf);
239
240         fghn = popen(buf, "r");
241         if (fghn == NULL) {
242                 printerr(0, "failed to call %s\n", gethostname_ex);
243                 return -1;
244         }
245
246         head = fgets(buf, bufsize, fghn);
247         if (head == NULL) {
248                 printerr(0, "can't read from %s\n", gethostname_ex);
249                 pclose(fghn);
250                 return -1;
251         }
252         if (pclose(fghn) == -1)
253                 printerr(1, "pclose failed, continue\n");
254
255         /* trim head/tail space */
256         while (is_space(*head))
257                 head++;
258
259         tail = head + strlen(head);
260         if (tail <= head) {
261                 printerr(0, "no output from %s\n", gethostname_ex);
262                 return -1;
263         }
264         while (is_space(*(tail - 1)))
265                 tail--;
266         if (tail <= head) {
267                 printerr(0, "output are all space from %s\n", gethostname_ex);
268                 return -1;
269         }
270         *tail = '\0';
271
272         /* start with '@' means error msg */
273         if (head[0] == '@') {
274                 printerr(0, "error from %s: %s\n", gethostname_ex, &head[1]);
275                 return -1;
276         }
277
278         if (tail - head > namebuflen) {
279                 printerr(0, "external hostname too long: %s\n", head);
280                 return -1;
281         }
282
283         printerr(2, "%s: net 0x%x, addr 0x%x => %s\n",
284                  lnd, net, addr, head);
285         strcpy(namebuf, head);
286         return 0;
287 }
288
289 struct convert_struct {
290         char                    *name;
291         lnd_nid2hostname_t      *nid2name;
292 };
293
294 static struct convert_struct converter[] = {
295         [0]       = { .name = "UNUSED0" },
296         [SOCKLND] = { .name = "SOCKLND", .nid2name = ipv4_nid2hostname },
297         [O2IBLND] = { .name = "O2IBLND", .nid2name = ipv4_nid2hostname },
298         [LOLND]   = { .name = "LOLND",   .nid2name = lolnd_nid2hostname },
299         [PTL4LND] = { .name = "PTL4LND", .nid2name = external_nid2hostname }
300 };
301
302 #define LND_MAX         (sizeof(converter) / sizeof(converter[0]))
303
304 int lnet_nid2hostname(lnet_nid_t nid, char *buf, int buflen)
305 {
306         uint32_t lnd, net, addr;
307
308         addr = LNET_NIDADDR(nid);
309         net = LNET_NIDNET(nid);
310         lnd = LNET_NETTYP(net);
311
312         if (lnd >= LND_MAX) {
313                 printerr(0, "ERROR: Unrecognized LND %u\n", lnd);
314                 return -1;
315         }
316
317         if (converter[lnd].nid2name == NULL) {
318                 printerr(0, "ERROR: %s converter not ready\n",
319                         converter[lnd].name);
320                 return -1;
321         }
322
323         return converter[lnd].nid2name(converter[lnd].name, net, addr,
324                                        buf, buflen);
325 }
326
327
328 /****************************************
329  * user mapping database handling       *
330  * (very rudiment)                      *
331  ****************************************/
332
333 #define MAPPING_GROW_SIZE       512
334 #define MAX_LINE_LEN            256
335
336 struct user_map_item {
337         char        *principal; /* NULL means match all */
338         lnet_nid_t   nid;
339         uid_t        uid;
340 };
341
342 struct user_mapping {
343         int                   nitems;
344         struct user_map_item *items;
345 };
346
347 static struct user_mapping mapping;
348 /* FIXME to be finished: monitor change of mapping database */
349 static int mapping_mtime = 0;
350
351 void cleanup_mapping(void)
352 {
353         if (mapping.items) {
354                 for (; mapping.nitems > 0; mapping.nitems--)
355                         if (mapping.items[mapping.nitems-1].principal)
356                                 free(mapping.items[mapping.nitems-1].principal);
357
358                 free(mapping.items);
359                 mapping.items = NULL;
360         }
361 }
362
363 static int grow_mapping(int nitems)
364 {
365         struct user_map_item *new;
366         int oldsize, newsize;
367
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)
373                 return 0;
374
375         newsize *= MAPPING_GROW_SIZE;
376         new = malloc(newsize);
377         if (!new) {
378                 printerr(0, "can't alloc mapping size %d\n", newsize);
379                 return -1;
380         }
381
382         if (mapping.items) {
383                 memcpy(new, mapping.items,
384                        mapping.nitems * sizeof(struct user_map_item));
385                 free(mapping.items);
386         }
387         mapping.items = new;
388         return 0;
389 }
390
391 uid_t parse_uid(char *uidstr)
392 {
393         struct passwd *pw;
394         char *p = NULL;
395         long uid;
396
397         pw = getpwnam(uidstr);
398         if (pw)
399                 return pw->pw_uid;
400
401         uid = strtol(uidstr, &p, 0);
402         if (*p == '\0')
403                 return (uid_t) uid;
404
405         return -1;
406 }
407
408 static int read_mapping_db(void)
409 {
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];
414         char *line;
415         lnet_nid_t nid;
416         uid_t dest_uid;
417         FILE *f;
418
419         /* cleanup old mappings */
420         cleanup_mapping();
421
422         f = fopen(MAPPING_DATABASE_FILE, "r");
423         if (!f) {
424                 printerr(0, "can't open mapping database: %s\n",
425                          MAPPING_DATABASE_FILE);
426                 return -1;
427         }
428
429         while ((line = fgets(linebuf, MAX_LINE_LEN, f)) != NULL) {
430                 char *name;
431
432                 if (sscanf(line, "%s %s %s", princ, nid_str, dest) != 3) {
433                         printerr(0, "mapping db: syntax error\n");
434                         continue;
435                 }
436
437                 if (!strcmp(princ, "*")) {
438                         name = NULL;
439                 } else {
440                         name = strdup(princ);
441                         if (!name) {
442                                 printerr(0, "fail to dup str %s\n", princ);
443                                 continue;
444                         }
445                 }
446
447                 if (!strcmp(nid_str, "*")) {
448                         nid = LNET_NID_ANY;
449                 } else {
450                         nid = libcfs_str2nid(nid_str);
451                         if (nid == LNET_NID_ANY) {
452                                 printerr(0, "fail to parse nid %s\n", nid_str);
453                                 if (name)
454                                 free(name);
455                                 continue;
456                         }
457                 }
458
459                 dest_uid = parse_uid(dest);
460                 if (dest_uid == -1) {
461                         printerr(0, "no valid user: %s\n", dest);
462                         if (name)
463                         free(name);
464                         continue;
465                 }
466
467                 if (grow_mapping(mapping.nitems + 1)) {
468                         printerr(0, "fail to grow mapping to %d\n",
469                                  mapping.nitems + 1);
470                         if (name)
471                         free(name);
472                         fclose(f);
473                         return -1;
474                 }
475
476                 mapping.items[mapping.nitems].principal = name;
477                 mapping.items[mapping.nitems].nid = nid;
478                 mapping.items[mapping.nitems].uid = dest_uid;
479                 mapping.nitems++;
480                 printerr(1, "add mapping: %s(%s/0x%llx) ==> %d\n",
481                          name, nid_str, nid, dest_uid);
482         }
483
484         fclose(f);
485         return 0;
486 }
487
488 static inline int mapping_changed(void)
489 {
490         struct stat st;
491
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) {
495                         return 0;
496                 } else {
497                         printerr(0, "Warning: stat %s failed: %s\n",
498                                  MAPPING_DATABASE_FILE, strerror(errno));
499
500                         mapping_mtime = 0;
501                         return 1;
502                 }
503         }
504
505         if (st.st_mtime != mapping_mtime) {
506                 mapping_mtime = st.st_mtime;
507                 return 1;
508         }
509
510         return 0;
511 }
512
513 int lookup_mapping(char *princ, lnet_nid_t nid, uid_t *uid)
514 {
515         int n;
516
517         *uid = -1;
518
519         /* FIXME race condition here */
520         if (mapping_changed()) {
521                 if (read_mapping_db())
522                         printerr(0, "all remote users will be denied\n");
523         }
524
525         for (n = 0; n < mapping.nitems; n++) {
526                 struct user_map_item *entry = &mapping.items[n];
527
528                 if (entry->nid != LNET_NID_ANY && entry->nid != nid)
529                         continue;
530                 if (!entry->principal || !strcasecmp(entry->principal, princ)) {
531                         printerr(1, "found mapping: %s ==> %d\n",
532                                  princ, entry->uid);
533                         *uid = entry->uid;
534                         return 0;
535                 }
536         }
537
538         printerr(2, "no mapping for %s/%#Lx\n", princ, nid);
539         return -1;
540 }