📄 smbcomopen.c
字号:
#include "headers.h"static voidsmblogprintattr(int cmd, ushort attr){ if (attr & SMB_ATTR_READ_ONLY) smblogprint(cmd, " readonly"); if (attr & SMB_ATTR_HIDDEN) smblogprint(cmd, " hidden"); if (attr & SMB_ATTR_SYSTEM) smblogprint(cmd, " system"); if (attr & SMB_ATTR_DIRECTORY) smblogprint(cmd, " directory"); if (attr & SMB_ATTR_ARCHIVE) smblogprint(cmd, " archive");}static SmbFile *openfile(SmbSession *s, SmbTree *t, char *path, ushort mode, ushort attr, ushort ofun, ulong createoptions, uvlong createsize, ushort *fidp, Dir **dp, ushort *actionp){ int p9mode; int share; Dir *d = nil; int fd = -1; ushort action; SmbFile *f = nil; SmbSharedFile *sf = nil; char *fullpath = nil; int diropen = 0; p9mode = (mode >> SMB_OPEN_MODE_ACCESS_SHIFT) & SMB_OPEN_MODE_ACCESS_MASK; share = (mode >> SMB_OPEN_MODE_SHARE_SHIFT) & SMB_OPEN_MODE_SHARE_MASK; if (share == SMB_OPEN_MODE_SHARE_COMPATIBILITY) { badshare: smbseterror(s, ERRDOS, ERRbadshare); goto done; } smbstringprint(&fullpath, "%s%s", t->serv->path, path); d = dirstat(fullpath); if (d) { /* file exists */ int ofunexist; if (d->mode & DMDIR) { if (createoptions & SMB_CO_FILE) { smbseterror(s, ERRDOS, ERRnoaccess); goto done; } } else if (createoptions & SMB_CO_DIRECTORY) { smbseterror(s, ERRDOS, ERRnoaccess); goto done; } sf = smbsharedfileget(d, p9mode, &share); if (sf == nil) goto badshare; action = 1; ofunexist = (ofun >> SMB_OFUN_EXIST_SHIFT) & SMB_OFUN_EXIST_MASK; if (ofunexist == SMB_OFUN_EXIST_FAIL) { smbseterror(s, ERRDOS, ERRfilexists); goto done; } else if (ofunexist == SMB_OFUN_EXIST_TRUNCATE) { if ((d->mode & DMDIR) || (p9mode != OWRITE && p9mode != ORDWR)) { smbseterror(s, ERRDOS, ERRbadaccess); goto done; } p9mode |= OTRUNC; action = 3; } else if (ofunexist != SMB_OFUN_EXIST_OPEN) { smbseterror(s, ERRDOS, ERRbadaccess); goto done; } if (d->mode & DMDIR) diropen = 1; else fd = open(fullpath, p9mode); } else { /* file does not exist */ ulong p9attr; action = 3; if ((ofun & SMB_OFUN_NOEXIST_CREATE) == 0) { smbseterror(s, ERRDOS, ERRbadfile); goto done; } if (createsize != 0) { smbseterror(s, ERRDOS, ERRunsup); goto done; }//smblogprint(-1, "creating: attr 0x%.4ux co 0x%.8lux\n", attr, createoptions); if (createoptions & SMB_CO_FILE) { attr &= SMB_ATTR_DIRECTORY; if (attr == 0) attr = SMB_ATTR_NORMAL; } else if (createoptions & SMB_CO_DIRECTORY) { attr &= ~SMB_ATTR_NORMAL; attr |= SMB_ATTR_DIRECTORY; }//smblogprint(-1, "creating: before conversion attr 0x%.4ux\n", attr); p9attr = smbdosattr2plan9mode(attr);//smblogprint(-1, "creating: after conversion p9attr 0%.uo\n", p9attr); fd = create(fullpath, p9mode, p9attr); if (fd >= 0) { d = dirfstat(fd); sf = smbsharedfileget(d, p9mode, &share); if (sf == nil) { close(fd); remove(path); goto badshare; } } } if (!diropen && fd < 0) { smbseterror(s, ERRSRV, ERRaccess); goto done; } f = smbemalloc(sizeof(SmbFile)); if (diropen) { f->ioallowed = 0; f->fd = -1; } else { f->ioallowed = 1; f->fd = fd; } f->name = smbestrdup(path); f->sf = sf; sf = nil; f->share = share; f->p9mode = p9mode; f->t = t; if (s->fidmap == nil) s->fidmap = smbidmapnew(); *fidp = smbidmapadd(s->fidmap, f); smblogprintif(smbglobals.log.fids, "openfile: 0x%.4ux/0x%.4ux %s\n", t->id, *fidp, path); if (actionp) *actionp = action; if (dp) { *dp = d; d = nil; }done: if (sf) smbsharedfileput(nil, sf, share); free(d); free(fullpath); return f;}SmbProcessResultsmbcomopenandx(SmbSession *s, SmbHeader *h, uchar *pdata, SmbBuffer *b){ uchar andxcommand; ushort andxoffset, flags, mode, sattr, attr; ulong createtime; ushort ofun; ulong createsize, timeout; char *path = nil; ulong andxoffsetfixupoffset; SmbProcessResult pr; ushort action; Dir *d = nil; SmbFile *f; SmbTree *t; ushort fid; if (!smbcheckwordcount("comopenandx", h, 15)) return SmbProcessResultFormat; andxcommand = *pdata++; pdata++; andxoffset = smbnhgets(pdata); pdata += 2; flags = smbnhgets(pdata); pdata += 2; mode = smbnhgets(pdata); pdata += 2; sattr = smbnhgets(pdata); pdata += 2; attr = smbnhgets(pdata); pdata += 2; createtime = smbnhgetl(pdata); pdata += 4; ofun = smbnhgets(pdata); pdata += 2; createsize = smbnhgetl(pdata); pdata += 4; timeout = smbnhgetl(pdata); pdata += 4; pdata += 4; USED(pdata); if (!smbbuffergetstring(b, h, SMB_STRING_PATH, &path)) { pr = SmbProcessResultFormat; goto done; } smbloglock(); smblogprint(h->command, "flags 0x%.4ux", flags); if (flags & SMB_OPEN_FLAGS_ADDITIONAL) smblogprint(h->command, " additional"); if (flags & SMB_OPEN_FLAGS_OPLOCK) smblogprint(h->command, " oplock"); if (flags & SMB_OPEN_FLAGS_OPBATCH) smblogprint(h->command, " opbatch"); smblogprint(h->command, "\n"); smblogprint(h->command, "mode 0x%.4ux", mode); switch ((mode >> SMB_OPEN_MODE_ACCESS_SHIFT) & SMB_OPEN_MODE_ACCESS_MASK) { case OREAD: smblogprint(h->command, " OREAD"); break; case OWRITE: smblogprint(h->command, " OWRITE"); break; case ORDWR: smblogprint(h->command, " ORDWR"); break; case OEXEC: smblogprint(h->command, " OEXEC"); break; } switch ((mode >> SMB_OPEN_MODE_SHARE_SHIFT) & SMB_OPEN_MODE_SHARE_MASK) { case SMB_OPEN_MODE_SHARE_COMPATIBILITY: smblogprint(h->command, " compatinility"); break; case SMB_OPEN_MODE_SHARE_EXCLUSIVE: smblogprint(h->command, " exclusive"); break; case SMB_OPEN_MODE_SHARE_DENY_WRITE: smblogprint(h->command, " deny write"); break; case SMB_OPEN_MODE_SHARE_DENY_READOREXEC: smblogprint(h->command, " deny readorxec"); break; case SMB_OPEN_MODE_SHARE_DENY_NONE: smblogprint(h->command, " deny none"); break; } if (mode & SMB_OPEN_MODE_WRITE_THROUGH) smblogprint(h->command, " write through"); smblogprint(h->command, "\n"); smblogprint(h->command, "sattr 0x%.4ux", sattr); smblogprintattr(h->command, sattr); smblogprint(h->command, "\n"); smblogprint(h->command, "attr 0x%.4ux", attr); smblogprintattr(h->command, attr); smblogprint(h->command, "\n"); smblogprint(h->command, "createtime 0x%.8lux\n", createtime); smblogprint(h->command, "ofun 0x%.4ux", ofun); if (ofun & SMB_OFUN_NOEXIST_CREATE) smblogprint(h->command, " noexistscreate"); else smblogprint(h->command, " noexistfail"); switch ((ofun >> SMB_OFUN_EXIST_SHIFT) & SMB_OFUN_EXIST_MASK) { case SMB_OFUN_EXIST_FAIL: smblogprint(h->command, " existfail"); break; case SMB_OFUN_EXIST_OPEN: smblogprint(h->command, " existopen"); break; case SMB_OFUN_EXIST_TRUNCATE: smblogprint(h->command, " existtruncate"); break; } smblogprint(h->command, "\n"); smblogprint(h->command, "createsize 0x%.8lux\n", createsize); smblogprint(h->command, "timeout 0x%.8lux\n", timeout); smblogprint(h->command, "path %s\n", path); smblogunlock(); t = smbidmapfind(s->tidmap, h->tid); if (t == nil) { smbseterror(s, ERRSRV, ERRinvtid); goto errordone; } f = openfile(s, t, path, mode, attr, ofun, 0, createsize, &fid, &d, &action); if (f == nil) { pr = SmbProcessResultError; goto done; } h->wordcount = 15; if (!smbbufferputandxheader(s->response, h, &s->peerinfo, andxcommand, &andxoffsetfixupoffset) || !smbbufferputs(s->response, fid) || !smbbufferputs(s->response, smbplan9mode2dosattr(d->mode)) || !smbbufferputl(s->response, smbplan9time2utime(d->mtime, s->tzoff)) || !smbbufferputl(s->response, smbplan9length2size32(d->length)) || !smbbufferputs(s->response, smbplan9mode2dosattr(d->mode)) // probbaly bogus || !smbbufferputs(s->response, 0) // all files are files || !smbbufferputs(s->response, 0) // pipe state || !smbbufferputs(s->response, action) || !smbbufferputl(s->response, 0) // fileID || !smbbufferputs(s->response, 0) || !smbbufferputs(s->response, 0)) { // bytecount 0 smbfileclose(s, f); pr = SmbProcessResultMisc; goto done; } if (andxcommand != SMB_COM_NO_ANDX_COMMAND) pr = smbchaincommand(s, h, andxoffsetfixupoffset, andxcommand, andxoffset, b); else pr = SmbProcessResultReply; goto done; errordone: pr = SmbProcessResultError;done: free(path); free(d); return pr;}SmbProcessResultsmbcomopen(SmbSession *s, SmbHeader *h, uchar *pdata, SmbBuffer *b){ uchar fmt;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -