Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add login_getpwclass to libutil as convenience function for



details:   https://anonhg.NetBSD.org/src/rev/4b712488be9e
branches:  trunk
changeset: 481698:4b712488be9e
user:      mjl <mjl%NetBSD.org@localhost>
date:      Fri Feb 04 02:17:14 2000 +0000

description:
Add login_getpwclass to libutil as convenience function for
programs originally for FreeBSD.
Add parsing of "setenv" parameter which can be used to set
up an initial environment on login.

diffstat:

 include/login_cap.h         |   6 ++-
 lib/libutil/login_cap.c     |  68 ++++++++++++++++++++++++++++++++++++++++++++-
 share/man/man5/login.conf.5 |   6 +++-
 usr.bin/login/login.c       |  12 ++++---
 4 files changed, 83 insertions(+), 9 deletions(-)

diffs (197 lines):

diff -r fe6a01a6c49f -r 4b712488be9e include/login_cap.h
--- a/include/login_cap.h       Fri Feb 04 02:03:02 2000 +0000
+++ b/include/login_cap.h       Fri Feb 04 02:17:14 2000 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: login_cap.h,v 1.1 2000/01/12 05:02:11 mjl Exp $ */
+/* $NetBSD: login_cap.h,v 1.2 2000/02/04 02:17:16 mjl Exp $ */
 
 /*-
  * Copyright (c) 1995,1997 Berkeley Software Design, Inc. All rights reserved.
@@ -49,7 +49,8 @@
 #define        LOGIN_SETRESOURCES      0x0010  /* Set resource limits */
 #define        LOGIN_SETUMASK          0x0020  /* Set umask */
 #define        LOGIN_SETUSER           0x0040  /* Set user */
-#define        LOGIN_SETALL            0x007f  /* Set all. */
+#define        LOGIN_SETENV            0x0080  /* Set user environment */
+#define        LOGIN_SETALL            0x00ff  /* Set all. */
 
 typedef struct {
        char    *lc_class;
@@ -62,6 +63,7 @@
 struct passwd;
 
 login_cap_t *login_getclass __P((char *));
+login_cap_t *login_getpwclass __P((const struct passwd *));
 void    login_close __P((login_cap_t *));
 int     login_getcapbool __P((login_cap_t *, char *, u_int));
 quad_t  login_getcapnum __P((login_cap_t *, char *, quad_t, quad_t));
diff -r fe6a01a6c49f -r 4b712488be9e lib/libutil/login_cap.c
--- a/lib/libutil/login_cap.c   Fri Feb 04 02:03:02 2000 +0000
+++ b/lib/libutil/login_cap.c   Fri Feb 04 02:17:14 2000 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: login_cap.c,v 1.3 2000/01/14 02:14:42 mjl Exp $ */
+/* $NetBSD: login_cap.c,v 1.4 2000/02/04 02:17:16 mjl Exp $ */
 
 /*-
  * Copyright (c) 1995,1997 Berkeley Software Design, Inc. All rights reserved.
@@ -39,6 +39,7 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 
+#include <ctype.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -59,6 +60,7 @@
 static u_quad_t strtolimit __P((char *, char **, int));
 static u_quad_t strtosize __P((char *, char **, int));
 static int gsetrl __P((login_cap_t *, int, char *, int type));
+static int setuserenv __P((login_cap_t *));
 
 login_cap_t *
 login_getclass(class)
@@ -123,6 +125,13 @@
        return (lc);
 }
 
+login_cap_t *
+login_getpwclass(pwd)
+       const struct passwd *pwd;
+{
+       return login_getclass(pwd ? pwd->pw_class : NULL);
+}
+
 char *
 login_getcapstr(lc, cap, def, e)
        login_cap_t *lc;
@@ -432,6 +441,60 @@
        return (0);
 }
 
+static int
+setuserenv(lc)
+       login_cap_t *lc;
+{
+       char *stop = ", \t";
+       int i, count;
+       char *ptr;
+       char **res;
+       char *str = login_getcapstr(lc, "setenv", NULL, NULL);
+                 
+       if(str == NULL || *str == '\0')
+               return 0;
+       
+       /* count the sub-strings */
+       for (i = 1, ptr = str; *ptr; i++) {
+               ptr += strcspn(ptr, stop);
+               if (*ptr)
+                       ptr++;
+               }
+
+       /* allocate ptr array and string */
+       count = i;
+       res = malloc( count * sizeof(char *) + strlen(str) + 1 );
+
+       if(!res)
+               return -1;
+       
+       ptr = (char *)res + count * sizeof(char *);
+       strcpy(ptr, str);
+
+       /* split string */
+       for (i = 0; *ptr && i < count; i++) {
+               res[i] = ptr;
+               ptr += strcspn(ptr, stop);
+               if (*ptr)
+                       *ptr++ = '\0';
+               }
+       
+       res[i] = NULL;
+
+       for (i = 0; i < count && res[i]; i++) {
+               if (*res[i] != '\0') {
+                       if ((ptr = strchr(res[i], '=')))
+                               *ptr++ = '\0';
+                       else 
+                               ptr = "";
+                       setenv(res[i], ptr, 1);
+               }
+       }
+       
+       return 0;
+}
+
+
 int
 setclasscontext(class, flags)
        char *class;
