pr_init-fix-20050623
[openafs.git] / src / aklog / aklog_param.c
1 /* 
2  * $Id$
3  * 
4  * Copyright 1990,1991 by the Massachusetts Institute of Technology
5  * For distribution and copying rights, see the file "mit-copyright.h"
6  */
7
8 #if !defined(lint) && !defined(SABER)
9 static char *rcsid = "$Id$";
10 #endif /* lint || SABER */
11
12 #include <afs/stds.h>
13 #include "aklog.h"
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <string.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #ifdef HAVE_MEMORY_H
22 #include <memory.h>
23 #endif
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27 #ifdef HAVE_MALLOC_H
28 #include <malloc.h>
29 #endif
30 #include <kerberosIV/krb.h>
31 #include <krb5.h>
32
33
34 #ifndef TRUE
35 #define TRUE 1
36 #endif
37
38 #ifndef FALSE
39 #define FALSE 0
40 #endif
41
42 #ifndef WINDOWS
43 #if !defined(HAVE_UNISTD_H) || (defined(__sun__) && ! defined(__svr4__))
44 extern int readlink ARGS((char *, char *, size_t));
45 #endif
46 /* extern int lstat ARGS((char *, struct stat *)); */
47 extern char *getwd ARGS((char *));
48 #endif /* WINDOWS */
49
50 static krb5_ccache  _krb425_ccache = 0;
51
52 #ifndef WINDOWS         /* Don't have lstat() */
53 #ifdef __STDC__
54 static int isdir(char *path, unsigned char *val)
55 #else
56 static int isdir(path, val)
57   char *path;
58   unsigned char *val;
59 #endif /* __STDC__ */
60 {
61     struct stat statbuf;
62
63     if (lstat(path, &statbuf) < 0)
64         return (-1);
65     else {
66         if ((statbuf.st_mode & S_IFMT) == S_IFDIR) 
67             *val = TRUE;
68         else
69             *val = FALSE;
70         return (0);
71     }  
72 }
73 #endif /* WINDOWS */
74
75 #ifdef __STDC__
76 static int get_cred(krb5_context context, 
77                         char *name, char *inst, char *realm, CREDENTIALS *c,
78                         krb5_creds **creds)
79 #else
80 static int get_cred(context, name, inst, realm, c, creds)
81   krb5_context context;
82   char *name;
83   char *inst;
84   char *realm;
85   CREDENTIALS *c;
86   krb5_creds **creds;
87 #endif /* __STDC__ */
88 {
89     krb5_creds increds;
90     krb5_error_code r;
91     static krb5_principal client_principal = 0;
92
93     memset((char *)&increds, 0, sizeof(increds));
94 /* ANL - instance may be ptr to a null string. Pass null then */
95     if ((r = krb5_build_principal(context, &increds.server,
96                      strlen(realm), realm,
97                      name,
98            (inst && strlen(inst)) ? inst : (void *) NULL,
99                      (void *) NULL))) {
100         return((int)r);
101     }
102
103     if (!_krb425_ccache)
104         krb5_cc_default(context, &_krb425_ccache);
105     if (!client_principal)
106         krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
107
108     increds.client = client_principal;
109     increds.times.endtime = 0;
110         /* Ask for DES since that is what V4 understands */
111     increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
112
113     r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
114     if (r)
115         return((int)r);
116
117 /*       This requires krb524d to be running with the KDC */
118     r = krb5_524_convert_creds(context, *creds, c);
119     return((int)r);
120 }
121
122
123 #ifdef __STDC__
124 static int get_user_realm(krb5_context context,char *realm)
125 #else
126 static int get_user_realm(context, realm)
127   krb5_context context;
128   char *realm;
129
130 #endif /* __STDC__ */
131 {
132     static krb5_principal client_principal = 0;
133     int i;
134
135     if (!_krb425_ccache)
136         krb5_cc_default(context, &_krb425_ccache);
137     if (!client_principal)
138         krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
139
140     i = krb5_princ_realm(context, client_principal)->length;
141     if (i > REALM_SZ-1) i = REALM_SZ-1;
142     strncpy(realm,krb5_princ_realm(context, client_principal)->data,i);
143     realm[i] = 0;
144     return(KSUCCESS);
145 }
146
147
148 #ifndef WINDOWS
149
150 #ifdef __STDC__
151 static void pstderr(char *string)
152 #else
153 static void pstderr(string)
154   char *string;
155 #endif /* __STDC__ */
156 {
157     write(2, string, strlen(string));
158 }
159
160
161 #ifdef __STDC__
162 static void pstdout(char *string)
163 #else
164 static void pstdout(string)
165   char *string;
166 #endif /* __STDC__ */
167 {
168     write(1, string, strlen(string));
169 }
170
171 #else /* WINDOWS */
172
173 static void pstderr(char *string)
174 {
175     if (_isatty(_fileno(stderr)))
176         fprintf(stderr, "%s\r\n", string);
177     else
178         MessageBox(NULL, string, AKLOG_DIALOG_NAME, MB_OK | MB_ICONSTOP);
179 }
180
181 static void pstdout(char *string)
182 {
183     if (_isatty(_fileno(stdout)))
184         fprintf(stdout, "%s\r\n", string);
185     else
186         MessageBox(NULL, string, AKLOG_DIALOG_NAME, MB_OK);
187 }
188
189 #endif /* WINDOWS */
190
191 #ifdef __STDC__
192 static void exitprog(char status)
193 #else
194 static void exitprog(status)
195   char status;
196 #endif /* __STDC__ */
197 {
198     exit(status);
199 }
200
201
202 #ifdef __STDC__
203 void aklog_init_params(aklog_params *params)
204 #else
205 void aklog_init_params(params)
206   aklog_params *params;
207 #endif /* __STDC__ */
208 {
209 #ifndef WINDOWS
210     params->readlink = readlink;
211     params->isdir = isdir;
212     params->getwd = getwd;
213 #endif
214     params->get_cred = get_cred;
215     params->get_user_realm = get_user_realm;
216     params->pstderr = pstderr;
217     params->pstdout = pstdout;
218     params->exitprog = exitprog;
219 }