irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

genOSAuth.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <pwd.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <vector>
10 
11 #include "authenticate.h"
12 #include "osauth.h"
13 
14 
15 int
16 main( int argc, char *argv[] ) {
17 
18  /* make sure the provided username matches what the OS thinks */
19  char * username = getenv( OS_AUTH_ENV_USER );
20  if ( username == NULL ) {
21  if ( argc > 0 ) {
22  /* probably means someone has run from command-line */
23  printf( "%s is run through an iRODS library call, and probably won't do anything useful for you.\n",
24  argv[0] );
25  printf( "%s exiting.\n", argv[0] );
26  return 1;
27  }
28  printf( "Username is null" );
29  return 1;
30  }
31  char username2[NAME_LEN];
32  int uid = osauthGetUsername( username2, NAME_LEN );
33  if ( uid == -1 ) {
34  return 1;
35  }
36  if ( strcmp( username, username2 ) ) {
37  printf( "Username %s does not match OS user %s",
38  username, username2 );
39  return 1;
40  }
41 
42  /* read the challenge from stdin */
43  int challenge_len;
44  if ( sizeof( challenge_len ) != read( 0, ( void* )&challenge_len, sizeof( challenge_len ) ) ) {
45  printf( "Couldn't read challenge length from stdin: %s",
46  strerror( errno ) );
47  return 1;
48  }
49  if ( challenge_len != CHALLENGE_LEN ) {
50  printf( "Challenge length must be %ju", ( uintmax_t )CHALLENGE_LEN );
51  return 1;
52  }
53  std::vector<char> challenge( CHALLENGE_LEN );
54  if ( CHALLENGE_LEN != read( 0, &challenge[0], CHALLENGE_LEN ) ) {
55  printf( "Couldn't read challenge from stdin: %s",
56  strerror( errno ) );
57  return 1;
58  }
59 
60  /* read the key from the key file */
61  char * keybuf = NULL;
62  int key_len;
63  if ( osauthGetKey( &keybuf, &key_len ) ) {
64  printf( "Error retrieving key. Exiting." );
65  return 1;
66  }
67 
68  char authenticator[16]; /* hard coded at 16 bytes .. size of an md5 hash */
69  if ( osauthGenerateAuthenticator( username, uid, &challenge[0], keybuf, key_len, authenticator, 16 ) ) {
70  printf( "Could not generate the authenticator" );
71  return 1;
72  }
73 
74  /* write out the authenticator to stdout */
75  if ( write( 1, authenticator, 16 ) == -1 ) {
76  int errsv = errno;
77  printf( "Error %s writing the authenticator to stdout.",
78  strerror( errsv ) );
79  return 1;
80  }
81 
82  return 0;
83 }
84 
NULL
#define NULL
Definition: rodsDef.h:70
osauth.h
OS_AUTH_ENV_USER
#define OS_AUTH_ENV_USER
Definition: osauth.h:10
main
int main(int argc, char *argv[])
Definition: genOSAuth.cpp:16
authenticate.h
osauthGetUsername
int osauthGetUsername(char *username, int username_len)
Definition: osauth.cpp:413
CHALLENGE_LEN
#define CHALLENGE_LEN
Definition: authenticate.h:10
osauthGetKey
int osauthGetKey(char **key, int *key_len)
Definition: osauth.cpp:167
NAME_LEN
#define NAME_LEN
Definition: rodsDef.h:55
osauthGenerateAuthenticator
int osauthGenerateAuthenticator(char *username, int uid, char *challenge, char *key, int key_len, char *authenticator, int authenticator_len)
Definition: osauth.cpp:107