Example #1
0
int file_get_info_by_id(mdb_conn *conn, int id, char *url, int pid, file_t **file)
{
    char mmckey[LEN_MMC_KEY];
    file_t *fl;
    size_t datalen;
    char *buf;
    int ret;
    
    if (id < 0 && (url == NULL || pid < 0))    return RET_RBTOP_INPUTE;

    if (id <= 0)
        snprintf(mmckey, LEN_MMC_KEY, "%s.%d.%s", PRE_MMC_FILE, pid, url);
    else
        snprintf(mmckey, LEN_MMC_KEY, "%s.%d", PRE_MMC_FILE, id);
    buf = mmc_get(mmckey, &datalen, 0);
    if (buf == NULL || datalen < sizeof(file_t)) {
        if (buf != NULL && datalen < sizeof(file_t)) {
            mtc_warn("get %d %d.%s info error from mmc %d",
                     id, pid, url, datalen);
        }
        if (mdb_get_errcode(conn) != MDB_ERR_NONE) {
            mtc_err("conn err %s", mdb_get_errmsg(conn));
            return RET_RBTOP_INPUTE;
        }
        fl = file_new();
        if (fl == NULL) return RET_RBTOP_MEMALLOCE;
        if (id <= 0) {
            LDB_QUERY_RAW(conn, "fileinfo", FILE_QUERY_COL,
                          "pid=%d AND name=$1", "s", pid, url);
        } else {
            LDB_QUERY_RAW(conn, "fileinfo", FILE_QUERY_COL,
                          "id=%d", NULL, id);
        }
        ret = FILE_GET_RAW(conn, fl);
        if (ret != MDB_ERR_NONE) {
            mtc_err("get %d %d.%s info failure from db %s",
                    id, pid, url, mdb_get_errmsg(conn));
            if (ret == MDB_ERR_NORESULT)
                return RET_RBTOP_NEXIST;
            return RET_RBTOP_SELECTE;
        } else {
            file_pack(fl, &buf, &datalen);
            mmc_store(MMC_OP_SET, mmckey, (void*)buf, datalen, ONE_HOUR, 0);
        }
    } else {
        ret = file_unpack(buf, datalen, &fl, NULL);
        if (ret != RET_RBTOP_OK) {
            mtc_err("assembly file from mmc error");
            return RET_RBTOP_MMCERR;
        }
    }
    free(buf);
    *file = fl;
    return RET_RBTOP_OK;
}
Example #2
0
File: otjt.c Project: kingiol/cmoon
int tjt_add_atom(HDF *hdf, mdb_conn *conn, session_t *ses)
{
    PRE_DBOP(hdf, conn);

    int aid, fid, uid;
    char *img, *exp, tbl[LEN_TB];
    int ret;

    uid = ses->member->uin;
    aid = ses->file->aid;
    fid = ses->file->id;
    img = hdf_get_value(hdf, PRE_QUERY".img", "");
    exp = hdf_get_value(hdf, PRE_QUERY".exp", "");

    snprintf(tbl, sizeof(tbl), "tjt_%d", aid);

    ret = MDATA_SET(conn, EVT_PLUGIN_TJT, NULL, FLAGS_NONE,
                    "INSERT INTO %s (fid, uid, img, exp) "
                    " VALUES (%d, %d, $1, $2)", "ss",
                    tbl, fid, uid, img, exp);
    if (ret != MDB_ERR_NONE) {
        mtc_err("add file err %s", mdb_get_errmsg(conn));
        return RET_RBTOP_INSERTE;
    }
    tjt_refresh_info(aid, fid);

    return RET_RBTOP_OK;
}
Example #3
0
char* user_login_auth(mdb_conn *conn, char *uid, char *pass)
{
	if (!conn || !uid || !pass) return NULL;

	char *p, *r;
	int ret, x;
	
	mdb_exec(conn, NULL, "SELECT password from account WHERE userid=$1;", "s", uid);
	if (mdb_get(conn, "s", &p) == MDB_ERR_NONE) {
		if (!strcmp(p, pass)) {
			
			r = calloc(1, LEN_SKEY+1);
			for (x = 0; x < LEN_SKEY; x++) {
				r[x] = (char)(65 + neo_rand(90-65));
			}
			r[x] = '\0';

			ret = mdb_exec(conn, NULL, "UPDATE account SET skey='$1' WHERE "
						   " userid=$2;", "ss", r, uid);
			if (ret != MDB_ERR_NONE) {
				mtc_err("exec failure %s", mdb_get_errmsg(conn));
				free(r);
				return NULL;
			}
			return r;
		}
	}

	return NULL;
}
Example #4
0
int file_get_info_by_uri(mdb_conn *conn, char *uri, file_t **file)
{
    file_t *fl;
    size_t datalen;
    char *buf;
    int ret;
    
    if (uri == NULL) return RET_RBTOP_INPUTE;

    buf = mmc_getf(&datalen, 0, PRE_MMC_FILE".%s", uri);
    if (buf == NULL || datalen < sizeof(file_t)) {
        if (buf != NULL && datalen < sizeof(file_t)) {
            mtc_warn("get %s info error from mmc %d", uri, datalen);
        }
        if (mdb_get_errcode(conn) != MDB_ERR_NONE) {
            mtc_err("conn err %s", mdb_get_errmsg(conn));
            return RET_RBTOP_INPUTE;
        }
        fl = file_new();
        if (fl == NULL) return RET_RBTOP_MEMALLOCE;
        LDB_QUERY_RAW(conn, "fileinfo", FILE_QUERY_COL, "uri=$1", "s", uri);
        ret = FILE_GET_RAW(conn, fl);
        if (ret != MDB_ERR_NONE) {
            mtc_err("get %s info failure from db %s", uri, mdb_get_errmsg(conn));
            if (ret == MDB_ERR_NORESULT)
                return RET_RBTOP_NEXIST;
            return RET_RBTOP_SELECTE;
        } else {
            file_pack(fl, &buf, &datalen);
            mmc_storef(MMC_OP_SET, (void*)buf, datalen, ONE_HOUR, 0,
                       PRE_MMC_FILE".%s", uri);
        }
    } else {
        ret = file_unpack(buf, datalen, &fl, NULL);
        if (ret != RET_RBTOP_OK) {
            mtc_err("assembly file from mmc error");
            return RET_RBTOP_MMCERR;
        }
    }
    free(buf);
    *file = fl;
    return RET_RBTOP_OK;
}
Example #5
0
int main(int argc, char **argv, char **envp)
{
	CGI *cgi = NULL;
	NEOERR *err;
	mdb_conn *conn = NULL;

	char *s, *u, *p, *r, *jcbk;

	mtc_init("test");
	mconfig_parse_file(SITE_CONFIG, &g_cfg);

	if (mdb_init(&conn, DB_DSN) != MDB_ERR_NONE) {
		mtc_err("init db error %s", mdb_get_errmsg(conn));
		printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
		printf("{errcode: %d}", SAM_ERR_INIT);
		return 1;
	}
	
	err = cgi_init(&cgi, NULL);
	if (err != STATUS_OK) {
		mtc_err("init cgi error");
		printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
		printf("初始化错误");
		return 1;
	}
	err = cgi_parse(cgi);
	if (err != STATUS_OK) {
		mtc_err("parse cgi error");
		hdf_set_value(cgi->hdf, PRE_OUTPUT".errmsg", "初始化出错");
		goto finish;
	}

	u = hdf_get_value(cgi->hdf, PRE_COOKIE".samuser", NULL);
	s = hdf_get_value(cgi->hdf, PRE_COOKIE".samkey", NULL);
	if (s && u) {
		if (user_has_login(conn, u, s)) {
			hdf_set_copy(cgi->hdf, PRE_OUTPUT".samuser", PRE_COOKIE".samuser");
			hdf_set_copy(cgi->hdf, PRE_OUTPUT".samkey", PRE_COOKIE".samkey");
			hdf_set_value(cgi->hdf, PRE_OUTPUT".rcode", "1");
			goto finish;
		}
	}

	hdf_set_value(cgi->hdf, PRE_OUTPUT".rcode", "2");

finish:
	cgi_display(cgi, F_TPL_TEST);
	cgi_destroy(&cgi);
	mdb_destroy(conn);
	return 0;
}
Example #6
0
int main(int argc, char **argv, char **envp)
{
	CGI *cgi = NULL;
	NEOERR *err;
	mdb_conn *conn = NULL;

	/* skey, user, pass, return jsoncallback*/
	char *s, *u, *p, *r, *jcbk;
	/* keeptime(hours)  */
	int t;
	char tm[LEN_TM_GMT];
	
	//sleep(20);
	mtc_init("login");
	mconfig_parse_file(SITE_CONFIG, &g_cfg);
	mutil_wrap_fcgi(argc, argv, envp);
	if (mdb_init(&conn, DB_DSN) != MDB_ERR_NONE) {
		mtc_err("init db error %s", mdb_get_errmsg(conn));
		printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
		printf("{errcode: %d}", SAM_ERR_INIT);
		return 1;
	}
	
#ifndef DROP_FCGI
	while (FCGI_Accept() >= 0) {
#endif

		/*
		 * cgi init 
		 */
		err = cgi_init(&cgi, NULL);
		if (err != STATUS_OK) {
			mtc_err("init cgi error");
			printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
			printf("{errcode: %d}", SAM_ERR_INIT);
			goto opfinish;
		}
		err = cgi_parse(cgi);
		if (err != STATUS_OK) {
			mtc_err("parse cgi error");
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_PARSE);
			goto opfinish;
		}
#if 0
		if (mutil_client_attack_cookie(cgi->hdf, "login", 30, 60)) {
			mtc_err("client attack");
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_NEEDREST);
			goto opfinish;
		}
