/* wydawca - automatic release submission daemon Copyright (C) 2007, 2008, 2009, 2010 Sergey Poznyakoff Wydawca 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 3 of the License, or (at your option) any later version. Wydawca 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 wydawca. If not, see . */ #include "wydawca.h" #include static struct grecs_symtab *triplet_table; static unsigned hash_triplet_hasher (void *data, unsigned long n_buckets) { struct file_triplet const *t = data; return grecs_hash_string (t->name, n_buckets); } /* Compare two strings for equality. */ static int hash_triplet_compare (void const *data1, void const *data2) { struct file_triplet const *t1 = data1; struct file_triplet const *t2 = data2; return strcmp (t1->name, t2->name); } /* Reclaim memory storage associated with a table entry */ void hash_triplet_free (void *data) { int i; struct file_triplet *tp = data; struct uploader_info *up; free (tp->name); for (i = 0; i < FILE_TYPE_COUNT; i++) { if (tp->file[i].name) free (tp->file[i].name); } free (tp->directive); free (tp->blurb); free (tp->tmp); txtacc_free (tp->acc); /* Free uploader list */ for (up = tp->uploader_list; up; ) { struct uploader_info *next = up->next; free (up); up = next; } free (tp); } char * triplet_strdup (struct file_triplet *tp, const char *str) { size_t len = strlen (str); txtacc_grow (tp->acc, str, len + 1); return txtacc_finish (tp->acc); } /* Register a file in the triplet table */ void register_file (struct file_info *finfo, const struct spool *spool) { struct file_triplet key, *ret; int install = 1; if (!triplet_table) { triplet_table = grecs_symtab_create (sizeof (struct file_triplet), hash_triplet_hasher, hash_triplet_compare, NULL, NULL, hash_triplet_free); if (!triplet_table) grecs_alloc_die (); } key.name = xmalloc (finfo->root_len + 1); memcpy (key.name, finfo->name, finfo->root_len); key.name[finfo->root_len] = 0; ret = grecs_symtab_lookup_or_install (triplet_table, &key, &install); if (!ret) grecs_alloc_die (); free (key.name); if (install) { ret->spool = spool; ret->acc = txtacc_create (); } ret->file[finfo->type] = *finfo; } /* Return true if any part of the triplet TRP was modified more than TTL seconds ago */ static int triplet_expired_p (struct file_triplet *trp, time_t ttl) { int i; time_t now = time (NULL); if (ttl == 0) return 0; for (i = 0; i < FILE_TYPE_COUNT; i++) { if (trp->file[i].name && (now - trp->file[i].sb.st_mtime) > ttl) { if (debug_level) logmsg (LOG_DEBUG, _("file %s expired"), trp->file[i].name); return 1; } } return 0; } enum triplet_state { triplet_directive, /* Short triplet: only a directive is present, but nothing more is required */ triplet_complete, /* A complete triplet: all three files are present and have the same owner */ triplet_incomplete, /* Incomplete triplet: some files are missing */ triplet_bad, /* Bad triplet. Should be removed immediately. */ }; static enum triplet_state check_triplet_state (struct file_triplet *trp) { if (trp->file[file_directive].name) { if (verify_directive_file (trp)) return triplet_bad; if (trp->file[file_dist].name == 0 && trp->file[file_signature].name == 0) { if (directive_get_value (trp, "filename", NULL)) return triplet_directive; } else if (trp->file[file_dist].name && trp->file[file_signature].name) { if (trp->file[file_dist].sb.st_uid == trp->file[file_signature].sb.st_uid && trp->file[file_dist].sb.st_uid == trp->file[file_directive].sb.st_uid) return triplet_complete; else { if (debug_level) logmsg (LOG_DEBUG, _("%s: invalid triplet: UIDs differ"), trp->name); return triplet_bad; } } } return triplet_incomplete; } /* Unlink all parts of the triplet TRP */ static void remove_triplet (struct file_triplet *trp) { int i; for (i = 0; i < FILE_TYPE_COUNT; i++) { if (trp->file[i].name) { logmsg (LOG_NOTICE, _("removing %s"), trp->file[i].name); if (!dry_run_mode && unlink (trp->file[i].name)) logmsg (LOG_ERR, _("cannot remove %s: %s"), trp->file[i].name, strerror (errno)); } } } /* Process a single triplet from the table */ static int triplet_processor (void *data, void *proc_data) { struct file_triplet *trp = data; if (debug_level) logmsg (LOG_DEBUG, "FILE %s, DIST=%s, SIG=%s, DIRECTIVE=%s", trp->name, SP (trp->file[file_dist].name), SP (trp->file[file_signature].name), SP (trp->file[file_directive].name)); switch (check_triplet_state (trp)) { case triplet_directive: case triplet_complete: if (debug_level) logmsg (LOG_DEBUG, _("processing triplet `%s'"), trp->name); if (process_directives (trp)) remove_triplet (trp); return 0; case triplet_incomplete: if (debug_level) logmsg (LOG_DEBUG, _("%s: incomplete triplet"), trp->name); /* ignore unless expired (see below); */ UPDATE_STATS (STAT_INCOMPLETE_TRIPLETS); break; case triplet_bad: UPDATE_STATS (STAT_BAD_TRIPLETS); remove_triplet (trp); return 0; } if (triplet_expired_p (trp, trp->spool->file_sweep_time)) { UPDATE_STATS (STAT_EXPIRED_TRIPLETS); remove_triplet (trp); } return 0; } /* Process all triplets from the table according to the SPOOL */ void enumerate_triplets (const struct spool *spool) { if (debug_level) logmsg (LOG_DEBUG, _("processing spool %s (%s)"), spool->tag, mu_url_to_string (spool->dest_url)); if (triplet_table) { grecs_symtab_enumerate (triplet_table, triplet_processor, NULL); grecs_symtab_clear (triplet_table); } } size_t count_collected_triplets () { return triplet_table ? grecs_symtab_count_entries (triplet_table) : 0; } static const char * expand_project_base (struct metadef *def, void *data) { struct file_triplet *trp = data; return trp->project; } static const char * expand_tag (struct metadef *def, void *data) { struct file_triplet *trp = data; return trp->spool->tag; } static const char * expand_url (struct metadef *def, void *data) { struct file_triplet *trp = data; return trp->spool->url; } static const char * expand_relative_dir (struct metadef *def, void *data) { struct file_triplet *trp = data; directive_get_value (trp, "directory", (const char**) &def->value); return def->value; } static const char * expand_dest_dir (struct metadef *def, void *data) { struct file_triplet *trp = data; return trp->spool->dest_dir; } static const char * expand_source_dir (struct metadef *def, void *data) { struct file_triplet *trp = data; return trp->spool->source_dir; } static void decode_file_mode (mode_t mode, char *string) { *string++ = mode & S_IRUSR ? 'r' : '-'; *string++ = mode & S_IWUSR ? 'w' : '-'; *string++ = (mode & S_ISUID ? (mode & S_IXUSR ? 's' : 'S') : (mode & S_IXUSR ? 'x' : '-')); *string++ = mode & S_IRGRP ? 'r' : '-'; *string++ = mode & S_IWGRP ? 'w' : '-'; *string++ = (mode & S_ISGID ? (mode & S_IXGRP ? 's' : 'S') : (mode & S_IXGRP ? 'x' : '-')); *string++ = mode & S_IROTH ? 'r' : '-'; *string++ = mode & S_IWOTH ? 'w' : '-'; *string++ = (mode & S_ISVTX ? (mode & S_IXOTH ? 't' : 'T') : (mode & S_IXOTH ? 'x' : '-')); *string = '\0'; } /* Width of "user/group size", with initial value chosen heuristically. This grows as needed, though this may cause some stairstepping in the output. Make it too small and the output will almost always look ragged. Make it too large and the output will be spaced out too far. */ static int ugswidth = 19; static int format_file_data (struct file_triplet *trp, enum file_type type, char **pret) { char modes[11]; struct file_info *info = trp->file + type; char timebuf[sizeof "YYYY-MM-DD HH:MM:SS +0000"]; struct passwd *pw; struct group *grp; char *sptr = NULL; size_t slen = 0; int pad; char *user_name; char *group_name; struct tm *tm; char *buf = NULL; size_t size = 0; if (!info->name) return 1; /* MODE OWNER GROUP SIZE MTIME FILE_NAME MD5SUM? */ modes[0] = '-'; /* Only regular files are allowed */ decode_file_mode (info->sb.st_mode, modes + 1); /* File time */ tm = localtime (&info->sb.st_mtime); strftime (timebuf, sizeof timebuf, "%Y-%m-%d %H:%M:%S %z", tm); pw = getpwuid (TRIPLET_UID (trp)); if (!pw) user_name = "unknown"; else user_name = pw->pw_name; grp = getgrgid (TRIPLET_GID (trp)); if (!grp) group_name = "unknown"; /* should not happen */ else group_name = grp->gr_name; /* Size */ if (grecs_asprintf (&sptr, &slen, "%lu", (unsigned long) info->sb.st_size)) xalloc_die (); /* Figure out padding and format the buffer */ slen = strlen (sptr); pad = strlen (user_name) + 1 + strlen (group_name) + 1 + slen; if (pad > ugswidth) ugswidth = pad; if (grecs_asprintf (&buf, &size, "%s %s %s %*s %s %s", modes, user_name, group_name, ugswidth - pad + slen, sptr, timebuf, info->name)) xalloc_die (); free (sptr); *pret = buf; return 0; } static const char * expand_triplet_ls_full (struct metadef *def, void *data) { struct file_triplet *trp = data; char *buf[FILE_TYPE_COUNT] = { NULL, NULL, NULL }; size_t size = 0; if (format_file_data (trp, file_dist, &buf[file_dist]) == 0) size += strlen (buf[file_dist]) + 1; if (format_file_data (trp, file_signature, &buf[file_signature]) == 0) size += strlen (buf[file_signature]) + 1; if (format_file_data (trp, file_directive, &buf[file_directive]) == 0) size += strlen (buf[file_directive]) + 1; def->value = def->storage = xmalloc (size + 1); def->value[0] = 0; if (buf[file_dist]) { strcat (def->value, buf[file_dist]); strcat (def->value, "\n"); } if (buf[file_signature]) { strcat (def->value, buf[file_signature]); strcat (def->value, "\n"); } if (buf[file_directive]) { strcat (def->value, buf[file_directive]); strcat (def->value, "\n"); } free (buf[file_dist]); free (buf[file_signature]); free (buf[file_directive]); return def->value; } static const char * expand_triplet_ls_upload (struct metadef *def, void *data) { struct file_triplet *trp = data; char *buf[2] = { NULL, NULL }; size_t size = 0; if (format_file_data (trp, file_dist, &buf[file_dist]) == 0) size += strlen (buf[file_dist]) + 1; if (format_file_data (trp, file_signature, &buf[file_signature]) == 0) size += strlen (buf[file_signature]) + 1; def->value = def->storage = xmalloc (size + 1); def->value[0] = 0; if (buf[file_dist]) { strcat (def->value, buf[file_dist]); strcat (def->value, "\n"); } if (buf[file_signature]) { strcat (def->value, buf[file_signature]); strcat (def->value, "\n"); } free (buf[file_dist]); free (buf[file_signature]); return def->value; } static const char * expand_triplet_dist (struct metadef *def, void *data) { struct file_triplet *trp = data; return trp->file[file_dist].name; } static const char * expand_triplet_sig (struct metadef *def, void *data) { struct file_triplet *trp = data; return trp->file[file_signature].name; } static const char * expand_triplet_dir (struct metadef *def, void *data) { struct file_triplet *trp = data; return trp->file[file_directive].name; } static const char * expand_triplet_ls_dist (struct metadef *def, void *data) { struct file_triplet *trp = data; format_file_data (trp, file_dist, &def->storage); def->value = def->storage; return def->value; } static const char * expand_triplet_ls_sig (struct metadef *def, void *data) { struct file_triplet *trp = data; format_file_data (trp, file_signature, &def->storage); def->value = def->storage; return def->value; } static const char * expand_triplet_ls_directive (struct metadef *def, void *data) { struct file_triplet *trp = data; format_file_data (trp, file_directive, &def->storage); def->value = def->storage; return def->value; } static const char * expand_user_name (struct metadef *def, void *data) { struct file_triplet *trp = data; if (trp->uploader) return trp->uploader->name; def->value = "UNKNOWN"; return def->value; } static const char * expand_user_real_name (struct metadef *def, void *data) { struct file_triplet *trp = data; if (trp->uploader) return trp->uploader->realname; def->value = "UNKNOWN"; return def->value; } static const char * expand_user_email (struct metadef *def, void *data) { struct file_triplet *trp = data; if (trp->uploader) return trp->uploader->email; def->value = "UNKNOWN"; return def->value; } static const char * expand_email_user (struct metadef *def, void *data) { struct file_triplet *trp = data; if (trp->uploader) { size_t size = 0; if (grecs_asprintf (&def->storage, &size, "\"%s\" <%s>", trp->uploader->realname, trp->uploader->email)) xalloc_die (); def->value = def->storage; } return def->value; } static const char * expand_report (struct metadef *def, void *data) { return report_string; } static const char * expand_comment (struct metadef *def, void *data) { struct file_triplet *trp = data; if (directive_get_value (trp, "comment", (const char**) &def->value)) def->value = ""; return def->value; } static const char * expand_check_diagn (struct metadef *def, void *data) { struct file_triplet *trp = data; return trp->check_diag; } static const char * expand_check_result (struct metadef *def, void *data) { struct file_triplet *trp = data; int status = trp->check_result; if (status == 0) return def->value = "0"; else if (WIFEXITED (status)) { size_t size = 0; def->storage = NULL; if (grecs_asprintf (&def->storage, &size, "%d", WEXITSTATUS (status))) xalloc_die (); } else if (WIFSIGNALED (status)) { size_t size = 0; def->storage = NULL; if (grecs_asprintf (&def->storage, &size, "SIG+%d", WTERMSIG (status))) xalloc_die (); } else return def->value = "[unrecognized return code]"; return def->value = def->storage; } #define DECL_EXPAND_TIMER(what) \ static const char * \ __cat2__(expand_timer_,what) (struct metadef *def, void *data) \ { \ wydawca_timer_t t = timer_stop ((char*)def->data); \ def->storage = timer_format_time (__cat2__(timer_get_,what) (t)); \ return def->value = def->storage; \ } DECL_EXPAND_TIMER(real) DECL_EXPAND_TIMER(user) DECL_EXPAND_TIMER(system) #define DECL_TIMER(name,t) \ { "timer:" #name ":" #t, NULL, __cat2__(expand_timer_,t), NULL, #name } #define DECL_FULL_TIMER(name) \ DECL_TIMER(name, real), \ DECL_TIMER(name, user), \ DECL_TIMER(name, system) struct metadef triplet_default_meta[] = { { "u", NULL, expand_user_name, NULL }, { "user", NULL, expand_user_name, NULL }, { "user:name", NULL, expand_user_name, NULL }, { "p", NULL, expand_project_base, NULL }, { "project", NULL, expand_project_base, NULL }, { "url", NULL, expand_url, NULL }, { "spool", NULL, expand_tag, NULL }, { "dir", NULL, expand_relative_dir, NULL }, { "dest-dir", NULL, expand_dest_dir, NULL }, { "source-dir", NULL, expand_source_dir, NULL }, { "comment", NULL, expand_comment, NULL }, { NULL } }; char * triplet_expand_dictionary_query (struct dictionary *dict, void *handle, struct file_triplet *trp) { char *p = meta_expand_string (dict->query, triplet_default_meta, trp, dict, handle); meta_free (triplet_default_meta); return p; } struct metadef triplet_meta[] = { { "project", NULL, expand_project_base, NULL }, { "url", NULL, expand_url, NULL }, { "spool", NULL, expand_tag, NULL }, { "dir", NULL, expand_relative_dir, NULL }, { "dest-dir", NULL, expand_dest_dir, NULL }, { "source-dir", NULL, expand_source_dir, NULL }, { "triplet:dist", NULL, expand_triplet_dist, NULL }, { "triplet:sig", NULL, expand_triplet_sig, NULL }, { "triplet:dir", NULL, expand_triplet_dir, NULL }, { "triplet:ls:full", NULL, expand_triplet_ls_full, NULL }, { "triplet:ls:upload", NULL, expand_triplet_ls_upload, NULL }, { "triplet:ls:dist", NULL, expand_triplet_ls_dist, NULL }, { "triplet:ls:sig", NULL, expand_triplet_ls_sig, NULL }, { "triplet:ls:dir", NULL, expand_triplet_ls_directive, NULL }, { "check:result", NULL, expand_check_result, NULL }, { "check:diagn", NULL, expand_check_diagn, NULL }, { "user", NULL, expand_user_name, NULL }, { "user:name", NULL, expand_user_name, NULL }, { "user:real-name", NULL, expand_user_real_name, NULL }, { "user:email", NULL, expand_user_email, NULL }, { "email:user", NULL, expand_email_user, NULL }, { "email:admin", NULL, expand_email_admin, NULL }, { "email:owner", NULL, expand_email_owner, NULL }, { "report", NULL, expand_report, NULL }, { "comment", NULL, expand_comment, NULL }, DECL_FULL_TIMER(wydawca), DECL_FULL_TIMER(triplet), DECL_FULL_TIMER(spool), { NULL } }; char * triplet_expand_param (const char *tmpl, struct file_triplet *trp) { char *p = meta_expand_string (tmpl, triplet_meta, trp, NULL, NULL); meta_free (triplet_meta); return p; }