Changeset - 89e9a3a9940c
[Not reviewed]
default
0 1 0
ethanzonca - 16 years ago 2009-10-10 19:43:19

Moved updating code to BlendNet
1 file changed with 0 insertions and 50 deletions:
0 comments (0 inline, 0 general)
src/server/slavefuncs.c
Show inline comments
 
/*
 
  Copyright 2009 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
 /*
 
  * Registration on server. Needs attention. Prevent account spamming.
 
  * distrenslave -c username email@example.com
 
 */
 

	
 
#include "protocol.h"
 
#include "asprintf.h"
 
#include "slavefuncs.h"
 
#include "distrenjob.h"
 
#include "execio.h"
 

	
 
#include <curl/curl.h>
 
#include <curl/types.h>
 
#include <curl/easy.h>
 

	
 
#include <stdio.h>
 
#include <string.h>
 
#include <unistd.h>
 
#include <stdlib.h>
 
#include <sys/stat.h>
 
#include <fcntl.h>
 

	
 

	
 

	
 
/**
 
 utility function for XPath-ish stuff:
 
 */
 
xmlNodePtr xml_quickxpath(xmlXPathContextPtr xpathctxt, xmlChar *path)
 
{
 
  xmlNodePtr toreturn;
 

	
 
  xmlXPathObjectPtr xmlxpathobjptr;
 
  xmlxpathobjptr = xmlXPathEval(path, xpathctxt);
 
  if(!xmlxpathobjptr
 
     || !xmlxpathobjptr->nodesetval->nodeNr)
 
    {
 
      fprintf(stderr, "XPath resolution failed for ``%s'' in ``%s'' (``%s'')\n", path, xpathctxt->doc->name, xpathctxt->doc->URL);
 
      return (xmlNodePtr)NULL;
 
    }
 

	
 
  toreturn = *(xmlxpathobjptr->nodesetval->nodeTab);
 

	
 
  xmlXPathFreeObject(xmlxpathobjptr);
 

	
 
  return toreturn;
 
}
 

	
 
/** Ensures that rendering engines on the computer are up-to-date */
 
int software_updatecheck(char *datadir){
 
  char *pathtoserverVersion;
 
  _distren_asprintf(&pathtoserverVersion, "%s/serverEngineVersion.info",datadir);
 

	
 
  char *engineDownloadPath;
 
  _distren_asprintf(&engineDownloadPath, "%s/engines/blender.tgz",datadir);
 

	
 
  char *pathtolocalVersion;
 
  _distren_asprintf(&pathtolocalVersion, "%s/envines/blender/version.info",datadir);
 

	
 
  curlget("http://protofusion.org/distren/srv/version.info",  pathtoserverVersion);
 
  struct stat buffer;
 
  char serverVersion[5]; // Version numbers are nice and short
 
  char localVersion[5]; // Version numbers are nice and short
 

	
 

	
 
  // Read server version
 
  {
 
    FILE * serverVersionFile;
 
    serverVersionFile = fopen(pathtoserverVersion, "r");
 
    fscanf(serverVersionFile, "%s",serverVersion);
 
    fclose(serverVersionFile);
 
  }
 

	
 
  // Read local version
 
  {
 
    FILE * localVersionFile;
 
    localVersionFile = fopen(pathtolocalVersion, "r");
 
    fscanf(localVersionFile, "%s",localVersion);
 
    fclose(localVersionFile);
 
  }
 

	
 

	
 
  // If a rendering engine was never downloaded
 
  if( stat(SYSCONFDIR "/engines/blender", &buffer) == -1){
 
    fprintf(stderr,"You don't have the blender engine. Preparing to download...");
 
    curlget("http://protofusion.org/distren/srv/blender-lin32-dist.tgz", engineDownloadPath); // Add calls for operating system info
 
    // untar(SYSCONFDIR "/engines/blender.tgz");
 
  }
 

	
 
  // If a rendering engine is out-of-date
 
  else if( serverVersion != localVersion){
 
    fprintf(stderr,"You don't have the latest blender engine. Preparing to download...");
 
    curlget("http://protofusion.org/distren/srv/blender-lin32-dist.tgz", engineDownloadPath); // Add calls for operating system info
 
    // untar(SYSCONFDIR "/engines/blender.tgz");
 
  }
 

	
 
  return 0;
 
}
 

	
 
