Exemplo n.º 1
0
static NEOERR* dyn_cmd_getadmin(struct queue_entry *q, struct cache *cd, mdb_conn *db)
{
    unsigned char *val = NULL; size_t vsize = 0;
    char *uname, *aname;
    int uid, aid;
    NEOERR *err;

    REQ_GET_PARAM_STR(q->hdfrcv, "uname", uname);
    REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname);
    uid = hash_string(uname);
    aid = hash_string(aname);

    if (cache_getf(cd, &val, &vsize, PREFIX_ADMIN"%d_%d", uid, aid)) {
        unpack_hdf(val, vsize, &q->hdfsnd);
    } else {
        MDB_QUERY_RAW(db, "track", TRACK_COL, "uid=%d AND aid=%d AND type=%d "
                      " ORDER BY id DESC LIMIT 1;",
                      NULL, uid, aid, TYPE_JOIN);
        err = mdb_set_row(q->hdfsnd, db, TRACK_COL, NULL);
        nerr_handle(&err, NERR_NOT_FOUND);
        if (err != STATUS_OK) return nerr_pass(err);

        CACHE_HDF(q->hdfsnd, ONE_MINUTE, PREFIX_ADMIN"%d_%d", uid, aid);
    }
    
    return STATUS_OK;
}
Exemplo n.º 2
0
static NEOERR* aic_cmd_app_getrlink(struct queue_entry *q, struct cache *cd, mdb_conn *db)
{
    char *aname;
    NEOERR *err;

    REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname);

    MDB_QUERY_RAW(db, "appreset", RESET_COL, "aname=$1", "s", aname);
    err = mdb_set_row(q->hdfsnd, db, RESET_COL, NULL);
    if (nerr_handle(&err, NERR_NOT_FOUND))
        return nerr_raise(REP_ERR_NRESET, "%s hasn't reseted", aname);

    return nerr_pass(err);
}
Exemplo n.º 3
0
/*
 * input : aname(STR)
 * return: NORMAL
 * reply : ["state": 0, ...] OR []
 */
static NEOERR* aic_cmd_appinfo(struct queue_entry *q, struct cache *cd, mdb_conn *db)
{
    unsigned char *val = NULL; size_t vsize = 0;
    int aid, pid;
    char *aname;
    NEOERR *err;

    REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname);
    aid = hash_string(aname);

    if (cache_getf(cd, &val, &vsize, PREFIX_APPINFO"%d", aid)) {
        unpack_hdf(val, vsize, &q->hdfsnd);
    } else {
        hdf_set_value(q->hdfsnd, "pname", aname);
        MDB_QUERY_RAW(db, "appinfo", APPINFO_COL, "aid=%d", NULL, aid);
        err = mdb_set_row(q->hdfsnd, db, APPINFO_COL, NULL);
        if (nerr_handle(&err, NERR_NOT_FOUND))
            return nerr_raise(REP_ERR_NREGIST, "%s %d hasn't regist", aname, aid);
        if (err != STATUS_OK) return nerr_pass(err);
        pid = hdf_get_int_value(q->hdfsnd, "pid", 0);
        if (pid != 0) {
            MDB_QUERY_RAW(db, "appinfo", "aname", "aid=%d", NULL, pid);
            err = mdb_set_row(q->hdfsnd, db, "pname", NULL);
            nerr_handle(&err, NERR_NOT_FOUND);
            if (err != STATUS_OK) return nerr_pass(err);
        }
        MDB_QUERY_RAW(db, "appinfo", "COUNT(*)+1 AS numuser", "pid=%d",
                      NULL, aid);
        err = mdb_set_row(q->hdfsnd, db, "numuser", NULL);
        if (err != STATUS_OK) return nerr_pass(err);

        CACHE_HDF(q->hdfsnd, AIC_CC_SEC, PREFIX_APPINFO"%d", aid);
    }
    
    return STATUS_OK;
}
Exemplo n.º 4
0
static EventEntry* aux_init_driver(void)
{
    struct aux_entry *e = calloc(1, sizeof(struct aux_entry));
    if (e == NULL) return NULL;
    NEOERR *err;

    e->base.name = (unsigned char*)strdup(PLUGIN_NAME);
    e->base.ksize = strlen(PLUGIN_NAME);
    e->base.process_driver = aux_process_driver;
    e->base.stop_driver = aux_stop_driver;
    //mevent_add_timer(&e->base.timers, 60, true, hint_timer_up_term);

    char *s = hdf_get_value(g_cfg, CONFIG_PATH".dbsn", NULL);
    err = mdb_init(&e->db, s);
    JUMP_NOK(err, error);
    
    e->cd = cache_create(hdf_get_int_value(g_cfg, CONFIG_PATH".numobjs", 1024), 0);
    if (e->cd == NULL) {
        wlog("init cache failure");
        goto error;
    }

    err = mdb_exec(e->db, NULL, "SELECT id from memory ORDER BY id DESC LIMIT 1", NULL);
    JUMP_NOK(err, error);
    err = mdb_get(e->db, "i", &m_memory_maxid);
    if (nerr_handle(&err, NERR_NOT_FOUND)) {
        mtc_err("table memory empty");
        wlog("table memory empty");
    }
    JUMP_NOK(err, error);
    
    return (EventEntry*)e;
    
error:
    if (e->base.name) free(e->base.name);
    if (e->db) mdb_destroy(e->db);
    if (e->cd) cache_free(e->cd);
    free(e);
    return NULL;
}
Exemplo n.º 5
0
/*
 * input : aname(STR) asn(STR) masn(STR) email(STR) state(INT)
 * return: NORMAL REP_ERR_ALREADYREGIST
 * reply : NULL
 */
