-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestclient.c
176 lines (153 loc) · 4.3 KB
/
testclient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>
#include <unistd.h> /* For getpass */
#include <string.h> /* For strdup */
#include <openssl/bio.h>
#include "pam_myproxy.h"
#include "myproxy_client.h"
/**
* Parses the given error code and prints a suitable error message
* \return 0 on success, 1 on general error, 2 on authentication error
*/
int parse_error_code(int rc, char *errstr,
cred_t *cred, pam_myproxy_opts_t *opts) {
/* Default return code: general error */
int prc=1;
switch(rc) {
case PAM_MYPROXY_SUCCESS:
prc=0;
break;
case PAM_MYPROXY_INVALID_USERNAME:
fprintf(stderr,"Invalid username %s\n", cred->username);
prc=2;
break;
case PAM_MYPROXY_INVALID_PASSWORD:
fprintf(stderr,"Invalid password\n");
prc=2;
break;
case PAM_MYPROXY_CERT_EXPIRED:
fprintf(stderr,"Certificate expired\n");
prc=2;
break;
case PAM_MYPROXY_OUT_OF_MEM:
fprintf(stderr,"Out of memory while retrieving proxy\n");
break;
case PAM_MYPROXY_INVAL:
fprintf(stderr,"Invalid input value configured\n");
break;
case PAM_MYPROXY_BUF_TOO_SMALL:
fprintf(stderr,
"Buffer too small when forming myproxy request\n");
break;
case PAM_MYPROXY_BIO_WRITE_ERR:
fprintf(stderr,"Error writing to bio\n");
break;
case PAM_MYPROXY_BIO_READ_ERR:
fprintf(stderr,"Error reading from bio\n");
break;
case PAM_MYPROXY_RESPONSE_ERR:
fprintf(stderr,
"Unexpected answer from myproxy server: %s\n",
errstr ? errstr : "");
break;
case PAM_MYPROXY_CSR_ERR:
fprintf(stderr,
"Error creating Certificate Signing Request\n");
break;
case PAM_MYPROXY_HOST_UNSET:
fprintf(stderr,"Myproxy server is unset\n");
break;
case PAM_MYPROXY_CTX_ERR:
fprintf(stderr,"Error setting up SSL CTX\n");
break;
case PAM_MYPROXY_SSL_ERR:
fprintf(stderr,"Error setting up SSL (pre-connect)\n");
break;
case PAM_MYPROXY_CONNECT_ERR:
fprintf(stderr,
"Error connecting to myproxy server %s:%d\n",
opts->endpoint.host,opts->endpoint.port);
break;
default:
fprintf(stderr,"Unknown error in pam myproxy\n");
break;
}
/* Free the errstr */
if (errstr) {
free(errstr);
errstr=NULL;
}
return prc;
}
/**
* Example myproxy client using the functions from myproxy_client
*/
int main(int argc, char *argv[]) {
cred_t cred={NULL,NULL,NULL,NULL,NULL};
pam_myproxy_opts_t opts;
int rc=0,prc;
BIO *bio=NULL;
char *errstr=NULL;
if (argc<2) {
fprintf(stderr,"Usage: %s <dn> [conffile]\n",argv[0]);
return 1;
}
/* Initialize opts */
_pam_myproxy_config_init(&opts);
/* Set conffile */
opts.conffile=(argv[2] ? strdup(argv[2]) : strdup(PAM_MYPROXY_CONF));
if (opts.conffile==NULL) {
fprintf(stderr,"Out of memory\n");
return 1;
}
/* Parse config file */
switch (_pam_myproxy_parse_config(&opts)) {
case 0:
break;
case -1:
fprintf(stderr,"Cannot read config file\n");
return 1;
case -2:
fprintf(stderr,"Permissions on config file are wrong\n");
return 1;
case -3:
fprintf(stderr,"Out of memory while parsing config file\n");
return 1;
default:
fprintf(stderr,"Unknown error while parsing config file\n");
return 1;
}
/* Set user credentials */
cred.username=argv[1];
cred.password=getpass("Enter myproxy password: ");
cred.privkey=NULL;
cred.chain=NULL;
prc=1; /* default exit code 1 */
/* Setup connection */
rc=_myproxy_connect_ssl(&bio, &opts.endpoint, &opts.certinfo);
if ( rc==PAM_MYPROXY_SUCCESS ) {
/* Try to obtain credentials */
rc=_myproxy(bio, &cred, opts.keysize, opts.lifetime, &errstr);
/* Close connection */
_myproxy_close_ssl(&bio);
}
/* Cleanup password */
_myproxy_free_password(&cred);
/* Parse error code */
prc=parse_error_code(rc, errstr, &cred,&opts);
/* Write proxy when so far successful */
if ( rc==PAM_MYPROXY_SUCCESS ) {
if (_myproxy_write_proxy(opts.proxyfmt,&cred)) {
fprintf(stderr,"Failed to write %s\n",
cred.proxyfile ? cred.proxyfile : "(null)");
prc=1;
} else {
printf("Proxy left in %s\n",cred.proxyfile);
prc=0;
}
}
/* Cleanup opts */
_pam_myproxy_config_free(&opts);
/* Free credentials */
_myproxy_free_cred(&cred);
return prc;
}