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 /*
  24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  25  * Use is subject to license terms.
  26  */
  27 
  28 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  29 /*        All Rights Reserved   */
  30 
  31 /*
  32  * Copyright (c) 2013 RackTop Systems.
  33  */
  34 
  35 /*LINTLIBRARY*/
  36 
  37 #include        <sys/types.h>
  38 #include        <stdio.h>
  39 #include        <string.h>
  40 #include        <userdefs.h>
  41 #include        <user_attr.h>
  42 #include        <limits.h>
  43 #include        <stdlib.h>
  44 #include        <stddef.h>
  45 #include        <time.h>
  46 #include        <unistd.h>
  47 #include        "userdisp.h"
  48 #include        "funcs.h"
  49 #include        "messages.h"
  50 
  51 /* Print out a NL when the line gets too long */
  52 #define PRINTNL()       \
  53         if (outcount > 40) { \
  54                 outcount = 0; \
  55                 (void) fprintf(fptr, "\n"); \
  56         }
  57 
  58 /*
  59  * getusrdef - get the user defaults file for the type of
  60  * user entry (user or role).  See libuserdefs
  61  */
  62 
  63 struct userdefs *
  64 getusrdef(char *usertype)
  65 {
  66         struct userdefs *ud;
  67 
  68         if (is_role(usertype))
  69                 ud = _get_roledefs();
  70         else
  71                 ud = _get_userdefs();
  72 
  73         return (ud);
  74 }
  75 
  76 void
  77 dispusrdef(FILE *fptr, unsigned flags, char *usertype)
  78 {
  79         struct userdefs *deflts = getusrdef(usertype);
  80         int outcount = 0;
  81 
  82         /* Print out values */
  83 
  84         if (flags & D_GROUP) {
  85                 outcount += fprintf(fptr, "group=%s,%ld  ",
  86                     deflts->defgname, deflts->defgroup);
  87                 PRINTNL();
  88         }
  89 
  90         if (flags & D_PROJ) {
  91                 outcount += fprintf(fptr, "project=%s,%ld  ",
  92                     deflts->defprojname, deflts->defproj);
  93                 PRINTNL();
  94         }
  95 
  96         if (flags & D_BASEDIR) {
  97                 outcount += fprintf(fptr, "basedir=%s  ", deflts->defparent);
  98                 PRINTNL();
  99         }
 100 
 101         if (flags & D_RID) {
 102                 outcount += fprintf(fptr, "rid=%ld  ", deflts->defrid);
 103                 PRINTNL();
 104         }
 105 
 106         if (flags & D_SKEL) {
 107                 outcount += fprintf(fptr, "skel=%s  ", deflts->defskel);
 108                 PRINTNL();
 109         }
 110 
 111         if (flags & D_SHELL) {
 112                 outcount += fprintf(fptr, "shell=%s  ", deflts->defshell);
 113                 PRINTNL();
 114         }
 115 
 116         if (flags & D_INACT) {
 117                 outcount += fprintf(fptr, "inactive=%d  ", deflts->definact);
 118                 PRINTNL();
 119         }
 120 
 121         if (flags & D_EXPIRE) {
 122                 outcount += fprintf(fptr, "expire=%s  ", deflts->defexpire);
 123                 PRINTNL();
 124         }
 125 
 126         if (flags & D_AUTH) {
 127                 outcount += fprintf(fptr, "auths=%s  ", deflts->defauth);
 128                 PRINTNL();
 129         }
 130 
 131         if (flags & D_PROF) {
 132                 outcount += fprintf(fptr, "profiles=%s  ", deflts->defprof);
 133                 PRINTNL();
 134         }
 135 
 136         if ((flags & D_ROLE) &&
 137             (!is_role(usertype))) {
 138                 outcount += fprintf(fptr, "roles=%s  ", deflts->defrole);
 139                 PRINTNL();
 140         }
 141 
 142         if (flags & D_LPRIV) {
 143                 outcount += fprintf(fptr, "limitpriv=%s  ",
 144                     deflts->deflimpriv);
 145                 PRINTNL();
 146         }
 147 
 148         if (flags & D_DPRIV) {
 149                 outcount += fprintf(fptr, "defaultpriv=%s  ",
 150                     deflts->defdfltpriv);
 151                 PRINTNL();
 152         }
 153 
 154         if (flags & D_LOCK) {
 155                 outcount += fprintf(fptr, "lock_after_retries=%s  ",
 156                     deflts->deflock_after_retries);
 157         }
 158 
 159         if (outcount > 0)
 160                 (void) fprintf(fptr, "\n");
 161 }
 162 
 163 /*
 164  * putusrdef -
 165  *      changes default values in defadduser file
 166  */
 167 int
 168 putusrdef(struct userdefs *defs, char *usertype)
 169 {
 170         FILE *fp = NULL;        /* default file - fptr */
 171         boolean_t locked = B_FALSE;
 172         int res;
 173         int ex = EX_UPDATE;
 174 
 175         if (is_role(usertype)) {
 176                 fp = fopen(DEFROLEFILE, "w");
 177         } else {
 178                 fp = fopen(DEFFILE, "w");
 179         }
 180         if (fp == NULL) {
 181                 errmsg(M_FAILED);
 182                 goto out;
 183         }
 184 
 185         if (lockf(fileno(fp), F_LOCK, 0) != 0) {
 186                 /* print error if can't lock whole of DEFFILE */
 187                 errmsg(M_UPDATE, "created");
 188                 goto out;
 189         }
 190         locked = B_TRUE;
 191 
 192         if (is_role(usertype)) {
 193                 res = fwrite_roledefs(fp, defs);
 194         } else {
 195                 res = fwrite_userdefs(fp, defs);
 196         }
 197         if (res <= 0) {
 198                 errmsg(M_UPDATE, "created");
 199                 goto out;
 200         }
 201         ex = EX_SUCCESS;
 202 
 203 out:
 204         if (fp != NULL) {
 205                 if (locked)
 206                         (void) lockf(fileno(fp), F_ULOCK, 0);
 207                 (void) fclose(fp);
 208         }
 209 
 210         return (ex);
 211 }