1 | /* |
---|
2 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/attach/getrealm.c,v $ |
---|
3 | * $Author: ghudson $ |
---|
4 | * |
---|
5 | * Copyright 1988 by the Massachusetts Institute of Technology. |
---|
6 | * |
---|
7 | * For copying and distribution information, please see the file |
---|
8 | * <mit-copyright.h>. |
---|
9 | * |
---|
10 | * routine to convert hostname into realm name. |
---|
11 | */ |
---|
12 | |
---|
13 | #include "config.h" |
---|
14 | #ifdef OLD_KERBEROS |
---|
15 | |
---|
16 | static char *rcsid_getrealm_c = |
---|
17 | "$Header: /afs/dev.mit.edu/source/repository/athena/bin/attach/getrealm.c,v 1.4 1996-09-19 22:13:13 ghudson Exp $"; |
---|
18 | |
---|
19 | #include <mit-copyright.h> |
---|
20 | #include <strings.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <krb.h> |
---|
23 | #include <sys/param.h> |
---|
24 | |
---|
25 | /* for Ultrix and friends ... */ |
---|
26 | #ifndef MAXHOSTNAMELEN |
---|
27 | #define MAXHOSTNAMELEN 64 |
---|
28 | #endif |
---|
29 | |
---|
30 | /* |
---|
31 | * krb_realmofhost. |
---|
32 | * Given a fully-qualified domain-style primary host name, |
---|
33 | * return the name of the Kerberos realm for the host. |
---|
34 | * If the hostname contains no discernable domain, or an error occurs, |
---|
35 | * return the local realm name, as supplied by get_krbrlm() |
---|
36 | * |
---|
37 | * The format of each line of the translation file is: |
---|
38 | * domain_name kerberos_realm |
---|
39 | * -or- |
---|
40 | * host_name kerberos_realm |
---|
41 | * |
---|
42 | * domain_name should be of the form .XXX.YYY (e.g. .LCS.MIT.EDU) |
---|
43 | * host names should be in the usual form (e.g. FOO.BAR.BAZ) |
---|
44 | */ |
---|
45 | |
---|
46 | static char ret_realm[REALM_SZ+1]; |
---|
47 | |
---|
48 | char * |
---|
49 | krb_realmofhost(host) |
---|
50 | char *host; |
---|
51 | { |
---|
52 | char *domain, *trans_idx; |
---|
53 | FILE *trans_file; |
---|
54 | char trans_host[MAXHOSTNAMELEN+1]; |
---|
55 | char trans_realm[REALM_SZ+1]; |
---|
56 | int retval; |
---|
57 | |
---|
58 | domain = strchr(host, '.'); |
---|
59 | |
---|
60 | /* prepare default */ |
---|
61 | if (domain) { |
---|
62 | strncpy(ret_realm, &domain[1], REALM_SZ); |
---|
63 | ret_realm[REALM_SZ] = '\0'; |
---|
64 | } else { |
---|
65 | get_krbrlm(ret_realm, 1); |
---|
66 | } |
---|
67 | |
---|
68 | if ((trans_file = fopen(KRB_RLM_TRANS, "r")) == (FILE *) 0) { |
---|
69 | /* krb_errno = KRB_NO_TRANS */ |
---|
70 | return(ret_realm); |
---|
71 | } |
---|
72 | while (1) { |
---|
73 | if ((retval = fscanf(trans_file, "%s %s", |
---|
74 | trans_host, trans_realm)) != 2) { |
---|
75 | if (retval == EOF) { |
---|
76 | fclose(trans_file); |
---|
77 | return(ret_realm); |
---|
78 | } |
---|
79 | continue; /* ignore broken lines */ |
---|
80 | } |
---|
81 | trans_host[MAXHOSTNAMELEN] = '\0'; |
---|
82 | trans_realm[REALM_SZ] = '\0'; |
---|
83 | if (!strcasecmp(trans_host, host)) { |
---|
84 | /* exact match of hostname, so return the realm */ |
---|
85 | (void) strcpy(ret_realm, trans_realm); |
---|
86 | fclose(trans_file); |
---|
87 | return(ret_realm); |
---|
88 | } |
---|
89 | if ((trans_host[0] = '.') && domain) { |
---|
90 | /* this is a domain match */ |
---|
91 | if (!strcasecmp(trans_host, domain)) { |
---|
92 | /* domain match, save for later */ |
---|
93 | (void) strcpy(ret_realm, trans_realm); |
---|
94 | continue; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | #endif |
---|