aurblup.c 5.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* aurblup - AUR blacklist updater
 *
 * Small utility to update the AUR package blacklist. Can be used in a cronjob.
 * Check the "README" file for details.
 */

#include <alpm.h>
#include <mysql.h>
#include <stdio.h>
#include <string.h>

#include "config.h"

#define alpm_die(...) die(__VA_ARGS__, alpm_strerrorlast());
#define mysql_die(...) die(__VA_ARGS__, mysql_error(c));

void die(const char *, ...);
18
void blacklist_add(const char *);
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
void blacklist_sync(alpm_list_t *);
alpm_list_t *get_package_list(alpm_list_t *);
alpm_list_t *create_db_list(void);
void read_config(const char *);
void init(void);
void cleanup(void);

static char *mysql_host = NULL;
static char *mysql_socket = NULL;
static char *mysql_user = NULL;
static char *mysql_passwd = NULL;
static char *mysql_db = NULL;

MYSQL *c;

void
die(const char *format, ...)
{
  va_list arg;

  va_start(arg, format);
  fprintf(stderr, "blackup: ");
  vfprintf(stderr, format, arg);
  va_end(arg);

  cleanup();
  exit(1);
}

void
49
blacklist_add(const char *name)
50
{
51
  char *esc = malloc(strlen(name) * 2 + 1);
52
53
  char query[1024];

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  mysql_real_escape_string(c, esc, name, strlen(name));
  *(esc + strcspn(esc, "<=>")) = 0;
  snprintf(query, 1024, "INSERT IGNORE INTO PackageBlacklist (Name) "
                        "VALUES ('%s');", esc);
  free(esc);

  if (mysql_query(c, query))
    mysql_die("failed to query MySQL database (\"%s\"): %s\n", query);
}

void
blacklist_sync(alpm_list_t *pkgs)
{
  alpm_list_t *r, *p;

69
70
71
72
73
74
75
  if (mysql_query(c, "LOCK TABLES PackageBlacklist WRITE;"))
    mysql_die("failed to lock MySQL table: %s\n");

  if (mysql_query(c, "DELETE FROM PackageBlacklist;"))
    mysql_die("failed to clear MySQL table: %s\n");

  for (r = pkgs; r; r = alpm_list_next(r)) {
76
77
78
    pmpkg_t *pkg = alpm_list_getdata(r);

    blacklist_add(alpm_pkg_get_name(pkg));
79

80
81
82
    for (p = alpm_pkg_get_provides(pkg); p; p = alpm_list_next(p)) {
      blacklist_add(alpm_list_getdata(p));
    }
83

84
85
86
    for (p = alpm_pkg_get_replaces(pkg); p; p = alpm_list_next(p)) {
      blacklist_add(alpm_list_getdata(p));
    }
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
  }

  if (mysql_query(c, "UNLOCK TABLES;"))
    mysql_die("failed to unlock MySQL tables: %s\n");
}

alpm_list_t *
get_package_list(alpm_list_t *dblist)
{
  alpm_list_t *r, *pkgs = NULL;

  for (r = dblist; r; r = alpm_list_next(r)) {
    pmdb_t *db = alpm_list_getdata(r);

    if (alpm_trans_init(0, NULL, NULL, NULL))
      alpm_die("failed to initialize ALPM transaction: %s\n");
    if (alpm_db_update(0, db) < 0)
      alpm_die("failed to update ALPM database: %s\n");
    if (alpm_trans_release())
      alpm_die("failed to release ALPM transaction: %s\n");

    pkgs = alpm_list_join(pkgs, alpm_list_copy(alpm_db_get_pkgcache(db)));
  }

  return pkgs;
}

