/*Evalua si el id de un usuario ha sido repetido y de ser
asi pregunta por otro o cancela la creacion del proceso*/
int set_uid(users *front)
{
  int uid,flag = 0;
  users *f = front;
  if(front == NULL)
    uid = set_int("el id del nuevo usuario",0);
  else
  {
    uid = set_int("el id del nuevo usuario",0);
    do
    {
      if(uid == f->uid)
      {
        flag = FAIL;
        break;
      }
    }while(next_user(&f,front) != FAIL);
    if(flag == FAIL)
    {
      printf("%s\n",REP_FAIL);
      if(cancel("creacion del usuario") == 0)
        uid = set_uid(front);
      else
        uid = FAIL;
    }
  }

  return uid;
}
/*Encuentra un usuario por id*/
users* find_user(int uid, users *front)
{
  users *chosen,*aux = front;
  chosen = NULL;
  do
  {
    if(uid == aux->uid)
    {
      chosen = aux;
      break;
    }
  }while(next_user(&aux,front) != FAIL);
  return chosen;
}
/*Muestra los usuarios esxistentes*/
void show_users(usersCtrl *ctrlU)
{
  users *f;
  if(ctrlU->front != NULL)
  {
    f = ctrlU->front;

    printf("\n    USUARIOS EXISTENTES\n");
    printf("|%7s|%15s|\n","ID","Descripcion");
    printf("=========================\n");
    do
    {
      printf("|(%5i)|%15s|\n",f->uid,f->n);
    }while(next_user(&f,ctrlU->front) != FAIL);
    printf("\n");
  }
  else
    printf("No se han creado usuarios.\n");
}
// This callback fires when the remote side requests a read from the channel.
// It call ntds_read_into_batch up to 20 times and feeds the results into
// an array which is then written back out into the channel's output buffer
static DWORD ntds_channel_read(Channel *channel, Packet *request,
	LPVOID context, LPVOID buffer, DWORD bufferSize, LPDWORD bytesRead)
{
	JET_ERR readStatus = JET_errSuccess;
	DWORD result = ERROR_SUCCESS;
	NTDSContext *ctx = (NTDSContext *)context;
	struct ntdsAccount batchedAccounts[20];
	DWORD batchSize = 0;

	for (int i = 0; i < 20; i++) {
		readStatus = ntds_read_into_batch(ctx, &batchedAccounts[i]);
		if (readStatus != JET_errSuccess) {
			break;
		}
		batchSize += sizeof(struct ntdsAccount);
		next_user(ctx->ntdsState, ctx->accountColumns);
	}

	memcpy(buffer, batchedAccounts, batchSize);
	*bytesRead = batchSize;

	return ERROR_SUCCESS;
}
// This is the raw NTDS command function. When the remote user
// sends a command request for extapi_ntds_parse, this function fires.
// It calls the setup routines for our Jet Instance, attaches the isntance
// to the NTDS.dit database the user specified, and creates our channel.
// The user interacts with the NTDS database through that channel from that point on.
DWORD ntds_parse(Remote *remote, Packet *packet)
{
	Packet *response = packet_create_response(packet);
	DWORD res = ERROR_SUCCESS;
	struct jetState *ntdsState = calloc(1,sizeof(struct jetState));
	PCHAR filePath = packet_get_tlv_value_string(packet, TLV_TYPE_NTDS_PATH);
	// Check if the File exists
	if (0xffffffff == GetFileAttributes(filePath)) {
		res = 2;
		goto out;
	}
	strncpy_s(ntdsState->ntdsPath, 255, filePath, 254);

	// Attempt to get the SysKey from the Registry
	unsigned char sysKey[17];
	if (!get_syskey(sysKey)) {
		res = GetLastError();
		goto out;
	}

	JET_ERR startupStatus = engine_startup(ntdsState);
	if (startupStatus != JET_errSuccess) {
		res = startupStatus;
		goto out;
	}

	// Start a Session in the Jet Instance
	JET_ERR sessionStatus = JetBeginSession(ntdsState->jetEngine, &ntdsState->jetSession, NULL, NULL);
	if (sessionStatus != JET_errSuccess) {
		JetTerm(ntdsState->jetEngine);
		res = sessionStatus;
		goto out;
	}
	JET_ERR openStatus = open_database(ntdsState);
	if (openStatus != JET_errSuccess) {
		JetEndSession(ntdsState->jetSession, (JET_GRBIT)NULL);
		JetTerm(ntdsState->jetEngine);
		res = openStatus;
		goto out;
	}
	JET_ERR tableStatus = JetOpenTable(ntdsState->jetSession, ntdsState->jetDatabase, "datatable", NULL, 0, JET_bitTableReadOnly | JET_bitTableSequential, &ntdsState->jetTable);
	if (tableStatus != JET_errSuccess) {
		engine_shutdown(ntdsState);
		res = tableStatus;
		goto out;
	}

	// Create the structure for holding all of the Column Definitions we need
	struct ntdsColumns *accountColumns = calloc(1, sizeof(struct ntdsColumns));

	JET_ERR columnStatus = get_column_info(ntdsState, accountColumns);
	if (columnStatus != JET_errSuccess) {
		engine_shutdown(ntdsState);
		free(accountColumns);
		res = columnStatus;
		goto out;
	}
	JET_ERR pekStatus;
	struct encryptedPEK *pekEncrypted = calloc(1,sizeof(struct encryptedPEK));
	struct decryptedPEK *pekDecrypted = calloc(1,sizeof(struct decryptedPEK));

	// Get and Decrypt the Password Encryption Key (PEK)
	pekStatus = get_PEK(ntdsState, accountColumns, pekEncrypted);
	if (pekStatus != JET_errSuccess) {
		res = pekStatus;
		free(accountColumns);
		free(pekEncrypted);
		free(pekDecrypted);
		engine_shutdown(ntdsState);
		goto out;
	}
	if (!decrypt_PEK(sysKey, pekEncrypted, pekDecrypted)) {
		res = GetLastError();
		free(accountColumns);
		free(pekEncrypted);
		free(pekDecrypted);
		engine_shutdown(ntdsState);
		goto out;
	}
	// Set our Cursor on the first User record
	JET_ERR cursorStatus = find_first(ntdsState);
	if (cursorStatus != JET_errSuccess) {
		res = cursorStatus;
		free(accountColumns);
		free(pekEncrypted);
		free(pekDecrypted);
		engine_shutdown(ntdsState);
		goto out;
	}
	cursorStatus = next_user(ntdsState, accountColumns);
	if (cursorStatus != JET_errSuccess) {
		res = cursorStatus;
		free(accountColumns);
		free(pekEncrypted);
		free(pekDecrypted);
		engine_shutdown(ntdsState);
		goto out;
	}

	// If we made it this far, it's time to set up our channel
	PoolChannelOps chops;
	Channel *newChannel;
	memset(&chops, 0, sizeof(chops));

	NTDSContext *ctx;
	// Allocate storage for the NTDS context
	if (!(ctx = calloc(1, sizeof(NTDSContext)))) {
		res = ERROR_NOT_ENOUGH_MEMORY;
		free(accountColumns);
		free(pekEncrypted);
		free(pekDecrypted);
		engine_shutdown(ntdsState);
		goto out;
	}

	ctx->accountColumns = accountColumns;
	ctx->ntdsState = ntdsState;
	ctx->pekDecrypted = pekDecrypted;

	// Initialize the pool operation handlers
	chops.native.context = ctx;
	chops.native.close = ntds_channel_close;
	chops.read = ntds_channel_read;
	if (!(newChannel = channel_create_pool(0, CHANNEL_FLAG_SYNCHRONOUS | CHANNEL_FLAG_COMPRESS, &chops)))
	{
		res = ERROR_NOT_ENOUGH_MEMORY;
		free(accountColumns);
		free(pekEncrypted);
		free(pekDecrypted);
		engine_shutdown(ntdsState);
		goto out;
	}

	channel_set_type(newChannel, "ntds");
	packet_add_tlv_uint(response, TLV_TYPE_CHANNEL_ID, channel_get_id(newChannel));

out:
	packet_transmit_response(res, remote, response);
	return ERROR_SUCCESS;
}
int main()
{
  int totalTime;
  totalTime = 0;
  int quantum;
  int op;
  int cpp = 0;
  pcbCtrl *ctrl;
  pcbStates *states;
  groupsCtrl *ctrlG;
  usersCtrl *ctrlU;
  ctrl = malloc(sizeof(pcbCtrl));
  states = malloc(sizeof(pcbStates));
  states->readys = malloc(sizeof(pcbCtrl));
  states->waiting = malloc(sizeof(pcbCtrl));
  states->sleeping = malloc(sizeof(pcbCtrl));
  ctrlG = malloc(sizeof(groupsCtrl));
  ctrlU = malloc(sizeof(usersCtrl));

  printf("\n");

  quantum = set_int("Quantum del programa", 1);

  if(val_npos(quantum, 1) != FAIL)
  {
    do
    {
      printf("\n \t\t<< SIMULACION DE ALGORITMO DE DESPACHO RONUD-ROBIN >>\n");
      print_options(0);
      printf("\n>");
      scanf("%i",&op);
      getchar();

      switch(op)
      {
        case 1:
        printf("\n");
          create_group(ctrlG);
          break;
        case 2:
        printf("\n");
          create_user(ctrlU);
          break;
        case 3:
        printf("\n");
          create_process(cpp,ctrl,states,ctrlG,ctrlU);
          cpp++;
          break;
        case 4:
        printf("\n");
          state_change(ctrl,states);
          break;
        case 5:
        printf("\n");
          show_everything(ctrl,states,ctrlG,ctrlU);
          break;
        case 6:
        printf("\n");
          rr(states, ctrl, quantum, &totalTime);
          break;
        case 7:
        printf("\n");
          del_option(ctrl,states,ctrlG,ctrlU);
          break;
        case 8:
          break;
        default:
          printf("Opcion invalida, vuelva a intentarlo.\n");
      }
    }while(op != 8);

    if(ctrl->front != NULL)
    {
      pcb *aux = ctrl->front;
      while( next_pcb(&aux,ctrl->front) != FAIL )
      free(aux);
    }

    if(ctrlG->front != NULL)
    {
      groups *aux = ctrlG->front;
      while( next_group(&aux,ctrlG->front) != FAIL )
      free(aux);
    }

    if(ctrlU->front != NULL)
    {
      users *aux = ctrlU->front;
      while( next_user(&aux,ctrlU->front) != FAIL )
      free(aux);
    }

    free(ctrl->front);
    free(ctrlG->front);
    free(ctrlU->front);
    free(ctrl);
    free(ctrlG);
    free(ctrlU);
  }

  return 0;
}
Beispiel #7
0
struct user_priority * ld_priority_file ( char * path )
{
    char				line[BUFSIZ],
					*p,
					*user,
					*next_user();
    static struct user_priority		pri_tbl;
    int					line_no	= 1,
    					opri;
    int fd;