/** Stub for deleting job data from the disk. @TODO: unstubify me! */
 
int delete_jobdata(int jobnum, char *datadir)
 
{
 
  char *jobpath;
 
  _distren_asprintf(&jobpath, "%s/%d", datadir, jobnum);
 
  rmdir(jobpath);
 
  fprintf(stderr, "I just failed to remove all of your job data because I can only delete empty directories! Haha!\nPlease contact the dev team :D\n\tWe'd be overjoyed to know that someone was willing to try to use this in this devel/design/planning stage ;-)\n");
 
  return 0;
 
}
 

	
 
/** Stub stub stubbiness ugh @TODO: Kill me. */
 
void tell_the_server(int stuff){
 
}
 

	
 
/** Function referenced by curlget() to write data to disk. */
 
size_t curl_writetodisk(void *ptr, size_t size, size_t nmemb, FILE *stream)
 
 {
 
    return fwrite(ptr, size, nmemb, stream);
 
  }
 

	
 
/** Helper function for cURL's progress display */
 
int curl_progress( char *Bar,double t,double d,double ultotal,double ulnow)
 
{
 
fprintf(stderr,"Downloading: %f%% complete\r",d/t*100);
 
return 0;
 
}
 

	
 
/** Retrieves a URL with cURL and saves it to disk */
 
int curlget(char *url, char *out){
 
  fprintf(stderr,"Preparing to download %s",url);
 
  double *Bar; // Stores cURL progress display info
 
  CURL *curl;
 
  CURLcode res;
 
  FILE *outfile;
 

	
 
  curl = curl_easy_init();
 
  if(curl) {
 
	outfile = fopen(out, "w"); // Open where we're writing to
 

	
 
  curl_easy_setopt(curl, CURLOPT_URL, url);
 
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
 
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writetodisk); // this MUST be set for win32 compat.
 
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
 
  curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curl_progress);
 
  curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &Bar);
 
  res = curl_easy_perform(curl);
 
  curl_easy_cleanup(curl);
 
  }
 
  fprintf(stderr,"\n"); // Clears out he progressbar's carriage return
 
  return res; // 0 means OK, nonzero means AAAH badness
 
}
 

	
 
/** Posts a file to a url with cUrl */
 
int curlpost(char *filename, char *url){
 
  char *targetname = "uploadedfile"; // Name of the target in the php file on the server (Don't change me unless you have different PHP code)
 
  CURL *curl;
 
  CURLcode res;
 
  struct curl_httppost *formpost=NULL;
 
  struct curl_httppost *lastptr=NULL;
 
  struct curl_slist *headerlist=NULL;
 
  static const char buf[] = "Expect:";
 

	
 
  curl_global_init(CURL_GLOBAL_ALL);
 

	
 
  /* upload field... */
 
  curl_formadd(&formpost,
 
               &lastptr,
 
               CURLFORM_COPYNAME, targetname,
 
               CURLFORM_FILE, filename,
 
               CURLFORM_END);
 
  /* filename field... */
 
  curl_formadd(&formpost,
 
               &lastptr,
 
               CURLFORM_COPYNAME, "filename",
 
               CURLFORM_COPYCONTENTS, filename,
 
               CURLFORM_END);
 
  /* submit field, not usually needed, just in case... */
 
  curl_formadd(&formpost,
 
               &lastptr,
 
               CURLFORM_COPYNAME, "submit",
 
               CURLFORM_COPYCONTENTS, "send",
 
               CURLFORM_END);
 

	
 
  curl = curl_easy_init();
 
  headerlist = curl_slist_append(headerlist, buf);
 
  if(curl) {
 
    /* Setting the URL to get the post, and the contents of the post */
 
    curl_easy_setopt(curl, CURLOPT_URL, url);
 
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
 
    res = curl_easy_perform(curl);
 

	
 
    curl_easy_cleanup(curl);
 
    /* cleanup the formpost junk */
 
    curl_formfree(formpost);
 
    curl_slist_free_all (headerlist);
 
  }
 
  return 0;
 
}
 

	
 
