8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / oamuser / user / funcs.c
blob93f6b458d561634a190790d32c86bb64362197e1
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 RackTop Systems.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <strings.h>
29 #include <auth_attr.h>
30 #include <prof_attr.h>
31 #include <user_attr.h>
32 #include <project.h>
33 #include <secdb.h>
34 #include <pwd.h>
35 #include <unistd.h>
36 #include <priv.h>
37 #include <errno.h>
38 #include <ctype.h>
39 #include <nss.h>
40 #include <bsm/libbsm.h>
41 #include <tsol/label.h>
42 #include "funcs.h"
43 #include "messages.h"
44 #undef GROUP
45 #include "userdefs.h"
47 typedef struct ua_key {
48 const char *key;
49 const char *(*check)(const char *);
50 const char *errstr;
51 char *newvalue;
52 } ua_key_t;
54 static const char role[] = "role name";
55 static const char prof[] = "profile name";
56 static const char proj[] = "project name";
57 static const char priv[] = "privilege set";
58 static const char auth[] = "authorization";
59 static const char type[] = "user type";
60 static const char lock[] = "lock_after_retries value";
61 static const char label[] = "label";
62 static const char idlecmd[] = "idlecmd value";
63 static const char idletime[] = "idletime value";
64 static const char auditflags[] = "audit mask";
65 static char auditerr[256];
68 static const char *check_auth(const char *);
69 static const char *check_prof(const char *);
70 static const char *check_role(const char *);
71 static const char *check_proj(const char *);
72 static const char *check_privset(const char *);
73 static const char *check_type(const char *);
74 static const char *check_lock_after_retries(const char *);
75 static const char *check_label(const char *);
76 static const char *check_idlecmd(const char *);
77 static const char *check_idletime(const char *);
78 static const char *check_auditflags(const char *);
80 int nkeys;
82 static ua_key_t keys[] = {
83 /* First entry is always set correctly in main() */
84 { USERATTR_TYPE_KW, check_type, type },
85 { USERATTR_AUTHS_KW, check_auth, auth },
86 { USERATTR_PROFILES_KW, check_prof, prof },
87 { USERATTR_ROLES_KW, check_role, role },
88 { USERATTR_DEFAULTPROJ_KW, check_proj, proj },
89 { USERATTR_LIMPRIV_KW, check_privset, priv },
90 { USERATTR_DFLTPRIV_KW, check_privset, priv },
91 { USERATTR_LOCK_AFTER_RETRIES_KW, check_lock_after_retries, lock },
92 { USERATTR_CLEARANCE, check_label, label },
93 { USERATTR_MINLABEL, check_label, label },
94 { USERATTR_IDLECMD_KW, check_idlecmd, idlecmd },
95 { USERATTR_IDLETIME_KW, check_idletime, idletime },
96 { USERATTR_AUDIT_FLAGS_KW, check_auditflags, auditflags },
99 #define NKEYS (sizeof (keys)/sizeof (ua_key_t))
102 * Change a key, there are three different call sequences:
104 * key, value - key with option letter, value.
105 * NULL, value - -K key=value option.
108 void
109 change_key(const char *key, char *value)
111 int i;
112 const char *res;
114 if (key == NULL) {
115 key = value;
116 value = strchr(value, '=');
117 /* Bad value */
118 if (value == NULL) {
119 errmsg(M_INVALID_VALUE);
120 exit(EX_BADARG);
122 *value++ = '\0';
125 for (i = 0; i < NKEYS; i++) {
126 if (strcmp(key, keys[i].key) == 0) {
127 if (keys[i].newvalue != NULL) {
128 /* Can't set a value twice */
129 errmsg(M_REDEFINED_KEY, key);
130 exit(EX_BADARG);
133 if (keys[i].check != NULL &&
134 (res = keys[i].check(value)) != NULL) {
135 errmsg(M_INVALID, res, keys[i].errstr);
136 exit(EX_BADARG);
138 keys[i].newvalue = value;
139 nkeys++;
140 return;
143 errmsg(M_INVALID_KEY, key);
144 exit(EX_BADARG);
148 * Add the keys to the argument vector.
150 void
151 addkey_args(char **argv, int *index)
153 int i;
155 for (i = 0; i < NKEYS; i++) {
156 const char *key = keys[i].key;
157 char *val = keys[i].newvalue;
158 size_t len;
159 char *arg;
161 if (val == NULL)
162 continue;
164 len = strlen(key) + strlen(val) + 2;
165 arg = malloc(len);
167 (void) snprintf(arg, len, "%s=%s", key, val);
168 argv[(*index)++] = "-K";
169 argv[(*index)++] = arg;
174 * Propose a default value for a key and get the actual value back.
175 * If the proposed default value is NULL, return the actual value set.
176 * The key argument is the user_attr key.
178 char *
179 getsetdefval(const char *key, char *dflt)
181 int i;
183 for (i = 0; i < NKEYS; i++)
184 if (strcmp(keys[i].key, key) == 0) {
185 if (keys[i].newvalue != NULL)
186 return (keys[i].newvalue);
187 else
188 return (keys[i].newvalue = dflt);
190 return (NULL);
193 char *
194 getusertype(char *cmdname)
196 static char usertype[MAX_TYPE_LENGTH];
197 char *cmd;
199 if ((cmd = strrchr(cmdname, '/')))
200 ++cmd;
201 else
202 cmd = cmdname;
204 /* get user type based on the program name */
205 if (strncmp(cmd, CMD_PREFIX_USER,
206 strlen(CMD_PREFIX_USER)) == 0)
207 strcpy(usertype, USERATTR_TYPE_NORMAL_KW);
208 else
209 strcpy(usertype, USERATTR_TYPE_NONADMIN_KW);
211 return (usertype);
215 is_role(char *usertype)
217 if (strcmp(usertype, USERATTR_TYPE_NONADMIN_KW) == 0)
218 return (1);
219 /* not a role */
220 return (0);
224 * Verifies the provided list of authorizations are all valid.
226 * Returns NULL if all authorization names are valid.
227 * Otherwise, returns the invalid authorization name
230 static const char *
231 check_auth(const char *auths)
233 char *authname;
234 authattr_t *result;
235 char *tmp;
236 struct passwd *pw;
237 int have_grant = 0;
239 tmp = strdup(auths);
240 if (tmp == NULL) {
241 errmsg(M_NOSPACE);
242 exit(EX_FAILURE);
245 authname = strtok(tmp, AUTH_SEP);
246 pw = getpwuid(getuid());
247 if (pw == NULL) {
248 return (authname);
251 while (authname != NULL) {
252 char *suffix;
253 char *authtoks;
255 /* Check if user has been granted this authorization */
256 if (!chkauthattr(authname, pw->pw_name))
257 return (authname);
259 /* Remove named object after slash */
260 if ((suffix = index(authname, KV_OBJECTCHAR)) != NULL)
261 *suffix = '\0';
263 /* Find the suffix */
264 if ((suffix = rindex(authname, '.')) == NULL)
265 return (authname);
267 /* Check for existence in auth_attr */
268 suffix++;
269 if (strcmp(suffix, KV_WILDCARD)) { /* Not a wildcard */
270 result = getauthnam(authname);
271 if (result == NULL) {
272 /* can't find the auth */
273 free_authattr(result);
274 return (authname);
276 free_authattr(result);
279 /* Check if user can delegate this authorization */
280 if (strcmp(suffix, "grant")) { /* Not a grant option */
281 authtoks = malloc(strlen(authname) + sizeof ("grant"));
282 strcpy(authtoks, authname);
283 have_grant = 0;
284 while ((suffix = rindex(authtoks, '.')) &&
285 !have_grant) {
286 strcpy(suffix, ".grant");
287 if (chkauthattr(authtoks, pw->pw_name))
288 have_grant = 1;
289 else
290 *suffix = '\0';
292 if (!have_grant)
293 return (authname);
295 authname = strtok(NULL, AUTH_SEP);
297 free(tmp);
298 return (NULL);
302 * Verifies the provided list of profile names are valid.
304 * Returns NULL if all profile names are valid.
305 * Otherwise, returns the invalid profile name
308 static const char *
309 check_prof(const char *profs)
311 char *profname;
312 profattr_t *result;
313 char *tmp;
315 tmp = strdup(profs);
316 if (tmp == NULL) {
317 errmsg(M_NOSPACE);
318 exit(EX_FAILURE);
321 profname = strtok(tmp, PROF_SEP);
322 while (profname != NULL) {
323 result = getprofnam(profname);
324 if (result == NULL) {
325 /* can't find the profile */
326 return (profname);
328 free_profattr(result);
329 profname = strtok(NULL, PROF_SEP);
331 free(tmp);
332 return (NULL);
337 * Verifies the provided list of role names are valid.
339 * Returns NULL if all role names are valid.
340 * Otherwise, returns the invalid role name
343 static const char *
344 check_role(const char *roles)
346 char *rolename;
347 userattr_t *result;
348 char *utype;
349 char *tmp;
351 tmp = strdup(roles);
352 if (tmp == NULL) {
353 errmsg(M_NOSPACE);
354 exit(EX_FAILURE);
357 rolename = strtok(tmp, ROLE_SEP);
358 while (rolename != NULL) {
359 result = getusernam(rolename);
360 if (result == NULL) {
361 /* can't find the rolename */
362 return (rolename);
364 /* Now, make sure it is a role */
365 utype = kva_match(result->attr, USERATTR_TYPE_KW);
366 if (utype == NULL) {
367 /* no user type defined. not a role */
368 free_userattr(result);
369 return (rolename);
371 if (strcmp(utype, USERATTR_TYPE_NONADMIN_KW) != 0) {
372 free_userattr(result);
373 return (rolename);
375 free_userattr(result);
376 rolename = strtok(NULL, ROLE_SEP);
378 free(tmp);
379 return (NULL);
382 static const char *
383 check_proj(const char *proj)
385 if (getprojidbyname(proj) < 0) {
386 return (proj);
387 } else {
388 return (NULL);
392 static const char *
393 check_privset(const char *pset)
395 priv_set_t *tmp;
396 const char *res;
398 tmp = priv_str_to_set(pset, ",", &res);
400 if (tmp != NULL) {
401 res = NULL;
402 priv_freeset(tmp);
403 } else if (res == NULL)
404 res = strerror(errno);
406 return (res);
409 static const char *
410 check_type(const char *type)
412 if (strcmp(type, USERATTR_TYPE_NONADMIN_KW) != 0 &&
413 strcmp(type, USERATTR_TYPE_NORMAL_KW) != 0)
414 return (type);
416 return (NULL);
419 static const char *
420 check_lock_after_retries(const char *keyval)
422 if (keyval != NULL) {
423 if ((strcasecmp(keyval, "no") != 0) &&
424 (strcasecmp(keyval, "yes") != 0) &&
425 (*keyval != '\0')) {
426 return (keyval);
429 return (NULL);
432 static const char *
433 check_label(const char *labelstr)
435 int err;
436 m_label_t *lbl = NULL;
438 if (!is_system_labeled())
439 return (NULL);
441 err = str_to_label(labelstr, &lbl, MAC_LABEL, L_NO_CORRECTION, NULL);
442 m_label_free(lbl);
444 if (err == -1)
445 return (labelstr);
447 return (NULL);
450 static const char *
451 check_idlecmd(const char *cmd)
453 if ((strcmp(cmd, USERATTR_IDLECMD_LOCK_KW) != 0) &&
454 (strcmp(cmd, USERATTR_IDLECMD_LOGOUT_KW) != 0)) {
455 return (cmd);
458 return (NULL);
461 static const char *
462 check_idletime(const char *time)
464 int c;
465 unsigned char *up = (unsigned char *)time;
467 c = *up;
468 while (c != '\0') {
469 if (!isdigit(c))
470 return (time);
471 c = *++up;
474 return (NULL);
477 static const char *
478 check_auditflags(const char *auditflags)
480 au_mask_t mask;
481 char *flags;
482 char *last = NULL;
483 char *err = "NULL";
485 /* if deleting audit_flags */
486 if (*auditflags == '\0') {
487 return (NULL);
490 if ((flags = _strdup_null((char *)auditflags)) == NULL) {
491 errmsg(M_NOSPACE);
492 exit(EX_FAILURE);
495 if (!__chkflags(_strtok_escape(flags, KV_AUDIT_DELIMIT, &last), &mask,
496 B_FALSE, &err)) {
497 (void) snprintf(auditerr, sizeof (auditerr),
498 "always mask \"%s\"", err);
499 free(flags);
500 return (auditerr);
502 if (!__chkflags(_strtok_escape(NULL, KV_AUDIT_DELIMIT, &last), &mask,
503 B_FALSE, &err)) {
504 (void) snprintf(auditerr, sizeof (auditerr),
505 "never mask \"%s\"", err);
506 free(flags);
507 return (auditerr);
509 if (last != NULL) {
510 (void) snprintf(auditerr, sizeof (auditerr), "\"%s\"",
511 auditflags);
512 free(flags);
513 return (auditerr);
515 free(flags);
517 return (NULL);