From 1f606b882193cf791e6d55c59babfa7d0e13a1b0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Tue, 28 Jan 1997 23:40:10 +0100
Subject: [PATCH] Moved password database related efuns to system-module

Rev: src/modules/spider/configure.in:1.14
Rev: src/modules/spider/spider.c:1.22
Rev: src/modules/system/Makefile.in:1.3
Rev: src/modules/system/configure.in:1.5
Rev: src/modules/system/passwords.c:1.1
Rev: src/modules/system/syslog.c:1.2
Rev: src/modules/system/system.c:1.9
Rev: src/modules/system/system.h:1.1
---
 .gitattributes                  |   2 +
 src/modules/spider/configure.in |   6 +-
 src/modules/spider/spider.c     |  94 ----------------------
 src/modules/system/Makefile.in  |   2 +-
 src/modules/system/configure.in |   3 +-
 src/modules/system/passwords.c  | 134 ++++++++++++++++++++++++++++++++
 src/modules/system/syslog.c     |  10 ++-
 src/modules/system/system.c     |  21 ++++-
 src/modules/system/system.h     |  39 ++++++++++
 9 files changed, 208 insertions(+), 103 deletions(-)
 create mode 100644 src/modules/system/passwords.c
 create mode 100644 src/modules/system/system.h

diff --git a/.gitattributes b/.gitattributes
index 2e3cb1f184..7a8469039a 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -68,8 +68,10 @@ testfont binary
 /src/modules/spider/dumudp.c foreign_ident
 /src/modules/sprintf/sprintf.c foreign_ident
 /src/modules/ssleay/ssleay.c foreign_ident
+/src/modules/system/passwords.c foreign_ident
 /src/modules/system/syslog.c foreign_ident
 /src/modules/system/system.c foreign_ident
+/src/modules/system/system.h foreign_ident
 /src/modules/system/system_machine.h.in foreign_ident
 /src/modules/zlibmod/zlibmod.c foreign_ident
 /src/object.c foreign_ident
diff --git a/src/modules/spider/configure.in b/src/modules/spider/configure.in
index 393d16a17f..de6c15a1ce 100644
--- a/src/modules/spider/configure.in
+++ b/src/modules/spider/configure.in
@@ -8,13 +8,13 @@ AC_CHECK_LIB(socket, socket)
 AC_CHECK_LIB(nsl, gethostbyname)
 
 AC_HAVE_HEADERS(arpa/inet.h sys/socketvar.h \
-	netinet/in.h sys/mman.h sys/utsname.h netdb.h sys/socket.h pwd.h\
-        shadow.h sync.h sys/sockio.h sys/conf.h stropts.h\
+	netinet/in.h sys/mman.h sys/utsname.h netdb.h sys/socket.h \
+        sync.h sys/sockio.h sys/conf.h stropts.h \
 	sys/uio.h linux/mman.h)
 
 AC_FUNC_MMAP
 
-AC_HAVE_FUNCS(perror strdup getpwnam getspnam sendmsg getpwent setpwent endpwent)
+AC_HAVE_FUNCS(perror strdup sendmsg)
 
 AC_CHECK_TYPE(time_t, long)
 
diff --git a/src/modules/spider/spider.c b/src/modules/spider/spider.c
index 8f1fbde6b5..9ce2366d2d 100644
--- a/src/modules/spider/spider.c
+++ b/src/modules/spider/spider.c
@@ -1140,100 +1140,6 @@ void f_mark_fd(INT32 args)
   push_int(0);
 }
 
