summaryrefslogtreecommitdiff
path: root/usr/src/cmd/rpcsvc/nis/utils/nischmod.c
blob: 823d9e1d776ee0ec2f3c0a2392e01e738b69d0a4 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 *	nischmod.c
 *
 * Copyright 1988-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * nischmod.c
 *
 * nis+ object chmod/chown/chgrp/chttl utility
 */

#include <stdio.h>
#include <string.h>
#include <rpc/rpc.h>
#include <rpcsvc/nis.h>
#include <stdlib.h>

extern int 	optind;
extern char	*optarg;

extern bool_t nis_verifycred();

#define	CMD_CHMOD 0
#define	CMD_CHOWN 1
#define	CMD_CHGRP 2
#define	CMD_CHTTL 3

int cmdnum;
char *cmdname;

void
usage()
{
	char *str;

	switch (cmdnum) {
	case CMD_CHMOD:
		str = "mode";
		break;
	case CMD_CHOWN:
		str = "owner";
		break;
	case CMD_CHGRP:
		str = "group";
		break;
	case CMD_CHTTL:
		str = "ttl";
		break;
	}

	fprintf(stderr, "usage: %s [-LPAf] %s name ...\n",
		cmdname, str);
	exit(1);
}


void
change_object(obj, udata)
	nis_object *obj;
	void *udata;
{
	switch (cmdnum) {
	case CMD_CHMOD:
		parse_rights(&(obj->zo_access), (char *)udata);
		break;
	case CMD_CHOWN:
		obj->zo_owner = (nis_name)udata;
		break;
	case CMD_CHGRP:
		obj->zo_group = (nis_name)udata;
		break;
	case CMD_CHTTL:
		obj->zo_ttl = *(ulong_t *)udata;
		if (obj->zo_data.zo_type == NIS_DIRECTORY_OBJ)
			obj->DI_data.do_ttl = *(ulong_t *)udata;
		break;
	}
}


static char force = 0;
static int error = 0;

static int
change_entry(name, ent, udata)
	nis_name 	name;
	nis_object	*ent;
	void		*udata;
{
	nis_object	newobj;
	nis_result	*res;

	newobj = *ent;
	change_object(&newobj, udata);
	res = nis_modify_entry(name, &newobj, MOD_SAMEOBJ);
	if (res->status != NIS_SUCCESS) {
		if (!force) {
			nis_perror(res->status, "can't modify entry");
			error = 1;
		}
	}
	nis_freeresult(res);
	return (0);
}


