summaryrefslogtreecommitdiff
path: root/src/sh/tree.cc
blob: 0ec8f161dd4cd1b8bc988faecea814cd299a5211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <sh/htc.h>
#include <sh/user.h>
#include <hdb/buffer.h>
#include <http/http.h>
#include <mt/dates.h>
#include <sys/stat.h>

const mstring index_tag="index.htc";
const mstring sindex_tag="/index.htc";
const mstring htc_mime="text/x-htc";
const mstring htx_mime="text/x-htx";
const mstring html_mime="text/html";

const mstring uri_tag="stat.URI";
const mstring refer_tag="stat.REFER";
const mstring parent_tag="stat.PARENT";
const mstring mime_tag="stat.MIME";

const mstring mod_tag="stat.MOD";
const mstring mtime_tag="stat.LASTMOD";
const mstring uid_tag="stat.UID";
const mstring gid_tag="stat.GID";
const mstring size_tag="stat.SIZE";

const mstring alias_tag="stat.ALIAS";
const mstring banner_tag="stat.BANNER";

const mstring xclear_tag="stat.X.CLEAR";
const mstring xcookie_tag="stat.X.COOKIE";
const mstring domain_tag="stat.X.DOMAIN";
const mstring user_tag="stat.X.USER";
const mstring xcache_tag="stat.X.CACHE";

const mstring xmod_tag="stat.XMOD";

/////////////////////////////////////////////////////////////////

TOK_IMPL(alias) {
  mstring acc;
  Env nenv(env,XSYSTEM,env.pwd);
  acc=env.arg(0)->expr;
  nenv.sloc(alias_tag,acc);
}  

TOK_IMPL(banner) {
  mstring acc1;
  Env nenv(env,XSYSTEM,env.pwd);
  swrite sw(acc1);
  nenv.parse(sw,env.arg(0));
  acc1+=' '; acc1+=env.arg(1)->expr;
  nenv.sloc(banner_tag,acc1);
}  

/////////////////////////////////////////////////////////////////

TOK_IMPL(clearance) {
  mstring acc;
  Env nenv(env,XSYSTEM,env.pwd);
  swrite sw(acc);
  nenv.parse(sw,env.arg(0));
  sref first,rest=acc;
  while(Split(first,rest))
    if(first==root_tag) THROW("user: ROOT group not allowed in clearance");
  nenv.sloc(xclear_tag,acc);
}  

TOK_IMPL(clearance_cookie) {
  mstring acc1,acc2;
  Env nenv(env,XSYSTEM,env.pwd);
  swrite sw1(acc1);
  nenv.parse(sw1,env.arg(0));
  swrite sw2(acc2);
  nenv.parse(sw2,env.arg(1));
  acc1+=' '; acc1+=acc2;
  nenv.sloc(xcookie_tag,acc1);
}  

TOK_IMPL(clearance_domain) {
  mstring acc;
  Env nenv(env,XSYSTEM,env.pwd);
  swrite sw(acc);
  nenv.parse(sw,env.arg(0));
  nenv.sloc(domain_tag,acc);
}  

TOK_IMPL(clearance_user) {
  mstring acc;
  Env nenv(env,XSYSTEM,env.pwd);
  swrite sw(acc);
  nenv.parse(sw,env.arg(0));
  nenv.sloc(user_tag,acc);
}  

TOK_IMPL(cache) {
  mstring acc;
  Env nenv(env,XSYSTEM,env.pwd);
  swrite sw(acc);
  nenv.parse(sw,env.arg(0));
  nenv.sloc(xcache_tag,acc);
}  

/////////////////////////////////////////////////////////////////

inline int reachable(const sref& uri)
{
  static const mstring sys_tag="/sys/";
  static const mstring bin_tag="/bin/";
  static const mstring htc_tag="/htc/";
  static const mstring sccs_tag="/SCCS/";
  static const mstring cvs_tag="/CVS/";
  if(uri.right_last(sindex_tag)==sindex_tag) return 0;
  if(uri.find(sys_tag)>=0) return 0;
  if(uri.find(bin_tag)>=0) return 0;
  if(uri.find(htc_tag)>=0) return 0;
  if(uri.find(sccs_tag)>=0) return 0;
  if(uri.find(cvs_tag)>=0) return 0;
  return 1;
}

