Actual source code: box.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: #include <petscwebclient.h>
  3: #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  4: #pragma gcc diagnostic ignored "-Wdeprecated-declarations"

  6: /*
  7:    These variables identify the code as a PETSc application to Box.

  9:    See -   http://stackoverflow.com/questions/4616553/using-oauth-in-free-open-source-software
 10:    Users can get their own application IDs - goto https://developers.box.com

 12: */
 13: #define PETSC_BOX_CLIENT_ID  "sse42nygt4zqgrdwi0luv79q1u1f0xza"
 14: #define PETSC_BOX_CLIENT_ST  "A0Dy4KgOYLB2JIYZqpbze4EzjeIiX5k4"

 16: #if defined(PETSC_HAVE_SAWS)
 17: #include <mongoose.h>

 19: static volatile char *result = NULL;

 21: static int PetscBoxWebServer_Private(struct mg_connection *conn)
 22: {
 23:   const struct mg_request_info *request_info = mg_get_request_info(conn);
 24:   result = (char*) request_info->query_string;
 25:   return 1;  /* Mongoose will now not handle the request */
 26: }

 28: /*
 29:     Box can only return an authorization code to a Webserver, hence we need to start one up and wait for
 30:     the authorization code to arrive from Box
 31: */
 32: static PetscErrorCode PetscBoxStartWebServer_Private(void)
 33: {
 34:   PetscErrorCode      ierr;
 35:   int                 optionsLen = 5;
 36:   const char          *options[optionsLen];
 37:   struct mg_callbacks callbacks;
 38:   struct mg_context   *ctx;
 39:   char                keyfile[PETSC_MAX_PATH_LEN];
 40:   PetscBool           exists;

 43:   options[0] = "listening_ports";
 44:   options[1] = "8081s";

 46:   PetscStrcpy(keyfile,"sslclient.pem");
 47:   PetscTestFile(keyfile,'r',&exists);
 48:   if (!exists) {
 49:     PetscGetHomeDirectory(keyfile,PETSC_MAX_PATH_LEN);
 50:     PetscStrcat(keyfile,"/");
 51:     PetscStrcat(keyfile,"sslclient.pem");
 52:     PetscTestFile(keyfile,'r',&exists);
 53:     if (!exists) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate sslclient.pem file in current directory or home directory");
 54:   }

 56:   options[2] = "ssl_certificate";
 57:   options[3] = keyfile;
 58:   options[4] = NULL;

 60:   /* Prepare callbacks structure. We have only one callback, the rest are NULL. */
 61:   PetscMemzero(&callbacks, sizeof(callbacks));
 62:   callbacks.begin_request = PetscBoxWebServer_Private;
 63:   ctx = mg_start(&callbacks, NULL, options);
 64:   if (!ctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to start up webserver");
 65:   while (!result) {};
 66:   return(0);
 67: }

 69: #if defined(PETSC_HAVE_UNISTD_H)
 70: #include <unistd.h>
 71: #endif

 75: /*@C
 76:      PetscBoxAuthorize - Get authorization and refresh token for accessing Box drive from PETSc

 78:    Not collective, only the first process in MPI_Comm does anything

 80:    Input Parameters:
 81: +  comm - the MPI communicator
 82: -  tokensize - size of the token arrays

 84:    Output Parameters:
 85: +  access_token - can be used with PetscBoxUpload() for this one session
 86: -  refresh_token - can be used for ever to obtain new access_tokens with PetscBoxRefresh(), guard this like a password
 87:                    it gives access to your Box Drive

 89:    Notes: This call requires stdout and stdin access from process 0 on the MPI communicator

 91:    You can run src/sys/webclient/examples/tutorials/boxobtainrefreshtoken to get a refresh token and then in the future pass it to
 92:    PETSc programs with -box_refresh_token XXX

 94:    This requires PETSc be installed using --with-saws or --download-saws

 96:    Requires the user have created a self-signed ssl certificate with

 98: $    saws/CA.pl  -newcert  (using the passphrase of password)
 99: $    cat newkey.pem newcert.pem > sslclient.pem

101:     and put the resulting file in either the current directory (with the application) or in the home directory. This seems kind of
102:     silly but it was all I could figure out.

104:    Level: intermediate

106: .seealso: PetscBoxRefresh(), PetscBoxUpload(), PetscURLShorten()

108: @*/
109: PetscErrorCode PetscBoxAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize)
110: {
111:   SSL_CTX        *ctx;
112:   SSL            *ssl;
113:   int            sock;
115:   char           buff[8*1024],body[1024];
116:   PetscMPIInt    rank;
117:   PetscBool      flg,found;

120:   MPI_Comm_rank(comm,&rank);
121:   if (!rank) {
122:     if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output");
123:     PetscPrintf(comm,"Cut and paste the following into your browser:\n\n"
124:                             "https://www.box.com/api/oauth2/authorize?"
125:                             "response_type=code&"
126:                             "client_id="
127:                             PETSC_BOX_CLIENT_ID
128:                             "&state=PETScState"
129:                             "\n\n");
130:     PetscBoxStartWebServer_Private();
131:     PetscStrbeginswith((const char*)result,"state=PETScState&code=",&flg);
132:     if (!flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Did not get expected string from Box got %s",result);
133:     PetscStrncpy(buff,(const char*)result+22,sizeof(buff));

135:     PetscSSLInitializeContext(&ctx);
136:     PetscHTTPSConnect("www.box.com",443,ctx,&sock,&ssl);
137:     PetscStrcpy(body,"code=");
138:     PetscStrcat(body,buff);
139:     PetscStrcat(body,"&client_id=");
140:     PetscStrcat(body,PETSC_BOX_CLIENT_ID);
141:     PetscStrcat(body,"&client_secret=");
142:     PetscStrcat(body,PETSC_BOX_CLIENT_ST);
143:     PetscStrcat(body,"&grant_type=authorization_code");

145:     PetscHTTPSRequest("POST","www.box.com/api/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));
146:     PetscSSLDestroyContext(ctx);
147:     close(sock);

149:     PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);
150:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Box did not return access token");
151:     PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found);
152:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Box did not return refresh token");

154:     PetscPrintf(comm,"Here is your Box refresh token, save it in a save place, in the future you can run PETSc\n");
155:     PetscPrintf(comm,"programs with the option -box_refresh_token %s\n",refresh_token);
156:     PetscPrintf(comm,"to access Box Drive automatically\n");
157:   }
158:   return(0);
159: }
160: #endif

