smbutil.c

来自「TCPDUMP的C语言源代码,是在数据链路层的应用」· C语言 代码 · 共 1,890 行 · 第 1/5 页

C
1,890
字号
	    }	    if (t != 0) {		lt = localtime(&t);		if (lt != NULL)		    tstring = asctime(lt);		else		    tstring = "(Can't convert time)\n";	    } else		tstring = "NULL\n";	    printf("%s", tstring);	    fmt++;	    while (isdigit((unsigned char)*fmt))		fmt++;	    break;	  }	default:	    putchar(*fmt);	    fmt++;	    break;	}    }    if (buf >= maxbuf && *fmt)	printf("END OF BUFFER\n");    return(buf);trunc:    printf("\n");    printf("WARNING: Short packet. Try increasing the snap length\n");    return(NULL);}const u_char *smb_fdata(const u_char *buf, const char *fmt, const u_char *maxbuf,    int unicodestr){    static int depth = 0;    char s[128];    char *p;    while (*fmt) {	switch (*fmt) {	case '*':	    fmt++;	    while (buf < maxbuf) {		const u_char *buf2;		depth++;		buf2 = smb_fdata(buf, fmt, maxbuf, unicodestr);		depth--;		if (buf2 == NULL)		    return(NULL);		if (buf2 == buf)		    return(buf);		buf = buf2;	    }	    return(buf);	case '|':	    fmt++;	    if (buf >= maxbuf)		return(buf);	    break;	case '%':	    fmt++;	    buf = maxbuf;	    break;	case '#':	    fmt++;	    return(buf);	    break;	case '[':	    fmt++;	    if (buf >= maxbuf)		return(buf);	    memset(s, 0, sizeof(s));	    p = strchr(fmt, ']');	    if ((size_t)(p - fmt + 1) > sizeof(s)) {		/* overrun */		return(buf);	    }	    strncpy(s, fmt, p - fmt);	    s[p - fmt] = '\0';	    fmt = p + 1;	    buf = smb_fdata1(buf, s, maxbuf, unicodestr);	    if (buf == NULL)		return(NULL);	    break;	default:	    putchar(*fmt);	    fmt++;	    fflush(stdout);	    break;	}    }    if (!depth && buf < maxbuf) {	size_t len = PTR_DIFF(maxbuf, buf);	printf("Data: (%lu bytes)\n", (unsigned long)len);	print_data(buf, len);	return(buf + len);    }    return(buf);}typedef struct {    const char *name;    int code;    const char *message;} err_code_struct;/* DOS Error Messages */static const err_code_struct dos_msgs[] = {    { "ERRbadfunc", 1, "Invalid function." },    { "ERRbadfile", 2, "File not found." },    { "ERRbadpath", 3, "Directory invalid." },    { "ERRnofids", 4, "No file descriptors available" },    { "ERRnoaccess", 5, "Access denied." },    { "ERRbadfid", 6, "Invalid file handle." },    { "ERRbadmcb", 7, "Memory control blocks destroyed." },    { "ERRnomem", 8, "Insufficient server memory to perform the requested function." },    { "ERRbadmem", 9, "Invalid memory block address." },    { "ERRbadenv", 10, "Invalid environment." },    { "ERRbadformat", 11, "Invalid format." },    { "ERRbadaccess", 12, "Invalid open mode." },    { "ERRbaddata", 13, "Invalid data." },    { "ERR", 14, "reserved." },    { "ERRbaddrive", 15, "Invalid drive specified." },    { "ERRremcd", 16, "A Delete Directory request attempted to remove the server's current directory." },    { "ERRdiffdevice", 17, "Not same device." },    { "ERRnofiles", 18, "A File Search command can find no more files matching the specified criteria." },    { "ERRbadshare", 32, "The sharing mode specified for an Open conflicts with existing FIDs on the file." },    { "ERRlock", 33, "A Lock request conflicted with an existing lock or specified an invalid mode, or an Unlock requested attempted to remove a lock held by another process." },    { "ERRfilexists", 80, "The file named in a Create Directory, Make New File or Link request already exists." },    { "ERRbadpipe", 230, "Pipe invalid." },    { "ERRpipebusy", 231, "All instances of the requested pipe are busy." },    { "ERRpipeclosing", 232, "Pipe close in progress." },    { "ERRnotconnected", 233, "No process on other end of pipe." },    { "ERRmoredata", 234, "There is more data to be returned." },    { NULL, -1, NULL } };/* Server Error Messages */err_code_struct server_msgs[] = {    { "ERRerror", 1, "Non-specific error code." },    { "ERRbadpw", 2, "Bad password - name/password pair in a Tree Connect or Session Setup are invalid." },    { "ERRbadtype", 3, "reserved." },    { "ERRaccess", 4, "The requester does not have the necessary access rights within the specified context for the requested function. The context is defined by the TID or the UID." },    { "ERRinvnid", 5, "The tree ID (TID) specified in a command was invalid." },    { "ERRinvnetname", 6, "Invalid network name in tree connect." },    { "ERRinvdevice", 7, "Invalid device - printer request made to non-printer connection or non-printer request made to printer connection." },    { "ERRqfull", 49, "Print queue full (files) -- returned by open print file." },    { "ERRqtoobig", 50, "Print queue full -- no space." },    { "ERRqeof", 51, "EOF on print queue dump." },    { "ERRinvpfid", 52, "Invalid print file FID." },    { "ERRsmbcmd", 64, "The server did not recognize the command received." },    { "ERRsrverror", 65, "The server encountered an internal error, e.g., system file unavailable." },    { "ERRfilespecs", 67, "The file handle (FID) and pathname parameters contained an invalid combination of values." },    { "ERRreserved", 68, "reserved." },    { "ERRbadpermits", 69, "The access permissions specified for a file or directory are not a valid combination.  The server cannot set the requested attribute." },    { "ERRreserved", 70, "reserved." },    { "ERRsetattrmode", 71, "The attribute mode in the Set File Attribute request is invalid." },    { "ERRpaused", 81, "Server is paused." },    { "ERRmsgoff", 82, "Not receiving messages." },    { "ERRnoroom", 83, "No room to buffer message." },    { "ERRrmuns", 87, "Too many remote user names." },    { "ERRtimeout", 88, "Operation timed out." },    { "ERRnoresource", 89, "No resources currently available for request." },    { "ERRtoomanyuids", 90, "Too many UIDs active on this session." },    { "ERRbaduid", 91, "The UID is not known as a valid ID on this session." },    { "ERRusempx", 250, "Temp unable to support Raw, use MPX mode." },    { "ERRusestd", 251, "Temp unable to support Raw, use standard read/write." },    { "ERRcontmpx", 252, "Continue in MPX mode." },    { "ERRreserved", 253, "reserved." },    { "ERRreserved", 254, "reserved." },    { "ERRnosupport", 0xFFFF, "Function not supported." },    { NULL, -1, NULL }};/* Hard Error Messages */err_code_struct hard_msgs[] = {    { "ERRnowrite", 19, "Attempt to write on write-protected diskette." },    { "ERRbadunit", 20, "Unknown unit." },    { "ERRnotready", 21, "Drive not ready." },    { "ERRbadcmd", 22, "Unknown command." },    { "ERRdata", 23, "Data error (CRC)." },    { "ERRbadreq", 24, "Bad request structure length." },    { "ERRseek", 25 , "Seek error." },    { "ERRbadmedia", 26, "Unknown media type." },    { "ERRbadsector", 27, "Sector not found." },    { "ERRnopaper", 28, "Printer out of paper." },    { "ERRwrite", 29, "Write fault." },    { "ERRread", 30, "Read fault." },    { "ERRgeneral", 31, "General failure." },    { "ERRbadshare", 32, "A open conflicts with an existing open." },    { "ERRlock", 33, "A Lock request conflicted with an existing lock or specified an invalid mode, or an Unlock requested attempted to remove a lock held by another process." },    { "ERRwrongdisk", 34, "The wrong disk was found in a drive." },    { "ERRFCBUnavail", 35, "No FCBs are available to process request." },    { "ERRsharebufexc", 36, "A sharing buffer has been exceeded." },    { NULL, -1, NULL }};static const struct {    int code;    const char *class;    const err_code_struct *err_msgs;} err_classes[] = {    { 0, "SUCCESS", NULL },    { 0x01, "ERRDOS", dos_msgs },    { 0x02, "ERRSRV", server_msgs },    { 0x03, "ERRHRD", hard_msgs },    { 0x04, "ERRXOS", NULL },    { 0xE1, "ERRRMX1", NULL },    { 0xE2, "ERRRMX2", NULL },    { 0xE3, "ERRRMX3", NULL },    { 0xFF, "ERRCMD", NULL },    { -1, NULL, NULL }};/* * return a SMB error string from a SMB buffer */char *smb_errstr(int class, int num){    static char ret[128];    int i, j;    ret[0] = 0;    for (i = 0; err_classes[i].class; i++)	if (err_classes[i].code == class) {	    if (err_classes[i].err_msgs) {		const err_code_struct *err = err_classes[i].err_msgs;		for (j = 0; err[j].name; j++)		    if (num == err[j].code) {			snprintf(ret, sizeof(ret), "%s - %s (%s)",			    err_classes[i].class, err[j].name, err[j].message);			return ret;		    }	    }	    snprintf(ret, sizeof(ret), "%s - %d", err_classes[i].class, num);	    return ret;	}    snprintf(ret, sizeof(ret), "ERROR: Unknown error (%d,%d)", class, num);    return(ret);}typedef struct {    u_int32_t code;    const char *name;} nt_err_code_struct;/* * NT Error codes */static const nt_err_code_struct nt_errors[] = {  { 0x00000000, "STATUS_SUCCESS" },  { 0x00000000, "STATUS_WAIT_0" },  { 0x00000001, "STATUS_WAIT_1" },  { 0x00000002, "STATUS_WAIT_2" },  { 0x00000003, "STATUS_WAIT_3" },  { 0x0000003F, "STATUS_WAIT_63" },  { 0x00000080, "STATUS_ABANDONED" },  { 0x00000080, "STATUS_ABANDONED_WAIT_0" },  { 0x000000BF, "STATUS_ABANDONED_WAIT_63" },  { 0x000000C0, "STATUS_USER_APC" },  { 0x00000100, "STATUS_KERNEL_APC" },  { 0x00000101, "STATUS_ALERTED" },  { 0x00000102, "STATUS_TIMEOUT" },  { 0x00000103, "STATUS_PENDING" },  { 0x00000104, "STATUS_REPARSE" },  { 0x00000105, "STATUS_MORE_ENTRIES" },  { 0x00000106, "STATUS_NOT_ALL_ASSIGNED" },  { 0x00000107, "STATUS_SOME_NOT_MAPPED" },  { 0x00000108, "STATUS_OPLOCK_BREAK_IN_PROGRESS" },  { 0x00000109, "STATUS_VOLUME_MOUNTED" },  { 0x0000010A, "STATUS_RXACT_COMMITTED" },  { 0x0000010B, "STATUS_NOTIFY_CLEANUP" },  { 0x0000010C, "STATUS_NOTIFY_ENUM_DIR" },  { 0x0000010D, "STATUS_NO_QUOTAS_FOR_ACCOUNT" },  { 0x0000010E, "STATUS_PRIMARY_TRANSPORT_CONNECT_FAILED" },  { 0x00000110, "STATUS_PAGE_FAULT_TRANSITION" },  { 0x00000111, "STATUS_PAGE_FAULT_DEMAND_ZERO" },  { 0x00000112, "STATUS_PAGE_FAULT_COPY_ON_WRITE" },  { 0x00000113, "STATUS_PAGE_FAULT_GUARD_PAGE" },  { 0x00000114, "STATUS_PAGE_FAULT_PAGING_FILE" },  { 0x00000115, "STATUS_CACHE_PAGE_LOCKED" },  { 0x00000116, "STATUS_CRASH_DUMP" },  { 0x00000117, "STATUS_BUFFER_ALL_ZEROS" },  { 0x00000118, "STATUS_REPARSE_OBJECT" },  { 0x0000045C, "STATUS_NO_SHUTDOWN_IN_PROGRESS" },  { 0x40000000, "STATUS_OBJECT_NAME_EXISTS" },  { 0x40000001, "STATUS_THREAD_WAS_SUSPENDED" },  { 0x40000002, "STATUS_WORKING_SET_LIMIT_RANGE" },  { 0x40000003, "STATUS_IMAGE_NOT_AT_BASE" },  { 0x40000004, "STATUS_RXACT_STATE_CREATED" },  { 0x40000005, "STATUS_SEGMENT_NOTIFICATION" },  { 0x40000006, "STATUS_LOCAL_USER_SESSION_KEY" },  { 0x40000007, "STATUS_BAD_CURRENT_DIRECTORY" },  { 0x40000008, "STATUS_SERIAL_MORE_WRITES" },  { 0x40000009, "STATUS_REGISTRY_RECOVERED" },  { 0x4000000A, "STATUS_FT_READ_RECOVERY_FROM_BACKUP" },  { 0x4000000B, "STATUS_FT_WRITE_RECOVERY" },  { 0x4000000C, "STATUS_SERIAL_COUNTER_TIMEOUT" },  { 0x4000000D, "STATUS_NULL_LM_PASSWORD" },  { 0x4000000E, "STATUS_IMAGE_MACHINE_TYPE_MISMATCH" },  { 0x4000000F, "STATUS_RECEIVE_PARTIAL" },  { 0x40000010, "STATUS_RECEIVE_EXPEDITED" },  { 0x40000011, "STATUS_RECEIVE_PARTIAL_EXPEDITED" },  { 0x40000012, "STATUS_EVENT_DONE" },  { 0x40000013, "STATUS_EVENT_PENDING" },  { 0x40000014, "STATUS_CHECKING_FILE_SYSTEM" },  { 0x40000015, "STATUS_FATAL_APP_EXIT" },  { 0x40000016, "STATUS_PREDEFINED_HANDLE" },  { 0x40000017, "STATUS_WAS_UNLOCKED" },  { 0x40000018, "STATUS_SERVICE_NOTIFICATION" },  { 0x40000019, "STATUS_WAS_LOCKED" },  { 0x4000001A, "STATUS_LOG_HARD_ERROR" },  { 0x4000001B, "STATUS_ALREADY_WIN32" },  { 0x4000001C, "STATUS_WX86_UNSIMULATE" },  { 0x4000001D, "STATUS_WX86_CONTINUE" },  { 0x4000001E, "STATUS_WX86_SINGLE_STEP" },  { 0x4000001F, "STATUS_WX86_BREAKPOINT" },  { 0x40000020, "STATUS_WX86_EXCEPTION_CONTINUE" },  { 0x40000021, "STATUS_WX86_EXCEPTION_LASTCHANCE" },  { 0x40000022, "STATUS_WX86_EXCEPTION_CHAIN" },  { 0x40000023, "STATUS_IMAGE_MACHINE_TYPE_MISMATCH_EXE" },  { 0x40000024, "STATUS_NO_YIELD_PERFORMED" },  { 0x40000025, "STATUS_TIMER_RESUME_IGNORED" },  { 0x80000001, "STATUS_GUARD_PAGE_VIOLATION" },  { 0x80000002, "STATUS_DATATYPE_MISALIGNMENT" },  { 0x80000003, "STATUS_BREAKPOINT" },  { 0x80000004, "STATUS_SINGLE_STEP" },  { 0x80000005, "STATUS_BUFFER_OVERFLOW" },  { 0x80000006, "STATUS_NO_MORE_FILES" },  { 0x80000007, "STATUS_WAKE_SYSTEM_DEBUGGER" },  { 0x8000000A, "STATUS_HANDLES_CLOSED" },  { 0x8000000B, "STATUS_NO_INHERITANCE" },  { 0x8000000C, "STATUS_GUID_SUBSTITUTION_MADE" },  { 0x8000000D, "STATUS_PARTIAL_COPY" },  { 0x8000000E, "STATUS_DEVICE_PAPER_EMPTY" },  { 0x8000000F, "STATUS_DEVICE_POWERED_OFF" },  { 0x80000010, "STATUS_DEVICE_OFF_LINE" },  { 0x80000011, "STATUS_DEVICE_BUSY" },  { 0x80000012, "STATUS_NO_MORE_EAS" },  { 0x80000013, "STATUS_INVALID_EA_NAME" },  { 0x80000014, "STATUS_EA_LIST_INCONSISTENT" },  { 0x80000015, "STATUS_INVALID_EA_FLAG" },  { 0x80000016, "STATUS_VERIFY_REQUIRED" },  { 0x80000017, "STATUS_EXTRANEOUS_INFORMATION" },  { 0x80000018, "STATUS_RXACT_COMMIT_NECESSARY" },  { 0x8000001A, "STATUS_NO_MORE_ENTRIES" },  { 0x8000001B, "STATUS_FILEMARK_DETECTED" },  { 0x8000001C, "STATUS_MEDIA_CHANGED" },  { 0x8000001D, "STATUS_BUS_RESET" },  { 0x8000001E, "STATUS_END_OF_MEDIA" },  { 0x8000001F, "STATUS_BEGINNING_OF_MEDIA" },  { 0x80000020, "STATUS_MEDIA_CHECK" },  { 0x80000021, "STATUS_SETMARK_DETECTED" },  { 0x80000022, "STATUS_NO_DATA_DETECTED" },  { 0x80000023, "STATUS_REDIRECTOR_HAS_OPEN_HANDLES" },  { 0x80000024, "STATUS_SERVER_HAS_OPEN_HANDLES" },  { 0x80000025, "STATUS_ALREADY_DISCONNECTED" },  { 0x80000026, "STATUS_LONGJUMP" },  { 0x80040111, "MAPI_E_LOGON_FAILED" },  { 0x80090300, "SEC_E_INSUFFICIENT_MEMORY" },  { 0x80090301, "SEC_E_INVALID_HANDLE" },  { 0x80090302, "SEC_E_UNSUPPORTED_FUNCTION" },  { 0x8009030B, "SEC_E_NO_IMPERSONATION" },  { 0x8009030D, "SEC_E_UNKNOWN_CREDENTIALS" },  { 0x8009030E, "SEC_E_NO_CREDENTIALS" },  { 0x8009030F, "SEC_E_MESSAGE_ALTERED" },

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?