/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "pg_config.h" IDENT("@(#) HL7hostent.cc (Gunther Schadow) 12/19/96"); #pragma implementation #include "HL7hostent.h" #include "exception.h" #include #include "UniMesg.h" #include "PGObject.h" #include #ifndef PGETC # define PGETC "/usr/local/lib/hl7/etc" #endif #define HL7HOSTS PGETC "/hl7hosts" extern "C" { char *config_open(const char *filename, int contlines); void config_close(); char *config_next(); char *config_skip(char **string); } HL7hostent::HL7hostent() { remote_host = NULL; connect_id = NULL; SenApp = NULL; SenFac = NULL; RecApp = NULL; RecFac = NULL; const char *syscall = config_open(HL7HOSTS, 1); if(syscall != NULL) ERROS("%s", syscall); } HL7hostent::HL7hostent(const HL7hostent &x) { (remote_host = x.remote_host) == NULL ? NULL : strdup(x.remote_host); (connect_id = x.connect_id) == NULL ? NULL : strdup(x.connect_id); (SenApp = x.SenApp) == NULL ? NULL : strdup(x.SenApp); (SenFac = x.SenFac) == NULL ? NULL : strdup(x.SenFac); (RecApp = x.RecApp) == NULL ? NULL : strdup(x.RecApp); (RecFac = x.RecFac) == NULL ? NULL : strdup(x.RecFac); allowed = x.allowed; const char *syscall = config_open(HL7HOSTS, 1); if(syscall != NULL) ERROS("%s", syscall); } const HL7hostent & HL7hostent::operator = (const HL7hostent &x) { if(remote_host != NULL) delete [] remote_host; if(connect_id != NULL) delete [] connect_id; if(SenApp != NULL) delete [] SenApp; if(SenFac != NULL) delete [] SenFac; if(RecApp != NULL) delete [] RecApp; if(RecFac != NULL) delete [] RecFac; (remote_host = x.remote_host) == NULL ? NULL : strdup(x.remote_host); (connect_id = x.connect_id) == NULL ? NULL : strdup(x.connect_id); (SenApp = x.SenApp) == NULL ? NULL : strdup(x.SenApp); (SenFac = x.SenFac) == NULL ? NULL : strdup(x.SenFac); (RecApp = x.RecApp) == NULL ? NULL : strdup(x.RecApp); (RecFac = x.RecFac) == NULL ? NULL : strdup(x.RecFac); allowed = x.allowed; return *this; } HL7hostent::~HL7hostent() { if(remote_host != NULL) delete [] remote_host; if(connect_id != NULL) delete [] connect_id; if(SenApp != NULL) delete [] SenApp; if(SenFac != NULL) delete [] SenFac; if(RecApp != NULL) delete [] RecApp; if(RecFac != NULL) delete [] RecFac; config_close(); } result HL7hostent::getent(void) { char *cfline = config_next(); if(cfline != NULL) { parse_line(cfline); return SUCCESS; } else return FAIL; } result HL7hostent::getent(const char *remh, const char *conid, const char *senApp, const char *senFac, const char *recApp, const char *recFac) { // just in case remh was loaded with sockinetbuf::peerhost() save it size_t sreml = strlen(remh); char sremh[sreml + 1]; memcpy(sremh, remh, sreml + 1); remh = sremh; while(getent() == SUCCESS) { if(remh != NULL && remote_host != NULL) if(strcasecmp(remh, remote_host) != 0) continue; if(conid != NULL && connect_id != NULL) if(strcasecmp(conid, connect_id) != 0) continue; if(senApp != NULL && SenApp != NULL) if(strcasecmp(senApp, SenApp) != 0) continue; if(senFac != NULL && SenFac != NULL) if(strcasecmp(senFac, SenFac) != 0) continue; if(recApp != NULL && RecApp != NULL) if(strcasecmp(recApp, RecApp) != 0) continue; if(recFac != NULL && RecFac != NULL) if(strcasecmp(recFac, RecFac) != 0) continue; return SUCCESS; } return FAIL; } void HL7hostent::parse_line(char *cfline) { if(remote_host != NULL) delete [] remote_host; if(connect_id != NULL) delete [] connect_id; if(SenApp != NULL) delete [] SenApp; if(SenFac != NULL) delete [] SenFac; if(RecApp != NULL) delete [] RecApp; if(RecFac != NULL) delete [] RecFac; remote_host = NULL; connect_id = NULL; SenApp = NULL; SenFac = NULL; RecApp = NULL; RecFac = NULL; const char **str = &remote_host; for(int i = 0; i < 6; i++) { if(cfline == NULL) str[i] = NULL; else if((str[i] = config_skip(&cfline)) == NULL) cfline = NULL; else if(strcmp(str[i], "*") == 0) str[i] = NULL; else str[i] = strdup(str[i]); } if(cfline == NULL) allowed.set(); else { char *msg = config_skip(&cfline); if(msg == NULL) allowed.set(); else { char oper = *msg; if(oper == '-') { allowed.set(); msg++; } else if(oper == '+') { allowed.clear(); msg++; } else { oper = '+'; allowed.clear(); } if(*msg == '\0') msg = config_skip(&cfline); while(msg != NULL) { ZMtyp zm; zm.str2o(msg); int umid = (int)UniMesId(zm); if(umid >= 0) if(oper == '+') allowed.set(umid); else allowed.clear(umid); msg = config_skip(&cfline); } } } // change hostname into canonical form if(remote_host != NULL) { sockinetaddr sina(remote_host); delete [] remote_host; remote_host = strdup(sina.gethostname()); } }