1/* 2 * Copyright (c) 2005, PADL Software Pty Ltd. 3 * All rights reserved. 4 * 5 * Portions Copyright (c) 2009 Apple Inc. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * 3. Neither the name of PADL Software nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35#include "kcm_locl.h" 36#include <getarg.h> 37 38static const char *config_file; /* location of kcm config file */ 39 40size_t max_request = 0; /* maximal size of a request */ 41char *socket_path = NULL; 42char *door_path = NULL; 43 44static char *max_request_str; /* `max_request' as a string */ 45 46#ifdef SUPPORT_DETACH 47int detach_from_console = -1; 48#define DETACH_IS_DEFAULT FALSE 49#endif 50 51static const char *system_cache_name = NULL; 52static const char *system_keytab = NULL; 53static const char *system_principal = NULL; 54static const char *system_server = NULL; 55static const char *system_user = NULL; 56static const char *system_group = NULL; 57 58static const char *renew_life = NULL; 59static const char *ticket_life = NULL; 60 61int launchd_flag = 0; 62int disallow_getting_krbtgt = 0; 63int use_uid_matching = 0; 64int max_num_requests = 100000; 65int kcm_timeout = -1; 66 67static int help_flag; 68static int version_flag; 69 70static struct getargs args[] = { 71 { 72 "cache-name", 0, arg_string, &system_cache_name, 73 "system cache name", "cachename" 74 }, 75 { 76 "config-file", 'c', arg_string, &config_file, 77 "location of config file", "file" 78 }, 79 { 80 "group", 'g', arg_string, &system_group, 81 "system cache group", "group" 82 }, 83 { 84 "max-request", 0, arg_string, &max_request, 85 "max size for a kcm-request", "size" 86 }, 87 { 88 "launchd", 0, arg_flag, &launchd_flag, 89 "when in use by launchd" 90 }, 91#ifdef SUPPORT_DETACH 92#if DETACH_IS_DEFAULT 93 { 94 "detach", 'D', arg_negative_flag, &detach_from_console, 95 "don't detach from console" 96 }, 97#else 98 { 99 "detach", 0 , arg_flag, &detach_from_console, 100 "detach from console" 101 }, 102#endif 103#endif 104 { "help", 'h', arg_flag, &help_flag }, 105 { 106 "system-principal", 'k', arg_string, &system_principal, 107 "system principal name", "principal" 108 }, 109 { 110 "lifetime", 'l', arg_string, &ticket_life, 111 "lifetime of system tickets", "time" 112 }, 113 { 114 "disallow-getting-krbtgt", 0, arg_flag, &disallow_getting_krbtgt, 115 "disable fetching krbtgt from the cache" 116 }, 117 { 118 "use-uid-matching", 0, arg_flag, &use_uid_matching, 119 "only use UID when matching allowed credentials or not" 120 }, 121 { 122 "renewable-life", 'r', arg_string, &renew_life, 123 "renewable lifetime of system tickets", "time" 124 }, 125 { 126 "socket-path", 's', arg_string, &socket_path, 127 "path to kcm domain socket", "path" 128 }, 129#ifdef HAVE_DOOR_CREATE 130 { 131 "door-path", 's', arg_string, &door_path, 132 "path to kcm door", "path" 133 }, 134#endif 135 { 136 "server", 'S', arg_string, &system_server, 137 "server to get system ticket for", "principal" 138 }, 139 { 140 "keytab", 't', arg_string, &system_keytab, 141 "system keytab name", "keytab" 142 }, 143 { 144 "user", 'u', arg_string, &system_user, 145 "system cache owner", "user" 146 }, 147 { 148 "number-requests", 0, arg_integer, &max_num_requests, 149 "number of requests processed before exit" 150 }, 151 { 152 "idle-timeout", 0, arg_integer, &kcm_timeout, 153 "number of seconds of idle time before timeout (0 disables timeout)", "seconds" 154 }, 155 { "version", 'v', arg_flag, &version_flag } 156}; 157 158static int num_args = sizeof(args) / sizeof(args[0]); 159 160static void 161usage(int ret) 162{ 163 arg_printusage (args, num_args, NULL, ""); 164 exit (ret); 165} 166 167static int parse_owners(kcm_ccache ccache) 168{ 169 uid_t uid = 0; 170 struct passwd *pw; 171 int uid_p = 0; 172 173 if (system_user != NULL) { 174 if (isdigit((unsigned char)system_user[0])) { 175 pw = getpwuid(atoi(system_user)); 176 } else { 177 pw = getpwnam(system_user); 178 } 179 if (pw == NULL) { 180 return errno; 181 } 182 183 system_user = strdup(pw->pw_name); 184 if (system_user == NULL) { 185 return ENOMEM; 186 } 187 188 uid = pw->pw_uid; uid_p = 1; 189 } 190 191 if (uid_p) 192 ccache->uid = uid; 193 else 194 ccache->uid = 0; /* geteuid() XXX */ 195 196 return 0; 197} 198 199static const char * 200kcm_system_config_get_string(const char *string) 201{ 202 return krb5_config_get_string(kcm_context, NULL, "kcm", 203 "system_ccache", string, NULL); 204} 205 206static krb5_error_code 207ccache_init_system(void) 208{ 209 kcm_ccache ccache; 210 krb5_error_code ret; 211 212 if (system_cache_name == NULL) 213 system_cache_name = kcm_system_config_get_string("cc_name"); 214 215 ret = kcm_ccache_new(kcm_context, 216 system_cache_name ? system_cache_name : "SYSTEM", 217 &ccache); 218 if (ret) 219 return ret; 220 221 ccache->flags |= KCM_FLAGS_OWNER_IS_SYSTEM; 222 ccache->flags |= KCM_FLAGS_USE_KEYTAB; 223 224 ret = parse_owners(ccache); 225 if (ret) 226 return ret; 227 228 ret = krb5_parse_name(kcm_context, system_principal, &ccache->client); 229 if (ret) { 230 kcm_release_ccache(kcm_context, ccache); 231 return ret; 232 } 233 234 if (system_server == NULL) 235 system_server = kcm_system_config_get_string("server"); 236 237 if (system_server != NULL) { 238 ret = krb5_parse_name(kcm_context, system_server, &ccache->server); 239 if (ret) { 240 kcm_release_ccache(kcm_context, ccache); 241 return ret; 242 } 243 } 244 245 if (system_keytab == NULL) 246 system_keytab = kcm_system_config_get_string("keytab_name"); 247 248 if (system_keytab != NULL) { 249 ret = krb5_kt_resolve(kcm_context, system_keytab, &ccache->keytab); 250 } else { 251 ret = krb5_kt_default(kcm_context, &ccache->keytab); 252 } 253 if (ret) { 254 kcm_release_ccache(kcm_context, ccache); 255 return ret; 256 } 257 258 if (renew_life == NULL) 259 renew_life = kcm_system_config_get_string("renew_life"); 260 261 if (renew_life == NULL) 262 renew_life = "1 month"; 263 264 if (renew_life != NULL) { 265 ccache->renew_life = parse_time(renew_life, "s"); 266 if (ccache->renew_life < 0) { 267 kcm_release_ccache(kcm_context, ccache); 268 return EINVAL; 269 } 270 } 271 272 if (ticket_life == NULL) 273 ticket_life = kcm_system_config_get_string("ticket_life"); 274 275 if (ticket_life != NULL) { 276 ccache->tkt_life = parse_time(ticket_life, "s"); 277 if (ccache->tkt_life < 0) { 278 kcm_release_ccache(kcm_context, ccache); 279 return EINVAL; 280 } 281 } 282 283 /* enqueue default actions for credentials cache */ 284 ret = kcm_ccache_enqueue_default(kcm_context, ccache, NULL); 285 286 kcm_release_ccache(kcm_context, ccache); /* retained by event queue */ 287 288 return ret; 289} 290 291void 292kcm_configure(int argc, char **argv) 293{ 294 krb5_error_code ret; 295 int optidx = 0; 296 const char *p; 297 298 while(getarg(args, num_args, argc, argv, &optidx)) 299 warnx("error at argument `%s'", argv[optidx]); 300 301 if(help_flag) 302 usage (0); 303 304 if (version_flag) { 305 print_version(NULL); 306 exit(0); 307 } 308 309 argc -= optidx; 310 311 if (argc != 0) 312 usage(1); 313 314 { 315 char **files; 316 317 if(config_file == NULL) 318 config_file = _PATH_KCM_CONF; 319 320 ret = krb5_prepend_config_files_default(config_file, &files); 321 if (ret) 322 krb5_err(kcm_context, 1, ret, "getting configuration files"); 323 324 ret = krb5_set_config_files(kcm_context, files); 325 krb5_free_config_files(files); 326 if(ret) 327 krb5_err(kcm_context, 1, ret, "reading configuration files"); 328 } 329 330 if(max_request_str) 331 max_request = parse_bytes(max_request_str, NULL); 332 333 if(max_request == 0){ 334 p = krb5_config_get_string (kcm_context, 335 NULL, 336 "kcm", 337 "max-request", 338 NULL); 339 if(p) 340 max_request = parse_bytes(p, NULL); 341 } 342 343 if (system_principal == NULL) { 344 system_principal = kcm_system_config_get_string("principal"); 345 } 346 347 if (system_principal != NULL) { 348 ret = ccache_init_system(); 349 if (ret) 350 krb5_err(kcm_context, 1, ret, "initializing system ccache"); 351 } 352 353 if (disallow_getting_krbtgt == -1) { 354 disallow_getting_krbtgt = 355 krb5_config_get_bool_default(kcm_context, NULL, FALSE, "kcm", 356 "disallow-getting-krbtgt", NULL); 357 } 358 359#ifdef SUPPORT_DETACH 360 if(detach_from_console == -1) 361 detach_from_console = krb5_config_get_bool_default(kcm_context, NULL, 362 DETACH_IS_DEFAULT, 363 "kcm", 364 "detach", NULL); 365#endif 366 kcm_openlog(); 367 if(max_request == 0) 368 max_request = 64 * 1024; 369} 370 371