It is possible to support a number of authorization schemes in the netCDF-C library.
With one exception, authorization in the netCDF-C library is delegated to the oc2 code, which in turn delegates it to the libcurl library. The exception is that the location of the rc file can be specified by setting the environment variable *NCRCFILE*.
> Note that the value of this environment variable should be the absolute path of the rc file, not the path to its containing directory.
OC can support user authorization using those provided by the curl library. This includes basic password authentication as well as certificate-based authorization.
With some exceptions (e.g. see the section on [redirection](#auth_REDIR)) The libcurl authorization mechanisms can be accessed in two ways
1. Inserting the username and password into the url, or
2. Accessing information from a so-called *rc* file named either *.daprc* or *.dodsrc*
# URL-Based Authentication {#auth_URL_AUTH}
For simple password based authentication, it is possible to directly insert the username and the password into a url in this form.
http://username:password@host/...
This username and password will be used if the server asks for authentication. Note that only simple password authentication is supported in this format. Specifically note that [redirection](#auth_REDIR) based authorization will not work with this.
# RC File Authentication {#auth_DODSRC}
The oc library supports an *rc* file mechanism to allow the passing of a number of parameters to liboc and libcurl.
The file must be called one of the following names: ".daprc" or ".dodsrc" If both .daprc and .dodsrc exist, then the .daprc file will take precedence.
Searching for the rc file first looks in the current directory and then in the home directory (as defined by the HOME environment variable). It is also possible to specify a direct path using the *-R* option to ocprint or using the *oc\_set\_rcfile* procedure (see oc.h). Note that for these latter cases, the path must be to the file itself, not to the containing directory.
The key HTTP.CREDENTIALS.USERPASSWORD can be used to set the simple password authentication. This is an alternative to setting it in the url. The value must be of the form "username:password".
## Cookie Jar {#auth_cookie_jar}
The `HTTP.COOKIEJAR` key specifies the name of file from which to read cookies (`CURLOPT_COOKIEJAR`) and also the file into which to store cookies (`CURLOPT_COOKIEFILE`). The same value is used for both CURLOPT values. It defaults to in-memory storage.
`HTTP.SSL.CERTIFICATE` specifies a file path for a file containing a PEM cerficate. This is typically used for client-side authentication.
`HTTP.SSL.KEY` is essentially the same as HTTP.SSL.CERTIFICATE and should usually have the same value.
`HTTP.SSL.KEYPASSWORD` specifies the password for accessing the HTTP.SSL.KEY HTTP.SSL.CERTIFICATE file.
`HTTP.SSL.CAPATH` specifies the path to a directory containing trusted certificates for validating server sertificates.
`HTTP.SSL.VALIDATE` is a boolean (1/0) value that if true (1) specifies that the client should verify the server's presented certificate.
`HTTP.PROXY_SERVER` specified the url for accessing the proxy: (e.g.http://[username:password@]host[:port])
## Redirection-Based Authentication {#auth_REDIR}
Some sites provide authentication by using a third party site to to the authentication. One example is [URS](https://uat.urs.earthdata.nasa.gov), the EOSDIS User Registration System.
1. The client contacts the server of interest (SOI), the actual data provider.
2. The SOI sends a redirect to the client to connect to the URS system.
3. The client authenticates with URS.
4. URS sends a redirect (with authorization information) to send the client back to the SOI to actually obtain the data.
In order for this to work with libcurl, the client will usually need to provide a .netrc` file so that the redirection will work correctly. The format of this `.netrc` file will contain content that typically look like this.
1. Specify the netrc file to liboc using the procedure in oc.h:
oc_set_netrc(OClink* link, const char* file)
(This is equivalent to the -N flag to ocprint).
2. Put the following line in your .daprc/.dodsrc file.
HTTP.NETRC=<pathtonetrcfile>
One final note. In using this, it is probable that you will need to specify a cookie jar (HTTP.COOKIEJAR) so that the redirect site can pass back authorization information.
Each line of the rc file can begin with a host+port enclosed in square brackets. The form is "host:port". If the port is not specified then the form is just "host". The reason that more of the url is not used is that libcurl's authorization grain is not any finer than host level.
If the url request from, say, the *oc\_open* method has a host+port matchine one of the prefixes in the rc file, then the corresponding entry will be used, otherwise ignored.
Some systems, notably ESG (Earth System Grid), requires the use of client-side certificates, as well as being [re-direction based](#auth_REDIR). This requires setting the following entries:
-`HTTP.COOKIEJAR` — a file path for storing cookies across re-direction.
-`HTTP.NETRC` — the path to the netrc file.
-`HTTP.SSL.CERTIFICATE` — the file path for the client side certificate file.
-`HTTP.SSL.KEY` — this should have the same value as `HTTP.SSL.CERTIFICATE`.
-`HTTP.SSL.CAPATH` — the path to a "certificates" directory.
-`HTTP.SSL.VALIDATE` — force validation of the server certificate.
# Appendix B. ESG Access in Detail {#auth_ESGDETAIL}
It is possible to access Earth Systems Grid (ESG) datasets from ESG servers through the C API using the techniques described in the section on [Client-Side Certificates](#auth_CLIENTCERTS).
In order to access ESG datasets, however, it is necessary to register as a user with ESG and to setup your environment so that proper authentication is established between an oc client program and the ESG data server. Specifically, it is necessary to use what is called "client-side keys" to enable this authentication. Normally, when a client accesses a server in a secure fashion (using "https"), the server provides an authentication certificate to the client. With client-side keys, the client must also provide a certificate to the server so that the server can know with whom it is communicating.
The oc library uses the *curl* library and it is that underlying library that must be properly configured.
## Terminology {#auth_terminology}
The key elements for client-side keys requires the constructions of two "stores" on the client side.
-`Keystore` - a repository to hold the client side key.
-`Truststore` - a repository to hold a chain of certificates that can
be used to validate the certificate sent by the server to the
client.
The server actually has a similar set of stores, but the client need not be concerned with those.
## Initial Steps {#auth_initial_steps}
The first step is to obtain authorization from ESG. Note that this information may evolve over time, and may be out of date. This discussion is in terms of BADC and NCSA. You will need to substitute as necessary.
1. Register at http://badc.nerc.ac.uk/register to obtain access to badc and to obtain an openid, which will looks something like:
https://ceda.ac.uk/openid/Firstname.Lastname
2. Ask BADC for access to whatever datasets are of interest.
3. Obtain short term credentials at http://grid.ncsa.illinois.edu/myproxy/MyProxyLogon/ You will need to download and run the MyProxyLogon program. This will create a keyfile in, typically, the directory ".globus". The keyfile will have a name similar to this: "x509up\_u13615" The other elements in ".globus" are certificates to use in validating the certificate your client gets from the server.
4. Obtain the program source ImportKey.java from this location: http://www.agentbob.info/agentbob/79-AB.html (read the whole page, it will help you understand the remaining steps).
## Building the KeyStore {#auth_build_keystore}
You will have to modify the keyfile in the previous step and then create
a keystore and install the key and a certificate. The commands are
Note, the file names "key.der" and "cert.der" can be whatever you choose. It is probably best to leave the .der extension, though.
### Building the TrustStore {#auth_build_truststore}
Building the truststore is a bit tricky because as provided, the certificates in ".globus" need some massaging. See the script below for the details. The primary command is this, which is executed for every certificate, c, in globus. It sticks the certificate into the file named "truststore"
Refer to the section on [Client-Side Certificates](#auth_CLIENTCERTS). The keys specified there must be set in the rc file to support ESG access.
-`HTTP.COOKIEJAR`=\~/.dods\_cookies
-`HTTP.NETRC`=\~/.netrc
-`HTTP.SSL.CERTIFICATE`=\~/esgkeystore
-`HTTP.SSL.KEY`=\~/esgkeystore
-`HTTP.SSL.CAPATH`=\~/.globus
-`HTTP.SSL.VALIDATE`=1
Of course, the file paths above are suggestions only; you can modify as needed. The `HTTP.SSL.CERTIFICATE` and `HTTP.SSL.KEY` entries should have same value, which is the file path for the certificate produced by MyProxyLogon. The `HTTP.SSL.CAPATH` entry should be the path to the "certificates" directory produced by MyProxyLogon.
As noted, also uses re-direction based authentication. So, when it receives an initial connection from a client, it redirects to a separate authentication server. When that server has authenticated the client, it redirects back to the original url to complete the request.
### Script for creating Stores {#auth_script_for_creating_stores}