]> gitweb.ps.run Git - ps-cgit/blob - shared.c
auth: add basic authentication filter framework
[ps-cgit] / shared.c
1 /* shared.c: global vars + some callback functions
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10 #include <stdio.h>
11
12 struct cgit_repolist cgit_repolist;
13 struct cgit_context ctx;
14
15 int chk_zero(int result, char *msg)
16 {
17         if (result != 0)
18                 die_errno("%s", msg);
19         return result;
20 }
21
22 int chk_positive(int result, char *msg)
23 {
24         if (result <= 0)
25                 die_errno("%s", msg);
26         return result;
27 }
28
29 int chk_non_negative(int result, char *msg)
30 {
31         if (result < 0)
32                 die_errno("%s", msg);
33         return result;
34 }
35
36 char *cgit_default_repo_desc = "[no description]";
37 struct cgit_repo *cgit_add_repo(const char *url)
38 {
39         struct cgit_repo *ret;
40
41         if (++cgit_repolist.count > cgit_repolist.length) {
42                 if (cgit_repolist.length == 0)
43                         cgit_repolist.length = 8;
44                 else
45                         cgit_repolist.length *= 2;
46                 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
47                                                cgit_repolist.length *
48                                                sizeof(struct cgit_repo));
49         }
50
51         ret = &cgit_repolist.repos[cgit_repolist.count-1];
52         memset(ret, 0, sizeof(struct cgit_repo));
53         ret->url = trim_end(url, '/');
54         ret->name = ret->url;
55         ret->path = NULL;
56         ret->desc = cgit_default_repo_desc;
57         ret->owner = NULL;
58         ret->section = ctx.cfg.section;
59         ret->snapshots = ctx.cfg.snapshots;
60         ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
61         ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
62         ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
63         ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
64         ret->enable_subject_links = ctx.cfg.enable_subject_links;
65         ret->max_stats = ctx.cfg.max_stats;
66         ret->branch_sort = ctx.cfg.branch_sort;
67         ret->commit_sort = ctx.cfg.commit_sort;
68         ret->module_link = ctx.cfg.module_link;
69         ret->readme = ctx.cfg.readme;
70         ret->mtime = -1;
71         ret->about_filter = ctx.cfg.about_filter;
72         ret->commit_filter = ctx.cfg.commit_filter;
73         ret->source_filter = ctx.cfg.source_filter;
74         ret->email_filter = ctx.cfg.email_filter;
75         ret->clone_url = ctx.cfg.clone_url;
76         ret->submodules.strdup_strings = 1;
77         return ret;
78 }
79
80 struct cgit_repo *cgit_get_repoinfo(const char *url)
81 {
82         int i;
83         struct cgit_repo *repo;
84
85         for (i = 0; i < cgit_repolist.count; i++) {
86                 repo = &cgit_repolist.repos[i];
87                 if (!strcmp(repo->url, url))
88                         return repo;
89         }
90         return NULL;
91 }
92
93 void *cgit_free_commitinfo(struct commitinfo *info)
94 {
95         free(info->author);
96         free(info->author_email);
97         free(info->committer);
98         free(info->committer_email);
99         free(info->subject);
100         free(info->msg);
101         free(info->msg_encoding);
102         free(info);
103         return NULL;
104 }
105
106 char *trim_end(const char *str, char c)
107 {
108         int len;
109
110         if (str == NULL)
111                 return NULL;
112         len = strlen(str);
113         while (len > 0 && str[len - 1] == c)
114                 len--;
115         if (len == 0)
116                 return NULL;
117         return xstrndup(str, len);
118 }
119
120 char *ensure_end(const char *str, char c)
121 {
122         size_t len = strlen(str);
123         char *result;
124
125         if (len && str[len - 1] == c)
126                 return xstrndup(str, len);
127
128         result = xmalloc(len + 2);
129         memcpy(result, str, len);
130         result[len] = '/';
131         result[len + 1] = '\0';
132         return result;
133 }
134
135 void strbuf_ensure_end(struct strbuf *sb, char c)
136 {
137         if (!sb->len || sb->buf[sb->len - 1] != c)
138                 strbuf_addch(sb, c);
139 }
140
141 char *strlpart(char *txt, int maxlen)
142 {
143         char *result;
144
145         if (!txt)
146                 return txt;
147
148         if (strlen(txt) <= maxlen)
149                 return txt;
150         result = xmalloc(maxlen + 1);
151         memcpy(result, txt, maxlen - 3);
152         result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
153         result[maxlen] = '\0';
154         return result;
155 }
156
157 char *strrpart(char *txt, int maxlen)
158 {
159         char *result;
160
161         if (!txt)
162                 return txt;
163
164         if (strlen(txt) <= maxlen)
165                 return txt;
166         result = xmalloc(maxlen + 1);
167         memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
168         result[0] = result[1] = result[2] = '.';
169         return result;
170 }
171
172 void cgit_add_ref(struct reflist *list, struct refinfo *ref)
173 {
174         size_t size;
175
176         if (list->count >= list->alloc) {
177                 list->alloc += (list->alloc ? list->alloc : 4);
178                 size = list->alloc * sizeof(struct refinfo *);
179                 list->refs = xrealloc(list->refs, size);
180         }
181         list->refs[list->count++] = ref;
182 }
183
184 static struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
185 {
186         struct refinfo *ref;
187
188         ref = xmalloc(sizeof (struct refinfo));
189         ref->refname = xstrdup(refname);
190         ref->object = parse_object(sha1);
191         switch (ref->object->type) {
192         case OBJ_TAG:
193                 ref->tag = cgit_parse_tag((struct tag *)ref->object);
194                 break;
195         case OBJ_COMMIT:
196                 ref->commit = cgit_parse_commit((struct commit *)ref->object);
197                 break;
198         }
199         return ref;
200 }
201
202 static void cgit_free_taginfo(struct taginfo *tag)
203 {
204         if (tag->tagger)
205                 free(tag->tagger);
206         if (tag->tagger_email)
207                 free(tag->tagger_email);
208         if (tag->msg)
209                 free(tag->msg);
210         free(tag);
211 }
212
213 static void cgit_free_refinfo(struct refinfo *ref)
214 {
215         if (ref->refname)
216                 free((char *)ref->refname);
217         switch (ref->object->type) {
218         case OBJ_TAG:
219                 cgit_free_taginfo(ref->tag);
220                 break;
221         case OBJ_COMMIT:
222                 cgit_free_commitinfo(ref->commit);
223                 break;
224         }
225         free(ref);
226 }
227
228 void cgit_free_reflist_inner(struct reflist *list)
229 {
230         int i;
231
232         for (i = 0; i < list->count; i++) {
233                 cgit_free_refinfo(list->refs[i]);
234         }
235         free(list->refs);
236 }
237
238 int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
239                   void *cb_data)
240 {
241         struct reflist *list = (struct reflist *)cb_data;
242         struct refinfo *info = cgit_mk_refinfo(refname, sha1);
243
244         if (info)
245                 cgit_add_ref(list, info);
246         return 0;
247 }
248
249 static void cgit_diff_tree_cb(struct diff_queue_struct *q,
250                               struct diff_options *options, void *data)
251 {
252         int i;
253
254         for (i = 0; i < q->nr; i++) {
255                 if (q->queue[i]->status == 'U')
256                         continue;
257                 ((filepair_fn)data)(q->queue[i]);
258         }
259 }
260
261 static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
262 {
263         enum object_type type;
264
265         if (is_null_sha1(sha1)) {
266                 file->ptr = (char *)"";
267                 file->size = 0;
268         } else {
269                 file->ptr = read_sha1_file(sha1, &type,
270                                            (unsigned long *)&file->size);
271         }
272         return 1;
273 }
274
275 /*
276  * Receive diff-buffers from xdiff and concatenate them as
277  * needed across multiple callbacks.
278  *
279  * This is basically a copy of xdiff-interface.c/xdiff_outf(),
280  * ripped from git and modified to use globals instead of
281  * a special callback-struct.
282  */
283 char *diffbuf = NULL;
284 int buflen = 0;
285
286 static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
287 {
288         int i;
289
290         for (i = 0; i < nbuf; i++) {
291                 if (mb[i].ptr[mb[i].size-1] != '\n') {
292                         /* Incomplete line */
293                         diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
294                         memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
295                         buflen += mb[i].size;
296                         continue;
297                 }
298
299                 /* we have a complete line */
300                 if (!diffbuf) {
301                         ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
302                         continue;
303                 }
304                 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
305                 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
306                 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
307                 free(diffbuf);
308                 diffbuf = NULL;
309                 buflen = 0;
310         }
311         if (diffbuf) {
312                 ((linediff_fn)priv)(diffbuf, buflen);
313                 free(diffbuf);
314                 diffbuf = NULL;
315                 buflen = 0;
316         }
317         return 0;
318 }
319
320 int cgit_diff_files(const unsigned char *old_sha1,
321                     const unsigned char *new_sha1, unsigned long *old_size,
322                     unsigned long *new_size, int *binary, int context,
323                     int ignorews, linediff_fn fn)
324 {
325         mmfile_t file1, file2;
326         xpparam_t diff_params;
327         xdemitconf_t emit_params;
328         xdemitcb_t emit_cb;
329
330         if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
331                 return 1;
332
333         *old_size = file1.size;
334         *new_size = file2.size;
335
336         if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
337             (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
338                 *binary = 1;
339                 if (file1.size)
340                         free(file1.ptr);
341                 if (file2.size)
342                         free(file2.ptr);
343                 return 0;
344         }
345
346         memset(&diff_params, 0, sizeof(diff_params));
347         memset(&emit_params, 0, sizeof(emit_params));
348         memset(&emit_cb, 0, sizeof(emit_cb));
349         diff_params.flags = XDF_NEED_MINIMAL;
350         if (ignorews)
351                 diff_params.flags |= XDF_IGNORE_WHITESPACE;
352         emit_params.ctxlen = context > 0 ? context : 3;
353         emit_params.flags = XDL_EMIT_FUNCNAMES;
354         emit_cb.outf = filediff_cb;
355         emit_cb.priv = fn;
356         xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
357         if (file1.size)
358                 free(file1.ptr);
359         if (file2.size)
360                 free(file2.ptr);
361         return 0;
362 }
363
364 void cgit_diff_tree(const unsigned char *old_sha1,
365                     const unsigned char *new_sha1,
366                     filepair_fn fn, const char *prefix, int ignorews)
367 {
368         struct diff_options opt;
369         struct pathspec_item item;
370
371         diff_setup(&opt);
372         opt.output_format = DIFF_FORMAT_CALLBACK;
373         opt.detect_rename = 1;
374         opt.rename_limit = ctx.cfg.renamelimit;
375         DIFF_OPT_SET(&opt, RECURSIVE);
376         if (ignorews)
377                 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
378         opt.format_callback = cgit_diff_tree_cb;
379         opt.format_callback_data = fn;
380         if (prefix) {
381                 item.match = prefix;
382                 item.len = strlen(prefix);
383                 opt.pathspec.nr = 1;
384                 opt.pathspec.items = &item;
385         }
386         diff_setup_done(&opt);
387
388         if (old_sha1 && !is_null_sha1(old_sha1))
389                 diff_tree_sha1(old_sha1, new_sha1, "", &opt);
390         else
391                 diff_root_tree_sha1(new_sha1, "", &opt);
392         diffcore_std(&opt);
393         diff_flush(&opt);
394 }
395
396 void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
397 {
398         unsigned char *old_sha1 = NULL;
399
400         if (commit->parents)
401                 old_sha1 = commit->parents->item->object.sha1;
402         cgit_diff_tree(old_sha1, commit->object.sha1, fn, prefix,
403                        ctx.qry.ignorews);
404 }
405
406 int cgit_parse_snapshots_mask(const char *str)
407 {
408         struct string_list tokens = STRING_LIST_INIT_DUP;
409         struct string_list_item *item;
410         const struct cgit_snapshot_format *f;
411         int rv = 0;
412
413         /* favor legacy setting */
414         if (atoi(str))
415                 return 1;
416
417         string_list_split(&tokens, str, ' ', -1);
418         string_list_remove_empty_items(&tokens, 0);
419
420         for_each_string_list_item(item, &tokens) {
421                 for (f = cgit_snapshot_formats; f->suffix; f++) {
422                         if (!strcmp(item->string, f->suffix) ||
423                             !strcmp(item->string, f->suffix + 1)) {
424                                 rv |= f->bit;
425                                 break;
426                         }
427                 }
428         }
429
430         string_list_clear(&tokens, 0);
431         return rv;
432 }
433
434 typedef struct {
435         char * name;
436         char * value;
437 } cgit_env_var;
438
439 void cgit_prepare_repo_env(struct cgit_repo * repo)
440 {
441         cgit_env_var env_vars[] = {
442                 { .name = "CGIT_REPO_URL", .value = repo->url },
443                 { .name = "CGIT_REPO_NAME", .value = repo->name },
444                 { .name = "CGIT_REPO_PATH", .value = repo->path },
445                 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
446                 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
447                 { .name = "CGIT_REPO_SECTION", .value = repo->section },
448                 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
449         };
450         int env_var_count = ARRAY_SIZE(env_vars);
451         cgit_env_var *p, *q;
452         static char *warn = "cgit warning: failed to set env: %s=%s\n";
453
454         p = env_vars;
455         q = p + env_var_count;
456         for (; p < q; p++)
457                 if (p->value && setenv(p->name, p->value, 1))
458                         fprintf(stderr, warn, p->name, p->value);
459 }
460
461 /* Read the content of the specified file into a newly allocated buffer,
462  * zeroterminate the buffer and return 0 on success, errno otherwise.
463  */
464 int readfile(const char *path, char **buf, size_t *size)
465 {
466         int fd, e;
467         struct stat st;
468
469         fd = open(path, O_RDONLY);
470         if (fd == -1)
471                 return errno;
472         if (fstat(fd, &st)) {
473                 e = errno;
474                 close(fd);
475                 return e;
476         }
477         if (!S_ISREG(st.st_mode)) {
478                 close(fd);
479                 return EISDIR;
480         }
481         *buf = xmalloc(st.st_size + 1);
482         *size = read_in_full(fd, *buf, st.st_size);
483         e = errno;
484         (*buf)[*size] = '\0';
485         close(fd);
486         return (*size == st.st_size ? 0 : e);
487 }
488
489 static int is_token_char(char c)
490 {
491         return isalnum(c) || c == '_';
492 }
493
494 /* Replace name with getenv(name), return pointer to zero-terminating char
495  */
496 static char *expand_macro(char *name, int maxlength)
497 {
498         char *value;
499         int len;
500
501         len = 0;
502         value = getenv(name);
503         if (value) {
504                 len = strlen(value);
505                 if (len > maxlength)
506                         len = maxlength;
507                 strncpy(name, value, len);
508         }
509         return name + len;
510 }
511
512 #define EXPBUFSIZE (1024 * 8)
513
514 /* Replace all tokens prefixed by '$' in the specified text with the
515  * value of the named environment variable.
516  * NB: the return value is a static buffer, i.e. it must be strdup'd
517  * by the caller.
518  */
519 char *expand_macros(const char *txt)
520 {
521         static char result[EXPBUFSIZE];
522         char *p, *start;
523         int len;
524
525         p = result;
526         start = NULL;
527         while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
528                 *p = *txt;
529                 if (start) {
530                         if (!is_token_char(*txt)) {
531                                 if (p - start > 0) {
532                                         *p = '\0';
533                                         len = result + EXPBUFSIZE - start - 1;
534                                         p = expand_macro(start, len) - 1;
535                                 }
536                                 start = NULL;
537                                 txt--;
538                         }
539                         p++;
540                         txt++;
541                         continue;
542                 }
543                 if (*txt == '$') {
544                         start = p;
545                         txt++;
546                         continue;
547                 }
548                 p++;
549                 txt++;
550         }
551         *p = '\0';
552         if (start && p - start > 0) {
553                 len = result + EXPBUFSIZE - start - 1;
554                 p = expand_macro(start, len);
555                 *p = '\0';
556         }
557         return result;
558 }