Branch data Line data Source code
1 : : /* authorize.c --- Authorization to services of authenticated principals.
2 : : * Copyright (C) 2003, 2004, 2006, 2007, 2008 Simon Josefsson
3 : : *
4 : : * This file is part of Shishi.
5 : : *
6 : : * Shishi is free software; you can redistribute it and/or modify it
7 : : * under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation; either version 3 of the License, or
9 : : * (at your option) any later version.
10 : : *
11 : : * Shishi is distributed in the hope that it will be useful, but
12 : : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 : : * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 : : * Floor, Boston, MA 02110-1301, USA
20 : : *
21 : : */
22 : :
23 : : #include "internal.h"
24 : :
25 : : #ifdef HAVE_PWD_H
26 : : # include <pwd.h>
27 : : #endif
28 : :
29 : : int
30 : 0 : shishi_authorize_strcmp (Shishi * handle, const char *principal,
31 : : const char *authzname)
32 : : {
33 [ # # ]: 0 : if (strcmp (principal, authzname) == 0)
34 : 0 : return 1;
35 : :
36 : 0 : return 0;
37 : : }
38 : :
39 : : /* MIT/Heimdal authorization method */
40 : : int
41 : 0 : shishi_authorize_k5login (Shishi * handle, const char *principal,
42 : : const char *authzname)
43 : : {
44 : : #if HAVE_PWD_H && HAVE_GETPWNAM
45 : : struct passwd *pwd;
46 : : struct stat sta;
47 : : FILE *fic;
48 : : char *ficname;
49 : 0 : char *line = NULL;
50 : 0 : size_t linelength = 0;
51 : 0 : int authorized = 0;
52 : :
53 : 0 : pwd = getpwnam (authzname);
54 [ # # # # ]: 0 : if (pwd == NULL || pwd->pw_dir == NULL)
55 : 0 : return 0;
56 : :
57 : 0 : asprintf (&ficname, "%s%s", pwd->pw_dir, ".k5login");
58 : :
59 [ # # ]: 0 : if (stat (ficname, &sta) != 0)
60 : : /* If file .k5login does not exist */
61 [ # # ]: 0 : if (strcmp (principal, authzname) == 0)
62 : 0 : return shishi_authorize_strcmp (handle, principal, authzname);
63 : :
64 : : /* Owner should be user or root */
65 [ # # ][ # # ]: 0 : if ((sta.st_uid != pwd->pw_uid) && (sta.st_uid != 0))
66 : : {
67 : 0 : free (ficname);
68 : 0 : return 0;
69 : : }
70 : :
71 : 0 : fic = fopen (ficname, "r");
72 [ # # ]: 0 : if (fic == NULL)
73 : : {
74 : 0 : free (ficname);
75 : 0 : return 0;
76 : : }
77 : :
78 [ # # ]: 0 : while (!feof (fic))
79 : : {
80 [ # # ]: 0 : if (getline (&line, &linelength, fic) == -1)
81 : 0 : break;
82 : 0 : line[linelength - 1] = '\0';
83 : :
84 [ # # ]: 0 : if (strcmp (principal, line) == 0)
85 : : {
86 : 0 : authorized = 1;
87 : 0 : break;
88 : : }
89 : : }
90 : :
91 : 0 : fclose (fic);
92 : 0 : free (ficname);
93 : 0 : free (line);
94 : :
95 : 0 : return authorized;
96 : : #else
97 : : return 0;
98 : : #endif
99 : : }
100 : :
101 : : struct Authorization_aliases
102 : : {
103 : : const char *name;
104 : : int type;
105 : : };
106 : :
107 : : static const struct Authorization_aliases authorization_aliases[] = {
108 : : {"basic", SHISHI_AUTHORIZATION_BASIC},
109 : : {"k5login", SHISHI_AUTHORIZATION_K5LOGIN}
110 : : };
111 : :
112 : : /**
113 : : * shishi_authorization_parse:
114 : : * @authorization: name of authorization type, e.g. "basic".
115 : : *
116 : : * Parse authorization type name.
117 : : *
118 : : * Return value: Return authorization type corresponding to a string.
119 : : **/
120 : : int
121 : 0 : shishi_authorization_parse (const char *authorization)
122 : : {
123 : : size_t i;
124 : : char *endptr;
125 : :
126 : 0 : i = strtol (authorization, &endptr, 0);
127 : :
128 [ # # ]: 0 : if (endptr != authorization)
129 : 0 : return i;
130 : :
131 [ # # ]: 0 : for (i = 0;
132 : : i < sizeof (authorization_aliases) / sizeof (authorization_aliases[0]);
133 : 0 : i++)
134 [ # # ]: 0 : if (strcasecmp (authorization, authorization_aliases[i].name) == 0)
135 : 0 : return authorization_aliases[i].type;
136 : :
137 : 0 : return -1;
138 : : }
139 : :
140 : : /**
141 : : * shishi_authorized_p:
142 : : * @handle: shishi handle as allocated by shishi_init().
143 : : * @tkt: input variable with ticket info.
144 : : * @authzname: authorization name.
145 : : *
146 : : * Simplistic authorization of @authzname against encrypted client
147 : : * principal name inside ticket. Currently this function only compare
148 : : * the principal name with @authzname using strcmp().
149 : : *
150 : : * Return value: Returns 1 if authzname is authorized for services by
151 : : * authenticated client principal, or 0 otherwise.
152 : : **/
153 : : int
154 : 0 : shishi_authorized_p (Shishi * handle, Shishi_tkt * tkt, const char *authzname)
155 : : {
156 : : char *client;
157 : : size_t i;
158 : : int rc;
159 : :
160 : 0 : rc = shishi_encticketpart_client (handle, shishi_tkt_encticketpart (tkt),
161 : : &client, NULL);
162 [ # # ]: 0 : if (rc != SHISHI_OK)
163 : 0 : return 0;
164 : :
165 [ # # ]: 0 : for (i = 0; i < handle->nauthorizationtypes; i++)
166 : : {
167 [ # # # ]: 0 : switch (handle->authorizationtypes[i])
168 : : {
169 : : case SHISHI_AUTHORIZATION_BASIC:
170 [ # # ]: 0 : if (shishi_authorize_strcmp (handle, client, authzname))
171 : 0 : return 1;
172 : 0 : break;
173 : :
174 : : case SHISHI_AUTHORIZATION_K5LOGIN:
175 [ # # ]: 0 : if (shishi_authorize_k5login (handle, client, authzname))
176 : 0 : return 1;
177 : 0 : break;
178 : :
179 : : default:
180 : 0 : return 0;
181 : : }
182 : : }
183 : :
184 : 0 : return 0;
185 : : }
|