2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
14 #include <sys/types.h>
27 #include <afs/com_err.h>
29 #include <afs/afsutil.h>
30 #include <afs/cellconfig.h>
32 #include "rxk5_utilafs.h"
34 #include <afs/ptclient.h>
38 #ifdef HAVE_KRB5_CREDS_KEYBLOCK
41 #ifdef HAVE_KRB5_CREDS_SESSION
42 #define USING_HEIMDAL 1
48 /* This code borrowed heavily from the previous version of log. Here is the
49 intro comment for that program: */
52 log -- tell the Andrew Cache Manager your password
57 Further modified in August 1987 to understand cell IDs.
59 Further modified in October 2006 to understand kerberos 5.
63 klog [principal [password]] [-t] [-c cellname] [-k <k5realm>]
66 principal is of the form 'name' or 'name@cell' which provides the
67 cellname. See the -c option below.
68 password is the user's password. This form is NOT recommended for
70 -t advises klog to write a Kerberos style ticket file in /tmp.
71 -c identifies cellname as the cell in which authentication is to take
73 -k identifies an alternate kerberos realm to use provide
74 authentication services for the cell.
77 #define KLOGEXIT(code) rx_Finalize(); \
79 static int CommandProc(struct cmd_syndesc *as, void *arock);
82 static char **zero_argv;
84 static krb5_context k5context;
85 static struct afsconf_dir *tdir;
86 static int always_evil = 2; /* gcc optimizes 0 into bss. fools. */
89 main(int argc, char *argv[])
91 struct cmd_syndesc *ts;
95 * The following signal action for AIX is necessary so that in case of a
96 * crash (i.e. core is generated) we can include the user's data section
97 * in the core dump. Unfortunately, by default, only a partial core is
98 * generated which, in many cases, isn't too useful.
100 struct sigaction nsa;
102 sigemptyset(&nsa.sa_mask);
103 nsa.sa_handler = SIG_DFL;
104 nsa.sa_flags = SA_FULLDUMP;
105 sigaction(SIGABRT, &nsa, NULL);
106 sigaction(SIGSEGV, &nsa, NULL);
111 ts = cmd_CreateSyntax(NULL, CommandProc, NULL,
112 "obtain Kerberos authentication");
129 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL|CMD_HIDDEN, 0);
130 cmd_Seek(ts, aPRINCIPAL);
131 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
132 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
133 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
134 cmd_AddParm(ts, "-k", CMD_SINGLE, CMD_OPTIONAL, "krb5 realm");
135 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL,
136 "read password from stdin");
137 cmd_AddParm(ts, "-silent", CMD_FLAG, CMD_OPTIONAL, "silent operation");
138 cmd_AddParm(ts, "-lifetime", CMD_SINGLE, CMD_OPTIONAL,
139 "ticket lifetime in hh[:mm[:ss]]");
140 cmd_AddParm(ts, "-setpag", CMD_FLAG, CMD_OPTIONAL,
141 "Create a new setpag before authenticating");
142 cmd_AddParm(ts, "-tmp", CMD_FLAG, CMD_OPTIONAL,
143 "write Kerberos-style ticket file in /tmp");
144 cmd_AddParm(ts, "-noprdb", CMD_FLAG, CMD_OPTIONAL, "don't consult pt");
145 cmd_AddParm(ts, "-unwrap", CMD_FLAG, CMD_OPTIONAL, "perform 524d conversion");
147 cmd_AddParm(ts, "-k5", CMD_FLAG, CMD_OPTIONAL, "get rxk5 credentials");
148 cmd_AddParm(ts, "-k4", CMD_FLAG, CMD_OPTIONAL, "get rxkad credentials");
150 ++ts->nParms; /* skip -k5 */
151 cmd_AddParm(ts, "-k4", CMD_FLAG, CMD_OPTIONAL|CMD_HIDDEN, 0);
154 code = cmd_Dispatch(argc, argv);
161 static char gpbuf[BUFSIZ];
162 /* read a password from stdin, stop on \n or eof */
164 memset(gpbuf, 0, sizeof(gpbuf));
165 for (i = 0; i < (sizeof(gpbuf) - 1); i++) {
167 if (tc == '\n' || tc == EOF)
175 silent_errors(const char *who,
180 /* ignore and don't print error */
183 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
185 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
186 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
187 #define num_comp(c, p) (krb5_princ_size(c, p))
188 #define realm_data(c, p) krb5_princ_realm(c, p)->data
189 #define realm_len(c, p) krb5_princ_realm(c, p)->length
191 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
193 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
194 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
195 #define num_comp(c, p) ((p)->name.name_string.len)
196 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
197 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
200 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
203 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
205 #define get_cred_keydata(c) c->keyblock.contents
206 #define get_cred_keylen(c) c->keyblock.length
207 #define get_creds_enctype(c) c->keyblock.enctype
209 #elif defined(HAVE_KRB5_CREDS_SESSION)
211 #define get_cred_keydata(c) c->session.keyvalue.data
212 #define get_cred_keylen(c) c->session.keyvalue.length
213 #define get_creds_enctype(c) c->session.keytype
216 #error "Must have either keyblock or session member of krb5_creds"
220 whoami(struct ktc_token *atoken,
221 struct afsconf_cell *cellconfig,
222 struct ktc_principal *aclient,
228 struct ubik_client *ptconn = 0;
229 struct rx_securityClass *sc;
230 struct rx_connection *conns[MAXSERVERS+1];
233 char tempname[PR_MAXNAMELEN + 1];
235 memset(lnames, 0, sizeof *lnames);
236 memset(lids, 0, sizeof *lids);
238 sc = rxkad_NewClientSecurityObject(rxkad_auth,
239 &atoken->sessionKey, atoken->kvno,
240 atoken->ticketLen, atoken->ticket);
241 for (i = 0; i < cellconfig->numServers; ++i)
242 conns[i] = rx_NewConnection(cellconfig->hostAddr[i].sin_addr.s_addr,
243 cellconfig->hostAddr[i].sin_port, PRSRV, sc, scIndex);
246 if ((code = ubik_ClientInit(conns, &ptconn)))
248 if (*aclient->instance)
249 snprintf (tempname, sizeof tempname, "%s.%s",
250 aclient->name, aclient->instance);
252 snprintf (tempname, sizeof tempname, "%s", aclient->name);
253 lnames->namelist_len = 1;
254 lnames->namelist_val = (prname *) tempname;
255 code = ubik_PR_NameToID(ptconn, 0, lnames, lids);
256 if (lids->idlist_val) {
257 *vicep = *lids->idlist_val;
260 if (lids->idlist_val) free(lids->idlist_val);
261 if (ptconn) ubik_ClientDestroy(ptconn);
266 k5_to_k4_name(krb5_context k5context,
267 krb5_principal k5princ,
268 struct ktc_principal *ktcprinc)
272 switch(num_comp(k5context, k5princ)) {
275 i = get_princ_len(k5context, k5princ, 1);
276 if (i > MAXKTCNAMELEN-1) i = MAXKTCNAMELEN-1;
277 memcpy(ktcprinc->instance, get_princ_str(k5context, k5princ, 1), i);
280 i = get_princ_len(k5context, k5princ, 0);
281 if (i > MAXKTCNAMELEN-1) i = MAXKTCNAMELEN-1;
282 memcpy(ktcprinc->name, get_princ_str(k5context, k5princ, 0), i);
289 /* save and reuse password. This is necessary to make
290 * "direct to service" authentication work with most
291 * flavors of kerberos, when the afs principal has no instance.
297 klog_prompter(krb5_context context,
302 krb5_prompt prompts[])
304 krb5_error_code code;
306 #ifndef USING_HEIMDAL
307 krb5_prompt_type *types;
309 struct kp_arg *kparg = (struct kp_arg *) a;
310 code = krb5_prompter_posix(context, a, name, banner, num_prompts, prompts);
311 if (code) return code;
312 #ifndef USING_HEIMDAL
313 if ((types = krb5_get_prompt_types(context)))
315 for (i = 0; i < num_prompts; ++i) {
317 type = prompts[i].type;
322 printf ("i%d t%d <%.*s>\n", i,
324 prompts[i].reply->length,
325 prompts[i].reply->data);
328 case KRB5_PROMPT_TYPE_PASSWORD:
329 case KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN:
330 memcpy(kparg->pstore, prompts[i].reply->data, prompts[i].reply->length);
331 kparg->pstore[prompts[i].reply->length] = 0;
332 *kparg->pp = kparg->pstore;
339 CommandProc(struct cmd_syndesc *as, void *arock)
341 krb5_principal princ = 0;
342 char *cell, *pname, **hrealms, *service;
343 char service_temp[MAXKTCREALMLEN + 20];
344 krb5_creds incred[1], mcred[1], *outcred = 0, *afscred;
346 krb5_get_init_creds_opt gic_opts[1];
347 char *tofree, *outname;
350 int i, dosetpag, evil, noprdb, id;
354 krb5_data enc_part[1];
355 time_t lifetime; /* requested ticket lifetime */
356 krb5_prompter_fct pf = NULL;
359 struct kp_arg klog_arg[1];
362 struct afsconf_cell cellconfig[1];
364 static char rn[] = "klog"; /*Routine name */
365 static int Pipe = 0; /* reading from a pipe */
366 static int Silent = 0; /* Don't want error messages */
368 int writeTicketFile = 0; /* write ticket file to /tmp */
371 memset(incred, 0, sizeof *incred);
372 /* blow away command line arguments */
373 for (i = 1; i < zero_argc; i++)
374 memset(zero_argv[i], 0, strlen(zero_argv[i]));
376 memset(klog_arg, 0, sizeof *klog_arg);
378 /* first determine quiet flag based on -silent switch */
379 Silent = (as->parms[aSILENT].items ? 1 : 0);
382 afs_set_com_err_hook(silent_errors);
385 if ((code = krb5_init_context(&k5context))) {
386 afs_com_err(rn, code, "while initializing Kerberos 5 library");
389 if ((code = rx_Init(0))) {
390 afs_com_err(rn, code, "while initializing rx");
393 initialize_U_error_table();
394 /*initialize_krb5_error_table();*/
395 initialize_RXK_error_table();
396 initialize_KTC_error_table();
397 initialize_ACFG_error_table();
398 /* initialize_rx_error_table(); */
399 if (!(tdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
400 afs_com_err(rn, 0, "can't get afs configuration (afsconf_Open(%s))",
401 rn, AFSDIR_CLIENT_ETC_DIRPATH);
405 /* Parse remaining arguments. */
407 dosetpag = !! as->parms[aSETPAG].items;
408 Pipe = !! as->parms[aPIPE].items;
409 writeTicketFile = !! as->parms[aTMP].items;
410 noprdb = !! as->parms[aNOPRDB].items;
411 evil = (always_evil&1) || !! as->parms[aUNWRAP].items;
415 if (as->parms[aK5].items)
416 authtype |= FORCE_RXK5;
417 if (as->parms[aK4].items)
418 authtype |= FORCE_RXKAD;
420 authtype |= env_afs_rxk5_default();
423 cell = as->parms[aCELL].items ? as->parms[aCELL].items->data : 0;
424 if ((code = afsconf_GetCellInfo(tdir, cell, "afsprot", cellconfig))) {
426 afs_com_err(rn, code, "Can't get cell information for '%s'", cell);
428 afs_com_err(rn, code, "Can't get determine local cell!");
432 if (as->parms[aKRBREALM].items) {
433 code = krb5_set_default_realm(k5context,
434 (const char *) as->parms[aKRBREALM].items);
436 afs_com_err(rn, code, "Can't make <%s> the default realm",
437 as->parms[aKRBREALM].items);
441 else if ((code = krb5_get_host_realm(k5context, cellconfig->hostName[0], &hrealms))) {
442 afs_com_err(rn, code, "Can't get realm for host <%s> in cell <%s>\n",
443 cellconfig->hostName[0], cellconfig->name);
446 if (hrealms && *hrealms) {
447 code = krb5_set_default_realm(k5context,
450 afs_com_err(rn, code, "Can't make <%s> the default realm",
455 if (hrealms) krb5_free_host_realm(k5context, hrealms);
459 if (as->parms[aPRINCIPAL].items) {
460 pname = as->parms[aPRINCIPAL].items->data;
462 /* No explicit name provided: use Unix uid. */
467 "Can't figure out your name from your user id (%d).", id);
469 fprintf(stderr, "%s: Try providing the user name.\n", rn);
474 code = krb5_parse_name(k5context, pname, &princ);
476 afs_com_err(rn, code, "Can't parse principal <%s>", pname);
480 if (as->parms[aPASSWORD].items) {
482 * Current argument is the desired password string. Remember it in
483 * our local buffer, and zero out the argument string - anyone can
484 * see it there with ps!
486 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
487 memset(as->parms[aPASSWORD].items->data, 0,
488 strlen(as->parms[aPASSWORD].items->data));
492 if (as->parms[aLIFETIME].items) {
493 char *life = as->parms[aLIFETIME].items->data;
494 char *sp; /* string ptr to rest of life */
495 lifetime = 3600 * strtol(life, &sp, 0); /* hours */
499 fprintf(stderr, "%s: translating '%s' to lifetime failed\n",
504 life = sp + 1; /* skip the colon */
505 lifetime += 60 * strtol(life, &sp, 0); /* minutes */
510 lifetime += strtol(life, &sp, 0); /* seconds */
522 /* Get the password if it wasn't provided. */
525 strncpy(passwd, getpipepass(), sizeof(passwd));
535 if (authtype & FORCE_RXK5) {
536 tofree = get_afs_krb5_svc_princ(cellconfig);
537 snprintf(service_temp, sizeof service_temp, "%s", tofree);
540 snprintf (service_temp, sizeof service_temp, "afs/%s", cellconfig->name);
544 service = service_temp;
546 klog_arg->pp = &pass;
547 klog_arg->pstore = passwd;
548 /* XXX should allow k5 to prompt in most cases -- what about expired pw?*/
549 krb5_get_init_creds_opt_init(gic_opts);
551 code = krb5_get_init_creds_password(k5context,
558 service, /* in_tkt_service */
560 if (code != KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN || service != service_temp) break;
562 if (authtype & FORCE_RXK5) break;
566 memset(passwd, 0, sizeof(passwd));
569 if (krb5_get_default_realm(k5context, &r))
572 afs_com_err(rn, code, "Unable to authenticate to use %s", service);
574 afs_com_err(rn, code, "Unable to authenticate in realm %s", r);
576 afs_com_err(rn, code, "Unable to authenticate to use cell %s",
585 for (;;writeTicketFile = 0) {
586 if (writeTicketFile) {
587 what = "getting default ccache";
588 code = krb5_cc_default(k5context, &cc);
590 what = "krb5_cc_resolve";
591 code = krb5_cc_resolve(k5context, "MEMORY:core", &cc);
592 if (code) goto Failed;
594 what = "initializing ccache";
595 code = krb5_cc_initialize(k5context, cc, princ);
596 if (code) goto Failed;
597 what = "writing Kerberos ticket file";
598 code = krb5_cc_store_cred(k5context, cc, incred);
599 if (code) goto Failed;
602 "Wrote ticket file to %s\n",
603 krb5_cc_get_name(k5context, cc));
607 afs_com_err(rn, code, what);
608 if (writeTicketFile) {
610 krb5_cc_close(k5context, cc);
618 for (service = service_temp;;service = "afs") {
619 memset(mcred, 0, sizeof *mcred);
620 mcred->client = princ;
621 code = krb5_parse_name(k5context, service, &mcred->server);
623 afs_com_err(rn, code, "Unable to parse service <%s>\n", service);
626 if (tofree) { free(tofree); tofree = 0; }
627 if (!(code = krb5_unparse_name(k5context, mcred->server, &outname)))
629 else outname = service;
630 code = krb5_get_credentials(k5context, 0, cc, mcred, &outcred);
631 krb5_free_principal(k5context, mcred->server);
632 if (code != KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN || service != service_temp) break;
634 if (authtype & FORCE_RXK5) break;
640 afs_com_err(rn, code, "Unable to get credentials to use %s", outname);
645 if (authtype & FORCE_RXK5) {
646 struct ktc_principal aserver[1];
649 memset(aserver, 0, sizeof *aserver);
650 strncpy(aserver->cell, cellconfig->name, MAXKTCREALMLEN-1);
651 code = ktc_SetK5Token(k5context, aserver, afscred, viceid, dosetpag);
653 afs_com_err(rn, code, "Unable to store tokens for cell %s\n",
660 struct ktc_principal aserver[1], aclient[1];
661 struct ktc_token atoken[1];
663 memset(atoken, 0, sizeof *atoken);
665 atoken->kvno = RXKAD_TKT_TYPE_KERBEROS_V5_ENCPART_ONLY;
666 if (afs_krb5_skip_ticket_wrapper(afscred->ticket.data,
667 afscred->ticket.length, &enc_part->data,
668 &enc_part->length)) {
669 afs_com_err(rn, 0, "Can't unwrap %s AFS credential",
674 atoken->kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
675 *enc_part = afscred->ticket;
677 atoken->startTime = afscred->times.starttime;
678 atoken->endTime = afscred->times.endtime;
679 memcpy(&atoken->sessionKey, get_cred_keydata(afscred),
680 get_cred_keylen(afscred));
681 memcpy(atoken->ticket, enc_part->data,
682 atoken->ticketLen = enc_part->length);
683 memset(aserver, 0, sizeof *aserver);
684 strncpy(aserver->name, "afs", 4);
685 strncpy(aserver->cell, cellconfig->name, MAXKTCREALMLEN-1);
686 memset(aclient, 0, sizeof *aclient);
687 i = realm_len(k5context, afscred->client);
688 if (i > MAXKTCREALMLEN-1) i = MAXKTCREALMLEN-1;
689 memcpy(aclient->cell, realm_data(k5context, afscred->client), i);
692 k5_to_k4_name(k5context, afscred->client, aclient);
693 code = whoami(atoken, cellconfig, aclient, &viceid);
695 afs_com_err(rn, code, "Can't get your viceid", cellconfig->name);
698 snprintf(aclient->name, MAXKTCNAMELEN-1, "AFS ID %d", viceid);
701 k5_to_k4_name(k5context, afscred->client, aclient);
702 code = ktc_SetToken(aserver, atoken, aclient, dosetpag);
704 afs_com_err(rn, code, "Unable to store tokens for cell %s\n",
710 krb5_free_principal(k5context, princ);
711 krb5_free_cred_contents(k5context, incred);
712 if (outcred) krb5_free_creds(k5context, outcred);
714 krb5_cc_close(k5context, cc);
715 if (tofree) free(tofree);