⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smblib-util.c

📁 一个功能非常全面的代理服务器源代码程序,
💻 C
📖 第 1 页 / 共 2 页
字号:
	Con_Handle->Encrypt_Key_Len = CVAL(SMB_Hdr(pkt), SMB_negrNTLM_ekl_offset);	p = (SMB_Hdr(pkt) + SMB_negrNTLM_buf_offset);	memcpy(Con_Handle->Encrypt_Key, p, 8);	p = (SMB_Hdr(pkt) + SMB_negrNTLM_buf_offset + Con_Handle->Encrypt_Key_Len);	strncpy(p, Con_Handle->Svr_PDom, sizeof(Con_Handle->Svr_PDom) - 1);	break;    default:#ifdef DEBUG	fprintf(stderr, "Unknown NegProt response format ... Ignored\n");	fprintf(stderr, "  wct = %i\n", CVAL(SMB_Hdr(pkt), SMB_hdr_wct_offset));#endif	break;    }#ifdef DEBUG    fprintf(stderr, "Protocol selected is: %i:%s\n", prot, Prots[prot]);#endif    RFCNB_Free_Pkt(pkt);    return (0);}/* Get our hostname */voidSMB_Get_My_Name(char *name, int len){    if (gethostname(name, len) < 0) {	/* Error getting name */	strncpy(name, "unknown", len);	/* Should check the error */#ifdef DEBUG	fprintf(stderr, "gethostname in SMB_Get_My_Name returned error:");	perror("");#endif    }    /* only keep the portion up to the first "." */}/* Send a TCON to the remote server ...               */SMB_Tree_HandleSMB_TreeConnect(SMB_Handle_Type Con_Handle,    SMB_Tree_Handle Tree_Handle,    char *path,    char *password,    char *device){    struct RFCNB_Pkt *pkt;    int param_len, pkt_len;    char *p;    SMB_Tree_Handle tree;    /* Figure out how much space is needed for path, password, dev ... */    if ((path == NULL) || (password == NULL) || (device == NULL)) {#ifdef DEBUG	fprintf(stderr, "Bad parameter passed to SMB_TreeConnect\n");#endif	SMBlib_errno = SMBlibE_BadParam;	return (NULL);    }    /* The + 2 is because of the \0 and the marker ...                    */    param_len = strlen(path) + 2 + strlen(password) + 2 + strlen(device) + 2;    /* The -1 accounts for the one byte smb_buf we have because some systems */    /* don't like char msg_buf[]                                             */    pkt_len = SMB_tcon_len + param_len;    pkt = (struct RFCNB_Pkt *) RFCNB_Alloc_Pkt(pkt_len);    if (pkt == NULL) {	SMBlib_errno = SMBlibE_NoSpace;	return (NULL);		/* Should handle the error */    }    /* Now allocate a tree for this to go into ... */    if (Tree_Handle == NULL) {	tree = (SMB_Tree_Handle) malloc(sizeof(struct SMB_Tree_Structure));	if (tree == NULL) {	    RFCNB_Free_Pkt(pkt);	    SMBlib_errno = SMBlibE_NoSpace;	    return (NULL);	}    } else {	tree = Tree_Handle;    }    tree->next = tree->prev = NULL;    tree->con = Con_Handle;    strncpy(tree->path, path, sizeof(tree->path));    strncpy(tree->device_type, device, sizeof(tree->device_type));    /* Now plug in the values ... */    memset(SMB_Hdr(pkt), 0, SMB_tcon_len);    SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF);	/* Plunk in IDF */    *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBtcon;    SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, Con_Handle->pid);    SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, 0);    SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, Con_Handle->mid);    SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, Con_Handle->uid);    *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 0;    SSVAL(SMB_Hdr(pkt), SMB_tcon_bcc_offset, param_len);    /* Now copy the param strings in with the right stuff */    p = (char *) (SMB_Hdr(pkt) + SMB_tcon_buf_offset);    *p = SMBasciiID;    strcpy(p + 1, path);    p = p + strlen(path) + 2;    *p = SMBasciiID;    strcpy(p + 1, password);    p = p + strlen(password) + 2;    *p = SMBasciiID;    strcpy(p + 1, device);    /* Now send the packet and sit back ... */    if (RFCNB_Send(Con_Handle->Trans_Connect, pkt, pkt_len) < 0) {#ifdef DEBUG	fprintf(stderr, "Error sending TCon request\n");#endif	if (Tree_Handle == NULL)	    free(tree);	RFCNB_Free_Pkt(pkt);	SMBlib_errno = -SMBlibE_SendFailed;	return (NULL);    }    /* Now get the response ... */    if (RFCNB_Recv(Con_Handle->Trans_Connect, pkt, pkt_len) < 0) {#ifdef DEBUG	fprintf(stderr, "Error receiving response to TCon\n");#endif	if (Tree_Handle == NULL)	    free(tree);	RFCNB_Free_Pkt(pkt);	SMBlib_errno = -SMBlibE_RecvFailed;	return (NULL);    }    /* Check out the response type ... */    if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) {	/* Process error */#ifdef DEBUG	fprintf(stderr, "SMB_TCon failed with errorclass = %i, Error Code = %i\n",	    CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset),	    SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset));#endif	if (Tree_Handle == NULL)	    free(tree);	SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset);	RFCNB_Free_Pkt(pkt);	SMBlib_errno = SMBlibE_Remote;	return (NULL);    }    tree->tid = SVAL(SMB_Hdr(pkt), SMB_tconr_tid_offset);    tree->mbs = SVAL(SMB_Hdr(pkt), SMB_tconr_mbs_offset);#ifdef DEBUG    fprintf(stderr, "TConn succeeded, with TID=%i, Max Xmit=%i\n",	tree->tid, tree->mbs);#endif    /* Now link the Tree to the Server Structure ... */    if (Con_Handle->first_tree == NULL) {	Con_Handle->first_tree = tree;	Con_Handle->last_tree = tree;    } else {	Con_Handle->last_tree->next = tree;	tree->prev = Con_Handle->last_tree;	Con_Handle->last_tree = tree;    }    RFCNB_Free_Pkt(pkt);    return (tree);}intSMB_TreeDisconnect(SMB_Tree_Handle Tree_Handle, BOOL discard){    struct RFCNB_Pkt *pkt;    int pkt_len;    pkt_len = SMB_tdis_len;    pkt = (struct RFCNB_Pkt *) RFCNB_Alloc_Pkt(pkt_len);    if (pkt == NULL) {	SMBlib_errno = SMBlibE_NoSpace;	return (SMBlibE_BAD);	/* Should handle the error */    }    /* Now plug in the values ... */    memset(SMB_Hdr(pkt), 0, SMB_tdis_len);    SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF);	/* Plunk in IDF */    *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBtdis;    SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, Tree_Handle->con->pid);    SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, Tree_Handle->con->mid);    SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, Tree_Handle->con->uid);    *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 0;    SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, Tree_Handle->tid);    SSVAL(SMB_Hdr(pkt), SMB_tcon_bcc_offset, 0);    /* Now send the packet and sit back ... */    if (RFCNB_Send(Tree_Handle->con->Trans_Connect, pkt, pkt_len) < 0) {#ifdef DEBUG	fprintf(stderr, "Error sending TDis request\n");#endif	RFCNB_Free_Pkt(pkt);	SMBlib_errno = -SMBlibE_SendFailed;	return (SMBlibE_BAD);    }    /* Now get the response ... */    if (RFCNB_Recv(Tree_Handle->con->Trans_Connect, pkt, pkt_len) < 0) {#ifdef DEBUG	fprintf(stderr, "Error receiving response to TCon\n");#endif	RFCNB_Free_Pkt(pkt);	SMBlib_errno = -SMBlibE_RecvFailed;	return (SMBlibE_BAD);    }    /* Check out the response type ... */    if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) {	/* Process error */#ifdef DEBUG	fprintf(stderr, "SMB_TDis failed with errorclass = %i, Error Code = %i\n",	    CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset),	    SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset));#endif	SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset);	RFCNB_Free_Pkt(pkt);	SMBlib_errno = SMBlibE_Remote;	return (SMBlibE_BAD);    }    Tree_Handle->tid = 0xFFFF;	/* Invalid TID */    Tree_Handle->mbs = 0;	/* Invalid     */#ifdef DEBUG    fprintf(stderr, "Tree disconnect successful ...\n");#endif    /* What about the tree handle ? */    if (discard == TRUE) {	/* Unlink it and free it ... */	if (Tree_Handle->next == NULL)	    Tree_Handle->con->first_tree = Tree_Handle->prev;	else	    Tree_Handle->next->prev = Tree_Handle->prev;	if (Tree_Handle->prev == NULL)	    Tree_Handle->con->last_tree = Tree_Handle->next;	else	    Tree_Handle->prev->next = Tree_Handle->next;    }    RFCNB_Free_Pkt(pkt);    return (0);}/* Pick up the last LMBlib error ... */intSMB_Get_Last_Error(){    return (SMBlib_errno);}/* Pick up the last error returned in an SMB packet          *//* We will need macros to extract error class and error code */intSMB_Get_Last_SMB_Err(){    return (SMBlib_SMB_Error);}/* Pick up the error message associated with an error from SMBlib  *//* Keep this table in sync with the message codes in smblib-common.h */static char *SMBlib_Error_Messages[] ={    "Request completed sucessfully.",    "Server returned a non-zero SMB Error Class and Code.",    "A lower layer protocol error occurred.",    "Function not yet implemented.",    "The protocol negotiated does not support the request.",    "No space available for operation.",    "One or more bad parameters passed.",    "None of the protocols we offered were accepted.",    "The attempt to send an SMB request failed. See protocol error info.",    "The attempt to get an SMB response failed. See protocol error info.",    "The logon request failed, but you were logged in as guest.",    "The attempt to call the remote server failed. See protocol error info.",    "The protocol dialect specified in a NegProt and accepted by the server is unknown.",  /* This next one simplifies error handling */    "No such error code.",    NULL};intSMB_Get_Error_Msg(int msg, char *msgbuf, int len){    if (msg >= 0) {	strncpy(msgbuf,	    SMBlib_Error_Messages[msg > SMBlibE_NoSuchMsg ? SMBlibE_NoSuchMsg : msg],	    len - 1);	msgbuf[len - 1] = 0;	/* Make sure it is a string */    } else {			/* Add the lower layer message ... */	char prot_msg[1024];	msg = -msg;		/* Make it positive */	strncpy(msgbuf,	    SMBlib_Error_Messages[msg > SMBlibE_NoSuchMsg ? SMBlibE_NoSuchMsg : msg],	    len - 1);	msgbuf[len - 1] = 0;	/* make sure it is a string */	if (strlen(msgbuf) < len) {	/* If there is space, put rest in */	    strncat(msgbuf, "\n\t", len - strlen(msgbuf));	    RFCNB_Get_Error(prot_msg, sizeof(prot_msg) - 1);	    strncat(msgbuf, prot_msg, len - strlen(msgbuf));	}    }    return 0;}

⌨️ 快捷键说明

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