6070aa138729315cc2dbadd2541775f87f8939c5
[gssweb.git] / json_gssapi / main.cpp
1 #include <commands/GSSImportName.h>
2 #include <commands/GSSInitSecContext.h>
3 #include <commands/GSSAcquireCred.h>
4 #include <datamodel/GSSBuffer.h>
5 #include <exception>
6 #include <iostream>
7 #include <string>
8 #ifdef WIN32
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <share.h>
13 #else
14 #include <unistd.h>
15 #endif
16 #include <util_json.h>
17 #include <GSSRequest.h>
18
19
20 using std::cin;
21 using std::cout;
22 using std::endl;
23 using std::getline;
24 using std::string;
25
26 int main(int argc, char **argv) {
27   /* Variables */
28   string output;
29   char *input;
30   size_t len;
31   
32   /* Error checking */
33   
34   /* Setup */
35   
36   /* Main processing */
37 #ifdef WIN32
38   if (argc < 2) {
39       return -1;
40   }
41   int fd;
42   if (_sopen_s(&fd, argv[1], _O_BINARY, _SH_DENYNO, _S_IREAD) != 0)
43   {
44       cout << "error :" << errno << " opening file: " << argv[1] << "\n";
45       return -1;
46   }
47   struct _stat fs;
48   if (_fstat(fd, &fs) != 0) {
49       cout << "error: " << errno << " from _fstat.\n";
50       return -1;
51   }
52
53   FILE *f =_fdopen(fd, "rb");
54   if (f == NULL) {
55       cout << "error: " << errno << " from _fdopen.\n";
56       return -1;
57   }
58   len = fs.st_size;
59   input = new char[len+1];
60   size_t count = fread(input, 1, len, f) ;
61   if (count != len) {
62       cout << "expected " << len << " bytes from fread; got " << count << ".\n";
63       return -1;
64   }
65   fclose(f);
66 #else
67   ssize_t readThisRound;
68   size_t readTotal, readRemaining;
69   do 
70   {
71     // Read 32 bit length
72     len = 0;
73     readThisRound = readTotal = 0;
74     while(4 != readTotal)
75     {
76       readThisRound = read(0, ((&len) + readTotal), 4 - readTotal);
77       readTotal += readThisRound;
78     }
79     
80     // Reads the number of bytes indicated by the above read
81     input = new char[len + 1];
82     readTotal = readThisRound = 0;
83     while (readTotal < len)
84     {
85       readRemaining = len - readTotal;
86       readThisRound = read( 0, &(input[readTotal]), readRemaining);
87       if (-1 == readThisRound)
88         break;
89       else
90         readTotal += readThisRound;
91     }
92     // ... and null-terminate it
93 #endif
94     input[len] = '\0';
95     
96     GSSRequest *req = new GSSRequest(string(input));
97     req->execute();
98     output = req->getResponse();
99     len = output.length();
100     
101     cout.write((char *)&len, 4);
102     cout << output;
103     cout.flush();
104 #ifndef WIN32
105   } while(1);
106 #endif
107   return 0;
108 }