sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libbsm / common / au_to.c
blob1f1b32037ee4fb35ea53ab9b46a8d9057cd1d37e
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 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <bsm/audit.h>
30 #include <bsm/audit_record.h>
31 #include <bsm/libbsm.h>
32 #include <priv.h>
33 #include <sys/ipc.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #include <sys/vnode.h>
38 #include <malloc.h>
39 #include <net/route.h>
40 #include <netinet/in.h>
41 #include <netinet/in_pcb.h>
42 #include <string.h>
43 #include <ucred.h>
44 #include <zone.h>
46 #define NGROUPS 16 /* XXX - temporary */
48 token_t *au_to_arg(char n, char *text, uint32_t v);
49 #pragma weak au_to_arg = au_to_arg32
50 token_t *au_to_return(char number, uint32_t value);
51 #pragma weak au_to_return = au_to_return32
53 static token_t *au_to_exec(char **, char);
55 static token_t *
56 get_token(int s)
58 token_t *token; /* Resultant token */
60 if ((token = (token_t *)malloc(sizeof (token_t))) == NULL)
61 return (NULL);
62 if ((token->tt_data = malloc(s)) == NULL) {
63 free(token);
64 return (NULL);
66 token->tt_size = s;
67 token->tt_next = NULL;
68 return (token);
72 * au_to_header
73 * return s:
74 * pointer to header token.
76 token_t *
77 au_to_header(au_event_t e_type, au_emod_t e_mod)
79 adr_t adr; /* adr memory stream header */
80 token_t *token; /* token pointer */
81 char version = TOKEN_VERSION; /* version of token family */
82 int32_t byte_count;
83 struct timeval tv;
84 #ifdef _LP64
85 char data_header = AUT_HEADER64; /* header for this token */
87 token = get_token(2 * sizeof (char) + sizeof (int32_t) +
88 2 * sizeof (int64_t) + 2 * sizeof (short));
89 #else
90 char data_header = AUT_HEADER32;
92 token = get_token(2 * sizeof (char) + 3 * sizeof (int32_t) +
93 2 * sizeof (short));
94 #endif
96 if (token == NULL)
97 return (NULL);
98 adr_start(&adr, token->tt_data);
99 adr_char(&adr, &data_header, 1); /* token ID */
100 adr_int32(&adr, &byte_count, 1); /* length of audit record */
101 adr_char(&adr, &version, 1); /* version of audit tokens */
102 adr_ushort(&adr, &e_type, 1); /* event ID */
103 adr_ushort(&adr, &e_mod, 1); /* event ID modifier */
104 #ifdef _LP64
105 adr_int64(&adr, (int64_t *)&tv, 2); /* time & date */
106 #else
107 adr_int32(&adr, (int32_t *)&tv, 2); /* time & date */
108 #endif
109 return (token);
113 * au_to_header_ex
114 * return s:
115 * pointer to header token.
117 token_t *
118 au_to_header_ex(au_event_t e_type, au_emod_t e_mod)
120 adr_t adr; /* adr memory stream header */
121 token_t *token; /* token pointer */
122 char version = TOKEN_VERSION; /* version of token family */
123 int32_t byte_count;
124 struct timeval tv;
125 auditinfo_addr_t audit_info;
126 au_tid_addr_t *host_info = &audit_info.ai_termid;
127 #ifdef _LP64
128 char data_header = AUT_HEADER64_EX; /* header for this token */
129 #else
130 char data_header = AUT_HEADER32_EX;
131 #endif
133 /* If our host address can't be determined, revert to un-extended hdr */
135 if (auditon(A_GETKAUDIT, (caddr_t)&audit_info,
136 sizeof (audit_info)) < 0)
137 return (au_to_header(e_type, e_mod));
139 if (host_info->at_type == AU_IPv6)
140 if (IN6_IS_ADDR_UNSPECIFIED((in6_addr_t *)host_info->at_addr))
141 return (au_to_header(e_type, e_mod));
142 else
143 if (host_info->at_addr[0] == htonl(INADDR_ANY))
144 return (au_to_header(e_type, e_mod));
146 #ifdef _LP64
147 token = get_token(2 * sizeof (char) + sizeof (int32_t) +
148 2 * sizeof (int64_t) + 2 * sizeof (short) +
149 sizeof (int32_t) + host_info->at_type);
150 #else
151 token = get_token(2 * sizeof (char) + 3 * sizeof (int32_t) +
152 2 * sizeof (short) + sizeof (int32_t) + host_info->at_type);
153 #endif
155 if (token == NULL)
156 return (NULL);
157 adr_start(&adr, token->tt_data);
158 adr_char(&adr, &data_header, 1); /* token ID */
159 adr_int32(&adr, &byte_count, 1); /* length of audit record */
160 adr_char(&adr, &version, 1); /* version of audit tokens */
161 adr_ushort(&adr, &e_type, 1); /* event ID */
162 adr_ushort(&adr, &e_mod, 1); /* event ID modifier */
163 adr_int32(&adr, (int32_t *)&host_info->at_type, 1);
164 adr_char(&adr, (char *)host_info->at_addr,
165 (int)host_info->at_type);
166 #ifdef _LP64
167 adr_int64(&adr, (int64_t *)&tv, 2); /* time & date */
168 #else
169 adr_int32(&adr, (int32_t *)&tv, 2); /* time & date */
170 #endif
171 return (token);
175 * au_to_trailer
176 * return s:
177 * pointer to a trailer token.
179 token_t *
180 au_to_trailer(void)
182 adr_t adr; /* adr memory stream header */
183 token_t *token; /* token pointer */
184 char data_header = AUT_TRAILER; /* header for this token */
185 short magic = (short)AUT_TRAILER_MAGIC; /* trailer magic number */
186 int32_t byte_count;
188 token = get_token(sizeof (char) + sizeof (int32_t) + sizeof (short));
189 if (token == NULL)
190 return (NULL);
191 adr_start(&adr, token->tt_data);
192 adr_char(&adr, &data_header, 1); /* token ID */
193 adr_short(&adr, &magic, 1); /* magic number */
194 adr_int32(&adr, &byte_count, 1); /* length of audit record */
196 return (token);
200 * au_to_arg32
201 * return s:
202 * pointer to an argument token.
204 token_t *
205 au_to_arg32(char n, char *text, uint32_t v)
207 token_t *token; /* local token */
208 adr_t adr; /* adr memory stream header */
209 char data_header = AUT_ARG32; /* header for this token */
210 short bytes; /* length of string */
212 bytes = strlen(text) + 1;
214 token = get_token((int)(2 * sizeof (char) + sizeof (int32_t) +
215 sizeof (short) + bytes));
216 if (token == NULL)
217 return (NULL);
218 adr_start(&adr, token->tt_data);
219 adr_char(&adr, &data_header, 1); /* token type */
220 adr_char(&adr, &n, 1); /* argument id */
221 adr_int32(&adr, (int32_t *)&v, 1); /* argument value */
222 adr_short(&adr, &bytes, 1);
223 adr_char(&adr, text, bytes);
225 return (token);
229 * au_to_arg64
230 * return s:
231 * pointer to an argument token.
233 token_t *
234 au_to_arg64(char n, char *text, uint64_t v)
236 token_t *token; /* local token */
237 adr_t adr; /* adr memory stream header */
238 char data_header = AUT_ARG64; /* header for this token */
239 short bytes; /* length of string */
241 bytes = strlen(text) + 1;
243 token = get_token((int)(2 * sizeof (char) + sizeof (int64_t) +
244 sizeof (short) + bytes));
245 if (token == NULL)
246 return (NULL);
247 adr_start(&adr, token->tt_data);
248 adr_char(&adr, &data_header, 1); /* token type */
249 adr_char(&adr, &n, 1); /* argument id */
250 adr_int64(&adr, (int64_t *)&v, 1); /* argument value */
251 adr_short(&adr, &bytes, 1);
252 adr_char(&adr, text, bytes);
254 return (token);
259 * au_to_attr
260 * return s:
261 * pointer to an attribute token.
263 token_t *
264 au_to_attr(struct vattr *attr)
266 token_t *token; /* local token */
267 adr_t adr; /* adr memory stream header */
268 int32_t value;
269 #ifdef _LP64
270 char data_header = AUT_ATTR64; /* header for this token */
272 token = get_token(sizeof (char) +
273 sizeof (int32_t) * 4 +
274 sizeof (int64_t) * 2);
275 #else
276 char data_header = AUT_ATTR32;
278 token = get_token(sizeof (char) + sizeof (int32_t) * 5 +
279 sizeof (int64_t));
280 #endif
282 if (token == NULL)
283 return (NULL);
284 adr_start(&adr, token->tt_data);
285 adr_char(&adr, &data_header, 1);
286 value = (int32_t)attr->va_mode;
287 adr_int32(&adr, &value, 1);
288 value = (int32_t)attr->va_uid;
289 adr_int32(&adr, &value, 1);
290 value = (int32_t)attr->va_gid;
291 adr_int32(&adr, &value, 1);
292 adr_int32(&adr, (int32_t *)&(attr->va_fsid), 1);
293 adr_int64(&adr, (int64_t *)&(attr->va_nodeid), 1);
294 #ifdef _LP64
295 adr_int64(&adr, (int64_t *)&(attr->va_rdev), 1);
296 #else
297 adr_int32(&adr, (int32_t *)&(attr->va_rdev), 1);
298 #endif
300 return (token);
304 * au_to_data
305 * return s:
306 * pointer to a data token.
308 token_t *
309 au_to_data(char unit_print, char unit_type, char unit_count, char *p)
311 adr_t adr; /* adr memory stream header */
312 token_t *token; /* token pointer */
313 char data_header = AUT_DATA; /* header for this token */
314 int byte_count; /* number of bytes */
316 if (p == NULL || unit_count < 1)
317 return (NULL);
320 * Check validity of print type
322 if (unit_print < AUP_BINARY || unit_print > AUP_STRING)
323 return (NULL);
325 switch (unit_type) {
326 case AUR_SHORT:
327 byte_count = unit_count * sizeof (short);
328 break;
329 case AUR_INT32:
330 byte_count = unit_count * sizeof (int32_t);
331 break;
332 case AUR_INT64:
333 byte_count = unit_count * sizeof (int64_t);
334 break;
335 /* case AUR_CHAR: */
336 case AUR_BYTE:
337 byte_count = unit_count * sizeof (char);
338 break;
339 default:
340 return (NULL);
343 token = get_token((int)(4 * sizeof (char) + byte_count));
344 if (token == NULL)
345 return (NULL);
346 adr_start(&adr, token->tt_data);
347 adr_char(&adr, &data_header, 1);
348 adr_char(&adr, &unit_print, 1);
349 adr_char(&adr, &unit_type, 1);
350 adr_char(&adr, &unit_count, 1);
352 switch (unit_type) {
353 case AUR_SHORT:
354 /* LINTED */
355 adr_short(&adr, (short *)p, unit_count);
356 break;
357 case AUR_INT32:
358 /* LINTED */
359 adr_int32(&adr, (int32_t *)p, unit_count);
360 break;
361 case AUR_INT64:
362 /* LINTED */
363 adr_int64(&adr, (int64_t *)p, unit_count);
364 break;
365 /* case AUR_CHAR: */
366 case AUR_BYTE:
367 adr_char(&adr, p, unit_count);
368 break;
371 return (token);
375 * au_to_privset
377 * priv_type (LIMIT, INHERIT...) is the first string and privilege
378 * in translated into the second string. The format is as follows:
380 * token id adr_char
381 * priv type adr_string (short, string)
382 * priv set adr_string (short, string)
384 * return s:
385 * pointer to a AUT_PRIV token.
387 token_t *
388 au_to_privset(const char *priv_type, const priv_set_t *privilege)
390 token_t *token; /* local token */
391 adr_t adr; /* adr memory stream header */
392 char data_header = AUT_PRIV; /* header for this token */
393 short t_bytes; /* length of type string */
394 short p_bytes; /* length of privilege string */
395 char *priv_string; /* privilege string */
397 t_bytes = strlen(priv_type) + 1;
399 if ((privilege == NULL) || (priv_string =
400 priv_set_to_str(privilege, ',',
401 PRIV_STR_LIT)) == NULL)
402 return (NULL);
404 p_bytes = strlen(priv_string) + 1;
406 token = get_token((int)(sizeof (char) + (2 * sizeof (short)) + t_bytes
407 + p_bytes));
408 if (token == NULL)
409 return (NULL);
411 adr_start(&adr, token->tt_data);
412 adr_char(&adr, &data_header, 1);
413 adr_short(&adr, &t_bytes, 1);
414 adr_char(&adr, (char *)priv_type, t_bytes);
415 adr_short(&adr, &p_bytes, 1);
416 adr_char(&adr, priv_string, p_bytes);
418 free(priv_string);
420 return (token);
424 * au_to_process
425 * return s:
426 * pointer to a process token.
429 token_t *
430 au_to_process(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
431 pid_t pid, au_asid_t sid, au_tid_t *tid)
433 token_t *token; /* local token */
434 adr_t adr; /* adr memory stream header */
435 #ifdef _LP64
436 char data_header = AUT_PROCESS64; /* header for this token */
438 token = get_token(sizeof (char) + 8 * sizeof (int32_t) +
439 sizeof (int64_t));
440 #else
441 char data_header = AUT_PROCESS32;
443 token = get_token(sizeof (char) + 9 * sizeof (int32_t));
444 #endif
446 if (token == NULL)
447 return (NULL);
448 adr_start(&adr, token->tt_data);
449 adr_char(&adr, &data_header, 1);
450 adr_int32(&adr, (int32_t *)&auid, 1);
451 adr_int32(&adr, (int32_t *)&euid, 1);
452 adr_int32(&adr, (int32_t *)&egid, 1);
453 adr_int32(&adr, (int32_t *)&ruid, 1);
454 adr_int32(&adr, (int32_t *)&rgid, 1);
455 adr_int32(&adr, (int32_t *)&pid, 1);
456 adr_int32(&adr, (int32_t *)&sid, 1);
457 #ifdef _LP64
458 adr_int64(&adr, (int64_t *)&tid->port, 1);
459 #else
460 adr_int32(&adr, (int32_t *)&tid->port, 1);
461 #endif
462 adr_int32(&adr, (int32_t *)&tid->machine, 1);
464 return (token);
468 * au_to_process_ex
469 * return s:
470 * pointer to a process_ex token.
472 token_t *
473 au_to_process_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
474 pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
476 token_t *token; /* local token */
477 adr_t adr; /* adr memory stream header */
478 char data_header; /* header for this token */
480 #ifdef _LP64
481 if (tid->at_type == AU_IPv6) {
482 data_header = AUT_PROCESS64_EX;
483 token = get_token(sizeof (char) + sizeof (int64_t) +
484 12 * sizeof (int32_t));
485 } else {
486 data_header = AUT_PROCESS64;
487 token = get_token(sizeof (char) + sizeof (int64_t) +
488 8 * sizeof (int32_t));
490 #else
491 if (tid->at_type == AU_IPv6) {
492 data_header = AUT_PROCESS32_EX;
493 token = get_token(sizeof (char) + 13 * sizeof (int32_t));
494 } else {
495 data_header = AUT_PROCESS32;
496 token = get_token(sizeof (char) + 9 * sizeof (int32_t));
498 #endif
499 if (token == NULL)
500 return (NULL);
501 adr_start(&adr, token->tt_data);
502 adr_char(&adr, &data_header, 1);
503 adr_int32(&adr, (int32_t *)&auid, 1);
504 adr_int32(&adr, (int32_t *)&euid, 1);
505 adr_int32(&adr, (int32_t *)&egid, 1);
506 adr_int32(&adr, (int32_t *)&ruid, 1);
507 adr_int32(&adr, (int32_t *)&rgid, 1);
508 adr_int32(&adr, (int32_t *)&pid, 1);
509 adr_int32(&adr, (int32_t *)&sid, 1);
510 #ifdef _LP64
511 adr_int64(&adr, (int64_t *)&tid->at_port, 1);
512 #else
513 adr_int32(&adr, (int32_t *)&tid->at_port, 1);
514 #endif
515 if (tid->at_type == AU_IPv6) {
516 adr_int32(&adr, (int32_t *)&tid->at_type, 1);
517 adr_char(&adr, (char *)tid->at_addr, 16);
518 } else {
519 adr_char(&adr, (char *)tid->at_addr, 4);
522 return (token);
526 * au_to_seq
527 * return s:
528 * pointer to token chain containing a sequence token
530 token_t *
531 au_to_seq(int audit_count)
533 token_t *token; /* local token */
534 adr_t adr; /* adr memory stream header */
535 char data_header = AUT_SEQ; /* header for this token */
537 token = get_token(sizeof (char) + sizeof (int32_t));
538 if (token == NULL)
539 return (NULL);
540 adr_start(&adr, token->tt_data);
541 adr_char(&adr, &data_header, 1);
542 adr_int32(&adr, (int32_t *)&audit_count, 1);
544 return (token);
548 * au_to_socket
549 * return s:
550 * pointer to mbuf chain containing a socket token.
552 token_t *
553 au_to_socket(struct oldsocket *so)
555 adr_t adr;
556 token_t *token;
557 char data_header = AUT_SOCKET;
558 struct inpcb *inp = so->so_pcb;
560 token = get_token(sizeof (char) + sizeof (short) * 3 +
561 sizeof (int32_t) * 2);
562 if (token == NULL)
563 return (NULL);
564 adr_start(&adr, token->tt_data);
565 adr_char(&adr, &data_header, 1);
566 adr_short(&adr, (short *)&so->so_type, 1);
567 adr_short(&adr, (short *)&inp->inp_lport, 1);
568 adr_int32(&adr, (int32_t *)&inp->inp_laddr, 1);
569 adr_short(&adr, (short *)&inp->inp_fport, 1);
570 adr_int32(&adr, (int32_t *)&inp->inp_faddr, 1);
572 return (token);
576 * au_to_subject
577 * return s:
578 * pointer to a process token.
581 token_t *
582 au_to_subject(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
583 pid_t pid, au_asid_t sid, au_tid_t *tid)
585 token_t *token; /* local token */
586 adr_t adr; /* adr memory stream header */
587 #ifdef _LP64
588 char data_header = AUT_SUBJECT64; /* header for this token */
590 token = get_token(sizeof (char) + sizeof (int64_t) +
591 8 * sizeof (int32_t));
592 #else
593 char data_header = AUT_SUBJECT32;
595 token = get_token(sizeof (char) + 9 * sizeof (int32_t));
596 #endif
598 if (token == NULL)
599 return (NULL);
600 adr_start(&adr, token->tt_data);
601 adr_char(&adr, &data_header, 1);
602 adr_int32(&adr, (int32_t *)&auid, 1);
603 adr_int32(&adr, (int32_t *)&euid, 1);
604 adr_int32(&adr, (int32_t *)&egid, 1);
605 adr_int32(&adr, (int32_t *)&ruid, 1);
606 adr_int32(&adr, (int32_t *)&rgid, 1);
607 adr_int32(&adr, (int32_t *)&pid, 1);
608 adr_int32(&adr, (int32_t *)&sid, 1);
609 #ifdef _LP64
610 adr_int64(&adr, (int64_t *)&tid->port, 1);
611 #else
612 adr_int32(&adr, (int32_t *)&tid->port, 1);
613 #endif
614 adr_int32(&adr, (int32_t *)&tid->machine, 1);
616 return (token);
620 * au_to_subject_ex
621 * return s:
622 * pointer to a process token.
625 token_t *
626 au_to_subject_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
627 pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
629 token_t *token; /* local token */
630 adr_t adr; /* adr memory stream header */
631 #ifdef _LP64
632 char data_header; /* header for this token */
634 if (tid->at_type == AU_IPv6) {
635 data_header = AUT_SUBJECT64_EX;
636 token = get_token(sizeof (char) + sizeof (int64_t) +
637 12 * sizeof (int32_t));
638 } else {
639 data_header = AUT_SUBJECT64;
640 token = get_token(sizeof (char) + sizeof (int64_t) +
641 8 * sizeof (int32_t));
643 #else
644 char data_header; /* header for this token */
646 if (tid->at_type == AU_IPv6) {
647 data_header = AUT_SUBJECT32_EX;
648 token = get_token(sizeof (char) + 13 * sizeof (int32_t));
649 } else {
650 data_header = AUT_SUBJECT32;
651 token = get_token(sizeof (char) + 9 * sizeof (int32_t));
653 #endif
655 if (token == NULL)
656 return (NULL);
657 adr_start(&adr, token->tt_data);
658 adr_char(&adr, &data_header, 1);
659 adr_int32(&adr, (int32_t *)&auid, 1);
660 adr_int32(&adr, (int32_t *)&euid, 1);
661 adr_int32(&adr, (int32_t *)&egid, 1);
662 adr_int32(&adr, (int32_t *)&ruid, 1);
663 adr_int32(&adr, (int32_t *)&rgid, 1);
664 adr_int32(&adr, (int32_t *)&pid, 1);
665 adr_int32(&adr, (int32_t *)&sid, 1);
666 #ifdef _LP64
667 adr_int64(&adr, (int64_t *)&tid->at_port, 1);
668 #else
669 adr_int32(&adr, (int32_t *)&tid->at_port, 1);
670 #endif
671 if (tid->at_type == AU_IPv6) {
672 adr_int32(&adr, (int32_t *)&tid->at_type, 1);
673 adr_char(&adr, (char *)tid->at_addr, 16);
674 } else {
675 adr_char(&adr, (char *)tid->at_addr, 4);
678 return (token);
682 * au_to_me
683 * return s:
684 * pointer to a process token.
687 token_t *
688 au_to_me(void)
690 auditinfo_addr_t info;
692 if (getaudit_addr(&info, sizeof (info)))
693 return (NULL);
694 return (au_to_subject_ex(info.ai_auid, geteuid(), getegid(), getuid(),
695 getgid(), getpid(), info.ai_asid, &info.ai_termid));
698 * au_to_text
699 * return s:
700 * pointer to a text token.
702 token_t *
703 au_to_text(char *text)
705 token_t *token; /* local token */
706 adr_t adr; /* adr memory stream header */
707 char data_header = AUT_TEXT; /* header for this token */
708 short bytes; /* length of string */
710 bytes = strlen(text) + 1;
711 token = get_token((int)(sizeof (char) + sizeof (short) + bytes));
712 if (token == NULL)
713 return (NULL);
714 adr_start(&adr, token->tt_data);
715 adr_char(&adr, &data_header, 1);
716 adr_short(&adr, &bytes, 1);
717 adr_char(&adr, text, bytes);
719 return (token);
723 * au_to_path
724 * return s:
725 * pointer to a path token.
727 token_t *
728 au_to_path(char *path)
730 token_t *token; /* local token */
731 adr_t adr; /* adr memory stream header */
732 char data_header = AUT_PATH; /* header for this token */
733 short bytes; /* length of string */
735 bytes = (short)strlen(path) + 1;
737 token = get_token((int)(sizeof (char) + sizeof (short) + bytes));
738 if (token == NULL)
739 return (NULL);
740 adr_start(&adr, token->tt_data);
741 adr_char(&adr, &data_header, 1);
742 adr_short(&adr, &bytes, 1);
743 adr_char(&adr, path, bytes);
745 return (token);
749 * au_to_cmd
750 * return s:
751 * pointer to an command line argument token
753 token_t *
754 au_to_cmd(uint_t argc, char **argv, char **envp)
756 token_t *token; /* local token */
757 adr_t adr; /* adr memory stream header */
758 char data_header = AUT_CMD; /* header for this token */
759 short len = 0;
760 short cnt = 0;
761 short envc = 0;
762 short largc = (short)argc;
765 * one char for the header, one short for argc,
766 * one short for # envp strings.
768 len = sizeof (char) + sizeof (short) + sizeof (short);
770 /* get sizes of strings */
772 for (cnt = 0; cnt < argc; cnt++) {
773 len += (short)sizeof (short) + (short)(strlen(argv[cnt]) + 1);
776 if (envp != NULL) {
777 for (envc = 0; envp[envc] != NULL; envc++) {
778 len += (short)sizeof (short) +
779 (short)(strlen(envp[envc]) + 1);
783 token = get_token(len);
784 if (token == NULL)
785 return (NULL);
787 adr_start(&adr, token->tt_data);
788 adr_char(&adr, &data_header, 1);
790 adr_short(&adr, &largc, 1);
792 for (cnt = 0; cnt < argc; cnt++) {
793 len = (short)(strlen(argv[cnt]) + 1);
794 adr_short(&adr, &len, 1);
795 adr_char(&adr, argv[cnt], len);
798 adr_short(&adr, &envc, 1);
800 for (cnt = 0; cnt < envc; cnt++) {
801 len = (short)(strlen(envp[cnt]) + 1);
802 adr_short(&adr, &len, 1);
803 adr_char(&adr, envp[cnt], len);
806 return (token);
810 * au_to_exit
811 * return s:
812 * pointer to a exit value token.
814 token_t *
815 au_to_exit(int retval, int err)
817 token_t *token; /* local token */
818 adr_t adr; /* adr memory stream header */
819 char data_header = AUT_EXIT; /* header for this token */
821 token = get_token(sizeof (char) + (2 * sizeof (int32_t)));
822 if (token == NULL)
823 return (NULL);
824 adr_start(&adr, token->tt_data);
825 adr_char(&adr, &data_header, 1);
826 adr_int32(&adr, (int32_t *)&retval, 1);
827 adr_int32(&adr, (int32_t *)&err, 1);
829 return (token);
833 * au_to_return
834 * return s:
835 * pointer to a return value token.
837 token_t *
838 au_to_return32(char number, uint32_t value)
840 token_t *token; /* local token */
841 adr_t adr; /* adr memory stream header */
842 char data_header = AUT_RETURN32; /* header for this token */
844 token = get_token(2 * sizeof (char) + sizeof (int32_t));
845 if (token == NULL)
846 return (NULL);
847 adr_start(&adr, token->tt_data);
848 adr_char(&adr, &data_header, 1);
849 adr_char(&adr, &number, 1);
850 adr_int32(&adr, (int32_t *)&value, 1);
852 return (token);
856 * au_to_return
857 * return s:
858 * pointer to a return value token.
860 token_t *
861 au_to_return64(char number, uint64_t value)
863 token_t *token; /* local token */
864 adr_t adr; /* adr memory stream header */
865 char data_header = AUT_RETURN64; /* header for this token */
867 token = get_token(2 * sizeof (char) + sizeof (int64_t));
868 if (token == NULL)
869 return (NULL);
870 adr_start(&adr, token->tt_data);
871 adr_char(&adr, &data_header, 1);
872 adr_char(&adr, &number, 1);
873 adr_int64(&adr, (int64_t *)&value, 1);
875 return (token);
880 * au_to_opaque
881 * return s:
882 * pointer to a opaque token.
884 token_t *
885 au_to_opaque(char *opaque, short bytes)
887 token_t *token; /* local token */
888 adr_t adr; /* adr memory stream header */
889 char data_header = AUT_OPAQUE; /* header for this token */
891 if (bytes < 1)
892 return (NULL);
894 token = get_token((int)(sizeof (char) + sizeof (short) + bytes));
895 if (token == NULL)
896 return (NULL);
897 adr_start(&adr, token->tt_data);
898 adr_char(&adr, &data_header, 1);
899 adr_short(&adr, &bytes, 1);
900 adr_char(&adr, opaque, bytes);
902 return (token);
906 * au_to_in_addr
907 * return s:
908 * pointer to an internet address token
910 token_t *
911 au_to_in_addr(struct in_addr *internet_addr)
913 token_t *token; /* local token */
914 adr_t adr; /* adr memory stream header */
915 char data_header = AUT_IN_ADDR; /* header for this token */
917 token = get_token(sizeof (char) + sizeof (struct in_addr));
918 if (token == NULL)
919 return (NULL);
920 adr_start(&adr, token->tt_data);
921 adr_char(&adr, &data_header, 1);
922 adr_char(&adr, (char *)internet_addr, sizeof (struct in_addr));
924 return (token);
928 * au_to_in_addr_ex
929 * return s:
930 * pointer to an internet extended token
932 token_t *
933 au_to_in_addr_ex(struct in6_addr *addr)
935 token_t *token;
936 adr_t adr;
938 if (IN6_IS_ADDR_V4MAPPED(addr)) {
939 ipaddr_t in4;
942 * An IPv4-mapped IPv6 address is really an IPv4 address
943 * in IPv6 format.
946 IN6_V4MAPPED_TO_IPADDR(addr, in4);
947 return (au_to_in_addr((struct in_addr *)&in4));
949 } else {
950 char data_header = AUT_IN_ADDR_EX;
951 int32_t type = AU_IPv6;
953 if ((token = get_token(sizeof (char) + sizeof (int32_t) +
954 sizeof (struct in6_addr))) == NULL) {
955 return (NULL);
958 adr_start(&adr, token->tt_data);
959 adr_char(&adr, &data_header, 1);
960 adr_int32(&adr, &type, 1);
961 adr_char(&adr, (char *)addr, sizeof (struct in6_addr));
964 return (token);
968 * au_to_iport
969 * return s:
970 * pointer to token chain containing a ip port address token
972 token_t *
973 au_to_iport(ushort_t iport)
975 token_t *token; /* local token */
976 adr_t adr; /* adr memory stream header */
977 char data_header = AUT_IPORT; /* header for this token */
979 token = get_token(sizeof (char) + sizeof (short));
980 if (token == NULL)
981 return (NULL);
982 adr_start(&adr, token->tt_data);
983 adr_char(&adr, &data_header, 1);
984 adr_short(&adr, (short *)&iport, 1);
986 return (token);
989 token_t *
990 au_to_ipc(char type, int id)
992 token_t *token; /* local token */
993 adr_t adr; /* adr memory stream header */
994 char data_header = AUT_IPC; /* header for this token */
996 token = get_token((2 * sizeof (char)) + sizeof (int32_t));
997 if (token == NULL)
998 return (NULL);
999 adr_start(&adr, token->tt_data);
1000 adr_char(&adr, &data_header, 1);
1001 adr_char(&adr, &type, 1);
1002 adr_int32(&adr, (int32_t *)&id, 1);
1004 return (token);
1008 * au_to_tid
1010 * output format depends on type; at present only IP v4 and v6 addresses
1011 * are defined.
1013 * IPv4 -- tid type, 16 bit remote port, 16 bit local port, ip type,
1014 * 32 bit IP address.
1015 * IPv6 -- tid type, 16 bit remote port, 16 bit local port, ip type,
1016 * 4 x 32 bit IP address.
1019 token_t *
1020 au_to_tid(au_generic_tid_t *tid)
1022 char data_header = AUT_TID; /* header for this token */
1023 adr_t adr; /* adr memory stream header */
1024 token_t *token; /* local token */
1025 au_ip_t *ip;
1027 switch (tid->gt_type) {
1028 case AU_IPADR:
1029 ip = &(tid->gt_adr.at_ip);
1030 token = get_token((int)(2 * sizeof (char) + 2 * sizeof (short) +
1031 sizeof (uint32_t) + ip->at_type));
1032 if (token == NULL)
1033 return (NULL);
1035 adr_start(&adr, token->tt_data);
1036 adr_char(&adr, &data_header, 1);
1037 adr_char(&adr, (char *)&(tid->gt_type), 1);
1038 adr_short(&adr, (short *)&(ip->at_r_port), 1);
1039 adr_short(&adr, (short *)&(ip->at_l_port), 1);
1040 adr_int32(&adr, (int32_t *)&(ip->at_type), 1);
1042 adr_char(&adr, (char *)ip->at_addr, ip->at_type);
1044 break;
1045 default:
1046 return (NULL);
1048 return (token);
1052 * The Modifier tokens
1056 * au_to_groups
1057 * return s:
1058 * pointer to a group list token.
1060 * This function is obsolete. Please use au_to_newgroups.
1062 token_t *
1063 au_to_groups(int *groups)
1065 token_t *token; /* local token */
1066 adr_t adr; /* adr memory stream header */
1067 char data_header = AUT_GROUPS; /* header for this token */
1069 token = get_token(sizeof (char) + NGROUPS * sizeof (int32_t));
1070 if (token == NULL)
1071 return (NULL);
1072 adr_start(&adr, token->tt_data);
1073 adr_char(&adr, &data_header, 1);
1074 adr_int32(&adr, (int32_t *)groups, NGROUPS);
1076 return (token);
1080 * au_to_newgroups
1081 * return s:
1082 * pointer to a group list token.
1084 token_t *
1085 au_to_newgroups(int n, gid_t *groups)
1087 token_t *token; /* local token */
1088 adr_t adr; /* adr memory stream header */
1089 char data_header = AUT_NEWGROUPS; /* header for this token */
1090 short n_groups;
1092 if (n < 0 || n > SHRT_MAX || groups == NULL)
1093 return (NULL);
1094 token = get_token(sizeof (char) + sizeof (short) + n * sizeof (gid_t));
1095 if (token == NULL)
1096 return (NULL);
1097 n_groups = (short)n;
1098 adr_start(&adr, token->tt_data);
1099 adr_char(&adr, &data_header, 1);
1100 adr_short(&adr, &n_groups, 1);
1101 adr_int32(&adr, (int32_t *)groups, n_groups);
1103 return (token);
1107 * au_to_exec_args
1108 * returns:
1109 * pointer to an exec args token.
1111 token_t *
1112 au_to_exec_args(char **argv)
1114 return (au_to_exec(argv, AUT_EXEC_ARGS));
1118 * au_to_exec_env
1119 * returns:
1120 * pointer to an exec args token.
1122 token_t *
1123 au_to_exec_env(char **envp)
1125 return (au_to_exec(envp, AUT_EXEC_ENV));
1129 * au_to_exec
1130 * returns:
1131 * pointer to an exec args token.
1133 static token_t *
1134 au_to_exec(char **v, char data_header)
1136 token_t *token;
1137 adr_t adr;
1138 char **p;
1139 int32_t n = 0;
1140 int len = 0;
1142 for (p = v; *p != NULL; p++) {
1143 len += strlen(*p) + 1;
1144 n++;
1146 token = get_token(sizeof (char) + sizeof (int32_t) + len);
1147 if (token == (token_t *)NULL)
1148 return ((token_t *)NULL);
1149 adr_start(&adr, token->tt_data);
1150 adr_char(&adr, &data_header, 1);
1151 adr_int32(&adr, &n, 1);
1152 for (p = v; *p != NULL; p++) {
1153 adr_char(&adr, *p, strlen(*p) + 1);
1155 return (token);
1159 * au_to_uauth
1160 * return s:
1161 * pointer to a uauth token.
1163 token_t *
1164 au_to_uauth(char *text)
1166 token_t *token; /* local token */
1167 adr_t adr; /* adr memory stream header */
1168 char data_header = AUT_UAUTH; /* header for this token */
1169 short bytes; /* length of string */
1171 bytes = strlen(text) + 1;
1173 token = get_token((int)(sizeof (char) + sizeof (short) + bytes));
1174 if (token == NULL)
1175 return (NULL);
1176 adr_start(&adr, token->tt_data);
1177 adr_char(&adr, &data_header, 1);
1178 adr_short(&adr, &bytes, 1);
1179 adr_char(&adr, text, bytes);
1181 return (token);
1185 * au_to_upriv
1186 * return s:
1187 * pointer to a use of privilege token.
1189 token_t *
1190 au_to_upriv(char sorf, char *priv)
1192 token_t *token; /* local token */
1193 adr_t adr; /* adr memory stream header */
1194 char data_header = AUT_UAUTH; /* header for this token */
1195 short bytes; /* length of string */
1197 bytes = strlen(priv) + 1;
1199 token = get_token(sizeof (char) + sizeof (char) + sizeof (short) +
1200 bytes);
1201 if (token == NULL)
1202 return (NULL);
1203 adr_start(&adr, token->tt_data);
1204 adr_char(&adr, &data_header, 1);
1205 adr_char(&adr, &sorf, 1); /* success/failure */
1206 adr_short(&adr, &bytes, 1);
1207 adr_char(&adr, priv, bytes);
1209 return (token);
1213 * au_to_user
1214 * return s:
1215 * pointer to a user token.
1217 token_t *
1218 au_to_user(uid_t uid, char *username)
1220 token_t *token; /* local token */
1221 adr_t adr; /* adr memory stream header */
1222 char data_header = AUT_USER; /* header for this token */
1223 short bytes; /* length of string */
1225 bytes = (short)strlen(username) + 1;
1227 token = get_token(sizeof (char) + sizeof (uid_t) + sizeof (short) +
1228 bytes);
1229 if (token == NULL)
1230 return (NULL);
1231 adr_start(&adr, token->tt_data);
1232 adr_char(&adr, &data_header, 1);
1233 adr_uid(&adr, &uid, 1);
1234 adr_short(&adr, &bytes, 1);
1235 adr_char(&adr, username, bytes);
1237 return (token);
1241 * au_to_xatom
1242 * return s:
1243 * pointer to a xatom token.
1245 token_t *
1246 au_to_xatom(char *atom)
1248 token_t *token; /* local token */
1249 adr_t adr; /* adr memory stream header */
1250 char data_header = AUT_XATOM; /* header for this token */
1251 short len;
1253 len = strlen(atom) + 1;
1255 token = get_token(sizeof (char) + sizeof (short) + len);
1256 if (token == NULL)
1257 return (NULL);
1258 adr_start(&adr, token->tt_data);
1259 adr_char(&adr, &data_header, 1);
1260 adr_short(&adr, (short *)&len, 1);
1261 adr_char(&adr, atom, len);
1263 return (token);
1267 * au_to_xselect
1268 * return s:
1269 * pointer to a X select token.
1271 token_t *
1272 au_to_xselect(char *propname, char *proptype, char *windata)
1274 token_t *token; /* local token */
1275 adr_t adr; /* adr memory stream header */
1276 char data_header = AUT_XSELECT; /* header for this token */
1277 short proplen;
1278 short typelen;
1279 short datalen;
1281 proplen = strlen(propname) + 1;
1282 typelen = strlen(proptype) + 1;
1283 datalen = strlen(windata) + 1;
1285 token = get_token(sizeof (char) + (sizeof (short) * 3) +
1286 proplen + typelen + datalen);
1287 if (token == NULL)
1288 return (NULL);
1289 adr_start(&adr, token->tt_data);
1290 adr_char(&adr, &data_header, 1);
1291 adr_short(&adr, &proplen, 1);
1292 adr_char(&adr, propname, proplen);
1293 adr_short(&adr, &typelen, 1);
1294 adr_char(&adr, proptype, typelen);
1295 adr_short(&adr, &datalen, 1);
1296 adr_char(&adr, windata, datalen);
1298 return (token);
1302 * x_common
1303 * return s:
1304 * pointer to a common X token.
1307 static token_t *
1308 x_common(char data_header, int32_t xid, uid_t cuid)
1310 token_t *token; /* local token */
1311 adr_t adr; /* adr memory stream header */
1313 token = get_token(sizeof (char) + sizeof (int32_t) + sizeof (uid_t));
1314 if (token == NULL)
1315 return (NULL);
1316 adr_start(&adr, token->tt_data);
1317 adr_char(&adr, &data_header, 1);
1318 adr_int32(&adr, &xid, 1);
1319 adr_uid(&adr, &cuid, 1);
1321 return (token);
1325 * au_to_xcolormap
1326 * return s:
1327 * pointer to a X Colormap token.
1330 token_t *
1331 au_to_xcolormap(int32_t xid, uid_t cuid)
1333 return (x_common(AUT_XCOLORMAP, xid, cuid));
1337 * au_to_xcursor
1338 * return s:
1339 * pointer to a X Cursor token.
1342 token_t *
1343 au_to_xcursor(int32_t xid, uid_t cuid)
1345 return (x_common(AUT_XCURSOR, xid, cuid));
1349 * au_to_xfont
1350 * return s:
1351 * pointer to a X Font token.
1354 token_t *
1355 au_to_xfont(int32_t xid, uid_t cuid)
1357 return (x_common(AUT_XFONT, xid, cuid));
1361 * au_to_xgc
1362 * return s:
1363 * pointer to a X Graphic Context token.
1366 token_t *
1367 au_to_xgc(int32_t xid, uid_t cuid)
1369 return (x_common(AUT_XGC, xid, cuid));
1373 * au_to_xpixmap
1374 * return s:
1375 * pointer to a X Pixal Map token.
1378 token_t *
1379 au_to_xpixmap(int32_t xid, uid_t cuid)
1381 return (x_common(AUT_XPIXMAP, xid, cuid));
1385 * au_to_xwindow
1386 * return s:
1387 * pointer to a X Window token.
1390 token_t *
1391 au_to_xwindow(int32_t xid, uid_t cuid)
1393 return (x_common(AUT_XWINDOW, xid, cuid));
1397 * au_to_xproperty
1398 * return s:
1399 * pointer to a X Property token.
1402 token_t *
1403 au_to_xproperty(int32_t xid, uid_t cuid, char *propname)
1405 token_t *token; /* local token */
1406 adr_t adr; /* adr memory stream header */
1407 char data_header = AUT_XPROPERTY; /* header for this token */
1408 short proplen;
1410 proplen = strlen(propname) + 1;
1412 token = get_token(sizeof (char) + sizeof (int32_t) + sizeof (uid_t) +
1413 sizeof (short) + proplen);
1414 if (token == NULL)
1415 return (NULL);
1416 adr_start(&adr, token->tt_data);
1417 adr_char(&adr, &data_header, 1);
1418 adr_int32(&adr, &xid, 1);
1419 adr_uid(&adr, &cuid, 1);
1420 adr_short(&adr, &proplen, 1);
1421 adr_char(&adr, propname, proplen);
1423 return (token);
1427 * au_to_xclient
1428 * return s:
1429 * pointer to a X Client token
1432 token_t *
1433 au_to_xclient(uint32_t client)
1435 token_t *token; /* local token */
1436 adr_t adr; /* adr memory stream header */
1437 char data_header = AUT_XCLIENT; /* header for this token */
1439 token = get_token(sizeof (char) + sizeof (uint32_t));
1440 if (token == NULL)
1441 return (NULL);
1442 adr_start(&adr, token->tt_data);
1443 adr_char(&adr, &data_header, 1);
1444 adr_int32(&adr, (int32_t *)&client, 1);
1446 return (token);
1451 * au_to_zonename
1452 * return s:
1453 * pointer to a zonename token.
1455 token_t *
1456 au_to_zonename(char *name)
1458 token_t *token; /* local token */
1459 adr_t adr; /* adr memory stream header */
1460 char data_header = AUT_ZONENAME; /* header for this token */
1461 short bytes; /* length of string */
1463 if (name == NULL)
1464 return (NULL);
1466 bytes = strlen(name) + 1;
1467 token = get_token((int)(sizeof (char) + sizeof (short) + bytes));
1468 if (token == NULL)
1469 return (NULL);
1470 adr_start(&adr, token->tt_data);
1471 adr_char(&adr, &data_header, 1);
1472 adr_short(&adr, &bytes, 1);
1473 adr_char(&adr, name, bytes);
1475 return (token);
1479 * au_to_fmri
1480 * return s:
1481 * pointer to a fmri token.
1483 token_t *
1484 au_to_fmri(char *fmri)
1486 token_t *token; /* local token */
1487 adr_t adr; /* adr memory stream header */
1488 char data_header = AUT_FMRI; /* header for this token */
1489 short bytes; /* length of string */
1491 if (fmri == NULL)
1492 return (NULL);
1494 bytes = strlen(fmri) + 1;
1495 token = get_token((int)(sizeof (char) + sizeof (short) + bytes));
1496 if (token == NULL)
1497 return (NULL);
1498 adr_start(&adr, token->tt_data);
1499 adr_char(&adr, &data_header, 1);
1500 adr_short(&adr, &bytes, 1);
1501 adr_char(&adr, fmri, bytes);
1503 return (token);