Exemplo n.º 1
0
LOGINREC *
get_login(int argc, char *argv[], OPTIONS *options)
{
	LOGINREC *login;
	int ch;
	char *username = NULL, *password = NULL;

	extern char *optarg;

	assert(options && argv);
	
	options->appname = tds_basename(argv[0]);
	options->colsep = default_colsep; /* may be overridden by -t */
	
	login = dblogin();
	
	if (!login) {
		fprintf(stderr, "%s: unable to allocate login structure\n", options->appname);
		exit(1);
	}
	
	DBSETLAPP(login, options->appname);
	
	options->servername = getenv("DSQUERY");
	
	while ((ch = getopt(argc, argv, "U:P:R:S:dD:i:o:e:t:H:hqv")) != -1) {
		switch (ch) {
		case 'U':
			username = strdup(optarg);
			break;
		case 'R': 
			  parse_pivot_description(options, optarg);
			  break;
		case 'P':
			password = getpassarg(optarg);
			break;
		case 'S':
			options->servername = strdup(optarg);
			break;
		case 'd':
		case 'D':
			options->database = strdup(optarg);
			break;
		case 'i':
			options->input_filename = strdup(optarg);
			break;
		case 'o':
			options->output_filename = strdup(optarg);
			break;
		case 'e':
			options->error_filename = strdup(optarg);
			break;
		case 't':
			unescape(optarg);
			options->colsep = strdup(optarg);
			break;
		case 'h':
			options->headers = stdout;
			break;
		case 'H':
			strcpy(options->hostname, optarg);
			break;
		case 'q':
			options->fquiet = 1;
			break;
		case 'v':
			options->fverbose = 1;
			break;
		case '?':
		default:
			usage(options->appname);
			exit(1);
		}
	}

	if (username) 
		DBSETLUSER(login, username);
	

	if( !options->hostname[0] ) {
		if (-1 == gethostname(options->hostname, sizeof(options->hostname))) {
			perror("unable to get hostname");
		}
	}

	if( options->hostname[0] ) {
		DBSETLHOST(login, options->hostname);
	}

	/* Look for a password if a username was provided, else assume domain login */
	if (password) {
		DBSETLPWD(login, password);
		memset(password, 0, strlen(password));
	} else if (username) {
		char password[128];

		readpassphrase("Password: ", password, sizeof(password), RPP_ECHO_OFF);
		DBSETLPWD(login, password);
        }

	if (!options->servername) {
		usage(options->appname);
		exit(1);
	}
	
	return login;
}
Exemplo n.º 2
0
static LOGINREC *
get_login(int argc, char *argv[], OPTIONS *options)
{
	LOGINREC *login;
	int ch;

	extern char *optarg;

	assert(options && argv);
	
	options->appname = tds_basename(argv[0]);
	options->colsep = default_colsep; /* may be overridden by -t */
	options->odbc_version = SQL_OV_ODBC3;	  /* may be overridden by -V */
	
	login = calloc(1, sizeof(LOGINREC));
	
	if (!login) {
		fprintf(stderr, "%s: unable to allocate login structure\n", options->appname);
		exit(1);
	}
	
	if (-1 == gethostname(options->hostname, sizeof(options->hostname))) {
		perror("unable to get hostname");
	} 

	while ((ch = getopt(argc, argv, "U:P:S:dD:i:o:e:t:V:hqv")) != -1) {
		char *p;
		switch (ch) {
		case 'U':
			login->username = strdup(optarg);
			break;
		case 'P':
			login->password = strdup(optarg);
			memset(optarg, 0, strlen(optarg));
			break;
		case 'S':
			options->servername = strdup(optarg);
			break;
		case 'd':
		case 'D':
			options->database = strdup(optarg);
			break;
		case 'i':
			options->input_filename = strdup(optarg);
			break;
		case 'o':
			options->output_filename = strdup(optarg);
			break;
		case 'e':
			options->error_filename = strdup(optarg);
			break;
		case 't':
			unescape(optarg);
			options->colsep = strdup(optarg);
			break;
		case 'h':
			options->headers = stdout;
			break;
		case 'q':
			options->fquiet = 1;
			break;
		case 'V':
			switch(strtol(optarg, &p, 10)) {
			case 3: 
				options->odbc_version = SQL_OV_ODBC3
				;
				break;
			case 2:
				options->odbc_version = SQL_OV_ODBC2;
				break;
			default:
				fprintf(stderr, "warning: -V must be 2 or 3, not %s.  Using default of 3\n", optarg);
				break;
			}
		case 'v':
			options->fverbose = 1;
			break;
		case '?':
		default:
			usage(options->appname);
			exit(1);
		}
	}
	
	if (!options->servername) {
		usage(options->appname);
		exit(1);
	}
	
	return login;
}
Exemplo n.º 3
0
int
main(int argc, char **argv)
{
	LOGINREC *login;
	DBINT db_i;
	RETCODE ret;
	
	read_login_info(argc, argv);

	printf("Start\n");
	dbinit();

	dberrhandle(syb_err_handler);
	dbmsghandle(syb_msg_handler);

	login = dblogin();
	DBSETLPWD(login, PASSWORD);
	DBSETLUSER(login, USER);
	DBSETLAPP(login, "setnull");


	printf("About to open %s.%s\n", SERVER, DATABASE);

	dbproc = dbopen(login, SERVER);
	if (strlen(DATABASE))
		dbuse(dbproc, DATABASE);
	dbloginfree(login);

	/* try to set an int */
	db_i = 0xdeadbeef;
	ret = dbsetnull(dbproc, INTBIND, 0, (BYTE *) &db_i);
	if (ret != SUCCEED) {
		fprintf(stderr, "dbsetnull returned error %d\n", (int) ret);
		failed = 1;
	}

	ret = dbsetnull(dbproc, INTBIND, 1, (BYTE *) &db_i);
	if (ret != SUCCEED) {
		fprintf(stderr, "dbsetnull returned error %d\n", (int) ret);
		failed = 1;
	}

	/* check result */
	db_i = 0;
	dbcmd(dbproc, "select convert(int, null)");

	dbsqlexec(dbproc);

	if (dbresults(dbproc) != SUCCEED) {
		fprintf(stderr, "Was expecting a row.\n");
		failed = 1;
		dbcancel(dbproc);
	}

	dbbind(dbproc, 1, INTBIND, 0, (BYTE *) &db_i);
	printf("db_i = %ld\n", (long int) db_i);

	if (dbnextrow(dbproc) != REG_ROW) {
		fprintf(stderr, "Was expecting a row.\n");
		failed = 1;
		dbcancel(dbproc);
	}
	printf("db_i = %ld\n", (long int) db_i);

	if (dbnextrow(dbproc) != NO_MORE_ROWS) {
		fprintf(stderr, "Only one row expected\n");
		dbcancel(dbproc);
		failed = 1;
	}

	while (dbresults(dbproc) == SUCCEED) {
		/* nop */
	}

	if (db_i != 0xdeadbeef) {
		fprintf(stderr, "Invalid NULL %ld returned (%s:%d)\n", (long int) db_i, tds_basename(__FILE__), __LINE__);
		failed = 1;
	}

	/* try if dbset null consider length */
	for (db_i = 1; db_i > 0; db_i <<= 1) {
		printf("db_i = %ld\n", (long int) db_i);
		ret = dbsetnull(dbproc, INTBIND, db_i, (BYTE *) &db_i);
		if (ret != SUCCEED) {
			fprintf(stderr, "dbsetnull returned error %d for bindlen %ld\n", (int) ret, (long int) db_i);
			failed = 1;
			break;
		}
	}

	char_test(NULL, -1, "123456");
	char_test(NULL, 0,  "123456");
	char_test(NULL, 2,  "  3456");

	char_test("foo", -1,  "foo456");
	char_test("foo", 0,   "foo456");
/*	char_test("foo", 2,   ""); */
	char_test("foo", 5,   "foo  6");

	char_test("foo ", -1,  "foo 56");
	char_test("foo ", 0,   "foo 56");
/*	char_test("foo ", 2,   ""); */
	char_test("foo ", 5,   "foo  6");

	char_test("foo ", 10,  "foo       _____");

	printf("dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
	dbexit();

	return failed ? 1 : 0;
}
Exemplo n.º 4
0
static void
char_test(const char *null, int bindlen, const char *expected)
{
	char db_c[16];
	RETCODE ret;

	if (null) {
		fprintf(stderr, "\tdbsetnull(CHARBIND, %u, '%s').\n", (unsigned int) strlen(null), null);
		ret = dbsetnull(dbproc, CHARBIND, strlen(null), (BYTE *) null);
		if (ret != SUCCEED) {
			fprintf(stderr, "dbsetnull returned error %d\n", (int) ret);
			failed = 1;
		}
	}

	memset(db_c, '_', sizeof(db_c));
	strcpy(db_c, "123456");
	dbcmd(dbproc, "select convert(char(20), null)");

	dbsqlexec(dbproc);

	if (dbresults(dbproc) != SUCCEED) {
		fprintf(stderr, "Was expecting a row.\n");
		failed = 1;
		dbcancel(dbproc);
	}

	fprintf(stderr, "dbbind(CHARBIND, bindlen= %d).\n", bindlen);
	dbbind(dbproc, 1, CHARBIND, bindlen, (BYTE *) &db_c);
	db_c[sizeof(db_c)-1] = 0;
	printf("buffer before/after dbnextrow: '%s'/", db_c);

	if (dbnextrow(dbproc) != REG_ROW) {
		fprintf(stderr, "Was expecting a row.\n");
		failed = 1;
		dbcancel(dbproc);
	}
	db_c[sizeof(db_c)-1] = 0;
	printf("'%s'\n", db_c);

	if (dbnextrow(dbproc) != NO_MORE_ROWS) {
		fprintf(stderr, "Only one row expected\n");
		dbcancel(dbproc);
		failed = 1;
	}

	while (dbresults(dbproc) == SUCCEED) {
		/* nop */
	}

	if (strcmp(db_c, expected) != 0) {
		fprintf(stderr, "Invalid NULL '%s' returned expected '%s' (%s:%d)\n", db_c, expected, tds_basename(__FILE__), __LINE__);
		failed = 1;
	}
}
Exemplo n.º 5
0
static LOGINREC *
get_login(int argc, char *argv[], OPTIONS *options)
{
	LOGINREC *login;
	int ch;

	extern char *optarg;
	extern int optind;

	assert(options && argv);
	
	options->appname = tds_basename(argv[0]);
	
	login = dblogin();
	
	if (!login) {
		fprintf(stderr, "%s: unable to allocate login structure\n", options->appname);
		exit(1);
	}
	
	DBSETLAPP(login, options->appname);
	
	if (-1 == gethostname(options->hostname, sizeof(options->hostname))) {
		perror("unable to get hostname");
	} else {
		DBSETLHOST(login, options->hostname);
	}

	while ((ch = getopt(argc, argv, "U:P:S:d:D:i:o:v")) != -1) {
		switch (ch) {
		case 'U':
			DBSETLUSER(login, optarg);
			break;
		case 'P':
			DBSETLPWD(login, optarg);
			break;
		case 'S':
			options->servername = strdup(optarg);
			break;
		case 'd':
		case 'D':
			options->database = strdup(optarg);
			break;
		case 'i':
			options->input_filename = strdup(optarg);
			break;
		case 'o':
			options->output_filename = strdup(optarg);
			break;
		case 'v':
			printf("%s\n\n%s", software_version, 
				"Copyright (C) 2004  James K. Lowden\n"
				"This program  is free software; you can redistribute it and/or\n"
				"modify it under the terms of the GNU General Public\n"
				"License as published by the Free Software Foundation\n");
				exit(1);
			break;
		case '?':
		default:
			usage(options->appname);
			exit(1);
		}
	}

	if (!options->servername) {
		usage(options->appname);
		exit(1);
	}

	options->optind = optind;
	
	return login;
}
Exemplo n.º 6
0
static CS_RETCODE
establish_login(int argc, char **argv)
{
	extern char *optarg;
	extern int optind;
	COMMON_PWD options = {0};
#if !defined(__MINGW32__) && !defined(_MSC_VER)
	int ch;
#endif

	BASENAME = tds_basename((char *)argv[0]);
	DIRNAME = dirname((char *)argv[0]);
	
#if !defined(__MINGW32__) && !defined(_MSC_VER)
	/* process command line options (handy for manual testing) */
	while ((ch = getopt(argc, argv, "U:P:S:D:f:m:v")) != -1) {
		switch (ch) {
		case 'U':
			strcpy(options.USER, optarg);
			break;
		case 'P':
			strcpy(options.PASSWORD, optarg);
			break;
		case 'S':
			strcpy(options.SERVER, optarg);
			break;
		case 'D':
			strcpy(options.DATABASE, optarg);
			break;
		case 'f': /* override default PWD file */
			PWD = strdup(optarg);
			break;
		case 'm':
			common_pwd.maxlength = strtol(optarg, NULL, 10);
		case 'v':
			common_pwd.fverbose = 1;
			break;
		case '?':
		default:
			fprintf(stderr, "usage:  %s [-v] [-f PWD]\n"
					"        [-U username] [-P password]\n"
					"        [-S servername] [-D database]\n"
					, BASENAME);
			exit(1);
		}
	}
#endif
	read_login_info();
	
	/* override PWD file with command-line options */
	
	if (*options.USER)
		strcpy(USER, options.USER);
	if (*options.PASSWORD)
		strcpy(PASSWORD, options.PASSWORD);
	if (*options.SERVER)
		strcpy(SERVER, options.SERVER);
	if (*options.DATABASE)
		strcpy(DATABASE, options.DATABASE);
	
	return (*USER && *SERVER && *DATABASE)? CS_SUCCEED : CS_FAIL;
}
Exemplo n.º 7
0
int
read_login_info(int argc, char **argv)
{
	int len;
	FILE *in = NULL;
#if !defined(__MINGW32__) && !defined(_MSC_VER)
	int ch;
#endif
	char line[512];
	char *s1, *s2;
	char filename[PATH_MAX];
	static const char *PWD = "../../../PWD";
	struct { char *username, *password, *servername, *database; char fverbose; } options;

#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_STACK)
#define MAX_STACK (8*1024*1024)

	struct rlimit rlim;

	if (!getrlimit(RLIMIT_STACK, &rlim) && (rlim.rlim_cur == RLIM_INFINITY || rlim.rlim_cur > MAX_STACK)) {
		rlim.rlim_cur = MAX_STACK;
		setrlimit(RLIMIT_STACK, &rlim);
	}
#endif

	setbuf(stdout, NULL);
	setbuf(stderr, NULL);

	free(ARGV0);
#ifdef __VMS
	{
		/* basename expects unix format */
		s1 = strrchr(argv[0], ';'); /* trim version; extension trimmed later */
		if (s1) *s1 = 0;
		const char *unixspec = decc$translate_vms(argv[0]);
		ARGV0 = strdup(unixspec);
	}
#else
	ARGV0 = strdup(argv[0]);
#endif
	
	BASENAME = tds_basename(ARGV0);
#if defined(_WIN32) || defined(__VMS)
	s1 = strrchr(BASENAME, '.');
	if (s1) *s1 = 0;
#endif
	DIRNAME = dirname(ARGV0);
	
	memset(&options, 0, sizeof(options));
	
#if !defined(__MINGW32__) && !defined(_MSC_VER)
	/* process command line options (handy for manual testing) */
	while ((ch = getopt(argc, (char**)argv, "U:P:S:D:f:v")) != -1) {
		switch (ch) {
		case 'U':
			options.username = strdup(optarg);
			break;
		case 'P':
			options.password = strdup(optarg);
			break;
		case 'S':
			options.servername = strdup(optarg);
			break;
		case 'D':
			options.database = strdup(optarg);
			break;
		case 'f': /* override default PWD file */
			PWD = strdup(optarg);
			break;
		case 'v':
			options.fverbose = 1; /* doesn't normally do anything */
			break;
		case '?':
		default:
			fprintf(stderr, "usage:  %s \n"
					"        [-U username] [-P password]\n"
					"        [-S servername] [-D database]\n"
					"        [-i input filename] [-o output filename] "
					"[-e error filename]\n"
					, BASENAME);
			exit(1);
		}
	}
#endif
	strcpy(filename, PWD);

	s1 = getenv("TDSPWDFILE");
	if (s1 && s1[0])
		in = fopen(s1, "r");
	if (!in)
		in = fopen(filename, "r");
	if (!in)
		in = fopen("PWD", "r");
	if (!in) {
		sprintf(filename, "%s/%s", (DIRNAME) ? DIRNAME : ".", PWD);

		in = fopen(filename, "r");
		if (!in) {
			fprintf(stderr, "Can not open %s file\n\n", filename);
			goto Override;
		}
	}

	while (fgets(line, 512, in)) {
		s1 = strtok(line, "=");
		s2 = strtok(NULL, "\n");
		if (!s1 || !s2)
			continue;
		if (!strcmp(s1, "UID")) {
			strcpy(USER, s2);
		} else if (!strcmp(s1, "SRV")) {
			strcpy(SERVER, s2);
		} else if (!strcmp(s1, "PWD")) {
			strcpy(PASSWORD, s2);
		} else if (!strcmp(s1, "DB")) {
			strcpy(DATABASE, s2);
		}
	}
	fclose(in);
	
	Override:
	/* apply command-line overrides */
	if (options.username) {
		strcpy(USER, options.username);
		free(options.username);
	}
	if (options.password) {
		strcpy(PASSWORD, options.password);
		free(options.password);
	}
	if (options.servername) {
		strcpy(SERVER, options.servername);
		free(options.servername);
	}
	if (options.database) {
		strcpy(DATABASE, options.database);
		free(options.database);
	}

	if (!*SERVER) {
		fprintf(stderr, "no servername provided, quitting.\n");
		exit(1);
	}
	
	printf("found %s.%s for %s in \"%s\"\n", SERVER, DATABASE, USER, filename);
	
#if 0
	dbrecftos(BASENAME);
#endif
	len = snprintf(sql_file, sizeof(sql_file), "%s/%s.sql", FREETDS_SRCDIR, BASENAME);
	assert(len >= 0 && len <= sizeof(sql_file));

	if (input_file)
		fclose(input_file);
	if ((input_file = fopen(sql_file, "r")) == NULL) {
		fflush(stdout);
		fprintf(stderr, "could not open SQL input file \"%s\"\n", sql_file);
	}

	if (!free_file_registered)
		atexit(free_file);
	free_file_registered = 1;
	
	printf("SQL text will be read from %s\n", sql_file);

	return 0;
}