338 lines
10 KiB
C
338 lines
10 KiB
C
/*
|
|
* Linux smoke test for NetWare AFP Rename.
|
|
*
|
|
* The helper sends the WebSDK/nwafp.h AFP Rename request through ncpfs/libncp
|
|
* and verifies that the source path disappears while the destination path gains
|
|
* an AFP Entry ID.
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <ncp/nwcalls.h>
|
|
#include <ncp/ncplib.h>
|
|
#include <ncp/kernel/ncp.h>
|
|
|
|
#ifndef NCPC_SUBFUNCTION
|
|
#define NCPC_SUBFUNCTION 0x10000
|
|
#endif
|
|
#ifndef NCPC_SFN
|
|
#define NCPC_SFN(FN, SFN) ((FN) | ((SFN) << 8) | NCPC_SUBFUNCTION)
|
|
#endif
|
|
|
|
#define AFP_RENAME 0x07
|
|
#define AFP_GET_ENTRY_ID_FROM_PATH 0x0c
|
|
#define NWE_INVALID_PATH 0x9c
|
|
#ifndef NWE_INVALID_NCP_PACKET_LENGTH
|
|
#define NWE_INVALID_NCP_PACKET_LENGTH 0x7e
|
|
#endif
|
|
|
|
static void usage(const char *prog)
|
|
{
|
|
fprintf(stderr,
|
|
"Usage: %s [--expect-completion CODE] [--volume N] "
|
|
"[--source-entry-id ID] [--dest-entry-id ID] [ncpfs options] SOURCE DEST\n"
|
|
"\n"
|
|
"ncpfs options are parsed by ncp_initialize(), for example:\n"
|
|
" -S SERVER -U USER -P PASSWORD -n\n"
|
|
"\n"
|
|
"Example:\n"
|
|
" %s -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/OLD SYS:PUBLIC/NEW\n",
|
|
prog, prog);
|
|
}
|
|
|
|
static int parse_u32(const char *text, uint32_t *value)
|
|
{
|
|
char *end = NULL;
|
|
unsigned long v;
|
|
|
|
errno = 0;
|
|
v = strtoul(text, &end, 0);
|
|
if (errno || !end || *end || v > 0xffffffffUL)
|
|
return -1;
|
|
*value = (uint32_t)v;
|
|
return 0;
|
|
}
|
|
|
|
static void cpu_to_be32(uint32_t v, uint8_t p[4])
|
|
{
|
|
p[0] = (uint8_t)(v >> 24);
|
|
p[1] = (uint8_t)(v >> 16);
|
|
p[2] = (uint8_t)(v >> 8);
|
|
p[3] = (uint8_t)v;
|
|
}
|
|
|
|
static uint32_t be32_to_cpu(const uint8_t p[4])
|
|
{
|
|
return ((uint32_t)p[0] << 24) |
|
|
((uint32_t)p[1] << 16) |
|
|
((uint32_t)p[2] << 8) |
|
|
p[3];
|
|
}
|
|
|
|
static int split_parent_leaf(const char *path, char *parent, size_t parent_size,
|
|
char *leaf, size_t leaf_size)
|
|
{
|
|
const char *slash = strrchr(path, '/');
|
|
const char *backslash = strrchr(path, '\\');
|
|
const char *sep = slash;
|
|
size_t parent_len;
|
|
size_t leaf_len;
|
|
|
|
if (backslash && (!sep || backslash > sep))
|
|
sep = backslash;
|
|
|
|
if (!sep) {
|
|
const char *colon = strchr(path, ':');
|
|
if (!colon || colon[1] == '\0')
|
|
return -1;
|
|
parent_len = (size_t)(colon - path + 1);
|
|
leaf_len = strlen(colon + 1);
|
|
if (parent_len >= parent_size || leaf_len >= leaf_size || !leaf_len)
|
|
return -1;
|
|
memcpy(parent, path, parent_len);
|
|
parent[parent_len] = '\0';
|
|
memcpy(leaf, colon + 1, leaf_len + 1);
|
|
return 0;
|
|
}
|
|
|
|
parent_len = (size_t)(sep - path);
|
|
leaf_len = strlen(sep + 1);
|
|
if (!parent_len || !leaf_len || parent_len >= parent_size ||
|
|
leaf_len >= leaf_size)
|
|
return -1;
|
|
|
|
memcpy(parent, path, parent_len);
|
|
parent[parent_len] = '\0';
|
|
memcpy(leaf, sep + 1, leaf_len + 1);
|
|
return 0;
|
|
}
|
|
|
|
static NWCCODE afp_get_entry_id_from_path(NWCONN_HANDLE conn,
|
|
const char *path,
|
|
uint32_t *entry_id)
|
|
{
|
|
size_t path_len = strlen(path);
|
|
uint8_t request[1 + 1 + 255];
|
|
uint8_t reply_buf[4];
|
|
NW_FRAGMENT reply;
|
|
NWCCODE err;
|
|
|
|
if (path_len > 255)
|
|
return NWE_INVALID_PATH;
|
|
|
|
request[0] = 0;
|
|
request[1] = (uint8_t)path_len;
|
|
memcpy(request + 2, path, path_len);
|
|
|
|
memset(reply_buf, 0, sizeof(reply_buf));
|
|
reply.fragAddr.rw = reply_buf;
|
|
reply.fragSize = sizeof(reply_buf);
|
|
|
|
err = NWRequestSimple(conn,
|
|
NCPC_SFN(0x23, AFP_GET_ENTRY_ID_FROM_PATH),
|
|
request,
|
|
2 + path_len,
|
|
&reply);
|
|
if (err)
|
|
return err;
|
|
if (reply.fragSize < 4)
|
|
return NWE_INVALID_NCP_PACKET_LENGTH;
|
|
|
|
*entry_id = be32_to_cpu(reply_buf);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
NWCONN_HANDLE conn;
|
|
NW_FRAGMENT reply;
|
|
long init_err = 0;
|
|
const char *source_path = NULL;
|
|
const char *dest_path = NULL;
|
|
uint32_t volume_number = 0;
|
|
uint32_t source_entry_id = 0;
|
|
uint32_t dest_entry_id = 0;
|
|
uint32_t expect_completion = 0xffffffffU;
|
|
int i;
|
|
char source_parent[256];
|
|
char source_leaf[256];
|
|
char dest_parent[256];
|
|
char dest_leaf[256];
|
|
size_t source_leaf_len;
|
|
size_t dest_leaf_len;
|
|
uint8_t request[1 + 4 + 4 + 1 + 255 + 1 + 255];
|
|
size_t req_len;
|
|
uint8_t reply_buf[1];
|
|
uint32_t lookup_entry_id = 0;
|
|
NWCCODE err;
|
|
|
|
if (NWCallsInit(NULL, NULL)) {
|
|
fprintf(stderr, "NWCallsInit failed\n");
|
|
return 2;
|
|
}
|
|
|
|
conn = ncp_initialize(&argc, argv, 1, &init_err);
|
|
if (!conn) {
|
|
fprintf(stderr, "ncp_initialize/login failed: %ld\n", init_err);
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
if (!strcmp(argv[i], "--volume")) {
|
|
if (++i >= argc || parse_u32(argv[i], &volume_number) ||
|
|
volume_number > 255) {
|
|
fprintf(stderr, "invalid --volume value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "--source-entry-id")) {
|
|
if (++i >= argc || parse_u32(argv[i], &source_entry_id)) {
|
|
fprintf(stderr, "invalid --source-entry-id value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "--dest-entry-id")) {
|
|
if (++i >= argc || parse_u32(argv[i], &dest_entry_id)) {
|
|
fprintf(stderr, "invalid --dest-entry-id value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "--expect-completion")) {
|
|
if (++i >= argc || parse_u32(argv[i], &expect_completion) ||
|
|
expect_completion > 255) {
|
|
fprintf(stderr, "invalid --expect-completion value\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
} else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
|
|
usage(argv[0]);
|
|
ncp_close(conn);
|
|
return 0;
|
|
} else if (!source_path) {
|
|
source_path = argv[i];
|
|
} else if (!dest_path) {
|
|
dest_path = argv[i];
|
|
} else {
|
|
fprintf(stderr, "unexpected argument: %s\n", argv[i]);
|
|
usage(argv[0]);
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
if (!source_path || !dest_path) {
|
|
usage(argv[0]);
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
|
|
if (split_parent_leaf(source_path, source_parent, sizeof(source_parent),
|
|
source_leaf, sizeof(source_leaf)) ||
|
|
split_parent_leaf(dest_path, dest_parent, sizeof(dest_parent),
|
|
dest_leaf, sizeof(dest_leaf))) {
|
|
fprintf(stderr, "both SOURCE and DEST must include a parent path\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
|
|
if (!source_entry_id) {
|
|
err = afp_get_entry_id_from_path(conn, source_parent, &source_entry_id);
|
|
if (err) {
|
|
fprintf(stderr, "AFP source parent lookup failed: parent=%s error=0x%02x\n",
|
|
source_parent, (unsigned)err & 0xff);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
}
|
|
if (!dest_entry_id) {
|
|
err = afp_get_entry_id_from_path(conn, dest_parent, &dest_entry_id);
|
|
if (err) {
|
|
fprintf(stderr, "AFP dest parent lookup failed: parent=%s error=0x%02x\n",
|
|
dest_parent, (unsigned)err & 0xff);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
source_leaf_len = strlen(source_leaf);
|
|
dest_leaf_len = strlen(dest_leaf);
|
|
if (source_leaf_len > 255 || dest_leaf_len > 255) {
|
|
fprintf(stderr, "source and destination leaf names must be <=255 bytes\n");
|
|
ncp_close(conn);
|
|
return 2;
|
|
}
|
|
|
|
request[0] = (uint8_t)volume_number;
|
|
cpu_to_be32(source_entry_id, request + 1);
|
|
cpu_to_be32(dest_entry_id, request + 5);
|
|
request[9] = (uint8_t)source_leaf_len;
|
|
memcpy(request + 10, source_leaf, source_leaf_len);
|
|
request[10 + source_leaf_len] = (uint8_t)dest_leaf_len;
|
|
memcpy(request + 11 + source_leaf_len, dest_leaf, dest_leaf_len);
|
|
req_len = 11 + source_leaf_len + dest_leaf_len;
|
|
|
|
memset(reply_buf, 0, sizeof(reply_buf));
|
|
reply.fragAddr.rw = reply_buf;
|
|
reply.fragSize = sizeof(reply_buf);
|
|
|
|
err = NWRequestSimple(conn,
|
|
NCPC_SFN(0x23, AFP_RENAME),
|
|
request,
|
|
req_len,
|
|
&reply);
|
|
|
|
if (expect_completion != 0xffffffffU) {
|
|
if (((unsigned)err & 0xff) != expect_completion) {
|
|
fprintf(stderr,
|
|
"AFP Rename expected completion 0x%02x but got 0x%02x: source=%s dest=%s\n",
|
|
(unsigned)expect_completion & 0xff,
|
|
(unsigned)err & 0xff, source_path, dest_path);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
printf("AFP Rename returned expected completion 0x%02x: source=%s dest=%s\n",
|
|
(unsigned)expect_completion & 0xff, source_path, dest_path);
|
|
ncp_close(conn);
|
|
return 0;
|
|
}
|
|
|
|
if (err) {
|
|
fprintf(stderr,
|
|
"AFP Rename failed: source=%s dest=%s source_entry_id=0x%08x dest_entry_id=0x%08x error=0x%02x\n",
|
|
source_path, dest_path, source_entry_id, dest_entry_id,
|
|
(unsigned)err & 0xff);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
|
|
err = afp_get_entry_id_from_path(conn, source_path, &lookup_entry_id);
|
|
if (!err) {
|
|
fprintf(stderr,
|
|
"AFP Rename verify failed: source still exists source=%s entry_id=0x%08x\n",
|
|
source_path, lookup_entry_id);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
|
|
err = afp_get_entry_id_from_path(conn, dest_path, &lookup_entry_id);
|
|
if (err) {
|
|
fprintf(stderr,
|
|
"AFP Rename verify failed: destination missing dest=%s error=0x%02x\n",
|
|
dest_path, (unsigned)err & 0xff);
|
|
ncp_close(conn);
|
|
return 1;
|
|
}
|
|
|
|
printf("AFP Rename source=%s dest=%s source_parent=%s dest_parent=%s source_entry_id=0x%08x dest_entry_id=0x%08x renamed_entry_id=0x%08x verified\n",
|
|
source_path, dest_path, source_parent, dest_parent,
|
|
source_entry_id, dest_entry_id, lookup_entry_id);
|
|
|
|
ncp_close(conn);
|
|
return 0;
|
|
}
|