    if ((fd = open_locked(path, "r", 0)) < 0) {
	if (errno == ENOENT) {
empty:
	    pri_tbl.deflt = LEVEL_DFLT;
	    pri_tbl.deflt_limit = LIMIT_DFLT;
	    memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
	    return (&pri_tbl);
	}
	return(0);
    }

    /* initialize table to empty */
    pri_tbl.deflt = -1;
    pri_tbl.deflt_limit = -1;
    memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));

    /* this loop reads the line containing the default priority,
       if any, and the first priority limit.  p is left pointing
       to the colon (:) in the line with the first limit. */

    while (1)
    {
	if (!(p = fdgets(line, BUFSIZ, fd)))
	    goto empty;
	p = line;
	pri = strtol(line, &p, 10);
	if (p == line)
	    goto Error;
	if (pri < PRI_MIN || pri > PRI_MAX)
	    goto Error;
	if (line_no == 1 && *p == '\n' && !p[1])
	    pri_tbl.deflt = pri;
	else
	    if (*p == ':')
	    {
		p++;
		break;
	    }
	    else
		goto Error;
	line_no++;
    }

    do
    {
	/* search list for this priority */
	opri = pri;
	if (!(user = next_user(fd, line, &p)))
	{
	    if (pri_tbl.deflt_limit == -1)
	    {
		pri_tbl.deflt_limit = opri;
		if (pri == -1) break;
		if (!(user = next_user(fd, line, &p))) goto Error;
	    }
	    else
	    {
Error:
	        errno = EBADF;
		close(fd);
		return(0);
	    }
	}

	do
	{
	    add_user (&pri_tbl, user, pri);
	}
	while ((user = next_user(fd, line, &p)));
    }
    while (pri != -1);

    if (pri_tbl.deflt == -1)
	pri_tbl.deflt = LEVEL_DFLT;

    if (pri_tbl.deflt_limit == -1)
	pri_tbl.deflt_limit = LIMIT_DFLT;

    close(fd);
    return (&pri_tbl);
}