File: boltctl-uidfmt.c

package info (click to toggle)
bolt 0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,396 kB
  • sloc: ansic: 21,784; python: 1,574; xml: 426; sh: 25; makefile: 13
file content (216 lines) | stat: -rw-r--r-- 4,795 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright © 2018 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

#include "bolt-str.h"
#include "bolt-term.h"

#include "boltctl-uidfmt.h"

#include <gio/gio.h>

#include <math.h>
#include <stdlib.h>

char *
bolt_uuid_format (const char *uuid,
                  const char *salt,
                  int         fmt)
{
  g_autoptr(GChecksum) chk = NULL;
  const char *tmp = NULL;
  guint op;
  int len;
  char *res;

  if (uuid == NULL)
    return NULL;

  g_return_val_if_fail (fmt > -1, NULL);

  op = fmt & 0xFF;

  switch (op)
    {
    case BOLT_UID_FORMAT_FULL:
      res = g_strdup (uuid);
      break;

    case BOLT_UID_FORMAT_SHORT:
      res = g_strdup_printf ("%.13s%s", uuid, bolt_glyph (ELLIPSIS));
      break;

    case BOLT_UID_FORMAT_ALIAS:
      chk = g_checksum_new (G_CHECKSUM_SHA1);
      if (salt)
        g_checksum_update (chk, (const guchar *) salt, -1);
      g_checksum_update (chk, (const guchar *) uuid, -1);

      tmp = g_checksum_get_string (chk);
      res = g_strdup_printf ("%.8s-%.4s-%.4s-%.4s-%.12s",
                             tmp, tmp + 8, tmp + 12, tmp + 16, tmp + 20);
      break;

    case BOLT_UID_FORMAT_LEN:
      len = MIN (fmt >> 8, 36);
      res = g_strdup_printf ("%.*s%s", len, uuid,
                             (len < 36 ? bolt_glyph (ELLIPSIS) : ""));
      break;

    default:
      res = NULL;
      g_warning ("unknown uuid format enum value: %d", fmt);
    }

  return res;
}


int
bolt_uuid_format_from_string (const char *str,
                              GError    **error)
{
  char *end;
  gint64 val;

  if (bolt_streq (str, "short"))
    return BOLT_UID_FORMAT_SHORT;
  else if (bolt_streq (str, "full"))
    return BOLT_UID_FORMAT_FULL;
  else if (bolt_streq (str, "alias"))
    return BOLT_UID_FORMAT_ALIAS;

  val = g_ascii_strtoll (str, &end, 10);

  if (str == end)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                   "uuid format: unknown style: %s", str);
      return -1;
    }
  else if (val < 1)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                   "uuid format: invalid number: %s", str);
      return -1;
    }
  else if (val > 36)
    {
      return BOLT_UID_FORMAT_FULL;
    }

  return BOLT_UID_FORMAT_LEN | (val << 8);
}


/*  */
int uuids_format;
GHashTable *uuids_table;
gboolean uuids_cleanup;
char *uuids_salt;

static void
format_uid_cleanup (void)
{
  g_clear_pointer (&uuids_table, g_hash_table_unref);
  g_clear_pointer (&uuids_salt, g_free);
  uuids_format = 0;
}

#define MACHINE_ID_PATH "/etc/machine-id"
#define BOOT_ID_PATH "/proc/sys/kernel/random/boot_id"

static char *
get_salt (void)
{
  char *salt = NULL;
  gboolean ok;

  ok = g_file_get_contents (MACHINE_ID_PATH, &salt, NULL, NULL);

  if (ok && !bolt_strzero (salt))
    {
      g_debug ("using machine-id as salt");
      return salt;
    }

  ok = g_file_get_contents (BOOT_ID_PATH, &salt, NULL, NULL);
  if (ok && !bolt_strzero (salt))
    {
      g_debug ("using boot-id as salt");
      return salt;
    }

  g_debug ("using PACKAGE_VERSION as pseudo-salt :(");
  return g_strdup (PACKAGE_VERSION);
}

int
format_uid_init (const char *str,
                 GError    **error)
{
  int fmt;

  g_return_val_if_fail (str != NULL, -1);

  fmt = bolt_uuid_format_from_string (str, error);
  if (fmt < 0)
    return fmt;

  uuids_format = fmt;

  if (uuids_table)
    g_hash_table_unref (uuids_table);

  uuids_table = g_hash_table_new_full (g_str_hash, g_str_equal,
                                       g_free, g_free);

  uuids_salt = get_salt ();

  if (!uuids_cleanup)
    {
      uuids_cleanup = TRUE;
      atexit (format_uid_cleanup);
    }

  return fmt;
}

const char *
format_uid (const char *uid)
{
  const char *res;
  char *key, *val;

  if (uid == NULL)
    uid = "<null>";

  res = g_hash_table_lookup (uuids_table, uid);

  if (res != NULL)
    return res;

  key = g_strdup (uid);
  val = bolt_uuid_format (uid, uuids_salt, uuids_format);
  g_hash_table_insert (uuids_table, key, val);

  return val;
}