@@ -521,6 +584,9 @@
                        return (-1);
                }
 
+       if (flags & LOGIN_SETENV)
+               setuserenv(lc);
+
        if (flags & LOGIN_SETPATH)
                setuserpath(lc, pwd ? pwd->pw_dir : "");
 
diff -r fe6a01a6c49f -r 4b712488be9e share/man/man5/login.conf.5
--- a/share/man/man5/login.conf.5       Fri Feb 04 02:03:02 2000 +0000
+++ b/share/man/man5/login.conf.5       Fri Feb 04 02:17:14 2000 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: login.conf.5,v 1.3 2000/01/22 09:31:35 mjl Exp $
+.\"    $NetBSD: login.conf.5,v 1.4 2000/02/04 02:17:16 mjl Exp $
 .\"
 .\" Copyright (c) 1995,1996,1997 Berkeley Software Design, Inc.
 .\" All rights reserved.
@@ -164,6 +164,10 @@
 Require home directory to login.
 .\"
 .sp
+.It setenv Ta list Ta "" Ta
+Comma separated list of environment variables and values to be set.
+.\"
+.sp
 .It shell Ta program Ta "" Ta
 Session shell to execute rather than the shell specified in the password file.
 The
diff -r fe6a01a6c49f -r 4b712488be9e usr.bin/login/login.c
--- a/usr.bin/login/login.c     Fri Feb 04 02:03:02 2000 +0000
+++ b/usr.bin/login/login.c     Fri Feb 04 02:17:14 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: login.c,v 1.52 2000/01/22 09:48:52 mjl Exp $       */
+/*     $NetBSD: login.c,v 1.53 2000/02/04 02:17:14 mjl Exp $       */
 
 /*-
  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
@@ -44,7 +44,7 @@
 #if 0
 static char sccsid[] = "@(#)login.c    8.4 (Berkeley) 4/2/94";
 #endif
-__RCSID("$NetBSD: login.c,v 1.52 2000/01/22 09:48:52 mjl Exp $");
+__RCSID("$NetBSD: login.c,v 1.53 2000/02/04 02:17:14 mjl Exp $");
 #endif /* not lint */
 
 /*
@@ -547,6 +547,11 @@
        if (krbtkfile_env)
                dofork();
 #endif
+
+       /* Destroy environment unless user has requested its preservation. */
+       if (!pflag)
+               environ = envinit;
+
 #ifdef LOGIN_CAP
        if (setusercontext(lc, pwd, pwd->pw_uid, 
            LOGIN_SETALL & ~LOGIN_SETPATH) != 0) {
@@ -580,9 +585,6 @@
        }
 #endif
        
-       /* Destroy environment unless user has requested its preservation. */
-       if (!pflag)
-               environ = envinit;
        (void)setenv("HOME", pwd->pw_dir, 1);
        (void)setenv("SHELL", pwd->pw_shell, 1);
        if (term[0] == '\0') {



Home | Main Index | Thread Index | Old Index