Whamcloud - gitweb
eaa30cd10439b1be4f89cd85f1d20469427e9214
[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 <stdlib.h>
43 #include <stdint.h>
44 #include <gssapi/gssapi.h>
45
46 #define LGSS_SVC_MGS_STR        "lustre_mgs"
47 #define LGSS_SVC_MDS_STR        "lustre_mds"
48 #define LGSS_SVC_OSS_STR        "lustre_oss"
49 #define LGSS_USR_ROOT_STR       "lustre_root"
50
51 typedef enum {
52         LGSS_SVC_MGS    = 0,
53         LGSS_SVC_MDS    = 1,
54         LGSS_SVC_OSS    = 2,
55         LGSS_SVC_MAX
56 } lgss_svc_t;
57
58 extern const char *lgss_svc_str[LGSS_SVC_MAX];
59
60 /****************************************
61  * inter-process locking                *
62  ****************************************/
63
64 typedef enum {
65         LGSS_MUTEX_KRB5         = 0,
66         LGSS_MUTEX_MAX
67 } lgss_mutex_id_t;
68
69 int lgss_mutex_lock(lgss_mutex_id_t mid);
70 int lgss_mutex_unlock(lgss_mutex_id_t mid);
71
72 /****************************************
73  * log facilities                       *
74  ****************************************/
75
76 /*
77  * log level:
78  * LL_ERR:      critical error messages
79  * LL_WARN:     warning (default)
80  * LL_INFO:     important infomation
81  * LL_DEBUG:    debugging
82  * LL_TRACE:    excessive tracing messages
83  */
84 typedef enum {
85         LL_ERR          = 0,
86         LL_WARN         = 1,
87         LL_INFO         = 2,
88         LL_DEBUG        = 3,
89         LL_TRACE        = 4,
90         LL_MAX
91 } loglevel_t;
92
93 extern loglevel_t g_log_level;
94
95 void lgss_set_loglevel(loglevel_t level);
96
97 void __logmsg(loglevel_t level, const char *func, const char *format, ...);
98 void __logmsg_gss(loglevel_t level, const char *func, const gss_OID mech,
99                   uint32_t major, uint32_t minor, const char *format, ...);
100
101 #define logmsg(loglevel, format, args...)                               \
102 do {                                                                    \
103         if (loglevel <= g_log_level)                                    \
104                 __logmsg(loglevel, __FUNCTION__, format, ##args);       \
105 } while (0)
106
107 #define logmsg_gss(loglevel, mech, major, minor, format, args...)       \
108 do {                                                                    \
109         if (loglevel <= g_log_level)                                    \
110                 __logmsg_gss(loglevel, __FUNCTION__, mech,              \
111                              major, minor, format, ##args);             \
112 } while (0)
113
114 #define lassert(exp)                                                    \
115 do {                                                                    \
116         if (!(exp)) {                                                   \
117                 logmsg(LL_ERR, "ASSERTION FAILED: %s", #exp);           \
118                 exit(-1);                                               \
119         }                                                               \
120 } while (0)
121
122 /*
123  * for compatible reason, we're using files (context_xxx.c) from nfs-utils
124  */
125 #define printerr(priority, format, args...)                             \
126         logmsg(priority, format, ##args)
127
128 #define pgsserr(msg, maj_stat, min_stat, mech)                          \
129         logmsg_gss(LL_ERR, mech, maj_stat, min_stat, "")
130
131 /****************************************
132  * GSS MECH, OIDs                       *
133  ****************************************/
134
135 extern gss_OID_desc krb5oid;
136 extern gss_OID_desc spkm3oid;
137
138 typedef enum {
139         LGSS_MECH_KRB5  = 0,
140 } lgss_mech_t;
141
142 /****************************************
143  * client credentials                   *
144  ****************************************/
145
146 struct lgss_cred;
147
148 struct lgss_mech_type {
149         char               *lmt_name;
150         lgss_mech_t         lmt_mech_n;
151
152         int                (*lmt_init)(void);
153         void               (*lmt_fini)(void);
154         int                (*lmt_prepare_cred)(struct lgss_cred *cred);
155         void               (*lmt_release_cred)(struct lgss_cred *cred);
156         int                (*lmt_using_cred)(struct lgss_cred *cred);
157 };
158
159 enum {
160         LGSS_ROOT_CRED_ROOT     = 0x01,
161         LGSS_ROOT_CRED_MDT      = 0x02,
162         LGSS_ROOT_CRED_OST      = 0x04,
163
164         LGSS_ROOT_CRED_NR       = 3
165 };
166
167 struct lgss_cred {
168         int                     lc_uid;
169         unsigned int            lc_root_flags;
170         uint64_t                lc_self_nid;
171         uint64_t                lc_tgt_nid;
172         uint32_t                lc_tgt_svc;
173
174         struct lgss_mech_type  *lc_mech;
175         void                   *lc_mech_cred;
176 };
177
178 struct lgss_mech_type *lgss_name2mech(const char *mech_name);
179 int  lgss_mech_initialize(struct lgss_mech_type *mech);
180 void lgss_mech_finalize(struct lgss_mech_type *mech);
181
182 struct lgss_cred * lgss_create_cred(struct lgss_mech_type *mech);
183 void lgss_destroy_cred(struct lgss_cred *cred);
184 int lgss_prepare_cred(struct lgss_cred *cred);
185 void lgss_release_cred(struct lgss_cred *cred);
186 int lgss_using_cred(struct lgss_cred *cred);
187
188 int lgss_get_service_str(char **string, uint32_t lsvc, uint64_t tgt_nid);
189
190
191 extern gss_OID_desc krb5oid;
192 extern gss_OID_desc spkm3oid;
193
194 static inline
195 int gss_OID_equal(gss_OID_desc *oid1, gss_OID_desc *oid2)
196 {
197         return (oid1->length == oid2->length &&
198                 memcmp(oid1->elements, oid2->elements, oid1->length) == 0);
199 }
200
201 #ifndef g_OID_equal
202 #define g_OID_equal(o1,o2)      gss_OID_equal((o1), (o2))
203 #endif
204
205 #endif /* LGSS_UTILS_H */