/* * Linux smoke test for NetWare AFP Create File. * * The helper sends the WebSDK/nwafp.h AFP Create File requests through * ncpfs/libncp and verifies that the returned AFP file ID matches a * subsequent AFP Entry ID From Path Name lookup for the created file. */ #include #include #include #include #include #include #include #include #ifndef NCPC_SUBFUNCTION #define NCPC_SUBFUNCTION 0x10000 #endif #ifndef NCPC_SFN #define NCPC_SFN(FN, SFN) ((FN) | ((SFN) << 8) | NCPC_SUBFUNCTION) #endif #define AFP_CREATE_FILE 0x02 #define AFP20_CREATE_FILE 0x0e #define AFP_GET_ENTRY_ID_FROM_PATH 0x0c #define NWE_INVALID_NAMESPACE 0xbf #define NWE_INVALID_PATH 0x9c #define NWE_FILE_EXISTS 0xff #ifndef NWE_INVALID_NCP_PACKET_LENGTH #define NWE_INVALID_NCP_PACKET_LENGTH 0x7e #endif static void usage(const char *prog) { fprintf(stderr, "Usage: %s [--afp20] [--delete-existing] [--expect-completion CODE] [--volume N] " "[--entry-id ID] [--type FOURCC] [--creator FOURCC] " "[ncpfs options] PATH\n" "\n" "ncpfs options are parsed by ncp_initialize(), for example:\n" " -S SERVER -U USER -P PASSWORD -n\n" "\n" "Examples:\n" " %s -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/AFPFILE\n" " %s --afp20 -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/AFPFILE2\n", prog, 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; /* directory handle 0, raw VOL:path */ 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 *path = NULL; uint32_t volume_number = 0; uint32_t base_entry_id = 0; uint32_t expect_completion = 0xffffffffU; int afp20 = 0; int delete_existing = 0; const char *finder_type = "TEXT"; const char *finder_creator = "MARS"; int i; char parent[256]; char leaf[256]; size_t leaf_len; uint8_t request[1 + 4 + 1 + 32 + 6 + 1 + 255]; size_t fixed_len; uint8_t reply_buf[4]; uint32_t created_entry_id; uint32_t lookup_entry_id; 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], "--afp20")) { afp20 = 1; } else if (!strcmp(argv[i], "--delete-existing")) { delete_existing = 1; } else 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], "--entry-id")) { if (++i >= argc || parse_u32(argv[i], &base_entry_id)) { fprintf(stderr, "invalid --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], "--type")) { if (++i >= argc || strlen(argv[i]) != 4) { fprintf(stderr, "--type must be exactly four characters\n"); ncp_close(conn); return 2; } finder_type = argv[i]; } else if (!strcmp(argv[i], "--creator")) { if (++i >= argc || strlen(argv[i]) != 4) { fprintf(stderr, "--creator must be exactly four characters\n"); ncp_close(conn); return 2; } finder_creator = argv[i]; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { usage(argv[0]); ncp_close(conn); return 0; } else if (!path) { path = argv[i]; } else { fprintf(stderr, "unexpected argument: %s\n", argv[i]); usage(argv[0]); ncp_close(conn); return 2; } } if (!path) { fprintf(stderr, "missing PATH\n"); usage(argv[0]); ncp_close(conn); return 2; } if (split_parent_leaf(path, parent, sizeof(parent), leaf, sizeof(leaf))) { fprintf(stderr, "PATH must include a parent and new file name: %s\n", path); ncp_close(conn); return 2; } leaf_len = strlen(leaf); if (leaf_len > 255) { fprintf(stderr, "file name is too long for AFP Create File: %zu\n", leaf_len); ncp_close(conn); return 2; } if (base_entry_id == 0) { err = afp_get_entry_id_from_path(conn, parent, &base_entry_id); if (err) { fprintf(stderr, "AFP parent Entry ID lookup failed for %s: 0x%04x\n", parent, (unsigned int)err); ncp_close(conn); return 1; } } memset(request, 0, sizeof(request)); request[0] = (uint8_t)volume_number; cpu_to_be32(base_entry_id, request + 1); request[5] = delete_existing ? 1 : 0; /* DeleteExistingFileFlag */ memcpy(request + 6, finder_type, 4); memcpy(request + 10, finder_creator, 4); if (afp20) { fixed_len = 44; /* volume..ProDOSInfo */ request[44] = (uint8_t)leaf_len; memcpy(request + 45, leaf, leaf_len); } else { fixed_len = 38; /* volume..FinderInfo */ request[38] = (uint8_t)leaf_len; memcpy(request + 39, leaf, 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, afp20 ? AFP20_CREATE_FILE : AFP_CREATE_FILE), request, fixed_len + 1 + leaf_len, &reply); if (expect_completion != 0xffffffffU) { if (((unsigned int)err & 0xff) == expect_completion) { printf("AFP Create File returned expected completion 0x%02x: " "subfunction=0x%02x path=%s parent_entry_id=0x%08x\n", (unsigned int)expect_completion, afp20 ? AFP20_CREATE_FILE : AFP_CREATE_FILE, path, base_entry_id); ncp_close(conn); return 0; } fprintf(stderr, "expected completion 0x%02x but got 0x%04x\n", (unsigned int)expect_completion, (unsigned int)err); ncp_close(conn); return 1; } if (err) { fprintf(stderr, "AFP Create File failed: subfunction=0x%02x " "path=%s parent_entry_id=0x%08x error=0x%04x\n", afp20 ? AFP20_CREATE_FILE : AFP_CREATE_FILE, path, base_entry_id, (unsigned int)err); ncp_close(conn); return 1; } if (reply.fragSize < 4) { fprintf(stderr, "AFP Create File reply too short: %u\n", (unsigned int)reply.fragSize); ncp_close(conn); return 1; } created_entry_id = be32_to_cpu(reply_buf); if (!created_entry_id) { fprintf(stderr, "AFP Create File returned entry_id 0\n"); ncp_close(conn); return 1; } err = afp_get_entry_id_from_path(conn, path, &lookup_entry_id); if (err) { fprintf(stderr, "AFP Entry ID lookup for created file failed: " "path=%s error=0x%04x\n", path, (unsigned int)err); ncp_close(conn); return 1; } if (created_entry_id != lookup_entry_id) { fprintf(stderr, "AFP Create File entry mismatch: created=0x%08x " "lookup=0x%08x path=%s\n", created_entry_id, lookup_entry_id, path); ncp_close(conn); return 1; } printf("AFP Create File subfunction=0x%02x path=%s parent=%s " "leaf=%s parent_entry_id=0x%08x entry_id=0x%08x verified\n", afp20 ? AFP20_CREATE_FILE : AFP_CREATE_FILE, path, parent, leaf, base_entry_id, created_entry_id); ncp_close(conn); return 0; }