Actual source code: box.c

petsc-3.11.4 2019-09-28
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

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

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

 78:    Input Parameters:
 79: +  comm - the MPI communicator
 80: -  tokensize - size of the token arrays

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

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

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

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

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

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

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

103:    Level: intermediate

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

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

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

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

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

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

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

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

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

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

172:    Output Parameter:
173: +   access_token - token that can be passed to PetscBoxUpload()
174: -   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

176:    Level: intermediate

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

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

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

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

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

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

236: #include <sys/stat.h>

238: /*@C
239:      PetscBoxUpload - Loads a file to the Box Drive

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

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

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

250:   Options Database:
251: .  -box_refresh_token   XXX

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

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

260:     With PETSc option -box_refresh_token  XXX given
261:     PetscBoxRefresh(comm,NULL,access_token,sizeof(access_token));
262:     PetscBoxUpload(comm,access_token,filename);

264:     With refresh token entered in some way by the user
265:     PetscBoxRefresh(comm,refresh_token,access_token,sizeof(access_token));
266:     PetscBoxUpload(comm,access_token,filename);

268:     PetscBoxAuthorize(comm,access_token,refresh_token,sizeof(access_token));
269:     PetscBoxUpload(comm,access_token,filename);

271:    Level: intermediate

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

275: @*/
276: PetscErrorCode PetscBoxUpload(MPI_Comm comm,const char access_token[],const char filename[])
277: {
278:   SSL_CTX        *ctx;
279:   SSL            *ssl;
280:   int            sock;
282:   char           head[1024],buff[8*1024],*body,*title;
283:   PetscMPIInt    rank;
284:   struct stat    sb;
285:   size_t         len,blen,rd;
286:   FILE           *fd;

289:   MPI_Comm_rank(comm,&rank);
290:   if (!rank) {
291:     PetscStrcpy(head,"Authorization: Bearer ");
292:     PetscStrcat(head,access_token);
293:     PetscStrcat(head,"\r\n");
294:     PetscStrcat(head,"uploadType: multipart\r\n");

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