static NEOERR* aic_cmd_appnew(struct queue_entry *q, struct cache *cd, mdb_conn *db)
{
    char *aname, *pname, *asn, *masn, *email;
    int aid, pid = 0, state;
    NEOERR *err;

    REQ_GET_PARAM_INT(q->hdfrcv, "state", state);
    REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname);
    REQ_GET_PARAM_STR(q->hdfrcv, "asn", asn);
    REQ_GET_PARAM_STR(q->hdfrcv, "masn", masn);
    REQ_GET_PARAM_STR(q->hdfrcv, "email", email);

    REQ_FETCH_PARAM_STR(q->hdfrcv, "pname", pname);

    aid = hash_string(aname);
    if (pname) pid = hash_string(pname);

    err = aic_cmd_appinfo(q, cd, db);
    nerr_handle(&err, REP_ERR_NREGIST);
    if (err != STATUS_OK) return nerr_pass(err);

    if (hdf_get_obj(q->hdfsnd, "state"))
        return nerr_raise(REP_ERR_ALREADYREGIST, "%s already regist", aname);

    MDB_EXEC(db, NULL, "INSERT INTO appinfo (aid, aname, "
             " pid, asn, masn, email, state) "
             " VALUES ($1, $2::varchar(256), $3, $4::varchar(256), "
             " $5::varchar(256), $6::varchar(256), $7);",
             "isisssi", aid, aname, pid, asn, masn, email, state);
    
    cache_delf(cd, PREFIX_APPINFO"%d", aid);
    if (pid > 0) {
        cache_delf(cd, PREFIX_APPOUSER"%d_0", pid);
    }

    return STATUS_OK;
}
Exemplo n.º 6
0
static NEOERR* aic_cmd_app_getsecy(struct queue_entry *q, struct cache *cd, mdb_conn *db)
{
    unsigned char *val = NULL; size_t vsize = 0;
    char *aname;
    int aid;
    NEOERR *err;

    REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname);
    aid = hash_string(aname);

    if (cache_getf(cd, &val, &vsize, PREFIX_SECY"%d", aid)) {
        unpack_hdf(val, vsize, &q->hdfsnd);
    } else {
        MDB_QUERY_RAW(db, "appinfo", " aname ",
                      " (aid=%d OR pid=%d) AND tune & %d = %d ",
                      NULL, aid, aid, LCS_TUNE_SECY, LCS_TUNE_SECY);
        err = mdb_set_row(q->hdfsnd, db, " aname ", NULL);
        nerr_handle(&err, NERR_NOT_FOUND);
        if (err != STATUS_OK) return nerr_pass(err);
        CACHE_HDF(q->hdfsnd, AIC_CC_SEC, PREFIX_SECY"%d", aid);
    }

    return STATUS_OK;
}
Exemplo n.º 7
0
/*
 * ids=0:1
 * { "0": { "1": { "ntt": "0", "nst": "0" } }, "success": "1" }
 *
 * ids=0:1,0:20
 * { "0": { "1": { "ntt": "0", "nst": "0" }, "20": { "ntt": "0", "nst": "0" } }, "success": "1" }
 *
 * ids=0:1,1:20
 * { "0": { "1": { "ntt": "0", "nst": "0" } }, "1": { "20": { "ntt": "0", "nst": "0" } }, "success": "1" } 
 */