#endif

		u = hdf_get_value(cgi->hdf, PRE_COOKIE".samuser", NULL);
		s = hdf_get_value(cgi->hdf, PRE_COOKIE".samkey", NULL);
		if (s && u) {
			if (user_has_login(conn, u, s)) {
				hdf_set_copy(cgi->hdf, PRE_OUTPUT".samuser", PRE_COOKIE".samuser");
				hdf_set_copy(cgi->hdf, PRE_OUTPUT".samkey", PRE_COOKIE".samkey");
				goto done;
			}
		}
		
		u = hdf_get_value(cgi->hdf, PRE_QUERY".u", NULL);
		p = hdf_get_value(cgi->hdf, PRE_QUERY".p", NULL);
		if (!u || !p) {
			mtc_err("parameter miss %s %s", u, p);
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_NEEDINPUT);
			goto opfinish;
		}
		s = user_login_auth(conn, u, p);
		if (!s) {
			mtc_err("login error %s %s", u, p);
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_PASSW);
			goto opfinish;
		}

		cgiwrap_write(P3P_HEADER, strlen(P3P_HEADER));
		cgi_cookie_set(cgi, "samuser", u, NULL, SITE_DOMAIN, NULL, 1, 0);
		cgi_cookie_set(cgi, "samkey", s, NULL, SITE_DOMAIN, NULL, 1, 0);
