~ubuntu-branches/debian/sid/apt-dater/sid

« back to all changes in this revision

Viewing changes to src/keyfiles.c

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2015-07-07 17:33:08 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20150707173308-7rkjhxnlq7gsykif
Tags: 1.0.2-1
* New upstream release.
  - Set the default of the opt-cmd-flags host option to "-t" since this is
    essential.
    Closes: #776392
  - Add new build dependency vim-common.
* Replace screen dependency with tmux.
* Create apt-dater group for session sharing.
* Add missing directories.
* Make the build reproducible.
  Closes: #789648
* Drop automake and autoconf build dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 *   Thomas Liske <liske@ibh.de>
5
5
 *
6
6
 * Copyright Holder:
7
 
 *   2008-2014 (C) IBH IT-Service GmbH [https://www.ibh.de/apt-dater/]
 
7
 *   2008-2015 (C) IBH IT-Service GmbH [https://www.ibh.de/apt-dater/]
8
8
 *
9
9
 * License:
10
10
 *   This program is free software; you can redistribute it and/or modify
23
23
 */
24
24
 
25
25
#include "apt-dater.h"
26
 
#include "screen.h"
27
26
#include "keyfiles.h"
28
27
#include "stats.h"
29
28
#include "lock.h"
 
29
#include "ttymux.h"
30
30
 
31
31
#ifdef HAVE_CONFIG_H
32
32
# include "config.h"
33
33
#endif
34
34
 
 
35
#include <errno.h>
 
36
 
35
37
#include <libxml/parser.h>
 
38
#include <libxml/xinclude.h>
36
39
#include <libxml/xpath.h>
37
40
 
38
41
#include "../conf/apt-dater.xml.inc"
39
42
#include "../conf/hosts.xml.inc"
 
43
#ifdef FEAT_TMUX
 
44
#include "../conf/tmux.conf.inc"
 
45
#else
40
46
#include "../conf/screenrc.inc"
 
47
#endif
41
48
 
42
49
void dump_config(const gchar *dir, const gchar *fn, const gchar *str, const unsigned int len) {
43
50
  gchar *pathtofile = g_strdup_printf("%s/%s", dir, (fn));
61
68
 
62
69
int chkForInitialConfig(const gchar *cfgdir, const gchar *cfgfile) {
63
70
  if(g_file_test(cfgdir, G_FILE_TEST_IS_DIR) == FALSE) {
64
 
    if(g_mkdir_with_parents (cfgdir, 0700)) return(1);
 
71
    if(g_mkdir_with_parents (cfgdir, S_IRWXU)) return(1);
65
72
  }
66
73
 
67
74
  dump_config(cfgdir, "apt-dater.xml", (gchar *)apt_dater_xml, apt_dater_xml_len);
 
75
 
 
76
  /* Convert legacy hosts.conf to new hosts.xml using external helper script. */
 
77
  gchar *fnold = g_strdup_printf("%s/%s", cfgdir, "hosts.conf");
 
78
  gchar *fnnew = g_strdup_printf("%s/%s", cfgdir, "hosts.xml");
 
79
  if((g_file_test(fnnew, G_FILE_TEST_IS_REGULAR|G_FILE_TEST_EXISTS) == FALSE) &&
 
80
     (g_file_test(fnold, G_FILE_TEST_IS_REGULAR|G_FILE_TEST_EXISTS) == TRUE)) {
 
81
 
 
82
    gchar *argv[3] = {PKGLIBDIR"/hosts2xml", "hosts2xml", NULL};
 
83
    GError *error = NULL;
 
84
    if(g_spawn_sync(g_getenv ("HOME"), argv, NULL, G_SPAWN_CHILD_INHERITS_STDIN, NULL, NULL, NULL, NULL, NULL, &error) == FALSE) {
 
85
      g_warning("%s", error->message);
 
86
      g_clear_error (&error);
 
87
    }
 
88
  }
 
89
 
68
90
  dump_config(cfgdir, "hosts.xml", (gchar *)hosts_xml, hosts_xml_len);
 
91
#ifdef FEAT_TMUX
 
92
  dump_config(cfgdir, "tmux.conf", (gchar *)tmux_conf, tmux_conf_len);
 
93
#else
69
94
  dump_config(cfgdir, "screenrc", (gchar *)screenrc, screenrc_len);
 
95
#endif
70
96
 
71
97
 return(0);
72
98
}
81
107
 g_free(cfg->cmd_refresh);
82
108
 g_free(cfg->cmd_upgrade);
83
109
 g_free(cfg->cmd_install);
 
110
#ifdef FEAT_TMUX
 
111
 g_free(cfg->tmuxconffile);
 
112
 g_free(cfg->tmuxsockpath);
 
113
#else
84
114
 g_free(cfg->screenrcfile);
85
115
 g_free(cfg->screentitle);
 
116
#endif
86
117
 g_strfreev(cfg->colors);
87
118
 
88
119
 g_free(cfg);
94
125
    lcfg->_type = T_CFGFILE;
95
126
#endif
96
127
 
97
 
    lcfg->hostsfile = g_strdup_printf("%s/%s/%s", g_get_user_config_dir(), PROG_NAME, "hosts.xml");
98
 
    lcfg->statsdir = g_strdup_printf("%s/%s/%s", g_get_user_cache_dir(), PROG_NAME, "stats");
99
 
 
100
 
    lcfg->screenrcfile = g_strdup_printf("%s/%s/%s", g_get_user_config_dir(), PROG_NAME, "screenrc");
101
 
 
102
128
    lcfg->dump_screen = TRUE;
103
 
    lcfg->query_maintainer = FALSE;
104
129
 
105
130
#ifdef FEAT_AUTOREF
106
131
    lcfg->auto_refresh = TRUE;
111
136
 
112
137
#ifdef FEAT_HISTORY
113
138
    lcfg->record_history = TRUE;
114
 
    lcfg->history_errpattern = "((?<!no )error|warning|fail)";
115
139
#endif
116
140
 
117
141
    lcfg->hook_pre_upgrade = "/etc/apt-dater/pre-upg.d";
165
189
  if(!sval)
166
190
    return defval;
167
191
 
168
 
  int ival = atoi(sval);
 
192
  int ival = strtol(sval, NULL, 0);
169
193
 
170
194
  g_free(sval);
171
195
 
225
249
    if(xcfg == NULL)
226
250
      return(FALSE);
227
251
 
 
252
    /* Handle Xincludes. */
 
253
    xmlXIncludeProcess(xcfg);
 
254
 
 
255
    /* Validate against DTD. */
 
256
    xmlValidCtxtPtr xval = xmlNewValidCtxt();
 
257
    if(xmlValidateDocument(xval, xcfg) == 0) {
 
258
      xmlFreeValidCtxt(xval);
 
259
      return(FALSE);
 
260
    }
 
261
    xmlFreeValidCtxt(xval);
 
262
 
228
263
    /* Allocate XPath context. */
229
264
    xmlXPathContextPtr xctx = xmlXPathNewContext(xcfg);
230
265
    if(!xctx) {
234
269
 
235
270
    xmlNodePtr s_ssh[2] = {getXNode(xctx, "/apt-dater/ssh"), NULL};
236
271
    xmlNodePtr s_path[2] = {getXNode(xctx, "/apt-dater/paths"), NULL};
 
272
#ifdef FEAT_TMUX
 
273
    xmlNodePtr s_tmux[2] = {getXNode(xctx, "/apt-dater/tmux"), NULL};
 
274
#else
237
275
    xmlNodePtr s_screen[2] = {getXNode(xctx, "/apt-dater/screen"), NULL};
 
276
#endif
238
277
    xmlNodePtr s_appearance[2] = {getXNode(xctx, "/apt-dater/appearance"), NULL};
239
278
    xmlNodePtr s_notify[2] = {getXNode(xctx, "/apt-dater/notify"), NULL};
240
279
    xmlNodePtr s_hooks[2] = {getXNode(xctx, "/apt-dater/hooks"), NULL};
248
287
    xmlNodePtr s_tclfilter[2] = {getXNode(xctx, "/apt-dater/tcl-filter"), NULL};
249
288
#endif
250
289
 
251
 
    lcfg->ssh_optflags = getXPropStr(s_ssh, "opt-cmd-flags", NULL);
 
290
    lcfg->ssh_optflags = getXPropStr(s_ssh, "opt-cmd-flags", "-t");
252
291
    lcfg->ssh_cmd = getXPropStr(s_ssh, "cmd", "/usr/bin/ssh");
253
292
    lcfg->sftp_cmd = getXPropStr(s_ssh, "sftp-cmd", "/usr/bin/sftp");
254
293
 
 
294
    lcfg->umask = getXPropInt(s_path, "umask", S_IRWXG | S_IRWXO);
 
295
    umask(lcfg->umask);
 
296
 
255
297
    lcfg->hostsfile = getXPropStr(s_path, "hosts-file", g_strdup_printf("%s/%s/%s", g_get_user_config_dir(), PROG_NAME, "hosts.xml"));
256
298
    lcfg->statsdir = getXPropStr(s_path, "stats-dir", g_strdup_printf("%s/%s/%s", g_get_user_cache_dir(), PROG_NAME, "stats"));
257
 
    g_mkdir_with_parents(lcfg->statsdir, S_IRWXU | S_IRWXG | S_IRWXO);
 
299
    if(g_mkdir_with_parents(lcfg->statsdir, S_IRWXU | S_IRWXG) == -1) {
 
300
      g_warning("Failed to create %s: %s", lcfg->statsdir, g_strerror(errno));
 
301
      exit(1);
 
302
    }
258
303
 
 
304
#ifdef FEAT_TMUX
 
305
    lcfg->tmuxconffile = getXPropStr(s_tmux, "conf-file", g_strdup_printf("%s/%s/%s", g_get_user_config_dir(), PROG_NAME, "tmux.conf"));
 
306
    lcfg->tmuxsockpath = getXPropStr(s_tmux, "socket-path", g_strdup_printf("%s/%s/%s", g_get_user_cache_dir(), PROG_NAME, "tmux"));
 
307
    if(g_mkdir_with_parents(lcfg->tmuxsockpath, S_IRWXU | S_IRWXG) == -1) {
 
308
      g_warning("Failed to create %s: %s", lcfg->tmuxsockpath, g_strerror(errno));
 
309
      exit(1);
 
310
    }
 
311
#else
259
312
    lcfg->screenrcfile = getXPropStr(s_screen, "rc-file", g_strdup_printf("%s/%s/%s", g_get_user_config_dir(), PROG_NAME, "screenrc"));
260
313
    lcfg->screentitle = getXPropStr(s_screen, "title", g_strdup("%m # %U%H"));
261
 
 
 
314
#endif
262
315
 
263
316
    lcfg->ssh_agent = getXPropBool(s_ssh, "spawn-agent", FALSE);
264
317
 
268
321
      int i;
269
322
      for(i = 0; i < s_addkeys->nodeNr; i++) {
270
323
        lcfg->ssh_add[i] = g_strdup((gchar *)xmlGetProp(s_addkeys->nodeTab[i], BAD_CAST("name")));
 
324
        xmlChar *c = xmlGetProp(s_addkeys->nodeTab[i], BAD_CAST("fn"));
 
325
        if(!c) {
 
326
            g_printerr(_("Empty SSH key filename (%s/@fn) in configuration."), xmlGetNodePath(s_addkeys->nodeTab[i]));
 
327
            exit(1);
 
328
        }
 
329
        lcfg->ssh_add[i] = g_strdup((gchar *)c);
271
330
      }
 
331
      lcfg->ssh_numadd = s_addkeys->nodeNr;
272
332
    }
273
333
    xmlXPathFreeNodeSet(s_addkeys);
274
334
 
 
335
#ifdef FEAT_TMUX
 
336
    // XXX needs to be ported to tmux XXX
 
337
    lcfg->dump_screen = FALSE;
 
338
    lcfg->query_maintainer = FALSE;
 
339
    // XXX needs to be ported to tmux XXX
 
340
#else
275
341
    lcfg->dump_screen = !getXPropBool(s_screen, "no-dumps", FALSE);
276
342
    lcfg->query_maintainer = getXPropBool(s_screen, "query-maintainer", FALSE);
 
343
#endif
277
344
 
278
345
    gchar *colors = getXPropStr(s_appearance, "colors", "menu brightgreen blue;status brightgreen blue;selector black red;");
279
346
    if(colors)
294
361
#ifdef FEAT_HISTORY
295
362
    lcfg->record_history = getXPropBool(s_history, "record", TRUE);
296
363
    lcfg->history_errpattern = getXPropStr(s_history, "err-pattern", "((?<!no )error|warning|fail)");
 
364
    lcfg->history_dir = getXPropStr(s_path, "history-dir", g_strdup_printf("%s/%s/history", g_get_user_data_dir(), PACKAGE));
297
365
#endif
298
366
 
299
367
    lcfg->hook_pre_upgrade = getXPropStr(s_hooks, "pre-upgrade", "/etc/apt-dater/pre-upg.d");
311
379
    return (TRUE);
312
380
}
313
381
 
 
382
gint cmp_hosts(gconstpointer a, gconstpointer b, gpointer p) {
 
383
  HostNode *ha = (HostNode *)a;
 
384
  HostNode *hb = (HostNode *)b;
 
385
  gint ret;
 
386
 
 
387
  ret = g_strcmp0(ha->group, hb->group);
 
388
  if(ret == 0)
 
389
    ret = g_strcmp0(ha->hostname, hb->hostname);
 
390
 
 
391
  return ret;
 
392
}
 
393
 
314
394
GList *loadHosts (const gchar *filename) {
315
395
    /* Parse hosts.xml document. */
316
396
    xmlDocPtr xcfg = xmlParseFile(filename);
317
397
    if(xcfg == NULL)
318
398
      return(FALSE);
319
399
 
 
400
    /* Handle Xincludes. */
 
401
    xmlXIncludeProcess(xcfg);
 
402
 
 
403
    /* Validate against DTD. */
 
404
    xmlValidCtxtPtr xval = xmlNewValidCtxt();
 
405
    if(xmlValidateDocument(xval, xcfg) == 0) {
 
406
      xmlFreeValidCtxt(xval);
 
407
      return(FALSE);
 
408
    }
 
409
    xmlFreeValidCtxt(xval);
 
410
 
320
411
    /* Allocate XPath context. */
321
412
    xmlXPathContextPtr xctx = xmlXPathNewContext(xcfg);
322
413
    if(!xctx) {
384
475
        hostnode->group = g_strdup((gchar *)groupname);
385
476
 
386
477
        hostnode->statsfile = g_strdup_printf("%s/%s:%d.stat", cfg->statsdir, hostnode->hostname, hostnode->ssh_port);
 
478
        hostnode->statstmpf = g_strdup_printf("%s/%s:%d.stat.new", cfg->statsdir, hostnode->hostname, hostnode->ssh_port);
387
479
        hostnode->fdlock = -1;
388
480
        hostnode->uuid[0] = 0;
389
481
        hostnode->tagged = FALSE;
390
482
 
391
483
        getUpdatesFromStat(hostnode);
392
484
 
393
 
        hostlist = g_list_append(hostlist, hostnode);
 
485
        TTYMUX_INITIALIZE(hostnode);
 
486
        stats_initialize(hostnode);
 
487
 
 
488
        hostlist = g_list_prepend(hostlist, hostnode);
394
489
 
395
490
        xmlFree(hostname);
396
491
      }
400
495
    }
401
496
 
402
497
    xmlXPathFreeObject(groups);
403
 
    return hostlist;
 
498
    return g_list_sort_with_data(hostlist, cmp_hosts, NULL);
404
499
}