static NEOERR* aux_cmd_cmtget(struct aux_entry *e, QueueEntry *q)
{
    unsigned char *val = NULL; size_t vsize = 0;
    int count, offset;
    char *ids, *idsdump, tok[128];
    int type = -1, oid = -1;
    NEOERR *err;

    REQ_GET_PARAM_STR(q->hdfrcv, "ids", ids);

    mdb_conn *db = e->db;
    struct cache *cd = e->cd;

    mdb_pagediv(q->hdfrcv, NULL, &count, &offset, NULL, q->hdfsnd);

    if (cache_getf(cd, &val, &vsize, PREFIX_COMMENT"%s_%d", ids, offset)) {
        unpack_hdf(val, vsize, &q->hdfsnd);
    } else {
        idsdump = strdup(ids);
        char *p = ids;
        while (*p) {
            if (*p == ':') {
                *p = '\0';
                type = atoi(ids);
                ids = p+1;
            }
            if (*p == ',') {
                *p = '\0';
                oid = atoi(ids);
                if (type >= 0 && oid >= 0) {
                    sprintf(tok, "%d.%d", type, oid);
                    MDB_PAGEDIV_SET(q->hdfsnd, tok, db, "comment",
                                    "type=%d AND statu=%d AND oid=%d",
                                      NULL, type, CMT_ST_NORMAL, oid);
                    MDB_QUERY_RAW(db, "comment", _COL_CMT,
                                  "type=%d AND statu=%d AND oid=%d "
                                  " ORDER BY intime DESC LIMIT %d OFFSET %d",
                                  NULL, type, CMT_ST_NORMAL, oid, count, offset);
                    sprintf(tok, "%d.%d.cmts", type, oid);
                    err = mdb_set_rows(q->hdfsnd, db, _COL_CMT, tok, NULL);
                    nerr_handle(&err, NERR_NOT_FOUND);
                    if (err != STATUS_OK) return nerr_pass(err);
                    mstr_html_escape(hdf_get_child(q->hdfsnd, tok), "content");
                    type = oid = -1;
                }
                ids = p+1;
            }
            p++;
        }
        oid = atoi(ids);
        if (type >= 0 && oid >=0) {
            sprintf(tok, "%d.%d", type, oid);
            MDB_PAGEDIV_SET(q->hdfsnd, tok, db, "comment",
                            "type=%d AND statu=%d AND oid=%d",
                            NULL, type, CMT_ST_NORMAL, oid);
            MDB_QUERY_RAW(db, "comment", _COL_CMT,
                          "type=%d AND statu=%d AND oid=%d "
                          " ORDER BY intime DESC LIMIT %d OFFSET %d",
                          NULL, type, CMT_ST_NORMAL, oid, count, offset);
            sprintf(tok, "%d.%d.cmts", type, oid);
            err = mdb_set_rows(q->hdfsnd, db, _COL_CMT, tok, NULL);
            nerr_handle(&err, NERR_NOT_FOUND);
            if (err != STATUS_OK) return nerr_pass(err);
            mstr_html_escape(hdf_get_child(q->hdfsnd, tok), "content");
        }
        
        CACHE_HDF(q->hdfsnd, CMT_CC_SEC, PREFIX_COMMENT"%s_%d", idsdump, offset);
        free(idsdump);
    }

    return STATUS_OK;
}
Exemplo n.º 8
0
NEOERR * rcfs_save (const char *path, const char *data, const char *user, 
                    const char *log)
{
  NEOERR *err;
  HDF *meta = NULL, *vers;
  char fpath[_POSIX_PATH_MAX];
  char buf[256];
  int version = 0;
  int fd;
  int lock;
  int x, l, w;

  err = rcfs_lock (path, &lock);
  if (err) return nerr_pass (err);
  do
  {
    err = rcfs_meta_load (path, &meta);
    if (err && nerr_handle (&err, NERR_NOT_FOUND))
    {
      /* new file! */
      err = hdf_init (&meta);
    }
    if (err) return nerr_pass (err);
    for (vers = hdf_get_child (meta, "Versions");
	vers;
	vers = hdf_obj_next (vers))
    {
      x = atoi (hdf_obj_name (vers));
      if (x > version) version = x;
    }

    /* new version */
    version++;
    snprintf (fpath, sizeof (fpath), "%s,%d", path, version);
    fd = open (fpath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
    if (fd == -1)
    {
      err = nerr_raise_errno (NERR_IO, "Unable to create file %s", fpath);
      break;
    }
    l = strlen(data);
    w = write (fd, data, l);
    if (w != l)
    {
      err = nerr_raise_errno (NERR_IO, "Unable to write file %s", fpath);
      close (fd);
      break;
    }
    close (fd);
    snprintf (buf, sizeof(buf), "Versions.%d.Log", version);
    err = hdf_set_value (meta, buf, log);
    if (err) break;
    snprintf (buf, sizeof(buf), "Versions.%d.User", version);
    err = hdf_set_value (meta, buf, user);
    if (err) break;
    snprintf (buf, sizeof(buf), "Versions.%d.Date", version);
    err = hdf_set_int_value (meta, buf, ne_timef());
    if (err) break;
    err = _meta_save (path, meta);
  } while (0);

  rcfs_unlock (lock);
  hdf_destroy (&meta);
  return nerr_pass (err);
}
Exemplo n.º 9
0
static NEOERR* city_cmd_s(struct city_entry *e, QueueEntry *q)
{
	unsigned char *val = NULL; size_t vsize = 0;
    char *p = NULL, *c;
	NEOERR *err;

    mdb_conn *db = e->db;
    struct cache *cd = e->cd;

    REQ_GET_PARAM_STR(q->hdfrcv, "c", c);
    REQ_FETCH_PARAM_STR(q->hdfrcv, "p", p);

    if (cache_getf(cd, &val, &vsize, PREFIX_CITY"%s.%s", p, c)) {
        unpack_hdf(val, vsize, &q->hdfsnd);
    } else {
        if (p) {
            char tok[64] = {0};
            strncpy(tok, p, 64);
            if (!strstr(p, "省") && !strstr(p, "区"))
                snprintf(tok, sizeof(tok), "%s省", p);

            MDB_QUERY_RAW(db, "city", _COL_CITY, "s=$1", "s", tok);
            mdb_set_row(q->hdfsnd, db, _COL_CITY, "province", MDB_FLAG_NO_ERR);
        }

        MDB_QUERY_RAW(db, "city", _COL_CITY, "s=$1", "s", c);
        err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "citys", NULL, MDB_FLAG_Z);
        if (nerr_handle(&err, NERR_NOT_FOUND)) {
            if (strstr(c, "市")) c[strlen(c)-3] = '\0';
            MDB_QUERY_RAW(db, "city", _COL_CITY, "s=$1", "s", c);
            err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "citys",
                               NULL, MDB_FLAG_EMPTY_OK);
        }
        if (err != STATUS_OK) return nerr_pass(err);

        /*
         * get city's parents
         */
        int cnt = 0;
        char *pid = hdf_get_valuef(q->hdfsnd, "citys.0.pid");
        while (pid && atoi(pid) > 0) {
            MDB_QUERY_RAW(db, "city", _COL_CITY, "id=%d", NULL, atoi(pid));
            err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "citys", NULL, MDB_FLAG_Z);
            TRACE_NOK(err);
            pid = hdf_get_valuef(q->hdfsnd, "citys.%d.pid", ++cnt);
        }
        
        /*
         * 北京市 has no subcities
         */
        /*
        int id = hdf_get_int_value(q->hdfsnd, "city.id", 0);
        MDB_QUERY_RAW(db, "city", _COL_CITY, "pid=%d", NULL, id);
        err = mdb_set_rows(q->hdfsnd, db, _COL_CITY, "subcities",
        NULL, MDB_FLAG_EMPTY_OK);
        if (err != STATUS_OK) return nerr_pass(err);
        */
        
        CACHE_HDF(q->hdfsnd, CITY_CC_SEC, PREFIX_CITY"%s.%s", p, c);
    }
    
    return STATUS_OK;
}
Exemplo n.º 10
0
int main(int argc, char **argv, char **envp)
{
  NEOERR *err;
  CGI *cgi;
  char *cs_file;
  char hdf_file[PATH_BUF_SIZE];
  char *p;

  /* CGI works by passing information from the server to the CGI program via
   * environment variables and stdin.  cgi_debug_init looks for a file as the
   * first argument, and loads it.  That file contains key=value pairs which
   * cgi_debug_init will load into the environment, allowing you to test your
   * program via the command line. */
  cgi_debug_init(argc, argv);

  /* The ClearSilver cgi toolkit accesses the CGI environment through a
   * wrapper.  This allows the program to be used in other environments and
   * fake the CGI environment, such as FastCGI, mod_python, PyApache, or even
   * just from Python to access the python objects instead of the libc API.
   * cgiwrap_init_std just sets up for the default CGI environment using the
   * libc api. */
  cgiwrap_init_std(argc, argv, envp);

  /* cgi_init creates a CGI struct, and parses the CGI environment variables. 
   * It creates an HDF structure as well.  */
  err = cgi_init(&cgi, NULL);
  if (err != STATUS_OK)
  {
    /* cgi_neo_error renders a NEOERR as an error CGI result */
    cgi_neo_error(cgi, err);
    /* nerr_warn_error logs the error to stderr and cleans up */
    nerr_warn_error(err);
    return -1;
  }

  /* CGI.PathTranslated is a CGI env var which maps the URL with the
   * DocumentRoot to give you the location of the referenced file on disk */
  cs_file = hdf_get_value(cgi->hdf, "CGI.PathTranslated", NULL);
  if (cs_file == NULL)
  {
    /* cgi_error returns a simple error page */
    cgi_error(cgi, "No PATH_TRANSLATED var");
    return -1;
  }

  /* The hdf.loadpaths variables specify where HDF and ClearSilver look for
   * files on the file system.  We start setting that up here based on
   * the directory of the file referenced */
  p = strrchr (cs_file, '/');
  if (p)
  {
    *p = '\0';
    err = hdf_set_value(cgi->hdf, "hdf.loadpaths.0", cs_file);
    chdir(cs_file);
    *p = '/';
    if (err)
    {
      cgi_neo_error(cgi, err);
      nerr_warn_error(err);
      return -1;
    }
  }
  /* Next, we look for a shared HDF static dataset in common.hdf */
  err = hdf_read_file(cgi->hdf, "common.hdf");
  if (err && !nerr_handle(&err, NERR_NOT_FOUND))
  {
    cgi_neo_error(cgi, err);
    nerr_warn_error(err);
    return -1;
  }
  /* Next, we look for an HDF file for this specific page.  We first look
   * for passedfile.html.hdf, then we check for a file by removing an extension
   * from the file, so something like passedfile.html we'll look for
   * passedfile.hdf */
  snprintf (hdf_file, sizeof(hdf_file), "%s.hdf", cs_file);
  err = hdf_read_file (cgi->hdf, hdf_file);
  if (err && !nerr_handle(&err, NERR_NOT_FOUND))
  {
    cgi_neo_error(cgi, err);
    nerr_warn_error(err);
    return -1;
  }
  p = strrchr (cs_file, '.');
  if (p)
  {
    *p = '\0';
    snprintf (hdf_file, sizeof(hdf_file), "%s.hdf", cs_file);
    *p = '.';
    err = hdf_read_file (cgi->hdf, hdf_file);
    if (err && !nerr_handle(&err, NERR_NOT_FOUND))
    {
      cgi_neo_error(cgi, err);
      nerr_warn_error(err);
      return -1;
    }
  }
  /* Lastly, we need to render a template.  The template is either the
   * file that was passed to us, or its specificed by CGI.StaticContent
   * in one of the HDF files we loaded above. */
  cs_file = hdf_get_value (cgi->hdf, "CGI.StaticContent", cs_file);
  err = cgi_display (cgi, cs_file);
  if (err != STATUS_OK)
  {
    cgi_neo_error(cgi, err);
    nerr_warn_error(err);
    return -1;
  }
  return 0;
}