]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/cifs/transnt.c
dc: fix off by one in stack overflow check (thanks BurnZeZ)
[plan9front.git] / sys / src / cmd / cifs / transnt.c
1 #include <u.h>
2 #include <libc.h>
3 #include <fcall.h>
4 #include <thread.h>
5 #include <9p.h>
6 #include "cifs.h"
7
8 static Pkt *
9 tnthdr(Session *s, Share *sp, int cmd)
10 {
11         Pkt *p;
12
13         p = cifshdr(s, sp, SMB_COM_NT_TRANSACT);
14         p->tbase = p8(p, 0);            /*  0  Max setup count to return */
15         pl16(p, 0);                     /*  1  reserved */
16         pl32(p, 0);                     /*  3  Total parameter count */
17         pl32(p, 0);                     /*  7  Total data count */
18         pl32(p, 64);                    /* 11  Max parameter count to return */
19         pl32(p, (MTU - T2HDRLEN)-64);   /* 15  Max data count to return */
20         pl32(p, 0);                     /* 19  Parameter count (in this buffer) */
21         pl32(p, 0);                     /* 23  Offset to parameters (in this buffer) */
22         pl32(p, 0);                     /* 27  Count of data  in this buffer */
23         pl32(p, 0);                     /* 31  Offset to data in this buffer */
24         p8(p, 1);                       /* 35  Count of setup words */
25         pl16(p, cmd);                   /* 37  setup[0] */
26         pl16(p, 0);                     /* padding ??!?!? */
27         pbytes(p);
28         return p;
29 }
30
31 static void
32 ptntparam(Pkt *p)
33 {
34         uchar *pos = p->pos;
35         assert(p->tbase != 0);
36
37         p->pos = p->tbase +23;
38         pl32(p, (pos - p->buf) - NBHDRLEN); /* param offset */
39
40         p->tparam = p->pos = pos;
41 }
42
43 static void
44 ptntdata(Pkt *p)
45 {
46         uchar *pos = p->pos;
47         assert(p->tbase != 0);
48         assert(p->tparam != 0);
49
50         p->pos = p->tbase +3;
51         pl32(p, pos - p->tparam);               /* total param count */
52
53         p->pos = p->tbase +19;
54         pl32(p, pos - p->tparam);               /* param count */
55
56         p->pos = p->tbase +31;
57         pl32(p, (pos - p->buf) - NBHDRLEN);     /* data offset */
58         p->tdata = p->pos = pos;
59 }
60
61 static int
62 tntrpc(Pkt *p)
63 {
64         int got;
65         uchar *pos;
66         assert(p->tbase != 0);
67         assert(p->tdata != 0);
68
69         pos = p->pos;
70
71         p->pos = p->tbase +7;
72         pl32(p, pos - p->tdata);                /* total data count */
73
74         p->pos = p->tbase +27;
75         pl32(p, pos - p->tdata);                /* data count */
76
77         p->pos = pos;
78         if((got = cifsrpc(p)) == -1)
79                 return -1;
80
81         g8(p);                          /* Reserved */
82         g8(p);                          /* Reserved */
83         g8(p);                          /* Reserved */
84         gl32(p);                        /* Total parameter count */
85         gl32(p);                        /* Total data count */
86         gl32(p);                        /* Parameter count in this buffer */
87         p->tparam = p->buf +NBHDRLEN +gl32(p); /* Parameter offset */
88         gl32(p);                        /* Parameter displacement */
89         gl32(p);                        /* Data count (this buffer); */
90         p->tdata = p->buf +NBHDRLEN +gl32(p); /* Data offset */
91         gl32(p);                        /* Data displacement */
92         g8(p);                          /* Setup count */
93         gl16(p);                        /* padding ???  */
94
95         return got;
96 }
97
98 static void
99 gtntdata(Pkt *p)
100 {
101         p->pos = p->tdata;
102 }
103
104
105 int
106 TNTquerysecurity(Session *s, Share *sp, int fh, char **usid, char **gsid)
107 {
108         Pkt *p;
109         uchar *base;
110         Fmt fmt, *f = &fmt;
111         int n, i, off2owner, off2group;
112
113         p = tnthdr(s, sp, NT_TRANSACT_QUERY_SECURITY_DESC);
114         ptntparam(p);
115
116         pl16(p, fh);            /* File handle */
117         pl16(p, 0);             /* Reserved */
118         pl32(p, QUERY_OWNER_SECURITY_INFORMATION |
119                 QUERY_GROUP_SECURITY_INFORMATION);
120
121         ptntdata(p);
122
123         if(tntrpc(p) == -1){
124                 free(p);
125                 return -1;
126         }
127
128         gtntdata(p);
129
130         base = p->pos;
131         gl16(p);                        /* revision */
132         gl16(p);                        /* type */
133         off2owner = gl32(p);            /* offset to owner */
134         off2group = gl32(p);            /* offset to group */
135         gl32(p);
136         gl32(p);
137
138         if(off2owner){
139                 p->pos = base +  off2owner;
140                 fmtstrinit(f);
141                 fmtprint(f, "S-%ud", g8(p));    /* revision */
142                 n = g8(p);                      /* num auth */
143                 fmtprint(f, "-%llud", gb48(p)); /* authority */
144                 for(i = 0; i < n; i++)
145                         fmtprint(f, "-%ud", gl32(p));   /* sub-authorities */
146                 *usid = fmtstrflush(f);
147         }
148
149         if(off2group){
150                 p->pos = base +  off2group;
151                 fmtstrinit(f);
152                 fmtprint(f, "S-%ud", g8(p));    /* revision */
153                 n = g8(p);                      /* num auth */
154                 fmtprint(f, "-%llud", gb48(p)); /* authority */
155                 for(i = 0; i < n; i++)
156                         fmtprint(f, "-%ud", gl32(p));   /* sub-authorities */
157                 *gsid = fmtstrflush(f);
158         }
159         free(p);
160         return 0;
161 }