Don't cast the pointer past to memset
[openafs.git] / src / tsm41 / aix_aklog.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/com_err.h>
13
14
15 #if defined(AFS_AIX51_ENV)
16 #include <sys/types.h>
17 #include <sys/param.h>
18 #include <stdio.h>
19 #include <locale.h>
20 #include <nl_types.h>
21 #include <pwd.h>
22 #include <netdb.h>
23 #include <sys/socket.h>
24 #include <sys/file.h>
25 #ifdef HAVE_SYS_PAG_H
26 #include <sys/pag.h>
27 #endif
28 #include <errno.h>
29 #include <usersec.h>
30 #include <syslog.h>
31
32 #include <krb5.h>
33
34 #include <afs/cellconfig.h>
35 #include <afs/dirpath.h>
36 #include <rx/rxkad.h>
37 #include <afs/auth.h>
38 #include <afs/ptserver.h>
39 #include "aix_auth_prototypes.h"
40
41 struct afsconf_cell ak_cellconfig; /* General information about the cell */
42 static char linkedcell[MAXCELLCHARS+1];
43 static krb5_ccache  _krb425_ccache = NULL;
44
45 #define AFSKEY "afs"
46 #define AFSINST ""
47
48 #ifndef ANAME_SZ
49 #define ANAME_SZ 40
50 #endif /* ANAME_SZ */
51 #ifndef REALM_SZ
52 #define REALM_SZ 40
53 #endif /* REALM_SZ */
54 #ifndef SNAME_SZ
55 #define SNAME_SZ 40
56 #endif /* SNAME_SZ */
57 #ifndef INST_SZ
58 #define INST_SZ 40
59 #endif /* INST_SZ */
60
61 /*
62  * Why doesn't AFS provide these prototypes?
63  */
64
65 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
66
67 /*
68  * Other prototypes
69  */
70
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 *);
74
75 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
76
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
82
83 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
84
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))
90
91 #else
92 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
93 #endif
94
95 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
96
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
100
101 #elif defined(HAVE_KRB5_CREDS_SESSION)
102
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
106
107 #else
108 #error "Must have either keyblock or session member of krb5_creds"
109 #endif
110
111 char *afs_realm_of_cell(krb5_context context, struct afsconf_cell *cellconfig, int fallback)
112 {
113     static char krbrlm[REALM_SZ+1];
114     char **hrealms = 0;
115     krb5_error_code retval;
116     
117     if (!cellconfig)
118         return 0;
119     
120     if (fallback) {
121         char * p;
122         p = strchr(cellconfig->hostName[0], '.');
123         if (p++)
124             strcpy(krbrlm, p);
125         else
126             strcpy(krbrlm, cellconfig->name);
127         for (p=krbrlm; *p; p++) {
128             if (islower(*p)) 
129                 *p = toupper(*p);
130         }
131     } else {
132         if (retval = krb5_get_host_realm(context,
133                                          cellconfig->hostName[0], &hrealms))
134             return 0; 
135         if(!hrealms[0]) return 0;
136         strcpy(krbrlm, hrealms[0]);
137         
138         if (hrealms) krb5_free_host_realm(context, hrealms);
139     }
140     return krbrlm;
141 }
142
143 int
144 aklog_authenticate(char *userName, char *response, int *reenter, char **message)
145 {
146     char *reason, *pword, prompt[256];
147     struct passwd *pwd;
148     int code, unixauthneeded, password_expires = -1;
149     int status;
150     krb5_context context;
151     
152     krb5_init_context(&context);
153     *reenter = 0;
154     *message = (char *)0;
155     
156     status = auth_to_cell(context, userName, NULL, NULL);
157     
158     if (status) {
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? */
163     }
164     
165 #if 0
166     /*
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
170      */
171     
172     if ((pwd = getpwuid(getuid())) != NULL) {
173         struct stat sbuf;
174         FILE *f;
175         char fcell[100], xlog_path[512];
176         
177         strcpy(xlog_path, pwd->pw_dir);
178         strcat(xlog_path, "/.xlog");
179         
180         if ((stat(xlog_path, &sbuf) == 0) &&
181             ((f = fopen(xlog_path, "r")) != NULL)) {
182             
183             while (fgets(fcell, 100, f) != NULL) {
184                 int auth_status;
185                 
186                 fcell[strlen(fcell) - 1] = '\0';
187                 
188                 auth_status = auth_to_cell(context, userName, fcell, NULL);
189                 if (status == AKLOG_SUCCESS)
190                     status = auth_status;
191                 else
192                     status = AKLOG_SOMETHINGSWRONG;
193             }
194         }
195     }
196 #endif
197     return AUTH_SUCCESS;
198 }
199
200 static krb5_error_code get_credv5(krb5_context context, char *user,  
201                                   char *name, char *inst, char *realm,
202                                   krb5_creds **creds)
203 {
204     krb5_creds increds;
205     krb5_error_code r;
206     static krb5_principal client_principal = 0;
207     char *str;
208     
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,
213                                   name,
214                                   (inst && strlen(inst)) ? inst : (void *) NULL,
215                                   (void *) NULL))) {
216         return r;
217     }
218     r = krb5_cc_default(context, &_krb425_ccache);
219     if (r) {
220         syslog(LOG_AUTH|LOG_ERR, "LAM aklog: krb5_cc_default returns %d", r);
221         return r;
222     }
223     r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal); 
224     if (r) {
225         syslog(LOG_AUTH|LOG_ERR, "LAM aklog: krb5_cc_get_principal returns %d", r);
226         return r;
227     } 
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;
232     
233     r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
234     
235     return r;
236 }
237
238
239 static int get_user_realm(krb5_context context, char *realm)
240 {
241     static krb5_principal client_principal = 0;
242     int i;
243     
244     if (!_krb425_ccache)
245         krb5_cc_default(context, &_krb425_ccache);
246     if (!client_principal)
247         krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
248     
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);
252     realm[i] = 0;
253     
254     return 0;
255 }
256
257 int
258 aklog_chpass(char *userName, char *oldPasswd, char *newPasswd, char **message)
259 {
260     return AUTH_SUCCESS;
261 }
262
263 int
264 aklog_passwdexpired(char *userName, char **message)
265 {
266     return AUTH_SUCCESS;
267 }
268
269 int
270 aklog_passwdrestrictions(char *userName, char *newPasswd, char *oldPasswd,
271                          char **message)
272 {
273     return AUTH_SUCCESS;
274 }
275
276 char *
277 aklog_getpasswd(char * userName)
278 {
279     errno = ENOSYS;
280     return NULL;
281 }
282
283
284 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *local_cell, char *linkedcell)
285 {
286     int status = 0;
287     struct afsconf_dir *configdir;
288     
289     memset(local_cell, 0, sizeof(local_cell));
290     memset(cellconfig, 0, sizeof(*cellconfig));
291     
292     if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
293         return AFSCONF_NODB;
294     }
295     
296     if (afsconf_GetLocalCell(configdir, local_cell, MAXCELLCHARS)) {
297         return AFSCONF_FAILURE;
298     }
299     
300     if ((cell == NULL) || (cell[0] == 0))
301         cell = local_cell;
302     
303     linkedcell[0] = '\0';
304     if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
305         status = AFSCONF_NOTFOUND;
306     }
307     if (cellconfig->linkedCell) 
308         strncpy(linkedcell,cellconfig->linkedCell,MAXCELLCHARS);
309     
310     (void) afsconf_Close(configdir);
311     
312     return(status);
313 }
314
315 /* 
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
318  * to.
319  */
320 static int auth_to_cell(krb5_context context, char *user, char *cell, char *realm)
321 {
322     int status = 0;
323     char username[BUFSIZ];      /* To hold client username structure */
324     afs_int32 viceId;           /* AFS uid of user */
325     
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;
341     struct passwd *pwd;
342     
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';
352     }
353     
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);
358         return(status);
359     }
360     
361     strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
362     cell_to_use[MAXCELLCHARS] = 0;
363     
364     /*
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
367      * cache.
368      */
369     
370     if (realm && realm[0]) {
371         strcpy(realm_of_cell, realm);
372     }
373     else {
374         char *afs_realm = afs_realm_of_cell(context, &ak_cellconfig, FALSE);
375         
376         if (!afs_realm) {
377             syslog(LOG_AUTH|LOG_ERR, "LAM aklog: afs_realm_of_cell returns %d", status);
378             return AFSCONF_FAILURE;
379         }
380         
381         strcpy(realm_of_cell, afs_realm);
382     }
383     
384     /* We use the afs.<cellname> convention here... 
385      *
386      * Doug Engert's original code had principals of the form:
387      *
388      * "afsx/cell@realm"
389      *
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
392      * principals:
393      *
394      * afs/<cell>@<realm>
395      * afs@<realm>
396      *
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.
401      */
402     
403     strcpy(name, AFSKEY);
404     
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) {
409             try_secondary = 1;
410             secondary_instance[0] = '\0';
411         }
412     } else {
413         primary_instance[0] = '\0';
414         try_secondary = 1;
415         strncpy(secondary_instance, cell_to_use,
416                 sizeof(secondary_instance));
417         secondary_instance[sizeof(secondary_instance)-1] = '\0';
418     }
419     
420     /* 
421      * Extract the session key from the ticket file and hand-frob an
422      * afs style authenticator.
423      */
424     
425     /*
426      * Try to obtain AFS tickets.  Because there are two valid service
427      * names, we will try both, but trying the more specific first.
428      *
429      *  afs/<cell>@<realm> i.e. allow for single name with "."
430      *  afs@<realm>
431      */
432     
433     status = get_credv5(context, user, name, primary_instance, realm_of_cell,
434                         &v5cred);
435     
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);
439         
440         if (!afs_realm) {
441             syslog(LOG_AUTH|LOG_ERR, "LAM aklog: afs_realm_of_cell returns %d", status);
442             return AFSCONF_FAILURE;
443         }
444         
445         strcpy(realm_of_cell, afs_realm);
446         
447         if (strcasecmp(cell_to_use, realm_of_cell) == 0) {
448             try_secondary = 1;
449             secondary_instance[0] = '\0';
450         }
451         
452         status = get_credv5(context, user, name, primary_instance, 
453                             realm_of_cell, &v5cred);
454     }
455     if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN || 
456         status == KRB5KRB_ERR_GENERIC) {
457         if (try_secondary)
458             status = get_credv5(context, user, name, secondary_instance,
459                                 realm_of_cell, &v5cred);
460     }
461     
462     if (status) {
463         syslog(LOG_AUTH|LOG_ERR, "LAM aklog: get_credv5 returns %d", status);
464         return status;
465     }
466     
467     strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
468     strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
469     strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
470     
471     /*
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.
475      */
476     
477     {
478         char *p;
479         int len;
480         
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';
486         
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);
493             p[len] = '\0';
494         }
495         
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);
504     }
505     
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;
509     }
510     if (strcmp(realm_of_user, realm_of_cell)) {
511         strcat(username, "@");
512         strcat(username, realm_of_user);
513     }
514     
515     strcpy(lastcell, aserver.cell);
516
517     /*
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.
525      */
526
527 #if 0
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);
533 #else
534     /* 
535      * This actually only works assuming that your uid and pts space match 
536      * and probably this works only for the local cell anyway.
537      */
538     
539     if ((uid = getuid()) == 0) {
540         if ((pwd = getpwnam(user)) == NULL) {
541             syslog(LOG_AUTH|LOG_ERR, "LAM aklog: getpwnam %s failed", user);
542             return AUTH_FAILURE;
543         }
544     }
545     
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));
549 #endif
550
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.
554      */
555     strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
556     strcpy(aclient.instance, "");
557     strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
558     
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.
564      */
565     write(2,"",0); /* dummy write */
566 #endif
567     afssetpag = (getpagvalue("afs") > 0) ? 1 : 0;
568     if (uid == 0) {
569         struct sigaction newAction, origAction;
570         pid_t cid, pcid;
571         int wstatus;
572         
573         sigemptyset(&newAction.sa_mask);
574         newAction.sa_handler = SIG_DFL;
575         newAction.sa_flags = 0;
576         status = sigaction(SIGCHLD, &newAction, &origAction);
577         if (status) {
578             syslog(LOG_AUTH|LOG_ERR, "LAM aklog: sigaction returned %d", status);
579             return AUTH_FAILURE;
580         }
581         syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog: in daemon? forking to set tokens");
582         cid = fork();
583         if (cid <= 0) {
584             syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog child: setting tokens");
585             setuid(pwd->pw_uid);
586             status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag);
587             if (status != 0)
588                 syslog(LOG_AUTH|LOG_ERR, "LAM aklog child: set tokens, returning %d", status);
589             exit((status == 0)?0:255);
590         } else {
591             do {
592                 pcid = waitpid(cid, &wstatus, 0);
593             } while ((pcid == -1) && (errno == EINTR));
594             if ((pcid == cid) && WIFEXITED(wstatus))
595                 status = WEXITSTATUS(wstatus);
596             else 
597                 status = -1;
598         } 
599         syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog: collected child status %d", status);
600         sigaction(SIGCHLD, &origAction, NULL);
601     } else {
602         status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag);
603     }
604     if (status != 0)
605         syslog(LOG_AUTH|LOG_ERR, "LAM aklog: set tokens returned %d", status);
606     else
607         syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog: set tokens, pag %d", getpagvalue("afs"));
608     return(status);
609 }
610
611 int
612 aklog_initialize(struct secmethod_table *meths)
613 {
614     memset(meths, 0, sizeof(struct secmethod_table));
615     syslog(LOG_AUTH|LOG_DEBUG, "LAM aklog loaded: uid %d pag %d", getuid(), getpagvalue("afs"));    
616     /*
617      * Initialize the exported interface routines.
618      * Aside from method_authenticate, these are just no-ops.
619      */
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;
625     
626     return (0);
627 }
628 #endif /* AFS_AIX51_ENV */