]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/gssd/gss_util.c
gssd: By default, don't spam syslog when users' credentials expire
[nfs-utils.git] / utils / gssd / gss_util.c
1 /*
2  *  Adapted in part from MIT Kerberos 5-1.2.1 slave/kprop.c and from
3  *  http://docs.sun.com/?p=/doc/816-1331/6m7oo9sms&a=view
4  *
5  *  Copyright (c) 2002 The Regents of the University of Michigan.
6  *  All rights reserved.
7  *
8  *  Andy Adamson <andros@umich.edu>
9  *  J. Bruce Fields <bfields@umich.edu>
10  *  Marius Aamodt Eriksen <marius@umich.edu>
11  */
12
13 /*
14  * slave/kprop.c
15  *
16  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
17  * All Rights Reserved.
18  *
19  * Export of this software from the United States of America may
20  *   require a specific license from the United States Government.
21  *   It is the responsibility of any person or organization contemplating
22  *   export to obtain such a license before exporting.
23  *
24  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
25  * distribute this software and its documentation for any purpose and
26  * without fee is hereby granted, provided that the above copyright
27  * notice appear in all copies and that both that copyright notice and
28  * this permission notice appear in supporting documentation, and that
29  * the name of M.I.T. not be used in advertising or publicity pertaining
30  * to distribution of the software without specific, written prior
31  * permission.  Furthermore if you modify this software you must label
32  * your software as modified software and not distribute it in such a
33  * fashion that it might be confused with the original M.I.T. software.
34  * M.I.T. makes no representations about the suitability of
35  * this software for any purpose.  It is provided "as is" without express
36  * or implied warranty.
37  */
38
39 /*
40  * Copyright 1994 by OpenVision Technologies, Inc.
41  *
42  * Permission to use, copy, modify, distribute, and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appears in all copies and
45  * that both that copyright notice and this permission notice appear in
46  * supporting documentation, and that the name of OpenVision not be used
47  * in advertising or publicity pertaining to distribution of the software
48  * without specific, written prior permission. OpenVision makes no
49  * representations about the suitability of this software for any
50  * purpose.  It is provided "as is" without express or implied warranty.
51  *
52  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
53  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
54  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
55  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
56  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
57  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
58  * PERFORMANCE OF THIS SOFTWARE.
59  */
60 #include "config.h"
61 #include <errno.h>
62 #include <stdio.h>
63 #include <ctype.h>
64 #include <sys/file.h>
65 #include <signal.h>
66 #include <string.h>
67 #include <sys/types.h>
68 #include <sys/time.h>
69 #include <sys/stat.h>
70 #include <sys/socket.h>
71 #include <netinet/in.h>
72 #include <sys/param.h>
73 #include <netdb.h>
74 #include <fcntl.h>
75 #include <gssapi/gssapi.h>
76 #if defined(HAVE_KRB5) && !defined(GSS_C_NT_HOSTBASED_SERVICE)
77 #include <gssapi/gssapi_generic.h>
78 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
79 #endif
80 #include "gss_util.h"
81 #include "err_util.h"
82 #include "gssd.h"
83 #ifdef HAVE_UNISTD_H
84 #include <unistd.h>
85 #endif
86 #include <stdlib.h>
87 #ifdef HAVE_COM_ERR_H
88 #include <com_err.h>
89 #endif
90
91 /* Global gssd_credentials handle */
92 gss_cred_id_t gssd_creds;
93
94 gss_OID g_mechOid = GSS_C_NULL_OID;;
95
96 #if 0
97 static void
98 display_status_1(char *m, u_int32_t code, int type, const gss_OID mech)
99 {
100         u_int32_t maj_stat, min_stat;
101         gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
102         u_int32_t msg_ctx = 0;
103         char *typestr;
104
105         switch (type) {
106         case GSS_C_GSS_CODE:
107                 typestr = "GSS";
108                 break;
109         case GSS_C_MECH_CODE:
110                 typestr = "mechanism";
111                 break;
112         default:
113                 return;
114                 /* NOTREACHED */
115         }
116
117         for (;;) {
118                 maj_stat = gss_display_status(&min_stat, code,
119                     type, mech, &msg_ctx, &msg);
120                 if (maj_stat != GSS_S_COMPLETE) {
121                         printerr(0, "ERROR: in call to "
122                                 "gss_display_status called from %s\n", m);
123                         break;
124                 } else {
125                         printerr(0, "ERROR: GSS-API: (%s) error in %s(): %s\n",
126                             typestr, m, (char *)msg.value);
127                 }
128
129                 if (msg.length != 0)
130                         (void) gss_release_buffer(&min_stat, &msg);
131
132                 if (msg_ctx == 0)
133                         break;
134         }
135 }
136 #endif
137
138 static void
139 display_status_2(char *m, u_int32_t major, u_int32_t minor, const gss_OID mech)
140 {
141         u_int32_t maj_stat1, min_stat1;
142         u_int32_t maj_stat2, min_stat2;
143         gss_buffer_desc maj_gss_buf = GSS_C_EMPTY_BUFFER;
144         gss_buffer_desc min_gss_buf = GSS_C_EMPTY_BUFFER;
145         char maj_buf[30], min_buf[30];
146         char *maj, *min;
147         u_int32_t msg_ctx = 0;
148         int msg_verbosity = 0;
149
150         /* Get major status message */
151         maj_stat1 = gss_display_status(&min_stat1, major,
152                 GSS_C_GSS_CODE, mech, &msg_ctx, &maj_gss_buf);
153
154         if (maj_stat1 != GSS_S_COMPLETE) {
155                 snprintf(maj_buf, sizeof(maj_buf), "(0x%08x)", major);
156                 maj = &maj_buf[0];
157         } else {
158                 maj = maj_gss_buf.value;
159         }
160
161         /* Get minor status message */
162         maj_stat2 = gss_display_status(&min_stat2, minor,
163                 GSS_C_MECH_CODE, mech, &msg_ctx, &min_gss_buf);
164
165         if (maj_stat2 != GSS_S_COMPLETE) {
166                 snprintf(min_buf, sizeof(min_buf), "(0x%08x)", minor);
167                 min = &min_buf[0];
168         } else {
169                 min = min_gss_buf.value;
170         }
171
172         if (major == GSS_S_CREDENTIALS_EXPIRED)
173                 msg_verbosity = 1;
174         printerr(msg_verbosity, "ERROR: GSS-API: error in %s(): %s - %s\n",
175                  m, maj, min);
176
177         if (maj_gss_buf.length != 0)
178                 (void) gss_release_buffer(&min_stat1, &maj_gss_buf);
179         if (min_gss_buf.length != 0)
180                 (void) gss_release_buffer(&min_stat2, &min_gss_buf);
181 }
182
183 void
184 pgsserr(char *msg, u_int32_t maj_stat, u_int32_t min_stat, const gss_OID mech)
185 {
186         display_status_2(msg, maj_stat, min_stat, mech);
187 }
188
189 int
190 gssd_acquire_cred(char *server_name)
191 {
192         gss_buffer_desc name;
193         gss_name_t target_name;
194         u_int32_t maj_stat, min_stat;
195         u_int32_t ignore_maj_stat, ignore_min_stat;
196         gss_buffer_desc pbuf;
197
198         name.value = (void *)server_name;
199         name.length = strlen(server_name);
200
201         maj_stat = gss_import_name(&min_stat, &name,
202                         (const gss_OID) GSS_C_NT_HOSTBASED_SERVICE,
203                         &target_name);
204
205         if (maj_stat != GSS_S_COMPLETE) {
206                 pgsserr("gss_import_name", maj_stat, min_stat, g_mechOid);
207                 return (FALSE);
208         }
209
210         maj_stat = gss_acquire_cred(&min_stat, target_name, 0,
211                         GSS_C_NULL_OID_SET, GSS_C_ACCEPT,
212                         &gssd_creds, NULL, NULL);
213
214         if (maj_stat != GSS_S_COMPLETE) {
215                 pgsserr("gss_acquire_cred", maj_stat, min_stat, g_mechOid);
216                 ignore_maj_stat = gss_display_name(&ignore_min_stat,
217                                 target_name, &pbuf, NULL);
218                 if (ignore_maj_stat == GSS_S_COMPLETE) {
219                         printerr(1, "Unable to obtain credentials for '%.*s'\n",
220                                  pbuf.length, pbuf.value);
221                         ignore_maj_stat = gss_release_buffer(&ignore_min_stat,
222                                                              &pbuf);
223                 }
224         }
225
226         ignore_maj_stat = gss_release_name(&ignore_min_stat, &target_name);
227
228         return (maj_stat == GSS_S_COMPLETE);
229 }
230
231 int gssd_check_mechs(void)
232 {
233         u_int32_t maj_stat, min_stat;
234         gss_OID_set supported_mechs = GSS_C_NO_OID_SET;
235         int retval = -1;
236
237         maj_stat = gss_indicate_mechs(&min_stat, &supported_mechs);
238         if (maj_stat != GSS_S_COMPLETE) {
239                 printerr(0, "Unable to obtain list of supported mechanisms. "
240                          "Check that gss library is properly configured.\n");
241                 goto out;
242         }
243         if (supported_mechs == GSS_C_NO_OID_SET ||
244             supported_mechs->count == 0) {
245                 printerr(0, "Unable to obtain list of supported mechanisms. "
246                          "Check that gss library is properly configured.\n");
247                 goto out;
248         }
249         maj_stat = gss_release_oid_set(&min_stat, &supported_mechs);
250         retval = 0;
251 out:
252         return retval;
253 }
254