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