forked from Unidata/netcdf-c
/
t_auth.c
222 lines (191 loc) · 4.85 KB
/
t_auth.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*! \file
Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
2015, 2016, 2017, 2018
University Corporation for Atmospheric Research/Unidata.
See \ref copyright file for more info.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#define DEBUG
#include "netcdf.h"
#include "nctestserver.h"
#undef NOEMBED
#undef NOLOCAL
#undef NOHOME
#define NOREDIR
#define KEEPRC
#define RC ".ocrc"
#define SPECRC "./ocrc"
#define USERPWD "tiggeUser:tigge"
#define COOKIEFILE "./cookies"
#define URL1 "https://%s@%s/dodsC/restrict/testData.nc"
#define URL2 "https://%s/dodsC/restrict/testData.nc"
#define URL3 "https://%s@thredds-test.ucar.edu/thredds/dodsC/restrict/testData.nc"
/* Embedded user:pwd */
static char url1[1024];
/* user:pwd from RC*/
static char url2[1024];
/* Test redirect from different machine*/
static char url3[1024];
static int testrc(const char* prefix, const char* url);
static void fillrc(const char* path);
static void killrc();
#ifdef DEBUG
static void
CHECK(int e, const char* msg)
{
if(e == NC_NOERR) return;
if(msg == NULL) msg = "Error";
fprintf(stderr,"%s: %s\n", msg, nc_strerror(e));
exit(1);
}
#endif
int
main(int argc, char** argv)
{
int ncid,retval,pass;
FILE* rc;
const char* dfaltsvc;
char buffer[8192];
const char* home;
fprintf(stderr,"Testing: Authorization\n");
dfaltsvc = nc_findtestserver("thredds",REMOTETESTSERVERS);
if(svc == NULL) {
fprintf(stderr,"WARNING: Cannot locate test server\n");
exit(0);
}
snprintf(url1,sizeof(url1),URL1,USERPWD,dfaltsvc); /* embedded */
snprintf(url2,sizeof(url2),URL2,dfaltsvc); /* using rc file */
#ifdef DEBUG
fprintf(stderr,"url1: %s\n",url1);
fprintf(stderr,"url2: %s\n",url2);
fflush(stderr);
#endif
pass = 1; /* assume success */
killrc();
fprintf(stderr,"Testing: Http Basic Authorization\n\n");
#ifndef NOEMBED
{
fprintf(stderr,"Testing: Embedded user:pwd: %s\n",url1);
retval = nc_open(url1, 0, &ncid);
if(retval != NC_NOERR) {
pass = 0;
fprintf(stderr,"*** FAIL: Testing embedded user:pwd\n");
} else {
fprintf(stderr,"*** PASS: Testing embedded user:pwd\n");
retval = nc_close(ncid);
}
fflush(stderr);
}
#endif
#ifndef NOLOCAL
{
/* Test 1: RC in ./ */
fprintf(stderr,"Testing: user:pwd in %s/%s: %s\n",".",RC);
if(!testrc(".",url2)) {
fprintf(stderr,"user:pwd in %s/%s failed\n",".",RC);
exit(1);
}
}
#endif
#ifndef NOHOME
{
/* Test 1: RC in HOME */
home = getenv("HOME");
fprintf(stderr,"user:pwd in %s/%s: %s\n",home,RC);
if(!testrc(home,url2)) {
fprintf(stderr,"user:pwd in %s/%s failed\n",home,RC);
exit(1);
}
}
#endif
#ifndef NOREDIR
{
fprintf(stderr,"Testing: Http Basic Redirect\n\n");
snprintf(url3,sizeof(url3),URL3,USERPWD);
fprintf(stderr,"Basic redirect: %s\n",url3);
retval = nc_open(url3, 0, &ncid);
if(retval != NC_NOERR) {
fprintf(stderr,"*** XFAIL: Basic redirect\n");
} else {
fprintf(stderr,"*** PASS: Basic redirect\n");
retval = nc_close(ncid);
}
fflush(stderr);
}
#endif
return !pass;
}
static int
testrc(const char* prefix, const char* url)
{
int pass = 1;
int retval;
int ncid;
char rcpath[8192];
FILE* rc;
snprintf(rcpath,sizeof(rcpath),"%s/%s",prefix,RC);
rc = fopen(rcpath,"w");
if(rc == NULL) {
fprintf(stderr,"Cannot create ./%s\n",RC);
exit(1);
}
fclose(rc);
fillrc(rcpath);
retval = nc_open(url, 0, &ncid);
if(retval != NC_NOERR) {
pass = 0;
fprintf(stderr,"*** FAIL: Testing: user:pwd in %s\n",rcpath);
} else {
retval = nc_close(ncid);
fprintf(stderr,"*** PASS: Testing: user:pwd in %s\n",rcpath);
}
fflush(stderr);
#ifndef KEEPRC
unlink(rcpath); /* delete the file */
#endif
return pass;
}
static void
fillrc(const char* path)
{
FILE* rc;
killrc();
rc = fopen(path,"w");
if(rc == NULL) {
fprintf(stderr,"cannot create rc file: %s\n",path);
exit(1);
}
#ifdef DEBUG
fprintf(rc,"HTTP.VERBOSE=1\n");
#endif
fprintf(rc,"HTTP.COOKIEJAR=%s\n",COOKIEFILE);
fprintf(rc,"HTTP.VALIDATE=1\n");
fprintf(rc,"HTTP.CREDENTIALS.USERPASSWORD=%s\n",USERPWD);
fclose(rc);
}
static void
killrc()
{
const char* home;
char path[1024];
#ifdef KEEPRC
fprintf(stderr,"kill: ./%s\n",RC);
#else
snprintf(path,sizeof(path),"%s/%s",".",RC);
unlink(path); /* delete the file */
#endif
home = getenv("HOME");
#ifdef KEEPRC
fprintf(stderr,"kill: %s/%s\n",home,RC);
#else
snprintf(path,sizeof(path),"%s/%s",home,RC);
unlink(path);
#endif
}