-
-
-#ifdef HAVE_PASSWD_H
-# include <passwd.h>
-#endif
-
-#ifdef HAVE_PWD_H
-# include <pwd.h>
-#endif
-
-#ifdef HAVE_SHADOW_H
-# include <shadow.h>
-#endif
-
-#if defined(HAVE_GETPWNAM) || defined(HAVE_GETPWUID) || defined(HAVE_SETPWENT) || defined(HAVE_ENDPWENT) || defined(HAVE_GETPWENT)
-static void push_pwent(struct passwd *ent)
-{
-  if(!ent)
-  {
-    push_int(0);
-    return;
-  }
-  push_text(ent->pw_name);
-
-#ifdef HAVE_GETSPNAM
-  if(!strcmp(ent->pw_passwd, "x"))
-  {
-    struct spwd *foo;
-    if((foo = getspnam(ent->pw_name)))
-      push_text(foo->sp_pwdp);
-    else
-      push_text("x");
-  } else 
-#endif /* Shadow password support */
-    push_text(ent->pw_passwd);
-  push_int(ent->pw_uid);
-  push_int(ent->pw_gid);
-  push_text(ent->pw_gecos);
-  push_text(ent->pw_dir);
-  push_text(ent->pw_shell);
-  f_aggregate(7);
-}
-#endif
-
-#ifdef HAVE_GETPWNAM
-void f_getpwnam(INT32 args)
-{
-  struct passwd *foo;
-  if(args!=1) error("Must pass one string to getpwnam(NAME)\n");
-  if(sp[-1].type != T_STRING)  error("Must pass string to getpwnam(NAME)\n");
-  foo = getpwnam((const char *)sp[-1].u.string->str);
-  pop_stack();
-  push_pwent(foo);
-}
-
-void f_getpwuid(INT32 args)
-{
-  struct passwd *foo;
-  if(args!=1) error("Must pass one integer to getpwuid(NAME)\n");
-  foo = getpwuid(sp[-1].u.integer);
-  pop_stack();
-  push_pwent(foo);
-}
-#endif
-
-#ifdef HAVE_GETPWENT
-void f_setpwent(INT32 args)
-{
-  setpwent();
-  pop_n_elems(args);
-  push_int(0);
-}
-
-void f_endpwent(INT32 args)
-{
-  setpwent();
-  pop_n_elems(args);
-  push_int(0);
-}
-
-void f_getpwent(INT32 args)
-{
-  struct passwd *foo;
-  pop_n_elems(args);
-  foo = getpwent();
-  if(!foo)
-  {
-    push_int(0);
-    return;
-  }
-  push_pwent(foo);
-}
-#endif
-
 #if 0
 void f_fcgi_create_listen_socket(INT32 args)
 {
diff --git a/src/modules/system/Makefile.in b/src/modules/system/Makefile.in
index 59be7cbe04..4bee07b236 100644
--- a/src/modules/system/Makefile.in
+++ b/src/modules/system/Makefile.in
@@ -1,6 +1,6 @@
 SRCDIR=@srcdir@
 VPATH=@srcdir@:@srcdir@/../..:../..
-FILES=system.o syslog.o
+FILES=system.o syslog.o passwords.o
 
 all: system.a
 
diff --git a/src/modules/system/configure.in b/src/modules/system/configure.in
index ef35ec2dc1..bdecd01c8f 100644
--- a/src/modules/system/configure.in
+++ b/src/modules/system/configure.in
@@ -15,10 +15,11 @@ AC_CHECK_LIB(nsl, gethostbyname)
 
 AC_HAVE_HEADERS(syslog.h sys/syslog.h sys/types.h errno.h unistd.h pwd.h \
 	sys/conf.h sys/socket.h netinet/in.h arpa/inet.h netdb.h stdarg.h \
-	sys/utsname.h)
+	sys/utsname.h pwd.h passwd.h shadow.h)
 
 AC_HAVE_FUNCS(syslog link symlink readlink \
 	initgroups seteuid setresuid geteuid getpgrp getpgid getppid \
+	getpwnam getspnam getpwent setpwent endpwent \
 	fchroot uname gethostname gethostbyname)
 
 
