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