alpm_list_t *
create_db_list(void)
{
  alpm_list_t *r, *dblist = NULL;
  int i;

  for (i = 0; i < sizeof(alpm_repos) / sizeof(char *); i++) {
    if (!alpm_db_register_sync(alpm_repos[i]))
      alpm_die("failed to register sync db \"%s\": %s\n", alpm_repos[i]);
  }

  if (!(dblist = alpm_option_get_syncdbs()))
    alpm_die("failed to get sync DBs: %s\n");

  for (r = dblist; r; r = alpm_list_next(r)) {
    pmdb_t *db = alpm_list_getdata(r);

    char server[1024];
    snprintf(server, 1024, ALPM_MIRROR, alpm_db_get_name(db));

    if (alpm_db_setserver(db, server))
      alpm_die("failed to set server \"%s\": %s\n", server);
  }

  return dblist;
}

void
read_config(const char *fn)
{
  FILE *fp;
  char line[128];
  char **t, **u, *p, *q;

  if (!(fp = fopen(fn, "r")))
    die("failed to open AUR config file (\"%s\")\n", fn);

  while (fgets(line, sizeof(line), fp)) {
    u = NULL;
    if (strstr(line, CONFIG_KEY_HOST)) {
      t = &mysql_host;
      u = &mysql_socket;
    }
    else if (strstr(line, CONFIG_KEY_USER)) t = &mysql_user;
    else if (strstr(line, CONFIG_KEY_PASSWD)) t = &mysql_passwd;
    else if (strstr(line, CONFIG_KEY_DB)) t = &mysql_db;
    else t = NULL;

    if (t) {
      strtok(line, "\"");
      strtok(NULL, "\"");
      strtok(NULL, "\"");
      p = strtok(NULL, "\"");

      if (u) {
        p = strtok(p, ":");
        q = strtok(NULL, ":");
      }
      else q = NULL;

      if (p && !*t) {
        *t = malloc(strlen(p) + 1);
        strncpy(*t, p, strlen(p) + 1);
      }

      if (q && !*u) {
        *u = malloc(strlen(q) + 1);
        strncpy(*u, q, strlen(q) + 1);
      }
    }
  }

  fclose(fp);

  if (!mysql_host)
    die("MySQL host setting not found in AUR config file\n");
  if (!mysql_user)
    die("MySQL user setting not found in AUR config file\n");
  if (!mysql_passwd)
    die("MySQL password setting not found in AUR config file\n");
  if (!mysql_db)
    die("MySQL database setting not found in AUR config file\n");
}

void
init(void)
{
Lukas Fleischer's avatar
Lukas Fleischer committed
201
202
  if (mysql_library_init(0, NULL, NULL))
    mysql_die("could not initialize MySQL library: %s\n");
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
  if (!(c = mysql_init(NULL)))
    mysql_die("failed to setup MySQL client: %s\n");
  if (!mysql_real_connect(c, mysql_host, mysql_user, mysql_passwd,
                          mysql_db, 0, mysql_socket, 0))
    mysql_die("failed to initiate MySQL connection to %s: %s\n", mysql_host);

  if (alpm_initialize())
    alpm_die("failed to initialize ALPM: %s\n");
  if (alpm_option_set_root("/"))
    alpm_die("failed to set ALPM root: %s\n");
  if (alpm_option_set_dbpath(ALPM_DBPATH))
    alpm_die("failed to set ALPM database path: %s\n");
}

void
cleanup(void)
{
  if (mysql_host) free(mysql_host);
  if (mysql_socket) free(mysql_socket);
  if (mysql_user) free(mysql_user);
  if (mysql_passwd) free(mysql_passwd);
  if (mysql_db) free(mysql_db);

  alpm_release();
  mysql_close(c);
Lukas Fleischer's avatar
Lukas Fleischer committed
228
  mysql_library_end();
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
}

int main(int argc, char *argv[])
{
  alpm_list_t *pkgs;

  read_config(AUR_CONFIG);
  init();
  pkgs = get_package_list(create_db_list());
  blacklist_sync(pkgs);
  alpm_list_free(pkgs);
  cleanup();

  return 0;
}