diff --git a/src/modules/system/passwords.c b/src/modules/system/passwords.c
new file mode 100644
index 0000000000..f05963a6f8
--- /dev/null
+++ b/src/modules/system/passwords.c
@@ -0,0 +1,134 @@
+/*
+ * $Id: passwords.c,v 1.1 1997/01/28 22:40:06 grubba Exp $
+ *
+ * Password handling for Pike.
+ *
+ * Henrik Grubbström 1997-01-28
+ */
+
+/*
+ * Includes
+ */
+
+#include "system_machine.h"
+#include "system.h"
+
+#include <global.h>
+
+RCSID("$Id: passwords.c,v 1.1 1997/01/28 22:40:06 grubba Exp $");
+
+#include <module_support.h>
+#include <interpret.h>
+#include <stralloc.h>
+#include <threads.h>
+#include <svalue.h>
+
+#ifdef HAVE_PASSWD_H
+# include <passwd.h>
+#endif /* HAVE_PASSWD_H */
+ 
+#ifdef HAVE_PWD_H
+# include <pwd.h>
+#endif /* HAVE_PWD_H */
+ 
+#ifdef HAVE_SHADOW_H
+# include <shadow.h>
+#endif /* HAVE_SHADOW_H */
+ 
+/*
+ * Functions
+ */
+
+#if defined(HAVE_GETPWNAM) || defined(HAVE_GETPWUID) || defined(HAVE_SETPWENT) || defined(HAVE_ENDPWENT) || defined(HAVE_GETPWENT)
+static void push_pwent(struct passwd *ent)
+{
+  if(!ent)
+  {
+    push_int(0);
+    return;
+  }
+  push_text(ent->pw_name);
+ 
+#ifdef HAVE_GETSPNAM
+  if(!strcmp(ent->pw_passwd, "x"))
+  {
+    struct spwd *foo;
+    if((foo = getspnam(ent->pw_name)))
+      push_text(foo->sp_pwdp);
+    else
+      push_text("x");
+  } else 
+#endif /* Shadow password support */
+    push_text(ent->pw_passwd);
+  push_int(ent->pw_uid);
+  push_int(ent->pw_gid);
+  push_text(ent->pw_gecos);
+  push_text(ent->pw_dir);
+  push_text(ent->pw_shell);
+  f_aggregate(7);
+}
+#endif
+ 
+#ifdef HAVE_GETPWNAM
+/* array getpwnam(string str) */
+void f_getpwnam(INT32 args)
+{
+  char *str;
+  struct passwd *foo;
+
+  get_all_args("getpwnam", args, "%s", &str);
+
+  foo = getpwnam(str);
+
+  pop_n_elems(args);
+  push_pwent(foo);
+}
+
+/* array getpwuid(int uid) */
+void f_getpwuid(INT32 args)
+{
+  int uid;
+  struct passwd *foo;
+  
+  get_all_args("getpwuid", args, "%i", &uid);
+
+  foo = getpwuid(uid);
+
+  pop_n_elems(args);
+  push_pwent(foo);
+}
+#endif
+ 
+#ifdef HAVE_GETPWENT
+/* int setpwent() */
+void f_setpwent(INT32 args)
+{
+  setpwent();
+  pop_n_elems(args);
+  push_int(0);
+}
+ 
+/* int endpwent() */
+void f_endpwent(INT32 args)
+{
+  endpwent();
+  pop_n_elems(args);
+  push_int(0);
+}
+
+/* int|array getpwent() */ 
+void f_getpwent(INT32 args)
+{
+  struct passwd *foo;
+  pop_n_elems(args);
+  foo = getpwent();
+  if(!foo)
+  {
+    push_int(0);
+    return;
+  }
+  push_pwent(foo);
+}
+
+#endif
+ 
diff --git a/src/modules/system/syslog.c b/src/modules/system/syslog.c
index 5e6f41545a..5b3c57fd64 100644
--- a/src/modules/system/syslog.c
+++ b/src/modules/system/syslog.c
@@ -1,5 +1,5 @@
 /*
- * $Id: syslog.c,v 1.1 1997/01/28 18:34:52 grubba Exp $
+ * $Id: syslog.c,v 1.2 1997/01/28 22:40:07 grubba Exp $
  *
  * Access to syslog from Pike.
  *
@@ -11,12 +11,18 @@
  */
 
 #include "system_machine.h"
