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>
12 #include <afs/com_err.h>
15 #if defined(AFS_AIX51_ENV)
16 #include <sys/types.h>
17 #include <sys/param.h>
23 #include <sys/socket.h>
34 #include <afs/cellconfig.h>
35 #include <afs/dirpath.h>
38 #include <afs/ptserver.h>
39 #include "aix_auth_prototypes.h"
41 struct afsconf_cell ak_cellconfig; /* General information about the cell */
42 static char linkedcell[MAXCELLCHARS+1];
43 static krb5_ccache _krb425_ccache = NULL;
62 * Why doesn't AFS provide these prototypes?
65 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
71 static krb5_error_code get_credv5(krb5_context context, char *, char *, char *,
72 char *, krb5_creds **);
73 static int get_user_realm(krb5_context, char *);
75 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
77 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
78 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
79 #define second_comp(c, p) (krb5_princ_size(c, p) > 1)
80 #define realm_data(c, p) krb5_princ_realm(c, p)->data
81 #define realm_len(c, p) krb5_princ_realm(c, p)->length
83 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
85 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
86 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
87 #define second_comp(c, p) (krb5_principal_get_comp_string(c, p, 1) != NULL)
88 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
89 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
92 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
95 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
97 #define get_cred_keydata(c) c->keyblock.contents
98 #define get_cred_keylen(c) c->keyblock.length
99 #define get_creds_enctype(c) c->keyblock.enctype
101 #elif defined(HAVE_KRB5_CREDS_SESSION)
103 #define get_cred_keydata(c) c->session.keyvalue.data
104 #define get_cred_keylen(c) c->session.keyvalue.length
105 #define get_creds_enctype(c) c->session.keytype
108 #error "Must have either keyblock or session member of krb5_creds"
111 char *afs_realm_of_cell(krb5_context context, struct afsconf_cell *cellconfig, int fallback)
113 static char krbrlm[REALM_SZ+1];
115 krb5_error_code retval;
122 p = strchr(cellconfig->hostName[0], '.');
126 strcpy(krbrlm, cellconfig->name);
127 for (p=krbrlm; *p; p++) {
132 if (retval = krb5_get_host_realm(context,
133 cellconfig->hostName[0], &hrealms))
135 if(!hrealms[0]) return 0;
136 strcpy(krbrlm, hrealms[0]);
138 if (hrealms) krb5_free_host_realm(context, hrealms);
144 aklog_authenticate(char *userName, char *response, int *reenter, char **message)
146 char *reason, *pword, prompt[256];
148 int code, unixauthneeded, password_expires = -1;
150 krb5_context context;
152 krb5_init_context(&context);
154 *message = (char *)0;
156 status = auth_to_cell(context, userName, NULL, NULL);
159 *message = (char *)malloc(1024);
160 sprintf(*message, "Unable to obtain AFS tokens: %s.\n",
161 afs_error_message(status));
162 return AUTH_FAILURE; /* NOTFOUND? */
167 * Local hack - if the person has a file in their home
168 * directory called ".xlog", read that for a list of
169 * extra cells to authenticate to
172 if ((pwd = getpwuid(getuid())) != NULL) {
175 char fcell[100], xlog_path[512];
177 strcpy(xlog_path, pwd->pw_dir);
178 strcat(xlog_path, "/.xlog");
180 if ((stat(xlog_path, &sbuf) == 0) &&
181 ((f = fopen(xlog_path, "r")) != NULL)) {
183 while (fgets(fcell, 100, f) != NULL) {
186 fcell[strlen(fcell) - 1] = '\0';
188 auth_status = auth_to_cell(context, userName, fcell, NULL);
189 if (status == AKLOG_SUCCESS)
190 status = auth_status;
192 status = AKLOG_SOMETHINGSWRONG;
200 static krb5_error_code get_credv5(krb5_context context, char *user,
201 char *name, char *inst, char *realm,
206 static krb5_principal client_principal = 0;
209 memset(&increds, 0, sizeof(increds));
210 /* instance may be ptr to a null string. Pass null then */
211 if ((r = krb5_build_principal(context, &increds.server,
212 strlen(realm), realm,
214 (inst && strlen(inst)) ? inst : (void *) NULL,
218 r = krb5_cc_default(context, &_krb425_ccache);
220 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: krb5_cc_default returns %d", r);
223 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
225 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: krb5_cc_get_principal returns %d", r);
228 increds.client = client_principal;
229 increds.times.endtime = 0;
230 /* Ask for DES since that is what V4 understands */
231 get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
233 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
239 static int get_user_realm(krb5_context context, char *realm)
241 static krb5_principal client_principal = 0;
245 krb5_cc_default(context, &_krb425_ccache);
246 if (!client_principal)
247 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
249 i = realm_len(context, client_principal);
250 if (i > REALM_SZ-1) i = REALM_SZ-1;
251 strncpy(realm,realm_data(context, client_principal), i);
258 aklog_chpass(char *userName, char *oldPasswd, char *newPasswd, char **message)
264 aklog_passwdexpired(char *userName, char **message)
270 aklog_passwdrestrictions(char *userName, char *newPasswd, char *oldPasswd,
277 aklog_getpasswd(char * userName)
284 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *local_cell, char *linkedcell)
287 struct afsconf_dir *configdir;
289 memset(local_cell, 0, sizeof(local_cell));
290 memset(cellconfig, 0, sizeof(*cellconfig));
292 if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
296 if (afsconf_GetLocalCell(configdir, local_cell, MAXCELLCHARS)) {
297 return AFSCONF_FAILURE;
300 if ((cell == NULL) || (cell[0] == 0))
303 linkedcell[0] = '\0';
304 if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
305 status = AFSCONF_NOTFOUND;
307 if (cellconfig->linkedCell)
308 strncpy(linkedcell,cellconfig->linkedCell,MAXCELLCHARS);
310 (void) afsconf_Close(configdir);
316 * Log to a cell. If the cell has already been logged to, return without
317 * doing anything. Otherwise, log to it and mark that it has been logged
320 static int auth_to_cell(krb5_context context, char *user, char *cell, char *realm)
323 char username[BUFSIZ]; /* To hold client username structure */
324 afs_int32 viceId; /* AFS uid of user */
326 char name[ANAME_SZ]; /* Name of afs key */
327 char primary_instance[INST_SZ]; /* Instance of afs key */
328 char secondary_instance[INST_SZ]; /* Backup instance to try */
329 int try_secondary = 0; /* Flag to indicate if we try second */
330 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
331 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
332 char local_cell[MAXCELLCHARS+1];
333 char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
334 static char lastcell[MAXCELLCHARS+1] = { 0 };
335 static char confname[512] = { 0 };
336 krb5_creds *v5cred = NULL;
337 struct ktc_principal aserver;
338 struct ktc_principal aclient;
339 struct ktc_token atoken, btoken;
340 int afssetpag = 0, uid = -1;
343 memset(name, 0, sizeof(name));
344 memset(primary_instance, 0, sizeof(primary_instance));
345 memset(secondary_instance, 0, sizeof(secondary_instance));
346 memset(realm_of_user, 0, sizeof(realm_of_user));
347 memset(realm_of_cell, 0, sizeof(realm_of_cell));
348 syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog starting: user %s uid %d", user, getuid());
349 if (confname[0] == '\0') {
350 strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
351 confname[sizeof(confname) - 2] = '\0';
354 /* NULL or empty cell returns information on local cell */
355 if ((status = get_cellconfig(cell, &ak_cellconfig,
356 local_cell, linkedcell))) {
357 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: get_cellconfig returns %d", status);
361 strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
362 cell_to_use[MAXCELLCHARS] = 0;
365 * Find out which realm we're supposed to authenticate to. If one
366 * is not included, use the kerberos realm found in the credentials
370 if (realm && realm[0]) {
371 strcpy(realm_of_cell, realm);
374 char *afs_realm = afs_realm_of_cell(context, &ak_cellconfig, FALSE);
377 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: afs_realm_of_cell returns %d", status);
378 return AFSCONF_FAILURE;
381 strcpy(realm_of_cell, afs_realm);
384 /* We use the afs.<cellname> convention here...
386 * Doug Engert's original code had principals of the form:
390 * in the KDC, so the name wouldn't conflict with DFS. Since we're
391 * not using DFS, I changed it just to look for the following
397 * Because people are transitioning from afs@realm to afs/cell,
398 * we configure things so that if the first one isn't found, we
399 * try the second one. You can select which one you prefer with
400 * a configure option.
403 strcpy(name, AFSKEY);
405 if (1 || strcasecmp(cell_to_use, realm_of_cell) != 0) {
406 strncpy(primary_instance, cell_to_use, sizeof(primary_instance));
407 primary_instance[sizeof(primary_instance)-1] = '\0';
408 if (strcasecmp(cell_to_use, realm_of_cell) == 0) {
410 secondary_instance[0] = '\0';
413 primary_instance[0] = '\0';
415 strncpy(secondary_instance, cell_to_use,
416 sizeof(secondary_instance));
417 secondary_instance[sizeof(secondary_instance)-1] = '\0';
421 * Extract the session key from the ticket file and hand-frob an
422 * afs style authenticator.
426 * Try to obtain AFS tickets. Because there are two valid service
427 * names, we will try both, but trying the more specific first.
429 * afs/<cell>@<realm> i.e. allow for single name with "."
433 status = get_credv5(context, user, name, primary_instance, realm_of_cell,
436 if ((status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
437 status == KRB5KRB_ERR_GENERIC) && !realm_of_cell[0]) {
438 char *afs_realm = afs_realm_of_cell(context, &ak_cellconfig, TRUE);
441 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: afs_realm_of_cell returns %d", status);
442 return AFSCONF_FAILURE;
445 strcpy(realm_of_cell, afs_realm);
447 if (strcasecmp(cell_to_use, realm_of_cell) == 0) {
449 secondary_instance[0] = '\0';
452 status = get_credv5(context, user, name, primary_instance,
453 realm_of_cell, &v5cred);
455 if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
456 status == KRB5KRB_ERR_GENERIC) {
458 status = get_credv5(context, user, name, secondary_instance,
459 realm_of_cell, &v5cred);
463 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: get_credv5 returns %d", status);
467 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
468 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
469 strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
472 * The default is to use rxkad2b, which means we put in a full
473 * V5 ticket. If the user specifies -524, we talk to the
474 * 524 ticket converter.
481 len = min(get_princ_len(context, v5cred->client, 0),
482 second_comp(context, v5cred->client) ?
483 MAXKTCNAMELEN - 2 : MAXKTCNAMELEN - 1);
484 strncpy(username, get_princ_str(context, v5cred->client, 0), len);
485 username[len] = '\0';
487 if (second_comp(context, v5cred->client)) {
488 strcat(username, ".");
489 p = username + strlen(username);
490 len = min(get_princ_len(context, v5cred->client, 1),
491 MAXKTCNAMELEN - strlen(username) - 1);
492 strncpy(p, get_princ_str(context, v5cred->client, 1), len);
496 memset(&atoken, 0, sizeof(atoken));
497 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
498 atoken.startTime = v5cred->times.starttime;;
499 atoken.endTime = v5cred->times.endtime;
500 memcpy(&atoken.sessionKey, get_cred_keydata(v5cred),
501 get_cred_keylen(v5cred));
502 atoken.ticketLen = v5cred->ticket.length;
503 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
506 if ((status = get_user_realm(context, realm_of_user))) {
507 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: get_user_realm returns %d", status);
508 return KRB5_REALM_UNKNOWN;
510 if (strcmp(realm_of_user, realm_of_cell)) {
511 strcat(username, "@");
512 strcat(username, realm_of_user);
515 strcpy(lastcell, aserver.cell);
518 * This is a crock, but it is Transarc's crock, so
519 * we have to play along in order to get the
520 * functionality. The way the afs id is stored is
521 * as a string in the username field of the token.
522 * Contrary to what you may think by looking at
523 * the code for tokens, this hack (AFS ID %d) will
524 * not work if you change %d to something else.
528 /* This actually crashes long-running daemons */
529 if (!pr_Initialize (0, confname, aserver.cell))
530 status = pr_SNameToId (username, &viceId);
531 if ((status == 0) && (viceId != ANONYMOUSID))
532 sprintf (username, "AFS ID %d", (int) viceId);
535 * This actually only works assuming that your uid and pts space match
536 * and probably this works only for the local cell anyway.
539 if ((uid = getuid()) == 0) {
540 if ((pwd = getpwnam(user)) == NULL) {
541 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: getpwnam %s failed", user);
546 /* Don't do first-time registration. Handle only the simple case */
547 if ((status == 0) && (viceId != ANONYMOUSID))
548 sprintf (username, "AFS ID %d", ((uid==0)?(int)pwd->pw_uid:(int)uid));
551 /* Reset the "aclient" structure before we call ktc_SetToken.
552 * This structure was first set by the ktc_GetToken call when
553 * we were comparing whether identical tokens already existed.
555 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
556 strcpy(aclient.instance, "");
557 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
559 #ifndef AFS_AIX51_ENV
560 /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
561 * this routine, it will not add the token. It is not clear what
562 * is going on here! So we will do the following operation.
563 * On AIX 5 this kills our parent. So we won't.
565 write(2,"",0); /* dummy write */
567 afssetpag = (getpagvalue("afs") > 0) ? 1 : 0;
569 struct sigaction newAction, origAction;
573 sigemptyset(&newAction.sa_mask);
574 newAction.sa_handler = SIG_DFL;
575 newAction.sa_flags = 0;
576 status = sigaction(SIGCHLD, &newAction, &origAction);
578 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: sigaction returned %d", status);
581 syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog: in daemon? forking to set tokens");
584 syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog child: setting tokens");
586 status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag);
588 syslog(LOG_AUTH|LOG_ERR, "LAM aklog child: set tokens, returning %d", status);
589 exit((status == 0)?0:255);
592 pcid = waitpid(cid, &wstatus, 0);
593 } while ((pcid == -1) && (errno == EINTR));
594 if ((pcid == cid) && WIFEXITED(wstatus))
595 status = WEXITSTATUS(wstatus);
599 syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog: collected child status %d", status);
600 sigaction(SIGCHLD, &origAction, NULL);
602 status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag);
605 syslog(LOG_AUTH|LOG_ERR, "LAM aklog: set tokens returned %d", status);
607 syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog: set tokens, pag %d", getpagvalue("afs"));
612 aklog_initialize(struct secmethod_table *meths)
614 memset(meths, 0, sizeof(struct secmethod_table));
615 syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog loaded: uid %d pag %d", getuid(), getpagvalue("afs"));
617 * Initialize the exported interface routines.
618 * Aside from method_authenticate, these are just no-ops.
620 meths->method_chpass = aklog_chpass;
621 meths->method_authenticate = aklog_authenticate;
622 meths->method_passwdexpired = aklog_passwdexpired;
623 meths->method_passwdrestrictions = aklog_passwdrestrictions;
624 meths->method_getpasswd = aklog_getpasswd;
628 #endif /* AFS_AIX51_ENV */