164: /*@C
165:      PetscBoxRefresh - Get a new authorization token for accessing Box drive from PETSc from a refresh token

167:    Not collective, only the first process in the MPI_Comm does anything

169:    Input Parameters:
170: +   comm - MPI communicator
171: .   refresh token - obtained with PetscBoxAuthorize(), if NULL PETSc will first look for one in the options data 
172:                     if not found it will call PetscBoxAuthorize()
173: -   tokensize - size of the output string access_token

175:    Output Parameter:
176: +   access_token - token that can be passed to PetscBoxUpload()
177: -   new_refresh_token - the old refresh token is no longer valid, not this is different than Google where the same refresh_token is used forever

179:    Level: intermediate

181: .seealso: PetscURLShorten(), PetscBoxAuthorize(), PetscBoxUpload()

183: @*/
184: PetscErrorCode PetscBoxRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],char new_refresh_token[],size_t tokensize)
185: {
186:   SSL_CTX        *ctx;
187:   SSL            *ssl;
188:   int            sock;
190:   char           buff[8*1024],body[1024];
191:   PetscMPIInt    rank;
192:   char           *refreshtoken = (char*)refresh_token;
193:   PetscBool      found;

196:   MPI_Comm_rank(comm,&rank);
197:   if (!rank) {
198:     if (!refresh_token) {
199:       PetscBool set;
200:       PetscMalloc1(512,&refreshtoken);
201:       PetscOptionsGetString(NULL,"-box_refresh_token",refreshtoken,512,&set);
202: #if defined(PETSC_HAVE_SAWS)
203:       if (!set) {
204:         PetscBoxAuthorize(comm,access_token,new_refresh_token,512*sizeof(char));
205:         PetscFree(refreshtoken);
206:         return(0);
207:       }
208: #else
209:       if (!set) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Must provide refresh token with -box_refresh_token XXX");
210: #endif
211:     }
212:     PetscSSLInitializeContext(&ctx);
213:     PetscHTTPSConnect("www.box.com",443,ctx,&sock,&ssl);
214:     PetscStrcpy(body,"client_id=");
215:     PetscStrcat(body,PETSC_BOX_CLIENT_ID);
216:     PetscStrcat(body,"&client_secret=");
217:     PetscStrcat(body,PETSC_BOX_CLIENT_ST);
218:     PetscStrcat(body,"&refresh_token=");
219:     PetscStrcat(body,refreshtoken);
220:     if (!refresh_token) {PetscFree(refreshtoken);}
221:     PetscStrcat(body,"&grant_type=refresh_token");

223:     PetscHTTPSRequest("POST","www.box.com/api/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));
224:     PetscSSLDestroyContext(ctx);
225:     close(sock);

227:     PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);
228:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Box did not return access token");
229:     PetscPullJSONValue(buff,"refresh_token",new_refresh_token,tokensize,&found);
230:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Box did not return refresh token");

232:     PetscPrintf(comm,"Here is your new Box refresh token, save it in a save place, in the future you can run PETSc\n");
233:     PetscPrintf(comm,"programs with the option -box_refresh_token %s\n",new_refresh_token);
234:     PetscPrintf(comm,"to access Box Drive automatically\n");
235:   }
236:   return(0);
237: }

