Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: PSNFTP2

PSNFTP2.m

Go to the documentation of this file.
  1. PSNFTP2 ;HP/ART - PPS-N National Drug File Updates File Transfer ;09/25/2015
  1. ;;4.0;NATIONAL DRUG FILE;**513,573**; 30 Oct 98;Build 6
  1. ;Supported ICRs/IAs
  1. ;External reference to ^%ZISH supported by DBIA 2320
  1. ;External reference to ^%ZISUTL supported by DBIA 2119
  1. ;External reference to ^XLFSTR supported by DBIA 10104
  1. ;Reference to ^PS(59.7 supported by DBIA 2613
  1. ;
  1. VMSFTP(PSRC,PSADDR,PSNUSER,PSWRKDIR,PSLOCDIR,PSREMDIR,PSREMFIL,PSCOMFIL,PSLOGFIL,PSDATFIL,PSERRMSG) ; VMS FTP
  1. ; ALL PARARMETERS ARE REQUIRED EXCEPT PASSWORD
  1. ;Inputs: PSRC - return code, by reference
  1. ; PSADDR - remote server address
  1. ; PSNUSER - target system user name
  1. ; PSWRKDIR - local work directory name
  1. ; PSLOCDIR - local directory name
  1. ; PSREMDIR - remote directory name
  1. ; PSREMFIL - remote file name
  1. ; PSCOMFIL - ftp .com file name
  1. ; PSLOGFIL - ftp log file name
  1. ; PSDATFIL - sftp commands file name
  1. ;Output: PSRC - populated return code
  1. ;
  1. ;check parameters
  1. I $G(PSADDR)="" S PSRC="0^no target server address" Q
  1. I $G(PSNUSER)="" S PSRC="0^no user name" Q
  1. I $G(PSWRKDIR)="" S PSRC="0^no local work directory name" Q
  1. I $G(PSLOCDIR)="" S PSRC="0^no local directory name" Q
  1. I $G(PSREMDIR)="" S PSRC="0^no remote directory name" Q
  1. I $G(PSREMFIL)="" S PSRC="0^no remote file name" Q
  1. I $G(PSCOMFIL)="" S PSRC="0^no ftp .com file name" Q
  1. I $G(PSLOGFIL)="" S PSRC="0^no ftp log file name" Q
  1. I $G(PSDATFIL)="" S PSRC="0^no sftp commands file name" Q
  1. ;
  1. ;create .dat file with sftp commands
  1. D CREATDAT^PSNFTP(.PSRC,PSDATFIL,PSWRKDIR,PSREMDIR,PSREMFIL) Q:'+PSRC
  1. ;
  1. ;create .com file
  1. N POP,FTPPORT S FTPPORT=""
  1. D OPEN^%ZISH("FILE1",PSWRKDIR,PSCOMFIL,"W")
  1. I POP S PSRC="0^failed to open ftp .com file" Q
  1. D USE^%ZISUTL("FILE1")
  1. W "$ set verify=(PROCEDURE,IMAGE)",!
  1. W "$ set default ",PSLOCDIR,!
  1. W "$ sftp"_$S(FTPPORT:" -oPort="_FTPPORT,1:"")_" -oIdentityFile="""_$$XVMSDIR(PSWRKDIR)_"VSSHID."" -""B"" "_PSWRKDIR_PSDATFIL_" -oUser="_PSNUSER_" "_PSADDR,!
  1. W "$ exit",!
  1. D CLOSE^%ZISH("FILE1")
  1. ;
  1. D OPEN^%ZISH("VSSHID",PSWRKDIR,"VSSHID.","W")
  1. I POP S PSRC="-1^FTP Script file <"_PSWRKDIR_"VSSHID.> could not be created." Q
  1. D USE^%ZISUTL("VSSHID")
  1. W "IDKEY "_$$XVMSDIR(PSWRKDIR)_"VSSHKEY"
  1. D CLOSE^%ZISH("VSSHID")
  1. ;
  1. EXECUTE ;Execute .COM file, create logfile
  1. N PSZFRC
  1. S PSZFRC=$ZF(-1,"@"_PSWRKDIR_PSCOMFIL_"/OUTPUT="_PSWRKDIR_PSLOGFIL)
  1. ; Error check
  1. I PSZFRC=-1 S PSRC="0^VMS OS command execution failed" Q
  1. ;
  1. ; Read Logfile into working global
  1. K ^TMP("PSNFTPLOG",$J)
  1. N PSXLOG
  1. S PSXLOG=$$FTG^%ZISH(PSWRKDIR,PSLOGFIL,$NA(^TMP("PSNFTPLOG",$J,1)),3)
  1. ; Check for error during ftp
  1. N PSPNG,PSPNG1,PSSTOP
  1. S PSPNG="",PSPNG1=0,PSSTOP=0
  1. F S PSPNG=$O(^TMP("PSNFTPLOG",$J,PSPNG)) Q:PSPNG=""!PSSTOP D
  1. . S PSPNG1=$G(^TMP("PSNFTPLOG",$J,PSPNG))
  1. . I PSPNG1["%TCPIP-E-SSH_FC_ERR_NO_S, file doesn't exist" S PSRC="0^Remote file was not found",PSSTOP=1 Q
  1. . I PSPNG1["%TCPIP-F-SSH_FATAL" S PSRC="0^non-specific fatal error",PSSTOP=1 Q
  1. . I PSPNG1["530 Login incorrect" S PSRC="0^server login failure",PSSTOP=1 Q
  1. . I PSPNG1["550-Failed to open" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["550 file not found" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["no such file" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["No such file" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["error processing output file" S PSRC="-1^remote file already downloaded",PSSTOP=1 Q
  1. . I PSPNG1["insufficient privilege" S PSRC="-1^remote file already downloaded",PSSTOP=1 Q
  1. . I PSPNG1["%SET-F-SEARCHFAIL" S PSRC="0^local file not found - change file permission",PSSTOP=1 Q
  1. . I PSPNG1["%TCPIP-E-SSH_FC_ERR_DEST" S PSRC="0^destination is not directory or does not exist",PSSTOP=1 Q
  1. . I PSPNG1["%TCPIP-E-SSH_FC_ERROR" S PSRC="0^destination is not directory or does not exist",PSSTOP=1 Q
  1. I PSSTOP=0 S PSERRMSG(1)="1^File Transfer is complete"
  1. D DELFILES($G(PSWRKDIR),$G(PSLOGFIL),$G(PSCOMFIL),$G(PSDATFIL))
  1. Q
  1. ;
  1. LINUXFTP(PSRC,PSADDR,PSNUSER,PSWRKDIR,PSLOCDIR,PSREMDIR,PSREMFIL,PSSHFILE,PSLOGFIL,PSDATFIL) ; Linux FTP
  1. ; ALL PARARMETERS ARE REQUIRED EXCEPT PASSWORD
  1. ;Inputs: PSRC - return code, by reference
  1. ; PSADDR - remote server address
  1. ; PSNUSER - target system user name
  1. ; PSWRKDIR - local work directory name
  1. ; PSLOCDIR - local directory name
  1. ; PSREMDIR - remote directory name
  1. ; PSREMFIL - remote file name
  1. ; PSSHFILE - ftp .sh file name
  1. ; PSLOGFIL - ftp log file name
  1. ; PSDATFIL - sftp commands file name
  1. ;Output: PSRC - populated return code
  1. ;
  1. ;check parameters
  1. I $G(PSADDR)="" S PSRC="0^no target server address" Q
  1. I $G(PSNUSER)="" S PSRC="0^no user name" Q
  1. I $G(PSWRKDIR)="" S PSRC="0^no local work directory name" Q
  1. I $G(PSLOCDIR)="" S PSRC="0^no local directory name" Q
  1. I $G(PSREMDIR)="" S PSRC="0^no remote directory name" Q
  1. I $G(PSREMFIL)="" S PSRC="0^no remote file name" Q
  1. I $G(PSSHFILE)="" S PSRC="0^no ftp .sh file name" Q
  1. I $G(PSLOGFIL)="" S PSRC="0^no ftp log file name" Q
  1. I $G(PSDATFIL)="" S PSRC="0^no sftp commands file name" Q
  1. ;
  1. ;create .dat file with sftp commands
  1. D CREATDAT^PSNFTP(.PSRC,PSDATFIL,PSWRKDIR,PSREMDIR,PSREMFIL) Q:'+PSRC
  1. ;create .sh file
  1. N POP,DEBUG1 S DEBUG1=1
  1. D OPEN^%ZISH("FILE1",PSWRKDIR,PSSHFILE,"W")
  1. I POP S PSRC="0^failed to open ftp .sh file" Q
  1. D USE^%ZISUTL("FILE1")
  1. ; Linux .sh commands
  1. W "#!/bin/bash",!!
  1. W "cd ",PSWRKDIR,!
  1. W "sftp"_" -oIdentityFile="""""_PSWRKDIR_"uxsshkey"""" -b "_PSWRKDIR_PSDATFIL_" -oStrictHostKeyChecking=no -oUser="_PSNUSER_" "_PSADDR_" >> "_PSWRKDIR_PSLOGFIL
  1. W !,"exit",!
  1. D CLOSE^%ZISH("FILE1")
  1. ;
  1. ; Execute .sh file, create logfile
  1. N PSZFRC
  1. S PSZFRC=$ZF(-1,PSWRKDIR_PSSHFILE_" >"_PSWRKDIR_PSLOGFIL)
  1. ; Error check
  1. I PSZFRC=-1 S PSRC="0^Linux OS command execution failed" Q
  1. ;
  1. ; Read Logfile into working global
  1. K ^TMP("PSNFTPLOG",$J)
  1. N PSXLOG
  1. S PSXLOG=$$FTG^%ZISH(PSWRKDIR,PSLOGFIL,$NA(^TMP("PSNFTPLOG",$J,1)),3)
  1. ; Check for error during ftp
  1. N PSPNG,PSPNG1,PSSTOP,UXEXIT
  1. S PSPNG="",(PSPNG1,PSSTOP,UXEXIT)=0
  1. F S PSPNG=$O(^TMP("PSNFTPLOG",$J,PSPNG)) Q:PSPNG=""!PSSTOP D
  1. . S PSPNG1=$G(^TMP("PSNFTPLOG",$J,PSPNG))
  1. . I $$UP^XLFSTR(PSPNG1)["SFTP> EXIT" S UXEXIT=1 Q
  1. . I PSPNG1["425 Not logged in" S PSRC="0^Login incorrect",PSSTOP=1 Q
  1. . I PSPNG1["530 Login incorrect" S PSRC="0^Login incorrect",PSSTOP=1 Q
  1. . I PSPNG1["550-Failed to open" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["550 file not found" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["no such file" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["No such file" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. I UXEXIT=0 S PSRC="-1^Remote file was not found",PSSTOP=1 Q
  1. I PSSTOP=0,UXEXIT S PSERRMSG(1)="1^File Transfer is complete"
  1. D DELFILES($G(PSWRKDIR),$G(PSLOGFIL),$G(PSCOMFIL),$G(PSDATFIL))
  1. Q
  1. ;
  1. WINFTP(PSRC,PSADDR,PSUID,PSWRKDIR,PSLOCDIR,PSREMDIR,PSREMFIL,PSCMDFIL,PSLOGFIL) ; Windows FTP
  1. ; ALL PARARMETERS ARE REQUIRED EXCEPT PASSWORD
  1. ;Inputs: PSRC - return code, by reference
  1. ; PSADDR - remote server address
  1. ; PSUID - target system user name
  1. ; PSWRKDIR - local work directory name
  1. ; PSLOCDIR - local directory name
  1. ; PSREMDIR - remote directory name
  1. ; PSREMFIL - remote file name
  1. ; PSCMDFIL - ftp commands file name
  1. ; PSLOGFIL - ftp log file name
  1. ;Output: PSRC - populated return code
  1. ;
  1. ;check parameters
  1. I $G(PSADDR)="" S PSRC="0^no target server address" Q
  1. I $G(PSUID)="" S PSRC="0^no user name" Q
  1. I $G(PSWRKDIR)="" S PSRC="0^no local work directory name" Q
  1. I $G(PSLOCDIR)="" S PSRC="0^no local directory name" Q
  1. I $G(PSREMDIR)="" S PSRC="0^no remote directory name" Q
  1. I $G(PSREMFIL)="" S PSRC="0^no remote file name" Q
  1. I $G(PSCMDFIL)="" S PSRC="0^no ftp commands file name" Q
  1. I $G(PSLOGFIL)="" S PSRC="0^no ftp log file name" Q
  1. ;create ftp commands file
  1. N POP
  1. D OPEN^%ZISH("FILE1",PSWRKDIR,PSCMDFIL,"W")
  1. I POP S PSRC="0^failed to open ftp commands file" Q
  1. D USE^%ZISUTL("FILE1")
  1. W "open ",PSADDR,!
  1. W PSUID,!
  1. W "lcd ",PSLOCDIR,!
  1. W "cd ",PSREMDIR,!
  1. W "ascii",!
  1. W "get ",PSREMFIL,!
  1. W "quit",!
  1. D CLOSE^%ZISH("FILE1")
  1. ; Execute ftp file, create logfile
  1. N PSZFRC
  1. S PSZFRC=$ZF(-1,"sftp -s:"_PSWRKDIR_PSCMDFIL_" >"_PSWRKDIR_PSLOGFIL)
  1. ; Error check
  1. I PSZFRC=-1 S PSRC="0^Windows OS command execution failed" Q
  1. ; Read Logfile into working global
  1. K ^TMP("PSNFTPLOG",$J)
  1. N PSXLOG
  1. S PSXLOG=$$FTG^%ZISH(PSWRKDIR,PSLOGFIL,$NA(^TMP("PSNFTPLOG",$J,1)),3)
  1. ; Check for error during ftp
  1. N PSPNG,PSPNG1,PSSTOP
  1. S PSPNG=""
  1. S PSPNG1=0
  1. S PSSTOP=0
  1. F S PSPNG=$O(^TMP("PSNFTPLOG",$J,PSPNG)) Q:PSPNG=""!PSSTOP D
  1. . S PSPNG1=$G(^TMP("PSNFTPLOG",$J,PSPNG))
  1. . I PSPNG1["530 Login incorrect" S PSRC="0^Login incorrect",PSSTOP=1 Q
  1. . I PSPNG1["425 Not logged in" S PSRC="0^Login incorrect",PSSTOP=1 Q
  1. . I PSPNG1["550-Failed to open" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["550 file not found" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["no such file" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. . I PSPNG1["No such file" S PSRC="-1^remote file not found",PSSTOP=1 Q
  1. Q
  1. ;
  1. FILSIZE(PSDIR,PSFILE,PSSIZE,PSNFLAG1) ;get the file size after retrieval
  1. N PSDIR,PSFSIZL,PSFSIZL2,DIE,DA,D0,DR,PSXLOG,PSSEQ,PSSEQD,PSIEN,ERROR,PSOS,X
  1. S PSOS=$$GETOS^PSNFTP
  1. S:'$D(PSDIR) PSDIR=$$GETD^PSNFTP()
  1. S PSFSIZL="PSFSIZE.LOG"
  1. S X=$ZF(-1,"DIR/SIZE=UNITS=BYTES "_PSDIR_PSFILE,PSDIR_PSFSIZL)
  1. S PSXLOG="",PSXLOG=$$FTG^%ZISH(PSDIR,PSFSIZL,$NA(^TMP("PSNFSIZELOG",$J,1)),3)
  1. S (PSSEQ,PSSEQD,PSIEN,DIE,DR,DA)=""
  1. F S PSSEQ=$O(^TMP("PSNFSIZELOG",$J,PSSEQ)) Q:PSSEQ="" D
  1. . S PSSEQD=$G(^TMP("PSNFSIZELOG",$J,PSSEQ))
  1. . I PSSEQD["Total of" S PSSIZE=$P(PSSEQD,",",2)
  1. S PSFSIZL2="PSFSIZE2.LOG"
  1. S X=$ZF(-1,"DIR/SIZE "_PSDIR_PSFILE,PSDIR_PSFSIZL2)
  1. S PSXLOG="",PSXLOG=$$FTG^%ZISH(PSDIR,PSFSIZL2,$NA(^TMP("PSNFSIZELOG2",$J,1)),3)
  1. S (PSSEQ,PSSEQD,PSIEN,DIE,DR,DA)=""
  1. F S PSSEQ=$O(^TMP("PSNFSIZELOG2",$J,PSSEQ)) Q:PSSEQ="" D
  1. . S PSSEQD=$G(^TMP("PSNFSIZELOG2",$J,PSSEQ))
  1. . I PSSEQD["Total of" S PSSIZE=PSSIZE_" or"_$P($P(PSSEQD,",",2),".")
  1. G:$G(PSNFLAG1) FILSIZQ
  1. S PSIEN="",PSIEN=$O(^PS(57.23,1,4,"B",PSFILE_";1",PSIEN))
  1. G FILSIZQ:PSIEN=""
  1. I '$G(PSNFLAG1) S DIE="^PS(57.23,1,4,",DA=PSIEN,DR="3///"_PSSIZE D ^DIE
  1. FILSIZQ ;
  1. I '$G(PSOS) S PSOS=$$GETOS^PSNFTP
  1. I PSOS["VMS" D
  1. . D VMSDEL(1,PSDIR,PSFSIZL)
  1. . D VMSDEL(1,PSDIR,PSFSIZL2)
  1. K ^TMP("PSNFSIZELOG",$J)
  1. Q
  1. ;
  1. VMSDEL(PSRC,PSDIR,PSFILE) ;Delete Local Host File
  1. ;Inputs: PSRC - return code, by reference
  1. ; PSDIR - directory name
  1. ; PSFILE - file name
  1. ;Output: PSRC - populated return code
  1. ;
  1. ;check parameters
  1. I $G(PSDIR)="" S PSRC="0^no directory name" Q
  1. I $G(PSFILE)="" S PSRC="0^no file name" Q
  1. S PSZFRC=$ZF(-1,"delete "_PSDIR_PSFILE_";*")
  1. I PSZFRC=-1 S PSRC="0^VMS OS command execution failed" Q
  1. Q
  1. ;
  1. SAVEKEYS(LOCDIR) ; Saves Key to local directory
  1. ;Input: LOCDIR - Local directory where the keys should be saved to
  1. N WLN,XPV
  1. I $$GET1^DIQ(57.23,1,39,"I")="SSH2" D
  1. . ;Saving the Private SSH Key
  1. . D OPEN^%ZISH("VSSHKEY",LOCDIR,"VSSHKEY","W")
  1. . D USE^%ZISUTL("VSSHKEY")
  1. . F WLN=1:1 Q:'$D(^PS(57.23,1,"PRVKEY",WLN)) D
  1. . . W $$DECRYP^XUSRB1(^PS(57.23,1,"PRVKEY",WLN,0)),!
  1. . D CLOSE^%ZISH("VSSHKEY")
  1. ;
  1. I $$OS^%ZOSV()["VMS" D Q
  1. . ;Saving the Public SSH Key (Assuming SSH2 format) - VMS Only
  1. . D OPEN^%ZISH("VSSHKEY",LOCDIR,"VSSHKEY.PUB","W")
  1. . D USE^%ZISUTL("VSSHKEY")
  1. . F WLN=1:1 Q:'$D(^PS(57.23,1,"PUBKEY",WLN)) D
  1. . . W $$DECRYP^XUSRB1(^PS(57.23,1,"PUBKEY",WLN,0)),!
  1. . D CLOSE^%ZISH("VSSHKEY")
  1. ;
  1. I $$OS^%ZOSV()["UNIX" D Q
  1. . ;If Key format is SSH2, convert VSSHKEY to OpenSSH format; Otherwise write directly from VistA
  1. . I $$GET1^DIQ(57.23,1,39,"I")="SSH2" D
  1. . . S XPV="S PV=$ZF(-1,""ssh-keygen -i -f "_LOCDIR_"VSSHKEY > "_LOCDIR_"uxsshkey"")"
  1. . . X XPV
  1. . E D
  1. . . ;Saving the Private SSH Key (OpenSSH Format)
  1. . . D OPEN^%ZISH("uxsshkey",LOCDIR,"uxsshkey","W")
  1. . . D USE^%ZISUTL("uxsshkey")
  1. . . F WLN=1:1 Q:'$D(^PS(57.23,1,"PRVKEY",WLN)) D
  1. . . . W $$DECRYP^XUSRB1(^PS(57.23,1,"PRVKEY",WLN,0)),!
  1. . . D CLOSE^%ZISH("uxsshkey")
  1. . S XPV="S PV=$ZF(-1,""chmod 600 "_LOCDIR_"uxsshkey"")"
  1. . X XPV
  1. Q
  1. DIREXIST(DIR) ; Returns whether the Linux Directory for sFTP already exists
  1. ;Input: DIR - Linux Directory name to be checked
  1. ;*573 Added condition check for IRIS
  1. N DIREXIST,PSNVER
  1. S PSNVER=$$UP^XLFSTR($$VERSION^%ZOSV(1))
  1. I DIR="" Q 0
  1. I $$OS^%ZOSV()'="UNIX" Q 0
  1. I PSNVER'["CACHE",PSNVER'["IRIS" Q 0
  1. I $E(DIR,$L(DIR))="/" S $E(DIR,$L(DIR))=""
  1. X "S DIREXIST=$ZSEARCH(DIR)"
  1. Q $S(DIREXIST="":0,1:1)
  1. ;
  1. MAKEDIR(DIR) ; Create a new directory
  1. ;Input: DIR - Linux Directory name to be created
  1. ;*573 Added condition check for IRIS
  1. N MKDIR,PSNVER
  1. S PSNVER=$$UP^XLFSTR($$VERSION^%ZOSV(1))
  1. I $$OS^%ZOSV()'="UNIX" Q
  1. I PSNVER'["CACHE",PSNVER'["IRIS" Q
  1. I $$DIREXIST(DIR) Q
  1. X "S MKDIR=$ZF(-1,""mkdir ""_DIR)"
  1. I 'MKDIR X "S MKDIR=$ZF(-1,""chmod 777 ""_DIR)"
  1. Q
  1. ;
  1. DELFILES(LOCDIR,LOGFILE,PSCOMFIL,PSDATFIL) ; Delete Files
  1. ;Input: LOCDIR - Local Directory
  1. ;
  1. N FILE2DEL,PSOOS
  1. I $G(LOCDIR)="" Q
  1. S PSOOS=$$OS^%ZOSV()
  1. S:$G(LOGFILE)'="" FILE2DEL(LOGFILE)=""
  1. S:$G(PSCOMFIL)'="" FILE2DEL(PSCOMFIL)=""
  1. S:$G(PSDATFIL)'="" FILE2DEL(PSDATFIL)=""
  1. I PSOOS["VMS" S FILE2DEL("VSSHID.")="",FILE2DEL("VSSHKEY.")="",FILE2DEL("VSSHKEY.PUB")=""
  1. I PSOOS["UNIX" S FILE2DEL("PSNSIZE.DAT")="",FILE2DEL("VSSHKEY")="",FILE2DEL("uxsshkey")=""
  1. I PSOOS["NT" S FILE2DEL("VSSHKEY")="",FILE2DEL("VSSHKEY.PUB")=""
  1. D DEL^%ZISH(LOCDIR,"FILE2DEL")
  1. Q
  1. ;
  1. XVMSDIR(VMSDIR) ; Converts a VMS directory
  1. ; Input: VMSDIR - OpenVMS directory name (e.g., "USER$:[SFTP.PPSN]")
  1. ; Output: $$XVMSDIR - Converted VMS directory (e.g., "/USER$/PPSN/")
  1. ;
  1. Q "/"_$TR(VMSDIR,".[]:","///")
  1. ;