/** Generates a SSH key with ssh-keygen */
 
int ssh_keygen(){
 
  /* Checks to see if the keys are already present. */
 
  int status;
 
  struct stat buffer;
 
  status = stat(SYSCONFDIR "/distren.id_rsa", &buffer);
 
  if(status != -1){
 
    fprintf(stderr, "***Please delete etc/distren.id_rsa and etc/distren.id_rsa.pub to register.\n");
 
    return 0;
 
  }
 

	
 
  /* start execio code */
 
  char *command = "ssh-keygen"; // @TODO: append .exe if win32?
 
  int ret;
 
  char *cmd[] = { command, "-q", "-f", SYSCONFDIR "/distren.id_rsa", "-N", "", (char *)NULL }; // TODO: Give me the correct args!
 
  char buf[10];
 
  struct execio *testrem;
 
  size_t readlen;
 
  ret = execio_open(&testrem, command, cmd); // @TODO: This path will be absolute for testing, should be relative to install on production
 
  buf[9] = '\0';
 
  while(!execio_read(testrem, buf, 9, &readlen))
 
    {
 
      if(readlen > 9) {
 
        fprintf(stderr, "Something is terribly wrong!\n");
 
      }
 
      buf[readlen] = '\0';
 
      fprintf(stderr, "read \"%s\"\n", buf);
 
    }
 
  execio_close(testrem);
 
  /* end execio code */
 

	
 
  // Supposedly execio returns 1 if it has bad args.
 
  if(ret == 1){
 
    fprintf(stderr, "Generating your key failed. Ensure that ssh-keygen is present!\n"); // Use different executor that searches the path? there is one...
 
    return 0;
 
  }
 
  else{
 
    fprintf(stderr,"We successfully generated your key! Yay!\n");
 
    return 1;
 
  }
 
return 0;
 
}
 

	
 
/** Registers the user on the DistRen server */
 
int register_user(char *username, char *email)
 
{
 

	
 
/* Note: this code moved here from after the useradd code, so useradd doesn't happen if there is an existing key, etc */
 
  fprintf(stderr,"Preparing to download setup key...\n");
 
  curlget("http://protofusion.org/distren/keys/setup_rsa", SYSCONFDIR "/setup_rsa"); // Grabs key to use for setup login
 

	
 
  /* puts the person's username in the conf */
 
  if(conf_replace(username) == 0){
 
    fprintf(stderr, "Failed!\n");
 
    return 0;
 
  }
 

	
 
  /* generates keys for login, @TODO: pub key must somehow be sent to the server. */
 
  if(ssh_keygen() == 0){
 
    fprintf(stderr, "Failed!\n");
 
    return 0;
 
  }
 

	
 
  /*
 
   * All created user accounts should be sandboxed accordingly, requiring a different skel, and the default shell to be rbash. Also,
 
   * a custom path defined in the .bashrc of the skel is needed.
 
   */
 
  int ret;
 
  char buf[10];
 
  struct execio *testrem;
 
  char *execargv[] =
 
    {
 
      "ssh",
 
      "distren_setup@protofusion.org",
 
      "-i",
 
      SYSCONFDIR "/setup_rsa",
 
      "-p",
 
      "23",
 
      "newuser",
 
      "-M",
 
      "-c",
 
      email,
 
      "-d",
 
      "/home/distren",
 
      "--gid",
 
      "541",
 
      username,
 
      (char *)NULL
 
    };
 
  size_t readlen;
 
  ret = execio_open(&testrem, "ssh", execargv); // TODO: Grab returns from this someday.
 
  buf[9] = '\0';
0 comments (0 inline, 0 general)