#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<pthread.h>

#include"zither.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"


extern global_data_t gd;


/*--------------- SLGET_LOGIC 1.1.7以上 UNDIFと同じで名前が違う------------------- */
int slget_logi(private_data_t * pd)
{
static int stat;

char * swap = (char *)calloc(IP_LEN, sizeof(char));

int ustat = self_memcmp(pd->rec.user, pd->rec.def_user); 
int astat = self_memcmp(pd->rec.user, "administrator");
int cgstat = self_memcmp(pd->rec.user, "cgiuser");

stat = 0; 
if(ustat == 0 && astat != 0 && cgstat != 0){ 
  if(0 == (self_memcmp("COMAND", pd->rec.data))){
     swap = file_send(gd.ms.sk[pd->rec.fdno],
                  pd->file.bat_filename, swap,
                  &stat,
                  (pd->life.life_int + pd->life.life_los + INT_PLUS),
                  (char)0x05);
     }
  else if(0 == (self_memcmp("PORT", pd->rec.data))){
     swap = file_send(gd.ms.sk[pd->rec.fdno],
                  pd->file.scan_filename,
                  swap,
                  &stat,
                  (pd->life.life_int + pd->life.life_los + INT_PLUS),
                  (char)0x05);
     }
  else if(0 == (self_memcmp("CONFIG", pd->rec.data))){
     swap = file_send(gd.ms.sk[pd->rec.fdno],
                  pd->file.conf_filename,
                  swap,
                  &stat,
                  (pd->life.life_int + pd->life.life_los + INT_PLUS),
                  (char)0x05);
     }
  else if(0 == (self_memcmp("STAT", pd->rec.data))){
     swap = file_send(gd.ms.sk[pd->rec.fdno],
                  pd->file.stat_filename,
                  swap,
                  &stat,
                  (pd->life.life_int + pd->life.life_los + INT_PLUS),
                  (char)0x05);
     }
  else if(0 == (self_memcmp("CRONFILE", pd->rec.data)) && gd.cron_onoff == 1){
     swap = file_send(gd.ms.sk[pd->rec.fdno],
                  pd->file.cronfile_filename,
                  swap,
                  &stat,
                  (pd->life.life_int + pd->life.life_los + INT_PLUS),
                  (char)0x05);
     }
  else if(0 == (self_memcmp("CRONLIST", pd->rec.data)) && gd.cron_onoff == 1){
     swap = file_send(gd.ms.sk[pd->rec.fdno],
                  pd->file.cronlist_filename,
                  swap,
                  &stat,
                  (pd->life.life_int + pd->life.life_los + INT_PLUS),
                  (char)0x05);
     }
  else{
     swap = safe_memcpy(swap, "ERR\n", BUF_MAX);
     stat = socket_send(gd.ms.sk[pd->rec.fdno], "500 NG UNDIF options.\n", (pd->life.life_int + pd->life.life_los + 15));
     }
  #ifdef LOG_OUT
  safe_pthread_mutex_lock(&con_mutex);
  log_out(gd.fp.con_fd, pd->rec.user, pd->rec.com, pd->rec.data,
   "200 OK It doesn't write it.", (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
  pthread_mutex_unlock(&con_mutex);
  #endif
  }
else{
  stat = socket_send(gd.ms.sk[pd->rec.fdno], "500 NG UNDIF command.\n", (pd->life.life_int + pd->life.life_los + 15));
  #ifdef LOG_OUT
  safe_pthread_mutex_lock(&con_mutex);
  log_out(gd.fp.con_fd, pd->rec.user, pd->rec.com, pd->rec.data,
    "500 NG UNDIF command.", (unsigned char *)gd.ms.inip_addr[pd->rec.fdno]);
  pthread_mutex_unlock(&con_mutex);
  #endif
  } 

free(swap);
return(stat);
}










