1 /*
2  * Copyright (c) 1994-2008 Carnegie Mellon University.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. The name "Carnegie Mellon University" must not be used to
17  *    endorse or promote products derived from this software without
18  *    prior written permission. For permission or any legal
19  *    details, please contact
20  *      Carnegie Mellon University
21  *      Center for Technology Transfer and Enterprise Creation
22  *      4615 Forbes Avenue
23  *      Suite 302
24  *      Pittsburgh, PA  15213
25  *      (412) 268-7393, fax: (412) 268-7395
26  *      innovation@andrew.cmu.edu
27  *
28  * 4. Redistributions of any form whatsoever must retain the following
29  *    acknowledgment:
30  *    "This product includes software developed by Computing Services
31  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
32  *
33  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
34  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
35  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
36  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
37  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
38  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
39  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40  */
41 
42 #include <config.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sysexits.h>
46 
47 #include "auth.h"
48 #include "libcyr_cfg.h"
49 #include "xmalloc.h"
50 
51 struct auth_mech *auth_mechs[] = {
52     &auth_unix,
53     &auth_pts,
54 #ifdef HAVE_KRB
55     &auth_krb,
56 #endif
57 #ifdef HAVE_GSSAPI_H
58     &auth_krb5,
59 #endif
60     NULL };
61 
auth_fromname(void)62 static struct auth_mech *auth_fromname(void)
63 {
64     int i;
65     const char *name = libcyrus_config_getstring(CYRUSOPT_AUTH_MECH);
66     static struct auth_mech *auth = NULL;
67 
68     if (auth)
69         return auth;
70 
71     for (i = 0; auth_mechs[i]; i++) {
72         if (!strcmp(auth_mechs[i]->name, name)) {
73             auth = auth_mechs[i]; break;
74         }
75     }
76     if (!auth) {
77         char errbuf[1024];
78         snprintf(errbuf, sizeof(errbuf),
79                  "Authorization mechanism %s not supported", name);
80         fatal(errbuf, EX_CONFIG);
81     }
82 
83     return auth;
84 }
85 
auth_memberof(const struct auth_state * auth_state,const char * identifier)86 EXPORTED int auth_memberof(const struct auth_state *auth_state, const char *identifier)
87 {
88     struct auth_mech *auth = auth_fromname();
89 
90     return auth->memberof(auth_state, identifier);
91 }
92 
auth_canonifyid(const char * identifier,size_t len)93 EXPORTED const char *auth_canonifyid(const char *identifier, size_t len)
94 {
95     struct auth_mech *auth = auth_fromname();
96 
97     return auth->canonifyid(identifier, len);
98 }
99 
auth_newstate(const char * identifier)100 EXPORTED struct auth_state *auth_newstate(const char *identifier)
101 {
102     struct auth_mech *auth = auth_fromname();
103 
104     return auth->newstate(identifier);
105 }
106 
auth_freestate(struct auth_state * auth_state)107 EXPORTED void auth_freestate(struct auth_state *auth_state)
108 {
109     struct auth_mech *auth = auth_fromname();
110 
111     if (auth_state) auth->freestate(auth_state);
112 }
113 
auth_groups(const struct auth_state * auth_state)114 EXPORTED strarray_t *auth_groups(const struct auth_state *auth_state)
115 {
116     struct auth_mech *auth = auth_fromname();
117 
118     return auth->groups(auth_state);
119 }
120