main(argc, argv)
	int argc;
	char *argv[];
{
	int c;
	char **plist, **p;
	ulong_t flinks = 0, fpath = 0, allres = 0;
	char *name;
	void *udata;
	uint_t	x;
	int bad_name;
	nis_result *ores, *mres;
	char oname[NIS_MAXNAMELEN];
	nis_object newobj;

	if (cmdname = strrchr(argv[0], '/'))
		cmdname++;
	else
		cmdname = argv[0];

	if (strcmp(cmdname, "nischmod") == 0)
		cmdnum = CMD_CHMOD;
	else if (strcmp(cmdname, "nischown") == 0)
		cmdnum = CMD_CHOWN;
	else if (strcmp(cmdname, "nischgrp") == 0)
		cmdnum = CMD_CHGRP;
	else if (strcmp(cmdname, "nischttl") == 0)
		cmdnum = CMD_CHTTL;
	else {
		fprintf(stderr, "%s: bad command.\n", cmdname);
		exit(1);
	}

	while ((c = getopt(argc, argv, "LPAf")) != -1) {
		switch (c) {
		case 'L':
			flinks = FOLLOW_LINKS;
			break;
		case 'P':
			fpath = FOLLOW_PATH;
			break;
		case 'A':
			allres = ALL_RESULTS;
			break;
		case 'f':
			force = 1;
			break;
		default:
			usage();
		}
	}

	if (argc - optind < 2)
		usage();

	switch (cmdnum) {
	case CMD_CHMOD:
		/*
		 * attempt to parse the access rights first
		 */
		udata = argv[optind++];
		if (!parse_rights(&x, udata)) {
			fprintf(stderr, "Can't parse access rights \"%s\"\n",
					argv[optind-1]);
			usage();
		}
		break;
	case CMD_CHOWN:
		/*
		 * get the principal name using psuedo expand name
		 * magic
		 */
		udata = argv[optind++];
		if ((plist = nis_getnames(udata)) == 0) {
			/* Could be either no mem or more likely bad name */
			nis_perror(NIS_BADNAME, udata);
			exit(1);
		}
		for (p = plist; *p; p++) {
			ulong_t flags;
			/*
			 * First try to verify through regular means and only
			 * if that fails, then try the Master server.
			 */
			flags = (ulong_t)(USE_DGRAM|FOLLOW_LINKS|FOLLOW_PATH);
			if (nis_verifycred(*p, flags) ||
			    nis_verifycred(*p, (ulong_t)(flags | MASTER_ONLY)))
				break;
		}
		if (*p == 0) {
			if (!force) {
				fprintf(stderr,
					"%s: principal not found\n",
					udata);
				exit(1);
			} else if (
				((char *)udata)[strlen((char *)udata)-1] != '.')
				exit(0);
		} else
			udata = *p;
		break;
	case CMD_CHGRP:
		/*
		 * get the group name using psuedo expand name magic
		 */
		udata = argv[optind++];
		if ((plist = nis_getnames(udata)) == 0) {
			/* Could be either no mem or more likely bad name */
			nis_perror(NIS_BADNAME, udata);
			exit(1);
		}
		for (p = plist; *p; p++)
			if (nis_verifygroup(*p) == NIS_SUCCESS)
				break;
		if (*p == 0) {
			if (!force) {
				fprintf(stderr,
					"%s: group not found\n",
					udata);
				exit(1);
			} else if (
				((char *)udata)[strlen((char *)udata)-1] != '.')
				exit(0);
		} else
			udata = *p;
		break;
	case CMD_CHTTL:
		if (!parse_time(&x, argv[optind++])) {
			fprintf(stderr, "can't parse time \"%s\"\n",
					argv[optind-1]);
			usage();
		}
		udata = &x;
		break;
	}

	while (argc > optind) {
		name = argv[optind++];

		/*
		 * Get the object using expand name magic.
		 */
		if (*name == '[') {
			ores = nis_list(name,
			    fpath|allres|FOLLOW_LINKS|MASTER_ONLY|EXPAND_NAME,
					change_entry, udata);
			if (ores->status != NIS_CBRESULTS &&
			    ores->status != NIS_NOTFOUND) {
				if (!force) {
					nis_perror(ores->status, name);
					error = 1;
				}
			}
		} else {
			ores = nis_lookup(name, flinks|MASTER_ONLY|EXPAND_NAME);
			if (ores->status != NIS_SUCCESS) {
				if (!force) {
					nis_perror(ores->status, name);
					error = 1;
				}
				goto loop;
			}

			/*
			 * Construct the name for the object that we found.
			 */
			bad_name = (snprintf(oname, sizeof (oname), "%s.",
					ores->objects.objects_val[0].zo_name)
						>= sizeof (oname));
			if (!bad_name &&
			    *(ores->objects.objects_val[0].zo_domain) != '.')
				bad_name = (strlcat(oname,
					ores->objects.objects_val[0].zo_domain,
					sizeof (oname)) >= sizeof (oname));

			if (bad_name) {
				if (!force) {
					fprintf(stderr, "%s: ", oname);
					nis_perror(NIS_BADNAME,
						"can't modify object");
					error = 1;
				}
				goto loop;
			}

			/*
			 * Modify the object.
			 */
			newobj = ores->objects.objects_val[0];
			change_object(&newobj, udata);
			mres = nis_modify(oname, &newobj);
			if (mres->status != NIS_SUCCESS) {
				if (!force) {
					fprintf(stderr, "%s: ", oname);
					nis_perror(mres->status,
						"can't modify object");
					error = 1;
				}
			}
			nis_freeresult(mres);
		}

	loop:
		nis_freeresult(ores);
	}

	exit(error);
}