summaryrefslogtreecommitdiff
path: root/utils/frobauth.c
blob: 5de77209c6378b827cfc5ca7936c014870528ff7 (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
/* Common interface for auth frobbing utilities

   Copyright (C) 1997 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This program 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 2, or (at
   your option) any later version.

   This program 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

/* This file is rather a mess of intertwined argps; it shoud be redone as a
   single level once argp can handle dynamic option frobbing.  XXX */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <hurd.h>
#include <hurd/process.h>

#include "frobauth.h"
#include "ugids.h"
#include "pids.h"

static struct argp common_argp;
static struct argp fr_ugids_argp;

static const struct argp_option common_options[] =
{
  {"verbose",   'v', 0, 0, "Print informational messages"},
  {"dry-run",   'n', 0, 0, "Don't do anything, just print what would be done"},
  { 0 }
};
static struct argp_child common_child_argps[] =
{
  { &pids_argp, 0, "Process selection:" },
  { 0 }
};

static const char common_args_doc[] = "USER...";
static const char common_doc[] =
  "\vBy default, all processes in the current login collection are selected";

static struct argp_child ugids_child_argps[] =
{
  { &ugids_argp, 0, "User/group ids:" },
  { 0 }
};

/* An argp on top of the base frobauth argp that provides switchable
   effective/available ids (XXX this junk should be moved into a single argp
   [indeed, into ugids_argp] once argp can deal with the init routine
   frobbing the argp source).  */
static const struct argp_option ea_options[] =
{
  {"available", 'a', 0, 0, "USER... specifies available ids"},
  {"effective", 'e', 0, 0, "USER... specifies effective ids"},
  { 0 }
};

static struct argp_child ea_posix_child_argps[] =
{
  { &common_argp },
  { &fr_ugids_argp },
  { 0 }
};

static struct argp_child no_ugids_child_argps[] =
{
  { &common_argp },
  { 0 }
};

/* This holds state information that's only active during parsing.  */
struct frobauth_argp_state
{
  struct frobauth *frobauth;
  struct pids_argp_params pids_argp_params;
  struct ugids_argp_params ugids_argp_params;
};

static error_t
common_parse_opt (int key, char *arg, struct argp_state *state)
{
  struct frobauth_argp_state *fs = state->input;
  struct frobauth *frobauth = fs->frobauth;

  switch (key)
    {
    case 'v':
      frobauth->verbose = 1; break;
    case 'n':
      frobauth->dry_run = 1; break;

    case ARGP_KEY_END:
      if (frobauth->num_pids == 0)
	/* No pids specified!  By default, do the current login collection.  */
	{
	  pid_t lid;
	  error_t err = proc_getloginid (getproc (), getpid (), &lid);

	  if (err)
	    argp_failure (state, 2, err,
			  "Couldn't get current login collection");

	  err = add_fn_pids (&frobauth->pids, &frobauth->num_pids,
			     lid, proc_getloginpids);
	  if (err)
	    argp_failure (state, 3, err,
			  "%d: Couldn't get login collection pids", lid);

	  return err;
	}
      break;

    case ARGP_KEY_INIT:
      memset (fs, 0, sizeof *fs);
      fs->frobauth = frobauth;
      fs->pids_argp_params.pids = &frobauth->pids;
      fs->pids_argp_params.num_pids = &frobauth->num_pids;
      state->child_inputs[0] = &fs->pids_argp_params;
      break;

    case ARGP_KEY_SUCCESS:
    case ARGP_KEY_ERROR:
      free (fs);

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static error_t
ugids_parse_opt (int key, char *arg, struct argp_state *state)
{
  struct frobauth_argp_state *fs = state->input;
  struct frobauth *frobauth = fs->frobauth;

  switch (key)
    {
    case ARGP_KEY_INIT:
      fs->ugids_argp_params.ugids = &frobauth->ugids;
      fs->ugids_argp_params.parse_user_args = 1;
      fs->ugids_argp_params.default_user = frobauth->default_user;
      fs->ugids_argp_params.require_ids = frobauth->require_ids;
      fs->pids_argp_params.pids = &frobauth->pids;
      fs->pids_argp_params.num_pids = &frobauth->num_pids;

      state->child_inputs[0] = &fs->ugids_argp_params;

      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static error_t
ea_parse_opt (int key, char *arg, struct argp_state *state)
{
  struct frobauth_argp_state *fs = state->hook;

  switch (key)
    {
    case 'a':
      fs->ugids_argp_params.user_args_are_available = 1;
      break;
    case 'e':
      fs->ugids_argp_params.user_args_are_effective = 1;
      break;

    case ARGP_KEY_ARG:
      if (!fs->ugids_argp_params.user_args_are_effective
	  && !fs->ugids_argp_params.user_args_are_available)
	/* Default to effective.  */
	fs->ugids_argp_params.user_args_are_effective = 1;

      /* Let someone else parse the arg.  */
      return ARGP_ERR_UNKNOWN;

    case ARGP_KEY_INIT:
      /* Initialize inputs for child parsers.  */
      fs = state->hook = malloc (sizeof (struct frobauth_argp_state));
      if (! fs)
	return ENOMEM;

      fs->frobauth = state->input;
      state->child_inputs[0] = fs; /* Pass our state to the common parser.  */
      state->child_inputs[1] = fs; /* Pass our state to the common parser.  */
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static error_t
posix_parse_opt (int key, char *arg, struct argp_state *state)
{
  struct frobauth_argp_state *fs;

  switch (key)
    {
    case ARGP_KEY_INIT:
      /* Initialize inputs for child parsers.  */
      fs = state->hook = malloc (sizeof (struct frobauth_argp_state));
      if (! fs)
	return ENOMEM;

      fs->frobauth = state->input;
      state->child_inputs[0] = fs; /* Pass our state to the common parser.  */
      state->child_inputs[1] = fs; /* Pass our state to the common parser.  */
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static error_t
no_ugids_parse_opt (int key, char *arg, struct argp_state *state)
{
  struct frobauth_argp_state *fs;

  switch (key)
    {
    case ARGP_KEY_INIT:
      /* Initialize inputs for child parsers.  */
      fs = state->hook = malloc (sizeof (struct frobauth_argp_state));
      if (! fs)
	return ENOMEM;

      fs->frobauth = state->input;
      state->child_inputs[0] = fs; /* Pass our state to the common parser.  */
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp common_argp =
{
  common_options, common_parse_opt, 0, common_doc, common_child_argps
};
static struct argp fr_ugids_argp =
{
  0, ugids_parse_opt, 0, 0, ugids_child_argps
};

/* Parse frobauth args/options, where user args are added as single ids to
   either the effective or available ids.  */
struct argp frobauth_ea_argp =
{
  ea_options, ea_parse_opt, 0, 0, ea_posix_child_argps
};

/* Parse frobauth args/options, where user args are added as posix user.  */
struct argp frobauth_posix_argp =
{
  0, posix_parse_opt, 0, 0, ea_posix_child_argps
};

/* Parse frobauth args/options, where user args are added as posix user.  */
struct argp frobauth_no_ugids_argp =
{
  0, no_ugids_parse_opt, 0, 0, no_ugids_child_argps
};