Esempio n. 1
0
int
main(int argc, char *argv[])
{
#define 	MAXBUF	4095

	char 	buf[ MAXBUF+1 ];
	int	iarg, ibuf, ichr;
	bool	debug = FALSE;
        CL_ERR_DESC     err_code;
        char		*p1 = NULL;
        char		pid[MAXBUF];
        char    	*database = ERx("");
        char    	*user = ERx("");
        char    	*xmlfile = ERx("");
        char    	sql_fname[LO_NM_LEN + 1];
        char    	*work_dir = NULL;
        char    	directory[MAX_LOC + 1]; 
        char    	*tmp_dir = NULL;
        char		tmp_buf[MAX_LOC + 1];
        char		subdir_buf[MAX_LOC + 1];
        char		sql_loc_buf[MAX_LOC + 1];
	char		*progname; 
        LOCATION    tmp_dir_loc;
        LOCATION    tmp_subdir_loc;
        LOCATION    tmp_buff_loc;
        LOCATION    curr_loc;
        LOCATION    sql_file_loc;
	char        *password = ERx("");
	char        *groupid = ERx("");
	ARGRET  rarg;
        i4      pos;
	LOCATION    xmlfile_loc;
	FILE	    *xmlfile_read;
	STATUS	    stat = FAIL;
	char	    dbuf[256];
	char	    encode[32];
        u_i4        tmppid;
        TM_STAMP    tm_stamp;


	/* Tell EX this is an ingres tool. */
   	(void) EXsetclient(EX_INGRES_TOOL);

	/* Call IIUGinit to initialize character set attribute table */
	if ( IIUGinit() != OK)
	    PCexit(FAIL);

	progname = ERget(F_XM0006_IMPXML);
	FEcopyright(progname, ERx("2001")); 

	/*
	** Get arguments from command line
	*/

	/* required parameters */

	if (FEutaopen(argc, argv, ERx("xmlimport")) != OK)
          PCexit(FAIL);

	/* database name is required */
	if (FEutaget(ERx("database"), 0, FARG_PROMPT, &rarg, &pos) != OK)
          PCexit(FAIL);
	database = rarg.dat.name;

        if (FEutaget(ERx("xmlfile"), 0, FARG_PROMPT, &rarg, &pos) != OK)
            PCexit(FAIL);
        xmlfile = rarg.dat.name;

        if (FEutaget(ERx("user"), 0, FARG_FAIL, &rarg, &pos) == OK)
            user = rarg.dat.name;

        if (FEutaget(ERx("password"), 0, FARG_FAIL, &rarg, &pos) == OK)
        {
            char *IIUIpassword();

            if ((password = IIUIpassword(ERx("-P"))) == NULL)
            {
                FEutaerr(BADARG, 1, ERx(""));
                PCexit(FAIL);
            }
        }

        if (FEutaget(ERx("groupid"), 0, FARG_FAIL, &rarg, &pos) == OK)
          groupid = rarg.dat.name;

        if (FEutaget(ERx("debug"), 0, FARG_FAIL, &rarg, &pos) == OK)
	  debug = TRUE;

        ibuf = STlength(buf); 

        /* b121678: pid is no longer based on process id, but it's
        ** a random number instead.
        */
        PCpid(&tmppid);
        TMget_stamp(&tm_stamp);
        MHsrand2(tmppid * tm_stamp.tms_usec);
        STprintf(pid, "%x", MHrand2());

#ifdef xDEBUG
        SIprintf(" the pid is: %s \n", pid);
#endif

        /* create the sql file */
	/* Avoid a name like "foo.xml.sql" on VMS, use pid.sql instead */
        STcopy(pid, sql_fname); 
        STcat(sql_fname, ".sql");

        /* 
        ** create in the temp location a directory 
        ** with the name pid. set this directory 
        ** as the working directory for impxml
        */

      NMloc (TEMP, PATH, NULL, &tmp_dir_loc);	
      /* make a location for TMP loc */
      /* print location name */
      LOcopy (&tmp_dir_loc, tmp_buf, &tmp_buff_loc);
      LOtos (&tmp_buff_loc, &tmp_dir);

#ifdef xDEBUG
      SIprintf ("temploc: %s \n", tmp_dir);
#endif

      /* make a subdir location with filename, pid */
      STcopy (pid, subdir_buf);				
      /* Initialize result loc so that everyone is happy */
      LOcopy (&tmp_dir_loc, sql_loc_buf, &sql_file_loc); 
      /* Generate location for temp subdirectory */
      if (LOfaddpath (&tmp_dir_loc, subdir_buf, &sql_file_loc) != OK)
      {
	 IIUGerr(E_XM0007_Locname_Failed, UG_ERR_FATAL, 2, tmp_dir, subdir_buf);
	 /* NOTREACHED */
      }

      /* print the location name */
      LOcopy (&sql_file_loc, tmp_buf, &tmp_buff_loc);    
      LOtos (&tmp_buff_loc, &work_dir);

#ifdef xDEBUG
	SIprintf ("work dir loc: %s \n", work_dir);
#endif

      /* create the subdir */
      if (LOcreate (&sql_file_loc) != OK) {
	 IIUGerr(E_XM0008_Create_Temp_Dir, UG_ERR_ERROR, 1, work_dir);
	 PCexit(FAIL);
      }

      STcopy(work_dir, directory);

#ifdef xDEBUG
      SIprintf ("sql file name: %s \n", sql_fname);
      SIprintf ("xml file name: %s \n", xmlfile);
#endif

      /* Execute the command impxml */	
      STprintf (buf, ERx( "impxml -d=\"%s\" -o=\"%s\" " ),  directory, sql_fname);

      /* encoding? */
      if ( (LOfroms(PATH & FILENAME, xmlfile, &xmlfile_loc) != OK)
          ||
	  (SIopen(&xmlfile_loc, "r", &xmlfile_read) != OK)
	  ||
	  (xmlfile_read == NULL)
	 )
      {
	  IIUGerr(E_XM0009_Cannot_Open_File, UG_ERR_ERROR, 1, xmlfile);
          PCexit(FAIL);
      }

      /* scan XML declaration for encoding, if any */
      if (stat = SIgetrec(dbuf, sizeof(dbuf) - 1, xmlfile_read) == OK)
      {
	  char 	*d = dbuf;
	  i4	i = 0;

	  for (d = dbuf; d != (dbuf + sizeof(dbuf)); d++)
	  {
	      if (MEcmp(d, ERx("encoding="), sizeof(ERx("encoding=")) - 1) == 0)
	      {
		  d += sizeof(ERx("encoding="));
		  while (MEcmp (d, "\'", sizeof(char)) && MEcmp(d, "\"", sizeof(char)))
		      MEcopy(d++, sizeof(char), &encode[i++]);
		  encode[i++] = MIN_CHAR;
		  encode[i] = EOS;
		  STcat(buf, ERx("-x="));
		  STcat(buf, encode);
		  break;
	      }
	  }
      }
      else if (stat != ENDFILE)
      {
          /* unable to read file, report error */
	  IIUGerr(E_XM000A_Cannot_Read_File, UG_ERR_ERROR, 1, xmlfile);
	  PCexit(FAIL);
      }

      stat = SIclose(xmlfile_read);

      STcat(buf, xmlfile);

#ifdef xDEBUG
      SIprintf ( " query send: %s \n", buf);
#endif

      /* 	Execute the command.  
      */

      if( PCcmdline((LOCATION *) NULL, buf, PC_WAIT, 
  		(LOCATION *)NULL, &err_code) != OK )
      {
	if (!debug)
	    LOdelete(&sql_file_loc);
	PCexit(FAIL);
      }

      /*
      **	we should run the sql script 
      **	sql dbname < new_filename
      */
      
      /* save the current location */
      LOcopy(&sql_file_loc, tmp_buf, &curr_loc);

      /* make a full location path to the location first */
      LOfroms(FILENAME, sql_fname, &tmp_buff_loc);
      LOstfile(&tmp_buff_loc, &curr_loc);
      LOcopy (&curr_loc, tmp_buf, &tmp_buff_loc);
      LOtos (&tmp_buff_loc, &tmp_dir);

#ifdef xDEBUG
      SIprintf ("sql file is: %s \n", tmp_dir);
#endif

      /* No space between < and input file for VMS */
      STprintf(buf, ERx( "sql -s %s %s %s %s <%s" ),
			database, user, password, groupid, tmp_dir);
#ifdef xDEBUG
      SIprintf (" query send: %s \n", buf);
#endif

      /*
      **	Execute the command.
      */

      if( PCcmdline((LOCATION *) NULL, buf, PC_WAIT, 
              (LOCATION *)NULL, &err_code) != OK )
      {
	if (!debug)
	    LOdelete(&sql_file_loc);
        PCexit(FAIL); 
      }

      /*
      **    Delete the location
      */
      if (!debug)
	LOdelete(&sql_file_loc);

      PCexit(OK);
}
Esempio n. 2
0
VOID
do_crack_cmd(i4 argc, char **argv)
{
	i4	i;
	i4	argc_remain;
	i4	obj_params;
	bool	rn_params;
	bool	bogus_param;
	bool	all_param;
	OOID	del_obj_type;
	STATUS	c;
	char	*with_ptr;
	char	*user_flag;
	char	*group_flag;
	char	*pwd_flag;
	char	*tmp_ptr;
	FE_DEL	*fd;
	FE_DEL	*prev_fd;
	char	pr_buf[256];		/* Holds any current prompt */


	/*
	** Default to no wildcard expansion and no specified object type.
	** Indicate no names seen yet on command line, and no bogus
	** parameters.  Indicate no object type specifications (there
	** should never be more than one).
	** Track remaining command line parameters.
	*/
	Dobj_wildcard = FALSE;
	del_obj_type = OC_UNDEFINED;
	obj_params = 0;
	rn_params = FALSE;
	bogus_param = FALSE;
	all_param = FALSE;
	argc_remain = argc - 1;	/* Bypass argv[0] - the program name!	*/
	Dobj_database = NULL;   /* assume no database name b66456       */

	i = 1;			/* First pull out any flags	*/
	while (i < argc)
	{
		if  (*(argv[i]) == '-')	/* Flag found. Set and clear out */
		{
			switch(*(argv[i] + 1))
			{
			case('r'):
			case('R'):
				CVlower((argv[i] + 1));
				if  (STcompare((argv[i] + 1),DO_RPT_FLAG) == 0)
				{
					del_obj_type = OC_REPORT;
					Dobj_pr_str = ERget(F_DE0008_Rep);
					obj_params++;
				}
				else
				{
					bogus_param = TRUE;
				}
				break;
			case('f'):
			case('F'):
				CVlower((argv[i] + 1));
				if  (STcompare((argv[i] + 1),DO_FORM_FLAG) == 0)
				{
					del_obj_type = OC_FORM;
					Dobj_pr_str = ERget(F_DE0004_Form);
					obj_params++;
				}
				else
				{
					bogus_param = TRUE;
				}
				break;
			case('j'):
			case('J'):
				CVlower((argv[i] + 1));
				if  (STcompare((argv[i] + 1),DO_JD_FLAG) == 0)
				{
					del_obj_type = OC_JOINDEF;
					Dobj_pr_str = ERget(F_DE0006_Jdef);
					obj_params++;
				}
				else
				{
					bogus_param = TRUE;
				}
				break;
			case('g'):
			case('G'):
				tmp_ptr = STalloc(argv[i]);
				CVlower((tmp_ptr + 1));
				if  (STcompare((tmp_ptr + 1),DO_GRAF_FLAG) == 0)
				{
					del_obj_type = OC_GRAPH;
					Dobj_pr_str = ERget(F_DE0005_Graph);
					obj_params++;
				}
				else if (*(argv[i] + 1) == 'G')
				{
					Dobj_gidflag = argv[i];	
				}
				else
				{
					bogus_param = TRUE;
				}
				MEfree((PTR)tmp_ptr);
				break;
			case('a'):
			case('A'):
				CVlower((argv[i] + 1));
				if  (STcompare((argv[i] + 1),DO_APPL_FLAG) == 0)
				{
					del_obj_type = OC_APPL;
					Dobj_pr_str = ERget(F_DE0003_App);
					obj_params++;
				}
				else if  (STcompare((argv[i] + 1),
						    DO_ALL_FLAG) == 0)
				{
					del_obj_type = OC_OBJECT;
					/*
					** No prompt string for -all
					*/
					Dobj_pr_str = ERget(F_DE0009_Dobj_O);
					all_param = TRUE;
				}
				else
				{
					bogus_param = TRUE;
				}
				break;
			case('q'):
			case('Q'):
				CVlower((argv[i] + 1));
				if  (STcompare((argv[i] + 1),DO_QBF_FLAG) == 0)
				{
					del_obj_type = OC_QBFNAME;
					Dobj_pr_str = ERget(F_DE0007_Qbfnm);
					obj_params++;
				}
				else
				{
					bogus_param = TRUE;
				}
				break;
			case('i'):
			case('I'):
				CVlower((argv[i] + 1));
				if  (STcompare((argv[i] + 1),DO_INCL_FLAG) == 0)
				{
					/*
					** Clear out the flag so we don't see
					** it when we look for DB and FE object
					** names.
					*/
					argv[i] = NULL;
					i++;
					if  (i >= argc)
					{
						IIUGerr(E_DE000B_Dobj_no_file,
							UG_ERR_ERROR,0);
						FEexits(ERx(""));
						PCexit(FAIL);
						break;
					}
					Dobj_dfile = argv[i];
				}
				else
				{
					bogus_param = TRUE;
				}
				break;
			case('s'):
			case('S'):
				CVlower((argv[i] + 1));
				if  (STcompare((argv[i] + 1),DO_QT_FLAG) == 0)
				{
					Dobj_silent = TRUE;
				}
				else
				{
					bogus_param = TRUE;
				}
				break;
			case('u'):
			case('U'):
				Dobj_uflag = argv[i];
				break;
			case('w'):
			case('W'):
				CVlower((argv[i] + 1));
				if  (STcompare((argv[i] + 1),DO_WILD_FLAG) == 0)
				{
					Dobj_wildcard = TRUE;
				}
				else
				{
					bogus_param = TRUE;
				}
				break;
			case('P'):	/* -P (password) flag set	*/
				Dobj_passflag = argv[i];	
				break;
			default:
				bogus_param = TRUE;
				break;
			}
			if  (bogus_param)
			{
				IIUGerr(E_DE000A_Dobj_bad_flag,
					UG_ERR_ERROR,2,argv[i],
					ERget(E_DE0014_Dobj_syntax));
				FEexits(ERx(""));
				PCexit(FAIL);
			}
			/*
			** Clear out the flag so we don't see it when
			** we look for DB and FE object names.
			*/
			argv[i] = NULL;
			argc_remain--;
		}
		i++;
	}


# ifdef DGC_AOS
	with_ptr = STalloc(ERx("dgc_mode='reading'");
	IIUIswc_SetWithClause(with_ptr);
# else
	with_ptr = ERx("");
# endif

	/*
	** If -all was specified, then we can't have -wildcard, -include,
	** object type specifications, or objects names as well.  argc_remain
	** should at most reflect a database name.
	*/
	if  ((all_param) &&
	     ((Dobj_wildcard) || (Dobj_dfile != NULL) ||
	      (obj_params > 0) || (argc_remain > 1)))
	{
		IIUGerr(E_DE0012_Dobj_badall,UG_ERR_ERROR,1,
			ERget(E_DE0014_Dobj_syntax));
		FEexits(ERx(""));
		PCexit(FAIL);
	}

	/*
	** If multiple object types were specified, then abort!
	*/
	if  (obj_params > 1)
	{
		IIUGerr(E_DE0015_Dobj_multype,UG_ERR_ERROR,1,
			ERget(E_DE0014_Dobj_syntax));
		FEexits(ERx(""));
		PCexit(FAIL);
	}
	/*
	** If no object type was specified, then abort!
	*/
	if  (del_obj_type == OC_UNDEFINED)
	{
		IIUGerr(E_DE0013_Dobj_notype,UG_ERR_ERROR,1,
			ERget(E_DE0014_Dobj_syntax));
		FEexits(ERx(""));
		PCexit(FAIL);
	}

	/*
	** Check to see that the database name was specified.
	** If so, it will be the first non-flag parameter.
	** If not, ask for one:
	*/
	i = 1;
	while (i < argc)
	{
		if  (argv[i] == NULL)
		{
		/*
		** If we reached a used option,
		**   then the database name is missing.  b66456
		*/
			break;
			/* i++; */
			/* continue; */
		}

		/*
		** If we reached an option then, no dbname  b66456
		*/
		if (*argv[i] == '-')
		  break;

		Dobj_database = argv[i++];
		break;
	}

	if  (Dobj_database == NULL)
	{
		c = FEprompt(ERget(FE_Database),TRUE,(sizeof(pr_buf) - 1),
			     &pr_buf[0]);
		Dobj_database = STalloc(&pr_buf[0]);
	}

	/*
	** If the '-P' flag has been set, prompt for the password
	*/
	if  (Dobj_passflag != NULL)
	{
		/*
		** If there is something wrong with the '-P' flag, such as the
		** user has specified the password on the command line,
		** IIUIpassword() will return NULL.  In such cases bail out.
		*/
		if  ((Dobj_passflag = IIUIpassword(Dobj_passflag)) == NULL)
		{
			IIUGerr(E_DE000A_Dobj_bad_flag,UG_ERR_ERROR,2,
				ERx("-P"),ERget(E_DE0014_Dobj_syntax));
			FEexits(ERx(""));
			PCexit(FAIL);
		}
	}

	/*
	** Open the database, will abort on FAIL.  We do it here because we
	** need to establish the invoking user name before we process the FE
	** object names.  We use "user" since owners of FE objects are always
	** stored in the FE catalogs as lower case.  This will have to change
	** to suser when we fully support authorization identifiers as
	** delimited identifiers, which can be other than lower case
	** (specifically FIPS which is UI_MIXED_CASE).
	*/
	user_flag = ERx("");
	group_flag = ERx("");
	pwd_flag = ERx("");
	if  (Dobj_uflag != NULL)
	{
		user_flag = Dobj_uflag;
	}
	if  (Dobj_gidflag != NULL)
	{
		group_flag = Dobj_gidflag;
	}
	if  (Dobj_passflag != NULL)
	{
		pwd_flag = Dobj_passflag;
	}
	if  (FEingres(Dobj_database,user_flag,group_flag,pwd_flag,NULL) != OK)
	{
		IIUGerr(E_DE0004_NoOpen,UG_ERR_ERROR,0);
		FEexits(ERx(""));
		PCexit(FAIL);
	}

	/*
	** If -all was specified, force a wildcard expansion of "%".
	** do_expand_name() will recognize del_obj_type == OC_OBJECT
	** as being special.
	*/
	if  (del_obj_type == OC_OBJECT)
	{
		Dobj_wildcard = TRUE;
		fd = (FE_DEL *)MEreqmem(0,sizeof(FE_DEL),TRUE,NULL);
		Ptr_fd_top = fd;
		prev_fd = (FE_DEL *)NULL;
		Cact_fd = fd;
		Cact_fd->fd_name_info = (FE_RSLV_NAME *)MEreqmem(0,
						sizeof(FE_RSLV_NAME),TRUE,NULL);
		Cact_fd->fd_name_info->name = STalloc(ERx("%"));
		Cact_fd->fd_below = (FE_DEL *)NULL;
		Cact_fd = do_expand_name(Cact_fd,&prev_fd,del_obj_type);
		return;
	}

	/*
	** The rest of the parameters will be FE object names.
	*/
	while (i < argc)
	{
		if  (argv[i] == NULL)
		{
			i++;
			continue;
		}
		rn_params = TRUE;
		fd = (FE_DEL *)MEreqmem(0,sizeof(FE_DEL),TRUE,NULL);
		if  (Ptr_fd_top == (FE_DEL *)NULL)
		{
			Ptr_fd_top = fd;
			prev_fd = (FE_DEL *)NULL;
		}
		else
		{
			Cact_fd->fd_below = fd;
			prev_fd = Cact_fd;
		}
		Cact_fd = fd;
		Cact_fd->fd_name_info = (FE_RSLV_NAME *)MEreqmem(0,
						sizeof(FE_RSLV_NAME),TRUE,NULL);
		Cact_fd->fd_name_info->name = STalloc(argv[i]);
		Cact_fd->fd_below = (FE_DEL *)NULL;
		Cact_fd = do_expand_name(Cact_fd,&prev_fd,del_obj_type);
		/*
		** Returns ptr to current end of FE_DEL chain, which may be
		** NULL if (still) 1st and no match.  If no match, the FE_DEL
		** and its name will have been de-allocated, and prev_fd and
		** Ptr_fd_top will have been be altered as required.
		*/
		i++;
	}

	if  (Dobj_dfile != NULL)
	{
		do_ifile_parse(Dobj_dfile,del_obj_type);
	}	

	/*
	** If we found anything, then we're all done
	*/
	if  (Ptr_fd_top != (FE_DEL *)NULL)
	{
		return;
	}

	/*
	** If we had FE object names either on the command line or in a
	** file but they all failed expansion/resolution, then don't prompt
	** for any names!
	*/
	if  ((rn_params) || (Dobj_dfile != NULL))
	{
		IIUGerr(E_DE000C_Dobj_no_input,UG_ERR_ERROR,1,
			Dobj_pr_str);
		FEexits(ERx(""));
		PCexit(FAIL);
	}

	/*
	** Prompt for no FE object name - either on
	** the command line or in a -list file.  Note that
	** we ignore any Dobj_silent in this instance.
	*/
	while (TRUE)
	{
		c = FEprompt(Dobj_pr_str,FALSE,(sizeof(pr_buf) - 1),&pr_buf[0]);
		if  ((c != OK) || (STtrmwhite(&pr_buf[0]) == 0))
		{
			/*
			** When NULL string entered (or error), all done
			*/
			break;
		}
		fd = (FE_DEL *)MEreqmem(0,sizeof(FE_DEL),TRUE,NULL);
		if  (Ptr_fd_top == (FE_DEL *)NULL)
		{
			Ptr_fd_top = fd;
			prev_fd = (FE_DEL *)NULL;
		}
		else
		{
			Cact_fd->fd_below = fd;
			prev_fd = Cact_fd;
		}
		Cact_fd = fd;
		Cact_fd->fd_name_info = (FE_RSLV_NAME *)MEreqmem(0,
						sizeof(FE_RSLV_NAME),TRUE,NULL);
		Cact_fd->fd_name_info->name = STalloc(&pr_buf[0]);
		Cact_fd->fd_below = (FE_DEL *)NULL;
		Cact_fd = do_expand_name(Cact_fd,&prev_fd,del_obj_type);
	}

	if  (Ptr_fd_top == (FE_DEL *)NULL)
	{
		IIUGerr(E_DE000C_Dobj_no_input,UG_ERR_ERROR,1,Dobj_pr_str);
		FEexits(ERx(""));
		PCexit(FAIL);
	}

	return;
}