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