summaryrefslogtreecommitdiff
path: root/src/map.c
blob: 0cd986192c19f99aa9d13d60b8e6c4822c3e2265 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* Owner/group mapping for tar

   Copyright 2015-2016 Free Software Foundation, Inc.

   This file is part of GNU tar.

   GNU tar 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.

   GNU tar 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 this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <system.h>
#include "common.h"
#include "wordsplit.h"
#include <hash.h>
#include <pwd.h>

struct mapentry
{
  uintmax_t orig_id;
  uintmax_t new_id;
  char *new_name;
};

static size_t
map_hash (void const *entry, size_t nbuckets)
{
  struct mapentry const *map = entry;
  return map->orig_id % nbuckets;
}

static bool
map_compare (void const *entry1, void const *entry2)
{
  struct mapentry const *map1 = entry1;
  struct mapentry const *map2 = entry2;
  return map1->orig_id == map2->orig_id;
}

static int
parse_id (uintmax_t *retval,
	  char const *arg, char const *what, uintmax_t maxval,
	  char const *file, unsigned line)
{
  uintmax_t v;
  char *p;
  
  errno = 0;
  v = strtoumax (arg, &p, 10);
  if (*p || errno)
    {
      error (0, 0, _("%s:%u: invalid %s: %s"),  file, line, what, arg);
      return -1;
    }
  if (v > maxval)
    {
      error (0, 0, _("%s:%u: %s out of range: %s"), file, line, what, arg);
      return -1;
    }
  *retval = v;
  return 0;
}

static void
map_read (Hash_table **ptab, char const *file,
	  uintmax_t (*name_to_id) (char const *), char const *what,
	  uintmax_t maxval)
{
  FILE *fp;
  char *buf = NULL;
  size_t bufsize = 0;
  ssize_t n;
  struct wordsplit ws;
  int wsopt;
  unsigned line;
  int err = 0;
  
  fp = fopen (file, "r");
  if (!fp)
    open_fatal (file);

  ws.ws_comment = "#";
  wsopt = WRDSF_COMMENT | WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_SQUEEZE_DELIMS
          | WRDSF_QUOTE;
  line = 0;
  while ((n = getline (&buf, &bufsize, fp)) > 0)
    {
      struct mapentry *ent;
      uintmax_t orig_id, new_id;
      char *name = NULL;
      char *colon;
      
      ++line;
      if (wordsplit (buf, &ws, wsopt))
	FATAL_ERROR ((0, 0, _("%s:%u: cannot split line: %s"),
		      file, line, wordsplit_strerror (&ws)));
      wsopt |= WRDSF_REUSE;
      if (ws.ws_wordc == 0)
	continue;
      if (ws.ws_wordc != 2)
	{
	  error (0, 0, _("%s:%u: malformed line"), file, line);
	  err = 1;
	  continue;
	}

      if (ws.ws_wordv[0][0] == '+')
	{
	  if (parse_id (&orig_id, ws.ws_wordv[0]+1, what, maxval, file, line)) 
	    {
	      err = 1;
	      continue;
	    }
	}
      else if (name_to_id)
	{
	  orig_id = name_to_id (ws.ws_wordv[0]);
	  if (orig_id == UINTMAX_MAX)
	    {
	      error (0, 0, _("%s:%u: can't obtain %s of %s"),
		     file, line, what, ws.ws_wordv[0]);
	      err = 1;
	      continue;
	    }
	}

      colon = strchr (ws.ws_wordv[1], ':');
      if (colon)
	{
	  if (colon > ws.ws_wordv[1])
	    name = ws.ws_wordv[1];
	  *colon++ = 0;
	  if (parse_id (&new_id, colon, what, maxval, file, line)) 
	    {
	      err = 1;
	      continue;
	    }
	}
      else if (ws.ws_wordv[1][0] == '+')
	{
	  if (parse_id (&new_id, ws.ws_wordv[1], what, maxval, file, line)) 
	    {
	      err = 1;
	      continue;
	    }
	}
      else
	{
	  name = ws.ws_wordv[1];
	  new_id = name_to_id (ws.ws_wordv[1]);
	  if (new_id == UINTMAX_MAX)
	    {
	      error (0, 0, _("%s:%u: can't obtain %s of %s"),
		     file, line, what, ws.ws_wordv[1]);
	      err = 1;
	      continue;
	    }
	}

      ent = xmalloc (sizeof (*ent));
      ent->orig_id = orig_id;
      ent->new_id = new_id;
      ent->new_name = name ? xstrdup (name) : NULL;
      
      if (!((*ptab
	     || (*ptab = hash_initialize (0, 0, map_hash, map_compare, 0)))
	    && hash_insert (*ptab, ent)))
	xalloc_die ();
    }
  if (wsopt & WRDSF_REUSE)
    wordsplit_free (&ws);
  fclose (fp);
  if (err)
    FATAL_ERROR ((0, 0, _("errors reading map file")));
}

/* UID translation */

static Hash_table *owner_map;

static uintmax_t
name_to_uid (char const *name)
{
  struct passwd *pw = getpwnam (name);
  return pw ? pw->pw_uid : UINTMAX_MAX;
}

void
owner_map_read (char const *file)
{
  map_read (&owner_map, file, name_to_uid, "UID", TYPE_MAXIMUM (uid_t));
}

int
owner_map_translate (uid_t uid, uid_t *new_uid, char const **new_name)
{
  int rc = 1;
  
  if (owner_map)
    {
      struct mapentry ent, *res;
  
      ent.orig_id = uid;
      res = hash_lookup (owner_map, &ent);
      if (res)
	{
	  *new_uid = res->new_id;
	  *new_name = res->new_name;
	  return 0;
	}
    }

  if (owner_option != (uid_t) -1)
    {
      *new_uid = owner_option;
      rc = 0;
    }
  if (owner_name_option)
    {
      *new_name = owner_name_option;
      rc = 0;
    }

  return rc;
}

/* GID translation */

static Hash_table *group_map;

static uintmax_t
name_to_gid (char const *name)
{
  struct group *gr = getgrnam (name);
  return gr ? gr->gr_gid : UINTMAX_MAX;
}

void
group_map_read (char const *file)
{
  map_read (&group_map, file, name_to_gid, "GID", TYPE_MAXIMUM (gid_t));
}

int
group_map_translate (gid_t gid, gid_t *new_gid, char const **new_name)
{
  int rc = 1;
  
  if (group_map)
    {
      struct mapentry ent, *res;
  
      ent.orig_id = gid;
      res = hash_lookup (group_map, &ent);
      if (res)
	{
	  *new_gid = res->new_id;
	  *new_name = res->new_name;
	  return 0;
	}
    }

  if (group_option != (uid_t) -1)
    {
      *new_gid = group_option;
      rc = 0;
    }
  if (group_name_option)
    {
      *new_name = group_name_option;
      rc = 0;
    }
  
  return rc;
}