Whamcloud - gitweb
LU-17402 kernel: update dotdot patch path for RHEL 8.10
[fs/lustre-release.git] / lustre / utils / gss / lgss_utils.h
1 /*
2  * Modifications for Lustre
3  *
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  *
6  * Author: Eric Mei <ericm@clusterfs.com>
7  */
8
9 /*
10   Copyright (c) 2004 The Regents of the University of Michigan.
11   All rights reserved.
12
13   Redistribution and use in source and binary forms, with or without
14   modification, are permitted provided that the following conditions
15   are met:
16
17   1. Redistributions of source code must retain the above copyright
18      notice, this list of conditions and the following disclaimer.
19   2. Redistributions in binary form must reproduce the above copyright
20      notice, this list of conditions and the following disclaimer in the
21      documentation and/or other materials provided with the distribution.
22   3. Neither the name of the University nor the names of its
23      contributors may be used to endorse or promote products derived
24      from this software without specific prior written permission.
25
26   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifndef LGSS_UTILS_H
40 #define LGSS_UTILS_H
41
42 #include <inttypes.h>
43 #include <stdlib.h>
44 #include <stdint.h>
45 #include <gssapi/gssapi.h>
46
47 #include "lsupport.h"
48 #include "err_util.h"
49
50 #define LGSS_SVC_MGS_STR        "lustre_mgs"
51 #define LGSS_SVC_MDS_STR        "lustre_mds"
52 #define LGSS_SVC_OSS_STR        "lustre_oss"
53 #define LGSS_SVC_HOST_STR       "host"
54 #define LGSS_USR_ROOT_STR       "lustre_root"
55
56 typedef enum {
57         LGSS_SVC_MGS    = 0,
58         LGSS_SVC_MDS    = 1,
59         LGSS_SVC_OSS    = 2,
60         LGSS_SVC_MAX
61 } lgss_svc_t;
62
63 extern const char *lgss_svc_str[LGSS_SVC_MAX];
64
65 /****************************************
66  * inter-process locking                *
67  ****************************************/
68
69 typedef enum {
70         LGSS_MUTEX_KRB5 = 0,
71         LGSS_MUTEX_MAX
72 } lgss_mutex_id_t;
73
74 int lgss_mutex_lock(lgss_mutex_id_t mid);
75 int lgss_mutex_unlock(lgss_mutex_id_t mid);
76
77 /****************************************
78  * log facilities                       *
79  ****************************************/
80
81 extern loglevel_t g_log_level;
82
83 void lgss_set_loglevel(loglevel_t level);
84
85 void __logmsg(loglevel_t level, const char *func, const char *format, ...)
86         __attribute__((format(printf, 3, 4)));
87
88 void __logmsg_gss(loglevel_t level, const char *func, const gss_OID mech,
89                   uint32_t major, uint32_t minor, const char *format, ...)
90         __attribute__((format(printf, 6, 7)));
91
92 void log_hexl(int pri, unsigned char *cp, int length);
93 void log_hex(int pri, unsigned char *cp, int length);
94
95 #define logmsg(loglevel, format, args...)                               \
96 do {                                                                    \
97         if (loglevel <= g_log_level)                                    \
98                 __logmsg(loglevel, __FUNCTION__, format, ##args);       \
99 } while (0)
100
101 #define logmsg_gss(loglevel, mech, major, minor, format, args...)       \
102 do {                                                                    \
103         if (loglevel <= g_log_level)                                    \
104                 __logmsg_gss(loglevel, __FUNCTION__, mech,              \
105                              major, minor, format, ##args);             \
106 } while (0)
107
108 #define lassert(exp)                                                    \
109 do {                                                                    \
110         if (!(exp)) {                                                   \
111                 logmsg(LL_ERR, "ASSERTION FAILED: %s", #exp);           \
112                 exit(-1);                                               \
113         }                                                               \
114 } while (0)
115
116 /*
117  * for compatible reason, we're using files (context_xxx.c) from nfs-utils
118  */
119 #define printerr(priority, format, args...)                             \
120         logmsg(priority, format, ##args)
121
122 #define pgsserr(msg, maj_stat, min_stat, mech)                          \
123         logmsg_gss(LL_ERR, mech, maj_stat, min_stat, msg)
124
125 /****************************************
126  * GSS MECH, OIDs                       *
127  ****************************************/
128
129 extern gss_OID_desc krb5oid;
130 extern gss_OID_desc spkm3oid;
131 extern gss_OID_desc nulloid;
132 extern gss_OID_desc skoid;
133
134 /****************************************
135  * client credentials                   *
136  ****************************************/
137
138 struct lgss_cred;
139
140 struct lgss_mech_type {
141         char            *lmt_name;
142         enum lgss_mech   lmt_mech_n;
143
144         int              (*lmt_init)(void);
145         void             (*lmt_fini)(void);
146         int              (*lmt_prepare_cred)(struct lgss_cred *cred);
147         void             (*lmt_release_cred)(struct lgss_cred *cred);
148         int              (*lmt_using_cred)(struct lgss_cred *cred);
149         int              (*lmt_validate_cred)(struct lgss_cred *cred,
150                                               gss_buffer_desc *token,
151                                               gss_buffer_desc *ctx_token);
152 };
153
154 struct lgss_cred {
155         int                     lc_uid;
156         unsigned int            lc_root_flags;
157         uint64_t                lc_self_nid;
158         uint64_t                lc_tgt_nid;
159         uint32_t                lc_tgt_svc;
160         char                    lc_svc_type;
161         char                    *lc_tgt_uuid;
162
163         struct lgss_mech_type   *lc_mech;
164         void                    *lc_mech_cred;
165         gss_buffer_desc         lc_mech_token;
166 };
167
168 struct lgss_mech_type *lgss_name2mech(const char *mech_name);
169 int  lgss_mech_initialize(struct lgss_mech_type *mech);
170 void lgss_mech_finalize(struct lgss_mech_type *mech);
171
172 struct lgss_cred * lgss_create_cred(struct lgss_mech_type *mech);
173 void lgss_destroy_cred(struct lgss_cred *cred);
174 int lgss_prepare_cred(struct lgss_cred *cred);
175 void lgss_release_cred(struct lgss_cred *cred);
176 int lgss_using_cred(struct lgss_cred *cred);
177 int lgss_validate_cred(struct lgss_cred *cred, gss_buffer_desc *token,
178                        gss_buffer_desc *ctx_token);
179 void lgss_fini(struct lgss_cred *cred);
180
181 int lgss_get_service_str(char **string, uint32_t lsvc, uint64_t tgt_nid);
182 int switch_identity(uid_t uid);
183
184 static inline
185 int gss_OID_equal(gss_OID_desc *oid1, gss_OID_desc *oid2)
186 {
187         return (oid1->length == oid2->length &&
188                 memcmp(oid1->elements, oid2->elements, oid1->length) == 0);
189 }
190
191 #ifndef g_OID_equal
192 #define g_OID_equal(o1,o2)      gss_OID_equal((o1), (o2))
193 #endif
194
195 #endif /* LGSS_UTILS_H */