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 void usage(int ret) __attribute__((noreturn)); 39 40static const char *config_file; /* location of kcm config file */ 41 42size_t max_request = 0; /* maximal size of a request */ 43char *socket_path = NULL; 44char *door_path = NULL; 45 46static char *max_request_str; /* `max_request' as a string */ 47 48#ifdef SUPPORT_DETACH 49int detach_from_console = -1; 50#define DETACH_IS_DEFAULT FALSE 51#endif 52 53static const char *system_cache_name = NULL; 54static const char *system_keytab = NULL; 55static const char *system_principal = NULL; 56static const char *system_server = NULL; 57static const char *system_user = NULL; 58static const char *system_group = NULL; 59 60static const char *renew_life = NULL; 61static const char *ticket_life = NULL; 62 63int launchd_flag = 0; 64int disallow_getting_krbtgt = 0; 65int use_uid_matching = 0; 66int max_num_requests = 100000; 67int kcm_timeout = -1; 68 69static int help_flag; 70static int version_flag; 71 72static struct getargs args[] = { 73 { 74 "cache-name", 0, arg_string, &system_cache_name, 75 "system cache name", "cachename" 76 }, 77 { 78 "config-file", 'c', arg_string, &config_file, 79 "location of config file", "file" 80 }, 81 { 82 "group", 'g', arg_string, &system_group, 83 "system cache group", "group" 84 }, 85 { 86 "max-request", 0, arg_string, &max_request, 87 "max size for a kcm-request", "size" 88 }, 89 { 90 "launchd", 0, arg_flag, &launchd_flag, 91 "when in use by launchd" 92 }, 93#ifdef SUPPORT_DETACH 94#if DETACH_IS_DEFAULT 95 { 96 "detach", 'D', arg_negative_flag, &detach_from_console, 97 "don't detach from console" 98 }, 99#else 100 { 101 "detach", 0 , arg_flag, &detach_from_console, 102 "detach from console" 103 }, 104#endif 105#endif 106 { "help", 'h', arg_flag, &help_flag }, 107 { 108 "system-principal", 'k', arg_string, &system_principal, 109 "system principal name", "principal" 110 }, 111 { 112 "lifetime", 'l', arg_string, &ticket_life, 113 "lifetime of system tickets", "time" 114 }, 115 { 116 "disallow-getting-krbtgt", 0, arg_flag, &disallow_getting_krbtgt, 117 "disable fetching krbtgt from the cache" 118 }, 119 { 120 "use-uid-matching", 0, arg_flag, &use_uid_matching, 121 "only use UID when matching allowed credentials or not" 122 }, 123 { 124 "renewable-life", 'r', arg_string, &renew_life, 125 "renewable lifetime of system tickets", "time" 126 }, 127 { 128 "socket-path", 's', arg_string, &socket_path, 129 "path to kcm domain socket", "path" 130 }, 131#ifdef HAVE_DOOR_CREATE 132 { 133 "door-path", 's', arg_string, &door_path, 134 "path to kcm door", "path" 135 }, 136#endif 137 { 138 "server", 'S', arg_string, &system_server, 139 "server to get system ticket for", "principal" 140 }, 141 { 142 "keytab", 't', arg_string, &system_keytab, 143 "system keytab name", "keytab" 144 }, 145 { 146 "user", 'u', arg_string, &system_user, 147 "system cache owner", "user" 148 }, 149 { 150 "number-requests", 0, arg_integer, &max_num_requests, 151 "number of requests processed before exit" 152 }, 153 { 154 "idle-timeout", 0, arg_integer, &kcm_timeout, 155 "number of seconds of idle time before timeout (0 disables timeout)", "seconds" 156 }, 157 { "version", 'v', arg_flag, &version_flag } 158}; 159 160static int num_args = sizeof(args) / sizeof(args[0]); 161 162static void 163usage(int ret) 164{ 165 arg_printusage (args, num_args, NULL, ""); 166 exit (ret); 167} 168 169static int parse_owners(kcm_ccache ccache) 170{ 171 uid_t uid = 0; 172 struct passwd *pw; 173 int uid_p = 0; 174 175 if (system_user != NULL) { 176 if (isdigit((unsigned char)system_user[0])) { 177 pw = getpwuid(atoi(system_user)); 178 } else { 179 pw = getpwnam(system_user); 180 } 181 if (pw == NULL) { 182 return errno; 183 } 184 185 system_user = strdup(pw->pw_name); 186 if (system_user == NULL) { 187 return ENOMEM; 188 } 189 190 uid = pw->pw_uid; uid_p = 1; 191 } 192 193 if (uid_p) 194 ccache->uid = uid; 195 else 196 ccache->uid = 0; /* geteuid() XXX */ 197 198 return 0; 199} 200 201static const char * 202kcm_system_config_get_string(const char *string) 203{ 204 return krb5_config_get_string(kcm_context, NULL, "kcm", 205 "system_ccache", string, NULL); 206} 207 208static krb5_error_code 209ccache_init_system(void) 210{ 211 kcm_ccache ccache; 212 krb5_error_code ret; 213 214 if (system_cache_name == NULL) 215 system_cache_name = kcm_system_config_get_string("cc_name"); 216 217 ret = kcm_ccache_new(kcm_context, 218 system_cache_name ? system_cache_name : "SYSTEM", 219 &ccache); 220 if (ret) 221 return ret; 222 223 ccache->flags |= KCM_FLAGS_OWNER_IS_SYSTEM; 224 ccache->flags |= KCM_FLAGS_USE_KEYTAB; 225 226 ret = parse_owners(ccache); 227 if (ret) 228 return ret; 229 230 ret = krb5_parse_name(kcm_context, system_principal, &ccache->client); 231 if (ret) { 232 kcm_release_ccache(kcm_context, ccache); 233 return ret; 234 } 235 236 if (system_server == NULL) 237 system_server = kcm_system_config_get_string("server"); 238 239 if (system_server != NULL) { 240 ret = krb5_parse_name(kcm_context, system_server, &ccache->server); 241 if (ret) { 242 kcm_release_ccache(kcm_context, ccache); 243 return ret; 244 } 245 } 246 247 if (system_keytab == NULL) 248 system_keytab = kcm_system_config_get_string("keytab_name"); 249 250 if (system_keytab != NULL) { 251 ret = krb5_kt_resolve(kcm_context, system_keytab, &ccache->keytab); 252 } else { 253 ret = krb5_kt_default(kcm_context, &ccache->keytab); 254 } 255 if (ret) { 256 kcm_release_ccache(kcm_context, ccache); 257 return ret; 258 } 259 260 if (renew_life == NULL) 261 renew_life = kcm_system_config_get_string("renew_life"); 262 263 if (renew_life == NULL) 264 renew_life = "1 month"; 265 266 if (renew_life != NULL) { 267 ccache->renew_life = parse_time(renew_life, "s"); 268 if (ccache->renew_life < 0) { 269 kcm_release_ccache(kcm_context, ccache); 270 return EINVAL; 271 } 272 } 273 274 if (ticket_life == NULL) 275 ticket_life = kcm_system_config_get_string("ticket_life"); 276 277 if (ticket_life != NULL) { 278 ccache->tkt_life = parse_time(ticket_life, "s"); 279 if (ccache->tkt_life < 0) { 280 kcm_release_ccache(kcm_context, ccache); 281 return EINVAL; 282 } 283 } 284 285 /* enqueue default actions for credentials cache */ 286 ret = kcm_ccache_enqueue_default(kcm_context, ccache, NULL); 287 288 kcm_release_ccache(kcm_context, ccache); /* retained by event queue */ 289 290 return ret; 291} 292 293void 294kcm_configure(int argc, char **argv) 295{ 296 krb5_error_code ret; 297 int optidx = 0; 298 const char *p; 299 300 while(getarg(args, num_args, argc, argv, &optidx)) 301 warnx("error at argument `%s'", argv[optidx]); 302 303 if(help_flag) 304 usage (0); 305 306 if (version_flag) { 307 print_version(NULL); 308 exit(0); 309 } 310 311 argc -= optidx; 312 313 if (argc != 0) 314 usage(1); 315 316 { 317 char **files; 318 319 if(config_file == NULL) 320 config_file = _PATH_KCM_CONF; 321 322 ret = krb5_prepend_config_files_default(config_file, &files); 323 if (ret) 324 krb5_err(kcm_context, 1, ret, "getting configuration files"); 325 326 ret = krb5_set_config_files(kcm_context, files); 327 krb5_free_config_files(files); 328 if(ret) 329 krb5_err(kcm_context, 1, ret, "reading configuration files"); 330 } 331 332 if(max_request_str) 333 max_request = parse_bytes(max_request_str, NULL); 334 335 if(max_request == 0){ 336 p = krb5_config_get_string (kcm_context, 337 NULL, 338 "kcm", 339 "max-request", 340 NULL); 341 if(p) 342 max_request = parse_bytes(p, NULL); 343 } 344 345 if (system_principal == NULL) { 346 system_principal = kcm_system_config_get_string("principal"); 347 } 348 349 if (system_principal != NULL) { 350 ret = ccache_init_system(); 351 if (ret) 352 krb5_err(kcm_context, 1, ret, "initializing system ccache"); 353 } 354 355 if (disallow_getting_krbtgt == -1) { 356 disallow_getting_krbtgt = 357 krb5_config_get_bool_default(kcm_context, NULL, FALSE, "kcm", 358 "disallow-getting-krbtgt", NULL); 359 } 360 361#ifdef SUPPORT_DETACH 362 if(detach_from_console == -1) 363 detach_from_console = krb5_config_get_bool_default(kcm_context, NULL, 364 DETACH_IS_DEFAULT, 365 "kcm", 366 "detach", NULL); 367#endif 368 kcm_openlog(); 369 if(max_request == 0) 370 max_request = 64 * 1024; 371} 372 373