/*
 * mars-nwe-dosutils - NetWare/DOS utility tools.
 *
 * Copyright (C) 2026 Mario Fetka
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

/*
 * Purpose: Shared trustee and rights helper implementation for GRANT, REMOVE and REVOKE style tools.
 * Depends on: net.h, trustee.h, ncpapi.h callers, netcall.c requester helpers, tools.c shared utility routines.
 */

#include "net.h"
#include "trustee.h"
#include <sys/types.h>
#include <sys/stat.h>

#ifndef S_IFDIR
#define S_IFDIR 0040000
#endif


/*
 * trustee_header_path
 *
 * Purpose:
 *   Formats the path header used by GRANT, REMOVE and REVOKE.
 *
 * Parameters:
 *   out  - destination buffer.
 *   path - source path to format.
 *   max  - destination buffer size.
 *
 * Notes:
 *   Novell trustee tools display the server and volume separator as
 *   SERVER/SYS: while tools such as RIGHTS keep SERVER\SYS:. Keep all
 *   remaining path separators as DOS backslashes.
 */
void trustee_header_path(char *out, char *path, int max)
{
  char *p;
  char *colon;

  tool_header_path(out, path, max);

  p = strchr(out, '\\');
  colon = strchr(out, ':');
  if (p && colon && p < colon)
    *p = '/';
}
/*
 * trustee_parse_right_word
 *
 * Purpose:
 *   Converts one Novell-style trustee right word into the corresponding
 *   386 trustee rights mask bits.
 *
 * Parameters:
 *   s      - right word or one-letter abbreviation, such as READ, R, CREATE,
 *            C, SUPERVISOR or S.
 *   rights - in/out accumulated rights mask updated on success.
 *
 * Returns:
 *   0 when the word was recognized, -1 for an unknown right word.
 *
 * Notes:
 *   The command parsers call this repeatedly so individual right words can be
 *   accumulated into one mask. The historical NONE handling is preserved here
 *   and should only be changed together with the GRANT/REVOKE compatibility
 *   tests.
 */
int trustee_parse_right_word(char *s, uint16 *rights)
{
  if (tool_strsame(s, "ALL")) {
    *rights = TRUSTEE_RIGHT_ALL_386;
    return(0);
  }

  if (tool_strsame(s, "N") || tool_strsame(s, "NONE")) {
    *rights = TRUSTEE_RIGHT_ALL_386;
    return(0);
  }

  if (tool_strsame(s, "S") || tool_strsame(s, "SUPERVISOR")) {
    *rights |= TRUSTEE_RIGHT_SUPER;
    return(0);
  }

  if (tool_strsame(s, "R") || tool_strsame(s, "READ")) {
    *rights |= TRUSTEE_RIGHT_READ;
    return(0);
  }

  if (tool_strsame(s, "W") || tool_strsame(s, "WRITE")) {
    *rights |= TRUSTEE_RIGHT_WRITE;
    return(0);
  }

  if (tool_strsame(s, "C") || tool_strsame(s, "CREATE")) {
    *rights |= TRUSTEE_RIGHT_CREATE;
    return(0);
  }

  if (tool_strsame(s, "E") || tool_strsame(s, "ERASE") ||
      tool_strsame(s, "D") || tool_strsame(s, "DELETE")) {
    *rights |= TRUSTEE_RIGHT_DELETE;
    return(0);
  }

  if (tool_strsame(s, "M") || tool_strsame(s, "MODIFY")) {
    *rights |= TRUSTEE_RIGHT_MODIFY;
    return(0);
  }

  if (tool_strsame(s, "F") || tool_strsame(s, "FILESCAN") ||
      tool_strsame(s, "FILE") || tool_strsame(s, "SCAN")) {
    *rights |= TRUSTEE_RIGHT_SEARCH;
    return(0);
  }

  if (tool_strsame(s, "A") || tool_strsame(s, "ACCESS") ||
      tool_strsame(s, "CONTROL") || tool_strsame(s, "ACCESSCONTROL")) {
    *rights |= TRUSTEE_RIGHT_OWNER;
    return(0);
  }

  return(-1);
}

/*
 * trustee_rights_bracket
 *
 * Purpose:
 *   Formats a 386 trustee rights mask as the Novell-style eight-character
 *   bracket body used by GRANT, REMOVE and REVOKE output.
 *
 * Parameters:
 *   rights - trustee rights mask.
 *   out    - caller-supplied buffer receiving an 8-character string plus NUL.
 *
 * Notes:
 *   Missing rights are rendered as spaces, not dashes. Keep this formatting
 *   stable because the command output is compared against Novell tools.
 */
void trustee_rights_bracket(uint16 rights, char *out)
{
  out[0] = (rights & TRUSTEE_RIGHT_SUPER)  ? 'S' : ' ';
  out[1] = (rights & TRUSTEE_RIGHT_READ)   ? 'R' : ' ';
  out[2] = (rights & TRUSTEE_RIGHT_WRITE)  ? 'W' : ' ';
  out[3] = (rights & TRUSTEE_RIGHT_CREATE) ? 'C' : ' ';
  out[4] = (rights & TRUSTEE_RIGHT_DELETE) ? 'E' : ' ';
  out[5] = (rights & TRUSTEE_RIGHT_MODIFY) ? 'M' : ' ';
  out[6] = (rights & TRUSTEE_RIGHT_SEARCH) ? 'F' : ' ';
  out[7] = (rights & TRUSTEE_RIGHT_OWNER)  ? 'A' : ' ';
  out[8] = '\0';
}

/*
 * trustee_lookup_object
 *
 * Purpose:
 *   Resolves a trustee name to a bindery object ID for GRANT/REMOVE/REVOKE
 *   style operations.
 *
 * Parameters:
 *   name          - bindery object name supplied on the command line.
 *   objtype       - in/out bindery object type. When objtype_given is false,
 *                   receives the matching USER or GROUP type.
 *   objtype_given - non-zero when the caller already selected a concrete
 *                   bindery object type.
 *
 * Returns:
 *   Non-zero bindery object ID on success, or zero when no matching object was
 *   found.
 *
 * NCP:
 *   Uses ncp17_35_get_bindery_object_id(). If no explicit type is supplied,
 *   USER is tried first and GROUP second, matching the common Novell command
 *   behavior.
 */
uint32 trustee_lookup_object(char *name, uint16 *objtype, int objtype_given)
{
  uint8 namebuf[48];
  uint32 object_id;

  strmaxcpy(namebuf, name, sizeof(namebuf) - 1);
  upstr(namebuf);

  if (objtype_given) {
    return(ncp17_35_get_bindery_object_id(namebuf, *objtype));
  }

  *objtype = TRUSTEE_BINDERY_USER;
  object_id = ncp17_35_get_bindery_object_id(namebuf, TRUSTEE_BINDERY_USER);
  if (object_id)
    return(object_id);

  *objtype = TRUSTEE_BINDERY_GROUP;
  object_id = ncp17_35_get_bindery_object_id(namebuf, TRUSTEE_BINDERY_GROUP);
  return(object_id);
}