#if 0
		t = hdf_get_int_value(cgi->hdf, PRE_QUERY".t", 0);
		mmisc_getdatetime_gmt(tm, sizeof(tm), "%A, %d-%b-%Y %T GMT", 60*60*t);
		cgi_cookie_set(cgi, "samkey", s, NULL, SITE_DOMAIN, tm, 1, 0);
#endif
		
		hdf_set_value(cgi->hdf, PRE_OUTPUT".samuser", u);
		hdf_set_value(cgi->hdf, PRE_OUTPUT".samkey", s);
		free(s);

	done:
		/*
		 * TODO set samkey, samuser to app's domain
		 * DONE this is done by jsonp
		 */
		hdf_set_value(cgi->hdf, PRE_OUTPUT".success", "1");
	opfinish:
		if (cgi) {
			r = hdf_get_value(cgi->hdf, PRE_QUERY".r", NULL);
			if (r) {
				cgi_redirect(cgi, r);
			} else {
				jcbk = hdf_get_value(cgi->hdf, PRE_QUERY".jsoncallback", NULL);
				if (jcbk != NULL) {
					mjson_execute_hdf(cgi->hdf, jcbk, 0);
				} else {
					mjson_output_hdf(cgi->hdf, 0);
				}
			}
#ifdef DEBUG_HDF
			hdf_write_file(cgi->hdf, HF_LOG_PATH"hdf.login");
#endif
			cgi_destroy(&cgi);
		}
		