+#include "system.h"
 
 #include <global.h>
 
 #ifdef HAVE_SYSLOG
 
-RCSID("$Id: syslog.c,v 1.1 1997/01/28 18:34:52 grubba Exp $");
+RCSID("$Id: syslog.c,v 1.2 1997/01/28 22:40:07 grubba Exp $");
+
+#include <interpret.h>
+#include <svalue.h>
+#include <stralloc.h>
+#include <threads.h>
 
 #ifdef HAVE_SYSLOG_H
 #include <syslog.h>
diff --git a/src/modules/system/system.c b/src/modules/system/system.c
index f6d9cbf435..241173eeac 100644
--- a/src/modules/system/system.c
+++ b/src/modules/system/system.c
@@ -1,5 +1,5 @@
 /*
- * $Id: system.c,v 1.8 1997/01/28 18:34:54 grubba Exp $
+ * $Id: system.c,v 1.9 1997/01/28 22:40:08 grubba Exp $
  *
  * System-call module for Pike
  *
@@ -11,9 +11,10 @@
  */
 
 #include "system_machine.h"
+#include "system.h"
 
 #include <global.h>
-RCSID("$Id: system.c,v 1.8 1997/01/28 18:34:54 grubba Exp $");
+RCSID("$Id: system.c,v 1.9 1997/01/28 22:40:08 grubba Exp $");
 #include <module_support.h>
 #include <las.h>
 #include <interpret.h>
@@ -757,6 +758,22 @@ void init_system_efuns(void)
   add_efun("syslog", f_syslog, "function(int,string:void)", 0);
   add_efun("closelog", f_closelog, "function(:void)", 0);
 #endif /* HAVE_SYSLOG */
+
+  /*
+   * From passwords.c
+   */
+#ifdef HAVE_GETPWNAM
+  add_efun("getpwnam", f_getpwnam, "function(string:array)", 
+	   OPT_EXTERNAL_DEPEND);
+  add_efun("getpwuid", f_getpwuid, "function(int:array)", OPT_EXTERNAL_DEPEND);
+#endif
+#ifdef HAVE_SETPWENT
+  add_efun("getpwent", f_getpwent, "function(void:int|array)",
+           OPT_EXTERNAL_DEPEND);
+  add_efun("setpwent", f_setpwent, "function(void:int)", OPT_EXTERNAL_DEPEND);
+  add_efun("endpwent", f_endpwent, "function(void:int)", OPT_EXTERNAL_DEPEND);
+#endif
+
 }
 
 void init_system_programs(void)
diff --git a/src/modules/system/system.h b/src/modules/system/system.h
new file mode 100644
index 0000000000..1732c7bb54
--- /dev/null
+++ b/src/modules/system/system.h
@@ -0,0 +1,39 @@
+/*
+ * $Id: system.h,v 1.1 1997/01/28 22:40:10 grubba Exp $
+ *
+ * Prototypes for the Pike system-module
+ *
+ * Henrik Grubbström 1997-01-28
+ */
+
+#ifndef PIKE_MODULES_SYSTEM_H
+#define PIKE_MODULES_SYSTEM_H
+
+/*
+ * Includes
+ */
+
+#include <global.h>
+#include <pike_types.h>
+
+/*
+ * Prototypes
+ */
+
+/*
+ * passwords.c
+ */
+void f_getpwnam(INT32 args);
+void f_getpwuid(INT32 args);
+void f_setpwent(INT32 args);
+void f_endpwent(INT32 args);
+void f_getpwent(INT32 args);
+
+/*
+ * syslog.c
+ */
+void f_openlog(INT32 args);
+void f_syslog(INT32 args);
+void f_closelog(INT32 args);
+
+#endif /* PIKE_MODULES_SYSTEM_H */
-- 
GitLab