239: #include <sys/stat.h>

243: /*@C
244:      PetscBoxUpload - Loads a file to the Box Drive

246:      This routine has not yet been written; it is just copied from Google Drive

248:      Not collective, only the first process in the MPI_Comm uploads the file

250:   Input Parameters:
251: +   comm - MPI communicator
252: .   access_token - obtained with PetscBoxRefresh(), pass NULL to have PETSc generate one
253: -   filename - file to upload; if you upload multiple times it will have different names each time on Box Drive

255:   Options Database:
256: .  -box_refresh_token   XXX

258:   Usage Patterns:
259:     With PETSc option -box_refresh_token  XXX given
260:     PetscBoxUpload(comm,NULL,filename);        will upload file with no user interaction

262:     Without PETSc option -box_refresh_token XXX given
263:     PetscBoxUpload(comm,NULL,filename);        for first use will prompt user to authorize access to Box Drive with their processor

265:     With PETSc option -box_refresh_token  XXX given
266:     PetscBoxRefresh(comm,NULL,access_token,sizeof(access_token));
267:     PetscBoxUpload(comm,access_token,filename);

269:     With refresh token entered in some way by the user
270:     PetscBoxRefresh(comm,refresh_token,access_token,sizeof(access_token));
271:     PetscBoxUpload(comm,access_token,filename);

273:     PetscBoxAuthorize(comm,access_token,refresh_token,sizeof(access_token));
274:     PetscBoxUpload(comm,access_token,filename);

276:    Level: intermediate

278: .seealso: PetscURLShorten(), PetscBoxAuthorize(), PetscBoxRefresh()

280: @*/
281: PetscErrorCode PetscBoxUpload(MPI_Comm comm,const char access_token[],const char filename[])
282: {
283:   SSL_CTX        *ctx;
284:   SSL            *ssl;
285:   int            sock;
287:   char           head[1024],buff[8*1024],*body,*title;
288:   PetscMPIInt    rank;
289:   struct stat    sb;
290:   size_t         len,blen,rd;
291:   FILE           *fd;

294:   MPI_Comm_rank(comm,&rank);
295:   if (!rank) {
296:     PetscStrcpy(head,"Authorization: Bearer ");
297:     PetscStrcat(head,access_token);
298:     PetscStrcat(head,"\r\n");
299:     PetscStrcat(head,"uploadType: multipart\r\n");

301:     stat(filename,&sb);
302:     if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to stat file: %s",filename);
303:     len = 1024 + sb.st_size;
304:     PetscMalloc1(len,&body);
305:     PetscStrcpy(body,"--foo_bar_baz\r\n"
306:                             "Content-Type: application/json\r\n\r\n"
307:                             "{");
308:     PetscPushJSONValue(body,"title",filename,len);
309:     PetscStrcat(body,",");
310:     PetscPushJSONValue(body,"mimeType","text.html",len);
311:     PetscStrcat(body,",");
312:     PetscPushJSONValue(body,"description","a file",len);
313:     PetscStrcat(body, "}\r\n\r\n"
314:                              "--foo_bar_baz\r\n"
315:                              "Content-Type: text/html\r\n\r\n");
316:     PetscStrlen(body,&blen);
317:     fd = fopen (filename, "r");
318:     if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open file: %s",filename);
319:     rd = fread (body+blen, sizeof (unsigned char), sb.st_size, fd);
320:     if (rd != (size_t)sb.st_size) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to read entire file: %s %d %d",filename,(int)rd,(int)sb.st_size);
321:     fclose(fd);
322:     body[blen + rd] = 0;
323:     PetscStrcat(body,"\r\n\r\n"
324:                             "--foo_bar_baz\r\n");
325:     PetscSSLInitializeContext(&ctx);
326:     PetscHTTPSConnect("www.boxapis.com",443,ctx,&sock,&ssl);
327:     PetscHTTPSRequest("POST","www.boxapis.com/upload/drive/v2/files/",head,"multipart/related; boundary=\"foo_bar_baz\"",body,ssl,buff,sizeof(buff));
328:     PetscFree(body);
329:     PetscSSLDestroyContext(ctx);
330:     close(sock);
331:     PetscStrstr(buff,"\"title\"",&title);
332:     if (!title) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Upload of file %s failed",filename);
333:   }
334:   return(0);
335: }