1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 1989 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* getacinfo.c - get audit control info */ 30 31 #include <stdio.h> 32 #include <string.h> 33 34 #define DIROP 0 35 #define OTHEROP 1 36 37 #define LEN 360 /* maximum audit control entry length */ 38 39 #define SUCCESS 0 40 #define EOF_WARN 1 41 #define REW_WARN 2 42 #define EOF_ERR -1 43 #define ERROR -2 44 #define FORMAT_ERR -3 45 46 47 static char *AUDIT_CTRL = "/etc/security/audit/audit_control"; 48 static char *MINLABEL = "minfree:"; 49 static char *DIRLABEL = "dir:"; 50 static char *FLGLABEL = "flags:"; 51 static int LASTOP; 52 static int DIRINIT; 53 static FILE *acf; /* pointer into /etc/security/audit/audit_control */ 54 55 void setac(void); 56 void endac(void); 57 58 /* getacinfo.c - get audit control info 59 * 60 * getacdir() - get audit control directories, one at a time 61 * getacflg() - get audit control flags 62 * getacmin() - get audit control directory min. fill value 63 * setac() - rewind the audit control file 64 * endac() - close the audit control file 65 */ 66 67 68 /* getacdir() - get audit control directories, one at a time 69 * 70 * input: len - size of dir buffer 71 * 72 * output: dir - directory string 73 * 74 * returns: 0 - entry read ok 75 * -1 - end of file 76 * -2 - error - can't open audit control file for read 77 * -3 - error - directory entry format error 78 * 1 - directory search started from beginning again 79 * 80 * notes: It is the responsibility of the calling function to 81 * check the status of the directory entry. 82 */ 83 84 int 85 getacdir(char *dir, int len) 86 { 87 int retstat = SUCCESS, gotone = 0, dirlen, dirst; 88 char entry[LEN]; 89 90 /* 91 * open file if it is not already opened 92 */ 93 if (acf == NULL && (acf = fopen(AUDIT_CTRL, "r")) == NULL) 94 retstat = ERROR; 95 else if (LASTOP != DIROP && DIRINIT == 1) { 96 retstat = REW_WARN; 97 setac(); 98 } else { 99 DIRINIT = 1; 100 LASTOP == DIROP; 101 } 102 if (retstat >= SUCCESS) { 103 do { 104 if (fgets(entry, LEN, acf) != NULL) { 105 switch(*entry) { 106 case '#': 107 break; 108 case 'd': 109 /* 110 * return directory entry 111 */ 112 if (!strncmp(entry,DIRLABEL,strlen(DIRLABEL))) { 113 if ((strlen(entry)+1) > len) 114 retstat = FORMAT_ERR; 115 else { 116 /* 117 * allow zero or one blank 118 * between colon and directory 119 */ 120 if (entry[strlen(DIRLABEL)] == ' ') { 121 dirst = strlen(DIRLABEL)+1; 122 dirlen = 123 strlen(entry) - 124 (strlen(DIRLABEL)+2); 125 } else { 126 dirst = strlen(DIRLABEL); 127 dirlen = 128 strlen(entry) - 129 (strlen(DIRLABEL)+1); 130 } 131 strcpy(dir, entry+dirst); 132 strcpy(dir+dirlen, "\0"); 133 gotone = 1; 134 } 135 } else 136 retstat = FORMAT_ERR; 137 break; 138 case 'm': 139 break; 140 case 'f': 141 break; 142 default: 143 break; 144 } 145 } else if ((feof(acf)) == 0) 146 retstat = ERROR; 147 else 148 retstat = EOF_ERR; 149 150 } while (gotone == 0 && retstat >= SUCCESS); 151 } 152 return (retstat); 153 } 154 155 /* 156 * getacmin() - get audit control directory min. fill value 157 * 158 * output: min_val - percentage of directory fill allowed 159 * 160 * returns: 0 - entry read ok 161 * 1 - end of file 162 * -2 - error; errno contains error number 163 * -3 - error - directory entry format error 164 */ 165 166 int 167 getacmin(int *min_val) 168 { 169 int retstat = SUCCESS, gotone = 0; 170 char entry[LEN]; 171 172 /* 173 * open file if it is not already opened 174 */ 175 if (acf == NULL && (acf = fopen(AUDIT_CTRL, "r")) == NULL) 176 retstat = ERROR; 177 else 178 rewind(acf); 179 180 if (retstat == SUCCESS) { 181 do { 182 if (fgets(entry, LEN, acf) != NULL) { 183 switch(*entry) { 184 case '#': 185 break; 186 case 'd': 187 break; 188 case 'm': 189 if (!strncmp(entry, MINLABEL, strlen(MINLABEL))) { 190 sscanf(entry+strlen(MINLABEL), "%d", min_val); 191 gotone = 1; 192 } else 193 retstat = FORMAT_ERR; 194 break; 195 case 'f': 196 break; 197 default: 198 break; 199 } 200 } else if ((feof(acf)) == 0) 201 retstat = ERROR; 202 else 203 retstat = EOF_WARN; 204 205 } while (gotone == 0 && retstat == SUCCESS); 206 } 207 208 if (LASTOP == DIROP) 209 LASTOP = OTHEROP; 210 else 211 endac(); 212 213 return (retstat); 214 } 215 216 /* getacflg() - get audit control flags 217 * 218 * output: auditstring - character representation of system audit flags 219 * 220 * returns: 0 - entry read ok 221 * 1 - end of file 222 * -2 - error - errno contains error number 223 * -3 - error - directory entry format error 224 */ 225 226 int 227 getacflg(char *auditstring, int len) 228 { 229 int retstat = SUCCESS, gotone = 0, minst, minlen; 230 char entry[LEN]; 231 232 /* 233 * open file if it is not already opened 234 */ 235 if (acf == NULL && (acf = fopen(AUDIT_CTRL, "r")) == NULL) 236 retstat = ERROR; 237 else 238 rewind(acf); 239 240 if (retstat == SUCCESS) { 241 do { 242 if (fgets(entry, LEN, acf) != NULL) { 243 switch(*entry) { 244 case '#': 245 break; 246 case 'd': 247 break; 248 case 'm': 249 break; 250 case 'f': 251 if ((strncmp(entry, FLGLABEL, strlen(FLGLABEL))) == 0) { 252 if (entry[strlen(FLGLABEL)] == ' ') { 253 minst = strlen(FLGLABEL)+1; 254 minlen = strlen(entry)-(strlen(FLGLABEL)+2); 255 } else { 256 minst = strlen(FLGLABEL); 257 minlen = strlen(entry)-(strlen(FLGLABEL)+1); 258 } 259 if (minlen > len) 260 retstat = FORMAT_ERR; 261 else { 262 strcpy(auditstring, entry+minst); 263 strcpy(auditstring+minlen, "\0"); 264 gotone = 1; 265 } 266 } else 267 retstat = FORMAT_ERR; 268 break; 269 default: 270 break; 271 } 272 } else if ((feof(acf)) == 0) 273 retstat = ERROR; 274 else 275 retstat = EOF_WARN; 276 277 } while (gotone == 0 && retstat == SUCCESS); 278 } 279 if (LASTOP == DIROP) 280 LASTOP = OTHEROP; 281 else 282 endac(); 283 284 return (retstat); 285 } 286 287 /* rewind the audit control file */ 288 void 289 setac(void) 290 { 291 if (acf == NULL) 292 acf = fopen(AUDIT_CTRL, "r"); 293 else 294 rewind(acf); 295 LASTOP = DIROP; 296 DIRINIT = 0; 297 } 298 299 300 /* close the audit control file */ 301 void 302 endac(void) 303 { 304 if (acf != NULL) { 305 fclose(acf); 306 acf = NULL; 307 } 308 LASTOP = DIROP; 309 DIRINIT = 0; 310 } 311