TOK_IMPL(staturi) {
  struct stat fs; int ok=0;
  mstring parent,refer,uri=env.URI(env[0]);
  if(reachable(uri)) {
    refer=uri; parent=refer.leftand_last('/');
    if(refer.back()=='/') { refer+=index_tag; parent=parent.popb().leftand_last('/'); }
    int s=stat(DocPath(env,refer).c_str(),&fs);
    if(s>=0) {
      if(fs.st_mode&24576) {
        uri+='/'; refer=uri+index_tag;
        s=stat(DocPath(env,refer).c_str(),&fs);
      }
      if(s>=0) {
        const sref* pmime=file2mime(refer);
        mstring mime=pmime?*pmime:http_mime_default;        
        mstring index=refer.leftand_last('/')+index_tag;

        Env nenv(env);
        nenv.aloc(uri_tag,uri);
        nenv.aloc(refer_tag,refer);
        nenv.aloc(parent_tag,parent);
        nenv.aloc(mime_tag,mime);

        nenv.aloc(mod_tag,itoa(fs.st_mode));
        nenv.aloc(uid_tag,itoa(fs.st_uid));
        nenv.aloc(gid_tag,itoa(fs.st_gid));
        nenv.aloc(size_tag,itoa(fs.st_size));
        nenv.aloc(mtime_tag,itoa(fs.st_mtime));

        nenv.aloc(alias_tag,ms_empty);
        nenv.aloc(banner_tag,ms_empty);

        nenv.aloc(xclear_tag,root_tag);
        nenv.aloc(xcookie_tag,ms_empty);
        nenv.aloc(domain_tag,ms_empty);
        nenv.aloc(user_tag,ms_empty);
        nenv.aloc(xcache_tag,ms_empty);

        nenv.aloc(xmod_tag,itoa(time(0)));

        mstring buffer;
        if(mime==htc_mime||mime==htx_mime||mime==html_mime) {
          LoadBuffer(buffer,DocPath(nenv,refer));
          try {
            Env e(nenv,XINIT,refer);
            mstream m;
            e.parse(m,buffer);
          }
          catch(const merror_t& e) {
            THROW(refer<<": "<<e.desc);
          }
        }
        if(mime!=htc_mime&&nenv.gloc(xclear_tag)==root_tag) {
          if(buffer.load(DocPath(nenv,index).c_str())>=0) {
            try {
              Env e(nenv,XINIT,index);
              mstream m;
              e.parse(m,buffer);
              nenv.sloc(alias_tag,ms_empty);
              nenv.sloc(banner_tag,ms_empty);
            }
            catch(const merror_t& e) {
              THROW(index<<": "<<e.desc);
            }
          }
        }
        nenv.parse(out,env.arg(1));
        ok=1;
      }
    }
  }
  if(!ok) env.parg(out,2);
}

/////////////////////////////////////////////////////////////////

void envAddTree(tokmap& T)
{
  TOK_ADD(alias,"r",tok_t::INIT,
          "%Set alias used in sitemap\n"
          "%Syntax: \\alias{name}\n"
          "%\n"
          "%If set, the URI appears in the automatic tree system.\n"
          );

  TOK_ADD(banner,"rr",tok_t::INIT,
          "%Set banner used in layout\n"
          "%Syntax: \\banner{height}{body}\n"
          "%\n"
          "%If set in index file, all URI descendants will use it.\n"
          );

  ///////////////////////////////////////////////////////
  
  TOK_ADD(clearance,"r",tok_t::INIT,
          "%Set clearance\n"
          "%Syntax: \\clearance{group list}\n"
          "%\n"
          "%A user must have clearance to all groups in order\n"
          "%to get access.\n"
          );
  TOK_ADD(clearance_domain,"r",tok_t::INIT,
          "%Restrict clearance to domain\n"
          "%Syntax: \\clearance.domain{domain list}\n"
          "%\n"
          "%A user must have clearance to at least one domain\n"
          "%in order to get access.\n"
          );
  TOK_ADD(clearance_cookie,"rr",tok_t::INIT,
          "%Restrict by cookie\n"
          "%Syntax: \\clearance.cookie{name}{value}\n"
          "%\n"
          "%A user must have the cookie set to get access.\n"
          );
  TOK_ADD(clearance_user,"r",tok_t::INIT,
          "%Restrict by user\n"
          "%Syntax: \\clearance.user{user list}\n"
          "%\n"
          "%A user must appear in the list in order to get access.\n"
          );

  ///////////////////////////////////////////////////////
  
  TOK_ADD(cache,"r",tok_t::INIT,
          "%Set cache time in seconds\n"
          "%Syntax: \\cache{t}\n"
          "%\n"
          "%If set, normal output is cached for the given time.\n"
          "%\n"
          "%There are a number of advantages of caching, the most\n"
          "%pronounced being server load. Appropriate cache time\n"
          "%is crucial to get a smooth server.\n"
          );
  
  ///////////////////////////////////////////////////////
  
  TOK_ADD(staturi,"xr?",tok_t::SYSONLY,
          "%Retrieve STAT and INIT information for URI\n"
          "%Syntax: \\staturi{uri}{body}<else>\n"
          "%\n"
          "%Do body if a reference to uri is found.\n"
          "%In the body environment, the record stat. is set\n"
          "%with the following variables:\n"
          "%\n"
          "%URI      the canonized URI\n"
          "%REFER    the file uri it refers to\n"
          "%PARENT   the parent uri\n"
          "%MOD      the file mode bits (unix)\n"
          "%UID      the file uid (unix)\n"
          "%GID      the file gid (unix)\n"
          "%SIZE     the file size (unix)\n"
          "%LASTMOD  the file modification time (unix)\n"
          "%MIME     the MIME type\n"
          "%ALIAS    alias used in tree\n"
          "%X.CLEAR  clerarance\n"
          "%X.COOKIE cookie clearance\n"
          "%X.DOMAIN domain clearance\n"
          "%X.USER   user clearance\n"
          "%X.CACHE  cache time in seconds\n"
          "%\n"
          "%The X. variables are inherited from index.htc\n"
          "%if X.CLEAR remains equal to ROOT after init.\n"
          "%\n"
          "%Unreachable directories: /sys/, /bin/, /htc/, /SCCS/ and /CVS/.\n"
          );
}