コード例 #1
0
ファイル: dbpool_oracle.c プロジェクト: dimimpou/gwlib
static void* oracle_open_conn(const DBConf *db_conf)
{
    OracleConf *cfg = db_conf->oracle;
    sword errorcode = 0;
    text version[512];
    struct ora_conn *conn = gw_malloc(sizeof(struct ora_conn));

    gw_assert(conn != NULL);
    memset(conn, 0, sizeof(struct ora_conn));

    debug("dbpool.oracle",0,"oracle_open_conn called");

    /* init OCI environment */
    errorcode = OCIEnvCreate(&conn->envp,
                             OCI_THREADED|OCI_ENV_NO_MUTEX,
                             NULL,
                             oracle_malloc,
                             oracle_realloc,
                             oracle_free,
                             0,0);
    if (errorcode != OCI_SUCCESS) {
         oracle_checkerr(NULL, errorcode);
         error(0, "Got error while OCIEnvCreate %d", errorcode);
         gw_free(conn);
         return NULL;
    }

    debug("dbpool.oracle",0,"oci environment created");

    /* allocate error handle */
    errorcode = OCIHandleAlloc(conn->envp, (dvoid**) &conn->errhp, 
                               OCI_HTYPE_ERROR, 0, 0);
    if (errorcode != OCI_SUCCESS) {
        oracle_checkerr(NULL, errorcode);
        OCIHandleFree(conn->envp, OCI_HTYPE_ENV);
        gw_free(conn);
        return NULL;
    }

    debug("dbpool.oracle",0,"oci error handle allocated");

    /* open oracle user session */
    errorcode = OCILogon(conn->envp, conn->errhp, &conn->svchp,
                         (unsigned char*)octstr_get_cstr(cfg->username), octstr_len(cfg->username),
                         (unsigned char*)octstr_get_cstr(cfg->password), octstr_len(cfg->password),
                         (unsigned char*)octstr_get_cstr(cfg->tnsname), octstr_len(cfg->tnsname));

    if (errorcode != OCI_SUCCESS) {
        oracle_checkerr(conn->errhp, errorcode);
        OCIHandleFree(conn->errhp, OCI_HTYPE_ERROR);
        OCIHandleFree(conn->envp, OCI_HTYPE_ENV);
        gw_free(conn);
        return NULL;
    }

    debug("dbpool.oracle",0,"connected to database");

    errorcode = OCIServerVersion(conn->svchp, conn->errhp, version, 
                                 sizeof(version), OCI_HTYPE_SVCCTX);
    if (errorcode != OCI_SUCCESS) {
        oracle_checkerr(conn->errhp, errorcode);
    } else {
        info(0, "Connected to: %s", version);
    }

    return conn;
}
コード例 #2
0
ファイル: oracle.c プロジェクト: strizhechenko/collectd
static int o_read_database (o_database_t *db) /* {{{ */
{
  size_t i;
  int status;

  if (db->oci_service_context != NULL)
  {
    OCIServer *server_handle;
    ub4 connection_status;

    server_handle = NULL;
    status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
        (void *) &server_handle, /* size pointer = */ NULL,
        OCI_ATTR_SERVER, oci_error);
    if (status != OCI_SUCCESS)
    {
      o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
          oci_error);
      return (-1);
    }

    if (server_handle == NULL)
    {
      connection_status = OCI_SERVER_NOT_CONNECTED;
    }
    else /* if (server_handle != NULL) */
    {
      connection_status = 0;
      status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
          (void *) &connection_status, /* size pointer = */ NULL,
          OCI_ATTR_SERVER_STATUS, oci_error);
      if (status != OCI_SUCCESS)
      {
        o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
            oci_error);
        return (-1);
      }
    }

    if (connection_status != OCI_SERVER_NORMAL)
    {
      INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
          db->name);
      OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
      db->oci_service_context = NULL;
    }
  } /* if (db->oci_service_context != NULL) */

  if (db->oci_service_context == NULL)
  {
    status = OCILogon (oci_env, oci_error,
        &db->oci_service_context,
        (OraText *) db->username, (ub4) strlen (db->username),
        (OraText *) db->password, (ub4) strlen (db->password),
        (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
    if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
    {
      char errfunc[256];

      ssnprintf (errfunc, sizeof (errfunc), "OCILogon(\"%s\")", db->connect_id);

      o_report_error ("o_read_database", db->name, NULL, errfunc, oci_error);
      DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
          db->connect_id, db->oci_service_context);
      db->oci_service_context = NULL;
      return (-1);
    }
    else if (status == OCI_SUCCESS_WITH_INFO)
    {
      /* TODO: Print NOTIFY message. */
    }
    assert (db->oci_service_context != NULL);
  }

  DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
      db->connect_id, db->oci_service_context);

  for (i = 0; i < db->queries_num; i++)
    o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);

  return (0);
} /* }}} int o_read_database */
コード例 #3
0
ファイル: oraclesim.c プロジェクト: ARPA-SIMC/libsim
OracleDbConnection *FC_FUNC_(oraclesim_init, ORACLESIM_INIT)
     (char *username, char *password, char *dbname, int *err) {
  sword status;
  OracleDbConnection *dbconnid;

  if ((dbconnid = (OracleDbConnection *) malloc(sizeof(OracleDbConnection)))
      == NULL) {
    *err = 1;
    return dbconnid;
  }

  dbconnid->envhp = NULL;
  dbconnid->errhp = NULL;
  dbconnid->svchp = NULL;
  dbconnid->stmthp = NULL;
  dbconnid->defn1p = NULL;
  dbconnid->defn2p = NULL; 
  dbconnid->defn3p = NULL;
  dbconnid->defn4p = NULL;
  dbconnid->defn5p = NULL;
  dbconnid->defn6p = NULL;
  dbconnid->defn7p = NULL;
  dbconnid->defn8p = NULL;
  dbconnid->bnd1p = NULL;
  dbconnid->bnd2p = NULL;
  dbconnid->bnd3p = NULL;
  dbconnid->bnd4p = NULL;
  dbconnid->table[0] = 0;
  dbconnid->errmsg[0] = 0;

  if ((status = OCIEnvCreate((OCIEnv **) &dbconnid->envhp, (ub4) OCI_DEFAULT,
			     (dvoid *) 0, (dvoid * (*)(dvoid *,size_t)) 0,
			     (dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
			     (void (*)(dvoid *, dvoid *)) 0, (size_t) 0,
			     (dvoid **) 0)) != OCI_SUCCESS) {
    snprintf(dbconnid->errmsg, MSGLEN, 
	     "Oracle error - OCIEnvCreate failed with status = %d", status);
    *err = 2;
  }
  /* Alloco l'error handle */
  else if (checkerr(NULL, OCIHandleAlloc(dbconnid->envhp,
					 (dvoid **) &dbconnid->errhp,
					 OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0))
	   != OCI_SUCCESS) *err = 3;
  /* Mi connetto */
  else if (checkerr(dbconnid, OCILogon(dbconnid->envhp, dbconnid->errhp,
				       &dbconnid->svchp,
				       (text *) username, strlen(username),
				       (text *) password, strlen(password),
				       (text *) dbname, strlen(dbname)))
	   != OCI_SUCCESS) *err = 4;
  /* Alloco lo statement handle */
  else if (checkerr(dbconnid, OCIHandleAlloc(dbconnid->envhp,
					    (dvoid **) &dbconnid->stmthp,
					     OCI_HTYPE_STMT, (size_t) 0,
					     (dvoid **) 0))
	   != OCI_SUCCESS) *err = 5;
  else
    *err = 0;
  return dbconnid;

}
コード例 #4
0
ファイル: main.c プロジェクト: mlange/dev
int main(int argc, char *argv[])
{
    long ii,
	 rows,
	 status;

    sb2 indicator;

    char data[80];

    char *dbuser,
	 *dbpass,
	 *dbconn;

    OCIEnv    *envhp  = NULL; 
    OCIStmt   *stmthp = NULL;
    OCIDefine *defnhp = NULL;
    OCIParam  *parmhp = NULL;
    OCIError  *errhp  = NULL; 
    OCISvcCtx *svchp  = NULL; 

    /* Set the database user, password and connection. */
    dbuser = DBUSER ? DBUSER : "";
    dbpass = DBPASS ? DBPASS : "";
    dbconn = DBCONN ? DBCONN : "";

    /* Initialize the OCI process environment. */
    status = OCIInitialize(OCI_DEFAULT, 0, 0, 0, 0);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIInitialize: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Allocate and initialize the environment handle pointer. */
    status = OCIEnvInit(&envhp, OCI_DEFAULT, 0, 0);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIEnvInit: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Allocate the error handle pointer. */
    status = OCIHandleAlloc(envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR, 0, 0);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIHandleAlloc: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Connect to the database. */
    status = OCILogon(envhp, errhp, &svchp,
                      (text *) dbuser, strlen(dbuser),
                      (text *) dbpass, strlen(dbpass),
                      (text *) dbconn, strlen(dbconn));
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCILogon: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Allocate a statement handle for ourselves. */
    status = OCIHandleAlloc(envhp, (dvoid **) &stmthp, OCI_HTYPE_STMT, 0, 0);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIHandleAlloc: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Prepare the SQL statement. */
    status = OCIStmtPrepare(stmthp, errhp, (text *) SQLSTMT, strlen(SQLSTMT), 
			    OCI_NTV_SYNTAX, OCI_DEFAULT);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIStmtPrepare: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    for (ii=0; ii<2; ii++)
    {

    /* Execute the SQL statement. */
    status = OCIStmtExecute(svchp, stmthp, errhp, 0, 0, NULL, NULL,OCI_DEFAULT);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIStmtExecute: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Bind the the output variable for this column.              */
    status = OCIDefineByPos(stmthp, &defnhp, errhp, 1, data, sizeof(data), 
			    SQLT_STR, &indicator, NULL, NULL, OCI_DEFAULT);
    if (status != OCI_SUCCESS)
    {
        fprintf(stderr, "OCIDefineByPos: %ld\n", status);
        exit(EXIT_FAILURE);
    }

    /* Fetch each row. */
    for (rows=0; ; rows++)
    {
	status = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
	if (status == OCI_NO_DATA)
	{
	    break;
	}
        else if (status != OCI_SUCCESS)
        {
            fprintf(stderr, "OCIStmtFetch: %ld\n", status);
            exit(EXIT_FAILURE);
        }

	printf("INDICATOR [%d]  VALUE [%s]\n", indicator, data);
    }

    printf("Command affected %ld row(s)\n\n", rows);

    }

    exit(EXIT_SUCCESS);
}
コード例 #5
0
ファイル: testOCI.cpp プロジェクト: gogosee/bandicoot
int main(int argc,char** argv)  
{  
	int nRet;  
	OCIEnv*      phOCIEnv;    //OCI环境句柄  
	OCIError*    phOCIErr;    //OCI错误句柄  
	OCISvcCtx*   phOCIConn;   //OCI数据库服务连接句柄  
 
	char acDBName[20],acUserName[20],acPwd[20];  
 
	//初始化OCI应用环境  
	nRet = OCIInitialize((ub4)OCI_DEFAULT,(dvoid*)0,(dvoid* (*)(dvoid*,size_t))0,
			(dvoid* (*)(dvoid*,dvoid*,size_t))0,(void (*)(dvoid*,dvoid*))0);  
	if(nRet)  
	{  
		printf("OCIInitialize() Failed: %d\n",(int)OCI_ERROR);  
		return (int)OCI_ERROR;  
	}  
 
	//初始化OCI环境句柄(由系统来负责分配OCI环境句柄)  
	nRet = OCIEnvInit(&phOCIEnv,(ub4)OCI_DEFAULT,(size_t)0,(dvoid**)0);  
	if(nRet)  
	{  
		printf("OCIEnvInit() Failed: %d\n",(int)OCI_ERROR);  
		return (int)OCI_ERROR;  
	}  
 
	//在OCI应用环境中依据OCI环境句柄来分配OCI的错误报告句柄  
	nRet = OCIHandleAlloc((dvoid*)phOCIEnv,(dvoid**)&phOCIErr,
		    (ub4)OCI_HTYPE_ERROR,(size_t)0,(dvoid**)0);  
	if(nRet)  
	{  
		printf("OCIHandleAlloc() Failed: %d\n",(int)OCI_ERROR);  
		return (int)OCI_ERROR;  
	}  
 
	//建立与Oracle的连接  
	memset(acDBName,0,sizeof(acDBName));  
	memset(acUserName,0,sizeof(acUserName));  
	memset(acPwd,0,sizeof(acPwd));  
 
	strcpy(acDBName,"simplegw");  
	strcpy(acUserName,"simplegw");  
	strcpy(acPwd,"simplegw");  
 
	//连接数据库  
	nRet = OCILogon(phOCIEnv,phOCIErr,&phOCIConn,(text*)acUserName,
		    strlen(acUserName),(text*)acPwd,strlen(acPwd),
	    (OraText*)acDBName,strlen(acDBName));  
	if(nRet)  
	{  
		printf("Connection Failed: %d\n\n",nRet);  
		exit(1);  
	}  
	printf("Connection OK: %d\n",nRet);  
	printf("执行一些SQL语句和数据库操作命令 ......\n\n");  







 
	//断开与数据库的连接  
	nRet = OCILogoff(phOCIConn,phOCIErr);  
	if(nRet)  
	{  
		printf("OCILogoff() Failed.\n\n");  
	}
	else 
	{  
		printf("Connecttion closed.\n\n");  
	}  
	//连接已经断开,把连接句柄置为空  
	phOCIConn = NULL;  
 
	//销毁OCI应用环境与相关的句柄  
	if(phOCIErr)  
	{  
		OCIHandleFree((dvoid*)phOCIErr,OCI_HTYPE_ERROR);  
		printf("OCI错误报告句柄已经释放.\n\n");  
	}  
 
	if(phOCIEnv)  
	{  
		OCIHandleFree((dvoid*)phOCIEnv,(ub4)OCI_HTYPE_ENV);  
		printf("OCI环境句柄已经释放.\n\n");  
	}  
	printf("OCI环境释放结束.\n\n");  
	return 0;  
} 
コード例 #6
0
ファイル: xoidkey.c プロジェクト: RicardoVivas/oracle-scripts
static void get_db_charsets(conn_info_t *params_p, ub2 *char_csid, 
                            ub2 *nchar_csid)
{
  OCIDefine  *defnp1 = (OCIDefine *) NULL;
  OCIDefine  *defnp2 = (OCIDefine *) NULL;
  oratext     parm[PARM_BUFLEN];
  oratext     value[OCI_NLS_MAXBUFSZ];
  ub2         parm_len = 0;
  ub2         value_len = 0;
  oci_t       ocistruct; 
  oci_t      *ocip = &ocistruct;
   
  *char_csid = 0;
  *nchar_csid = 0;
  memset (ocip, 0, sizeof(ocistruct));

  if (OCIEnvCreate(&ocip->envp, OCI_OBJECT, (dvoid *)0,
                   (dvoid * (*)(dvoid *, size_t)) 0,
                   (dvoid * (*)(dvoid *, dvoid *, size_t))0,
                   (void (*)(dvoid *, dvoid *)) 0,
                   (size_t) 0, (dvoid **) 0))
  {
    ocierror(ocip, (char *)"OCIEnvCreate() failed", TRUE);
  }

  if (OCIHandleAlloc((dvoid *) ocip->envp, (dvoid **) &ocip->errp,
                     (ub4) OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0))
  {
    ocierror(ocip, (char *)"OCIHandleAlloc(OCI_HTYPE_ERROR) failed", TRUE);
  }

  OCICALL(ocip, 
          OCILogon(ocip->envp, ocip->errp, &ocip->svcp,
                   params_p->user, params_p->userlen,
                   params_p->passw, params_p->passwlen,
                   params_p->dbname, params_p->dbnamelen));

  OCICALL(ocip, 
          OCIHandleAlloc((dvoid *) ocip->envp, (dvoid **) &ocip->stmtp,
                         (ub4) OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0));

  /* Execute stmt to select the db nls char and nchar character set */ 
  OCICALL(ocip, 
          OCIStmtPrepare(ocip->stmtp, ocip->errp,
                         (CONST text *)GET_DB_CHARSETS,
                         (ub4)strlen((char *)GET_DB_CHARSETS),
                         (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));

  OCICALL(ocip,
          OCIDefineByPos(ocip->stmtp, &defnp1,
                         ocip->errp, (ub4) 1, parm,
                         PARM_BUFLEN, SQLT_CHR, (void*) 0,
                         &parm_len, (ub2 *)0, OCI_DEFAULT));

  OCICALL(ocip,
          OCIDefineByPos(ocip->stmtp, &defnp2,
                         ocip->errp, (ub4) 2, value,
                         OCI_NLS_MAXBUFSZ, SQLT_CHR, (void*) 0,
                         &value_len, (ub2 *)0, OCI_DEFAULT));

  OCICALL(ocip, 
          OCIStmtExecute(ocip->svcp, ocip->stmtp, 
                         ocip->errp, (ub4)0, (ub4)0, 
                         (const OCISnapshot *)0,
                         (OCISnapshot *)0, (ub4)OCI_DEFAULT));

  while (OCIStmtFetch(ocip->stmtp, ocip->errp, 1,
                      OCI_FETCH_NEXT, OCI_DEFAULT) == OCI_SUCCESS)
  {
    value[value_len] = '\0';
    if (parm_len == strlen("NLS_CHARACTERSET") &&
        !memcmp(parm, "NLS_CHARACTERSET", parm_len))
    {
      *char_csid = OCINlsCharSetNameToId(ocip->envp, value);
      printf("Outbound database NLS_CHARACTERSET = %.*s (csid = %d) \n",
             value_len, value, *char_csid);
    }
    else if (parm_len == strlen("NLS_NCHAR_CHARACTERSET") &&
             !memcmp(parm, "NLS_NCHAR_CHARACTERSET", parm_len))
    {
      *nchar_csid = OCINlsCharSetNameToId(ocip->envp, value);
      printf("Outbound database NLS_NCHAR_CHARACTERSET = %.*s (csid = %d) \n",
             value_len, value, *nchar_csid);
    }
  }

  disconnect_db(ocip);
}
コード例 #7
0
int start_oracle(int s, char *ip, int port, unsigned char options, char *miscptr, FILE * fp) {
  char *empty = "";
  char *login, *pass, buffer[200], sid[100];

  if (strlen(login = hydra_get_next_login()) == 0)
    login = empty;
  if (strlen(pass = hydra_get_next_password()) == 0)
    pass = empty;

  strncpy(sid, miscptr, sizeof(sid));
  snprintf(buffer, sizeof(buffer), "//%s:%d/%s", hydra_address2string(ip), port, sid);

  /*

     To use the Easy Connect naming method, PHP must be linked with Oracle 10g or greater Client libraries.
     The Easy Connect string for Oracle 10g is of the form: [//]host_name[:port][/service_name].
     With Oracle 11g, the syntax is: [//]host_name[:port][/service_name][:server_type][/instance_name].
     Service names can be found by running the Oracle utility lsnrctl status on the database server machine.

     The tnsnames.ora file can be in the Oracle Net search path, which includes $ORACLE_HOME/network/admin
     and /etc. Alternatively set TNS_ADMIN so that $TNS_ADMIN/tnsnames.ora is read. Make sure the web
     daemon has read access to the file. 

   */

  if (OCIInitialize(OCI_DEFAULT, NULL, NULL, NULL, NULL)) {
    print_oracle_error("OCIInitialize");
    return 4;
  }
  if (OCIEnvInit(&o_environment, OCI_DEFAULT, 0, NULL)) {
    print_oracle_error("OCIEnvInit");
    return 4;
  }
  if (OCIEnvInit(&o_environment, OCI_DEFAULT, 0, NULL)) {
    print_oracle_error("OCIEnvInit 2");
    return 4;
  }
  if (OCIHandleAlloc(o_environment, (dvoid **) & o_error, OCI_HTYPE_ERROR, (size_t) 0, NULL)) {
    print_oracle_error("OCIHandleAlloc");
    return 4;
  }

  if (OCILogon(o_environment, o_error, &o_servicecontext, (const OraText *) login, strlen(login), (const OraText *) pass, strlen(pass), (const OraText *) buffer, strlen(buffer))) {
    OCIErrorGet(o_error, 1, NULL, &o_errorcode, o_errormsg, sizeof(o_errormsg), OCI_HTYPE_ERROR);
    //database: oracle_error: ORA-01017: invalid username/password; logon denied
    //database: oracle_error: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
    //database: oracle_error: ORA-28000: the account is locked
    //Failed login attempts is set to 10 by default
    if (verbose) {
      hydra_report(stderr, "[VERBOSE] database: oracle_error: %s\n", o_errormsg);
    }
    if (strstr((const char *) o_errormsg, "ORA-12514") != NULL) {
      hydra_report(stderr, "[ERROR] ORACLE SID is not valid, you should try to enumerate them.\n");
    }
    if (strstr((const char *) o_errormsg, "ORA-28000") != NULL) {
      hydra_report(stderr, "[ERROR] ORACLE account %s is locked.\n", login);
    }

    if (o_error) {
      OCIHandleFree((dvoid *) o_error, OCI_HTYPE_ERROR);
    }

    hydra_completed_pair();
    //by default, set in sqlnet.ora, the trace file is generated in pwd to log any errors happening,
    //as we don't care, we are deleting the file
    //set these parameters to not generate the file
    //LOG_DIRECTORY_CLIENT = /dev/null
    //LOG_FILE_CLIENT = /dev/null
    unlink("sqlnet.log");

    return 2;
  } else {
    OCILogoff(o_servicecontext, o_error);
    if (o_error) {
      OCIHandleFree((dvoid *) o_error, OCI_HTYPE_ERROR);
    }
    hydra_report_found_host(port, ip, "oracle", fp);
    hydra_completed_pair_found();
  }
  if (memcmp(hydra_get_next_pair(), &HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0)
    return 3;
  return 1;
}