#ifndef DROP_FCGI
 	} /* FCGI_Accept() */
#endif

	mdb_destroy(conn);
	return 0;
}
Example #7
0
int main(int argc, char **argv, char **envp)
{
	CGI *cgi = NULL;
	NEOERR *err;
	mdb_conn *conn = NULL;

	/* skey, user, pass, return jsoncallback*/
	char *s, *u, *r, *jcbk;
	
	//sleep(20);
	mtc_init("logout");
	mconfig_parse_file(SITE_CONFIG, &g_cfg);
	mutil_wrap_fcgi(argc, argv, envp);
	if (mdb_init(&conn, DB_DSN) != MDB_ERR_NONE) {
		mtc_err("init db error %s", mdb_get_errmsg(conn));
		printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
		printf("{errcode: %d}", SAM_ERR_INIT);
		return 1;
	}
	
#ifndef DROP_FCGI
	while (FCGI_Accept() >= 0) {
#endif

		/*
		 * cgi init 
		 */
		err = cgi_init(&cgi, NULL);
		if (err != STATUS_OK) {
			mtc_err("init cgi error");
			printf("Content-Type: text/html; charset=UTF-8\r\n\r\n");
			printf("{errcode: %d}", SAM_ERR_INIT);
			goto opfinish;
		}
		err = cgi_parse(cgi);
		if (err != STATUS_OK) {
			mtc_err("parse cgi error");
			hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_PARSE);
			goto opfinish;
		}

		u = hdf_get_value(cgi->hdf, PRE_COOKIE".samuser", NULL);
		s = hdf_get_value(cgi->hdf, PRE_COOKIE".samkey", NULL);
		if (s && u) {
			if (user_has_login(conn, u, s)) {
				user_logout(conn, u);
				cgi_cookie_clear(cgi, "samuser", SITE_DOMAIN, NULL);
				cgi_cookie_clear(cgi, "samkey", SITE_DOMAIN, NULL);
				hdf_set_value(cgi->hdf, PRE_OUTPUT".success", "1");
				goto opfinish;
			}
		}
		hdf_set_int_value(cgi->hdf, PRE_OUTPUT".errcode", SAM_ERR_NOTLOGIN);
	opfinish:
		if (cgi) {
			r = hdf_get_value(cgi->hdf, PRE_QUERY".r", NULL);
			if (r) {
				cgi_redirect(cgi, r);
			} else {
				jcbk = hdf_get_value(cgi->hdf, PRE_QUERY".jsoncallback", NULL);
				if (jcbk != NULL) {
					mjson_execute_hdf(cgi->hdf, jcbk, 0);
				} else {
					mjson_output_hdf(cgi->hdf, 0);
				}
			}
#ifdef DEBUG_HDF
			hdf_write_file(cgi->hdf, HF_LOG_PATH"hdf.logout");
#endif
			cgi_destroy(&cgi);
		}
		
#ifndef DROP_FCGI
 	} /* FCGI_Accept() */
#endif

	mdb_destroy(conn);
	return 0;
}